blob: ce481325115fe7cb8a3b007276f2ce2a519a7383 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Implementation of the Transmission Control Protocol(TCP).
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Mark Evans, <evansmp@uhura.aston.ac.uk>
11 * Corey Minyard <wf-rch!minyard@relay.EU.net>
12 * Florian La Roche, <flla@stud.uni-sb.de>
13 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
14 * Linus Torvalds, <torvalds@cs.helsinki.fi>
15 * Alan Cox, <gw4pts@gw4pts.ampr.org>
16 * Matthew Dillon, <dillon@apollo.west.oic.com>
17 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
18 * Jorge Cwik, <jorge@laser.satlink.net>
19 */
20
21/*
22 * Changes:
23 * Pedro Roque : Fast Retransmit/Recovery.
24 * Two receive queues.
25 * Retransmit queue handled by TCP.
26 * Better retransmit timer handling.
27 * New congestion avoidance.
28 * Header prediction.
29 * Variable renaming.
30 *
31 * Eric : Fast Retransmit.
32 * Randy Scott : MSS option defines.
33 * Eric Schenk : Fixes to slow start algorithm.
34 * Eric Schenk : Yet another double ACK bug.
35 * Eric Schenk : Delayed ACK bug fixes.
36 * Eric Schenk : Floyd style fast retrans war avoidance.
37 * David S. Miller : Don't allow zero congestion window.
38 * Eric Schenk : Fix retransmitter so that it sends
39 * next packet on ack of previous packet.
40 * Andi Kleen : Moved open_request checking here
41 * and process RSTs for open_requests.
42 * Andi Kleen : Better prune_queue, and other fixes.
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -080043 * Andrey Savochkin: Fix RTT measurements in the presence of
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * timestamps.
45 * Andrey Savochkin: Check sequence numbers correctly when
46 * removing SACKs due to in sequence incoming
47 * data segments.
48 * Andi Kleen: Make sure we never ack data there is not
49 * enough room for. Also make this condition
50 * a fatal error if it might still happen.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090051 * Andi Kleen: Add tcp_measure_rcv_mss to make
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * connections with MSS<min(MTU,ann. MSS)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090053 * work without delayed acks.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * Andi Kleen: Process packets with PSH set in the
55 * fast path.
56 * J Hadi Salim: ECN support
57 * Andrei Gurtov,
58 * Pasi Sarolahti,
59 * Panu Kuhlberg: Experimental audit of TCP (re)transmission
60 * engine. Lots of bugs are found.
61 * Pasi Sarolahti: F-RTO for dealing with spurious RTOs
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
63
Joe Perchesafd465032012-03-12 07:03:32 +000064#define pr_fmt(fmt) "TCP: " fmt
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090067#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/module.h>
69#include <linux/sysctl.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070070#include <linux/kernel.h>
Eric Dumazetad971f62014-10-11 15:17:29 -070071#include <linux/prefetch.h>
Satoru SATOH5ffc02a2008-05-04 22:14:42 -070072#include <net/dst.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/tcp.h>
74#include <net/inet_common.h>
75#include <linux/ipsec.h>
76#include <asm/unaligned.h>
Willem de Bruijne1c8a602014-08-04 22:11:50 -040077#include <linux/errqueue.h>
Song Liu59415212017-10-23 09:20:25 -070078#include <trace/events/tcp.h>
Ursula Braun60e2a772017-10-25 11:01:45 +020079#include <linux/unaligned/access_ok.h>
80#include <linux/static_key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Eric Dumazet282f23c2012-07-17 10:13:05 +020082/* rfc5961 challenge ack rate limiting */
Eric Dumazet75ff39c2016-07-10 10:04:02 +020083int sysctl_tcp_challenge_ack_limit = 1000;
Eric Dumazet282f23c2012-07-17 10:13:05 +020084
Brian Haleyab32ea52006-09-22 14:15:41 -070085int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
Yuchung Chengf6722582015-10-16 21:57:42 -070086int sysctl_tcp_min_rtt_wlen __read_mostly = 300;
Neal Cardwell032ee422015-02-06 16:04:38 -050087int sysctl_tcp_invalid_ratelimit __read_mostly = HZ/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#define FLAG_DATA 0x01 /* Incoming frame contained data. */
90#define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */
91#define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */
92#define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */
93#define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */
94#define FLAG_DATA_SACKED 0x20 /* New SACK. */
95#define FLAG_ECE 0x40 /* ECE in this ACK */
Yuchung Cheng291a00d2015-07-01 14:11:14 -070096#define FLAG_LOST_RETRANS 0x80 /* This ACK marks some retransmission lost */
Florian Westphal31770e32017-08-30 19:24:58 +020097#define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/
Yuchung Chenge33099f2013-03-20 13:33:00 +000098#define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */
Ilpo Järvinen2e605292007-08-02 19:46:58 -070099#define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
Ryousei Takano564262c2007-10-25 23:03:52 -0700100#define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
Neal Cardwelldf92c832017-08-03 09:19:54 -0400101#define FLAG_SET_XMIT_TIMER 0x1000 /* Set TLP or RTO timer */
Ilpo Järvinencadbd032007-12-31 04:49:21 -0800102#define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
Eric Dumazet12fb3dd2013-04-19 07:19:48 +0000103#define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -0700104#define FLAG_NO_CHALLENGE_ACK 0x8000 /* do not call tcp_send_challenge_ack() */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106#define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
107#define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
108#define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE)
109#define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700112#define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Yuchung Chenge662ca42016-02-02 10:33:04 -0800114#define REXMIT_NONE 0 /* no loss recovery to do */
115#define REXMIT_LOST 1 /* retransmit packets marked lost */
116#define REXMIT_NEW 2 /* FRTO-style transmit of unsent/new packets */
117
Marcelo Ricardo Leitner0b9aefe2017-04-01 11:00:21 -0300118static void tcp_gro_dev_warn(struct sock *sk, const struct sk_buff *skb,
119 unsigned int len)
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200120{
121 static bool __once __read_mostly;
122
123 if (!__once) {
124 struct net_device *dev;
125
126 __once = true;
127
128 rcu_read_lock();
129 dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif);
Marcelo Ricardo Leitner0b9aefe2017-04-01 11:00:21 -0300130 if (!dev || len >= dev->mtu)
131 pr_warn("%s: Driver has suspect GRO implementation, TCP performance may be compromised.\n",
132 dev ? dev->name : "Unknown driver");
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200133 rcu_read_unlock();
134 }
135}
136
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900137/* Adapt the MSS value used to make delayed ack decision to the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * real world.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900139 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800140static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700142 struct inet_connection_sock *icsk = inet_csk(sk);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900143 const unsigned int lss = icsk->icsk_ack.last_seg_size;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700144 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900146 icsk->icsk_ack.last_seg_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* skb->len may jitter because of SACKs, even if peer
149 * sends good full-sized frames.
150 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800151 len = skb_shinfo(skb)->gso_size ? : skb->len;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700152 if (len >= icsk->icsk_ack.rcv_mss) {
Marcelo Ricardo Leitnerdcb17d22016-12-05 18:37:13 -0200153 icsk->icsk_ack.rcv_mss = min_t(unsigned int, len,
154 tcp_sk(sk)->advmss);
Marcelo Ricardo Leitner0b9aefe2017-04-01 11:00:21 -0300155 /* Account for possibly-removed options */
156 if (unlikely(len > icsk->icsk_ack.rcv_mss +
157 MAX_TCP_OPTION_SPACE))
158 tcp_gro_dev_warn(sk, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 } else {
160 /* Otherwise, we make more careful check taking into account,
161 * that SACKs block is variable.
162 *
163 * "len" is invariant segment length, including TCP header.
164 */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700165 len += skb->data - skb_transport_header(skb);
William Allen Simpsonbee7ca92009-11-10 09:51:18 +0000166 if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 /* If PSH is not set, packet should be
168 * full sized, provided peer TCP is not badly broken.
169 * This observation (if it is correct 8)) allows
170 * to handle super-low mtu links fairly.
171 */
172 (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700173 !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* Subtract also invariant (if peer is RFC compliant),
175 * tcp header plus fixed timestamp option length.
176 * Resulting "len" is MSS free of SACK jitter.
177 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700178 len -= tcp_sk(sk)->tcp_header_len;
179 icsk->icsk_ack.last_seg_size = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (len == lss) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700181 icsk->icsk_ack.rcv_mss = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return;
183 }
184 }
Alexey Kuznetsov1ef96962006-09-19 12:52:50 -0700185 if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
186 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700187 icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189}
190
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700191static void tcp_incr_quickack(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700193 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000194 unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800196 if (quickacks == 0)
197 quickacks = 2;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700198 if (quickacks > icsk->icsk_ack.quick)
199 icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
stephen hemminger1b9f4092010-09-28 19:30:14 +0000202static void tcp_enter_quickack_mode(struct sock *sk)
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);
205 tcp_incr_quickack(sk);
206 icsk->icsk_ack.pingpong = 0;
207 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/* Send ACKs quickly, if "quick" count is not exhausted
211 * and the session is not interactive.
212 */
213
Jon Maxwell2251ae42015-07-08 10:12:28 +1000214static bool tcp_in_quickack_mode(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700216 const struct inet_connection_sock *icsk = inet_csk(sk);
Jon Maxwell2251ae42015-07-08 10:12:28 +1000217 const struct dst_entry *dst = __sk_dst_get(sk);
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000218
Jon Maxwell2251ae42015-07-08 10:12:28 +1000219 return (dst && dst_metric(dst, RTAX_QUICKACK)) ||
220 (icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Florian Westphal735d3832014-09-29 13:08:30 +0200223static void tcp_ecn_queue_cwr(struct tcp_sock *tp)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700224{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800225 if (tp->ecn_flags & TCP_ECN_OK)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700226 tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
227}
228
Florian Westphal735d3832014-09-29 13:08:30 +0200229static void tcp_ecn_accept_cwr(struct tcp_sock *tp, const struct sk_buff *skb)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700230{
231 if (tcp_hdr(skb)->cwr)
232 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
233}
234
Florian Westphal735d3832014-09-29 13:08:30 +0200235static void tcp_ecn_withdraw_cwr(struct tcp_sock *tp)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700236{
237 tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
238}
239
Florian Westphal735d3832014-09-29 13:08:30 +0200240static void __tcp_ecn_check_ce(struct tcp_sock *tp, const struct sk_buff *skb)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700241{
Eric Dumazetb82d1bb2011-09-27 02:20:08 -0400242 switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000243 case INET_ECN_NOT_ECT:
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700244 /* Funny extension: if ECT is not set on a segment,
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000245 * and we already seen ECT on a previous segment,
246 * it is probably a retransmit.
247 */
248 if (tp->ecn_flags & TCP_ECN_SEEN)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700249 tcp_enter_quickack_mode((struct sock *)tp);
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000250 break;
251 case INET_ECN_CE:
Florian Westphal98900922014-09-26 22:37:35 +0200252 if (tcp_ca_needs_ecn((struct sock *)tp))
253 tcp_ca_event((struct sock *)tp, CA_EVENT_ECN_IS_CE);
254
Eric Dumazetaae06bf2012-08-06 11:04:43 +0000255 if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) {
256 /* Better not delay acks, sender can have a very low cwnd */
257 tcp_enter_quickack_mode((struct sock *)tp);
258 tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
259 }
Eric Dumazet7a269ff2011-09-22 20:02:19 +0000260 tp->ecn_flags |= TCP_ECN_SEEN;
Florian Westphal98900922014-09-26 22:37:35 +0200261 break;
262 default:
263 if (tcp_ca_needs_ecn((struct sock *)tp))
264 tcp_ca_event((struct sock *)tp, CA_EVENT_ECN_NO_CE);
265 tp->ecn_flags |= TCP_ECN_SEEN;
266 break;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700267 }
268}
269
Florian Westphal735d3832014-09-29 13:08:30 +0200270static void tcp_ecn_check_ce(struct tcp_sock *tp, const struct sk_buff *skb)
271{
272 if (tp->ecn_flags & TCP_ECN_OK)
273 __tcp_ecn_check_ce(tp, skb);
274}
275
276static void tcp_ecn_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700277{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800278 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700279 tp->ecn_flags &= ~TCP_ECN_OK;
280}
281
Florian Westphal735d3832014-09-29 13:08:30 +0200282static void tcp_ecn_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700283{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800284 if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700285 tp->ecn_flags &= ~TCP_ECN_OK;
286}
287
Florian Westphal735d3832014-09-29 13:08:30 +0200288static bool tcp_ecn_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700289{
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800290 if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
Eric Dumazeta2a385d2012-05-16 23:15:34 +0000291 return true;
292 return false;
Ilpo Järvinenbdf1ee52007-05-27 02:04:16 -0700293}
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295/* Buffer size and advertised window tuning.
296 *
297 * 1. Tuning sk->sk_sndbuf, when connection enters established state.
298 */
299
Eric Dumazet6ae70532013-10-01 10:23:44 -0700300static void tcp_sndbuf_expand(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Eric Dumazet6ae70532013-10-01 10:23:44 -0700302 const struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng77bfc172016-09-19 23:39:20 -0400303 const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
Eric Dumazet6ae70532013-10-01 10:23:44 -0700304 int sndmem, per_mss;
305 u32 nr_segs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Eric Dumazet6ae70532013-10-01 10:23:44 -0700307 /* Worst case is non GSO/TSO : each frame consumes one skb
308 * and skb->head is kmalloced using power of two area of memory
309 */
310 per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) +
311 MAX_TCP_HEADER +
312 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
313
314 per_mss = roundup_pow_of_two(per_mss) +
315 SKB_DATA_ALIGN(sizeof(struct sk_buff));
316
317 nr_segs = max_t(u32, TCP_INIT_CWND, tp->snd_cwnd);
318 nr_segs = max_t(u32, nr_segs, tp->reordering + 1);
319
320 /* Fast Recovery (RFC 5681 3.2) :
321 * Cubic needs 1.7 factor, rounded to 2 to include
322 * extra cushion (application might react slowly to POLLOUT)
323 */
Yuchung Cheng77bfc172016-09-19 23:39:20 -0400324 sndmem = ca_ops->sndbuf_expand ? ca_ops->sndbuf_expand(sk) : 2;
325 sndmem *= nr_segs * per_mss;
Eric Dumazet6ae70532013-10-01 10:23:44 -0700326
Eric Dumazet06a59ec2011-10-13 18:24:42 +0000327 if (sk->sk_sndbuf < sndmem)
328 sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
332 *
333 * All tcp_full_space() is split to two parts: "network" buffer, allocated
334 * forward and advertised in receiver window (tp->rcv_wnd) and
335 * "application buffer", required to isolate scheduling/application
336 * latencies from network.
337 * window_clamp is maximal advertised window. It can be less than
338 * tcp_full_space(), in this case tcp_full_space() - window_clamp
339 * is reserved for "application" buffer. The less window_clamp is
340 * the smoother our behaviour from viewpoint of network, but the lower
341 * throughput and the higher sensitivity of the connection to losses. 8)
342 *
343 * rcv_ssthresh is more strict window_clamp used at "slow start"
344 * phase to predict further behaviour of this connection.
345 * It is used for two goals:
346 * - to enforce header prediction at sender, even when application
347 * requires some significant "application buffer". It is check #1.
348 * - to prevent pruning of receive queue because of misprediction
349 * of receiver window. Check #2.
350 *
351 * The scheme does not work when sender sends good segments opening
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800352 * window and then starts to feed us spaghetti. But it should work
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 * in common situations. Otherwise, we have to rely on queue collapsing.
354 */
355
356/* Slow part of check#2. */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700357static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700359 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* Optimize this! */
Eric Dumazet94f08932017-10-26 21:55:09 -0700361 int truesize = tcp_win_from_space(sk, skb->truesize) >> 1;
362 int window = tcp_win_from_space(sk, sysctl_tcp_rmem[2]) >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 while (tp->rcv_ssthresh <= window) {
365 if (truesize <= skb->len)
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700366 return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 truesize >>= 1;
369 window >>= 1;
370 }
371 return 0;
372}
373
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400374static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700376 struct tcp_sock *tp = tcp_sk(sk);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Check #1 */
379 if (tp->rcv_ssthresh < tp->window_clamp &&
380 (int)tp->rcv_ssthresh < tcp_space(sk) &&
Eric Dumazetb8da51e2015-05-15 12:39:27 -0700381 !tcp_under_memory_pressure(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int incr;
383
384 /* Check #2. Increase window, if skb with such overhead
385 * will fit to rcvbuf in future.
386 */
Eric Dumazet94f08932017-10-26 21:55:09 -0700387 if (tcp_win_from_space(sk, skb->truesize) <= skb->len)
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800388 incr = 2 * tp->advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 else
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700390 incr = __tcp_grow_window(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 if (incr) {
Eric Dumazet4d846f02012-04-16 23:28:07 +0000393 incr = max_t(int, incr, 2 * skb->len);
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800394 tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr,
395 tp->window_clamp);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700396 inet_csk(sk)->icsk_ack.quick |= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 }
399}
400
401/* 3. Tuning rcvbuf, when connection enters established state. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static void tcp_fixup_rcvbuf(struct sock *sk)
403{
Eric Dumazete9266a02011-10-20 16:53:56 -0400404 u32 mss = tcp_sk(sk)->advmss;
Eric Dumazete9266a02011-10-20 16:53:56 -0400405 int rcvmem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Yuchung Cheng85f16522013-06-11 15:35:32 -0700407 rcvmem = 2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) *
408 tcp_default_init_rwnd(mss);
Eric Dumazete9266a02011-10-20 16:53:56 -0400409
Eric Dumazetb0983d32013-09-20 13:56:58 -0700410 /* Dynamic Right Sizing (DRS) has 2 to 3 RTT latency
411 * Allow enough cushion so that sender is not limited by our window
412 */
Eric Dumazet4540c0c2017-10-27 07:47:22 -0700413 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf)
Eric Dumazetb0983d32013-09-20 13:56:58 -0700414 rcvmem <<= 2;
415
Eric Dumazete9266a02011-10-20 16:53:56 -0400416 if (sk->sk_rcvbuf < rcvmem)
417 sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800420/* 4. Try to fixup all. It is made immediately after connection enters
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * established state.
422 */
Jerry Chu10467162012-08-31 12:29:11 +0000423void tcp_init_buffer_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Eric Dumazet0c126542017-10-26 21:55:08 -0700425 int tcp_app_win = sock_net(sk)->ipv4.sysctl_tcp_app_win;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 struct tcp_sock *tp = tcp_sk(sk);
427 int maxwin;
428
429 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
430 tcp_fixup_rcvbuf(sk);
431 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
Eric Dumazet6ae70532013-10-01 10:23:44 -0700432 tcp_sndbuf_expand(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 tp->rcvq_space.space = tp->rcv_wnd;
Eric Dumazet9a568de2017-05-16 14:00:14 -0700435 tcp_mstamp_refresh(tp);
Eric Dumazet645f4c62017-04-25 10:15:41 -0700436 tp->rcvq_space.time = tp->tcp_mstamp;
Eric Dumazetb0983d32013-09-20 13:56:58 -0700437 tp->rcvq_space.seq = tp->copied_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 maxwin = tcp_full_space(sk);
440
441 if (tp->window_clamp >= maxwin) {
442 tp->window_clamp = maxwin;
443
Eric Dumazet0c126542017-10-26 21:55:08 -0700444 if (tcp_app_win && maxwin > 4 * tp->advmss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 tp->window_clamp = max(maxwin -
Eric Dumazet0c126542017-10-26 21:55:08 -0700446 (maxwin >> tcp_app_win),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 4 * tp->advmss);
448 }
449
450 /* Force reservation of one segment. */
Eric Dumazet0c126542017-10-26 21:55:08 -0700451 if (tcp_app_win &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 tp->window_clamp > 2 * tp->advmss &&
453 tp->window_clamp + tp->advmss > maxwin)
454 tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
455
456 tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
Eric Dumazetc2203cf2017-05-16 14:00:04 -0700457 tp->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/* 5. Recalculate window clamp after socket hit its memory bounds. */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700461static void tcp_clamp_window(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700463 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300464 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300466 icsk->icsk_ack.quick = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
John Heffner326f36e2005-11-10 17:11:48 -0800468 if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
469 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
Eric Dumazetb8da51e2015-05-15 12:39:27 -0700470 !tcp_under_memory_pressure(sk) &&
Glauber Costa180d8cd2011-12-11 21:47:02 +0000471 sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {
John Heffner326f36e2005-11-10 17:11:48 -0800472 sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
473 sysctl_tcp_rmem[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
John Heffner326f36e2005-11-10 17:11:48 -0800475 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800476 tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800479/* Initialize RCV_MSS value.
480 * RCV_MSS is an our guess about MSS used by the peer.
481 * We haven't any direct information about the MSS.
482 * It's better to underestimate the RCV_MSS rather than overestimate.
483 * Overestimations make us ACKing less frequently than needed.
484 * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
485 */
486void tcp_initialize_rcv_mss(struct sock *sk)
487{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400488 const struct tcp_sock *tp = tcp_sk(sk);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800489 unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
490
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800491 hint = min(hint, tp->rcv_wnd / 2);
William Allen Simpsonbee7ca92009-11-10 09:51:18 +0000492 hint = min(hint, TCP_MSS_DEFAULT);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800493 hint = max(hint, TCP_MIN_MSS);
494
495 inet_csk(sk)->icsk_ack.rcv_mss = hint;
496}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000497EXPORT_SYMBOL(tcp_initialize_rcv_mss);
Stephen Hemminger40efc6f2006-01-03 16:03:49 -0800498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/* Receiver "autotuning" code.
500 *
501 * The algorithm for RTT estimation w/o timestamps is based on
502 * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200503 * <http://public.lanl.gov/radiant/pubs.html#DRS>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 *
505 * More detail on this code can be found at
Justin P. Mattock631dd1a2010-10-18 11:03:14 +0200506 * <http://staff.psc.edu/jheffner/>,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * though this reference is out of date. A new paper
508 * is pending.
509 */
510static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
511{
Eric Dumazet645f4c62017-04-25 10:15:41 -0700512 u32 new_sample = tp->rcv_rtt_est.rtt_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 long m = sample;
514
515 if (m == 0)
516 m = 1;
517
518 if (new_sample != 0) {
519 /* If we sample in larger samples in the non-timestamp
520 * case, we could grossly overestimate the RTT especially
521 * with chatty applications or bulk transfer apps which
522 * are stalled on filesystem I/O.
523 *
524 * Also, since we are only going for a minimum in the
Stephen Hemminger31f34262005-11-15 15:17:10 -0800525 * non-timestamp case, we do not smooth things out
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800526 * else with timestamps disabled convergence takes too
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * long.
528 */
529 if (!win_dep) {
530 m -= (new_sample >> 3);
531 new_sample += m;
Neal Cardwell18a223e2012-04-10 07:59:20 +0000532 } else {
533 m <<= 3;
534 if (m < new_sample)
535 new_sample = m;
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 } else {
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800538 /* No previous measure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 new_sample = m << 3;
540 }
541
Eric Dumazet645f4c62017-04-25 10:15:41 -0700542 tp->rcv_rtt_est.rtt_us = new_sample;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
546{
Eric Dumazet645f4c62017-04-25 10:15:41 -0700547 u32 delta_us;
548
Eric Dumazet9a568de2017-05-16 14:00:14 -0700549 if (tp->rcv_rtt_est.time == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto new_measure;
551 if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
552 return;
Eric Dumazet9a568de2017-05-16 14:00:14 -0700553 delta_us = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcv_rtt_est.time);
Eric Dumazet645f4c62017-04-25 10:15:41 -0700554 tcp_rcv_rtt_update(tp, delta_us, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556new_measure:
557 tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
Eric Dumazet645f4c62017-04-25 10:15:41 -0700558 tp->rcv_rtt_est.time = tp->tcp_mstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800561static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
562 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700564 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet9a568de2017-05-16 14:00:14 -0700565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (tp->rx_opt.rcv_tsecr &&
567 (TCP_SKB_CB(skb)->end_seq -
Eric Dumazet9a568de2017-05-16 14:00:14 -0700568 TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss)) {
569 u32 delta = tcp_time_stamp(tp) - tp->rx_opt.rcv_tsecr;
570 u32 delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ);
571
572 tcp_rcv_rtt_update(tp, delta_us, 0);
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/*
577 * This function should be called every time data is copied to user space.
578 * It calculates the appropriate TCP receive buffer space.
579 */
580void tcp_rcv_space_adjust(struct sock *sk)
581{
582 struct tcp_sock *tp = tcp_sk(sk);
583 int time;
Eric Dumazetb0983d32013-09-20 13:56:58 -0700584 int copied;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900585
Eric Dumazet9a568de2017-05-16 14:00:14 -0700586 time = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcvq_space.time);
Eric Dumazet645f4c62017-04-25 10:15:41 -0700587 if (time < (tp->rcv_rtt_est.rtt_us >> 3) || tp->rcv_rtt_est.rtt_us == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900589
Eric Dumazetb0983d32013-09-20 13:56:58 -0700590 /* Number of bytes copied to user in last RTT */
591 copied = tp->copied_seq - tp->rcvq_space.seq;
592 if (copied <= tp->rcvq_space.space)
593 goto new_measure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Eric Dumazetb0983d32013-09-20 13:56:58 -0700595 /* A bit of theory :
596 * copied = bytes received in previous RTT, our base window
597 * To cope with packet losses, we need a 2x factor
598 * To cope with slow start, and sender growing its cwin by 100 %
599 * every RTT, we need a 4x factor, because the ACK we are sending
600 * now is for the next RTT, not the current one :
601 * <prev RTT . ><current RTT .. ><next RTT .... >
602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Eric Dumazet4540c0c2017-10-27 07:47:22 -0700604 if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
Eric Dumazetb0983d32013-09-20 13:56:58 -0700605 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
606 int rcvwin, rcvmem, rcvbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Eric Dumazetb0983d32013-09-20 13:56:58 -0700608 /* minimal window to cope with packet losses, assuming
609 * steady state. Add some cushion because of small variations.
610 */
611 rcvwin = (copied << 1) + 16 * tp->advmss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Eric Dumazetb0983d32013-09-20 13:56:58 -0700613 /* If rate increased by 25%,
614 * assume slow start, rcvwin = 3 * copied
615 * If rate increased by 50%,
616 * assume sender can use 2x growth, rcvwin = 4 * copied
617 */
618 if (copied >=
619 tp->rcvq_space.space + (tp->rcvq_space.space >> 2)) {
620 if (copied >=
621 tp->rcvq_space.space + (tp->rcvq_space.space >> 1))
622 rcvwin <<= 1;
623 else
624 rcvwin += (rcvwin >> 1);
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Eric Dumazetb0983d32013-09-20 13:56:58 -0700627 rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
Eric Dumazet94f08932017-10-26 21:55:09 -0700628 while (tcp_win_from_space(sk, rcvmem) < tp->advmss)
Eric Dumazetb0983d32013-09-20 13:56:58 -0700629 rcvmem += 128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Eric Dumazetb0983d32013-09-20 13:56:58 -0700631 rcvbuf = min(rcvwin / tp->advmss * rcvmem, sysctl_tcp_rmem[2]);
632 if (rcvbuf > sk->sk_rcvbuf) {
633 sk->sk_rcvbuf = rcvbuf;
634
635 /* Make the window clamp follow along. */
636 tp->window_clamp = rcvwin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638 }
Eric Dumazetb0983d32013-09-20 13:56:58 -0700639 tp->rcvq_space.space = copied;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641new_measure:
642 tp->rcvq_space.seq = tp->copied_seq;
Eric Dumazet645f4c62017-04-25 10:15:41 -0700643 tp->rcvq_space.time = tp->tcp_mstamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646/* There is something which you must keep in mind when you analyze the
647 * behavior of the tp->ato delayed ack timeout interval. When a
648 * connection starts up, we want to ack as quickly as possible. The
649 * problem is that "good" TCP's do slow start at the beginning of data
650 * transmission. The means that until we send the first few ACK's the
651 * sender will sit on his end and only queue most of his data, because
652 * he can only send snd_cwnd unacked packets at any given time. For
653 * each ACK we send, he increments snd_cwnd and transmits more of his
654 * queue. -DaveM
655 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700656static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700658 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700659 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 u32 now;
661
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700662 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700664 tcp_measure_rcv_mss(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 tcp_rcv_rtt_measure(tp);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900667
Eric Dumazet70eabf02017-05-16 14:00:07 -0700668 now = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700670 if (!icsk->icsk_ack.ato) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* The _first_ data packet received, initialize
672 * delayed ACK engine.
673 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700674 tcp_incr_quickack(sk);
675 icsk->icsk_ack.ato = TCP_ATO_MIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 } else {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700677 int m = now - icsk->icsk_ack.lrcvtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Ilpo Järvinen056834d2007-12-31 14:57:14 -0800679 if (m <= TCP_ATO_MIN / 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* The fastest case is the first. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700681 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
682 } else if (m < icsk->icsk_ack.ato) {
683 icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
684 if (icsk->icsk_ack.ato > icsk->icsk_rto)
685 icsk->icsk_ack.ato = icsk->icsk_rto;
686 } else if (m > icsk->icsk_rto) {
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800687 /* Too long gap. Apparently sender failed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * restart window, so that we send ACKs quickly.
689 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700690 tcp_incr_quickack(sk);
Hideo Aoki3ab224b2007-12-31 00:11:19 -0800691 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 }
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700694 icsk->icsk_ack.lrcvtime = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Florian Westphal735d3832014-09-29 13:08:30 +0200696 tcp_ecn_check_ce(tp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if (skb->len >= 128)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -0700699 tcp_grow_window(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/* Called to compute a smoothed rtt estimate. The data fed to this
703 * routine either comes from timestamps, or from segments that were
704 * known _not_ to have been retransmitted [see Karn/Partridge
705 * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
706 * piece by Van Jacobson.
707 * NOTE: the next three routines used to be one big routine.
708 * To save cycles in the RFC 1323 implementation it was better to break
709 * it up into three procedures. -- erics
710 */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800711static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300713 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet740b0f12014-02-26 14:02:48 -0800714 long m = mrtt_us; /* RTT */
715 u32 srtt = tp->srtt_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* The following amusing code comes from Jacobson's
718 * article in SIGCOMM '88. Note that rtt and mdev
719 * are scaled versions of rtt and mean deviation.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900720 * This is designed to be as fast as possible
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * m stands for "measurement".
722 *
723 * On a 1990 paper the rto value is changed to:
724 * RTO = rtt + 4 * mdev
725 *
726 * Funny. This algorithm seems to be very broken.
727 * These formulae increase RTO, when it should be decreased, increase
Stephen Hemminger31f34262005-11-15 15:17:10 -0800728 * too slowly, when it should be increased quickly, decrease too quickly
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
730 * does not matter how to _calculate_ it. Seems, it was trap
731 * that VJ failed to avoid. 8)
732 */
Eric Dumazet4a5ab4e2014-02-06 15:57:10 -0800733 if (srtt != 0) {
734 m -= (srtt >> 3); /* m is now error in rtt est */
735 srtt += m; /* rtt = 7/8 rtt + 1/8 new */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (m < 0) {
737 m = -m; /* m is now abs(error) */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800738 m -= (tp->mdev_us >> 2); /* similar update on mdev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 /* This is similar to one of Eifel findings.
740 * Eifel blocks mdev updates when rtt decreases.
741 * This solution is a bit different: we use finer gain
742 * for mdev in this case (alpha*beta).
743 * Like Eifel it also prevents growth of rto,
744 * but also it limits too fast rto decreases,
745 * happening in pure Eifel.
746 */
747 if (m > 0)
748 m >>= 3;
749 } else {
Eric Dumazet740b0f12014-02-26 14:02:48 -0800750 m -= (tp->mdev_us >> 2); /* similar update on mdev */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Eric Dumazet740b0f12014-02-26 14:02:48 -0800752 tp->mdev_us += m; /* mdev = 3/4 mdev + 1/4 new */
753 if (tp->mdev_us > tp->mdev_max_us) {
754 tp->mdev_max_us = tp->mdev_us;
755 if (tp->mdev_max_us > tp->rttvar_us)
756 tp->rttvar_us = tp->mdev_max_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 if (after(tp->snd_una, tp->rtt_seq)) {
Eric Dumazet740b0f12014-02-26 14:02:48 -0800759 if (tp->mdev_max_us < tp->rttvar_us)
760 tp->rttvar_us -= (tp->rttvar_us - tp->mdev_max_us) >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 tp->rtt_seq = tp->snd_nxt;
Eric Dumazet740b0f12014-02-26 14:02:48 -0800762 tp->mdev_max_us = tcp_rto_min_us(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764 } else {
765 /* no previous measure. */
Eric Dumazet4a5ab4e2014-02-06 15:57:10 -0800766 srtt = m << 3; /* take the measured time to be rtt */
Eric Dumazet740b0f12014-02-26 14:02:48 -0800767 tp->mdev_us = m << 1; /* make sure rto = 3*rtt */
768 tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk));
769 tp->mdev_max_us = tp->rttvar_us;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 tp->rtt_seq = tp->snd_nxt;
771 }
Eric Dumazet740b0f12014-02-26 14:02:48 -0800772 tp->srtt_us = max(1U, srtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700775/* Set the sk_pacing_rate to allow proper sizing of TSO packets.
776 * Note: TCP stack does not yet implement pacing.
777 * FQ packet scheduler can be used to implement cheap but effective
778 * TCP pacing, to smooth the burst on large writes when packets
779 * in flight is significantly lower than cwnd (or rwin)
780 */
Eric Dumazet43e122b2015-08-21 17:38:02 -0700781int sysctl_tcp_pacing_ss_ratio __read_mostly = 200;
782int sysctl_tcp_pacing_ca_ratio __read_mostly = 120;
783
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700784static void tcp_update_pacing_rate(struct sock *sk)
785{
786 const struct tcp_sock *tp = tcp_sk(sk);
787 u64 rate;
788
789 /* set sk_pacing_rate to 200 % of current rate (mss * cwnd / srtt) */
Eric Dumazet43e122b2015-08-21 17:38:02 -0700790 rate = (u64)tp->mss_cache * ((USEC_PER_SEC / 100) << 3);
791
792 /* current rate is (cwnd * mss) / srtt
793 * In Slow Start [1], set sk_pacing_rate to 200 % the current rate.
794 * In Congestion Avoidance phase, set it to 120 % the current rate.
795 *
796 * [1] : Normal Slow Start condition is (tp->snd_cwnd < tp->snd_ssthresh)
797 * If snd_cwnd >= (tp->snd_ssthresh / 2), we are approaching
798 * end of slow start and should slow down.
799 */
800 if (tp->snd_cwnd < tp->snd_ssthresh / 2)
801 rate *= sysctl_tcp_pacing_ss_ratio;
802 else
803 rate *= sysctl_tcp_pacing_ca_ratio;
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700804
805 rate *= max(tp->snd_cwnd, tp->packets_out);
806
Eric Dumazet740b0f12014-02-26 14:02:48 -0800807 if (likely(tp->srtt_us))
808 do_div(rate, tp->srtt_us);
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700809
Eric Dumazetba537422013-10-09 17:14:52 -0700810 /* ACCESS_ONCE() is needed because sch_fq fetches sk_pacing_rate
811 * without any lock. We want to make sure compiler wont store
812 * intermediate values in this location.
813 */
814 ACCESS_ONCE(sk->sk_pacing_rate) = min_t(u64, rate,
815 sk->sk_max_pacing_rate);
Eric Dumazet95bd09e2013-08-27 05:46:32 -0700816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/* Calculate rto without backoff. This is the second half of Van Jacobson's
819 * routine referred to above.
820 */
stephen hemmingerf7e56a72013-12-29 11:39:51 -0800821static void tcp_set_rto(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700823 const struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* Old crap is replaced with new one. 8)
825 *
826 * More seriously:
827 * 1. If rtt variance happened to be less 50msec, it is hallucination.
828 * It cannot be less due to utterly erratic ACK generation made
829 * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
830 * to do with delayed acks, because at cwnd>2 true delack timeout
831 * is invisible. Actually, Linux-2.4 also generates erratic
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800832 * ACKs in some circumstances.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000834 inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* 2. Fixups made earlier cannot be right.
837 * If we do not estimate RTO correctly without them,
838 * all the algo is pure shit and should be replaced
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -0800839 * with correct one. It is exactly, which we pretend to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Ilpo Järvinenee6aac52008-12-05 22:43:08 -0800842 /* NOTE: clamping at TCP_RTO_MIN is not required, current algo
843 * guarantees that rto is higher.
844 */
Damian Lukowskif1ecd5d2009-08-26 00:16:31 +0000845 tcp_bound_rto(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400848__u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
851
Gerrit Renker22b71c82010-08-29 19:23:12 +0000852 if (!cwnd)
David S. Miller442b9632011-02-02 17:05:11 -0800853 cwnd = TCP_INIT_CWND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
855}
856
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300857/*
858 * Packet counting of FACK is based on in-order assumptions, therefore TCP
859 * disables it when reordering is detected
860 */
David S. Miller4aabd8e2012-07-09 16:07:30 -0700861void tcp_disable_fack(struct tcp_sock *tp)
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300862{
Ilpo Järvinen85cc3912007-11-15 19:39:31 -0800863 /* RFC3517 uses different metric in lost marker => reset on change */
864 if (tcp_is_fack(tp))
865 tp->lost_skb_hint = NULL;
Vijay Subramanianab562222011-12-20 13:23:24 +0000866 tp->rx_opt.sack_ok &= ~TCP_FACK_ENABLED;
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300867}
868
Ryousei Takano564262c2007-10-25 23:03:52 -0700869/* Take a notice that peer is sending D-SACKs */
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300870static void tcp_dsack_seen(struct tcp_sock *tp)
871{
Vijay Subramanianab562222011-12-20 13:23:24 +0000872 tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300873}
874
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300875static void tcp_update_reordering(struct sock *sk, const int metric,
876 const int ts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300878 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng2d2517e2017-04-04 14:15:40 -0700879 int mib_idx;
Pavel Emelyanov40b215e2008-07-03 01:05:41 -0700880
Soheil Hassas Yeganeh6f5b24e2017-05-16 17:39:02 -0400881 if (WARN_ON_ONCE(metric < 0))
882 return;
883
Yuchung Cheng2d2517e2017-04-04 14:15:40 -0700884 if (metric > tp->reordering) {
Eric Dumazetc6e21802017-10-26 21:55:06 -0700885 tp->reordering = min(sock_net(sk)->ipv4.sysctl_tcp_max_reordering, metric);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#if FASTRETRANS_DEBUG > 1
Joe Perches91df42b2012-05-15 14:11:54 +0000888 pr_debug("Disorder%d %d %u f%u s%u rr%d\n",
889 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
890 tp->reordering,
891 tp->fackets_out,
892 tp->sacked_out,
893 tp->undo_marker ? tp->undo_retrans : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894#endif
Ilpo Järvinene60402d2007-08-09 15:14:46 +0300895 tcp_disable_fack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
Yuchung Chengeed530b2012-05-02 13:30:03 +0000897
Yuchung Cheng4f41b1c2015-10-16 21:57:47 -0700898 tp->rack.reord = 1;
Yuchung Cheng2d2517e2017-04-04 14:15:40 -0700899
900 /* This exciting event is worth to be remembered. 8) */
901 if (ts)
902 mib_idx = LINUX_MIB_TCPTSREORDER;
903 else if (tcp_is_reno(tp))
904 mib_idx = LINUX_MIB_TCPRENOREORDER;
905 else if (tcp_is_fack(tp))
906 mib_idx = LINUX_MIB_TCPFACKREORDER;
907 else
908 mib_idx = LINUX_MIB_TCPSACKREORDER;
909
910 NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700913/* This must be called before lost_out is incremented */
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700914static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
915{
Ian Morris51456b22015-04-03 09:17:26 +0100916 if (!tp->retransmit_skb_hint ||
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700917 before(TCP_SKB_CB(skb)->seq,
918 TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700919 tp->retransmit_skb_hint = skb;
Ilpo Järvinenc8c213f2008-09-20 21:18:55 -0700920}
921
Neal Cardwell0682e692016-09-19 23:39:13 -0400922/* Sum the number of packets on the wire we have marked as lost.
923 * There are two cases we care about here:
924 * a) Packet hasn't been marked lost (nor retransmitted),
925 * and this is the first loss.
926 * b) Packet has been marked both lost and retransmitted,
927 * and this means we think it was lost again.
928 */
929static void tcp_sum_lost(struct tcp_sock *tp, struct sk_buff *skb)
930{
931 __u8 sacked = TCP_SKB_CB(skb)->sacked;
932
933 if (!(sacked & TCPCB_LOST) ||
934 ((sacked & TCPCB_LOST) && (sacked & TCPCB_SACKED_RETRANS)))
935 tp->lost += tcp_skb_pcount(skb);
936}
937
Ilpo Järvinen41ea36e2008-09-20 21:19:22 -0700938static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb)
939{
940 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
941 tcp_verify_retransmit_hint(tp, skb);
942
943 tp->lost_out += tcp_skb_pcount(skb);
Neal Cardwell0682e692016-09-19 23:39:13 -0400944 tcp_sum_lost(tp, skb);
Ilpo Järvinen41ea36e2008-09-20 21:19:22 -0700945 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
946 }
947}
948
Yuchung Cheng4f41b1c2015-10-16 21:57:47 -0700949void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, struct sk_buff *skb)
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700950{
951 tcp_verify_retransmit_hint(tp, skb);
952
Neal Cardwell0682e692016-09-19 23:39:13 -0400953 tcp_sum_lost(tp, skb);
Ilpo Järvinen006f5822008-09-20 21:20:20 -0700954 if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
955 tp->lost_out += tcp_skb_pcount(skb);
956 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
957 }
958}
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960/* This procedure tags the retransmission queue when SACKs arrive.
961 *
962 * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
963 * Packets in queue with these bits set are counted in variables
964 * sacked_out, retrans_out and lost_out, correspondingly.
965 *
966 * Valid combinations are:
967 * Tag InFlight Description
968 * 0 1 - orig segment is in flight.
969 * S 0 - nothing flies, orig reached receiver.
970 * L 0 - nothing flies, orig lost by net.
971 * R 2 - both orig and retransmit are in flight.
972 * L|R 1 - orig is lost, retransmit is in flight.
973 * S|R 1 - orig reached receiver, retrans is still in flight.
974 * (L|S|R is logically valid, it could occur when L|R is sacked,
975 * but it is equivalent to plain S and code short-curcuits it to S.
976 * L|S is logically invalid, it would mean -1 packet in flight 8))
977 *
978 * These 6 states form finite state machine, controlled by the following events:
979 * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
980 * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
Yuchung Cheng974c1232012-01-19 14:42:21 +0000981 * 3. Loss detection event of two flavors:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 * A. Scoreboard estimator decided the packet is lost.
983 * A'. Reno "three dupacks" marks head of queue lost.
Yuchung Cheng974c1232012-01-19 14:42:21 +0000984 * A''. Its FACK modification, head until snd.fack is lost.
985 * B. SACK arrives sacking SND.NXT at the moment, when the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * segment was retransmitted.
987 * 4. D-SACK added new rule: D-SACK changes any tag to S.
988 *
989 * It is pleasant to note, that state diagram turns out to be commutative,
990 * so that we are allowed not to be bothered by order of our actions,
991 * when multiple events arrive simultaneously. (see the function below).
992 *
993 * Reordering detection.
994 * --------------------
995 * Reordering metric is maximal distance, which a packet can be displaced
996 * in packet stream. With SACKs we can estimate it:
997 *
998 * 1. SACK fills old hole and the corresponding segment was not
999 * ever retransmitted -> reordering. Alas, we cannot use it
1000 * when segment was retransmitted.
1001 * 2. The last flaw is solved with D-SACK. D-SACK arrives
1002 * for retransmitted and already SACKed segment -> reordering..
1003 * Both of these heuristics are not used in Loss state, when we cannot
1004 * account for retransmits accurately.
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001005 *
1006 * SACK block validation.
1007 * ----------------------
1008 *
1009 * SACK block range validation checks that the received SACK block fits to
1010 * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
1011 * Note that SND.UNA is not included to the range though being valid because
Ilpo Järvinen0e835332007-10-01 15:28:17 -07001012 * it means that the receiver is rather inconsistent with itself reporting
1013 * SACK reneging when it should advance SND.UNA. Such SACK block this is
1014 * perfectly valid, however, in light of RFC2018 which explicitly states
1015 * that "SACK block MUST reflect the newest segment. Even if the newest
1016 * segment is going to be discarded ...", not that it looks very clever
1017 * in case of head skb. Due to potentional receiver driven attacks, we
1018 * choose to avoid immediate execution of a walk in write queue due to
1019 * reneging and defer head skb's loss recovery to standard loss recovery
1020 * procedure that will eventually trigger (nothing forbids us doing this).
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001021 *
1022 * Implements also blockage to start_seq wrap-around. Problem lies in the
1023 * fact that though start_seq (s) is before end_seq (i.e., not reversed),
1024 * there's no guarantee that it will be before snd_nxt (n). The problem
1025 * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
1026 * wrap (s_w):
1027 *
1028 * <- outs wnd -> <- wrapzone ->
1029 * u e n u_w e_w s n_w
1030 * | | | | | | |
1031 * |<------------+------+----- TCP seqno space --------------+---------->|
1032 * ...-- <2^31 ->| |<--------...
1033 * ...---- >2^31 ------>| |<--------...
1034 *
1035 * Current code wouldn't be vulnerable but it's better still to discard such
1036 * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
1037 * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
1038 * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
1039 * equal to the ideal case (infinite seqno space without wrap caused issues).
1040 *
1041 * With D-SACK the lower bound is extended to cover sequence space below
1042 * SND.UNA down to undo_marker, which is the last point of interest. Yet
Ryousei Takano564262c2007-10-25 23:03:52 -07001043 * again, D-SACK block must not to go across snd_una (for the same reason as
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001044 * for the normal SACK blocks, explained above). But there all simplicity
1045 * ends, TCP might receive valid D-SACKs below that. As long as they reside
1046 * fully below undo_marker they do not affect behavior in anyway and can
1047 * therefore be safely ignored. In rare cases (which are more or less
1048 * theoretical ones), the D-SACK will nicely cross that boundary due to skb
1049 * fragmentation and packet reordering past skb's retransmission. To consider
1050 * them correctly, the acceptable range must be extended even more though
1051 * the exact amount is rather hard to quantify. However, tp->max_window can
1052 * be used as an exaggerated estimate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001054static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack,
1055 u32 start_seq, u32 end_seq)
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001056{
1057 /* Too far in future, or reversed (interpretation is ambiguous) */
1058 if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001059 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001060
1061 /* Nasty start_seq wrap-around check (see comments above) */
1062 if (!before(start_seq, tp->snd_nxt))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001063 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001064
Ryousei Takano564262c2007-10-25 23:03:52 -07001065 /* In outstanding window? ...This is valid exit for D-SACKs too.
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001066 * start_seq == snd_una is non-sensical (see comments above)
1067 */
1068 if (after(start_seq, tp->snd_una))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001069 return true;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001070
1071 if (!is_dsack || !tp->undo_marker)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001072 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001073
1074 /* ...Then it's D-SACK, and must reside below snd_una completely */
Zheng Yanf779b2d2011-09-18 22:37:34 -04001075 if (after(end_seq, tp->snd_una))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001076 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001077
1078 if (!before(start_seq, tp->undo_marker))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001079 return true;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001080
1081 /* Too old */
1082 if (!after(end_seq, tp->undo_marker))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001083 return false;
Ilpo Järvinen5b3c9882007-08-24 22:54:44 -07001084
1085 /* Undo_marker boundary crossing (overestimates a lot). Known already:
1086 * start_seq < undo_marker and end_seq >= undo_marker.
1087 */
1088 return !before(start_seq, end_seq - tp->max_window);
1089}
1090
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001091static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb,
1092 struct tcp_sack_block_wire *sp, int num_sacks,
1093 u32 prior_snd_una)
David S. Millerd06e0212007-06-18 22:43:06 -07001094{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07001095 struct tcp_sock *tp = tcp_sk(sk);
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001096 u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
1097 u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001098 bool dup_sack = false;
David S. Millerd06e0212007-06-18 22:43:06 -07001099
1100 if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001101 dup_sack = true;
Ilpo Järvinene60402d2007-08-09 15:14:46 +03001102 tcp_dsack_seen(tp);
Eric Dumazetc10d9312016-04-29 14:16:47 -07001103 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
David S. Millerd06e0212007-06-18 22:43:06 -07001104 } else if (num_sacks > 1) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001105 u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
1106 u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
David S. Millerd06e0212007-06-18 22:43:06 -07001107
1108 if (!after(end_seq_0, end_seq_1) &&
1109 !before(start_seq_0, start_seq_1)) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001110 dup_sack = true;
Ilpo Järvinene60402d2007-08-09 15:14:46 +03001111 tcp_dsack_seen(tp);
Eric Dumazetc10d9312016-04-29 14:16:47 -07001112 NET_INC_STATS(sock_net(sk),
Pavel Emelyanovde0744a2008-07-16 20:31:16 -07001113 LINUX_MIB_TCPDSACKOFORECV);
David S. Millerd06e0212007-06-18 22:43:06 -07001114 }
1115 }
1116
1117 /* D-SACK for already forgotten data... Do dumb counting. */
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001118 if (dup_sack && tp->undo_marker && tp->undo_retrans > 0 &&
David S. Millerd06e0212007-06-18 22:43:06 -07001119 !after(end_seq_0, prior_snd_una) &&
1120 after(end_seq_0, tp->undo_marker))
1121 tp->undo_retrans--;
1122
1123 return dup_sack;
1124}
1125
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001126struct tcp_sacktag_state {
Eric Dumazet740b0f12014-02-26 14:02:48 -08001127 int reord;
1128 int fack_count;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02001129 /* Timestamps for earliest and latest never-retransmitted segment
1130 * that was SACKed. RTO needs the earliest RTT to stay conservative,
1131 * but congestion control should still get an accurate delay signal.
1132 */
Eric Dumazet9a568de2017-05-16 14:00:14 -07001133 u64 first_sackt;
1134 u64 last_sackt;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001135 struct rate_sample *rate;
Eric Dumazet740b0f12014-02-26 14:02:48 -08001136 int flag;
Eric Dumazet75c119a2017-10-05 22:21:27 -07001137 unsigned int mss_now;
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001138};
1139
Ilpo Järvinend1935942007-10-11 17:34:25 -07001140/* Check if skb is fully within the SACK block. In presence of GSO skbs,
1141 * the incoming SACK may not exactly match but we can find smaller MSS
1142 * aligned portion of it that matches. Therefore we might need to fragment
1143 * which may fail and creates some hassle (caller must handle error case
1144 * returns).
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001145 *
1146 * FIXME: this could be merged to shift decision code
Ilpo Järvinend1935942007-10-11 17:34:25 -07001147 */
Adrian Bunk0f79efd2007-10-26 03:57:36 -07001148static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001149 u32 start_seq, u32 end_seq)
Ilpo Järvinend1935942007-10-11 17:34:25 -07001150{
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001151 int err;
1152 bool in_sack;
Ilpo Järvinend1935942007-10-11 17:34:25 -07001153 unsigned int pkt_len;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001154 unsigned int mss;
Ilpo Järvinend1935942007-10-11 17:34:25 -07001155
1156 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1157 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1158
1159 if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1160 after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001161 mss = tcp_skb_mss(skb);
Ilpo Järvinend1935942007-10-11 17:34:25 -07001162 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1163
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001164 if (!in_sack) {
Ilpo Järvinend1935942007-10-11 17:34:25 -07001165 pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001166 if (pkt_len < mss)
1167 pkt_len = mss;
1168 } else {
Ilpo Järvinend1935942007-10-11 17:34:25 -07001169 pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001170 if (pkt_len < mss)
1171 return -EINVAL;
1172 }
1173
1174 /* Round if necessary so that SACKs cover only full MSSes
1175 * and/or the remaining small portion (if present)
1176 */
1177 if (pkt_len > mss) {
1178 unsigned int new_len = (pkt_len / mss) * mss;
Yuchung Chengb451e5d2017-05-10 17:01:27 -07001179 if (!in_sack && new_len < pkt_len)
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001180 new_len += mss;
Ilpo Järvinenadb92db2008-11-24 21:13:50 -08001181 pkt_len = new_len;
1182 }
Yuchung Chengb451e5d2017-05-10 17:01:27 -07001183
1184 if (pkt_len >= skb->len && !in_sack)
1185 return 0;
1186
Eric Dumazet75c119a2017-10-05 22:21:27 -07001187 err = tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb,
1188 pkt_len, mss, GFP_ATOMIC);
Ilpo Järvinend1935942007-10-11 17:34:25 -07001189 if (err < 0)
1190 return err;
1191 }
1192
1193 return in_sack;
1194}
1195
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001196/* Mark the given newly-SACKed range as such, adjusting counters and hints. */
1197static u8 tcp_sacktag_one(struct sock *sk,
1198 struct tcp_sacktag_state *state, u8 sacked,
1199 u32 start_seq, u32 end_seq,
Eric Dumazet740b0f12014-02-26 14:02:48 -08001200 int dup_sack, int pcount,
Eric Dumazet9a568de2017-05-16 14:00:14 -07001201 u64 xmit_time)
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001202{
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001203 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001204 int fack_count = state->fack_count;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001205
1206 /* Account D-SACK for retransmitted packet. */
1207 if (dup_sack && (sacked & TCPCB_RETRANS)) {
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001208 if (tp->undo_marker && tp->undo_retrans > 0 &&
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001209 after(end_seq, tp->undo_marker))
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001210 tp->undo_retrans--;
Ilpo Järvinenede9f3b2007-12-02 00:47:58 +02001211 if (sacked & TCPCB_SACKED_ACKED)
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001212 state->reord = min(fack_count, state->reord);
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001213 }
1214
1215 /* Nothing to do; acked frame is about to be dropped (was ACKed). */
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001216 if (!after(end_seq, tp->snd_una))
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001217 return sacked;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001218
1219 if (!(sacked & TCPCB_SACKED_ACKED)) {
Eric Dumazetd2329f12017-04-25 10:15:38 -07001220 tcp_rack_advance(tp, sacked, end_seq, xmit_time);
Yuchung Cheng659a8ad2015-10-16 21:57:46 -07001221
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001222 if (sacked & TCPCB_SACKED_RETRANS) {
1223 /* If the segment is not tagged as lost,
1224 * we do not clear RETRANS, believing
1225 * that retransmission is still in flight.
1226 */
1227 if (sacked & TCPCB_LOST) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001228 sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001229 tp->lost_out -= pcount;
1230 tp->retrans_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001231 }
1232 } else {
1233 if (!(sacked & TCPCB_RETRANS)) {
1234 /* New sack for not retransmitted frame,
1235 * which was in hole. It is reordering.
1236 */
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001237 if (before(start_seq,
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001238 tcp_highest_sack_seq(tp)))
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001239 state->reord = min(fack_count,
1240 state->reord);
Yuchung Chenge33099f2013-03-20 13:33:00 +00001241 if (!after(end_seq, tp->high_seq))
1242 state->flag |= FLAG_ORIG_SACK_ACKED;
Eric Dumazet9a568de2017-05-16 14:00:14 -07001243 if (state->first_sackt == 0)
1244 state->first_sackt = xmit_time;
1245 state->last_sackt = xmit_time;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001246 }
1247
1248 if (sacked & TCPCB_LOST) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001249 sacked &= ~TCPCB_LOST;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001250 tp->lost_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001251 }
1252 }
1253
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001254 sacked |= TCPCB_SACKED_ACKED;
1255 state->flag |= FLAG_DATA_SACKED;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001256 tp->sacked_out += pcount;
Yuchung Chengddf1af62016-02-02 10:33:06 -08001257 tp->delivered += pcount; /* Out-of-order packets delivered */
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001258
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001259 fack_count += pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001260
1261 /* Lost marker hint past SACKed? Tweak RFC3517 cnt */
Ian Morris00db4122015-04-03 09:17:27 +01001262 if (!tcp_is_fack(tp) && tp->lost_skb_hint &&
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001263 before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001264 tp->lost_cnt_hint += pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001265
1266 if (fack_count > tp->fackets_out)
1267 tp->fackets_out = fack_count;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001268 }
1269
1270 /* D-SACK. We can detect redundant retransmission in S|R and plain R
1271 * frames and clear it. undo_retrans is decreased above, L|R frames
1272 * are accounted above as well.
1273 */
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001274 if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
1275 sacked &= ~TCPCB_SACKED_RETRANS;
Ilpo Järvinenf58b22f2008-11-24 21:14:43 -08001276 tp->retrans_out -= pcount;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001277 }
1278
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001279 return sacked;
Ilpo Järvinen9e10c47c2007-11-15 19:44:56 -08001280}
1281
Neal Cardwelldaef52b2012-02-12 18:37:10 +00001282/* Shift newly-SACKed bytes from this skb to the immediately previous
1283 * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
1284 */
Eric Dumazetf3319812017-10-05 22:21:26 -07001285static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev,
1286 struct sk_buff *skb,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001287 struct tcp_sacktag_state *state,
1288 unsigned int pcount, int shifted, int mss,
1289 bool dup_sack)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001290{
1291 struct tcp_sock *tp = tcp_sk(sk);
Neal Cardwelldaef52b2012-02-12 18:37:10 +00001292 u32 start_seq = TCP_SKB_CB(skb)->seq; /* start of newly-SACKed */
1293 u32 end_seq = start_seq + shifted; /* end of newly-SACKed */
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001294
1295 BUG_ON(!pcount);
1296
Neal Cardwell4c90d3b2012-02-26 10:06:19 +00001297 /* Adjust counters and hints for the newly sacked sequence
1298 * range but discard the return value since prev is already
1299 * marked. We must tag the range first because the seq
1300 * advancement below implicitly advances
1301 * tcp_highest_sack_seq() when skb is highest_sack.
1302 */
1303 tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked,
Yuchung Cheng59c9af42013-07-22 16:20:47 -07001304 start_seq, end_seq, dup_sack, pcount,
Eric Dumazet9a568de2017-05-16 14:00:14 -07001305 skb->skb_mstamp);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001306 tcp_rate_skb_delivered(sk, skb, state->rate);
Neal Cardwell4c90d3b2012-02-26 10:06:19 +00001307
1308 if (skb == tp->lost_skb_hint)
Neal Cardwell0af2a0d2012-02-13 20:22:08 +00001309 tp->lost_cnt_hint += pcount;
1310
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001311 TCP_SKB_CB(prev)->end_seq += shifted;
1312 TCP_SKB_CB(skb)->seq += shifted;
1313
Eric Dumazetcd7d8492014-09-24 04:11:22 -07001314 tcp_skb_pcount_add(prev, pcount);
1315 BUG_ON(tcp_skb_pcount(skb) < pcount);
1316 tcp_skb_pcount_add(skb, -pcount);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001317
1318 /* When we're adding to gso_segs == 1, gso_size will be zero,
1319 * in theory this shouldn't be necessary but as long as DSACK
1320 * code can come after this skb later on it's better to keep
1321 * setting gso_size to something.
1322 */
Eric Dumazetf69ad292015-06-11 09:15:18 -07001323 if (!TCP_SKB_CB(prev)->tcp_gso_size)
1324 TCP_SKB_CB(prev)->tcp_gso_size = mss;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001325
1326 /* CHECKME: To clear or not to clear? Mimics normal skb currently */
Eric Dumazet51466a72015-06-11 09:15:16 -07001327 if (tcp_skb_pcount(skb) <= 1)
Eric Dumazetf69ad292015-06-11 09:15:18 -07001328 TCP_SKB_CB(skb)->tcp_gso_size = 0;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001329
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001330 /* Difference in this won't matter, both ACKed by the same cumul. ACK */
1331 TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1332
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001333 if (skb->len > 0) {
1334 BUG_ON(!tcp_skb_pcount(skb));
Eric Dumazetc10d9312016-04-29 14:16:47 -07001335 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTED);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001336 return false;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001337 }
1338
1339 /* Whole SKB was eaten :-) */
1340
Ilpo Järvinen92ee76b2008-11-24 21:26:56 -08001341 if (skb == tp->retransmit_skb_hint)
1342 tp->retransmit_skb_hint = prev;
Ilpo Järvinen92ee76b2008-11-24 21:26:56 -08001343 if (skb == tp->lost_skb_hint) {
1344 tp->lost_skb_hint = prev;
1345 tp->lost_cnt_hint -= tcp_skb_pcount(prev);
1346 }
1347
Eric Dumazet5e8a402f2013-10-04 10:31:41 -07001348 TCP_SKB_CB(prev)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
Martin KaFai Laua643b5d2016-04-25 14:44:49 -07001349 TCP_SKB_CB(prev)->eor = TCP_SKB_CB(skb)->eor;
Eric Dumazet5e8a402f2013-10-04 10:31:41 -07001350 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
1351 TCP_SKB_CB(prev)->end_seq++;
1352
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001353 if (skb == tcp_highest_sack(sk))
1354 tcp_advance_highest_sack(sk, skb);
1355
Martin KaFai Laucfea5a62016-04-19 22:39:29 -07001356 tcp_skb_collapse_tstamp(prev, skb);
Eric Dumazet9a568de2017-05-16 14:00:14 -07001357 if (unlikely(TCP_SKB_CB(prev)->tx.delivered_mstamp))
1358 TCP_SKB_CB(prev)->tx.delivered_mstamp = 0;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001359
Eric Dumazet75c119a2017-10-05 22:21:27 -07001360 tcp_rtx_queue_unlink_and_free(skb, sk);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001361
Eric Dumazetc10d9312016-04-29 14:16:47 -07001362 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKMERGED);
Ilpo Järvinen111cc8b2008-11-24 21:27:22 -08001363
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001364 return true;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001365}
1366
1367/* I wish gso_size would have a bit more sane initialization than
1368 * something-or-zero which complicates things
1369 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001370static int tcp_skb_seglen(const struct sk_buff *skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001371{
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001372 return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001373}
1374
1375/* Shifting pages past head area doesn't work */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001376static int skb_can_shift(const struct sk_buff *skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001377{
1378 return !skb_headlen(skb) && skb_is_nonlinear(skb);
1379}
1380
1381/* Try collapsing SACK blocks spanning across multiple skbs to a single
1382 * skb.
1383 */
1384static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001385 struct tcp_sacktag_state *state,
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001386 u32 start_seq, u32 end_seq,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001387 bool dup_sack)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001388{
1389 struct tcp_sock *tp = tcp_sk(sk);
1390 struct sk_buff *prev;
1391 int mss;
1392 int pcount = 0;
1393 int len;
1394 int in_sack;
1395
1396 if (!sk_can_gso(sk))
1397 goto fallback;
1398
1399 /* Normally R but no L won't result in plain S */
1400 if (!dup_sack &&
Ilpo Järvinen9969ca52008-12-05 22:41:06 -08001401 (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001402 goto fallback;
1403 if (!skb_can_shift(skb))
1404 goto fallback;
1405 /* This frame is about to be dropped (was ACKed). */
1406 if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1407 goto fallback;
1408
1409 /* Can only happen with delayed DSACK + discard craziness */
Eric Dumazet75c119a2017-10-05 22:21:27 -07001410 prev = skb_rb_prev(skb);
1411 if (!prev)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001412 goto fallback;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001413
1414 if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1415 goto fallback;
1416
Martin KaFai Laua643b5d2016-04-25 14:44:49 -07001417 if (!tcp_skb_can_collapse_to(prev))
1418 goto fallback;
1419
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001420 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1421 !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1422
1423 if (in_sack) {
1424 len = skb->len;
1425 pcount = tcp_skb_pcount(skb);
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001426 mss = tcp_skb_seglen(skb);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001427
1428 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1429 * drop this restriction as unnecessary
1430 */
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001431 if (mss != tcp_skb_seglen(prev))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001432 goto fallback;
1433 } else {
1434 if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1435 goto noop;
1436 /* CHECKME: This is non-MSS split case only?, this will
1437 * cause skipped skbs due to advancing loop btw, original
1438 * has that feature too
1439 */
1440 if (tcp_skb_pcount(skb) <= 1)
1441 goto noop;
1442
1443 in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1444 if (!in_sack) {
1445 /* TODO: head merge to next could be attempted here
1446 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1447 * though it might not be worth of the additional hassle
1448 *
1449 * ...we can probably just fallback to what was done
1450 * previously. We could try merging non-SACKed ones
1451 * as well but it probably isn't going to buy off
1452 * because later SACKs might again split them, and
1453 * it would make skb timestamp tracking considerably
1454 * harder problem.
1455 */
1456 goto fallback;
1457 }
1458
1459 len = end_seq - TCP_SKB_CB(skb)->seq;
1460 BUG_ON(len < 0);
1461 BUG_ON(len > skb->len);
1462
1463 /* MSS boundaries should be honoured or else pcount will
1464 * severely break even though it makes things bit trickier.
1465 * Optimize common case to avoid most of the divides
1466 */
1467 mss = tcp_skb_mss(skb);
1468
1469 /* TODO: Fix DSACKs to not fragment already SACKed and we can
1470 * drop this restriction as unnecessary
1471 */
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001472 if (mss != tcp_skb_seglen(prev))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001473 goto fallback;
1474
1475 if (len == mss) {
1476 pcount = 1;
1477 } else if (len < mss) {
1478 goto noop;
1479 } else {
1480 pcount = len / mss;
1481 len = pcount * mss;
1482 }
1483 }
1484
Neal Cardwell4648dc92012-03-05 19:35:04 +00001485 /* tcp_sacktag_one() won't SACK-tag ranges below snd_una */
1486 if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
1487 goto fallback;
1488
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001489 if (!skb_shift(prev, skb, len))
1490 goto fallback;
Eric Dumazetf3319812017-10-05 22:21:26 -07001491 if (!tcp_shifted_skb(sk, prev, skb, state, pcount, len, mss, dup_sack))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001492 goto out;
1493
1494 /* Hole filled allows collapsing with the next as well, this is very
1495 * useful when hole on every nth skb pattern happens
1496 */
Eric Dumazet75c119a2017-10-05 22:21:27 -07001497 skb = skb_rb_next(prev);
1498 if (!skb)
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001499 goto out;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001500
Ilpo Järvinenf0bc52f2008-12-05 22:40:47 -08001501 if (!skb_can_shift(skb) ||
Ilpo Järvinenf0bc52f2008-12-05 22:40:47 -08001502 ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08001503 (mss != tcp_skb_seglen(skb)))
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001504 goto out;
1505
1506 len = skb->len;
1507 if (skb_shift(prev, skb, len)) {
1508 pcount += tcp_skb_pcount(skb);
Eric Dumazetf3319812017-10-05 22:21:26 -07001509 tcp_shifted_skb(sk, prev, skb, state, tcp_skb_pcount(skb),
1510 len, mss, 0);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001511 }
1512
1513out:
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001514 state->fack_count += pcount;
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001515 return prev;
1516
1517noop:
1518 return skb;
1519
1520fallback:
Eric Dumazetc10d9312016-04-29 14:16:47 -07001521 NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001522 return NULL;
1523}
1524
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001525static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
1526 struct tcp_sack_block *next_dup,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001527 struct tcp_sacktag_state *state,
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001528 u32 start_seq, u32 end_seq,
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001529 bool dup_sack_in)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001530{
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001531 struct tcp_sock *tp = tcp_sk(sk);
1532 struct sk_buff *tmp;
1533
Eric Dumazet75c119a2017-10-05 22:21:27 -07001534 skb_rbtree_walk_from(skb) {
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001535 int in_sack = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001536 bool dup_sack = dup_sack_in;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001537
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001538 /* queue is in-order => we can short-circuit the walk early */
1539 if (!before(TCP_SKB_CB(skb)->seq, end_seq))
1540 break;
1541
Ian Morris00db4122015-04-03 09:17:27 +01001542 if (next_dup &&
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001543 before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
1544 in_sack = tcp_match_skb_to_sack(sk, skb,
1545 next_dup->start_seq,
1546 next_dup->end_seq);
1547 if (in_sack > 0)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001548 dup_sack = true;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001549 }
1550
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001551 /* skb reference here is a bit tricky to get right, since
1552 * shifting can eat and free both this skb and the next,
1553 * so not even _safe variant of the loop is enough.
1554 */
1555 if (in_sack <= 0) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001556 tmp = tcp_shift_skb_data(sk, skb, state,
1557 start_seq, end_seq, dup_sack);
Ian Morris00db4122015-04-03 09:17:27 +01001558 if (tmp) {
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001559 if (tmp != skb) {
1560 skb = tmp;
1561 continue;
1562 }
1563
1564 in_sack = 0;
1565 } else {
1566 in_sack = tcp_match_skb_to_sack(sk, skb,
1567 start_seq,
1568 end_seq);
1569 }
1570 }
1571
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001572 if (unlikely(in_sack < 0))
1573 break;
1574
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001575 if (in_sack) {
Neal Cardwellcc9a6722012-02-12 18:37:09 +00001576 TCP_SKB_CB(skb)->sacked =
1577 tcp_sacktag_one(sk,
1578 state,
1579 TCP_SKB_CB(skb)->sacked,
1580 TCP_SKB_CB(skb)->seq,
1581 TCP_SKB_CB(skb)->end_seq,
1582 dup_sack,
Yuchung Cheng59c9af42013-07-22 16:20:47 -07001583 tcp_skb_pcount(skb),
Eric Dumazet9a568de2017-05-16 14:00:14 -07001584 skb->skb_mstamp);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001585 tcp_rate_skb_delivered(sk, skb, state->rate);
Eric Dumazete2080072017-10-04 12:59:58 -07001586 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
1587 list_del_init(&skb->tcp_tsorted_anchor);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001588
Ilpo Järvinen832d11c2008-11-24 21:20:15 -08001589 if (!before(TCP_SKB_CB(skb)->seq,
1590 tcp_highest_sack_seq(tp)))
1591 tcp_advance_highest_sack(sk, skb);
1592 }
1593
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001594 state->fack_count += tcp_skb_pcount(skb);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001595 }
1596 return skb;
1597}
1598
Eric Dumazet75c119a2017-10-05 22:21:27 -07001599static struct sk_buff *tcp_sacktag_bsearch(struct sock *sk,
1600 struct tcp_sacktag_state *state,
1601 u32 seq)
1602{
1603 struct rb_node *parent, **p = &sk->tcp_rtx_queue.rb_node;
1604 struct sk_buff *skb;
1605 int unack_bytes;
1606
1607 while (*p) {
1608 parent = *p;
1609 skb = rb_to_skb(parent);
1610 if (before(seq, TCP_SKB_CB(skb)->seq)) {
1611 p = &parent->rb_left;
1612 continue;
1613 }
1614 if (!before(seq, TCP_SKB_CB(skb)->end_seq)) {
1615 p = &parent->rb_right;
1616 continue;
1617 }
1618
1619 state->fack_count = 0;
1620 unack_bytes = TCP_SKB_CB(skb)->seq - tcp_sk(sk)->snd_una;
1621 if (state->mss_now && unack_bytes > 0)
1622 state->fack_count = unack_bytes / state->mss_now;
1623
1624 return skb;
1625 }
1626 return NULL;
1627}
1628
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001629static 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{
Eric Dumazet75c119a2017-10-05 22:21:27 -07001633 if (skb && after(TCP_SKB_CB(skb)->seq, skip_to_seq))
1634 return skb;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001635
Eric Dumazet75c119a2017-10-05 22:21:27 -07001636 return tcp_sacktag_bsearch(sk, state, skip_to_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001637}
1638
1639static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
1640 struct sock *sk,
1641 struct tcp_sack_block *next_dup,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001642 struct tcp_sacktag_state *state,
1643 u32 skip_to_seq)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001644{
Ian Morris51456b22015-04-03 09:17:26 +01001645 if (!next_dup)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001646 return skb;
1647
1648 if (before(next_dup->start_seq, skip_to_seq)) {
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001649 skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
1650 skb = tcp_sacktag_walk(skb, sk, NULL, state,
1651 next_dup->start_seq, next_dup->end_seq,
1652 1);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001653 }
1654
1655 return skb;
1656}
1657
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001658static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001659{
1660 return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1661}
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663static int
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001664tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001665 u32 prior_snd_una, struct tcp_sacktag_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001668 const unsigned char *ptr = (skb_transport_header(ack_skb) +
1669 TCP_SKB_CB(ack_skb)->sacked);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001670 struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
Adam Langley4389dde2008-07-19 00:07:02 -07001671 struct tcp_sack_block sp[TCP_NUM_SACKS];
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001672 struct tcp_sack_block *cache;
1673 struct sk_buff *skb;
Adam Langley4389dde2008-07-19 00:07:02 -07001674 int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001675 int used_sacks;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001676 bool found_dup_sack = false;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001677 int i, j;
Baruch Evenfda03fb2007-02-04 23:35:57 -08001678 int first_sack_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001680 state->flag = 0;
1681 state->reord = tp->packets_out;
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001682
Ilpo Järvinend738cd82007-03-24 21:03:23 -07001683 if (!tp->sacked_out) {
Ilpo Järvinende83c052007-10-07 23:37:55 -07001684 if (WARN_ON(tp->fackets_out))
1685 tp->fackets_out = 0;
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001686 tcp_highest_sack_reset(sk);
Ilpo Järvinend738cd82007-03-24 21:03:23 -07001687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07001689 found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
David S. Millerd06e0212007-06-18 22:43:06 -07001690 num_sacks, prior_snd_una);
Yuchung Chengb9f64822016-09-19 23:39:14 -04001691 if (found_dup_sack) {
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001692 state->flag |= FLAG_DSACKING_ACK;
Yuchung Chengb9f64822016-09-19 23:39:14 -04001693 tp->delivered++; /* A spurious retransmission is delivered */
1694 }
Baruch Even6f746512007-02-04 23:36:42 -08001695
1696 /* Eliminate too old ACKs, but take into
1697 * account more or less fresh ones, they can
1698 * contain valid SACK info.
1699 */
1700 if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
1701 return 0;
1702
Ilpo Järvinen96a2d412007-11-14 15:47:18 -08001703 if (!tp->packets_out)
1704 goto out;
1705
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001706 used_sacks = 0;
1707 first_sack_index = 0;
1708 for (i = 0; i < num_sacks; i++) {
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001709 bool dup_sack = !i && found_dup_sack;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001710
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07001711 sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq);
1712 sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001713
1714 if (!tcp_is_sackblock_valid(tp, dup_sack,
1715 sp[used_sacks].start_seq,
1716 sp[used_sacks].end_seq)) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001717 int mib_idx;
1718
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001719 if (dup_sack) {
1720 if (!tp->undo_marker)
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001721 mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001722 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001723 mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001724 } else {
1725 /* Don't count olds caused by ACK reordering */
1726 if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) &&
1727 !after(sp[used_sacks].end_seq, tp->snd_una))
1728 continue;
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001729 mib_idx = LINUX_MIB_TCPSACKDISCARD;
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001730 }
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07001731
Eric Dumazetc10d9312016-04-29 14:16:47 -07001732 NET_INC_STATS(sock_net(sk), mib_idx);
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001733 if (i == 0)
1734 first_sack_index = -1;
1735 continue;
1736 }
1737
1738 /* Ignore very old stuff early */
1739 if (!after(sp[used_sacks].end_seq, prior_snd_una))
1740 continue;
1741
1742 used_sacks++;
1743 }
1744
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001745 /* order SACK blocks to allow in order walk of the retrans queue */
1746 for (i = used_sacks - 1; i > 0; i--) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001747 for (j = 0; j < i; j++) {
1748 if (after(sp[j].start_seq, sp[j + 1].start_seq)) {
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001749 swap(sp[j], sp[j + 1]);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001750
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001751 /* Track where the first SACK block goes to */
1752 if (j == first_sack_index)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001753 first_sack_index = j + 1;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001754 }
1755 }
1756 }
1757
Eric Dumazet75c119a2017-10-05 22:21:27 -07001758 state->mss_now = tcp_current_mss(sk);
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001759 state->fack_count = 0;
Eric Dumazet75c119a2017-10-05 22:21:27 -07001760 skb = NULL;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001761 i = 0;
1762
1763 if (!tp->sacked_out) {
1764 /* It's already past, so skip checking against it */
1765 cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
1766 } else {
1767 cache = tp->recv_sack_cache;
1768 /* Skip empty blocks in at head of the cache */
1769 while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq &&
1770 !cache->end_seq)
1771 cache++;
Baruch Evenfda03fb2007-02-04 23:35:57 -08001772 }
1773
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001774 while (i < used_sacks) {
Ilpo Järvinenfd6dad62007-11-15 19:49:47 -08001775 u32 start_seq = sp[i].start_seq;
1776 u32 end_seq = sp[i].end_seq;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001777 bool dup_sack = (found_dup_sack && (i == first_sack_index));
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001778 struct tcp_sack_block *next_dup = NULL;
Ilpo Järvinene56d6cd2007-11-01 00:09:37 -07001779
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001780 if (found_dup_sack && ((i + 1) == first_sack_index))
1781 next_dup = &sp[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001783 /* Skip too early cached blocks */
1784 while (tcp_sack_cache_ok(tp, cache) &&
1785 !before(start_seq, cache->end_seq))
1786 cache++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001788 /* Can skip some work by looking recv_sack_cache? */
1789 if (tcp_sack_cache_ok(tp, cache) && !dup_sack &&
1790 after(end_seq, cache->start_seq)) {
David S. Millerfe067e82007-03-07 12:12:44 -08001791
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001792 /* Head todo? */
1793 if (before(start_seq, cache->start_seq)) {
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001794 skb = tcp_sacktag_skip(skb, sk, state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001795 start_seq);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001796 skb = tcp_sacktag_walk(skb, sk, next_dup,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001797 state,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001798 start_seq,
1799 cache->start_seq,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001800 dup_sack);
Baruch Evenfda03fb2007-02-04 23:35:57 -08001801 }
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08001802
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001803 /* Rest of the block already fully processed? */
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001804 if (!after(end_seq, cache->end_seq))
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001805 goto advance_sp;
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001806
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001807 skb = tcp_maybe_skipping_dsack(skb, sk, next_dup,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001808 state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001809 cache->end_seq);
Ilpo Järvinene56d6cd2007-11-01 00:09:37 -07001810
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001811 /* ...tail remains todo... */
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001812 if (tcp_highest_sack_seq(tp) == cache->end_seq) {
Ilpo Järvinen20de20b2007-11-16 16:17:05 -08001813 /* ...but better entrypoint exists! */
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001814 skb = tcp_highest_sack(sk);
Ian Morris51456b22015-04-03 09:17:26 +01001815 if (!skb)
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001816 break;
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001817 state->fack_count = tp->fackets_out;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001818 cache++;
1819 goto walk;
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001822 skb = tcp_sacktag_skip(skb, sk, state, cache->end_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001823 /* Check overlap against next cached too (past this one already) */
1824 cache++;
1825 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
Ilpo Järvinenfbd52eb2007-11-10 21:24:19 -08001827
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001828 if (!before(start_seq, tcp_highest_sack_seq(tp))) {
1829 skb = tcp_highest_sack(sk);
Ian Morris51456b22015-04-03 09:17:26 +01001830 if (!skb)
Ilpo Järvinen6859d492007-12-02 00:48:06 +02001831 break;
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001832 state->fack_count = tp->fackets_out;
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001833 }
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001834 skb = tcp_sacktag_skip(skb, sk, state, start_seq);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001835
1836walk:
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001837 skb = tcp_sacktag_walk(skb, sk, next_dup, state,
Ilpo Järvinena1197f52008-12-05 22:42:22 -08001838 start_seq, end_seq, dup_sack);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001839
1840advance_sp:
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001841 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
1843
Ilpo Järvinen68f83532007-11-15 19:50:37 -08001844 /* Clear the head of the cache sack blocks so we can skip it next time */
1845 for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) {
1846 tp->recv_sack_cache[i].start_seq = 0;
1847 tp->recv_sack_cache[i].end_seq = 0;
1848 }
1849 for (j = 0; j < used_sacks; j++)
1850 tp->recv_sack_cache[i++] = sp[j];
1851
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001852 if ((state->reord < tp->fackets_out) &&
Yuchung Cheng9b441902013-03-20 13:32:58 +00001853 ((inet_csk(sk)->icsk_ca_state != TCP_CA_Loss) || tp->undo_marker))
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001854 tcp_update_reordering(sk, tp->fackets_out - state->reord, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Yuchung Cheng9dac8832015-04-29 11:28:30 -07001856 tcp_verify_left_out(tp);
Ilpo Järvinen96a2d412007-11-14 15:47:18 -08001857out:
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859#if FASTRETRANS_DEBUG > 0
Ilpo Järvinen547b7922008-07-25 21:43:18 -07001860 WARN_ON((int)tp->sacked_out < 0);
1861 WARN_ON((int)tp->lost_out < 0);
1862 WARN_ON((int)tp->retrans_out < 0);
1863 WARN_ON((int)tcp_packets_in_flight(tp) < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864#endif
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02001865 return state->flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
Ilpo Järvinen882beba2008-04-07 22:33:07 -07001868/* Limits sacked_out so that sum with lost_out isn't ever larger than
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001869 * packets_out. Returns false if sacked_out adjustement wasn't necessary.
Ilpo Järvinen30935cf2007-02-21 23:01:36 -08001870 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001871static bool tcp_limit_reno_sacked(struct tcp_sock *tp)
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001872{
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001873 u32 holes;
1874
1875 holes = max(tp->lost_out, 1U);
1876 holes = min(holes, tp->packets_out);
1877
1878 if ((tp->sacked_out + holes) > tp->packets_out) {
1879 tp->sacked_out = tp->packets_out - holes;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001880 return true;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001881 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001882 return false;
Ilpo Järvinen882beba2008-04-07 22:33:07 -07001883}
1884
1885/* If we receive more dupacks than we expected counting segments
1886 * in assumption of absent reordering, interpret this as reordering.
1887 * The only another reason could be bug in receiver TCP.
1888 */
1889static void tcp_check_reno_reordering(struct sock *sk, const int addend)
1890{
1891 struct tcp_sock *tp = tcp_sk(sk);
1892 if (tcp_limit_reno_sacked(tp))
1893 tcp_update_reordering(sk, tp->packets_out + addend, 0);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001894}
1895
1896/* Emulate SACKs for SACKless connection: account for a new dupack. */
1897
1898static void tcp_add_reno_sack(struct sock *sk)
1899{
1900 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Chengddf1af62016-02-02 10:33:06 -08001901 u32 prior_sacked = tp->sacked_out;
1902
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001903 tp->sacked_out++;
1904 tcp_check_reno_reordering(sk, 0);
Yuchung Chengddf1af62016-02-02 10:33:06 -08001905 if (tp->sacked_out > prior_sacked)
1906 tp->delivered++; /* Some out-of-order packet is delivered */
Ilpo Järvinen005903b2007-08-09 14:44:16 +03001907 tcp_verify_left_out(tp);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001908}
1909
1910/* Account for ACK, ACKing some data in Reno Recovery phase. */
1911
1912static void tcp_remove_reno_sacks(struct sock *sk, int acked)
1913{
1914 struct tcp_sock *tp = tcp_sk(sk);
1915
1916 if (acked > 0) {
1917 /* One ACK acked hole. The rest eat duplicate ACKs. */
Yuchung Chengddf1af62016-02-02 10:33:06 -08001918 tp->delivered += max_t(int, acked - tp->sacked_out, 1);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001919 if (acked - 1 >= tp->sacked_out)
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001920 tp->sacked_out = 0;
1921 else
Ilpo Järvinen056834d2007-12-31 14:57:14 -08001922 tp->sacked_out -= acked - 1;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001923 }
1924 tcp_check_reno_reordering(sk, acked);
Ilpo Järvinen005903b2007-08-09 14:44:16 +03001925 tcp_verify_left_out(tp);
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001926}
1927
1928static inline void tcp_reset_reno_sack(struct tcp_sock *tp)
1929{
1930 tp->sacked_out = 0;
Ilpo Järvinen4ddf6672007-05-27 01:52:00 -07001931}
1932
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001933void tcp_clear_retrans(struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 tp->retrans_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 tp->lost_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 tp->undo_marker = 0;
Yuchung Cheng6e08d5e2014-07-02 12:07:16 -07001938 tp->undo_retrans = -1;
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07001939 tp->fackets_out = 0;
1940 tp->sacked_out = 0;
1941}
1942
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001943static inline void tcp_init_undo(struct tcp_sock *tp)
1944{
1945 tp->undo_marker = tp->snd_una;
1946 /* Retransmission still in flight may cause DSACKs later. */
1947 tp->undo_retrans = tp->retrans_out ? : -1;
1948}
1949
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001950/* Enter Loss state. If we detect SACK reneging, forget all SACK information
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 * and reset tags completely, otherwise preserve SACKs. If receiver
1952 * dropped its ofo queue, we will know this due to reneging detection.
1953 */
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001954void tcp_enter_loss(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001956 const struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov1043e252016-02-03 09:46:52 +02001958 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 struct sk_buff *skb;
Yuchung Chengcc663f42017-04-07 11:42:05 -07001960 bool new_recovery = icsk->icsk_ca_state < TCP_CA_Recovery;
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001961 bool is_reneg; /* is receiver reneging on SACKs? */
Neal Cardwell0682e692016-09-19 23:39:13 -04001962 bool mark_lost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 /* Reduce ssthresh if it has not yet been made inside this window. */
Yuchung Chenge33099f2013-03-20 13:33:00 +00001965 if (icsk->icsk_ca_state <= TCP_CA_Disorder ||
1966 !after(tp->high_seq, tp->snd_una) ||
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001967 (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) {
1968 tp->prior_ssthresh = tcp_current_ssthresh(sk);
Yuchung Cheng4faf7832017-08-03 20:38:51 -07001969 tp->prior_cwnd = tp->snd_cwnd;
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001970 tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
1971 tcp_ca_event(sk, CA_EVENT_LOSS);
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001972 tcp_init_undo(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 tp->snd_cwnd = 1;
1975 tp->snd_cwnd_cnt = 0;
Eric Dumazetc2203cf2017-05-16 14:00:04 -07001976 tp->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Yuchung Cheng989e04c2014-08-22 14:15:22 -07001978 tp->retrans_out = 0;
1979 tp->lost_out = 0;
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07001980
1981 if (tcp_is_reno(tp))
1982 tcp_reset_reno_sack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Eric Dumazet75c119a2017-10-05 22:21:27 -07001984 skb = tcp_rtx_queue_head(sk);
Neal Cardwell5ae344c2014-08-04 19:12:29 -04001985 is_reneg = skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED);
1986 if (is_reneg) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07001987 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSACKRENEGING);
Ilpo Järvinen4cd82992007-10-11 17:34:57 -07001988 tp->sacked_out = 0;
1989 tp->fackets_out = 0;
Ilpo Järvinenb7689202007-09-20 11:37:19 -07001990 }
Ilpo Järvinen64edc272008-09-20 21:18:32 -07001991 tcp_clear_all_retrans_hints(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Eric Dumazet75c119a2017-10-05 22:21:27 -07001993 skb_rbtree_walk_from(skb) {
Neal Cardwell0682e692016-09-19 23:39:13 -04001994 mark_lost = (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||
1995 is_reneg);
1996 if (mark_lost)
1997 tcp_sum_lost(tp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED;
Neal Cardwell0682e692016-09-19 23:39:13 -04001999 if (mark_lost) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
2001 TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
2002 tp->lost_out += tcp_skb_pcount(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
2004 }
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002005 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Yuchung Cheng74c181d2013-08-12 16:41:25 -07002007 /* Timeout in disordered state after receiving substantial DUPACKs
2008 * suggests that the degree of reordering is over-estimated.
2009 */
2010 if (icsk->icsk_ca_state <= TCP_CA_Disorder &&
Nikolay Borisov1043e252016-02-03 09:46:52 +02002011 tp->sacked_out >= net->ipv4.sysctl_tcp_reordering)
Yuchung Cheng74c181d2013-08-12 16:41:25 -07002012 tp->reordering = min_t(unsigned int, tp->reordering,
Nikolay Borisov1043e252016-02-03 09:46:52 +02002013 net->ipv4.sysctl_tcp_reordering);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002014 tcp_set_ca_state(sk, TCP_CA_Loss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 tp->high_seq = tp->snd_nxt;
Florian Westphal735d3832014-09-29 13:08:30 +02002016 tcp_ecn_queue_cwr(tp);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002017
Yuchung Chengcc663f42017-04-07 11:42:05 -07002018 /* F-RTO RFC5682 sec 3.1 step 1: retransmit SND.UNA if no previous
2019 * loss recovery is underway except recurring timeout(s) on
2020 * the same SND.UNA (sec 3.2). Disable F-RTO on path MTU probing
2021 *
2022 * In theory F-RTO can be used repeatedly during loss recovery.
2023 * In practice this interacts badly with broken middle-boxes that
2024 * falsely raise the receive window, which results in repeated
2025 * timeouts and stop-and-go behavior.
Yuchung Chenge33099f2013-03-20 13:33:00 +00002026 */
Eric Dumazetaf9b69a2017-10-26 21:55:10 -07002027 tp->frto = net->ipv4.sysctl_tcp_frto &&
Yuchung Chengcc663f42017-04-07 11:42:05 -07002028 (new_recovery || icsk->icsk_retransmits) &&
2029 !inet_csk(sk)->icsk_mtup.probe_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002032/* If ACK arrived pointing to a remembered SACK, it means that our
2033 * remembered SACKs do not reflect real state of receiver i.e.
2034 * receiver _host_ is heavily congested (or buggy).
2035 *
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002036 * To avoid big spurious retransmission bursts due to transient SACK
2037 * scoreboard oddities that look like reneging, we give the receiver a
2038 * little time (max(RTT/2, 10ms)) to send us some more ACKs that will
2039 * restore sanity to the SACK scoreboard. If the apparent reneging
2040 * persists until this RTO then we'll clear the SACK scoreboard.
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002041 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002042static bool tcp_check_sack_reneging(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002044 if (flag & FLAG_SACK_RENEGING) {
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002045 struct tcp_sock *tp = tcp_sk(sk);
2046 unsigned long delay = max(usecs_to_jiffies(tp->srtt_us >> 4),
2047 msecs_to_jiffies(10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002049 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
Neal Cardwell5ae344c2014-08-04 19:12:29 -04002050 delay, TCP_RTO_MAX);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002051 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002053 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002056static inline int tcp_fackets_out(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002058 return tcp_is_reno(tp) ? tp->sacked_out + 1 : tp->fackets_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002061/* Heurestics to calculate number of duplicate ACKs. There's no dupACKs
2062 * counter when SACK is enabled (without SACK, sacked_out is used for
2063 * that purpose).
2064 *
2065 * Instead, with FACK TCP uses fackets_out that includes both SACKed
2066 * segments up to the highest received SACK block so far and holes in
2067 * between them.
2068 *
2069 * With reordering, holes may still be in flight, so RFC3517 recovery
2070 * uses pure sacked_out (total number of SACKed segments) even though
2071 * it violates the RFC that uses duplicate ACKs, often these are equal
2072 * but when e.g. out-of-window ACKs or packet duplication occurs,
2073 * they differ. Since neither occurs due to loss, TCP should really
2074 * ignore them.
2075 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04002076static inline int tcp_dupack_heuristics(const struct tcp_sock *tp)
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002077{
2078 return tcp_is_fack(tp) ? tp->fackets_out : tp->sacked_out + 1;
2079}
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081/* Linux NewReno/SACK/FACK/ECN state machine.
2082 * --------------------------------------
2083 *
2084 * "Open" Normal state, no dubious events, fast path.
2085 * "Disorder" In all the respects it is "Open",
2086 * but requires a bit more attention. It is entered when
2087 * we see some SACKs or dupacks. It is split of "Open"
2088 * mainly to move some processing from fast path to slow one.
2089 * "CWR" CWND was reduced due to some Congestion Notification event.
2090 * It can be ECN, ICMP source quench, local device congestion.
2091 * "Recovery" CWND was reduced, we are fast-retransmitting.
2092 * "Loss" CWND was reduced due to RTO timeout or SACK reneging.
2093 *
2094 * tcp_fastretrans_alert() is entered:
2095 * - each incoming ACK, if state is not "Open"
2096 * - when arrived ACK is unusual, namely:
2097 * * SACK
2098 * * Duplicate ACK.
2099 * * ECN ECE.
2100 *
2101 * Counting packets in flight is pretty simple.
2102 *
2103 * in_flight = packets_out - left_out + retrans_out
2104 *
2105 * packets_out is SND.NXT-SND.UNA counted in packets.
2106 *
2107 * retrans_out is number of retransmitted segments.
2108 *
2109 * left_out is number of segments left network, but not ACKed yet.
2110 *
2111 * left_out = sacked_out + lost_out
2112 *
2113 * sacked_out: Packets, which arrived to receiver out of order
2114 * and hence not ACKed. With SACKs this number is simply
2115 * amount of SACKed data. Even without SACKs
2116 * it is easy to give pretty reliable estimate of this number,
2117 * counting duplicate ACKs.
2118 *
2119 * lost_out: Packets lost by network. TCP has no explicit
2120 * "loss notification" feedback from network (for now).
2121 * It means that this number can be only _guessed_.
2122 * Actually, it is the heuristics to predict lossage that
2123 * distinguishes different algorithms.
2124 *
2125 * F.e. after RTO, when all the queue is considered as lost,
2126 * lost_out = packets_out and in_flight = retrans_out.
2127 *
Yuchung Chenga0370b32017-01-12 22:11:36 -08002128 * Essentially, we have now a few algorithms detecting
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 * lost packets.
2130 *
Yuchung Chenga0370b32017-01-12 22:11:36 -08002131 * If the receiver supports SACK:
2132 *
2133 * RFC6675/3517: It is the conventional algorithm. A packet is
2134 * considered lost if the number of higher sequence packets
2135 * SACKed is greater than or equal the DUPACK thoreshold
2136 * (reordering). This is implemented in tcp_mark_head_lost and
2137 * tcp_update_scoreboard.
2138 *
2139 * RACK (draft-ietf-tcpm-rack-01): it is a newer algorithm
2140 * (2017-) that checks timing instead of counting DUPACKs.
2141 * Essentially a packet is considered lost if it's not S/ACKed
2142 * after RTT + reordering_window, where both metrics are
2143 * dynamically measured and adjusted. This is implemented in
2144 * tcp_rack_mark_lost.
2145 *
Yuchung Cheng94bdc972017-01-12 22:11:42 -08002146 * FACK (Disabled by default. Subsumbed by RACK):
2147 * It is the simplest heuristics. As soon as we decided
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 * that something is lost, we decide that _all_ not SACKed
2149 * packets until the most forward SACK are lost. I.e.
2150 * lost_out = fackets_out - sacked_out and left_out = fackets_out.
2151 * It is absolutely correct estimate, if network does not reorder
2152 * packets. And it loses any connection to reality when reordering
2153 * takes place. We use FACK by default until reordering
2154 * is suspected on the path to this destination.
2155 *
Yuchung Chenga0370b32017-01-12 22:11:36 -08002156 * If the receiver does not support SACK:
2157 *
2158 * NewReno (RFC6582): in Recovery we assume that one segment
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 * is lost (classic Reno). While we are in Recovery and
2160 * a partial ACK arrives, we assume that one more packet
2161 * is lost (NewReno). This heuristics are the same in NewReno
2162 * and SACK.
2163 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 * Really tricky (and requiring careful tuning) part of algorithm
2165 * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue().
2166 * The first determines the moment _when_ we should reduce CWND and,
2167 * hence, slow down forward transmission. In fact, it determines the moment
2168 * when we decide that hole is caused by loss, rather than by a reorder.
2169 *
2170 * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill
2171 * holes, caused by lost packets.
2172 *
2173 * And the most logically complicated part of algorithm is undo
2174 * heuristics. We detect false retransmits due to both too early
2175 * fast retransmit (reordering) and underestimated RTO, analyzing
2176 * timestamps and D-SACKs. When we detect that some segments were
2177 * retransmitted by mistake and CWND reduction was wrong, we undo
2178 * window reduction and abort recovery phase. This logic is hidden
2179 * inside several functions named tcp_try_undo_<something>.
2180 */
2181
2182/* This function decides, when we should leave Disordered state
2183 * and enter Recovery phase, reducing congestion window.
2184 *
2185 * Main question: may we further continue forward transmission
2186 * with the same cwnd?
2187 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002188static bool tcp_time_to_recover(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002190 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 /* Trick#1: The loss is proven. */
2193 if (tp->lost_out)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002194 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 /* Not-A-Trick#2 : Classic rule... */
Andreas Petlundea84e552009-10-27 03:27:21 +00002197 if (tcp_dupack_heuristics(tp) > tp->reordering)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002198 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002200 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
Yuchung Cheng974c1232012-01-19 14:42:21 +00002203/* Detect loss in event "A" above by marking head of queue up as lost.
2204 * For FACK or non-SACK(Reno) senders, the first "packets" number of segments
2205 * are considered lost. For RFC3517 SACK, a segment is considered lost if it
2206 * has at least tp->reordering SACKed seqments above it; "packets" refers to
2207 * the maximum SACKed segments to pass before reaching this limit.
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002208 */
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002209static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002211 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 struct sk_buff *skb;
Neal Cardwelld88270e2016-01-25 14:01:53 -08002213 int cnt, oldcnt, lost;
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002214 unsigned int mss;
Yuchung Cheng974c1232012-01-19 14:42:21 +00002215 /* Use SACK to deduce losses of new sequences sent during recovery */
2216 const u32 loss_high = tcp_is_sack(tp) ? tp->snd_nxt : tp->high_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002218 WARN_ON(packets > tp->packets_out);
Eric Dumazet5e76ee42017-10-05 22:21:24 -07002219 skb = tp->lost_skb_hint;
2220 if (skb) {
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002221 /* Head already handled? */
Eric Dumazet5e76ee42017-10-05 22:21:24 -07002222 if (mark_head && after(TCP_SKB_CB(skb)->seq, tp->snd_una))
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002223 return;
Eric Dumazet5e76ee42017-10-05 22:21:24 -07002224 cnt = tp->lost_cnt_hint;
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002225 } else {
Eric Dumazet75c119a2017-10-05 22:21:27 -07002226 skb = tcp_rtx_queue_head(sk);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002227 cnt = 0;
2228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Eric Dumazet75c119a2017-10-05 22:21:27 -07002230 skb_rbtree_walk_from(skb) {
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002231 /* TODO: do this better */
2232 /* this is not the most efficient way to do this... */
2233 tp->lost_skb_hint = skb;
2234 tp->lost_cnt_hint = cnt;
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002235
Yuchung Cheng974c1232012-01-19 14:42:21 +00002236 if (after(TCP_SKB_CB(skb)->end_seq, loss_high))
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002237 break;
2238
2239 oldcnt = cnt;
Ilpo Järvinenad1984e2008-01-30 20:06:02 -08002240 if (tcp_is_fack(tp) || tcp_is_reno(tp) ||
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002241 (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
2242 cnt += tcp_skb_pcount(skb);
2243
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002244 if (cnt > packets) {
Yuchung Chengb3de7552010-09-24 13:22:06 +00002245 if ((tcp_is_sack(tp) && !tcp_is_fack(tp)) ||
Neal Cardwellc0638c22012-03-02 21:36:51 +00002246 (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||
Yuchung Chengb3de7552010-09-24 13:22:06 +00002247 (oldcnt >= packets))
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002248 break;
2249
Eric Dumazetf69ad292015-06-11 09:15:18 -07002250 mss = tcp_skb_mss(skb);
Neal Cardwelld88270e2016-01-25 14:01:53 -08002251 /* If needed, chop off the prefix to mark as lost. */
2252 lost = (packets - oldcnt) * mss;
2253 if (lost < skb->len &&
Eric Dumazet75c119a2017-10-05 22:21:27 -07002254 tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb,
2255 lost, mss, GFP_ATOMIC) < 0)
Ilpo Järvinenc137f3d2008-04-07 22:32:38 -07002256 break;
2257 cnt = packets;
2258 }
2259
Ilpo Järvinen41ea36e2008-09-20 21:19:22 -07002260 tcp_skb_mark_lost(tp, skb);
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002261
2262 if (mark_head)
2263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002265 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
2268/* Account newly detected lost packet(s) */
2269
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002270static void tcp_update_scoreboard(struct sock *sk, int fast_rexmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002272 struct tcp_sock *tp = tcp_sk(sk);
2273
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002274 if (tcp_is_reno(tp)) {
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002275 tcp_mark_head_lost(sk, 1, 1);
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002276 } else if (tcp_is_fack(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 int lost = tp->fackets_out - tp->reordering;
2278 if (lost <= 0)
2279 lost = 1;
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002280 tcp_mark_head_lost(sk, lost, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 } else {
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002282 int sacked_upto = tp->sacked_out - tp->reordering;
Ilpo Järvinen1fdb9362010-10-14 01:42:30 +00002283 if (sacked_upto >= 0)
2284 tcp_mark_head_lost(sk, sacked_upto, 0);
2285 else if (fast_rexmit)
2286 tcp_mark_head_lost(sk, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Yuchung Cheng77c63122015-10-16 21:57:44 -07002290static bool tcp_tsopt_ecr_before(const struct tcp_sock *tp, u32 when)
2291{
2292 return tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
2293 before(tp->rx_opt.rcv_tsecr, when);
2294}
2295
Yuchung Cheng659a8ad2015-10-16 21:57:46 -07002296/* skb is spurious retransmitted if the returned timestamp echo
2297 * reply is prior to the skb transmission time
2298 */
2299static bool tcp_skb_spurious_retrans(const struct tcp_sock *tp,
2300 const struct sk_buff *skb)
2301{
2302 return (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS) &&
2303 tcp_tsopt_ecr_before(tp, tcp_skb_timestamp(skb));
2304}
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306/* Nothing was retransmitted or returned timestamp is less
2307 * than timestamp of the first retransmission.
2308 */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00002309static inline bool tcp_packet_delayed(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 return !tp->retrans_stamp ||
Yuchung Cheng77c63122015-10-16 21:57:44 -07002312 tcp_tsopt_ecr_before(tp, tp->retrans_stamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
2315/* Undo procedures. */
2316
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002317/* We can clear retrans_stamp when there are no retransmissions in the
2318 * window. It would seem that it is trivially available for us in
2319 * tp->retrans_out, however, that kind of assumptions doesn't consider
2320 * what will happen if errors occur when sending retransmission for the
2321 * second time. ...It could the that such segment has only
2322 * TCPCB_EVER_RETRANS set at the present time. It seems that checking
2323 * the head skb is enough except for some reneging corner cases that
2324 * are not worth the effort.
2325 *
2326 * Main reason for all this complexity is the fact that connection dying
2327 * time now depends on the validity of the retrans_stamp, in particular,
2328 * that successive retransmissions of a segment must not advance
2329 * retrans_stamp under any conditions.
2330 */
2331static bool tcp_any_retrans_done(const struct sock *sk)
2332{
2333 const struct tcp_sock *tp = tcp_sk(sk);
2334 struct sk_buff *skb;
2335
2336 if (tp->retrans_out)
2337 return true;
2338
Eric Dumazet75c119a2017-10-05 22:21:27 -07002339 skb = tcp_rtx_queue_head(sk);
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002340 if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS))
2341 return true;
2342
2343 return false;
2344}
2345
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002346static void DBGUNDO(struct sock *sk, const char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
Joe Perches39347882017-09-10 19:02:25 -07002348#if FASTRETRANS_DEBUG > 1
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002349 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 struct inet_sock *inet = inet_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002351
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002352 if (sk->sk_family == AF_INET) {
Joe Perches91df42b2012-05-15 14:11:54 +00002353 pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n",
2354 msg,
2355 &inet->inet_daddr, ntohs(inet->inet_dport),
2356 tp->snd_cwnd, tcp_left_out(tp),
2357 tp->snd_ssthresh, tp->prior_ssthresh,
2358 tp->packets_out);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002359 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +00002360#if IS_ENABLED(CONFIG_IPV6)
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002361 else if (sk->sk_family == AF_INET6) {
Joe Perches91df42b2012-05-15 14:11:54 +00002362 pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n",
2363 msg,
Eric Dumazet019b1c92016-09-22 17:54:00 -07002364 &sk->sk_v6_daddr, ntohs(inet->inet_dport),
Joe Perches91df42b2012-05-15 14:11:54 +00002365 tp->snd_cwnd, tcp_left_out(tp),
2366 tp->snd_ssthresh, tp->prior_ssthresh,
2367 tp->packets_out);
YOSHIFUJI Hideaki569508c2008-04-14 04:09:36 -07002368 }
2369#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370#endif
Joe Perches39347882017-09-10 19:02:25 -07002371}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Yuchung Cheng7026b912013-05-29 14:20:13 +00002373static void tcp_undo_cwnd_reduction(struct sock *sk, bool unmark_loss)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002375 struct tcp_sock *tp = tcp_sk(sk);
2376
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002377 if (unmark_loss) {
2378 struct sk_buff *skb;
2379
Eric Dumazet75c119a2017-10-05 22:21:27 -07002380 skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002381 TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
2382 }
2383 tp->lost_out = 0;
2384 tcp_clear_all_retrans_hints(tp);
2385 }
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (tp->prior_ssthresh) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002388 const struct inet_connection_sock *icsk = inet_csk(sk);
2389
Florian Westphale9799182016-11-21 14:18:38 +01002390 tp->snd_cwnd = icsk->icsk_ca_ops->undo_cwnd(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Yuchung Cheng7026b912013-05-29 14:20:13 +00002392 if (tp->prior_ssthresh > tp->snd_ssthresh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 tp->snd_ssthresh = tp->prior_ssthresh;
Florian Westphal735d3832014-09-29 13:08:30 +02002394 tcp_ecn_withdraw_cwr(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002397 tp->snd_cwnd_stamp = tcp_jiffies32;
Yuchung Cheng7026b912013-05-29 14:20:13 +00002398 tp->undo_marker = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00002401static inline bool tcp_may_undo(const struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002403 return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
2406/* People celebrate: "We love our President!" */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002407static bool tcp_try_undo_recovery(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002409 struct tcp_sock *tp = tcp_sk(sk);
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (tcp_may_undo(tp)) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002412 int mib_idx;
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 /* Happy end! We did not retransmit anything
2415 * or our original transmission succeeded.
2416 */
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002417 DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans");
Yuchung Cheng7026b912013-05-29 14:20:13 +00002418 tcp_undo_cwnd_reduction(sk, false);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002419 if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss)
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002420 mib_idx = LINUX_MIB_TCPLOSSUNDO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07002422 mib_idx = LINUX_MIB_TCPFULLUNDO;
2423
Eric Dumazetc10d9312016-04-29 14:16:47 -07002424 NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002426 if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 /* Hold old state until something *above* high_seq
2428 * is ACKed. For Reno it is MUST to prevent false
2429 * fast retransmits (RFC2582). SACK TCP is safe. */
Marcelo Leitner1f37bf82014-11-04 17:15:08 -02002430 if (!tcp_any_retrans_done(sk))
2431 tp->retrans_stamp = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002432 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 }
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002434 tcp_set_ca_state(sk, TCP_CA_Open);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002435 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
2438/* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002439static bool tcp_try_undo_dsack(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002441 struct tcp_sock *tp = tcp_sk(sk);
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (tp->undo_marker && !tp->undo_retrans) {
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002444 DBGUNDO(sk, "D-SACK");
Yuchung Cheng7026b912013-05-29 14:20:13 +00002445 tcp_undo_cwnd_reduction(sk, false);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002446 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKUNDO);
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002447 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002449 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
Yuchung Chenge33099f2013-03-20 13:33:00 +00002452/* Undo during loss recovery after partial ACK or using F-RTO. */
2453static bool tcp_try_undo_loss(struct sock *sk, bool frto_undo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002455 struct tcp_sock *tp = tcp_sk(sk);
2456
Yuchung Chenge33099f2013-03-20 13:33:00 +00002457 if (frto_undo || tcp_may_undo(tp)) {
Yuchung Cheng7026b912013-05-29 14:20:13 +00002458 tcp_undo_cwnd_reduction(sk, true);
Stephen Hemminger6a438bb2005-11-10 17:14:59 -08002459
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002460 DBGUNDO(sk, "partial loss");
Eric Dumazetc10d9312016-04-29 14:16:47 -07002461 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSUNDO);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002462 if (frto_undo)
Eric Dumazetc10d9312016-04-29 14:16:47 -07002463 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07002464 LINUX_MIB_TCPSPURIOUSRTOS);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07002465 inet_csk(sk)->icsk_retransmits = 0;
Yuchung Chenge33099f2013-03-20 13:33:00 +00002466 if (frto_undo || tcp_is_sack(tp))
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002467 tcp_set_ca_state(sk, TCP_CA_Open);
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002468 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00002470 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Yuchung Cheng37598242015-07-01 14:11:15 -07002473/* The cwnd reduction in CWR and Recovery uses the PRR algorithm in RFC 6937.
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002474 * It computes the number of packets to send (sndcnt) based on packets newly
2475 * delivered:
2476 * 1) If the packets in flight is larger than ssthresh, PRR spreads the
2477 * cwnd reductions across a full RTT.
Yuchung Cheng37598242015-07-01 14:11:15 -07002478 * 2) Otherwise PRR uses packet conservation to send as much as delivered.
2479 * But when the retransmits are acked without further losses, PRR
2480 * slow starts cwnd up to ssthresh to speed up the recovery.
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002481 */
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002482static void tcp_init_cwnd_reduction(struct sock *sk)
Yuchung Cheng684bad12012-09-02 17:38:04 +00002483{
2484 struct tcp_sock *tp = tcp_sk(sk);
2485
2486 tp->high_seq = tp->snd_nxt;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00002487 tp->tlp_high_seq = 0;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002488 tp->snd_cwnd_cnt = 0;
2489 tp->prior_cwnd = tp->snd_cwnd;
2490 tp->prr_delivered = 0;
2491 tp->prr_out = 0;
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002492 tp->snd_ssthresh = inet_csk(sk)->icsk_ca_ops->ssthresh(sk);
Florian Westphal735d3832014-09-29 13:08:30 +02002493 tcp_ecn_queue_cwr(tp);
Yuchung Cheng684bad12012-09-02 17:38:04 +00002494}
2495
Yuchung Cheng57dde7f2017-01-12 22:11:33 -08002496void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int flag)
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002497{
2498 struct tcp_sock *tp = tcp_sk(sk);
2499 int sndcnt = 0;
2500 int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp);
2501
Yuchung Cheng8b8a3212016-01-06 12:42:38 -08002502 if (newly_acked_sacked <= 0 || WARN_ON_ONCE(!tp->prior_cwnd))
2503 return;
2504
Yuchung Cheng684bad12012-09-02 17:38:04 +00002505 tp->prr_delivered += newly_acked_sacked;
Yuchung Cheng37598242015-07-01 14:11:15 -07002506 if (delta < 0) {
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002507 u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered +
2508 tp->prior_cwnd - 1;
2509 sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out;
Yuchung Cheng37598242015-07-01 14:11:15 -07002510 } else if ((flag & FLAG_RETRANS_DATA_ACKED) &&
2511 !(flag & FLAG_LOST_RETRANS)) {
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002512 sndcnt = min_t(int, delta,
2513 max_t(int, tp->prr_delivered - tp->prr_out,
2514 newly_acked_sacked) + 1);
Yuchung Cheng37598242015-07-01 14:11:15 -07002515 } else {
2516 sndcnt = min(delta, newly_acked_sacked);
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002517 }
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002518 /* Force a fast retransmit upon entering fast recovery */
2519 sndcnt = max(sndcnt, (tp->prr_out ? 0 : 1));
Yuchung Chengfb4d3d12012-09-02 17:38:03 +00002520 tp->snd_cwnd = tcp_packets_in_flight(tp) + sndcnt;
2521}
2522
Yuchung Cheng684bad12012-09-02 17:38:04 +00002523static inline void tcp_end_cwnd_reduction(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002525 struct tcp_sock *tp = tcp_sk(sk);
Nandita Dukkipatia262f0c2011-08-21 20:21:57 +00002526
Yuchung Chengc0402762016-09-19 23:39:21 -04002527 if (inet_csk(sk)->icsk_ca_ops->cong_control)
2528 return;
2529
Yuchung Cheng684bad12012-09-02 17:38:04 +00002530 /* Reset cwnd to ssthresh in CWR or Recovery (unless it's undone) */
Yuchung Chenged254972017-08-01 13:22:32 -07002531 if (tp->snd_ssthresh < TCP_INFINITE_SSTHRESH &&
2532 (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR || tp->undo_marker)) {
Yuchung Cheng684bad12012-09-02 17:38:04 +00002533 tp->snd_cwnd = tp->snd_ssthresh;
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002534 tp->snd_cwnd_stamp = tcp_jiffies32;
Yuchung Cheng67d41202011-03-14 10:57:03 +00002535 }
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002536 tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Yuchung Cheng684bad12012-09-02 17:38:04 +00002539/* Enter CWR state. Disable cwnd undo since congestion is proven with ECN */
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002540void tcp_enter_cwr(struct sock *sk)
Yuchung Cheng09484d12012-09-02 17:38:02 +00002541{
2542 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002543
2544 tp->prior_ssthresh = 0;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002545 if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
Yuchung Cheng09484d12012-09-02 17:38:02 +00002546 tp->undo_marker = 0;
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002547 tcp_init_cwnd_reduction(sk);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002548 tcp_set_ca_state(sk, TCP_CA_CWR);
2549 }
2550}
Kenneth Klette Jonassen7782ad82015-06-10 19:08:16 +02002551EXPORT_SYMBOL(tcp_enter_cwr);
Yuchung Cheng09484d12012-09-02 17:38:02 +00002552
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002553static void tcp_try_keep_open(struct sock *sk)
2554{
2555 struct tcp_sock *tp = tcp_sk(sk);
2556 int state = TCP_CA_Open;
2557
Neal Cardwellf6982042011-11-16 08:58:04 +00002558 if (tcp_left_out(tp) || tcp_any_retrans_done(sk))
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002559 state = TCP_CA_Disorder;
2560
2561 if (inet_csk(sk)->icsk_ca_state != state) {
2562 tcp_set_ca_state(sk, state);
2563 tp->high_seq = tp->snd_nxt;
2564 }
2565}
2566
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002567static void tcp_try_to_open(struct sock *sk, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002569 struct tcp_sock *tp = tcp_sk(sk);
2570
Ilpo Järvinen86426c22007-08-09 14:45:17 +03002571 tcp_verify_left_out(tp);
2572
Yuchung Cheng9b441902013-03-20 13:32:58 +00002573 if (!tcp_any_retrans_done(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 tp->retrans_stamp = 0;
2575
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002576 if (flag & FLAG_ECE)
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002577 tcp_enter_cwr(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002579 if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) {
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07002580 tcp_try_keep_open(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582}
2583
John Heffner5d424d52006-03-20 17:53:41 -08002584static void tcp_mtup_probe_failed(struct sock *sk)
2585{
2586 struct inet_connection_sock *icsk = inet_csk(sk);
2587
2588 icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1;
2589 icsk->icsk_mtup.probe_size = 0;
Eric Dumazetc10d9312016-04-29 14:16:47 -07002590 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPFAIL);
John Heffner5d424d52006-03-20 17:53:41 -08002591}
2592
Ilpo Järvinen72211e92009-03-14 14:23:04 +00002593static void tcp_mtup_probe_success(struct sock *sk)
John Heffner5d424d52006-03-20 17:53:41 -08002594{
2595 struct tcp_sock *tp = tcp_sk(sk);
2596 struct inet_connection_sock *icsk = inet_csk(sk);
2597
2598 /* FIXME: breaks with very large cwnd */
2599 tp->prior_ssthresh = tcp_current_ssthresh(sk);
2600 tp->snd_cwnd = tp->snd_cwnd *
2601 tcp_mss_to_mtu(sk, tp->mss_cache) /
2602 icsk->icsk_mtup.probe_size;
2603 tp->snd_cwnd_cnt = 0;
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002604 tp->snd_cwnd_stamp = tcp_jiffies32;
John Heffner9c6d5e52010-10-06 21:18:02 -07002605 tp->snd_ssthresh = tcp_current_ssthresh(sk);
John Heffner5d424d52006-03-20 17:53:41 -08002606
2607 icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size;
2608 icsk->icsk_mtup.probe_size = 0;
2609 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002610 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPSUCCESS);
John Heffner5d424d52006-03-20 17:53:41 -08002611}
2612
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002613/* Do a simple retransmit without using the backoff mechanisms in
2614 * tcp_timer. This is used for path mtu discovery.
2615 * The socket is already locked here.
2616 */
2617void tcp_simple_retransmit(struct sock *sk)
2618{
2619 const struct inet_connection_sock *icsk = inet_csk(sk);
2620 struct tcp_sock *tp = tcp_sk(sk);
2621 struct sk_buff *skb;
Ilpo Järvinen0c54b852009-03-14 14:23:05 +00002622 unsigned int mss = tcp_current_mss(sk);
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002623 u32 prior_lost = tp->lost_out;
2624
Eric Dumazet75c119a2017-10-05 22:21:27 -07002625 skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
Ilpo Järvinen775ffab2008-12-05 22:41:26 -08002626 if (tcp_skb_seglen(skb) > mss &&
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002627 !(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) {
2628 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
2629 TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
2630 tp->retrans_out -= tcp_skb_pcount(skb);
2631 }
2632 tcp_skb_mark_lost_uncond_verify(tp, skb);
2633 }
2634 }
2635
2636 tcp_clear_retrans_hints_partial(tp);
2637
2638 if (prior_lost == tp->lost_out)
2639 return;
2640
2641 if (tcp_is_reno(tp))
2642 tcp_limit_reno_sacked(tp);
2643
2644 tcp_verify_left_out(tp);
2645
2646 /* Don't muck with the congestion window here.
2647 * Reason is that we do not increase amount of _data_
2648 * in network, but units changed and effective
2649 * cwnd/ssthresh really reduced now.
2650 */
2651 if (icsk->icsk_ca_state != TCP_CA_Loss) {
2652 tp->high_seq = tp->snd_nxt;
2653 tp->snd_ssthresh = tcp_current_ssthresh(sk);
2654 tp->prior_ssthresh = 0;
2655 tp->undo_marker = 0;
2656 tcp_set_ca_state(sk, TCP_CA_Loss);
2657 }
2658 tcp_xmit_retransmit_queue(sk);
2659}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002660EXPORT_SYMBOL(tcp_simple_retransmit);
Ilpo Järvinene1aa6802008-11-24 21:11:55 -08002661
Yuchung Cheng57dde7f2017-01-12 22:11:33 -08002662void tcp_enter_recovery(struct sock *sk, bool ece_ack)
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002663{
2664 struct tcp_sock *tp = tcp_sk(sk);
2665 int mib_idx;
2666
2667 if (tcp_is_reno(tp))
2668 mib_idx = LINUX_MIB_TCPRENORECOVERY;
2669 else
2670 mib_idx = LINUX_MIB_TCPSACKRECOVERY;
2671
Eric Dumazetc10d9312016-04-29 14:16:47 -07002672 NET_INC_STATS(sock_net(sk), mib_idx);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002673
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002674 tp->prior_ssthresh = 0;
Yuchung Cheng989e04c2014-08-22 14:15:22 -07002675 tcp_init_undo(tp);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002676
Yuchung Cheng291a00d2015-07-01 14:11:14 -07002677 if (!tcp_in_cwnd_reduction(sk)) {
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002678 if (!ece_ack)
2679 tp->prior_ssthresh = tcp_current_ssthresh(sk);
Christoph Paasch5ee2c942014-07-14 16:58:32 +02002680 tcp_init_cwnd_reduction(sk);
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002681 }
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002682 tcp_set_ca_state(sk, TCP_CA_Recovery);
2683}
2684
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002685/* Process an ACK in CA_Loss state. Move to CA_Open if lost data are
2686 * recovered or spurious. Otherwise retransmits more on partial ACKs.
2687 */
Yuchung Chenge662ca42016-02-02 10:33:04 -08002688static void tcp_process_loss(struct sock *sk, int flag, bool is_dupack,
2689 int *rexmit)
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002690{
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002691 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Chenge33099f2013-03-20 13:33:00 +00002692 bool recovered = !before(tp->snd_una, tp->high_seq);
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002693
Yuchung Chengda34ac72015-05-18 12:31:44 -07002694 if ((flag & FLAG_SND_UNA_ADVANCED) &&
2695 tcp_try_undo_loss(sk, false))
2696 return;
2697
Yuchung Cheng89fe18e2017-01-12 22:11:37 -08002698 /* The ACK (s)acks some never-retransmitted data meaning not all
2699 * the data packets before the timeout were lost. Therefore we
2700 * undo the congestion window and state. This is essentially
2701 * the operation in F-RTO (RFC5682 section 3.1 step 3.b). Since
2702 * a retransmitted skb is permantly marked, we can apply such an
2703 * operation even if F-RTO was not used.
2704 */
2705 if ((flag & FLAG_ORIG_SACK_ACKED) &&
2706 tcp_try_undo_loss(sk, tp->undo_marker))
2707 return;
Yuchung Cheng0cfa5c02014-05-30 15:25:59 -07002708
Yuchung Cheng89fe18e2017-01-12 22:11:37 -08002709 if (tp->frto) { /* F-RTO RFC5682 sec 3.1 (sack enhanced version). */
Yuchung Chengb7b0ed92015-05-18 12:31:45 -07002710 if (after(tp->snd_nxt, tp->high_seq)) {
2711 if (flag & FLAG_DATA_SACKED || is_dupack)
2712 tp->frto = 0; /* Step 3.a. loss was real */
Yuchung Chenge33099f2013-03-20 13:33:00 +00002713 } else if (flag & FLAG_SND_UNA_ADVANCED && !recovered) {
2714 tp->high_seq = tp->snd_nxt;
Yuchung Chenge662ca42016-02-02 10:33:04 -08002715 /* Step 2.b. Try send new data (but deferred until cwnd
2716 * is updated in tcp_ack()). Otherwise fall back to
2717 * the conventional recovery.
2718 */
Eric Dumazet75c119a2017-10-05 22:21:27 -07002719 if (!tcp_write_queue_empty(sk) &&
Yuchung Chenge662ca42016-02-02 10:33:04 -08002720 after(tcp_wnd_end(tp), tp->snd_nxt)) {
2721 *rexmit = REXMIT_NEW;
2722 return;
2723 }
Yuchung Chenge33099f2013-03-20 13:33:00 +00002724 tp->frto = 0;
2725 }
2726 }
2727
2728 if (recovered) {
2729 /* F-RTO RFC5682 sec 3.1 step 2.a and 1st part of step 3.a */
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002730 tcp_try_undo_recovery(sk);
2731 return;
2732 }
Yuchung Chenge33099f2013-03-20 13:33:00 +00002733 if (tcp_is_reno(tp)) {
2734 /* A Reno DUPACK means new data in F-RTO step 2.b above are
2735 * delivered. Lower inflight to clock out (re)tranmissions.
2736 */
2737 if (after(tp->snd_nxt, tp->high_seq) && is_dupack)
2738 tcp_add_reno_sack(sk);
2739 else if (flag & FLAG_SND_UNA_ADVANCED)
2740 tcp_reset_reno_sack(tp);
2741 }
Yuchung Chenge662ca42016-02-02 10:33:04 -08002742 *rexmit = REXMIT_LOST;
Yuchung Chengab42d9e2013-03-20 13:32:59 +00002743}
2744
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002745/* Undo during fast recovery after partial ACK. */
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002746static bool tcp_try_undo_partial(struct sock *sk, const int acked)
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002747{
2748 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002749
Yuchung Cheng7026b912013-05-29 14:20:13 +00002750 if (tp->undo_marker && tcp_packet_delayed(tp)) {
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002751 /* Plain luck! Hole if filled with delayed
2752 * packet, rather than with a retransmit.
2753 */
Yuchung Cheng7026b912013-05-29 14:20:13 +00002754 tcp_update_reordering(sk, tcp_fackets_out(tp) + acked, 1);
2755
2756 /* We are getting evidence that the reordering degree is higher
2757 * than we realized. If there are no retransmits out then we
2758 * can undo. Otherwise we clock out new packets but do not
2759 * mark more packets lost or retransmit more.
2760 */
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002761 if (tp->retrans_out)
Yuchung Cheng7026b912013-05-29 14:20:13 +00002762 return true;
Yuchung Cheng7026b912013-05-29 14:20:13 +00002763
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002764 if (!tcp_any_retrans_done(sk))
2765 tp->retrans_stamp = 0;
2766
Yuchung Cheng7026b912013-05-29 14:20:13 +00002767 DBGUNDO(sk, "partial recovery");
2768 tcp_undo_cwnd_reduction(sk, true);
Eric Dumazetc10d9312016-04-29 14:16:47 -07002769 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO);
Yuchung Cheng7026b912013-05-29 14:20:13 +00002770 tcp_try_keep_open(sk);
2771 return true;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002772 }
Yuchung Cheng7026b912013-05-29 14:20:13 +00002773 return false;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002774}
2775
Eric Dumazetefab8f82017-04-25 10:15:35 -07002776static void tcp_rack_identify_loss(struct sock *sk, int *ack_flag)
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002777{
2778 struct tcp_sock *tp = tcp_sk(sk);
2779
2780 /* Use RACK to detect loss */
Eric Dumazete20223f2017-10-26 21:54:57 -07002781 if (sock_net(sk)->ipv4.sysctl_tcp_recovery & TCP_RACK_LOSS_DETECTION) {
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002782 u32 prior_retrans = tp->retrans_out;
2783
Eric Dumazet128eda82017-04-25 10:15:34 -07002784 tcp_rack_mark_lost(sk);
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002785 if (prior_retrans > tp->retrans_out)
2786 *ack_flag |= FLAG_LOST_RETRANS;
2787 }
2788}
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790/* Process an event, which can update packets-in-flight not trivially.
2791 * Main goal of this function is to calculate new estimate for left_out,
2792 * taking into account both packets sitting in receiver's buffer and
2793 * packets lost by network.
2794 *
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002795 * Besides that it updates the congestion state when packet loss or ECN
2796 * is detected. But it does not reduce the cwnd, it is done by the
2797 * congestion control later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 *
2799 * It does _not_ decide what to send, it is made in function
2800 * tcp_xmit_retransmit_queue().
2801 */
Yuchung Cheng68049732013-05-29 14:20:11 +00002802static void tcp_fastretrans_alert(struct sock *sk, const int acked,
Eric Dumazet1317a9d2017-04-25 10:15:36 -07002803 bool is_dupack, int *ack_flag, int *rexmit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002805 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002807 int fast_rexmit = 0, flag = *ack_flag;
Yuchung Cheng6a63df42013-05-29 14:20:12 +00002808 bool do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) &&
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002809 (tcp_fackets_out(tp) > tp->reordering));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Eric Dumazet8ba6dda2017-10-05 22:21:25 -07002811 if (!tp->packets_out && tp->sacked_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 tp->sacked_out = 0;
Eric Dumazet8ba6dda2017-10-05 22:21:25 -07002813 if (!tp->sacked_out && tp->fackets_out)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 tp->fackets_out = 0;
2815
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002816 /* Now state machine starts.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 * A. ECE, hence prohibit cwnd undoing, the reduction is required. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08002818 if (flag & FLAG_ECE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 tp->prior_ssthresh = 0;
2820
2821 /* B. In all the states check for reneging SACKs. */
Ilpo Järvinencadbd032007-12-31 04:49:21 -08002822 if (tcp_check_sack_reneging(sk, flag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return;
2824
Yuchung Cheng974c1232012-01-19 14:42:21 +00002825 /* C. Check consistency of the current state. */
Ilpo Järvinen005903b2007-08-09 14:44:16 +03002826 tcp_verify_left_out(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Yuchung Cheng974c1232012-01-19 14:42:21 +00002828 /* D. Check state exit conditions. State can be terminated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 * when high_seq is ACKed. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002830 if (icsk->icsk_ca_state == TCP_CA_Open) {
Ilpo Järvinen547b7922008-07-25 21:43:18 -07002831 WARN_ON(tp->retrans_out != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 tp->retrans_stamp = 0;
2833 } else if (!before(tp->snd_una, tp->high_seq)) {
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002834 switch (icsk->icsk_ca_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 case TCP_CA_CWR:
2836 /* CWR is to be held something *above* high_seq
2837 * is ACKed for CWR bit to reach receiver. */
2838 if (tp->snd_una != tp->high_seq) {
Yuchung Cheng684bad12012-09-02 17:38:04 +00002839 tcp_end_cwnd_reduction(sk);
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002840 tcp_set_ca_state(sk, TCP_CA_Open);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
2842 break;
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 case TCP_CA_Recovery:
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002845 if (tcp_is_reno(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 tcp_reset_reno_sack(tp);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002847 if (tcp_try_undo_recovery(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return;
Yuchung Cheng684bad12012-09-02 17:38:04 +00002849 tcp_end_cwnd_reduction(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 break;
2851 }
2852 }
2853
Yuchung Cheng974c1232012-01-19 14:42:21 +00002854 /* E. Process state. */
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002855 switch (icsk->icsk_ca_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 case TCP_CA_Recovery:
Ilpo Järvinen2e605292007-08-02 19:46:58 -07002857 if (!(flag & FLAG_SND_UNA_ADVANCED)) {
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002858 if (tcp_is_reno(tp) && is_dupack)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002859 tcp_add_reno_sack(sk);
Yuchung Cheng7026b912013-05-29 14:20:13 +00002860 } else {
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002861 if (tcp_try_undo_partial(sk, acked))
Yuchung Cheng7026b912013-05-29 14:20:13 +00002862 return;
2863 /* Partial ACK arrived. Force fast retransmit. */
2864 do_lost = tcp_is_reno(tp) ||
2865 tcp_fackets_out(tp) > tp->reordering;
2866 }
Yuchung Chengc7d9d6a2013-05-29 14:20:14 +00002867 if (tcp_try_undo_dsack(sk)) {
2868 tcp_try_keep_open(sk);
2869 return;
2870 }
Eric Dumazetefab8f82017-04-25 10:15:35 -07002871 tcp_rack_identify_loss(sk, ack_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 break;
2873 case TCP_CA_Loss:
Yuchung Chenge662ca42016-02-02 10:33:04 -08002874 tcp_process_loss(sk, flag, is_dupack, rexmit);
Eric Dumazetefab8f82017-04-25 10:15:35 -07002875 tcp_rack_identify_loss(sk, ack_flag);
Yuchung Cheng98e36d42017-01-12 22:11:35 -08002876 if (!(icsk->icsk_ca_state == TCP_CA_Open ||
2877 (*ack_flag & FLAG_LOST_RETRANS)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return;
Yuchung Cheng291a00d2015-07-01 14:11:14 -07002879 /* Change state if cwnd is undone or retransmits are lost */
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05002880 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 default:
Ilpo Järvinene60402d2007-08-09 15:14:46 +03002882 if (tcp_is_reno(tp)) {
Ilpo Järvinen2e605292007-08-02 19:46:58 -07002883 if (flag & FLAG_SND_UNA_ADVANCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 tcp_reset_reno_sack(tp);
2885 if (is_dupack)
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002886 tcp_add_reno_sack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
2888
Neal Cardwellf6982042011-11-16 08:58:04 +00002889 if (icsk->icsk_ca_state <= TCP_CA_Disorder)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07002890 tcp_try_undo_dsack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Eric Dumazetefab8f82017-04-25 10:15:35 -07002892 tcp_rack_identify_loss(sk, ack_flag);
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002893 if (!tcp_time_to_recover(sk, flag)) {
Yuchung Cheng31ba0c12016-02-02 10:33:05 -08002894 tcp_try_to_open(sk, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return;
2896 }
2897
John Heffner5d424d52006-03-20 17:53:41 -08002898 /* MTU probe failure: don't reduce cwnd */
2899 if (icsk->icsk_ca_state < TCP_CA_CWR &&
2900 icsk->icsk_mtup.probe_size &&
John Heffner0e7b1362006-03-20 21:32:58 -08002901 tp->snd_una == tp->mtu_probe.probe_seq_start) {
John Heffner5d424d52006-03-20 17:53:41 -08002902 tcp_mtup_probe_failed(sk);
2903 /* Restores the reduction we did in tcp_mtup_probe() */
2904 tp->snd_cwnd++;
2905 tcp_simple_retransmit(sk);
2906 return;
2907 }
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 /* Otherwise enter Recovery state */
Yuchung Cheng1fbc3402012-05-02 13:30:02 +00002910 tcp_enter_recovery(sk, (flag & FLAG_ECE));
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002911 fast_rexmit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
2913
Yuchung Cheng3e59cb02013-05-17 13:45:05 +00002914 if (do_lost)
Ilpo Järvinen85cc3912007-11-15 19:39:31 -08002915 tcp_update_scoreboard(sk, fast_rexmit);
Yuchung Chenge662ca42016-02-02 10:33:04 -08002916 *rexmit = REXMIT_LOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}
2918
Yuchung Chengf6722582015-10-16 21:57:42 -07002919static void tcp_update_rtt_min(struct sock *sk, u32 rtt_us)
2920{
Neal Cardwell64033892016-09-19 23:39:10 -04002921 struct tcp_sock *tp = tcp_sk(sk);
2922 u32 wlen = sysctl_tcp_min_rtt_wlen * HZ;
Yuchung Chengf6722582015-10-16 21:57:42 -07002923
Eric Dumazetac9517f2017-05-16 14:00:13 -07002924 minmax_running_min(&tp->rtt_min, wlen, tcp_jiffies32,
Neal Cardwell64033892016-09-19 23:39:10 -04002925 rtt_us ? : jiffies_to_usecs(1));
Yuchung Chengf6722582015-10-16 21:57:42 -07002926}
2927
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002928static bool tcp_ack_update_rtt(struct sock *sk, const int flag,
2929 long seq_rtt_us, long sack_rtt_us,
2930 long ca_rtt_us, struct rate_sample *rs)
Ilpo Järvinen41834b72008-12-05 22:43:26 -08002931{
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002932 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen41834b72008-12-05 22:43:26 -08002933
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002934 /* Prefer RTT measured from ACK's timing to TS-ECR. This is because
2935 * broken middle-boxes or peers may corrupt TS-ECR fields. But
2936 * Karn's algorithm forbids taking RTT if some retransmitted data
2937 * is acked (RFC6298).
2938 */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002939 if (seq_rtt_us < 0)
2940 seq_rtt_us = sack_rtt_us;
Yuchung Chenged084952013-07-22 16:20:48 -07002941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 /* RTTM Rule: A TSecr value received in a segment is used to
2943 * update the averaged RTT measurement only if the segment
2944 * acknowledges some new data, i.e., only if it advances the
2945 * left edge of the send window.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 * See draft-ietf-tcplw-high-performance-00, section 3.3.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002948 if (seq_rtt_us < 0 && tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
Eric Dumazet9a568de2017-05-16 14:00:14 -07002949 flag & FLAG_ACKED) {
2950 u32 delta = tcp_time_stamp(tp) - tp->rx_opt.rcv_tsecr;
2951 u32 delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ);
2952
2953 seq_rtt_us = ca_rtt_us = delta_us;
2954 }
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002955 rs->rtt_us = ca_rtt_us; /* RTT of last (S)ACKed packet (or -1) */
Eric Dumazet740b0f12014-02-26 14:02:48 -08002956 if (seq_rtt_us < 0)
Yuchung Chenged084952013-07-22 16:20:48 -07002957 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Yuchung Chengf6722582015-10-16 21:57:42 -07002959 /* ca_rtt_us >= 0 is counting on the invariant that ca_rtt_us is
2960 * always taken together with ACK, SACK, or TS-opts. Any negative
2961 * values will be skipped with the seq_rtt_us < 0 check above.
2962 */
2963 tcp_update_rtt_min(sk, ca_rtt_us);
Eric Dumazet740b0f12014-02-26 14:02:48 -08002964 tcp_rtt_estimator(sk, seq_rtt_us);
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002965 tcp_set_rto(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Yuchung Cheng5b08e472013-07-22 16:20:46 -07002967 /* RFC6298: only reset backoff on valid RTT measurement. */
2968 inet_csk(sk)->icsk_backoff = 0;
Yuchung Chenged084952013-07-22 16:20:48 -07002969 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
Yuchung Cheng375fe022013-07-22 16:20:45 -07002972/* Compute time elapsed between (last) SYNACK and the ACK completing 3WHS. */
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002973void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req)
Yuchung Cheng375fe022013-07-22 16:20:45 -07002974{
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002975 struct rate_sample rs;
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002976 long rtt_us = -1L;
Yuchung Cheng375fe022013-07-22 16:20:45 -07002977
Eric Dumazet9a568de2017-05-16 14:00:14 -07002978 if (req && !req->num_retrans && tcp_rsk(req)->snt_synack)
2979 rtt_us = tcp_stamp_us_delta(tcp_clock_us(), tcp_rsk(req)->snt_synack);
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002980
Yuchung Cheng775e68a2017-05-31 11:30:53 -07002981 tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, rtt_us, -1L, rtt_us, &rs);
Yuchung Cheng375fe022013-07-22 16:20:45 -07002982}
2983
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07002984
Eric Dumazet24901552014-05-02 21:18:05 -07002985static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03002987 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet24901552014-05-02 21:18:05 -07002988
2989 icsk->icsk_ca_ops->cong_avoid(sk, ack, acked);
Eric Dumazetc2203cf2017-05-16 14:00:04 -07002990 tcp_sk(sk)->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991}
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993/* Restart timer after forward progress on connection.
2994 * RFC2988 recommends to restart timer to now+rto.
2995 */
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002996void tcp_rearm_rto(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997{
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00002998 const struct inet_connection_sock *icsk = inet_csk(sk);
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00002999 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003000
Jerry Chu168a8f52012-08-31 12:29:13 +00003001 /* If the retrans timer is currently being used by Fast Open
3002 * for SYN-ACK retrans purpose, stay put.
3003 */
3004 if (tp->fastopen_rsk)
3005 return;
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 if (!tp->packets_out) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003008 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 } else {
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003010 u32 rto = inet_csk(sk)->icsk_rto;
3011 /* Offset the time elapsed after installing regular RTO */
Yuchung Chengbec41a12017-01-12 22:11:39 -08003012 if (icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003013 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Neal Cardwelle1a10ef2017-08-03 09:19:52 -04003014 s64 delta_us = tcp_rto_delta_us(sk);
Eric Dumazetb17b8a22017-05-18 09:15:58 -07003015 /* delta_us may not be positive if the socket is locked
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003016 * when the retrans timer fires and is rescheduled.
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003017 */
Neal Cardwellcdbeb632017-08-16 17:53:36 -04003018 rto = usecs_to_jiffies(max_t(int, delta_us, 1));
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003019 }
3020 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto,
3021 TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
Yuchung Cheng750ea2b2012-05-02 13:30:04 +00003023}
3024
Neal Cardwelldf92c832017-08-03 09:19:54 -04003025/* Try to schedule a loss probe; if that doesn't work, then schedule an RTO. */
3026static void tcp_set_xmit_timer(struct sock *sk)
3027{
3028 if (!tcp_schedule_loss_probe(sk))
3029 tcp_rearm_rto(sk);
3030}
3031
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003032/* If we get here, the whole TSO packet has not been acked. */
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003033static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
3035 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003036 u32 packets_acked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003038 BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
3040 packets_acked = tcp_skb_pcount(skb);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003041 if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return 0;
3043 packets_acked -= tcp_skb_pcount(skb);
3044
3045 if (packets_acked) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 BUG_ON(tcp_skb_pcount(skb) == 0);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003047 BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003050 return packets_acked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
Eric Dumazetad971f62014-10-11 15:17:29 -07003053static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb,
3054 u32 prior_snd_una)
3055{
3056 const struct skb_shared_info *shinfo;
3057
3058 /* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */
Soheil Hassas Yeganeh6b084922016-04-02 23:08:08 -04003059 if (likely(!TCP_SKB_CB(skb)->txstamp_ack))
Eric Dumazetad971f62014-10-11 15:17:29 -07003060 return;
3061
3062 shinfo = skb_shinfo(skb);
Soheil Hassas Yeganeh0a2cf202016-04-27 23:39:01 -04003063 if (!before(shinfo->tskey, prior_snd_una) &&
Eric Dumazete2080072017-10-04 12:59:58 -07003064 before(shinfo->tskey, tcp_sk(sk)->snd_una)) {
3065 tcp_skb_tsorted_save(skb) {
3066 __skb_tstamp_tx(skb, NULL, sk, SCM_TSTAMP_ACK);
3067 } tcp_skb_tsorted_restore(skb);
3068 }
Eric Dumazetad971f62014-10-11 15:17:29 -07003069}
3070
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003071/* Remove acknowledged frames from the retransmission queue. If our packet
3072 * is before the ack sequence we can discard it as it's confirmed to have
3073 * arrived at the other end.
3074 */
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07003075static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
Yuchung Cheng3ebd88712016-02-02 10:33:07 -08003076 u32 prior_snd_una, int *acked,
Yuchung Chengdeed7be2017-01-12 22:11:32 -08003077 struct tcp_sacktag_state *sack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
Stephen Hemminger2d2abba2005-11-10 16:56:12 -08003079 const struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet9a568de2017-05-16 14:00:14 -07003080 u64 first_ackt, last_ackt;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003081 struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003082 u32 prior_sacked = tp->sacked_out;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003083 u32 reord = tp->packets_out;
Eric Dumazet75c119a2017-10-05 22:21:27 -07003084 struct sk_buff *skb, *next;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003085 bool fully_acked = true;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003086 long sack_rtt_us = -1L;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003087 long seq_rtt_us = -1L;
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003088 long ca_rtt_us = -1L;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003089 u32 pkts_acked = 0;
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003090 u32 last_in_flight = 0;
Yuchung Cheng2f715c12013-10-24 08:59:27 -07003091 bool rtt_update;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003092 int flag = 0;
3093
Eric Dumazet9a568de2017-05-16 14:00:14 -07003094 first_ackt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Eric Dumazet75c119a2017-10-05 22:21:27 -07003096 for (skb = skb_rb_first(&sk->tcp_rtx_queue); skb; skb = next) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003097 struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003098 u8 sacked = scb->sacked;
Eric Dumazet740b0f12014-02-26 14:02:48 -08003099 u32 acked_pcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Eric Dumazetad971f62014-10-11 15:17:29 -07003101 tcp_ack_tstamp(sk, skb, prior_snd_una);
Willem de Bruijn712a7222014-08-12 14:53:16 -04003102
Gavin McCullagh2072c222007-12-29 19:11:21 -08003103 /* Determine how many packets and what bytes were acked, tso and else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (after(scb->end_seq, tp->snd_una)) {
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003105 if (tcp_skb_pcount(skb) == 1 ||
3106 !after(tp->snd_una, scb->seq))
3107 break;
3108
Ilpo Järvinen72018832007-12-30 04:37:55 -08003109 acked_pcount = tcp_tso_acked(sk, skb);
3110 if (!acked_pcount)
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003111 break;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003112 fully_acked = false;
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003113 } else {
Ilpo Järvinen72018832007-12-30 04:37:55 -08003114 acked_pcount = tcp_skb_pcount(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
3116
Eric Dumazetad971f62014-10-11 15:17:29 -07003117 if (unlikely(sacked & TCPCB_RETRANS)) {
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003118 if (sacked & TCPCB_SACKED_RETRANS)
Ilpo Järvinen72018832007-12-30 04:37:55 -08003119 tp->retrans_out -= acked_pcount;
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003120 flag |= FLAG_RETRANS_DATA_ACKED;
Kenneth Klette Jonassen3d0d26c2015-04-11 02:17:49 +02003121 } else if (!(sacked & TCPCB_SACKED_ACKED)) {
Eric Dumazet740b0f12014-02-26 14:02:48 -08003122 last_ackt = skb->skb_mstamp;
Eric Dumazet9a568de2017-05-16 14:00:14 -07003123 WARN_ON_ONCE(last_ackt == 0);
3124 if (!first_ackt)
Eric Dumazet740b0f12014-02-26 14:02:48 -08003125 first_ackt = last_ackt;
3126
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003127 last_in_flight = TCP_SKB_CB(skb)->tx.in_flight;
Kenneth Klette Jonassen3d0d26c2015-04-11 02:17:49 +02003128 reord = min(pkts_acked, reord);
3129 if (!after(scb->end_seq, tp->high_seq))
3130 flag |= FLAG_ORIG_SACK_ACKED;
Stephen Hemminger2d2abba2005-11-10 16:56:12 -08003131 }
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003132
Yuchung Chengddf1af62016-02-02 10:33:06 -08003133 if (sacked & TCPCB_SACKED_ACKED) {
Ilpo Järvinen72018832007-12-30 04:37:55 -08003134 tp->sacked_out -= acked_pcount;
Yuchung Chengddf1af62016-02-02 10:33:06 -08003135 } else if (tcp_is_sack(tp)) {
3136 tp->delivered += acked_pcount;
3137 if (!tcp_skb_spurious_retrans(tp, skb))
Yuchung Cheng1d0833d2017-01-12 22:11:34 -08003138 tcp_rack_advance(tp, sacked, scb->end_seq,
Eric Dumazet9a568de2017-05-16 14:00:14 -07003139 skb->skb_mstamp);
Yuchung Chengddf1af62016-02-02 10:33:06 -08003140 }
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003141 if (sacked & TCPCB_LOST)
Ilpo Järvinen72018832007-12-30 04:37:55 -08003142 tp->lost_out -= acked_pcount;
Ilpo Järvinen89d478f2007-12-30 04:35:27 -08003143
Ilpo Järvinen72018832007-12-30 04:37:55 -08003144 tp->packets_out -= acked_pcount;
3145 pkts_acked += acked_pcount;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003146 tcp_rate_skb_delivered(sk, skb, sack->rate);
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003147
Ilpo Järvinen009a2e32007-09-20 11:34:38 -07003148 /* Initial outgoing SYN's get put onto the write_queue
3149 * just like anything else we transmit. It is not
3150 * true data, and if we misinform our callers that
3151 * this ACK acks real data, we will erroneously exit
3152 * connection startup slow start one packet too
3153 * quickly. This is severely frowned upon behavior.
3154 */
Eric Dumazetad971f62014-10-11 15:17:29 -07003155 if (likely(!(scb->tcp_flags & TCPHDR_SYN))) {
Ilpo Järvinen009a2e32007-09-20 11:34:38 -07003156 flag |= FLAG_DATA_ACKED;
3157 } else {
3158 flag |= FLAG_SYN_ACKED;
3159 tp->retrans_stamp = 0;
3160 }
3161
Ilpo Järvinen13fcf852007-10-09 01:28:45 -07003162 if (!fully_acked)
3163 break;
3164
Eric Dumazet75c119a2017-10-05 22:21:27 -07003165 next = skb_rb_next(skb);
Eric Dumazetad971f62014-10-11 15:17:29 -07003166 if (unlikely(skb == tp->retransmit_skb_hint))
Ilpo Järvinenef9da472008-09-20 21:25:15 -07003167 tp->retransmit_skb_hint = NULL;
Eric Dumazetad971f62014-10-11 15:17:29 -07003168 if (unlikely(skb == tp->lost_skb_hint))
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003169 tp->lost_skb_hint = NULL;
Eric Dumazet75c119a2017-10-05 22:21:27 -07003170 tcp_rtx_queue_unlink_and_free(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
3172
Francis Yan0f872302016-11-27 23:07:14 -08003173 if (!skb)
3174 tcp_chrono_stop(sk, TCP_CHRONO_BUSY);
3175
Ilpo Järvinen33f5f572008-10-07 14:43:06 -07003176 if (likely(between(tp->snd_up, prior_snd_una, tp->snd_una)))
3177 tp->snd_up = tp->snd_una;
3178
Ilpo Järvinencadbd032007-12-31 04:49:21 -08003179 if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
3180 flag |= FLAG_SACK_RENEGING;
3181
Eric Dumazet9a568de2017-05-16 14:00:14 -07003182 if (likely(first_ackt) && !(flag & FLAG_RETRANS_DATA_ACKED)) {
3183 seq_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, first_ackt);
3184 ca_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, last_ackt);
Kenneth Klette Jonassen31231a82015-05-01 01:10:58 +02003185 }
Eric Dumazet9a568de2017-05-16 14:00:14 -07003186 if (sack->first_sackt) {
3187 sack_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, sack->first_sackt);
3188 ca_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, sack->last_sackt);
Eric Dumazet740b0f12014-02-26 14:02:48 -08003189 }
Yuchung Chengf6722582015-10-16 21:57:42 -07003190 rtt_update = tcp_ack_update_rtt(sk, flag, seq_rtt_us, sack_rtt_us,
Yuchung Cheng775e68a2017-05-31 11:30:53 -07003191 ca_rtt_us, sack->rate);
Yuchung Chenged084952013-07-22 16:20:48 -07003192
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003193 if (flag & FLAG_ACKED) {
Neal Cardwelldf92c832017-08-03 09:19:54 -04003194 flag |= FLAG_SET_XMIT_TIMER; /* set TLP or RTO timer */
Ilpo Järvinen72211e92009-03-14 14:23:04 +00003195 if (unlikely(icsk->icsk_mtup.probe_size &&
3196 !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {
3197 tcp_mtup_probe_success(sk);
3198 }
3199
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003200 if (tcp_is_reno(tp)) {
3201 tcp_remove_reno_sacks(sk, pkts_acked);
3202 } else {
Ilpo Järvinen59a08cb2009-02-28 04:44:28 +00003203 int delta;
3204
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003205 /* Non-retransmitted hole got filled? That's reordering */
Soheil Hassas Yeganehbafbb9c2017-05-15 17:05:47 -04003206 if (reord < prior_fackets && reord <= tp->fackets_out)
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003207 tcp_update_reordering(sk, tp->fackets_out - reord, 0);
Ilpo Järvinen90638a02008-09-20 21:25:52 -07003208
Ilpo Järvinen59a08cb2009-02-28 04:44:28 +00003209 delta = tcp_is_fack(tp) ? pkts_acked :
3210 prior_sacked - tp->sacked_out;
3211 tp->lost_cnt_hint -= min(tp->lost_cnt_hint, delta);
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003212 }
3213
Ilpo Järvinen91fed7a2007-10-09 01:24:15 -07003214 tp->fackets_out -= min(pkts_acked, tp->fackets_out);
Ilpo Järvinen68f83532007-11-15 19:50:37 -08003215
Eric Dumazet740b0f12014-02-26 14:02:48 -08003216 } else if (skb && rtt_update && sack_rtt_us >= 0 &&
Eric Dumazet9a568de2017-05-16 14:00:14 -07003217 sack_rtt_us > tcp_stamp_us_delta(tp->tcp_mstamp, skb->skb_mstamp)) {
Yuchung Cheng2f715c12013-10-24 08:59:27 -07003218 /* Do not re-arm RTO if the sack RTT is measured from data sent
3219 * after when the head was last (re)transmitted. Otherwise the
3220 * timeout may continue to extend in loss recovery.
3221 */
Neal Cardwelldf92c832017-08-03 09:19:54 -04003222 flag |= FLAG_SET_XMIT_TIMER; /* set TLP or RTO timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
3224
Lawrence Brakmo756ee172016-05-11 10:02:13 -07003225 if (icsk->icsk_ca_ops->pkts_acked) {
3226 struct ack_sample sample = { .pkts_acked = pkts_acked,
Yuchung Cheng775e68a2017-05-31 11:30:53 -07003227 .rtt_us = sack->rate->rtt_us,
Lawrence Brakmo6f094b92016-06-08 21:16:44 -07003228 .in_flight = last_in_flight };
Lawrence Brakmo756ee172016-05-11 10:02:13 -07003229
3230 icsk->icsk_ca_ops->pkts_acked(sk, &sample);
3231 }
Kenneth Klette Jonassen138998f2015-05-01 01:10:59 +02003232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233#if FASTRETRANS_DEBUG > 0
Ilpo Järvinen547b7922008-07-25 21:43:18 -07003234 WARN_ON((int)tp->sacked_out < 0);
3235 WARN_ON((int)tp->lost_out < 0);
3236 WARN_ON((int)tp->retrans_out < 0);
Ilpo Järvinene60402d2007-08-09 15:14:46 +03003237 if (!tp->packets_out && tcp_is_sack(tp)) {
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07003238 icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 if (tp->lost_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003240 pr_debug("Leak l=%u %d\n",
3241 tp->lost_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 tp->lost_out = 0;
3243 }
3244 if (tp->sacked_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003245 pr_debug("Leak s=%u %d\n",
3246 tp->sacked_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 tp->sacked_out = 0;
3248 }
3249 if (tp->retrans_out) {
Joe Perches91df42b2012-05-15 14:11:54 +00003250 pr_debug("Leak r=%u %d\n",
3251 tp->retrans_out, icsk->icsk_ca_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 tp->retrans_out = 0;
3253 }
3254 }
3255#endif
Yuchung Cheng3ebd88712016-02-02 10:33:07 -08003256 *acked = pkts_acked;
Ilpo Järvinen7c46a032007-09-20 11:33:43 -07003257 return flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
3260static void tcp_ack_probe(struct sock *sk)
3261{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003262 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -07003263 struct sk_buff *head = tcp_send_head(sk);
3264 const struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 /* Was it a usable window open? */
Eric Dumazet75c119a2017-10-05 22:21:27 -07003267 if (!head)
3268 return;
3269 if (!after(TCP_SKB_CB(head)->end_seq, tcp_wnd_end(tp))) {
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003270 icsk->icsk_backoff = 0;
3271 inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 /* Socket must be waked up by subsequent tcp_data_snd_check().
3273 * This function is not for random using!
3274 */
3275 } else {
Eric Dumazet21c8fe92015-05-06 14:26:24 -07003276 unsigned long when = tcp_probe0_when(sk, TCP_RTO_MAX);
Eric Dumazetfcdd1cf2014-09-22 13:19:44 -07003277
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003278 inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
Eric Dumazetfcdd1cf2014-09-22 13:19:44 -07003279 when, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
3281}
3282
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003283static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
Eric Dumazeta02cec22010-09-22 20:43:57 +00003285 return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||
3286 inet_csk(sk)->icsk_ca_state != TCP_CA_Open;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003289/* Decide wheather to run the increase function of congestion control. */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003290static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003292 /* If reordering is high then always grow cwnd whenever data is
3293 * delivered regardless of its ordering. Otherwise stay conservative
Yuchung Cheng16edfe72013-09-05 15:36:09 -07003294 * and only grow cwnd on in-order delivery (RFC5681). A stretched ACK w/
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003295 * new SACK or ECE mark may first advance cwnd here and later reduce
3296 * cwnd in tcp_fastretrans_alert() based on more states.
3297 */
Nikolay Borisov1043e252016-02-03 09:46:52 +02003298 if (tcp_sk(sk)->reordering > sock_net(sk)->ipv4.sysctl_tcp_reordering)
Yuchung Cheng0f7cc9a2013-08-21 17:29:23 -07003299 return flag & FLAG_FORWARD_PROGRESS;
3300
Yuchung Cheng16edfe72013-09-05 15:36:09 -07003301 return flag & FLAG_DATA_ACKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302}
3303
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003304/* The "ultimate" congestion control function that aims to replace the rigid
3305 * cwnd increase and decrease control (tcp_cong_avoid,tcp_*cwnd_reduction).
3306 * It's called toward the end of processing an ACK with precise rate
3307 * information. All transmission or retransmission are delayed afterwards.
3308 */
3309static void tcp_cong_control(struct sock *sk, u32 ack, u32 acked_sacked,
Yuchung Chengc0402762016-09-19 23:39:21 -04003310 int flag, const struct rate_sample *rs)
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003311{
Yuchung Chengc0402762016-09-19 23:39:21 -04003312 const struct inet_connection_sock *icsk = inet_csk(sk);
3313
3314 if (icsk->icsk_ca_ops->cong_control) {
3315 icsk->icsk_ca_ops->cong_control(sk, rs);
3316 return;
3317 }
3318
Yuchung Chengd452e6c2016-02-02 10:33:09 -08003319 if (tcp_in_cwnd_reduction(sk)) {
3320 /* Reduce cwnd if state mandates */
3321 tcp_cwnd_reduction(sk, acked_sacked, flag);
3322 } else if (tcp_may_raise_cwnd(sk, flag)) {
3323 /* Advance cwnd if state allows */
3324 tcp_cong_avoid(sk, ack, acked_sacked);
3325 }
3326 tcp_update_pacing_rate(sk);
3327}
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329/* Check that window update is acceptable.
3330 * The function assumes that snd_una<=ack<=snd_next.
3331 */
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003332static inline bool tcp_may_update_window(const struct tcp_sock *tp,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003333 const u32 ack, const u32 ack_seq,
3334 const u32 nwin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
Eric Dumazeta02cec22010-09-22 20:43:57 +00003336 return after(ack, tp->snd_una) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 after(ack_seq, tp->snd_wl1) ||
Eric Dumazeta02cec22010-09-22 20:43:57 +00003338 (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339}
3340
Eric Dumazet0df48c22015-04-28 15:28:17 -07003341/* If we update tp->snd_una, also update tp->bytes_acked */
3342static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
3343{
3344 u32 delta = ack - tp->snd_una;
3345
Eric Dumazet46cc6e42016-05-03 16:56:03 -07003346 sock_owned_by_me((struct sock *)tp);
Eric Dumazet0df48c22015-04-28 15:28:17 -07003347 tp->bytes_acked += delta;
3348 tp->snd_una = ack;
3349}
3350
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07003351/* If we update tp->rcv_nxt, also update tp->bytes_received */
3352static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
3353{
3354 u32 delta = seq - tp->rcv_nxt;
3355
Eric Dumazet46cc6e42016-05-03 16:56:03 -07003356 sock_owned_by_me((struct sock *)tp);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07003357 tp->bytes_received += delta;
3358 tp->rcv_nxt = seq;
3359}
3360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361/* Update our send window.
3362 *
3363 * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
3364 * and in FreeBSD. NetBSD's one is even worse.) is wrong.
3365 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003366static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003367 u32 ack_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07003369 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 int flag = 0;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07003371 u32 nwin = ntohs(tcp_hdr(skb)->window);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07003373 if (likely(!tcp_hdr(skb)->syn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 nwin <<= tp->rx_opt.snd_wscale;
3375
3376 if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {
3377 flag |= FLAG_WIN_UPDATE;
Hantzis Fotisee7537b2009-03-02 22:42:02 -08003378 tcp_update_wl(tp, ack_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
3380 if (tp->snd_wnd != nwin) {
3381 tp->snd_wnd = nwin;
3382
Florian Westphal31770e32017-08-30 19:24:58 +02003383 /* Note, it is the only place, where
3384 * fast path is recovered for sending TCP.
3385 */
3386 tp->pred_flags = 0;
3387 tcp_fast_path_check(sk);
3388
Eric Dumazet75c119a2017-10-05 22:21:27 -07003389 if (!tcp_write_queue_empty(sk))
Eric Dumazet6f021c62015-08-21 12:30:00 -07003390 tcp_slow_start_after_idle_check(sk);
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (nwin > tp->max_window) {
3393 tp->max_window = nwin;
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08003394 tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396 }
3397 }
3398
Eric Dumazet0df48c22015-04-28 15:28:17 -07003399 tcp_snd_una_update(tp, ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
3401 return flag;
3402}
3403
Jason Baron083ae302016-07-14 11:38:40 -04003404static bool __tcp_oow_rate_limited(struct net *net, int mib_idx,
3405 u32 *last_oow_ack_time)
3406{
3407 if (*last_oow_ack_time) {
Eric Dumazet594208a2017-05-16 14:00:10 -07003408 s32 elapsed = (s32)(tcp_jiffies32 - *last_oow_ack_time);
Jason Baron083ae302016-07-14 11:38:40 -04003409
3410 if (0 <= elapsed && elapsed < sysctl_tcp_invalid_ratelimit) {
3411 NET_INC_STATS(net, mib_idx);
3412 return true; /* rate-limited: don't send yet! */
3413 }
3414 }
3415
Eric Dumazet594208a2017-05-16 14:00:10 -07003416 *last_oow_ack_time = tcp_jiffies32;
Jason Baron083ae302016-07-14 11:38:40 -04003417
3418 return false; /* not rate-limited: go ahead, send dupack now! */
3419}
3420
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003421/* Return true if we're currently rate-limiting out-of-window ACKs and
3422 * thus shouldn't send a dupack right now. We rate-limit dupacks in
3423 * response to out-of-window SYNs or ACKs to mitigate ACK loops or DoS
3424 * attacks that send repeated SYNs or ACKs for the same connection. To
3425 * do this, we do not send a duplicate SYNACK or ACK if the remote
3426 * endpoint is sending out-of-window SYNs or pure ACKs at a high rate.
3427 */
3428bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
3429 int mib_idx, u32 *last_oow_ack_time)
3430{
3431 /* Data packets without SYNs are not likely part of an ACK loop. */
3432 if ((TCP_SKB_CB(skb)->seq != TCP_SKB_CB(skb)->end_seq) &&
3433 !tcp_hdr(skb)->syn)
Jason Baron083ae302016-07-14 11:38:40 -04003434 return false;
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003435
Jason Baron083ae302016-07-14 11:38:40 -04003436 return __tcp_oow_rate_limited(net, mib_idx, last_oow_ack_time);
Eric Dumazet7970ddc2015-03-16 21:06:20 -07003437}
3438
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003439/* RFC 5961 7 [ACK Throttling] */
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003440static void tcp_send_challenge_ack(struct sock *sk, const struct sk_buff *skb)
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003441{
3442 /* unprotected vars, we dont care of overwrites */
3443 static u32 challenge_timestamp;
3444 static unsigned int challenge_count;
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003445 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003446 u32 count, now;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003447
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003448 /* First check our per-socket dupack rate limit. */
Jason Baron083ae302016-07-14 11:38:40 -04003449 if (__tcp_oow_rate_limited(sock_net(sk),
3450 LINUX_MIB_TCPACKSKIPPEDCHALLENGE,
3451 &tp->last_oow_ack_time))
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003452 return;
3453
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003454 /* Then check host-wide RFC 5961 rate limit. */
Neal Cardwellf2b2c582015-02-06 16:04:40 -05003455 now = jiffies / HZ;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003456 if (now != challenge_timestamp) {
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003457 u32 half = (sysctl_tcp_challenge_ack_limit + 1) >> 1;
3458
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003459 challenge_timestamp = now;
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003460 WRITE_ONCE(challenge_count, half +
3461 prandom_u32_max(sysctl_tcp_challenge_ack_limit));
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003462 }
Eric Dumazet75ff39c2016-07-10 10:04:02 +02003463 count = READ_ONCE(challenge_count);
3464 if (count > 0) {
3465 WRITE_ONCE(challenge_count, count - 1);
Eric Dumazetc10d9312016-04-29 14:16:47 -07003466 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPCHALLENGEACK);
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003467 tcp_send_ack(sk);
3468 }
3469}
3470
Eric Dumazet12fb3dd2013-04-19 07:19:48 +00003471static void tcp_store_ts_recent(struct tcp_sock *tp)
3472{
3473 tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
3474 tp->rx_opt.ts_recent_stamp = get_seconds();
3475}
3476
3477static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
3478{
3479 if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) {
3480 /* PAWS bug workaround wrt. ACK frames, the PAWS discard
3481 * extra check below makes sure this can only happen
3482 * for pure ACK frames. -DaveM
3483 *
3484 * Not only, also it occurs for expired timestamps.
3485 */
3486
3487 if (tcp_paws_check(&tp->rx_opt, 0))
3488 tcp_store_ts_recent(tp);
3489 }
3490}
3491
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003492/* This routine deals with acks during a TLP episode.
Sébastien Barré08abdff2015-01-12 10:30:40 +01003493 * We mark the end of a TLP episode on receiving TLP dupack or when
3494 * ack is after tlp_high_seq.
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003495 * Ref: loss detection algorithm in draft-dukkipati-tcpm-tcp-loss-probe.
3496 */
3497static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
3498{
3499 struct tcp_sock *tp = tcp_sk(sk);
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003500
Sébastien Barré08abdff2015-01-12 10:30:40 +01003501 if (before(ack, tp->tlp_high_seq))
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003502 return;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003503
Sébastien Barré08abdff2015-01-12 10:30:40 +01003504 if (flag & FLAG_DSACKING_ACK) {
3505 /* This DSACK means original and TLP probe arrived; no loss */
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003506 tp->tlp_high_seq = 0;
Sébastien Barré08abdff2015-01-12 10:30:40 +01003507 } else if (after(ack, tp->tlp_high_seq)) {
3508 /* ACK advances: there was a loss, so reduce cwnd. Reset
3509 * tlp_high_seq in tcp_init_cwnd_reduction()
3510 */
3511 tcp_init_cwnd_reduction(sk);
3512 tcp_set_ca_state(sk, TCP_CA_CWR);
3513 tcp_end_cwnd_reduction(sk);
3514 tcp_try_keep_open(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07003515 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07003516 LINUX_MIB_TCPLOSSPROBERECOVERY);
Sébastien Barré08abdff2015-01-12 10:30:40 +01003517 } else if (!(flag & (FLAG_SND_UNA_ADVANCED |
3518 FLAG_NOT_DUP | FLAG_DATA_SACKED))) {
3519 /* Pure dupack: original and TLP probe arrived; no loss */
3520 tp->tlp_high_seq = 0;
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003521 }
3522}
3523
Florian Westphal7354c8c2014-09-26 22:37:34 +02003524static inline void tcp_in_ack_event(struct sock *sk, u32 flags)
3525{
3526 const struct inet_connection_sock *icsk = inet_csk(sk);
3527
3528 if (icsk->icsk_ca_ops->in_ack_event)
3529 icsk->icsk_ca_ops->in_ack_event(sk, flags);
3530}
3531
Yuchung Chenge662ca42016-02-02 10:33:04 -08003532/* Congestion control has updated the cwnd already. So if we're in
3533 * loss recovery then now we do any new sends (for FRTO) or
3534 * retransmits (for CA_Loss or CA_recovery) that make sense.
3535 */
3536static void tcp_xmit_recovery(struct sock *sk, int rexmit)
3537{
3538 struct tcp_sock *tp = tcp_sk(sk);
3539
3540 if (rexmit == REXMIT_NONE)
3541 return;
3542
3543 if (unlikely(rexmit == 2)) {
3544 __tcp_push_pending_frames(sk, tcp_current_mss(sk),
3545 TCP_NAGLE_OFF);
3546 if (after(tp->snd_nxt, tp->high_seq))
3547 return;
3548 tp->frto = 0;
3549 }
3550 tcp_xmit_retransmit_queue(sk);
3551}
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553/* This routine deals with incoming acks, but not outgoing ones. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003554static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003556 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 struct tcp_sock *tp = tcp_sk(sk);
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003558 struct tcp_sacktag_state sack_state;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003559 struct rate_sample rs = { .prior_delivered = 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 u32 prior_snd_una = tp->snd_una;
3561 u32 ack_seq = TCP_SKB_CB(skb)->seq;
3562 u32 ack = TCP_SKB_CB(skb)->ack_seq;
Neal Cardwell7d2b55f2011-11-16 08:58:01 +00003563 bool is_dupack = false;
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003564 u32 prior_fackets;
Nandita Dukkipati35f079e2013-05-21 15:12:07 +00003565 int prior_packets = tp->packets_out;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003566 u32 delivered = tp->delivered;
3567 u32 lost = tp->lost;
Yuchung Cheng68049732013-05-29 14:20:11 +00003568 int acked = 0; /* Number of packets newly acked */
Yuchung Chenge662ca42016-02-02 10:33:04 -08003569 int rexmit = REXMIT_NONE; /* Flag to (re)transmit to recover losses */
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003570
Eric Dumazet9a568de2017-05-16 14:00:14 -07003571 sack_state.first_sackt = 0;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003572 sack_state.rate = &rs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Eric Dumazet75c119a2017-10-05 22:21:27 -07003574 /* We very likely will need to access rtx queue. */
3575 prefetch(sk->tcp_rtx_queue.rb_node);
Eric Dumazetad971f62014-10-11 15:17:29 -07003576
John Dykstra96e0bf42009-03-22 21:49:57 -07003577 /* If the ack is older than previous acks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 * then we can probably ignore it.
3579 */
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003580 if (before(ack, prior_snd_una)) {
3581 /* RFC 5961 5.2 [Blind Data Injection Attack].[Mitigation] */
3582 if (before(ack, prior_snd_una - tp->max_window)) {
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -07003583 if (!(flag & FLAG_NO_CHALLENGE_ACK))
3584 tcp_send_challenge_ack(sk, skb);
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003585 return -1;
3586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 goto old_ack;
Eric Dumazet354e4aa2012-10-21 19:57:11 +00003588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
John Dykstra96e0bf42009-03-22 21:49:57 -07003590 /* If the ack includes data we haven't sent yet, discard
3591 * this segment (RFC793 Section 3.9).
3592 */
3593 if (after(ack, tp->snd_nxt))
3594 goto invalid_ack;
3595
Neal Cardwell0c9ab092014-08-14 16:13:07 -04003596 if (after(ack, prior_snd_una)) {
Ilpo Järvinen2e605292007-08-02 19:46:58 -07003597 flag |= FLAG_SND_UNA_ADVANCED;
Neal Cardwell0c9ab092014-08-14 16:13:07 -04003598 icsk->icsk_retransmits = 0;
3599 }
Ilpo Järvinen2e605292007-08-02 19:46:58 -07003600
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003601 prior_fackets = tp->fackets_out;
Yuchung Chengb9f64822016-09-19 23:39:14 -04003602 rs.prior_in_flight = tcp_packets_in_flight(tp);
Ilpo Järvinenc7caf8d2007-11-10 21:22:18 -08003603
Eric Dumazet12fb3dd2013-04-19 07:19:48 +00003604 /* ts_recent update must be made after we are sure that the packet
3605 * is in window.
3606 */
3607 if (flag & FLAG_UPDATE_TS_RECENT)
3608 tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
3609
Florian Westphal31770e32017-08-30 19:24:58 +02003610 if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
3611 /* Window is constant, pure forward advance.
3612 * No more checks are required.
3613 * Note, we use the fact that SND.UNA>=SND.WL2.
3614 */
3615 tcp_update_wl(tp, ack_seq);
3616 tcp_snd_una_update(tp, ack);
3617 flag |= FLAG_WIN_UPDATE;
3618
3619 tcp_in_ack_event(sk, CA_ACK_WIN_UPDATE);
3620
3621 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPACKS);
3622 } else {
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003623 u32 ack_ev_flags = CA_ACK_SLOWPATH;
Florian Westphal98900922014-09-26 22:37:35 +02003624
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003625 if (ack_seq != TCP_SKB_CB(skb)->end_seq)
3626 flag |= FLAG_DATA;
3627 else
3628 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPUREACKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003630 flag |= tcp_ack_update_window(sk, skb, ack, ack_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Florian Westphalc1d2b4c2017-08-30 19:24:57 +02003632 if (TCP_SKB_CB(skb)->sacked)
3633 flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
3634 &sack_state);
3635
3636 if (tcp_ecn_rcv_ecn_echo(tp, tcp_hdr(skb))) {
3637 flag |= FLAG_ECE;
3638 ack_ev_flags |= CA_ACK_ECE;
3639 }
3640
3641 if (flag & FLAG_WIN_UPDATE)
3642 ack_ev_flags |= CA_ACK_WIN_UPDATE;
3643
3644 tcp_in_ack_event(sk, ack_ev_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 }
3646
3647 /* We passed data and got it acked, remove any soft error
3648 * log. Something worked...
3649 */
3650 sk->sk_err_soft = 0;
David S. Miller4b53fb62008-07-23 16:38:45 -07003651 icsk->icsk_probes_out = 0;
Eric Dumazet70eabf02017-05-16 14:00:07 -07003652 tp->rcv_tstamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 if (!prior_packets)
3654 goto no_queue;
3655
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 /* See if we can take anything off of the retransmit queue. */
Yuchung Cheng3ebd88712016-02-02 10:33:07 -08003657 flag |= tcp_clean_rtx_queue(sk, prior_fackets, prior_snd_una, &acked,
Yuchung Chengdeed7be2017-01-12 22:11:32 -08003658 &sack_state);
Nandita Dukkipatia262f0c2011-08-21 20:21:57 +00003659
Neal Cardwelldf92c832017-08-03 09:19:54 -04003660 if (tp->tlp_high_seq)
3661 tcp_process_tlp_ack(sk, ack, flag);
3662 /* If needed, reset TLP/RTO timer; RACK may later override this. */
3663 if (flag & FLAG_SET_XMIT_TIMER)
3664 tcp_set_xmit_timer(sk);
3665
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03003666 if (tcp_ack_is_dubious(sk, flag)) {
Neal Cardwell7d2b55f2011-11-16 08:58:01 +00003667 is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP));
Eric Dumazet1317a9d2017-04-25 10:15:36 -07003668 tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 }
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003670
Julian Anastasovc3a2e832017-02-06 23:14:14 +02003671 if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP))
3672 sk_dst_confirm(sk);
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +00003673
Yuchung Chengb9f64822016-09-19 23:39:14 -04003674 delivered = tp->delivered - delivered; /* freshly ACKed or SACKed */
3675 lost = tp->lost - lost; /* freshly marked lost */
Eric Dumazet88d5c652017-04-25 10:15:37 -07003676 tcp_rate_gen(sk, delivered, lost, sack_state.rate);
Yuchung Chengdeed7be2017-01-12 22:11:32 -08003677 tcp_cong_control(sk, ack, delivered, flag, sack_state.rate);
Yuchung Chenge662ca42016-02-02 10:33:04 -08003678 tcp_xmit_recovery(sk, rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return 1;
3680
3681no_queue:
Neal Cardwell5628adf2011-11-16 08:58:02 +00003682 /* If data was DSACKed, see if we can undo a cwnd reduction. */
3683 if (flag & FLAG_DSACKING_ACK)
Eric Dumazet1317a9d2017-04-25 10:15:36 -07003684 tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 /* If this ack opens up a zero window, clear backoff. It was
3686 * being used to time the probes, and is probably far higher than
3687 * it needs to be for normal retransmission.
3688 */
Eric Dumazet75c119a2017-10-05 22:21:27 -07003689 tcp_ack_probe(sk);
Nandita Dukkipati9b717a82013-03-11 10:00:44 +00003690
3691 if (tp->tlp_high_seq)
3692 tcp_process_tlp_ack(sk, ack, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return 1;
3694
John Dykstra96e0bf42009-03-22 21:49:57 -07003695invalid_ack:
3696 SOCK_DEBUG(sk, "Ack %u after %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
3697 return -1;
3698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699old_ack:
Neal Cardwelle95ae2f2011-11-16 08:58:03 +00003700 /* If data was SACKed, tag it and see if we should send more data.
3701 * If data was DSACKed, see if we can undo a cwnd reduction.
3702 */
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07003703 if (TCP_SKB_CB(skb)->sacked) {
Yuchung Cheng59c9af42013-07-22 16:20:47 -07003704 flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
Kenneth Klette Jonassen196da972015-05-01 01:10:57 +02003705 &sack_state);
Eric Dumazet1317a9d2017-04-25 10:15:36 -07003706 tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
Yuchung Chenge662ca42016-02-02 10:33:04 -08003707 tcp_xmit_recovery(sk, rexmit);
Ilpo Järvinen8aca6cb2008-06-04 11:34:22 -07003708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
John Dykstra96e0bf42009-03-22 21:49:57 -07003710 SOCK_DEBUG(sk, "Ack %u before %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 return 0;
3712}
3713
Daniel Lee7f9b8382015-04-06 14:37:26 -07003714static void tcp_parse_fastopen_option(int len, const unsigned char *cookie,
3715 bool syn, struct tcp_fastopen_cookie *foc,
3716 bool exp_opt)
3717{
3718 /* Valid only in SYN or SYN-ACK with an even length. */
3719 if (!foc || !syn || len < 0 || (len & 1))
3720 return;
3721
3722 if (len >= TCP_FASTOPEN_COOKIE_MIN &&
3723 len <= TCP_FASTOPEN_COOKIE_MAX)
3724 memcpy(foc->val, cookie, len);
3725 else if (len != 0)
3726 len = -1;
3727 foc->len = len;
3728 foc->exp = exp_opt;
3729}
3730
Ursula Braun60e2a772017-10-25 11:01:45 +02003731static void smc_parse_options(const struct tcphdr *th,
3732 struct tcp_options_received *opt_rx,
3733 const unsigned char *ptr,
3734 int opsize)
3735{
3736#if IS_ENABLED(CONFIG_SMC)
3737 if (static_branch_unlikely(&tcp_have_smc)) {
3738 if (th->syn && !(opsize & 1) &&
3739 opsize >= TCPOLEN_EXP_SMC_BASE &&
3740 get_unaligned_be32(ptr) == TCPOPT_SMC_MAGIC)
3741 opt_rx->smc_ok = 1;
3742 }
3743#endif
3744}
3745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746/* Look for tcp options. Normally only called on SYN and SYNACK packets.
3747 * But, this can also be called on packets in the established flow when
3748 * the fast version below fails.
3749 */
Eric Dumazeteed29f12017-06-07 10:34:36 -07003750void tcp_parse_options(const struct net *net,
3751 const struct sk_buff *skb,
Christoph Paasch1a2c6182013-03-17 08:23:34 +00003752 struct tcp_options_received *opt_rx, int estab,
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003753 struct tcp_fastopen_cookie *foc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003755 const unsigned char *ptr;
3756 const struct tcphdr *th = tcp_hdr(skb);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003757 int length = (th->doff * 4) - sizeof(struct tcphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003759 ptr = (const unsigned char *)(th + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 opt_rx->saw_tstamp = 0;
3761
Stephen Hemminger2de979b2007-03-08 20:45:19 -08003762 while (length > 0) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003763 int opcode = *ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 int opsize;
3765
3766 switch (opcode) {
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003767 case TCPOPT_EOL:
3768 return;
3769 case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
3770 length--;
3771 continue;
3772 default:
3773 opsize = *ptr++;
3774 if (opsize < 2) /* "silly options" */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return;
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003776 if (opsize > length)
3777 return; /* don't parse partial options */
3778 switch (opcode) {
3779 case TCPOPT_MSS:
3780 if (opsize == TCPOLEN_MSS && th->syn && !estab) {
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07003781 u16 in_mss = get_unaligned_be16(ptr);
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003782 if (in_mss) {
3783 if (opt_rx->user_mss &&
3784 opt_rx->user_mss < in_mss)
3785 in_mss = opt_rx->user_mss;
3786 opt_rx->mss_clamp = in_mss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 }
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003788 }
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003789 break;
3790 case TCPOPT_WINDOW:
3791 if (opsize == TCPOLEN_WINDOW && th->syn &&
Eric Dumazet9bb37ef2017-06-07 10:34:38 -07003792 !estab && net->ipv4.sysctl_tcp_window_scaling) {
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003793 __u8 snd_wscale = *(__u8 *)ptr;
3794 opt_rx->wscale_ok = 1;
Gao Feng589c49c2017-04-04 21:09:48 +08003795 if (snd_wscale > TCP_MAX_WSCALE) {
3796 net_info_ratelimited("%s: Illegal window scaling value %d > %u received\n",
Joe Perchese87cc472012-05-13 21:56:26 +00003797 __func__,
Gao Feng589c49c2017-04-04 21:09:48 +08003798 snd_wscale,
3799 TCP_MAX_WSCALE);
3800 snd_wscale = TCP_MAX_WSCALE;
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003801 }
3802 opt_rx->snd_wscale = snd_wscale;
3803 }
3804 break;
3805 case TCPOPT_TIMESTAMP:
3806 if ((opsize == TCPOLEN_TIMESTAMP) &&
3807 ((estab && opt_rx->tstamp_ok) ||
Eric Dumazet5d2ed052017-06-07 10:34:39 -07003808 (!estab && net->ipv4.sysctl_tcp_timestamps))) {
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003809 opt_rx->saw_tstamp = 1;
Harvey Harrisond3e2ce32008-05-02 16:26:16 -07003810 opt_rx->rcv_tsval = get_unaligned_be32(ptr);
3811 opt_rx->rcv_tsecr = get_unaligned_be32(ptr + 4);
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003812 }
3813 break;
3814 case TCPOPT_SACK_PERM:
3815 if (opsize == TCPOLEN_SACK_PERM && th->syn &&
Eric Dumazetf9301032017-06-07 10:34:37 -07003816 !estab && net->ipv4.sysctl_tcp_sack) {
Vijay Subramanianab562222011-12-20 13:23:24 +00003817 opt_rx->sack_ok = TCP_SACK_SEEN;
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003818 tcp_sack_reset(opt_rx);
3819 }
3820 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003821
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003822 case TCPOPT_SACK:
3823 if ((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) &&
3824 !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) &&
3825 opt_rx->sack_ok) {
3826 TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th;
3827 }
3828 break;
3829#ifdef CONFIG_TCP_MD5SIG
3830 case TCPOPT_MD5SIG:
3831 /*
3832 * The MD5 Hash has already been
3833 * checked (see tcp_v{4,6}_do_rcv()).
3834 */
3835 break;
3836#endif
Daniel Lee7f9b8382015-04-06 14:37:26 -07003837 case TCPOPT_FASTOPEN:
3838 tcp_parse_fastopen_option(
3839 opsize - TCPOLEN_FASTOPEN_BASE,
3840 ptr, th->syn, foc, false);
3841 break;
3842
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003843 case TCPOPT_EXP:
3844 /* Fast Open option shares code 254 using a
Daniel Lee7f9b8382015-04-06 14:37:26 -07003845 * 16 bits magic number.
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003846 */
Daniel Lee7f9b8382015-04-06 14:37:26 -07003847 if (opsize >= TCPOLEN_EXP_FASTOPEN_BASE &&
3848 get_unaligned_be16(ptr) ==
3849 TCPOPT_FASTOPEN_MAGIC)
3850 tcp_parse_fastopen_option(opsize -
3851 TCPOLEN_EXP_FASTOPEN_BASE,
3852 ptr + 2, th->syn, foc, true);
Ursula Braun60e2a772017-10-25 11:01:45 +02003853 else
3854 smc_parse_options(th, opt_rx, ptr,
3855 opsize);
Yuchung Cheng2100c8d2012-07-19 06:43:05 +00003856 break;
3857
3858 }
Ilpo Järvinenf038ac82008-01-03 20:36:55 -08003859 ptr += opsize-2;
3860 length -= opsize;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 }
3863}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00003864EXPORT_SYMBOL(tcp_parse_options);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003866static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr *th)
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003867{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003868 const __be32 *ptr = (const __be32 *)(th + 1);
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003869
3870 if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
3871 | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
3872 tp->rx_opt.saw_tstamp = 1;
3873 ++ptr;
3874 tp->rx_opt.rcv_tsval = ntohl(*ptr);
3875 ++ptr;
Andrew Vagine3e12022013-08-27 12:21:55 +04003876 if (*ptr)
3877 tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset;
3878 else
3879 tp->rx_opt.rcv_tsecr = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003880 return true;
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003881 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003882 return false;
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003883}
3884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885/* Fast parse options. This hopes to only see timestamps.
3886 * If it is wrong it falls back on tcp_parse_options().
3887 */
Eric Dumazeteed29f12017-06-07 10:34:36 -07003888static bool tcp_fast_parse_options(const struct net *net,
3889 const struct sk_buff *skb,
Christoph Paasch1a2c6182013-03-17 08:23:34 +00003890 const struct tcphdr *th, struct tcp_sock *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
William Allen Simpson4957faade2009-12-02 18:25:27 +00003892 /* In the spirit of fast parsing, compare doff directly to constant
3893 * values. Because equality is used, short doff can be ignored here.
3894 */
3895 if (th->doff == (sizeof(*th) / 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 tp->rx_opt.saw_tstamp = 0;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003897 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 } else if (tp->rx_opt.tstamp_ok &&
William Allen Simpson4957faade2009-12-02 18:25:27 +00003899 th->doff == ((sizeof(*th) + TCPOLEN_TSTAMP_ALIGNED) / 4)) {
Ilpo Järvinena4356b22008-08-23 05:12:29 -07003900 if (tcp_parse_aligned_timestamp(tp, th))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003901 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
Andrey Vaginee684b62013-02-11 05:50:19 +00003903
Eric Dumazeteed29f12017-06-07 10:34:36 -07003904 tcp_parse_options(net, skb, &tp->rx_opt, 1, NULL);
Andrew Vagine3e12022013-08-27 12:21:55 +04003905 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
Andrey Vaginee684b62013-02-11 05:50:19 +00003906 tp->rx_opt.rcv_tsecr -= tp->tsoffset;
3907
Eric Dumazeta2a385d2012-05-16 23:15:34 +00003908 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909}
3910
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003911#ifdef CONFIG_TCP_MD5SIG
3912/*
3913 * Parse MD5 Signature option
3914 */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003915const u8 *tcp_parse_md5sig_option(const struct tcphdr *th)
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003916{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003917 int length = (th->doff << 2) - sizeof(*th);
3918 const u8 *ptr = (const u8 *)(th + 1);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003919
3920 /* If the TCP option is too short, we can short cut */
3921 if (length < TCPOLEN_MD5SIG)
3922 return NULL;
3923
3924 while (length > 0) {
3925 int opcode = *ptr++;
3926 int opsize;
3927
Weilong Chena22318e2013-12-23 14:37:26 +08003928 switch (opcode) {
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003929 case TCPOPT_EOL:
3930 return NULL;
3931 case TCPOPT_NOP:
3932 length--;
3933 continue;
3934 default:
3935 opsize = *ptr++;
3936 if (opsize < 2 || opsize > length)
3937 return NULL;
3938 if (opcode == TCPOPT_MD5SIG)
Dmitry Popovba78e2d2010-08-07 20:24:28 -07003939 return opsize == TCPOLEN_MD5SIG ? ptr : NULL;
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003940 }
3941 ptr += opsize - 2;
3942 length -= opsize;
3943 }
3944 return NULL;
3945}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00003946EXPORT_SYMBOL(tcp_parse_md5sig_option);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +09003947#endif
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949/* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM
3950 *
3951 * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
3952 * it can pass through stack. So, the following predicate verifies that
3953 * this segment is not used for anything but congestion avoidance or
3954 * fast retransmit. Moreover, we even are able to eliminate most of such
3955 * second order effects, if we apply some small "replay" window (~RTO)
3956 * to timestamp space.
3957 *
3958 * All these measures still do not guarantee that we reject wrapped ACKs
3959 * on networks with high bandwidth, when sequence space is recycled fastly,
3960 * but it guarantees that such events will be very rare and do not affect
3961 * connection seriously. This doesn't look nice, but alas, PAWS is really
3962 * buggy extension.
3963 *
3964 * [ Later note. Even worse! It is buggy for segments _with_ data. RFC
3965 * states that events when retransmit arrives after original data are rare.
3966 * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is
3967 * the biggest problem on large power networks even with minor reordering.
3968 * OK, let's give it small replay window. If peer clock is even 1hz, it is safe
3969 * up to bandwidth of 18Gigabit/sec. 8) ]
3970 */
3971
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003972static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04003974 const struct tcp_sock *tp = tcp_sk(sk);
3975 const struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 u32 seq = TCP_SKB_CB(skb)->seq;
3977 u32 ack = TCP_SKB_CB(skb)->ack_seq;
3978
3979 return (/* 1. Pure ACK with correct sequence number. */
3980 (th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) &&
3981
3982 /* 2. ... and duplicate ACK. */
3983 ack == tp->snd_una &&
3984
3985 /* 3. ... and does not update window. */
3986 !tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) &&
3987
3988 /* 4. ... and sits in replay window. */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003989 (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 -07003990}
3991
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00003992static inline bool tcp_paws_discard(const struct sock *sk,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08003993 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07003995 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinenc887e6d2009-03-14 14:23:03 +00003996
3997 return !tcp_paws_check(&tp->rx_opt, TCP_PAWS_WINDOW) &&
3998 !tcp_disordered_ack(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
4001/* Check segment sequence number for validity.
4002 *
4003 * Segment controls are considered valid, if the segment
4004 * fits to the window after truncation to the window. Acceptability
4005 * of data (and SYN, FIN, of course) is checked separately.
4006 * See tcp_data_queue(), for example.
4007 *
4008 * Also, controls (RST is main one) are accepted using RCV.WUP instead
4009 * of RCV.NXT. Peer still did not advance his SND.UNA when we
4010 * delayed ACK, so that hisSND.UNA<=ourRCV.WUP.
4011 * (borrowed from freebsd)
4012 */
4013
Vijay Subramanian67b95bd2012-07-19 21:32:18 +00004014static inline bool tcp_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015{
4016 return !before(end_seq, tp->rcv_wup) &&
4017 !after(seq, tp->rcv_nxt + tcp_receive_window(tp));
4018}
4019
4020/* When we get a reset we do this. */
Jerry Chu10467162012-08-31 12:29:11 +00004021void tcp_reset(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022{
Song Liu59415212017-10-23 09:20:25 -07004023 trace_tcp_receive_reset(sk);
4024
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 /* We want the right error as BSD sees it (and indeed as we do). */
4026 switch (sk->sk_state) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004027 case TCP_SYN_SENT:
4028 sk->sk_err = ECONNREFUSED;
4029 break;
4030 case TCP_CLOSE_WAIT:
4031 sk->sk_err = EPIPE;
4032 break;
4033 case TCP_CLOSE:
4034 return;
4035 default:
4036 sk->sk_err = ECONNRESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 }
Tom Marshalla4d25802010-09-20 15:42:05 -07004038 /* This barrier is coupled with smp_rmb() in tcp_poll() */
4039 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Eric Dumazet3d476262017-04-18 09:45:51 -07004041 tcp_done(sk);
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 if (!sock_flag(sk, SOCK_DEAD))
4044 sk->sk_error_report(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045}
4046
4047/*
4048 * Process the FIN bit. This now behaves as it is supposed to work
4049 * and the FIN takes effect when it is validly part of sequence
4050 * space. Not before when we get holes.
4051 *
4052 * If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
4053 * (and thence onto LAST-ACK and finally, CLOSE, we never enter
4054 * TIME-WAIT)
4055 *
4056 * If we are in FINWAIT-1, a received FIN indicates simultaneous
4057 * close and we go into CLOSING (and later onto TIME-WAIT)
4058 *
4059 * If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
4060 */
Eric Dumazete3e17b72016-02-06 11:16:28 -08004061void tcp_fin(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
4063 struct tcp_sock *tp = tcp_sk(sk);
4064
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004065 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
4067 sk->sk_shutdown |= RCV_SHUTDOWN;
4068 sock_set_flag(sk, SOCK_DONE);
4069
4070 switch (sk->sk_state) {
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004071 case TCP_SYN_RECV:
4072 case TCP_ESTABLISHED:
4073 /* Move to CLOSE_WAIT */
4074 tcp_set_state(sk, TCP_CLOSE_WAIT);
Jon Maxwell2251ae42015-07-08 10:12:28 +10004075 inet_csk(sk)->icsk_ack.pingpong = 1;
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004076 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004078 case TCP_CLOSE_WAIT:
4079 case TCP_CLOSING:
4080 /* Received a retransmission of the FIN, do
4081 * nothing.
4082 */
4083 break;
4084 case TCP_LAST_ACK:
4085 /* RFC793: Remain in the LAST-ACK state. */
4086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004088 case TCP_FIN_WAIT1:
4089 /* This case occurs when a simultaneous close
4090 * happens, we must ack the received FIN and
4091 * enter the CLOSING state.
4092 */
4093 tcp_send_ack(sk);
4094 tcp_set_state(sk, TCP_CLOSING);
4095 break;
4096 case TCP_FIN_WAIT2:
4097 /* Received a FIN -- send ACK and enter TIME_WAIT. */
4098 tcp_send_ack(sk);
4099 tcp_time_wait(sk, TCP_TIME_WAIT, 0);
4100 break;
4101 default:
4102 /* Only TCP_LISTEN and TCP_CLOSE are left, in these
4103 * cases we should never reach this piece of code.
4104 */
Joe Perches058bd4d2012-03-11 18:36:11 +00004105 pr_err("%s: Impossible, sk->sk_state=%d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08004106 __func__, sk->sk_state);
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004107 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07004108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
4110 /* It _is_ possible, that we have something out-of-order _after_ FIN.
4111 * Probably, we should reset in this case. For now drop them.
4112 */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004113 skb_rbtree_purge(&tp->out_of_order_queue);
Ilpo Järvinene60402d2007-08-09 15:14:46 +03004114 if (tcp_is_sack(tp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 tcp_sack_reset(&tp->rx_opt);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004116 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
4118 if (!sock_flag(sk, SOCK_DEAD)) {
4119 sk->sk_state_change(sk);
4120
4121 /* Do not send POLL_HUP for half duplex close. */
4122 if (sk->sk_shutdown == SHUTDOWN_MASK ||
4123 sk->sk_state == TCP_CLOSE)
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08004124 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 else
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +08004126 sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 }
4128}
4129
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004130static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004131 u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
4133 if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) {
4134 if (before(seq, sp->start_seq))
4135 sp->start_seq = seq;
4136 if (after(end_seq, sp->end_seq))
4137 sp->end_seq = end_seq;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004138 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 }
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004140 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004143static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004145 struct tcp_sock *tp = tcp_sk(sk);
4146
Eric Dumazet6496f6b2017-10-26 21:55:07 -07004147 if (tcp_is_sack(tp) && sock_net(sk)->ipv4.sysctl_tcp_dsack) {
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004148 int mib_idx;
4149
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 if (before(seq, tp->rcv_nxt))
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004151 mib_idx = LINUX_MIB_TCPDSACKOLDSENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 else
Pavel Emelyanov40b215e2008-07-03 01:05:41 -07004153 mib_idx = LINUX_MIB_TCPDSACKOFOSENT;
4154
Eric Dumazetc10d9312016-04-29 14:16:47 -07004155 NET_INC_STATS(sock_net(sk), mib_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
4157 tp->rx_opt.dsack = 1;
4158 tp->duplicate_sack[0].start_seq = seq;
4159 tp->duplicate_sack[0].end_seq = end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 }
4161}
4162
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004163static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004165 struct tcp_sock *tp = tcp_sk(sk);
4166
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 if (!tp->rx_opt.dsack)
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004168 tcp_dsack_set(sk, seq, end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 else
4170 tcp_sack_extend(tp->duplicate_sack, seq, end_seq);
4171}
4172
Eric Dumazetcf533ea2011-10-21 05:22:42 -04004173static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
4175 struct tcp_sock *tp = tcp_sk(sk);
4176
4177 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
4178 before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07004179 NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004180 tcp_enter_quickack_mode(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Eric Dumazet6496f6b2017-10-26 21:55:07 -07004182 if (tcp_is_sack(tp) && sock_net(sk)->ipv4.sysctl_tcp_dsack) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 u32 end_seq = TCP_SKB_CB(skb)->end_seq;
4184
4185 if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))
4186 end_seq = tp->rcv_nxt;
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004187 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 }
4189 }
4190
4191 tcp_send_ack(sk);
4192}
4193
4194/* These routines update the SACK block as out-of-order packets arrive or
4195 * in-order packets close up the sequence space.
4196 */
4197static void tcp_sack_maybe_coalesce(struct tcp_sock *tp)
4198{
4199 int this_sack;
4200 struct tcp_sack_block *sp = &tp->selective_acks[0];
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004201 struct tcp_sack_block *swalk = sp + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
4203 /* See if the recent change to the first SACK eats into
4204 * or hits the sequence space of other SACK blocks, if so coalesce.
4205 */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004206 for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) {
4208 int i;
4209
4210 /* Zap SWALK, by moving every further SACK up by one slot.
4211 * Decrease num_sacks.
4212 */
4213 tp->rx_opt.num_sacks--;
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004214 for (i = this_sack; i < tp->rx_opt.num_sacks; i++)
4215 sp[i] = sp[i + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 continue;
4217 }
4218 this_sack++, swalk++;
4219 }
4220}
4221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq)
4223{
4224 struct tcp_sock *tp = tcp_sk(sk);
4225 struct tcp_sack_block *sp = &tp->selective_acks[0];
4226 int cur_sacks = tp->rx_opt.num_sacks;
4227 int this_sack;
4228
4229 if (!cur_sacks)
4230 goto new_sack;
4231
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004232 for (this_sack = 0; this_sack < cur_sacks; this_sack++, sp++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 if (tcp_sack_extend(sp, seq, end_seq)) {
4234 /* Rotate this_sack to the first one. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004235 for (; this_sack > 0; this_sack--, sp--)
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07004236 swap(*sp, *(sp - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 if (cur_sacks > 1)
4238 tcp_sack_maybe_coalesce(tp);
4239 return;
4240 }
4241 }
4242
4243 /* Could not find an adjacent existing SACK, build a new one,
4244 * put it at the front, and shift everyone else down. We
4245 * always know there is at least one SACK present already here.
4246 *
4247 * If the sack array is full, forget about the last one.
4248 */
Adam Langley4389dde2008-07-19 00:07:02 -07004249 if (this_sack >= TCP_NUM_SACKS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 this_sack--;
4251 tp->rx_opt.num_sacks--;
4252 sp--;
4253 }
Stephen Hemminger2de979b2007-03-08 20:45:19 -08004254 for (; this_sack > 0; this_sack--, sp--)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004255 *sp = *(sp - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257new_sack:
4258 /* Build the new head SACK, and we're done. */
4259 sp->start_seq = seq;
4260 sp->end_seq = end_seq;
4261 tp->rx_opt.num_sacks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262}
4263
4264/* RCV.NXT advances, some SACKs should be eaten. */
4265
4266static void tcp_sack_remove(struct tcp_sock *tp)
4267{
4268 struct tcp_sack_block *sp = &tp->selective_acks[0];
4269 int num_sacks = tp->rx_opt.num_sacks;
4270 int this_sack;
4271
4272 /* Empty ofo queue, hence, all the SACKs are eaten. Clear. */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004273 if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 tp->rx_opt.num_sacks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 return;
4276 }
4277
Ilpo Järvinen056834d2007-12-31 14:57:14 -08004278 for (this_sack = 0; this_sack < num_sacks;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 /* Check if the start of the sack is covered by RCV.NXT. */
4280 if (!before(tp->rcv_nxt, sp->start_seq)) {
4281 int i;
4282
4283 /* RCV.NXT must cover all the block! */
Ilpo Järvinen547b7922008-07-25 21:43:18 -07004284 WARN_ON(before(tp->rcv_nxt, sp->end_seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
4286 /* Zap this SACK, by moving forward any other SACKS. */
Weilong Chena22318e2013-12-23 14:37:26 +08004287 for (i = this_sack+1; i < num_sacks; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 tp->selective_acks[i-1] = tp->selective_acks[i];
4289 num_sacks--;
4290 continue;
4291 }
4292 this_sack++;
4293 sp++;
4294 }
Ilpo Järvinen5861f8e2009-03-14 14:23:01 +00004295 tp->rx_opt.num_sacks = num_sacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
Eric Dumazet1402d362012-04-23 07:11:42 +00004298/**
4299 * tcp_try_coalesce - try to merge skb to prior one
4300 * @sk: socket
Mike Maloney98aaa912017-08-22 17:08:48 -04004301 * @dest: destination queue
Eric Dumazet1402d362012-04-23 07:11:42 +00004302 * @to: prior buffer
4303 * @from: buffer to add in queue
Eric Dumazet923dd342012-05-02 07:55:58 +00004304 * @fragstolen: pointer to boolean
Eric Dumazet1402d362012-04-23 07:11:42 +00004305 *
4306 * Before queueing skb @from after @to, try to merge them
4307 * to reduce overall memory use and queue lengths, if cost is small.
4308 * Packets in ofo or receive queues can stay a long time.
4309 * Better try to coalesce them right now to avoid future collapses.
Eric Dumazet783c1752012-04-23 17:34:36 +00004310 * Returns true if caller should free @from instead of queueing it
Eric Dumazet1402d362012-04-23 07:11:42 +00004311 */
Eric Dumazet783c1752012-04-23 17:34:36 +00004312static bool tcp_try_coalesce(struct sock *sk,
4313 struct sk_buff *to,
Eric Dumazet329033f2012-04-27 00:38:33 +00004314 struct sk_buff *from,
4315 bool *fragstolen)
Eric Dumazet1402d362012-04-23 07:11:42 +00004316{
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004317 int delta;
Eric Dumazet1402d362012-04-23 07:11:42 +00004318
Eric Dumazet329033f2012-04-27 00:38:33 +00004319 *fragstolen = false;
Alexander Duyck34a802a2012-05-02 21:19:09 +00004320
Eric Dumazet1ca7ee32012-05-23 17:51:37 +00004321 /* Its possible this segment overlaps with prior segment in queue */
4322 if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
4323 return false;
4324
Eric Dumazetbad43ca2012-05-19 03:02:02 +00004325 if (!skb_try_coalesce(to, from, fragstolen, &delta))
Eric Dumazet783c1752012-04-23 17:34:36 +00004326 return false;
Alexander Duyck34a802a2012-05-02 21:19:09 +00004327
Alexander Duyck34a802a2012-05-02 21:19:09 +00004328 atomic_add(delta, &sk->sk_rmem_alloc);
4329 sk_mem_charge(sk, delta);
Eric Dumazetc10d9312016-04-29 14:16:47 -07004330 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE);
Alexander Duyck34a802a2012-05-02 21:19:09 +00004331 TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
4332 TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
Eric Dumazete93a0432014-09-15 04:19:52 -07004333 TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags;
Mike Maloney98aaa912017-08-22 17:08:48 -04004334
4335 if (TCP_SKB_CB(from)->has_rxtstamp) {
4336 TCP_SKB_CB(to)->has_rxtstamp = true;
Eric Dumazetbffa72c2017-09-19 05:14:24 -07004337 to->tstamp = from->tstamp;
Mike Maloney98aaa912017-08-22 17:08:48 -04004338 }
4339
Alexander Duyck34a802a2012-05-02 21:19:09 +00004340 return true;
Eric Dumazet1402d362012-04-23 07:11:42 +00004341}
4342
Eric Dumazet532182c2016-04-01 08:52:19 -07004343static void tcp_drop(struct sock *sk, struct sk_buff *skb)
4344{
4345 sk_drops_add(sk, skb);
4346 __kfree_skb(skb);
4347}
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349/* This one checks to see if we can put data from the
4350 * out_of_order queue into the receive_queue.
4351 */
4352static void tcp_ofo_queue(struct sock *sk)
4353{
4354 struct tcp_sock *tp = tcp_sk(sk);
4355 __u32 dsack_high = tp->rcv_nxt;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004356 bool fin, fragstolen, eaten;
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004357 struct sk_buff *skb, *tail;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004358 struct rb_node *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004360 p = rb_first(&tp->out_of_order_queue);
4361 while (p) {
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004362 skb = rb_to_skb(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
4364 break;
4365
4366 if (before(TCP_SKB_CB(skb)->seq, dsack_high)) {
4367 __u32 dsack = dsack_high;
4368 if (before(TCP_SKB_CB(skb)->end_seq, dsack_high))
4369 dsack_high = TCP_SKB_CB(skb)->end_seq;
4370 tcp_dsack_extend(sk, TCP_SKB_CB(skb)->seq, dsack);
4371 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004372 p = rb_next(p);
4373 rb_erase(&skb->rbnode, &tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004375 if (unlikely(!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 SOCK_DEBUG(sk, "ofo packet was already received\n");
Eric Dumazet532182c2016-04-01 08:52:19 -07004377 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 continue;
4379 }
4380 SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n",
4381 tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
4382 TCP_SKB_CB(skb)->end_seq);
4383
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004384 tail = skb_peek_tail(&sk->sk_receive_queue);
Eric Dumazetbffa72c2017-09-19 05:14:24 -07004385 eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07004386 tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004387 fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004388 if (!eaten)
4389 __skb_queue_tail(&sk->sk_receive_queue, skb);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004390 else
Eric Dumazetbd1e75a2014-09-19 08:26:20 -07004391 kfree_skb_partial(skb, fragstolen);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004392
4393 if (unlikely(fin)) {
4394 tcp_fin(sk);
4395 /* tcp_fin() purges tp->out_of_order_queue,
4396 * so we must end this loop right now.
4397 */
4398 break;
4399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 }
4401}
4402
4403static bool tcp_prune_ofo_queue(struct sock *sk);
4404static int tcp_prune_queue(struct sock *sk);
4405
4406static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb,
4407 unsigned int size)
4408{
4409 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
4410 !sk_rmem_schedule(sk, skb, size)) {
4411
4412 if (tcp_prune_queue(sk) < 0)
4413 return -1;
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004414
Eric Dumazet36a65032016-08-17 14:17:09 -07004415 while (!sk_rmem_schedule(sk, skb, size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 if (!tcp_prune_ofo_queue(sk))
4417 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 }
4419 }
4420 return 0;
4421}
4422
Eric Dumazete86b29192012-03-18 11:06:44 +00004423static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
4424{
4425 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004426 struct rb_node **p, *parent;
Eric Dumazete86b29192012-03-18 11:06:44 +00004427 struct sk_buff *skb1;
4428 u32 seq, end_seq;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004429 bool fragstolen;
Eric Dumazete86b29192012-03-18 11:06:44 +00004430
Florian Westphal735d3832014-09-29 13:08:30 +02004431 tcp_ecn_check_ce(tp, skb);
Eric Dumazete86b29192012-03-18 11:06:44 +00004432
Mel Gormanc76562b2012-07-31 16:44:41 -07004433 if (unlikely(tcp_try_rmem_schedule(sk, skb, skb->truesize))) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07004434 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFODROP);
Eric Dumazet532182c2016-04-01 08:52:19 -07004435 tcp_drop(sk, skb);
Eric Dumazete86b29192012-03-18 11:06:44 +00004436 return;
4437 }
4438
Florian Westphal31770e32017-08-30 19:24:58 +02004439 /* Disable header prediction. */
4440 tp->pred_flags = 0;
Eric Dumazete86b29192012-03-18 11:06:44 +00004441 inet_csk_schedule_ack(sk);
4442
Eric Dumazetc10d9312016-04-29 14:16:47 -07004443 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004444 seq = TCP_SKB_CB(skb)->seq;
4445 end_seq = TCP_SKB_CB(skb)->end_seq;
Eric Dumazete86b29192012-03-18 11:06:44 +00004446 SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n",
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004447 tp->rcv_nxt, seq, end_seq);
Eric Dumazete86b29192012-03-18 11:06:44 +00004448
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004449 p = &tp->out_of_order_queue.rb_node;
4450 if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
Eric Dumazete86b29192012-03-18 11:06:44 +00004451 /* Initial out of order segment, build 1 SACK. */
4452 if (tcp_is_sack(tp)) {
4453 tp->rx_opt.num_sacks = 1;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004454 tp->selective_acks[0].start_seq = seq;
4455 tp->selective_acks[0].end_seq = end_seq;
Eric Dumazete86b29192012-03-18 11:06:44 +00004456 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004457 rb_link_node(&skb->rbnode, NULL, p);
4458 rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
4459 tp->ooo_last_skb = skb;
Eric Dumazete86b29192012-03-18 11:06:44 +00004460 goto end;
4461 }
4462
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004463 /* In the typical case, we are adding an skb to the end of the list.
4464 * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
4465 */
Eric Dumazetbffa72c2017-09-19 05:14:24 -07004466 if (tcp_try_coalesce(sk, tp->ooo_last_skb,
Mike Maloney98aaa912017-08-22 17:08:48 -04004467 skb, &fragstolen)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004468coalesce_done:
4469 tcp_grow_window(sk, skb);
4470 kfree_skb_partial(skb, fragstolen);
4471 skb = NULL;
4472 goto add_sack;
Eric Dumazete86b29192012-03-18 11:06:44 +00004473 }
Eric Dumazet2594a2a2016-09-09 14:22:45 -07004474 /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
4475 if (!before(seq, TCP_SKB_CB(tp->ooo_last_skb)->end_seq)) {
4476 parent = &tp->ooo_last_skb->rbnode;
4477 p = &parent->rb_right;
4478 goto insert;
Eric Dumazete86b29192012-03-18 11:06:44 +00004479 }
4480
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004481 /* Find place to insert this segment. Handle overlaps on the way. */
4482 parent = NULL;
4483 while (*p) {
4484 parent = *p;
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004485 skb1 = rb_to_skb(parent);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004486 if (before(seq, TCP_SKB_CB(skb1)->seq)) {
4487 p = &parent->rb_left;
4488 continue;
Eric Dumazete86b29192012-03-18 11:06:44 +00004489 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004490 if (before(seq, TCP_SKB_CB(skb1)->end_seq)) {
4491 if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
4492 /* All the bits are present. Drop. */
4493 NET_INC_STATS(sock_net(sk),
4494 LINUX_MIB_TCPOFOMERGE);
4495 __kfree_skb(skb);
4496 skb = NULL;
4497 tcp_dsack_set(sk, seq, end_seq);
4498 goto add_sack;
4499 }
4500 if (after(seq, TCP_SKB_CB(skb1)->seq)) {
4501 /* Partial overlap. */
4502 tcp_dsack_set(sk, seq, TCP_SKB_CB(skb1)->end_seq);
4503 } else {
4504 /* skb's seq == skb1's seq and skb covers skb1.
4505 * Replace skb1 with skb.
4506 */
4507 rb_replace_node(&skb1->rbnode, &skb->rbnode,
4508 &tp->out_of_order_queue);
4509 tcp_dsack_extend(sk,
4510 TCP_SKB_CB(skb1)->seq,
4511 TCP_SKB_CB(skb1)->end_seq);
4512 NET_INC_STATS(sock_net(sk),
4513 LINUX_MIB_TCPOFOMERGE);
4514 __kfree_skb(skb1);
Eric Dumazet76f0dcb2016-09-13 22:55:05 -07004515 goto merge_right;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004516 }
Eric Dumazetbffa72c2017-09-19 05:14:24 -07004517 } else if (tcp_try_coalesce(sk, skb1,
Mike Maloney98aaa912017-08-22 17:08:48 -04004518 skb, &fragstolen)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004519 goto coalesce_done;
4520 }
4521 p = &parent->rb_right;
Eric Dumazete86b29192012-03-18 11:06:44 +00004522 }
Eric Dumazet2594a2a2016-09-09 14:22:45 -07004523insert:
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004524 /* Insert segment into RB tree. */
4525 rb_link_node(&skb->rbnode, parent, p);
4526 rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
Eric Dumazete86b29192012-03-18 11:06:44 +00004527
Eric Dumazet76f0dcb2016-09-13 22:55:05 -07004528merge_right:
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004529 /* Remove other segments covered by skb. */
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004530 while ((skb1 = skb_rb_next(skb)) != NULL) {
Eric Dumazete86b29192012-03-18 11:06:44 +00004531 if (!after(end_seq, TCP_SKB_CB(skb1)->seq))
4532 break;
4533 if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
4534 tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
4535 end_seq);
4536 break;
4537 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004538 rb_erase(&skb1->rbnode, &tp->out_of_order_queue);
Eric Dumazete86b29192012-03-18 11:06:44 +00004539 tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
4540 TCP_SKB_CB(skb1)->end_seq);
Eric Dumazetc10d9312016-04-29 14:16:47 -07004541 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
Eric Dumazet532182c2016-04-01 08:52:19 -07004542 tcp_drop(sk, skb1);
Eric Dumazete86b29192012-03-18 11:06:44 +00004543 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004544 /* If there is no skb after us, we are the last_skb ! */
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004545 if (!skb1)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004546 tp->ooo_last_skb = skb;
Eric Dumazete86b29192012-03-18 11:06:44 +00004547
4548add_sack:
4549 if (tcp_is_sack(tp))
4550 tcp_sack_new_ofo_skb(sk, seq, end_seq);
4551end:
Eric Dumazet4e4f1fc2013-09-06 10:35:58 -07004552 if (skb) {
4553 tcp_grow_window(sk, skb);
Eric Dumazet60b1af32017-01-24 14:57:36 -08004554 skb_condense(skb);
Eric Dumazete86b29192012-03-18 11:06:44 +00004555 skb_set_owner_r(skb, sk);
Eric Dumazet4e4f1fc2013-09-06 10:35:58 -07004556 }
Eric Dumazete86b29192012-03-18 11:06:44 +00004557}
4558
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004559static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int hdrlen,
Eric Dumazetb081f852012-05-02 09:58:29 +00004560 bool *fragstolen)
4561{
4562 int eaten;
4563 struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue);
4564
4565 __skb_pull(skb, hdrlen);
4566 eaten = (tail &&
Eric Dumazetbffa72c2017-09-19 05:14:24 -07004567 tcp_try_coalesce(sk, tail,
Mike Maloney98aaa912017-08-22 17:08:48 -04004568 skb, fragstolen)) ? 1 : 0;
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07004569 tcp_rcv_nxt_update(tcp_sk(sk), TCP_SKB_CB(skb)->end_seq);
Eric Dumazetb081f852012-05-02 09:58:29 +00004570 if (!eaten) {
4571 __skb_queue_tail(&sk->sk_receive_queue, skb);
4572 skb_set_owner_r(skb, sk);
4573 }
4574 return eaten;
4575}
Eric Dumazete86b29192012-03-18 11:06:44 +00004576
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004577int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
4578{
Eric Dumazetcb934712014-09-17 03:14:42 -07004579 struct sk_buff *skb;
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004580 int err = -ENOMEM;
4581 int data_len = 0;
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004582 bool fragstolen;
4583
Pavel Emelyanovc454e612012-10-29 05:05:33 +00004584 if (size == 0)
4585 return 0;
4586
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004587 if (size > PAGE_SIZE) {
4588 int npages = min_t(size_t, size >> PAGE_SHIFT, MAX_SKB_FRAGS);
4589
4590 data_len = npages << PAGE_SHIFT;
4591 size = data_len + (size & ~PAGE_MASK);
4592 }
4593 skb = alloc_skb_with_frags(size - data_len, data_len,
4594 PAGE_ALLOC_COSTLY_ORDER,
4595 &err, sk->sk_allocation);
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004596 if (!skb)
4597 goto err;
4598
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004599 skb_put(skb, size - data_len);
4600 skb->data_len = data_len;
4601 skb->len = size;
4602
Eric Dumazetcb934712014-09-17 03:14:42 -07004603 if (tcp_try_rmem_schedule(sk, skb, skb->truesize))
Mel Gormanc76562b2012-07-31 16:44:41 -07004604 goto err_free;
4605
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004606 err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
4607 if (err)
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004608 goto err_free;
4609
4610 TCP_SKB_CB(skb)->seq = tcp_sk(sk)->rcv_nxt;
4611 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size;
4612 TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1;
4613
Eric Dumazetcb934712014-09-17 03:14:42 -07004614 if (tcp_queue_rcv(sk, skb, 0, &fragstolen)) {
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004615 WARN_ON_ONCE(fragstolen); /* should not happen */
4616 __kfree_skb(skb);
4617 }
4618 return size;
4619
4620err_free:
4621 kfree_skb(skb);
4622err:
Eric Dumazet5d4c9bf2015-11-18 21:03:33 -08004623 return err;
4624
Pavel Emelyanov292e8d82012-05-10 01:49:41 +00004625}
4626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
4628{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet5357f0b2017-08-01 07:02:44 -07004630 bool fragstolen;
4631 int eaten;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Eric Dumazet532182c2016-04-01 08:52:19 -07004633 if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
4634 __kfree_skb(skb);
4635 return;
4636 }
Eric Dumazetf84af322010-04-28 15:31:51 -07004637 skb_dst_drop(skb);
Peter Pan(潘卫平)155c6e12014-09-24 22:17:02 +08004638 __skb_pull(skb, tcp_hdr(skb)->doff * 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Florian Westphal735d3832014-09-29 13:08:30 +02004640 tcp_ecn_accept_cwr(tp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
Ilpo Järvinen5861f8e2009-03-14 14:23:01 +00004642 tp->rx_opt.dsack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
4644 /* Queue data for delivery to the user.
4645 * Packets in sequence go to the receive queue.
4646 * Out of sequence packets to the out_of_order_queue.
4647 */
4648 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
4649 if (tcp_receive_window(tp) == 0)
4650 goto out_of_window;
4651
4652 /* Ok. In sequence. In window. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653queue_and_out:
Eric Dumazet5357f0b2017-08-01 07:02:44 -07004654 if (skb_queue_len(&sk->sk_receive_queue) == 0)
4655 sk_forced_mem_schedule(sk, skb->truesize);
4656 else if (tcp_try_rmem_schedule(sk, skb, skb->truesize))
4657 goto drop;
4658
Florian Westphalc13ee2a2017-07-30 03:57:19 +02004659 eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen);
Eric Dumazetbdd1f9e2015-04-28 15:28:18 -07004660 tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
Stephen Hemminger2de979b2007-03-08 20:45:19 -08004661 if (skb->len)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07004662 tcp_event_data_recv(sk, skb);
Peter Pan(潘卫平)155c6e12014-09-24 22:17:02 +08004663 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
Eric Dumazet20c4cb72011-10-20 17:44:03 -04004664 tcp_fin(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004666 if (!RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 tcp_ofo_queue(sk);
4668
4669 /* RFC2581. 4.2. SHOULD send immediate ACK, when
4670 * gap in queue is filled.
4671 */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004672 if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004673 inet_csk(sk)->icsk_ack.pingpong = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 }
4675
4676 if (tp->rx_opt.num_sacks)
4677 tcp_sack_remove(tp);
4678
Florian Westphal31770e32017-08-30 19:24:58 +02004679 tcp_fast_path_check(sk);
4680
Eric Dumazet923dd342012-05-02 07:55:58 +00004681 if (eaten > 0)
4682 kfree_skb_partial(skb, fragstolen);
Eric Dumazet1d57f192012-09-17 12:51:39 +00004683 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04004684 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 return;
4686 }
4687
4688 if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
4689 /* A retransmit, 2nd most common case. Force an immediate ack. */
Eric Dumazetc10d9312016-04-29 14:16:47 -07004690 NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004691 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
4693out_of_window:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004694 tcp_enter_quickack_mode(sk);
4695 inet_csk_schedule_ack(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696drop:
Eric Dumazet532182c2016-04-01 08:52:19 -07004697 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 return;
4699 }
4700
4701 /* Out of window. F.e. zero window probe. */
4702 if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp)))
4703 goto out_of_window;
4704
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07004705 tcp_enter_quickack_mode(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
4707 if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
4708 /* Partial packet, seq < rcv_next < end_seq */
4709 SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n",
4710 tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
4711 TCP_SKB_CB(skb)->end_seq);
4712
Pavel Emelyanov1ed83462008-07-16 20:29:51 -07004713 tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09004714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 /* If window is closed, drop tail of packet. But after
4716 * remembering D-SACK for its head made in previous line.
4717 */
4718 if (!tcp_receive_window(tp))
4719 goto out_of_window;
4720 goto queue_and_out;
4721 }
4722
Eric Dumazete86b29192012-03-18 11:06:44 +00004723 tcp_data_queue_ofo(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724}
4725
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004726static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *list)
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004727{
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004728 if (list)
4729 return !skb_queue_is_last(list, skb) ? skb->next : NULL;
David S. Miller91521942009-05-28 21:35:47 -07004730
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004731 return skb_rb_next(skb);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004732}
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004733
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004734static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
4735 struct sk_buff_head *list,
4736 struct rb_root *root)
4737{
4738 struct sk_buff *next = tcp_skb_next(skb, list);
4739
4740 if (list)
4741 __skb_unlink(skb, list);
4742 else
4743 rb_erase(&skb->rbnode, root);
4744
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004745 __kfree_skb(skb);
Eric Dumazetc10d9312016-04-29 14:16:47 -07004746 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOLLAPSED);
Ilpo Järvinen2cf46632008-08-23 05:11:41 -07004747
4748 return next;
4749}
4750
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004751/* Insert skb into rb tree, ordered by TCP_SKB_CB(skb)->seq */
Eric Dumazet75c119a2017-10-05 22:21:27 -07004752void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004753{
4754 struct rb_node **p = &root->rb_node;
4755 struct rb_node *parent = NULL;
4756 struct sk_buff *skb1;
4757
4758 while (*p) {
4759 parent = *p;
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004760 skb1 = rb_to_skb(parent);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004761 if (before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb1)->seq))
4762 p = &parent->rb_left;
4763 else
4764 p = &parent->rb_right;
4765 }
4766 rb_link_node(&skb->rbnode, parent, p);
4767 rb_insert_color(&skb->rbnode, root);
4768}
4769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770/* Collapse contiguous sequence of skbs head..tail with
4771 * sequence numbers start..end.
David S. Miller91521942009-05-28 21:35:47 -07004772 *
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004773 * If tail is NULL, this means until the end of the queue.
David S. Miller91521942009-05-28 21:35:47 -07004774 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 * Segments with FIN/SYN are not collapsed (only because this
4776 * simplifies code)
4777 */
4778static void
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004779tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
4780 struct sk_buff *head, struct sk_buff *tail, u32 start, u32 end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004782 struct sk_buff *skb = head, *n;
4783 struct sk_buff_head tmp;
David S. Miller91521942009-05-28 21:35:47 -07004784 bool end_of_skbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08004786 /* First, check that queue is collapsible and find
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004787 * the point where collapsing can be useful.
4788 */
David S. Miller91521942009-05-28 21:35:47 -07004789restart:
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004790 for (end_of_skbs = true; skb != NULL && skb != tail; skb = n) {
4791 n = tcp_skb_next(skb, list);
4792
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 /* No new bits? It is possible on ofo queue. */
4794 if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004795 skb = tcp_collapse_one(sk, skb, list, root);
David S. Miller91521942009-05-28 21:35:47 -07004796 if (!skb)
4797 break;
4798 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 }
4800
4801 /* The first skb to collapse is:
4802 * - not SYN/FIN and
4803 * - bloated or contains data before "start" or
4804 * overlaps to the next one.
4805 */
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004806 if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) &&
Eric Dumazet94f08932017-10-26 21:55:09 -07004807 (tcp_win_from_space(sk, skb->truesize) > skb->len ||
David S. Miller91521942009-05-28 21:35:47 -07004808 before(TCP_SKB_CB(skb)->seq, start))) {
4809 end_of_skbs = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 break;
David S. Miller91521942009-05-28 21:35:47 -07004811 }
4812
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004813 if (n && n != tail &&
4814 TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(n)->seq) {
4815 end_of_skbs = false;
4816 break;
David S. Miller91521942009-05-28 21:35:47 -07004817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
4819 /* Decided to skip this, advance start seq. */
4820 start = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 }
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004822 if (end_of_skbs ||
4823 (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 return;
4825
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004826 __skb_queue_head_init(&tmp);
4827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 while (before(start, end)) {
Eric Dumazetb3d6cb92014-09-15 04:19:53 -07004829 int copy = min_t(int, SKB_MAX_ORDER(0, 0), end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 struct sk_buff *nskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
Eric Dumazetb3d6cb92014-09-15 04:19:53 -07004832 nskb = alloc_skb(copy, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 if (!nskb)
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004834 break;
Arnaldo Carvalho de Meloc51957d2007-03-10 12:47:22 -03004835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
4837 TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004838 if (list)
4839 __skb_queue_before(list, skb, nskb);
4840 else
4841 __skb_queue_tail(&tmp, nskb); /* defer rbtree insertion */
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004842 skb_set_owner_r(nskb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
4844 /* Copy data, releasing collapsed skbs. */
4845 while (copy > 0) {
4846 int offset = start - TCP_SKB_CB(skb)->seq;
4847 int size = TCP_SKB_CB(skb)->end_seq - start;
4848
Kris Katterjohn09a62662006-01-08 22:24:28 -08004849 BUG_ON(offset < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 if (size > 0) {
4851 size = min(copy, size);
4852 if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
4853 BUG();
4854 TCP_SKB_CB(nskb)->end_seq += size;
4855 copy -= size;
4856 start += size;
4857 }
4858 if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004859 skb = tcp_collapse_one(sk, skb, list, root);
David S. Miller91521942009-05-28 21:35:47 -07004860 if (!skb ||
4861 skb == tail ||
Eric Dumazete11ecdd2014-09-15 04:19:51 -07004862 (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004863 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 }
4865 }
4866 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004867end:
4868 skb_queue_walk_safe(&tmp, skb, n)
4869 tcp_rbtree_insert(root, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870}
4871
4872/* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
4873 * and tcp_collapse() them until all the queue is collapsed.
4874 */
4875static void tcp_collapse_ofo_queue(struct sock *sk)
4876{
4877 struct tcp_sock *tp = tcp_sk(sk);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004878 struct sk_buff *skb, *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 u32 start, end;
4880
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004881 skb = skb_rb_first(&tp->out_of_order_queue);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004882new_range:
4883 if (!skb) {
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004884 tp->ooo_last_skb = skb_rb_last(&tp->out_of_order_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 return;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 start = TCP_SKB_CB(skb)->seq;
4888 end = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004890 for (head = skb;;) {
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004891 skb = skb_rb_next(skb);
David S. Miller91521942009-05-28 21:35:47 -07004892
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004893 /* Range is terminated when we see a gap or when
4894 * we are at the queue end.
4895 */
David S. Miller91521942009-05-28 21:35:47 -07004896 if (!skb ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 after(TCP_SKB_CB(skb)->seq, end) ||
4898 before(TCP_SKB_CB(skb)->end_seq, start)) {
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004899 tcp_collapse(sk, NULL, &tp->out_of_order_queue,
David S. Miller8728b832005-08-09 19:25:21 -07004900 head, skb, start, end);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004901 goto new_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 }
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004903
4904 if (unlikely(before(TCP_SKB_CB(skb)->seq, start)))
4905 start = TCP_SKB_CB(skb)->seq;
4906 if (after(TCP_SKB_CB(skb)->end_seq, end))
4907 end = TCP_SKB_CB(skb)->end_seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 }
4909}
4910
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004911/*
Eric Dumazet36a65032016-08-17 14:17:09 -07004912 * Clean the out-of-order queue to make room.
4913 * We drop high sequences packets to :
4914 * 1) Let a chance for holes to be filled.
4915 * 2) not add too big latencies if thousands of packets sit there.
4916 * (But if application shrinks SO_RCVBUF, we could still end up
4917 * freeing whole queue here)
4918 *
4919 * Return true if queue has shrunk.
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004920 */
Eric Dumazeta2a385d2012-05-16 23:15:34 +00004921static bool tcp_prune_ofo_queue(struct sock *sk)
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004922{
4923 struct tcp_sock *tp = tcp_sk(sk);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004924 struct rb_node *node, *prev;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004925
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004926 if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
Eric Dumazet36a65032016-08-17 14:17:09 -07004927 return false;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004928
Eric Dumazet36a65032016-08-17 14:17:09 -07004929 NET_INC_STATS(sock_net(sk), LINUX_MIB_OFOPRUNED);
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004930 node = &tp->ooo_last_skb->rbnode;
4931 do {
4932 prev = rb_prev(node);
4933 rb_erase(node, &tp->out_of_order_queue);
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004934 tcp_drop(sk, rb_to_skb(node));
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004935 sk_mem_reclaim(sk);
Eric Dumazet36a65032016-08-17 14:17:09 -07004936 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
4937 !tcp_under_memory_pressure(sk))
4938 break;
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004939 node = prev;
4940 } while (node);
Eric Dumazet18a4c0e2017-10-05 22:21:21 -07004941 tp->ooo_last_skb = rb_to_skb(prev);
Eric Dumazet36a65032016-08-17 14:17:09 -07004942
4943 /* Reset SACK state. A conforming SACK implementation will
4944 * do the same at a timeout based retransmit. When a connection
4945 * is in a sad state like this, we care only about integrity
4946 * of the connection not performance.
4947 */
4948 if (tp->rx_opt.sack_ok)
4949 tcp_sack_reset(&tp->rx_opt);
4950 return true;
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004951}
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953/* Reduce allocated memory if we can, trying to get
4954 * the socket within its memory limits again.
4955 *
4956 * Return less than zero if we should start dropping frames
4957 * until the socket owning process reads some of the data
4958 * to stabilize the situation.
4959 */
4960static int tcp_prune_queue(struct sock *sk)
4961{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09004962 struct tcp_sock *tp = tcp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
4964 SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq);
4965
Eric Dumazetc10d9312016-04-29 14:16:47 -07004966 NET_INC_STATS(sock_net(sk), LINUX_MIB_PRUNECALLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
4968 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07004969 tcp_clamp_window(sk);
Eric Dumazetb8da51e2015-05-15 12:39:27 -07004970 else if (tcp_under_memory_pressure(sk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
4972
4973 tcp_collapse_ofo_queue(sk);
David S. Miller91521942009-05-28 21:35:47 -07004974 if (!skb_queue_empty(&sk->sk_receive_queue))
Yaogong Wang9f5afea2016-09-07 14:49:28 -07004975 tcp_collapse(sk, &sk->sk_receive_queue, NULL,
David S. Miller91521942009-05-28 21:35:47 -07004976 skb_peek(&sk->sk_receive_queue),
4977 NULL,
4978 tp->copied_seq, tp->rcv_nxt);
Hideo Aoki3ab224b2007-12-31 00:11:19 -08004979 sk_mem_reclaim(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
4981 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
4982 return 0;
4983
4984 /* Collapsing did not help, destructive actions follow.
4985 * This must not ever occur. */
4986
Vitaliy Gusevb000cd32008-04-15 00:33:38 -07004987 tcp_prune_ofo_queue(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988
4989 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
4990 return 0;
4991
4992 /* If we are really being abused, tell the caller to silently
4993 * drop receive data on the floor. It will get retransmitted
4994 * and hopefully then we'll have sufficient space.
4995 */
Eric Dumazetc10d9312016-04-29 14:16:47 -07004996 NET_INC_STATS(sock_net(sk), LINUX_MIB_RCVPRUNED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
4998 /* Massive buffer overcommit. */
Florian Westphal31770e32017-08-30 19:24:58 +02004999 tp->pred_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 return -1;
5001}
5002
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005003static bool tcp_should_expand_sndbuf(const struct sock *sk)
David S. Miller0d9901d2005-07-05 15:21:10 -07005004{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005005 const struct tcp_sock *tp = tcp_sk(sk);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005006
David S. Miller0d9901d2005-07-05 15:21:10 -07005007 /* If the user specified a specific send buffer setting, do
5008 * not modify it.
5009 */
5010 if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005011 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005012
5013 /* If we are under global TCP memory pressure, do not expand. */
Eric Dumazetb8da51e2015-05-15 12:39:27 -07005014 if (tcp_under_memory_pressure(sk))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005015 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005016
5017 /* If we are under soft global TCP memory pressure, do not expand. */
Glauber Costa180d8cd2011-12-11 21:47:02 +00005018 if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0))
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005019 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005020
5021 /* If we filled the congestion window, do not expand. */
Neal Cardwell65148902015-02-20 13:33:16 -05005022 if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005023 return false;
David S. Miller0d9901d2005-07-05 15:21:10 -07005024
Eric Dumazeta2a385d2012-05-16 23:15:34 +00005025 return true;
David S. Miller0d9901d2005-07-05 15:21:10 -07005026}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
5028/* When incoming ACK allowed to free some skb from write_queue,
5029 * we remember this event in flag SOCK_QUEUE_SHRUNK and wake up socket
5030 * on the exit from tcp input handler.
5031 *
5032 * PROBLEM: sndbuf expansion does not work well with largesend.
5033 */
5034static void tcp_new_space(struct sock *sk)
5035{
5036 struct tcp_sock *tp = tcp_sk(sk);
5037
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005038 if (tcp_should_expand_sndbuf(sk)) {
Eric Dumazet6ae70532013-10-01 10:23:44 -07005039 tcp_sndbuf_expand(sk);
Eric Dumazetc2203cf2017-05-16 14:00:04 -07005040 tp->snd_cwnd_stamp = tcp_jiffies32;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 }
5042
5043 sk->sk_write_space(sk);
5044}
5045
Stephen Hemminger40efc6f2006-01-03 16:03:49 -08005046static void tcp_check_space(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047{
5048 if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) {
5049 sock_reset_flag(sk, SOCK_QUEUE_SHRUNK);
jbaron@akamai.com3c715122015-04-20 20:05:07 +00005050 /* pairs with tcp_poll() */
Jason Baron56d80622017-01-24 21:49:41 -05005051 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 if (sk->sk_socket &&
Francis Yanb0f71bd2016-11-27 23:07:16 -08005053 test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 tcp_new_space(sk);
Francis Yanb0f71bd2016-11-27 23:07:16 -08005055 if (!test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
5056 tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
5057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 }
5059}
5060
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005061static inline void tcp_data_snd_check(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062{
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005063 tcp_push_pending_frames(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 tcp_check_space(sk);
5065}
5066
5067/*
5068 * Check if sending an ack is needed.
5069 */
5070static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
5071{
5072 struct tcp_sock *tp = tcp_sk(sk);
5073
5074 /* More than one full frame received... */
Joe Perches9d4fb272009-11-23 10:41:23 -08005075 if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 /* ... and right edge of window advances far enough.
5077 * (tcp_recvmsg() will send ACK otherwise). Or...
5078 */
Joe Perches9d4fb272009-11-23 10:41:23 -08005079 __tcp_select_window(sk) >= tp->rcv_wnd) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 /* We ACK each frame or... */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005081 tcp_in_quickack_mode(sk) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 /* We have out of order data. */
Yaogong Wang9f5afea2016-09-07 14:49:28 -07005083 (ofo_possible && !RB_EMPTY_ROOT(&tp->out_of_order_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 /* Then ack it now */
5085 tcp_send_ack(sk);
5086 } else {
5087 /* Else, send delayed ack. */
5088 tcp_send_delayed_ack(sk);
5089 }
5090}
5091
Stephen Hemminger40efc6f2006-01-03 16:03:49 -08005092static inline void tcp_ack_snd_check(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005094 if (!inet_csk_ack_scheduled(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 /* We sent a data segment already. */
5096 return;
5097 }
5098 __tcp_ack_snd_check(sk, 1);
5099}
5100
5101/*
5102 * This routine is only called when we have urgent data
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08005103 * signaled. Its the 'slow' part of tcp_urg. It could be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 * moved inline now as tcp_urg is only called from one
5105 * place. We handle URGent data wrong. We have to - as
5106 * BSD still doesn't use the correction from RFC961.
5107 * For 1003.1g we should support a new option TCP_STDURG to permit
5108 * either form (or just set the sysctl tcp_stdurg).
5109 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005110
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005111static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112{
5113 struct tcp_sock *tp = tcp_sk(sk);
5114 u32 ptr = ntohs(th->urg_ptr);
5115
Eric Dumazet3f4c7c62017-10-26 21:55:01 -07005116 if (ptr && !sock_net(sk)->ipv4.sysctl_tcp_stdurg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 ptr--;
5118 ptr += ntohl(th->seq);
5119
5120 /* Ignore urgent data that we've already seen and read. */
5121 if (after(tp->copied_seq, ptr))
5122 return;
5123
5124 /* Do not replay urg ptr.
5125 *
5126 * NOTE: interesting situation not covered by specs.
5127 * Misbehaving sender may send urg ptr, pointing to segment,
5128 * which we already have in ofo queue. We are not able to fetch
5129 * such data and will stay in TCP_URG_NOTYET until will be eaten
5130 * by recvmsg(). Seems, we are not obliged to handle such wicked
5131 * situations. But it is worth to think about possibility of some
5132 * DoSes using some hypothetical application level deadlock.
5133 */
5134 if (before(ptr, tp->rcv_nxt))
5135 return;
5136
5137 /* Do we already have a newer (or duplicate) urgent pointer? */
5138 if (tp->urg_data && !after(ptr, tp->urg_seq))
5139 return;
5140
5141 /* Tell the world about our new urgent pointer. */
5142 sk_send_sigurg(sk);
5143
5144 /* We may be adding urgent data when the last byte read was
5145 * urgent. To do this requires some care. We cannot just ignore
5146 * tp->copied_seq since we would read the last urgent byte again
5147 * as data, nor can we alter copied_seq until this data arrives
Stephen Hemmingercaa20d9a2005-11-10 17:13:47 -08005148 * or we break the semantics of SIOCATMARK (and thus sockatmark())
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 *
5150 * NOTE. Double Dutch. Rendering to plain English: author of comment
5151 * above did something sort of send("A", MSG_OOB); send("B", MSG_OOB);
5152 * and expect that both A and B disappear from stream. This is _wrong_.
5153 * Though this happens in BSD with high probability, this is occasional.
5154 * Any application relying on this is buggy. Note also, that fix "works"
5155 * only in this artificial test. Insert some normal data between A and B and we will
5156 * decline of BSD again. Verdict: it is better to remove to trap
5157 * buggy users.
5158 */
5159 if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005160 !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
5162 tp->copied_seq++;
5163 if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
David S. Miller8728b832005-08-09 19:25:21 -07005164 __skb_unlink(skb, &sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 __kfree_skb(skb);
5166 }
5167 }
5168
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005169 tp->urg_data = TCP_URG_NOTYET;
5170 tp->urg_seq = ptr;
Florian Westphal31770e32017-08-30 19:24:58 +02005171
5172 /* Disable header prediction. */
5173 tp->pred_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174}
5175
5176/* This is the 'fast' part of urgent handling. */
Eric Dumazetcf533ea2011-10-21 05:22:42 -04005177static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178{
5179 struct tcp_sock *tp = tcp_sk(sk);
5180
5181 /* Check if we get a new urgent pointer - normally not. */
5182 if (th->urg)
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005183 tcp_check_urg(sk, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
5185 /* Do we wait for any urgent data? - normally not... */
5186 if (tp->urg_data == TCP_URG_NOTYET) {
5187 u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) -
5188 th->syn;
5189
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005190 /* Is the urgent pointer pointing into this packet? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 if (ptr < skb->len) {
5192 u8 tmp;
5193 if (skb_copy_bits(skb, ptr, &tmp, 1))
5194 BUG();
5195 tp->urg_data = TCP_URG_VALID | tmp;
5196 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04005197 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 }
5199 }
5200}
5201
Jason Baron0e40f4c2017-01-17 13:37:19 -05005202/* Accept RST for rcv_nxt - 1 after a FIN.
5203 * When tcp connections are abruptly terminated from Mac OSX (via ^C), a
5204 * FIN is sent followed by a RST packet. The RST is sent with the same
5205 * sequence number as the FIN, and thus according to RFC 5961 a challenge
5206 * ACK should be sent. However, Mac OSX rate limits replies to challenge
5207 * ACKs on the closed socket. In addition middleboxes can drop either the
5208 * challenge ACK or a subsequent RST.
5209 */
5210static bool tcp_reset_check(const struct sock *sk, const struct sk_buff *skb)
5211{
5212 struct tcp_sock *tp = tcp_sk(sk);
5213
5214 return unlikely(TCP_SKB_CB(skb)->seq == (tp->rcv_nxt - 1) &&
5215 (1 << sk->sk_state) & (TCPF_CLOSE_WAIT | TCPF_LAST_ACK |
5216 TCPF_CLOSING));
5217}
5218
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005219/* Does PAWS and seqno based validation of an incoming segment, flags will
5220 * play significant role here.
5221 */
Eric Dumazet0c246042012-07-17 01:41:30 +00005222static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
5223 const struct tcphdr *th, int syn_inerr)
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005224{
5225 struct tcp_sock *tp = tcp_sk(sk);
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005226 bool rst_seq_match = false;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005227
5228 /* RFC1323: H1. Apply PAWS check first. */
Eric Dumazeteed29f12017-06-07 10:34:36 -07005229 if (tcp_fast_parse_options(sock_net(sk), skb, th, tp) &&
5230 tp->rx_opt.saw_tstamp &&
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005231 tcp_paws_discard(sk, skb)) {
5232 if (!th->rst) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07005233 NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005234 if (!tcp_oow_rate_limited(sock_net(sk), skb,
5235 LINUX_MIB_TCPACKSKIPPEDPAWS,
5236 &tp->last_oow_ack_time))
5237 tcp_send_dupack(sk, skb);
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005238 goto discard;
5239 }
5240 /* Reset is accepted even if it did not pass PAWS. */
5241 }
5242
5243 /* Step 1: check sequence number */
5244 if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {
5245 /* RFC793, page 37: "In all states except SYN-SENT, all reset
5246 * (RST) segments are validated by checking their SEQ-fields."
5247 * And page 69: "If an incoming segment is not acceptable,
5248 * an acknowledgment should be sent in reply (unless the RST
5249 * bit is set, if so drop the segment and return)".
5250 */
Eric Dumazete3715892012-07-17 12:29:30 +00005251 if (!th->rst) {
5252 if (th->syn)
5253 goto syn_challenge;
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005254 if (!tcp_oow_rate_limited(sock_net(sk), skb,
5255 LINUX_MIB_TCPACKSKIPPEDSEQ,
5256 &tp->last_oow_ack_time))
5257 tcp_send_dupack(sk, skb);
Jason Baron0e40f4c2017-01-17 13:37:19 -05005258 } else if (tcp_reset_check(sk, skb)) {
5259 tcp_reset(sk);
Eric Dumazete3715892012-07-17 12:29:30 +00005260 }
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005261 goto discard;
5262 }
5263
5264 /* Step 2: check RST bit */
5265 if (th->rst) {
Jason Baron0e40f4c2017-01-17 13:37:19 -05005266 /* RFC 5961 3.2 (extend to match against (RCV.NXT - 1) after a
5267 * FIN and SACK too if available):
5268 * If seq num matches RCV.NXT or (RCV.NXT - 1) after a FIN, or
5269 * the right-most SACK block,
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005270 * then
Eric Dumazet282f23c2012-07-17 10:13:05 +02005271 * RESET the connection
5272 * else
5273 * Send a challenge ACK
5274 */
Jason Baron0e40f4c2017-01-17 13:37:19 -05005275 if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt ||
5276 tcp_reset_check(sk, skb)) {
Pau Espin Pedrole00431b2016-06-07 16:30:34 +02005277 rst_seq_match = true;
5278 } else if (tcp_is_sack(tp) && tp->rx_opt.num_sacks > 0) {
5279 struct tcp_sack_block *sp = &tp->selective_acks[0];
5280 int max_sack = sp[0].end_seq;
5281 int this_sack;
5282
5283 for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;
5284 ++this_sack) {
5285 max_sack = after(sp[this_sack].end_seq,
5286 max_sack) ?
5287 sp[this_sack].end_seq : max_sack;
5288 }
5289
5290 if (TCP_SKB_CB(skb)->seq == max_sack)
5291 rst_seq_match = true;
5292 }
5293
5294 if (rst_seq_match)
Eric Dumazet282f23c2012-07-17 10:13:05 +02005295 tcp_reset(sk);
Wei Wangcf1ef3f2017-04-20 14:45:46 -07005296 else {
5297 /* Disable TFO if RST is out-of-order
5298 * and no data has been received
5299 * for current active TFO socket
5300 */
5301 if (tp->syn_fastopen && !tp->data_segs_in &&
5302 sk->sk_state == TCP_ESTABLISHED)
Wei Wang46c2fa32017-04-20 14:45:47 -07005303 tcp_fastopen_active_disable(sk);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005304 tcp_send_challenge_ack(sk, skb);
Wei Wangcf1ef3f2017-04-20 14:45:46 -07005305 }
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005306 goto discard;
5307 }
5308
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005309 /* step 3: check security and precedence [ignored] */
5310
Eric Dumazet0c246042012-07-17 01:41:30 +00005311 /* step 4: Check for a SYN
Sowmini Varadhancd214532014-10-30 12:48:08 -04005312 * RFC 5961 4.2 : Send a challenge ack
Eric Dumazet0c246042012-07-17 01:41:30 +00005313 */
5314 if (th->syn) {
Eric Dumazete3715892012-07-17 12:29:30 +00005315syn_challenge:
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005316 if (syn_inerr)
Eric Dumazetc10d9312016-04-29 14:16:47 -07005317 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
5318 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE);
Neal Cardwellf2b2c582015-02-06 16:04:40 -05005319 tcp_send_challenge_ack(sk, skb);
Eric Dumazet0c246042012-07-17 01:41:30 +00005320 goto discard;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005321 }
5322
Eric Dumazet0c246042012-07-17 01:41:30 +00005323 return true;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005324
5325discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005326 tcp_drop(sk, skb);
Eric Dumazet0c246042012-07-17 01:41:30 +00005327 return false;
Ilpo Järvinencbe2d122008-08-23 05:10:12 -07005328}
5329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330/*
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005331 * TCP receive function for the ESTABLISHED state.
Florian Westphal31770e32017-08-30 19:24:58 +02005332 *
5333 * It is split into a fast path and a slow path. The fast path is
5334 * disabled when:
5335 * - A zero window was announced from us - zero window probing
5336 * is only handled properly in the slow path.
5337 * - Out of order segments arrived.
5338 * - Urgent data is expected.
5339 * - There is no buffer space left
5340 * - Unexpected TCP flags/window values/header lengths are received
5341 * (detected by checking the TCP header against pred_flags)
5342 * - Data is sent in both directions. Fast path only supports pure senders
5343 * or pure receivers (this means either the sequence number or the ack
5344 * value must stay constant)
5345 * - Unexpected TCP option.
5346 *
5347 * When these conditions are not satisfied it drops into a standard
5348 * receive procedure patterned after RFC793 to handle all cases.
5349 * The first three cases are guaranteed by proper pred_flags setting,
5350 * the rest is checked inline. Fast processing is turned on in
5351 * tcp_data_queue when everything is OK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 */
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005353void tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
Matvejchikov Ilyae42e24c2017-07-24 16:02:12 +04005354 const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355{
Matvejchikov Ilyae42e24c2017-07-24 16:02:12 +04005356 unsigned int len = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 struct tcp_sock *tp = tcp_sk(sk);
5358
Eric Dumazet9a568de2017-05-16 14:00:14 -07005359 tcp_mstamp_refresh(tp);
Ian Morris51456b22015-04-03 09:17:26 +01005360 if (unlikely(!sk->sk_rx_dst))
Eric Dumazet5d299f32012-08-06 05:09:33 +00005361 inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
Florian Westphal31770e32017-08-30 19:24:58 +02005362 /*
5363 * Header prediction.
5364 * The code loosely follows the one in the famous
5365 * "30 instruction TCP receive" Van Jacobson mail.
5366 *
5367 * Van's trick is to deposit buffers into socket queue
5368 * on a device interrupt, to call tcp_recv function
5369 * on the receive process context and checksum and copy
5370 * the buffer to user space. smart...
5371 *
5372 * Our current scheme is not silly either but we take the
5373 * extra cost of the net_bh soft interrupt processing...
5374 * We do checksum and copy also but from device to kernel.
5375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376
5377 tp->rx_opt.saw_tstamp = 0;
5378
Florian Westphal31770e32017-08-30 19:24:58 +02005379 /* pred_flags is 0xS?10 << 16 + snd_wnd
5380 * if header_prediction is to be made
5381 * 'S' will always be tp->tcp_header_len >> 2
5382 * '?' will be 0 for the fast path, otherwise pred_flags is 0 to
5383 * turn it off (when there are holes in the receive
5384 * space for instance)
5385 * PSH flag is ignored.
5386 */
5387
5388 if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
5389 TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&
5390 !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
5391 int tcp_header_len = tp->tcp_header_len;
5392
5393 /* Timestamp header prediction: tcp_header_len
5394 * is automatically equal to th->doff*4 due to pred_flags
5395 * match.
5396 */
5397
5398 /* Check timestamp */
5399 if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
5400 /* No? Slow path! */
5401 if (!tcp_parse_aligned_timestamp(tp, th))
5402 goto slow_path;
5403
5404 /* If PAWS failed, check it more carefully in slow path */
5405 if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0)
5406 goto slow_path;
5407
5408 /* DO NOT update ts_recent here, if checksum fails
5409 * and timestamp was corrupted part, it will result
5410 * in a hung connection since we will drop all
5411 * future packets due to the PAWS test.
5412 */
5413 }
5414
5415 if (len <= tcp_header_len) {
5416 /* Bulk data transfer: sender */
5417 if (len == tcp_header_len) {
5418 /* Predicted packet is in window by definition.
5419 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
5420 * Hence, check seq<=rcv_wup reduces to:
5421 */
5422 if (tcp_header_len ==
5423 (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
5424 tp->rcv_nxt == tp->rcv_wup)
5425 tcp_store_ts_recent(tp);
5426
5427 /* We know that such packets are checksummed
5428 * on entry.
5429 */
5430 tcp_ack(sk, skb, 0);
5431 __kfree_skb(skb);
5432 tcp_data_snd_check(sk);
5433 return;
5434 } else { /* Header too small */
5435 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
5436 goto discard;
5437 }
5438 } else {
5439 int eaten = 0;
5440 bool fragstolen = false;
5441
5442 if (tcp_checksum_complete(skb))
5443 goto csum_error;
5444
5445 if ((int)skb->truesize > sk->sk_forward_alloc)
5446 goto step5;
5447
5448 /* Predicted packet is in window by definition.
5449 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
5450 * Hence, check seq<=rcv_wup reduces to:
5451 */
5452 if (tcp_header_len ==
5453 (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
5454 tp->rcv_nxt == tp->rcv_wup)
5455 tcp_store_ts_recent(tp);
5456
5457 tcp_rcv_rtt_measure_ts(sk, skb);
5458
5459 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPHITS);
5460
5461 /* Bulk data transfer: receiver */
5462 eaten = tcp_queue_rcv(sk, skb, tcp_header_len,
5463 &fragstolen);
5464
5465 tcp_event_data_recv(sk, skb);
5466
5467 if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
5468 /* Well, only one small jumplet in fast path... */
5469 tcp_ack(sk, skb, FLAG_DATA);
5470 tcp_data_snd_check(sk);
5471 if (!inet_csk_ack_scheduled(sk))
5472 goto no_ack;
5473 }
5474
5475 __tcp_ack_snd_check(sk, 0);
5476no_ack:
5477 if (eaten)
5478 kfree_skb_partial(skb, fragstolen);
5479 sk->sk_data_ready(sk);
5480 return;
5481 }
5482 }
5483
5484slow_path:
Eric Dumazetfb3477c2016-04-29 14:16:48 -07005485 if (len < (th->doff << 2) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 goto csum_error;
5487
Calvin Owens0c228e82014-11-20 15:09:53 -08005488 if (!th->ack && !th->rst && !th->syn)
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00005489 goto discard;
5490
Florian Westphal31770e32017-08-30 19:24:58 +02005491 /*
5492 * Standard slow path.
5493 */
5494
Eric Dumazet0c246042012-07-17 01:41:30 +00005495 if (!tcp_validate_incoming(sk, skb, th, 1))
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005496 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
Florian Westphal31770e32017-08-30 19:24:58 +02005498step5:
5499 if (tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0)
John Dykstra96e0bf42009-03-22 21:49:57 -07005500 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005502 tcp_rcv_rtt_measure_ts(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
5504 /* Process urgent data. */
5505 tcp_urg(sk, skb, th);
5506
5507 /* step 7: process the segment text */
5508 tcp_data_queue(sk, skb);
5509
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005510 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 tcp_ack_snd_check(sk);
Vijay Subramanianc995ae22013-09-03 12:23:22 -07005512 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
5514csum_error:
Eric Dumazetc10d9312016-04-29 14:16:47 -07005515 TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
5516 TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
5518discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005519 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00005521EXPORT_SYMBOL(tcp_rcv_established);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005523void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
5524{
5525 struct tcp_sock *tp = tcp_sk(sk);
5526 struct inet_connection_sock *icsk = inet_csk(sk);
5527
5528 tcp_set_state(sk, TCP_ESTABLISHED);
Eric Dumazet70eabf02017-05-16 14:00:07 -07005529 icsk->icsk_ack.lrcvtime = tcp_jiffies32;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005530
Ian Morris00db4122015-04-03 09:17:27 +01005531 if (skb) {
Eric Dumazet5d299f32012-08-06 05:09:33 +00005532 icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005533 security_inet_conn_established(sk, skb);
David S. Miller41063e92012-06-19 21:22:05 -07005534 }
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005535
Wei Wang27204aa2017-10-04 10:03:44 -07005536 tcp_init_transfer(sk, BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB);
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005537
5538 /* Prevent spurious tcp_cwnd_restart() on first data
5539 * packet.
5540 */
Eric Dumazetd635fbe2017-05-16 14:00:03 -07005541 tp->lsndtime = tcp_jiffies32;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005542
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005543 if (sock_flag(sk, SOCK_KEEPOPEN))
5544 inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp));
Florian Westphal31770e32017-08-30 19:24:58 +02005545
5546 if (!tp->rx_opt.snd_wscale)
5547 __tcp_fast_path_on(tp, tp->snd_wnd);
5548 else
5549 tp->pred_flags = 0;
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005550}
5551
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005552static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
5553 struct tcp_fastopen_cookie *cookie)
5554{
5555 struct tcp_sock *tp = tcp_sk(sk);
Eric Dumazet75c119a2017-10-05 22:21:27 -07005556 struct sk_buff *data = tp->syn_data ? tcp_rtx_queue_head(sk) : NULL;
Daniel Lee2646c832015-04-06 14:37:27 -07005557 u16 mss = tp->rx_opt.mss_clamp, try_exp = 0;
5558 bool syn_drop = false;
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005559
5560 if (mss == tp->rx_opt.user_mss) {
5561 struct tcp_options_received opt;
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005562
5563 /* Get original SYNACK MSS value if user MSS sets mss_clamp */
5564 tcp_clear_options(&opt);
5565 opt.user_mss = opt.mss_clamp = 0;
Eric Dumazeteed29f12017-06-07 10:34:36 -07005566 tcp_parse_options(sock_net(sk), synack, &opt, 0, NULL);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005567 mss = opt.mss_clamp;
5568 }
5569
Daniel Lee2646c832015-04-06 14:37:27 -07005570 if (!tp->syn_fastopen) {
5571 /* Ignore an unsolicited cookie */
Yuchung Cheng67da22d2012-07-19 06:43:11 +00005572 cookie->len = -1;
Daniel Lee2646c832015-04-06 14:37:27 -07005573 } else if (tp->total_retrans) {
5574 /* SYN timed out and the SYN-ACK neither has a cookie nor
5575 * acknowledges data. Presumably the remote received only
5576 * the retransmitted (regular) SYNs: either the original
5577 * SYN-data or the corresponding SYN-ACK was dropped.
5578 */
5579 syn_drop = (cookie->len < 0 && data);
5580 } else if (cookie->len < 0 && !tp->syn_data) {
5581 /* We requested a cookie but didn't get it. If we did not use
5582 * the (old) exp opt format then try so next time (try_exp=1).
5583 * Otherwise we go back to use the RFC7413 opt (try_exp=2).
5584 */
5585 try_exp = tp->syn_fastopen_exp ? 2 : 1;
5586 }
Yuchung Cheng67da22d2012-07-19 06:43:11 +00005587
Daniel Lee2646c832015-04-06 14:37:27 -07005588 tcp_fastopen_cache_set(sk, mss, cookie, syn_drop, try_exp);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005589
5590 if (data) { /* Retransmit unacked data in SYN */
Eric Dumazet75c119a2017-10-05 22:21:27 -07005591 skb_rbtree_walk_from(data) {
5592 if (__tcp_retransmit_skb(sk, data, 1))
Yuchung Cheng93b174a2012-12-06 08:45:32 +00005593 break;
5594 }
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005595 tcp_rearm_rto(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07005596 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005597 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005598 return true;
5599 }
Yuchung Cheng6f736012012-10-19 15:14:44 +00005600 tp->syn_data_acked = tp->syn_data;
Yuchung Chengf19c29e2014-03-03 12:31:36 -08005601 if (tp->syn_data_acked)
Eric Dumazetc10d9312016-04-29 14:16:47 -07005602 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005603 LINUX_MIB_TCPFASTOPENACTIVE);
Eric Dumazet61d2bca2016-02-01 21:03:07 -08005604
5605 tcp_fastopen_add_skb(sk, synack);
5606
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005607 return false;
5608}
5609
Ursula Braun60e2a772017-10-25 11:01:45 +02005610static void smc_check_reset_syn(struct tcp_sock *tp)
5611{
5612#if IS_ENABLED(CONFIG_SMC)
5613 if (static_branch_unlikely(&tcp_have_smc)) {
5614 if (tp->syn_smc && !tp->rx_opt.smc_ok)
5615 tp->syn_smc = 0;
5616 }
5617#endif
5618}
5619
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
Eric Dumazetbda07a62015-09-29 07:42:40 -07005621 const struct tcphdr *th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622{
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005623 struct inet_connection_sock *icsk = inet_csk(sk);
William Allen Simpson4957faade2009-12-02 18:25:27 +00005624 struct tcp_sock *tp = tcp_sk(sk);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005625 struct tcp_fastopen_cookie foc = { .len = -1 };
William Allen Simpson4957faade2009-12-02 18:25:27 +00005626 int saved_clamp = tp->rx_opt.mss_clamp;
Eric Dumazet0f9fa832017-04-18 09:45:52 -07005627 bool fastopen_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
Eric Dumazeteed29f12017-06-07 10:34:36 -07005629 tcp_parse_options(sock_net(sk), skb, &tp->rx_opt, 0, &foc);
Andrew Vagine3e12022013-08-27 12:21:55 +04005630 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
Andrey Vaginee684b62013-02-11 05:50:19 +00005631 tp->rx_opt.rcv_tsecr -= tp->tsoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
5633 if (th->ack) {
5634 /* rfc793:
5635 * "If the state is SYN-SENT then
5636 * first check the ACK bit
5637 * If the ACK bit is set
5638 * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
5639 * a reset (unless the RST bit is set, if so drop
5640 * the segment and return)"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 */
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005642 if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
5643 after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 goto reset_and_undo;
5645
5646 if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
5647 !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp,
Eric Dumazet9a568de2017-05-16 14:00:14 -07005648 tcp_time_stamp(tp))) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07005649 NET_INC_STATS(sock_net(sk),
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07005650 LINUX_MIB_PAWSACTIVEREJECTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 goto reset_and_undo;
5652 }
5653
5654 /* Now ACK is acceptable.
5655 *
5656 * "If the RST bit is set
5657 * If the ACK was acceptable then signal the user "error:
5658 * connection reset", drop the segment, enter CLOSED state,
5659 * delete TCB, and return."
5660 */
5661
5662 if (th->rst) {
5663 tcp_reset(sk);
5664 goto discard;
5665 }
5666
5667 /* rfc793:
5668 * "fifth, if neither of the SYN or RST bits is set then
5669 * drop the segment and return."
5670 *
5671 * See note below!
5672 * --ANK(990513)
5673 */
5674 if (!th->syn)
5675 goto discard_and_undo;
5676
5677 /* rfc793:
5678 * "If the SYN bit is on ...
5679 * are acceptable then ...
5680 * (our SYN has been ACKed), change the connection
5681 * state to ESTABLISHED..."
5682 */
5683
Florian Westphal735d3832014-09-29 13:08:30 +02005684 tcp_ecn_rcv_synack(tp, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Razvan Ghitulete1b3a6922012-08-14 16:30:20 +03005686 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
Florian Westphal31770e32017-08-30 19:24:58 +02005687 tcp_ack(sk, skb, FLAG_SLOWPATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
5689 /* Ok.. it's good. Set up sequence numbers and
5690 * move to established.
5691 */
5692 tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
5693 tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
5694
5695 /* RFC1323: The window in SYN & SYN/ACK segments is
5696 * never scaled.
5697 */
5698 tp->snd_wnd = ntohs(th->window);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
5700 if (!tp->rx_opt.wscale_ok) {
5701 tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0;
5702 tp->window_clamp = min(tp->window_clamp, 65535U);
5703 }
5704
5705 if (tp->rx_opt.saw_tstamp) {
5706 tp->rx_opt.tstamp_ok = 1;
5707 tp->tcp_header_len =
5708 sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
5709 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
5710 tcp_store_ts_recent(tp);
5711 } else {
5712 tp->tcp_header_len = sizeof(struct tcphdr);
5713 }
5714
Eric Dumazet0bc65a22017-10-26 21:55:04 -07005715 if (tcp_is_sack(tp) && sock_net(sk)->ipv4.sysctl_tcp_fack)
Ilpo Järvinene60402d2007-08-09 15:14:46 +03005716 tcp_enable_fack(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005718 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 tcp_initialize_rcv_mss(sk);
5720
5721 /* Remember, tcp_poll() does not lock socket!
5722 * Change state from SYN-SENT only after copied_seq
5723 * is initialized. */
5724 tp->copied_seq = tp->rcv_nxt;
William Allen Simpson4957faade2009-12-02 18:25:27 +00005725
Ursula Braun60e2a772017-10-25 11:01:45 +02005726 smc_check_reset_syn(tp);
5727
Ralf Baechlee16aa202006-12-07 00:11:33 -08005728 smp_mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
Pavel Emelyanov370816a2012-04-19 03:40:01 +00005730 tcp_finish_connect(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
Eric Dumazet0f9fa832017-04-18 09:45:52 -07005732 fastopen_fail = (tp->syn_fastopen || tp->syn_data) &&
5733 tcp_rcv_fastopen_synack(sk, skb, &foc);
Yuchung Cheng8e4178c2012-07-19 06:43:08 +00005734
Eric Dumazet0f9fa832017-04-18 09:45:52 -07005735 if (!sock_flag(sk, SOCK_DEAD)) {
5736 sk->sk_state_change(sk);
5737 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
5738 }
5739 if (fastopen_fail)
5740 return -1;
Arnaldo Carvalho de Melo295f7322005-08-09 20:11:56 -07005741 if (sk->sk_write_pending ||
5742 icsk->icsk_accept_queue.rskq_defer_accept ||
5743 icsk->icsk_ack.pingpong) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 /* Save one ACK. Data will be ready after
5745 * several ticks, if write_pending is set.
5746 *
5747 * It may be deleted, but with this feature tcpdumps
5748 * look so _wonderfully_ clever, that I was not able
5749 * to stand against the temptation 8) --ANK
5750 */
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005751 inet_csk_schedule_ack(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07005752 tcp_enter_quickack_mode(sk);
Arnaldo Carvalho de Melo3f421ba2005-08-09 20:11:08 -07005753 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
5754 TCP_DELACK_MAX, TCP_RTO_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
5756discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07005757 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 return 0;
5759 } else {
5760 tcp_send_ack(sk);
5761 }
5762 return -1;
5763 }
5764
5765 /* No ACK in the segment */
5766
5767 if (th->rst) {
5768 /* rfc793:
5769 * "If the RST bit is set
5770 *
5771 * Otherwise (no ACK) drop the segment and return."
5772 */
5773
5774 goto discard_and_undo;
5775 }
5776
5777 /* PAWS check. */
Ilpo Järvinen056834d2007-12-31 14:57:14 -08005778 if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp &&
Ilpo Järvinenc887e6d2009-03-14 14:23:03 +00005779 tcp_paws_reject(&tp->rx_opt, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 goto discard_and_undo;
5781
5782 if (th->syn) {
5783 /* We see SYN without ACK. It is attempt of
5784 * simultaneous connect with crossed SYNs.
5785 * Particularly, it can be connect to self.
5786 */
5787 tcp_set_state(sk, TCP_SYN_RECV);
5788
5789 if (tp->rx_opt.saw_tstamp) {
5790 tp->rx_opt.tstamp_ok = 1;
5791 tcp_store_ts_recent(tp);
5792 tp->tcp_header_len =
5793 sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
5794 } else {
5795 tp->tcp_header_len = sizeof(struct tcphdr);
5796 }
5797
5798 tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
Eric Dumazet142a2e72015-11-26 08:18:14 -08005799 tp->copied_seq = tp->rcv_nxt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
5801
5802 /* RFC1323: The window in SYN & SYN/ACK segments is
5803 * never scaled.
5804 */
5805 tp->snd_wnd = ntohs(th->window);
5806 tp->snd_wl1 = TCP_SKB_CB(skb)->seq;
5807 tp->max_window = tp->snd_wnd;
5808
Florian Westphal735d3832014-09-29 13:08:30 +02005809 tcp_ecn_rcv_syn(tp, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
John Heffner5d424d52006-03-20 17:53:41 -08005811 tcp_mtup_init(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08005812 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 tcp_initialize_rcv_mss(sk);
5814
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 tcp_send_synack(sk);
5816#if 0
5817 /* Note, we could accept data and URG from this segment.
Jerry Chu168a8f52012-08-31 12:29:13 +00005818 * There are no obstacles to make this (except that we must
5819 * either change tcp_recvmsg() to prevent it from returning data
5820 * before 3WHS completes per RFC793, or employ TCP Fast Open).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 *
5822 * However, if we ignore data in ACKless segments sometimes,
5823 * we have no reasons to accept it sometimes.
5824 * Also, seems the code doing it in step6 of tcp_rcv_state_process
5825 * is not flawless. So, discard packet for sanity.
5826 * Uncomment this return to process the data.
5827 */
5828 return -1;
5829#else
5830 goto discard;
5831#endif
5832 }
5833 /* "fifth, if neither of the SYN or RST bits is set then
5834 * drop the segment and return."
5835 */
5836
5837discard_and_undo:
5838 tcp_clear_options(&tp->rx_opt);
5839 tp->rx_opt.mss_clamp = saved_clamp;
5840 goto discard;
5841
5842reset_and_undo:
5843 tcp_clear_options(&tp->rx_opt);
5844 tp->rx_opt.mss_clamp = saved_clamp;
5845 return 1;
5846}
5847
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848/*
5849 * This function implements the receiving procedure of RFC 793 for
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005850 * all states except ESTABLISHED and TIME_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 * It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
5852 * address independent.
5853 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09005854
Eric Dumazet72ab4a82015-09-29 07:42:41 -07005855int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856{
5857 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08005858 struct inet_connection_sock *icsk = inet_csk(sk);
Eric Dumazet72ab4a82015-09-29 07:42:41 -07005859 const struct tcphdr *th = tcp_hdr(skb);
Jerry Chu168a8f52012-08-31 12:29:13 +00005860 struct request_sock *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 int queued = 0;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005862 bool acceptable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 switch (sk->sk_state) {
5865 case TCP_CLOSE:
5866 goto discard;
5867
5868 case TCP_LISTEN:
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005869 if (th->ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 return 1;
5871
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005872 if (th->rst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 goto discard;
5874
Stephen Hemminger2de979b2007-03-08 20:45:19 -08005875 if (th->syn) {
Eric Dumazetfdf5af02011-12-02 23:41:42 +00005876 if (th->fin)
5877 goto discard;
Eric Dumazet449809a2017-03-01 08:39:49 -08005878 /* It is possible that we process SYN packets from backlog,
5879 * so we need to make sure to disable BH right there.
5880 */
5881 local_bh_disable();
5882 acceptable = icsk->icsk_af_ops->conn_request(sk, skb) >= 0;
5883 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
Eric Dumazet449809a2017-03-01 08:39:49 -08005885 if (!acceptable)
5886 return 1;
Eric Dumazet0aea76d2016-04-21 22:13:01 -07005887 consume_skb(skb);
Masayuki Nakagawafb7e2392007-01-23 20:15:06 -08005888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 }
5890 goto discard;
5891
5892 case TCP_SYN_SENT:
Eric Dumazet8804b272016-04-13 22:05:40 -07005893 tp->rx_opt.saw_tstamp = 0;
Eric Dumazet9a568de2017-05-16 14:00:14 -07005894 tcp_mstamp_refresh(tp);
Eric Dumazetbda07a62015-09-29 07:42:40 -07005895 queued = tcp_rcv_synsent_state_process(sk, skb, th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 if (queued >= 0)
5897 return queued;
5898
5899 /* Do step6 onward by hand. */
5900 tcp_urg(sk, skb, th);
5901 __kfree_skb(skb);
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07005902 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 return 0;
5904 }
5905
Eric Dumazet9a568de2017-05-16 14:00:14 -07005906 tcp_mstamp_refresh(tp);
Eric Dumazet8804b272016-04-13 22:05:40 -07005907 tp->rx_opt.saw_tstamp = 0;
Jerry Chu168a8f52012-08-31 12:29:13 +00005908 req = tp->fastopen_rsk;
Ian Morris00db4122015-04-03 09:17:27 +01005909 if (req) {
Jerry Chu37561f62012-10-22 11:26:36 +00005910 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
Jerry Chu168a8f52012-08-31 12:29:13 +00005911 sk->sk_state != TCP_FIN_WAIT1);
5912
Ian Morris51456b22015-04-03 09:17:26 +01005913 if (!tcp_check_req(sk, skb, req, true))
Jerry Chu168a8f52012-08-31 12:29:13 +00005914 goto discard;
Neal Cardwelle69bebd2012-09-22 04:18:57 +00005915 }
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00005916
Calvin Owens0c228e82014-11-20 15:09:53 -08005917 if (!th->ack && !th->rst && !th->syn)
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00005918 goto discard;
5919
Neal Cardwelle69bebd2012-09-22 04:18:57 +00005920 if (!tcp_validate_incoming(sk, skb, th, 0))
Eric Dumazet0c246042012-07-17 01:41:30 +00005921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
5923 /* step 5: check the ACK field */
Florian Westphal31770e32017-08-30 19:24:58 +02005924 acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
5925 FLAG_UPDATE_TS_RECENT |
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -07005926 FLAG_NO_CHALLENGE_ACK) > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927
Eric Dumazetd0e1a1b2017-05-23 15:24:46 -07005928 if (!acceptable) {
5929 if (sk->sk_state == TCP_SYN_RECV)
5930 return 1; /* send one RST */
5931 tcp_send_challenge_ack(sk, skb);
5932 goto discard;
5933 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005934 switch (sk->sk_state) {
5935 case TCP_SYN_RECV:
Yuchung Cheng0f1c28a2015-09-18 11:36:14 -07005936 if (!tp->srtt_us)
5937 tcp_synack_rtt_meas(sk, req);
5938
Joe Perches61eb90032013-05-24 18:36:13 +00005939 /* Once we leave TCP_SYN_RECV, we no longer need req
5940 * so release it.
5941 */
5942 if (req) {
Yuchung Cheng7e32b442016-09-21 16:16:15 -07005943 inet_csk(sk)->icsk_retransmits = 0;
Joe Perches61eb90032013-05-24 18:36:13 +00005944 reqsk_fastopen_remove(sk, req, false);
Wei Wang6d050812017-10-04 10:04:04 -07005945 /* Re-arm the timer because data may have been sent out.
5946 * This is similar to the regular data transmission case
5947 * when new data has just been ack'ed.
5948 *
5949 * (TFO) - we could try to be more aggressive and
5950 * retransmitting any data sooner based on when they
5951 * are sent out.
5952 */
5953 tcp_rearm_rto(sk);
Joe Perches61eb90032013-05-24 18:36:13 +00005954 } else {
Wei Wang27204aa2017-10-04 10:03:44 -07005955 tcp_init_transfer(sk, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB);
Joe Perches61eb90032013-05-24 18:36:13 +00005956 tp->copied_seq = tp->rcv_nxt;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005957 }
Joe Perches61eb90032013-05-24 18:36:13 +00005958 smp_mb();
5959 tcp_set_state(sk, TCP_ESTABLISHED);
5960 sk->sk_state_change(sk);
5961
5962 /* Note, that this wakeup is only for marginal crossed SYN case.
5963 * Passively open sockets are not waked up, because
5964 * sk->sk_sleep == NULL and sk->sk_socket == NULL.
5965 */
5966 if (sk->sk_socket)
5967 sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
5968
5969 tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
5970 tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
5971 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
5972
5973 if (tp->rx_opt.tstamp_ok)
5974 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
5975
Yuchung Chengc0402762016-09-19 23:39:21 -04005976 if (!inet_csk(sk)->icsk_ca_ops->cong_control)
5977 tcp_update_pacing_rate(sk);
Neal Cardwell02cf4eb2013-10-21 15:40:19 -04005978
Joe Perches61eb90032013-05-24 18:36:13 +00005979 /* Prevent spurious tcp_cwnd_restart() on first data packet */
Eric Dumazetd635fbe2017-05-16 14:00:03 -07005980 tp->lsndtime = tcp_jiffies32;
Joe Perches61eb90032013-05-24 18:36:13 +00005981
5982 tcp_initialize_rcv_mss(sk);
Florian Westphal31770e32017-08-30 19:24:58 +02005983 tcp_fast_path_on(tp);
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005984 break;
5985
Joe Perchesc48b22d2013-05-24 18:06:58 +00005986 case TCP_FIN_WAIT1: {
Joe Perchesc48b22d2013-05-24 18:06:58 +00005987 int tmo;
5988
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005989 /* If we enter the TCP_FIN_WAIT1 state and we are a
5990 * Fast Open socket and this is the first acceptable
5991 * ACK we have received, this would have acknowledged
5992 * our SYNACK so stop the SYNACK timer.
5993 */
Ian Morris00db4122015-04-03 09:17:27 +01005994 if (req) {
Eric Dumazet1f6afc82013-05-24 15:03:54 +00005995 /* We no longer need the request sock. */
5996 reqsk_fastopen_remove(sk, req, false);
5997 tcp_rearm_rto(sk);
5998 }
Joe Perchesc48b22d2013-05-24 18:06:58 +00005999 if (tp->snd_una != tp->write_seq)
6000 break;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006001
Joe Perchesc48b22d2013-05-24 18:06:58 +00006002 tcp_set_state(sk, TCP_FIN_WAIT2);
6003 sk->sk_shutdown |= SEND_SHUTDOWN;
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006004
Julian Anastasovc3a2e832017-02-06 23:14:14 +02006005 sk_dst_confirm(sk);
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006006
Joe Perchesc48b22d2013-05-24 18:06:58 +00006007 if (!sock_flag(sk, SOCK_DEAD)) {
6008 /* Wake up lingering close() */
6009 sk->sk_state_change(sk);
6010 break;
6011 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006012
Wei Wangcf1ef3f2017-04-20 14:45:46 -07006013 if (tp->linger2 < 0) {
6014 tcp_done(sk);
6015 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
6016 return 1;
6017 }
6018 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
6019 after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
6020 /* Receive out of order FIN after close() */
6021 if (tp->syn_fastopen && th->fin)
Wei Wang46c2fa32017-04-20 14:45:47 -07006022 tcp_fastopen_active_disable(sk);
Joe Perchesc48b22d2013-05-24 18:06:58 +00006023 tcp_done(sk);
Eric Dumazetc10d9312016-04-29 14:16:47 -07006024 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
Joe Perchesc48b22d2013-05-24 18:06:58 +00006025 return 1;
6026 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006027
Joe Perchesc48b22d2013-05-24 18:06:58 +00006028 tmo = tcp_fin_time(sk);
6029 if (tmo > TCP_TIMEWAIT_LEN) {
6030 inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
6031 } else if (th->fin || sock_owned_by_user(sk)) {
6032 /* Bad case. We could lose such FIN otherwise.
6033 * It is not a big problem, but it looks confusing
6034 * and not so rare event. We still can lose it now,
6035 * if it spins in bh_lock_sock(), but it is really
6036 * marginal case.
6037 */
6038 inet_csk_reset_keepalive_timer(sk, tmo);
6039 } else {
6040 tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
6041 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006043 break;
Joe Perchesc48b22d2013-05-24 18:06:58 +00006044 }
Eric Dumazet1f6afc82013-05-24 15:03:54 +00006045
6046 case TCP_CLOSING:
6047 if (tp->snd_una == tp->write_seq) {
6048 tcp_time_wait(sk, TCP_TIME_WAIT, 0);
6049 goto discard;
6050 }
6051 break;
6052
6053 case TCP_LAST_ACK:
6054 if (tp->snd_una == tp->write_seq) {
6055 tcp_update_metrics(sk);
6056 tcp_done(sk);
6057 goto discard;
6058 }
6059 break;
Eric Dumazetc3ae62af2012-12-26 12:44:34 +00006060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
6062 /* step 6: check the URG bit */
6063 tcp_urg(sk, skb, th);
6064
6065 /* step 7: process the segment text */
6066 switch (sk->sk_state) {
6067 case TCP_CLOSE_WAIT:
6068 case TCP_CLOSING:
6069 case TCP_LAST_ACK:
6070 if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
6071 break;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05006072 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 case TCP_FIN_WAIT1:
6074 case TCP_FIN_WAIT2:
6075 /* RFC 793 says to queue data in these states,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006076 * RFC 1122 says we MUST send a reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 * BSD 4.4 also does reset.
6078 */
6079 if (sk->sk_shutdown & RCV_SHUTDOWN) {
6080 if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
6081 after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07006082 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 tcp_reset(sk);
6084 return 1;
6085 }
6086 }
6087 /* Fall through */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006088 case TCP_ESTABLISHED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 tcp_data_queue(sk, skb);
6090 queued = 1;
6091 break;
6092 }
6093
6094 /* tcp_data could move socket to TIME-WAIT */
6095 if (sk->sk_state != TCP_CLOSE) {
Ilpo Järvinen9e412ba2007-04-20 22:18:02 -07006096 tcp_data_snd_check(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 tcp_ack_snd_check(sk);
6098 }
6099
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09006100 if (!queued) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101discard:
Eric Dumazet532182c2016-04-01 08:52:19 -07006102 tcp_drop(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 }
6104 return 0;
6105}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106EXPORT_SYMBOL(tcp_rcv_state_process);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006107
6108static inline void pr_drop_req(struct request_sock *req, __u16 port, int family)
6109{
6110 struct inet_request_sock *ireq = inet_rsk(req);
6111
6112 if (family == AF_INET)
Joe Perchesba7a46f2014-11-11 10:59:17 -08006113 net_dbg_ratelimited("drop open request from %pI4/%u\n",
6114 &ireq->ir_rmt_addr, port);
Octavian Purdila4135ab82014-06-28 21:20:54 +03006115#if IS_ENABLED(CONFIG_IPV6)
6116 else if (family == AF_INET6)
Joe Perchesba7a46f2014-11-11 10:59:17 -08006117 net_dbg_ratelimited("drop open request from %pI6/%u\n",
6118 &ireq->ir_v6_rmt_addr, port);
Octavian Purdila4135ab82014-06-28 21:20:54 +03006119#endif
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006120}
6121
Florian Westphald82bd122014-09-29 13:08:29 +02006122/* RFC3168 : 6.1.1 SYN packets must not have ECT/ECN bits set
6123 *
6124 * If we receive a SYN packet with these bits set, it means a
6125 * network is playing bad games with TOS bits. In order to
6126 * avoid possible false congestion notifications, we disable
stephen hemmingerf4e715c2014-10-29 16:05:06 -07006127 * TCP ECN negotiation.
Florian Westphald82bd122014-09-29 13:08:29 +02006128 *
6129 * Exception: tcp_ca wants ECN. This is required for DCTCP
Florian Westphal843c2fd2015-01-30 20:45:20 +01006130 * congestion control: Linux DCTCP asserts ECT on all packets,
6131 * including SYN, which is most optimal solution; however,
6132 * others, such as FreeBSD do not.
Florian Westphald82bd122014-09-29 13:08:29 +02006133 */
6134static void tcp_ecn_create_request(struct request_sock *req,
6135 const struct sk_buff *skb,
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006136 const struct sock *listen_sk,
6137 const struct dst_entry *dst)
Florian Westphald82bd122014-09-29 13:08:29 +02006138{
6139 const struct tcphdr *th = tcp_hdr(skb);
6140 const struct net *net = sock_net(listen_sk);
6141 bool th_ecn = th->ece && th->cwr;
Florian Westphal843c2fd2015-01-30 20:45:20 +01006142 bool ect, ecn_ok;
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006143 u32 ecn_ok_dst;
Florian Westphald82bd122014-09-29 13:08:29 +02006144
6145 if (!th_ecn)
6146 return;
6147
6148 ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006149 ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
6150 ecn_ok = net->ipv4.sysctl_tcp_ecn || ecn_ok_dst;
Florian Westphald82bd122014-09-29 13:08:29 +02006151
Daniel Borkmannc3a8d942015-08-31 15:58:47 +02006152 if ((!ect && ecn_ok) || tcp_ca_needs_ecn(listen_sk) ||
Lawrence Brakmo91b5b212017-06-30 20:02:49 -07006153 (ecn_ok_dst & DST_FEATURE_ECN_CA) ||
6154 tcp_bpf_ca_needs_ecn((struct sock *)req))
Florian Westphald82bd122014-09-29 13:08:29 +02006155 inet_rsk(req)->ecn_ok = 1;
6156}
6157
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006158static void tcp_openreq_init(struct request_sock *req,
6159 const struct tcp_options_received *rx_opt,
6160 struct sk_buff *skb, const struct sock *sk)
6161{
6162 struct inet_request_sock *ireq = inet_rsk(req);
6163
Eric Dumazeted53d0a2015-10-08 19:33:23 -07006164 req->rsk_rcv_wnd = 0; /* So that tcp_send_synack() knows! */
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006165 req->cookie_ts = 0;
6166 tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
6167 tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
Eric Dumazet9a568de2017-05-16 14:00:14 -07006168 tcp_rsk(req)->snt_synack = tcp_clock_us();
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006169 tcp_rsk(req)->last_oow_ack_time = 0;
6170 req->mss = rx_opt->mss_clamp;
6171 req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
6172 ireq->tstamp_ok = rx_opt->tstamp_ok;
6173 ireq->sack_ok = rx_opt->sack_ok;
6174 ireq->snd_wscale = rx_opt->snd_wscale;
6175 ireq->wscale_ok = rx_opt->wscale_ok;
6176 ireq->acked = 0;
6177 ireq->ecn_ok = 0;
6178 ireq->ir_rmt_port = tcp_hdr(skb)->source;
6179 ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
6180 ireq->ir_mark = inet_request_mark(sk, skb);
Ursula Braun60e2a772017-10-25 11:01:45 +02006181#if IS_ENABLED(CONFIG_SMC)
6182 ireq->smc_ok = rx_opt->smc_ok;
6183#endif
Eric Dumazet1bfc4432015-03-16 21:06:19 -07006184}
6185
Eric Dumazete49bb332015-03-17 18:32:27 -07006186struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
Eric Dumazeta1a53442015-10-04 21:08:11 -07006187 struct sock *sk_listener,
6188 bool attach_listener)
Eric Dumazete49bb332015-03-17 18:32:27 -07006189{
Eric Dumazeta1a53442015-10-04 21:08:11 -07006190 struct request_sock *req = reqsk_alloc(ops, sk_listener,
6191 attach_listener);
Eric Dumazete49bb332015-03-17 18:32:27 -07006192
6193 if (req) {
6194 struct inet_request_sock *ireq = inet_rsk(req);
6195
6196 kmemcheck_annotate_bitfield(ireq, flags);
Eric Dumazetc92e8c02017-10-20 09:04:13 -07006197 ireq->ireq_opt = NULL;
Huw Davies56ac42b2016-06-27 15:05:28 -04006198#if IS_ENABLED(CONFIG_IPV6)
6199 ireq->pktopts = NULL;
6200#endif
Eric Dumazete49bb332015-03-17 18:32:27 -07006201 atomic64_set(&ireq->ir_cookie, 0);
6202 ireq->ireq_state = TCP_NEW_SYN_RECV;
6203 write_pnet(&ireq->ireq_net, sock_net(sk_listener));
Eric Dumazet0144a812015-03-24 21:45:56 -07006204 ireq->ireq_family = sk_listener->sk_family;
Eric Dumazete49bb332015-03-17 18:32:27 -07006205 }
6206
6207 return req;
6208}
6209EXPORT_SYMBOL(inet_reqsk_alloc);
6210
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006211/*
6212 * Return true if a syncookie should be sent
6213 */
Eric Dumazet2985aaa2015-09-29 07:42:51 -07006214static bool tcp_syn_flood_action(const struct sock *sk,
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006215 const struct sk_buff *skb,
6216 const char *proto)
6217{
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006218 struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006219 const char *msg = "Dropping request";
6220 bool want_cookie = false;
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006221 struct net *net = sock_net(sk);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006222
6223#ifdef CONFIG_SYN_COOKIES
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006224 if (net->ipv4.sysctl_tcp_syncookies) {
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006225 msg = "Sending cookies";
6226 want_cookie = true;
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07006227 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006228 } else
6229#endif
Eric Dumazet02a1d6e2016-04-27 16:44:39 -07006230 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006231
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006232 if (!queue->synflood_warned &&
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006233 net->ipv4.sysctl_tcp_syncookies != 2 &&
Eric Dumazet8d2675f2015-10-02 11:43:25 -07006234 xchg(&queue->synflood_warned, 1) == 0)
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006235 pr_info("%s: Possible SYN flooding on port %d. %s. Check SNMP counters.\n",
6236 proto, ntohs(tcp_hdr(skb)->dest), msg);
Eric Dumazet2985aaa2015-09-29 07:42:51 -07006237
Eric Dumazet41d25fe2015-03-25 15:08:47 -07006238 return want_cookie;
6239}
6240
Eric Dumazetcd8ae852015-05-03 21:34:46 -07006241static void tcp_reqsk_record_syn(const struct sock *sk,
6242 struct request_sock *req,
6243 const struct sk_buff *skb)
6244{
6245 if (tcp_sk(sk)->save_syn) {
6246 u32 len = skb_network_header_len(skb) + tcp_hdrlen(skb);
6247 u32 *copy;
6248
6249 copy = kmalloc(len + sizeof(u32), GFP_ATOMIC);
6250 if (copy) {
6251 copy[0] = len;
6252 memcpy(&copy[1], skb_network_header(skb), len);
6253 req->saved_syn = copy;
6254 }
6255 }
6256}
6257
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006258int tcp_conn_request(struct request_sock_ops *rsk_ops,
6259 const struct tcp_request_sock_ops *af_ops,
6260 struct sock *sk, struct sk_buff *skb)
6261{
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006262 struct tcp_fastopen_cookie foc = { .len = -1 };
Eric Dumazet7c85af82015-09-24 17:16:05 -07006263 __u32 isn = TCP_SKB_CB(skb)->tcp_tw_isn;
6264 struct tcp_options_received tmp_opt;
6265 struct tcp_sock *tp = tcp_sk(sk);
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006266 struct net *net = sock_net(sk);
Eric Dumazet7c85af82015-09-24 17:16:05 -07006267 struct sock *fastopen_sk = NULL;
Eric Dumazet7c85af82015-09-24 17:16:05 -07006268 struct request_sock *req;
6269 bool want_cookie = false;
Eric Dumazeteaa72dc2017-08-29 15:16:01 -07006270 struct dst_entry *dst;
Eric Dumazet7c85af82015-09-24 17:16:05 -07006271 struct flowi fl;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006272
6273 /* TW buckets are converted to open requests without
6274 * limitations, they conserve resources and peer is
6275 * evidently real one.
6276 */
Nikolay Borisov12ed8242016-02-03 09:46:51 +02006277 if ((net->ipv4.sysctl_tcp_syncookies == 2 ||
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006278 inet_csk_reqsk_queue_is_full(sk)) && !isn) {
6279 want_cookie = tcp_syn_flood_action(sk, skb, rsk_ops->slab_name);
6280 if (!want_cookie)
6281 goto drop;
6282 }
6283
Eric Dumazet5ea8ea22016-10-26 09:27:57 -07006284 if (sk_acceptq_is_full(sk)) {
Eric Dumazetc10d9312016-04-29 14:16:47 -07006285 NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006286 goto drop;
6287 }
6288
Eric Dumazeta1a53442015-10-04 21:08:11 -07006289 req = inet_reqsk_alloc(rsk_ops, sk, !want_cookie);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006290 if (!req)
6291 goto drop;
6292
6293 tcp_rsk(req)->af_specific = af_ops;
Florian Westphal95a22ca2016-12-01 11:32:06 +01006294 tcp_rsk(req)->ts_off = 0;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006295
6296 tcp_clear_options(&tmp_opt);
6297 tmp_opt.mss_clamp = af_ops->mss_clamp;
6298 tmp_opt.user_mss = tp->rx_opt.user_mss;
Eric Dumazeteed29f12017-06-07 10:34:36 -07006299 tcp_parse_options(sock_net(sk), skb, &tmp_opt, 0,
6300 want_cookie ? NULL : &foc);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006301
6302 if (want_cookie && !tmp_opt.saw_tstamp)
6303 tcp_clear_options(&tmp_opt);
6304
6305 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
6306 tcp_openreq_init(req, &tmp_opt, skb, sk);
KOVACS Krisztian7a682572016-09-23 11:27:42 +02006307 inet_rsk(req)->no_srccheck = inet_sk(sk)->transparent;
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006308
Eric Dumazet16f86162015-03-13 15:51:10 -07006309 /* Note: tcp_v6_init_req() might override ir_iif for link locals */
David Ahern6dd9a142015-12-16 13:20:44 -08006310 inet_rsk(req)->ir_iif = inet_request_bound_dev_if(sk, skb);
Eric Dumazet16f86162015-03-13 15:51:10 -07006311
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006312 af_ops->init_req(req, sk, skb);
6313
6314 if (security_inet_conn_request(sk, skb, req))
6315 goto drop_and_free;
6316
Eric Dumazet84b114b2017-05-05 06:56:54 -07006317 if (tmp_opt.tstamp_ok)
Eric Dumazet5d2ed052017-06-07 10:34:39 -07006318 tcp_rsk(req)->ts_off = af_ops->init_ts_off(net, skb);
Florian Westphal95a22ca2016-12-01 11:32:06 +01006319
Tonghao Zhang49c71582017-08-21 23:33:48 -07006320 dst = af_ops->route_req(sk, &fl, req);
6321 if (!dst)
6322 goto drop_and_free;
6323
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006324 if (!want_cookie && !isn) {
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006325 /* Kill the following clause, if you dislike this way. */
Soheil Hassas Yeganeh4396e462017-03-15 16:30:46 -04006326 if (!net->ipv4.sysctl_tcp_syncookies &&
6327 (net->ipv4.sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
6328 (net->ipv4.sysctl_max_syn_backlog >> 2)) &&
6329 !tcp_peer_is_proven(req, dst)) {
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006330 /* Without syncookies last quarter of
6331 * backlog is filled with destinations,
6332 * proven to be alive.
6333 * It means that we continue to communicate
6334 * to destinations, already remembered
6335 * to the moment of synflood.
6336 */
6337 pr_drop_req(req, ntohs(tcp_hdr(skb)->source),
6338 rsk_ops->family);
6339 goto drop_and_release;
6340 }
6341
Eric Dumazet84b114b2017-05-05 06:56:54 -07006342 isn = af_ops->init_seq(skb);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006343 }
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006344
Florian Westphalf7b3bec2014-11-03 17:35:03 +01006345 tcp_ecn_create_request(req, skb, sk, dst);
6346
6347 if (want_cookie) {
6348 isn = cookie_init_sequence(af_ops, sk, skb, &req->mss);
6349 req->cookie_ts = tmp_opt.tstamp_ok;
6350 if (!tmp_opt.tstamp_ok)
6351 inet_rsk(req)->ecn_ok = 0;
6352 }
6353
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006354 tcp_rsk(req)->snt_isn = isn;
Eric Dumazet58d607d2015-09-15 15:24:20 -07006355 tcp_rsk(req)->txhash = net_tx_rndhash();
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006356 tcp_openreq_init_rwin(req, sk, dst);
Eric Dumazetca6fb062015-10-02 11:43:35 -07006357 if (!want_cookie) {
Eric Dumazetca6fb062015-10-02 11:43:35 -07006358 tcp_reqsk_record_syn(sk, req, skb);
Christoph Paasch71c02372017-10-23 13:22:23 -07006359 fastopen_sk = tcp_try_fastopen(sk, skb, req, &foc, dst);
Eric Dumazetca6fb062015-10-02 11:43:35 -07006360 }
Eric Dumazet7c85af82015-09-24 17:16:05 -07006361 if (fastopen_sk) {
Eric Dumazetca6fb062015-10-02 11:43:35 -07006362 af_ops->send_synack(fastopen_sk, dst, &fl, req,
Eric Dumazetb3d05142016-04-13 22:05:39 -07006363 &foc, TCP_SYNACK_FASTOPEN);
Eric Dumazet7656d842015-10-04 21:08:07 -07006364 /* Add the child socket directly into the accept queue */
6365 inet_csk_reqsk_queue_add(sk, req, fastopen_sk);
6366 sk->sk_data_ready(sk);
6367 bh_unlock_sock(fastopen_sk);
Eric Dumazet7c85af82015-09-24 17:16:05 -07006368 sock_put(fastopen_sk);
6369 } else {
Eric Dumazet9439ce02015-03-17 18:32:29 -07006370 tcp_rsk(req)->tfo_listener = false;
Eric Dumazetca6fb062015-10-02 11:43:35 -07006371 if (!want_cookie)
Lawrence Brakmo8550f322017-06-30 20:02:42 -07006372 inet_csk_reqsk_queue_hash_add(sk, req,
6373 tcp_timeout_init((struct sock *)req));
Eric Dumazetb3d05142016-04-13 22:05:39 -07006374 af_ops->send_synack(sk, dst, &fl, req, &foc,
6375 !want_cookie ? TCP_SYNACK_NORMAL :
6376 TCP_SYNACK_COOKIE);
Eric Dumazet9caad862016-04-01 08:52:20 -07006377 if (want_cookie) {
6378 reqsk_free(req);
6379 return 0;
6380 }
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006381 }
Eric Dumazetca6fb062015-10-02 11:43:35 -07006382 reqsk_put(req);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006383 return 0;
6384
6385drop_and_release:
6386 dst_release(dst);
6387drop_and_free:
6388 reqsk_free(req);
6389drop:
Eric Dumazet9caad862016-04-01 08:52:20 -07006390 tcp_listendrop(sk);
Octavian Purdila1fb6f152014-06-25 17:10:02 +03006391 return 0;
6392}
6393EXPORT_SYMBOL(tcp_conn_request);