xref: /linux/net/ipv4/tcp_input.c (revision 35f079ebbc860dcd1cca70890c9c8d59c1145525)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Implementation of the Transmission Control Protocol(TCP).
71da177e4SLinus Torvalds  *
802c30a84SJesper Juhl  * Authors:	Ross Biro
91da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
101da177e4SLinus Torvalds  *		Mark Evans, <evansmp@uhura.aston.ac.uk>
111da177e4SLinus Torvalds  *		Corey Minyard <wf-rch!minyard@relay.EU.net>
121da177e4SLinus Torvalds  *		Florian La Roche, <flla@stud.uni-sb.de>
131da177e4SLinus Torvalds  *		Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
141da177e4SLinus Torvalds  *		Linus Torvalds, <torvalds@cs.helsinki.fi>
151da177e4SLinus Torvalds  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
161da177e4SLinus Torvalds  *		Matthew Dillon, <dillon@apollo.west.oic.com>
171da177e4SLinus Torvalds  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
181da177e4SLinus Torvalds  *		Jorge Cwik, <jorge@laser.satlink.net>
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds /*
221da177e4SLinus Torvalds  * Changes:
231da177e4SLinus Torvalds  *		Pedro Roque	:	Fast Retransmit/Recovery.
241da177e4SLinus Torvalds  *					Two receive queues.
251da177e4SLinus Torvalds  *					Retransmit queue handled by TCP.
261da177e4SLinus Torvalds  *					Better retransmit timer handling.
271da177e4SLinus Torvalds  *					New congestion avoidance.
281da177e4SLinus Torvalds  *					Header prediction.
291da177e4SLinus Torvalds  *					Variable renaming.
301da177e4SLinus Torvalds  *
311da177e4SLinus Torvalds  *		Eric		:	Fast Retransmit.
321da177e4SLinus Torvalds  *		Randy Scott	:	MSS option defines.
331da177e4SLinus Torvalds  *		Eric Schenk	:	Fixes to slow start algorithm.
341da177e4SLinus Torvalds  *		Eric Schenk	:	Yet another double ACK bug.
351da177e4SLinus Torvalds  *		Eric Schenk	:	Delayed ACK bug fixes.
361da177e4SLinus Torvalds  *		Eric Schenk	:	Floyd style fast retrans war avoidance.
371da177e4SLinus Torvalds  *		David S. Miller	:	Don't allow zero congestion window.
381da177e4SLinus Torvalds  *		Eric Schenk	:	Fix retransmitter so that it sends
391da177e4SLinus Torvalds  *					next packet on ack of previous packet.
401da177e4SLinus Torvalds  *		Andi Kleen	:	Moved open_request checking here
411da177e4SLinus Torvalds  *					and process RSTs for open_requests.
421da177e4SLinus Torvalds  *		Andi Kleen	:	Better prune_queue, and other fixes.
43caa20d9aSStephen Hemminger  *		Andrey Savochkin:	Fix RTT measurements in the presence of
441da177e4SLinus Torvalds  *					timestamps.
451da177e4SLinus Torvalds  *		Andrey Savochkin:	Check sequence numbers correctly when
461da177e4SLinus Torvalds  *					removing SACKs due to in sequence incoming
471da177e4SLinus Torvalds  *					data segments.
481da177e4SLinus Torvalds  *		Andi Kleen:		Make sure we never ack data there is not
491da177e4SLinus Torvalds  *					enough room for. Also make this condition
501da177e4SLinus Torvalds  *					a fatal error if it might still happen.
511da177e4SLinus Torvalds  *		Andi Kleen:		Add tcp_measure_rcv_mss to make
521da177e4SLinus Torvalds  *					connections with MSS<min(MTU,ann. MSS)
531da177e4SLinus Torvalds  *					work without delayed acks.
541da177e4SLinus Torvalds  *		Andi Kleen:		Process packets with PSH set in the
551da177e4SLinus Torvalds  *					fast path.
561da177e4SLinus Torvalds  *		J Hadi Salim:		ECN support
571da177e4SLinus Torvalds  *	 	Andrei Gurtov,
581da177e4SLinus Torvalds  *		Pasi Sarolahti,
591da177e4SLinus Torvalds  *		Panu Kuhlberg:		Experimental audit of TCP (re)transmission
601da177e4SLinus Torvalds  *					engine. Lots of bugs are found.
611da177e4SLinus Torvalds  *		Pasi Sarolahti:		F-RTO for dealing with spurious RTOs
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds 
64afd46503SJoe Perches #define pr_fmt(fmt) "TCP: " fmt
65afd46503SJoe Perches 
661da177e4SLinus Torvalds #include <linux/mm.h>
675a0e3ad6STejun Heo #include <linux/slab.h>
681da177e4SLinus Torvalds #include <linux/module.h>
691da177e4SLinus Torvalds #include <linux/sysctl.h>
70a0bffffcSIlpo Järvinen #include <linux/kernel.h>
715ffc02a1SSatoru SATOH #include <net/dst.h>
721da177e4SLinus Torvalds #include <net/tcp.h>
731da177e4SLinus Torvalds #include <net/inet_common.h>
741da177e4SLinus Torvalds #include <linux/ipsec.h>
751da177e4SLinus Torvalds #include <asm/unaligned.h>
761a2449a8SChris Leech #include <net/netdma.h>
771da177e4SLinus Torvalds 
78ab32ea5dSBrian Haley int sysctl_tcp_timestamps __read_mostly = 1;
79ab32ea5dSBrian Haley int sysctl_tcp_window_scaling __read_mostly = 1;
80ab32ea5dSBrian Haley int sysctl_tcp_sack __read_mostly = 1;
81ab32ea5dSBrian Haley int sysctl_tcp_fack __read_mostly = 1;
82ab32ea5dSBrian Haley int sysctl_tcp_reordering __read_mostly = TCP_FASTRETRANS_THRESH;
834bc2f18bSEric Dumazet EXPORT_SYMBOL(sysctl_tcp_reordering);
84ab32ea5dSBrian Haley int sysctl_tcp_dsack __read_mostly = 1;
85ab32ea5dSBrian Haley int sysctl_tcp_app_win __read_mostly = 31;
86b49960a0SEric Dumazet int sysctl_tcp_adv_win_scale __read_mostly = 1;
874bc2f18bSEric Dumazet EXPORT_SYMBOL(sysctl_tcp_adv_win_scale);
881da177e4SLinus Torvalds 
89282f23c6SEric Dumazet /* rfc5961 challenge ack rate limiting */
90282f23c6SEric Dumazet int sysctl_tcp_challenge_ack_limit = 100;
91282f23c6SEric Dumazet 
92ab32ea5dSBrian Haley int sysctl_tcp_stdurg __read_mostly;
93ab32ea5dSBrian Haley int sysctl_tcp_rfc1337 __read_mostly;
94ab32ea5dSBrian Haley int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
95c96fd3d4SIlpo Järvinen int sysctl_tcp_frto __read_mostly = 2;
961da177e4SLinus Torvalds 
977e380175SAndreas Petlund int sysctl_tcp_thin_dupack __read_mostly;
987e380175SAndreas Petlund 
99ab32ea5dSBrian Haley int sysctl_tcp_moderate_rcvbuf __read_mostly = 1;
1006ba8a3b1SNandita Dukkipati int sysctl_tcp_early_retrans __read_mostly = 3;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds #define FLAG_DATA		0x01 /* Incoming frame contained data.		*/
1031da177e4SLinus Torvalds #define FLAG_WIN_UPDATE		0x02 /* Incoming ACK was a window update.	*/
1041da177e4SLinus Torvalds #define FLAG_DATA_ACKED		0x04 /* This ACK acknowledged new data.		*/
1051da177e4SLinus Torvalds #define FLAG_RETRANS_DATA_ACKED	0x08 /* "" "" some of which was retransmitted.	*/
1061da177e4SLinus Torvalds #define FLAG_SYN_ACKED		0x10 /* This ACK acknowledged SYN.		*/
1071da177e4SLinus Torvalds #define FLAG_DATA_SACKED	0x20 /* New SACK.				*/
1081da177e4SLinus Torvalds #define FLAG_ECE		0x40 /* ECE in this ACK				*/
1091da177e4SLinus Torvalds #define FLAG_SLOWPATH		0x100 /* Do not skip RFC checks for window update.*/
110e33099f9SYuchung Cheng #define FLAG_ORIG_SACK_ACKED	0x200 /* Never retransmitted data are (s)acked	*/
1112e605294SIlpo Järvinen #define FLAG_SND_UNA_ADVANCED	0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
112564262c1SRyousei Takano #define FLAG_DSACKING_ACK	0x800 /* SACK blocks contained D-SACK info */
113cadbd031SIlpo Järvinen #define FLAG_SACK_RENEGING	0x2000 /* snd_una advanced to a sacked seq */
11412fb3dd9SEric Dumazet #define FLAG_UPDATE_TS_RECENT	0x4000 /* tcp_replace_ts_recent() */
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #define FLAG_ACKED		(FLAG_DATA_ACKED|FLAG_SYN_ACKED)
1171da177e4SLinus Torvalds #define FLAG_NOT_DUP		(FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
1181da177e4SLinus Torvalds #define FLAG_CA_ALERT		(FLAG_DATA_SACKED|FLAG_ECE)
1191da177e4SLinus Torvalds #define FLAG_FORWARD_PROGRESS	(FLAG_ACKED|FLAG_DATA_SACKED)
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
122bdf1ee5dSIlpo Järvinen #define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /* Adapt the MSS value used to make delayed ack decision to the
1251da177e4SLinus Torvalds  * real world.
1261da177e4SLinus Torvalds  */
127056834d9SIlpo Järvinen static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
1281da177e4SLinus Torvalds {
129463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
130463c84b9SArnaldo Carvalho de Melo 	const unsigned int lss = icsk->icsk_ack.last_seg_size;
131463c84b9SArnaldo Carvalho de Melo 	unsigned int len;
1321da177e4SLinus Torvalds 
133463c84b9SArnaldo Carvalho de Melo 	icsk->icsk_ack.last_seg_size = 0;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	/* skb->len may jitter because of SACKs, even if peer
1361da177e4SLinus Torvalds 	 * sends good full-sized frames.
1371da177e4SLinus Torvalds 	 */
138ff9b5e0fSHerbert Xu 	len = skb_shinfo(skb)->gso_size ? : skb->len;
139463c84b9SArnaldo Carvalho de Melo 	if (len >= icsk->icsk_ack.rcv_mss) {
140463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.rcv_mss = len;
1411da177e4SLinus Torvalds 	} else {
1421da177e4SLinus Torvalds 		/* Otherwise, we make more careful check taking into account,
1431da177e4SLinus Torvalds 		 * that SACKs block is variable.
1441da177e4SLinus Torvalds 		 *
1451da177e4SLinus Torvalds 		 * "len" is invariant segment length, including TCP header.
1461da177e4SLinus Torvalds 		 */
1479c70220bSArnaldo Carvalho de Melo 		len += skb->data - skb_transport_header(skb);
148bee7ca9eSWilliam Allen Simpson 		if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
1491da177e4SLinus Torvalds 		    /* If PSH is not set, packet should be
1501da177e4SLinus Torvalds 		     * full sized, provided peer TCP is not badly broken.
1511da177e4SLinus Torvalds 		     * This observation (if it is correct 8)) allows
1521da177e4SLinus Torvalds 		     * to handle super-low mtu links fairly.
1531da177e4SLinus Torvalds 		     */
1541da177e4SLinus Torvalds 		    (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
155aa8223c7SArnaldo Carvalho de Melo 		     !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
1561da177e4SLinus Torvalds 			/* Subtract also invariant (if peer is RFC compliant),
1571da177e4SLinus Torvalds 			 * tcp header plus fixed timestamp option length.
1581da177e4SLinus Torvalds 			 * Resulting "len" is MSS free of SACK jitter.
1591da177e4SLinus Torvalds 			 */
160463c84b9SArnaldo Carvalho de Melo 			len -= tcp_sk(sk)->tcp_header_len;
161463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.last_seg_size = len;
1621da177e4SLinus Torvalds 			if (len == lss) {
163463c84b9SArnaldo Carvalho de Melo 				icsk->icsk_ack.rcv_mss = len;
1641da177e4SLinus Torvalds 				return;
1651da177e4SLinus Torvalds 			}
1661da177e4SLinus Torvalds 		}
1671ef9696cSAlexey Kuznetsov 		if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
1681ef9696cSAlexey Kuznetsov 			icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
169463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
173463c84b9SArnaldo Carvalho de Melo static void tcp_incr_quickack(struct sock *sk)
1741da177e4SLinus Torvalds {
175463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
17695c96174SEric Dumazet 	unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	if (quickacks == 0)
1791da177e4SLinus Torvalds 		quickacks = 2;
180463c84b9SArnaldo Carvalho de Melo 	if (quickacks > icsk->icsk_ack.quick)
181463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS);
1821da177e4SLinus Torvalds }
1831da177e4SLinus Torvalds 
1841b9f4092Sstephen hemminger static void tcp_enter_quickack_mode(struct sock *sk)
1851da177e4SLinus Torvalds {
186463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
187463c84b9SArnaldo Carvalho de Melo 	tcp_incr_quickack(sk);
188463c84b9SArnaldo Carvalho de Melo 	icsk->icsk_ack.pingpong = 0;
189463c84b9SArnaldo Carvalho de Melo 	icsk->icsk_ack.ato = TCP_ATO_MIN;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /* Send ACKs quickly, if "quick" count is not exhausted
1931da177e4SLinus Torvalds  * and the session is not interactive.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds 
196a2a385d6SEric Dumazet static inline bool tcp_in_quickack_mode(const struct sock *sk)
1971da177e4SLinus Torvalds {
198463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
199a2a385d6SEric Dumazet 
200463c84b9SArnaldo Carvalho de Melo 	return icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
203bdf1ee5dSIlpo Järvinen static inline void TCP_ECN_queue_cwr(struct tcp_sock *tp)
204bdf1ee5dSIlpo Järvinen {
205bdf1ee5dSIlpo Järvinen 	if (tp->ecn_flags & TCP_ECN_OK)
206bdf1ee5dSIlpo Järvinen 		tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
207bdf1ee5dSIlpo Järvinen }
208bdf1ee5dSIlpo Järvinen 
209cf533ea5SEric Dumazet static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, const struct sk_buff *skb)
210bdf1ee5dSIlpo Järvinen {
211bdf1ee5dSIlpo Järvinen 	if (tcp_hdr(skb)->cwr)
212bdf1ee5dSIlpo Järvinen 		tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
213bdf1ee5dSIlpo Järvinen }
214bdf1ee5dSIlpo Järvinen 
215bdf1ee5dSIlpo Järvinen static inline void TCP_ECN_withdraw_cwr(struct tcp_sock *tp)
216bdf1ee5dSIlpo Järvinen {
217bdf1ee5dSIlpo Järvinen 	tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
218bdf1ee5dSIlpo Järvinen }
219bdf1ee5dSIlpo Järvinen 
2207a269ffaSEric Dumazet static inline void TCP_ECN_check_ce(struct tcp_sock *tp, const struct sk_buff *skb)
221bdf1ee5dSIlpo Järvinen {
2227a269ffaSEric Dumazet 	if (!(tp->ecn_flags & TCP_ECN_OK))
2237a269ffaSEric Dumazet 		return;
2247a269ffaSEric Dumazet 
225b82d1bb4SEric Dumazet 	switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
2267a269ffaSEric Dumazet 	case INET_ECN_NOT_ECT:
227bdf1ee5dSIlpo Järvinen 		/* Funny extension: if ECT is not set on a segment,
2287a269ffaSEric Dumazet 		 * and we already seen ECT on a previous segment,
2297a269ffaSEric Dumazet 		 * it is probably a retransmit.
2307a269ffaSEric Dumazet 		 */
2317a269ffaSEric Dumazet 		if (tp->ecn_flags & TCP_ECN_SEEN)
232bdf1ee5dSIlpo Järvinen 			tcp_enter_quickack_mode((struct sock *)tp);
2337a269ffaSEric Dumazet 		break;
2347a269ffaSEric Dumazet 	case INET_ECN_CE:
235aae06bf5SEric Dumazet 		if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) {
236aae06bf5SEric Dumazet 			/* Better not delay acks, sender can have a very low cwnd */
237aae06bf5SEric Dumazet 			tcp_enter_quickack_mode((struct sock *)tp);
2387a269ffaSEric Dumazet 			tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
239aae06bf5SEric Dumazet 		}
2407a269ffaSEric Dumazet 		/* fallinto */
2417a269ffaSEric Dumazet 	default:
2427a269ffaSEric Dumazet 		tp->ecn_flags |= TCP_ECN_SEEN;
243bdf1ee5dSIlpo Järvinen 	}
244bdf1ee5dSIlpo Järvinen }
245bdf1ee5dSIlpo Järvinen 
246cf533ea5SEric Dumazet static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)
247bdf1ee5dSIlpo Järvinen {
248bdf1ee5dSIlpo Järvinen 	if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
249bdf1ee5dSIlpo Järvinen 		tp->ecn_flags &= ~TCP_ECN_OK;
250bdf1ee5dSIlpo Järvinen }
251bdf1ee5dSIlpo Järvinen 
252cf533ea5SEric Dumazet static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)
253bdf1ee5dSIlpo Järvinen {
254bdf1ee5dSIlpo Järvinen 	if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
255bdf1ee5dSIlpo Järvinen 		tp->ecn_flags &= ~TCP_ECN_OK;
256bdf1ee5dSIlpo Järvinen }
257bdf1ee5dSIlpo Järvinen 
258a2a385d6SEric Dumazet static bool TCP_ECN_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)
259bdf1ee5dSIlpo Järvinen {
260bdf1ee5dSIlpo Järvinen 	if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
261a2a385d6SEric Dumazet 		return true;
262a2a385d6SEric Dumazet 	return false;
263bdf1ee5dSIlpo Järvinen }
264bdf1ee5dSIlpo Järvinen 
2651da177e4SLinus Torvalds /* Buffer size and advertised window tuning.
2661da177e4SLinus Torvalds  *
2671da177e4SLinus Torvalds  * 1. Tuning sk->sk_sndbuf, when connection enters established state.
2681da177e4SLinus Torvalds  */
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds static void tcp_fixup_sndbuf(struct sock *sk)
2711da177e4SLinus Torvalds {
27287fb4b7bSEric Dumazet 	int sndmem = SKB_TRUESIZE(tcp_sk(sk)->rx_opt.mss_clamp + MAX_TCP_HEADER);
2731da177e4SLinus Torvalds 
27406a59ecbSEric Dumazet 	sndmem *= TCP_INIT_CWND;
27506a59ecbSEric Dumazet 	if (sk->sk_sndbuf < sndmem)
27606a59ecbSEric Dumazet 		sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds /* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
2801da177e4SLinus Torvalds  *
2811da177e4SLinus Torvalds  * All tcp_full_space() is split to two parts: "network" buffer, allocated
2821da177e4SLinus Torvalds  * forward and advertised in receiver window (tp->rcv_wnd) and
2831da177e4SLinus Torvalds  * "application buffer", required to isolate scheduling/application
2841da177e4SLinus Torvalds  * latencies from network.
2851da177e4SLinus Torvalds  * window_clamp is maximal advertised window. It can be less than
2861da177e4SLinus Torvalds  * tcp_full_space(), in this case tcp_full_space() - window_clamp
2871da177e4SLinus Torvalds  * is reserved for "application" buffer. The less window_clamp is
2881da177e4SLinus Torvalds  * the smoother our behaviour from viewpoint of network, but the lower
2891da177e4SLinus Torvalds  * throughput and the higher sensitivity of the connection to losses. 8)
2901da177e4SLinus Torvalds  *
2911da177e4SLinus Torvalds  * rcv_ssthresh is more strict window_clamp used at "slow start"
2921da177e4SLinus Torvalds  * phase to predict further behaviour of this connection.
2931da177e4SLinus Torvalds  * It is used for two goals:
2941da177e4SLinus Torvalds  * - to enforce header prediction at sender, even when application
2951da177e4SLinus Torvalds  *   requires some significant "application buffer". It is check #1.
2961da177e4SLinus Torvalds  * - to prevent pruning of receive queue because of misprediction
2971da177e4SLinus Torvalds  *   of receiver window. Check #2.
2981da177e4SLinus Torvalds  *
2991da177e4SLinus Torvalds  * The scheme does not work when sender sends good segments opening
300caa20d9aSStephen Hemminger  * window and then starts to feed us spaghetti. But it should work
3011da177e4SLinus Torvalds  * in common situations. Otherwise, we have to rely on queue collapsing.
3021da177e4SLinus Torvalds  */
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds /* Slow part of check#2. */
3059e412ba7SIlpo Järvinen static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
3061da177e4SLinus Torvalds {
3079e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
3081da177e4SLinus Torvalds 	/* Optimize this! */
309dfd4f0aeSEric Dumazet 	int truesize = tcp_win_from_space(skb->truesize) >> 1;
310dfd4f0aeSEric Dumazet 	int window = tcp_win_from_space(sysctl_tcp_rmem[2]) >> 1;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	while (tp->rcv_ssthresh <= window) {
3131da177e4SLinus Torvalds 		if (truesize <= skb->len)
314463c84b9SArnaldo Carvalho de Melo 			return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 		truesize >>= 1;
3171da177e4SLinus Torvalds 		window >>= 1;
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 	return 0;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
322cf533ea5SEric Dumazet static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb)
3231da177e4SLinus Torvalds {
3249e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
3259e412ba7SIlpo Järvinen 
3261da177e4SLinus Torvalds 	/* Check #1 */
3271da177e4SLinus Torvalds 	if (tp->rcv_ssthresh < tp->window_clamp &&
3281da177e4SLinus Torvalds 	    (int)tp->rcv_ssthresh < tcp_space(sk) &&
329180d8cd9SGlauber Costa 	    !sk_under_memory_pressure(sk)) {
3301da177e4SLinus Torvalds 		int incr;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 		/* Check #2. Increase window, if skb with such overhead
3331da177e4SLinus Torvalds 		 * will fit to rcvbuf in future.
3341da177e4SLinus Torvalds 		 */
3351da177e4SLinus Torvalds 		if (tcp_win_from_space(skb->truesize) <= skb->len)
3361da177e4SLinus Torvalds 			incr = 2 * tp->advmss;
3371da177e4SLinus Torvalds 		else
3389e412ba7SIlpo Järvinen 			incr = __tcp_grow_window(sk, skb);
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 		if (incr) {
3414d846f02SEric Dumazet 			incr = max_t(int, incr, 2 * skb->len);
342056834d9SIlpo Järvinen 			tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr,
343056834d9SIlpo Järvinen 					       tp->window_clamp);
344463c84b9SArnaldo Carvalho de Melo 			inet_csk(sk)->icsk_ack.quick |= 1;
3451da177e4SLinus Torvalds 		}
3461da177e4SLinus Torvalds 	}
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds /* 3. Tuning rcvbuf, when connection enters established state. */
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds static void tcp_fixup_rcvbuf(struct sock *sk)
3521da177e4SLinus Torvalds {
353e9266a02SEric Dumazet 	u32 mss = tcp_sk(sk)->advmss;
354e9266a02SEric Dumazet 	u32 icwnd = TCP_DEFAULT_INIT_RCVWND;
355e9266a02SEric Dumazet 	int rcvmem;
3561da177e4SLinus Torvalds 
357e9266a02SEric Dumazet 	/* Limit to 10 segments if mss <= 1460,
358e9266a02SEric Dumazet 	 * or 14600/mss segments, with a minimum of two segments.
3591da177e4SLinus Torvalds 	 */
360e9266a02SEric Dumazet 	if (mss > 1460)
361e9266a02SEric Dumazet 		icwnd = max_t(u32, (1460 * TCP_DEFAULT_INIT_RCVWND) / mss, 2);
362e9266a02SEric Dumazet 
363e9266a02SEric Dumazet 	rcvmem = SKB_TRUESIZE(mss + MAX_TCP_HEADER);
364e9266a02SEric Dumazet 	while (tcp_win_from_space(rcvmem) < mss)
3651da177e4SLinus Torvalds 		rcvmem += 128;
366e9266a02SEric Dumazet 
367e9266a02SEric Dumazet 	rcvmem *= icwnd;
368e9266a02SEric Dumazet 
369e9266a02SEric Dumazet 	if (sk->sk_rcvbuf < rcvmem)
370e9266a02SEric Dumazet 		sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]);
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
373caa20d9aSStephen Hemminger /* 4. Try to fixup all. It is made immediately after connection enters
3741da177e4SLinus Torvalds  *    established state.
3751da177e4SLinus Torvalds  */
37610467163SJerry Chu void tcp_init_buffer_space(struct sock *sk)
3771da177e4SLinus Torvalds {
3781da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
3791da177e4SLinus Torvalds 	int maxwin;
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
3821da177e4SLinus Torvalds 		tcp_fixup_rcvbuf(sk);
3831da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
3841da177e4SLinus Torvalds 		tcp_fixup_sndbuf(sk);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	tp->rcvq_space.space = tp->rcv_wnd;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	maxwin = tcp_full_space(sk);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 	if (tp->window_clamp >= maxwin) {
3911da177e4SLinus Torvalds 		tp->window_clamp = maxwin;
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 		if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
3941da177e4SLinus Torvalds 			tp->window_clamp = max(maxwin -
3951da177e4SLinus Torvalds 					       (maxwin >> sysctl_tcp_app_win),
3961da177e4SLinus Torvalds 					       4 * tp->advmss);
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	/* Force reservation of one segment. */
4001da177e4SLinus Torvalds 	if (sysctl_tcp_app_win &&
4011da177e4SLinus Torvalds 	    tp->window_clamp > 2 * tp->advmss &&
4021da177e4SLinus Torvalds 	    tp->window_clamp + tp->advmss > maxwin)
4031da177e4SLinus Torvalds 		tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
4061da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds /* 5. Recalculate window clamp after socket hit its memory bounds. */
4109e412ba7SIlpo Järvinen static void tcp_clamp_window(struct sock *sk)
4111da177e4SLinus Torvalds {
4129e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
4136687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
4141da177e4SLinus Torvalds 
4156687e988SArnaldo Carvalho de Melo 	icsk->icsk_ack.quick = 0;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
4181da177e4SLinus Torvalds 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
419180d8cd9SGlauber Costa 	    !sk_under_memory_pressure(sk) &&
420180d8cd9SGlauber Costa 	    sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {
4211da177e4SLinus Torvalds 		sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
4221da177e4SLinus Torvalds 				    sysctl_tcp_rmem[2]);
4231da177e4SLinus Torvalds 	}
424326f36e9SJohn Heffner 	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
4251da177e4SLinus Torvalds 		tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
42840efc6faSStephen Hemminger /* Initialize RCV_MSS value.
42940efc6faSStephen Hemminger  * RCV_MSS is an our guess about MSS used by the peer.
43040efc6faSStephen Hemminger  * We haven't any direct information about the MSS.
43140efc6faSStephen Hemminger  * It's better to underestimate the RCV_MSS rather than overestimate.
43240efc6faSStephen Hemminger  * Overestimations make us ACKing less frequently than needed.
43340efc6faSStephen Hemminger  * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
43440efc6faSStephen Hemminger  */
43540efc6faSStephen Hemminger void tcp_initialize_rcv_mss(struct sock *sk)
43640efc6faSStephen Hemminger {
437cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
43840efc6faSStephen Hemminger 	unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
43940efc6faSStephen Hemminger 
44040efc6faSStephen Hemminger 	hint = min(hint, tp->rcv_wnd / 2);
441bee7ca9eSWilliam Allen Simpson 	hint = min(hint, TCP_MSS_DEFAULT);
44240efc6faSStephen Hemminger 	hint = max(hint, TCP_MIN_MSS);
44340efc6faSStephen Hemminger 
44440efc6faSStephen Hemminger 	inet_csk(sk)->icsk_ack.rcv_mss = hint;
44540efc6faSStephen Hemminger }
4464bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_initialize_rcv_mss);
44740efc6faSStephen Hemminger 
4481da177e4SLinus Torvalds /* Receiver "autotuning" code.
4491da177e4SLinus Torvalds  *
4501da177e4SLinus Torvalds  * The algorithm for RTT estimation w/o timestamps is based on
4511da177e4SLinus Torvalds  * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
452631dd1a8SJustin P. Mattock  * <http://public.lanl.gov/radiant/pubs.html#DRS>
4531da177e4SLinus Torvalds  *
4541da177e4SLinus Torvalds  * More detail on this code can be found at
455631dd1a8SJustin P. Mattock  * <http://staff.psc.edu/jheffner/>,
4561da177e4SLinus Torvalds  * though this reference is out of date.  A new paper
4571da177e4SLinus Torvalds  * is pending.
4581da177e4SLinus Torvalds  */
4591da177e4SLinus Torvalds static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	u32 new_sample = tp->rcv_rtt_est.rtt;
4621da177e4SLinus Torvalds 	long m = sample;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	if (m == 0)
4651da177e4SLinus Torvalds 		m = 1;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	if (new_sample != 0) {
4681da177e4SLinus Torvalds 		/* If we sample in larger samples in the non-timestamp
4691da177e4SLinus Torvalds 		 * case, we could grossly overestimate the RTT especially
4701da177e4SLinus Torvalds 		 * with chatty applications or bulk transfer apps which
4711da177e4SLinus Torvalds 		 * are stalled on filesystem I/O.
4721da177e4SLinus Torvalds 		 *
4731da177e4SLinus Torvalds 		 * Also, since we are only going for a minimum in the
47431f34269SStephen Hemminger 		 * non-timestamp case, we do not smooth things out
475caa20d9aSStephen Hemminger 		 * else with timestamps disabled convergence takes too
4761da177e4SLinus Torvalds 		 * long.
4771da177e4SLinus Torvalds 		 */
4781da177e4SLinus Torvalds 		if (!win_dep) {
4791da177e4SLinus Torvalds 			m -= (new_sample >> 3);
4801da177e4SLinus Torvalds 			new_sample += m;
48118a223e0SNeal Cardwell 		} else {
48218a223e0SNeal Cardwell 			m <<= 3;
48318a223e0SNeal Cardwell 			if (m < new_sample)
48418a223e0SNeal Cardwell 				new_sample = m;
48518a223e0SNeal Cardwell 		}
4861da177e4SLinus Torvalds 	} else {
487caa20d9aSStephen Hemminger 		/* No previous measure. */
4881da177e4SLinus Torvalds 		new_sample = m << 3;
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	if (tp->rcv_rtt_est.rtt != new_sample)
4921da177e4SLinus Torvalds 		tp->rcv_rtt_est.rtt = new_sample;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
4961da177e4SLinus Torvalds {
4971da177e4SLinus Torvalds 	if (tp->rcv_rtt_est.time == 0)
4981da177e4SLinus Torvalds 		goto new_measure;
4991da177e4SLinus Torvalds 	if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
5001da177e4SLinus Torvalds 		return;
501651913ceSNeal Cardwell 	tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rcv_rtt_est.time, 1);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds new_measure:
5041da177e4SLinus Torvalds 	tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
5051da177e4SLinus Torvalds 	tp->rcv_rtt_est.time = tcp_time_stamp;
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
508056834d9SIlpo Järvinen static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
509056834d9SIlpo Järvinen 					  const struct sk_buff *skb)
5101da177e4SLinus Torvalds {
511463c84b9SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
5121da177e4SLinus Torvalds 	if (tp->rx_opt.rcv_tsecr &&
5131da177e4SLinus Torvalds 	    (TCP_SKB_CB(skb)->end_seq -
514463c84b9SArnaldo Carvalho de Melo 	     TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss))
5151da177e4SLinus Torvalds 		tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0);
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds /*
5191da177e4SLinus Torvalds  * This function should be called every time data is copied to user space.
5201da177e4SLinus Torvalds  * It calculates the appropriate TCP receive buffer space.
5211da177e4SLinus Torvalds  */
5221da177e4SLinus Torvalds void tcp_rcv_space_adjust(struct sock *sk)
5231da177e4SLinus Torvalds {
5241da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
5251da177e4SLinus Torvalds 	int time;
5261da177e4SLinus Torvalds 	int space;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	if (tp->rcvq_space.time == 0)
5291da177e4SLinus Torvalds 		goto new_measure;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	time = tcp_time_stamp - tp->rcvq_space.time;
532056834d9SIlpo Järvinen 	if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)
5331da177e4SLinus Torvalds 		return;
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	space = 2 * (tp->copied_seq - tp->rcvq_space.seq);
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	space = max(tp->rcvq_space.space, space);
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	if (tp->rcvq_space.space != space) {
5401da177e4SLinus Torvalds 		int rcvmem;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 		tp->rcvq_space.space = space;
5431da177e4SLinus Torvalds 
5446fcf9412SJohn Heffner 		if (sysctl_tcp_moderate_rcvbuf &&
5456fcf9412SJohn Heffner 		    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
5461da177e4SLinus Torvalds 			int new_clamp = space;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 			/* Receive space grows, normalize in order to
5491da177e4SLinus Torvalds 			 * take into account packet headers and sk_buff
5501da177e4SLinus Torvalds 			 * structure overhead.
5511da177e4SLinus Torvalds 			 */
5521da177e4SLinus Torvalds 			space /= tp->advmss;
5531da177e4SLinus Torvalds 			if (!space)
5541da177e4SLinus Torvalds 				space = 1;
55587fb4b7bSEric Dumazet 			rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
5561da177e4SLinus Torvalds 			while (tcp_win_from_space(rcvmem) < tp->advmss)
5571da177e4SLinus Torvalds 				rcvmem += 128;
5581da177e4SLinus Torvalds 			space *= rcvmem;
5591da177e4SLinus Torvalds 			space = min(space, sysctl_tcp_rmem[2]);
5601da177e4SLinus Torvalds 			if (space > sk->sk_rcvbuf) {
5611da177e4SLinus Torvalds 				sk->sk_rcvbuf = space;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 				/* Make the window clamp follow along.  */
5641da177e4SLinus Torvalds 				tp->window_clamp = new_clamp;
5651da177e4SLinus Torvalds 			}
5661da177e4SLinus Torvalds 		}
5671da177e4SLinus Torvalds 	}
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds new_measure:
5701da177e4SLinus Torvalds 	tp->rcvq_space.seq = tp->copied_seq;
5711da177e4SLinus Torvalds 	tp->rcvq_space.time = tcp_time_stamp;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds /* There is something which you must keep in mind when you analyze the
5751da177e4SLinus Torvalds  * behavior of the tp->ato delayed ack timeout interval.  When a
5761da177e4SLinus Torvalds  * connection starts up, we want to ack as quickly as possible.  The
5771da177e4SLinus Torvalds  * problem is that "good" TCP's do slow start at the beginning of data
5781da177e4SLinus Torvalds  * transmission.  The means that until we send the first few ACK's the
5791da177e4SLinus Torvalds  * sender will sit on his end and only queue most of his data, because
5801da177e4SLinus Torvalds  * he can only send snd_cwnd unacked packets at any given time.  For
5811da177e4SLinus Torvalds  * each ACK we send, he increments snd_cwnd and transmits more of his
5821da177e4SLinus Torvalds  * queue.  -DaveM
5831da177e4SLinus Torvalds  */
5849e412ba7SIlpo Järvinen static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
5851da177e4SLinus Torvalds {
5869e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
587463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
5881da177e4SLinus Torvalds 	u32 now;
5891da177e4SLinus Torvalds 
590463c84b9SArnaldo Carvalho de Melo 	inet_csk_schedule_ack(sk);
5911da177e4SLinus Torvalds 
592463c84b9SArnaldo Carvalho de Melo 	tcp_measure_rcv_mss(sk, skb);
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	tcp_rcv_rtt_measure(tp);
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	now = tcp_time_stamp;
5971da177e4SLinus Torvalds 
598463c84b9SArnaldo Carvalho de Melo 	if (!icsk->icsk_ack.ato) {
5991da177e4SLinus Torvalds 		/* The _first_ data packet received, initialize
6001da177e4SLinus Torvalds 		 * delayed ACK engine.
6011da177e4SLinus Torvalds 		 */
602463c84b9SArnaldo Carvalho de Melo 		tcp_incr_quickack(sk);
603463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.ato = TCP_ATO_MIN;
6041da177e4SLinus Torvalds 	} else {
605463c84b9SArnaldo Carvalho de Melo 		int m = now - icsk->icsk_ack.lrcvtime;
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 		if (m <= TCP_ATO_MIN / 2) {
6081da177e4SLinus Torvalds 			/* The fastest case is the first. */
609463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
610463c84b9SArnaldo Carvalho de Melo 		} else if (m < icsk->icsk_ack.ato) {
611463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
612463c84b9SArnaldo Carvalho de Melo 			if (icsk->icsk_ack.ato > icsk->icsk_rto)
613463c84b9SArnaldo Carvalho de Melo 				icsk->icsk_ack.ato = icsk->icsk_rto;
614463c84b9SArnaldo Carvalho de Melo 		} else if (m > icsk->icsk_rto) {
615caa20d9aSStephen Hemminger 			/* Too long gap. Apparently sender failed to
6161da177e4SLinus Torvalds 			 * restart window, so that we send ACKs quickly.
6171da177e4SLinus Torvalds 			 */
618463c84b9SArnaldo Carvalho de Melo 			tcp_incr_quickack(sk);
6193ab224beSHideo Aoki 			sk_mem_reclaim(sk);
6201da177e4SLinus Torvalds 		}
6211da177e4SLinus Torvalds 	}
622463c84b9SArnaldo Carvalho de Melo 	icsk->icsk_ack.lrcvtime = now;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	TCP_ECN_check_ce(tp, skb);
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	if (skb->len >= 128)
6279e412ba7SIlpo Järvinen 		tcp_grow_window(sk, skb);
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds /* Called to compute a smoothed rtt estimate. The data fed to this
6311da177e4SLinus Torvalds  * routine either comes from timestamps, or from segments that were
6321da177e4SLinus Torvalds  * known _not_ to have been retransmitted [see Karn/Partridge
6331da177e4SLinus Torvalds  * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
6341da177e4SLinus Torvalds  * piece by Van Jacobson.
6351da177e4SLinus Torvalds  * NOTE: the next three routines used to be one big routine.
6361da177e4SLinus Torvalds  * To save cycles in the RFC 1323 implementation it was better to break
6371da177e4SLinus Torvalds  * it up into three procedures. -- erics
6381da177e4SLinus Torvalds  */
6392d2abbabSStephen Hemminger static void tcp_rtt_estimator(struct sock *sk, const __u32 mrtt)
6401da177e4SLinus Torvalds {
6416687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
6421da177e4SLinus Torvalds 	long m = mrtt; /* RTT */
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	/*	The following amusing code comes from Jacobson's
6451da177e4SLinus Torvalds 	 *	article in SIGCOMM '88.  Note that rtt and mdev
6461da177e4SLinus Torvalds 	 *	are scaled versions of rtt and mean deviation.
6471da177e4SLinus Torvalds 	 *	This is designed to be as fast as possible
6481da177e4SLinus Torvalds 	 *	m stands for "measurement".
6491da177e4SLinus Torvalds 	 *
6501da177e4SLinus Torvalds 	 *	On a 1990 paper the rto value is changed to:
6511da177e4SLinus Torvalds 	 *	RTO = rtt + 4 * mdev
6521da177e4SLinus Torvalds 	 *
6531da177e4SLinus Torvalds 	 * Funny. This algorithm seems to be very broken.
6541da177e4SLinus Torvalds 	 * These formulae increase RTO, when it should be decreased, increase
65531f34269SStephen Hemminger 	 * too slowly, when it should be increased quickly, decrease too quickly
6561da177e4SLinus Torvalds 	 * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
6571da177e4SLinus Torvalds 	 * does not matter how to _calculate_ it. Seems, it was trap
6581da177e4SLinus Torvalds 	 * that VJ failed to avoid. 8)
6591da177e4SLinus Torvalds 	 */
6601da177e4SLinus Torvalds 	if (m == 0)
6611da177e4SLinus Torvalds 		m = 1;
6621da177e4SLinus Torvalds 	if (tp->srtt != 0) {
6631da177e4SLinus Torvalds 		m -= (tp->srtt >> 3);	/* m is now error in rtt est */
6641da177e4SLinus Torvalds 		tp->srtt += m;		/* rtt = 7/8 rtt + 1/8 new */
6651da177e4SLinus Torvalds 		if (m < 0) {
6661da177e4SLinus Torvalds 			m = -m;		/* m is now abs(error) */
6671da177e4SLinus Torvalds 			m -= (tp->mdev >> 2);   /* similar update on mdev */
6681da177e4SLinus Torvalds 			/* This is similar to one of Eifel findings.
6691da177e4SLinus Torvalds 			 * Eifel blocks mdev updates when rtt decreases.
6701da177e4SLinus Torvalds 			 * This solution is a bit different: we use finer gain
6711da177e4SLinus Torvalds 			 * for mdev in this case (alpha*beta).
6721da177e4SLinus Torvalds 			 * Like Eifel it also prevents growth of rto,
6731da177e4SLinus Torvalds 			 * but also it limits too fast rto decreases,
6741da177e4SLinus Torvalds 			 * happening in pure Eifel.
6751da177e4SLinus Torvalds 			 */
6761da177e4SLinus Torvalds 			if (m > 0)
6771da177e4SLinus Torvalds 				m >>= 3;
6781da177e4SLinus Torvalds 		} else {
6791da177e4SLinus Torvalds 			m -= (tp->mdev >> 2);   /* similar update on mdev */
6801da177e4SLinus Torvalds 		}
6811da177e4SLinus Torvalds 		tp->mdev += m;	    	/* mdev = 3/4 mdev + 1/4 new */
6821da177e4SLinus Torvalds 		if (tp->mdev > tp->mdev_max) {
6831da177e4SLinus Torvalds 			tp->mdev_max = tp->mdev;
6841da177e4SLinus Torvalds 			if (tp->mdev_max > tp->rttvar)
6851da177e4SLinus Torvalds 				tp->rttvar = tp->mdev_max;
6861da177e4SLinus Torvalds 		}
6871da177e4SLinus Torvalds 		if (after(tp->snd_una, tp->rtt_seq)) {
6881da177e4SLinus Torvalds 			if (tp->mdev_max < tp->rttvar)
6891da177e4SLinus Torvalds 				tp->rttvar -= (tp->rttvar - tp->mdev_max) >> 2;
6901da177e4SLinus Torvalds 			tp->rtt_seq = tp->snd_nxt;
69105bb1fadSDavid S. Miller 			tp->mdev_max = tcp_rto_min(sk);
6921da177e4SLinus Torvalds 		}
6931da177e4SLinus Torvalds 	} else {
6941da177e4SLinus Torvalds 		/* no previous measure. */
6951da177e4SLinus Torvalds 		tp->srtt = m << 3;	/* take the measured time to be rtt */
6961da177e4SLinus Torvalds 		tp->mdev = m << 1;	/* make sure rto = 3*rtt */
69705bb1fadSDavid S. Miller 		tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
6981da177e4SLinus Torvalds 		tp->rtt_seq = tp->snd_nxt;
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds /* Calculate rto without backoff.  This is the second half of Van Jacobson's
7031da177e4SLinus Torvalds  * routine referred to above.
7041da177e4SLinus Torvalds  */
7054aabd8efSDavid S. Miller void tcp_set_rto(struct sock *sk)
7061da177e4SLinus Torvalds {
707463c84b9SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
7081da177e4SLinus Torvalds 	/* Old crap is replaced with new one. 8)
7091da177e4SLinus Torvalds 	 *
7101da177e4SLinus Torvalds 	 * More seriously:
7111da177e4SLinus Torvalds 	 * 1. If rtt variance happened to be less 50msec, it is hallucination.
7121da177e4SLinus Torvalds 	 *    It cannot be less due to utterly erratic ACK generation made
7131da177e4SLinus Torvalds 	 *    at least by solaris and freebsd. "Erratic ACKs" has _nothing_
7141da177e4SLinus Torvalds 	 *    to do with delayed acks, because at cwnd>2 true delack timeout
7151da177e4SLinus Torvalds 	 *    is invisible. Actually, Linux-2.4 also generates erratic
716caa20d9aSStephen Hemminger 	 *    ACKs in some circumstances.
7171da177e4SLinus Torvalds 	 */
718f1ecd5d9SDamian Lukowski 	inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds 	/* 2. Fixups made earlier cannot be right.
7211da177e4SLinus Torvalds 	 *    If we do not estimate RTO correctly without them,
7221da177e4SLinus Torvalds 	 *    all the algo is pure shit and should be replaced
723caa20d9aSStephen Hemminger 	 *    with correct one. It is exactly, which we pretend to do.
7241da177e4SLinus Torvalds 	 */
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	/* NOTE: clamping at TCP_RTO_MIN is not required, current algo
7271da177e4SLinus Torvalds 	 * guarantees that rto is higher.
7281da177e4SLinus Torvalds 	 */
729f1ecd5d9SDamian Lukowski 	tcp_bound_rto(sk);
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
732cf533ea5SEric Dumazet __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)
7331da177e4SLinus Torvalds {
7341da177e4SLinus Torvalds 	__u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
7351da177e4SLinus Torvalds 
73622b71c8fSGerrit Renker 	if (!cwnd)
737442b9635SDavid S. Miller 		cwnd = TCP_INIT_CWND;
7381da177e4SLinus Torvalds 	return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
741e60402d0SIlpo Järvinen /*
742e60402d0SIlpo Järvinen  * Packet counting of FACK is based on in-order assumptions, therefore TCP
743e60402d0SIlpo Järvinen  * disables it when reordering is detected
744e60402d0SIlpo Järvinen  */
7454aabd8efSDavid S. Miller void tcp_disable_fack(struct tcp_sock *tp)
746e60402d0SIlpo Järvinen {
74785cc391cSIlpo Järvinen 	/* RFC3517 uses different metric in lost marker => reset on change */
74885cc391cSIlpo Järvinen 	if (tcp_is_fack(tp))
74985cc391cSIlpo Järvinen 		tp->lost_skb_hint = NULL;
750ab56222aSVijay Subramanian 	tp->rx_opt.sack_ok &= ~TCP_FACK_ENABLED;
751e60402d0SIlpo Järvinen }
752e60402d0SIlpo Järvinen 
753564262c1SRyousei Takano /* Take a notice that peer is sending D-SACKs */
754e60402d0SIlpo Järvinen static void tcp_dsack_seen(struct tcp_sock *tp)
755e60402d0SIlpo Järvinen {
756ab56222aSVijay Subramanian 	tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;
757e60402d0SIlpo Järvinen }
758e60402d0SIlpo Järvinen 
7596687e988SArnaldo Carvalho de Melo static void tcp_update_reordering(struct sock *sk, const int metric,
7606687e988SArnaldo Carvalho de Melo 				  const int ts)
7611da177e4SLinus Torvalds {
7626687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
7631da177e4SLinus Torvalds 	if (metric > tp->reordering) {
76440b215e5SPavel Emelyanov 		int mib_idx;
76540b215e5SPavel Emelyanov 
7661da177e4SLinus Torvalds 		tp->reordering = min(TCP_MAX_REORDERING, metric);
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 		/* This exciting event is worth to be remembered. 8) */
7691da177e4SLinus Torvalds 		if (ts)
77040b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPTSREORDER;
771e60402d0SIlpo Järvinen 		else if (tcp_is_reno(tp))
77240b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPRENOREORDER;
773e60402d0SIlpo Järvinen 		else if (tcp_is_fack(tp))
77440b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPFACKREORDER;
7751da177e4SLinus Torvalds 		else
77640b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPSACKREORDER;
77740b215e5SPavel Emelyanov 
778de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), mib_idx);
7791da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 1
78091df42beSJoe Perches 		pr_debug("Disorder%d %d %u f%u s%u rr%d\n",
7816687e988SArnaldo Carvalho de Melo 			 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
7821da177e4SLinus Torvalds 			 tp->reordering,
7831da177e4SLinus Torvalds 			 tp->fackets_out,
7841da177e4SLinus Torvalds 			 tp->sacked_out,
7851da177e4SLinus Torvalds 			 tp->undo_marker ? tp->undo_retrans : 0);
7861da177e4SLinus Torvalds #endif
787e60402d0SIlpo Järvinen 		tcp_disable_fack(tp);
7881da177e4SLinus Torvalds 	}
789eed530b6SYuchung Cheng 
790eed530b6SYuchung Cheng 	if (metric > 0)
791eed530b6SYuchung Cheng 		tcp_disable_early_retrans(tp);
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
794006f582cSIlpo Järvinen /* This must be called before lost_out is incremented */
795c8c213f2SIlpo Järvinen static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
796c8c213f2SIlpo Järvinen {
797006f582cSIlpo Järvinen 	if ((tp->retransmit_skb_hint == NULL) ||
798c8c213f2SIlpo Järvinen 	    before(TCP_SKB_CB(skb)->seq,
799c8c213f2SIlpo Järvinen 		   TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
800006f582cSIlpo Järvinen 		tp->retransmit_skb_hint = skb;
801006f582cSIlpo Järvinen 
802006f582cSIlpo Järvinen 	if (!tp->lost_out ||
803006f582cSIlpo Järvinen 	    after(TCP_SKB_CB(skb)->end_seq, tp->retransmit_high))
804006f582cSIlpo Järvinen 		tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
805c8c213f2SIlpo Järvinen }
806c8c213f2SIlpo Järvinen 
80741ea36e3SIlpo Järvinen static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb)
80841ea36e3SIlpo Järvinen {
80941ea36e3SIlpo Järvinen 	if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
81041ea36e3SIlpo Järvinen 		tcp_verify_retransmit_hint(tp, skb);
81141ea36e3SIlpo Järvinen 
81241ea36e3SIlpo Järvinen 		tp->lost_out += tcp_skb_pcount(skb);
81341ea36e3SIlpo Järvinen 		TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
81441ea36e3SIlpo Järvinen 	}
81541ea36e3SIlpo Järvinen }
81641ea36e3SIlpo Järvinen 
817e1aa680fSIlpo Järvinen static void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp,
818e1aa680fSIlpo Järvinen 					    struct sk_buff *skb)
819006f582cSIlpo Järvinen {
820006f582cSIlpo Järvinen 	tcp_verify_retransmit_hint(tp, skb);
821006f582cSIlpo Järvinen 
822006f582cSIlpo Järvinen 	if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
823006f582cSIlpo Järvinen 		tp->lost_out += tcp_skb_pcount(skb);
824006f582cSIlpo Järvinen 		TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
825006f582cSIlpo Järvinen 	}
826006f582cSIlpo Järvinen }
827006f582cSIlpo Järvinen 
8281da177e4SLinus Torvalds /* This procedure tags the retransmission queue when SACKs arrive.
8291da177e4SLinus Torvalds  *
8301da177e4SLinus Torvalds  * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
8311da177e4SLinus Torvalds  * Packets in queue with these bits set are counted in variables
8321da177e4SLinus Torvalds  * sacked_out, retrans_out and lost_out, correspondingly.
8331da177e4SLinus Torvalds  *
8341da177e4SLinus Torvalds  * Valid combinations are:
8351da177e4SLinus Torvalds  * Tag  InFlight	Description
8361da177e4SLinus Torvalds  * 0	1		- orig segment is in flight.
8371da177e4SLinus Torvalds  * S	0		- nothing flies, orig reached receiver.
8381da177e4SLinus Torvalds  * L	0		- nothing flies, orig lost by net.
8391da177e4SLinus Torvalds  * R	2		- both orig and retransmit are in flight.
8401da177e4SLinus Torvalds  * L|R	1		- orig is lost, retransmit is in flight.
8411da177e4SLinus Torvalds  * S|R  1		- orig reached receiver, retrans is still in flight.
8421da177e4SLinus Torvalds  * (L|S|R is logically valid, it could occur when L|R is sacked,
8431da177e4SLinus Torvalds  *  but it is equivalent to plain S and code short-curcuits it to S.
8441da177e4SLinus Torvalds  *  L|S is logically invalid, it would mean -1 packet in flight 8))
8451da177e4SLinus Torvalds  *
8461da177e4SLinus Torvalds  * These 6 states form finite state machine, controlled by the following events:
8471da177e4SLinus Torvalds  * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
8481da177e4SLinus Torvalds  * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
849974c1236SYuchung Cheng  * 3. Loss detection event of two flavors:
8501da177e4SLinus Torvalds  *	A. Scoreboard estimator decided the packet is lost.
8511da177e4SLinus Torvalds  *	   A'. Reno "three dupacks" marks head of queue lost.
852974c1236SYuchung Cheng  *	   A''. Its FACK modification, head until snd.fack is lost.
853974c1236SYuchung Cheng  *	B. SACK arrives sacking SND.NXT at the moment, when the
8541da177e4SLinus Torvalds  *	   segment was retransmitted.
8551da177e4SLinus Torvalds  * 4. D-SACK added new rule: D-SACK changes any tag to S.
8561da177e4SLinus Torvalds  *
8571da177e4SLinus Torvalds  * It is pleasant to note, that state diagram turns out to be commutative,
8581da177e4SLinus Torvalds  * so that we are allowed not to be bothered by order of our actions,
8591da177e4SLinus Torvalds  * when multiple events arrive simultaneously. (see the function below).
8601da177e4SLinus Torvalds  *
8611da177e4SLinus Torvalds  * Reordering detection.
8621da177e4SLinus Torvalds  * --------------------
8631da177e4SLinus Torvalds  * Reordering metric is maximal distance, which a packet can be displaced
8641da177e4SLinus Torvalds  * in packet stream. With SACKs we can estimate it:
8651da177e4SLinus Torvalds  *
8661da177e4SLinus Torvalds  * 1. SACK fills old hole and the corresponding segment was not
8671da177e4SLinus Torvalds  *    ever retransmitted -> reordering. Alas, we cannot use it
8681da177e4SLinus Torvalds  *    when segment was retransmitted.
8691da177e4SLinus Torvalds  * 2. The last flaw is solved with D-SACK. D-SACK arrives
8701da177e4SLinus Torvalds  *    for retransmitted and already SACKed segment -> reordering..
8711da177e4SLinus Torvalds  * Both of these heuristics are not used in Loss state, when we cannot
8721da177e4SLinus Torvalds  * account for retransmits accurately.
8735b3c9882SIlpo Järvinen  *
8745b3c9882SIlpo Järvinen  * SACK block validation.
8755b3c9882SIlpo Järvinen  * ----------------------
8765b3c9882SIlpo Järvinen  *
8775b3c9882SIlpo Järvinen  * SACK block range validation checks that the received SACK block fits to
8785b3c9882SIlpo Järvinen  * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
8795b3c9882SIlpo Järvinen  * Note that SND.UNA is not included to the range though being valid because
8800e835331SIlpo Järvinen  * it means that the receiver is rather inconsistent with itself reporting
8810e835331SIlpo Järvinen  * SACK reneging when it should advance SND.UNA. Such SACK block this is
8820e835331SIlpo Järvinen  * perfectly valid, however, in light of RFC2018 which explicitly states
8830e835331SIlpo Järvinen  * that "SACK block MUST reflect the newest segment.  Even if the newest
8840e835331SIlpo Järvinen  * segment is going to be discarded ...", not that it looks very clever
8850e835331SIlpo Järvinen  * in case of head skb. Due to potentional receiver driven attacks, we
8860e835331SIlpo Järvinen  * choose to avoid immediate execution of a walk in write queue due to
8870e835331SIlpo Järvinen  * reneging and defer head skb's loss recovery to standard loss recovery
8880e835331SIlpo Järvinen  * procedure that will eventually trigger (nothing forbids us doing this).
8895b3c9882SIlpo Järvinen  *
8905b3c9882SIlpo Järvinen  * Implements also blockage to start_seq wrap-around. Problem lies in the
8915b3c9882SIlpo Järvinen  * fact that though start_seq (s) is before end_seq (i.e., not reversed),
8925b3c9882SIlpo Järvinen  * there's no guarantee that it will be before snd_nxt (n). The problem
8935b3c9882SIlpo Järvinen  * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
8945b3c9882SIlpo Järvinen  * wrap (s_w):
8955b3c9882SIlpo Järvinen  *
8965b3c9882SIlpo Järvinen  *         <- outs wnd ->                          <- wrapzone ->
8975b3c9882SIlpo Järvinen  *         u     e      n                         u_w   e_w  s n_w
8985b3c9882SIlpo Järvinen  *         |     |      |                          |     |   |  |
8995b3c9882SIlpo Järvinen  * |<------------+------+----- TCP seqno space --------------+---------->|
9005b3c9882SIlpo Järvinen  * ...-- <2^31 ->|                                           |<--------...
9015b3c9882SIlpo Järvinen  * ...---- >2^31 ------>|                                    |<--------...
9025b3c9882SIlpo Järvinen  *
9035b3c9882SIlpo Järvinen  * Current code wouldn't be vulnerable but it's better still to discard such
9045b3c9882SIlpo Järvinen  * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
9055b3c9882SIlpo Järvinen  * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
9065b3c9882SIlpo Järvinen  * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
9075b3c9882SIlpo Järvinen  * equal to the ideal case (infinite seqno space without wrap caused issues).
9085b3c9882SIlpo Järvinen  *
9095b3c9882SIlpo Järvinen  * With D-SACK the lower bound is extended to cover sequence space below
9105b3c9882SIlpo Järvinen  * SND.UNA down to undo_marker, which is the last point of interest. Yet
911564262c1SRyousei Takano  * again, D-SACK block must not to go across snd_una (for the same reason as
9125b3c9882SIlpo Järvinen  * for the normal SACK blocks, explained above). But there all simplicity
9135b3c9882SIlpo Järvinen  * ends, TCP might receive valid D-SACKs below that. As long as they reside
9145b3c9882SIlpo Järvinen  * fully below undo_marker they do not affect behavior in anyway and can
9155b3c9882SIlpo Järvinen  * therefore be safely ignored. In rare cases (which are more or less
9165b3c9882SIlpo Järvinen  * theoretical ones), the D-SACK will nicely cross that boundary due to skb
9175b3c9882SIlpo Järvinen  * fragmentation and packet reordering past skb's retransmission. To consider
9185b3c9882SIlpo Järvinen  * them correctly, the acceptable range must be extended even more though
9195b3c9882SIlpo Järvinen  * the exact amount is rather hard to quantify. However, tp->max_window can
9205b3c9882SIlpo Järvinen  * be used as an exaggerated estimate.
9211da177e4SLinus Torvalds  */
922a2a385d6SEric Dumazet static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack,
9235b3c9882SIlpo Järvinen 				   u32 start_seq, u32 end_seq)
9245b3c9882SIlpo Järvinen {
9255b3c9882SIlpo Järvinen 	/* Too far in future, or reversed (interpretation is ambiguous) */
9265b3c9882SIlpo Järvinen 	if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
927a2a385d6SEric Dumazet 		return false;
9285b3c9882SIlpo Järvinen 
9295b3c9882SIlpo Järvinen 	/* Nasty start_seq wrap-around check (see comments above) */
9305b3c9882SIlpo Järvinen 	if (!before(start_seq, tp->snd_nxt))
931a2a385d6SEric Dumazet 		return false;
9325b3c9882SIlpo Järvinen 
933564262c1SRyousei Takano 	/* In outstanding window? ...This is valid exit for D-SACKs too.
9345b3c9882SIlpo Järvinen 	 * start_seq == snd_una is non-sensical (see comments above)
9355b3c9882SIlpo Järvinen 	 */
9365b3c9882SIlpo Järvinen 	if (after(start_seq, tp->snd_una))
937a2a385d6SEric Dumazet 		return true;
9385b3c9882SIlpo Järvinen 
9395b3c9882SIlpo Järvinen 	if (!is_dsack || !tp->undo_marker)
940a2a385d6SEric Dumazet 		return false;
9415b3c9882SIlpo Järvinen 
9425b3c9882SIlpo Järvinen 	/* ...Then it's D-SACK, and must reside below snd_una completely */
943f779b2d6SZheng Yan 	if (after(end_seq, tp->snd_una))
944a2a385d6SEric Dumazet 		return false;
9455b3c9882SIlpo Järvinen 
9465b3c9882SIlpo Järvinen 	if (!before(start_seq, tp->undo_marker))
947a2a385d6SEric Dumazet 		return true;
9485b3c9882SIlpo Järvinen 
9495b3c9882SIlpo Järvinen 	/* Too old */
9505b3c9882SIlpo Järvinen 	if (!after(end_seq, tp->undo_marker))
951a2a385d6SEric Dumazet 		return false;
9525b3c9882SIlpo Järvinen 
9535b3c9882SIlpo Järvinen 	/* Undo_marker boundary crossing (overestimates a lot). Known already:
9545b3c9882SIlpo Järvinen 	 *   start_seq < undo_marker and end_seq >= undo_marker.
9555b3c9882SIlpo Järvinen 	 */
9565b3c9882SIlpo Järvinen 	return !before(start_seq, end_seq - tp->max_window);
9575b3c9882SIlpo Järvinen }
9585b3c9882SIlpo Järvinen 
9591c1e87edSIlpo Järvinen /* Check for lost retransmit. This superb idea is borrowed from "ratehalving".
960974c1236SYuchung Cheng  * Event "B". Later note: FACK people cheated me again 8), we have to account
9611c1e87edSIlpo Järvinen  * for reordering! Ugly, but should help.
962f785a8e2SIlpo Järvinen  *
963f785a8e2SIlpo Järvinen  * Search retransmitted skbs from write_queue that were sent when snd_nxt was
964f785a8e2SIlpo Järvinen  * less than what is now known to be received by the other end (derived from
9659f58f3b7SIlpo Järvinen  * highest SACK block). Also calculate the lowest snd_nxt among the remaining
9669f58f3b7SIlpo Järvinen  * retransmitted skbs to avoid some costly processing per ACKs.
9671c1e87edSIlpo Järvinen  */
968407ef1deSIlpo Järvinen static void tcp_mark_lost_retrans(struct sock *sk)
9691c1e87edSIlpo Järvinen {
9709f58f3b7SIlpo Järvinen 	const struct inet_connection_sock *icsk = inet_csk(sk);
9711c1e87edSIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
9721c1e87edSIlpo Järvinen 	struct sk_buff *skb;
973f785a8e2SIlpo Järvinen 	int cnt = 0;
974df2e014bSIlpo Järvinen 	u32 new_low_seq = tp->snd_nxt;
9756859d494SIlpo Järvinen 	u32 received_upto = tcp_highest_sack_seq(tp);
9769f58f3b7SIlpo Järvinen 
9779f58f3b7SIlpo Järvinen 	if (!tcp_is_fack(tp) || !tp->retrans_out ||
9789f58f3b7SIlpo Järvinen 	    !after(received_upto, tp->lost_retrans_low) ||
9799f58f3b7SIlpo Järvinen 	    icsk->icsk_ca_state != TCP_CA_Recovery)
980407ef1deSIlpo Järvinen 		return;
9811c1e87edSIlpo Järvinen 
9821c1e87edSIlpo Järvinen 	tcp_for_write_queue(skb, sk) {
9831c1e87edSIlpo Järvinen 		u32 ack_seq = TCP_SKB_CB(skb)->ack_seq;
9841c1e87edSIlpo Järvinen 
9851c1e87edSIlpo Järvinen 		if (skb == tcp_send_head(sk))
9861c1e87edSIlpo Järvinen 			break;
987f785a8e2SIlpo Järvinen 		if (cnt == tp->retrans_out)
9881c1e87edSIlpo Järvinen 			break;
9891c1e87edSIlpo Järvinen 		if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
9901c1e87edSIlpo Järvinen 			continue;
9911c1e87edSIlpo Järvinen 
992f785a8e2SIlpo Järvinen 		if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS))
993f785a8e2SIlpo Järvinen 			continue;
994f785a8e2SIlpo Järvinen 
995d0af4160SIlpo Järvinen 		/* TODO: We would like to get rid of tcp_is_fack(tp) only
996d0af4160SIlpo Järvinen 		 * constraint here (see above) but figuring out that at
997d0af4160SIlpo Järvinen 		 * least tp->reordering SACK blocks reside between ack_seq
998d0af4160SIlpo Järvinen 		 * and received_upto is not easy task to do cheaply with
999d0af4160SIlpo Järvinen 		 * the available datastructures.
1000d0af4160SIlpo Järvinen 		 *
1001d0af4160SIlpo Järvinen 		 * Whether FACK should check here for tp->reordering segs
1002d0af4160SIlpo Järvinen 		 * in-between one could argue for either way (it would be
1003d0af4160SIlpo Järvinen 		 * rather simple to implement as we could count fack_count
1004d0af4160SIlpo Järvinen 		 * during the walk and do tp->fackets_out - fack_count).
1005d0af4160SIlpo Järvinen 		 */
1006d0af4160SIlpo Järvinen 		if (after(received_upto, ack_seq)) {
10071c1e87edSIlpo Järvinen 			TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
10081c1e87edSIlpo Järvinen 			tp->retrans_out -= tcp_skb_pcount(skb);
10091c1e87edSIlpo Järvinen 
1010006f582cSIlpo Järvinen 			tcp_skb_mark_lost_uncond_verify(tp, skb);
1011de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT);
1012f785a8e2SIlpo Järvinen 		} else {
1013df2e014bSIlpo Järvinen 			if (before(ack_seq, new_low_seq))
1014b08d6cb2SIlpo Järvinen 				new_low_seq = ack_seq;
1015f785a8e2SIlpo Järvinen 			cnt += tcp_skb_pcount(skb);
10161c1e87edSIlpo Järvinen 		}
10171c1e87edSIlpo Järvinen 	}
1018b08d6cb2SIlpo Järvinen 
1019b08d6cb2SIlpo Järvinen 	if (tp->retrans_out)
1020b08d6cb2SIlpo Järvinen 		tp->lost_retrans_low = new_low_seq;
10211c1e87edSIlpo Järvinen }
10225b3c9882SIlpo Järvinen 
1023a2a385d6SEric Dumazet static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb,
1024d06e021dSDavid S. Miller 			    struct tcp_sack_block_wire *sp, int num_sacks,
1025d06e021dSDavid S. Miller 			    u32 prior_snd_una)
1026d06e021dSDavid S. Miller {
10271ed83465SPavel Emelyanov 	struct tcp_sock *tp = tcp_sk(sk);
1028d3e2ce3bSHarvey Harrison 	u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
1029d3e2ce3bSHarvey Harrison 	u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
1030a2a385d6SEric Dumazet 	bool dup_sack = false;
1031d06e021dSDavid S. Miller 
1032d06e021dSDavid S. Miller 	if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
1033a2a385d6SEric Dumazet 		dup_sack = true;
1034e60402d0SIlpo Järvinen 		tcp_dsack_seen(tp);
1035de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
1036d06e021dSDavid S. Miller 	} else if (num_sacks > 1) {
1037d3e2ce3bSHarvey Harrison 		u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
1038d3e2ce3bSHarvey Harrison 		u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
1039d06e021dSDavid S. Miller 
1040d06e021dSDavid S. Miller 		if (!after(end_seq_0, end_seq_1) &&
1041d06e021dSDavid S. Miller 		    !before(start_seq_0, start_seq_1)) {
1042a2a385d6SEric Dumazet 			dup_sack = true;
1043e60402d0SIlpo Järvinen 			tcp_dsack_seen(tp);
1044de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk),
1045de0744afSPavel Emelyanov 					LINUX_MIB_TCPDSACKOFORECV);
1046d06e021dSDavid S. Miller 		}
1047d06e021dSDavid S. Miller 	}
1048d06e021dSDavid S. Miller 
1049d06e021dSDavid S. Miller 	/* D-SACK for already forgotten data... Do dumb counting. */
1050c24f691bSYuchung Cheng 	if (dup_sack && tp->undo_marker && tp->undo_retrans &&
1051d06e021dSDavid S. Miller 	    !after(end_seq_0, prior_snd_una) &&
1052d06e021dSDavid S. Miller 	    after(end_seq_0, tp->undo_marker))
1053d06e021dSDavid S. Miller 		tp->undo_retrans--;
1054d06e021dSDavid S. Miller 
1055d06e021dSDavid S. Miller 	return dup_sack;
1056d06e021dSDavid S. Miller }
1057d06e021dSDavid S. Miller 
1058a1197f5aSIlpo Järvinen struct tcp_sacktag_state {
1059a1197f5aSIlpo Järvinen 	int reord;
1060a1197f5aSIlpo Järvinen 	int fack_count;
1061a1197f5aSIlpo Järvinen 	int flag;
1062a1197f5aSIlpo Järvinen };
1063a1197f5aSIlpo Järvinen 
1064d1935942SIlpo Järvinen /* Check if skb is fully within the SACK block. In presence of GSO skbs,
1065d1935942SIlpo Järvinen  * the incoming SACK may not exactly match but we can find smaller MSS
1066d1935942SIlpo Järvinen  * aligned portion of it that matches. Therefore we might need to fragment
1067d1935942SIlpo Järvinen  * which may fail and creates some hassle (caller must handle error case
1068d1935942SIlpo Järvinen  * returns).
1069832d11c5SIlpo Järvinen  *
1070832d11c5SIlpo Järvinen  * FIXME: this could be merged to shift decision code
1071d1935942SIlpo Järvinen  */
10720f79efdcSAdrian Bunk static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
1073d1935942SIlpo Järvinen 				  u32 start_seq, u32 end_seq)
1074d1935942SIlpo Järvinen {
1075a2a385d6SEric Dumazet 	int err;
1076a2a385d6SEric Dumazet 	bool in_sack;
1077d1935942SIlpo Järvinen 	unsigned int pkt_len;
1078adb92db8SIlpo Järvinen 	unsigned int mss;
1079d1935942SIlpo Järvinen 
1080d1935942SIlpo Järvinen 	in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1081d1935942SIlpo Järvinen 		  !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1082d1935942SIlpo Järvinen 
1083d1935942SIlpo Järvinen 	if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1084d1935942SIlpo Järvinen 	    after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
1085adb92db8SIlpo Järvinen 		mss = tcp_skb_mss(skb);
1086d1935942SIlpo Järvinen 		in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1087d1935942SIlpo Järvinen 
1088adb92db8SIlpo Järvinen 		if (!in_sack) {
1089d1935942SIlpo Järvinen 			pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
1090adb92db8SIlpo Järvinen 			if (pkt_len < mss)
1091adb92db8SIlpo Järvinen 				pkt_len = mss;
1092adb92db8SIlpo Järvinen 		} else {
1093d1935942SIlpo Järvinen 			pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
1094adb92db8SIlpo Järvinen 			if (pkt_len < mss)
1095adb92db8SIlpo Järvinen 				return -EINVAL;
1096adb92db8SIlpo Järvinen 		}
1097adb92db8SIlpo Järvinen 
1098adb92db8SIlpo Järvinen 		/* Round if necessary so that SACKs cover only full MSSes
1099adb92db8SIlpo Järvinen 		 * and/or the remaining small portion (if present)
1100adb92db8SIlpo Järvinen 		 */
1101adb92db8SIlpo Järvinen 		if (pkt_len > mss) {
1102adb92db8SIlpo Järvinen 			unsigned int new_len = (pkt_len / mss) * mss;
1103adb92db8SIlpo Järvinen 			if (!in_sack && new_len < pkt_len) {
1104adb92db8SIlpo Järvinen 				new_len += mss;
1105adb92db8SIlpo Järvinen 				if (new_len > skb->len)
1106adb92db8SIlpo Järvinen 					return 0;
1107adb92db8SIlpo Järvinen 			}
1108adb92db8SIlpo Järvinen 			pkt_len = new_len;
1109adb92db8SIlpo Järvinen 		}
1110adb92db8SIlpo Järvinen 		err = tcp_fragment(sk, skb, pkt_len, mss);
1111d1935942SIlpo Järvinen 		if (err < 0)
1112d1935942SIlpo Järvinen 			return err;
1113d1935942SIlpo Järvinen 	}
1114d1935942SIlpo Järvinen 
1115d1935942SIlpo Järvinen 	return in_sack;
1116d1935942SIlpo Järvinen }
1117d1935942SIlpo Järvinen 
1118cc9a672eSNeal Cardwell /* Mark the given newly-SACKed range as such, adjusting counters and hints. */
1119cc9a672eSNeal Cardwell static u8 tcp_sacktag_one(struct sock *sk,
1120cc9a672eSNeal Cardwell 			  struct tcp_sacktag_state *state, u8 sacked,
1121cc9a672eSNeal Cardwell 			  u32 start_seq, u32 end_seq,
1122a2a385d6SEric Dumazet 			  bool dup_sack, int pcount)
11239e10c47cSIlpo Järvinen {
11246859d494SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
1125a1197f5aSIlpo Järvinen 	int fack_count = state->fack_count;
11269e10c47cSIlpo Järvinen 
11279e10c47cSIlpo Järvinen 	/* Account D-SACK for retransmitted packet. */
11289e10c47cSIlpo Järvinen 	if (dup_sack && (sacked & TCPCB_RETRANS)) {
1129c24f691bSYuchung Cheng 		if (tp->undo_marker && tp->undo_retrans &&
1130cc9a672eSNeal Cardwell 		    after(end_seq, tp->undo_marker))
11319e10c47cSIlpo Järvinen 			tp->undo_retrans--;
1132ede9f3b1SIlpo Järvinen 		if (sacked & TCPCB_SACKED_ACKED)
1133a1197f5aSIlpo Järvinen 			state->reord = min(fack_count, state->reord);
11349e10c47cSIlpo Järvinen 	}
11359e10c47cSIlpo Järvinen 
11369e10c47cSIlpo Järvinen 	/* Nothing to do; acked frame is about to be dropped (was ACKed). */
1137cc9a672eSNeal Cardwell 	if (!after(end_seq, tp->snd_una))
1138a1197f5aSIlpo Järvinen 		return sacked;
11399e10c47cSIlpo Järvinen 
11409e10c47cSIlpo Järvinen 	if (!(sacked & TCPCB_SACKED_ACKED)) {
11419e10c47cSIlpo Järvinen 		if (sacked & TCPCB_SACKED_RETRANS) {
11429e10c47cSIlpo Järvinen 			/* If the segment is not tagged as lost,
11439e10c47cSIlpo Järvinen 			 * we do not clear RETRANS, believing
11449e10c47cSIlpo Järvinen 			 * that retransmission is still in flight.
11459e10c47cSIlpo Järvinen 			 */
11469e10c47cSIlpo Järvinen 			if (sacked & TCPCB_LOST) {
1147a1197f5aSIlpo Järvinen 				sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
1148f58b22fdSIlpo Järvinen 				tp->lost_out -= pcount;
1149f58b22fdSIlpo Järvinen 				tp->retrans_out -= pcount;
11509e10c47cSIlpo Järvinen 			}
11519e10c47cSIlpo Järvinen 		} else {
11529e10c47cSIlpo Järvinen 			if (!(sacked & TCPCB_RETRANS)) {
11539e10c47cSIlpo Järvinen 				/* New sack for not retransmitted frame,
11549e10c47cSIlpo Järvinen 				 * which was in hole. It is reordering.
11559e10c47cSIlpo Järvinen 				 */
1156cc9a672eSNeal Cardwell 				if (before(start_seq,
11579e10c47cSIlpo Järvinen 					   tcp_highest_sack_seq(tp)))
1158a1197f5aSIlpo Järvinen 					state->reord = min(fack_count,
1159a1197f5aSIlpo Järvinen 							   state->reord);
1160e33099f9SYuchung Cheng 				if (!after(end_seq, tp->high_seq))
1161e33099f9SYuchung Cheng 					state->flag |= FLAG_ORIG_SACK_ACKED;
11629e10c47cSIlpo Järvinen 			}
11639e10c47cSIlpo Järvinen 
11649e10c47cSIlpo Järvinen 			if (sacked & TCPCB_LOST) {
1165a1197f5aSIlpo Järvinen 				sacked &= ~TCPCB_LOST;
1166f58b22fdSIlpo Järvinen 				tp->lost_out -= pcount;
11679e10c47cSIlpo Järvinen 			}
11689e10c47cSIlpo Järvinen 		}
11699e10c47cSIlpo Järvinen 
1170a1197f5aSIlpo Järvinen 		sacked |= TCPCB_SACKED_ACKED;
1171a1197f5aSIlpo Järvinen 		state->flag |= FLAG_DATA_SACKED;
1172f58b22fdSIlpo Järvinen 		tp->sacked_out += pcount;
11739e10c47cSIlpo Järvinen 
1174f58b22fdSIlpo Järvinen 		fack_count += pcount;
11759e10c47cSIlpo Järvinen 
11769e10c47cSIlpo Järvinen 		/* Lost marker hint past SACKed? Tweak RFC3517 cnt */
11779e10c47cSIlpo Järvinen 		if (!tcp_is_fack(tp) && (tp->lost_skb_hint != NULL) &&
1178cc9a672eSNeal Cardwell 		    before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))
1179f58b22fdSIlpo Järvinen 			tp->lost_cnt_hint += pcount;
11809e10c47cSIlpo Järvinen 
11819e10c47cSIlpo Järvinen 		if (fack_count > tp->fackets_out)
11829e10c47cSIlpo Järvinen 			tp->fackets_out = fack_count;
11839e10c47cSIlpo Järvinen 	}
11849e10c47cSIlpo Järvinen 
11859e10c47cSIlpo Järvinen 	/* D-SACK. We can detect redundant retransmission in S|R and plain R
11869e10c47cSIlpo Järvinen 	 * frames and clear it. undo_retrans is decreased above, L|R frames
11879e10c47cSIlpo Järvinen 	 * are accounted above as well.
11889e10c47cSIlpo Järvinen 	 */
1189a1197f5aSIlpo Järvinen 	if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
1190a1197f5aSIlpo Järvinen 		sacked &= ~TCPCB_SACKED_RETRANS;
1191f58b22fdSIlpo Järvinen 		tp->retrans_out -= pcount;
11929e10c47cSIlpo Järvinen 	}
11939e10c47cSIlpo Järvinen 
1194a1197f5aSIlpo Järvinen 	return sacked;
11959e10c47cSIlpo Järvinen }
11969e10c47cSIlpo Järvinen 
1197daef52baSNeal Cardwell /* Shift newly-SACKed bytes from this skb to the immediately previous
1198daef52baSNeal Cardwell  * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
1199daef52baSNeal Cardwell  */
1200a2a385d6SEric Dumazet static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,
1201a1197f5aSIlpo Järvinen 			    struct tcp_sacktag_state *state,
12029ec06ff5SIlpo Järvinen 			    unsigned int pcount, int shifted, int mss,
1203a2a385d6SEric Dumazet 			    bool dup_sack)
1204832d11c5SIlpo Järvinen {
1205832d11c5SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
120650133161SIlpo Järvinen 	struct sk_buff *prev = tcp_write_queue_prev(sk, skb);
1207daef52baSNeal Cardwell 	u32 start_seq = TCP_SKB_CB(skb)->seq;	/* start of newly-SACKed */
1208daef52baSNeal Cardwell 	u32 end_seq = start_seq + shifted;	/* end of newly-SACKed */
1209832d11c5SIlpo Järvinen 
1210832d11c5SIlpo Järvinen 	BUG_ON(!pcount);
1211832d11c5SIlpo Järvinen 
12124c90d3b3SNeal Cardwell 	/* Adjust counters and hints for the newly sacked sequence
12134c90d3b3SNeal Cardwell 	 * range but discard the return value since prev is already
12144c90d3b3SNeal Cardwell 	 * marked. We must tag the range first because the seq
12154c90d3b3SNeal Cardwell 	 * advancement below implicitly advances
12164c90d3b3SNeal Cardwell 	 * tcp_highest_sack_seq() when skb is highest_sack.
12174c90d3b3SNeal Cardwell 	 */
12184c90d3b3SNeal Cardwell 	tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked,
12194c90d3b3SNeal Cardwell 			start_seq, end_seq, dup_sack, pcount);
12204c90d3b3SNeal Cardwell 
12214c90d3b3SNeal Cardwell 	if (skb == tp->lost_skb_hint)
12220af2a0d0SNeal Cardwell 		tp->lost_cnt_hint += pcount;
12230af2a0d0SNeal Cardwell 
1224832d11c5SIlpo Järvinen 	TCP_SKB_CB(prev)->end_seq += shifted;
1225832d11c5SIlpo Järvinen 	TCP_SKB_CB(skb)->seq += shifted;
1226832d11c5SIlpo Järvinen 
1227832d11c5SIlpo Järvinen 	skb_shinfo(prev)->gso_segs += pcount;
1228832d11c5SIlpo Järvinen 	BUG_ON(skb_shinfo(skb)->gso_segs < pcount);
1229832d11c5SIlpo Järvinen 	skb_shinfo(skb)->gso_segs -= pcount;
1230832d11c5SIlpo Järvinen 
1231832d11c5SIlpo Järvinen 	/* When we're adding to gso_segs == 1, gso_size will be zero,
1232832d11c5SIlpo Järvinen 	 * in theory this shouldn't be necessary but as long as DSACK
1233832d11c5SIlpo Järvinen 	 * code can come after this skb later on it's better to keep
1234832d11c5SIlpo Järvinen 	 * setting gso_size to something.
1235832d11c5SIlpo Järvinen 	 */
1236832d11c5SIlpo Järvinen 	if (!skb_shinfo(prev)->gso_size) {
1237832d11c5SIlpo Järvinen 		skb_shinfo(prev)->gso_size = mss;
1238c9af6db4SPravin B Shelar 		skb_shinfo(prev)->gso_type = sk->sk_gso_type;
1239832d11c5SIlpo Järvinen 	}
1240832d11c5SIlpo Järvinen 
1241832d11c5SIlpo Järvinen 	/* CHECKME: To clear or not to clear? Mimics normal skb currently */
1242832d11c5SIlpo Järvinen 	if (skb_shinfo(skb)->gso_segs <= 1) {
1243832d11c5SIlpo Järvinen 		skb_shinfo(skb)->gso_size = 0;
1244c9af6db4SPravin B Shelar 		skb_shinfo(skb)->gso_type = 0;
1245832d11c5SIlpo Järvinen 	}
1246832d11c5SIlpo Järvinen 
1247832d11c5SIlpo Järvinen 	/* Difference in this won't matter, both ACKed by the same cumul. ACK */
1248832d11c5SIlpo Järvinen 	TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1249832d11c5SIlpo Järvinen 
1250832d11c5SIlpo Järvinen 	if (skb->len > 0) {
1251832d11c5SIlpo Järvinen 		BUG_ON(!tcp_skb_pcount(skb));
1252111cc8b9SIlpo Järvinen 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTED);
1253a2a385d6SEric Dumazet 		return false;
1254832d11c5SIlpo Järvinen 	}
1255832d11c5SIlpo Järvinen 
1256832d11c5SIlpo Järvinen 	/* Whole SKB was eaten :-) */
1257832d11c5SIlpo Järvinen 
125892ee76b6SIlpo Järvinen 	if (skb == tp->retransmit_skb_hint)
125992ee76b6SIlpo Järvinen 		tp->retransmit_skb_hint = prev;
126092ee76b6SIlpo Järvinen 	if (skb == tp->scoreboard_skb_hint)
126192ee76b6SIlpo Järvinen 		tp->scoreboard_skb_hint = prev;
126292ee76b6SIlpo Järvinen 	if (skb == tp->lost_skb_hint) {
126392ee76b6SIlpo Järvinen 		tp->lost_skb_hint = prev;
126492ee76b6SIlpo Järvinen 		tp->lost_cnt_hint -= tcp_skb_pcount(prev);
126592ee76b6SIlpo Järvinen 	}
126692ee76b6SIlpo Järvinen 
12674de075e0SEric Dumazet 	TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(prev)->tcp_flags;
1268832d11c5SIlpo Järvinen 	if (skb == tcp_highest_sack(sk))
1269832d11c5SIlpo Järvinen 		tcp_advance_highest_sack(sk, skb);
1270832d11c5SIlpo Järvinen 
1271832d11c5SIlpo Järvinen 	tcp_unlink_write_queue(skb, sk);
1272832d11c5SIlpo Järvinen 	sk_wmem_free_skb(sk, skb);
1273832d11c5SIlpo Järvinen 
1274111cc8b9SIlpo Järvinen 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKMERGED);
1275111cc8b9SIlpo Järvinen 
1276a2a385d6SEric Dumazet 	return true;
1277832d11c5SIlpo Järvinen }
1278832d11c5SIlpo Järvinen 
1279832d11c5SIlpo Järvinen /* I wish gso_size would have a bit more sane initialization than
1280832d11c5SIlpo Järvinen  * something-or-zero which complicates things
1281832d11c5SIlpo Järvinen  */
1282cf533ea5SEric Dumazet static int tcp_skb_seglen(const struct sk_buff *skb)
1283832d11c5SIlpo Järvinen {
1284775ffabfSIlpo Järvinen 	return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
1285832d11c5SIlpo Järvinen }
1286832d11c5SIlpo Järvinen 
1287832d11c5SIlpo Järvinen /* Shifting pages past head area doesn't work */
1288cf533ea5SEric Dumazet static int skb_can_shift(const struct sk_buff *skb)
1289832d11c5SIlpo Järvinen {
1290832d11c5SIlpo Järvinen 	return !skb_headlen(skb) && skb_is_nonlinear(skb);
1291832d11c5SIlpo Järvinen }
1292832d11c5SIlpo Järvinen 
1293832d11c5SIlpo Järvinen /* Try collapsing SACK blocks spanning across multiple skbs to a single
1294832d11c5SIlpo Järvinen  * skb.
1295832d11c5SIlpo Järvinen  */
1296832d11c5SIlpo Järvinen static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
1297a1197f5aSIlpo Järvinen 					  struct tcp_sacktag_state *state,
1298832d11c5SIlpo Järvinen 					  u32 start_seq, u32 end_seq,
1299a2a385d6SEric Dumazet 					  bool dup_sack)
1300832d11c5SIlpo Järvinen {
1301832d11c5SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
1302832d11c5SIlpo Järvinen 	struct sk_buff *prev;
1303832d11c5SIlpo Järvinen 	int mss;
1304832d11c5SIlpo Järvinen 	int pcount = 0;
1305832d11c5SIlpo Järvinen 	int len;
1306832d11c5SIlpo Järvinen 	int in_sack;
1307832d11c5SIlpo Järvinen 
1308832d11c5SIlpo Järvinen 	if (!sk_can_gso(sk))
1309832d11c5SIlpo Järvinen 		goto fallback;
1310832d11c5SIlpo Järvinen 
1311832d11c5SIlpo Järvinen 	/* Normally R but no L won't result in plain S */
1312832d11c5SIlpo Järvinen 	if (!dup_sack &&
13139969ca5fSIlpo Järvinen 	    (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
1314832d11c5SIlpo Järvinen 		goto fallback;
1315832d11c5SIlpo Järvinen 	if (!skb_can_shift(skb))
1316832d11c5SIlpo Järvinen 		goto fallback;
1317832d11c5SIlpo Järvinen 	/* This frame is about to be dropped (was ACKed). */
1318832d11c5SIlpo Järvinen 	if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1319832d11c5SIlpo Järvinen 		goto fallback;
1320832d11c5SIlpo Järvinen 
1321832d11c5SIlpo Järvinen 	/* Can only happen with delayed DSACK + discard craziness */
1322832d11c5SIlpo Järvinen 	if (unlikely(skb == tcp_write_queue_head(sk)))
1323832d11c5SIlpo Järvinen 		goto fallback;
1324832d11c5SIlpo Järvinen 	prev = tcp_write_queue_prev(sk, skb);
1325832d11c5SIlpo Järvinen 
1326832d11c5SIlpo Järvinen 	if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1327832d11c5SIlpo Järvinen 		goto fallback;
1328832d11c5SIlpo Järvinen 
1329832d11c5SIlpo Järvinen 	in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1330832d11c5SIlpo Järvinen 		  !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1331832d11c5SIlpo Järvinen 
1332832d11c5SIlpo Järvinen 	if (in_sack) {
1333832d11c5SIlpo Järvinen 		len = skb->len;
1334832d11c5SIlpo Järvinen 		pcount = tcp_skb_pcount(skb);
1335775ffabfSIlpo Järvinen 		mss = tcp_skb_seglen(skb);
1336832d11c5SIlpo Järvinen 
1337832d11c5SIlpo Järvinen 		/* TODO: Fix DSACKs to not fragment already SACKed and we can
1338832d11c5SIlpo Järvinen 		 * drop this restriction as unnecessary
1339832d11c5SIlpo Järvinen 		 */
1340775ffabfSIlpo Järvinen 		if (mss != tcp_skb_seglen(prev))
1341832d11c5SIlpo Järvinen 			goto fallback;
1342832d11c5SIlpo Järvinen 	} else {
1343832d11c5SIlpo Järvinen 		if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1344832d11c5SIlpo Järvinen 			goto noop;
1345832d11c5SIlpo Järvinen 		/* CHECKME: This is non-MSS split case only?, this will
1346832d11c5SIlpo Järvinen 		 * cause skipped skbs due to advancing loop btw, original
1347832d11c5SIlpo Järvinen 		 * has that feature too
1348832d11c5SIlpo Järvinen 		 */
1349832d11c5SIlpo Järvinen 		if (tcp_skb_pcount(skb) <= 1)
1350832d11c5SIlpo Järvinen 			goto noop;
1351832d11c5SIlpo Järvinen 
1352832d11c5SIlpo Järvinen 		in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1353832d11c5SIlpo Järvinen 		if (!in_sack) {
1354832d11c5SIlpo Järvinen 			/* TODO: head merge to next could be attempted here
1355832d11c5SIlpo Järvinen 			 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1356832d11c5SIlpo Järvinen 			 * though it might not be worth of the additional hassle
1357832d11c5SIlpo Järvinen 			 *
1358832d11c5SIlpo Järvinen 			 * ...we can probably just fallback to what was done
1359832d11c5SIlpo Järvinen 			 * previously. We could try merging non-SACKed ones
1360832d11c5SIlpo Järvinen 			 * as well but it probably isn't going to buy off
1361832d11c5SIlpo Järvinen 			 * because later SACKs might again split them, and
1362832d11c5SIlpo Järvinen 			 * it would make skb timestamp tracking considerably
1363832d11c5SIlpo Järvinen 			 * harder problem.
1364832d11c5SIlpo Järvinen 			 */
1365832d11c5SIlpo Järvinen 			goto fallback;
1366832d11c5SIlpo Järvinen 		}
1367832d11c5SIlpo Järvinen 
1368832d11c5SIlpo Järvinen 		len = end_seq - TCP_SKB_CB(skb)->seq;
1369832d11c5SIlpo Järvinen 		BUG_ON(len < 0);
1370832d11c5SIlpo Järvinen 		BUG_ON(len > skb->len);
1371832d11c5SIlpo Järvinen 
1372832d11c5SIlpo Järvinen 		/* MSS boundaries should be honoured or else pcount will
1373832d11c5SIlpo Järvinen 		 * severely break even though it makes things bit trickier.
1374832d11c5SIlpo Järvinen 		 * Optimize common case to avoid most of the divides
1375832d11c5SIlpo Järvinen 		 */
1376832d11c5SIlpo Järvinen 		mss = tcp_skb_mss(skb);
1377832d11c5SIlpo Järvinen 
1378832d11c5SIlpo Järvinen 		/* TODO: Fix DSACKs to not fragment already SACKed and we can
1379832d11c5SIlpo Järvinen 		 * drop this restriction as unnecessary
1380832d11c5SIlpo Järvinen 		 */
1381775ffabfSIlpo Järvinen 		if (mss != tcp_skb_seglen(prev))
1382832d11c5SIlpo Järvinen 			goto fallback;
1383832d11c5SIlpo Järvinen 
1384832d11c5SIlpo Järvinen 		if (len == mss) {
1385832d11c5SIlpo Järvinen 			pcount = 1;
1386832d11c5SIlpo Järvinen 		} else if (len < mss) {
1387832d11c5SIlpo Järvinen 			goto noop;
1388832d11c5SIlpo Järvinen 		} else {
1389832d11c5SIlpo Järvinen 			pcount = len / mss;
1390832d11c5SIlpo Järvinen 			len = pcount * mss;
1391832d11c5SIlpo Järvinen 		}
1392832d11c5SIlpo Järvinen 	}
1393832d11c5SIlpo Järvinen 
13944648dc97SNeal Cardwell 	/* tcp_sacktag_one() won't SACK-tag ranges below snd_una */
13954648dc97SNeal Cardwell 	if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
13964648dc97SNeal Cardwell 		goto fallback;
13974648dc97SNeal Cardwell 
1398832d11c5SIlpo Järvinen 	if (!skb_shift(prev, skb, len))
1399832d11c5SIlpo Järvinen 		goto fallback;
14009ec06ff5SIlpo Järvinen 	if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack))
1401832d11c5SIlpo Järvinen 		goto out;
1402832d11c5SIlpo Järvinen 
1403832d11c5SIlpo Järvinen 	/* Hole filled allows collapsing with the next as well, this is very
1404832d11c5SIlpo Järvinen 	 * useful when hole on every nth skb pattern happens
1405832d11c5SIlpo Järvinen 	 */
1406832d11c5SIlpo Järvinen 	if (prev == tcp_write_queue_tail(sk))
1407832d11c5SIlpo Järvinen 		goto out;
1408832d11c5SIlpo Järvinen 	skb = tcp_write_queue_next(sk, prev);
1409832d11c5SIlpo Järvinen 
1410f0bc52f3SIlpo Järvinen 	if (!skb_can_shift(skb) ||
1411f0bc52f3SIlpo Järvinen 	    (skb == tcp_send_head(sk)) ||
1412f0bc52f3SIlpo Järvinen 	    ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
1413775ffabfSIlpo Järvinen 	    (mss != tcp_skb_seglen(skb)))
1414832d11c5SIlpo Järvinen 		goto out;
1415832d11c5SIlpo Järvinen 
1416832d11c5SIlpo Järvinen 	len = skb->len;
1417832d11c5SIlpo Järvinen 	if (skb_shift(prev, skb, len)) {
1418832d11c5SIlpo Järvinen 		pcount += tcp_skb_pcount(skb);
14199ec06ff5SIlpo Järvinen 		tcp_shifted_skb(sk, skb, state, tcp_skb_pcount(skb), len, mss, 0);
1420832d11c5SIlpo Järvinen 	}
1421832d11c5SIlpo Järvinen 
1422832d11c5SIlpo Järvinen out:
1423a1197f5aSIlpo Järvinen 	state->fack_count += pcount;
1424832d11c5SIlpo Järvinen 	return prev;
1425832d11c5SIlpo Järvinen 
1426832d11c5SIlpo Järvinen noop:
1427832d11c5SIlpo Järvinen 	return skb;
1428832d11c5SIlpo Järvinen 
1429832d11c5SIlpo Järvinen fallback:
1430111cc8b9SIlpo Järvinen 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
1431832d11c5SIlpo Järvinen 	return NULL;
1432832d11c5SIlpo Järvinen }
1433832d11c5SIlpo Järvinen 
143468f8353bSIlpo Järvinen static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
143568f8353bSIlpo Järvinen 					struct tcp_sack_block *next_dup,
1436a1197f5aSIlpo Järvinen 					struct tcp_sacktag_state *state,
143768f8353bSIlpo Järvinen 					u32 start_seq, u32 end_seq,
1438a2a385d6SEric Dumazet 					bool dup_sack_in)
143968f8353bSIlpo Järvinen {
1440832d11c5SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
1441832d11c5SIlpo Järvinen 	struct sk_buff *tmp;
1442832d11c5SIlpo Järvinen 
144368f8353bSIlpo Järvinen 	tcp_for_write_queue_from(skb, sk) {
144468f8353bSIlpo Järvinen 		int in_sack = 0;
1445a2a385d6SEric Dumazet 		bool dup_sack = dup_sack_in;
144668f8353bSIlpo Järvinen 
144768f8353bSIlpo Järvinen 		if (skb == tcp_send_head(sk))
144868f8353bSIlpo Järvinen 			break;
144968f8353bSIlpo Järvinen 
145068f8353bSIlpo Järvinen 		/* queue is in-order => we can short-circuit the walk early */
145168f8353bSIlpo Järvinen 		if (!before(TCP_SKB_CB(skb)->seq, end_seq))
145268f8353bSIlpo Järvinen 			break;
145368f8353bSIlpo Järvinen 
145468f8353bSIlpo Järvinen 		if ((next_dup != NULL) &&
145568f8353bSIlpo Järvinen 		    before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
145668f8353bSIlpo Järvinen 			in_sack = tcp_match_skb_to_sack(sk, skb,
145768f8353bSIlpo Järvinen 							next_dup->start_seq,
145868f8353bSIlpo Järvinen 							next_dup->end_seq);
145968f8353bSIlpo Järvinen 			if (in_sack > 0)
1460a2a385d6SEric Dumazet 				dup_sack = true;
146168f8353bSIlpo Järvinen 		}
146268f8353bSIlpo Järvinen 
1463832d11c5SIlpo Järvinen 		/* skb reference here is a bit tricky to get right, since
1464832d11c5SIlpo Järvinen 		 * shifting can eat and free both this skb and the next,
1465832d11c5SIlpo Järvinen 		 * so not even _safe variant of the loop is enough.
1466832d11c5SIlpo Järvinen 		 */
1467832d11c5SIlpo Järvinen 		if (in_sack <= 0) {
1468a1197f5aSIlpo Järvinen 			tmp = tcp_shift_skb_data(sk, skb, state,
1469a1197f5aSIlpo Järvinen 						 start_seq, end_seq, dup_sack);
1470832d11c5SIlpo Järvinen 			if (tmp != NULL) {
1471832d11c5SIlpo Järvinen 				if (tmp != skb) {
1472832d11c5SIlpo Järvinen 					skb = tmp;
1473832d11c5SIlpo Järvinen 					continue;
1474832d11c5SIlpo Järvinen 				}
1475832d11c5SIlpo Järvinen 
1476832d11c5SIlpo Järvinen 				in_sack = 0;
1477832d11c5SIlpo Järvinen 			} else {
1478832d11c5SIlpo Järvinen 				in_sack = tcp_match_skb_to_sack(sk, skb,
1479832d11c5SIlpo Järvinen 								start_seq,
1480056834d9SIlpo Järvinen 								end_seq);
1481832d11c5SIlpo Järvinen 			}
1482832d11c5SIlpo Järvinen 		}
1483832d11c5SIlpo Järvinen 
148468f8353bSIlpo Järvinen 		if (unlikely(in_sack < 0))
148568f8353bSIlpo Järvinen 			break;
148668f8353bSIlpo Järvinen 
1487832d11c5SIlpo Järvinen 		if (in_sack) {
1488cc9a672eSNeal Cardwell 			TCP_SKB_CB(skb)->sacked =
1489cc9a672eSNeal Cardwell 				tcp_sacktag_one(sk,
1490a1197f5aSIlpo Järvinen 						state,
1491cc9a672eSNeal Cardwell 						TCP_SKB_CB(skb)->sacked,
1492cc9a672eSNeal Cardwell 						TCP_SKB_CB(skb)->seq,
1493cc9a672eSNeal Cardwell 						TCP_SKB_CB(skb)->end_seq,
1494a1197f5aSIlpo Järvinen 						dup_sack,
1495f58b22fdSIlpo Järvinen 						tcp_skb_pcount(skb));
149668f8353bSIlpo Järvinen 
1497832d11c5SIlpo Järvinen 			if (!before(TCP_SKB_CB(skb)->seq,
1498832d11c5SIlpo Järvinen 				    tcp_highest_sack_seq(tp)))
1499832d11c5SIlpo Järvinen 				tcp_advance_highest_sack(sk, skb);
1500832d11c5SIlpo Järvinen 		}
1501832d11c5SIlpo Järvinen 
1502a1197f5aSIlpo Järvinen 		state->fack_count += tcp_skb_pcount(skb);
150368f8353bSIlpo Järvinen 	}
150468f8353bSIlpo Järvinen 	return skb;
150568f8353bSIlpo Järvinen }
150668f8353bSIlpo Järvinen 
150768f8353bSIlpo Järvinen /* Avoid all extra work that is being done by sacktag while walking in
150868f8353bSIlpo Järvinen  * a normal way
150968f8353bSIlpo Järvinen  */
151068f8353bSIlpo Järvinen static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
1511a1197f5aSIlpo Järvinen 					struct tcp_sacktag_state *state,
1512a1197f5aSIlpo Järvinen 					u32 skip_to_seq)
151368f8353bSIlpo Järvinen {
151468f8353bSIlpo Järvinen 	tcp_for_write_queue_from(skb, sk) {
151568f8353bSIlpo Järvinen 		if (skb == tcp_send_head(sk))
151668f8353bSIlpo Järvinen 			break;
151768f8353bSIlpo Järvinen 
1518e8bae275SIlpo Järvinen 		if (after(TCP_SKB_CB(skb)->end_seq, skip_to_seq))
151968f8353bSIlpo Järvinen 			break;
1520d152a7d8SIlpo Järvinen 
1521a1197f5aSIlpo Järvinen 		state->fack_count += tcp_skb_pcount(skb);
152268f8353bSIlpo Järvinen 	}
152368f8353bSIlpo Järvinen 	return skb;
152468f8353bSIlpo Järvinen }
152568f8353bSIlpo Järvinen 
152668f8353bSIlpo Järvinen static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
152768f8353bSIlpo Järvinen 						struct sock *sk,
152868f8353bSIlpo Järvinen 						struct tcp_sack_block *next_dup,
1529a1197f5aSIlpo Järvinen 						struct tcp_sacktag_state *state,
1530a1197f5aSIlpo Järvinen 						u32 skip_to_seq)
153168f8353bSIlpo Järvinen {
153268f8353bSIlpo Järvinen 	if (next_dup == NULL)
153368f8353bSIlpo Järvinen 		return skb;
153468f8353bSIlpo Järvinen 
153568f8353bSIlpo Järvinen 	if (before(next_dup->start_seq, skip_to_seq)) {
1536a1197f5aSIlpo Järvinen 		skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
1537a1197f5aSIlpo Järvinen 		skb = tcp_sacktag_walk(skb, sk, NULL, state,
153868f8353bSIlpo Järvinen 				       next_dup->start_seq, next_dup->end_seq,
1539a1197f5aSIlpo Järvinen 				       1);
154068f8353bSIlpo Järvinen 	}
154168f8353bSIlpo Järvinen 
154268f8353bSIlpo Järvinen 	return skb;
154368f8353bSIlpo Järvinen }
154468f8353bSIlpo Järvinen 
1545cf533ea5SEric Dumazet static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
154668f8353bSIlpo Järvinen {
154768f8353bSIlpo Järvinen 	return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
154868f8353bSIlpo Järvinen }
154968f8353bSIlpo Järvinen 
15501da177e4SLinus Torvalds static int
1551cf533ea5SEric Dumazet tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
1552056834d9SIlpo Järvinen 			u32 prior_snd_una)
15531da177e4SLinus Torvalds {
15541da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
1555cf533ea5SEric Dumazet 	const unsigned char *ptr = (skb_transport_header(ack_skb) +
15569c70220bSArnaldo Carvalho de Melo 				    TCP_SKB_CB(ack_skb)->sacked);
1557fd6dad61SIlpo Järvinen 	struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
15584389ddedSAdam Langley 	struct tcp_sack_block sp[TCP_NUM_SACKS];
155968f8353bSIlpo Järvinen 	struct tcp_sack_block *cache;
1560a1197f5aSIlpo Järvinen 	struct tcp_sacktag_state state;
156168f8353bSIlpo Järvinen 	struct sk_buff *skb;
15624389ddedSAdam Langley 	int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
1563fd6dad61SIlpo Järvinen 	int used_sacks;
1564a2a385d6SEric Dumazet 	bool found_dup_sack = false;
156568f8353bSIlpo Järvinen 	int i, j;
1566fda03fbbSBaruch Even 	int first_sack_index;
15671da177e4SLinus Torvalds 
1568a1197f5aSIlpo Järvinen 	state.flag = 0;
1569a1197f5aSIlpo Järvinen 	state.reord = tp->packets_out;
1570a1197f5aSIlpo Järvinen 
1571d738cd8fSIlpo Järvinen 	if (!tp->sacked_out) {
1572de83c058SIlpo Järvinen 		if (WARN_ON(tp->fackets_out))
15731da177e4SLinus Torvalds 			tp->fackets_out = 0;
15746859d494SIlpo Järvinen 		tcp_highest_sack_reset(sk);
1575d738cd8fSIlpo Järvinen 	}
15761da177e4SLinus Torvalds 
15771ed83465SPavel Emelyanov 	found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
1578d06e021dSDavid S. Miller 					 num_sacks, prior_snd_una);
1579d06e021dSDavid S. Miller 	if (found_dup_sack)
1580a1197f5aSIlpo Järvinen 		state.flag |= FLAG_DSACKING_ACK;
15816f74651aSBaruch Even 
15826f74651aSBaruch Even 	/* Eliminate too old ACKs, but take into
15836f74651aSBaruch Even 	 * account more or less fresh ones, they can
15846f74651aSBaruch Even 	 * contain valid SACK info.
15856f74651aSBaruch Even 	 */
15866f74651aSBaruch Even 	if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
15876f74651aSBaruch Even 		return 0;
15886f74651aSBaruch Even 
158996a2d41aSIlpo Järvinen 	if (!tp->packets_out)
159096a2d41aSIlpo Järvinen 		goto out;
159196a2d41aSIlpo Järvinen 
1592fd6dad61SIlpo Järvinen 	used_sacks = 0;
1593fd6dad61SIlpo Järvinen 	first_sack_index = 0;
1594fd6dad61SIlpo Järvinen 	for (i = 0; i < num_sacks; i++) {
1595a2a385d6SEric Dumazet 		bool dup_sack = !i && found_dup_sack;
1596fd6dad61SIlpo Järvinen 
1597d3e2ce3bSHarvey Harrison 		sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq);
1598d3e2ce3bSHarvey Harrison 		sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq);
1599fd6dad61SIlpo Järvinen 
1600fd6dad61SIlpo Järvinen 		if (!tcp_is_sackblock_valid(tp, dup_sack,
1601fd6dad61SIlpo Järvinen 					    sp[used_sacks].start_seq,
1602fd6dad61SIlpo Järvinen 					    sp[used_sacks].end_seq)) {
160340b215e5SPavel Emelyanov 			int mib_idx;
160440b215e5SPavel Emelyanov 
1605fd6dad61SIlpo Järvinen 			if (dup_sack) {
1606fd6dad61SIlpo Järvinen 				if (!tp->undo_marker)
160740b215e5SPavel Emelyanov 					mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO;
1608fd6dad61SIlpo Järvinen 				else
160940b215e5SPavel Emelyanov 					mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD;
1610fd6dad61SIlpo Järvinen 			} else {
1611fd6dad61SIlpo Järvinen 				/* Don't count olds caused by ACK reordering */
1612fd6dad61SIlpo Järvinen 				if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) &&
1613fd6dad61SIlpo Järvinen 				    !after(sp[used_sacks].end_seq, tp->snd_una))
1614fd6dad61SIlpo Järvinen 					continue;
161540b215e5SPavel Emelyanov 				mib_idx = LINUX_MIB_TCPSACKDISCARD;
1616fd6dad61SIlpo Järvinen 			}
161740b215e5SPavel Emelyanov 
1618de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk), mib_idx);
1619fd6dad61SIlpo Järvinen 			if (i == 0)
1620fd6dad61SIlpo Järvinen 				first_sack_index = -1;
1621fd6dad61SIlpo Järvinen 			continue;
1622fd6dad61SIlpo Järvinen 		}
1623fd6dad61SIlpo Järvinen 
1624fd6dad61SIlpo Järvinen 		/* Ignore very old stuff early */
1625fd6dad61SIlpo Järvinen 		if (!after(sp[used_sacks].end_seq, prior_snd_una))
1626fd6dad61SIlpo Järvinen 			continue;
1627fd6dad61SIlpo Järvinen 
1628fd6dad61SIlpo Järvinen 		used_sacks++;
1629fd6dad61SIlpo Järvinen 	}
1630fd6dad61SIlpo Järvinen 
16316a438bbeSStephen Hemminger 	/* order SACK blocks to allow in order walk of the retrans queue */
1632fd6dad61SIlpo Järvinen 	for (i = used_sacks - 1; i > 0; i--) {
16336a438bbeSStephen Hemminger 		for (j = 0; j < i; j++) {
1634fd6dad61SIlpo Järvinen 			if (after(sp[j].start_seq, sp[j + 1].start_seq)) {
1635a0bffffcSIlpo Järvinen 				swap(sp[j], sp[j + 1]);
1636fda03fbbSBaruch Even 
1637fda03fbbSBaruch Even 				/* Track where the first SACK block goes to */
1638fda03fbbSBaruch Even 				if (j == first_sack_index)
1639fda03fbbSBaruch Even 					first_sack_index = j + 1;
16406a438bbeSStephen Hemminger 			}
16416a438bbeSStephen Hemminger 		}
16426a438bbeSStephen Hemminger 	}
16436a438bbeSStephen Hemminger 
164468f8353bSIlpo Järvinen 	skb = tcp_write_queue_head(sk);
1645a1197f5aSIlpo Järvinen 	state.fack_count = 0;
164668f8353bSIlpo Järvinen 	i = 0;
164768f8353bSIlpo Järvinen 
164868f8353bSIlpo Järvinen 	if (!tp->sacked_out) {
164968f8353bSIlpo Järvinen 		/* It's already past, so skip checking against it */
165068f8353bSIlpo Järvinen 		cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
165168f8353bSIlpo Järvinen 	} else {
165268f8353bSIlpo Järvinen 		cache = tp->recv_sack_cache;
165368f8353bSIlpo Järvinen 		/* Skip empty blocks in at head of the cache */
165468f8353bSIlpo Järvinen 		while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq &&
165568f8353bSIlpo Järvinen 		       !cache->end_seq)
165668f8353bSIlpo Järvinen 			cache++;
1657fda03fbbSBaruch Even 	}
1658fda03fbbSBaruch Even 
165968f8353bSIlpo Järvinen 	while (i < used_sacks) {
1660fd6dad61SIlpo Järvinen 		u32 start_seq = sp[i].start_seq;
1661fd6dad61SIlpo Järvinen 		u32 end_seq = sp[i].end_seq;
1662a2a385d6SEric Dumazet 		bool dup_sack = (found_dup_sack && (i == first_sack_index));
166368f8353bSIlpo Järvinen 		struct tcp_sack_block *next_dup = NULL;
1664e56d6cd6SIlpo Järvinen 
166568f8353bSIlpo Järvinen 		if (found_dup_sack && ((i + 1) == first_sack_index))
166668f8353bSIlpo Järvinen 			next_dup = &sp[i + 1];
16671da177e4SLinus Torvalds 
166868f8353bSIlpo Järvinen 		/* Skip too early cached blocks */
166968f8353bSIlpo Järvinen 		while (tcp_sack_cache_ok(tp, cache) &&
167068f8353bSIlpo Järvinen 		       !before(start_seq, cache->end_seq))
167168f8353bSIlpo Järvinen 			cache++;
16721da177e4SLinus Torvalds 
167368f8353bSIlpo Järvinen 		/* Can skip some work by looking recv_sack_cache? */
167468f8353bSIlpo Järvinen 		if (tcp_sack_cache_ok(tp, cache) && !dup_sack &&
167568f8353bSIlpo Järvinen 		    after(end_seq, cache->start_seq)) {
1676fe067e8aSDavid S. Miller 
167768f8353bSIlpo Järvinen 			/* Head todo? */
167868f8353bSIlpo Järvinen 			if (before(start_seq, cache->start_seq)) {
1679a1197f5aSIlpo Järvinen 				skb = tcp_sacktag_skip(skb, sk, &state,
1680a1197f5aSIlpo Järvinen 						       start_seq);
1681056834d9SIlpo Järvinen 				skb = tcp_sacktag_walk(skb, sk, next_dup,
1682a1197f5aSIlpo Järvinen 						       &state,
1683056834d9SIlpo Järvinen 						       start_seq,
1684056834d9SIlpo Järvinen 						       cache->start_seq,
1685a1197f5aSIlpo Järvinen 						       dup_sack);
1686fda03fbbSBaruch Even 			}
16876a438bbeSStephen Hemminger 
168868f8353bSIlpo Järvinen 			/* Rest of the block already fully processed? */
168920de20beSIlpo Järvinen 			if (!after(end_seq, cache->end_seq))
169020de20beSIlpo Järvinen 				goto advance_sp;
169120de20beSIlpo Järvinen 
1692056834d9SIlpo Järvinen 			skb = tcp_maybe_skipping_dsack(skb, sk, next_dup,
1693a1197f5aSIlpo Järvinen 						       &state,
1694a1197f5aSIlpo Järvinen 						       cache->end_seq);
169568f8353bSIlpo Järvinen 
169668f8353bSIlpo Järvinen 			/* ...tail remains todo... */
16976859d494SIlpo Järvinen 			if (tcp_highest_sack_seq(tp) == cache->end_seq) {
169820de20beSIlpo Järvinen 				/* ...but better entrypoint exists! */
16996859d494SIlpo Järvinen 				skb = tcp_highest_sack(sk);
17006859d494SIlpo Järvinen 				if (skb == NULL)
17016859d494SIlpo Järvinen 					break;
1702a1197f5aSIlpo Järvinen 				state.fack_count = tp->fackets_out;
170368f8353bSIlpo Järvinen 				cache++;
170468f8353bSIlpo Järvinen 				goto walk;
1705e56d6cd6SIlpo Järvinen 			}
1706e56d6cd6SIlpo Järvinen 
1707a1197f5aSIlpo Järvinen 			skb = tcp_sacktag_skip(skb, sk, &state, cache->end_seq);
170868f8353bSIlpo Järvinen 			/* Check overlap against next cached too (past this one already) */
170968f8353bSIlpo Järvinen 			cache++;
171068f8353bSIlpo Järvinen 			continue;
17111da177e4SLinus Torvalds 		}
1712fbd52eb2SIlpo Järvinen 
17136859d494SIlpo Järvinen 		if (!before(start_seq, tcp_highest_sack_seq(tp))) {
17146859d494SIlpo Järvinen 			skb = tcp_highest_sack(sk);
17156859d494SIlpo Järvinen 			if (skb == NULL)
17166859d494SIlpo Järvinen 				break;
1717a1197f5aSIlpo Järvinen 			state.fack_count = tp->fackets_out;
171868f8353bSIlpo Järvinen 		}
1719a1197f5aSIlpo Järvinen 		skb = tcp_sacktag_skip(skb, sk, &state, start_seq);
172068f8353bSIlpo Järvinen 
172168f8353bSIlpo Järvinen walk:
1722a1197f5aSIlpo Järvinen 		skb = tcp_sacktag_walk(skb, sk, next_dup, &state,
1723a1197f5aSIlpo Järvinen 				       start_seq, end_seq, dup_sack);
172468f8353bSIlpo Järvinen 
172568f8353bSIlpo Järvinen advance_sp:
172668f8353bSIlpo Järvinen 		i++;
17271da177e4SLinus Torvalds 	}
17281da177e4SLinus Torvalds 
172968f8353bSIlpo Järvinen 	/* Clear the head of the cache sack blocks so we can skip it next time */
173068f8353bSIlpo Järvinen 	for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) {
173168f8353bSIlpo Järvinen 		tp->recv_sack_cache[i].start_seq = 0;
173268f8353bSIlpo Järvinen 		tp->recv_sack_cache[i].end_seq = 0;
173368f8353bSIlpo Järvinen 	}
173468f8353bSIlpo Järvinen 	for (j = 0; j < used_sacks; j++)
173568f8353bSIlpo Järvinen 		tp->recv_sack_cache[i++] = sp[j];
173668f8353bSIlpo Järvinen 
1737407ef1deSIlpo Järvinen 	tcp_mark_lost_retrans(sk);
17381da177e4SLinus Torvalds 
173986426c22SIlpo Järvinen 	tcp_verify_left_out(tp);
174086426c22SIlpo Järvinen 
1741a1197f5aSIlpo Järvinen 	if ((state.reord < tp->fackets_out) &&
17429b44190dSYuchung Cheng 	    ((inet_csk(sk)->icsk_ca_state != TCP_CA_Loss) || tp->undo_marker))
1743a1197f5aSIlpo Järvinen 		tcp_update_reordering(sk, tp->fackets_out - state.reord, 0);
17441da177e4SLinus Torvalds 
174596a2d41aSIlpo Järvinen out:
174696a2d41aSIlpo Järvinen 
17471da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0
1748547b792cSIlpo Järvinen 	WARN_ON((int)tp->sacked_out < 0);
1749547b792cSIlpo Järvinen 	WARN_ON((int)tp->lost_out < 0);
1750547b792cSIlpo Järvinen 	WARN_ON((int)tp->retrans_out < 0);
1751547b792cSIlpo Järvinen 	WARN_ON((int)tcp_packets_in_flight(tp) < 0);
17521da177e4SLinus Torvalds #endif
1753a1197f5aSIlpo Järvinen 	return state.flag;
17541da177e4SLinus Torvalds }
17551da177e4SLinus Torvalds 
1756882bebaaSIlpo Järvinen /* Limits sacked_out so that sum with lost_out isn't ever larger than
1757a2a385d6SEric Dumazet  * packets_out. Returns false if sacked_out adjustement wasn't necessary.
175830935cf4SIlpo Järvinen  */
1759a2a385d6SEric Dumazet static bool tcp_limit_reno_sacked(struct tcp_sock *tp)
17604ddf6676SIlpo Järvinen {
17614ddf6676SIlpo Järvinen 	u32 holes;
17624ddf6676SIlpo Järvinen 
17634ddf6676SIlpo Järvinen 	holes = max(tp->lost_out, 1U);
17644ddf6676SIlpo Järvinen 	holes = min(holes, tp->packets_out);
17654ddf6676SIlpo Järvinen 
17664ddf6676SIlpo Järvinen 	if ((tp->sacked_out + holes) > tp->packets_out) {
17674ddf6676SIlpo Järvinen 		tp->sacked_out = tp->packets_out - holes;
1768a2a385d6SEric Dumazet 		return true;
17694ddf6676SIlpo Järvinen 	}
1770a2a385d6SEric Dumazet 	return false;
1771882bebaaSIlpo Järvinen }
1772882bebaaSIlpo Järvinen 
1773882bebaaSIlpo Järvinen /* If we receive more dupacks than we expected counting segments
1774882bebaaSIlpo Järvinen  * in assumption of absent reordering, interpret this as reordering.
1775882bebaaSIlpo Järvinen  * The only another reason could be bug in receiver TCP.
1776882bebaaSIlpo Järvinen  */
1777882bebaaSIlpo Järvinen static void tcp_check_reno_reordering(struct sock *sk, const int addend)
1778882bebaaSIlpo Järvinen {
1779882bebaaSIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
1780882bebaaSIlpo Järvinen 	if (tcp_limit_reno_sacked(tp))
1781882bebaaSIlpo Järvinen 		tcp_update_reordering(sk, tp->packets_out + addend, 0);
17824ddf6676SIlpo Järvinen }
17834ddf6676SIlpo Järvinen 
17844ddf6676SIlpo Järvinen /* Emulate SACKs for SACKless connection: account for a new dupack. */
17854ddf6676SIlpo Järvinen 
17864ddf6676SIlpo Järvinen static void tcp_add_reno_sack(struct sock *sk)
17874ddf6676SIlpo Järvinen {
17884ddf6676SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
17894ddf6676SIlpo Järvinen 	tp->sacked_out++;
17904ddf6676SIlpo Järvinen 	tcp_check_reno_reordering(sk, 0);
1791005903bcSIlpo Järvinen 	tcp_verify_left_out(tp);
17924ddf6676SIlpo Järvinen }
17934ddf6676SIlpo Järvinen 
17944ddf6676SIlpo Järvinen /* Account for ACK, ACKing some data in Reno Recovery phase. */
17954ddf6676SIlpo Järvinen 
17964ddf6676SIlpo Järvinen static void tcp_remove_reno_sacks(struct sock *sk, int acked)
17974ddf6676SIlpo Järvinen {
17984ddf6676SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
17994ddf6676SIlpo Järvinen 
18004ddf6676SIlpo Järvinen 	if (acked > 0) {
18014ddf6676SIlpo Järvinen 		/* One ACK acked hole. The rest eat duplicate ACKs. */
18024ddf6676SIlpo Järvinen 		if (acked - 1 >= tp->sacked_out)
18034ddf6676SIlpo Järvinen 			tp->sacked_out = 0;
18044ddf6676SIlpo Järvinen 		else
18054ddf6676SIlpo Järvinen 			tp->sacked_out -= acked - 1;
18064ddf6676SIlpo Järvinen 	}
18074ddf6676SIlpo Järvinen 	tcp_check_reno_reordering(sk, acked);
1808005903bcSIlpo Järvinen 	tcp_verify_left_out(tp);
18094ddf6676SIlpo Järvinen }
18104ddf6676SIlpo Järvinen 
18114ddf6676SIlpo Järvinen static inline void tcp_reset_reno_sack(struct tcp_sock *tp)
18124ddf6676SIlpo Järvinen {
18134ddf6676SIlpo Järvinen 	tp->sacked_out = 0;
18144ddf6676SIlpo Järvinen }
18154ddf6676SIlpo Järvinen 
18164cd82999SIlpo Järvinen static void tcp_clear_retrans_partial(struct tcp_sock *tp)
18171da177e4SLinus Torvalds {
18181da177e4SLinus Torvalds 	tp->retrans_out = 0;
18191da177e4SLinus Torvalds 	tp->lost_out = 0;
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds 	tp->undo_marker = 0;
18221da177e4SLinus Torvalds 	tp->undo_retrans = 0;
18231da177e4SLinus Torvalds }
18241da177e4SLinus Torvalds 
18254cd82999SIlpo Järvinen void tcp_clear_retrans(struct tcp_sock *tp)
18264cd82999SIlpo Järvinen {
18274cd82999SIlpo Järvinen 	tcp_clear_retrans_partial(tp);
18284cd82999SIlpo Järvinen 
18294cd82999SIlpo Järvinen 	tp->fackets_out = 0;
18304cd82999SIlpo Järvinen 	tp->sacked_out = 0;
18314cd82999SIlpo Järvinen }
18324cd82999SIlpo Järvinen 
18331da177e4SLinus Torvalds /* Enter Loss state. If "how" is not zero, forget all SACK information
18341da177e4SLinus Torvalds  * and reset tags completely, otherwise preserve SACKs. If receiver
18351da177e4SLinus Torvalds  * dropped its ofo queue, we will know this due to reneging detection.
18361da177e4SLinus Torvalds  */
18371da177e4SLinus Torvalds void tcp_enter_loss(struct sock *sk, int how)
18381da177e4SLinus Torvalds {
18396687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
18401da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
18411da177e4SLinus Torvalds 	struct sk_buff *skb;
1842e33099f9SYuchung Cheng 	bool new_recovery = false;
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	/* Reduce ssthresh if it has not yet been made inside this window. */
1845e33099f9SYuchung Cheng 	if (icsk->icsk_ca_state <= TCP_CA_Disorder ||
1846e33099f9SYuchung Cheng 	    !after(tp->high_seq, tp->snd_una) ||
18476687e988SArnaldo Carvalho de Melo 	    (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) {
1848e33099f9SYuchung Cheng 		new_recovery = true;
18496687e988SArnaldo Carvalho de Melo 		tp->prior_ssthresh = tcp_current_ssthresh(sk);
18506687e988SArnaldo Carvalho de Melo 		tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
18516687e988SArnaldo Carvalho de Melo 		tcp_ca_event(sk, CA_EVENT_LOSS);
18521da177e4SLinus Torvalds 	}
18531da177e4SLinus Torvalds 	tp->snd_cwnd	   = 1;
18541da177e4SLinus Torvalds 	tp->snd_cwnd_cnt   = 0;
18551da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
18561da177e4SLinus Torvalds 
18574cd82999SIlpo Järvinen 	tcp_clear_retrans_partial(tp);
18584cd82999SIlpo Järvinen 
18594cd82999SIlpo Järvinen 	if (tcp_is_reno(tp))
18604cd82999SIlpo Järvinen 		tcp_reset_reno_sack(tp);
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds 	tp->undo_marker = tp->snd_una;
18637ebe183cSYuchung Cheng 	if (how) {
18644cd82999SIlpo Järvinen 		tp->sacked_out = 0;
18654cd82999SIlpo Järvinen 		tp->fackets_out = 0;
1866b7689205SIlpo Järvinen 	}
186764edc273SIlpo Järvinen 	tcp_clear_all_retrans_hints(tp);
18681da177e4SLinus Torvalds 
1869fe067e8aSDavid S. Miller 	tcp_for_write_queue(skb, sk) {
1870fe067e8aSDavid S. Miller 		if (skb == tcp_send_head(sk))
1871fe067e8aSDavid S. Miller 			break;
18724cd82999SIlpo Järvinen 
18731da177e4SLinus Torvalds 		if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS)
18741da177e4SLinus Torvalds 			tp->undo_marker = 0;
18751da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED;
18761da177e4SLinus Torvalds 		if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED) || how) {
18771da177e4SLinus Torvalds 			TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
18781da177e4SLinus Torvalds 			TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
18791da177e4SLinus Torvalds 			tp->lost_out += tcp_skb_pcount(skb);
1880006f582cSIlpo Järvinen 			tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
18811da177e4SLinus Torvalds 		}
18821da177e4SLinus Torvalds 	}
1883005903bcSIlpo Järvinen 	tcp_verify_left_out(tp);
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 	tp->reordering = min_t(unsigned int, tp->reordering,
18861da177e4SLinus Torvalds 			       sysctl_tcp_reordering);
18876687e988SArnaldo Carvalho de Melo 	tcp_set_ca_state(sk, TCP_CA_Loss);
18881da177e4SLinus Torvalds 	tp->high_seq = tp->snd_nxt;
18891da177e4SLinus Torvalds 	TCP_ECN_queue_cwr(tp);
1890e33099f9SYuchung Cheng 
1891e33099f9SYuchung Cheng 	/* F-RTO RFC5682 sec 3.1 step 1: retransmit SND.UNA if no previous
1892e33099f9SYuchung Cheng 	 * loss recovery is underway except recurring timeout(s) on
1893e33099f9SYuchung Cheng 	 * the same SND.UNA (sec 3.2). Disable F-RTO on path MTU probing
1894e33099f9SYuchung Cheng 	 */
1895e33099f9SYuchung Cheng 	tp->frto = sysctl_tcp_frto &&
1896e33099f9SYuchung Cheng 		   (new_recovery || icsk->icsk_retransmits) &&
1897e33099f9SYuchung Cheng 		   !inet_csk(sk)->icsk_mtup.probe_size;
18981da177e4SLinus Torvalds }
18991da177e4SLinus Torvalds 
1900cadbd031SIlpo Järvinen /* If ACK arrived pointing to a remembered SACK, it means that our
1901cadbd031SIlpo Järvinen  * remembered SACKs do not reflect real state of receiver i.e.
19021da177e4SLinus Torvalds  * receiver _host_ is heavily congested (or buggy).
1903cadbd031SIlpo Järvinen  *
19041da177e4SLinus Torvalds  * Do processing similar to RTO timeout.
19051da177e4SLinus Torvalds  */
1906a2a385d6SEric Dumazet static bool tcp_check_sack_reneging(struct sock *sk, int flag)
1907cadbd031SIlpo Järvinen {
1908cadbd031SIlpo Järvinen 	if (flag & FLAG_SACK_RENEGING) {
19096687e988SArnaldo Carvalho de Melo 		struct inet_connection_sock *icsk = inet_csk(sk);
1910de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSACKRENEGING);
19111da177e4SLinus Torvalds 
19121da177e4SLinus Torvalds 		tcp_enter_loss(sk, 1);
19136687e988SArnaldo Carvalho de Melo 		icsk->icsk_retransmits++;
1914fe067e8aSDavid S. Miller 		tcp_retransmit_skb(sk, tcp_write_queue_head(sk));
1915463c84b9SArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
19166687e988SArnaldo Carvalho de Melo 					  icsk->icsk_rto, TCP_RTO_MAX);
1917a2a385d6SEric Dumazet 		return true;
19181da177e4SLinus Torvalds 	}
1919a2a385d6SEric Dumazet 	return false;
19201da177e4SLinus Torvalds }
19211da177e4SLinus Torvalds 
1922cf533ea5SEric Dumazet static inline int tcp_fackets_out(const struct tcp_sock *tp)
19231da177e4SLinus Torvalds {
1924e60402d0SIlpo Järvinen 	return tcp_is_reno(tp) ? tp->sacked_out + 1 : tp->fackets_out;
19251da177e4SLinus Torvalds }
19261da177e4SLinus Torvalds 
192785cc391cSIlpo Järvinen /* Heurestics to calculate number of duplicate ACKs. There's no dupACKs
192885cc391cSIlpo Järvinen  * counter when SACK is enabled (without SACK, sacked_out is used for
192985cc391cSIlpo Järvinen  * that purpose).
193085cc391cSIlpo Järvinen  *
193185cc391cSIlpo Järvinen  * Instead, with FACK TCP uses fackets_out that includes both SACKed
193285cc391cSIlpo Järvinen  * segments up to the highest received SACK block so far and holes in
193385cc391cSIlpo Järvinen  * between them.
193485cc391cSIlpo Järvinen  *
193585cc391cSIlpo Järvinen  * With reordering, holes may still be in flight, so RFC3517 recovery
193685cc391cSIlpo Järvinen  * uses pure sacked_out (total number of SACKed segments) even though
193785cc391cSIlpo Järvinen  * it violates the RFC that uses duplicate ACKs, often these are equal
193885cc391cSIlpo Järvinen  * but when e.g. out-of-window ACKs or packet duplication occurs,
193985cc391cSIlpo Järvinen  * they differ. Since neither occurs due to loss, TCP should really
194085cc391cSIlpo Järvinen  * ignore them.
194185cc391cSIlpo Järvinen  */
1942cf533ea5SEric Dumazet static inline int tcp_dupack_heuristics(const struct tcp_sock *tp)
194385cc391cSIlpo Järvinen {
194485cc391cSIlpo Järvinen 	return tcp_is_fack(tp) ? tp->fackets_out : tp->sacked_out + 1;
194585cc391cSIlpo Järvinen }
194685cc391cSIlpo Järvinen 
1947750ea2baSYuchung Cheng static bool tcp_pause_early_retransmit(struct sock *sk, int flag)
1948750ea2baSYuchung Cheng {
1949750ea2baSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
1950750ea2baSYuchung Cheng 	unsigned long delay;
1951750ea2baSYuchung Cheng 
1952750ea2baSYuchung Cheng 	/* Delay early retransmit and entering fast recovery for
1953750ea2baSYuchung Cheng 	 * max(RTT/4, 2msec) unless ack has ECE mark, no RTT samples
1954750ea2baSYuchung Cheng 	 * available, or RTO is scheduled to fire first.
1955750ea2baSYuchung Cheng 	 */
19566ba8a3b1SNandita Dukkipati 	if (sysctl_tcp_early_retrans < 2 || sysctl_tcp_early_retrans > 3 ||
19576ba8a3b1SNandita Dukkipati 	    (flag & FLAG_ECE) || !tp->srtt)
1958750ea2baSYuchung Cheng 		return false;
1959750ea2baSYuchung Cheng 
1960750ea2baSYuchung Cheng 	delay = max_t(unsigned long, (tp->srtt >> 5), msecs_to_jiffies(2));
1961750ea2baSYuchung Cheng 	if (!time_after(inet_csk(sk)->icsk_timeout, (jiffies + delay)))
1962750ea2baSYuchung Cheng 		return false;
1963750ea2baSYuchung Cheng 
19646ba8a3b1SNandita Dukkipati 	inet_csk_reset_xmit_timer(sk, ICSK_TIME_EARLY_RETRANS, delay,
19656ba8a3b1SNandita Dukkipati 				  TCP_RTO_MAX);
1966750ea2baSYuchung Cheng 	return true;
1967750ea2baSYuchung Cheng }
1968750ea2baSYuchung Cheng 
1969cf533ea5SEric Dumazet static inline int tcp_skb_timedout(const struct sock *sk,
1970cf533ea5SEric Dumazet 				   const struct sk_buff *skb)
19711da177e4SLinus Torvalds {
1972a02cec21SEric Dumazet 	return tcp_time_stamp - TCP_SKB_CB(skb)->when > inet_csk(sk)->icsk_rto;
19731da177e4SLinus Torvalds }
19741da177e4SLinus Torvalds 
1975cf533ea5SEric Dumazet static inline int tcp_head_timedout(const struct sock *sk)
19761da177e4SLinus Torvalds {
1977cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
19789e412ba7SIlpo Järvinen 
19791da177e4SLinus Torvalds 	return tp->packets_out &&
1980fe067e8aSDavid S. Miller 	       tcp_skb_timedout(sk, tcp_write_queue_head(sk));
19811da177e4SLinus Torvalds }
19821da177e4SLinus Torvalds 
19831da177e4SLinus Torvalds /* Linux NewReno/SACK/FACK/ECN state machine.
19841da177e4SLinus Torvalds  * --------------------------------------
19851da177e4SLinus Torvalds  *
19861da177e4SLinus Torvalds  * "Open"	Normal state, no dubious events, fast path.
19871da177e4SLinus Torvalds  * "Disorder"   In all the respects it is "Open",
19881da177e4SLinus Torvalds  *		but requires a bit more attention. It is entered when
19891da177e4SLinus Torvalds  *		we see some SACKs or dupacks. It is split of "Open"
19901da177e4SLinus Torvalds  *		mainly to move some processing from fast path to slow one.
19911da177e4SLinus Torvalds  * "CWR"	CWND was reduced due to some Congestion Notification event.
19921da177e4SLinus Torvalds  *		It can be ECN, ICMP source quench, local device congestion.
19931da177e4SLinus Torvalds  * "Recovery"	CWND was reduced, we are fast-retransmitting.
19941da177e4SLinus Torvalds  * "Loss"	CWND was reduced due to RTO timeout or SACK reneging.
19951da177e4SLinus Torvalds  *
19961da177e4SLinus Torvalds  * tcp_fastretrans_alert() is entered:
19971da177e4SLinus Torvalds  * - each incoming ACK, if state is not "Open"
19981da177e4SLinus Torvalds  * - when arrived ACK is unusual, namely:
19991da177e4SLinus Torvalds  *	* SACK
20001da177e4SLinus Torvalds  *	* Duplicate ACK.
20011da177e4SLinus Torvalds  *	* ECN ECE.
20021da177e4SLinus Torvalds  *
20031da177e4SLinus Torvalds  * Counting packets in flight is pretty simple.
20041da177e4SLinus Torvalds  *
20051da177e4SLinus Torvalds  *	in_flight = packets_out - left_out + retrans_out
20061da177e4SLinus Torvalds  *
20071da177e4SLinus Torvalds  *	packets_out is SND.NXT-SND.UNA counted in packets.
20081da177e4SLinus Torvalds  *
20091da177e4SLinus Torvalds  *	retrans_out is number of retransmitted segments.
20101da177e4SLinus Torvalds  *
20111da177e4SLinus Torvalds  *	left_out is number of segments left network, but not ACKed yet.
20121da177e4SLinus Torvalds  *
20131da177e4SLinus Torvalds  *		left_out = sacked_out + lost_out
20141da177e4SLinus Torvalds  *
20151da177e4SLinus Torvalds  *     sacked_out: Packets, which arrived to receiver out of order
20161da177e4SLinus Torvalds  *		   and hence not ACKed. With SACKs this number is simply
20171da177e4SLinus Torvalds  *		   amount of SACKed data. Even without SACKs
20181da177e4SLinus Torvalds  *		   it is easy to give pretty reliable estimate of this number,
20191da177e4SLinus Torvalds  *		   counting duplicate ACKs.
20201da177e4SLinus Torvalds  *
20211da177e4SLinus Torvalds  *       lost_out: Packets lost by network. TCP has no explicit
20221da177e4SLinus Torvalds  *		   "loss notification" feedback from network (for now).
20231da177e4SLinus Torvalds  *		   It means that this number can be only _guessed_.
20241da177e4SLinus Torvalds  *		   Actually, it is the heuristics to predict lossage that
20251da177e4SLinus Torvalds  *		   distinguishes different algorithms.
20261da177e4SLinus Torvalds  *
20271da177e4SLinus Torvalds  *	F.e. after RTO, when all the queue is considered as lost,
20281da177e4SLinus Torvalds  *	lost_out = packets_out and in_flight = retrans_out.
20291da177e4SLinus Torvalds  *
20301da177e4SLinus Torvalds  *		Essentially, we have now two algorithms counting
20311da177e4SLinus Torvalds  *		lost packets.
20321da177e4SLinus Torvalds  *
20331da177e4SLinus Torvalds  *		FACK: It is the simplest heuristics. As soon as we decided
20341da177e4SLinus Torvalds  *		that something is lost, we decide that _all_ not SACKed
20351da177e4SLinus Torvalds  *		packets until the most forward SACK are lost. I.e.
20361da177e4SLinus Torvalds  *		lost_out = fackets_out - sacked_out and left_out = fackets_out.
20371da177e4SLinus Torvalds  *		It is absolutely correct estimate, if network does not reorder
20381da177e4SLinus Torvalds  *		packets. And it loses any connection to reality when reordering
20391da177e4SLinus Torvalds  *		takes place. We use FACK by default until reordering
20401da177e4SLinus Torvalds  *		is suspected on the path to this destination.
20411da177e4SLinus Torvalds  *
20421da177e4SLinus Torvalds  *		NewReno: when Recovery is entered, we assume that one segment
20431da177e4SLinus Torvalds  *		is lost (classic Reno). While we are in Recovery and
20441da177e4SLinus Torvalds  *		a partial ACK arrives, we assume that one more packet
20451da177e4SLinus Torvalds  *		is lost (NewReno). This heuristics are the same in NewReno
20461da177e4SLinus Torvalds  *		and SACK.
20471da177e4SLinus Torvalds  *
20481da177e4SLinus Torvalds  *  Imagine, that's all! Forget about all this shamanism about CWND inflation
20491da177e4SLinus Torvalds  *  deflation etc. CWND is real congestion window, never inflated, changes
20501da177e4SLinus Torvalds  *  only according to classic VJ rules.
20511da177e4SLinus Torvalds  *
20521da177e4SLinus Torvalds  * Really tricky (and requiring careful tuning) part of algorithm
20531da177e4SLinus Torvalds  * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue().
20541da177e4SLinus Torvalds  * The first determines the moment _when_ we should reduce CWND and,
20551da177e4SLinus Torvalds  * hence, slow down forward transmission. In fact, it determines the moment
20561da177e4SLinus Torvalds  * when we decide that hole is caused by loss, rather than by a reorder.
20571da177e4SLinus Torvalds  *
20581da177e4SLinus Torvalds  * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill
20591da177e4SLinus Torvalds  * holes, caused by lost packets.
20601da177e4SLinus Torvalds  *
20611da177e4SLinus Torvalds  * And the most logically complicated part of algorithm is undo
20621da177e4SLinus Torvalds  * heuristics. We detect false retransmits due to both too early
20631da177e4SLinus Torvalds  * fast retransmit (reordering) and underestimated RTO, analyzing
20641da177e4SLinus Torvalds  * timestamps and D-SACKs. When we detect that some segments were
20651da177e4SLinus Torvalds  * retransmitted by mistake and CWND reduction was wrong, we undo
20661da177e4SLinus Torvalds  * window reduction and abort recovery phase. This logic is hidden
20671da177e4SLinus Torvalds  * inside several functions named tcp_try_undo_<something>.
20681da177e4SLinus Torvalds  */
20691da177e4SLinus Torvalds 
20701da177e4SLinus Torvalds /* This function decides, when we should leave Disordered state
20711da177e4SLinus Torvalds  * and enter Recovery phase, reducing congestion window.
20721da177e4SLinus Torvalds  *
20731da177e4SLinus Torvalds  * Main question: may we further continue forward transmission
20741da177e4SLinus Torvalds  * with the same cwnd?
20751da177e4SLinus Torvalds  */
2076a2a385d6SEric Dumazet static bool tcp_time_to_recover(struct sock *sk, int flag)
20771da177e4SLinus Torvalds {
20789e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
20791da177e4SLinus Torvalds 	__u32 packets_out;
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds 	/* Trick#1: The loss is proven. */
20821da177e4SLinus Torvalds 	if (tp->lost_out)
2083a2a385d6SEric Dumazet 		return true;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 	/* Not-A-Trick#2 : Classic rule... */
2086ea84e555SAndreas Petlund 	if (tcp_dupack_heuristics(tp) > tp->reordering)
2087a2a385d6SEric Dumazet 		return true;
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 	/* Trick#3 : when we use RFC2988 timer restart, fast
20901da177e4SLinus Torvalds 	 * retransmit can be triggered by timeout of queue head.
20911da177e4SLinus Torvalds 	 */
209285cc391cSIlpo Järvinen 	if (tcp_is_fack(tp) && tcp_head_timedout(sk))
2093a2a385d6SEric Dumazet 		return true;
20941da177e4SLinus Torvalds 
20951da177e4SLinus Torvalds 	/* Trick#4: It is still not OK... But will it be useful to delay
20961da177e4SLinus Torvalds 	 * recovery more?
20971da177e4SLinus Torvalds 	 */
20981da177e4SLinus Torvalds 	packets_out = tp->packets_out;
20991da177e4SLinus Torvalds 	if (packets_out <= tp->reordering &&
21001da177e4SLinus Torvalds 	    tp->sacked_out >= max_t(__u32, packets_out/2, sysctl_tcp_reordering) &&
21019e412ba7SIlpo Järvinen 	    !tcp_may_send_now(sk)) {
21021da177e4SLinus Torvalds 		/* We have nothing to send. This connection is limited
21031da177e4SLinus Torvalds 		 * either by receiver window or by application.
21041da177e4SLinus Torvalds 		 */
2105a2a385d6SEric Dumazet 		return true;
21061da177e4SLinus Torvalds 	}
21071da177e4SLinus Torvalds 
21087e380175SAndreas Petlund 	/* If a thin stream is detected, retransmit after first
21097e380175SAndreas Petlund 	 * received dupack. Employ only if SACK is supported in order
21107e380175SAndreas Petlund 	 * to avoid possible corner-case series of spurious retransmissions
21117e380175SAndreas Petlund 	 * Use only if there are no unsent data.
21127e380175SAndreas Petlund 	 */
21137e380175SAndreas Petlund 	if ((tp->thin_dupack || sysctl_tcp_thin_dupack) &&
21147e380175SAndreas Petlund 	    tcp_stream_is_thin(tp) && tcp_dupack_heuristics(tp) > 1 &&
21157e380175SAndreas Petlund 	    tcp_is_sack(tp) && !tcp_send_head(sk))
2116a2a385d6SEric Dumazet 		return true;
21177e380175SAndreas Petlund 
2118eed530b6SYuchung Cheng 	/* Trick#6: TCP early retransmit, per RFC5827.  To avoid spurious
2119eed530b6SYuchung Cheng 	 * retransmissions due to small network reorderings, we implement
2120eed530b6SYuchung Cheng 	 * Mitigation A.3 in the RFC and delay the retransmission for a short
2121eed530b6SYuchung Cheng 	 * interval if appropriate.
2122eed530b6SYuchung Cheng 	 */
2123eed530b6SYuchung Cheng 	if (tp->do_early_retrans && !tp->retrans_out && tp->sacked_out &&
21246ba8a3b1SNandita Dukkipati 	    (tp->packets_out >= (tp->sacked_out + 1) && tp->packets_out < 4) &&
2125eed530b6SYuchung Cheng 	    !tcp_may_send_now(sk))
2126750ea2baSYuchung Cheng 		return !tcp_pause_early_retransmit(sk, flag);
2127eed530b6SYuchung Cheng 
2128a2a385d6SEric Dumazet 	return false;
21291da177e4SLinus Torvalds }
21301da177e4SLinus Torvalds 
21317363a5b2SIlpo Järvinen /* New heuristics: it is possible only after we switched to restart timer
21327363a5b2SIlpo Järvinen  * each time when something is ACKed. Hence, we can detect timed out packets
21337363a5b2SIlpo Järvinen  * during fast retransmit without falling to slow start.
21347363a5b2SIlpo Järvinen  *
21357363a5b2SIlpo Järvinen  * Usefulness of this as is very questionable, since we should know which of
21367363a5b2SIlpo Järvinen  * the segments is the next to timeout which is relatively expensive to find
21377363a5b2SIlpo Järvinen  * in general case unless we add some data structure just for that. The
21387363a5b2SIlpo Järvinen  * current approach certainly won't find the right one too often and when it
21397363a5b2SIlpo Järvinen  * finally does find _something_ it usually marks large part of the window
21407363a5b2SIlpo Järvinen  * right away (because a retransmission with a larger timestamp blocks the
21417363a5b2SIlpo Järvinen  * loop from advancing). -ij
21427363a5b2SIlpo Järvinen  */
21437363a5b2SIlpo Järvinen static void tcp_timeout_skbs(struct sock *sk)
21447363a5b2SIlpo Järvinen {
21457363a5b2SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
21467363a5b2SIlpo Järvinen 	struct sk_buff *skb;
21477363a5b2SIlpo Järvinen 
21487363a5b2SIlpo Järvinen 	if (!tcp_is_fack(tp) || !tcp_head_timedout(sk))
21497363a5b2SIlpo Järvinen 		return;
21507363a5b2SIlpo Järvinen 
21517363a5b2SIlpo Järvinen 	skb = tp->scoreboard_skb_hint;
21527363a5b2SIlpo Järvinen 	if (tp->scoreboard_skb_hint == NULL)
21537363a5b2SIlpo Järvinen 		skb = tcp_write_queue_head(sk);
21547363a5b2SIlpo Järvinen 
21557363a5b2SIlpo Järvinen 	tcp_for_write_queue_from(skb, sk) {
21567363a5b2SIlpo Järvinen 		if (skb == tcp_send_head(sk))
21577363a5b2SIlpo Järvinen 			break;
21587363a5b2SIlpo Järvinen 		if (!tcp_skb_timedout(sk, skb))
21597363a5b2SIlpo Järvinen 			break;
21607363a5b2SIlpo Järvinen 
21617363a5b2SIlpo Järvinen 		tcp_skb_mark_lost(tp, skb);
21627363a5b2SIlpo Järvinen 	}
21637363a5b2SIlpo Järvinen 
21647363a5b2SIlpo Järvinen 	tp->scoreboard_skb_hint = skb;
21657363a5b2SIlpo Järvinen 
21667363a5b2SIlpo Järvinen 	tcp_verify_left_out(tp);
21677363a5b2SIlpo Järvinen }
21687363a5b2SIlpo Järvinen 
2169974c1236SYuchung Cheng /* Detect loss in event "A" above by marking head of queue up as lost.
2170974c1236SYuchung Cheng  * For FACK or non-SACK(Reno) senders, the first "packets" number of segments
2171974c1236SYuchung Cheng  * are considered lost. For RFC3517 SACK, a segment is considered lost if it
2172974c1236SYuchung Cheng  * has at least tp->reordering SACKed seqments above it; "packets" refers to
2173974c1236SYuchung Cheng  * the maximum SACKed segments to pass before reaching this limit.
217485cc391cSIlpo Järvinen  */
21751fdb9361SIlpo Järvinen static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)
21761da177e4SLinus Torvalds {
21779e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
21781da177e4SLinus Torvalds 	struct sk_buff *skb;
2179c137f3ddSIlpo Järvinen 	int cnt, oldcnt;
2180c137f3ddSIlpo Järvinen 	int err;
2181c137f3ddSIlpo Järvinen 	unsigned int mss;
2182974c1236SYuchung Cheng 	/* Use SACK to deduce losses of new sequences sent during recovery */
2183974c1236SYuchung Cheng 	const u32 loss_high = tcp_is_sack(tp) ?  tp->snd_nxt : tp->high_seq;
21841da177e4SLinus Torvalds 
2185547b792cSIlpo Järvinen 	WARN_ON(packets > tp->packets_out);
21866a438bbeSStephen Hemminger 	if (tp->lost_skb_hint) {
21876a438bbeSStephen Hemminger 		skb = tp->lost_skb_hint;
21886a438bbeSStephen Hemminger 		cnt = tp->lost_cnt_hint;
21891fdb9361SIlpo Järvinen 		/* Head already handled? */
21901fdb9361SIlpo Järvinen 		if (mark_head && skb != tcp_write_queue_head(sk))
21911fdb9361SIlpo Järvinen 			return;
21926a438bbeSStephen Hemminger 	} else {
2193fe067e8aSDavid S. Miller 		skb = tcp_write_queue_head(sk);
21946a438bbeSStephen Hemminger 		cnt = 0;
21956a438bbeSStephen Hemminger 	}
21961da177e4SLinus Torvalds 
2197fe067e8aSDavid S. Miller 	tcp_for_write_queue_from(skb, sk) {
2198fe067e8aSDavid S. Miller 		if (skb == tcp_send_head(sk))
2199fe067e8aSDavid S. Miller 			break;
22006a438bbeSStephen Hemminger 		/* TODO: do this better */
22016a438bbeSStephen Hemminger 		/* this is not the most efficient way to do this... */
22026a438bbeSStephen Hemminger 		tp->lost_skb_hint = skb;
22036a438bbeSStephen Hemminger 		tp->lost_cnt_hint = cnt;
220485cc391cSIlpo Järvinen 
2205974c1236SYuchung Cheng 		if (after(TCP_SKB_CB(skb)->end_seq, loss_high))
2206c137f3ddSIlpo Järvinen 			break;
2207c137f3ddSIlpo Järvinen 
2208c137f3ddSIlpo Järvinen 		oldcnt = cnt;
2209ad1984e8SIlpo Järvinen 		if (tcp_is_fack(tp) || tcp_is_reno(tp) ||
221085cc391cSIlpo Järvinen 		    (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
22116a438bbeSStephen Hemminger 			cnt += tcp_skb_pcount(skb);
221285cc391cSIlpo Järvinen 
2213c137f3ddSIlpo Järvinen 		if (cnt > packets) {
2214b3de7559SYuchung Cheng 			if ((tcp_is_sack(tp) && !tcp_is_fack(tp)) ||
2215c0638c24SNeal Cardwell 			    (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||
2216b3de7559SYuchung Cheng 			    (oldcnt >= packets))
22171da177e4SLinus Torvalds 				break;
2218c137f3ddSIlpo Järvinen 
2219c137f3ddSIlpo Järvinen 			mss = skb_shinfo(skb)->gso_size;
2220c137f3ddSIlpo Järvinen 			err = tcp_fragment(sk, skb, (packets - oldcnt) * mss, mss);
2221c137f3ddSIlpo Järvinen 			if (err < 0)
2222c137f3ddSIlpo Järvinen 				break;
2223c137f3ddSIlpo Järvinen 			cnt = packets;
2224c137f3ddSIlpo Järvinen 		}
2225c137f3ddSIlpo Järvinen 
222641ea36e3SIlpo Järvinen 		tcp_skb_mark_lost(tp, skb);
22271fdb9361SIlpo Järvinen 
22281fdb9361SIlpo Järvinen 		if (mark_head)
22291fdb9361SIlpo Järvinen 			break;
22301da177e4SLinus Torvalds 	}
2231005903bcSIlpo Järvinen 	tcp_verify_left_out(tp);
22321da177e4SLinus Torvalds }
22331da177e4SLinus Torvalds 
22341da177e4SLinus Torvalds /* Account newly detected lost packet(s) */
22351da177e4SLinus Torvalds 
223685cc391cSIlpo Järvinen static void tcp_update_scoreboard(struct sock *sk, int fast_rexmit)
22371da177e4SLinus Torvalds {
22389e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
22399e412ba7SIlpo Järvinen 
224085cc391cSIlpo Järvinen 	if (tcp_is_reno(tp)) {
22411fdb9361SIlpo Järvinen 		tcp_mark_head_lost(sk, 1, 1);
224285cc391cSIlpo Järvinen 	} else if (tcp_is_fack(tp)) {
22431da177e4SLinus Torvalds 		int lost = tp->fackets_out - tp->reordering;
22441da177e4SLinus Torvalds 		if (lost <= 0)
22451da177e4SLinus Torvalds 			lost = 1;
22461fdb9361SIlpo Järvinen 		tcp_mark_head_lost(sk, lost, 0);
22471da177e4SLinus Torvalds 	} else {
224885cc391cSIlpo Järvinen 		int sacked_upto = tp->sacked_out - tp->reordering;
22491fdb9361SIlpo Järvinen 		if (sacked_upto >= 0)
22501fdb9361SIlpo Järvinen 			tcp_mark_head_lost(sk, sacked_upto, 0);
22511fdb9361SIlpo Järvinen 		else if (fast_rexmit)
22521fdb9361SIlpo Järvinen 			tcp_mark_head_lost(sk, 1, 1);
22531da177e4SLinus Torvalds 	}
22541da177e4SLinus Torvalds 
22557363a5b2SIlpo Järvinen 	tcp_timeout_skbs(sk);
22561da177e4SLinus Torvalds }
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds /* CWND moderation, preventing bursts due to too big ACKs
22591da177e4SLinus Torvalds  * in dubious situations.
22601da177e4SLinus Torvalds  */
22611da177e4SLinus Torvalds static inline void tcp_moderate_cwnd(struct tcp_sock *tp)
22621da177e4SLinus Torvalds {
22631da177e4SLinus Torvalds 	tp->snd_cwnd = min(tp->snd_cwnd,
22641da177e4SLinus Torvalds 			   tcp_packets_in_flight(tp) + tcp_max_burst(tp));
22651da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
22661da177e4SLinus Torvalds }
22671da177e4SLinus Torvalds 
22681da177e4SLinus Torvalds /* Nothing was retransmitted or returned timestamp is less
22691da177e4SLinus Torvalds  * than timestamp of the first retransmission.
22701da177e4SLinus Torvalds  */
227167b95bd7SVijay Subramanian static inline bool tcp_packet_delayed(const struct tcp_sock *tp)
22721da177e4SLinus Torvalds {
22731da177e4SLinus Torvalds 	return !tp->retrans_stamp ||
22741da177e4SLinus Torvalds 		(tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
2275d7ee147dSArnd Hannemann 		 before(tp->rx_opt.rcv_tsecr, tp->retrans_stamp));
22761da177e4SLinus Torvalds }
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds /* Undo procedures. */
22791da177e4SLinus Torvalds 
22801da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 1
22819e412ba7SIlpo Järvinen static void DBGUNDO(struct sock *sk, const char *msg)
22821da177e4SLinus Torvalds {
22839e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
22841da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
22859e412ba7SIlpo Järvinen 
2286569508c9SYOSHIFUJI Hideaki 	if (sk->sk_family == AF_INET) {
228791df42beSJoe Perches 		pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n",
22881da177e4SLinus Torvalds 			 msg,
2289288fcee8SJoe Perches 			 &inet->inet_daddr, ntohs(inet->inet_dport),
229083ae4088SIlpo Järvinen 			 tp->snd_cwnd, tcp_left_out(tp),
22911da177e4SLinus Torvalds 			 tp->snd_ssthresh, tp->prior_ssthresh,
22921da177e4SLinus Torvalds 			 tp->packets_out);
22931da177e4SLinus Torvalds 	}
2294dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
2295569508c9SYOSHIFUJI Hideaki 	else if (sk->sk_family == AF_INET6) {
2296569508c9SYOSHIFUJI Hideaki 		struct ipv6_pinfo *np = inet6_sk(sk);
229791df42beSJoe Perches 		pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n",
2298569508c9SYOSHIFUJI Hideaki 			 msg,
2299288fcee8SJoe Perches 			 &np->daddr, ntohs(inet->inet_dport),
2300569508c9SYOSHIFUJI Hideaki 			 tp->snd_cwnd, tcp_left_out(tp),
2301569508c9SYOSHIFUJI Hideaki 			 tp->snd_ssthresh, tp->prior_ssthresh,
2302569508c9SYOSHIFUJI Hideaki 			 tp->packets_out);
2303569508c9SYOSHIFUJI Hideaki 	}
2304569508c9SYOSHIFUJI Hideaki #endif
2305569508c9SYOSHIFUJI Hideaki }
23061da177e4SLinus Torvalds #else
23071da177e4SLinus Torvalds #define DBGUNDO(x...) do { } while (0)
23081da177e4SLinus Torvalds #endif
23091da177e4SLinus Torvalds 
2310f6152737SDavid S. Miller static void tcp_undo_cwr(struct sock *sk, const bool undo_ssthresh)
23111da177e4SLinus Torvalds {
23126687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
23136687e988SArnaldo Carvalho de Melo 
23141da177e4SLinus Torvalds 	if (tp->prior_ssthresh) {
23156687e988SArnaldo Carvalho de Melo 		const struct inet_connection_sock *icsk = inet_csk(sk);
23166687e988SArnaldo Carvalho de Melo 
23176687e988SArnaldo Carvalho de Melo 		if (icsk->icsk_ca_ops->undo_cwnd)
23186687e988SArnaldo Carvalho de Melo 			tp->snd_cwnd = icsk->icsk_ca_ops->undo_cwnd(sk);
23191da177e4SLinus Torvalds 		else
23201da177e4SLinus Torvalds 			tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh << 1);
23211da177e4SLinus Torvalds 
232267d4120aSYuchung Cheng 		if (undo_ssthresh && tp->prior_ssthresh > tp->snd_ssthresh) {
23231da177e4SLinus Torvalds 			tp->snd_ssthresh = tp->prior_ssthresh;
23241da177e4SLinus Torvalds 			TCP_ECN_withdraw_cwr(tp);
23251da177e4SLinus Torvalds 		}
23261da177e4SLinus Torvalds 	} else {
23271da177e4SLinus Torvalds 		tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh);
23281da177e4SLinus Torvalds 	}
23291da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
23301da177e4SLinus Torvalds }
23311da177e4SLinus Torvalds 
233267b95bd7SVijay Subramanian static inline bool tcp_may_undo(const struct tcp_sock *tp)
23331da177e4SLinus Torvalds {
2334056834d9SIlpo Järvinen 	return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp));
23351da177e4SLinus Torvalds }
23361da177e4SLinus Torvalds 
23371da177e4SLinus Torvalds /* People celebrate: "We love our President!" */
2338a2a385d6SEric Dumazet static bool tcp_try_undo_recovery(struct sock *sk)
23391da177e4SLinus Torvalds {
23409e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
23419e412ba7SIlpo Järvinen 
23421da177e4SLinus Torvalds 	if (tcp_may_undo(tp)) {
234340b215e5SPavel Emelyanov 		int mib_idx;
234440b215e5SPavel Emelyanov 
23451da177e4SLinus Torvalds 		/* Happy end! We did not retransmit anything
23461da177e4SLinus Torvalds 		 * or our original transmission succeeded.
23471da177e4SLinus Torvalds 		 */
23489e412ba7SIlpo Järvinen 		DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans");
2349f6152737SDavid S. Miller 		tcp_undo_cwr(sk, true);
23506687e988SArnaldo Carvalho de Melo 		if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss)
235140b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPLOSSUNDO;
23521da177e4SLinus Torvalds 		else
235340b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPFULLUNDO;
235440b215e5SPavel Emelyanov 
2355de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), mib_idx);
23561da177e4SLinus Torvalds 		tp->undo_marker = 0;
23571da177e4SLinus Torvalds 	}
2358e60402d0SIlpo Järvinen 	if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) {
23591da177e4SLinus Torvalds 		/* Hold old state until something *above* high_seq
23601da177e4SLinus Torvalds 		 * is ACKed. For Reno it is MUST to prevent false
23611da177e4SLinus Torvalds 		 * fast retransmits (RFC2582). SACK TCP is safe. */
23621da177e4SLinus Torvalds 		tcp_moderate_cwnd(tp);
2363a2a385d6SEric Dumazet 		return true;
23641da177e4SLinus Torvalds 	}
23656687e988SArnaldo Carvalho de Melo 	tcp_set_ca_state(sk, TCP_CA_Open);
2366a2a385d6SEric Dumazet 	return false;
23671da177e4SLinus Torvalds }
23681da177e4SLinus Torvalds 
23691da177e4SLinus Torvalds /* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */
23709e412ba7SIlpo Järvinen static void tcp_try_undo_dsack(struct sock *sk)
23711da177e4SLinus Torvalds {
23729e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
23739e412ba7SIlpo Järvinen 
23741da177e4SLinus Torvalds 	if (tp->undo_marker && !tp->undo_retrans) {
23759e412ba7SIlpo Järvinen 		DBGUNDO(sk, "D-SACK");
2376f6152737SDavid S. Miller 		tcp_undo_cwr(sk, true);
23771da177e4SLinus Torvalds 		tp->undo_marker = 0;
2378de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKUNDO);
23791da177e4SLinus Torvalds 	}
23801da177e4SLinus Torvalds }
23811da177e4SLinus Torvalds 
238277722b17SIlpo Järvinen /* We can clear retrans_stamp when there are no retransmissions in the
238377722b17SIlpo Järvinen  * window. It would seem that it is trivially available for us in
238477722b17SIlpo Järvinen  * tp->retrans_out, however, that kind of assumptions doesn't consider
238577722b17SIlpo Järvinen  * what will happen if errors occur when sending retransmission for the
238677722b17SIlpo Järvinen  * second time. ...It could the that such segment has only
238777722b17SIlpo Järvinen  * TCPCB_EVER_RETRANS set at the present time. It seems that checking
238877722b17SIlpo Järvinen  * the head skb is enough except for some reneging corner cases that
238977722b17SIlpo Järvinen  * are not worth the effort.
239077722b17SIlpo Järvinen  *
239177722b17SIlpo Järvinen  * Main reason for all this complexity is the fact that connection dying
239277722b17SIlpo Järvinen  * time now depends on the validity of the retrans_stamp, in particular,
239377722b17SIlpo Järvinen  * that successive retransmissions of a segment must not advance
239477722b17SIlpo Järvinen  * retrans_stamp under any conditions.
239577722b17SIlpo Järvinen  */
2396a2a385d6SEric Dumazet static bool tcp_any_retrans_done(const struct sock *sk)
239777722b17SIlpo Järvinen {
2398cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
239977722b17SIlpo Järvinen 	struct sk_buff *skb;
240077722b17SIlpo Järvinen 
240177722b17SIlpo Järvinen 	if (tp->retrans_out)
2402a2a385d6SEric Dumazet 		return true;
240377722b17SIlpo Järvinen 
240477722b17SIlpo Järvinen 	skb = tcp_write_queue_head(sk);
240577722b17SIlpo Järvinen 	if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS))
2406a2a385d6SEric Dumazet 		return true;
240777722b17SIlpo Järvinen 
2408a2a385d6SEric Dumazet 	return false;
240977722b17SIlpo Järvinen }
241077722b17SIlpo Järvinen 
24111da177e4SLinus Torvalds /* Undo during fast recovery after partial ACK. */
24121da177e4SLinus Torvalds 
24139e412ba7SIlpo Järvinen static int tcp_try_undo_partial(struct sock *sk, int acked)
24141da177e4SLinus Torvalds {
24159e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
24161da177e4SLinus Torvalds 	/* Partial ACK arrived. Force Hoe's retransmit. */
241785cc391cSIlpo Järvinen 	int failed = tcp_is_reno(tp) || (tcp_fackets_out(tp) > tp->reordering);
24181da177e4SLinus Torvalds 
24191da177e4SLinus Torvalds 	if (tcp_may_undo(tp)) {
24201da177e4SLinus Torvalds 		/* Plain luck! Hole if filled with delayed
24211da177e4SLinus Torvalds 		 * packet, rather than with a retransmit.
24221da177e4SLinus Torvalds 		 */
242377722b17SIlpo Järvinen 		if (!tcp_any_retrans_done(sk))
24241da177e4SLinus Torvalds 			tp->retrans_stamp = 0;
24251da177e4SLinus Torvalds 
24266687e988SArnaldo Carvalho de Melo 		tcp_update_reordering(sk, tcp_fackets_out(tp) + acked, 1);
24271da177e4SLinus Torvalds 
24289e412ba7SIlpo Järvinen 		DBGUNDO(sk, "Hoe");
2429f6152737SDavid S. Miller 		tcp_undo_cwr(sk, false);
2430de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO);
24311da177e4SLinus Torvalds 
24321da177e4SLinus Torvalds 		/* So... Do not make Hoe's retransmit yet.
24331da177e4SLinus Torvalds 		 * If the first packet was delayed, the rest
24341da177e4SLinus Torvalds 		 * ones are most probably delayed as well.
24351da177e4SLinus Torvalds 		 */
24361da177e4SLinus Torvalds 		failed = 0;
24371da177e4SLinus Torvalds 	}
24381da177e4SLinus Torvalds 	return failed;
24391da177e4SLinus Torvalds }
24401da177e4SLinus Torvalds 
2441e33099f9SYuchung Cheng /* Undo during loss recovery after partial ACK or using F-RTO. */
2442e33099f9SYuchung Cheng static bool tcp_try_undo_loss(struct sock *sk, bool frto_undo)
24431da177e4SLinus Torvalds {
24449e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
24459e412ba7SIlpo Järvinen 
2446e33099f9SYuchung Cheng 	if (frto_undo || tcp_may_undo(tp)) {
24471da177e4SLinus Torvalds 		struct sk_buff *skb;
2448fe067e8aSDavid S. Miller 		tcp_for_write_queue(skb, sk) {
2449fe067e8aSDavid S. Miller 			if (skb == tcp_send_head(sk))
2450fe067e8aSDavid S. Miller 				break;
24511da177e4SLinus Torvalds 			TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
24521da177e4SLinus Torvalds 		}
24536a438bbeSStephen Hemminger 
24545af4ec23SIlpo Järvinen 		tcp_clear_all_retrans_hints(tp);
24556a438bbeSStephen Hemminger 
24569e412ba7SIlpo Järvinen 		DBGUNDO(sk, "partial loss");
24571da177e4SLinus Torvalds 		tp->lost_out = 0;
2458f6152737SDavid S. Miller 		tcp_undo_cwr(sk, true);
2459de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSSUNDO);
2460e33099f9SYuchung Cheng 		if (frto_undo)
2461e33099f9SYuchung Cheng 			NET_INC_STATS_BH(sock_net(sk),
2462e33099f9SYuchung Cheng 					 LINUX_MIB_TCPSPURIOUSRTOS);
2463463c84b9SArnaldo Carvalho de Melo 		inet_csk(sk)->icsk_retransmits = 0;
24641da177e4SLinus Torvalds 		tp->undo_marker = 0;
2465e33099f9SYuchung Cheng 		if (frto_undo || tcp_is_sack(tp))
24666687e988SArnaldo Carvalho de Melo 			tcp_set_ca_state(sk, TCP_CA_Open);
2467a2a385d6SEric Dumazet 		return true;
24681da177e4SLinus Torvalds 	}
2469a2a385d6SEric Dumazet 	return false;
24701da177e4SLinus Torvalds }
24711da177e4SLinus Torvalds 
2472684bad11SYuchung Cheng /* The cwnd reduction in CWR and Recovery use the PRR algorithm
2473684bad11SYuchung Cheng  * https://datatracker.ietf.org/doc/draft-ietf-tcpm-proportional-rate-reduction/
2474fb4d3d1dSYuchung Cheng  * It computes the number of packets to send (sndcnt) based on packets newly
2475fb4d3d1dSYuchung Cheng  * delivered:
2476fb4d3d1dSYuchung Cheng  *   1) If the packets in flight is larger than ssthresh, PRR spreads the
2477fb4d3d1dSYuchung Cheng  *	cwnd reductions across a full RTT.
2478fb4d3d1dSYuchung Cheng  *   2) If packets in flight is lower than ssthresh (such as due to excess
2479fb4d3d1dSYuchung Cheng  *	losses and/or application stalls), do not perform any further cwnd
2480fb4d3d1dSYuchung Cheng  *	reductions, but instead slow start up to ssthresh.
2481fb4d3d1dSYuchung Cheng  */
2482684bad11SYuchung Cheng static void tcp_init_cwnd_reduction(struct sock *sk, const bool set_ssthresh)
2483684bad11SYuchung Cheng {
2484684bad11SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
2485684bad11SYuchung Cheng 
2486684bad11SYuchung Cheng 	tp->high_seq = tp->snd_nxt;
24879b717a8dSNandita Dukkipati 	tp->tlp_high_seq = 0;
2488684bad11SYuchung Cheng 	tp->snd_cwnd_cnt = 0;
2489684bad11SYuchung Cheng 	tp->prior_cwnd = tp->snd_cwnd;
2490684bad11SYuchung Cheng 	tp->prr_delivered = 0;
2491684bad11SYuchung Cheng 	tp->prr_out = 0;
2492684bad11SYuchung Cheng 	if (set_ssthresh)
2493684bad11SYuchung Cheng 		tp->snd_ssthresh = inet_csk(sk)->icsk_ca_ops->ssthresh(sk);
2494684bad11SYuchung Cheng 	TCP_ECN_queue_cwr(tp);
2495684bad11SYuchung Cheng }
2496684bad11SYuchung Cheng 
2497684bad11SYuchung Cheng static void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked,
2498684bad11SYuchung Cheng 			       int fast_rexmit)
2499fb4d3d1dSYuchung Cheng {
2500fb4d3d1dSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
2501fb4d3d1dSYuchung Cheng 	int sndcnt = 0;
2502fb4d3d1dSYuchung Cheng 	int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp);
2503fb4d3d1dSYuchung Cheng 
2504684bad11SYuchung Cheng 	tp->prr_delivered += newly_acked_sacked;
2505fb4d3d1dSYuchung Cheng 	if (tcp_packets_in_flight(tp) > tp->snd_ssthresh) {
2506fb4d3d1dSYuchung Cheng 		u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered +
2507fb4d3d1dSYuchung Cheng 			       tp->prior_cwnd - 1;
2508fb4d3d1dSYuchung Cheng 		sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out;
2509fb4d3d1dSYuchung Cheng 	} else {
2510fb4d3d1dSYuchung Cheng 		sndcnt = min_t(int, delta,
2511fb4d3d1dSYuchung Cheng 			       max_t(int, tp->prr_delivered - tp->prr_out,
2512fb4d3d1dSYuchung Cheng 				     newly_acked_sacked) + 1);
2513fb4d3d1dSYuchung Cheng 	}
2514fb4d3d1dSYuchung Cheng 
2515fb4d3d1dSYuchung Cheng 	sndcnt = max(sndcnt, (fast_rexmit ? 1 : 0));
2516fb4d3d1dSYuchung Cheng 	tp->snd_cwnd = tcp_packets_in_flight(tp) + sndcnt;
2517fb4d3d1dSYuchung Cheng }
2518fb4d3d1dSYuchung Cheng 
2519684bad11SYuchung Cheng static inline void tcp_end_cwnd_reduction(struct sock *sk)
25201da177e4SLinus Torvalds {
25216687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
2522a262f0cdSNandita Dukkipati 
2523684bad11SYuchung Cheng 	/* Reset cwnd to ssthresh in CWR or Recovery (unless it's undone) */
2524684bad11SYuchung Cheng 	if (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR ||
2525684bad11SYuchung Cheng 	    (tp->undo_marker && tp->snd_ssthresh < TCP_INFINITE_SSTHRESH)) {
252667d4120aSYuchung Cheng 		tp->snd_cwnd = tp->snd_ssthresh;
25271da177e4SLinus Torvalds 		tp->snd_cwnd_stamp = tcp_time_stamp;
252867d4120aSYuchung Cheng 	}
25296687e988SArnaldo Carvalho de Melo 	tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR);
25301da177e4SLinus Torvalds }
25311da177e4SLinus Torvalds 
2532684bad11SYuchung Cheng /* Enter CWR state. Disable cwnd undo since congestion is proven with ECN */
253309484d1fSYuchung Cheng void tcp_enter_cwr(struct sock *sk, const int set_ssthresh)
253409484d1fSYuchung Cheng {
253509484d1fSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
253609484d1fSYuchung Cheng 
253709484d1fSYuchung Cheng 	tp->prior_ssthresh = 0;
2538684bad11SYuchung Cheng 	if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
253909484d1fSYuchung Cheng 		tp->undo_marker = 0;
2540684bad11SYuchung Cheng 		tcp_init_cwnd_reduction(sk, set_ssthresh);
254109484d1fSYuchung Cheng 		tcp_set_ca_state(sk, TCP_CA_CWR);
254209484d1fSYuchung Cheng 	}
254309484d1fSYuchung Cheng }
254409484d1fSYuchung Cheng 
25458aca6cb1SIlpo Järvinen static void tcp_try_keep_open(struct sock *sk)
25468aca6cb1SIlpo Järvinen {
25478aca6cb1SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
25488aca6cb1SIlpo Järvinen 	int state = TCP_CA_Open;
25498aca6cb1SIlpo Järvinen 
2550f698204bSNeal Cardwell 	if (tcp_left_out(tp) || tcp_any_retrans_done(sk))
25518aca6cb1SIlpo Järvinen 		state = TCP_CA_Disorder;
25528aca6cb1SIlpo Järvinen 
25538aca6cb1SIlpo Järvinen 	if (inet_csk(sk)->icsk_ca_state != state) {
25548aca6cb1SIlpo Järvinen 		tcp_set_ca_state(sk, state);
25558aca6cb1SIlpo Järvinen 		tp->high_seq = tp->snd_nxt;
25568aca6cb1SIlpo Järvinen 	}
25578aca6cb1SIlpo Järvinen }
25588aca6cb1SIlpo Järvinen 
2559684bad11SYuchung Cheng static void tcp_try_to_open(struct sock *sk, int flag, int newly_acked_sacked)
25601da177e4SLinus Torvalds {
25619e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
25629e412ba7SIlpo Järvinen 
256386426c22SIlpo Järvinen 	tcp_verify_left_out(tp);
256486426c22SIlpo Järvinen 
25659b44190dSYuchung Cheng 	if (!tcp_any_retrans_done(sk))
25661da177e4SLinus Torvalds 		tp->retrans_stamp = 0;
25671da177e4SLinus Torvalds 
25681da177e4SLinus Torvalds 	if (flag & FLAG_ECE)
25693cfe3baaSIlpo Järvinen 		tcp_enter_cwr(sk, 1);
25701da177e4SLinus Torvalds 
25716687e988SArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) {
25728aca6cb1SIlpo Järvinen 		tcp_try_keep_open(sk);
25738cd6d616SNeal Cardwell 		if (inet_csk(sk)->icsk_ca_state != TCP_CA_Open)
25741da177e4SLinus Torvalds 			tcp_moderate_cwnd(tp);
25751da177e4SLinus Torvalds 	} else {
2576684bad11SYuchung Cheng 		tcp_cwnd_reduction(sk, newly_acked_sacked, 0);
25771da177e4SLinus Torvalds 	}
25781da177e4SLinus Torvalds }
25791da177e4SLinus Torvalds 
25805d424d5aSJohn Heffner static void tcp_mtup_probe_failed(struct sock *sk)
25815d424d5aSJohn Heffner {
25825d424d5aSJohn Heffner 	struct inet_connection_sock *icsk = inet_csk(sk);
25835d424d5aSJohn Heffner 
25845d424d5aSJohn Heffner 	icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1;
25855d424d5aSJohn Heffner 	icsk->icsk_mtup.probe_size = 0;
25865d424d5aSJohn Heffner }
25875d424d5aSJohn Heffner 
258872211e90SIlpo Järvinen static void tcp_mtup_probe_success(struct sock *sk)
25895d424d5aSJohn Heffner {
25905d424d5aSJohn Heffner 	struct tcp_sock *tp = tcp_sk(sk);
25915d424d5aSJohn Heffner 	struct inet_connection_sock *icsk = inet_csk(sk);
25925d424d5aSJohn Heffner 
25935d424d5aSJohn Heffner 	/* FIXME: breaks with very large cwnd */
25945d424d5aSJohn Heffner 	tp->prior_ssthresh = tcp_current_ssthresh(sk);
25955d424d5aSJohn Heffner 	tp->snd_cwnd = tp->snd_cwnd *
25965d424d5aSJohn Heffner 		       tcp_mss_to_mtu(sk, tp->mss_cache) /
25975d424d5aSJohn Heffner 		       icsk->icsk_mtup.probe_size;
25985d424d5aSJohn Heffner 	tp->snd_cwnd_cnt = 0;
25995d424d5aSJohn Heffner 	tp->snd_cwnd_stamp = tcp_time_stamp;
26009c6d5e55SJohn Heffner 	tp->snd_ssthresh = tcp_current_ssthresh(sk);
26015d424d5aSJohn Heffner 
26025d424d5aSJohn Heffner 	icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size;
26035d424d5aSJohn Heffner 	icsk->icsk_mtup.probe_size = 0;
26045d424d5aSJohn Heffner 	tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
26055d424d5aSJohn Heffner }
26065d424d5aSJohn Heffner 
2607e1aa680fSIlpo Järvinen /* Do a simple retransmit without using the backoff mechanisms in
2608e1aa680fSIlpo Järvinen  * tcp_timer. This is used for path mtu discovery.
2609e1aa680fSIlpo Järvinen  * The socket is already locked here.
2610e1aa680fSIlpo Järvinen  */
2611e1aa680fSIlpo Järvinen void tcp_simple_retransmit(struct sock *sk)
2612e1aa680fSIlpo Järvinen {
2613e1aa680fSIlpo Järvinen 	const struct inet_connection_sock *icsk = inet_csk(sk);
2614e1aa680fSIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
2615e1aa680fSIlpo Järvinen 	struct sk_buff *skb;
26160c54b85fSIlpo Järvinen 	unsigned int mss = tcp_current_mss(sk);
2617e1aa680fSIlpo Järvinen 	u32 prior_lost = tp->lost_out;
2618e1aa680fSIlpo Järvinen 
2619e1aa680fSIlpo Järvinen 	tcp_for_write_queue(skb, sk) {
2620e1aa680fSIlpo Järvinen 		if (skb == tcp_send_head(sk))
2621e1aa680fSIlpo Järvinen 			break;
2622775ffabfSIlpo Järvinen 		if (tcp_skb_seglen(skb) > mss &&
2623e1aa680fSIlpo Järvinen 		    !(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) {
2624e1aa680fSIlpo Järvinen 			if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
2625e1aa680fSIlpo Järvinen 				TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
2626e1aa680fSIlpo Järvinen 				tp->retrans_out -= tcp_skb_pcount(skb);
2627e1aa680fSIlpo Järvinen 			}
2628e1aa680fSIlpo Järvinen 			tcp_skb_mark_lost_uncond_verify(tp, skb);
2629e1aa680fSIlpo Järvinen 		}
2630e1aa680fSIlpo Järvinen 	}
2631e1aa680fSIlpo Järvinen 
2632e1aa680fSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
2633e1aa680fSIlpo Järvinen 
2634e1aa680fSIlpo Järvinen 	if (prior_lost == tp->lost_out)
2635e1aa680fSIlpo Järvinen 		return;
2636e1aa680fSIlpo Järvinen 
2637e1aa680fSIlpo Järvinen 	if (tcp_is_reno(tp))
2638e1aa680fSIlpo Järvinen 		tcp_limit_reno_sacked(tp);
2639e1aa680fSIlpo Järvinen 
2640e1aa680fSIlpo Järvinen 	tcp_verify_left_out(tp);
2641e1aa680fSIlpo Järvinen 
2642e1aa680fSIlpo Järvinen 	/* Don't muck with the congestion window here.
2643e1aa680fSIlpo Järvinen 	 * Reason is that we do not increase amount of _data_
2644e1aa680fSIlpo Järvinen 	 * in network, but units changed and effective
2645e1aa680fSIlpo Järvinen 	 * cwnd/ssthresh really reduced now.
2646e1aa680fSIlpo Järvinen 	 */
2647e1aa680fSIlpo Järvinen 	if (icsk->icsk_ca_state != TCP_CA_Loss) {
2648e1aa680fSIlpo Järvinen 		tp->high_seq = tp->snd_nxt;
2649e1aa680fSIlpo Järvinen 		tp->snd_ssthresh = tcp_current_ssthresh(sk);
2650e1aa680fSIlpo Järvinen 		tp->prior_ssthresh = 0;
2651e1aa680fSIlpo Järvinen 		tp->undo_marker = 0;
2652e1aa680fSIlpo Järvinen 		tcp_set_ca_state(sk, TCP_CA_Loss);
2653e1aa680fSIlpo Järvinen 	}
2654e1aa680fSIlpo Järvinen 	tcp_xmit_retransmit_queue(sk);
2655e1aa680fSIlpo Järvinen }
26564bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_simple_retransmit);
2657e1aa680fSIlpo Järvinen 
26581fbc3405SYuchung Cheng static void tcp_enter_recovery(struct sock *sk, bool ece_ack)
26591fbc3405SYuchung Cheng {
26601fbc3405SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
26611fbc3405SYuchung Cheng 	int mib_idx;
26621fbc3405SYuchung Cheng 
26631fbc3405SYuchung Cheng 	if (tcp_is_reno(tp))
26641fbc3405SYuchung Cheng 		mib_idx = LINUX_MIB_TCPRENORECOVERY;
26651fbc3405SYuchung Cheng 	else
26661fbc3405SYuchung Cheng 		mib_idx = LINUX_MIB_TCPSACKRECOVERY;
26671fbc3405SYuchung Cheng 
26681fbc3405SYuchung Cheng 	NET_INC_STATS_BH(sock_net(sk), mib_idx);
26691fbc3405SYuchung Cheng 
26701fbc3405SYuchung Cheng 	tp->prior_ssthresh = 0;
26711fbc3405SYuchung Cheng 	tp->undo_marker = tp->snd_una;
26721fbc3405SYuchung Cheng 	tp->undo_retrans = tp->retrans_out;
26731fbc3405SYuchung Cheng 
26741fbc3405SYuchung Cheng 	if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
26751fbc3405SYuchung Cheng 		if (!ece_ack)
26761fbc3405SYuchung Cheng 			tp->prior_ssthresh = tcp_current_ssthresh(sk);
2677684bad11SYuchung Cheng 		tcp_init_cwnd_reduction(sk, true);
26781fbc3405SYuchung Cheng 	}
26791fbc3405SYuchung Cheng 	tcp_set_ca_state(sk, TCP_CA_Recovery);
26801fbc3405SYuchung Cheng }
26811fbc3405SYuchung Cheng 
2682ab42d9eeSYuchung Cheng /* Process an ACK in CA_Loss state. Move to CA_Open if lost data are
2683ab42d9eeSYuchung Cheng  * recovered or spurious. Otherwise retransmits more on partial ACKs.
2684ab42d9eeSYuchung Cheng  */
2685e33099f9SYuchung Cheng static void tcp_process_loss(struct sock *sk, int flag, bool is_dupack)
2686ab42d9eeSYuchung Cheng {
2687ab42d9eeSYuchung Cheng 	struct inet_connection_sock *icsk = inet_csk(sk);
2688ab42d9eeSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
2689e33099f9SYuchung Cheng 	bool recovered = !before(tp->snd_una, tp->high_seq);
2690ab42d9eeSYuchung Cheng 
2691e33099f9SYuchung Cheng 	if (tp->frto) { /* F-RTO RFC5682 sec 3.1 (sack enhanced version). */
2692e33099f9SYuchung Cheng 		if (flag & FLAG_ORIG_SACK_ACKED) {
2693e33099f9SYuchung Cheng 			/* Step 3.b. A timeout is spurious if not all data are
2694e33099f9SYuchung Cheng 			 * lost, i.e., never-retransmitted data are (s)acked.
2695e33099f9SYuchung Cheng 			 */
2696e33099f9SYuchung Cheng 			tcp_try_undo_loss(sk, true);
2697e33099f9SYuchung Cheng 			return;
2698e33099f9SYuchung Cheng 		}
2699e33099f9SYuchung Cheng 		if (after(tp->snd_nxt, tp->high_seq) &&
2700e33099f9SYuchung Cheng 		    (flag & FLAG_DATA_SACKED || is_dupack)) {
2701e33099f9SYuchung Cheng 			tp->frto = 0; /* Loss was real: 2nd part of step 3.a */
2702e33099f9SYuchung Cheng 		} else if (flag & FLAG_SND_UNA_ADVANCED && !recovered) {
2703e33099f9SYuchung Cheng 			tp->high_seq = tp->snd_nxt;
2704e33099f9SYuchung Cheng 			__tcp_push_pending_frames(sk, tcp_current_mss(sk),
2705e33099f9SYuchung Cheng 						  TCP_NAGLE_OFF);
2706e33099f9SYuchung Cheng 			if (after(tp->snd_nxt, tp->high_seq))
2707e33099f9SYuchung Cheng 				return; /* Step 2.b */
2708e33099f9SYuchung Cheng 			tp->frto = 0;
2709e33099f9SYuchung Cheng 		}
2710e33099f9SYuchung Cheng 	}
2711e33099f9SYuchung Cheng 
2712e33099f9SYuchung Cheng 	if (recovered) {
2713e33099f9SYuchung Cheng 		/* F-RTO RFC5682 sec 3.1 step 2.a and 1st part of step 3.a */
2714ab42d9eeSYuchung Cheng 		icsk->icsk_retransmits = 0;
2715ab42d9eeSYuchung Cheng 		tcp_try_undo_recovery(sk);
2716ab42d9eeSYuchung Cheng 		return;
2717ab42d9eeSYuchung Cheng 	}
2718ab42d9eeSYuchung Cheng 	if (flag & FLAG_DATA_ACKED)
2719ab42d9eeSYuchung Cheng 		icsk->icsk_retransmits = 0;
2720e33099f9SYuchung Cheng 	if (tcp_is_reno(tp)) {
2721e33099f9SYuchung Cheng 		/* A Reno DUPACK means new data in F-RTO step 2.b above are
2722e33099f9SYuchung Cheng 		 * delivered. Lower inflight to clock out (re)tranmissions.
2723e33099f9SYuchung Cheng 		 */
2724e33099f9SYuchung Cheng 		if (after(tp->snd_nxt, tp->high_seq) && is_dupack)
2725e33099f9SYuchung Cheng 			tcp_add_reno_sack(sk);
2726e33099f9SYuchung Cheng 		else if (flag & FLAG_SND_UNA_ADVANCED)
2727ab42d9eeSYuchung Cheng 			tcp_reset_reno_sack(tp);
2728e33099f9SYuchung Cheng 	}
2729e33099f9SYuchung Cheng 	if (tcp_try_undo_loss(sk, false))
2730ab42d9eeSYuchung Cheng 		return;
2731ab42d9eeSYuchung Cheng 	tcp_xmit_retransmit_queue(sk);
2732ab42d9eeSYuchung Cheng }
2733ab42d9eeSYuchung Cheng 
27341da177e4SLinus Torvalds /* Process an event, which can update packets-in-flight not trivially.
27351da177e4SLinus Torvalds  * Main goal of this function is to calculate new estimate for left_out,
27361da177e4SLinus Torvalds  * taking into account both packets sitting in receiver's buffer and
27371da177e4SLinus Torvalds  * packets lost by network.
27381da177e4SLinus Torvalds  *
27391da177e4SLinus Torvalds  * Besides that it does CWND reduction, when packet loss is detected
27401da177e4SLinus Torvalds  * and changes state of machine.
27411da177e4SLinus Torvalds  *
27421da177e4SLinus Torvalds  * It does _not_ decide what to send, it is made in function
27431da177e4SLinus Torvalds  * tcp_xmit_retransmit_queue().
27441da177e4SLinus Torvalds  */
2745a262f0cdSNandita Dukkipati static void tcp_fastretrans_alert(struct sock *sk, int pkts_acked,
2746*35f079ebSNandita Dukkipati 				  int prior_sacked, int prior_packets,
2747*35f079ebSNandita Dukkipati 				  bool is_dupack, int flag)
27481da177e4SLinus Torvalds {
27496687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
27501da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
27512e605294SIlpo Järvinen 	int do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) &&
275285cc391cSIlpo Järvinen 				    (tcp_fackets_out(tp) > tp->reordering));
27537c4a56feSYuchung Cheng 	int newly_acked_sacked = 0;
27541fbc3405SYuchung Cheng 	int fast_rexmit = 0;
27551da177e4SLinus Torvalds 
27563ccd3130SIlpo Järvinen 	if (WARN_ON(!tp->packets_out && tp->sacked_out))
27571da177e4SLinus Torvalds 		tp->sacked_out = 0;
275891fed7a1SIlpo Järvinen 	if (WARN_ON(!tp->sacked_out && tp->fackets_out))
27591da177e4SLinus Torvalds 		tp->fackets_out = 0;
27601da177e4SLinus Torvalds 
27611da177e4SLinus Torvalds 	/* Now state machine starts.
27621da177e4SLinus Torvalds 	 * A. ECE, hence prohibit cwnd undoing, the reduction is required. */
27631da177e4SLinus Torvalds 	if (flag & FLAG_ECE)
27641da177e4SLinus Torvalds 		tp->prior_ssthresh = 0;
27651da177e4SLinus Torvalds 
27661da177e4SLinus Torvalds 	/* B. In all the states check for reneging SACKs. */
2767cadbd031SIlpo Järvinen 	if (tcp_check_sack_reneging(sk, flag))
27681da177e4SLinus Torvalds 		return;
27691da177e4SLinus Torvalds 
2770974c1236SYuchung Cheng 	/* C. Check consistency of the current state. */
2771005903bcSIlpo Järvinen 	tcp_verify_left_out(tp);
27721da177e4SLinus Torvalds 
2773974c1236SYuchung Cheng 	/* D. Check state exit conditions. State can be terminated
27741da177e4SLinus Torvalds 	 *    when high_seq is ACKed. */
27756687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_state == TCP_CA_Open) {
2776547b792cSIlpo Järvinen 		WARN_ON(tp->retrans_out != 0);
27771da177e4SLinus Torvalds 		tp->retrans_stamp = 0;
27781da177e4SLinus Torvalds 	} else if (!before(tp->snd_una, tp->high_seq)) {
27796687e988SArnaldo Carvalho de Melo 		switch (icsk->icsk_ca_state) {
27801da177e4SLinus Torvalds 		case TCP_CA_CWR:
27811da177e4SLinus Torvalds 			/* CWR is to be held something *above* high_seq
27821da177e4SLinus Torvalds 			 * is ACKed for CWR bit to reach receiver. */
27831da177e4SLinus Torvalds 			if (tp->snd_una != tp->high_seq) {
2784684bad11SYuchung Cheng 				tcp_end_cwnd_reduction(sk);
27856687e988SArnaldo Carvalho de Melo 				tcp_set_ca_state(sk, TCP_CA_Open);
27861da177e4SLinus Torvalds 			}
27871da177e4SLinus Torvalds 			break;
27881da177e4SLinus Torvalds 
27891da177e4SLinus Torvalds 		case TCP_CA_Recovery:
2790e60402d0SIlpo Järvinen 			if (tcp_is_reno(tp))
27911da177e4SLinus Torvalds 				tcp_reset_reno_sack(tp);
27929e412ba7SIlpo Järvinen 			if (tcp_try_undo_recovery(sk))
27931da177e4SLinus Torvalds 				return;
2794684bad11SYuchung Cheng 			tcp_end_cwnd_reduction(sk);
27951da177e4SLinus Torvalds 			break;
27961da177e4SLinus Torvalds 		}
27971da177e4SLinus Torvalds 	}
27981da177e4SLinus Torvalds 
2799974c1236SYuchung Cheng 	/* E. Process state. */
28006687e988SArnaldo Carvalho de Melo 	switch (icsk->icsk_ca_state) {
28011da177e4SLinus Torvalds 	case TCP_CA_Recovery:
28022e605294SIlpo Järvinen 		if (!(flag & FLAG_SND_UNA_ADVANCED)) {
2803e60402d0SIlpo Järvinen 			if (tcp_is_reno(tp) && is_dupack)
28046687e988SArnaldo Carvalho de Melo 				tcp_add_reno_sack(sk);
28051b6d427bSIlpo Järvinen 		} else
28061b6d427bSIlpo Järvinen 			do_lost = tcp_try_undo_partial(sk, pkts_acked);
2807*35f079ebSNandita Dukkipati 		newly_acked_sacked = prior_packets - tp->packets_out +
2808*35f079ebSNandita Dukkipati 				     tp->sacked_out - prior_sacked;
28091da177e4SLinus Torvalds 		break;
28101da177e4SLinus Torvalds 	case TCP_CA_Loss:
2811e33099f9SYuchung Cheng 		tcp_process_loss(sk, flag, is_dupack);
28126687e988SArnaldo Carvalho de Melo 		if (icsk->icsk_ca_state != TCP_CA_Open)
28131da177e4SLinus Torvalds 			return;
2814ab42d9eeSYuchung Cheng 		/* Fall through to processing in Open state. */
28151da177e4SLinus Torvalds 	default:
2816e60402d0SIlpo Järvinen 		if (tcp_is_reno(tp)) {
28172e605294SIlpo Järvinen 			if (flag & FLAG_SND_UNA_ADVANCED)
28181da177e4SLinus Torvalds 				tcp_reset_reno_sack(tp);
28191da177e4SLinus Torvalds 			if (is_dupack)
28206687e988SArnaldo Carvalho de Melo 				tcp_add_reno_sack(sk);
28211da177e4SLinus Torvalds 		}
2822*35f079ebSNandita Dukkipati 		newly_acked_sacked = prior_packets - tp->packets_out +
2823*35f079ebSNandita Dukkipati 				     tp->sacked_out - prior_sacked;
28241da177e4SLinus Torvalds 
2825f698204bSNeal Cardwell 		if (icsk->icsk_ca_state <= TCP_CA_Disorder)
28269e412ba7SIlpo Järvinen 			tcp_try_undo_dsack(sk);
28271da177e4SLinus Torvalds 
2828750ea2baSYuchung Cheng 		if (!tcp_time_to_recover(sk, flag)) {
2829684bad11SYuchung Cheng 			tcp_try_to_open(sk, flag, newly_acked_sacked);
28301da177e4SLinus Torvalds 			return;
28311da177e4SLinus Torvalds 		}
28321da177e4SLinus Torvalds 
28335d424d5aSJohn Heffner 		/* MTU probe failure: don't reduce cwnd */
28345d424d5aSJohn Heffner 		if (icsk->icsk_ca_state < TCP_CA_CWR &&
28355d424d5aSJohn Heffner 		    icsk->icsk_mtup.probe_size &&
28360e7b1368SJohn Heffner 		    tp->snd_una == tp->mtu_probe.probe_seq_start) {
28375d424d5aSJohn Heffner 			tcp_mtup_probe_failed(sk);
28385d424d5aSJohn Heffner 			/* Restores the reduction we did in tcp_mtup_probe() */
28395d424d5aSJohn Heffner 			tp->snd_cwnd++;
28405d424d5aSJohn Heffner 			tcp_simple_retransmit(sk);
28415d424d5aSJohn Heffner 			return;
28425d424d5aSJohn Heffner 		}
28435d424d5aSJohn Heffner 
28441da177e4SLinus Torvalds 		/* Otherwise enter Recovery state */
28451fbc3405SYuchung Cheng 		tcp_enter_recovery(sk, (flag & FLAG_ECE));
284685cc391cSIlpo Järvinen 		fast_rexmit = 1;
28471da177e4SLinus Torvalds 	}
28481da177e4SLinus Torvalds 
284985cc391cSIlpo Järvinen 	if (do_lost || (tcp_is_fack(tp) && tcp_head_timedout(sk)))
285085cc391cSIlpo Järvinen 		tcp_update_scoreboard(sk, fast_rexmit);
2851684bad11SYuchung Cheng 	tcp_cwnd_reduction(sk, newly_acked_sacked, fast_rexmit);
28521da177e4SLinus Torvalds 	tcp_xmit_retransmit_queue(sk);
28531da177e4SLinus Torvalds }
28541da177e4SLinus Torvalds 
28559ad7c049SJerry Chu void tcp_valid_rtt_meas(struct sock *sk, u32 seq_rtt)
285641834b73SIlpo Järvinen {
285741834b73SIlpo Järvinen 	tcp_rtt_estimator(sk, seq_rtt);
285841834b73SIlpo Järvinen 	tcp_set_rto(sk);
285941834b73SIlpo Järvinen 	inet_csk(sk)->icsk_backoff = 0;
286041834b73SIlpo Järvinen }
28619ad7c049SJerry Chu EXPORT_SYMBOL(tcp_valid_rtt_meas);
286241834b73SIlpo Järvinen 
28631da177e4SLinus Torvalds /* Read draft-ietf-tcplw-high-performance before mucking
2864caa20d9aSStephen Hemminger  * with this code. (Supersedes RFC1323)
28651da177e4SLinus Torvalds  */
28662d2abbabSStephen Hemminger static void tcp_ack_saw_tstamp(struct sock *sk, int flag)
28671da177e4SLinus Torvalds {
28681da177e4SLinus Torvalds 	/* RTTM Rule: A TSecr value received in a segment is used to
28691da177e4SLinus Torvalds 	 * update the averaged RTT measurement only if the segment
28701da177e4SLinus Torvalds 	 * acknowledges some new data, i.e., only if it advances the
28711da177e4SLinus Torvalds 	 * left edge of the send window.
28721da177e4SLinus Torvalds 	 *
28731da177e4SLinus Torvalds 	 * See draft-ietf-tcplw-high-performance-00, section 3.3.
28741da177e4SLinus Torvalds 	 * 1998/04/10 Andrey V. Savochkin <saw@msu.ru>
28751da177e4SLinus Torvalds 	 *
28761da177e4SLinus Torvalds 	 * Changed: reset backoff as soon as we see the first valid sample.
2877caa20d9aSStephen Hemminger 	 * If we do not, we get strongly overestimated rto. With timestamps
28781da177e4SLinus Torvalds 	 * samples are accepted even from very old segments: f.e., when rtt=1
28791da177e4SLinus Torvalds 	 * increases to 8, we retransmit 5 times and after 8 seconds delayed
28801da177e4SLinus Torvalds 	 * answer arrives rto becomes 120 seconds! If at least one of segments
28811da177e4SLinus Torvalds 	 * in window is lost... Voila.	 			--ANK (010210)
28821da177e4SLinus Torvalds 	 */
2883463c84b9SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
288441834b73SIlpo Järvinen 
288541834b73SIlpo Järvinen 	tcp_valid_rtt_meas(sk, tcp_time_stamp - tp->rx_opt.rcv_tsecr);
28861da177e4SLinus Torvalds }
28871da177e4SLinus Torvalds 
28882d2abbabSStephen Hemminger static void tcp_ack_no_tstamp(struct sock *sk, u32 seq_rtt, int flag)
28891da177e4SLinus Torvalds {
28901da177e4SLinus Torvalds 	/* We don't have a timestamp. Can only use
28911da177e4SLinus Torvalds 	 * packets that are not retransmitted to determine
28921da177e4SLinus Torvalds 	 * rtt estimates. Also, we must not reset the
28931da177e4SLinus Torvalds 	 * backoff for rto until we get a non-retransmitted
28941da177e4SLinus Torvalds 	 * packet. This allows us to deal with a situation
28951da177e4SLinus Torvalds 	 * where the network delay has increased suddenly.
28961da177e4SLinus Torvalds 	 * I.e. Karn's algorithm. (SIGCOMM '87, p5.)
28971da177e4SLinus Torvalds 	 */
28981da177e4SLinus Torvalds 
28991da177e4SLinus Torvalds 	if (flag & FLAG_RETRANS_DATA_ACKED)
29001da177e4SLinus Torvalds 		return;
29011da177e4SLinus Torvalds 
290241834b73SIlpo Järvinen 	tcp_valid_rtt_meas(sk, seq_rtt);
29031da177e4SLinus Torvalds }
29041da177e4SLinus Torvalds 
2905463c84b9SArnaldo Carvalho de Melo static inline void tcp_ack_update_rtt(struct sock *sk, const int flag,
29062d2abbabSStephen Hemminger 				      const s32 seq_rtt)
29071da177e4SLinus Torvalds {
2908463c84b9SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
29091da177e4SLinus Torvalds 	/* Note that peer MAY send zero echo. In this case it is ignored. (rfc1323) */
29101da177e4SLinus Torvalds 	if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
29112d2abbabSStephen Hemminger 		tcp_ack_saw_tstamp(sk, flag);
29121da177e4SLinus Torvalds 	else if (seq_rtt >= 0)
29132d2abbabSStephen Hemminger 		tcp_ack_no_tstamp(sk, seq_rtt, flag);
29141da177e4SLinus Torvalds }
29151da177e4SLinus Torvalds 
2916c3a05c60SIlpo Järvinen static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
29171da177e4SLinus Torvalds {
29186687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
2919c3a05c60SIlpo Järvinen 	icsk->icsk_ca_ops->cong_avoid(sk, ack, in_flight);
29206687e988SArnaldo Carvalho de Melo 	tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp;
29211da177e4SLinus Torvalds }
29221da177e4SLinus Torvalds 
29231da177e4SLinus Torvalds /* Restart timer after forward progress on connection.
29241da177e4SLinus Torvalds  * RFC2988 recommends to restart timer to now+rto.
29251da177e4SLinus Torvalds  */
2926750ea2baSYuchung Cheng void tcp_rearm_rto(struct sock *sk)
29271da177e4SLinus Torvalds {
29286ba8a3b1SNandita Dukkipati 	const struct inet_connection_sock *icsk = inet_csk(sk);
2929750ea2baSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
29309e412ba7SIlpo Järvinen 
2931168a8f58SJerry Chu 	/* If the retrans timer is currently being used by Fast Open
2932168a8f58SJerry Chu 	 * for SYN-ACK retrans purpose, stay put.
2933168a8f58SJerry Chu 	 */
2934168a8f58SJerry Chu 	if (tp->fastopen_rsk)
2935168a8f58SJerry Chu 		return;
2936168a8f58SJerry Chu 
29371da177e4SLinus Torvalds 	if (!tp->packets_out) {
2938463c84b9SArnaldo Carvalho de Melo 		inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
29391da177e4SLinus Torvalds 	} else {
2940750ea2baSYuchung Cheng 		u32 rto = inet_csk(sk)->icsk_rto;
2941750ea2baSYuchung Cheng 		/* Offset the time elapsed after installing regular RTO */
29426ba8a3b1SNandita Dukkipati 		if (icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
29436ba8a3b1SNandita Dukkipati 		    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
2944750ea2baSYuchung Cheng 			struct sk_buff *skb = tcp_write_queue_head(sk);
2945750ea2baSYuchung Cheng 			const u32 rto_time_stamp = TCP_SKB_CB(skb)->when + rto;
2946750ea2baSYuchung Cheng 			s32 delta = (s32)(rto_time_stamp - tcp_time_stamp);
2947750ea2baSYuchung Cheng 			/* delta may not be positive if the socket is locked
29486ba8a3b1SNandita Dukkipati 			 * when the retrans timer fires and is rescheduled.
2949750ea2baSYuchung Cheng 			 */
2950750ea2baSYuchung Cheng 			if (delta > 0)
2951750ea2baSYuchung Cheng 				rto = delta;
29521da177e4SLinus Torvalds 		}
2953750ea2baSYuchung Cheng 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto,
2954750ea2baSYuchung Cheng 					  TCP_RTO_MAX);
2955750ea2baSYuchung Cheng 	}
2956750ea2baSYuchung Cheng }
2957750ea2baSYuchung Cheng 
2958750ea2baSYuchung Cheng /* This function is called when the delayed ER timer fires. TCP enters
2959750ea2baSYuchung Cheng  * fast recovery and performs fast-retransmit.
2960750ea2baSYuchung Cheng  */
2961750ea2baSYuchung Cheng void tcp_resume_early_retransmit(struct sock *sk)
2962750ea2baSYuchung Cheng {
2963750ea2baSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
2964750ea2baSYuchung Cheng 
2965750ea2baSYuchung Cheng 	tcp_rearm_rto(sk);
2966750ea2baSYuchung Cheng 
2967750ea2baSYuchung Cheng 	/* Stop if ER is disabled after the delayed ER timer is scheduled */
2968750ea2baSYuchung Cheng 	if (!tp->do_early_retrans)
2969750ea2baSYuchung Cheng 		return;
2970750ea2baSYuchung Cheng 
2971750ea2baSYuchung Cheng 	tcp_enter_recovery(sk, false);
2972750ea2baSYuchung Cheng 	tcp_update_scoreboard(sk, 1);
2973750ea2baSYuchung Cheng 	tcp_xmit_retransmit_queue(sk);
29741da177e4SLinus Torvalds }
29751da177e4SLinus Torvalds 
29767c46a03eSIlpo Järvinen /* If we get here, the whole TSO packet has not been acked. */
297713fcf850SIlpo Järvinen static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)
29781da177e4SLinus Torvalds {
29791da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
29807c46a03eSIlpo Järvinen 	u32 packets_acked;
29811da177e4SLinus Torvalds 
29827c46a03eSIlpo Järvinen 	BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una));
29831da177e4SLinus Torvalds 
29841da177e4SLinus Torvalds 	packets_acked = tcp_skb_pcount(skb);
29857c46a03eSIlpo Järvinen 	if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
29861da177e4SLinus Torvalds 		return 0;
29871da177e4SLinus Torvalds 	packets_acked -= tcp_skb_pcount(skb);
29881da177e4SLinus Torvalds 
29891da177e4SLinus Torvalds 	if (packets_acked) {
29901da177e4SLinus Torvalds 		BUG_ON(tcp_skb_pcount(skb) == 0);
29917c46a03eSIlpo Järvinen 		BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq));
29921da177e4SLinus Torvalds 	}
29931da177e4SLinus Torvalds 
299413fcf850SIlpo Järvinen 	return packets_acked;
29951da177e4SLinus Torvalds }
29961da177e4SLinus Torvalds 
29977c46a03eSIlpo Järvinen /* Remove acknowledged frames from the retransmission queue. If our packet
29987c46a03eSIlpo Järvinen  * is before the ack sequence we can discard it as it's confirmed to have
29997c46a03eSIlpo Järvinen  * arrived at the other end.
30007c46a03eSIlpo Järvinen  */
300133f5f57eSIlpo Järvinen static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
300233f5f57eSIlpo Järvinen 			       u32 prior_snd_una)
30031da177e4SLinus Torvalds {
30041da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
30052d2abbabSStephen Hemminger 	const struct inet_connection_sock *icsk = inet_csk(sk);
30061da177e4SLinus Torvalds 	struct sk_buff *skb;
30077c46a03eSIlpo Järvinen 	u32 now = tcp_time_stamp;
3008a2a385d6SEric Dumazet 	int fully_acked = true;
30097c46a03eSIlpo Järvinen 	int flag = 0;
301072018835SIlpo Järvinen 	u32 pkts_acked = 0;
3011c7caf8d3SIlpo Järvinen 	u32 reord = tp->packets_out;
301290638a04SIlpo Järvinen 	u32 prior_sacked = tp->sacked_out;
30137c46a03eSIlpo Järvinen 	s32 seq_rtt = -1;
30142072c228SGavin McCullagh 	s32 ca_seq_rtt = -1;
3015b9ce204fSIlpo Järvinen 	ktime_t last_ackt = net_invalid_timestamp();
30161da177e4SLinus Torvalds 
30177c46a03eSIlpo Järvinen 	while ((skb = tcp_write_queue_head(sk)) && skb != tcp_send_head(sk)) {
30181da177e4SLinus Torvalds 		struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
301972018835SIlpo Järvinen 		u32 acked_pcount;
30207c46a03eSIlpo Järvinen 		u8 sacked = scb->sacked;
30211da177e4SLinus Torvalds 
30222072c228SGavin McCullagh 		/* Determine how many packets and what bytes were acked, tso and else */
30231da177e4SLinus Torvalds 		if (after(scb->end_seq, tp->snd_una)) {
302413fcf850SIlpo Järvinen 			if (tcp_skb_pcount(skb) == 1 ||
302513fcf850SIlpo Järvinen 			    !after(tp->snd_una, scb->seq))
30261da177e4SLinus Torvalds 				break;
302713fcf850SIlpo Järvinen 
302872018835SIlpo Järvinen 			acked_pcount = tcp_tso_acked(sk, skb);
302972018835SIlpo Järvinen 			if (!acked_pcount)
303013fcf850SIlpo Järvinen 				break;
303113fcf850SIlpo Järvinen 
3032a2a385d6SEric Dumazet 			fully_acked = false;
303313fcf850SIlpo Järvinen 		} else {
303472018835SIlpo Järvinen 			acked_pcount = tcp_skb_pcount(skb);
30351da177e4SLinus Torvalds 		}
30361da177e4SLinus Torvalds 
30371da177e4SLinus Torvalds 		if (sacked & TCPCB_RETRANS) {
30381da177e4SLinus Torvalds 			if (sacked & TCPCB_SACKED_RETRANS)
303972018835SIlpo Järvinen 				tp->retrans_out -= acked_pcount;
30407c46a03eSIlpo Järvinen 			flag |= FLAG_RETRANS_DATA_ACKED;
30412072c228SGavin McCullagh 			ca_seq_rtt = -1;
30421da177e4SLinus Torvalds 			seq_rtt = -1;
3043c7caf8d3SIlpo Järvinen 		} else {
30442072c228SGavin McCullagh 			ca_seq_rtt = now - scb->when;
3045164891aaSStephen Hemminger 			last_ackt = skb->tstamp;
30462072c228SGavin McCullagh 			if (seq_rtt < 0) {
30472072c228SGavin McCullagh 				seq_rtt = ca_seq_rtt;
3048a61bbcf2SPatrick McHardy 			}
3049c7caf8d3SIlpo Järvinen 			if (!(sacked & TCPCB_SACKED_ACKED))
305072018835SIlpo Järvinen 				reord = min(pkts_acked, reord);
3051e33099f9SYuchung Cheng 			if (!after(scb->end_seq, tp->high_seq))
3052e33099f9SYuchung Cheng 				flag |= FLAG_ORIG_SACK_ACKED;
3053c7caf8d3SIlpo Järvinen 		}
30547c46a03eSIlpo Järvinen 
30551da177e4SLinus Torvalds 		if (sacked & TCPCB_SACKED_ACKED)
305672018835SIlpo Järvinen 			tp->sacked_out -= acked_pcount;
30571da177e4SLinus Torvalds 		if (sacked & TCPCB_LOST)
305872018835SIlpo Järvinen 			tp->lost_out -= acked_pcount;
30597c46a03eSIlpo Järvinen 
306072018835SIlpo Järvinen 		tp->packets_out -= acked_pcount;
306172018835SIlpo Järvinen 		pkts_acked += acked_pcount;
306213fcf850SIlpo Järvinen 
3063009a2e3eSIlpo Järvinen 		/* Initial outgoing SYN's get put onto the write_queue
3064009a2e3eSIlpo Järvinen 		 * just like anything else we transmit.  It is not
3065009a2e3eSIlpo Järvinen 		 * true data, and if we misinform our callers that
3066009a2e3eSIlpo Järvinen 		 * this ACK acks real data, we will erroneously exit
3067009a2e3eSIlpo Järvinen 		 * connection startup slow start one packet too
3068009a2e3eSIlpo Järvinen 		 * quickly.  This is severely frowned upon behavior.
3069009a2e3eSIlpo Järvinen 		 */
30704de075e0SEric Dumazet 		if (!(scb->tcp_flags & TCPHDR_SYN)) {
3071009a2e3eSIlpo Järvinen 			flag |= FLAG_DATA_ACKED;
3072009a2e3eSIlpo Järvinen 		} else {
3073009a2e3eSIlpo Järvinen 			flag |= FLAG_SYN_ACKED;
3074009a2e3eSIlpo Järvinen 			tp->retrans_stamp = 0;
3075009a2e3eSIlpo Järvinen 		}
3076009a2e3eSIlpo Järvinen 
307713fcf850SIlpo Järvinen 		if (!fully_acked)
307813fcf850SIlpo Järvinen 			break;
307913fcf850SIlpo Järvinen 
3080fe067e8aSDavid S. Miller 		tcp_unlink_write_queue(skb, sk);
30813ab224beSHideo Aoki 		sk_wmem_free_skb(sk, skb);
308290638a04SIlpo Järvinen 		tp->scoreboard_skb_hint = NULL;
3083ef9da47cSIlpo Järvinen 		if (skb == tp->retransmit_skb_hint)
3084ef9da47cSIlpo Järvinen 			tp->retransmit_skb_hint = NULL;
308590638a04SIlpo Järvinen 		if (skb == tp->lost_skb_hint)
308690638a04SIlpo Järvinen 			tp->lost_skb_hint = NULL;
30871da177e4SLinus Torvalds 	}
30881da177e4SLinus Torvalds 
308933f5f57eSIlpo Järvinen 	if (likely(between(tp->snd_up, prior_snd_una, tp->snd_una)))
309033f5f57eSIlpo Järvinen 		tp->snd_up = tp->snd_una;
309133f5f57eSIlpo Järvinen 
3092cadbd031SIlpo Järvinen 	if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
3093cadbd031SIlpo Järvinen 		flag |= FLAG_SACK_RENEGING;
3094cadbd031SIlpo Järvinen 
30957c46a03eSIlpo Järvinen 	if (flag & FLAG_ACKED) {
3096164891aaSStephen Hemminger 		const struct tcp_congestion_ops *ca_ops
3097164891aaSStephen Hemminger 			= inet_csk(sk)->icsk_ca_ops;
3098164891aaSStephen Hemminger 
309972211e90SIlpo Järvinen 		if (unlikely(icsk->icsk_mtup.probe_size &&
310072211e90SIlpo Järvinen 			     !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {
310172211e90SIlpo Järvinen 			tcp_mtup_probe_success(sk);
310272211e90SIlpo Järvinen 		}
310372211e90SIlpo Järvinen 
31047c46a03eSIlpo Järvinen 		tcp_ack_update_rtt(sk, flag, seq_rtt);
31056728e7dcSIlpo Järvinen 		tcp_rearm_rto(sk);
3106317a76f9SStephen Hemminger 
3107c7caf8d3SIlpo Järvinen 		if (tcp_is_reno(tp)) {
3108c7caf8d3SIlpo Järvinen 			tcp_remove_reno_sacks(sk, pkts_acked);
3109c7caf8d3SIlpo Järvinen 		} else {
311059a08cbaSIlpo Järvinen 			int delta;
311159a08cbaSIlpo Järvinen 
3112c7caf8d3SIlpo Järvinen 			/* Non-retransmitted hole got filled? That's reordering */
3113c7caf8d3SIlpo Järvinen 			if (reord < prior_fackets)
3114c7caf8d3SIlpo Järvinen 				tcp_update_reordering(sk, tp->fackets_out - reord, 0);
311590638a04SIlpo Järvinen 
311659a08cbaSIlpo Järvinen 			delta = tcp_is_fack(tp) ? pkts_acked :
311759a08cbaSIlpo Järvinen 						  prior_sacked - tp->sacked_out;
311859a08cbaSIlpo Järvinen 			tp->lost_cnt_hint -= min(tp->lost_cnt_hint, delta);
3119c7caf8d3SIlpo Järvinen 		}
3120c7caf8d3SIlpo Järvinen 
312191fed7a1SIlpo Järvinen 		tp->fackets_out -= min(pkts_acked, tp->fackets_out);
312268f8353bSIlpo Järvinen 
312330cfd0baSStephen Hemminger 		if (ca_ops->pkts_acked) {
312430cfd0baSStephen Hemminger 			s32 rtt_us = -1;
3125b9ce204fSIlpo Järvinen 
312630cfd0baSStephen Hemminger 			/* Is the ACK triggering packet unambiguous? */
31277c46a03eSIlpo Järvinen 			if (!(flag & FLAG_RETRANS_DATA_ACKED)) {
312830cfd0baSStephen Hemminger 				/* High resolution needed and available? */
312930cfd0baSStephen Hemminger 				if (ca_ops->flags & TCP_CONG_RTT_STAMP &&
313030cfd0baSStephen Hemminger 				    !ktime_equal(last_ackt,
313130cfd0baSStephen Hemminger 						 net_invalid_timestamp()))
313230cfd0baSStephen Hemminger 					rtt_us = ktime_us_delta(ktime_get_real(),
313330cfd0baSStephen Hemminger 								last_ackt);
3134febf0819Sstephen hemminger 				else if (ca_seq_rtt >= 0)
31352072c228SGavin McCullagh 					rtt_us = jiffies_to_usecs(ca_seq_rtt);
313630cfd0baSStephen Hemminger 			}
313730cfd0baSStephen Hemminger 
313830cfd0baSStephen Hemminger 			ca_ops->pkts_acked(sk, pkts_acked, rtt_us);
313930cfd0baSStephen Hemminger 		}
31401da177e4SLinus Torvalds 	}
31411da177e4SLinus Torvalds 
31421da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0
3143547b792cSIlpo Järvinen 	WARN_ON((int)tp->sacked_out < 0);
3144547b792cSIlpo Järvinen 	WARN_ON((int)tp->lost_out < 0);
3145547b792cSIlpo Järvinen 	WARN_ON((int)tp->retrans_out < 0);
3146e60402d0SIlpo Järvinen 	if (!tp->packets_out && tcp_is_sack(tp)) {
3147cfcabdccSStephen Hemminger 		icsk = inet_csk(sk);
31481da177e4SLinus Torvalds 		if (tp->lost_out) {
314991df42beSJoe Perches 			pr_debug("Leak l=%u %d\n",
31506687e988SArnaldo Carvalho de Melo 				 tp->lost_out, icsk->icsk_ca_state);
31511da177e4SLinus Torvalds 			tp->lost_out = 0;
31521da177e4SLinus Torvalds 		}
31531da177e4SLinus Torvalds 		if (tp->sacked_out) {
315491df42beSJoe Perches 			pr_debug("Leak s=%u %d\n",
31556687e988SArnaldo Carvalho de Melo 				 tp->sacked_out, icsk->icsk_ca_state);
31561da177e4SLinus Torvalds 			tp->sacked_out = 0;
31571da177e4SLinus Torvalds 		}
31581da177e4SLinus Torvalds 		if (tp->retrans_out) {
315991df42beSJoe Perches 			pr_debug("Leak r=%u %d\n",
31606687e988SArnaldo Carvalho de Melo 				 tp->retrans_out, icsk->icsk_ca_state);
31611da177e4SLinus Torvalds 			tp->retrans_out = 0;
31621da177e4SLinus Torvalds 		}
31631da177e4SLinus Torvalds 	}
31641da177e4SLinus Torvalds #endif
31657c46a03eSIlpo Järvinen 	return flag;
31661da177e4SLinus Torvalds }
31671da177e4SLinus Torvalds 
31681da177e4SLinus Torvalds static void tcp_ack_probe(struct sock *sk)
31691da177e4SLinus Torvalds {
3170463c84b9SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
3171463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
31721da177e4SLinus Torvalds 
31731da177e4SLinus Torvalds 	/* Was it a usable window open? */
31741da177e4SLinus Torvalds 
317590840defSIlpo Järvinen 	if (!after(TCP_SKB_CB(tcp_send_head(sk))->end_seq, tcp_wnd_end(tp))) {
3176463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_backoff = 0;
3177463c84b9SArnaldo Carvalho de Melo 		inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0);
31781da177e4SLinus Torvalds 		/* Socket must be waked up by subsequent tcp_data_snd_check().
31791da177e4SLinus Torvalds 		 * This function is not for random using!
31801da177e4SLinus Torvalds 		 */
31811da177e4SLinus Torvalds 	} else {
3182463c84b9SArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
31833f421baaSArnaldo Carvalho de Melo 					  min(icsk->icsk_rto << icsk->icsk_backoff, TCP_RTO_MAX),
31843f421baaSArnaldo Carvalho de Melo 					  TCP_RTO_MAX);
31851da177e4SLinus Torvalds 	}
31861da177e4SLinus Torvalds }
31871da177e4SLinus Torvalds 
318867b95bd7SVijay Subramanian static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag)
31891da177e4SLinus Torvalds {
3190a02cec21SEric Dumazet 	return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||
3191a02cec21SEric Dumazet 		inet_csk(sk)->icsk_ca_state != TCP_CA_Open;
31921da177e4SLinus Torvalds }
31931da177e4SLinus Torvalds 
319467b95bd7SVijay Subramanian static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
31951da177e4SLinus Torvalds {
31966687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
31971da177e4SLinus Torvalds 	return (!(flag & FLAG_ECE) || tp->snd_cwnd < tp->snd_ssthresh) &&
3198684bad11SYuchung Cheng 		!tcp_in_cwnd_reduction(sk);
31991da177e4SLinus Torvalds }
32001da177e4SLinus Torvalds 
32011da177e4SLinus Torvalds /* Check that window update is acceptable.
32021da177e4SLinus Torvalds  * The function assumes that snd_una<=ack<=snd_next.
32031da177e4SLinus Torvalds  */
320467b95bd7SVijay Subramanian static inline bool tcp_may_update_window(const struct tcp_sock *tp,
3205056834d9SIlpo Järvinen 					const u32 ack, const u32 ack_seq,
3206056834d9SIlpo Järvinen 					const u32 nwin)
32071da177e4SLinus Torvalds {
3208a02cec21SEric Dumazet 	return	after(ack, tp->snd_una) ||
32091da177e4SLinus Torvalds 		after(ack_seq, tp->snd_wl1) ||
3210a02cec21SEric Dumazet 		(ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd);
32111da177e4SLinus Torvalds }
32121da177e4SLinus Torvalds 
32131da177e4SLinus Torvalds /* Update our send window.
32141da177e4SLinus Torvalds  *
32151da177e4SLinus Torvalds  * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
32161da177e4SLinus Torvalds  * and in FreeBSD. NetBSD's one is even worse.) is wrong.
32171da177e4SLinus Torvalds  */
3218cf533ea5SEric Dumazet static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,
32199e412ba7SIlpo Järvinen 				 u32 ack_seq)
32201da177e4SLinus Torvalds {
32219e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
32221da177e4SLinus Torvalds 	int flag = 0;
3223aa8223c7SArnaldo Carvalho de Melo 	u32 nwin = ntohs(tcp_hdr(skb)->window);
32241da177e4SLinus Torvalds 
3225aa8223c7SArnaldo Carvalho de Melo 	if (likely(!tcp_hdr(skb)->syn))
32261da177e4SLinus Torvalds 		nwin <<= tp->rx_opt.snd_wscale;
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds 	if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {
32291da177e4SLinus Torvalds 		flag |= FLAG_WIN_UPDATE;
3230ee7537b6SHantzis Fotis 		tcp_update_wl(tp, ack_seq);
32311da177e4SLinus Torvalds 
32321da177e4SLinus Torvalds 		if (tp->snd_wnd != nwin) {
32331da177e4SLinus Torvalds 			tp->snd_wnd = nwin;
32341da177e4SLinus Torvalds 
32351da177e4SLinus Torvalds 			/* Note, it is the only place, where
32361da177e4SLinus Torvalds 			 * fast path is recovered for sending TCP.
32371da177e4SLinus Torvalds 			 */
32382ad41065SHerbert Xu 			tp->pred_flags = 0;
32399e412ba7SIlpo Järvinen 			tcp_fast_path_check(sk);
32401da177e4SLinus Torvalds 
32411da177e4SLinus Torvalds 			if (nwin > tp->max_window) {
32421da177e4SLinus Torvalds 				tp->max_window = nwin;
3243d83d8461SArnaldo Carvalho de Melo 				tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie);
32441da177e4SLinus Torvalds 			}
32451da177e4SLinus Torvalds 		}
32461da177e4SLinus Torvalds 	}
32471da177e4SLinus Torvalds 
32481da177e4SLinus Torvalds 	tp->snd_una = ack;
32491da177e4SLinus Torvalds 
32501da177e4SLinus Torvalds 	return flag;
32511da177e4SLinus Torvalds }
32521da177e4SLinus Torvalds 
3253354e4aa3SEric Dumazet /* RFC 5961 7 [ACK Throttling] */
3254354e4aa3SEric Dumazet static void tcp_send_challenge_ack(struct sock *sk)
3255354e4aa3SEric Dumazet {
3256354e4aa3SEric Dumazet 	/* unprotected vars, we dont care of overwrites */
3257354e4aa3SEric Dumazet 	static u32 challenge_timestamp;
3258354e4aa3SEric Dumazet 	static unsigned int challenge_count;
3259354e4aa3SEric Dumazet 	u32 now = jiffies / HZ;
3260354e4aa3SEric Dumazet 
3261354e4aa3SEric Dumazet 	if (now != challenge_timestamp) {
3262354e4aa3SEric Dumazet 		challenge_timestamp = now;
3263354e4aa3SEric Dumazet 		challenge_count = 0;
3264354e4aa3SEric Dumazet 	}
3265354e4aa3SEric Dumazet 	if (++challenge_count <= sysctl_tcp_challenge_ack_limit) {
3266354e4aa3SEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPCHALLENGEACK);
3267354e4aa3SEric Dumazet 		tcp_send_ack(sk);
3268354e4aa3SEric Dumazet 	}
3269354e4aa3SEric Dumazet }
3270354e4aa3SEric Dumazet 
327112fb3dd9SEric Dumazet static void tcp_store_ts_recent(struct tcp_sock *tp)
327212fb3dd9SEric Dumazet {
327312fb3dd9SEric Dumazet 	tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
327412fb3dd9SEric Dumazet 	tp->rx_opt.ts_recent_stamp = get_seconds();
327512fb3dd9SEric Dumazet }
327612fb3dd9SEric Dumazet 
327712fb3dd9SEric Dumazet static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
327812fb3dd9SEric Dumazet {
327912fb3dd9SEric Dumazet 	if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) {
328012fb3dd9SEric Dumazet 		/* PAWS bug workaround wrt. ACK frames, the PAWS discard
328112fb3dd9SEric Dumazet 		 * extra check below makes sure this can only happen
328212fb3dd9SEric Dumazet 		 * for pure ACK frames.  -DaveM
328312fb3dd9SEric Dumazet 		 *
328412fb3dd9SEric Dumazet 		 * Not only, also it occurs for expired timestamps.
328512fb3dd9SEric Dumazet 		 */
328612fb3dd9SEric Dumazet 
328712fb3dd9SEric Dumazet 		if (tcp_paws_check(&tp->rx_opt, 0))
328812fb3dd9SEric Dumazet 			tcp_store_ts_recent(tp);
328912fb3dd9SEric Dumazet 	}
329012fb3dd9SEric Dumazet }
329112fb3dd9SEric Dumazet 
32929b717a8dSNandita Dukkipati /* This routine deals with acks during a TLP episode.
32939b717a8dSNandita Dukkipati  * Ref: loss detection algorithm in draft-dukkipati-tcpm-tcp-loss-probe.
32949b717a8dSNandita Dukkipati  */
32959b717a8dSNandita Dukkipati static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
32969b717a8dSNandita Dukkipati {
32979b717a8dSNandita Dukkipati 	struct tcp_sock *tp = tcp_sk(sk);
32989b717a8dSNandita Dukkipati 	bool is_tlp_dupack = (ack == tp->tlp_high_seq) &&
32999b717a8dSNandita Dukkipati 			     !(flag & (FLAG_SND_UNA_ADVANCED |
33009b717a8dSNandita Dukkipati 				       FLAG_NOT_DUP | FLAG_DATA_SACKED));
33019b717a8dSNandita Dukkipati 
33029b717a8dSNandita Dukkipati 	/* Mark the end of TLP episode on receiving TLP dupack or when
33039b717a8dSNandita Dukkipati 	 * ack is after tlp_high_seq.
33049b717a8dSNandita Dukkipati 	 */
33059b717a8dSNandita Dukkipati 	if (is_tlp_dupack) {
33069b717a8dSNandita Dukkipati 		tp->tlp_high_seq = 0;
33079b717a8dSNandita Dukkipati 		return;
33089b717a8dSNandita Dukkipati 	}
33099b717a8dSNandita Dukkipati 
33109b717a8dSNandita Dukkipati 	if (after(ack, tp->tlp_high_seq)) {
33119b717a8dSNandita Dukkipati 		tp->tlp_high_seq = 0;
33129b717a8dSNandita Dukkipati 		/* Don't reduce cwnd if DSACK arrives for TLP retrans. */
33139b717a8dSNandita Dukkipati 		if (!(flag & FLAG_DSACKING_ACK)) {
33149b717a8dSNandita Dukkipati 			tcp_init_cwnd_reduction(sk, true);
33159b717a8dSNandita Dukkipati 			tcp_set_ca_state(sk, TCP_CA_CWR);
33169b717a8dSNandita Dukkipati 			tcp_end_cwnd_reduction(sk);
33179b717a8dSNandita Dukkipati 			tcp_set_ca_state(sk, TCP_CA_Open);
33189b717a8dSNandita Dukkipati 			NET_INC_STATS_BH(sock_net(sk),
33199b717a8dSNandita Dukkipati 					 LINUX_MIB_TCPLOSSPROBERECOVERY);
33209b717a8dSNandita Dukkipati 		}
33219b717a8dSNandita Dukkipati 	}
33229b717a8dSNandita Dukkipati }
33239b717a8dSNandita Dukkipati 
33241da177e4SLinus Torvalds /* This routine deals with incoming acks, but not outgoing ones. */
3325cf533ea5SEric Dumazet static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
33261da177e4SLinus Torvalds {
33276687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
33281da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
33291da177e4SLinus Torvalds 	u32 prior_snd_una = tp->snd_una;
33301da177e4SLinus Torvalds 	u32 ack_seq = TCP_SKB_CB(skb)->seq;
33311da177e4SLinus Torvalds 	u32 ack = TCP_SKB_CB(skb)->ack_seq;
33327d2b55f8SNeal Cardwell 	bool is_dupack = false;
33331da177e4SLinus Torvalds 	u32 prior_in_flight;
3334c7caf8d3SIlpo Järvinen 	u32 prior_fackets;
3335*35f079ebSNandita Dukkipati 	int prior_packets = tp->packets_out;
3336a262f0cdSNandita Dukkipati 	int prior_sacked = tp->sacked_out;
33377d2b55f8SNeal Cardwell 	int pkts_acked = 0;
3338*35f079ebSNandita Dukkipati 	int previous_packets_out = 0;
33391da177e4SLinus Torvalds 
334096e0bf4bSJohn Dykstra 	/* If the ack is older than previous acks
33411da177e4SLinus Torvalds 	 * then we can probably ignore it.
33421da177e4SLinus Torvalds 	 */
3343354e4aa3SEric Dumazet 	if (before(ack, prior_snd_una)) {
3344354e4aa3SEric Dumazet 		/* RFC 5961 5.2 [Blind Data Injection Attack].[Mitigation] */
3345354e4aa3SEric Dumazet 		if (before(ack, prior_snd_una - tp->max_window)) {
3346354e4aa3SEric Dumazet 			tcp_send_challenge_ack(sk);
3347354e4aa3SEric Dumazet 			return -1;
3348354e4aa3SEric Dumazet 		}
33491da177e4SLinus Torvalds 		goto old_ack;
3350354e4aa3SEric Dumazet 	}
33511da177e4SLinus Torvalds 
335296e0bf4bSJohn Dykstra 	/* If the ack includes data we haven't sent yet, discard
335396e0bf4bSJohn Dykstra 	 * this segment (RFC793 Section 3.9).
335496e0bf4bSJohn Dykstra 	 */
335596e0bf4bSJohn Dykstra 	if (after(ack, tp->snd_nxt))
335696e0bf4bSJohn Dykstra 		goto invalid_ack;
335796e0bf4bSJohn Dykstra 
33586ba8a3b1SNandita Dukkipati 	if (icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
33596ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE)
3360750ea2baSYuchung Cheng 		tcp_rearm_rto(sk);
3361750ea2baSYuchung Cheng 
33622e605294SIlpo Järvinen 	if (after(ack, prior_snd_una))
33632e605294SIlpo Järvinen 		flag |= FLAG_SND_UNA_ADVANCED;
33642e605294SIlpo Järvinen 
3365c7caf8d3SIlpo Järvinen 	prior_fackets = tp->fackets_out;
336652d34081SIlpo Järvinen 	prior_in_flight = tcp_packets_in_flight(tp);
3367c7caf8d3SIlpo Järvinen 
336812fb3dd9SEric Dumazet 	/* ts_recent update must be made after we are sure that the packet
336912fb3dd9SEric Dumazet 	 * is in window.
337012fb3dd9SEric Dumazet 	 */
337112fb3dd9SEric Dumazet 	if (flag & FLAG_UPDATE_TS_RECENT)
337212fb3dd9SEric Dumazet 		tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
337312fb3dd9SEric Dumazet 
33741da177e4SLinus Torvalds 	if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
33751da177e4SLinus Torvalds 		/* Window is constant, pure forward advance.
33761da177e4SLinus Torvalds 		 * No more checks are required.
33771da177e4SLinus Torvalds 		 * Note, we use the fact that SND.UNA>=SND.WL2.
33781da177e4SLinus Torvalds 		 */
3379ee7537b6SHantzis Fotis 		tcp_update_wl(tp, ack_seq);
33801da177e4SLinus Torvalds 		tp->snd_una = ack;
33811da177e4SLinus Torvalds 		flag |= FLAG_WIN_UPDATE;
33821da177e4SLinus Torvalds 
33836687e988SArnaldo Carvalho de Melo 		tcp_ca_event(sk, CA_EVENT_FAST_ACK);
3384317a76f9SStephen Hemminger 
3385de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPACKS);
33861da177e4SLinus Torvalds 	} else {
33871da177e4SLinus Torvalds 		if (ack_seq != TCP_SKB_CB(skb)->end_seq)
33881da177e4SLinus Torvalds 			flag |= FLAG_DATA;
33891da177e4SLinus Torvalds 		else
3390de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPPUREACKS);
33911da177e4SLinus Torvalds 
33929e412ba7SIlpo Järvinen 		flag |= tcp_ack_update_window(sk, skb, ack, ack_seq);
33931da177e4SLinus Torvalds 
33941da177e4SLinus Torvalds 		if (TCP_SKB_CB(skb)->sacked)
33951da177e4SLinus Torvalds 			flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una);
33961da177e4SLinus Torvalds 
3397aa8223c7SArnaldo Carvalho de Melo 		if (TCP_ECN_rcv_ecn_echo(tp, tcp_hdr(skb)))
33981da177e4SLinus Torvalds 			flag |= FLAG_ECE;
33991da177e4SLinus Torvalds 
34006687e988SArnaldo Carvalho de Melo 		tcp_ca_event(sk, CA_EVENT_SLOW_ACK);
34011da177e4SLinus Torvalds 	}
34021da177e4SLinus Torvalds 
34031da177e4SLinus Torvalds 	/* We passed data and got it acked, remove any soft error
34041da177e4SLinus Torvalds 	 * log. Something worked...
34051da177e4SLinus Torvalds 	 */
34061da177e4SLinus Torvalds 	sk->sk_err_soft = 0;
34074b53fb67SDavid S. Miller 	icsk->icsk_probes_out = 0;
34081da177e4SLinus Torvalds 	tp->rcv_tstamp = tcp_time_stamp;
34091da177e4SLinus Torvalds 	if (!prior_packets)
34101da177e4SLinus Torvalds 		goto no_queue;
34111da177e4SLinus Torvalds 
34121da177e4SLinus Torvalds 	/* See if we can take anything off of the retransmit queue. */
3413*35f079ebSNandita Dukkipati 	previous_packets_out = tp->packets_out;
341433f5f57eSIlpo Järvinen 	flag |= tcp_clean_rtx_queue(sk, prior_fackets, prior_snd_una);
34151da177e4SLinus Torvalds 
3416*35f079ebSNandita Dukkipati 	pkts_acked = previous_packets_out - tp->packets_out;
3417a262f0cdSNandita Dukkipati 
34186687e988SArnaldo Carvalho de Melo 	if (tcp_ack_is_dubious(sk, flag)) {
3419caa20d9aSStephen Hemminger 		/* Advance CWND, if state allows this. */
34209b44190dSYuchung Cheng 		if ((flag & FLAG_DATA_ACKED) && tcp_may_raise_cwnd(sk, flag))
3421c3a05c60SIlpo Järvinen 			tcp_cong_avoid(sk, ack, prior_in_flight);
34227d2b55f8SNeal Cardwell 		is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP));
34237c4a56feSYuchung Cheng 		tcp_fastretrans_alert(sk, pkts_acked, prior_sacked,
3424*35f079ebSNandita Dukkipati 				      prior_packets, is_dupack, flag);
34251da177e4SLinus Torvalds 	} else {
34269b44190dSYuchung Cheng 		if (flag & FLAG_DATA_ACKED)
3427c3a05c60SIlpo Järvinen 			tcp_cong_avoid(sk, ack, prior_in_flight);
34281da177e4SLinus Torvalds 	}
34291da177e4SLinus Torvalds 
34309b717a8dSNandita Dukkipati 	if (tp->tlp_high_seq)
34319b717a8dSNandita Dukkipati 		tcp_process_tlp_ack(sk, ack, flag);
34329b717a8dSNandita Dukkipati 
34335110effeSDavid S. Miller 	if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP)) {
34345110effeSDavid S. Miller 		struct dst_entry *dst = __sk_dst_get(sk);
34355110effeSDavid S. Miller 		if (dst)
34365110effeSDavid S. Miller 			dst_confirm(dst);
34375110effeSDavid S. Miller 	}
34386ba8a3b1SNandita Dukkipati 
34396ba8a3b1SNandita Dukkipati 	if (icsk->icsk_pending == ICSK_TIME_RETRANS)
34406ba8a3b1SNandita Dukkipati 		tcp_schedule_loss_probe(sk);
34411da177e4SLinus Torvalds 	return 1;
34421da177e4SLinus Torvalds 
34431da177e4SLinus Torvalds no_queue:
34445628adf1SNeal Cardwell 	/* If data was DSACKed, see if we can undo a cwnd reduction. */
34455628adf1SNeal Cardwell 	if (flag & FLAG_DSACKING_ACK)
34467c4a56feSYuchung Cheng 		tcp_fastretrans_alert(sk, pkts_acked, prior_sacked,
3447*35f079ebSNandita Dukkipati 				      prior_packets, is_dupack, flag);
34481da177e4SLinus Torvalds 	/* If this ack opens up a zero window, clear backoff.  It was
34491da177e4SLinus Torvalds 	 * being used to time the probes, and is probably far higher than
34501da177e4SLinus Torvalds 	 * it needs to be for normal retransmission.
34511da177e4SLinus Torvalds 	 */
3452fe067e8aSDavid S. Miller 	if (tcp_send_head(sk))
34531da177e4SLinus Torvalds 		tcp_ack_probe(sk);
34549b717a8dSNandita Dukkipati 
34559b717a8dSNandita Dukkipati 	if (tp->tlp_high_seq)
34569b717a8dSNandita Dukkipati 		tcp_process_tlp_ack(sk, ack, flag);
34571da177e4SLinus Torvalds 	return 1;
34581da177e4SLinus Torvalds 
345996e0bf4bSJohn Dykstra invalid_ack:
346096e0bf4bSJohn Dykstra 	SOCK_DEBUG(sk, "Ack %u after %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
346196e0bf4bSJohn Dykstra 	return -1;
346296e0bf4bSJohn Dykstra 
34631da177e4SLinus Torvalds old_ack:
3464e95ae2f2SNeal Cardwell 	/* If data was SACKed, tag it and see if we should send more data.
3465e95ae2f2SNeal Cardwell 	 * If data was DSACKed, see if we can undo a cwnd reduction.
3466e95ae2f2SNeal Cardwell 	 */
34678aca6cb1SIlpo Järvinen 	if (TCP_SKB_CB(skb)->sacked) {
3468e95ae2f2SNeal Cardwell 		flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una);
34697c4a56feSYuchung Cheng 		tcp_fastretrans_alert(sk, pkts_acked, prior_sacked,
3470*35f079ebSNandita Dukkipati 				      prior_packets, is_dupack, flag);
34718aca6cb1SIlpo Järvinen 	}
34721da177e4SLinus Torvalds 
347396e0bf4bSJohn Dykstra 	SOCK_DEBUG(sk, "Ack %u before %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
34741da177e4SLinus Torvalds 	return 0;
34751da177e4SLinus Torvalds }
34761da177e4SLinus Torvalds 
34771da177e4SLinus Torvalds /* Look for tcp options. Normally only called on SYN and SYNACK packets.
34781da177e4SLinus Torvalds  * But, this can also be called on packets in the established flow when
34791da177e4SLinus Torvalds  * the fast version below fails.
34801da177e4SLinus Torvalds  */
34811a2c6181SChristoph Paasch void tcp_parse_options(const struct sk_buff *skb,
34821a2c6181SChristoph Paasch 		       struct tcp_options_received *opt_rx, int estab,
34832100c8d2SYuchung Cheng 		       struct tcp_fastopen_cookie *foc)
34841da177e4SLinus Torvalds {
3485cf533ea5SEric Dumazet 	const unsigned char *ptr;
3486cf533ea5SEric Dumazet 	const struct tcphdr *th = tcp_hdr(skb);
34871da177e4SLinus Torvalds 	int length = (th->doff * 4) - sizeof(struct tcphdr);
34881da177e4SLinus Torvalds 
3489cf533ea5SEric Dumazet 	ptr = (const unsigned char *)(th + 1);
34901da177e4SLinus Torvalds 	opt_rx->saw_tstamp = 0;
34911da177e4SLinus Torvalds 
34921da177e4SLinus Torvalds 	while (length > 0) {
34931da177e4SLinus Torvalds 		int opcode = *ptr++;
34941da177e4SLinus Torvalds 		int opsize;
34951da177e4SLinus Torvalds 
34961da177e4SLinus Torvalds 		switch (opcode) {
34971da177e4SLinus Torvalds 		case TCPOPT_EOL:
34981da177e4SLinus Torvalds 			return;
34991da177e4SLinus Torvalds 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
35001da177e4SLinus Torvalds 			length--;
35011da177e4SLinus Torvalds 			continue;
35021da177e4SLinus Torvalds 		default:
35031da177e4SLinus Torvalds 			opsize = *ptr++;
35041da177e4SLinus Torvalds 			if (opsize < 2) /* "silly options" */
35051da177e4SLinus Torvalds 				return;
35061da177e4SLinus Torvalds 			if (opsize > length)
35071da177e4SLinus Torvalds 				return;	/* don't parse partial options */
35081da177e4SLinus Torvalds 			switch (opcode) {
35091da177e4SLinus Torvalds 			case TCPOPT_MSS:
35101da177e4SLinus Torvalds 				if (opsize == TCPOLEN_MSS && th->syn && !estab) {
3511d3e2ce3bSHarvey Harrison 					u16 in_mss = get_unaligned_be16(ptr);
35121da177e4SLinus Torvalds 					if (in_mss) {
3513f038ac8fSIlpo Järvinen 						if (opt_rx->user_mss &&
3514f038ac8fSIlpo Järvinen 						    opt_rx->user_mss < in_mss)
35151da177e4SLinus Torvalds 							in_mss = opt_rx->user_mss;
35161da177e4SLinus Torvalds 						opt_rx->mss_clamp = in_mss;
35171da177e4SLinus Torvalds 					}
35181da177e4SLinus Torvalds 				}
35191da177e4SLinus Torvalds 				break;
35201da177e4SLinus Torvalds 			case TCPOPT_WINDOW:
3521f038ac8fSIlpo Järvinen 				if (opsize == TCPOLEN_WINDOW && th->syn &&
3522bb5b7c11SDavid S. Miller 				    !estab && sysctl_tcp_window_scaling) {
35231da177e4SLinus Torvalds 					__u8 snd_wscale = *(__u8 *)ptr;
35241da177e4SLinus Torvalds 					opt_rx->wscale_ok = 1;
35251da177e4SLinus Torvalds 					if (snd_wscale > 14) {
3526e87cc472SJoe Perches 						net_info_ratelimited("%s: Illegal window scaling value %d >14 received\n",
3527058bd4d2SJoe Perches 								     __func__,
35281da177e4SLinus Torvalds 								     snd_wscale);
35291da177e4SLinus Torvalds 						snd_wscale = 14;
35301da177e4SLinus Torvalds 					}
35311da177e4SLinus Torvalds 					opt_rx->snd_wscale = snd_wscale;
35321da177e4SLinus Torvalds 				}
35331da177e4SLinus Torvalds 				break;
35341da177e4SLinus Torvalds 			case TCPOPT_TIMESTAMP:
3535f038ac8fSIlpo Järvinen 				if ((opsize == TCPOLEN_TIMESTAMP) &&
3536f038ac8fSIlpo Järvinen 				    ((estab && opt_rx->tstamp_ok) ||
3537bb5b7c11SDavid S. Miller 				     (!estab && sysctl_tcp_timestamps))) {
35381da177e4SLinus Torvalds 					opt_rx->saw_tstamp = 1;
3539d3e2ce3bSHarvey Harrison 					opt_rx->rcv_tsval = get_unaligned_be32(ptr);
3540d3e2ce3bSHarvey Harrison 					opt_rx->rcv_tsecr = get_unaligned_be32(ptr + 4);
35411da177e4SLinus Torvalds 				}
35421da177e4SLinus Torvalds 				break;
35431da177e4SLinus Torvalds 			case TCPOPT_SACK_PERM:
3544f038ac8fSIlpo Järvinen 				if (opsize == TCPOLEN_SACK_PERM && th->syn &&
3545bb5b7c11SDavid S. Miller 				    !estab && sysctl_tcp_sack) {
3546ab56222aSVijay Subramanian 					opt_rx->sack_ok = TCP_SACK_SEEN;
35471da177e4SLinus Torvalds 					tcp_sack_reset(opt_rx);
35481da177e4SLinus Torvalds 				}
35491da177e4SLinus Torvalds 				break;
35501da177e4SLinus Torvalds 
35511da177e4SLinus Torvalds 			case TCPOPT_SACK:
35521da177e4SLinus Torvalds 				if ((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) &&
35531da177e4SLinus Torvalds 				   !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) &&
35541da177e4SLinus Torvalds 				   opt_rx->sack_ok) {
35551da177e4SLinus Torvalds 					TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th;
35561da177e4SLinus Torvalds 				}
3557d7ea5b91SIlpo Järvinen 				break;
3558cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
3559cfb6eeb4SYOSHIFUJI Hideaki 			case TCPOPT_MD5SIG:
3560cfb6eeb4SYOSHIFUJI Hideaki 				/*
3561cfb6eeb4SYOSHIFUJI Hideaki 				 * The MD5 Hash has already been
3562cfb6eeb4SYOSHIFUJI Hideaki 				 * checked (see tcp_v{4,6}_do_rcv()).
3563cfb6eeb4SYOSHIFUJI Hideaki 				 */
3564cfb6eeb4SYOSHIFUJI Hideaki 				break;
3565cfb6eeb4SYOSHIFUJI Hideaki #endif
35662100c8d2SYuchung Cheng 			case TCPOPT_EXP:
35672100c8d2SYuchung Cheng 				/* Fast Open option shares code 254 using a
35682100c8d2SYuchung Cheng 				 * 16 bits magic number. It's valid only in
35692100c8d2SYuchung Cheng 				 * SYN or SYN-ACK with an even size.
35702100c8d2SYuchung Cheng 				 */
35712100c8d2SYuchung Cheng 				if (opsize < TCPOLEN_EXP_FASTOPEN_BASE ||
35722100c8d2SYuchung Cheng 				    get_unaligned_be16(ptr) != TCPOPT_FASTOPEN_MAGIC ||
35732100c8d2SYuchung Cheng 				    foc == NULL || !th->syn || (opsize & 1))
35742100c8d2SYuchung Cheng 					break;
35752100c8d2SYuchung Cheng 				foc->len = opsize - TCPOLEN_EXP_FASTOPEN_BASE;
35762100c8d2SYuchung Cheng 				if (foc->len >= TCP_FASTOPEN_COOKIE_MIN &&
35772100c8d2SYuchung Cheng 				    foc->len <= TCP_FASTOPEN_COOKIE_MAX)
35782100c8d2SYuchung Cheng 					memcpy(foc->val, ptr + 2, foc->len);
35792100c8d2SYuchung Cheng 				else if (foc->len != 0)
35802100c8d2SYuchung Cheng 					foc->len = -1;
35812100c8d2SYuchung Cheng 				break;
35822100c8d2SYuchung Cheng 
35832100c8d2SYuchung Cheng 			}
35841da177e4SLinus Torvalds 			ptr += opsize-2;
35851da177e4SLinus Torvalds 			length -= opsize;
35863ff50b79SStephen Hemminger 		}
35871da177e4SLinus Torvalds 	}
35881da177e4SLinus Torvalds }
35894bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_parse_options);
35901da177e4SLinus Torvalds 
3591a2a385d6SEric Dumazet static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr *th)
3592a4356b29SIlpo Järvinen {
3593cf533ea5SEric Dumazet 	const __be32 *ptr = (const __be32 *)(th + 1);
3594a4356b29SIlpo Järvinen 
3595a4356b29SIlpo Järvinen 	if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
3596a4356b29SIlpo Järvinen 			  | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
3597a4356b29SIlpo Järvinen 		tp->rx_opt.saw_tstamp = 1;
3598a4356b29SIlpo Järvinen 		++ptr;
3599a4356b29SIlpo Järvinen 		tp->rx_opt.rcv_tsval = ntohl(*ptr);
3600a4356b29SIlpo Järvinen 		++ptr;
3601ee684b6fSAndrey Vagin 		tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset;
3602a2a385d6SEric Dumazet 		return true;
3603a4356b29SIlpo Järvinen 	}
3604a2a385d6SEric Dumazet 	return false;
3605a4356b29SIlpo Järvinen }
3606a4356b29SIlpo Järvinen 
36071da177e4SLinus Torvalds /* Fast parse options. This hopes to only see timestamps.
36081da177e4SLinus Torvalds  * If it is wrong it falls back on tcp_parse_options().
36091da177e4SLinus Torvalds  */
3610a2a385d6SEric Dumazet static bool tcp_fast_parse_options(const struct sk_buff *skb,
36111a2c6181SChristoph Paasch 				   const struct tcphdr *th, struct tcp_sock *tp)
36121da177e4SLinus Torvalds {
36134957faadSWilliam Allen Simpson 	/* In the spirit of fast parsing, compare doff directly to constant
36144957faadSWilliam Allen Simpson 	 * values.  Because equality is used, short doff can be ignored here.
36154957faadSWilliam Allen Simpson 	 */
36164957faadSWilliam Allen Simpson 	if (th->doff == (sizeof(*th) / 4)) {
36171da177e4SLinus Torvalds 		tp->rx_opt.saw_tstamp = 0;
3618a2a385d6SEric Dumazet 		return false;
36191da177e4SLinus Torvalds 	} else if (tp->rx_opt.tstamp_ok &&
36204957faadSWilliam Allen Simpson 		   th->doff == ((sizeof(*th) + TCPOLEN_TSTAMP_ALIGNED) / 4)) {
3621a4356b29SIlpo Järvinen 		if (tcp_parse_aligned_timestamp(tp, th))
3622a2a385d6SEric Dumazet 			return true;
36231da177e4SLinus Torvalds 	}
3624ee684b6fSAndrey Vagin 
36251a2c6181SChristoph Paasch 	tcp_parse_options(skb, &tp->rx_opt, 1, NULL);
3626ee684b6fSAndrey Vagin 	if (tp->rx_opt.saw_tstamp)
3627ee684b6fSAndrey Vagin 		tp->rx_opt.rcv_tsecr -= tp->tsoffset;
3628ee684b6fSAndrey Vagin 
3629a2a385d6SEric Dumazet 	return true;
36301da177e4SLinus Torvalds }
36311da177e4SLinus Torvalds 
36327d5d5525SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
36337d5d5525SYOSHIFUJI Hideaki /*
36347d5d5525SYOSHIFUJI Hideaki  * Parse MD5 Signature option
36357d5d5525SYOSHIFUJI Hideaki  */
3636cf533ea5SEric Dumazet const u8 *tcp_parse_md5sig_option(const struct tcphdr *th)
36377d5d5525SYOSHIFUJI Hideaki {
36387d5d5525SYOSHIFUJI Hideaki 	int length = (th->doff << 2) - sizeof(*th);
3639cf533ea5SEric Dumazet 	const u8 *ptr = (const u8 *)(th + 1);
36407d5d5525SYOSHIFUJI Hideaki 
36417d5d5525SYOSHIFUJI Hideaki 	/* If the TCP option is too short, we can short cut */
36427d5d5525SYOSHIFUJI Hideaki 	if (length < TCPOLEN_MD5SIG)
36437d5d5525SYOSHIFUJI Hideaki 		return NULL;
36447d5d5525SYOSHIFUJI Hideaki 
36457d5d5525SYOSHIFUJI Hideaki 	while (length > 0) {
36467d5d5525SYOSHIFUJI Hideaki 		int opcode = *ptr++;
36477d5d5525SYOSHIFUJI Hideaki 		int opsize;
36487d5d5525SYOSHIFUJI Hideaki 
36497d5d5525SYOSHIFUJI Hideaki 		switch(opcode) {
36507d5d5525SYOSHIFUJI Hideaki 		case TCPOPT_EOL:
36517d5d5525SYOSHIFUJI Hideaki 			return NULL;
36527d5d5525SYOSHIFUJI Hideaki 		case TCPOPT_NOP:
36537d5d5525SYOSHIFUJI Hideaki 			length--;
36547d5d5525SYOSHIFUJI Hideaki 			continue;
36557d5d5525SYOSHIFUJI Hideaki 		default:
36567d5d5525SYOSHIFUJI Hideaki 			opsize = *ptr++;
36577d5d5525SYOSHIFUJI Hideaki 			if (opsize < 2 || opsize > length)
36587d5d5525SYOSHIFUJI Hideaki 				return NULL;
36597d5d5525SYOSHIFUJI Hideaki 			if (opcode == TCPOPT_MD5SIG)
3660ba78e2ddSDmitry Popov 				return opsize == TCPOLEN_MD5SIG ? ptr : NULL;
36617d5d5525SYOSHIFUJI Hideaki 		}
36627d5d5525SYOSHIFUJI Hideaki 		ptr += opsize - 2;
36637d5d5525SYOSHIFUJI Hideaki 		length -= opsize;
36647d5d5525SYOSHIFUJI Hideaki 	}
36657d5d5525SYOSHIFUJI Hideaki 	return NULL;
36667d5d5525SYOSHIFUJI Hideaki }
36674bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_parse_md5sig_option);
36687d5d5525SYOSHIFUJI Hideaki #endif
36697d5d5525SYOSHIFUJI Hideaki 
36701da177e4SLinus Torvalds /* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM
36711da177e4SLinus Torvalds  *
36721da177e4SLinus Torvalds  * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
36731da177e4SLinus Torvalds  * it can pass through stack. So, the following predicate verifies that
36741da177e4SLinus Torvalds  * this segment is not used for anything but congestion avoidance or
36751da177e4SLinus Torvalds  * fast retransmit. Moreover, we even are able to eliminate most of such
36761da177e4SLinus Torvalds  * second order effects, if we apply some small "replay" window (~RTO)
36771da177e4SLinus Torvalds  * to timestamp space.
36781da177e4SLinus Torvalds  *
36791da177e4SLinus Torvalds  * All these measures still do not guarantee that we reject wrapped ACKs
36801da177e4SLinus Torvalds  * on networks with high bandwidth, when sequence space is recycled fastly,
36811da177e4SLinus Torvalds  * but it guarantees that such events will be very rare and do not affect
36821da177e4SLinus Torvalds  * connection seriously. This doesn't look nice, but alas, PAWS is really
36831da177e4SLinus Torvalds  * buggy extension.
36841da177e4SLinus Torvalds  *
36851da177e4SLinus Torvalds  * [ Later note. Even worse! It is buggy for segments _with_ data. RFC
36861da177e4SLinus Torvalds  * states that events when retransmit arrives after original data are rare.
36871da177e4SLinus Torvalds  * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is
36881da177e4SLinus Torvalds  * the biggest problem on large power networks even with minor reordering.
36891da177e4SLinus Torvalds  * OK, let's give it small replay window. If peer clock is even 1hz, it is safe
36901da177e4SLinus Torvalds  * up to bandwidth of 18Gigabit/sec. 8) ]
36911da177e4SLinus Torvalds  */
36921da177e4SLinus Torvalds 
3693463c84b9SArnaldo Carvalho de Melo static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb)
36941da177e4SLinus Torvalds {
3695cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
3696cf533ea5SEric Dumazet 	const struct tcphdr *th = tcp_hdr(skb);
36971da177e4SLinus Torvalds 	u32 seq = TCP_SKB_CB(skb)->seq;
36981da177e4SLinus Torvalds 	u32 ack = TCP_SKB_CB(skb)->ack_seq;
36991da177e4SLinus Torvalds 
37001da177e4SLinus Torvalds 	return (/* 1. Pure ACK with correct sequence number. */
37011da177e4SLinus Torvalds 		(th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) &&
37021da177e4SLinus Torvalds 
37031da177e4SLinus Torvalds 		/* 2. ... and duplicate ACK. */
37041da177e4SLinus Torvalds 		ack == tp->snd_una &&
37051da177e4SLinus Torvalds 
37061da177e4SLinus Torvalds 		/* 3. ... and does not update window. */
37071da177e4SLinus Torvalds 		!tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) &&
37081da177e4SLinus Torvalds 
37091da177e4SLinus Torvalds 		/* 4. ... and sits in replay window. */
3710463c84b9SArnaldo Carvalho de Melo 		(s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) <= (inet_csk(sk)->icsk_rto * 1024) / HZ);
37111da177e4SLinus Torvalds }
37121da177e4SLinus Torvalds 
371367b95bd7SVijay Subramanian static inline bool tcp_paws_discard(const struct sock *sk,
3714056834d9SIlpo Järvinen 				   const struct sk_buff *skb)
37151da177e4SLinus Torvalds {
3716463c84b9SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
3717c887e6d2SIlpo Järvinen 
3718c887e6d2SIlpo Järvinen 	return !tcp_paws_check(&tp->rx_opt, TCP_PAWS_WINDOW) &&
3719c887e6d2SIlpo Järvinen 	       !tcp_disordered_ack(sk, skb);
37201da177e4SLinus Torvalds }
37211da177e4SLinus Torvalds 
37221da177e4SLinus Torvalds /* Check segment sequence number for validity.
37231da177e4SLinus Torvalds  *
37241da177e4SLinus Torvalds  * Segment controls are considered valid, if the segment
37251da177e4SLinus Torvalds  * fits to the window after truncation to the window. Acceptability
37261da177e4SLinus Torvalds  * of data (and SYN, FIN, of course) is checked separately.
37271da177e4SLinus Torvalds  * See tcp_data_queue(), for example.
37281da177e4SLinus Torvalds  *
37291da177e4SLinus Torvalds  * Also, controls (RST is main one) are accepted using RCV.WUP instead
37301da177e4SLinus Torvalds  * of RCV.NXT. Peer still did not advance his SND.UNA when we
37311da177e4SLinus Torvalds  * delayed ACK, so that hisSND.UNA<=ourRCV.WUP.
37321da177e4SLinus Torvalds  * (borrowed from freebsd)
37331da177e4SLinus Torvalds  */
37341da177e4SLinus Torvalds 
373567b95bd7SVijay Subramanian static inline bool tcp_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq)
37361da177e4SLinus Torvalds {
37371da177e4SLinus Torvalds 	return	!before(end_seq, tp->rcv_wup) &&
37381da177e4SLinus Torvalds 		!after(seq, tp->rcv_nxt + tcp_receive_window(tp));
37391da177e4SLinus Torvalds }
37401da177e4SLinus Torvalds 
37411da177e4SLinus Torvalds /* When we get a reset we do this. */
374210467163SJerry Chu void tcp_reset(struct sock *sk)
37431da177e4SLinus Torvalds {
37441da177e4SLinus Torvalds 	/* We want the right error as BSD sees it (and indeed as we do). */
37451da177e4SLinus Torvalds 	switch (sk->sk_state) {
37461da177e4SLinus Torvalds 	case TCP_SYN_SENT:
37471da177e4SLinus Torvalds 		sk->sk_err = ECONNREFUSED;
37481da177e4SLinus Torvalds 		break;
37491da177e4SLinus Torvalds 	case TCP_CLOSE_WAIT:
37501da177e4SLinus Torvalds 		sk->sk_err = EPIPE;
37511da177e4SLinus Torvalds 		break;
37521da177e4SLinus Torvalds 	case TCP_CLOSE:
37531da177e4SLinus Torvalds 		return;
37541da177e4SLinus Torvalds 	default:
37551da177e4SLinus Torvalds 		sk->sk_err = ECONNRESET;
37561da177e4SLinus Torvalds 	}
3757a4d25803STom Marshall 	/* This barrier is coupled with smp_rmb() in tcp_poll() */
3758a4d25803STom Marshall 	smp_wmb();
37591da177e4SLinus Torvalds 
37601da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD))
37611da177e4SLinus Torvalds 		sk->sk_error_report(sk);
37621da177e4SLinus Torvalds 
37631da177e4SLinus Torvalds 	tcp_done(sk);
37641da177e4SLinus Torvalds }
37651da177e4SLinus Torvalds 
37661da177e4SLinus Torvalds /*
37671da177e4SLinus Torvalds  * 	Process the FIN bit. This now behaves as it is supposed to work
37681da177e4SLinus Torvalds  *	and the FIN takes effect when it is validly part of sequence
37691da177e4SLinus Torvalds  *	space. Not before when we get holes.
37701da177e4SLinus Torvalds  *
37711da177e4SLinus Torvalds  *	If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
37721da177e4SLinus Torvalds  *	(and thence onto LAST-ACK and finally, CLOSE, we never enter
37731da177e4SLinus Torvalds  *	TIME-WAIT)
37741da177e4SLinus Torvalds  *
37751da177e4SLinus Torvalds  *	If we are in FINWAIT-1, a received FIN indicates simultaneous
37761da177e4SLinus Torvalds  *	close and we go into CLOSING (and later onto TIME-WAIT)
37771da177e4SLinus Torvalds  *
37781da177e4SLinus Torvalds  *	If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
37791da177e4SLinus Torvalds  */
378020c4cb79SEric Dumazet static void tcp_fin(struct sock *sk)
37811da177e4SLinus Torvalds {
37821da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
37831da177e4SLinus Torvalds 
3784463c84b9SArnaldo Carvalho de Melo 	inet_csk_schedule_ack(sk);
37851da177e4SLinus Torvalds 
37861da177e4SLinus Torvalds 	sk->sk_shutdown |= RCV_SHUTDOWN;
37871da177e4SLinus Torvalds 	sock_set_flag(sk, SOCK_DONE);
37881da177e4SLinus Torvalds 
37891da177e4SLinus Torvalds 	switch (sk->sk_state) {
37901da177e4SLinus Torvalds 	case TCP_SYN_RECV:
37911da177e4SLinus Torvalds 	case TCP_ESTABLISHED:
37921da177e4SLinus Torvalds 		/* Move to CLOSE_WAIT */
37931da177e4SLinus Torvalds 		tcp_set_state(sk, TCP_CLOSE_WAIT);
3794463c84b9SArnaldo Carvalho de Melo 		inet_csk(sk)->icsk_ack.pingpong = 1;
37951da177e4SLinus Torvalds 		break;
37961da177e4SLinus Torvalds 
37971da177e4SLinus Torvalds 	case TCP_CLOSE_WAIT:
37981da177e4SLinus Torvalds 	case TCP_CLOSING:
37991da177e4SLinus Torvalds 		/* Received a retransmission of the FIN, do
38001da177e4SLinus Torvalds 		 * nothing.
38011da177e4SLinus Torvalds 		 */
38021da177e4SLinus Torvalds 		break;
38031da177e4SLinus Torvalds 	case TCP_LAST_ACK:
38041da177e4SLinus Torvalds 		/* RFC793: Remain in the LAST-ACK state. */
38051da177e4SLinus Torvalds 		break;
38061da177e4SLinus Torvalds 
38071da177e4SLinus Torvalds 	case TCP_FIN_WAIT1:
38081da177e4SLinus Torvalds 		/* This case occurs when a simultaneous close
38091da177e4SLinus Torvalds 		 * happens, we must ack the received FIN and
38101da177e4SLinus Torvalds 		 * enter the CLOSING state.
38111da177e4SLinus Torvalds 		 */
38121da177e4SLinus Torvalds 		tcp_send_ack(sk);
38131da177e4SLinus Torvalds 		tcp_set_state(sk, TCP_CLOSING);
38141da177e4SLinus Torvalds 		break;
38151da177e4SLinus Torvalds 	case TCP_FIN_WAIT2:
38161da177e4SLinus Torvalds 		/* Received a FIN -- send ACK and enter TIME_WAIT. */
38171da177e4SLinus Torvalds 		tcp_send_ack(sk);
38181da177e4SLinus Torvalds 		tcp_time_wait(sk, TCP_TIME_WAIT, 0);
38191da177e4SLinus Torvalds 		break;
38201da177e4SLinus Torvalds 	default:
38211da177e4SLinus Torvalds 		/* Only TCP_LISTEN and TCP_CLOSE are left, in these
38221da177e4SLinus Torvalds 		 * cases we should never reach this piece of code.
38231da177e4SLinus Torvalds 		 */
3824058bd4d2SJoe Perches 		pr_err("%s: Impossible, sk->sk_state=%d\n",
38250dc47877SHarvey Harrison 		       __func__, sk->sk_state);
38261da177e4SLinus Torvalds 		break;
38273ff50b79SStephen Hemminger 	}
38281da177e4SLinus Torvalds 
38291da177e4SLinus Torvalds 	/* It _is_ possible, that we have something out-of-order _after_ FIN.
38301da177e4SLinus Torvalds 	 * Probably, we should reset in this case. For now drop them.
38311da177e4SLinus Torvalds 	 */
38321da177e4SLinus Torvalds 	__skb_queue_purge(&tp->out_of_order_queue);
3833e60402d0SIlpo Järvinen 	if (tcp_is_sack(tp))
38341da177e4SLinus Torvalds 		tcp_sack_reset(&tp->rx_opt);
38353ab224beSHideo Aoki 	sk_mem_reclaim(sk);
38361da177e4SLinus Torvalds 
38371da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD)) {
38381da177e4SLinus Torvalds 		sk->sk_state_change(sk);
38391da177e4SLinus Torvalds 
38401da177e4SLinus Torvalds 		/* Do not send POLL_HUP for half duplex close. */
38411da177e4SLinus Torvalds 		if (sk->sk_shutdown == SHUTDOWN_MASK ||
38421da177e4SLinus Torvalds 		    sk->sk_state == TCP_CLOSE)
38438d8ad9d7SPavel Emelyanov 			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
38441da177e4SLinus Torvalds 		else
38458d8ad9d7SPavel Emelyanov 			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
38461da177e4SLinus Torvalds 	}
38471da177e4SLinus Torvalds }
38481da177e4SLinus Torvalds 
3849a2a385d6SEric Dumazet static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,
3850056834d9SIlpo Järvinen 				  u32 end_seq)
38511da177e4SLinus Torvalds {
38521da177e4SLinus Torvalds 	if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) {
38531da177e4SLinus Torvalds 		if (before(seq, sp->start_seq))
38541da177e4SLinus Torvalds 			sp->start_seq = seq;
38551da177e4SLinus Torvalds 		if (after(end_seq, sp->end_seq))
38561da177e4SLinus Torvalds 			sp->end_seq = end_seq;
3857a2a385d6SEric Dumazet 		return true;
38581da177e4SLinus Torvalds 	}
3859a2a385d6SEric Dumazet 	return false;
38601da177e4SLinus Torvalds }
38611da177e4SLinus Torvalds 
38621ed83465SPavel Emelyanov static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq)
38631da177e4SLinus Torvalds {
38641ed83465SPavel Emelyanov 	struct tcp_sock *tp = tcp_sk(sk);
38651ed83465SPavel Emelyanov 
3866bb5b7c11SDavid S. Miller 	if (tcp_is_sack(tp) && sysctl_tcp_dsack) {
386740b215e5SPavel Emelyanov 		int mib_idx;
386840b215e5SPavel Emelyanov 
38691da177e4SLinus Torvalds 		if (before(seq, tp->rcv_nxt))
387040b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPDSACKOLDSENT;
38711da177e4SLinus Torvalds 		else
387240b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPDSACKOFOSENT;
387340b215e5SPavel Emelyanov 
3874de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), mib_idx);
38751da177e4SLinus Torvalds 
38761da177e4SLinus Torvalds 		tp->rx_opt.dsack = 1;
38771da177e4SLinus Torvalds 		tp->duplicate_sack[0].start_seq = seq;
38781da177e4SLinus Torvalds 		tp->duplicate_sack[0].end_seq = end_seq;
38791da177e4SLinus Torvalds 	}
38801da177e4SLinus Torvalds }
38811da177e4SLinus Torvalds 
38821ed83465SPavel Emelyanov static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq)
38831da177e4SLinus Torvalds {
38841ed83465SPavel Emelyanov 	struct tcp_sock *tp = tcp_sk(sk);
38851ed83465SPavel Emelyanov 
38861da177e4SLinus Torvalds 	if (!tp->rx_opt.dsack)
38871ed83465SPavel Emelyanov 		tcp_dsack_set(sk, seq, end_seq);
38881da177e4SLinus Torvalds 	else
38891da177e4SLinus Torvalds 		tcp_sack_extend(tp->duplicate_sack, seq, end_seq);
38901da177e4SLinus Torvalds }
38911da177e4SLinus Torvalds 
3892cf533ea5SEric Dumazet static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb)
38931da177e4SLinus Torvalds {
38941da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
38951da177e4SLinus Torvalds 
38961da177e4SLinus Torvalds 	if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
38971da177e4SLinus Torvalds 	    before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
3898de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
3899463c84b9SArnaldo Carvalho de Melo 		tcp_enter_quickack_mode(sk);
39001da177e4SLinus Torvalds 
3901bb5b7c11SDavid S. Miller 		if (tcp_is_sack(tp) && sysctl_tcp_dsack) {
39021da177e4SLinus Torvalds 			u32 end_seq = TCP_SKB_CB(skb)->end_seq;
39031da177e4SLinus Torvalds 
39041da177e4SLinus Torvalds 			if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))
39051da177e4SLinus Torvalds 				end_seq = tp->rcv_nxt;
39061ed83465SPavel Emelyanov 			tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, end_seq);
39071da177e4SLinus Torvalds 		}
39081da177e4SLinus Torvalds 	}
39091da177e4SLinus Torvalds 
39101da177e4SLinus Torvalds 	tcp_send_ack(sk);
39111da177e4SLinus Torvalds }
39121da177e4SLinus Torvalds 
39131da177e4SLinus Torvalds /* These routines update the SACK block as out-of-order packets arrive or
39141da177e4SLinus Torvalds  * in-order packets close up the sequence space.
39151da177e4SLinus Torvalds  */
39161da177e4SLinus Torvalds static void tcp_sack_maybe_coalesce(struct tcp_sock *tp)
39171da177e4SLinus Torvalds {
39181da177e4SLinus Torvalds 	int this_sack;
39191da177e4SLinus Torvalds 	struct tcp_sack_block *sp = &tp->selective_acks[0];
39201da177e4SLinus Torvalds 	struct tcp_sack_block *swalk = sp + 1;
39211da177e4SLinus Torvalds 
39221da177e4SLinus Torvalds 	/* See if the recent change to the first SACK eats into
39231da177e4SLinus Torvalds 	 * or hits the sequence space of other SACK blocks, if so coalesce.
39241da177e4SLinus Torvalds 	 */
39251da177e4SLinus Torvalds 	for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;) {
39261da177e4SLinus Torvalds 		if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) {
39271da177e4SLinus Torvalds 			int i;
39281da177e4SLinus Torvalds 
39291da177e4SLinus Torvalds 			/* Zap SWALK, by moving every further SACK up by one slot.
39301da177e4SLinus Torvalds 			 * Decrease num_sacks.
39311da177e4SLinus Torvalds 			 */
39321da177e4SLinus Torvalds 			tp->rx_opt.num_sacks--;
39331da177e4SLinus Torvalds 			for (i = this_sack; i < tp->rx_opt.num_sacks; i++)
39341da177e4SLinus Torvalds 				sp[i] = sp[i + 1];
39351da177e4SLinus Torvalds 			continue;
39361da177e4SLinus Torvalds 		}
39371da177e4SLinus Torvalds 		this_sack++, swalk++;
39381da177e4SLinus Torvalds 	}
39391da177e4SLinus Torvalds }
39401da177e4SLinus Torvalds 
39411da177e4SLinus Torvalds static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq)
39421da177e4SLinus Torvalds {
39431da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
39441da177e4SLinus Torvalds 	struct tcp_sack_block *sp = &tp->selective_acks[0];
39451da177e4SLinus Torvalds 	int cur_sacks = tp->rx_opt.num_sacks;
39461da177e4SLinus Torvalds 	int this_sack;
39471da177e4SLinus Torvalds 
39481da177e4SLinus Torvalds 	if (!cur_sacks)
39491da177e4SLinus Torvalds 		goto new_sack;
39501da177e4SLinus Torvalds 
39511da177e4SLinus Torvalds 	for (this_sack = 0; this_sack < cur_sacks; this_sack++, sp++) {
39521da177e4SLinus Torvalds 		if (tcp_sack_extend(sp, seq, end_seq)) {
39531da177e4SLinus Torvalds 			/* Rotate this_sack to the first one. */
39541da177e4SLinus Torvalds 			for (; this_sack > 0; this_sack--, sp--)
3955a0bffffcSIlpo Järvinen 				swap(*sp, *(sp - 1));
39561da177e4SLinus Torvalds 			if (cur_sacks > 1)
39571da177e4SLinus Torvalds 				tcp_sack_maybe_coalesce(tp);
39581da177e4SLinus Torvalds 			return;
39591da177e4SLinus Torvalds 		}
39601da177e4SLinus Torvalds 	}
39611da177e4SLinus Torvalds 
39621da177e4SLinus Torvalds 	/* Could not find an adjacent existing SACK, build a new one,
39631da177e4SLinus Torvalds 	 * put it at the front, and shift everyone else down.  We
39641da177e4SLinus Torvalds 	 * always know there is at least one SACK present already here.
39651da177e4SLinus Torvalds 	 *
39661da177e4SLinus Torvalds 	 * If the sack array is full, forget about the last one.
39671da177e4SLinus Torvalds 	 */
39684389ddedSAdam Langley 	if (this_sack >= TCP_NUM_SACKS) {
39691da177e4SLinus Torvalds 		this_sack--;
39701da177e4SLinus Torvalds 		tp->rx_opt.num_sacks--;
39711da177e4SLinus Torvalds 		sp--;
39721da177e4SLinus Torvalds 	}
39731da177e4SLinus Torvalds 	for (; this_sack > 0; this_sack--, sp--)
39741da177e4SLinus Torvalds 		*sp = *(sp - 1);
39751da177e4SLinus Torvalds 
39761da177e4SLinus Torvalds new_sack:
39771da177e4SLinus Torvalds 	/* Build the new head SACK, and we're done. */
39781da177e4SLinus Torvalds 	sp->start_seq = seq;
39791da177e4SLinus Torvalds 	sp->end_seq = end_seq;
39801da177e4SLinus Torvalds 	tp->rx_opt.num_sacks++;
39811da177e4SLinus Torvalds }
39821da177e4SLinus Torvalds 
39831da177e4SLinus Torvalds /* RCV.NXT advances, some SACKs should be eaten. */
39841da177e4SLinus Torvalds 
39851da177e4SLinus Torvalds static void tcp_sack_remove(struct tcp_sock *tp)
39861da177e4SLinus Torvalds {
39871da177e4SLinus Torvalds 	struct tcp_sack_block *sp = &tp->selective_acks[0];
39881da177e4SLinus Torvalds 	int num_sacks = tp->rx_opt.num_sacks;
39891da177e4SLinus Torvalds 	int this_sack;
39901da177e4SLinus Torvalds 
39911da177e4SLinus Torvalds 	/* Empty ofo queue, hence, all the SACKs are eaten. Clear. */
3992b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue)) {
39931da177e4SLinus Torvalds 		tp->rx_opt.num_sacks = 0;
39941da177e4SLinus Torvalds 		return;
39951da177e4SLinus Torvalds 	}
39961da177e4SLinus Torvalds 
39971da177e4SLinus Torvalds 	for (this_sack = 0; this_sack < num_sacks;) {
39981da177e4SLinus Torvalds 		/* Check if the start of the sack is covered by RCV.NXT. */
39991da177e4SLinus Torvalds 		if (!before(tp->rcv_nxt, sp->start_seq)) {
40001da177e4SLinus Torvalds 			int i;
40011da177e4SLinus Torvalds 
40021da177e4SLinus Torvalds 			/* RCV.NXT must cover all the block! */
4003547b792cSIlpo Järvinen 			WARN_ON(before(tp->rcv_nxt, sp->end_seq));
40041da177e4SLinus Torvalds 
40051da177e4SLinus Torvalds 			/* Zap this SACK, by moving forward any other SACKS. */
40061da177e4SLinus Torvalds 			for (i=this_sack+1; i < num_sacks; i++)
40071da177e4SLinus Torvalds 				tp->selective_acks[i-1] = tp->selective_acks[i];
40081da177e4SLinus Torvalds 			num_sacks--;
40091da177e4SLinus Torvalds 			continue;
40101da177e4SLinus Torvalds 		}
40111da177e4SLinus Torvalds 		this_sack++;
40121da177e4SLinus Torvalds 		sp++;
40131da177e4SLinus Torvalds 	}
40141da177e4SLinus Torvalds 	tp->rx_opt.num_sacks = num_sacks;
40151da177e4SLinus Torvalds }
40161da177e4SLinus Torvalds 
40171da177e4SLinus Torvalds /* This one checks to see if we can put data from the
40181da177e4SLinus Torvalds  * out_of_order queue into the receive_queue.
40191da177e4SLinus Torvalds  */
40201da177e4SLinus Torvalds static void tcp_ofo_queue(struct sock *sk)
40211da177e4SLinus Torvalds {
40221da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
40231da177e4SLinus Torvalds 	__u32 dsack_high = tp->rcv_nxt;
40241da177e4SLinus Torvalds 	struct sk_buff *skb;
40251da177e4SLinus Torvalds 
40261da177e4SLinus Torvalds 	while ((skb = skb_peek(&tp->out_of_order_queue)) != NULL) {
40271da177e4SLinus Torvalds 		if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
40281da177e4SLinus Torvalds 			break;
40291da177e4SLinus Torvalds 
40301da177e4SLinus Torvalds 		if (before(TCP_SKB_CB(skb)->seq, dsack_high)) {
40311da177e4SLinus Torvalds 			__u32 dsack = dsack_high;
40321da177e4SLinus Torvalds 			if (before(TCP_SKB_CB(skb)->end_seq, dsack_high))
40331da177e4SLinus Torvalds 				dsack_high = TCP_SKB_CB(skb)->end_seq;
40341ed83465SPavel Emelyanov 			tcp_dsack_extend(sk, TCP_SKB_CB(skb)->seq, dsack);
40351da177e4SLinus Torvalds 		}
40361da177e4SLinus Torvalds 
40371da177e4SLinus Torvalds 		if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
40381da177e4SLinus Torvalds 			SOCK_DEBUG(sk, "ofo packet was already received\n");
40398728b834SDavid S. Miller 			__skb_unlink(skb, &tp->out_of_order_queue);
40401da177e4SLinus Torvalds 			__kfree_skb(skb);
40411da177e4SLinus Torvalds 			continue;
40421da177e4SLinus Torvalds 		}
40431da177e4SLinus Torvalds 		SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n",
40441da177e4SLinus Torvalds 			   tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
40451da177e4SLinus Torvalds 			   TCP_SKB_CB(skb)->end_seq);
40461da177e4SLinus Torvalds 
40478728b834SDavid S. Miller 		__skb_unlink(skb, &tp->out_of_order_queue);
40481da177e4SLinus Torvalds 		__skb_queue_tail(&sk->sk_receive_queue, skb);
40491da177e4SLinus Torvalds 		tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
4050aa8223c7SArnaldo Carvalho de Melo 		if (tcp_hdr(skb)->fin)
405120c4cb79SEric Dumazet 			tcp_fin(sk);
40521da177e4SLinus Torvalds 	}
40531da177e4SLinus Torvalds }
40541da177e4SLinus Torvalds 
4055a2a385d6SEric Dumazet static bool tcp_prune_ofo_queue(struct sock *sk);
40561da177e4SLinus Torvalds static int tcp_prune_queue(struct sock *sk);
40571da177e4SLinus Torvalds 
4058c76562b6SMel Gorman static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb,
4059c76562b6SMel Gorman 				 unsigned int size)
4060b000cd37SVitaliy Gusev {
4061b000cd37SVitaliy Gusev 	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
4062c76562b6SMel Gorman 	    !sk_rmem_schedule(sk, skb, size)) {
4063b000cd37SVitaliy Gusev 
4064b000cd37SVitaliy Gusev 		if (tcp_prune_queue(sk) < 0)
4065b000cd37SVitaliy Gusev 			return -1;
4066b000cd37SVitaliy Gusev 
4067c76562b6SMel Gorman 		if (!sk_rmem_schedule(sk, skb, size)) {
406856f367bbSVitaliy Gusev 			if (!tcp_prune_ofo_queue(sk))
406956f367bbSVitaliy Gusev 				return -1;
407056f367bbSVitaliy Gusev 
4071c76562b6SMel Gorman 			if (!sk_rmem_schedule(sk, skb, size))
4072b000cd37SVitaliy Gusev 				return -1;
4073b000cd37SVitaliy Gusev 		}
4074b000cd37SVitaliy Gusev 	}
4075b000cd37SVitaliy Gusev 	return 0;
4076b000cd37SVitaliy Gusev }
4077b000cd37SVitaliy Gusev 
40781402d366SEric Dumazet /**
40791402d366SEric Dumazet  * tcp_try_coalesce - try to merge skb to prior one
40801402d366SEric Dumazet  * @sk: socket
40811402d366SEric Dumazet  * @to: prior buffer
40821402d366SEric Dumazet  * @from: buffer to add in queue
4083923dd347SEric Dumazet  * @fragstolen: pointer to boolean
40841402d366SEric Dumazet  *
40851402d366SEric Dumazet  * Before queueing skb @from after @to, try to merge them
40861402d366SEric Dumazet  * to reduce overall memory use and queue lengths, if cost is small.
40871402d366SEric Dumazet  * Packets in ofo or receive queues can stay a long time.
40881402d366SEric Dumazet  * Better try to coalesce them right now to avoid future collapses.
4089783c175fSEric Dumazet  * Returns true if caller should free @from instead of queueing it
40901402d366SEric Dumazet  */
4091783c175fSEric Dumazet static bool tcp_try_coalesce(struct sock *sk,
40921402d366SEric Dumazet 			     struct sk_buff *to,
4093329033f6SEric Dumazet 			     struct sk_buff *from,
4094329033f6SEric Dumazet 			     bool *fragstolen)
40951402d366SEric Dumazet {
4096bad43ca8SEric Dumazet 	int delta;
40971402d366SEric Dumazet 
4098329033f6SEric Dumazet 	*fragstolen = false;
409934a802a5SAlexander Duyck 
4100bad43ca8SEric Dumazet 	if (tcp_hdr(from)->fin)
4101bad43ca8SEric Dumazet 		return false;
41021ca7ee30SEric Dumazet 
41031ca7ee30SEric Dumazet 	/* Its possible this segment overlaps with prior segment in queue */
41041ca7ee30SEric Dumazet 	if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
41051ca7ee30SEric Dumazet 		return false;
41061ca7ee30SEric Dumazet 
4107bad43ca8SEric Dumazet 	if (!skb_try_coalesce(to, from, fragstolen, &delta))
4108783c175fSEric Dumazet 		return false;
410934a802a5SAlexander Duyck 
41101402d366SEric Dumazet 	atomic_add(delta, &sk->sk_rmem_alloc);
41111402d366SEric Dumazet 	sk_mem_charge(sk, delta);
411234a802a5SAlexander Duyck 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE);
411334a802a5SAlexander Duyck 	TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
411434a802a5SAlexander Duyck 	TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
411534a802a5SAlexander Duyck 	return true;
41161402d366SEric Dumazet }
41171402d366SEric Dumazet 
4118e86b2919SEric Dumazet static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
4119e86b2919SEric Dumazet {
4120e86b2919SEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
4121e86b2919SEric Dumazet 	struct sk_buff *skb1;
4122e86b2919SEric Dumazet 	u32 seq, end_seq;
4123e86b2919SEric Dumazet 
4124e86b2919SEric Dumazet 	TCP_ECN_check_ce(tp, skb);
4125e86b2919SEric Dumazet 
4126c76562b6SMel Gorman 	if (unlikely(tcp_try_rmem_schedule(sk, skb, skb->truesize))) {
4127a6df1ae9SEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFODROP);
4128e86b2919SEric Dumazet 		__kfree_skb(skb);
4129e86b2919SEric Dumazet 		return;
4130e86b2919SEric Dumazet 	}
4131e86b2919SEric Dumazet 
4132e86b2919SEric Dumazet 	/* Disable header prediction. */
4133e86b2919SEric Dumazet 	tp->pred_flags = 0;
4134e86b2919SEric Dumazet 	inet_csk_schedule_ack(sk);
4135e86b2919SEric Dumazet 
4136a6df1ae9SEric Dumazet 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
4137e86b2919SEric Dumazet 	SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n",
4138e86b2919SEric Dumazet 		   tp->rcv_nxt, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
4139e86b2919SEric Dumazet 
4140e86b2919SEric Dumazet 	skb1 = skb_peek_tail(&tp->out_of_order_queue);
4141e86b2919SEric Dumazet 	if (!skb1) {
4142e86b2919SEric Dumazet 		/* Initial out of order segment, build 1 SACK. */
4143e86b2919SEric Dumazet 		if (tcp_is_sack(tp)) {
4144e86b2919SEric Dumazet 			tp->rx_opt.num_sacks = 1;
4145e86b2919SEric Dumazet 			tp->selective_acks[0].start_seq = TCP_SKB_CB(skb)->seq;
4146e86b2919SEric Dumazet 			tp->selective_acks[0].end_seq =
4147e86b2919SEric Dumazet 						TCP_SKB_CB(skb)->end_seq;
4148e86b2919SEric Dumazet 		}
4149e86b2919SEric Dumazet 		__skb_queue_head(&tp->out_of_order_queue, skb);
4150e86b2919SEric Dumazet 		goto end;
4151e86b2919SEric Dumazet 	}
4152e86b2919SEric Dumazet 
4153e86b2919SEric Dumazet 	seq = TCP_SKB_CB(skb)->seq;
4154e86b2919SEric Dumazet 	end_seq = TCP_SKB_CB(skb)->end_seq;
4155e86b2919SEric Dumazet 
4156e86b2919SEric Dumazet 	if (seq == TCP_SKB_CB(skb1)->end_seq) {
4157329033f6SEric Dumazet 		bool fragstolen;
4158329033f6SEric Dumazet 
4159329033f6SEric Dumazet 		if (!tcp_try_coalesce(sk, skb1, skb, &fragstolen)) {
41601402d366SEric Dumazet 			__skb_queue_after(&tp->out_of_order_queue, skb1, skb);
41611402d366SEric Dumazet 		} else {
4162923dd347SEric Dumazet 			kfree_skb_partial(skb, fragstolen);
4163c8628155SEric Dumazet 			skb = NULL;
4164c8628155SEric Dumazet 		}
4165e86b2919SEric Dumazet 
4166e86b2919SEric Dumazet 		if (!tp->rx_opt.num_sacks ||
4167e86b2919SEric Dumazet 		    tp->selective_acks[0].end_seq != seq)
4168e86b2919SEric Dumazet 			goto add_sack;
4169e86b2919SEric Dumazet 
4170e86b2919SEric Dumazet 		/* Common case: data arrive in order after hole. */
4171e86b2919SEric Dumazet 		tp->selective_acks[0].end_seq = end_seq;
4172e86b2919SEric Dumazet 		goto end;
4173e86b2919SEric Dumazet 	}
4174e86b2919SEric Dumazet 
4175e86b2919SEric Dumazet 	/* Find place to insert this segment. */
4176e86b2919SEric Dumazet 	while (1) {
4177e86b2919SEric Dumazet 		if (!after(TCP_SKB_CB(skb1)->seq, seq))
4178e86b2919SEric Dumazet 			break;
4179e86b2919SEric Dumazet 		if (skb_queue_is_first(&tp->out_of_order_queue, skb1)) {
4180e86b2919SEric Dumazet 			skb1 = NULL;
4181e86b2919SEric Dumazet 			break;
4182e86b2919SEric Dumazet 		}
4183e86b2919SEric Dumazet 		skb1 = skb_queue_prev(&tp->out_of_order_queue, skb1);
4184e86b2919SEric Dumazet 	}
4185e86b2919SEric Dumazet 
4186e86b2919SEric Dumazet 	/* Do skb overlap to previous one? */
4187e86b2919SEric Dumazet 	if (skb1 && before(seq, TCP_SKB_CB(skb1)->end_seq)) {
4188e86b2919SEric Dumazet 		if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
4189e86b2919SEric Dumazet 			/* All the bits are present. Drop. */
4190a6df1ae9SEric Dumazet 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
4191e86b2919SEric Dumazet 			__kfree_skb(skb);
4192e86b2919SEric Dumazet 			skb = NULL;
4193e86b2919SEric Dumazet 			tcp_dsack_set(sk, seq, end_seq);
4194e86b2919SEric Dumazet 			goto add_sack;
4195e86b2919SEric Dumazet 		}
4196e86b2919SEric Dumazet 		if (after(seq, TCP_SKB_CB(skb1)->seq)) {
4197e86b2919SEric Dumazet 			/* Partial overlap. */
4198e86b2919SEric Dumazet 			tcp_dsack_set(sk, seq,
4199e86b2919SEric Dumazet 				      TCP_SKB_CB(skb1)->end_seq);
4200e86b2919SEric Dumazet 		} else {
4201e86b2919SEric Dumazet 			if (skb_queue_is_first(&tp->out_of_order_queue,
4202e86b2919SEric Dumazet 					       skb1))
4203e86b2919SEric Dumazet 				skb1 = NULL;
4204e86b2919SEric Dumazet 			else
4205e86b2919SEric Dumazet 				skb1 = skb_queue_prev(
4206e86b2919SEric Dumazet 					&tp->out_of_order_queue,
4207e86b2919SEric Dumazet 					skb1);
4208e86b2919SEric Dumazet 		}
4209e86b2919SEric Dumazet 	}
4210e86b2919SEric Dumazet 	if (!skb1)
4211e86b2919SEric Dumazet 		__skb_queue_head(&tp->out_of_order_queue, skb);
4212e86b2919SEric Dumazet 	else
4213e86b2919SEric Dumazet 		__skb_queue_after(&tp->out_of_order_queue, skb1, skb);
4214e86b2919SEric Dumazet 
4215e86b2919SEric Dumazet 	/* And clean segments covered by new one as whole. */
4216e86b2919SEric Dumazet 	while (!skb_queue_is_last(&tp->out_of_order_queue, skb)) {
4217e86b2919SEric Dumazet 		skb1 = skb_queue_next(&tp->out_of_order_queue, skb);
4218e86b2919SEric Dumazet 
4219e86b2919SEric Dumazet 		if (!after(end_seq, TCP_SKB_CB(skb1)->seq))
4220e86b2919SEric Dumazet 			break;
4221e86b2919SEric Dumazet 		if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
4222e86b2919SEric Dumazet 			tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
4223e86b2919SEric Dumazet 					 end_seq);
4224e86b2919SEric Dumazet 			break;
4225e86b2919SEric Dumazet 		}
4226e86b2919SEric Dumazet 		__skb_unlink(skb1, &tp->out_of_order_queue);
4227e86b2919SEric Dumazet 		tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
4228e86b2919SEric Dumazet 				 TCP_SKB_CB(skb1)->end_seq);
4229a6df1ae9SEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
4230e86b2919SEric Dumazet 		__kfree_skb(skb1);
4231e86b2919SEric Dumazet 	}
4232e86b2919SEric Dumazet 
4233e86b2919SEric Dumazet add_sack:
4234e86b2919SEric Dumazet 	if (tcp_is_sack(tp))
4235e86b2919SEric Dumazet 		tcp_sack_new_ofo_skb(sk, seq, end_seq);
4236e86b2919SEric Dumazet end:
4237e86b2919SEric Dumazet 	if (skb)
4238e86b2919SEric Dumazet 		skb_set_owner_r(skb, sk);
4239e86b2919SEric Dumazet }
4240e86b2919SEric Dumazet 
4241292e8d8cSPavel Emelyanov static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int hdrlen,
4242b081f85cSEric Dumazet 		  bool *fragstolen)
4243b081f85cSEric Dumazet {
4244b081f85cSEric Dumazet 	int eaten;
4245b081f85cSEric Dumazet 	struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue);
4246b081f85cSEric Dumazet 
4247b081f85cSEric Dumazet 	__skb_pull(skb, hdrlen);
4248b081f85cSEric Dumazet 	eaten = (tail &&
4249b081f85cSEric Dumazet 		 tcp_try_coalesce(sk, tail, skb, fragstolen)) ? 1 : 0;
4250b081f85cSEric Dumazet 	tcp_sk(sk)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
4251b081f85cSEric Dumazet 	if (!eaten) {
4252b081f85cSEric Dumazet 		__skb_queue_tail(&sk->sk_receive_queue, skb);
4253b081f85cSEric Dumazet 		skb_set_owner_r(skb, sk);
4254b081f85cSEric Dumazet 	}
4255b081f85cSEric Dumazet 	return eaten;
4256b081f85cSEric Dumazet }
4257e86b2919SEric Dumazet 
4258292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
4259292e8d8cSPavel Emelyanov {
4260c76562b6SMel Gorman 	struct sk_buff *skb = NULL;
4261292e8d8cSPavel Emelyanov 	struct tcphdr *th;
4262292e8d8cSPavel Emelyanov 	bool fragstolen;
4263292e8d8cSPavel Emelyanov 
4264c454e611SPavel Emelyanov 	if (size == 0)
4265c454e611SPavel Emelyanov 		return 0;
4266c454e611SPavel Emelyanov 
4267292e8d8cSPavel Emelyanov 	skb = alloc_skb(size + sizeof(*th), sk->sk_allocation);
4268292e8d8cSPavel Emelyanov 	if (!skb)
4269292e8d8cSPavel Emelyanov 		goto err;
4270292e8d8cSPavel Emelyanov 
4271c76562b6SMel Gorman 	if (tcp_try_rmem_schedule(sk, skb, size + sizeof(*th)))
4272c76562b6SMel Gorman 		goto err_free;
4273c76562b6SMel Gorman 
4274292e8d8cSPavel Emelyanov 	th = (struct tcphdr *)skb_put(skb, sizeof(*th));
4275292e8d8cSPavel Emelyanov 	skb_reset_transport_header(skb);
4276292e8d8cSPavel Emelyanov 	memset(th, 0, sizeof(*th));
4277292e8d8cSPavel Emelyanov 
4278292e8d8cSPavel Emelyanov 	if (memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size))
4279292e8d8cSPavel Emelyanov 		goto err_free;
4280292e8d8cSPavel Emelyanov 
4281292e8d8cSPavel Emelyanov 	TCP_SKB_CB(skb)->seq = tcp_sk(sk)->rcv_nxt;
4282292e8d8cSPavel Emelyanov 	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size;
4283292e8d8cSPavel Emelyanov 	TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1;
4284292e8d8cSPavel Emelyanov 
4285292e8d8cSPavel Emelyanov 	if (tcp_queue_rcv(sk, skb, sizeof(*th), &fragstolen)) {
4286292e8d8cSPavel Emelyanov 		WARN_ON_ONCE(fragstolen); /* should not happen */
4287292e8d8cSPavel Emelyanov 		__kfree_skb(skb);
4288292e8d8cSPavel Emelyanov 	}
4289292e8d8cSPavel Emelyanov 	return size;
4290292e8d8cSPavel Emelyanov 
4291292e8d8cSPavel Emelyanov err_free:
4292292e8d8cSPavel Emelyanov 	kfree_skb(skb);
4293292e8d8cSPavel Emelyanov err:
4294292e8d8cSPavel Emelyanov 	return -ENOMEM;
4295292e8d8cSPavel Emelyanov }
4296292e8d8cSPavel Emelyanov 
42971da177e4SLinus Torvalds static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
42981da177e4SLinus Torvalds {
4299cf533ea5SEric Dumazet 	const struct tcphdr *th = tcp_hdr(skb);
43001da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
43011da177e4SLinus Torvalds 	int eaten = -1;
4302329033f6SEric Dumazet 	bool fragstolen = false;
43031da177e4SLinus Torvalds 
43041da177e4SLinus Torvalds 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq)
43051da177e4SLinus Torvalds 		goto drop;
43061da177e4SLinus Torvalds 
4307f84af32cSEric Dumazet 	skb_dst_drop(skb);
43081da177e4SLinus Torvalds 	__skb_pull(skb, th->doff * 4);
43091da177e4SLinus Torvalds 
43101da177e4SLinus Torvalds 	TCP_ECN_accept_cwr(tp, skb);
43111da177e4SLinus Torvalds 
43121da177e4SLinus Torvalds 	tp->rx_opt.dsack = 0;
43131da177e4SLinus Torvalds 
43141da177e4SLinus Torvalds 	/*  Queue data for delivery to the user.
43151da177e4SLinus Torvalds 	 *  Packets in sequence go to the receive queue.
43161da177e4SLinus Torvalds 	 *  Out of sequence packets to the out_of_order_queue.
43171da177e4SLinus Torvalds 	 */
43181da177e4SLinus Torvalds 	if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
43191da177e4SLinus Torvalds 		if (tcp_receive_window(tp) == 0)
43201da177e4SLinus Torvalds 			goto out_of_window;
43211da177e4SLinus Torvalds 
43221da177e4SLinus Torvalds 		/* Ok. In sequence. In window. */
43231da177e4SLinus Torvalds 		if (tp->ucopy.task == current &&
43241da177e4SLinus Torvalds 		    tp->copied_seq == tp->rcv_nxt && tp->ucopy.len &&
43251da177e4SLinus Torvalds 		    sock_owned_by_user(sk) && !tp->urg_data) {
43261da177e4SLinus Torvalds 			int chunk = min_t(unsigned int, skb->len,
43271da177e4SLinus Torvalds 					  tp->ucopy.len);
43281da177e4SLinus Torvalds 
43291da177e4SLinus Torvalds 			__set_current_state(TASK_RUNNING);
43301da177e4SLinus Torvalds 
43311da177e4SLinus Torvalds 			local_bh_enable();
43321da177e4SLinus Torvalds 			if (!skb_copy_datagram_iovec(skb, 0, tp->ucopy.iov, chunk)) {
43331da177e4SLinus Torvalds 				tp->ucopy.len -= chunk;
43341da177e4SLinus Torvalds 				tp->copied_seq += chunk;
433544f5324bSJerry Chu 				eaten = (chunk == skb->len);
43361da177e4SLinus Torvalds 				tcp_rcv_space_adjust(sk);
43371da177e4SLinus Torvalds 			}
43381da177e4SLinus Torvalds 			local_bh_disable();
43391da177e4SLinus Torvalds 		}
43401da177e4SLinus Torvalds 
43411da177e4SLinus Torvalds 		if (eaten <= 0) {
43421da177e4SLinus Torvalds queue_and_out:
43431da177e4SLinus Torvalds 			if (eaten < 0 &&
4344c76562b6SMel Gorman 			    tcp_try_rmem_schedule(sk, skb, skb->truesize))
43451da177e4SLinus Torvalds 				goto drop;
4346b000cd37SVitaliy Gusev 
4347b081f85cSEric Dumazet 			eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen);
43481402d366SEric Dumazet 		}
43491da177e4SLinus Torvalds 		tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
43501da177e4SLinus Torvalds 		if (skb->len)
43519e412ba7SIlpo Järvinen 			tcp_event_data_recv(sk, skb);
43521da177e4SLinus Torvalds 		if (th->fin)
435320c4cb79SEric Dumazet 			tcp_fin(sk);
43541da177e4SLinus Torvalds 
4355b03efcfbSDavid S. Miller 		if (!skb_queue_empty(&tp->out_of_order_queue)) {
43561da177e4SLinus Torvalds 			tcp_ofo_queue(sk);
43571da177e4SLinus Torvalds 
43581da177e4SLinus Torvalds 			/* RFC2581. 4.2. SHOULD send immediate ACK, when
43591da177e4SLinus Torvalds 			 * gap in queue is filled.
43601da177e4SLinus Torvalds 			 */
4361b03efcfbSDavid S. Miller 			if (skb_queue_empty(&tp->out_of_order_queue))
4362463c84b9SArnaldo Carvalho de Melo 				inet_csk(sk)->icsk_ack.pingpong = 0;
43631da177e4SLinus Torvalds 		}
43641da177e4SLinus Torvalds 
43651da177e4SLinus Torvalds 		if (tp->rx_opt.num_sacks)
43661da177e4SLinus Torvalds 			tcp_sack_remove(tp);
43671da177e4SLinus Torvalds 
43689e412ba7SIlpo Järvinen 		tcp_fast_path_check(sk);
43691da177e4SLinus Torvalds 
4370923dd347SEric Dumazet 		if (eaten > 0)
4371923dd347SEric Dumazet 			kfree_skb_partial(skb, fragstolen);
43721d57f195SEric Dumazet 		if (!sock_flag(sk, SOCK_DEAD))
43731da177e4SLinus Torvalds 			sk->sk_data_ready(sk, 0);
43741da177e4SLinus Torvalds 		return;
43751da177e4SLinus Torvalds 	}
43761da177e4SLinus Torvalds 
43771da177e4SLinus Torvalds 	if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
43781da177e4SLinus Torvalds 		/* A retransmit, 2nd most common case.  Force an immediate ack. */
4379de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
43801ed83465SPavel Emelyanov 		tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
43811da177e4SLinus Torvalds 
43821da177e4SLinus Torvalds out_of_window:
4383463c84b9SArnaldo Carvalho de Melo 		tcp_enter_quickack_mode(sk);
4384463c84b9SArnaldo Carvalho de Melo 		inet_csk_schedule_ack(sk);
43851da177e4SLinus Torvalds drop:
43861da177e4SLinus Torvalds 		__kfree_skb(skb);
43871da177e4SLinus Torvalds 		return;
43881da177e4SLinus Torvalds 	}
43891da177e4SLinus Torvalds 
43901da177e4SLinus Torvalds 	/* Out of window. F.e. zero window probe. */
43911da177e4SLinus Torvalds 	if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp)))
43921da177e4SLinus Torvalds 		goto out_of_window;
43931da177e4SLinus Torvalds 
4394463c84b9SArnaldo Carvalho de Melo 	tcp_enter_quickack_mode(sk);
43951da177e4SLinus Torvalds 
43961da177e4SLinus Torvalds 	if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
43971da177e4SLinus Torvalds 		/* Partial packet, seq < rcv_next < end_seq */
43981da177e4SLinus Torvalds 		SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n",
43991da177e4SLinus Torvalds 			   tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
44001da177e4SLinus Torvalds 			   TCP_SKB_CB(skb)->end_seq);
44011da177e4SLinus Torvalds 
44021ed83465SPavel Emelyanov 		tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
44031da177e4SLinus Torvalds 
44041da177e4SLinus Torvalds 		/* If window is closed, drop tail of packet. But after
44051da177e4SLinus Torvalds 		 * remembering D-SACK for its head made in previous line.
44061da177e4SLinus Torvalds 		 */
44071da177e4SLinus Torvalds 		if (!tcp_receive_window(tp))
44081da177e4SLinus Torvalds 			goto out_of_window;
44091da177e4SLinus Torvalds 		goto queue_and_out;
44101da177e4SLinus Torvalds 	}
44111da177e4SLinus Torvalds 
4412e86b2919SEric Dumazet 	tcp_data_queue_ofo(sk, skb);
44131da177e4SLinus Torvalds }
44141da177e4SLinus Torvalds 
44152cf46637SIlpo Järvinen static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
44162cf46637SIlpo Järvinen 					struct sk_buff_head *list)
44172cf46637SIlpo Järvinen {
441891521944SDavid S. Miller 	struct sk_buff *next = NULL;
441991521944SDavid S. Miller 
442091521944SDavid S. Miller 	if (!skb_queue_is_last(list, skb))
442191521944SDavid S. Miller 		next = skb_queue_next(list, skb);
44222cf46637SIlpo Järvinen 
44232cf46637SIlpo Järvinen 	__skb_unlink(skb, list);
44242cf46637SIlpo Järvinen 	__kfree_skb(skb);
44252cf46637SIlpo Järvinen 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRCVCOLLAPSED);
44262cf46637SIlpo Järvinen 
44272cf46637SIlpo Järvinen 	return next;
44282cf46637SIlpo Järvinen }
44292cf46637SIlpo Järvinen 
44301da177e4SLinus Torvalds /* Collapse contiguous sequence of skbs head..tail with
44311da177e4SLinus Torvalds  * sequence numbers start..end.
443291521944SDavid S. Miller  *
443391521944SDavid S. Miller  * If tail is NULL, this means until the end of the list.
443491521944SDavid S. Miller  *
44351da177e4SLinus Torvalds  * Segments with FIN/SYN are not collapsed (only because this
44361da177e4SLinus Torvalds  * simplifies code)
44371da177e4SLinus Torvalds  */
44381da177e4SLinus Torvalds static void
44398728b834SDavid S. Miller tcp_collapse(struct sock *sk, struct sk_buff_head *list,
44408728b834SDavid S. Miller 	     struct sk_buff *head, struct sk_buff *tail,
44418728b834SDavid S. Miller 	     u32 start, u32 end)
44421da177e4SLinus Torvalds {
444391521944SDavid S. Miller 	struct sk_buff *skb, *n;
444491521944SDavid S. Miller 	bool end_of_skbs;
44451da177e4SLinus Torvalds 
4446caa20d9aSStephen Hemminger 	/* First, check that queue is collapsible and find
44471da177e4SLinus Torvalds 	 * the point where collapsing can be useful. */
444891521944SDavid S. Miller 	skb = head;
444991521944SDavid S. Miller restart:
445091521944SDavid S. Miller 	end_of_skbs = true;
445191521944SDavid S. Miller 	skb_queue_walk_from_safe(list, skb, n) {
445291521944SDavid S. Miller 		if (skb == tail)
445391521944SDavid S. Miller 			break;
44541da177e4SLinus Torvalds 		/* No new bits? It is possible on ofo queue. */
44551da177e4SLinus Torvalds 		if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
44562cf46637SIlpo Järvinen 			skb = tcp_collapse_one(sk, skb, list);
445791521944SDavid S. Miller 			if (!skb)
445891521944SDavid S. Miller 				break;
445991521944SDavid S. Miller 			goto restart;
44601da177e4SLinus Torvalds 		}
44611da177e4SLinus Torvalds 
44621da177e4SLinus Torvalds 		/* The first skb to collapse is:
44631da177e4SLinus Torvalds 		 * - not SYN/FIN and
44641da177e4SLinus Torvalds 		 * - bloated or contains data before "start" or
44651da177e4SLinus Torvalds 		 *   overlaps to the next one.
44661da177e4SLinus Torvalds 		 */
4467aa8223c7SArnaldo Carvalho de Melo 		if (!tcp_hdr(skb)->syn && !tcp_hdr(skb)->fin &&
44681da177e4SLinus Torvalds 		    (tcp_win_from_space(skb->truesize) > skb->len ||
446991521944SDavid S. Miller 		     before(TCP_SKB_CB(skb)->seq, start))) {
447091521944SDavid S. Miller 			end_of_skbs = false;
44711da177e4SLinus Torvalds 			break;
447291521944SDavid S. Miller 		}
447391521944SDavid S. Miller 
447491521944SDavid S. Miller 		if (!skb_queue_is_last(list, skb)) {
447591521944SDavid S. Miller 			struct sk_buff *next = skb_queue_next(list, skb);
447691521944SDavid S. Miller 			if (next != tail &&
447791521944SDavid S. Miller 			    TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(next)->seq) {
447891521944SDavid S. Miller 				end_of_skbs = false;
447991521944SDavid S. Miller 				break;
448091521944SDavid S. Miller 			}
448191521944SDavid S. Miller 		}
44821da177e4SLinus Torvalds 
44831da177e4SLinus Torvalds 		/* Decided to skip this, advance start seq. */
44841da177e4SLinus Torvalds 		start = TCP_SKB_CB(skb)->end_seq;
44851da177e4SLinus Torvalds 	}
448691521944SDavid S. Miller 	if (end_of_skbs || tcp_hdr(skb)->syn || tcp_hdr(skb)->fin)
44871da177e4SLinus Torvalds 		return;
44881da177e4SLinus Torvalds 
44891da177e4SLinus Torvalds 	while (before(start, end)) {
44901da177e4SLinus Torvalds 		struct sk_buff *nskb;
4491c2636b4dSChuck Lever 		unsigned int header = skb_headroom(skb);
44921da177e4SLinus Torvalds 		int copy = SKB_MAX_ORDER(header, 0);
44931da177e4SLinus Torvalds 
44941da177e4SLinus Torvalds 		/* Too big header? This can happen with IPv6. */
44951da177e4SLinus Torvalds 		if (copy < 0)
44961da177e4SLinus Torvalds 			return;
44971da177e4SLinus Torvalds 		if (end - start < copy)
44981da177e4SLinus Torvalds 			copy = end - start;
44991da177e4SLinus Torvalds 		nskb = alloc_skb(copy + header, GFP_ATOMIC);
45001da177e4SLinus Torvalds 		if (!nskb)
45011da177e4SLinus Torvalds 			return;
4502c51957daSArnaldo Carvalho de Melo 
450398e399f8SArnaldo Carvalho de Melo 		skb_set_mac_header(nskb, skb_mac_header(skb) - skb->head);
45049c70220bSArnaldo Carvalho de Melo 		skb_set_network_header(nskb, (skb_network_header(skb) -
45059c70220bSArnaldo Carvalho de Melo 					      skb->head));
45069c70220bSArnaldo Carvalho de Melo 		skb_set_transport_header(nskb, (skb_transport_header(skb) -
45079c70220bSArnaldo Carvalho de Melo 						skb->head));
45081da177e4SLinus Torvalds 		skb_reserve(nskb, header);
45091da177e4SLinus Torvalds 		memcpy(nskb->head, skb->head, header);
45101da177e4SLinus Torvalds 		memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
45111da177e4SLinus Torvalds 		TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
451243f59c89SDavid S. Miller 		__skb_queue_before(list, skb, nskb);
45133ab224beSHideo Aoki 		skb_set_owner_r(nskb, sk);
45141da177e4SLinus Torvalds 
45151da177e4SLinus Torvalds 		/* Copy data, releasing collapsed skbs. */
45161da177e4SLinus Torvalds 		while (copy > 0) {
45171da177e4SLinus Torvalds 			int offset = start - TCP_SKB_CB(skb)->seq;
45181da177e4SLinus Torvalds 			int size = TCP_SKB_CB(skb)->end_seq - start;
45191da177e4SLinus Torvalds 
452009a62660SKris Katterjohn 			BUG_ON(offset < 0);
45211da177e4SLinus Torvalds 			if (size > 0) {
45221da177e4SLinus Torvalds 				size = min(copy, size);
45231da177e4SLinus Torvalds 				if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
45241da177e4SLinus Torvalds 					BUG();
45251da177e4SLinus Torvalds 				TCP_SKB_CB(nskb)->end_seq += size;
45261da177e4SLinus Torvalds 				copy -= size;
45271da177e4SLinus Torvalds 				start += size;
45281da177e4SLinus Torvalds 			}
45291da177e4SLinus Torvalds 			if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
45302cf46637SIlpo Järvinen 				skb = tcp_collapse_one(sk, skb, list);
453191521944SDavid S. Miller 				if (!skb ||
453291521944SDavid S. Miller 				    skb == tail ||
4533aa8223c7SArnaldo Carvalho de Melo 				    tcp_hdr(skb)->syn ||
4534aa8223c7SArnaldo Carvalho de Melo 				    tcp_hdr(skb)->fin)
45351da177e4SLinus Torvalds 					return;
45361da177e4SLinus Torvalds 			}
45371da177e4SLinus Torvalds 		}
45381da177e4SLinus Torvalds 	}
45391da177e4SLinus Torvalds }
45401da177e4SLinus Torvalds 
45411da177e4SLinus Torvalds /* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
45421da177e4SLinus Torvalds  * and tcp_collapse() them until all the queue is collapsed.
45431da177e4SLinus Torvalds  */
45441da177e4SLinus Torvalds static void tcp_collapse_ofo_queue(struct sock *sk)
45451da177e4SLinus Torvalds {
45461da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
45471da177e4SLinus Torvalds 	struct sk_buff *skb = skb_peek(&tp->out_of_order_queue);
45481da177e4SLinus Torvalds 	struct sk_buff *head;
45491da177e4SLinus Torvalds 	u32 start, end;
45501da177e4SLinus Torvalds 
45511da177e4SLinus Torvalds 	if (skb == NULL)
45521da177e4SLinus Torvalds 		return;
45531da177e4SLinus Torvalds 
45541da177e4SLinus Torvalds 	start = TCP_SKB_CB(skb)->seq;
45551da177e4SLinus Torvalds 	end = TCP_SKB_CB(skb)->end_seq;
45561da177e4SLinus Torvalds 	head = skb;
45571da177e4SLinus Torvalds 
45581da177e4SLinus Torvalds 	for (;;) {
455991521944SDavid S. Miller 		struct sk_buff *next = NULL;
456091521944SDavid S. Miller 
456191521944SDavid S. Miller 		if (!skb_queue_is_last(&tp->out_of_order_queue, skb))
456291521944SDavid S. Miller 			next = skb_queue_next(&tp->out_of_order_queue, skb);
456391521944SDavid S. Miller 		skb = next;
45641da177e4SLinus Torvalds 
45651da177e4SLinus Torvalds 		/* Segment is terminated when we see gap or when
45661da177e4SLinus Torvalds 		 * we are at the end of all the queue. */
456791521944SDavid S. Miller 		if (!skb ||
45681da177e4SLinus Torvalds 		    after(TCP_SKB_CB(skb)->seq, end) ||
45691da177e4SLinus Torvalds 		    before(TCP_SKB_CB(skb)->end_seq, start)) {
45708728b834SDavid S. Miller 			tcp_collapse(sk, &tp->out_of_order_queue,
45718728b834SDavid S. Miller 				     head, skb, start, end);
45721da177e4SLinus Torvalds 			head = skb;
457391521944SDavid S. Miller 			if (!skb)
45741da177e4SLinus Torvalds 				break;
45751da177e4SLinus Torvalds 			/* Start new segment */
45761da177e4SLinus Torvalds 			start = TCP_SKB_CB(skb)->seq;
45771da177e4SLinus Torvalds 			end = TCP_SKB_CB(skb)->end_seq;
45781da177e4SLinus Torvalds 		} else {
45791da177e4SLinus Torvalds 			if (before(TCP_SKB_CB(skb)->seq, start))
45801da177e4SLinus Torvalds 				start = TCP_SKB_CB(skb)->seq;
45811da177e4SLinus Torvalds 			if (after(TCP_SKB_CB(skb)->end_seq, end))
45821da177e4SLinus Torvalds 				end = TCP_SKB_CB(skb)->end_seq;
45831da177e4SLinus Torvalds 		}
45841da177e4SLinus Torvalds 	}
45851da177e4SLinus Torvalds }
45861da177e4SLinus Torvalds 
4587b000cd37SVitaliy Gusev /*
4588b000cd37SVitaliy Gusev  * Purge the out-of-order queue.
458956f367bbSVitaliy Gusev  * Return true if queue was pruned.
4590b000cd37SVitaliy Gusev  */
4591a2a385d6SEric Dumazet static bool tcp_prune_ofo_queue(struct sock *sk)
4592b000cd37SVitaliy Gusev {
4593b000cd37SVitaliy Gusev 	struct tcp_sock *tp = tcp_sk(sk);
4594a2a385d6SEric Dumazet 	bool res = false;
4595b000cd37SVitaliy Gusev 
4596b000cd37SVitaliy Gusev 	if (!skb_queue_empty(&tp->out_of_order_queue)) {
4597de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_OFOPRUNED);
4598b000cd37SVitaliy Gusev 		__skb_queue_purge(&tp->out_of_order_queue);
4599b000cd37SVitaliy Gusev 
4600b000cd37SVitaliy Gusev 		/* Reset SACK state.  A conforming SACK implementation will
4601b000cd37SVitaliy Gusev 		 * do the same at a timeout based retransmit.  When a connection
4602b000cd37SVitaliy Gusev 		 * is in a sad state like this, we care only about integrity
4603b000cd37SVitaliy Gusev 		 * of the connection not performance.
4604b000cd37SVitaliy Gusev 		 */
4605b000cd37SVitaliy Gusev 		if (tp->rx_opt.sack_ok)
4606b000cd37SVitaliy Gusev 			tcp_sack_reset(&tp->rx_opt);
4607b000cd37SVitaliy Gusev 		sk_mem_reclaim(sk);
4608a2a385d6SEric Dumazet 		res = true;
4609b000cd37SVitaliy Gusev 	}
461056f367bbSVitaliy Gusev 	return res;
4611b000cd37SVitaliy Gusev }
4612b000cd37SVitaliy Gusev 
46131da177e4SLinus Torvalds /* Reduce allocated memory if we can, trying to get
46141da177e4SLinus Torvalds  * the socket within its memory limits again.
46151da177e4SLinus Torvalds  *
46161da177e4SLinus Torvalds  * Return less than zero if we should start dropping frames
46171da177e4SLinus Torvalds  * until the socket owning process reads some of the data
46181da177e4SLinus Torvalds  * to stabilize the situation.
46191da177e4SLinus Torvalds  */
46201da177e4SLinus Torvalds static int tcp_prune_queue(struct sock *sk)
46211da177e4SLinus Torvalds {
46221da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
46231da177e4SLinus Torvalds 
46241da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq);
46251da177e4SLinus Torvalds 
4626de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PRUNECALLED);
46271da177e4SLinus Torvalds 
46281da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
46299e412ba7SIlpo Järvinen 		tcp_clamp_window(sk);
4630180d8cd9SGlauber Costa 	else if (sk_under_memory_pressure(sk))
46311da177e4SLinus Torvalds 		tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
46321da177e4SLinus Torvalds 
46331da177e4SLinus Torvalds 	tcp_collapse_ofo_queue(sk);
463491521944SDavid S. Miller 	if (!skb_queue_empty(&sk->sk_receive_queue))
46358728b834SDavid S. Miller 		tcp_collapse(sk, &sk->sk_receive_queue,
463691521944SDavid S. Miller 			     skb_peek(&sk->sk_receive_queue),
463791521944SDavid S. Miller 			     NULL,
46381da177e4SLinus Torvalds 			     tp->copied_seq, tp->rcv_nxt);
46393ab224beSHideo Aoki 	sk_mem_reclaim(sk);
46401da177e4SLinus Torvalds 
46411da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
46421da177e4SLinus Torvalds 		return 0;
46431da177e4SLinus Torvalds 
46441da177e4SLinus Torvalds 	/* Collapsing did not help, destructive actions follow.
46451da177e4SLinus Torvalds 	 * This must not ever occur. */
46461da177e4SLinus Torvalds 
4647b000cd37SVitaliy Gusev 	tcp_prune_ofo_queue(sk);
46481da177e4SLinus Torvalds 
46491da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
46501da177e4SLinus Torvalds 		return 0;
46511da177e4SLinus Torvalds 
46521da177e4SLinus Torvalds 	/* If we are really being abused, tell the caller to silently
46531da177e4SLinus Torvalds 	 * drop receive data on the floor.  It will get retransmitted
46541da177e4SLinus Torvalds 	 * and hopefully then we'll have sufficient space.
46551da177e4SLinus Torvalds 	 */
4656de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_RCVPRUNED);
46571da177e4SLinus Torvalds 
46581da177e4SLinus Torvalds 	/* Massive buffer overcommit. */
46591da177e4SLinus Torvalds 	tp->pred_flags = 0;
46601da177e4SLinus Torvalds 	return -1;
46611da177e4SLinus Torvalds }
46621da177e4SLinus Torvalds 
46631da177e4SLinus Torvalds /* RFC2861, slow part. Adjust cwnd, after it was not full during one rto.
46641da177e4SLinus Torvalds  * As additional protections, we do not touch cwnd in retransmission phases,
46651da177e4SLinus Torvalds  * and if application hit its sndbuf limit recently.
46661da177e4SLinus Torvalds  */
46671da177e4SLinus Torvalds void tcp_cwnd_application_limited(struct sock *sk)
46681da177e4SLinus Torvalds {
46691da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
46701da177e4SLinus Torvalds 
46716687e988SArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_ca_state == TCP_CA_Open &&
46721da177e4SLinus Torvalds 	    sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
46731da177e4SLinus Torvalds 		/* Limited by application or receiver window. */
4674d254bcdbSIlpo Järvinen 		u32 init_win = tcp_init_cwnd(tp, __sk_dst_get(sk));
4675d254bcdbSIlpo Järvinen 		u32 win_used = max(tp->snd_cwnd_used, init_win);
46761da177e4SLinus Torvalds 		if (win_used < tp->snd_cwnd) {
46776687e988SArnaldo Carvalho de Melo 			tp->snd_ssthresh = tcp_current_ssthresh(sk);
46781da177e4SLinus Torvalds 			tp->snd_cwnd = (tp->snd_cwnd + win_used) >> 1;
46791da177e4SLinus Torvalds 		}
46801da177e4SLinus Torvalds 		tp->snd_cwnd_used = 0;
46811da177e4SLinus Torvalds 	}
46821da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
46831da177e4SLinus Torvalds }
46841da177e4SLinus Torvalds 
4685a2a385d6SEric Dumazet static bool tcp_should_expand_sndbuf(const struct sock *sk)
46860d9901dfSDavid S. Miller {
4687cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
46889e412ba7SIlpo Järvinen 
46890d9901dfSDavid S. Miller 	/* If the user specified a specific send buffer setting, do
46900d9901dfSDavid S. Miller 	 * not modify it.
46910d9901dfSDavid S. Miller 	 */
46920d9901dfSDavid S. Miller 	if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
4693a2a385d6SEric Dumazet 		return false;
46940d9901dfSDavid S. Miller 
46950d9901dfSDavid S. Miller 	/* If we are under global TCP memory pressure, do not expand.  */
4696180d8cd9SGlauber Costa 	if (sk_under_memory_pressure(sk))
4697a2a385d6SEric Dumazet 		return false;
46980d9901dfSDavid S. Miller 
46990d9901dfSDavid S. Miller 	/* If we are under soft global TCP memory pressure, do not expand.  */
4700180d8cd9SGlauber Costa 	if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0))
4701a2a385d6SEric Dumazet 		return false;
47020d9901dfSDavid S. Miller 
47030d9901dfSDavid S. Miller 	/* If we filled the congestion window, do not expand.  */
47040d9901dfSDavid S. Miller 	if (tp->packets_out >= tp->snd_cwnd)
4705a2a385d6SEric Dumazet 		return false;
47060d9901dfSDavid S. Miller 
4707a2a385d6SEric Dumazet 	return true;
47080d9901dfSDavid S. Miller }
47091da177e4SLinus Torvalds 
47101da177e4SLinus Torvalds /* When incoming ACK allowed to free some skb from write_queue,
47111da177e4SLinus Torvalds  * we remember this event in flag SOCK_QUEUE_SHRUNK and wake up socket
47121da177e4SLinus Torvalds  * on the exit from tcp input handler.
47131da177e4SLinus Torvalds  *
47141da177e4SLinus Torvalds  * PROBLEM: sndbuf expansion does not work well with largesend.
47151da177e4SLinus Torvalds  */
47161da177e4SLinus Torvalds static void tcp_new_space(struct sock *sk)
47171da177e4SLinus Torvalds {
47181da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
47191da177e4SLinus Torvalds 
47209e412ba7SIlpo Järvinen 	if (tcp_should_expand_sndbuf(sk)) {
472187fb4b7bSEric Dumazet 		int sndmem = SKB_TRUESIZE(max_t(u32,
472287fb4b7bSEric Dumazet 						tp->rx_opt.mss_clamp,
472387fb4b7bSEric Dumazet 						tp->mss_cache) +
472487fb4b7bSEric Dumazet 					  MAX_TCP_HEADER);
47254a7e5609SIlpo Järvinen 		int demanded = max_t(unsigned int, tp->snd_cwnd,
47261da177e4SLinus Torvalds 				     tp->reordering + 1);
47271da177e4SLinus Torvalds 		sndmem *= 2 * demanded;
47281da177e4SLinus Torvalds 		if (sndmem > sk->sk_sndbuf)
47291da177e4SLinus Torvalds 			sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
47301da177e4SLinus Torvalds 		tp->snd_cwnd_stamp = tcp_time_stamp;
47311da177e4SLinus Torvalds 	}
47321da177e4SLinus Torvalds 
47331da177e4SLinus Torvalds 	sk->sk_write_space(sk);
47341da177e4SLinus Torvalds }
47351da177e4SLinus Torvalds 
473640efc6faSStephen Hemminger static void tcp_check_space(struct sock *sk)
47371da177e4SLinus Torvalds {
47381da177e4SLinus Torvalds 	if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) {
47391da177e4SLinus Torvalds 		sock_reset_flag(sk, SOCK_QUEUE_SHRUNK);
47401da177e4SLinus Torvalds 		if (sk->sk_socket &&
47411da177e4SLinus Torvalds 		    test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
47421da177e4SLinus Torvalds 			tcp_new_space(sk);
47431da177e4SLinus Torvalds 	}
47441da177e4SLinus Torvalds }
47451da177e4SLinus Torvalds 
47469e412ba7SIlpo Järvinen static inline void tcp_data_snd_check(struct sock *sk)
47471da177e4SLinus Torvalds {
47489e412ba7SIlpo Järvinen 	tcp_push_pending_frames(sk);
47491da177e4SLinus Torvalds 	tcp_check_space(sk);
47501da177e4SLinus Torvalds }
47511da177e4SLinus Torvalds 
47521da177e4SLinus Torvalds /*
47531da177e4SLinus Torvalds  * Check if sending an ack is needed.
47541da177e4SLinus Torvalds  */
47551da177e4SLinus Torvalds static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
47561da177e4SLinus Torvalds {
47571da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
47581da177e4SLinus Torvalds 
47591da177e4SLinus Torvalds 	    /* More than one full frame received... */
47609d4fb27dSJoe Perches 	if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
47611da177e4SLinus Torvalds 	     /* ... and right edge of window advances far enough.
47621da177e4SLinus Torvalds 	      * (tcp_recvmsg() will send ACK otherwise). Or...
47631da177e4SLinus Torvalds 	      */
47649d4fb27dSJoe Perches 	     __tcp_select_window(sk) >= tp->rcv_wnd) ||
47651da177e4SLinus Torvalds 	    /* We ACK each frame or... */
4766463c84b9SArnaldo Carvalho de Melo 	    tcp_in_quickack_mode(sk) ||
47671da177e4SLinus Torvalds 	    /* We have out of order data. */
4768056834d9SIlpo Järvinen 	    (ofo_possible && skb_peek(&tp->out_of_order_queue))) {
47691da177e4SLinus Torvalds 		/* Then ack it now */
47701da177e4SLinus Torvalds 		tcp_send_ack(sk);
47711da177e4SLinus Torvalds 	} else {
47721da177e4SLinus Torvalds 		/* Else, send delayed ack. */
47731da177e4SLinus Torvalds 		tcp_send_delayed_ack(sk);
47741da177e4SLinus Torvalds 	}
47751da177e4SLinus Torvalds }
47761da177e4SLinus Torvalds 
477740efc6faSStephen Hemminger static inline void tcp_ack_snd_check(struct sock *sk)
47781da177e4SLinus Torvalds {
4779463c84b9SArnaldo Carvalho de Melo 	if (!inet_csk_ack_scheduled(sk)) {
47801da177e4SLinus Torvalds 		/* We sent a data segment already. */
47811da177e4SLinus Torvalds 		return;
47821da177e4SLinus Torvalds 	}
47831da177e4SLinus Torvalds 	__tcp_ack_snd_check(sk, 1);
47841da177e4SLinus Torvalds }
47851da177e4SLinus Torvalds 
47861da177e4SLinus Torvalds /*
47871da177e4SLinus Torvalds  *	This routine is only called when we have urgent data
4788caa20d9aSStephen Hemminger  *	signaled. Its the 'slow' part of tcp_urg. It could be
47891da177e4SLinus Torvalds  *	moved inline now as tcp_urg is only called from one
47901da177e4SLinus Torvalds  *	place. We handle URGent data wrong. We have to - as
47911da177e4SLinus Torvalds  *	BSD still doesn't use the correction from RFC961.
47921da177e4SLinus Torvalds  *	For 1003.1g we should support a new option TCP_STDURG to permit
47931da177e4SLinus Torvalds  *	either form (or just set the sysctl tcp_stdurg).
47941da177e4SLinus Torvalds  */
47951da177e4SLinus Torvalds 
4796cf533ea5SEric Dumazet static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
47971da177e4SLinus Torvalds {
47981da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
47991da177e4SLinus Torvalds 	u32 ptr = ntohs(th->urg_ptr);
48001da177e4SLinus Torvalds 
48011da177e4SLinus Torvalds 	if (ptr && !sysctl_tcp_stdurg)
48021da177e4SLinus Torvalds 		ptr--;
48031da177e4SLinus Torvalds 	ptr += ntohl(th->seq);
48041da177e4SLinus Torvalds 
48051da177e4SLinus Torvalds 	/* Ignore urgent data that we've already seen and read. */
48061da177e4SLinus Torvalds 	if (after(tp->copied_seq, ptr))
48071da177e4SLinus Torvalds 		return;
48081da177e4SLinus Torvalds 
48091da177e4SLinus Torvalds 	/* Do not replay urg ptr.
48101da177e4SLinus Torvalds 	 *
48111da177e4SLinus Torvalds 	 * NOTE: interesting situation not covered by specs.
48121da177e4SLinus Torvalds 	 * Misbehaving sender may send urg ptr, pointing to segment,
48131da177e4SLinus Torvalds 	 * which we already have in ofo queue. We are not able to fetch
48141da177e4SLinus Torvalds 	 * such data and will stay in TCP_URG_NOTYET until will be eaten
48151da177e4SLinus Torvalds 	 * by recvmsg(). Seems, we are not obliged to handle such wicked
48161da177e4SLinus Torvalds 	 * situations. But it is worth to think about possibility of some
48171da177e4SLinus Torvalds 	 * DoSes using some hypothetical application level deadlock.
48181da177e4SLinus Torvalds 	 */
48191da177e4SLinus Torvalds 	if (before(ptr, tp->rcv_nxt))
48201da177e4SLinus Torvalds 		return;
48211da177e4SLinus Torvalds 
48221da177e4SLinus Torvalds 	/* Do we already have a newer (or duplicate) urgent pointer? */
48231da177e4SLinus Torvalds 	if (tp->urg_data && !after(ptr, tp->urg_seq))
48241da177e4SLinus Torvalds 		return;
48251da177e4SLinus Torvalds 
48261da177e4SLinus Torvalds 	/* Tell the world about our new urgent pointer. */
48271da177e4SLinus Torvalds 	sk_send_sigurg(sk);
48281da177e4SLinus Torvalds 
48291da177e4SLinus Torvalds 	/* We may be adding urgent data when the last byte read was
48301da177e4SLinus Torvalds 	 * urgent. To do this requires some care. We cannot just ignore
48311da177e4SLinus Torvalds 	 * tp->copied_seq since we would read the last urgent byte again
48321da177e4SLinus Torvalds 	 * as data, nor can we alter copied_seq until this data arrives
4833caa20d9aSStephen Hemminger 	 * or we break the semantics of SIOCATMARK (and thus sockatmark())
48341da177e4SLinus Torvalds 	 *
48351da177e4SLinus Torvalds 	 * NOTE. Double Dutch. Rendering to plain English: author of comment
48361da177e4SLinus Torvalds 	 * above did something sort of 	send("A", MSG_OOB); send("B", MSG_OOB);
48371da177e4SLinus Torvalds 	 * and expect that both A and B disappear from stream. This is _wrong_.
48381da177e4SLinus Torvalds 	 * Though this happens in BSD with high probability, this is occasional.
48391da177e4SLinus Torvalds 	 * Any application relying on this is buggy. Note also, that fix "works"
48401da177e4SLinus Torvalds 	 * only in this artificial test. Insert some normal data between A and B and we will
48411da177e4SLinus Torvalds 	 * decline of BSD again. Verdict: it is better to remove to trap
48421da177e4SLinus Torvalds 	 * buggy users.
48431da177e4SLinus Torvalds 	 */
48441da177e4SLinus Torvalds 	if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
4845056834d9SIlpo Järvinen 	    !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
48461da177e4SLinus Torvalds 		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
48471da177e4SLinus Torvalds 		tp->copied_seq++;
48481da177e4SLinus Torvalds 		if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
48498728b834SDavid S. Miller 			__skb_unlink(skb, &sk->sk_receive_queue);
48501da177e4SLinus Torvalds 			__kfree_skb(skb);
48511da177e4SLinus Torvalds 		}
48521da177e4SLinus Torvalds 	}
48531da177e4SLinus Torvalds 
48541da177e4SLinus Torvalds 	tp->urg_data = TCP_URG_NOTYET;
48551da177e4SLinus Torvalds 	tp->urg_seq = ptr;
48561da177e4SLinus Torvalds 
48571da177e4SLinus Torvalds 	/* Disable header prediction. */
48581da177e4SLinus Torvalds 	tp->pred_flags = 0;
48591da177e4SLinus Torvalds }
48601da177e4SLinus Torvalds 
48611da177e4SLinus Torvalds /* This is the 'fast' part of urgent handling. */
4862cf533ea5SEric Dumazet static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th)
48631da177e4SLinus Torvalds {
48641da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
48651da177e4SLinus Torvalds 
48661da177e4SLinus Torvalds 	/* Check if we get a new urgent pointer - normally not. */
48671da177e4SLinus Torvalds 	if (th->urg)
48681da177e4SLinus Torvalds 		tcp_check_urg(sk, th);
48691da177e4SLinus Torvalds 
48701da177e4SLinus Torvalds 	/* Do we wait for any urgent data? - normally not... */
48711da177e4SLinus Torvalds 	if (tp->urg_data == TCP_URG_NOTYET) {
48721da177e4SLinus Torvalds 		u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) -
48731da177e4SLinus Torvalds 			  th->syn;
48741da177e4SLinus Torvalds 
48751da177e4SLinus Torvalds 		/* Is the urgent pointer pointing into this packet? */
48761da177e4SLinus Torvalds 		if (ptr < skb->len) {
48771da177e4SLinus Torvalds 			u8 tmp;
48781da177e4SLinus Torvalds 			if (skb_copy_bits(skb, ptr, &tmp, 1))
48791da177e4SLinus Torvalds 				BUG();
48801da177e4SLinus Torvalds 			tp->urg_data = TCP_URG_VALID | tmp;
48811da177e4SLinus Torvalds 			if (!sock_flag(sk, SOCK_DEAD))
48821da177e4SLinus Torvalds 				sk->sk_data_ready(sk, 0);
48831da177e4SLinus Torvalds 		}
48841da177e4SLinus Torvalds 	}
48851da177e4SLinus Torvalds }
48861da177e4SLinus Torvalds 
48871da177e4SLinus Torvalds static int tcp_copy_to_iovec(struct sock *sk, struct sk_buff *skb, int hlen)
48881da177e4SLinus Torvalds {
48891da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
48901da177e4SLinus Torvalds 	int chunk = skb->len - hlen;
48911da177e4SLinus Torvalds 	int err;
48921da177e4SLinus Torvalds 
48931da177e4SLinus Torvalds 	local_bh_enable();
489460476372SHerbert Xu 	if (skb_csum_unnecessary(skb))
48951da177e4SLinus Torvalds 		err = skb_copy_datagram_iovec(skb, hlen, tp->ucopy.iov, chunk);
48961da177e4SLinus Torvalds 	else
48971da177e4SLinus Torvalds 		err = skb_copy_and_csum_datagram_iovec(skb, hlen,
48981da177e4SLinus Torvalds 						       tp->ucopy.iov);
48991da177e4SLinus Torvalds 
49001da177e4SLinus Torvalds 	if (!err) {
49011da177e4SLinus Torvalds 		tp->ucopy.len -= chunk;
49021da177e4SLinus Torvalds 		tp->copied_seq += chunk;
49031da177e4SLinus Torvalds 		tcp_rcv_space_adjust(sk);
49041da177e4SLinus Torvalds 	}
49051da177e4SLinus Torvalds 
49061da177e4SLinus Torvalds 	local_bh_disable();
49071da177e4SLinus Torvalds 	return err;
49081da177e4SLinus Torvalds }
49091da177e4SLinus Torvalds 
4910056834d9SIlpo Järvinen static __sum16 __tcp_checksum_complete_user(struct sock *sk,
4911056834d9SIlpo Järvinen 					    struct sk_buff *skb)
49121da177e4SLinus Torvalds {
4913b51655b9SAl Viro 	__sum16 result;
49141da177e4SLinus Torvalds 
49151da177e4SLinus Torvalds 	if (sock_owned_by_user(sk)) {
49161da177e4SLinus Torvalds 		local_bh_enable();
49171da177e4SLinus Torvalds 		result = __tcp_checksum_complete(skb);
49181da177e4SLinus Torvalds 		local_bh_disable();
49191da177e4SLinus Torvalds 	} else {
49201da177e4SLinus Torvalds 		result = __tcp_checksum_complete(skb);
49211da177e4SLinus Torvalds 	}
49221da177e4SLinus Torvalds 	return result;
49231da177e4SLinus Torvalds }
49241da177e4SLinus Torvalds 
492567b95bd7SVijay Subramanian static inline bool tcp_checksum_complete_user(struct sock *sk,
4926056834d9SIlpo Järvinen 					     struct sk_buff *skb)
49271da177e4SLinus Torvalds {
492860476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
49291da177e4SLinus Torvalds 	       __tcp_checksum_complete_user(sk, skb);
49301da177e4SLinus Torvalds }
49311da177e4SLinus Torvalds 
49321a2449a8SChris Leech #ifdef CONFIG_NET_DMA
4933a2a385d6SEric Dumazet static bool tcp_dma_try_early_copy(struct sock *sk, struct sk_buff *skb,
4934056834d9SIlpo Järvinen 				  int hlen)
49351a2449a8SChris Leech {
49361a2449a8SChris Leech 	struct tcp_sock *tp = tcp_sk(sk);
49371a2449a8SChris Leech 	int chunk = skb->len - hlen;
49381a2449a8SChris Leech 	int dma_cookie;
4939a2a385d6SEric Dumazet 	bool copied_early = false;
49401a2449a8SChris Leech 
49411a2449a8SChris Leech 	if (tp->ucopy.wakeup)
4942a2a385d6SEric Dumazet 		return false;
49431a2449a8SChris Leech 
49441a2449a8SChris Leech 	if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
4945a2bd1140SDave Jiang 		tp->ucopy.dma_chan = net_dma_find_channel();
49461a2449a8SChris Leech 
494760476372SHerbert Xu 	if (tp->ucopy.dma_chan && skb_csum_unnecessary(skb)) {
49481a2449a8SChris Leech 
49491a2449a8SChris Leech 		dma_cookie = dma_skb_copy_datagram_iovec(tp->ucopy.dma_chan,
4950056834d9SIlpo Järvinen 							 skb, hlen,
4951056834d9SIlpo Järvinen 							 tp->ucopy.iov, chunk,
4952056834d9SIlpo Järvinen 							 tp->ucopy.pinned_list);
49531a2449a8SChris Leech 
49541a2449a8SChris Leech 		if (dma_cookie < 0)
49551a2449a8SChris Leech 			goto out;
49561a2449a8SChris Leech 
49571a2449a8SChris Leech 		tp->ucopy.dma_cookie = dma_cookie;
4958a2a385d6SEric Dumazet 		copied_early = true;
49591a2449a8SChris Leech 
49601a2449a8SChris Leech 		tp->ucopy.len -= chunk;
49611a2449a8SChris Leech 		tp->copied_seq += chunk;
49621a2449a8SChris Leech 		tcp_rcv_space_adjust(sk);
49631a2449a8SChris Leech 
49641a2449a8SChris Leech 		if ((tp->ucopy.len == 0) ||
4965aa8223c7SArnaldo Carvalho de Melo 		    (tcp_flag_word(tcp_hdr(skb)) & TCP_FLAG_PSH) ||
49661a2449a8SChris Leech 		    (atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1))) {
49671a2449a8SChris Leech 			tp->ucopy.wakeup = 1;
49681a2449a8SChris Leech 			sk->sk_data_ready(sk, 0);
49691a2449a8SChris Leech 		}
49701a2449a8SChris Leech 	} else if (chunk > 0) {
49711a2449a8SChris Leech 		tp->ucopy.wakeup = 1;
49721a2449a8SChris Leech 		sk->sk_data_ready(sk, 0);
49731a2449a8SChris Leech 	}
49741a2449a8SChris Leech out:
49751a2449a8SChris Leech 	return copied_early;
49761a2449a8SChris Leech }
49771a2449a8SChris Leech #endif /* CONFIG_NET_DMA */
49781a2449a8SChris Leech 
4979cbe2d128SIlpo Järvinen /* Does PAWS and seqno based validation of an incoming segment, flags will
4980cbe2d128SIlpo Järvinen  * play significant role here.
4981cbe2d128SIlpo Järvinen  */
49820c24604bSEric Dumazet static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
4983cf533ea5SEric Dumazet 				  const struct tcphdr *th, int syn_inerr)
4984cbe2d128SIlpo Järvinen {
4985cbe2d128SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
4986cbe2d128SIlpo Järvinen 
4987cbe2d128SIlpo Järvinen 	/* RFC1323: H1. Apply PAWS check first. */
49881a2c6181SChristoph Paasch 	if (tcp_fast_parse_options(skb, th, tp) && tp->rx_opt.saw_tstamp &&
4989cbe2d128SIlpo Järvinen 	    tcp_paws_discard(sk, skb)) {
4990cbe2d128SIlpo Järvinen 		if (!th->rst) {
4991cbe2d128SIlpo Järvinen 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
4992cbe2d128SIlpo Järvinen 			tcp_send_dupack(sk, skb);
4993cbe2d128SIlpo Järvinen 			goto discard;
4994cbe2d128SIlpo Järvinen 		}
4995cbe2d128SIlpo Järvinen 		/* Reset is accepted even if it did not pass PAWS. */
4996cbe2d128SIlpo Järvinen 	}
4997cbe2d128SIlpo Järvinen 
4998cbe2d128SIlpo Järvinen 	/* Step 1: check sequence number */
4999cbe2d128SIlpo Järvinen 	if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {
5000cbe2d128SIlpo Järvinen 		/* RFC793, page 37: "In all states except SYN-SENT, all reset
5001cbe2d128SIlpo Järvinen 		 * (RST) segments are validated by checking their SEQ-fields."
5002cbe2d128SIlpo Järvinen 		 * And page 69: "If an incoming segment is not acceptable,
5003cbe2d128SIlpo Järvinen 		 * an acknowledgment should be sent in reply (unless the RST
5004cbe2d128SIlpo Järvinen 		 * bit is set, if so drop the segment and return)".
5005cbe2d128SIlpo Järvinen 		 */
5006e3715899SEric Dumazet 		if (!th->rst) {
5007e3715899SEric Dumazet 			if (th->syn)
5008e3715899SEric Dumazet 				goto syn_challenge;
5009cbe2d128SIlpo Järvinen 			tcp_send_dupack(sk, skb);
5010e3715899SEric Dumazet 		}
5011cbe2d128SIlpo Järvinen 		goto discard;
5012cbe2d128SIlpo Järvinen 	}
5013cbe2d128SIlpo Järvinen 
5014cbe2d128SIlpo Järvinen 	/* Step 2: check RST bit */
5015cbe2d128SIlpo Järvinen 	if (th->rst) {
5016282f23c6SEric Dumazet 		/* RFC 5961 3.2 :
5017282f23c6SEric Dumazet 		 * If sequence number exactly matches RCV.NXT, then
5018282f23c6SEric Dumazet 		 *     RESET the connection
5019282f23c6SEric Dumazet 		 * else
5020282f23c6SEric Dumazet 		 *     Send a challenge ACK
5021282f23c6SEric Dumazet 		 */
5022282f23c6SEric Dumazet 		if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt)
5023cbe2d128SIlpo Järvinen 			tcp_reset(sk);
5024282f23c6SEric Dumazet 		else
5025282f23c6SEric Dumazet 			tcp_send_challenge_ack(sk);
5026cbe2d128SIlpo Järvinen 		goto discard;
5027cbe2d128SIlpo Järvinen 	}
5028cbe2d128SIlpo Järvinen 
5029cbe2d128SIlpo Järvinen 	/* step 3: check security and precedence [ignored] */
5030cbe2d128SIlpo Järvinen 
50310c24604bSEric Dumazet 	/* step 4: Check for a SYN
50320c24604bSEric Dumazet 	 * RFC 5691 4.2 : Send a challenge ack
50330c24604bSEric Dumazet 	 */
50340c24604bSEric Dumazet 	if (th->syn) {
5035e3715899SEric Dumazet syn_challenge:
5036cbe2d128SIlpo Järvinen 		if (syn_inerr)
5037cbe2d128SIlpo Järvinen 			TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
50380c24604bSEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE);
50390c24604bSEric Dumazet 		tcp_send_challenge_ack(sk);
50400c24604bSEric Dumazet 		goto discard;
5041cbe2d128SIlpo Järvinen 	}
5042cbe2d128SIlpo Järvinen 
50430c24604bSEric Dumazet 	return true;
5044cbe2d128SIlpo Järvinen 
5045cbe2d128SIlpo Järvinen discard:
5046cbe2d128SIlpo Järvinen 	__kfree_skb(skb);
50470c24604bSEric Dumazet 	return false;
5048cbe2d128SIlpo Järvinen }
5049cbe2d128SIlpo Järvinen 
50501da177e4SLinus Torvalds /*
50511da177e4SLinus Torvalds  *	TCP receive function for the ESTABLISHED state.
50521da177e4SLinus Torvalds  *
50531da177e4SLinus Torvalds  *	It is split into a fast path and a slow path. The fast path is
50541da177e4SLinus Torvalds  * 	disabled when:
50551da177e4SLinus Torvalds  *	- A zero window was announced from us - zero window probing
50561da177e4SLinus Torvalds  *        is only handled properly in the slow path.
50571da177e4SLinus Torvalds  *	- Out of order segments arrived.
50581da177e4SLinus Torvalds  *	- Urgent data is expected.
50591da177e4SLinus Torvalds  *	- There is no buffer space left
50601da177e4SLinus Torvalds  *	- Unexpected TCP flags/window values/header lengths are received
50611da177e4SLinus Torvalds  *	  (detected by checking the TCP header against pred_flags)
50621da177e4SLinus Torvalds  *	- Data is sent in both directions. Fast path only supports pure senders
50631da177e4SLinus Torvalds  *	  or pure receivers (this means either the sequence number or the ack
50641da177e4SLinus Torvalds  *	  value must stay constant)
50651da177e4SLinus Torvalds  *	- Unexpected TCP option.
50661da177e4SLinus Torvalds  *
50671da177e4SLinus Torvalds  *	When these conditions are not satisfied it drops into a standard
50681da177e4SLinus Torvalds  *	receive procedure patterned after RFC793 to handle all cases.
50691da177e4SLinus Torvalds  *	The first three cases are guaranteed by proper pred_flags setting,
50701da177e4SLinus Torvalds  *	the rest is checked inline. Fast processing is turned on in
50711da177e4SLinus Torvalds  *	tcp_data_queue when everything is OK.
50721da177e4SLinus Torvalds  */
50731da177e4SLinus Torvalds int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
5074cf533ea5SEric Dumazet 			const struct tcphdr *th, unsigned int len)
50751da177e4SLinus Torvalds {
50761da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
50771da177e4SLinus Torvalds 
50785d299f3dSEric Dumazet 	if (unlikely(sk->sk_rx_dst == NULL))
50795d299f3dSEric Dumazet 		inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
50801da177e4SLinus Torvalds 	/*
50811da177e4SLinus Torvalds 	 *	Header prediction.
50821da177e4SLinus Torvalds 	 *	The code loosely follows the one in the famous
50831da177e4SLinus Torvalds 	 *	"30 instruction TCP receive" Van Jacobson mail.
50841da177e4SLinus Torvalds 	 *
50851da177e4SLinus Torvalds 	 *	Van's trick is to deposit buffers into socket queue
50861da177e4SLinus Torvalds 	 *	on a device interrupt, to call tcp_recv function
50871da177e4SLinus Torvalds 	 *	on the receive process context and checksum and copy
50881da177e4SLinus Torvalds 	 *	the buffer to user space. smart...
50891da177e4SLinus Torvalds 	 *
50901da177e4SLinus Torvalds 	 *	Our current scheme is not silly either but we take the
50911da177e4SLinus Torvalds 	 *	extra cost of the net_bh soft interrupt processing...
50921da177e4SLinus Torvalds 	 *	We do checksum and copy also but from device to kernel.
50931da177e4SLinus Torvalds 	 */
50941da177e4SLinus Torvalds 
50951da177e4SLinus Torvalds 	tp->rx_opt.saw_tstamp = 0;
50961da177e4SLinus Torvalds 
50971da177e4SLinus Torvalds 	/*	pred_flags is 0xS?10 << 16 + snd_wnd
5098caa20d9aSStephen Hemminger 	 *	if header_prediction is to be made
50991da177e4SLinus Torvalds 	 *	'S' will always be tp->tcp_header_len >> 2
51001da177e4SLinus Torvalds 	 *	'?' will be 0 for the fast path, otherwise pred_flags is 0 to
51011da177e4SLinus Torvalds 	 *  turn it off	(when there are holes in the receive
51021da177e4SLinus Torvalds 	 *	 space for instance)
51031da177e4SLinus Torvalds 	 *	PSH flag is ignored.
51041da177e4SLinus Torvalds 	 */
51051da177e4SLinus Torvalds 
51061da177e4SLinus Torvalds 	if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
510796e0bf4bSJohn Dykstra 	    TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&
510896e0bf4bSJohn Dykstra 	    !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
51091da177e4SLinus Torvalds 		int tcp_header_len = tp->tcp_header_len;
51101da177e4SLinus Torvalds 
51111da177e4SLinus Torvalds 		/* Timestamp header prediction: tcp_header_len
51121da177e4SLinus Torvalds 		 * is automatically equal to th->doff*4 due to pred_flags
51131da177e4SLinus Torvalds 		 * match.
51141da177e4SLinus Torvalds 		 */
51151da177e4SLinus Torvalds 
51161da177e4SLinus Torvalds 		/* Check timestamp */
51171da177e4SLinus Torvalds 		if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
51181da177e4SLinus Torvalds 			/* No? Slow path! */
5119a4356b29SIlpo Järvinen 			if (!tcp_parse_aligned_timestamp(tp, th))
51201da177e4SLinus Torvalds 				goto slow_path;
51211da177e4SLinus Torvalds 
51221da177e4SLinus Torvalds 			/* If PAWS failed, check it more carefully in slow path */
51231da177e4SLinus Torvalds 			if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0)
51241da177e4SLinus Torvalds 				goto slow_path;
51251da177e4SLinus Torvalds 
51261da177e4SLinus Torvalds 			/* DO NOT update ts_recent here, if checksum fails
51271da177e4SLinus Torvalds 			 * and timestamp was corrupted part, it will result
51281da177e4SLinus Torvalds 			 * in a hung connection since we will drop all
51291da177e4SLinus Torvalds 			 * future packets due to the PAWS test.
51301da177e4SLinus Torvalds 			 */
51311da177e4SLinus Torvalds 		}
51321da177e4SLinus Torvalds 
51331da177e4SLinus Torvalds 		if (len <= tcp_header_len) {
51341da177e4SLinus Torvalds 			/* Bulk data transfer: sender */
51351da177e4SLinus Torvalds 			if (len == tcp_header_len) {
51361da177e4SLinus Torvalds 				/* Predicted packet is in window by definition.
51371da177e4SLinus Torvalds 				 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
51381da177e4SLinus Torvalds 				 * Hence, check seq<=rcv_wup reduces to:
51391da177e4SLinus Torvalds 				 */
51401da177e4SLinus Torvalds 				if (tcp_header_len ==
51411da177e4SLinus Torvalds 				    (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
51421da177e4SLinus Torvalds 				    tp->rcv_nxt == tp->rcv_wup)
51431da177e4SLinus Torvalds 					tcp_store_ts_recent(tp);
51441da177e4SLinus Torvalds 
51451da177e4SLinus Torvalds 				/* We know that such packets are checksummed
51461da177e4SLinus Torvalds 				 * on entry.
51471da177e4SLinus Torvalds 				 */
51481da177e4SLinus Torvalds 				tcp_ack(sk, skb, 0);
51491da177e4SLinus Torvalds 				__kfree_skb(skb);
51509e412ba7SIlpo Järvinen 				tcp_data_snd_check(sk);
51511da177e4SLinus Torvalds 				return 0;
51521da177e4SLinus Torvalds 			} else { /* Header too small */
515363231bddSPavel Emelyanov 				TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
51541da177e4SLinus Torvalds 				goto discard;
51551da177e4SLinus Torvalds 			}
51561da177e4SLinus Torvalds 		} else {
51571da177e4SLinus Torvalds 			int eaten = 0;
51581a2449a8SChris Leech 			int copied_early = 0;
5159b081f85cSEric Dumazet 			bool fragstolen = false;
51601da177e4SLinus Torvalds 
51611a2449a8SChris Leech 			if (tp->copied_seq == tp->rcv_nxt &&
51621a2449a8SChris Leech 			    len - tcp_header_len <= tp->ucopy.len) {
51631a2449a8SChris Leech #ifdef CONFIG_NET_DMA
516459ea33a6SJiri Kosina 				if (tp->ucopy.task == current &&
516559ea33a6SJiri Kosina 				    sock_owned_by_user(sk) &&
516659ea33a6SJiri Kosina 				    tcp_dma_try_early_copy(sk, skb, tcp_header_len)) {
51671a2449a8SChris Leech 					copied_early = 1;
51681a2449a8SChris Leech 					eaten = 1;
51691a2449a8SChris Leech 				}
51701a2449a8SChris Leech #endif
5171056834d9SIlpo Järvinen 				if (tp->ucopy.task == current &&
5172056834d9SIlpo Järvinen 				    sock_owned_by_user(sk) && !copied_early) {
51731da177e4SLinus Torvalds 					__set_current_state(TASK_RUNNING);
51741da177e4SLinus Torvalds 
51751a2449a8SChris Leech 					if (!tcp_copy_to_iovec(sk, skb, tcp_header_len))
51761a2449a8SChris Leech 						eaten = 1;
51771a2449a8SChris Leech 				}
51781a2449a8SChris Leech 				if (eaten) {
51791da177e4SLinus Torvalds 					/* Predicted packet is in window by definition.
51801da177e4SLinus Torvalds 					 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
51811da177e4SLinus Torvalds 					 * Hence, check seq<=rcv_wup reduces to:
51821da177e4SLinus Torvalds 					 */
51831da177e4SLinus Torvalds 					if (tcp_header_len ==
51841da177e4SLinus Torvalds 					    (sizeof(struct tcphdr) +
51851da177e4SLinus Torvalds 					     TCPOLEN_TSTAMP_ALIGNED) &&
51861da177e4SLinus Torvalds 					    tp->rcv_nxt == tp->rcv_wup)
51871da177e4SLinus Torvalds 						tcp_store_ts_recent(tp);
51881da177e4SLinus Torvalds 
5189463c84b9SArnaldo Carvalho de Melo 					tcp_rcv_rtt_measure_ts(sk, skb);
51901da177e4SLinus Torvalds 
51911da177e4SLinus Torvalds 					__skb_pull(skb, tcp_header_len);
51921da177e4SLinus Torvalds 					tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
5193de0744afSPavel Emelyanov 					NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITSTOUSER);
51941da177e4SLinus Torvalds 				}
51951a2449a8SChris Leech 				if (copied_early)
51961a2449a8SChris Leech 					tcp_cleanup_rbuf(sk, skb->len);
51971da177e4SLinus Torvalds 			}
51981da177e4SLinus Torvalds 			if (!eaten) {
51991da177e4SLinus Torvalds 				if (tcp_checksum_complete_user(sk, skb))
52001da177e4SLinus Torvalds 					goto csum_error;
52011da177e4SLinus Torvalds 
5202aab2b4bfSNeal Cardwell 				if ((int)skb->truesize > sk->sk_forward_alloc)
5203aab2b4bfSNeal Cardwell 					goto step5;
5204aab2b4bfSNeal Cardwell 
52051da177e4SLinus Torvalds 				/* Predicted packet is in window by definition.
52061da177e4SLinus Torvalds 				 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
52071da177e4SLinus Torvalds 				 * Hence, check seq<=rcv_wup reduces to:
52081da177e4SLinus Torvalds 				 */
52091da177e4SLinus Torvalds 				if (tcp_header_len ==
52101da177e4SLinus Torvalds 				    (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
52111da177e4SLinus Torvalds 				    tp->rcv_nxt == tp->rcv_wup)
52121da177e4SLinus Torvalds 					tcp_store_ts_recent(tp);
52131da177e4SLinus Torvalds 
5214463c84b9SArnaldo Carvalho de Melo 				tcp_rcv_rtt_measure_ts(sk, skb);
52151da177e4SLinus Torvalds 
5216de0744afSPavel Emelyanov 				NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITS);
52171da177e4SLinus Torvalds 
52181da177e4SLinus Torvalds 				/* Bulk data transfer: receiver */
5219b081f85cSEric Dumazet 				eaten = tcp_queue_rcv(sk, skb, tcp_header_len,
5220b081f85cSEric Dumazet 						      &fragstolen);
52211da177e4SLinus Torvalds 			}
52221da177e4SLinus Torvalds 
52239e412ba7SIlpo Järvinen 			tcp_event_data_recv(sk, skb);
52241da177e4SLinus Torvalds 
52251da177e4SLinus Torvalds 			if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
52261da177e4SLinus Torvalds 				/* Well, only one small jumplet in fast path... */
52271da177e4SLinus Torvalds 				tcp_ack(sk, skb, FLAG_DATA);
52289e412ba7SIlpo Järvinen 				tcp_data_snd_check(sk);
5229463c84b9SArnaldo Carvalho de Melo 				if (!inet_csk_ack_scheduled(sk))
52301da177e4SLinus Torvalds 					goto no_ack;
52311da177e4SLinus Torvalds 			}
52321da177e4SLinus Torvalds 
523353240c20SAli Saidi 			if (!copied_early || tp->rcv_nxt != tp->rcv_wup)
52341da177e4SLinus Torvalds 				__tcp_ack_snd_check(sk, 0);
52351da177e4SLinus Torvalds no_ack:
52361a2449a8SChris Leech #ifdef CONFIG_NET_DMA
52371a2449a8SChris Leech 			if (copied_early)
52381a2449a8SChris Leech 				__skb_queue_tail(&sk->sk_async_wait_queue, skb);
52391a2449a8SChris Leech 			else
52401a2449a8SChris Leech #endif
52411da177e4SLinus Torvalds 			if (eaten)
5242b081f85cSEric Dumazet 				kfree_skb_partial(skb, fragstolen);
52431da177e4SLinus Torvalds 			sk->sk_data_ready(sk, 0);
52441da177e4SLinus Torvalds 			return 0;
52451da177e4SLinus Torvalds 		}
52461da177e4SLinus Torvalds 	}
52471da177e4SLinus Torvalds 
52481da177e4SLinus Torvalds slow_path:
52491da177e4SLinus Torvalds 	if (len < (th->doff << 2) || tcp_checksum_complete_user(sk, skb))
52501da177e4SLinus Torvalds 		goto csum_error;
52511da177e4SLinus Torvalds 
52527b514a88SEric Dumazet 	if (!th->ack && !th->rst)
5253c3ae62afSEric Dumazet 		goto discard;
5254c3ae62afSEric Dumazet 
52551da177e4SLinus Torvalds 	/*
52561da177e4SLinus Torvalds 	 *	Standard slow path.
52571da177e4SLinus Torvalds 	 */
52581da177e4SLinus Torvalds 
52590c24604bSEric Dumazet 	if (!tcp_validate_incoming(sk, skb, th, 1))
52600c24604bSEric Dumazet 		return 0;
52611da177e4SLinus Torvalds 
52621da177e4SLinus Torvalds step5:
526312fb3dd9SEric Dumazet 	if (tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0)
526496e0bf4bSJohn Dykstra 		goto discard;
52651da177e4SLinus Torvalds 
5266463c84b9SArnaldo Carvalho de Melo 	tcp_rcv_rtt_measure_ts(sk, skb);
52671da177e4SLinus Torvalds 
52681da177e4SLinus Torvalds 	/* Process urgent data. */
52691da177e4SLinus Torvalds 	tcp_urg(sk, skb, th);
52701da177e4SLinus Torvalds 
52711da177e4SLinus Torvalds 	/* step 7: process the segment text */
52721da177e4SLinus Torvalds 	tcp_data_queue(sk, skb);
52731da177e4SLinus Torvalds 
52749e412ba7SIlpo Järvinen 	tcp_data_snd_check(sk);
52751da177e4SLinus Torvalds 	tcp_ack_snd_check(sk);
52761da177e4SLinus Torvalds 	return 0;
52771da177e4SLinus Torvalds 
52781da177e4SLinus Torvalds csum_error:
52796a5dc9e5SEric Dumazet 	TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_CSUMERRORS);
528063231bddSPavel Emelyanov 	TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
52811da177e4SLinus Torvalds 
52821da177e4SLinus Torvalds discard:
52831da177e4SLinus Torvalds 	__kfree_skb(skb);
52841da177e4SLinus Torvalds 	return 0;
52851da177e4SLinus Torvalds }
52864bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_rcv_established);
52871da177e4SLinus Torvalds 
5288370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
5289370816aeSPavel Emelyanov {
5290370816aeSPavel Emelyanov 	struct tcp_sock *tp = tcp_sk(sk);
5291370816aeSPavel Emelyanov 	struct inet_connection_sock *icsk = inet_csk(sk);
5292370816aeSPavel Emelyanov 
5293370816aeSPavel Emelyanov 	tcp_set_state(sk, TCP_ESTABLISHED);
5294370816aeSPavel Emelyanov 
529541063e9dSDavid S. Miller 	if (skb != NULL) {
52965d299f3dSEric Dumazet 		icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
5297370816aeSPavel Emelyanov 		security_inet_conn_established(sk, skb);
529841063e9dSDavid S. Miller 	}
5299370816aeSPavel Emelyanov 
5300370816aeSPavel Emelyanov 	/* Make sure socket is routed, for correct metrics.  */
5301370816aeSPavel Emelyanov 	icsk->icsk_af_ops->rebuild_header(sk);
5302370816aeSPavel Emelyanov 
5303370816aeSPavel Emelyanov 	tcp_init_metrics(sk);
5304370816aeSPavel Emelyanov 
5305370816aeSPavel Emelyanov 	tcp_init_congestion_control(sk);
5306370816aeSPavel Emelyanov 
5307370816aeSPavel Emelyanov 	/* Prevent spurious tcp_cwnd_restart() on first data
5308370816aeSPavel Emelyanov 	 * packet.
5309370816aeSPavel Emelyanov 	 */
5310370816aeSPavel Emelyanov 	tp->lsndtime = tcp_time_stamp;
5311370816aeSPavel Emelyanov 
5312370816aeSPavel Emelyanov 	tcp_init_buffer_space(sk);
5313370816aeSPavel Emelyanov 
5314370816aeSPavel Emelyanov 	if (sock_flag(sk, SOCK_KEEPOPEN))
5315370816aeSPavel Emelyanov 		inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp));
5316370816aeSPavel Emelyanov 
5317370816aeSPavel Emelyanov 	if (!tp->rx_opt.snd_wscale)
5318370816aeSPavel Emelyanov 		__tcp_fast_path_on(tp, tp->snd_wnd);
5319370816aeSPavel Emelyanov 	else
5320370816aeSPavel Emelyanov 		tp->pred_flags = 0;
5321370816aeSPavel Emelyanov 
5322370816aeSPavel Emelyanov 	if (!sock_flag(sk, SOCK_DEAD)) {
5323370816aeSPavel Emelyanov 		sk->sk_state_change(sk);
5324370816aeSPavel Emelyanov 		sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
5325370816aeSPavel Emelyanov 	}
5326370816aeSPavel Emelyanov }
5327370816aeSPavel Emelyanov 
53288e4178c1SYuchung Cheng static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
53298e4178c1SYuchung Cheng 				    struct tcp_fastopen_cookie *cookie)
53308e4178c1SYuchung Cheng {
53318e4178c1SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
533267da22d2SYuchung Cheng 	struct sk_buff *data = tp->syn_data ? tcp_write_queue_head(sk) : NULL;
53338e4178c1SYuchung Cheng 	u16 mss = tp->rx_opt.mss_clamp;
5334aab48743SYuchung Cheng 	bool syn_drop;
53358e4178c1SYuchung Cheng 
53368e4178c1SYuchung Cheng 	if (mss == tp->rx_opt.user_mss) {
53378e4178c1SYuchung Cheng 		struct tcp_options_received opt;
53388e4178c1SYuchung Cheng 
53398e4178c1SYuchung Cheng 		/* Get original SYNACK MSS value if user MSS sets mss_clamp */
53408e4178c1SYuchung Cheng 		tcp_clear_options(&opt);
53418e4178c1SYuchung Cheng 		opt.user_mss = opt.mss_clamp = 0;
53421a2c6181SChristoph Paasch 		tcp_parse_options(synack, &opt, 0, NULL);
53438e4178c1SYuchung Cheng 		mss = opt.mss_clamp;
53448e4178c1SYuchung Cheng 	}
53458e4178c1SYuchung Cheng 
534667da22d2SYuchung Cheng 	if (!tp->syn_fastopen)  /* Ignore an unsolicited cookie */
534767da22d2SYuchung Cheng 		cookie->len = -1;
534867da22d2SYuchung Cheng 
5349aab48743SYuchung Cheng 	/* The SYN-ACK neither has cookie nor acknowledges the data. Presumably
5350aab48743SYuchung Cheng 	 * the remote receives only the retransmitted (regular) SYNs: either
5351aab48743SYuchung Cheng 	 * the original SYN-data or the corresponding SYN-ACK is lost.
5352aab48743SYuchung Cheng 	 */
535366555e92SYuchung Cheng 	syn_drop = (cookie->len <= 0 && data && tp->total_retrans);
5354aab48743SYuchung Cheng 
5355aab48743SYuchung Cheng 	tcp_fastopen_cache_set(sk, mss, cookie, syn_drop);
53568e4178c1SYuchung Cheng 
53578e4178c1SYuchung Cheng 	if (data) { /* Retransmit unacked data in SYN */
535893b174adSYuchung Cheng 		tcp_for_write_queue_from(data, sk) {
535993b174adSYuchung Cheng 			if (data == tcp_send_head(sk) ||
536093b174adSYuchung Cheng 			    __tcp_retransmit_skb(sk, data))
536193b174adSYuchung Cheng 				break;
536293b174adSYuchung Cheng 		}
53638e4178c1SYuchung Cheng 		tcp_rearm_rto(sk);
53648e4178c1SYuchung Cheng 		return true;
53658e4178c1SYuchung Cheng 	}
53666f73601eSYuchung Cheng 	tp->syn_data_acked = tp->syn_data;
53678e4178c1SYuchung Cheng 	return false;
53688e4178c1SYuchung Cheng }
53698e4178c1SYuchung Cheng 
53701da177e4SLinus Torvalds static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
5371cf533ea5SEric Dumazet 					 const struct tcphdr *th, unsigned int len)
53721da177e4SLinus Torvalds {
5373d83d8461SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
53744957faadSWilliam Allen Simpson 	struct tcp_sock *tp = tcp_sk(sk);
53758e4178c1SYuchung Cheng 	struct tcp_fastopen_cookie foc = { .len = -1 };
53764957faadSWilliam Allen Simpson 	int saved_clamp = tp->rx_opt.mss_clamp;
53771da177e4SLinus Torvalds 
53781a2c6181SChristoph Paasch 	tcp_parse_options(skb, &tp->rx_opt, 0, &foc);
5379ee684b6fSAndrey Vagin 	if (tp->rx_opt.saw_tstamp)
5380ee684b6fSAndrey Vagin 		tp->rx_opt.rcv_tsecr -= tp->tsoffset;
53811da177e4SLinus Torvalds 
53821da177e4SLinus Torvalds 	if (th->ack) {
53831da177e4SLinus Torvalds 		/* rfc793:
53841da177e4SLinus Torvalds 		 * "If the state is SYN-SENT then
53851da177e4SLinus Torvalds 		 *    first check the ACK bit
53861da177e4SLinus Torvalds 		 *      If the ACK bit is set
53871da177e4SLinus Torvalds 		 *	  If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
53881da177e4SLinus Torvalds 		 *        a reset (unless the RST bit is set, if so drop
53891da177e4SLinus Torvalds 		 *        the segment and return)"
53901da177e4SLinus Torvalds 		 */
53918e4178c1SYuchung Cheng 		if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
53928e4178c1SYuchung Cheng 		    after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt))
53931da177e4SLinus Torvalds 			goto reset_and_undo;
53941da177e4SLinus Torvalds 
53951da177e4SLinus Torvalds 		if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
53961da177e4SLinus Torvalds 		    !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp,
53971da177e4SLinus Torvalds 			     tcp_time_stamp)) {
5398de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSACTIVEREJECTED);
53991da177e4SLinus Torvalds 			goto reset_and_undo;
54001da177e4SLinus Torvalds 		}
54011da177e4SLinus Torvalds 
54021da177e4SLinus Torvalds 		/* Now ACK is acceptable.
54031da177e4SLinus Torvalds 		 *
54041da177e4SLinus Torvalds 		 * "If the RST bit is set
54051da177e4SLinus Torvalds 		 *    If the ACK was acceptable then signal the user "error:
54061da177e4SLinus Torvalds 		 *    connection reset", drop the segment, enter CLOSED state,
54071da177e4SLinus Torvalds 		 *    delete TCB, and return."
54081da177e4SLinus Torvalds 		 */
54091da177e4SLinus Torvalds 
54101da177e4SLinus Torvalds 		if (th->rst) {
54111da177e4SLinus Torvalds 			tcp_reset(sk);
54121da177e4SLinus Torvalds 			goto discard;
54131da177e4SLinus Torvalds 		}
54141da177e4SLinus Torvalds 
54151da177e4SLinus Torvalds 		/* rfc793:
54161da177e4SLinus Torvalds 		 *   "fifth, if neither of the SYN or RST bits is set then
54171da177e4SLinus Torvalds 		 *    drop the segment and return."
54181da177e4SLinus Torvalds 		 *
54191da177e4SLinus Torvalds 		 *    See note below!
54201da177e4SLinus Torvalds 		 *                                        --ANK(990513)
54211da177e4SLinus Torvalds 		 */
54221da177e4SLinus Torvalds 		if (!th->syn)
54231da177e4SLinus Torvalds 			goto discard_and_undo;
54241da177e4SLinus Torvalds 
54251da177e4SLinus Torvalds 		/* rfc793:
54261da177e4SLinus Torvalds 		 *   "If the SYN bit is on ...
54271da177e4SLinus Torvalds 		 *    are acceptable then ...
54281da177e4SLinus Torvalds 		 *    (our SYN has been ACKed), change the connection
54291da177e4SLinus Torvalds 		 *    state to ESTABLISHED..."
54301da177e4SLinus Torvalds 		 */
54311da177e4SLinus Torvalds 
54321da177e4SLinus Torvalds 		TCP_ECN_rcv_synack(tp, th);
54331da177e4SLinus Torvalds 
54341b3a6926SRazvan Ghitulete 		tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
54351da177e4SLinus Torvalds 		tcp_ack(sk, skb, FLAG_SLOWPATH);
54361da177e4SLinus Torvalds 
54371da177e4SLinus Torvalds 		/* Ok.. it's good. Set up sequence numbers and
54381da177e4SLinus Torvalds 		 * move to established.
54391da177e4SLinus Torvalds 		 */
54401da177e4SLinus Torvalds 		tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
54411da177e4SLinus Torvalds 		tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
54421da177e4SLinus Torvalds 
54431da177e4SLinus Torvalds 		/* RFC1323: The window in SYN & SYN/ACK segments is
54441da177e4SLinus Torvalds 		 * never scaled.
54451da177e4SLinus Torvalds 		 */
54461da177e4SLinus Torvalds 		tp->snd_wnd = ntohs(th->window);
54471da177e4SLinus Torvalds 
54481da177e4SLinus Torvalds 		if (!tp->rx_opt.wscale_ok) {
54491da177e4SLinus Torvalds 			tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0;
54501da177e4SLinus Torvalds 			tp->window_clamp = min(tp->window_clamp, 65535U);
54511da177e4SLinus Torvalds 		}
54521da177e4SLinus Torvalds 
54531da177e4SLinus Torvalds 		if (tp->rx_opt.saw_tstamp) {
54541da177e4SLinus Torvalds 			tp->rx_opt.tstamp_ok	   = 1;
54551da177e4SLinus Torvalds 			tp->tcp_header_len =
54561da177e4SLinus Torvalds 				sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
54571da177e4SLinus Torvalds 			tp->advmss	    -= TCPOLEN_TSTAMP_ALIGNED;
54581da177e4SLinus Torvalds 			tcp_store_ts_recent(tp);
54591da177e4SLinus Torvalds 		} else {
54601da177e4SLinus Torvalds 			tp->tcp_header_len = sizeof(struct tcphdr);
54611da177e4SLinus Torvalds 		}
54621da177e4SLinus Torvalds 
5463e60402d0SIlpo Järvinen 		if (tcp_is_sack(tp) && sysctl_tcp_fack)
5464e60402d0SIlpo Järvinen 			tcp_enable_fack(tp);
54651da177e4SLinus Torvalds 
54665d424d5aSJohn Heffner 		tcp_mtup_init(sk);
5467d83d8461SArnaldo Carvalho de Melo 		tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
54681da177e4SLinus Torvalds 		tcp_initialize_rcv_mss(sk);
54691da177e4SLinus Torvalds 
54701da177e4SLinus Torvalds 		/* Remember, tcp_poll() does not lock socket!
54711da177e4SLinus Torvalds 		 * Change state from SYN-SENT only after copied_seq
54721da177e4SLinus Torvalds 		 * is initialized. */
54731da177e4SLinus Torvalds 		tp->copied_seq = tp->rcv_nxt;
54744957faadSWilliam Allen Simpson 
5475e16aa207SRalf Baechle 		smp_mb();
54761da177e4SLinus Torvalds 
5477370816aeSPavel Emelyanov 		tcp_finish_connect(sk, skb);
54781da177e4SLinus Torvalds 
547967da22d2SYuchung Cheng 		if ((tp->syn_fastopen || tp->syn_data) &&
548067da22d2SYuchung Cheng 		    tcp_rcv_fastopen_synack(sk, skb, &foc))
54818e4178c1SYuchung Cheng 			return -1;
54828e4178c1SYuchung Cheng 
5483295f7324SArnaldo Carvalho de Melo 		if (sk->sk_write_pending ||
5484295f7324SArnaldo Carvalho de Melo 		    icsk->icsk_accept_queue.rskq_defer_accept ||
5485295f7324SArnaldo Carvalho de Melo 		    icsk->icsk_ack.pingpong) {
54861da177e4SLinus Torvalds 			/* Save one ACK. Data will be ready after
54871da177e4SLinus Torvalds 			 * several ticks, if write_pending is set.
54881da177e4SLinus Torvalds 			 *
54891da177e4SLinus Torvalds 			 * It may be deleted, but with this feature tcpdumps
54901da177e4SLinus Torvalds 			 * look so _wonderfully_ clever, that I was not able
54911da177e4SLinus Torvalds 			 * to stand against the temptation 8)     --ANK
54921da177e4SLinus Torvalds 			 */
5493463c84b9SArnaldo Carvalho de Melo 			inet_csk_schedule_ack(sk);
5494295f7324SArnaldo Carvalho de Melo 			icsk->icsk_ack.lrcvtime = tcp_time_stamp;
5495463c84b9SArnaldo Carvalho de Melo 			tcp_enter_quickack_mode(sk);
54963f421baaSArnaldo Carvalho de Melo 			inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
54973f421baaSArnaldo Carvalho de Melo 						  TCP_DELACK_MAX, TCP_RTO_MAX);
54981da177e4SLinus Torvalds 
54991da177e4SLinus Torvalds discard:
55001da177e4SLinus Torvalds 			__kfree_skb(skb);
55011da177e4SLinus Torvalds 			return 0;
55021da177e4SLinus Torvalds 		} else {
55031da177e4SLinus Torvalds 			tcp_send_ack(sk);
55041da177e4SLinus Torvalds 		}
55051da177e4SLinus Torvalds 		return -1;
55061da177e4SLinus Torvalds 	}
55071da177e4SLinus Torvalds 
55081da177e4SLinus Torvalds 	/* No ACK in the segment */
55091da177e4SLinus Torvalds 
55101da177e4SLinus Torvalds 	if (th->rst) {
55111da177e4SLinus Torvalds 		/* rfc793:
55121da177e4SLinus Torvalds 		 * "If the RST bit is set
55131da177e4SLinus Torvalds 		 *
55141da177e4SLinus Torvalds 		 *      Otherwise (no ACK) drop the segment and return."
55151da177e4SLinus Torvalds 		 */
55161da177e4SLinus Torvalds 
55171da177e4SLinus Torvalds 		goto discard_and_undo;
55181da177e4SLinus Torvalds 	}
55191da177e4SLinus Torvalds 
55201da177e4SLinus Torvalds 	/* PAWS check. */
5521056834d9SIlpo Järvinen 	if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp &&
5522c887e6d2SIlpo Järvinen 	    tcp_paws_reject(&tp->rx_opt, 0))
55231da177e4SLinus Torvalds 		goto discard_and_undo;
55241da177e4SLinus Torvalds 
55251da177e4SLinus Torvalds 	if (th->syn) {
55261da177e4SLinus Torvalds 		/* We see SYN without ACK. It is attempt of
55271da177e4SLinus Torvalds 		 * simultaneous connect with crossed SYNs.
55281da177e4SLinus Torvalds 		 * Particularly, it can be connect to self.
55291da177e4SLinus Torvalds 		 */
55301da177e4SLinus Torvalds 		tcp_set_state(sk, TCP_SYN_RECV);
55311da177e4SLinus Torvalds 
55321da177e4SLinus Torvalds 		if (tp->rx_opt.saw_tstamp) {
55331da177e4SLinus Torvalds 			tp->rx_opt.tstamp_ok = 1;
55341da177e4SLinus Torvalds 			tcp_store_ts_recent(tp);
55351da177e4SLinus Torvalds 			tp->tcp_header_len =
55361da177e4SLinus Torvalds 				sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
55371da177e4SLinus Torvalds 		} else {
55381da177e4SLinus Torvalds 			tp->tcp_header_len = sizeof(struct tcphdr);
55391da177e4SLinus Torvalds 		}
55401da177e4SLinus Torvalds 
55411da177e4SLinus Torvalds 		tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
55421da177e4SLinus Torvalds 		tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
55431da177e4SLinus Torvalds 
55441da177e4SLinus Torvalds 		/* RFC1323: The window in SYN & SYN/ACK segments is
55451da177e4SLinus Torvalds 		 * never scaled.
55461da177e4SLinus Torvalds 		 */
55471da177e4SLinus Torvalds 		tp->snd_wnd    = ntohs(th->window);
55481da177e4SLinus Torvalds 		tp->snd_wl1    = TCP_SKB_CB(skb)->seq;
55491da177e4SLinus Torvalds 		tp->max_window = tp->snd_wnd;
55501da177e4SLinus Torvalds 
55511da177e4SLinus Torvalds 		TCP_ECN_rcv_syn(tp, th);
55521da177e4SLinus Torvalds 
55535d424d5aSJohn Heffner 		tcp_mtup_init(sk);
5554d83d8461SArnaldo Carvalho de Melo 		tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
55551da177e4SLinus Torvalds 		tcp_initialize_rcv_mss(sk);
55561da177e4SLinus Torvalds 
55571da177e4SLinus Torvalds 		tcp_send_synack(sk);
55581da177e4SLinus Torvalds #if 0
55591da177e4SLinus Torvalds 		/* Note, we could accept data and URG from this segment.
5560168a8f58SJerry Chu 		 * There are no obstacles to make this (except that we must
5561168a8f58SJerry Chu 		 * either change tcp_recvmsg() to prevent it from returning data
5562168a8f58SJerry Chu 		 * before 3WHS completes per RFC793, or employ TCP Fast Open).
55631da177e4SLinus Torvalds 		 *
55641da177e4SLinus Torvalds 		 * However, if we ignore data in ACKless segments sometimes,
55651da177e4SLinus Torvalds 		 * we have no reasons to accept it sometimes.
55661da177e4SLinus Torvalds 		 * Also, seems the code doing it in step6 of tcp_rcv_state_process
55671da177e4SLinus Torvalds 		 * is not flawless. So, discard packet for sanity.
55681da177e4SLinus Torvalds 		 * Uncomment this return to process the data.
55691da177e4SLinus Torvalds 		 */
55701da177e4SLinus Torvalds 		return -1;
55711da177e4SLinus Torvalds #else
55721da177e4SLinus Torvalds 		goto discard;
55731da177e4SLinus Torvalds #endif
55741da177e4SLinus Torvalds 	}
55751da177e4SLinus Torvalds 	/* "fifth, if neither of the SYN or RST bits is set then
55761da177e4SLinus Torvalds 	 * drop the segment and return."
55771da177e4SLinus Torvalds 	 */
55781da177e4SLinus Torvalds 
55791da177e4SLinus Torvalds discard_and_undo:
55801da177e4SLinus Torvalds 	tcp_clear_options(&tp->rx_opt);
55811da177e4SLinus Torvalds 	tp->rx_opt.mss_clamp = saved_clamp;
55821da177e4SLinus Torvalds 	goto discard;
55831da177e4SLinus Torvalds 
55841da177e4SLinus Torvalds reset_and_undo:
55851da177e4SLinus Torvalds 	tcp_clear_options(&tp->rx_opt);
55861da177e4SLinus Torvalds 	tp->rx_opt.mss_clamp = saved_clamp;
55871da177e4SLinus Torvalds 	return 1;
55881da177e4SLinus Torvalds }
55891da177e4SLinus Torvalds 
55901da177e4SLinus Torvalds /*
55911da177e4SLinus Torvalds  *	This function implements the receiving procedure of RFC 793 for
55921da177e4SLinus Torvalds  *	all states except ESTABLISHED and TIME_WAIT.
55931da177e4SLinus Torvalds  *	It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
55941da177e4SLinus Torvalds  *	address independent.
55951da177e4SLinus Torvalds  */
55961da177e4SLinus Torvalds 
55971da177e4SLinus Torvalds int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
5598cf533ea5SEric Dumazet 			  const struct tcphdr *th, unsigned int len)
55991da177e4SLinus Torvalds {
56001da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
56018292a17aSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
5602168a8f58SJerry Chu 	struct request_sock *req;
56031da177e4SLinus Torvalds 	int queued = 0;
56041da177e4SLinus Torvalds 
56051da177e4SLinus Torvalds 	tp->rx_opt.saw_tstamp = 0;
56061da177e4SLinus Torvalds 
56071da177e4SLinus Torvalds 	switch (sk->sk_state) {
56081da177e4SLinus Torvalds 	case TCP_CLOSE:
56091da177e4SLinus Torvalds 		goto discard;
56101da177e4SLinus Torvalds 
56111da177e4SLinus Torvalds 	case TCP_LISTEN:
56121da177e4SLinus Torvalds 		if (th->ack)
56131da177e4SLinus Torvalds 			return 1;
56141da177e4SLinus Torvalds 
56151da177e4SLinus Torvalds 		if (th->rst)
56161da177e4SLinus Torvalds 			goto discard;
56171da177e4SLinus Torvalds 
56181da177e4SLinus Torvalds 		if (th->syn) {
5619fdf5af0dSEric Dumazet 			if (th->fin)
5620fdf5af0dSEric Dumazet 				goto discard;
56218292a17aSArnaldo Carvalho de Melo 			if (icsk->icsk_af_ops->conn_request(sk, skb) < 0)
56221da177e4SLinus Torvalds 				return 1;
56231da177e4SLinus Torvalds 
56241da177e4SLinus Torvalds 			/* Now we have several options: In theory there is
56251da177e4SLinus Torvalds 			 * nothing else in the frame. KA9Q has an option to
56261da177e4SLinus Torvalds 			 * send data with the syn, BSD accepts data with the
56271da177e4SLinus Torvalds 			 * syn up to the [to be] advertised window and
56281da177e4SLinus Torvalds 			 * Solaris 2.1 gives you a protocol error. For now
56291da177e4SLinus Torvalds 			 * we just ignore it, that fits the spec precisely
56301da177e4SLinus Torvalds 			 * and avoids incompatibilities. It would be nice in
56311da177e4SLinus Torvalds 			 * future to drop through and process the data.
56321da177e4SLinus Torvalds 			 *
56331da177e4SLinus Torvalds 			 * Now that TTCP is starting to be used we ought to
56341da177e4SLinus Torvalds 			 * queue this data.
56351da177e4SLinus Torvalds 			 * But, this leaves one open to an easy denial of
56361da177e4SLinus Torvalds 			 * service attack, and SYN cookies can't defend
56371da177e4SLinus Torvalds 			 * against this problem. So, we drop the data
5638fb7e2399SMasayuki Nakagawa 			 * in the interest of security over speed unless
5639fb7e2399SMasayuki Nakagawa 			 * it's still in use.
56401da177e4SLinus Torvalds 			 */
5641fb7e2399SMasayuki Nakagawa 			kfree_skb(skb);
5642fb7e2399SMasayuki Nakagawa 			return 0;
56431da177e4SLinus Torvalds 		}
56441da177e4SLinus Torvalds 		goto discard;
56451da177e4SLinus Torvalds 
56461da177e4SLinus Torvalds 	case TCP_SYN_SENT:
56471da177e4SLinus Torvalds 		queued = tcp_rcv_synsent_state_process(sk, skb, th, len);
56481da177e4SLinus Torvalds 		if (queued >= 0)
56491da177e4SLinus Torvalds 			return queued;
56501da177e4SLinus Torvalds 
56511da177e4SLinus Torvalds 		/* Do step6 onward by hand. */
56521da177e4SLinus Torvalds 		tcp_urg(sk, skb, th);
56531da177e4SLinus Torvalds 		__kfree_skb(skb);
56549e412ba7SIlpo Järvinen 		tcp_data_snd_check(sk);
56551da177e4SLinus Torvalds 		return 0;
56561da177e4SLinus Torvalds 	}
56571da177e4SLinus Torvalds 
5658168a8f58SJerry Chu 	req = tp->fastopen_rsk;
5659168a8f58SJerry Chu 	if (req != NULL) {
566037561f68SJerry Chu 		WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
5661168a8f58SJerry Chu 		    sk->sk_state != TCP_FIN_WAIT1);
5662168a8f58SJerry Chu 
5663168a8f58SJerry Chu 		if (tcp_check_req(sk, skb, req, NULL, true) == NULL)
5664168a8f58SJerry Chu 			goto discard;
5665e69bebdeSNeal Cardwell 	}
5666c3ae62afSEric Dumazet 
56677b514a88SEric Dumazet 	if (!th->ack && !th->rst)
5668c3ae62afSEric Dumazet 		goto discard;
5669c3ae62afSEric Dumazet 
5670e69bebdeSNeal Cardwell 	if (!tcp_validate_incoming(sk, skb, th, 0))
56710c24604bSEric Dumazet 		return 0;
56721da177e4SLinus Torvalds 
56731da177e4SLinus Torvalds 	/* step 5: check the ACK field */
5674c3ae62afSEric Dumazet 	if (true) {
567512fb3dd9SEric Dumazet 		int acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
567612fb3dd9SEric Dumazet 						  FLAG_UPDATE_TS_RECENT) > 0;
56771da177e4SLinus Torvalds 
56781da177e4SLinus Torvalds 		switch (sk->sk_state) {
56791da177e4SLinus Torvalds 		case TCP_SYN_RECV:
56801da177e4SLinus Torvalds 			if (acceptable) {
5681168a8f58SJerry Chu 				/* Once we leave TCP_SYN_RECV, we no longer
5682168a8f58SJerry Chu 				 * need req so release it.
5683168a8f58SJerry Chu 				 */
5684168a8f58SJerry Chu 				if (req) {
5685016818d0SNeal Cardwell 					tcp_synack_rtt_meas(sk, req);
5686e6c022a4SEric Dumazet 					tp->total_retrans = req->num_retrans;
568730099b2eSNeal Cardwell 
5688168a8f58SJerry Chu 					reqsk_fastopen_remove(sk, req, false);
5689168a8f58SJerry Chu 				} else {
5690168a8f58SJerry Chu 					/* Make sure socket is routed, for
5691168a8f58SJerry Chu 					 * correct metrics.
5692168a8f58SJerry Chu 					 */
5693168a8f58SJerry Chu 					icsk->icsk_af_ops->rebuild_header(sk);
5694168a8f58SJerry Chu 					tcp_init_congestion_control(sk);
5695168a8f58SJerry Chu 
5696168a8f58SJerry Chu 					tcp_mtup_init(sk);
5697168a8f58SJerry Chu 					tcp_init_buffer_space(sk);
56981da177e4SLinus Torvalds 					tp->copied_seq = tp->rcv_nxt;
5699168a8f58SJerry Chu 				}
5700e16aa207SRalf Baechle 				smp_mb();
57011da177e4SLinus Torvalds 				tcp_set_state(sk, TCP_ESTABLISHED);
57021da177e4SLinus Torvalds 				sk->sk_state_change(sk);
57031da177e4SLinus Torvalds 
57041da177e4SLinus Torvalds 				/* Note, that this wakeup is only for marginal
57051da177e4SLinus Torvalds 				 * crossed SYN case. Passively open sockets
57061da177e4SLinus Torvalds 				 * are not waked up, because sk->sk_sleep ==
57071da177e4SLinus Torvalds 				 * NULL and sk->sk_socket == NULL.
57081da177e4SLinus Torvalds 				 */
57098d8ad9d7SPavel Emelyanov 				if (sk->sk_socket)
57108d8ad9d7SPavel Emelyanov 					sk_wake_async(sk,
57118d8ad9d7SPavel Emelyanov 						      SOCK_WAKE_IO, POLL_OUT);
57121da177e4SLinus Torvalds 
57131da177e4SLinus Torvalds 				tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
57141da177e4SLinus Torvalds 				tp->snd_wnd = ntohs(th->window) <<
57151da177e4SLinus Torvalds 					      tp->rx_opt.snd_wscale;
5716ee7537b6SHantzis Fotis 				tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
57171da177e4SLinus Torvalds 
57181da177e4SLinus Torvalds 				if (tp->rx_opt.tstamp_ok)
57191da177e4SLinus Torvalds 					tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
57201da177e4SLinus Torvalds 
5721168a8f58SJerry Chu 				if (req) {
5722168a8f58SJerry Chu 					/* Re-arm the timer because data may
5723168a8f58SJerry Chu 					 * have been sent out. This is similar
5724168a8f58SJerry Chu 					 * to the regular data transmission case
5725168a8f58SJerry Chu 					 * when new data has just been ack'ed.
5726168a8f58SJerry Chu 					 *
5727168a8f58SJerry Chu 					 * (TFO) - we could try to be more
5728168a8f58SJerry Chu 					 * aggressive and retranmitting any data
5729168a8f58SJerry Chu 					 * sooner based on when they were sent
5730168a8f58SJerry Chu 					 * out.
57311da177e4SLinus Torvalds 					 */
5732168a8f58SJerry Chu 					tcp_rearm_rto(sk);
5733168a8f58SJerry Chu 				} else
57341da177e4SLinus Torvalds 					tcp_init_metrics(sk);
57351da177e4SLinus Torvalds 
57361da177e4SLinus Torvalds 				/* Prevent spurious tcp_cwnd_restart() on
57371da177e4SLinus Torvalds 				 * first data packet.
57381da177e4SLinus Torvalds 				 */
57391da177e4SLinus Torvalds 				tp->lsndtime = tcp_time_stamp;
57401da177e4SLinus Torvalds 
57411da177e4SLinus Torvalds 				tcp_initialize_rcv_mss(sk);
57421da177e4SLinus Torvalds 				tcp_fast_path_on(tp);
57431da177e4SLinus Torvalds 			} else {
57441da177e4SLinus Torvalds 				return 1;
57451da177e4SLinus Torvalds 			}
57461da177e4SLinus Torvalds 			break;
57471da177e4SLinus Torvalds 
57481da177e4SLinus Torvalds 		case TCP_FIN_WAIT1:
5749168a8f58SJerry Chu 			/* If we enter the TCP_FIN_WAIT1 state and we are a
5750168a8f58SJerry Chu 			 * Fast Open socket and this is the first acceptable
5751168a8f58SJerry Chu 			 * ACK we have received, this would have acknowledged
5752168a8f58SJerry Chu 			 * our SYNACK so stop the SYNACK timer.
5753168a8f58SJerry Chu 			 */
575437561f68SJerry Chu 			if (req != NULL) {
575537561f68SJerry Chu 				/* Return RST if ack_seq is invalid.
575637561f68SJerry Chu 				 * Note that RFC793 only says to generate a
575737561f68SJerry Chu 				 * DUPACK for it but for TCP Fast Open it seems
575837561f68SJerry Chu 				 * better to treat this case like TCP_SYN_RECV
575937561f68SJerry Chu 				 * above.
576037561f68SJerry Chu 				 */
576137561f68SJerry Chu 				if (!acceptable)
576237561f68SJerry Chu 					return 1;
5763168a8f58SJerry Chu 				/* We no longer need the request sock. */
5764168a8f58SJerry Chu 				reqsk_fastopen_remove(sk, req, false);
5765168a8f58SJerry Chu 				tcp_rearm_rto(sk);
5766168a8f58SJerry Chu 			}
57671da177e4SLinus Torvalds 			if (tp->snd_una == tp->write_seq) {
57685110effeSDavid S. Miller 				struct dst_entry *dst;
57695110effeSDavid S. Miller 
57701da177e4SLinus Torvalds 				tcp_set_state(sk, TCP_FIN_WAIT2);
57711da177e4SLinus Torvalds 				sk->sk_shutdown |= SEND_SHUTDOWN;
57725110effeSDavid S. Miller 
57735110effeSDavid S. Miller 				dst = __sk_dst_get(sk);
57745110effeSDavid S. Miller 				if (dst)
57755110effeSDavid S. Miller 					dst_confirm(dst);
57761da177e4SLinus Torvalds 
57771da177e4SLinus Torvalds 				if (!sock_flag(sk, SOCK_DEAD))
57781da177e4SLinus Torvalds 					/* Wake up lingering close() */
57791da177e4SLinus Torvalds 					sk->sk_state_change(sk);
57801da177e4SLinus Torvalds 				else {
57811da177e4SLinus Torvalds 					int tmo;
57821da177e4SLinus Torvalds 
57831da177e4SLinus Torvalds 					if (tp->linger2 < 0 ||
57841da177e4SLinus Torvalds 					    (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
57851da177e4SLinus Torvalds 					     after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) {
57861da177e4SLinus Torvalds 						tcp_done(sk);
5787de0744afSPavel Emelyanov 						NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
57881da177e4SLinus Torvalds 						return 1;
57891da177e4SLinus Torvalds 					}
57901da177e4SLinus Torvalds 
5791463c84b9SArnaldo Carvalho de Melo 					tmo = tcp_fin_time(sk);
57921da177e4SLinus Torvalds 					if (tmo > TCP_TIMEWAIT_LEN) {
5793463c84b9SArnaldo Carvalho de Melo 						inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
57941da177e4SLinus Torvalds 					} else if (th->fin || sock_owned_by_user(sk)) {
57951da177e4SLinus Torvalds 						/* Bad case. We could lose such FIN otherwise.
57961da177e4SLinus Torvalds 						 * It is not a big problem, but it looks confusing
57971da177e4SLinus Torvalds 						 * and not so rare event. We still can lose it now,
57981da177e4SLinus Torvalds 						 * if it spins in bh_lock_sock(), but it is really
57991da177e4SLinus Torvalds 						 * marginal case.
58001da177e4SLinus Torvalds 						 */
5801463c84b9SArnaldo Carvalho de Melo 						inet_csk_reset_keepalive_timer(sk, tmo);
58021da177e4SLinus Torvalds 					} else {
58031da177e4SLinus Torvalds 						tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
58041da177e4SLinus Torvalds 						goto discard;
58051da177e4SLinus Torvalds 					}
58061da177e4SLinus Torvalds 				}
58071da177e4SLinus Torvalds 			}
58081da177e4SLinus Torvalds 			break;
58091da177e4SLinus Torvalds 
58101da177e4SLinus Torvalds 		case TCP_CLOSING:
58111da177e4SLinus Torvalds 			if (tp->snd_una == tp->write_seq) {
58121da177e4SLinus Torvalds 				tcp_time_wait(sk, TCP_TIME_WAIT, 0);
58131da177e4SLinus Torvalds 				goto discard;
58141da177e4SLinus Torvalds 			}
58151da177e4SLinus Torvalds 			break;
58161da177e4SLinus Torvalds 
58171da177e4SLinus Torvalds 		case TCP_LAST_ACK:
58181da177e4SLinus Torvalds 			if (tp->snd_una == tp->write_seq) {
58191da177e4SLinus Torvalds 				tcp_update_metrics(sk);
58201da177e4SLinus Torvalds 				tcp_done(sk);
58211da177e4SLinus Torvalds 				goto discard;
58221da177e4SLinus Torvalds 			}
58231da177e4SLinus Torvalds 			break;
58241da177e4SLinus Torvalds 		}
5825c3ae62afSEric Dumazet 	}
58261da177e4SLinus Torvalds 
58271da177e4SLinus Torvalds 	/* step 6: check the URG bit */
58281da177e4SLinus Torvalds 	tcp_urg(sk, skb, th);
58291da177e4SLinus Torvalds 
58301da177e4SLinus Torvalds 	/* step 7: process the segment text */
58311da177e4SLinus Torvalds 	switch (sk->sk_state) {
58321da177e4SLinus Torvalds 	case TCP_CLOSE_WAIT:
58331da177e4SLinus Torvalds 	case TCP_CLOSING:
58341da177e4SLinus Torvalds 	case TCP_LAST_ACK:
58351da177e4SLinus Torvalds 		if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
58361da177e4SLinus Torvalds 			break;
58371da177e4SLinus Torvalds 	case TCP_FIN_WAIT1:
58381da177e4SLinus Torvalds 	case TCP_FIN_WAIT2:
58391da177e4SLinus Torvalds 		/* RFC 793 says to queue data in these states,
58401da177e4SLinus Torvalds 		 * RFC 1122 says we MUST send a reset.
58411da177e4SLinus Torvalds 		 * BSD 4.4 also does reset.
58421da177e4SLinus Torvalds 		 */
58431da177e4SLinus Torvalds 		if (sk->sk_shutdown & RCV_SHUTDOWN) {
58441da177e4SLinus Torvalds 			if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
58451da177e4SLinus Torvalds 			    after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
5846de0744afSPavel Emelyanov 				NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
58471da177e4SLinus Torvalds 				tcp_reset(sk);
58481da177e4SLinus Torvalds 				return 1;
58491da177e4SLinus Torvalds 			}
58501da177e4SLinus Torvalds 		}
58511da177e4SLinus Torvalds 		/* Fall through */
58521da177e4SLinus Torvalds 	case TCP_ESTABLISHED:
58531da177e4SLinus Torvalds 		tcp_data_queue(sk, skb);
58541da177e4SLinus Torvalds 		queued = 1;
58551da177e4SLinus Torvalds 		break;
58561da177e4SLinus Torvalds 	}
58571da177e4SLinus Torvalds 
58581da177e4SLinus Torvalds 	/* tcp_data could move socket to TIME-WAIT */
58591da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE) {
58609e412ba7SIlpo Järvinen 		tcp_data_snd_check(sk);
58611da177e4SLinus Torvalds 		tcp_ack_snd_check(sk);
58621da177e4SLinus Torvalds 	}
58631da177e4SLinus Torvalds 
58641da177e4SLinus Torvalds 	if (!queued) {
58651da177e4SLinus Torvalds discard:
58661da177e4SLinus Torvalds 		__kfree_skb(skb);
58671da177e4SLinus Torvalds 	}
58681da177e4SLinus Torvalds 	return 0;
58691da177e4SLinus Torvalds }
58701da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_rcv_state_process);
5871