blob: 1b95e22383029a90b78714aaea17e0f7f67fcd9d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Brian Haleyab32ea52006-09-22 14:15:41 -070079int sysctl_tcp_timestamps __read_mostly = 1;
80int sysctl_tcp_window_scaling __read_mostly = 1;
81int sysctl_tcp_sack __read_mostly = 1;
82int sysctl_tcp_fack __read_mostly = 1;
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Andreas Petlund7e380172010-02-18 04:48:19 +000098int sysctl_tcp_thin_dupack __read_mostly;
99
Brian Haleyab32ea52006-09-22 14:15:41 -0700100int sysctl_tcp_moderate_rcvbuf __read_mostly = 1;
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000101int sysctl_tcp_early_retrans __read_mostly = 3;
Neal Cardwell032ee422015-02-06 16:04:38 -0500102int sysctl_tcp_invalid_ratelimit __read_mostly = HZ/2;
JP Abgrall7220b972014-02-07 18:40:10 -0800103int sysctl_tcp_default_init_rwnd __read_mostly = TCP_INIT_CWND * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#define FLAG_DATA 0x01 /* Incoming frame contained data. */
106#define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */
107#define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */
108#define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */
109#define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */
110#define FLAG_DATA_SACKED 0x20 /* New SACK. */
111#define FLAG_ECE 0x40 /* ECE in this ACK */
Yuchung Cheng291a00d2015-07-01 14:11:14 -0700112#define FLAG_LOST_RETRANS 0x80 /* This ACK marks some retransmission lost */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/
Yuchung Chenge33099f2013-03-20 13:33:00 +0000114#define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */
Ilpo Järvinen2e605292007-08-02 19:46:58 -0700115#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
Ryousei Takano564262c12007-10-25 23:03:52 -0700116#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
Ilpo Järvinencadbd032007-12-31 04:49:21 -0800117#define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
Eric Dumazet12fb3dd2013-04-19 07:19:48 +0000118#define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */
Eric Dumazet71c0b332017-05-23 15:24:46 -0700119#define FLAG_NO_CHALLENGE_ACK 0x8000 /* do not call tcp_send_challenge_ack() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121#define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
122#define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
123#define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE)
124#define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700127#define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Yuchung Chenge662ca42016-02-02 10:33:04 -0800129#define REXMIT_NONE 0 /* no loss recovery to do */
130#define REXMIT_LOST 1 /* retransmit packets marked lost */
131#define REXMIT_NEW 2 /* FRTO-style transmit of unsent/new packets */
132
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200133static void tcp_gro_dev_warn(struct sock *sk, const struct sk_buff *skb)
134{
135 static bool __once __read_mostly;
136
137 if (!__once) {
138 struct net_device *dev;
139
140 __once = true;
141
142 rcu_read_lock();
143 dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif);
144 pr_warn("%s: Driver has suspect GRO implementation, TCP performance may be compromised.\n",
145 dev ? dev->name : "Unknown driver");
146 rcu_read_unlock();
147 }
148}
149
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900150/* Adapt the MSS value used to make delayed ack decision to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * real world.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900152 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800153static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700155 struct inet_connection_sock *icsk = inet_csk(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900156 const unsigned int lss = icsk->icsk_ack.last_seg_size;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700157 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900159 icsk->icsk_ack.last_seg_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 /* skb->len may jitter because of SACKs, even if peer
162 * sends good full-sized frames.
163 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800164 len = skb_shinfo(skb)->gso_size ? : skb->len;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700165 if (len >= icsk->icsk_ack.rcv_mss) {
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200166 icsk->icsk_ack.rcv_mss = min_t(unsigned int, len,
167 tcp_sk(sk)->advmss);
168 if (unlikely(icsk->icsk_ack.rcv_mss != len))
169 tcp_gro_dev_warn(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 } else {
171 /* Otherwise, we make more careful check taking into account,
172 * that SACKs block is variable.
173 *
174 * "len" is invariant segment length, including TCP header.
175 */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700176 len += skb->data - skb_transport_header(skb);
William Allen Simpsonbee7ca92009-11-10 09:51:18 +0000177 if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* If PSH is not set, packet should be
179 * full sized, provided peer TCP is not badly broken.
180 * This observation (if it is correct 8)) allows
181 * to handle super-low mtu links fairly.
182 */
183 (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700184 !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* Subtract also invariant (if peer is RFC compliant),
186 * tcp header plus fixed timestamp option length.
187 * Resulting "len" is MSS free of SACK jitter.
188 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700189 len -= tcp_sk(sk)->tcp_header_len;
190 icsk->icsk_ack.last_seg_size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (len == lss) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700192 icsk->icsk_ack.rcv_mss = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return;
194 }
195 }
Alexey Kuznetsov1ef96962006-09-19 12:52:50 -0700196 if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
197 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700198 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200}
201
Eric Dumazet90cf17d2018-05-21 15:08:56 -0700202static void tcp_incr_quickack(struct sock *sk, unsigned int max_quickacks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700204 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000205 unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800207 if (quickacks == 0)
208 quickacks = 2;
Eric Dumazet90cf17d2018-05-21 15:08:56 -0700209 quickacks = min(quickacks, max_quickacks);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700210 if (quickacks > icsk->icsk_ack.quick)
Eric Dumazet90cf17d2018-05-21 15:08:56 -0700211 icsk->icsk_ack.quick = quickacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Eric Dumazet90cf17d2018-05-21 15:08:56 -0700214void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks)
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);
Eric Dumazet90cf17d2018-05-21 15:08:56 -0700217
218 tcp_incr_quickack(sk, max_quickacks);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700219 icsk->icsk_ack.pingpong = 0;
220 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
Yuchung Cheng87367112018-07-18 13:56:36 -0700222EXPORT_SYMBOL(tcp_enter_quickack_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224/* Send ACKs quickly, if "quick" count is not exhausted
225 * and the session is not interactive.
226 */
227
Jon Maxwell2251ae42015-07-08 10:12:28 +1000228static bool tcp_in_quickack_mode(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700230 const struct inet_connection_sock *icsk = inet_csk(sk);
Jon Maxwell2251ae42015-07-08 10:12:28 +1000231 const struct dst_entry *dst = __sk_dst_get(sk);
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000232
Jon Maxwell2251ae42015-07-08 10:12:28 +1000233 return (dst && dst_metric(dst, RTAX_QUICKACK)) ||
234 (icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Florian Westphal735d3832014-09-29 13:08:30 +0200237static void tcp_ecn_queue_cwr(struct tcp_sock *tp)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700238{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800239 if (tp->ecn_flags & TCP_ECN_OK)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700240 tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
241}
242
Florian Westphal735d3832014-09-29 13:08:30 +0200243static void tcp_ecn_accept_cwr(struct tcp_sock *tp, const struct sk_buff *skb)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700244{
245 if (tcp_hdr(skb)->cwr)
246 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
247}
248
Florian Westphal735d3832014-09-29 13:08:30 +0200249static void tcp_ecn_withdraw_cwr(struct tcp_sock *tp)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700250{
251 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
252}
253
Yousuk Seung095ab5f42018-06-04 15:29:51 -0700254static void __tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700255{
Yousuk Seung095ab5f42018-06-04 15:29:51 -0700256 struct tcp_sock *tp = tcp_sk(sk);
257
Eric Dumazetb82d1bb2011-09-27 02:20:08 -0400258 switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000259 case INET_ECN_NOT_ECT:
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700260 /* Funny extension: if ECT is not set on a segment,
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000261 * and we already seen ECT on a previous segment,
262 * it is probably a retransmit.
263 */
264 if (tp->ecn_flags & TCP_ECN_SEEN)
Eric Dumazet019ea512018-06-27 08:47:21 -0700265 tcp_enter_quickack_mode(sk, 2);
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000266 break;
267 case INET_ECN_CE:
Yousuk Seung095ab5f42018-06-04 15:29:51 -0700268 if (tcp_ca_needs_ecn(sk))
269 tcp_ca_event(sk, CA_EVENT_ECN_IS_CE);
Florian Westphal98900922014-09-26 22:37:35 +0200270
Eric Dumazetaae06bf2012-08-06 11:04:43 +0000271 if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) {
272 /* Better not delay acks, sender can have a very low cwnd */
Eric Dumazet019ea512018-06-27 08:47:21 -0700273 tcp_enter_quickack_mode(sk, 2);
Eric Dumazetaae06bf2012-08-06 11:04:43 +0000274 tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
275 }
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000276 tp->ecn_flags |= TCP_ECN_SEEN;
Florian Westphal98900922014-09-26 22:37:35 +0200277 break;
278 default:
Yousuk Seung095ab5f42018-06-04 15:29:51 -0700279 if (tcp_ca_needs_ecn(sk))
280 tcp_ca_event(sk, CA_EVENT_ECN_NO_CE);
Florian Westphal98900922014-09-26 22:37:35 +0200281 tp->ecn_flags |= TCP_ECN_SEEN;
282 break;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700283 }
284}
285
Yousuk Seung095ab5f42018-06-04 15:29:51 -0700286static void tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb)
Florian Westphal735d3832014-09-29 13:08:30 +0200287{
Yousuk Seung095ab5f42018-06-04 15:29:51 -0700288 if (tcp_sk(sk)->ecn_flags & TCP_ECN_OK)
289 __tcp_ecn_check_ce(sk, skb);
Florian Westphal735d3832014-09-29 13:08:30 +0200290}
291
292static void tcp_ecn_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700293{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800294 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700295 tp->ecn_flags &= ~TCP_ECN_OK;
296}
297
Florian Westphal735d3832014-09-29 13:08:30 +0200298static void tcp_ecn_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700299{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800300 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700301 tp->ecn_flags &= ~TCP_ECN_OK;
302}
303
Florian Westphal735d3832014-09-29 13:08:30 +0200304static bool tcp_ecn_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700305{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800306 if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000307 return true;
308 return false;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/* Buffer size and advertised window tuning.
312 *
313 * 1. Tuning sk->sk_sndbuf, when connection enters established state.
314 */
315
Eric Dumazet6ae70532013-10-01 10:23:44 -0700316static void tcp_sndbuf_expand(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Eric Dumazet6ae70532013-10-01 10:23:44 -0700318 const struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng77bfc172016-09-19 23:39:20 -0400319 const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
Eric Dumazet6ae70532013-10-01 10:23:44 -0700320 int sndmem, per_mss;
321 u32 nr_segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Eric Dumazet6ae70532013-10-01 10:23:44 -0700323 /* Worst case is non GSO/TSO : each frame consumes one skb
324 * and skb->head is kmalloced using power of two area of memory
325 */
326 per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) +
327 MAX_TCP_HEADER +
328 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
329
330 per_mss = roundup_pow_of_two(per_mss) +
331 SKB_DATA_ALIGN(sizeof(struct sk_buff));
332
333 nr_segs = max_t(u32, TCP_INIT_CWND, tp->snd_cwnd);
334 nr_segs = max_t(u32, nr_segs, tp->reordering + 1);
335
336 /* Fast Recovery (RFC 5681 3.2) :
337 * Cubic needs 1.7 factor, rounded to 2 to include
338 * extra cushion (application might react slowly to POLLOUT)
339 */
Yuchung Cheng77bfc172016-09-19 23:39:20 -0400340 sndmem = ca_ops->sndbuf_expand ? ca_ops->sndbuf_expand(sk) : 2;
341 sndmem *= nr_segs * per_mss;
Eric Dumazet6ae70532013-10-01 10:23:44 -0700342
Eric Dumazet06a59ec2011-10-13 18:24:42 +0000343 if (sk->sk_sndbuf < sndmem)
344 sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
348 *
349 * All tcp_full_space() is split to two parts: "network" buffer, allocated
350 * forward and advertised in receiver window (tp->rcv_wnd) and
351 * "application buffer", required to isolate scheduling/application
352 * latencies from network.
353 * window_clamp is maximal advertised window. It can be less than
354 * tcp_full_space(), in this case tcp_full_space() - window_clamp
355 * is reserved for "application" buffer. The less window_clamp is
356 * the smoother our behaviour from viewpoint of network, but the lower
357 * throughput and the higher sensitivity of the connection to losses. 8)
358 *
359 * rcv_ssthresh is more strict window_clamp used at "slow start"
360 * phase to predict further behaviour of this connection.
361 * It is used for two goals:
362 * - to enforce header prediction at sender, even when application
363 * requires some significant "application buffer". It is check #1.
364 * - to prevent pruning of receive queue because of misprediction
365 * of receiver window. Check #2.
366 *
367 * The scheme does not work when sender sends good segments opening
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800368 * window and then starts to feed us spaghetti. But it should work
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * in common situations. Otherwise, we have to rely on queue collapsing.
370 */
371
372/* Slow part of check#2. */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700373static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700375 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* Optimize this! */
Eric Dumazetdfd4f0a2007-12-21 06:07:53 -0800377 int truesize = tcp_win_from_space(skb->truesize) >> 1;
378 int window = tcp_win_from_space(sysctl_tcp_rmem[2]) >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 while (tp->rcv_ssthresh <= window) {
381 if (truesize <= skb->len)
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700382 return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 truesize >>= 1;
385 window >>= 1;
386 }
387 return 0;
388}
389
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400390static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700392 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet80eae5e2019-04-16 10:55:20 -0700393 int room;
394
395 room = min_t(int, tp->window_clamp, tcp_space(sk)) - tp->rcv_ssthresh;
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Check #1 */
Eric Dumazet80eae5e2019-04-16 10:55:20 -0700398 if (room > 0 && !tcp_under_memory_pressure(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int incr;
400
401 /* Check #2. Increase window, if skb with such overhead
402 * will fit to rcvbuf in future.
403 */
404 if (tcp_win_from_space(skb->truesize) <= skb->len)
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800405 incr = 2 * tp->advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 else
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700407 incr = __tcp_grow_window(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (incr) {
Eric Dumazet4d846f02012-04-16 23:28:07 +0000410 incr = max_t(int, incr, 2 * skb->len);
Eric Dumazet80eae5e2019-04-16 10:55:20 -0700411 tp->rcv_ssthresh += min(room, incr);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700412 inet_csk(sk)->icsk_ack.quick |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 }
415}
416
417/* 3. Tuning rcvbuf, when connection enters established state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static void tcp_fixup_rcvbuf(struct sock *sk)
419{
Eric Dumazete9266a02011-10-20 16:53:56 -0400420 u32 mss = tcp_sk(sk)->advmss;
Eric Dumazete9266a02011-10-20 16:53:56 -0400421 int rcvmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Yuchung Cheng85f16522013-06-11 15:35:32 -0700423 rcvmem = 2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) *
424 tcp_default_init_rwnd(mss);
Eric Dumazete9266a02011-10-20 16:53:56 -0400425
Eric Dumazetb0983d32013-09-20 13:56:58 -0700426 /* Dynamic Right Sizing (DRS) has 2 to 3 RTT latency
427 * Allow enough cushion so that sender is not limited by our window
428 */
429 if (sysctl_tcp_moderate_rcvbuf)
430 rcvmem <<= 2;
431
Eric Dumazete9266a02011-10-20 16:53:56 -0400432 if (sk->sk_rcvbuf < rcvmem)
433 sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800436/* 4. Try to fixup all. It is made immediately after connection enters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * established state.
438 */
Jerry Chu10467162012-08-31 12:29:11 +0000439void tcp_init_buffer_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct tcp_sock *tp = tcp_sk(sk);
442 int maxwin;
443
444 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
445 tcp_fixup_rcvbuf(sk);
446 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
Eric Dumazet6ae70532013-10-01 10:23:44 -0700447 tcp_sndbuf_expand(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 tp->rcvq_space.space = tp->rcv_wnd;
Eric Dumazetb0983d32013-09-20 13:56:58 -0700450 tp->rcvq_space.time = tcp_time_stamp;
451 tp->rcvq_space.seq = tp->copied_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 maxwin = tcp_full_space(sk);
454
455 if (tp->window_clamp >= maxwin) {
456 tp->window_clamp = maxwin;
457
458 if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
459 tp->window_clamp = max(maxwin -
460 (maxwin >> sysctl_tcp_app_win),
461 4 * tp->advmss);
462 }
463
464 /* Force reservation of one segment. */
465 if (sysctl_tcp_app_win &&
466 tp->window_clamp > 2 * tp->advmss &&
467 tp->window_clamp + tp->advmss > maxwin)
468 tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
469
470 tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
471 tp->snd_cwnd_stamp = tcp_time_stamp;
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/* 5. Recalculate window clamp after socket hit its memory bounds. */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700475static void tcp_clamp_window(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700477 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300478 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300480 icsk->icsk_ack.quick = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
John Heffner326f36e2005-11-10 17:11:48 -0800482 if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
483 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
Eric Dumazetb8da51e2015-05-15 12:39:27 -0700484 !tcp_under_memory_pressure(sk) &&
Glauber Costa180d8cd2011-12-11 21:47:02 +0000485 sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {
John Heffner326f36e2005-11-10 17:11:48 -0800486 sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
487 sysctl_tcp_rmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
John Heffner326f36e2005-11-10 17:11:48 -0800489 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800490 tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800493/* Initialize RCV_MSS value.
494 * RCV_MSS is an our guess about MSS used by the peer.
495 * We haven't any direct information about the MSS.
496 * It's better to underestimate the RCV_MSS rather than overestimate.
497 * Overestimations make us ACKing less frequently than needed.
498 * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
499 */
500void tcp_initialize_rcv_mss(struct sock *sk)
501{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400502 const struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800503 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
504
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800505 hint = min(hint, tp->rcv_wnd / 2);
William Allen Simpsonbee7ca92009-11-10 09:51:18 +0000506 hint = min(hint, TCP_MSS_DEFAULT);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800507 hint = max(hint, TCP_MIN_MSS);
508
509 inet_csk(sk)->icsk_ack.rcv_mss = hint;
510}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000511EXPORT_SYMBOL(tcp_initialize_rcv_mss);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/* Receiver "autotuning" code.
514 *
515 * The algorithm for RTT estimation w/o timestamps is based on
516 * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200517 * <http://public.lanl.gov/radiant/pubs.html#DRS>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 *
519 * More detail on this code can be found at
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200520 * <http://staff.psc.edu/jheffner/>,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * though this reference is out of date. A new paper
522 * is pending.
523 */
524static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
525{
526 u32 new_sample = tp->rcv_rtt_est.rtt;
527 long m = sample;
528
529 if (m == 0)
530 m = 1;
531
532 if (new_sample != 0) {
533 /* If we sample in larger samples in the non-timestamp
534 * case, we could grossly overestimate the RTT especially
535 * with chatty applications or bulk transfer apps which
536 * are stalled on filesystem I/O.
537 *
538 * Also, since we are only going for a minimum in the
Stephen Hemminger31f34262005-11-15 15:17:10 -0800539 * non-timestamp case, we do not smooth things out
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800540 * else with timestamps disabled convergence takes too
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 * long.
542 */
543 if (!win_dep) {
544 m -= (new_sample >> 3);
545 new_sample += m;
Neal Cardwell18a223e2012-04-10 07:59:20 +0000546 } else {
547 m <<= 3;
548 if (m < new_sample)
549 new_sample = m;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 } else {
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800552 /* No previous measure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 new_sample = m << 3;
554 }
555
556 if (tp->rcv_rtt_est.rtt != new_sample)
557 tp->rcv_rtt_est.rtt = new_sample;
558}
559
560static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
561{
562 if (tp->rcv_rtt_est.time == 0)
563 goto new_measure;
564 if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
565 return;
Neal Cardwell651913c2012-04-27 11:29:37 -0400566 tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rcv_rtt_est.time, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568new_measure:
569 tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
570 tp->rcv_rtt_est.time = tcp_time_stamp;
571}
572
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800573static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
574 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700576 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (tp->rx_opt.rcv_tsecr &&
578 (TCP_SKB_CB(skb)->end_seq -
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700579 TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0);
581}
582
583/*
584 * This function should be called every time data is copied to user space.
585 * It calculates the appropriate TCP receive buffer space.
586 */
587void tcp_rcv_space_adjust(struct sock *sk)
588{
589 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet7966e762017-12-10 17:55:03 -0800590 u32 copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 int time;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 time = tcp_time_stamp - tp->rcvq_space.time;
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800594 if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900596
Eric Dumazetb0983d32013-09-20 13:56:58 -0700597 /* Number of bytes copied to user in last RTT */
598 copied = tp->copied_seq - tp->rcvq_space.seq;
599 if (copied <= tp->rcvq_space.space)
600 goto new_measure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Eric Dumazetb0983d32013-09-20 13:56:58 -0700602 /* A bit of theory :
603 * copied = bytes received in previous RTT, our base window
604 * To cope with packet losses, we need a 2x factor
605 * To cope with slow start, and sender growing its cwin by 100 %
606 * every RTT, we need a 4x factor, because the ACK we are sending
607 * now is for the next RTT, not the current one :
608 * <prev RTT . ><current RTT .. ><next RTT .... >
609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Eric Dumazetb0983d32013-09-20 13:56:58 -0700611 if (sysctl_tcp_moderate_rcvbuf &&
612 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
Eric Dumazet7966e762017-12-10 17:55:03 -0800613 int rcvmem, rcvbuf;
614 u64 rcvwin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Eric Dumazetb0983d32013-09-20 13:56:58 -0700616 /* minimal window to cope with packet losses, assuming
617 * steady state. Add some cushion because of small variations.
618 */
Eric Dumazet7966e762017-12-10 17:55:03 -0800619 rcvwin = ((u64)copied << 1) + 16 * tp->advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Eric Dumazetb0983d32013-09-20 13:56:58 -0700621 /* If rate increased by 25%,
622 * assume slow start, rcvwin = 3 * copied
623 * If rate increased by 50%,
624 * assume sender can use 2x growth, rcvwin = 4 * copied
625 */
626 if (copied >=
627 tp->rcvq_space.space + (tp->rcvq_space.space >> 2)) {
628 if (copied >=
629 tp->rcvq_space.space + (tp->rcvq_space.space >> 1))
630 rcvwin <<= 1;
631 else
632 rcvwin += (rcvwin >> 1);
633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Eric Dumazetb0983d32013-09-20 13:56:58 -0700635 rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
636 while (tcp_win_from_space(rcvmem) < tp->advmss)
637 rcvmem += 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Eric Dumazet7966e762017-12-10 17:55:03 -0800639 do_div(rcvwin, tp->advmss);
640 rcvbuf = min_t(u64, rcvwin * rcvmem, sysctl_tcp_rmem[2]);
Eric Dumazetb0983d32013-09-20 13:56:58 -0700641 if (rcvbuf > sk->sk_rcvbuf) {
642 sk->sk_rcvbuf = rcvbuf;
643
644 /* Make the window clamp follow along. */
Eric Dumazet4e43b6a2017-12-10 17:55:02 -0800645 tp->window_clamp = tcp_win_from_space(rcvbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647 }
Eric Dumazetb0983d32013-09-20 13:56:58 -0700648 tp->rcvq_space.space = copied;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650new_measure:
651 tp->rcvq_space.seq = tp->copied_seq;
652 tp->rcvq_space.time = tcp_time_stamp;
653}
654
655/* There is something which you must keep in mind when you analyze the
656 * behavior of the tp->ato delayed ack timeout interval. When a
657 * connection starts up, we want to ack as quickly as possible. The
658 * problem is that "good" TCP's do slow start at the beginning of data
659 * transmission. The means that until we send the first few ACK's the
660 * sender will sit on his end and only queue most of his data, because
661 * he can only send snd_cwnd unacked packets at any given time. For
662 * each ACK we send, he increments snd_cwnd and transmits more of his
663 * queue. -DaveM
664 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700665static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700667 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700668 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 u32 now;
670
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700671 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700673 tcp_measure_rcv_mss(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 tcp_rcv_rtt_measure(tp);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 now = tcp_time_stamp;
678
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700679 if (!icsk->icsk_ack.ato) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* The _first_ data packet received, initialize
681 * delayed ACK engine.
682 */
Eric Dumazet90cf17d2018-05-21 15:08:56 -0700683 tcp_incr_quickack(sk, TCP_MAX_QUICKACKS);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700684 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 } else {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700686 int m = now - icsk->icsk_ack.lrcvtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800688 if (m <= TCP_ATO_MIN / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* The fastest case is the first. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700690 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
691 } else if (m < icsk->icsk_ack.ato) {
692 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
693 if (icsk->icsk_ack.ato > icsk->icsk_rto)
694 icsk->icsk_ack.ato = icsk->icsk_rto;
695 } else if (m > icsk->icsk_rto) {
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800696 /* Too long gap. Apparently sender failed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * restart window, so that we send ACKs quickly.
698 */
Eric Dumazet90cf17d2018-05-21 15:08:56 -0700699 tcp_incr_quickack(sk, TCP_MAX_QUICKACKS);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800700 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700703 icsk->icsk_ack.lrcvtime = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Yousuk Seung095ab5f42018-06-04 15:29:51 -0700705 tcp_ecn_check_ce(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 if (skb->len >= 128)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700708 tcp_grow_window(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711/* Called to compute a smoothed rtt estimate. The data fed to this
712 * routine either comes from timestamps, or from segments that were
713 * known _not_ to have been retransmitted [see Karn/Partridge
714 * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
715 * piece by Van Jacobson.
716 * NOTE: the next three routines used to be one big routine.
717 * To save cycles in the RFC 1323 implementation it was better to break
718 * it up into three procedures. -- erics
719 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800720static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300722 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet740b0f12014-02-26 14:02:48 -0800723 long m = mrtt_us; /* RTT */
724 u32 srtt = tp->srtt_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* The following amusing code comes from Jacobson's
727 * article in SIGCOMM '88. Note that rtt and mdev
728 * are scaled versions of rtt and mean deviation.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900729 * This is designed to be as fast as possible
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * m stands for "measurement".
731 *
732 * On a 1990 paper the rto value is changed to:
733 * RTO = rtt + 4 * mdev
734 *
735 * Funny. This algorithm seems to be very broken.
736 * These formulae increase RTO, when it should be decreased, increase
Stephen Hemminger31f34262005-11-15 15:17:10 -0800737 * too slowly, when it should be increased quickly, decrease too quickly
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
739 * does not matter how to _calculate_ it. Seems, it was trap
740 * that VJ failed to avoid. 8)
741 */
Eric Dumazet4a5ab4e2014-02-06 15:57:10 -0800742 if (srtt != 0) {
743 m -= (srtt >> 3); /* m is now error in rtt est */
744 srtt += m; /* rtt = 7/8 rtt + 1/8 new */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (m < 0) {
746 m = -m; /* m is now abs(error) */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800747 m -= (tp->mdev_us >> 2); /* similar update on mdev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /* This is similar to one of Eifel findings.
749 * Eifel blocks mdev updates when rtt decreases.
750 * This solution is a bit different: we use finer gain
751 * for mdev in this case (alpha*beta).
752 * Like Eifel it also prevents growth of rto,
753 * but also it limits too fast rto decreases,
754 * happening in pure Eifel.
755 */
756 if (m > 0)
757 m >>= 3;
758 } else {
Eric Dumazet740b0f12014-02-26 14:02:48 -0800759 m -= (tp->mdev_us >> 2); /* similar update on mdev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Eric Dumazet740b0f12014-02-26 14:02:48 -0800761 tp->mdev_us += m; /* mdev = 3/4 mdev + 1/4 new */
762 if (tp->mdev_us > tp->mdev_max_us) {
763 tp->mdev_max_us = tp->mdev_us;
764 if (tp->mdev_max_us > tp->rttvar_us)
765 tp->rttvar_us = tp->mdev_max_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 if (after(tp->snd_una, tp->rtt_seq)) {
Eric Dumazet740b0f12014-02-26 14:02:48 -0800768 if (tp->mdev_max_us < tp->rttvar_us)
769 tp->rttvar_us -= (tp->rttvar_us - tp->mdev_max_us) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 tp->rtt_seq = tp->snd_nxt;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800771 tp->mdev_max_us = tcp_rto_min_us(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 } else {
774 /* no previous measure. */
Eric Dumazet4a5ab4e2014-02-06 15:57:10 -0800775 srtt = m << 3; /* take the measured time to be rtt */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800776 tp->mdev_us = m << 1; /* make sure rto = 3*rtt */
777 tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk));
778 tp->mdev_max_us = tp->rttvar_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 tp->rtt_seq = tp->snd_nxt;
780 }
Eric Dumazet740b0f12014-02-26 14:02:48 -0800781 tp->srtt_us = max(1U, srtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700784/* Set the sk_pacing_rate to allow proper sizing of TSO packets.
785 * Note: TCP stack does not yet implement pacing.
786 * FQ packet scheduler can be used to implement cheap but effective
787 * TCP pacing, to smooth the burst on large writes when packets
788 * in flight is significantly lower than cwnd (or rwin)
789 */
Eric Dumazet43e122b2015-08-21 17:38:02 -0700790int sysctl_tcp_pacing_ss_ratio __read_mostly = 200;
791int sysctl_tcp_pacing_ca_ratio __read_mostly = 120;
792
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700793static void tcp_update_pacing_rate(struct sock *sk)
794{
795 const struct tcp_sock *tp = tcp_sk(sk);
796 u64 rate;
797
798 /* set sk_pacing_rate to 200 % of current rate (mss * cwnd / srtt) */
Eric Dumazet43e122b2015-08-21 17:38:02 -0700799 rate = (u64)tp->mss_cache * ((USEC_PER_SEC / 100) << 3);
800
801 /* current rate is (cwnd * mss) / srtt
802 * In Slow Start [1], set sk_pacing_rate to 200 % the current rate.
803 * In Congestion Avoidance phase, set it to 120 % the current rate.
804 *
805 * [1] : Normal Slow Start condition is (tp->snd_cwnd < tp->snd_ssthresh)
806 * If snd_cwnd >= (tp->snd_ssthresh / 2), we are approaching
807 * end of slow start and should slow down.
808 */
809 if (tp->snd_cwnd < tp->snd_ssthresh / 2)
810 rate *= sysctl_tcp_pacing_ss_ratio;
811 else
812 rate *= sysctl_tcp_pacing_ca_ratio;
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700813
814 rate *= max(tp->snd_cwnd, tp->packets_out);
815
Eric Dumazet740b0f12014-02-26 14:02:48 -0800816 if (likely(tp->srtt_us))
817 do_div(rate, tp->srtt_us);
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700818
Eric Dumazetba537422013-10-09 17:14:52 -0700819 /* ACCESS_ONCE() is needed because sch_fq fetches sk_pacing_rate
820 * without any lock. We want to make sure compiler wont store
821 * intermediate values in this location.
822 */
823 ACCESS_ONCE(sk->sk_pacing_rate) = min_t(u64, rate,
824 sk->sk_max_pacing_rate);
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700825}
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827/* Calculate rto without backoff. This is the second half of Van Jacobson's
828 * routine referred to above.
829 */
stephen hemmingerf7e56a72013-12-29 11:39:51 -0800830static void tcp_set_rto(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700832 const struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* Old crap is replaced with new one. 8)
834 *
835 * More seriously:
836 * 1. If rtt variance happened to be less 50msec, it is hallucination.
837 * It cannot be less due to utterly erratic ACK generation made
838 * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
839 * to do with delayed acks, because at cwnd>2 true delack timeout
840 * is invisible. Actually, Linux-2.4 also generates erratic
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800841 * ACKs in some circumstances.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000843 inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* 2. Fixups made earlier cannot be right.
846 * If we do not estimate RTO correctly without them,
847 * all the algo is pure shit and should be replaced
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800848 * with correct one. It is exactly, which we pretend to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Ilpo Järvinenee6aac52008-12-05 22:43:08 -0800851 /* NOTE: clamping at TCP_RTO_MIN is not required, current algo
852 * guarantees that rto is higher.
853 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000854 tcp_bound_rto(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400857__u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
860
Gerrit Renker22b71c82010-08-29 19:23:12 +0000861 if (!cwnd)
David S. Miller442b9632011-02-02 17:05:11 -0800862 cwnd = TCP_INIT_CWND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
864}
865
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300866/*
867 * Packet counting of FACK is based on in-order assumptions, therefore TCP
868 * disables it when reordering is detected
869 */
David S. Miller4aabd8e2012-07-09 16:07:30 -0700870void tcp_disable_fack(struct tcp_sock *tp)
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300871{
Ilpo Järvinen85cc3912007-11-15 19:39:31 -0800872 /* RFC3517 uses different metric in lost marker => reset on change */
873 if (tcp_is_fack(tp))
874 tp->lost_skb_hint = NULL;
Vijay Subramanianab562222011-12-20 13:23:24 +0000875 tp->rx_opt.sack_ok &= ~TCP_FACK_ENABLED;
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300876}
877
Ryousei Takano564262c12007-10-25 23:03:52 -0700878/* Take a notice that peer is sending D-SACKs */
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300879static void tcp_dsack_seen(struct tcp_sock *tp)
880{
Vijay Subramanianab562222011-12-20 13:23:24 +0000881 tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300882}
883
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300884static void tcp_update_reordering(struct sock *sk, const int metric,
885 const int ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300887 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (metric > tp->reordering) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700889 int mib_idx;
890
Eric Dumazetdca145f2014-10-27 21:45:24 -0700891 tp->reordering = min(sysctl_tcp_max_reordering, metric);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* This exciting event is worth to be remembered. 8) */
894 if (ts)
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700895 mib_idx = LINUX_MIB_TCPTSREORDER;
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300896 else if (tcp_is_reno(tp))
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700897 mib_idx = LINUX_MIB_TCPRENOREORDER;
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300898 else if (tcp_is_fack(tp))
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700899 mib_idx = LINUX_MIB_TCPFACKREORDER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700901 mib_idx = LINUX_MIB_TCPSACKREORDER;
902
Eric Dumazetc10d9312016-04-29 14:16:47 -0700903 NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904#if FASTRETRANS_DEBUG > 1
Joe Perches91df42b2012-05-15 14:11:54 +0000905 pr_debug("Disorder%d %d %u f%u s%u rr%d\n",
906 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
907 tp->reordering,
908 tp->fackets_out,
909 tp->sacked_out,
910 tp->undo_marker ? tp->undo_retrans : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911#endif
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300912 tcp_disable_fack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Yuchung Chengeed530b2012-05-02 13:30:03 +0000914
915 if (metric > 0)
916 tcp_disable_early_retrans(tp);
Yuchung Cheng4f41b1c2015-10-16 21:57:47 -0700917 tp->rack.reord = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700920/* This must be called before lost_out is incremented */
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700921static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
922{
Ian Morris51456b22015-04-03 09:17:26 +0100923 if (!tp->retransmit_skb_hint ||
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700924 before(TCP_SKB_CB(skb)->seq,
925 TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700926 tp->retransmit_skb_hint = skb;
927
928 if (!tp->lost_out ||
929 after(TCP_SKB_CB(skb)->end_seq, tp->retransmit_high))
930 tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
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 Takano564262c12007-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 Takano564262c12007-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 */
1144 struct skb_mstamp first_sackt;
1145 struct skb_mstamp last_sackt;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001146 struct rate_sample *rate;
Eric Dumazet740b0f12014-02-26 14:02:48 -08001147 int flag;
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001148};
1149
Ilpo Järvinend1935942007-10-11 17:34:25 -07001150/* Check if skb is fully within the SACK block. In presence of GSO skbs,
1151 * the incoming SACK may not exactly match but we can find smaller MSS
1152 * aligned portion of it that matches. Therefore we might need to fragment
1153 * which may fail and creates some hassle (caller must handle error case
1154 * returns).
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001155 *
1156 * FIXME: this could be merged to shift decision code
Ilpo Järvinend1935942007-10-11 17:34:25 -07001157 */
Adrian Bunk0f79efd2007-10-26 03:57:36 -07001158static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001159 u32 start_seq, u32 end_seq)
Ilpo Järvinend1935942007-10-11 17:34:25 -07001160{
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001161 int err;
1162 bool in_sack;
Ilpo Järvinend1935942007-10-11 17:34:25 -07001163 unsigned int pkt_len;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001164 unsigned int mss;
Ilpo Järvinend1935942007-10-11 17:34:25 -07001165
1166 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1167 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1168
1169 if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1170 after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001171 mss = tcp_skb_mss(skb);
Ilpo Järvinend1935942007-10-11 17:34:25 -07001172 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1173
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001174 if (!in_sack) {
Ilpo Järvinend1935942007-10-11 17:34:25 -07001175 pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001176 if (pkt_len < mss)
1177 pkt_len = mss;
1178 } else {
Ilpo Järvinend1935942007-10-11 17:34:25 -07001179 pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001180 if (pkt_len < mss)
1181 return -EINVAL;
1182 }
1183
1184 /* Round if necessary so that SACKs cover only full MSSes
1185 * and/or the remaining small portion (if present)
1186 */
1187 if (pkt_len > mss) {
1188 unsigned int new_len = (pkt_len / mss) * mss;
Yuchung Cheng8d625242017-05-10 17:01:27 -07001189 if (!in_sack && new_len < pkt_len)
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001190 new_len += mss;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001191 pkt_len = new_len;
1192 }
Yuchung Cheng8d625242017-05-10 17:01:27 -07001193
1194 if (pkt_len >= skb->len && !in_sack)
1195 return 0;
1196
Octavian Purdila6cc55e02014-06-06 17:32:37 +03001197 err = tcp_fragment(sk, skb, pkt_len, mss, GFP_ATOMIC);
Ilpo Järvinend1935942007-10-11 17:34:25 -07001198 if (err < 0)
1199 return err;
1200 }
1201
1202 return in_sack;
1203}
1204
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001205/* Mark the given newly-SACKed range as such, adjusting counters and hints. */
1206static u8 tcp_sacktag_one(struct sock *sk,
1207 struct tcp_sacktag_state *state, u8 sacked,
1208 u32 start_seq, u32 end_seq,
Eric Dumazet740b0f12014-02-26 14:02:48 -08001209 int dup_sack, int pcount,
1210 const struct skb_mstamp *xmit_time)
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001211{
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001212 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001213 int fack_count = state->fack_count;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001214
1215 /* Account D-SACK for retransmitted packet. */
1216 if (dup_sack && (sacked & TCPCB_RETRANS)) {
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001217 if (tp->undo_marker && tp->undo_retrans > 0 &&
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001218 after(end_seq, tp->undo_marker))
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001219 tp->undo_retrans--;
Ilpo Järvinenede9f3b2007-12-02 00:47:58 +02001220 if (sacked & TCPCB_SACKED_ACKED)
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001221 state->reord = min(fack_count, state->reord);
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001222 }
1223
1224 /* Nothing to do; acked frame is about to be dropped (was ACKed). */
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001225 if (!after(end_seq, tp->snd_una))
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001226 return sacked;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001227
1228 if (!(sacked & TCPCB_SACKED_ACKED)) {
Yuchung Cheng659a8ad2015-10-16 21:57:46 -07001229 tcp_rack_advance(tp, xmit_time, sacked);
1230
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001231 if (sacked & TCPCB_SACKED_RETRANS) {
1232 /* If the segment is not tagged as lost,
1233 * we do not clear RETRANS, believing
1234 * that retransmission is still in flight.
1235 */
1236 if (sacked & TCPCB_LOST) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001237 sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001238 tp->lost_out -= pcount;
1239 tp->retrans_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001240 }
1241 } else {
1242 if (!(sacked & TCPCB_RETRANS)) {
1243 /* New sack for not retransmitted frame,
1244 * which was in hole. It is reordering.
1245 */
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001246 if (before(start_seq,
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001247 tcp_highest_sack_seq(tp)))
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001248 state->reord = min(fack_count,
1249 state->reord);
Yuchung Chenge33099f2013-03-20 13:33:00 +00001250 if (!after(end_seq, tp->high_seq))
1251 state->flag |= FLAG_ORIG_SACK_ACKED;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02001252 if (state->first_sackt.v64 == 0)
1253 state->first_sackt = *xmit_time;
1254 state->last_sackt = *xmit_time;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001255 }
1256
1257 if (sacked & TCPCB_LOST) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001258 sacked &= ~TCPCB_LOST;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001259 tp->lost_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001260 }
1261 }
1262
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001263 sacked |= TCPCB_SACKED_ACKED;
1264 state->flag |= FLAG_DATA_SACKED;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001265 tp->sacked_out += pcount;
Yuchung Chengddf1af62016-02-02 10:33:06 -08001266 tp->delivered += pcount; /* Out-of-order packets delivered */
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001267
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001268 fack_count += pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001269
1270 /* Lost marker hint past SACKed? Tweak RFC3517 cnt */
Ian Morris00db4122015-04-03 09:17:27 +01001271 if (!tcp_is_fack(tp) && tp->lost_skb_hint &&
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001272 before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001273 tp->lost_cnt_hint += pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001274
1275 if (fack_count > tp->fackets_out)
1276 tp->fackets_out = fack_count;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001277 }
1278
1279 /* D-SACK. We can detect redundant retransmission in S|R and plain R
1280 * frames and clear it. undo_retrans is decreased above, L|R frames
1281 * are accounted above as well.
1282 */
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001283 if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
1284 sacked &= ~TCPCB_SACKED_RETRANS;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001285 tp->retrans_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001286 }
1287
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001288 return sacked;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001289}
1290
Neal Cardwelldaef52b2012-02-12 18:37:10 +00001291/* Shift newly-SACKed bytes from this skb to the immediately previous
1292 * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
1293 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001294static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,
1295 struct tcp_sacktag_state *state,
1296 unsigned int pcount, int shifted, int mss,
1297 bool dup_sack)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001298{
1299 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen50133162008-12-05 22:42:41 -08001300 struct sk_buff *prev = tcp_write_queue_prev(sk, skb);
Neal Cardwelldaef52b2012-02-12 18:37:10 +00001301 u32 start_seq = TCP_SKB_CB(skb)->seq; /* start of newly-SACKed */
1302 u32 end_seq = start_seq + shifted; /* end of newly-SACKed */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001303
1304 BUG_ON(!pcount);
1305
Neal Cardwell4c90d3b2012-02-26 10:06:19 +00001306 /* Adjust counters and hints for the newly sacked sequence
1307 * range but discard the return value since prev is already
1308 * marked. We must tag the range first because the seq
1309 * advancement below implicitly advances
1310 * tcp_highest_sack_seq() when skb is highest_sack.
1311 */
1312 tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked,
Yuchung Cheng59c9af42013-07-22 16:20:47 -07001313 start_seq, end_seq, dup_sack, pcount,
Eric Dumazet740b0f12014-02-26 14:02:48 -08001314 &skb->skb_mstamp);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001315 tcp_rate_skb_delivered(sk, skb, state->rate);
Neal Cardwell4c90d3b2012-02-26 10:06:19 +00001316
1317 if (skb == tp->lost_skb_hint)
Neal Cardwell0af2a0d2012-02-13 20:22:08 +00001318 tp->lost_cnt_hint += pcount;
1319
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001320 TCP_SKB_CB(prev)->end_seq += shifted;
1321 TCP_SKB_CB(skb)->seq += shifted;
1322
Eric Dumazetcd7d8492014-09-24 04:11:22 -07001323 tcp_skb_pcount_add(prev, pcount);
Eric Dumazetcc1b58c2019-06-15 17:31:03 -07001324 WARN_ON_ONCE(tcp_skb_pcount(skb) < pcount);
Eric Dumazetcd7d8492014-09-24 04:11:22 -07001325 tcp_skb_pcount_add(skb, -pcount);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001326
1327 /* When we're adding to gso_segs == 1, gso_size will be zero,
1328 * in theory this shouldn't be necessary but as long as DSACK
1329 * code can come after this skb later on it's better to keep
1330 * setting gso_size to something.
1331 */
Eric Dumazetf69ad292015-06-11 09:15:18 -07001332 if (!TCP_SKB_CB(prev)->tcp_gso_size)
1333 TCP_SKB_CB(prev)->tcp_gso_size = mss;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001334
1335 /* CHECKME: To clear or not to clear? Mimics normal skb currently */
Eric Dumazet51466a72015-06-11 09:15:16 -07001336 if (tcp_skb_pcount(skb) <= 1)
Eric Dumazetf69ad292015-06-11 09:15:18 -07001337 TCP_SKB_CB(skb)->tcp_gso_size = 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001338
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001339 /* Difference in this won't matter, both ACKed by the same cumul. ACK */
1340 TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1341
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001342 if (skb->len > 0) {
1343 BUG_ON(!tcp_skb_pcount(skb));
Eric Dumazetc10d9312016-04-29 14:16:47 -07001344 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTED);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001345 return false;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001346 }
1347
1348 /* Whole SKB was eaten :-) */
1349
Ilpo Järvinen92ee76b2008-11-24 21:26:56 -08001350 if (skb == tp->retransmit_skb_hint)
1351 tp->retransmit_skb_hint = prev;
Ilpo Järvinen92ee76b2008-11-24 21:26:56 -08001352 if (skb == tp->lost_skb_hint) {
1353 tp->lost_skb_hint = prev;
1354 tp->lost_cnt_hint -= tcp_skb_pcount(prev);
1355 }
1356
Eric Dumazet5e8a402f2013-10-04 10:31:41 -07001357 TCP_SKB_CB(prev)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
Martin KaFai Laua643b5d2016-04-25 14:44:49 -07001358 TCP_SKB_CB(prev)->eor = TCP_SKB_CB(skb)->eor;
Eric Dumazet5e8a402f2013-10-04 10:31:41 -07001359 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
1360 TCP_SKB_CB(prev)->end_seq++;
1361
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001362 if (skb == tcp_highest_sack(sk))
1363 tcp_advance_highest_sack(sk, skb);
1364
Martin KaFai Laucfea5a62016-04-19 22:39:29 -07001365 tcp_skb_collapse_tstamp(prev, skb);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001366 if (unlikely(TCP_SKB_CB(prev)->tx.delivered_mstamp.v64))
1367 TCP_SKB_CB(prev)->tx.delivered_mstamp.v64 = 0;
1368
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001369 tcp_unlink_write_queue(skb, sk);
1370 sk_wmem_free_skb(sk, skb);
1371
Eric Dumazetc10d9312016-04-29 14:16:47 -07001372 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKMERGED);
Ilpo Järvinen111cc8b2008-11-24 21:27:22 -08001373
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001374 return true;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001375}
1376
1377/* I wish gso_size would have a bit more sane initialization than
1378 * something-or-zero which complicates things
1379 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001380static int tcp_skb_seglen(const struct sk_buff *skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001381{
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001382 return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001383}
1384
1385/* Shifting pages past head area doesn't work */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001386static int skb_can_shift(const struct sk_buff *skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001387{
1388 return !skb_headlen(skb) && skb_is_nonlinear(skb);
1389}
1390
Eric Dumazetcc1b58c2019-06-15 17:31:03 -07001391int tcp_skb_shift(struct sk_buff *to, struct sk_buff *from,
1392 int pcount, int shiftlen)
1393{
1394 /* TCP min gso_size is 8 bytes (TCP_MIN_GSO_SIZE)
1395 * Since TCP_SKB_CB(skb)->tcp_gso_segs is 16 bits, we need
1396 * to make sure not storing more than 65535 * 8 bytes per skb,
1397 * even if current MSS is bigger.
1398 */
1399 if (unlikely(to->len + shiftlen >= 65535 * TCP_MIN_GSO_SIZE))
1400 return 0;
1401 if (unlikely(tcp_skb_pcount(to) + pcount > 65535))
1402 return 0;
1403 return skb_shift(to, from, shiftlen);
1404}
1405
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001406/* Try collapsing SACK blocks spanning across multiple skbs to a single
1407 * skb.
1408 */
1409static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001410 struct tcp_sacktag_state *state,
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001411 u32 start_seq, u32 end_seq,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001412 bool dup_sack)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001413{
1414 struct tcp_sock *tp = tcp_sk(sk);
1415 struct sk_buff *prev;
1416 int mss;
Eric Dumazetcc1b58c2019-06-15 17:31:03 -07001417 int next_pcount;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001418 int pcount = 0;
1419 int len;
1420 int in_sack;
1421
1422 if (!sk_can_gso(sk))
1423 goto fallback;
1424
1425 /* Normally R but no L won't result in plain S */
1426 if (!dup_sack &&
Ilpo Järvinen9969ca52008-12-05 22:41:06 -08001427 (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001428 goto fallback;
1429 if (!skb_can_shift(skb))
1430 goto fallback;
1431 /* This frame is about to be dropped (was ACKed). */
1432 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1433 goto fallback;
1434
1435 /* Can only happen with delayed DSACK + discard craziness */
1436 if (unlikely(skb == tcp_write_queue_head(sk)))
1437 goto fallback;
1438 prev = tcp_write_queue_prev(sk, skb);
1439
1440 if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1441 goto fallback;
1442
Martin KaFai Laua643b5d2016-04-25 14:44:49 -07001443 if (!tcp_skb_can_collapse_to(prev))
1444 goto fallback;
1445
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001446 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1447 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1448
1449 if (in_sack) {
1450 len = skb->len;
1451 pcount = tcp_skb_pcount(skb);
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001452 mss = tcp_skb_seglen(skb);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001453
1454 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1455 * drop this restriction as unnecessary
1456 */
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001457 if (mss != tcp_skb_seglen(prev))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001458 goto fallback;
1459 } else {
1460 if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1461 goto noop;
1462 /* CHECKME: This is non-MSS split case only?, this will
1463 * cause skipped skbs due to advancing loop btw, original
1464 * has that feature too
1465 */
1466 if (tcp_skb_pcount(skb) <= 1)
1467 goto noop;
1468
1469 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1470 if (!in_sack) {
1471 /* TODO: head merge to next could be attempted here
1472 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1473 * though it might not be worth of the additional hassle
1474 *
1475 * ...we can probably just fallback to what was done
1476 * previously. We could try merging non-SACKed ones
1477 * as well but it probably isn't going to buy off
1478 * because later SACKs might again split them, and
1479 * it would make skb timestamp tracking considerably
1480 * harder problem.
1481 */
1482 goto fallback;
1483 }
1484
1485 len = end_seq - TCP_SKB_CB(skb)->seq;
1486 BUG_ON(len < 0);
1487 BUG_ON(len > skb->len);
1488
1489 /* MSS boundaries should be honoured or else pcount will
1490 * severely break even though it makes things bit trickier.
1491 * Optimize common case to avoid most of the divides
1492 */
1493 mss = tcp_skb_mss(skb);
1494
1495 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1496 * drop this restriction as unnecessary
1497 */
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001498 if (mss != tcp_skb_seglen(prev))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001499 goto fallback;
1500
1501 if (len == mss) {
1502 pcount = 1;
1503 } else if (len < mss) {
1504 goto noop;
1505 } else {
1506 pcount = len / mss;
1507 len = pcount * mss;
1508 }
1509 }
1510
Neal Cardwell4648dc92012-03-05 19:35:04 +00001511 /* tcp_sacktag_one() won't SACK-tag ranges below snd_una */
1512 if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
1513 goto fallback;
1514
Eric Dumazetcc1b58c2019-06-15 17:31:03 -07001515 if (!tcp_skb_shift(prev, skb, pcount, len))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001516 goto fallback;
Ilpo Järvinen9ec06ff2009-03-01 00:21:36 -08001517 if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001518 goto out;
1519
1520 /* Hole filled allows collapsing with the next as well, this is very
1521 * useful when hole on every nth skb pattern happens
1522 */
1523 if (prev == tcp_write_queue_tail(sk))
1524 goto out;
1525 skb = tcp_write_queue_next(sk, prev);
1526
Ilpo Järvinenf0bc52f2008-12-05 22:40:47 -08001527 if (!skb_can_shift(skb) ||
1528 (skb == tcp_send_head(sk)) ||
1529 ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001530 (mss != tcp_skb_seglen(skb)))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001531 goto out;
1532
1533 len = skb->len;
Eric Dumazetcc1b58c2019-06-15 17:31:03 -07001534 next_pcount = tcp_skb_pcount(skb);
1535 if (tcp_skb_shift(prev, skb, next_pcount, len)) {
1536 pcount += next_pcount;
1537 tcp_shifted_skb(sk, skb, state, next_pcount, len, mss, 0);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001538 }
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001539out:
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001540 state->fack_count += pcount;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001541 return prev;
1542
1543noop:
1544 return skb;
1545
1546fallback:
Eric Dumazetc10d9312016-04-29 14:16:47 -07001547 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001548 return NULL;
1549}
1550
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001551static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
1552 struct tcp_sack_block *next_dup,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001553 struct tcp_sacktag_state *state,
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001554 u32 start_seq, u32 end_seq,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001555 bool dup_sack_in)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001556{
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001557 struct tcp_sock *tp = tcp_sk(sk);
1558 struct sk_buff *tmp;
1559
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001560 tcp_for_write_queue_from(skb, sk) {
1561 int in_sack = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001562 bool dup_sack = dup_sack_in;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001563
1564 if (skb == tcp_send_head(sk))
1565 break;
1566
1567 /* queue is in-order => we can short-circuit the walk early */
1568 if (!before(TCP_SKB_CB(skb)->seq, end_seq))
1569 break;
1570
Ian Morris00db4122015-04-03 09:17:27 +01001571 if (next_dup &&
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001572 before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
1573 in_sack = tcp_match_skb_to_sack(sk, skb,
1574 next_dup->start_seq,
1575 next_dup->end_seq);
1576 if (in_sack > 0)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001577 dup_sack = true;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001578 }
1579
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001580 /* skb reference here is a bit tricky to get right, since
1581 * shifting can eat and free both this skb and the next,
1582 * so not even _safe variant of the loop is enough.
1583 */
1584 if (in_sack <= 0) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001585 tmp = tcp_shift_skb_data(sk, skb, state,
1586 start_seq, end_seq, dup_sack);
Ian Morris00db4122015-04-03 09:17:27 +01001587 if (tmp) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001588 if (tmp != skb) {
1589 skb = tmp;
1590 continue;
1591 }
1592
1593 in_sack = 0;
1594 } else {
1595 in_sack = tcp_match_skb_to_sack(sk, skb,
1596 start_seq,
1597 end_seq);
1598 }
1599 }
1600
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001601 if (unlikely(in_sack < 0))
1602 break;
1603
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001604 if (in_sack) {
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001605 TCP_SKB_CB(skb)->sacked =
1606 tcp_sacktag_one(sk,
1607 state,
1608 TCP_SKB_CB(skb)->sacked,
1609 TCP_SKB_CB(skb)->seq,
1610 TCP_SKB_CB(skb)->end_seq,
1611 dup_sack,
Yuchung Cheng59c9af42013-07-22 16:20:47 -07001612 tcp_skb_pcount(skb),
Eric Dumazet740b0f12014-02-26 14:02:48 -08001613 &skb->skb_mstamp);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001614 tcp_rate_skb_delivered(sk, skb, state->rate);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001615
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001616 if (!before(TCP_SKB_CB(skb)->seq,
1617 tcp_highest_sack_seq(tp)))
1618 tcp_advance_highest_sack(sk, skb);
1619 }
1620
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001621 state->fack_count += tcp_skb_pcount(skb);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001622 }
1623 return skb;
1624}
1625
1626/* Avoid all extra work that is being done by sacktag while walking in
1627 * a normal way
1628 */
1629static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001630 struct tcp_sacktag_state *state,
1631 u32 skip_to_seq)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001632{
1633 tcp_for_write_queue_from(skb, sk) {
1634 if (skb == tcp_send_head(sk))
1635 break;
1636
Ilpo Järvinene8bae272008-11-24 21:12:28 -08001637 if (after(TCP_SKB_CB(skb)->end_seq, skip_to_seq))
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001638 break;
Ilpo Järvinend152a7d2008-03-03 12:10:16 -08001639
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001640 state->fack_count += tcp_skb_pcount(skb);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001641 }
1642 return skb;
1643}
1644
1645static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
1646 struct sock *sk,
1647 struct tcp_sack_block *next_dup,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001648 struct tcp_sacktag_state *state,
1649 u32 skip_to_seq)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001650{
Ian Morris51456b22015-04-03 09:17:26 +01001651 if (!next_dup)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001652 return skb;
1653
1654 if (before(next_dup->start_seq, skip_to_seq)) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001655 skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
1656 skb = tcp_sacktag_walk(skb, sk, NULL, state,
1657 next_dup->start_seq, next_dup->end_seq,
1658 1);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001659 }
1660
1661 return skb;
1662}
1663
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001664static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001665{
1666 return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1667}
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669static int
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001670tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001671 u32 prior_snd_una, struct tcp_sacktag_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001674 const unsigned char *ptr = (skb_transport_header(ack_skb) +
1675 TCP_SKB_CB(ack_skb)->sacked);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001676 struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
Adam Langley4389dde2008-07-19 00:07:02 -07001677 struct tcp_sack_block sp[TCP_NUM_SACKS];
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001678 struct tcp_sack_block *cache;
1679 struct sk_buff *skb;
Adam Langley4389dde2008-07-19 00:07:02 -07001680 int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001681 int used_sacks;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001682 bool found_dup_sack = false;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001683 int i, j;
Baruch Evenfda03fb2007-02-04 23:35:57 -08001684 int first_sack_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001686 state->flag = 0;
1687 state->reord = tp->packets_out;
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001688
Ilpo Järvinend738cd82007-03-24 21:03:23 -07001689 if (!tp->sacked_out) {
Ilpo Järvinende83c052007-10-07 23:37:55 -07001690 if (WARN_ON(tp->fackets_out))
1691 tp->fackets_out = 0;
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001692 tcp_highest_sack_reset(sk);
Ilpo Järvinend738cd82007-03-24 21:03:23 -07001693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07001695 found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
David S. Millerd06e0212007-06-18 22:43:06 -07001696 num_sacks, prior_snd_una);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001697 if (found_dup_sack) {
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001698 state->flag |= FLAG_DSACKING_ACK;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001699 tp->delivered++; /* A spurious retransmission is delivered */
1700 }
Baruch Even6f746512007-02-04 23:36:42 -08001701
1702 /* Eliminate too old ACKs, but take into
1703 * account more or less fresh ones, they can
1704 * contain valid SACK info.
1705 */
1706 if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
1707 return 0;
1708
Ilpo Järvinen96a2d412007-11-14 15:47:18 -08001709 if (!tp->packets_out)
1710 goto out;
1711
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001712 used_sacks = 0;
1713 first_sack_index = 0;
1714 for (i = 0; i < num_sacks; i++) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001715 bool dup_sack = !i && found_dup_sack;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001716
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001717 sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq);
1718 sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001719
1720 if (!tcp_is_sackblock_valid(tp, dup_sack,
1721 sp[used_sacks].start_seq,
1722 sp[used_sacks].end_seq)) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001723 int mib_idx;
1724
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001725 if (dup_sack) {
1726 if (!tp->undo_marker)
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001727 mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001728 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001729 mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001730 } else {
1731 /* Don't count olds caused by ACK reordering */
1732 if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) &&
1733 !after(sp[used_sacks].end_seq, tp->snd_una))
1734 continue;
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001735 mib_idx = LINUX_MIB_TCPSACKDISCARD;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001736 }
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001737
Eric Dumazetc10d9312016-04-29 14:16:47 -07001738 NET_INC_STATS(sock_net(sk), mib_idx);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001739 if (i == 0)
1740 first_sack_index = -1;
1741 continue;
1742 }
1743
1744 /* Ignore very old stuff early */
1745 if (!after(sp[used_sacks].end_seq, prior_snd_una))
1746 continue;
1747
1748 used_sacks++;
1749 }
1750
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001751 /* order SACK blocks to allow in order walk of the retrans queue */
1752 for (i = used_sacks - 1; i > 0; i--) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001753 for (j = 0; j < i; j++) {
1754 if (after(sp[j].start_seq, sp[j + 1].start_seq)) {
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001755 swap(sp[j], sp[j + 1]);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001756
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001757 /* Track where the first SACK block goes to */
1758 if (j == first_sack_index)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001759 first_sack_index = j + 1;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001760 }
1761 }
1762 }
1763
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001764 skb = tcp_write_queue_head(sk);
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001765 state->fack_count = 0;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001766 i = 0;
1767
1768 if (!tp->sacked_out) {
1769 /* It's already past, so skip checking against it */
1770 cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1771 } else {
1772 cache = tp->recv_sack_cache;
1773 /* Skip empty blocks in at head of the cache */
1774 while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq &&
1775 !cache->end_seq)
1776 cache++;
Baruch Evenfda03fb2007-02-04 23:35:57 -08001777 }
1778
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001779 while (i < used_sacks) {
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001780 u32 start_seq = sp[i].start_seq;
1781 u32 end_seq = sp[i].end_seq;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001782 bool dup_sack = (found_dup_sack && (i == first_sack_index));
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001783 struct tcp_sack_block *next_dup = NULL;
Ilpo Järvinene56d6cd2007-11-01 00:09:37 -07001784
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001785 if (found_dup_sack && ((i + 1) == first_sack_index))
1786 next_dup = &sp[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001788 /* Skip too early cached blocks */
1789 while (tcp_sack_cache_ok(tp, cache) &&
1790 !before(start_seq, cache->end_seq))
1791 cache++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001793 /* Can skip some work by looking recv_sack_cache? */
1794 if (tcp_sack_cache_ok(tp, cache) && !dup_sack &&
1795 after(end_seq, cache->start_seq)) {
David S. Millerfe067e82007-03-07 12:12:44 -08001796
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001797 /* Head todo? */
1798 if (before(start_seq, cache->start_seq)) {
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001799 skb = tcp_sacktag_skip(skb, sk, state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001800 start_seq);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001801 skb = tcp_sacktag_walk(skb, sk, next_dup,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001802 state,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001803 start_seq,
1804 cache->start_seq,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001805 dup_sack);
Baruch Evenfda03fb2007-02-04 23:35:57 -08001806 }
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001807
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001808 /* Rest of the block already fully processed? */
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001809 if (!after(end_seq, cache->end_seq))
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001810 goto advance_sp;
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001811
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001812 skb = tcp_maybe_skipping_dsack(skb, sk, next_dup,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001813 state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001814 cache->end_seq);
Ilpo Järvinene56d6cd2007-11-01 00:09:37 -07001815
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001816 /* ...tail remains todo... */
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001817 if (tcp_highest_sack_seq(tp) == cache->end_seq) {
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001818 /* ...but better entrypoint exists! */
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001819 skb = tcp_highest_sack(sk);
Ian Morris51456b22015-04-03 09:17:26 +01001820 if (!skb)
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001821 break;
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001822 state->fack_count = tp->fackets_out;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001823 cache++;
1824 goto walk;
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001827 skb = tcp_sacktag_skip(skb, sk, state, cache->end_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001828 /* Check overlap against next cached too (past this one already) */
1829 cache++;
1830 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
Ilpo Järvinenfbd52eb2007-11-10 21:24:19 -08001832
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001833 if (!before(start_seq, tcp_highest_sack_seq(tp))) {
1834 skb = tcp_highest_sack(sk);
Ian Morris51456b22015-04-03 09:17:26 +01001835 if (!skb)
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001836 break;
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001837 state->fack_count = tp->fackets_out;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001838 }
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001839 skb = tcp_sacktag_skip(skb, sk, state, start_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001840
1841walk:
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001842 skb = tcp_sacktag_walk(skb, sk, next_dup, state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001843 start_seq, end_seq, dup_sack);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001844
1845advance_sp:
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001846 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001849 /* Clear the head of the cache sack blocks so we can skip it next time */
1850 for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) {
1851 tp->recv_sack_cache[i].start_seq = 0;
1852 tp->recv_sack_cache[i].end_seq = 0;
1853 }
1854 for (j = 0; j < used_sacks; j++)
1855 tp->recv_sack_cache[i++] = sp[j];
1856
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001857 if ((state->reord < tp->fackets_out) &&
Yuchung Cheng9b441902013-03-20 13:32:58 +00001858 ((inet_csk(sk)->icsk_ca_state != TCP_CA_Loss) || tp->undo_marker))
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001859 tcp_update_reordering(sk, tp->fackets_out - state->reord, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
Yuchung Cheng9dac8832015-04-29 11:28:30 -07001861 tcp_verify_left_out(tp);
Ilpo Järvinen96a2d412007-11-14 15:47:18 -08001862out:
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864#if FASTRETRANS_DEBUG > 0
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001865 WARN_ON((int)tp->sacked_out < 0);
1866 WARN_ON((int)tp->lost_out < 0);
1867 WARN_ON((int)tp->retrans_out < 0);
1868 WARN_ON((int)tcp_packets_in_flight(tp) < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869#endif
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001870 return state->flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Ilpo Järvinen882beba2008-04-07 22:33:07 -07001873/* Limits sacked_out so that sum with lost_out isn't ever larger than
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001874 * packets_out. Returns false if sacked_out adjustement wasn't necessary.
Ilpo Järvinen30935cf2007-02-21 23:01:36 -08001875 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001876static bool tcp_limit_reno_sacked(struct tcp_sock *tp)
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001877{
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001878 u32 holes;
1879
1880 holes = max(tp->lost_out, 1U);
1881 holes = min(holes, tp->packets_out);
1882
1883 if ((tp->sacked_out + holes) > tp->packets_out) {
1884 tp->sacked_out = tp->packets_out - holes;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001885 return true;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001886 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001887 return false;
Ilpo Järvinen882beba2008-04-07 22:33:07 -07001888}
1889
1890/* If we receive more dupacks than we expected counting segments
1891 * in assumption of absent reordering, interpret this as reordering.
1892 * The only another reason could be bug in receiver TCP.
1893 */
1894static void tcp_check_reno_reordering(struct sock *sk, const int addend)
1895{
1896 struct tcp_sock *tp = tcp_sk(sk);
1897 if (tcp_limit_reno_sacked(tp))
1898 tcp_update_reordering(sk, tp->packets_out + addend, 0);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001899}
1900
1901/* Emulate SACKs for SACKless connection: account for a new dupack. */
1902
1903static void tcp_add_reno_sack(struct sock *sk)
1904{
1905 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Chengddf1af62016-02-02 10:33:06 -08001906 u32 prior_sacked = tp->sacked_out;
1907
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001908 tp->sacked_out++;
1909 tcp_check_reno_reordering(sk, 0);
Yuchung Chengddf1af62016-02-02 10:33:06 -08001910 if (tp->sacked_out > prior_sacked)
1911 tp->delivered++; /* Some out-of-order packet is delivered */
Ilpo Järvinen005903b2007-08-09 14:44:16 +03001912 tcp_verify_left_out(tp);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001913}
1914
1915/* Account for ACK, ACKing some data in Reno Recovery phase. */
1916
1917static void tcp_remove_reno_sacks(struct sock *sk, int acked)
1918{
1919 struct tcp_sock *tp = tcp_sk(sk);
1920
1921 if (acked > 0) {
1922 /* One ACK acked hole. The rest eat duplicate ACKs. */
Yuchung Chengddf1af62016-02-02 10:33:06 -08001923 tp->delivered += max_t(int, acked - tp->sacked_out, 1);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001924 if (acked - 1 >= tp->sacked_out)
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001925 tp->sacked_out = 0;
1926 else
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001927 tp->sacked_out -= acked - 1;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001928 }
1929 tcp_check_reno_reordering(sk, acked);
Ilpo Järvinen005903b2007-08-09 14:44:16 +03001930 tcp_verify_left_out(tp);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001931}
1932
1933static inline void tcp_reset_reno_sack(struct tcp_sock *tp)
1934{
1935 tp->sacked_out = 0;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001936}
1937
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001938void tcp_clear_retrans(struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 tp->retrans_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 tp->lost_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 tp->undo_marker = 0;
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001943 tp->undo_retrans = -1;
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07001944 tp->fackets_out = 0;
1945 tp->sacked_out = 0;
1946}
1947
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001948static inline void tcp_init_undo(struct tcp_sock *tp)
1949{
1950 tp->undo_marker = tp->snd_una;
1951 /* Retransmission still in flight may cause DSACKs later. */
1952 tp->undo_retrans = tp->retrans_out ? : -1;
1953}
1954
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001955/* Enter Loss state. If we detect SACK reneging, forget all SACK information
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 * and reset tags completely, otherwise preserve SACKs. If receiver
1957 * dropped its ofo queue, we will know this due to reneging detection.
1958 */
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001959void tcp_enter_loss(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001961 const struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov1043e252016-02-03 09:46:52 +02001963 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct sk_buff *skb;
Yuchung Chengf82b6812015-07-13 12:10:20 -07001965 bool new_recovery = icsk->icsk_ca_state < TCP_CA_Recovery;
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001966 bool is_reneg; /* is receiver reneging on SACKs? */
Neal Cardwell0682e692016-09-19 23:39:13 -04001967 bool mark_lost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 /* Reduce ssthresh if it has not yet been made inside this window. */
Yuchung Chenge33099f2013-03-20 13:33:00 +00001970 if (icsk->icsk_ca_state <= TCP_CA_Disorder ||
1971 !after(tp->high_seq, tp->snd_una) ||
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001972 (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) {
1973 tp->prior_ssthresh = tcp_current_ssthresh(sk);
1974 tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
1975 tcp_ca_event(sk, CA_EVENT_LOSS);
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001976 tcp_init_undo(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978 tp->snd_cwnd = 1;
1979 tp->snd_cwnd_cnt = 0;
1980 tp->snd_cwnd_stamp = tcp_time_stamp;
1981
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001982 tp->retrans_out = 0;
1983 tp->lost_out = 0;
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07001984
1985 if (tcp_is_reno(tp))
1986 tcp_reset_reno_sack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001988 skb = tcp_write_queue_head(sk);
1989 is_reneg = skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED);
1990 if (is_reneg) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07001991 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSACKRENEGING);
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07001992 tp->sacked_out = 0;
1993 tp->fackets_out = 0;
Yousuk Seunge74fe722017-12-07 13:41:34 -08001994 /* Mark SACK reneging until we recover from this loss event. */
1995 tp->is_sack_reneg = 1;
Ilpo Järvinenb7689202007-09-20 11:37:19 -07001996 }
Ilpo Järvinen64edc272008-09-20 21:18:32 -07001997 tcp_clear_all_retrans_hints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
David S. Millerfe067e82007-03-07 12:12:44 -08001999 tcp_for_write_queue(skb, sk) {
2000 if (skb == tcp_send_head(sk))
2001 break;
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07002002
Neal Cardwell0682e692016-09-19 23:39:13 -04002003 mark_lost = (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||
2004 is_reneg);
2005 if (mark_lost)
2006 tcp_sum_lost(tp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED;
Neal Cardwell0682e692016-09-19 23:39:13 -04002008 if (mark_lost) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
2010 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
2011 tp->lost_out += tcp_skb_pcount(skb);
Ilpo Järvinen006f5822008-09-20 21:20:20 -07002012 tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
2014 }
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002015 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Yuchung Cheng74c181d2013-08-12 16:41:25 -07002017 /* Timeout in disordered state after receiving substantial DUPACKs
2018 * suggests that the degree of reordering is over-estimated.
2019 */
2020 if (icsk->icsk_ca_state <= TCP_CA_Disorder &&
Nikolay Borisov1043e252016-02-03 09:46:52 +02002021 tp->sacked_out >= net->ipv4.sysctl_tcp_reordering)
Yuchung Cheng74c181d2013-08-12 16:41:25 -07002022 tp->reordering = min_t(unsigned int, tp->reordering,
Nikolay Borisov1043e252016-02-03 09:46:52 +02002023 net->ipv4.sysctl_tcp_reordering);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002024 tcp_set_ca_state(sk, TCP_CA_Loss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 tp->high_seq = tp->snd_nxt;
Florian Westphal735d3832014-09-29 13:08:30 +02002026 tcp_ecn_queue_cwr(tp);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002027
2028 /* F-RTO RFC5682 sec 3.1 step 1: retransmit SND.UNA if no previous
2029 * loss recovery is underway except recurring timeout(s) on
2030 * the same SND.UNA (sec 3.2). Disable F-RTO on path MTU probing
2031 */
2032 tp->frto = sysctl_tcp_frto &&
2033 (new_recovery || icsk->icsk_retransmits) &&
2034 !inet_csk(sk)->icsk_mtup.probe_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002037/* If ACK arrived pointing to a remembered SACK, it means that our
2038 * remembered SACKs do not reflect real state of receiver i.e.
2039 * receiver _host_ is heavily congested (or buggy).
2040 *
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002041 * To avoid big spurious retransmission bursts due to transient SACK
2042 * scoreboard oddities that look like reneging, we give the receiver a
2043 * little time (max(RTT/2, 10ms)) to send us some more ACKs that will
2044 * restore sanity to the SACK scoreboard. If the apparent reneging
2045 * persists until this RTO then we'll clear the SACK scoreboard.
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002046 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002047static bool tcp_check_sack_reneging(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002049 if (flag & FLAG_SACK_RENEGING) {
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002050 struct tcp_sock *tp = tcp_sk(sk);
2051 unsigned long delay = max(usecs_to_jiffies(tp->srtt_us >> 4),
2052 msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002054 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002055 delay, TCP_RTO_MAX);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002056 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002058 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002061static inline int tcp_fackets_out(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002063 return tcp_is_reno(tp) ? tp->sacked_out + 1 : tp->fackets_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002066/* Heurestics to calculate number of duplicate ACKs. There's no dupACKs
2067 * counter when SACK is enabled (without SACK, sacked_out is used for
2068 * that purpose).
2069 *
2070 * Instead, with FACK TCP uses fackets_out that includes both SACKed
2071 * segments up to the highest received SACK block so far and holes in
2072 * between them.
2073 *
2074 * With reordering, holes may still be in flight, so RFC3517 recovery
2075 * uses pure sacked_out (total number of SACKed segments) even though
2076 * it violates the RFC that uses duplicate ACKs, often these are equal
2077 * but when e.g. out-of-window ACKs or packet duplication occurs,
2078 * they differ. Since neither occurs due to loss, TCP should really
2079 * ignore them.
2080 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002081static inline int tcp_dupack_heuristics(const struct tcp_sock *tp)
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002082{
2083 return tcp_is_fack(tp) ? tp->fackets_out : tp->sacked_out + 1;
2084}
2085
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002086static bool tcp_pause_early_retransmit(struct sock *sk, int flag)
2087{
2088 struct tcp_sock *tp = tcp_sk(sk);
2089 unsigned long delay;
2090
2091 /* Delay early retransmit and entering fast recovery for
2092 * max(RTT/4, 2msec) unless ack has ECE mark, no RTT samples
2093 * available, or RTO is scheduled to fire first.
2094 */
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00002095 if (sysctl_tcp_early_retrans < 2 || sysctl_tcp_early_retrans > 3 ||
Eric Dumazet740b0f12014-02-26 14:02:48 -08002096 (flag & FLAG_ECE) || !tp->srtt_us)
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002097 return false;
2098
Eric Dumazet740b0f12014-02-26 14:02:48 -08002099 delay = max(usecs_to_jiffies(tp->srtt_us >> 5),
2100 msecs_to_jiffies(2));
2101
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002102 if (!time_after(inet_csk(sk)->icsk_timeout, (jiffies + delay)))
2103 return false;
2104
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00002105 inet_csk_reset_xmit_timer(sk, ICSK_TIME_EARLY_RETRANS, delay,
2106 TCP_RTO_MAX);
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002107 return true;
2108}
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110/* Linux NewReno/SACK/FACK/ECN state machine.
2111 * --------------------------------------
2112 *
2113 * "Open" Normal state, no dubious events, fast path.
2114 * "Disorder" In all the respects it is "Open",
2115 * but requires a bit more attention. It is entered when
2116 * we see some SACKs or dupacks. It is split of "Open"
2117 * mainly to move some processing from fast path to slow one.
2118 * "CWR" CWND was reduced due to some Congestion Notification event.
2119 * It can be ECN, ICMP source quench, local device congestion.
2120 * "Recovery" CWND was reduced, we are fast-retransmitting.
2121 * "Loss" CWND was reduced due to RTO timeout or SACK reneging.
2122 *
2123 * tcp_fastretrans_alert() is entered:
2124 * - each incoming ACK, if state is not "Open"
2125 * - when arrived ACK is unusual, namely:
2126 * * SACK
2127 * * Duplicate ACK.
2128 * * ECN ECE.
2129 *
2130 * Counting packets in flight is pretty simple.
2131 *
2132 * in_flight = packets_out - left_out + retrans_out
2133 *
2134 * packets_out is SND.NXT-SND.UNA counted in packets.
2135 *
2136 * retrans_out is number of retransmitted segments.
2137 *
2138 * left_out is number of segments left network, but not ACKed yet.
2139 *
2140 * left_out = sacked_out + lost_out
2141 *
2142 * sacked_out: Packets, which arrived to receiver out of order
2143 * and hence not ACKed. With SACKs this number is simply
2144 * amount of SACKed data. Even without SACKs
2145 * it is easy to give pretty reliable estimate of this number,
2146 * counting duplicate ACKs.
2147 *
2148 * lost_out: Packets lost by network. TCP has no explicit
2149 * "loss notification" feedback from network (for now).
2150 * It means that this number can be only _guessed_.
2151 * Actually, it is the heuristics to predict lossage that
2152 * distinguishes different algorithms.
2153 *
2154 * F.e. after RTO, when all the queue is considered as lost,
2155 * lost_out = packets_out and in_flight = retrans_out.
2156 *
2157 * Essentially, we have now two algorithms counting
2158 * lost packets.
2159 *
2160 * FACK: It is the simplest heuristics. As soon as we decided
2161 * that something is lost, we decide that _all_ not SACKed
2162 * packets until the most forward SACK are lost. I.e.
2163 * lost_out = fackets_out - sacked_out and left_out = fackets_out.
2164 * It is absolutely correct estimate, if network does not reorder
2165 * packets. And it loses any connection to reality when reordering
2166 * takes place. We use FACK by default until reordering
2167 * is suspected on the path to this destination.
2168 *
2169 * NewReno: when Recovery is entered, we assume that one segment
2170 * 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 *
2175 * Imagine, that's all! Forget about all this shamanism about CWND inflation
2176 * deflation etc. CWND is real congestion window, never inflated, changes
2177 * only according to classic VJ rules.
2178 *
2179 * Really tricky (and requiring careful tuning) part of algorithm
2180 * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue().
2181 * The first determines the moment _when_ we should reduce CWND and,
2182 * hence, slow down forward transmission. In fact, it determines the moment
2183 * when we decide that hole is caused by loss, rather than by a reorder.
2184 *
2185 * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill
2186 * holes, caused by lost packets.
2187 *
2188 * And the most logically complicated part of algorithm is undo
2189 * heuristics. We detect false retransmits due to both too early
2190 * fast retransmit (reordering) and underestimated RTO, analyzing
2191 * timestamps and D-SACKs. When we detect that some segments were
2192 * retransmitted by mistake and CWND reduction was wrong, we undo
2193 * window reduction and abort recovery phase. This logic is hidden
2194 * inside several functions named tcp_try_undo_<something>.
2195 */
2196
2197/* This function decides, when we should leave Disordered state
2198 * and enter Recovery phase, reducing congestion window.
2199 *
2200 * Main question: may we further continue forward transmission
2201 * with the same cwnd?
2202 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002203static bool tcp_time_to_recover(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002205 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 __u32 packets_out;
Nikolay Borisov1043e252016-02-03 09:46:52 +02002207 int tcp_reordering = sock_net(sk)->ipv4.sysctl_tcp_reordering;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 /* Trick#1: The loss is proven. */
2210 if (tp->lost_out)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002211 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 /* Not-A-Trick#2 : Classic rule... */
Andreas Petlundea84e552009-10-27 03:27:21 +00002214 if (tcp_dupack_heuristics(tp) > tp->reordering)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002215 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 /* Trick#4: It is still not OK... But will it be useful to delay
2218 * recovery more?
2219 */
2220 packets_out = tp->packets_out;
2221 if (packets_out <= tp->reordering &&
Nikolay Borisov1043e252016-02-03 09:46:52 +02002222 tp->sacked_out >= max_t(__u32, packets_out/2, tcp_reordering) &&
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002223 !tcp_may_send_now(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /* We have nothing to send. This connection is limited
2225 * either by receiver window or by application.
2226 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002227 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229
Andreas Petlund7e380172010-02-18 04:48:19 +00002230 /* If a thin stream is detected, retransmit after first
2231 * received dupack. Employ only if SACK is supported in order
2232 * to avoid possible corner-case series of spurious retransmissions
2233 * Use only if there are no unsent data.
2234 */
2235 if ((tp->thin_dupack || sysctl_tcp_thin_dupack) &&
2236 tcp_stream_is_thin(tp) && tcp_dupack_heuristics(tp) > 1 &&
2237 tcp_is_sack(tp) && !tcp_send_head(sk))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002238 return true;
Andreas Petlund7e380172010-02-18 04:48:19 +00002239
Yuchung Chengeed530b2012-05-02 13:30:03 +00002240 /* Trick#6: TCP early retransmit, per RFC5827. To avoid spurious
2241 * retransmissions due to small network reorderings, we implement
2242 * Mitigation A.3 in the RFC and delay the retransmission for a short
2243 * interval if appropriate.
2244 */
2245 if (tp->do_early_retrans && !tp->retrans_out && tp->sacked_out &&
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00002246 (tp->packets_out >= (tp->sacked_out + 1) && tp->packets_out < 4) &&
Yuchung Chengeed530b2012-05-02 13:30:03 +00002247 !tcp_may_send_now(sk))
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002248 return !tcp_pause_early_retransmit(sk, flag);
Yuchung Chengeed530b2012-05-02 13:30:03 +00002249
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002250 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
Yuchung Cheng974c1232012-01-19 14:42:21 +00002253/* Detect loss in event "A" above by marking head of queue up as lost.
2254 * For FACK or non-SACK(Reno) senders, the first "packets" number of segments
2255 * are considered lost. For RFC3517 SACK, a segment is considered lost if it
2256 * has at least tp->reordering SACKed seqments above it; "packets" refers to
2257 * the maximum SACKed segments to pass before reaching this limit.
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002258 */
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002259static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002261 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 struct sk_buff *skb;
Neal Cardwelld88270e2016-01-25 14:01:53 -08002263 int cnt, oldcnt, lost;
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002264 unsigned int mss;
Yuchung Cheng974c1232012-01-19 14:42:21 +00002265 /* Use SACK to deduce losses of new sequences sent during recovery */
2266 const u32 loss_high = tcp_is_sack(tp) ? tp->snd_nxt : tp->high_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002268 WARN_ON(packets > tp->packets_out);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002269 if (tp->lost_skb_hint) {
2270 skb = tp->lost_skb_hint;
2271 cnt = tp->lost_cnt_hint;
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002272 /* Head already handled? */
2273 if (mark_head && skb != tcp_write_queue_head(sk))
2274 return;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002275 } else {
David S. Millerfe067e82007-03-07 12:12:44 -08002276 skb = tcp_write_queue_head(sk);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002277 cnt = 0;
2278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
David S. Millerfe067e82007-03-07 12:12:44 -08002280 tcp_for_write_queue_from(skb, sk) {
2281 if (skb == tcp_send_head(sk))
2282 break;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002283 /* TODO: do this better */
2284 /* this is not the most efficient way to do this... */
2285 tp->lost_skb_hint = skb;
2286 tp->lost_cnt_hint = cnt;
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002287
Yuchung Cheng974c1232012-01-19 14:42:21 +00002288 if (after(TCP_SKB_CB(skb)->end_seq, loss_high))
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002289 break;
2290
2291 oldcnt = cnt;
Ilpo Järvinenad1984e2008-01-30 20:06:02 -08002292 if (tcp_is_fack(tp) || tcp_is_reno(tp) ||
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002293 (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
2294 cnt += tcp_skb_pcount(skb);
2295
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002296 if (cnt > packets) {
Yuchung Chengb3de7552010-09-24 13:22:06 +00002297 if ((tcp_is_sack(tp) && !tcp_is_fack(tp)) ||
Neal Cardwellc0638c22012-03-02 21:36:51 +00002298 (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||
Yuchung Chengb3de7552010-09-24 13:22:06 +00002299 (oldcnt >= packets))
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002300 break;
2301
Eric Dumazetf69ad292015-06-11 09:15:18 -07002302 mss = tcp_skb_mss(skb);
Neal Cardwelld88270e2016-01-25 14:01:53 -08002303 /* If needed, chop off the prefix to mark as lost. */
2304 lost = (packets - oldcnt) * mss;
2305 if (lost < skb->len &&
2306 tcp_fragment(sk, skb, lost, mss, GFP_ATOMIC) < 0)
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002307 break;
2308 cnt = packets;
2309 }
2310
Ilpo Järvinen41ea36e2008-09-20 21:19:22 -07002311 tcp_skb_mark_lost(tp, skb);
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002312
2313 if (mark_head)
2314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002316 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
2319/* Account newly detected lost packet(s) */
2320
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002321static void tcp_update_scoreboard(struct sock *sk, int fast_rexmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002323 struct tcp_sock *tp = tcp_sk(sk);
2324
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002325 if (tcp_is_reno(tp)) {
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002326 tcp_mark_head_lost(sk, 1, 1);
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002327 } else if (tcp_is_fack(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 int lost = tp->fackets_out - tp->reordering;
2329 if (lost <= 0)
2330 lost = 1;
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002331 tcp_mark_head_lost(sk, lost, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 } else {
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002333 int sacked_upto = tp->sacked_out - tp->reordering;
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002334 if (sacked_upto >= 0)
2335 tcp_mark_head_lost(sk, sacked_upto, 0);
2336 else if (fast_rexmit)
2337 tcp_mark_head_lost(sk, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Yuchung Cheng77c63122015-10-16 21:57:44 -07002341static bool tcp_tsopt_ecr_before(const struct tcp_sock *tp, u32 when)
2342{
2343 return tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
2344 before(tp->rx_opt.rcv_tsecr, when);
2345}
2346
Yuchung Cheng659a8ad2015-10-16 21:57:46 -07002347/* skb is spurious retransmitted if the returned timestamp echo
2348 * reply is prior to the skb transmission time
2349 */
2350static bool tcp_skb_spurious_retrans(const struct tcp_sock *tp,
2351 const struct sk_buff *skb)
2352{
2353 return (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS) &&
2354 tcp_tsopt_ecr_before(tp, tcp_skb_timestamp(skb));
2355}
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357/* Nothing was retransmitted or returned timestamp is less
2358 * than timestamp of the first retransmission.
2359 */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00002360static inline bool tcp_packet_delayed(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
2362 return !tp->retrans_stamp ||
Yuchung Cheng77c63122015-10-16 21:57:44 -07002363 tcp_tsopt_ecr_before(tp, tp->retrans_stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
2366/* Undo procedures. */
2367
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002368/* We can clear retrans_stamp when there are no retransmissions in the
2369 * window. It would seem that it is trivially available for us in
2370 * tp->retrans_out, however, that kind of assumptions doesn't consider
2371 * what will happen if errors occur when sending retransmission for the
2372 * second time. ...It could the that such segment has only
2373 * TCPCB_EVER_RETRANS set at the present time. It seems that checking
2374 * the head skb is enough except for some reneging corner cases that
2375 * are not worth the effort.
2376 *
2377 * Main reason for all this complexity is the fact that connection dying
2378 * time now depends on the validity of the retrans_stamp, in particular,
2379 * that successive retransmissions of a segment must not advance
2380 * retrans_stamp under any conditions.
2381 */
2382static bool tcp_any_retrans_done(const struct sock *sk)
2383{
2384 const struct tcp_sock *tp = tcp_sk(sk);
2385 struct sk_buff *skb;
2386
2387 if (tp->retrans_out)
2388 return true;
2389
2390 skb = tcp_write_queue_head(sk);
2391 if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS))
2392 return true;
2393
2394 return false;
2395}
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397#if FASTRETRANS_DEBUG > 1
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002398static void DBGUNDO(struct sock *sk, const char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002400 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 struct inet_sock *inet = inet_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002402
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002403 if (sk->sk_family == AF_INET) {
Joe Perches91df42b2012-05-15 14:11:54 +00002404 pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n",
2405 msg,
2406 &inet->inet_daddr, ntohs(inet->inet_dport),
2407 tp->snd_cwnd, tcp_left_out(tp),
2408 tp->snd_ssthresh, tp->prior_ssthresh,
2409 tp->packets_out);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002410 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002411#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002412 else if (sk->sk_family == AF_INET6) {
Joe Perches91df42b2012-05-15 14:11:54 +00002413 pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n",
2414 msg,
Eric Dumazet019b1c92016-09-22 17:54:00 -07002415 &sk->sk_v6_daddr, ntohs(inet->inet_dport),
Joe Perches91df42b2012-05-15 14:11:54 +00002416 tp->snd_cwnd, tcp_left_out(tp),
2417 tp->snd_ssthresh, tp->prior_ssthresh,
2418 tp->packets_out);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002419 }
2420#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422#else
2423#define DBGUNDO(x...) do { } while (0)
2424#endif
2425
Yuchung Cheng7026b912013-05-29 14:20:13 +00002426static void tcp_undo_cwnd_reduction(struct sock *sk, bool unmark_loss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002428 struct tcp_sock *tp = tcp_sk(sk);
2429
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002430 if (unmark_loss) {
2431 struct sk_buff *skb;
2432
2433 tcp_for_write_queue(skb, sk) {
2434 if (skb == tcp_send_head(sk))
2435 break;
2436 TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
2437 }
2438 tp->lost_out = 0;
2439 tcp_clear_all_retrans_hints(tp);
2440 }
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if (tp->prior_ssthresh) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002443 const struct inet_connection_sock *icsk = inet_csk(sk);
2444
2445 if (icsk->icsk_ca_ops->undo_cwnd)
2446 tp->snd_cwnd = icsk->icsk_ca_ops->undo_cwnd(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 else
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002448 tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Yuchung Cheng7026b912013-05-29 14:20:13 +00002450 if (tp->prior_ssthresh > tp->snd_ssthresh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 tp->snd_ssthresh = tp->prior_ssthresh;
Florian Westphal735d3832014-09-29 13:08:30 +02002452 tcp_ecn_withdraw_cwr(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 tp->snd_cwnd_stamp = tcp_time_stamp;
Yuchung Cheng7026b912013-05-29 14:20:13 +00002456 tp->undo_marker = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00002459static inline bool tcp_may_undo(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002461 return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
2464/* People celebrate: "We love our President!" */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002465static bool tcp_try_undo_recovery(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002467 struct tcp_sock *tp = tcp_sk(sk);
2468
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (tcp_may_undo(tp)) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002470 int mib_idx;
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /* Happy end! We did not retransmit anything
2473 * or our original transmission succeeded.
2474 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002475 DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans");
Yuchung Cheng7026b912013-05-29 14:20:13 +00002476 tcp_undo_cwnd_reduction(sk, false);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002477 if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss)
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002478 mib_idx = LINUX_MIB_TCPLOSSUNDO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002480 mib_idx = LINUX_MIB_TCPFULLUNDO;
2481
Eric Dumazetc10d9312016-04-29 14:16:47 -07002482 NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002484 if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 /* Hold old state until something *above* high_seq
2486 * is ACKed. For Reno it is MUST to prevent false
2487 * fast retransmits (RFC2582). SACK TCP is safe. */
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002488 if (!tcp_any_retrans_done(sk))
2489 tp->retrans_stamp = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002490 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002492 tcp_set_ca_state(sk, TCP_CA_Open);
Yousuk Seunge74fe722017-12-07 13:41:34 -08002493 tp->is_sack_reneg = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002494 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
2497/* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002498static bool tcp_try_undo_dsack(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002500 struct tcp_sock *tp = tcp_sk(sk);
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (tp->undo_marker && !tp->undo_retrans) {
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002503 DBGUNDO(sk, "D-SACK");
Yuchung Cheng7026b912013-05-29 14:20:13 +00002504 tcp_undo_cwnd_reduction(sk, false);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002505 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKUNDO);
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002506 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002508 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Yuchung Chenge33099f2013-03-20 13:33:00 +00002511/* Undo during loss recovery after partial ACK or using F-RTO. */
2512static bool tcp_try_undo_loss(struct sock *sk, bool frto_undo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002514 struct tcp_sock *tp = tcp_sk(sk);
2515
Yuchung Chenge33099f2013-03-20 13:33:00 +00002516 if (frto_undo || tcp_may_undo(tp)) {
Yuchung Cheng7026b912013-05-29 14:20:13 +00002517 tcp_undo_cwnd_reduction(sk, true);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002518
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002519 DBGUNDO(sk, "partial loss");
Eric Dumazetc10d9312016-04-29 14:16:47 -07002520 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSUNDO);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002521 if (frto_undo)
Eric Dumazetc10d9312016-04-29 14:16:47 -07002522 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07002523 LINUX_MIB_TCPSPURIOUSRTOS);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002524 inet_csk(sk)->icsk_retransmits = 0;
Yousuk Seunge74fe722017-12-07 13:41:34 -08002525 if (frto_undo || tcp_is_sack(tp)) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002526 tcp_set_ca_state(sk, TCP_CA_Open);
Yousuk Seunge74fe722017-12-07 13:41:34 -08002527 tp->is_sack_reneg = 0;
2528 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002529 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002531 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Yuchung Cheng37598242015-07-01 14:11:15 -07002534/* The cwnd reduction in CWR and Recovery uses the PRR algorithm in RFC 6937.
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002535 * It computes the number of packets to send (sndcnt) based on packets newly
2536 * delivered:
2537 * 1) If the packets in flight is larger than ssthresh, PRR spreads the
2538 * cwnd reductions across a full RTT.
Yuchung Cheng37598242015-07-01 14:11:15 -07002539 * 2) Otherwise PRR uses packet conservation to send as much as delivered.
2540 * But when the retransmits are acked without further losses, PRR
2541 * slow starts cwnd up to ssthresh to speed up the recovery.
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002542 */
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002543static void tcp_init_cwnd_reduction(struct sock *sk)
Yuchung Cheng684bad12012-09-02 17:38:04 +00002544{
2545 struct tcp_sock *tp = tcp_sk(sk);
2546
2547 tp->high_seq = tp->snd_nxt;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00002548 tp->tlp_high_seq = 0;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002549 tp->snd_cwnd_cnt = 0;
2550 tp->prior_cwnd = tp->snd_cwnd;
2551 tp->prr_delivered = 0;
2552 tp->prr_out = 0;
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002553 tp->snd_ssthresh = inet_csk(sk)->icsk_ca_ops->ssthresh(sk);
Florian Westphal735d3832014-09-29 13:08:30 +02002554 tcp_ecn_queue_cwr(tp);
Yuchung Cheng684bad12012-09-02 17:38:04 +00002555}
2556
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002557static void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked,
2558 int flag)
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002559{
2560 struct tcp_sock *tp = tcp_sk(sk);
2561 int sndcnt = 0;
2562 int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp);
2563
Yuchung Cheng8b8a3212016-01-06 12:42:38 -08002564 if (newly_acked_sacked <= 0 || WARN_ON_ONCE(!tp->prior_cwnd))
2565 return;
2566
Yuchung Cheng684bad12012-09-02 17:38:04 +00002567 tp->prr_delivered += newly_acked_sacked;
Yuchung Cheng37598242015-07-01 14:11:15 -07002568 if (delta < 0) {
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002569 u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered +
2570 tp->prior_cwnd - 1;
2571 sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out;
Yuchung Cheng37598242015-07-01 14:11:15 -07002572 } else if ((flag & FLAG_RETRANS_DATA_ACKED) &&
2573 !(flag & FLAG_LOST_RETRANS)) {
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002574 sndcnt = min_t(int, delta,
2575 max_t(int, tp->prr_delivered - tp->prr_out,
2576 newly_acked_sacked) + 1);
Yuchung Cheng37598242015-07-01 14:11:15 -07002577 } else {
2578 sndcnt = min(delta, newly_acked_sacked);
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002579 }
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002580 /* Force a fast retransmit upon entering fast recovery */
2581 sndcnt = max(sndcnt, (tp->prr_out ? 0 : 1));
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002582 tp->snd_cwnd = tcp_packets_in_flight(tp) + sndcnt;
2583}
2584
Yuchung Cheng684bad12012-09-02 17:38:04 +00002585static inline void tcp_end_cwnd_reduction(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002587 struct tcp_sock *tp = tcp_sk(sk);
Nandita Dukkipatia262f0c2011-08-21 20:21:57 +00002588
Yuchung Chengc0402762016-09-19 23:39:21 -04002589 if (inet_csk(sk)->icsk_ca_ops->cong_control)
2590 return;
2591
Yuchung Cheng684bad12012-09-02 17:38:04 +00002592 /* Reset cwnd to ssthresh in CWR or Recovery (unless it's undone) */
Yuchung Cheng3914a7e2017-08-01 13:22:32 -07002593 if (tp->snd_ssthresh < TCP_INFINITE_SSTHRESH &&
2594 (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR || tp->undo_marker)) {
Yuchung Cheng684bad12012-09-02 17:38:04 +00002595 tp->snd_cwnd = tp->snd_ssthresh;
2596 tp->snd_cwnd_stamp = tcp_time_stamp;
Yuchung Cheng67d41202011-03-14 10:57:03 +00002597 }
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002598 tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
Yuchung Cheng684bad12012-09-02 17:38:04 +00002601/* Enter CWR state. Disable cwnd undo since congestion is proven with ECN */
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002602void tcp_enter_cwr(struct sock *sk)
Yuchung Cheng09484d12012-09-02 17:38:02 +00002603{
2604 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002605
2606 tp->prior_ssthresh = 0;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002607 if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
Yuchung Cheng09484d12012-09-02 17:38:02 +00002608 tp->undo_marker = 0;
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002609 tcp_init_cwnd_reduction(sk);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002610 tcp_set_ca_state(sk, TCP_CA_CWR);
2611 }
2612}
Kenneth Klette Jonassen7782ad82015-06-10 19:08:16 +02002613EXPORT_SYMBOL(tcp_enter_cwr);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002614
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002615static void tcp_try_keep_open(struct sock *sk)
2616{
2617 struct tcp_sock *tp = tcp_sk(sk);
2618 int state = TCP_CA_Open;
2619
Neal Cardwellf6982042011-11-16 08:58:04 +00002620 if (tcp_left_out(tp) || tcp_any_retrans_done(sk))
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002621 state = TCP_CA_Disorder;
2622
2623 if (inet_csk(sk)->icsk_ca_state != state) {
2624 tcp_set_ca_state(sk, state);
2625 tp->high_seq = tp->snd_nxt;
2626 }
2627}
2628
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002629static void tcp_try_to_open(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002631 struct tcp_sock *tp = tcp_sk(sk);
2632
Ilpo Järvinen86426c22007-08-09 14:45:17 +03002633 tcp_verify_left_out(tp);
2634
Yuchung Cheng9b441902013-03-20 13:32:58 +00002635 if (!tcp_any_retrans_done(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 tp->retrans_stamp = 0;
2637
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002638 if (flag & FLAG_ECE)
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002639 tcp_enter_cwr(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002641 if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) {
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002642 tcp_try_keep_open(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
2644}
2645
John Heffner5d424d52006-03-20 17:53:41 -08002646static void tcp_mtup_probe_failed(struct sock *sk)
2647{
2648 struct inet_connection_sock *icsk = inet_csk(sk);
2649
2650 icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1;
2651 icsk->icsk_mtup.probe_size = 0;
Eric Dumazetc10d9312016-04-29 14:16:47 -07002652 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPFAIL);
John Heffner5d424d52006-03-20 17:53:41 -08002653}
2654
Ilpo Järvinen72211e92009-03-14 14:23:04 +00002655static void tcp_mtup_probe_success(struct sock *sk)
John Heffner5d424d52006-03-20 17:53:41 -08002656{
2657 struct tcp_sock *tp = tcp_sk(sk);
2658 struct inet_connection_sock *icsk = inet_csk(sk);
2659
2660 /* FIXME: breaks with very large cwnd */
2661 tp->prior_ssthresh = tcp_current_ssthresh(sk);
2662 tp->snd_cwnd = tp->snd_cwnd *
2663 tcp_mss_to_mtu(sk, tp->mss_cache) /
2664 icsk->icsk_mtup.probe_size;
2665 tp->snd_cwnd_cnt = 0;
2666 tp->snd_cwnd_stamp = tcp_time_stamp;
John Heffner9c6d5e52010-10-06 21:18:02 -07002667 tp->snd_ssthresh = tcp_current_ssthresh(sk);
John Heffner5d424d52006-03-20 17:53:41 -08002668
2669 icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size;
2670 icsk->icsk_mtup.probe_size = 0;
2671 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002672 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPSUCCESS);
John Heffner5d424d52006-03-20 17:53:41 -08002673}
2674
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002675/* Do a simple retransmit without using the backoff mechanisms in
2676 * tcp_timer. This is used for path mtu discovery.
2677 * The socket is already locked here.
2678 */
2679void tcp_simple_retransmit(struct sock *sk)
2680{
2681 const struct inet_connection_sock *icsk = inet_csk(sk);
2682 struct tcp_sock *tp = tcp_sk(sk);
2683 struct sk_buff *skb;
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00002684 unsigned int mss = tcp_current_mss(sk);
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002685 u32 prior_lost = tp->lost_out;
2686
2687 tcp_for_write_queue(skb, sk) {
2688 if (skb == tcp_send_head(sk))
2689 break;
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08002690 if (tcp_skb_seglen(skb) > mss &&
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002691 !(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) {
2692 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
2693 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
2694 tp->retrans_out -= tcp_skb_pcount(skb);
2695 }
2696 tcp_skb_mark_lost_uncond_verify(tp, skb);
2697 }
2698 }
2699
2700 tcp_clear_retrans_hints_partial(tp);
2701
2702 if (prior_lost == tp->lost_out)
2703 return;
2704
2705 if (tcp_is_reno(tp))
2706 tcp_limit_reno_sacked(tp);
2707
2708 tcp_verify_left_out(tp);
2709
2710 /* Don't muck with the congestion window here.
2711 * Reason is that we do not increase amount of _data_
2712 * in network, but units changed and effective
2713 * cwnd/ssthresh really reduced now.
2714 */
2715 if (icsk->icsk_ca_state != TCP_CA_Loss) {
2716 tp->high_seq = tp->snd_nxt;
2717 tp->snd_ssthresh = tcp_current_ssthresh(sk);
2718 tp->prior_ssthresh = 0;
2719 tp->undo_marker = 0;
2720 tcp_set_ca_state(sk, TCP_CA_Loss);
2721 }
2722 tcp_xmit_retransmit_queue(sk);
2723}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002724EXPORT_SYMBOL(tcp_simple_retransmit);
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002725
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002726static void tcp_enter_recovery(struct sock *sk, bool ece_ack)
2727{
2728 struct tcp_sock *tp = tcp_sk(sk);
2729 int mib_idx;
2730
2731 if (tcp_is_reno(tp))
2732 mib_idx = LINUX_MIB_TCPRENORECOVERY;
2733 else
2734 mib_idx = LINUX_MIB_TCPSACKRECOVERY;
2735
Eric Dumazetc10d9312016-04-29 14:16:47 -07002736 NET_INC_STATS(sock_net(sk), mib_idx);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002737
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002738 tp->prior_ssthresh = 0;
Yuchung Cheng989e04c2014-08-22 14:15:22 -07002739 tcp_init_undo(tp);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002740
Yuchung Cheng291a00d2015-07-01 14:11:14 -07002741 if (!tcp_in_cwnd_reduction(sk)) {
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002742 if (!ece_ack)
2743 tp->prior_ssthresh = tcp_current_ssthresh(sk);
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002744 tcp_init_cwnd_reduction(sk);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002745 }
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002746 tcp_set_ca_state(sk, TCP_CA_Recovery);
2747}
2748
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002749/* Process an ACK in CA_Loss state. Move to CA_Open if lost data are
2750 * recovered or spurious. Otherwise retransmits more on partial ACKs.
2751 */
Yuchung Chenge662ca42016-02-02 10:33:04 -08002752static void tcp_process_loss(struct sock *sk, int flag, bool is_dupack,
2753 int *rexmit)
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002754{
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002755 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002756 bool recovered = !before(tp->snd_una, tp->high_seq);
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002757
Yuchung Chengda34ac72015-05-18 12:31:44 -07002758 if ((flag & FLAG_SND_UNA_ADVANCED) &&
2759 tcp_try_undo_loss(sk, false))
2760 return;
2761
Yuchung Chenge33099f2013-03-20 13:33:00 +00002762 if (tp->frto) { /* F-RTO RFC5682 sec 3.1 (sack enhanced version). */
Yuchung Cheng0cfa5c02014-05-30 15:25:59 -07002763 /* Step 3.b. A timeout is spurious if not all data are
2764 * lost, i.e., never-retransmitted data are (s)acked.
2765 */
Yuchung Chengda34ac72015-05-18 12:31:44 -07002766 if ((flag & FLAG_ORIG_SACK_ACKED) &&
2767 tcp_try_undo_loss(sk, true))
Yuchung Chenge33099f2013-03-20 13:33:00 +00002768 return;
Yuchung Cheng0cfa5c02014-05-30 15:25:59 -07002769
Yuchung Chengb7b0ed92015-05-18 12:31:45 -07002770 if (after(tp->snd_nxt, tp->high_seq)) {
2771 if (flag & FLAG_DATA_SACKED || is_dupack)
2772 tp->frto = 0; /* Step 3.a. loss was real */
Yuchung Chenge33099f2013-03-20 13:33:00 +00002773 } else if (flag & FLAG_SND_UNA_ADVANCED && !recovered) {
2774 tp->high_seq = tp->snd_nxt;
Yuchung Chenge662ca42016-02-02 10:33:04 -08002775 /* Step 2.b. Try send new data (but deferred until cwnd
2776 * is updated in tcp_ack()). Otherwise fall back to
2777 * the conventional recovery.
2778 */
2779 if (tcp_send_head(sk) &&
2780 after(tcp_wnd_end(tp), tp->snd_nxt)) {
2781 *rexmit = REXMIT_NEW;
2782 return;
2783 }
Yuchung Chenge33099f2013-03-20 13:33:00 +00002784 tp->frto = 0;
2785 }
2786 }
2787
2788 if (recovered) {
2789 /* F-RTO RFC5682 sec 3.1 step 2.a and 1st part of step 3.a */
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002790 tcp_try_undo_recovery(sk);
2791 return;
2792 }
Yuchung Chenge33099f2013-03-20 13:33:00 +00002793 if (tcp_is_reno(tp)) {
2794 /* A Reno DUPACK means new data in F-RTO step 2.b above are
2795 * delivered. Lower inflight to clock out (re)tranmissions.
2796 */
2797 if (after(tp->snd_nxt, tp->high_seq) && is_dupack)
2798 tcp_add_reno_sack(sk);
2799 else if (flag & FLAG_SND_UNA_ADVANCED)
2800 tcp_reset_reno_sack(tp);
2801 }
Yuchung Chenge662ca42016-02-02 10:33:04 -08002802 *rexmit = REXMIT_LOST;
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002803}
2804
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002805/* Undo during fast recovery after partial ACK. */
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002806static bool tcp_try_undo_partial(struct sock *sk, const int acked)
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002807{
2808 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002809
Yuchung Cheng7026b912013-05-29 14:20:13 +00002810 if (tp->undo_marker && tcp_packet_delayed(tp)) {
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002811 /* Plain luck! Hole if filled with delayed
2812 * packet, rather than with a retransmit.
2813 */
Yuchung Cheng7026b912013-05-29 14:20:13 +00002814 tcp_update_reordering(sk, tcp_fackets_out(tp) + acked, 1);
2815
2816 /* We are getting evidence that the reordering degree is higher
2817 * than we realized. If there are no retransmits out then we
2818 * can undo. Otherwise we clock out new packets but do not
2819 * mark more packets lost or retransmit more.
2820 */
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002821 if (tp->retrans_out)
Yuchung Cheng7026b912013-05-29 14:20:13 +00002822 return true;
Yuchung Cheng7026b912013-05-29 14:20:13 +00002823
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002824 if (!tcp_any_retrans_done(sk))
2825 tp->retrans_stamp = 0;
2826
Yuchung Cheng7026b912013-05-29 14:20:13 +00002827 DBGUNDO(sk, "partial recovery");
2828 tcp_undo_cwnd_reduction(sk, true);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002829 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO);
Yuchung Cheng7026b912013-05-29 14:20:13 +00002830 tcp_try_keep_open(sk);
2831 return true;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002832 }
Yuchung Cheng7026b912013-05-29 14:20:13 +00002833 return false;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002834}
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836/* Process an event, which can update packets-in-flight not trivially.
2837 * Main goal of this function is to calculate new estimate for left_out,
2838 * taking into account both packets sitting in receiver's buffer and
2839 * packets lost by network.
2840 *
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002841 * Besides that it updates the congestion state when packet loss or ECN
2842 * is detected. But it does not reduce the cwnd, it is done by the
2843 * congestion control later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 *
2845 * It does _not_ decide what to send, it is made in function
2846 * tcp_xmit_retransmit_queue().
2847 */
Yuchung Cheng68049732013-05-29 14:20:11 +00002848static void tcp_fastretrans_alert(struct sock *sk, const int acked,
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002849 bool is_dupack, int *ack_flag, int *rexmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002851 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002853 int fast_rexmit = 0, flag = *ack_flag;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002854 bool do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) &&
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002855 (tcp_fackets_out(tp) > tp->reordering));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Eric Dumazet418894c2017-10-05 22:21:25 -07002857 if (!tp->packets_out && tp->sacked_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 tp->sacked_out = 0;
Eric Dumazet418894c2017-10-05 22:21:25 -07002859 if (!tp->sacked_out && tp->fackets_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 tp->fackets_out = 0;
2861
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002862 /* Now state machine starts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 * A. ECE, hence prohibit cwnd undoing, the reduction is required. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002864 if (flag & FLAG_ECE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 tp->prior_ssthresh = 0;
2866
2867 /* B. In all the states check for reneging SACKs. */
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002868 if (tcp_check_sack_reneging(sk, flag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return;
2870
Yuchung Cheng974c1232012-01-19 14:42:21 +00002871 /* C. Check consistency of the current state. */
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002872 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Yuchung Cheng974c1232012-01-19 14:42:21 +00002874 /* D. Check state exit conditions. State can be terminated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 * when high_seq is ACKed. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002876 if (icsk->icsk_ca_state == TCP_CA_Open) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002877 WARN_ON(tp->retrans_out != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 tp->retrans_stamp = 0;
2879 } else if (!before(tp->snd_una, tp->high_seq)) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002880 switch (icsk->icsk_ca_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 case TCP_CA_CWR:
2882 /* CWR is to be held something *above* high_seq
2883 * is ACKed for CWR bit to reach receiver. */
2884 if (tp->snd_una != tp->high_seq) {
Yuchung Cheng684bad12012-09-02 17:38:04 +00002885 tcp_end_cwnd_reduction(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002886 tcp_set_ca_state(sk, TCP_CA_Open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
2888 break;
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 case TCP_CA_Recovery:
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002891 if (tcp_is_reno(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 tcp_reset_reno_sack(tp);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002893 if (tcp_try_undo_recovery(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002895 tcp_end_cwnd_reduction(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 break;
2897 }
2898 }
2899
Yuchung Cheng4f41b1c2015-10-16 21:57:47 -07002900 /* Use RACK to detect loss */
2901 if (sysctl_tcp_recovery & TCP_RACK_LOST_RETRANS &&
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002902 tcp_rack_mark_lost(sk)) {
Yuchung Cheng4f41b1c2015-10-16 21:57:47 -07002903 flag |= FLAG_LOST_RETRANS;
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002904 *ack_flag |= FLAG_LOST_RETRANS;
2905 }
Yuchung Cheng4f41b1c2015-10-16 21:57:47 -07002906
Yuchung Cheng974c1232012-01-19 14:42:21 +00002907 /* E. Process state. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002908 switch (icsk->icsk_ca_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 case TCP_CA_Recovery:
Ilpo Järvinen2e605292007-08-02 19:46:58 -07002910 if (!(flag & FLAG_SND_UNA_ADVANCED)) {
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002911 if (tcp_is_reno(tp) && is_dupack)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002912 tcp_add_reno_sack(sk);
Yuchung Cheng7026b912013-05-29 14:20:13 +00002913 } else {
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002914 if (tcp_try_undo_partial(sk, acked))
Yuchung Cheng7026b912013-05-29 14:20:13 +00002915 return;
2916 /* Partial ACK arrived. Force fast retransmit. */
2917 do_lost = tcp_is_reno(tp) ||
2918 tcp_fackets_out(tp) > tp->reordering;
2919 }
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002920 if (tcp_try_undo_dsack(sk)) {
2921 tcp_try_keep_open(sk);
2922 return;
2923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 break;
2925 case TCP_CA_Loss:
Yuchung Chenge662ca42016-02-02 10:33:04 -08002926 tcp_process_loss(sk, flag, is_dupack, rexmit);
Yuchung Cheng291a00d2015-07-01 14:11:14 -07002927 if (icsk->icsk_ca_state != TCP_CA_Open &&
2928 !(flag & FLAG_LOST_RETRANS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return;
Yuchung Cheng291a00d2015-07-01 14:11:14 -07002930 /* Change state if cwnd is undone or retransmits are lost */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 default:
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002932 if (tcp_is_reno(tp)) {
Ilpo Järvinen2e605292007-08-02 19:46:58 -07002933 if (flag & FLAG_SND_UNA_ADVANCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 tcp_reset_reno_sack(tp);
2935 if (is_dupack)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002936 tcp_add_reno_sack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938
Neal Cardwellf6982042011-11-16 08:58:04 +00002939 if (icsk->icsk_ca_state <= TCP_CA_Disorder)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002940 tcp_try_undo_dsack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002942 if (!tcp_time_to_recover(sk, flag)) {
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002943 tcp_try_to_open(sk, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return;
2945 }
2946
John Heffner5d424d52006-03-20 17:53:41 -08002947 /* MTU probe failure: don't reduce cwnd */
2948 if (icsk->icsk_ca_state < TCP_CA_CWR &&
2949 icsk->icsk_mtup.probe_size &&
John Heffner0e7b1362006-03-20 21:32:58 -08002950 tp->snd_una == tp->mtu_probe.probe_seq_start) {
John Heffner5d424d52006-03-20 17:53:41 -08002951 tcp_mtup_probe_failed(sk);
2952 /* Restores the reduction we did in tcp_mtup_probe() */
2953 tp->snd_cwnd++;
2954 tcp_simple_retransmit(sk);
2955 return;
2956 }
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /* Otherwise enter Recovery state */
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002959 tcp_enter_recovery(sk, (flag & FLAG_ECE));
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002960 fast_rexmit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
2962
Yuchung Cheng3e59cb02013-05-17 13:45:05 +00002963 if (do_lost)
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002964 tcp_update_scoreboard(sk, fast_rexmit);
Yuchung Chenge662ca42016-02-02 10:33:04 -08002965 *rexmit = REXMIT_LOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966}
2967
Yuchung Chengf6722582015-10-16 21:57:42 -07002968static void tcp_update_rtt_min(struct sock *sk, u32 rtt_us)
2969{
Neal Cardwell64033892016-09-19 23:39:10 -04002970 struct tcp_sock *tp = tcp_sk(sk);
2971 u32 wlen = sysctl_tcp_min_rtt_wlen * HZ;
Yuchung Chengf6722582015-10-16 21:57:42 -07002972
Neal Cardwell64033892016-09-19 23:39:10 -04002973 minmax_running_min(&tp->rtt_min, wlen, tcp_time_stamp,
2974 rtt_us ? : jiffies_to_usecs(1));
Yuchung Chengf6722582015-10-16 21:57:42 -07002975}
2976
Yuchung Chenged084952013-07-22 16:20:48 -07002977static inline bool tcp_ack_update_rtt(struct sock *sk, const int flag,
Yuchung Chengf6722582015-10-16 21:57:42 -07002978 long seq_rtt_us, long sack_rtt_us,
2979 long ca_rtt_us)
Ilpo Järvinen41834b72008-12-05 22:43:26 -08002980{
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002981 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen41834b72008-12-05 22:43:26 -08002982
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002983 /* Prefer RTT measured from ACK's timing to TS-ECR. This is because
2984 * broken middle-boxes or peers may corrupt TS-ECR fields. But
2985 * Karn's algorithm forbids taking RTT if some retransmitted data
2986 * is acked (RFC6298).
2987 */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002988 if (seq_rtt_us < 0)
2989 seq_rtt_us = sack_rtt_us;
Yuchung Chenged084952013-07-22 16:20:48 -07002990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 /* RTTM Rule: A TSecr value received in a segment is used to
2992 * update the averaged RTT measurement only if the segment
2993 * acknowledges some new data, i.e., only if it advances the
2994 * left edge of the send window.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 * See draft-ietf-tcplw-high-performance-00, section 3.3.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002997 if (seq_rtt_us < 0 && tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
Yuchung Cheng2909d872013-10-24 08:55:25 -07002998 flag & FLAG_ACKED)
Yuchung Chengf6722582015-10-16 21:57:42 -07002999 seq_rtt_us = ca_rtt_us = jiffies_to_usecs(tcp_time_stamp -
3000 tp->rx_opt.rcv_tsecr);
Eric Dumazet740b0f12014-02-26 14:02:48 -08003001 if (seq_rtt_us < 0)
Yuchung Chenged084952013-07-22 16:20:48 -07003002 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Yuchung Chengf6722582015-10-16 21:57:42 -07003004 /* ca_rtt_us >= 0 is counting on the invariant that ca_rtt_us is
3005 * always taken together with ACK, SACK, or TS-opts. Any negative
3006 * values will be skipped with the seq_rtt_us < 0 check above.
3007 */
3008 tcp_update_rtt_min(sk, ca_rtt_us);
Eric Dumazet740b0f12014-02-26 14:02:48 -08003009 tcp_rtt_estimator(sk, seq_rtt_us);
Yuchung Cheng5b08e472013-07-22 16:20:46 -07003010 tcp_set_rto(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Yuchung Cheng5b08e472013-07-22 16:20:46 -07003012 /* RFC6298: only reset backoff on valid RTT measurement. */
3013 inet_csk(sk)->icsk_backoff = 0;
Yuchung Chenged084952013-07-22 16:20:48 -07003014 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
Yuchung Cheng375fe022013-07-22 16:20:45 -07003017/* Compute time elapsed between (last) SYNACK and the ACK completing 3WHS. */
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07003018void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req)
Yuchung Cheng375fe022013-07-22 16:20:45 -07003019{
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07003020 long rtt_us = -1L;
Yuchung Cheng375fe022013-07-22 16:20:45 -07003021
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07003022 if (req && !req->num_retrans && tcp_rsk(req)->snt_synack.v64) {
3023 struct skb_mstamp now;
Yuchung Chengbc15afa2013-10-24 08:44:25 -07003024
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07003025 skb_mstamp_get(&now);
3026 rtt_us = skb_mstamp_us_delta(&now, &tcp_rsk(req)->snt_synack);
3027 }
3028
Yuchung Chengf6722582015-10-16 21:57:42 -07003029 tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, rtt_us, -1L, rtt_us);
Yuchung Cheng375fe022013-07-22 16:20:45 -07003030}
3031
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07003032
Eric Dumazet24901552014-05-02 21:18:05 -07003033static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003035 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet24901552014-05-02 21:18:05 -07003036
3037 icsk->icsk_ca_ops->cong_avoid(sk, ack, acked);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003038 tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041/* Restart timer after forward progress on connection.
3042 * RFC2988 recommends to restart timer to now+rto.
3043 */
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003044void tcp_rearm_rto(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003046 const struct inet_connection_sock *icsk = inet_csk(sk);
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003047 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003048
Jerry Chu168a8f52012-08-31 12:29:13 +00003049 /* If the retrans timer is currently being used by Fast Open
3050 * for SYN-ACK retrans purpose, stay put.
3051 */
3052 if (tp->fastopen_rsk)
3053 return;
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 if (!tp->packets_out) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003056 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 } else {
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003058 u32 rto = inet_csk(sk)->icsk_rto;
3059 /* Offset the time elapsed after installing regular RTO */
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003060 if (icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
3061 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003062 struct sk_buff *skb = tcp_write_queue_head(sk);
Eric Dumazet7faee5c2014-09-05 15:33:33 -07003063 const u32 rto_time_stamp =
3064 tcp_skb_timestamp(skb) + rto;
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003065 s32 delta = (s32)(rto_time_stamp - tcp_time_stamp);
3066 /* delta may not be positive if the socket is locked
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003067 * when the retrans timer fires and is rescheduled.
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003068 */
Neal Cardwellaadbe1f2017-08-16 17:53:36 -04003069 rto = max(delta, 1);
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003070 }
3071 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto,
3072 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003074}
3075
3076/* This function is called when the delayed ER timer fires. TCP enters
3077 * fast recovery and performs fast-retransmit.
3078 */
3079void tcp_resume_early_retransmit(struct sock *sk)
3080{
3081 struct tcp_sock *tp = tcp_sk(sk);
3082
3083 tcp_rearm_rto(sk);
3084
3085 /* Stop if ER is disabled after the delayed ER timer is scheduled */
3086 if (!tp->do_early_retrans)
3087 return;
3088
3089 tcp_enter_recovery(sk, false);
3090 tcp_update_scoreboard(sk, 1);
3091 tcp_xmit_retransmit_queue(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003094/* If we get here, the whole TSO packet has not been acked. */
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003095static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096{
3097 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003098 u32 packets_acked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003100 BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 packets_acked = tcp_skb_pcount(skb);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003103 if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return 0;
3105 packets_acked -= tcp_skb_pcount(skb);
3106
3107 if (packets_acked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 BUG_ON(tcp_skb_pcount(skb) == 0);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003109 BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 }
3111
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003112 return packets_acked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
Eric Dumazetad971f62014-10-11 15:17:29 -07003115static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb,
3116 u32 prior_snd_una)
3117{
3118 const struct skb_shared_info *shinfo;
3119
3120 /* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */
Soheil Hassas Yeganeh6b084922016-04-02 23:08:08 -04003121 if (likely(!TCP_SKB_CB(skb)->txstamp_ack))
Eric Dumazetad971f62014-10-11 15:17:29 -07003122 return;
3123
3124 shinfo = skb_shinfo(skb);
Soheil Hassas Yeganeh0a2cf202016-04-27 23:39:01 -04003125 if (!before(shinfo->tskey, prior_snd_una) &&
Martin KaFai Lau479f85c2016-04-18 15:39:53 -07003126 before(shinfo->tskey, tcp_sk(sk)->snd_una))
Eric Dumazetad971f62014-10-11 15:17:29 -07003127 __skb_tstamp_tx(skb, NULL, sk, SCM_TSTAMP_ACK);
3128}
3129
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003130/* Remove acknowledged frames from the retransmission queue. If our packet
3131 * is before the ack sequence we can discard it as it's confirmed to have
3132 * arrived at the other end.
3133 */
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07003134static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
Yuchung Cheng3ebd88712016-02-02 10:33:07 -08003135 u32 prior_snd_una, int *acked,
Yuchung Chengb9f64822016-09-19 23:39:14 -04003136 struct tcp_sacktag_state *sack,
3137 struct skb_mstamp *now)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Stephen Hemminger2d2abba2005-11-10 16:56:12 -08003139 const struct inet_connection_sock *icsk = inet_csk(sk);
Yuchung Chengb9f64822016-09-19 23:39:14 -04003140 struct skb_mstamp first_ackt, last_ackt;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003141 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003142 u32 prior_sacked = tp->sacked_out;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003143 u32 reord = tp->packets_out;
3144 bool fully_acked = true;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003145 long sack_rtt_us = -1L;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003146 long seq_rtt_us = -1L;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003147 long ca_rtt_us = -1L;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003148 struct sk_buff *skb;
3149 u32 pkts_acked = 0;
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003150 u32 last_in_flight = 0;
Yuchung Cheng2f715c12013-10-24 08:59:27 -07003151 bool rtt_update;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003152 int flag = 0;
3153
3154 first_ackt.v64 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003156 while ((skb = tcp_write_queue_head(sk)) && skb != tcp_send_head(sk)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003157 struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003158 u8 sacked = scb->sacked;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003159 u32 acked_pcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Eric Dumazetad971f62014-10-11 15:17:29 -07003161 tcp_ack_tstamp(sk, skb, prior_snd_una);
Willem de Bruijn712a7222014-08-12 14:53:16 -04003162
Gavin McCullagh2072c222007-12-29 19:11:21 -08003163 /* Determine how many packets and what bytes were acked, tso and else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if (after(scb->end_seq, tp->snd_una)) {
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003165 if (tcp_skb_pcount(skb) == 1 ||
3166 !after(tp->snd_una, scb->seq))
3167 break;
3168
Ilpo Järvinen72018832007-12-30 04:37:55 -08003169 acked_pcount = tcp_tso_acked(sk, skb);
3170 if (!acked_pcount)
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003171 break;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003172 fully_acked = false;
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003173 } else {
Eric Dumazetad971f62014-10-11 15:17:29 -07003174 /* Speedup tcp_unlink_write_queue() and next loop */
3175 prefetchw(skb->next);
Ilpo Järvinen72018832007-12-30 04:37:55 -08003176 acked_pcount = tcp_skb_pcount(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
3178
Eric Dumazetad971f62014-10-11 15:17:29 -07003179 if (unlikely(sacked & TCPCB_RETRANS)) {
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003180 if (sacked & TCPCB_SACKED_RETRANS)
Ilpo Järvinen72018832007-12-30 04:37:55 -08003181 tp->retrans_out -= acked_pcount;
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003182 flag |= FLAG_RETRANS_DATA_ACKED;
Kenneth Klette Jonassen3d0d26c2015-04-11 02:17:49 +02003183 } else if (!(sacked & TCPCB_SACKED_ACKED)) {
Eric Dumazet740b0f12014-02-26 14:02:48 -08003184 last_ackt = skb->skb_mstamp;
Eric Dumazet431a9122014-03-09 17:36:02 -07003185 WARN_ON_ONCE(last_ackt.v64 == 0);
Eric Dumazet740b0f12014-02-26 14:02:48 -08003186 if (!first_ackt.v64)
3187 first_ackt = last_ackt;
3188
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003189 last_in_flight = TCP_SKB_CB(skb)->tx.in_flight;
Kenneth Klette Jonassen3d0d26c2015-04-11 02:17:49 +02003190 reord = min(pkts_acked, reord);
3191 if (!after(scb->end_seq, tp->high_seq))
3192 flag |= FLAG_ORIG_SACK_ACKED;
Stephen Hemminger2d2abba2005-11-10 16:56:12 -08003193 }
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003194
Yuchung Chengddf1af62016-02-02 10:33:06 -08003195 if (sacked & TCPCB_SACKED_ACKED) {
Ilpo Järvinen72018832007-12-30 04:37:55 -08003196 tp->sacked_out -= acked_pcount;
Yuchung Chengddf1af62016-02-02 10:33:06 -08003197 } else if (tcp_is_sack(tp)) {
3198 tp->delivered += acked_pcount;
3199 if (!tcp_skb_spurious_retrans(tp, skb))
3200 tcp_rack_advance(tp, &skb->skb_mstamp, sacked);
3201 }
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003202 if (sacked & TCPCB_LOST)
Ilpo Järvinen72018832007-12-30 04:37:55 -08003203 tp->lost_out -= acked_pcount;
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003204
Ilpo Järvinen72018832007-12-30 04:37:55 -08003205 tp->packets_out -= acked_pcount;
3206 pkts_acked += acked_pcount;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003207 tcp_rate_skb_delivered(sk, skb, sack->rate);
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003208
Ilpo Järvinen009a2e32007-09-20 11:34:38 -07003209 /* Initial outgoing SYN's get put onto the write_queue
3210 * just like anything else we transmit. It is not
3211 * true data, and if we misinform our callers that
3212 * this ACK acks real data, we will erroneously exit
3213 * connection startup slow start one packet too
3214 * quickly. This is severely frowned upon behavior.
3215 */
Eric Dumazetad971f62014-10-11 15:17:29 -07003216 if (likely(!(scb->tcp_flags & TCPHDR_SYN))) {
Ilpo Järvinen009a2e32007-09-20 11:34:38 -07003217 flag |= FLAG_DATA_ACKED;
3218 } else {
3219 flag |= FLAG_SYN_ACKED;
3220 tp->retrans_stamp = 0;
3221 }
3222
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003223 if (!fully_acked)
3224 break;
3225
David S. Millerfe067e82007-03-07 12:12:44 -08003226 tcp_unlink_write_queue(skb, sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08003227 sk_wmem_free_skb(sk, skb);
Eric Dumazetad971f62014-10-11 15:17:29 -07003228 if (unlikely(skb == tp->retransmit_skb_hint))
Ilpo Järvinenef9da472008-09-20 21:25:15 -07003229 tp->retransmit_skb_hint = NULL;
Eric Dumazetad971f62014-10-11 15:17:29 -07003230 if (unlikely(skb == tp->lost_skb_hint))
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003231 tp->lost_skb_hint = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 }
3233
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07003234 if (likely(between(tp->snd_up, prior_snd_una, tp->snd_una)))
3235 tp->snd_up = tp->snd_una;
3236
Ilpo Järvinencadbd032007-12-31 04:49:21 -08003237 if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
3238 flag |= FLAG_SACK_RENEGING;
3239
Yuchung Cheng9e45a3e2015-10-16 21:57:41 -07003240 if (likely(first_ackt.v64) && !(flag & FLAG_RETRANS_DATA_ACKED)) {
Yuchung Chengb9f64822016-09-19 23:39:14 -04003241 seq_rtt_us = skb_mstamp_us_delta(now, &first_ackt);
3242 ca_rtt_us = skb_mstamp_us_delta(now, &last_ackt);
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003243 }
3244 if (sack->first_sackt.v64) {
Yuchung Chengb9f64822016-09-19 23:39:14 -04003245 sack_rtt_us = skb_mstamp_us_delta(now, &sack->first_sackt);
3246 ca_rtt_us = skb_mstamp_us_delta(now, &sack->last_sackt);
Eric Dumazet740b0f12014-02-26 14:02:48 -08003247 }
Yuchung Chengb9f64822016-09-19 23:39:14 -04003248 sack->rate->rtt_us = ca_rtt_us; /* RTT of last (S)ACKed packet, or -1 */
Yuchung Chengf6722582015-10-16 21:57:42 -07003249 rtt_update = tcp_ack_update_rtt(sk, flag, seq_rtt_us, sack_rtt_us,
3250 ca_rtt_us);
Yuchung Chenged084952013-07-22 16:20:48 -07003251
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003252 if (flag & FLAG_ACKED) {
Yuchung Cheng2f715c12013-10-24 08:59:27 -07003253 tcp_rearm_rto(sk);
Ilpo Järvinen72211e92009-03-14 14:23:04 +00003254 if (unlikely(icsk->icsk_mtup.probe_size &&
3255 !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {
3256 tcp_mtup_probe_success(sk);
3257 }
3258
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003259 if (tcp_is_reno(tp)) {
3260 tcp_remove_reno_sacks(sk, pkts_acked);
Ilpo Järvinen65fb77c2018-06-29 13:07:53 +03003261
3262 /* If any of the cumulatively ACKed segments was
3263 * retransmitted, non-SACK case cannot confirm that
3264 * progress was due to original transmission due to
3265 * lack of TCPCB_SACKED_ACKED bits even if some of
3266 * the packets may have been never retransmitted.
3267 */
3268 if (flag & FLAG_RETRANS_DATA_ACKED)
3269 flag &= ~FLAG_ORIG_SACK_ACKED;
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003270 } else {
Ilpo Järvinen59a08cb2009-02-28 04:44:28 +00003271 int delta;
3272
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003273 /* Non-retransmitted hole got filled? That's reordering */
Soheil Hassas Yeganeh0174b072017-05-15 17:05:47 -04003274 if (reord < prior_fackets && reord <= tp->fackets_out)
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003275 tcp_update_reordering(sk, tp->fackets_out - reord, 0);
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003276
Ilpo Järvinen59a08cb2009-02-28 04:44:28 +00003277 delta = tcp_is_fack(tp) ? pkts_acked :
3278 prior_sacked - tp->sacked_out;
3279 tp->lost_cnt_hint -= min(tp->lost_cnt_hint, delta);
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003280 }
3281
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -07003282 tp->fackets_out -= min(pkts_acked, tp->fackets_out);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08003283
Eric Dumazet740b0f12014-02-26 14:02:48 -08003284 } else if (skb && rtt_update && sack_rtt_us >= 0 &&
Yuchung Chengb9f64822016-09-19 23:39:14 -04003285 sack_rtt_us > skb_mstamp_us_delta(now, &skb->skb_mstamp)) {
Yuchung Cheng2f715c12013-10-24 08:59:27 -07003286 /* Do not re-arm RTO if the sack RTT is measured from data sent
3287 * after when the head was last (re)transmitted. Otherwise the
3288 * timeout may continue to extend in loss recovery.
3289 */
3290 tcp_rearm_rto(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 }
3292
Lawrence Brakmo756ee172016-05-11 10:02:13 -07003293 if (icsk->icsk_ca_ops->pkts_acked) {
3294 struct ack_sample sample = { .pkts_acked = pkts_acked,
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003295 .rtt_us = ca_rtt_us,
3296 .in_flight = last_in_flight };
Lawrence Brakmo756ee172016-05-11 10:02:13 -07003297
3298 icsk->icsk_ca_ops->pkts_acked(sk, &sample);
3299 }
Kenneth Klette Jonassen138998f2015-05-01 01:10:59 +02003300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301#if FASTRETRANS_DEBUG > 0
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003302 WARN_ON((int)tp->sacked_out < 0);
3303 WARN_ON((int)tp->lost_out < 0);
3304 WARN_ON((int)tp->retrans_out < 0);
Ilpo Järvinene60402d2007-08-09 15:14:46 +03003305 if (!tp->packets_out && tcp_is_sack(tp)) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07003306 icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 if (tp->lost_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003308 pr_debug("Leak l=%u %d\n",
3309 tp->lost_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 tp->lost_out = 0;
3311 }
3312 if (tp->sacked_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003313 pr_debug("Leak s=%u %d\n",
3314 tp->sacked_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 tp->sacked_out = 0;
3316 }
3317 if (tp->retrans_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003318 pr_debug("Leak r=%u %d\n",
3319 tp->retrans_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 tp->retrans_out = 0;
3321 }
3322 }
3323#endif
Yuchung Cheng3ebd88712016-02-02 10:33:07 -08003324 *acked = pkts_acked;
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003325 return flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
3328static void tcp_ack_probe(struct sock *sk)
3329{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003330 const struct tcp_sock *tp = tcp_sk(sk);
3331 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
3333 /* Was it a usable window open? */
3334
Ilpo Järvinen90840de2007-12-31 04:48:41 -08003335 if (!after(TCP_SKB_CB(tcp_send_head(sk))->end_seq, tcp_wnd_end(tp))) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003336 icsk->icsk_backoff = 0;
3337 inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 /* Socket must be waked up by subsequent tcp_data_snd_check().
3339 * This function is not for random using!
3340 */
3341 } else {
Eric Dumazet21c8fe92015-05-06 14:26:24 -07003342 unsigned long when = tcp_probe0_when(sk, TCP_RTO_MAX);
Eric Dumazetfcdd1cf2014-09-22 13:19:44 -07003343
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003344 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
Eric Dumazetfcdd1cf2014-09-22 13:19:44 -07003345 when, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 }
3347}
3348
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003349static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
Eric Dumazeta02cec22010-09-22 20:43:57 +00003351 return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||
3352 inet_csk(sk)->icsk_ca_state != TCP_CA_Open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003355/* Decide wheather to run the increase function of congestion control. */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003356static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003358 /* If reordering is high then always grow cwnd whenever data is
3359 * delivered regardless of its ordering. Otherwise stay conservative
Yuchung Cheng16edfe72013-09-05 15:36:09 -07003360 * and only grow cwnd on in-order delivery (RFC5681). A stretched ACK w/
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003361 * new SACK or ECE mark may first advance cwnd here and later reduce
3362 * cwnd in tcp_fastretrans_alert() based on more states.
3363 */
Nikolay Borisov1043e252016-02-03 09:46:52 +02003364 if (tcp_sk(sk)->reordering > sock_net(sk)->ipv4.sysctl_tcp_reordering)
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003365 return flag & FLAG_FORWARD_PROGRESS;
3366
Yuchung Cheng16edfe72013-09-05 15:36:09 -07003367 return flag & FLAG_DATA_ACKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368}
3369
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003370/* The "ultimate" congestion control function that aims to replace the rigid
3371 * cwnd increase and decrease control (tcp_cong_avoid,tcp_*cwnd_reduction).
3372 * It's called toward the end of processing an ACK with precise rate
3373 * information. All transmission or retransmission are delayed afterwards.
3374 */
3375static void tcp_cong_control(struct sock *sk, u32 ack, u32 acked_sacked,
Yuchung Chengc0402762016-09-19 23:39:21 -04003376 int flag, const struct rate_sample *rs)
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003377{
Yuchung Chengc0402762016-09-19 23:39:21 -04003378 const struct inet_connection_sock *icsk = inet_csk(sk);
3379
3380 if (icsk->icsk_ca_ops->cong_control) {
3381 icsk->icsk_ca_ops->cong_control(sk, rs);
3382 return;
3383 }
3384
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003385 if (tcp_in_cwnd_reduction(sk)) {
3386 /* Reduce cwnd if state mandates */
3387 tcp_cwnd_reduction(sk, acked_sacked, flag);
3388 } else if (tcp_may_raise_cwnd(sk, flag)) {
3389 /* Advance cwnd if state allows */
3390 tcp_cong_avoid(sk, ack, acked_sacked);
3391 }
3392 tcp_update_pacing_rate(sk);
3393}
3394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395/* Check that window update is acceptable.
3396 * The function assumes that snd_una<=ack<=snd_next.
3397 */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003398static inline bool tcp_may_update_window(const struct tcp_sock *tp,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003399 const u32 ack, const u32 ack_seq,
3400 const u32 nwin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401{
Eric Dumazeta02cec22010-09-22 20:43:57 +00003402 return after(ack, tp->snd_una) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 after(ack_seq, tp->snd_wl1) ||
Eric Dumazeta02cec22010-09-22 20:43:57 +00003404 (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405}
3406
Eric Dumazet0df48c22015-04-28 15:28:17 -07003407/* If we update tp->snd_una, also update tp->bytes_acked */
3408static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
3409{
3410 u32 delta = ack - tp->snd_una;
3411
Eric Dumazet46cc6e42016-05-03 16:56:03 -07003412 sock_owned_by_me((struct sock *)tp);
3413 u64_stats_update_begin_raw(&tp->syncp);
Eric Dumazet0df48c22015-04-28 15:28:17 -07003414 tp->bytes_acked += delta;
Eric Dumazet46cc6e42016-05-03 16:56:03 -07003415 u64_stats_update_end_raw(&tp->syncp);
Eric Dumazet0df48c22015-04-28 15:28:17 -07003416 tp->snd_una = ack;
3417}
3418
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07003419/* If we update tp->rcv_nxt, also update tp->bytes_received */
3420static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
3421{
3422 u32 delta = seq - tp->rcv_nxt;
3423
Eric Dumazet46cc6e42016-05-03 16:56:03 -07003424 sock_owned_by_me((struct sock *)tp);
3425 u64_stats_update_begin_raw(&tp->syncp);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07003426 tp->bytes_received += delta;
Eric Dumazet46cc6e42016-05-03 16:56:03 -07003427 u64_stats_update_end_raw(&tp->syncp);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07003428 tp->rcv_nxt = seq;
3429}
3430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431/* Update our send window.
3432 *
3433 * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
3434 * and in FreeBSD. NetBSD's one is even worse.) is wrong.
3435 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003436static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003437 u32 ack_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003439 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 int flag = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07003441 u32 nwin = ntohs(tcp_hdr(skb)->window);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07003443 if (likely(!tcp_hdr(skb)->syn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 nwin <<= tp->rx_opt.snd_wscale;
3445
3446 if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {
3447 flag |= FLAG_WIN_UPDATE;
Hantzis Fotisee7537b2009-03-02 22:42:02 -08003448 tcp_update_wl(tp, ack_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 if (tp->snd_wnd != nwin) {
3451 tp->snd_wnd = nwin;
3452
3453 /* Note, it is the only place, where
3454 * fast path is recovered for sending TCP.
3455 */
Herbert Xu2ad41062005-10-27 18:47:46 +10003456 tp->pred_flags = 0;
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003457 tcp_fast_path_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Eric Dumazet6f021c62015-08-21 12:30:00 -07003459 if (tcp_send_head(sk))
3460 tcp_slow_start_after_idle_check(sk);
3461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (nwin > tp->max_window) {
3463 tp->max_window = nwin;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08003464 tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 }
3466 }
3467 }
3468
Eric Dumazet0df48c22015-04-28 15:28:17 -07003469 tcp_snd_una_update(tp, ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 return flag;
3472}
3473
Jason Baron083ae302016-07-14 11:38:40 -04003474static bool __tcp_oow_rate_limited(struct net *net, int mib_idx,
3475 u32 *last_oow_ack_time)
3476{
3477 if (*last_oow_ack_time) {
3478 s32 elapsed = (s32)(tcp_time_stamp - *last_oow_ack_time);
3479
3480 if (0 <= elapsed && elapsed < sysctl_tcp_invalid_ratelimit) {
3481 NET_INC_STATS(net, mib_idx);
3482 return true; /* rate-limited: don't send yet! */
3483 }
3484 }
3485
3486 *last_oow_ack_time = tcp_time_stamp;
3487
3488 return false; /* not rate-limited: go ahead, send dupack now! */
3489}
3490
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003491/* Return true if we're currently rate-limiting out-of-window ACKs and
3492 * thus shouldn't send a dupack right now. We rate-limit dupacks in
3493 * response to out-of-window SYNs or ACKs to mitigate ACK loops or DoS
3494 * attacks that send repeated SYNs or ACKs for the same connection. To
3495 * do this, we do not send a duplicate SYNACK or ACK if the remote
3496 * endpoint is sending out-of-window SYNs or pure ACKs at a high rate.
3497 */
3498bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
3499 int mib_idx, u32 *last_oow_ack_time)
3500{
3501 /* Data packets without SYNs are not likely part of an ACK loop. */
3502 if ((TCP_SKB_CB(skb)->seq != TCP_SKB_CB(skb)->end_seq) &&
3503 !tcp_hdr(skb)->syn)
Jason Baron083ae302016-07-14 11:38:40 -04003504 return false;
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003505
Jason Baron083ae302016-07-14 11:38:40 -04003506 return __tcp_oow_rate_limited(net, mib_idx, last_oow_ack_time);
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003507}
3508
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003509/* RFC 5961 7 [ACK Throttling] */
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003510static void tcp_send_challenge_ack(struct sock *sk, const struct sk_buff *skb)
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003511{
3512 /* unprotected vars, we dont care of overwrites */
3513 static u32 challenge_timestamp;
3514 static unsigned int challenge_count;
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003515 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003516 u32 count, now;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003517
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003518 /* First check our per-socket dupack rate limit. */
Jason Baron083ae302016-07-14 11:38:40 -04003519 if (__tcp_oow_rate_limited(sock_net(sk),
3520 LINUX_MIB_TCPACKSKIPPEDCHALLENGE,
3521 &tp->last_oow_ack_time))
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003522 return;
3523
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003524 /* Then check host-wide RFC 5961 rate limit. */
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003525 now = jiffies / HZ;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003526 if (now != challenge_timestamp) {
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003527 u32 half = (sysctl_tcp_challenge_ack_limit + 1) >> 1;
3528
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003529 challenge_timestamp = now;
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003530 WRITE_ONCE(challenge_count, half +
3531 prandom_u32_max(sysctl_tcp_challenge_ack_limit));
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003532 }
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003533 count = READ_ONCE(challenge_count);
3534 if (count > 0) {
3535 WRITE_ONCE(challenge_count, count - 1);
Eric Dumazetc10d9312016-04-29 14:16:47 -07003536 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPCHALLENGEACK);
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003537 tcp_send_ack(sk);
3538 }
3539}
3540
Eric Dumazet12fb3dd2013-04-19 07:19:48 +00003541static void tcp_store_ts_recent(struct tcp_sock *tp)
3542{
3543 tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
3544 tp->rx_opt.ts_recent_stamp = get_seconds();
3545}
3546
3547static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
3548{
3549 if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) {
3550 /* PAWS bug workaround wrt. ACK frames, the PAWS discard
3551 * extra check below makes sure this can only happen
3552 * for pure ACK frames. -DaveM
3553 *
3554 * Not only, also it occurs for expired timestamps.
3555 */
3556
3557 if (tcp_paws_check(&tp->rx_opt, 0))
3558 tcp_store_ts_recent(tp);
3559 }
3560}
3561
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003562/* This routine deals with acks during a TLP episode.
Sébastien Barré08abdff2015-01-12 10:30:40 +01003563 * We mark the end of a TLP episode on receiving TLP dupack or when
3564 * ack is after tlp_high_seq.
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003565 * Ref: loss detection algorithm in draft-dukkipati-tcpm-tcp-loss-probe.
3566 */
3567static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
3568{
3569 struct tcp_sock *tp = tcp_sk(sk);
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003570
Sébastien Barré08abdff2015-01-12 10:30:40 +01003571 if (before(ack, tp->tlp_high_seq))
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003572 return;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003573
Sébastien Barré08abdff2015-01-12 10:30:40 +01003574 if (flag & FLAG_DSACKING_ACK) {
3575 /* This DSACK means original and TLP probe arrived; no loss */
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003576 tp->tlp_high_seq = 0;
Sébastien Barré08abdff2015-01-12 10:30:40 +01003577 } else if (after(ack, tp->tlp_high_seq)) {
3578 /* ACK advances: there was a loss, so reduce cwnd. Reset
3579 * tlp_high_seq in tcp_init_cwnd_reduction()
3580 */
3581 tcp_init_cwnd_reduction(sk);
3582 tcp_set_ca_state(sk, TCP_CA_CWR);
3583 tcp_end_cwnd_reduction(sk);
3584 tcp_try_keep_open(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07003585 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07003586 LINUX_MIB_TCPLOSSPROBERECOVERY);
Sébastien Barré08abdff2015-01-12 10:30:40 +01003587 } else if (!(flag & (FLAG_SND_UNA_ADVANCED |
3588 FLAG_NOT_DUP | FLAG_DATA_SACKED))) {
3589 /* Pure dupack: original and TLP probe arrived; no loss */
3590 tp->tlp_high_seq = 0;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003591 }
3592}
3593
Florian Westphal7354c8c2014-09-26 22:37:34 +02003594static inline void tcp_in_ack_event(struct sock *sk, u32 flags)
3595{
3596 const struct inet_connection_sock *icsk = inet_csk(sk);
3597
3598 if (icsk->icsk_ca_ops->in_ack_event)
3599 icsk->icsk_ca_ops->in_ack_event(sk, flags);
3600}
3601
Yuchung Chenge662ca42016-02-02 10:33:04 -08003602/* Congestion control has updated the cwnd already. So if we're in
3603 * loss recovery then now we do any new sends (for FRTO) or
3604 * retransmits (for CA_Loss or CA_recovery) that make sense.
3605 */
3606static void tcp_xmit_recovery(struct sock *sk, int rexmit)
3607{
3608 struct tcp_sock *tp = tcp_sk(sk);
3609
3610 if (rexmit == REXMIT_NONE)
3611 return;
3612
3613 if (unlikely(rexmit == 2)) {
3614 __tcp_push_pending_frames(sk, tcp_current_mss(sk),
3615 TCP_NAGLE_OFF);
3616 if (after(tp->snd_nxt, tp->high_seq))
3617 return;
3618 tp->frto = 0;
3619 }
3620 tcp_xmit_retransmit_queue(sk);
3621}
3622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623/* This routine deals with incoming acks, but not outgoing ones. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003624static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003626 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 struct tcp_sock *tp = tcp_sk(sk);
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003628 struct tcp_sacktag_state sack_state;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003629 struct rate_sample rs = { .prior_delivered = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 u32 prior_snd_una = tp->snd_una;
Yousuk Seunge74fe722017-12-07 13:41:34 -08003631 bool is_sack_reneg = tp->is_sack_reneg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 u32 ack_seq = TCP_SKB_CB(skb)->seq;
3633 u32 ack = TCP_SKB_CB(skb)->ack_seq;
Neal Cardwell7d2b55f2011-11-16 08:58:01 +00003634 bool is_dupack = false;
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003635 u32 prior_fackets;
Nandita Dukkipati35f079e2013-05-21 15:12:07 +00003636 int prior_packets = tp->packets_out;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003637 u32 delivered = tp->delivered;
3638 u32 lost = tp->lost;
Yuchung Cheng68049732013-05-29 14:20:11 +00003639 int acked = 0; /* Number of packets newly acked */
Yuchung Chenge662ca42016-02-02 10:33:04 -08003640 int rexmit = REXMIT_NONE; /* Flag to (re)transmit to recover losses */
Yuchung Chengb9f64822016-09-19 23:39:14 -04003641 struct skb_mstamp now;
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003642
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003643 sack_state.first_sackt.v64 = 0;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003644 sack_state.rate = &rs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Eric Dumazetad971f62014-10-11 15:17:29 -07003646 /* We very likely will need to access write queue head. */
3647 prefetchw(sk->sk_write_queue.next);
3648
John Dykstra96e0bf42009-03-22 21:49:57 -07003649 /* If the ack is older than previous acks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 * then we can probably ignore it.
3651 */
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003652 if (before(ack, prior_snd_una)) {
3653 /* RFC 5961 5.2 [Blind Data Injection Attack].[Mitigation] */
3654 if (before(ack, prior_snd_una - tp->max_window)) {
Eric Dumazet71c0b332017-05-23 15:24:46 -07003655 if (!(flag & FLAG_NO_CHALLENGE_ACK))
3656 tcp_send_challenge_ack(sk, skb);
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003657 return -1;
3658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 goto old_ack;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
John Dykstra96e0bf42009-03-22 21:49:57 -07003662 /* If the ack includes data we haven't sent yet, discard
3663 * this segment (RFC793 Section 3.9).
3664 */
3665 if (after(ack, tp->snd_nxt))
3666 goto invalid_ack;
3667
Yuchung Chengb9f64822016-09-19 23:39:14 -04003668 skb_mstamp_get(&now);
3669
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003670 if (icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
3671 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE)
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003672 tcp_rearm_rto(sk);
3673
Neal Cardwell0c9ab092014-08-14 16:13:07 -04003674 if (after(ack, prior_snd_una)) {
Ilpo Järvinen2e605292007-08-02 19:46:58 -07003675 flag |= FLAG_SND_UNA_ADVANCED;
Neal Cardwell0c9ab092014-08-14 16:13:07 -04003676 icsk->icsk_retransmits = 0;
3677 }
Ilpo Järvinen2e605292007-08-02 19:46:58 -07003678
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003679 prior_fackets = tp->fackets_out;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003680 rs.prior_in_flight = tcp_packets_in_flight(tp);
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003681
Eric Dumazet12fb3dd2013-04-19 07:19:48 +00003682 /* ts_recent update must be made after we are sure that the packet
3683 * is in window.
3684 */
3685 if (flag & FLAG_UPDATE_TS_RECENT)
3686 tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
3687
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003688 if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 /* Window is constant, pure forward advance.
3690 * No more checks are required.
3691 * Note, we use the fact that SND.UNA>=SND.WL2.
3692 */
Hantzis Fotisee7537b2009-03-02 22:42:02 -08003693 tcp_update_wl(tp, ack_seq);
Eric Dumazet0df48c22015-04-28 15:28:17 -07003694 tcp_snd_una_update(tp, ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 flag |= FLAG_WIN_UPDATE;
3696
Florian Westphal98900922014-09-26 22:37:35 +02003697 tcp_in_ack_event(sk, CA_ACK_WIN_UPDATE);
Stephen Hemminger317a76f2005-06-23 12:19:55 -07003698
Eric Dumazetc10d9312016-04-29 14:16:47 -07003699 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 } else {
Florian Westphal98900922014-09-26 22:37:35 +02003701 u32 ack_ev_flags = CA_ACK_SLOWPATH;
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 if (ack_seq != TCP_SKB_CB(skb)->end_seq)
3704 flag |= FLAG_DATA;
3705 else
Eric Dumazetc10d9312016-04-29 14:16:47 -07003706 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPUREACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003708 flag |= tcp_ack_update_window(sk, skb, ack, ack_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710 if (TCP_SKB_CB(skb)->sacked)
Yuchung Cheng59c9af42013-07-22 16:20:47 -07003711 flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003712 &sack_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
Florian Westphal735d3832014-09-29 13:08:30 +02003714 if (tcp_ecn_rcv_ecn_echo(tp, tcp_hdr(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 flag |= FLAG_ECE;
Florian Westphal98900922014-09-26 22:37:35 +02003716 ack_ev_flags |= CA_ACK_ECE;
3717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Florian Westphal98900922014-09-26 22:37:35 +02003719 if (flag & FLAG_WIN_UPDATE)
3720 ack_ev_flags |= CA_ACK_WIN_UPDATE;
3721
3722 tcp_in_ack_event(sk, ack_ev_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 }
3724
3725 /* We passed data and got it acked, remove any soft error
3726 * log. Something worked...
3727 */
3728 sk->sk_err_soft = 0;
David S. Miller4b53fb62008-07-23 16:38:45 -07003729 icsk->icsk_probes_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 tp->rcv_tstamp = tcp_time_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 if (!prior_packets)
3732 goto no_queue;
3733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 /* See if we can take anything off of the retransmit queue. */
Yuchung Cheng3ebd88712016-02-02 10:33:07 -08003735 flag |= tcp_clean_rtx_queue(sk, prior_fackets, prior_snd_una, &acked,
Yuchung Chengb9f64822016-09-19 23:39:14 -04003736 &sack_state, &now);
Nandita Dukkipatia262f0c2011-08-21 20:21:57 +00003737
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003738 if (tcp_ack_is_dubious(sk, flag)) {
Neal Cardwell7d2b55f2011-11-16 08:58:01 +00003739 is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP));
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08003740 tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 }
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003742 if (tp->tlp_high_seq)
3743 tcp_process_tlp_ack(sk, ack, flag);
3744
David S. Miller5110effe2012-07-02 02:21:03 -07003745 if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP)) {
3746 struct dst_entry *dst = __sk_dst_get(sk);
3747 if (dst)
3748 dst_confirm(dst);
3749 }
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003750
3751 if (icsk->icsk_pending == ICSK_TIME_RETRANS)
3752 tcp_schedule_loss_probe(sk);
Yuchung Chengb9f64822016-09-19 23:39:14 -04003753 delivered = tp->delivered - delivered; /* freshly ACKed or SACKed */
3754 lost = tp->lost - lost; /* freshly marked lost */
Yousuk Seunge74fe722017-12-07 13:41:34 -08003755 tcp_rate_gen(sk, delivered, lost, is_sack_reneg, &now, &rs);
Yuchung Chengc0402762016-09-19 23:39:21 -04003756 tcp_cong_control(sk, ack, delivered, flag, &rs);
Yuchung Chenge662ca42016-02-02 10:33:04 -08003757 tcp_xmit_recovery(sk, rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 return 1;
3759
3760no_queue:
Neal Cardwell5628adf2011-11-16 08:58:02 +00003761 /* If data was DSACKed, see if we can undo a cwnd reduction. */
3762 if (flag & FLAG_DSACKING_ACK)
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08003763 tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 /* If this ack opens up a zero window, clear backoff. It was
3765 * being used to time the probes, and is probably far higher than
3766 * it needs to be for normal retransmission.
3767 */
David S. Millerfe067e82007-03-07 12:12:44 -08003768 if (tcp_send_head(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 tcp_ack_probe(sk);
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003770
3771 if (tp->tlp_high_seq)
3772 tcp_process_tlp_ack(sk, ack, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 return 1;
3774
John Dykstra96e0bf42009-03-22 21:49:57 -07003775invalid_ack:
3776 SOCK_DEBUG(sk, "Ack %u after %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
3777 return -1;
3778
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779old_ack:
Neal Cardwelle95ae2f2011-11-16 08:58:03 +00003780 /* If data was SACKed, tag it and see if we should send more data.
3781 * If data was DSACKed, see if we can undo a cwnd reduction.
3782 */
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07003783 if (TCP_SKB_CB(skb)->sacked) {
Yuchung Cheng59c9af42013-07-22 16:20:47 -07003784 flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003785 &sack_state);
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08003786 tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
Yuchung Chenge662ca42016-02-02 10:33:04 -08003787 tcp_xmit_recovery(sk, rexmit);
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07003788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
John Dykstra96e0bf42009-03-22 21:49:57 -07003790 SOCK_DEBUG(sk, "Ack %u before %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return 0;
3792}
3793
Daniel Lee7f9b8382015-04-06 14:37:26 -07003794static void tcp_parse_fastopen_option(int len, const unsigned char *cookie,
3795 bool syn, struct tcp_fastopen_cookie *foc,
3796 bool exp_opt)
3797{
3798 /* Valid only in SYN or SYN-ACK with an even length. */
3799 if (!foc || !syn || len < 0 || (len & 1))
3800 return;
3801
3802 if (len >= TCP_FASTOPEN_COOKIE_MIN &&
3803 len <= TCP_FASTOPEN_COOKIE_MAX)
3804 memcpy(foc->val, cookie, len);
3805 else if (len != 0)
3806 len = -1;
3807 foc->len = len;
3808 foc->exp = exp_opt;
3809}
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811/* Look for tcp options. Normally only called on SYN and SYNACK packets.
3812 * But, this can also be called on packets in the established flow when
3813 * the fast version below fails.
3814 */
Christoph Paasch1a2c6182013-03-17 08:23:34 +00003815void tcp_parse_options(const struct sk_buff *skb,
3816 struct tcp_options_received *opt_rx, int estab,
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003817 struct tcp_fastopen_cookie *foc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003819 const unsigned char *ptr;
3820 const struct tcphdr *th = tcp_hdr(skb);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003821 int length = (th->doff * 4) - sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003823 ptr = (const unsigned char *)(th + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 opt_rx->saw_tstamp = 0;
3825
Stephen Hemminger2de979b2007-03-08 20:45:19 -08003826 while (length > 0) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003827 int opcode = *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 int opsize;
3829
3830 switch (opcode) {
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003831 case TCPOPT_EOL:
3832 return;
3833 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
3834 length--;
3835 continue;
3836 default:
3837 opsize = *ptr++;
3838 if (opsize < 2) /* "silly options" */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 return;
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003840 if (opsize > length)
3841 return; /* don't parse partial options */
3842 switch (opcode) {
3843 case TCPOPT_MSS:
3844 if (opsize == TCPOLEN_MSS && th->syn && !estab) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07003845 u16 in_mss = get_unaligned_be16(ptr);
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003846 if (in_mss) {
3847 if (opt_rx->user_mss &&
3848 opt_rx->user_mss < in_mss)
3849 in_mss = opt_rx->user_mss;
3850 opt_rx->mss_clamp = in_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 }
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003852 }
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003853 break;
3854 case TCPOPT_WINDOW:
3855 if (opsize == TCPOLEN_WINDOW && th->syn &&
David S. Millerbb5b7c12009-12-15 20:56:42 -08003856 !estab && sysctl_tcp_window_scaling) {
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003857 __u8 snd_wscale = *(__u8 *)ptr;
3858 opt_rx->wscale_ok = 1;
3859 if (snd_wscale > 14) {
Joe Perchese87cc472012-05-13 21:56:26 +00003860 net_info_ratelimited("%s: Illegal window scaling value %d >14 received\n",
3861 __func__,
3862 snd_wscale);
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003863 snd_wscale = 14;
3864 }
3865 opt_rx->snd_wscale = snd_wscale;
3866 }
3867 break;
3868 case TCPOPT_TIMESTAMP:
3869 if ((opsize == TCPOLEN_TIMESTAMP) &&
3870 ((estab && opt_rx->tstamp_ok) ||
David S. Millerbb5b7c12009-12-15 20:56:42 -08003871 (!estab && sysctl_tcp_timestamps))) {
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003872 opt_rx->saw_tstamp = 1;
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07003873 opt_rx->rcv_tsval = get_unaligned_be32(ptr);
3874 opt_rx->rcv_tsecr = get_unaligned_be32(ptr + 4);
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003875 }
3876 break;
3877 case TCPOPT_SACK_PERM:
3878 if (opsize == TCPOLEN_SACK_PERM && th->syn &&
David S. Millerbb5b7c12009-12-15 20:56:42 -08003879 !estab && sysctl_tcp_sack) {
Vijay Subramanianab562222011-12-20 13:23:24 +00003880 opt_rx->sack_ok = TCP_SACK_SEEN;
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003881 tcp_sack_reset(opt_rx);
3882 }
3883 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003884
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003885 case TCPOPT_SACK:
3886 if ((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) &&
3887 !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) &&
3888 opt_rx->sack_ok) {
3889 TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th;
3890 }
3891 break;
3892#ifdef CONFIG_TCP_MD5SIG
3893 case TCPOPT_MD5SIG:
3894 /*
3895 * The MD5 Hash has already been
3896 * checked (see tcp_v{4,6}_do_rcv()).
3897 */
3898 break;
3899#endif
Daniel Lee7f9b8382015-04-06 14:37:26 -07003900 case TCPOPT_FASTOPEN:
3901 tcp_parse_fastopen_option(
3902 opsize - TCPOLEN_FASTOPEN_BASE,
3903 ptr, th->syn, foc, false);
3904 break;
3905
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003906 case TCPOPT_EXP:
3907 /* Fast Open option shares code 254 using a
Daniel Lee7f9b8382015-04-06 14:37:26 -07003908 * 16 bits magic number.
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003909 */
Daniel Lee7f9b8382015-04-06 14:37:26 -07003910 if (opsize >= TCPOLEN_EXP_FASTOPEN_BASE &&
3911 get_unaligned_be16(ptr) ==
3912 TCPOPT_FASTOPEN_MAGIC)
3913 tcp_parse_fastopen_option(opsize -
3914 TCPOLEN_EXP_FASTOPEN_BASE,
3915 ptr + 2, th->syn, foc, true);
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003916 break;
3917
3918 }
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003919 ptr += opsize-2;
3920 length -= opsize;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 }
3923}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00003924EXPORT_SYMBOL(tcp_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003926static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003927{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003928 const __be32 *ptr = (const __be32 *)(th + 1);
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003929
3930 if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
3931 | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
3932 tp->rx_opt.saw_tstamp = 1;
3933 ++ptr;
3934 tp->rx_opt.rcv_tsval = ntohl(*ptr);
3935 ++ptr;
Andrew Vagine3e12022013-08-27 12:21:55 +04003936 if (*ptr)
3937 tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset;
3938 else
3939 tp->rx_opt.rcv_tsecr = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003940 return true;
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003941 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003942 return false;
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003943}
3944
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945/* Fast parse options. This hopes to only see timestamps.
3946 * If it is wrong it falls back on tcp_parse_options().
3947 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003948static bool tcp_fast_parse_options(const struct sk_buff *skb,
Christoph Paasch1a2c6182013-03-17 08:23:34 +00003949 const struct tcphdr *th, struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
William Allen Simpson4957faade2009-12-02 18:25:27 +00003951 /* In the spirit of fast parsing, compare doff directly to constant
3952 * values. Because equality is used, short doff can be ignored here.
3953 */
3954 if (th->doff == (sizeof(*th) / 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 tp->rx_opt.saw_tstamp = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003956 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 } else if (tp->rx_opt.tstamp_ok &&
William Allen Simpson4957faade2009-12-02 18:25:27 +00003958 th->doff == ((sizeof(*th) + TCPOLEN_TSTAMP_ALIGNED) / 4)) {
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003959 if (tcp_parse_aligned_timestamp(tp, th))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003960 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 }
Andrey Vaginee684b62013-02-11 05:50:19 +00003962
Christoph Paasch1a2c6182013-03-17 08:23:34 +00003963 tcp_parse_options(skb, &tp->rx_opt, 1, NULL);
Andrew Vagine3e12022013-08-27 12:21:55 +04003964 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
Andrey Vaginee684b62013-02-11 05:50:19 +00003965 tp->rx_opt.rcv_tsecr -= tp->tsoffset;
3966
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003967 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968}
3969
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003970#ifdef CONFIG_TCP_MD5SIG
3971/*
3972 * Parse MD5 Signature option
3973 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003974const u8 *tcp_parse_md5sig_option(const struct tcphdr *th)
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003975{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003976 int length = (th->doff << 2) - sizeof(*th);
3977 const u8 *ptr = (const u8 *)(th + 1);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003978
Jann Hornf8382592018-04-20 15:57:30 +02003979 /* If not enough data remaining, we can short cut */
3980 while (length >= TCPOLEN_MD5SIG) {
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003981 int opcode = *ptr++;
3982 int opsize;
3983
Weilong Chena22318e2013-12-23 14:37:26 +08003984 switch (opcode) {
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003985 case TCPOPT_EOL:
3986 return NULL;
3987 case TCPOPT_NOP:
3988 length--;
3989 continue;
3990 default:
3991 opsize = *ptr++;
3992 if (opsize < 2 || opsize > length)
3993 return NULL;
3994 if (opcode == TCPOPT_MD5SIG)
Dmitry Popovba78e2d2010-08-07 20:24:28 -07003995 return opsize == TCPOLEN_MD5SIG ? ptr : NULL;
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003996 }
3997 ptr += opsize - 2;
3998 length -= opsize;
3999 }
4000 return NULL;
4001}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00004002EXPORT_SYMBOL(tcp_parse_md5sig_option);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09004003#endif
4004
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005/* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM
4006 *
4007 * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
4008 * it can pass through stack. So, the following predicate verifies that
4009 * this segment is not used for anything but congestion avoidance or
4010 * fast retransmit. Moreover, we even are able to eliminate most of such
4011 * second order effects, if we apply some small "replay" window (~RTO)
4012 * to timestamp space.
4013 *
4014 * All these measures still do not guarantee that we reject wrapped ACKs
4015 * on networks with high bandwidth, when sequence space is recycled fastly,
4016 * but it guarantees that such events will be very rare and do not affect
4017 * connection seriously. This doesn't look nice, but alas, PAWS is really
4018 * buggy extension.
4019 *
4020 * [ Later note. Even worse! It is buggy for segments _with_ data. RFC
4021 * states that events when retransmit arrives after original data are rare.
4022 * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is
4023 * the biggest problem on large power networks even with minor reordering.
4024 * OK, let's give it small replay window. If peer clock is even 1hz, it is safe
4025 * up to bandwidth of 18Gigabit/sec. 8) ]
4026 */
4027
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004028static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04004030 const struct tcp_sock *tp = tcp_sk(sk);
4031 const struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 u32 seq = TCP_SKB_CB(skb)->seq;
4033 u32 ack = TCP_SKB_CB(skb)->ack_seq;
4034
4035 return (/* 1. Pure ACK with correct sequence number. */
4036 (th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) &&
4037
4038 /* 2. ... and duplicate ACK. */
4039 ack == tp->snd_una &&
4040
4041 /* 3. ... and does not update window. */
4042 !tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) &&
4043
4044 /* 4. ... and sits in replay window. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004045 (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 -07004046}
4047
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00004048static inline bool tcp_paws_discard(const struct sock *sk,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004049 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004051 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinenc887e6d2009-03-14 14:23:03 +00004052
4053 return !tcp_paws_check(&tp->rx_opt, TCP_PAWS_WINDOW) &&
4054 !tcp_disordered_ack(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
4057/* Check segment sequence number for validity.
4058 *
4059 * Segment controls are considered valid, if the segment
4060 * fits to the window after truncation to the window. Acceptability
4061 * of data (and SYN, FIN, of course) is checked separately.
4062 * See tcp_data_queue(), for example.
4063 *
4064 * Also, controls (RST is main one) are accepted using RCV.WUP instead
4065 * of RCV.NXT. Peer still did not advance his SND.UNA when we
4066 * delayed ACK, so that hisSND.UNA<=ourRCV.WUP.
4067 * (borrowed from freebsd)
4068 */
4069
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00004070static inline bool tcp_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
4072 return !before(end_seq, tp->rcv_wup) &&
4073 !after(seq, tp->rcv_nxt + tcp_receive_window(tp));
4074}
4075
4076/* When we get a reset we do this. */
Jerry Chu10467162012-08-31 12:29:11 +00004077void tcp_reset(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
4079 /* We want the right error as BSD sees it (and indeed as we do). */
4080 switch (sk->sk_state) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004081 case TCP_SYN_SENT:
4082 sk->sk_err = ECONNREFUSED;
4083 break;
4084 case TCP_CLOSE_WAIT:
4085 sk->sk_err = EPIPE;
4086 break;
4087 case TCP_CLOSE:
4088 return;
4089 default:
4090 sk->sk_err = ECONNRESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 }
Tom Marshalla4d25802010-09-20 15:42:05 -07004092 /* This barrier is coupled with smp_rmb() in tcp_poll() */
4093 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
4095 if (!sock_flag(sk, SOCK_DEAD))
4096 sk->sk_error_report(sk);
4097
4098 tcp_done(sk);
4099}
4100
4101/*
4102 * Process the FIN bit. This now behaves as it is supposed to work
4103 * and the FIN takes effect when it is validly part of sequence
4104 * space. Not before when we get holes.
4105 *
4106 * If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
4107 * (and thence onto LAST-ACK and finally, CLOSE, we never enter
4108 * TIME-WAIT)
4109 *
4110 * If we are in FINWAIT-1, a received FIN indicates simultaneous
4111 * close and we go into CLOSING (and later onto TIME-WAIT)
4112 *
4113 * If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
4114 */
Eric Dumazete3e17b72016-02-06 11:16:28 -08004115void tcp_fin(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
4117 struct tcp_sock *tp = tcp_sk(sk);
4118
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004119 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
4121 sk->sk_shutdown |= RCV_SHUTDOWN;
4122 sock_set_flag(sk, SOCK_DONE);
4123
4124 switch (sk->sk_state) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004125 case TCP_SYN_RECV:
4126 case TCP_ESTABLISHED:
4127 /* Move to CLOSE_WAIT */
4128 tcp_set_state(sk, TCP_CLOSE_WAIT);
Jon Maxwell2251ae42015-07-08 10:12:28 +10004129 inet_csk(sk)->icsk_ack.pingpong = 1;
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004132 case TCP_CLOSE_WAIT:
4133 case TCP_CLOSING:
4134 /* Received a retransmission of the FIN, do
4135 * nothing.
4136 */
4137 break;
4138 case TCP_LAST_ACK:
4139 /* RFC793: Remain in the LAST-ACK state. */
4140 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004142 case TCP_FIN_WAIT1:
4143 /* This case occurs when a simultaneous close
4144 * happens, we must ack the received FIN and
4145 * enter the CLOSING state.
4146 */
4147 tcp_send_ack(sk);
4148 tcp_set_state(sk, TCP_CLOSING);
4149 break;
4150 case TCP_FIN_WAIT2:
4151 /* Received a FIN -- send ACK and enter TIME_WAIT. */
4152 tcp_send_ack(sk);
4153 tcp_time_wait(sk, TCP_TIME_WAIT, 0);
4154 break;
4155 default:
4156 /* Only TCP_LISTEN and TCP_CLOSE are left, in these
4157 * cases we should never reach this piece of code.
4158 */
Joe Perches058bd4d2012-03-11 18:36:11 +00004159 pr_err("%s: Impossible, sk->sk_state=%d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08004160 __func__, sk->sk_state);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004161 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164 /* It _is_ possible, that we have something out-of-order _after_ FIN.
4165 * Probably, we should reset in this case. For now drop them.
4166 */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004167 skb_rbtree_purge(&tp->out_of_order_queue);
Ilpo Järvinene60402d2007-08-09 15:14:46 +03004168 if (tcp_is_sack(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 tcp_sack_reset(&tp->rx_opt);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004170 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 if (!sock_flag(sk, SOCK_DEAD)) {
4173 sk->sk_state_change(sk);
4174
4175 /* Do not send POLL_HUP for half duplex close. */
4176 if (sk->sk_shutdown == SHUTDOWN_MASK ||
4177 sk->sk_state == TCP_CLOSE)
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08004178 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 else
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08004180 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 }
4182}
4183
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004184static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004185 u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
4187 if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) {
4188 if (before(seq, sp->start_seq))
4189 sp->start_seq = seq;
4190 if (after(end_seq, sp->end_seq))
4191 sp->end_seq = end_seq;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004192 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004194 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195}
4196
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004197static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004199 struct tcp_sock *tp = tcp_sk(sk);
4200
David S. Millerbb5b7c12009-12-15 20:56:42 -08004201 if (tcp_is_sack(tp) && sysctl_tcp_dsack) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004202 int mib_idx;
4203
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 if (before(seq, tp->rcv_nxt))
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004205 mib_idx = LINUX_MIB_TCPDSACKOLDSENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004207 mib_idx = LINUX_MIB_TCPDSACKOFOSENT;
4208
Eric Dumazetc10d9312016-04-29 14:16:47 -07004209 NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 tp->rx_opt.dsack = 1;
4212 tp->duplicate_sack[0].start_seq = seq;
4213 tp->duplicate_sack[0].end_seq = end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 }
4215}
4216
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004217static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004219 struct tcp_sock *tp = tcp_sk(sk);
4220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 if (!tp->rx_opt.dsack)
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004222 tcp_dsack_set(sk, seq, end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 else
4224 tcp_sack_extend(tp->duplicate_sack, seq, end_seq);
4225}
4226
Eric Dumazetcf533ea2011-10-21 05:22:42 -04004227static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228{
4229 struct tcp_sock *tp = tcp_sk(sk);
4230
4231 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
4232 before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07004233 NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
Eric Dumazet90cf17d2018-05-21 15:08:56 -07004234 tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
David S. Millerbb5b7c12009-12-15 20:56:42 -08004236 if (tcp_is_sack(tp) && sysctl_tcp_dsack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 u32 end_seq = TCP_SKB_CB(skb)->end_seq;
4238
4239 if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))
4240 end_seq = tp->rcv_nxt;
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004241 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 }
4243 }
4244
4245 tcp_send_ack(sk);
4246}
4247
4248/* These routines update the SACK block as out-of-order packets arrive or
4249 * in-order packets close up the sequence space.
4250 */
4251static void tcp_sack_maybe_coalesce(struct tcp_sock *tp)
4252{
4253 int this_sack;
4254 struct tcp_sack_block *sp = &tp->selective_acks[0];
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004255 struct tcp_sack_block *swalk = sp + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257 /* See if the recent change to the first SACK eats into
4258 * or hits the sequence space of other SACK blocks, if so coalesce.
4259 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004260 for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) {
4262 int i;
4263
4264 /* Zap SWALK, by moving every further SACK up by one slot.
4265 * Decrease num_sacks.
4266 */
4267 tp->rx_opt.num_sacks--;
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004268 for (i = this_sack; i < tp->rx_opt.num_sacks; i++)
4269 sp[i] = sp[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 continue;
4271 }
4272 this_sack++, swalk++;
4273 }
4274}
4275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq)
4277{
4278 struct tcp_sock *tp = tcp_sk(sk);
4279 struct tcp_sack_block *sp = &tp->selective_acks[0];
4280 int cur_sacks = tp->rx_opt.num_sacks;
4281 int this_sack;
4282
4283 if (!cur_sacks)
4284 goto new_sack;
4285
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004286 for (this_sack = 0; this_sack < cur_sacks; this_sack++, sp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 if (tcp_sack_extend(sp, seq, end_seq)) {
4288 /* Rotate this_sack to the first one. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004289 for (; this_sack > 0; this_sack--, sp--)
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07004290 swap(*sp, *(sp - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 if (cur_sacks > 1)
4292 tcp_sack_maybe_coalesce(tp);
4293 return;
4294 }
4295 }
4296
4297 /* Could not find an adjacent existing SACK, build a new one,
4298 * put it at the front, and shift everyone else down. We
4299 * always know there is at least one SACK present already here.
4300 *
4301 * If the sack array is full, forget about the last one.
4302 */
Adam Langley4389dde2008-07-19 00:07:02 -07004303 if (this_sack >= TCP_NUM_SACKS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 this_sack--;
4305 tp->rx_opt.num_sacks--;
4306 sp--;
4307 }
Stephen Hemminger2de979b2007-03-08 20:45:19 -08004308 for (; this_sack > 0; this_sack--, sp--)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004309 *sp = *(sp - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
4311new_sack:
4312 /* Build the new head SACK, and we're done. */
4313 sp->start_seq = seq;
4314 sp->end_seq = end_seq;
4315 tp->rx_opt.num_sacks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
4318/* RCV.NXT advances, some SACKs should be eaten. */
4319
4320static void tcp_sack_remove(struct tcp_sock *tp)
4321{
4322 struct tcp_sack_block *sp = &tp->selective_acks[0];
4323 int num_sacks = tp->rx_opt.num_sacks;
4324 int this_sack;
4325
4326 /* Empty ofo queue, hence, all the SACKs are eaten. Clear. */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004327 if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 tp->rx_opt.num_sacks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return;
4330 }
4331
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004332 for (this_sack = 0; this_sack < num_sacks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 /* Check if the start of the sack is covered by RCV.NXT. */
4334 if (!before(tp->rcv_nxt, sp->start_seq)) {
4335 int i;
4336
4337 /* RCV.NXT must cover all the block! */
Ilpo Järvinen547b7922008-07-25 21:43:18 -07004338 WARN_ON(before(tp->rcv_nxt, sp->end_seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 /* Zap this SACK, by moving forward any other SACKS. */
Weilong Chena22318e2013-12-23 14:37:26 +08004341 for (i = this_sack+1; i < num_sacks; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 tp->selective_acks[i-1] = tp->selective_acks[i];
4343 num_sacks--;
4344 continue;
4345 }
4346 this_sack++;
4347 sp++;
4348 }
Ilpo Järvinen5861f8e2009-03-14 14:23:01 +00004349 tp->rx_opt.num_sacks = num_sacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350}
4351
Eric Dumazet1402d362012-04-23 07:11:42 +00004352/**
4353 * tcp_try_coalesce - try to merge skb to prior one
4354 * @sk: socket
4355 * @to: prior buffer
4356 * @from: buffer to add in queue
Eric Dumazet923dd342012-05-02 07:55:58 +00004357 * @fragstolen: pointer to boolean
Eric Dumazet1402d362012-04-23 07:11:42 +00004358 *
4359 * Before queueing skb @from after @to, try to merge them
4360 * to reduce overall memory use and queue lengths, if cost is small.
4361 * Packets in ofo or receive queues can stay a long time.
4362 * Better try to coalesce them right now to avoid future collapses.
Eric Dumazet783c1752012-04-23 17:34:36 +00004363 * Returns true if caller should free @from instead of queueing it
Eric Dumazet1402d362012-04-23 07:11:42 +00004364 */
Eric Dumazet783c1752012-04-23 17:34:36 +00004365static bool tcp_try_coalesce(struct sock *sk,
4366 struct sk_buff *to,
Eric Dumazet329033f2012-04-27 00:38:33 +00004367 struct sk_buff *from,
4368 bool *fragstolen)
Eric Dumazet1402d362012-04-23 07:11:42 +00004369{
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004370 int delta;
Eric Dumazet1402d362012-04-23 07:11:42 +00004371
Eric Dumazet329033f2012-04-27 00:38:33 +00004372 *fragstolen = false;
Alexander Duyck34a802a2012-05-02 21:19:09 +00004373
Eric Dumazet1ca7ee32012-05-23 17:51:37 +00004374 /* Its possible this segment overlaps with prior segment in queue */
4375 if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
4376 return false;
4377
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004378 if (!skb_try_coalesce(to, from, fragstolen, &delta))
Eric Dumazet783c1752012-04-23 17:34:36 +00004379 return false;
Alexander Duyck34a802a2012-05-02 21:19:09 +00004380
Alexander Duyck34a802a2012-05-02 21:19:09 +00004381 atomic_add(delta, &sk->sk_rmem_alloc);
4382 sk_mem_charge(sk, delta);
Eric Dumazetc10d9312016-04-29 14:16:47 -07004383 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE);
Alexander Duyck34a802a2012-05-02 21:19:09 +00004384 TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
4385 TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
Eric Dumazete93a0432014-09-15 04:19:52 -07004386 TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags;
Alexander Duyck34a802a2012-05-02 21:19:09 +00004387 return true;
Eric Dumazet1402d362012-04-23 07:11:42 +00004388}
4389
Eric Dumazet36ee1062018-07-23 09:28:21 -07004390static bool tcp_ooo_try_coalesce(struct sock *sk,
4391 struct sk_buff *to,
4392 struct sk_buff *from,
4393 bool *fragstolen)
4394{
4395 bool res = tcp_try_coalesce(sk, to, from, fragstolen);
4396
4397 /* In case tcp_drop() is called later, update to->gso_segs */
4398 if (res) {
4399 u32 gso_segs = max_t(u16, 1, skb_shinfo(to)->gso_segs) +
4400 max_t(u16, 1, skb_shinfo(from)->gso_segs);
4401
4402 skb_shinfo(to)->gso_segs = min_t(u32, gso_segs, 0xFFFF);
4403 }
4404 return res;
4405}
4406
Eric Dumazet532182c2016-04-01 08:52:19 -07004407static void tcp_drop(struct sock *sk, struct sk_buff *skb)
4408{
4409 sk_drops_add(sk, skb);
4410 __kfree_skb(skb);
4411}
4412
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413/* This one checks to see if we can put data from the
4414 * out_of_order queue into the receive_queue.
4415 */
4416static void tcp_ofo_queue(struct sock *sk)
4417{
4418 struct tcp_sock *tp = tcp_sk(sk);
4419 __u32 dsack_high = tp->rcv_nxt;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004420 bool fin, fragstolen, eaten;
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004421 struct sk_buff *skb, *tail;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004422 struct rb_node *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004424 p = rb_first(&tp->out_of_order_queue);
4425 while (p) {
Eric Dumazetb475cf32018-10-10 12:30:12 -07004426 skb = rb_to_skb(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
4428 break;
4429
4430 if (before(TCP_SKB_CB(skb)->seq, dsack_high)) {
4431 __u32 dsack = dsack_high;
4432 if (before(TCP_SKB_CB(skb)->end_seq, dsack_high))
4433 dsack_high = TCP_SKB_CB(skb)->end_seq;
4434 tcp_dsack_extend(sk, TCP_SKB_CB(skb)->seq, dsack);
4435 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004436 p = rb_next(p);
4437 rb_erase(&skb->rbnode, &tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004439 if (unlikely(!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 SOCK_DEBUG(sk, "ofo packet was already received\n");
Eric Dumazet532182c2016-04-01 08:52:19 -07004441 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 continue;
4443 }
4444 SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n",
4445 tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
4446 TCP_SKB_CB(skb)->end_seq);
4447
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004448 tail = skb_peek_tail(&sk->sk_receive_queue);
4449 eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07004450 tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004451 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004452 if (!eaten)
4453 __skb_queue_tail(&sk->sk_receive_queue, skb);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004454 else
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004455 kfree_skb_partial(skb, fragstolen);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004456
4457 if (unlikely(fin)) {
4458 tcp_fin(sk);
4459 /* tcp_fin() purges tp->out_of_order_queue,
4460 * so we must end this loop right now.
4461 */
4462 break;
4463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 }
4465}
4466
4467static bool tcp_prune_ofo_queue(struct sock *sk);
4468static int tcp_prune_queue(struct sock *sk);
4469
4470static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb,
4471 unsigned int size)
4472{
4473 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
4474 !sk_rmem_schedule(sk, skb, size)) {
4475
4476 if (tcp_prune_queue(sk) < 0)
4477 return -1;
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004478
Eric Dumazet36a65032016-08-17 14:17:09 -07004479 while (!sk_rmem_schedule(sk, skb, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 if (!tcp_prune_ofo_queue(sk))
4481 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 }
4483 }
4484 return 0;
4485}
4486
Eric Dumazete86b29192012-03-18 11:06:44 +00004487static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
4488{
4489 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazetb475cf32018-10-10 12:30:12 -07004490 struct rb_node **p, *parent;
Eric Dumazete86b29192012-03-18 11:06:44 +00004491 struct sk_buff *skb1;
4492 u32 seq, end_seq;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004493 bool fragstolen;
Eric Dumazete86b29192012-03-18 11:06:44 +00004494
Yousuk Seung095ab5f42018-06-04 15:29:51 -07004495 tcp_ecn_check_ce(sk, skb);
Eric Dumazete86b29192012-03-18 11:06:44 +00004496
Mel Gormanc76562b2012-07-31 16:44:41 -07004497 if (unlikely(tcp_try_rmem_schedule(sk, skb, skb->truesize))) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07004498 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFODROP);
Eric Dumazet532182c2016-04-01 08:52:19 -07004499 tcp_drop(sk, skb);
Eric Dumazete86b29192012-03-18 11:06:44 +00004500 return;
4501 }
4502
4503 /* Disable header prediction. */
4504 tp->pred_flags = 0;
4505 inet_csk_schedule_ack(sk);
4506
Eric Dumazetc10d9312016-04-29 14:16:47 -07004507 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004508 seq = TCP_SKB_CB(skb)->seq;
4509 end_seq = TCP_SKB_CB(skb)->end_seq;
Eric Dumazete86b29192012-03-18 11:06:44 +00004510 SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n",
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004511 tp->rcv_nxt, seq, end_seq);
Eric Dumazete86b29192012-03-18 11:06:44 +00004512
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004513 p = &tp->out_of_order_queue.rb_node;
4514 if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
Eric Dumazete86b29192012-03-18 11:06:44 +00004515 /* Initial out of order segment, build 1 SACK. */
4516 if (tcp_is_sack(tp)) {
4517 tp->rx_opt.num_sacks = 1;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004518 tp->selective_acks[0].start_seq = seq;
4519 tp->selective_acks[0].end_seq = end_seq;
Eric Dumazete86b29192012-03-18 11:06:44 +00004520 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004521 rb_link_node(&skb->rbnode, NULL, p);
4522 rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
4523 tp->ooo_last_skb = skb;
Eric Dumazete86b29192012-03-18 11:06:44 +00004524 goto end;
4525 }
4526
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004527 /* In the typical case, we are adding an skb to the end of the list.
4528 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
4529 */
Eric Dumazet36ee1062018-07-23 09:28:21 -07004530 if (tcp_ooo_try_coalesce(sk, tp->ooo_last_skb,
4531 skb, &fragstolen)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004532coalesce_done:
4533 tcp_grow_window(sk, skb);
4534 kfree_skb_partial(skb, fragstolen);
4535 skb = NULL;
4536 goto add_sack;
Eric Dumazete86b29192012-03-18 11:06:44 +00004537 }
Eric Dumazet2594a2a2016-09-09 14:22:45 -07004538 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
4539 if (!before(seq, TCP_SKB_CB(tp->ooo_last_skb)->end_seq)) {
4540 parent = &tp->ooo_last_skb->rbnode;
4541 p = &parent->rb_right;
4542 goto insert;
Eric Dumazete86b29192012-03-18 11:06:44 +00004543 }
4544
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004545 /* Find place to insert this segment. Handle overlaps on the way. */
4546 parent = NULL;
4547 while (*p) {
4548 parent = *p;
Eric Dumazetb475cf32018-10-10 12:30:12 -07004549 skb1 = rb_to_skb(parent);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004550 if (before(seq, TCP_SKB_CB(skb1)->seq)) {
4551 p = &parent->rb_left;
4552 continue;
Eric Dumazete86b29192012-03-18 11:06:44 +00004553 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004554 if (before(seq, TCP_SKB_CB(skb1)->end_seq)) {
4555 if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
4556 /* All the bits are present. Drop. */
4557 NET_INC_STATS(sock_net(sk),
4558 LINUX_MIB_TCPOFOMERGE);
Eric Dumazet94623c72018-07-23 09:28:20 -07004559 tcp_drop(sk, skb);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004560 skb = NULL;
4561 tcp_dsack_set(sk, seq, end_seq);
4562 goto add_sack;
4563 }
4564 if (after(seq, TCP_SKB_CB(skb1)->seq)) {
4565 /* Partial overlap. */
4566 tcp_dsack_set(sk, seq, TCP_SKB_CB(skb1)->end_seq);
4567 } else {
4568 /* skb's seq == skb1's seq and skb covers skb1.
4569 * Replace skb1 with skb.
4570 */
4571 rb_replace_node(&skb1->rbnode, &skb->rbnode,
4572 &tp->out_of_order_queue);
4573 tcp_dsack_extend(sk,
4574 TCP_SKB_CB(skb1)->seq,
4575 TCP_SKB_CB(skb1)->end_seq);
4576 NET_INC_STATS(sock_net(sk),
4577 LINUX_MIB_TCPOFOMERGE);
Eric Dumazet94623c72018-07-23 09:28:20 -07004578 tcp_drop(sk, skb1);
Eric Dumazet76f0dcb2016-09-13 22:55:05 -07004579 goto merge_right;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004580 }
Eric Dumazet36ee1062018-07-23 09:28:21 -07004581 } else if (tcp_ooo_try_coalesce(sk, skb1,
4582 skb, &fragstolen)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004583 goto coalesce_done;
4584 }
4585 p = &parent->rb_right;
Eric Dumazete86b29192012-03-18 11:06:44 +00004586 }
Eric Dumazet2594a2a2016-09-09 14:22:45 -07004587insert:
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004588 /* Insert segment into RB tree. */
4589 rb_link_node(&skb->rbnode, parent, p);
4590 rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
Eric Dumazete86b29192012-03-18 11:06:44 +00004591
Eric Dumazet76f0dcb2016-09-13 22:55:05 -07004592merge_right:
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004593 /* Remove other segments covered by skb. */
Eric Dumazetb475cf32018-10-10 12:30:12 -07004594 while ((skb1 = skb_rb_next(skb)) != NULL) {
Eric Dumazete86b29192012-03-18 11:06:44 +00004595 if (!after(end_seq, TCP_SKB_CB(skb1)->seq))
4596 break;
4597 if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
4598 tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
4599 end_seq);
4600 break;
4601 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004602 rb_erase(&skb1->rbnode, &tp->out_of_order_queue);
Eric Dumazete86b29192012-03-18 11:06:44 +00004603 tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
4604 TCP_SKB_CB(skb1)->end_seq);
Eric Dumazetc10d9312016-04-29 14:16:47 -07004605 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
Eric Dumazet532182c2016-04-01 08:52:19 -07004606 tcp_drop(sk, skb1);
Eric Dumazete86b29192012-03-18 11:06:44 +00004607 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004608 /* If there is no skb after us, we are the last_skb ! */
Eric Dumazetb475cf32018-10-10 12:30:12 -07004609 if (!skb1)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004610 tp->ooo_last_skb = skb;
Eric Dumazete86b29192012-03-18 11:06:44 +00004611
4612add_sack:
4613 if (tcp_is_sack(tp))
4614 tcp_sack_new_ofo_skb(sk, seq, end_seq);
4615end:
Eric Dumazet4e4f1fc2013-09-06 10:35:58 -07004616 if (skb) {
4617 tcp_grow_window(sk, skb);
Eric Dumazete86b29192012-03-18 11:06:44 +00004618 skb_set_owner_r(skb, sk);
Eric Dumazet4e4f1fc2013-09-06 10:35:58 -07004619 }
Eric Dumazete86b29192012-03-18 11:06:44 +00004620}
4621
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004622static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int hdrlen,
Eric Dumazetb081f852012-05-02 09:58:29 +00004623 bool *fragstolen)
4624{
4625 int eaten;
4626 struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue);
4627
4628 __skb_pull(skb, hdrlen);
4629 eaten = (tail &&
4630 tcp_try_coalesce(sk, tail, skb, fragstolen)) ? 1 : 0;
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07004631 tcp_rcv_nxt_update(tcp_sk(sk), TCP_SKB_CB(skb)->end_seq);
Eric Dumazetb081f852012-05-02 09:58:29 +00004632 if (!eaten) {
4633 __skb_queue_tail(&sk->sk_receive_queue, skb);
4634 skb_set_owner_r(skb, sk);
4635 }
4636 return eaten;
4637}
Eric Dumazete86b29192012-03-18 11:06:44 +00004638
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004639int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
4640{
Eric Dumazetcb934712014-09-17 03:14:42 -07004641 struct sk_buff *skb;
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004642 int err = -ENOMEM;
4643 int data_len = 0;
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004644 bool fragstolen;
4645
Pavel Emelyanovc454e612012-10-29 05:05:33 +00004646 if (size == 0)
4647 return 0;
4648
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004649 if (size > PAGE_SIZE) {
4650 int npages = min_t(size_t, size >> PAGE_SHIFT, MAX_SKB_FRAGS);
4651
4652 data_len = npages << PAGE_SHIFT;
4653 size = data_len + (size & ~PAGE_MASK);
4654 }
4655 skb = alloc_skb_with_frags(size - data_len, data_len,
4656 PAGE_ALLOC_COSTLY_ORDER,
4657 &err, sk->sk_allocation);
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004658 if (!skb)
4659 goto err;
4660
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004661 skb_put(skb, size - data_len);
4662 skb->data_len = data_len;
4663 skb->len = size;
4664
Eric Dumazetcb934712014-09-17 03:14:42 -07004665 if (tcp_try_rmem_schedule(sk, skb, skb->truesize))
Mel Gormanc76562b2012-07-31 16:44:41 -07004666 goto err_free;
4667
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004668 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
4669 if (err)
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004670 goto err_free;
4671
4672 TCP_SKB_CB(skb)->seq = tcp_sk(sk)->rcv_nxt;
4673 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size;
4674 TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1;
4675
Eric Dumazetcb934712014-09-17 03:14:42 -07004676 if (tcp_queue_rcv(sk, skb, 0, &fragstolen)) {
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004677 WARN_ON_ONCE(fragstolen); /* should not happen */
4678 __kfree_skb(skb);
4679 }
4680 return size;
4681
4682err_free:
4683 kfree_skb(skb);
4684err:
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004685 return err;
4686
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004687}
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
4690{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet329033f2012-04-27 00:38:33 +00004692 bool fragstolen = false;
Eric Dumazet532182c2016-04-01 08:52:19 -07004693 int eaten = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694
Eric Dumazet532182c2016-04-01 08:52:19 -07004695 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
4696 __kfree_skb(skb);
4697 return;
4698 }
Eric Dumazetf84af322010-04-28 15:31:51 -07004699 skb_dst_drop(skb);
Peter Pan(潘卫平)155c6e12014-09-24 22:17:02 +08004700 __skb_pull(skb, tcp_hdr(skb)->doff * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Florian Westphal735d3832014-09-29 13:08:30 +02004702 tcp_ecn_accept_cwr(tp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
Ilpo Järvinen5861f8e2009-03-14 14:23:01 +00004704 tp->rx_opt.dsack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
4706 /* Queue data for delivery to the user.
4707 * Packets in sequence go to the receive queue.
4708 * Out of sequence packets to the out_of_order_queue.
4709 */
4710 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
4711 if (tcp_receive_window(tp) == 0)
4712 goto out_of_window;
4713
4714 /* Ok. In sequence. In window. */
4715 if (tp->ucopy.task == current &&
4716 tp->copied_seq == tp->rcv_nxt && tp->ucopy.len &&
4717 sock_owned_by_user(sk) && !tp->urg_data) {
4718 int chunk = min_t(unsigned int, skb->len,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004719 tp->ucopy.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
4721 __set_current_state(TASK_RUNNING);
4722
Al Virof4362a22014-11-24 13:26:06 -05004723 if (!skb_copy_datagram_msg(skb, 0, tp->ucopy.msg, chunk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 tp->ucopy.len -= chunk;
4725 tp->copied_seq += chunk;
Jerry Chu44f53242011-01-25 13:46:30 -08004726 eaten = (chunk == skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 tcp_rcv_space_adjust(sk);
4728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 }
4730
4731 if (eaten <= 0) {
4732queue_and_out:
Eric Dumazet76dfa602015-05-15 12:39:29 -07004733 if (eaten < 0) {
4734 if (skb_queue_len(&sk->sk_receive_queue) == 0)
4735 sk_forced_mem_schedule(sk, skb->truesize);
4736 else if (tcp_try_rmem_schedule(sk, skb, skb->truesize))
4737 goto drop;
4738 }
Eric Dumazetb081f852012-05-02 09:58:29 +00004739 eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07004741 tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
Stephen Hemminger2de979b2007-03-08 20:45:19 -08004742 if (skb->len)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07004743 tcp_event_data_recv(sk, skb);
Peter Pan(潘卫平)155c6e12014-09-24 22:17:02 +08004744 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
Eric Dumazet20c4cb72011-10-20 17:44:03 -04004745 tcp_fin(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004747 if (!RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 tcp_ofo_queue(sk);
4749
4750 /* RFC2581. 4.2. SHOULD send immediate ACK, when
4751 * gap in queue is filled.
4752 */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004753 if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004754 inet_csk(sk)->icsk_ack.pingpong = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 }
4756
4757 if (tp->rx_opt.num_sacks)
4758 tcp_sack_remove(tp);
4759
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07004760 tcp_fast_path_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
Eric Dumazet923dd342012-05-02 07:55:58 +00004762 if (eaten > 0)
4763 kfree_skb_partial(skb, fragstolen);
Eric Dumazet1d57f192012-09-17 12:51:39 +00004764 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04004765 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 return;
4767 }
4768
4769 if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
4770 /* A retransmit, 2nd most common case. Force an immediate ack. */
Eric Dumazetc10d9312016-04-29 14:16:47 -07004771 NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004772 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
4774out_of_window:
Eric Dumazet90cf17d2018-05-21 15:08:56 -07004775 tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004776 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777drop:
Eric Dumazet532182c2016-04-01 08:52:19 -07004778 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 return;
4780 }
4781
4782 /* Out of window. F.e. zero window probe. */
4783 if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp)))
4784 goto out_of_window;
4785
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
4787 /* Partial packet, seq < rcv_next < end_seq */
4788 SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n",
4789 tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
4790 TCP_SKB_CB(skb)->end_seq);
4791
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004792 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09004793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 /* If window is closed, drop tail of packet. But after
4795 * remembering D-SACK for its head made in previous line.
4796 */
4797 if (!tcp_receive_window(tp))
4798 goto out_of_window;
4799 goto queue_and_out;
4800 }
4801
Eric Dumazete86b29192012-03-18 11:06:44 +00004802 tcp_data_queue_ofo(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803}
4804
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004805static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *list)
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004806{
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004807 if (list)
4808 return !skb_queue_is_last(list, skb) ? skb->next : NULL;
David S. Miller91521942009-05-28 21:35:47 -07004809
Eric Dumazetb475cf32018-10-10 12:30:12 -07004810 return skb_rb_next(skb);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004811}
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004812
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004813static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
4814 struct sk_buff_head *list,
4815 struct rb_root *root)
4816{
4817 struct sk_buff *next = tcp_skb_next(skb, list);
4818
4819 if (list)
4820 __skb_unlink(skb, list);
4821 else
4822 rb_erase(&skb->rbnode, root);
4823
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004824 __kfree_skb(skb);
Eric Dumazetc10d9312016-04-29 14:16:47 -07004825 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOLLAPSED);
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004826
4827 return next;
4828}
4829
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004830/* Insert skb into rb tree, ordered by TCP_SKB_CB(skb)->seq */
4831static void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb)
4832{
4833 struct rb_node **p = &root->rb_node;
4834 struct rb_node *parent = NULL;
4835 struct sk_buff *skb1;
4836
4837 while (*p) {
4838 parent = *p;
Eric Dumazetb475cf32018-10-10 12:30:12 -07004839 skb1 = rb_to_skb(parent);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004840 if (before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb1)->seq))
4841 p = &parent->rb_left;
4842 else
4843 p = &parent->rb_right;
4844 }
4845 rb_link_node(&skb->rbnode, parent, p);
4846 rb_insert_color(&skb->rbnode, root);
4847}
4848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849/* Collapse contiguous sequence of skbs head..tail with
4850 * sequence numbers start..end.
David S. Miller91521942009-05-28 21:35:47 -07004851 *
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004852 * If tail is NULL, this means until the end of the queue.
David S. Miller91521942009-05-28 21:35:47 -07004853 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 * Segments with FIN/SYN are not collapsed (only because this
4855 * simplifies code)
4856 */
4857static void
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004858tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
4859 struct sk_buff *head, struct sk_buff *tail, u32 start, u32 end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860{
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004861 struct sk_buff *skb = head, *n;
4862 struct sk_buff_head tmp;
David S. Miller91521942009-05-28 21:35:47 -07004863 bool end_of_skbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08004865 /* First, check that queue is collapsible and find
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004866 * the point where collapsing can be useful.
4867 */
David S. Miller91521942009-05-28 21:35:47 -07004868restart:
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004869 for (end_of_skbs = true; skb != NULL && skb != tail; skb = n) {
4870 n = tcp_skb_next(skb, list);
4871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 /* No new bits? It is possible on ofo queue. */
4873 if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004874 skb = tcp_collapse_one(sk, skb, list, root);
David S. Miller91521942009-05-28 21:35:47 -07004875 if (!skb)
4876 break;
4877 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 }
4879
4880 /* The first skb to collapse is:
4881 * - not SYN/FIN and
4882 * - bloated or contains data before "start" or
4883 * overlaps to the next one.
4884 */
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004885 if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 (tcp_win_from_space(skb->truesize) > skb->len ||
David S. Miller91521942009-05-28 21:35:47 -07004887 before(TCP_SKB_CB(skb)->seq, start))) {
4888 end_of_skbs = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 break;
David S. Miller91521942009-05-28 21:35:47 -07004890 }
4891
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004892 if (n && n != tail &&
4893 TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(n)->seq) {
4894 end_of_skbs = false;
4895 break;
David S. Miller91521942009-05-28 21:35:47 -07004896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
4898 /* Decided to skip this, advance start seq. */
4899 start = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 }
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004901 if (end_of_skbs ||
4902 (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 return;
4904
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004905 __skb_queue_head_init(&tmp);
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 while (before(start, end)) {
Eric Dumazetb3d6cb92014-09-15 04:19:53 -07004908 int copy = min_t(int, SKB_MAX_ORDER(0, 0), end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Eric Dumazetb3d6cb92014-09-15 04:19:53 -07004911 nskb = alloc_skb(copy, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 if (!nskb)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004913 break;
Arnaldo Carvalho de Meloc51957d2007-03-10 12:47:22 -03004914
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
4916 TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004917 if (list)
4918 __skb_queue_before(list, skb, nskb);
4919 else
4920 __skb_queue_tail(&tmp, nskb); /* defer rbtree insertion */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004921 skb_set_owner_r(nskb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 /* Copy data, releasing collapsed skbs. */
4924 while (copy > 0) {
4925 int offset = start - TCP_SKB_CB(skb)->seq;
4926 int size = TCP_SKB_CB(skb)->end_seq - start;
4927
Kris Katterjohn09a62662006-01-08 22:24:28 -08004928 BUG_ON(offset < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 if (size > 0) {
4930 size = min(copy, size);
4931 if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
4932 BUG();
4933 TCP_SKB_CB(nskb)->end_seq += size;
4934 copy -= size;
4935 start += size;
4936 }
4937 if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004938 skb = tcp_collapse_one(sk, skb, list, root);
David S. Miller91521942009-05-28 21:35:47 -07004939 if (!skb ||
4940 skb == tail ||
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004941 (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004942 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 }
4944 }
4945 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004946end:
4947 skb_queue_walk_safe(&tmp, skb, n)
4948 tcp_rbtree_insert(root, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949}
4950
4951/* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
4952 * and tcp_collapse() them until all the queue is collapsed.
4953 */
4954static void tcp_collapse_ofo_queue(struct sock *sk)
4955{
4956 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet34912e62018-07-27 12:08:46 +02004957 u32 range_truesize, sum_tiny = 0;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004958 struct sk_buff *skb, *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 u32 start, end;
4960
Eric Dumazetb475cf32018-10-10 12:30:12 -07004961 skb = skb_rb_first(&tp->out_of_order_queue);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004962new_range:
4963 if (!skb) {
Eric Dumazetb475cf32018-10-10 12:30:12 -07004964 tp->ooo_last_skb = skb_rb_last(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 return;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 start = TCP_SKB_CB(skb)->seq;
4968 end = TCP_SKB_CB(skb)->end_seq;
Eric Dumazet34912e62018-07-27 12:08:46 +02004969 range_truesize = skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004971 for (head = skb;;) {
Eric Dumazetb475cf32018-10-10 12:30:12 -07004972 skb = skb_rb_next(skb);
David S. Miller91521942009-05-28 21:35:47 -07004973
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004974 /* Range is terminated when we see a gap or when
4975 * we are at the queue end.
4976 */
David S. Miller91521942009-05-28 21:35:47 -07004977 if (!skb ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 after(TCP_SKB_CB(skb)->seq, end) ||
4979 before(TCP_SKB_CB(skb)->end_seq, start)) {
Eric Dumazet34912e62018-07-27 12:08:46 +02004980 /* Do not attempt collapsing tiny skbs */
4981 if (range_truesize != head->truesize ||
4982 end - start >= SKB_WITH_OVERHEAD(SK_MEM_QUANTUM)) {
4983 tcp_collapse(sk, NULL, &tp->out_of_order_queue,
4984 head, skb, start, end);
4985 } else {
4986 sum_tiny += range_truesize;
4987 if (sum_tiny > sk->sk_rcvbuf >> 3)
4988 return;
4989 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004990 goto new_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004992
Eric Dumazet34912e62018-07-27 12:08:46 +02004993 range_truesize += skb->truesize;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004994 if (unlikely(before(TCP_SKB_CB(skb)->seq, start)))
4995 start = TCP_SKB_CB(skb)->seq;
4996 if (after(TCP_SKB_CB(skb)->end_seq, end))
4997 end = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 }
4999}
5000
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07005001/*
Eric Dumazet36a65032016-08-17 14:17:09 -07005002 * Clean the out-of-order queue to make room.
5003 * We drop high sequences packets to :
5004 * 1) Let a chance for holes to be filled.
5005 * 2) not add too big latencies if thousands of packets sit there.
5006 * (But if application shrinks SO_RCVBUF, we could still end up
5007 * freeing whole queue here)
Eric Dumazet2b4dbcd2018-07-27 12:08:44 +02005008 * 3) Drop at least 12.5 % of sk_rcvbuf to avoid malicious attacks.
Eric Dumazet36a65032016-08-17 14:17:09 -07005009 *
5010 * Return true if queue has shrunk.
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07005011 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005012static bool tcp_prune_ofo_queue(struct sock *sk)
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07005013{
5014 struct tcp_sock *tp = tcp_sk(sk);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07005015 struct rb_node *node, *prev;
Eric Dumazet2b4dbcd2018-07-27 12:08:44 +02005016 int goal;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07005017
Yaogong Wang9f5afea2016-09-07 14:49:28 -07005018 if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
Eric Dumazet36a65032016-08-17 14:17:09 -07005019 return false;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07005020
Eric Dumazet36a65032016-08-17 14:17:09 -07005021 NET_INC_STATS(sock_net(sk), LINUX_MIB_OFOPRUNED);
Eric Dumazet2b4dbcd2018-07-27 12:08:44 +02005022 goal = sk->sk_rcvbuf >> 3;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07005023 node = &tp->ooo_last_skb->rbnode;
5024 do {
5025 prev = rb_prev(node);
5026 rb_erase(node, &tp->out_of_order_queue);
Eric Dumazet2b4dbcd2018-07-27 12:08:44 +02005027 goal -= rb_to_skb(node)->truesize;
Eric Dumazetb475cf32018-10-10 12:30:12 -07005028 tcp_drop(sk, rb_to_skb(node));
Eric Dumazet2b4dbcd2018-07-27 12:08:44 +02005029 if (!prev || goal <= 0) {
5030 sk_mem_reclaim(sk);
5031 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
5032 !tcp_under_memory_pressure(sk))
5033 break;
5034 goal = sk->sk_rcvbuf >> 3;
5035 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07005036 node = prev;
5037 } while (node);
Eric Dumazetb475cf32018-10-10 12:30:12 -07005038 tp->ooo_last_skb = rb_to_skb(prev);
Eric Dumazet36a65032016-08-17 14:17:09 -07005039
5040 /* Reset SACK state. A conforming SACK implementation will
5041 * do the same at a timeout based retransmit. When a connection
5042 * is in a sad state like this, we care only about integrity
5043 * of the connection not performance.
5044 */
5045 if (tp->rx_opt.sack_ok)
5046 tcp_sack_reset(&tp->rx_opt);
5047 return true;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07005048}
5049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050/* Reduce allocated memory if we can, trying to get
5051 * the socket within its memory limits again.
5052 *
5053 * Return less than zero if we should start dropping frames
5054 * until the socket owning process reads some of the data
5055 * to stabilize the situation.
5056 */
5057static int tcp_prune_queue(struct sock *sk)
5058{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005059 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
5061 SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq);
5062
Eric Dumazetc10d9312016-04-29 14:16:47 -07005063 NET_INC_STATS(sock_net(sk), LINUX_MIB_PRUNECALLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
5065 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005066 tcp_clamp_window(sk);
Eric Dumazetb8da51e2015-05-15 12:39:27 -07005067 else if (tcp_under_memory_pressure(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
5069
Eric Dumazet8e719202018-07-27 12:08:45 +02005070 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
5071 return 0;
5072
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 tcp_collapse_ofo_queue(sk);
David S. Miller91521942009-05-28 21:35:47 -07005074 if (!skb_queue_empty(&sk->sk_receive_queue))
Yaogong Wang9f5afea2016-09-07 14:49:28 -07005075 tcp_collapse(sk, &sk->sk_receive_queue, NULL,
David S. Miller91521942009-05-28 21:35:47 -07005076 skb_peek(&sk->sk_receive_queue),
5077 NULL,
5078 tp->copied_seq, tp->rcv_nxt);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08005079 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
5081 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
5082 return 0;
5083
5084 /* Collapsing did not help, destructive actions follow.
5085 * This must not ever occur. */
5086
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07005087 tcp_prune_ofo_queue(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088
5089 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
5090 return 0;
5091
5092 /* If we are really being abused, tell the caller to silently
5093 * drop receive data on the floor. It will get retransmitted
5094 * and hopefully then we'll have sufficient space.
5095 */
Eric Dumazetc10d9312016-04-29 14:16:47 -07005096 NET_INC_STATS(sock_net(sk), LINUX_MIB_RCVPRUNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
5098 /* Massive buffer overcommit. */
5099 tp->pred_flags = 0;
5100 return -1;
5101}
5102
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005103static bool tcp_should_expand_sndbuf(const struct sock *sk)
David S. Miller0d9901d2005-07-05 15:21:10 -07005104{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005105 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005106
David S. Miller0d9901d2005-07-05 15:21:10 -07005107 /* If the user specified a specific send buffer setting, do
5108 * not modify it.
5109 */
5110 if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005111 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005112
5113 /* If we are under global TCP memory pressure, do not expand. */
Eric Dumazetb8da51e2015-05-15 12:39:27 -07005114 if (tcp_under_memory_pressure(sk))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005115 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005116
5117 /* If we are under soft global TCP memory pressure, do not expand. */
Glauber Costa180d8cd2011-12-11 21:47:02 +00005118 if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005119 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005120
5121 /* If we filled the congestion window, do not expand. */
Neal Cardwell65148902015-02-20 13:33:16 -05005122 if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005123 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005124
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005125 return true;
David S. Miller0d9901d2005-07-05 15:21:10 -07005126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
5128/* When incoming ACK allowed to free some skb from write_queue,
5129 * we remember this event in flag SOCK_QUEUE_SHRUNK and wake up socket
5130 * on the exit from tcp input handler.
5131 *
5132 * PROBLEM: sndbuf expansion does not work well with largesend.
5133 */
5134static void tcp_new_space(struct sock *sk)
5135{
5136 struct tcp_sock *tp = tcp_sk(sk);
5137
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005138 if (tcp_should_expand_sndbuf(sk)) {
Eric Dumazet6ae70532013-10-01 10:23:44 -07005139 tcp_sndbuf_expand(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 tp->snd_cwnd_stamp = tcp_time_stamp;
5141 }
5142
5143 sk->sk_write_space(sk);
5144}
5145
Stephen Hemminger40efc6f2006-01-03 16:03:49 -08005146static void tcp_check_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147{
5148 if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) {
5149 sock_reset_flag(sk, SOCK_QUEUE_SHRUNK);
jbaron@akamai.com3c715122015-04-20 20:05:07 +00005150 /* pairs with tcp_poll() */
Jason Baronfe8bdc92017-01-24 21:49:41 -05005151 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 if (sk->sk_socket &&
5153 test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
5154 tcp_new_space(sk);
5155 }
5156}
5157
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005158static inline void tcp_data_snd_check(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005160 tcp_push_pending_frames(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 tcp_check_space(sk);
5162}
5163
5164/*
5165 * Check if sending an ack is needed.
5166 */
5167static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
5168{
5169 struct tcp_sock *tp = tcp_sk(sk);
5170
5171 /* More than one full frame received... */
Joe Perches9d4fb272009-11-23 10:41:23 -08005172 if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 /* ... and right edge of window advances far enough.
5174 * (tcp_recvmsg() will send ACK otherwise). Or...
5175 */
Joe Perches9d4fb272009-11-23 10:41:23 -08005176 __tcp_select_window(sk) >= tp->rcv_wnd) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 /* We ACK each frame or... */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005178 tcp_in_quickack_mode(sk) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 /* We have out of order data. */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07005180 (ofo_possible && !RB_EMPTY_ROOT(&tp->out_of_order_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 /* Then ack it now */
5182 tcp_send_ack(sk);
5183 } else {
5184 /* Else, send delayed ack. */
5185 tcp_send_delayed_ack(sk);
5186 }
5187}
5188
Stephen Hemminger40efc6f2006-01-03 16:03:49 -08005189static inline void tcp_ack_snd_check(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005191 if (!inet_csk_ack_scheduled(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 /* We sent a data segment already. */
5193 return;
5194 }
5195 __tcp_ack_snd_check(sk, 1);
5196}
5197
5198/*
5199 * This routine is only called when we have urgent data
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08005200 * signaled. Its the 'slow' part of tcp_urg. It could be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 * moved inline now as tcp_urg is only called from one
5202 * place. We handle URGent data wrong. We have to - as
5203 * BSD still doesn't use the correction from RFC961.
5204 * For 1003.1g we should support a new option TCP_STDURG to permit
5205 * either form (or just set the sysctl tcp_stdurg).
5206 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005207
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005208static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209{
5210 struct tcp_sock *tp = tcp_sk(sk);
5211 u32 ptr = ntohs(th->urg_ptr);
5212
5213 if (ptr && !sysctl_tcp_stdurg)
5214 ptr--;
5215 ptr += ntohl(th->seq);
5216
5217 /* Ignore urgent data that we've already seen and read. */
5218 if (after(tp->copied_seq, ptr))
5219 return;
5220
5221 /* Do not replay urg ptr.
5222 *
5223 * NOTE: interesting situation not covered by specs.
5224 * Misbehaving sender may send urg ptr, pointing to segment,
5225 * which we already have in ofo queue. We are not able to fetch
5226 * such data and will stay in TCP_URG_NOTYET until will be eaten
5227 * by recvmsg(). Seems, we are not obliged to handle such wicked
5228 * situations. But it is worth to think about possibility of some
5229 * DoSes using some hypothetical application level deadlock.
5230 */
5231 if (before(ptr, tp->rcv_nxt))
5232 return;
5233
5234 /* Do we already have a newer (or duplicate) urgent pointer? */
5235 if (tp->urg_data && !after(ptr, tp->urg_seq))
5236 return;
5237
5238 /* Tell the world about our new urgent pointer. */
5239 sk_send_sigurg(sk);
5240
5241 /* We may be adding urgent data when the last byte read was
5242 * urgent. To do this requires some care. We cannot just ignore
5243 * tp->copied_seq since we would read the last urgent byte again
5244 * as data, nor can we alter copied_seq until this data arrives
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08005245 * or we break the semantics of SIOCATMARK (and thus sockatmark())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 *
5247 * NOTE. Double Dutch. Rendering to plain English: author of comment
5248 * above did something sort of send("A", MSG_OOB); send("B", MSG_OOB);
5249 * and expect that both A and B disappear from stream. This is _wrong_.
5250 * Though this happens in BSD with high probability, this is occasional.
5251 * Any application relying on this is buggy. Note also, that fix "works"
5252 * only in this artificial test. Insert some normal data between A and B and we will
5253 * decline of BSD again. Verdict: it is better to remove to trap
5254 * buggy users.
5255 */
5256 if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005257 !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
5259 tp->copied_seq++;
5260 if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
David S. Miller8728b832005-08-09 19:25:21 -07005261 __skb_unlink(skb, &sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 __kfree_skb(skb);
5263 }
5264 }
5265
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005266 tp->urg_data = TCP_URG_NOTYET;
5267 tp->urg_seq = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268
5269 /* Disable header prediction. */
5270 tp->pred_flags = 0;
5271}
5272
5273/* This is the 'fast' part of urgent handling. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005274static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275{
5276 struct tcp_sock *tp = tcp_sk(sk);
5277
5278 /* Check if we get a new urgent pointer - normally not. */
5279 if (th->urg)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005280 tcp_check_urg(sk, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281
5282 /* Do we wait for any urgent data? - normally not... */
5283 if (tp->urg_data == TCP_URG_NOTYET) {
5284 u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) -
5285 th->syn;
5286
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005287 /* Is the urgent pointer pointing into this packet? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 if (ptr < skb->len) {
5289 u8 tmp;
5290 if (skb_copy_bits(skb, ptr, &tmp, 1))
5291 BUG();
5292 tp->urg_data = TCP_URG_VALID | tmp;
5293 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04005294 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 }
5296 }
5297}
5298
5299static int tcp_copy_to_iovec(struct sock *sk, struct sk_buff *skb, int hlen)
5300{
5301 struct tcp_sock *tp = tcp_sk(sk);
5302 int chunk = skb->len - hlen;
5303 int err;
5304
Herbert Xu60476372007-04-09 11:59:39 -07005305 if (skb_csum_unnecessary(skb))
Al Virof4362a22014-11-24 13:26:06 -05005306 err = skb_copy_datagram_msg(skb, hlen, tp->ucopy.msg, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 else
Al Virof4362a22014-11-24 13:26:06 -05005308 err = skb_copy_and_csum_datagram_msg(skb, hlen, tp->ucopy.msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
5310 if (!err) {
5311 tp->ucopy.len -= chunk;
5312 tp->copied_seq += chunk;
5313 tcp_rcv_space_adjust(sk);
5314 }
5315
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 return err;
5317}
5318
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005319/* Does PAWS and seqno based validation of an incoming segment, flags will
5320 * play significant role here.
5321 */
Eric Dumazet0c246042012-07-17 01:41:30 +00005322static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
5323 const struct tcphdr *th, int syn_inerr)
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005324{
5325 struct tcp_sock *tp = tcp_sk(sk);
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005326 bool rst_seq_match = false;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005327
5328 /* RFC1323: H1. Apply PAWS check first. */
Christoph Paasch1a2c6182013-03-17 08:23:34 +00005329 if (tcp_fast_parse_options(skb, th, tp) && tp->rx_opt.saw_tstamp &&
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005330 tcp_paws_discard(sk, skb)) {
5331 if (!th->rst) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07005332 NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005333 if (!tcp_oow_rate_limited(sock_net(sk), skb,
5334 LINUX_MIB_TCPACKSKIPPEDPAWS,
5335 &tp->last_oow_ack_time))
5336 tcp_send_dupack(sk, skb);
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005337 goto discard;
5338 }
5339 /* Reset is accepted even if it did not pass PAWS. */
5340 }
5341
5342 /* Step 1: check sequence number */
5343 if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {
5344 /* RFC793, page 37: "In all states except SYN-SENT, all reset
5345 * (RST) segments are validated by checking their SEQ-fields."
5346 * And page 69: "If an incoming segment is not acceptable,
5347 * an acknowledgment should be sent in reply (unless the RST
5348 * bit is set, if so drop the segment and return)".
5349 */
Eric Dumazete3715892012-07-17 12:29:30 +00005350 if (!th->rst) {
5351 if (th->syn)
5352 goto syn_challenge;
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005353 if (!tcp_oow_rate_limited(sock_net(sk), skb,
5354 LINUX_MIB_TCPACKSKIPPEDSEQ,
5355 &tp->last_oow_ack_time))
5356 tcp_send_dupack(sk, skb);
Eric Dumazete3715892012-07-17 12:29:30 +00005357 }
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005358 goto discard;
5359 }
5360
5361 /* Step 2: check RST bit */
5362 if (th->rst) {
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005363 /* RFC 5961 3.2 (extend to match against SACK too if available):
5364 * If seq num matches RCV.NXT or the right-most SACK block,
5365 * then
Eric Dumazet282f23c2012-07-17 10:13:05 +02005366 * RESET the connection
5367 * else
5368 * Send a challenge ACK
5369 */
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005370 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
5371 rst_seq_match = true;
5372 } else if (tcp_is_sack(tp) && tp->rx_opt.num_sacks > 0) {
5373 struct tcp_sack_block *sp = &tp->selective_acks[0];
5374 int max_sack = sp[0].end_seq;
5375 int this_sack;
5376
5377 for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;
5378 ++this_sack) {
5379 max_sack = after(sp[this_sack].end_seq,
5380 max_sack) ?
5381 sp[this_sack].end_seq : max_sack;
5382 }
5383
5384 if (TCP_SKB_CB(skb)->seq == max_sack)
5385 rst_seq_match = true;
5386 }
5387
5388 if (rst_seq_match)
Eric Dumazet282f23c2012-07-17 10:13:05 +02005389 tcp_reset(sk);
5390 else
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005391 tcp_send_challenge_ack(sk, skb);
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005392 goto discard;
5393 }
5394
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005395 /* step 3: check security and precedence [ignored] */
5396
Eric Dumazet0c246042012-07-17 01:41:30 +00005397 /* step 4: Check for a SYN
Sowmini Varadhancd214532014-10-30 12:48:08 -04005398 * RFC 5961 4.2 : Send a challenge ack
Eric Dumazet0c246042012-07-17 01:41:30 +00005399 */
5400 if (th->syn) {
Eric Dumazete3715892012-07-17 12:29:30 +00005401syn_challenge:
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005402 if (syn_inerr)
Eric Dumazetc10d9312016-04-29 14:16:47 -07005403 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
5404 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005405 tcp_send_challenge_ack(sk, skb);
Eric Dumazet0c246042012-07-17 01:41:30 +00005406 goto discard;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005407 }
5408
Eric Dumazet0c246042012-07-17 01:41:30 +00005409 return true;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005410
5411discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005412 tcp_drop(sk, skb);
Eric Dumazet0c246042012-07-17 01:41:30 +00005413 return false;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005414}
5415
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416/*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005417 * TCP receive function for the ESTABLISHED state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005419 * It is split into a fast path and a slow path. The fast path is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 * disabled when:
5421 * - A zero window was announced from us - zero window probing
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005422 * is only handled properly in the slow path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 * - Out of order segments arrived.
5424 * - Urgent data is expected.
5425 * - There is no buffer space left
5426 * - Unexpected TCP flags/window values/header lengths are received
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005427 * (detected by checking the TCP header against pred_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 * - Data is sent in both directions. Fast path only supports pure senders
5429 * or pure receivers (this means either the sequence number or the ack
5430 * value must stay constant)
5431 * - Unexpected TCP option.
5432 *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005433 * When these conditions are not satisfied it drops into a standard
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 * receive procedure patterned after RFC793 to handle all cases.
5435 * The first three cases are guaranteed by proper pred_flags setting,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005436 * the rest is checked inline. Fast processing is turned on in
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 * tcp_data_queue when everything is OK.
5438 */
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005439void tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
5440 const struct tcphdr *th, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441{
5442 struct tcp_sock *tp = tcp_sk(sk);
5443
Ian Morris51456b22015-04-03 09:17:26 +01005444 if (unlikely(!sk->sk_rx_dst))
Eric Dumazet5d299f32012-08-06 05:09:33 +00005445 inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 /*
5447 * Header prediction.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005448 * The code loosely follows the one in the famous
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 * "30 instruction TCP receive" Van Jacobson mail.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005450 *
5451 * Van's trick is to deposit buffers into socket queue
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 * on a device interrupt, to call tcp_recv function
5453 * on the receive process context and checksum and copy
5454 * the buffer to user space. smart...
5455 *
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005456 * Our current scheme is not silly either but we take the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 * extra cost of the net_bh soft interrupt processing...
5458 * We do checksum and copy also but from device to kernel.
5459 */
5460
5461 tp->rx_opt.saw_tstamp = 0;
5462
5463 /* pred_flags is 0xS?10 << 16 + snd_wnd
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08005464 * if header_prediction is to be made
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 * 'S' will always be tp->tcp_header_len >> 2
5466 * '?' will be 0 for the fast path, otherwise pred_flags is 0 to
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005467 * turn it off (when there are holes in the receive
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 * space for instance)
5469 * PSH flag is ignored.
5470 */
5471
5472 if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
John Dykstra96e0bf42009-03-22 21:49:57 -07005473 TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&
5474 !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 int tcp_header_len = tp->tcp_header_len;
5476
5477 /* Timestamp header prediction: tcp_header_len
5478 * is automatically equal to th->doff*4 due to pred_flags
5479 * match.
5480 */
5481
5482 /* Check timestamp */
5483 if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 /* No? Slow path! */
Ilpo Järvinena4356b22008-08-23 05:12:29 -07005485 if (!tcp_parse_aligned_timestamp(tp, th))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 goto slow_path;
5487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 /* If PAWS failed, check it more carefully in slow path */
5489 if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0)
5490 goto slow_path;
5491
5492 /* DO NOT update ts_recent here, if checksum fails
5493 * and timestamp was corrupted part, it will result
5494 * in a hung connection since we will drop all
5495 * future packets due to the PAWS test.
5496 */
5497 }
5498
5499 if (len <= tcp_header_len) {
5500 /* Bulk data transfer: sender */
5501 if (len == tcp_header_len) {
5502 /* Predicted packet is in window by definition.
5503 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
5504 * Hence, check seq<=rcv_wup reduces to:
5505 */
5506 if (tcp_header_len ==
5507 (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
5508 tp->rcv_nxt == tp->rcv_wup)
5509 tcp_store_ts_recent(tp);
5510
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 /* We know that such packets are checksummed
5512 * on entry.
5513 */
5514 tcp_ack(sk, skb, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005515 __kfree_skb(skb);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005516 tcp_data_snd_check(sk);
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005517 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 } else { /* Header too small */
Eric Dumazetc10d9312016-04-29 14:16:47 -07005519 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 goto discard;
5521 }
5522 } else {
5523 int eaten = 0;
Eric Dumazetb081f852012-05-02 09:58:29 +00005524 bool fragstolen = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
Dan Williamsd27f9bc2013-12-30 11:37:15 -08005526 if (tp->ucopy.task == current &&
5527 tp->copied_seq == tp->rcv_nxt &&
5528 len - tcp_header_len <= tp->ucopy.len &&
5529 sock_owned_by_user(sk)) {
5530 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
Dan Williamsd27f9bc2013-12-30 11:37:15 -08005532 if (!tcp_copy_to_iovec(sk, skb, tcp_header_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 /* Predicted packet is in window by definition.
5534 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
5535 * Hence, check seq<=rcv_wup reduces to:
5536 */
5537 if (tcp_header_len ==
5538 (sizeof(struct tcphdr) +
5539 TCPOLEN_TSTAMP_ALIGNED) &&
5540 tp->rcv_nxt == tp->rcv_wup)
5541 tcp_store_ts_recent(tp);
5542
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005543 tcp_rcv_rtt_measure_ts(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544
5545 __skb_pull(skb, tcp_header_len);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07005546 tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
Eric Dumazetc10d9312016-04-29 14:16:47 -07005547 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005548 LINUX_MIB_TCPHPHITSTOUSER);
Dan Williamsd27f9bc2013-12-30 11:37:15 -08005549 eaten = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 }
5551 }
5552 if (!eaten) {
Eric Dumazetfb3477c2016-04-29 14:16:48 -07005553 if (tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 goto csum_error;
5555
Neal Cardwellaab2b4b2013-03-04 06:23:05 +00005556 if ((int)skb->truesize > sk->sk_forward_alloc)
5557 goto step5;
5558
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 /* Predicted packet is in window by definition.
5560 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
5561 * Hence, check seq<=rcv_wup reduces to:
5562 */
5563 if (tcp_header_len ==
5564 (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
5565 tp->rcv_nxt == tp->rcv_wup)
5566 tcp_store_ts_recent(tp);
5567
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005568 tcp_rcv_rtt_measure_ts(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569
Eric Dumazetc10d9312016-04-29 14:16:47 -07005570 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPHITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
5572 /* Bulk data transfer: receiver */
Eric Dumazetb081f852012-05-02 09:58:29 +00005573 eaten = tcp_queue_rcv(sk, skb, tcp_header_len,
5574 &fragstolen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 }
5576
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005577 tcp_event_data_recv(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
5579 if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
5580 /* Well, only one small jumplet in fast path... */
5581 tcp_ack(sk, skb, FLAG_DATA);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005582 tcp_data_snd_check(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005583 if (!inet_csk_ack_scheduled(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 goto no_ack;
5585 }
5586
Dan Williamsd27f9bc2013-12-30 11:37:15 -08005587 __tcp_ack_snd_check(sk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588no_ack:
5589 if (eaten)
Eric Dumazetb081f852012-05-02 09:58:29 +00005590 kfree_skb_partial(skb, fragstolen);
David S. Miller676d2362014-04-11 16:15:36 -04005591 sk->sk_data_ready(sk);
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005592 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 }
5594 }
5595
5596slow_path:
Eric Dumazetfb3477c2016-04-29 14:16:48 -07005597 if (len < (th->doff << 2) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 goto csum_error;
5599
Calvin Owens0c228e82014-11-20 15:09:53 -08005600 if (!th->ack && !th->rst && !th->syn)
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00005601 goto discard;
5602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 * Standard slow path.
5605 */
5606
Eric Dumazet0c246042012-07-17 01:41:30 +00005607 if (!tcp_validate_incoming(sk, skb, th, 1))
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005608 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
5610step5:
Eric Dumazet12fb3dd2013-04-19 07:19:48 +00005611 if (tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0)
John Dykstra96e0bf42009-03-22 21:49:57 -07005612 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005614 tcp_rcv_rtt_measure_ts(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
5616 /* Process urgent data. */
5617 tcp_urg(sk, skb, th);
5618
5619 /* step 7: process the segment text */
5620 tcp_data_queue(sk, skb);
5621
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005622 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 tcp_ack_snd_check(sk);
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005624 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
5626csum_error:
Eric Dumazetc10d9312016-04-29 14:16:47 -07005627 TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
5628 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629
5630discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005631 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00005633EXPORT_SYMBOL(tcp_rcv_established);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005635void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
5636{
5637 struct tcp_sock *tp = tcp_sk(sk);
5638 struct inet_connection_sock *icsk = inet_csk(sk);
5639
5640 tcp_set_state(sk, TCP_ESTABLISHED);
Eric Dumazet9e13bce2017-03-22 08:10:21 -07005641 icsk->icsk_ack.lrcvtime = tcp_time_stamp;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005642
Ian Morris00db4122015-04-03 09:17:27 +01005643 if (skb) {
Eric Dumazet5d299f32012-08-06 05:09:33 +00005644 icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005645 security_inet_conn_established(sk, skb);
David S. Miller41063e92012-06-19 21:22:05 -07005646 }
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005647
5648 /* Make sure socket is routed, for correct metrics. */
5649 icsk->icsk_af_ops->rebuild_header(sk);
5650
5651 tcp_init_metrics(sk);
5652
5653 tcp_init_congestion_control(sk);
5654
5655 /* Prevent spurious tcp_cwnd_restart() on first data
5656 * packet.
5657 */
5658 tp->lsndtime = tcp_time_stamp;
5659
5660 tcp_init_buffer_space(sk);
5661
5662 if (sock_flag(sk, SOCK_KEEPOPEN))
5663 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp));
5664
5665 if (!tp->rx_opt.snd_wscale)
5666 __tcp_fast_path_on(tp, tp->snd_wnd);
5667 else
5668 tp->pred_flags = 0;
5669
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005670}
5671
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005672static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
5673 struct tcp_fastopen_cookie *cookie)
5674{
5675 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng67da22d2012-07-19 06:43:11 +00005676 struct sk_buff *data = tp->syn_data ? tcp_write_queue_head(sk) : NULL;
Daniel Lee2646c832015-04-06 14:37:27 -07005677 u16 mss = tp->rx_opt.mss_clamp, try_exp = 0;
5678 bool syn_drop = false;
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005679
5680 if (mss == tp->rx_opt.user_mss) {
5681 struct tcp_options_received opt;
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005682
5683 /* Get original SYNACK MSS value if user MSS sets mss_clamp */
5684 tcp_clear_options(&opt);
5685 opt.user_mss = opt.mss_clamp = 0;
Christoph Paasch1a2c6182013-03-17 08:23:34 +00005686 tcp_parse_options(synack, &opt, 0, NULL);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005687 mss = opt.mss_clamp;
5688 }
5689
Daniel Lee2646c832015-04-06 14:37:27 -07005690 if (!tp->syn_fastopen) {
5691 /* Ignore an unsolicited cookie */
Yuchung Cheng67da22d2012-07-19 06:43:11 +00005692 cookie->len = -1;
Daniel Lee2646c832015-04-06 14:37:27 -07005693 } else if (tp->total_retrans) {
5694 /* SYN timed out and the SYN-ACK neither has a cookie nor
5695 * acknowledges data. Presumably the remote received only
5696 * the retransmitted (regular) SYNs: either the original
5697 * SYN-data or the corresponding SYN-ACK was dropped.
5698 */
5699 syn_drop = (cookie->len < 0 && data);
5700 } else if (cookie->len < 0 && !tp->syn_data) {
5701 /* We requested a cookie but didn't get it. If we did not use
5702 * the (old) exp opt format then try so next time (try_exp=1).
5703 * Otherwise we go back to use the RFC7413 opt (try_exp=2).
5704 */
5705 try_exp = tp->syn_fastopen_exp ? 2 : 1;
5706 }
Yuchung Cheng67da22d2012-07-19 06:43:11 +00005707
Daniel Lee2646c832015-04-06 14:37:27 -07005708 tcp_fastopen_cache_set(sk, mss, cookie, syn_drop, try_exp);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005709
5710 if (data) { /* Retransmit unacked data in SYN */
Yuchung Cheng93b174a2012-12-06 08:45:32 +00005711 tcp_for_write_queue_from(data, sk) {
5712 if (data == tcp_send_head(sk) ||
Eric Dumazet10d3be52016-04-21 10:55:23 -07005713 __tcp_retransmit_skb(sk, data, 1))
Yuchung Cheng93b174a2012-12-06 08:45:32 +00005714 break;
5715 }
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005716 tcp_rearm_rto(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07005717 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005718 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005719 return true;
5720 }
Yuchung Cheng6f736012012-10-19 15:14:44 +00005721 tp->syn_data_acked = tp->syn_data;
Yuchung Chengf19c29e2014-03-03 12:31:36 -08005722 if (tp->syn_data_acked)
Eric Dumazetc10d9312016-04-29 14:16:47 -07005723 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005724 LINUX_MIB_TCPFASTOPENACTIVE);
Eric Dumazet61d2bca2016-02-01 21:03:07 -08005725
5726 tcp_fastopen_add_skb(sk, synack);
5727
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005728 return false;
5729}
5730
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
Eric Dumazetbda07a62015-09-29 07:42:40 -07005732 const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733{
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005734 struct inet_connection_sock *icsk = inet_csk(sk);
William Allen Simpson4957faade2009-12-02 18:25:27 +00005735 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005736 struct tcp_fastopen_cookie foc = { .len = -1 };
William Allen Simpson4957faade2009-12-02 18:25:27 +00005737 int saved_clamp = tp->rx_opt.mss_clamp;
Eric Dumazet5f634312017-04-18 09:45:52 -07005738 bool fastopen_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
Christoph Paasch1a2c6182013-03-17 08:23:34 +00005740 tcp_parse_options(skb, &tp->rx_opt, 0, &foc);
Andrew Vagine3e12022013-08-27 12:21:55 +04005741 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
Andrey Vaginee684b62013-02-11 05:50:19 +00005742 tp->rx_opt.rcv_tsecr -= tp->tsoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
5744 if (th->ack) {
5745 /* rfc793:
5746 * "If the state is SYN-SENT then
5747 * first check the ACK bit
5748 * If the ACK bit is set
5749 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
5750 * a reset (unless the RST bit is set, if so drop
5751 * the segment and return)"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 */
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005753 if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
5754 after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 goto reset_and_undo;
5756
5757 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
5758 !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp,
5759 tcp_time_stamp)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07005760 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005761 LINUX_MIB_PAWSACTIVEREJECTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 goto reset_and_undo;
5763 }
5764
5765 /* Now ACK is acceptable.
5766 *
5767 * "If the RST bit is set
5768 * If the ACK was acceptable then signal the user "error:
5769 * connection reset", drop the segment, enter CLOSED state,
5770 * delete TCB, and return."
5771 */
5772
5773 if (th->rst) {
5774 tcp_reset(sk);
5775 goto discard;
5776 }
5777
5778 /* rfc793:
5779 * "fifth, if neither of the SYN or RST bits is set then
5780 * drop the segment and return."
5781 *
5782 * See note below!
5783 * --ANK(990513)
5784 */
5785 if (!th->syn)
5786 goto discard_and_undo;
5787
5788 /* rfc793:
5789 * "If the SYN bit is on ...
5790 * are acceptable then ...
5791 * (our SYN has been ACKed), change the connection
5792 * state to ESTABLISHED..."
5793 */
5794
Florian Westphal735d3832014-09-29 13:08:30 +02005795 tcp_ecn_rcv_synack(tp, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
Razvan Ghitulete1b3a6922012-08-14 16:30:20 +03005797 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 tcp_ack(sk, skb, FLAG_SLOWPATH);
5799
5800 /* Ok.. it's good. Set up sequence numbers and
5801 * move to established.
5802 */
5803 tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
5804 tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
5805
5806 /* RFC1323: The window in SYN & SYN/ACK segments is
5807 * never scaled.
5808 */
5809 tp->snd_wnd = ntohs(th->window);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
5811 if (!tp->rx_opt.wscale_ok) {
5812 tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0;
5813 tp->window_clamp = min(tp->window_clamp, 65535U);
5814 }
5815
5816 if (tp->rx_opt.saw_tstamp) {
5817 tp->rx_opt.tstamp_ok = 1;
5818 tp->tcp_header_len =
5819 sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
5820 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
5821 tcp_store_ts_recent(tp);
5822 } else {
5823 tp->tcp_header_len = sizeof(struct tcphdr);
5824 }
5825
Ilpo Järvinene60402d2007-08-09 15:14:46 +03005826 if (tcp_is_sack(tp) && sysctl_tcp_fack)
5827 tcp_enable_fack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
John Heffner5d424d52006-03-20 17:53:41 -08005829 tcp_mtup_init(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005830 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 tcp_initialize_rcv_mss(sk);
5832
5833 /* Remember, tcp_poll() does not lock socket!
5834 * Change state from SYN-SENT only after copied_seq
5835 * is initialized. */
5836 tp->copied_seq = tp->rcv_nxt;
William Allen Simpson4957faade2009-12-02 18:25:27 +00005837
Ralf Baechlee16aa202006-12-07 00:11:33 -08005838 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005840 tcp_finish_connect(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
Eric Dumazet5f634312017-04-18 09:45:52 -07005842 fastopen_fail = (tp->syn_fastopen || tp->syn_data) &&
5843 tcp_rcv_fastopen_synack(sk, skb, &foc);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005844
Eric Dumazet5f634312017-04-18 09:45:52 -07005845 if (!sock_flag(sk, SOCK_DEAD)) {
5846 sk->sk_state_change(sk);
5847 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
5848 }
5849 if (fastopen_fail)
5850 return -1;
Arnaldo Carvalho de Melo295f7322005-08-09 20:11:56 -07005851 if (sk->sk_write_pending ||
5852 icsk->icsk_accept_queue.rskq_defer_accept ||
5853 icsk->icsk_ack.pingpong) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 /* Save one ACK. Data will be ready after
5855 * several ticks, if write_pending is set.
5856 *
5857 * It may be deleted, but with this feature tcpdumps
5858 * look so _wonderfully_ clever, that I was not able
5859 * to stand against the temptation 8) --ANK
5860 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005861 inet_csk_schedule_ack(sk);
Eric Dumazet90cf17d2018-05-21 15:08:56 -07005862 tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07005863 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
5864 TCP_DELACK_MAX, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
5866discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005867 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 return 0;
5869 } else {
5870 tcp_send_ack(sk);
5871 }
5872 return -1;
5873 }
5874
5875 /* No ACK in the segment */
5876
5877 if (th->rst) {
5878 /* rfc793:
5879 * "If the RST bit is set
5880 *
5881 * Otherwise (no ACK) drop the segment and return."
5882 */
5883
5884 goto discard_and_undo;
5885 }
5886
5887 /* PAWS check. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005888 if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp &&
Ilpo Järvinenc887e6d2009-03-14 14:23:03 +00005889 tcp_paws_reject(&tp->rx_opt, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 goto discard_and_undo;
5891
5892 if (th->syn) {
5893 /* We see SYN without ACK. It is attempt of
5894 * simultaneous connect with crossed SYNs.
5895 * Particularly, it can be connect to self.
5896 */
5897 tcp_set_state(sk, TCP_SYN_RECV);
5898
5899 if (tp->rx_opt.saw_tstamp) {
5900 tp->rx_opt.tstamp_ok = 1;
5901 tcp_store_ts_recent(tp);
5902 tp->tcp_header_len =
5903 sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
5904 } else {
5905 tp->tcp_header_len = sizeof(struct tcphdr);
5906 }
5907
5908 tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
Eric Dumazet142a2e72015-11-26 08:18:14 -08005909 tp->copied_seq = tp->rcv_nxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
5911
5912 /* RFC1323: The window in SYN & SYN/ACK segments is
5913 * never scaled.
5914 */
5915 tp->snd_wnd = ntohs(th->window);
5916 tp->snd_wl1 = TCP_SKB_CB(skb)->seq;
5917 tp->max_window = tp->snd_wnd;
5918
Florian Westphal735d3832014-09-29 13:08:30 +02005919 tcp_ecn_rcv_syn(tp, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
John Heffner5d424d52006-03-20 17:53:41 -08005921 tcp_mtup_init(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005922 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 tcp_initialize_rcv_mss(sk);
5924
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 tcp_send_synack(sk);
5926#if 0
5927 /* Note, we could accept data and URG from this segment.
Jerry Chu168a8f52012-08-31 12:29:13 +00005928 * There are no obstacles to make this (except that we must
5929 * either change tcp_recvmsg() to prevent it from returning data
5930 * before 3WHS completes per RFC793, or employ TCP Fast Open).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 *
5932 * However, if we ignore data in ACKless segments sometimes,
5933 * we have no reasons to accept it sometimes.
5934 * Also, seems the code doing it in step6 of tcp_rcv_state_process
5935 * is not flawless. So, discard packet for sanity.
5936 * Uncomment this return to process the data.
5937 */
5938 return -1;
5939#else
5940 goto discard;
5941#endif
5942 }
5943 /* "fifth, if neither of the SYN or RST bits is set then
5944 * drop the segment and return."
5945 */
5946
5947discard_and_undo:
5948 tcp_clear_options(&tp->rx_opt);
5949 tp->rx_opt.mss_clamp = saved_clamp;
5950 goto discard;
5951
5952reset_and_undo:
5953 tcp_clear_options(&tp->rx_opt);
5954 tp->rx_opt.mss_clamp = saved_clamp;
5955 return 1;
5956}
5957
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958/*
5959 * This function implements the receiving procedure of RFC 793 for
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005960 * all states except ESTABLISHED and TIME_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 * It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
5962 * address independent.
5963 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005964
Eric Dumazet72ab4a82015-09-29 07:42:41 -07005965int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966{
5967 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08005968 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet72ab4a82015-09-29 07:42:41 -07005969 const struct tcphdr *th = tcp_hdr(skb);
Jerry Chu168a8f52012-08-31 12:29:13 +00005970 struct request_sock *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 int queued = 0;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005972 bool acceptable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 switch (sk->sk_state) {
5975 case TCP_CLOSE:
5976 goto discard;
5977
5978 case TCP_LISTEN:
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005979 if (th->ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 return 1;
5981
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005982 if (th->rst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 goto discard;
5984
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005985 if (th->syn) {
Eric Dumazetfdf5af02011-12-02 23:41:42 +00005986 if (th->fin)
5987 goto discard;
Eric Dumazetbbaeb9b2017-03-01 08:39:49 -08005988 /* It is possible that we process SYN packets from backlog,
Eric Dumazet4cded0a2018-10-01 15:02:26 -07005989 * so we need to make sure to disable BH and RCU right there.
Eric Dumazetbbaeb9b2017-03-01 08:39:49 -08005990 */
Eric Dumazet4cded0a2018-10-01 15:02:26 -07005991 rcu_read_lock();
Eric Dumazetbbaeb9b2017-03-01 08:39:49 -08005992 local_bh_disable();
5993 acceptable = icsk->icsk_af_ops->conn_request(sk, skb) >= 0;
5994 local_bh_enable();
Eric Dumazet4cded0a2018-10-01 15:02:26 -07005995 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
Eric Dumazetbbaeb9b2017-03-01 08:39:49 -08005997 if (!acceptable)
5998 return 1;
Eric Dumazet0aea76d2016-04-21 22:13:01 -07005999 consume_skb(skb);
Masayuki Nakagawafb7e2392007-01-23 20:15:06 -08006000 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 }
6002 goto discard;
6003
6004 case TCP_SYN_SENT:
Eric Dumazet8804b272016-04-13 22:05:40 -07006005 tp->rx_opt.saw_tstamp = 0;
Eric Dumazetbda07a62015-09-29 07:42:40 -07006006 queued = tcp_rcv_synsent_state_process(sk, skb, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 if (queued >= 0)
6008 return queued;
6009
6010 /* Do step6 onward by hand. */
6011 tcp_urg(sk, skb, th);
6012 __kfree_skb(skb);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07006013 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 return 0;
6015 }
6016
Eric Dumazet8804b272016-04-13 22:05:40 -07006017 tp->rx_opt.saw_tstamp = 0;
Jerry Chu168a8f52012-08-31 12:29:13 +00006018 req = tp->fastopen_rsk;
Ian Morris00db4122015-04-03 09:17:27 +01006019 if (req) {
Jerry Chu37561f62012-10-22 11:26:36 +00006020 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
Jerry Chu168a8f52012-08-31 12:29:13 +00006021 sk->sk_state != TCP_FIN_WAIT1);
6022
Ian Morris51456b22015-04-03 09:17:26 +01006023 if (!tcp_check_req(sk, skb, req, true))
Jerry Chu168a8f52012-08-31 12:29:13 +00006024 goto discard;
Neal Cardwelle69bebd2012-09-22 04:18:57 +00006025 }
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00006026
Calvin Owens0c228e82014-11-20 15:09:53 -08006027 if (!th->ack && !th->rst && !th->syn)
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00006028 goto discard;
6029
Neal Cardwelle69bebd2012-09-22 04:18:57 +00006030 if (!tcp_validate_incoming(sk, skb, th, 0))
Eric Dumazet0c246042012-07-17 01:41:30 +00006031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
6033 /* step 5: check the ACK field */
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006034 acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
Eric Dumazet71c0b332017-05-23 15:24:46 -07006035 FLAG_UPDATE_TS_RECENT |
6036 FLAG_NO_CHALLENGE_ACK) > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
Eric Dumazet71c0b332017-05-23 15:24:46 -07006038 if (!acceptable) {
6039 if (sk->sk_state == TCP_SYN_RECV)
6040 return 1; /* send one RST */
6041 tcp_send_challenge_ack(sk, skb);
6042 goto discard;
6043 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006044 switch (sk->sk_state) {
6045 case TCP_SYN_RECV:
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07006046 if (!tp->srtt_us)
6047 tcp_synack_rtt_meas(sk, req);
6048
Joe Perches61eb90032013-05-24 18:36:13 +00006049 /* Once we leave TCP_SYN_RECV, we no longer need req
6050 * so release it.
6051 */
6052 if (req) {
Yuchung Cheng7e32b442016-09-21 16:16:15 -07006053 inet_csk(sk)->icsk_retransmits = 0;
Joe Perches61eb90032013-05-24 18:36:13 +00006054 reqsk_fastopen_remove(sk, req, false);
6055 } else {
6056 /* Make sure socket is routed, for correct metrics. */
6057 icsk->icsk_af_ops->rebuild_header(sk);
6058 tcp_init_congestion_control(sk);
6059
6060 tcp_mtup_init(sk);
Joe Perches61eb90032013-05-24 18:36:13 +00006061 tp->copied_seq = tp->rcv_nxt;
Eric Dumazetb0983d32013-09-20 13:56:58 -07006062 tcp_init_buffer_space(sk);
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006063 }
Joe Perches61eb90032013-05-24 18:36:13 +00006064 smp_mb();
6065 tcp_set_state(sk, TCP_ESTABLISHED);
6066 sk->sk_state_change(sk);
6067
6068 /* Note, that this wakeup is only for marginal crossed SYN case.
6069 * Passively open sockets are not waked up, because
6070 * sk->sk_sleep == NULL and sk->sk_socket == NULL.
6071 */
6072 if (sk->sk_socket)
6073 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
6074
6075 tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
6076 tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
6077 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
6078
6079 if (tp->rx_opt.tstamp_ok)
6080 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
6081
6082 if (req) {
6083 /* Re-arm the timer because data may have been sent out.
6084 * This is similar to the regular data transmission case
6085 * when new data has just been ack'ed.
6086 *
6087 * (TFO) - we could try to be more aggressive and
6088 * retransmitting any data sooner based on when they
6089 * are sent out.
6090 */
6091 tcp_rearm_rto(sk);
6092 } else
6093 tcp_init_metrics(sk);
6094
Yuchung Chengc0402762016-09-19 23:39:21 -04006095 if (!inet_csk(sk)->icsk_ca_ops->cong_control)
6096 tcp_update_pacing_rate(sk);
Neal Cardwell02cf4eb2013-10-21 15:40:19 -04006097
Joe Perches61eb90032013-05-24 18:36:13 +00006098 /* Prevent spurious tcp_cwnd_restart() on first data packet */
6099 tp->lsndtime = tcp_time_stamp;
6100
6101 tcp_initialize_rcv_mss(sk);
6102 tcp_fast_path_on(tp);
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006103 break;
6104
Joe Perchesc48b22d2013-05-24 18:06:58 +00006105 case TCP_FIN_WAIT1: {
6106 struct dst_entry *dst;
6107 int tmo;
6108
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006109 /* If we enter the TCP_FIN_WAIT1 state and we are a
6110 * Fast Open socket and this is the first acceptable
6111 * ACK we have received, this would have acknowledged
6112 * our SYNACK so stop the SYNACK timer.
6113 */
Ian Morris00db4122015-04-03 09:17:27 +01006114 if (req) {
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006115 /* We no longer need the request sock. */
6116 reqsk_fastopen_remove(sk, req, false);
6117 tcp_rearm_rto(sk);
6118 }
Joe Perchesc48b22d2013-05-24 18:06:58 +00006119 if (tp->snd_una != tp->write_seq)
6120 break;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006121
Joe Perchesc48b22d2013-05-24 18:06:58 +00006122 tcp_set_state(sk, TCP_FIN_WAIT2);
6123 sk->sk_shutdown |= SEND_SHUTDOWN;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006124
Joe Perchesc48b22d2013-05-24 18:06:58 +00006125 dst = __sk_dst_get(sk);
6126 if (dst)
6127 dst_confirm(dst);
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006128
Joe Perchesc48b22d2013-05-24 18:06:58 +00006129 if (!sock_flag(sk, SOCK_DEAD)) {
6130 /* Wake up lingering close() */
6131 sk->sk_state_change(sk);
6132 break;
6133 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006134
Joe Perchesc48b22d2013-05-24 18:06:58 +00006135 if (tp->linger2 < 0 ||
6136 (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
6137 after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) {
6138 tcp_done(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07006139 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
Joe Perchesc48b22d2013-05-24 18:06:58 +00006140 return 1;
6141 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006142
Joe Perchesc48b22d2013-05-24 18:06:58 +00006143 tmo = tcp_fin_time(sk);
6144 if (tmo > TCP_TIMEWAIT_LEN) {
6145 inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
6146 } else if (th->fin || sock_owned_by_user(sk)) {
6147 /* Bad case. We could lose such FIN otherwise.
6148 * It is not a big problem, but it looks confusing
6149 * and not so rare event. We still can lose it now,
6150 * if it spins in bh_lock_sock(), but it is really
6151 * marginal case.
6152 */
6153 inet_csk_reset_keepalive_timer(sk, tmo);
6154 } else {
6155 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
6156 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006158 break;
Joe Perchesc48b22d2013-05-24 18:06:58 +00006159 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006160
6161 case TCP_CLOSING:
6162 if (tp->snd_una == tp->write_seq) {
6163 tcp_time_wait(sk, TCP_TIME_WAIT, 0);
6164 goto discard;
6165 }
6166 break;
6167
6168 case TCP_LAST_ACK:
6169 if (tp->snd_una == tp->write_seq) {
6170 tcp_update_metrics(sk);
6171 tcp_done(sk);
6172 goto discard;
6173 }
6174 break;
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00006175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
6177 /* step 6: check the URG bit */
6178 tcp_urg(sk, skb, th);
6179
6180 /* step 7: process the segment text */
6181 switch (sk->sk_state) {
6182 case TCP_CLOSE_WAIT:
6183 case TCP_CLOSING:
6184 case TCP_LAST_ACK:
6185 if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
6186 break;
6187 case TCP_FIN_WAIT1:
6188 case TCP_FIN_WAIT2:
6189 /* RFC 793 says to queue data in these states,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006190 * RFC 1122 says we MUST send a reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 * BSD 4.4 also does reset.
6192 */
6193 if (sk->sk_shutdown & RCV_SHUTDOWN) {
6194 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
6195 after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07006196 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 tcp_reset(sk);
6198 return 1;
6199 }
6200 }
6201 /* Fall through */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006202 case TCP_ESTABLISHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 tcp_data_queue(sk, skb);
6204 queued = 1;
6205 break;
6206 }
6207
6208 /* tcp_data could move socket to TIME-WAIT */
6209 if (sk->sk_state != TCP_CLOSE) {
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07006210 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 tcp_ack_snd_check(sk);
6212 }
6213
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006214 if (!queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07006216 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 }
6218 return 0;
6219}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220EXPORT_SYMBOL(tcp_rcv_state_process);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006221
6222static inline void pr_drop_req(struct request_sock *req, __u16 port, int family)
6223{
6224 struct inet_request_sock *ireq = inet_rsk(req);
6225
6226 if (family == AF_INET)
Joe Perchesba7a46f2014-11-11 10:59:17 -08006227 net_dbg_ratelimited("drop open request from %pI4/%u\n",
6228 &ireq->ir_rmt_addr, port);
Octavian Purdila4135ab82014-06-28 21:20:54 +03006229#if IS_ENABLED(CONFIG_IPV6)
6230 else if (family == AF_INET6)
Joe Perchesba7a46f2014-11-11 10:59:17 -08006231 net_dbg_ratelimited("drop open request from %pI6/%u\n",
6232 &ireq->ir_v6_rmt_addr, port);
Octavian Purdila4135ab82014-06-28 21:20:54 +03006233#endif
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006234}
6235
Florian Westphald82bd122014-09-29 13:08:29 +02006236/* RFC3168 : 6.1.1 SYN packets must not have ECT/ECN bits set
6237 *
6238 * If we receive a SYN packet with these bits set, it means a
6239 * network is playing bad games with TOS bits. In order to
6240 * avoid possible false congestion notifications, we disable
stephen hemmingerf4e715c2014-10-29 16:05:06 -07006241 * TCP ECN negotiation.
Florian Westphald82bd122014-09-29 13:08:29 +02006242 *
6243 * Exception: tcp_ca wants ECN. This is required for DCTCP
Florian Westphal843c2fd2015-01-30 20:45:20 +01006244 * congestion control: Linux DCTCP asserts ECT on all packets,
6245 * including SYN, which is most optimal solution; however,
6246 * others, such as FreeBSD do not.
Florian Westphald82bd122014-09-29 13:08:29 +02006247 */
6248static void tcp_ecn_create_request(struct request_sock *req,
6249 const struct sk_buff *skb,
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006250 const struct sock *listen_sk,
6251 const struct dst_entry *dst)
Florian Westphald82bd122014-09-29 13:08:29 +02006252{
6253 const struct tcphdr *th = tcp_hdr(skb);
6254 const struct net *net = sock_net(listen_sk);
6255 bool th_ecn = th->ece && th->cwr;
Florian Westphal843c2fd2015-01-30 20:45:20 +01006256 bool ect, ecn_ok;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006257 u32 ecn_ok_dst;
Florian Westphald82bd122014-09-29 13:08:29 +02006258
6259 if (!th_ecn)
6260 return;
6261
6262 ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006263 ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
6264 ecn_ok = net->ipv4.sysctl_tcp_ecn || ecn_ok_dst;
Florian Westphald82bd122014-09-29 13:08:29 +02006265
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006266 if ((!ect && ecn_ok) || tcp_ca_needs_ecn(listen_sk) ||
6267 (ecn_ok_dst & DST_FEATURE_ECN_CA))
Florian Westphald82bd122014-09-29 13:08:29 +02006268 inet_rsk(req)->ecn_ok = 1;
6269}
6270
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006271static void tcp_openreq_init(struct request_sock *req,
6272 const struct tcp_options_received *rx_opt,
6273 struct sk_buff *skb, const struct sock *sk)
6274{
6275 struct inet_request_sock *ireq = inet_rsk(req);
6276
Eric Dumazeted53d0a2015-10-08 19:33:23 -07006277 req->rsk_rcv_wnd = 0; /* So that tcp_send_synack() knows! */
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006278 req->cookie_ts = 0;
6279 tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
6280 tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07006281 skb_mstamp_get(&tcp_rsk(req)->snt_synack);
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006282 tcp_rsk(req)->last_oow_ack_time = 0;
6283 req->mss = rx_opt->mss_clamp;
6284 req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
6285 ireq->tstamp_ok = rx_opt->tstamp_ok;
6286 ireq->sack_ok = rx_opt->sack_ok;
6287 ireq->snd_wscale = rx_opt->snd_wscale;
6288 ireq->wscale_ok = rx_opt->wscale_ok;
6289 ireq->acked = 0;
6290 ireq->ecn_ok = 0;
6291 ireq->ir_rmt_port = tcp_hdr(skb)->source;
6292 ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
6293 ireq->ir_mark = inet_request_mark(sk, skb);
6294}
6295
Eric Dumazete49bb332015-03-17 18:32:27 -07006296struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
Eric Dumazeta1a53442015-10-04 21:08:11 -07006297 struct sock *sk_listener,
6298 bool attach_listener)
Eric Dumazete49bb332015-03-17 18:32:27 -07006299{
Eric Dumazeta1a53442015-10-04 21:08:11 -07006300 struct request_sock *req = reqsk_alloc(ops, sk_listener,
6301 attach_listener);
Eric Dumazete49bb332015-03-17 18:32:27 -07006302
6303 if (req) {
6304 struct inet_request_sock *ireq = inet_rsk(req);
6305
6306 kmemcheck_annotate_bitfield(ireq, flags);
Eric Dumazet2ffd2612017-10-20 09:04:13 -07006307 ireq->ireq_opt = NULL;
Huw Davies56ac42b2016-06-27 15:05:28 -04006308#if IS_ENABLED(CONFIG_IPV6)
6309 ireq->pktopts = NULL;
6310#endif
Eric Dumazete49bb332015-03-17 18:32:27 -07006311 atomic64_set(&ireq->ir_cookie, 0);
6312 ireq->ireq_state = TCP_NEW_SYN_RECV;
6313 write_pnet(&ireq->ireq_net, sock_net(sk_listener));
Eric Dumazet0144a812015-03-24 21:45:56 -07006314 ireq->ireq_family = sk_listener->sk_family;
Eric Dumazete49bb332015-03-17 18:32:27 -07006315 }
6316
6317 return req;
6318}
6319EXPORT_SYMBOL(inet_reqsk_alloc);
6320
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006321/*
6322 * Return true if a syncookie should be sent
6323 */
Eric Dumazet2985aaa2015-09-29 07:42:51 -07006324static bool tcp_syn_flood_action(const struct sock *sk,
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006325 const struct sk_buff *skb,
6326 const char *proto)
6327{
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006328 struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006329 const char *msg = "Dropping request";
6330 bool want_cookie = false;
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006331 struct net *net = sock_net(sk);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006332
6333#ifdef CONFIG_SYN_COOKIES
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006334 if (net->ipv4.sysctl_tcp_syncookies) {
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006335 msg = "Sending cookies";
6336 want_cookie = true;
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07006337 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006338 } else
6339#endif
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07006340 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006341
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006342 if (!queue->synflood_warned &&
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006343 net->ipv4.sysctl_tcp_syncookies != 2 &&
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006344 xchg(&queue->synflood_warned, 1) == 0)
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006345 pr_info("%s: Possible SYN flooding on port %d. %s. Check SNMP counters.\n",
6346 proto, ntohs(tcp_hdr(skb)->dest), msg);
Eric Dumazet2985aaa2015-09-29 07:42:51 -07006347
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006348 return want_cookie;
6349}
6350
Eric Dumazetcd8ae852015-05-03 21:34:46 -07006351static void tcp_reqsk_record_syn(const struct sock *sk,
6352 struct request_sock *req,
6353 const struct sk_buff *skb)
6354{
6355 if (tcp_sk(sk)->save_syn) {
6356 u32 len = skb_network_header_len(skb) + tcp_hdrlen(skb);
6357 u32 *copy;
6358
6359 copy = kmalloc(len + sizeof(u32), GFP_ATOMIC);
6360 if (copy) {
6361 copy[0] = len;
6362 memcpy(&copy[1], skb_network_header(skb), len);
6363 req->saved_syn = copy;
6364 }
6365 }
6366}
6367
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006368int tcp_conn_request(struct request_sock_ops *rsk_ops,
6369 const struct tcp_request_sock_ops *af_ops,
6370 struct sock *sk, struct sk_buff *skb)
6371{
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006372 struct tcp_fastopen_cookie foc = { .len = -1 };
Eric Dumazet7c85af82015-09-24 17:16:05 -07006373 __u32 isn = TCP_SKB_CB(skb)->tcp_tw_isn;
6374 struct tcp_options_received tmp_opt;
6375 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006376 struct net *net = sock_net(sk);
Eric Dumazet7c85af82015-09-24 17:16:05 -07006377 struct sock *fastopen_sk = NULL;
6378 struct dst_entry *dst = NULL;
6379 struct request_sock *req;
6380 bool want_cookie = false;
6381 struct flowi fl;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006382
6383 /* TW buckets are converted to open requests without
6384 * limitations, they conserve resources and peer is
6385 * evidently real one.
6386 */
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006387 if ((net->ipv4.sysctl_tcp_syncookies == 2 ||
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006388 inet_csk_reqsk_queue_is_full(sk)) && !isn) {
6389 want_cookie = tcp_syn_flood_action(sk, skb, rsk_ops->slab_name);
6390 if (!want_cookie)
6391 goto drop;
6392 }
6393
Eric Dumazetdfe4f692016-10-26 09:27:57 -07006394 if (sk_acceptq_is_full(sk)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07006395 NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006396 goto drop;
6397 }
6398
Eric Dumazeta1a53442015-10-04 21:08:11 -07006399 req = inet_reqsk_alloc(rsk_ops, sk, !want_cookie);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006400 if (!req)
6401 goto drop;
6402
6403 tcp_rsk(req)->af_specific = af_ops;
6404
6405 tcp_clear_options(&tmp_opt);
6406 tmp_opt.mss_clamp = af_ops->mss_clamp;
6407 tmp_opt.user_mss = tp->rx_opt.user_mss;
6408 tcp_parse_options(skb, &tmp_opt, 0, want_cookie ? NULL : &foc);
6409
6410 if (want_cookie && !tmp_opt.saw_tstamp)
6411 tcp_clear_options(&tmp_opt);
6412
6413 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
6414 tcp_openreq_init(req, &tmp_opt, skb, sk);
KOVACS Krisztian7a682572016-09-23 11:27:42 +02006415 inet_rsk(req)->no_srccheck = inet_sk(sk)->transparent;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006416
Eric Dumazet16f86162015-03-13 15:51:10 -07006417 /* Note: tcp_v6_init_req() might override ir_iif for link locals */
David Ahern6dd9a142015-12-16 13:20:44 -08006418 inet_rsk(req)->ir_iif = inet_request_bound_dev_if(sk, skb);
Eric Dumazet16f86162015-03-13 15:51:10 -07006419
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006420 af_ops->init_req(req, sk, skb);
6421
6422 if (security_inet_conn_request(sk, skb, req))
6423 goto drop_and_free;
6424
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006425 if (!want_cookie && !isn) {
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006426 /* VJ's idea. We save last timestamp seen
6427 * from the destination in peer table, when entering
6428 * state TIME-WAIT, and check against it before
6429 * accepting new connection request.
6430 *
6431 * If "isn" is not zero, this request hit alive
6432 * timewait bucket, so that all the necessary checks
6433 * are made in the function processing timewait state.
6434 */
Hannes Frederic Sowaa26552a2014-08-14 22:06:12 +02006435 if (tcp_death_row.sysctl_tw_recycle) {
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006436 bool strict;
6437
6438 dst = af_ops->route_req(sk, &fl, req, &strict);
Hannes Frederic Sowaa26552a2014-08-14 22:06:12 +02006439
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006440 if (dst && strict &&
Hannes Frederic Sowaa26552a2014-08-14 22:06:12 +02006441 !tcp_peer_is_proven(req, dst, true,
6442 tmp_opt.saw_tstamp)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07006443 NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006444 goto drop_and_release;
6445 }
6446 }
6447 /* Kill the following clause, if you dislike this way. */
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006448 else if (!net->ipv4.sysctl_tcp_syncookies &&
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006449 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
6450 (sysctl_max_syn_backlog >> 2)) &&
Hannes Frederic Sowaa26552a2014-08-14 22:06:12 +02006451 !tcp_peer_is_proven(req, dst, false,
6452 tmp_opt.saw_tstamp)) {
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006453 /* Without syncookies last quarter of
6454 * backlog is filled with destinations,
6455 * proven to be alive.
6456 * It means that we continue to communicate
6457 * to destinations, already remembered
6458 * to the moment of synflood.
6459 */
6460 pr_drop_req(req, ntohs(tcp_hdr(skb)->source),
6461 rsk_ops->family);
6462 goto drop_and_release;
6463 }
6464
6465 isn = af_ops->init_seq(skb);
6466 }
6467 if (!dst) {
6468 dst = af_ops->route_req(sk, &fl, req, NULL);
6469 if (!dst)
6470 goto drop_and_free;
6471 }
6472
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006473 tcp_ecn_create_request(req, skb, sk, dst);
6474
6475 if (want_cookie) {
6476 isn = cookie_init_sequence(af_ops, sk, skb, &req->mss);
6477 req->cookie_ts = tmp_opt.tstamp_ok;
6478 if (!tmp_opt.tstamp_ok)
6479 inet_rsk(req)->ecn_ok = 0;
6480 }
6481
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006482 tcp_rsk(req)->snt_isn = isn;
Eric Dumazet58d607d2015-09-15 15:24:20 -07006483 tcp_rsk(req)->txhash = net_tx_rndhash();
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006484 tcp_openreq_init_rwin(req, sk, dst);
Eric Dumazetca6fb062015-10-02 11:43:35 -07006485 if (!want_cookie) {
Eric Dumazetca6fb062015-10-02 11:43:35 -07006486 tcp_reqsk_record_syn(sk, req, skb);
Eric Dumazet7656d842015-10-04 21:08:07 -07006487 fastopen_sk = tcp_try_fastopen(sk, skb, req, &foc, dst);
Eric Dumazetca6fb062015-10-02 11:43:35 -07006488 }
Eric Dumazet7c85af82015-09-24 17:16:05 -07006489 if (fastopen_sk) {
Eric Dumazetca6fb062015-10-02 11:43:35 -07006490 af_ops->send_synack(fastopen_sk, dst, &fl, req,
Eric Dumazetb3d05142016-04-13 22:05:39 -07006491 &foc, TCP_SYNACK_FASTOPEN);
Eric Dumazet7656d842015-10-04 21:08:07 -07006492 /* Add the child socket directly into the accept queue */
Guillaume Nault40852d52019-03-08 22:09:47 +01006493 if (!inet_csk_reqsk_queue_add(sk, req, fastopen_sk)) {
6494 reqsk_fastopen_remove(fastopen_sk, req, false);
6495 bh_unlock_sock(fastopen_sk);
6496 sock_put(fastopen_sk);
6497 reqsk_put(req);
6498 goto drop;
6499 }
Eric Dumazet7656d842015-10-04 21:08:07 -07006500 sk->sk_data_ready(sk);
6501 bh_unlock_sock(fastopen_sk);
Eric Dumazet7c85af82015-09-24 17:16:05 -07006502 sock_put(fastopen_sk);
6503 } else {
Eric Dumazet9439ce02015-03-17 18:32:29 -07006504 tcp_rsk(req)->tfo_listener = false;
Eric Dumazetca6fb062015-10-02 11:43:35 -07006505 if (!want_cookie)
6506 inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
Eric Dumazetb3d05142016-04-13 22:05:39 -07006507 af_ops->send_synack(sk, dst, &fl, req, &foc,
6508 !want_cookie ? TCP_SYNACK_NORMAL :
6509 TCP_SYNACK_COOKIE);
Eric Dumazet9caad862016-04-01 08:52:20 -07006510 if (want_cookie) {
6511 reqsk_free(req);
6512 return 0;
6513 }
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006514 }
Eric Dumazetca6fb062015-10-02 11:43:35 -07006515 reqsk_put(req);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006516 return 0;
6517
6518drop_and_release:
6519 dst_release(dst);
6520drop_and_free:
6521 reqsk_free(req);
6522drop:
Eric Dumazet9caad862016-04-01 08:52:20 -07006523 tcp_listendrop(sk);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006524 return 0;
6525}
6526EXPORT_SYMBOL(tcp_conn_request);