xref: /linux/net/ipv4/tcp_input.c (revision 12ed8244ed8b31b023ea6d2851fd8b15f2999e9b)
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>
71ad971f61SEric Dumazet #include <linux/prefetch.h>
725ffc02a1SSatoru SATOH #include <net/dst.h>
731da177e4SLinus Torvalds #include <net/tcp.h>
741da177e4SLinus Torvalds #include <net/inet_common.h>
751da177e4SLinus Torvalds #include <linux/ipsec.h>
761da177e4SLinus Torvalds #include <asm/unaligned.h>
77e1c8a607SWillem de Bruijn #include <linux/errqueue.h>
781da177e4SLinus Torvalds 
79ab32ea5dSBrian Haley int sysctl_tcp_timestamps __read_mostly = 1;
80ab32ea5dSBrian Haley int sysctl_tcp_window_scaling __read_mostly = 1;
81ab32ea5dSBrian Haley int sysctl_tcp_sack __read_mostly = 1;
82ab32ea5dSBrian Haley int sysctl_tcp_fack __read_mostly = 1;
83ab32ea5dSBrian Haley int sysctl_tcp_reordering __read_mostly = TCP_FASTRETRANS_THRESH;
84dca145ffSEric Dumazet int sysctl_tcp_max_reordering __read_mostly = 300;
854bc2f18bSEric Dumazet EXPORT_SYMBOL(sysctl_tcp_reordering);
86ab32ea5dSBrian Haley int sysctl_tcp_dsack __read_mostly = 1;
87ab32ea5dSBrian Haley int sysctl_tcp_app_win __read_mostly = 31;
88b49960a0SEric Dumazet int sysctl_tcp_adv_win_scale __read_mostly = 1;
894bc2f18bSEric Dumazet EXPORT_SYMBOL(sysctl_tcp_adv_win_scale);
901da177e4SLinus Torvalds 
91282f23c6SEric Dumazet /* rfc5961 challenge ack rate limiting */
92282f23c6SEric Dumazet int sysctl_tcp_challenge_ack_limit = 100;
93282f23c6SEric Dumazet 
94ab32ea5dSBrian Haley int sysctl_tcp_stdurg __read_mostly;
95ab32ea5dSBrian Haley int sysctl_tcp_rfc1337 __read_mostly;
96ab32ea5dSBrian Haley int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
97c96fd3d4SIlpo Järvinen int sysctl_tcp_frto __read_mostly = 2;
98f6722583SYuchung Cheng int sysctl_tcp_min_rtt_wlen __read_mostly = 300;
991da177e4SLinus Torvalds 
1007e380175SAndreas Petlund int sysctl_tcp_thin_dupack __read_mostly;
1017e380175SAndreas Petlund 
102ab32ea5dSBrian Haley int sysctl_tcp_moderate_rcvbuf __read_mostly = 1;
1036ba8a3b1SNandita Dukkipati int sysctl_tcp_early_retrans __read_mostly = 3;
104032ee423SNeal Cardwell int sysctl_tcp_invalid_ratelimit __read_mostly = HZ/2;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds #define FLAG_DATA		0x01 /* Incoming frame contained data.		*/
1071da177e4SLinus Torvalds #define FLAG_WIN_UPDATE		0x02 /* Incoming ACK was a window update.	*/
1081da177e4SLinus Torvalds #define FLAG_DATA_ACKED		0x04 /* This ACK acknowledged new data.		*/
1091da177e4SLinus Torvalds #define FLAG_RETRANS_DATA_ACKED	0x08 /* "" "" some of which was retransmitted.	*/
1101da177e4SLinus Torvalds #define FLAG_SYN_ACKED		0x10 /* This ACK acknowledged SYN.		*/
1111da177e4SLinus Torvalds #define FLAG_DATA_SACKED	0x20 /* New SACK.				*/
1121da177e4SLinus Torvalds #define FLAG_ECE		0x40 /* ECE in this ACK				*/
113291a00d1SYuchung Cheng #define FLAG_LOST_RETRANS	0x80 /* This ACK marks some retransmission lost */
1141da177e4SLinus Torvalds #define FLAG_SLOWPATH		0x100 /* Do not skip RFC checks for window update.*/
115e33099f9SYuchung Cheng #define FLAG_ORIG_SACK_ACKED	0x200 /* Never retransmitted data are (s)acked	*/
1162e605294SIlpo Järvinen #define FLAG_SND_UNA_ADVANCED	0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
117564262c1SRyousei Takano #define FLAG_DSACKING_ACK	0x800 /* SACK blocks contained D-SACK info */
118cadbd031SIlpo Järvinen #define FLAG_SACK_RENEGING	0x2000 /* snd_una advanced to a sacked seq */
11912fb3dd9SEric Dumazet #define FLAG_UPDATE_TS_RECENT	0x4000 /* tcp_replace_ts_recent() */
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds #define FLAG_ACKED		(FLAG_DATA_ACKED|FLAG_SYN_ACKED)
1221da177e4SLinus Torvalds #define FLAG_NOT_DUP		(FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
1231da177e4SLinus Torvalds #define FLAG_CA_ALERT		(FLAG_DATA_SACKED|FLAG_ECE)
1241da177e4SLinus Torvalds #define FLAG_FORWARD_PROGRESS	(FLAG_ACKED|FLAG_DATA_SACKED)
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
127bdf1ee5dSIlpo Järvinen #define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
1281da177e4SLinus Torvalds 
129e662ca40SYuchung Cheng #define REXMIT_NONE	0 /* no loss recovery to do */
130e662ca40SYuchung Cheng #define REXMIT_LOST	1 /* retransmit packets marked lost */
131e662ca40SYuchung Cheng #define REXMIT_NEW	2 /* FRTO-style transmit of unsent/new packets */
132e662ca40SYuchung Cheng 
1331da177e4SLinus Torvalds /* Adapt the MSS value used to make delayed ack decision to the
1341da177e4SLinus Torvalds  * real world.
1351da177e4SLinus Torvalds  */
136056834d9SIlpo Järvinen static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
1371da177e4SLinus Torvalds {
138463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
139463c84b9SArnaldo Carvalho de Melo 	const unsigned int lss = icsk->icsk_ack.last_seg_size;
140463c84b9SArnaldo Carvalho de Melo 	unsigned int len;
1411da177e4SLinus Torvalds 
142463c84b9SArnaldo Carvalho de Melo 	icsk->icsk_ack.last_seg_size = 0;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	/* skb->len may jitter because of SACKs, even if peer
1451da177e4SLinus Torvalds 	 * sends good full-sized frames.
1461da177e4SLinus Torvalds 	 */
147ff9b5e0fSHerbert Xu 	len = skb_shinfo(skb)->gso_size ? : skb->len;
148463c84b9SArnaldo Carvalho de Melo 	if (len >= icsk->icsk_ack.rcv_mss) {
149463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.rcv_mss = len;
1501da177e4SLinus Torvalds 	} else {
1511da177e4SLinus Torvalds 		/* Otherwise, we make more careful check taking into account,
1521da177e4SLinus Torvalds 		 * that SACKs block is variable.
1531da177e4SLinus Torvalds 		 *
1541da177e4SLinus Torvalds 		 * "len" is invariant segment length, including TCP header.
1551da177e4SLinus Torvalds 		 */
1569c70220bSArnaldo Carvalho de Melo 		len += skb->data - skb_transport_header(skb);
157bee7ca9eSWilliam Allen Simpson 		if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
1581da177e4SLinus Torvalds 		    /* If PSH is not set, packet should be
1591da177e4SLinus Torvalds 		     * full sized, provided peer TCP is not badly broken.
1601da177e4SLinus Torvalds 		     * This observation (if it is correct 8)) allows
1611da177e4SLinus Torvalds 		     * to handle super-low mtu links fairly.
1621da177e4SLinus Torvalds 		     */
1631da177e4SLinus Torvalds 		    (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
164aa8223c7SArnaldo Carvalho de Melo 		     !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
1651da177e4SLinus Torvalds 			/* Subtract also invariant (if peer is RFC compliant),
1661da177e4SLinus Torvalds 			 * tcp header plus fixed timestamp option length.
1671da177e4SLinus Torvalds 			 * Resulting "len" is MSS free of SACK jitter.
1681da177e4SLinus Torvalds 			 */
169463c84b9SArnaldo Carvalho de Melo 			len -= tcp_sk(sk)->tcp_header_len;
170463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.last_seg_size = len;
1711da177e4SLinus Torvalds 			if (len == lss) {
172463c84b9SArnaldo Carvalho de Melo 				icsk->icsk_ack.rcv_mss = len;
1731da177e4SLinus Torvalds 				return;
1741da177e4SLinus Torvalds 			}
1751da177e4SLinus Torvalds 		}
1761ef9696cSAlexey Kuznetsov 		if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
1771ef9696cSAlexey Kuznetsov 			icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
178463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
1791da177e4SLinus Torvalds 	}
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
182463c84b9SArnaldo Carvalho de Melo static void tcp_incr_quickack(struct sock *sk)
1831da177e4SLinus Torvalds {
184463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
18595c96174SEric Dumazet 	unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	if (quickacks == 0)
1881da177e4SLinus Torvalds 		quickacks = 2;
189463c84b9SArnaldo Carvalho de Melo 	if (quickacks > icsk->icsk_ack.quick)
190463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS);
1911da177e4SLinus Torvalds }
1921da177e4SLinus Torvalds 
1931b9f4092Sstephen hemminger static void tcp_enter_quickack_mode(struct sock *sk)
1941da177e4SLinus Torvalds {
195463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
196463c84b9SArnaldo Carvalho de Melo 	tcp_incr_quickack(sk);
197463c84b9SArnaldo Carvalho de Melo 	icsk->icsk_ack.pingpong = 0;
198463c84b9SArnaldo Carvalho de Melo 	icsk->icsk_ack.ato = TCP_ATO_MIN;
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /* Send ACKs quickly, if "quick" count is not exhausted
2021da177e4SLinus Torvalds  * and the session is not interactive.
2031da177e4SLinus Torvalds  */
2041da177e4SLinus Torvalds 
2052251ae46SJon Maxwell static bool tcp_in_quickack_mode(struct sock *sk)
2061da177e4SLinus Torvalds {
207463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
2082251ae46SJon Maxwell 	const struct dst_entry *dst = __sk_dst_get(sk);
209a2a385d6SEric Dumazet 
2102251ae46SJon Maxwell 	return (dst && dst_metric(dst, RTAX_QUICKACK)) ||
2112251ae46SJon Maxwell 		(icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
214735d3831SFlorian Westphal static void tcp_ecn_queue_cwr(struct tcp_sock *tp)
215bdf1ee5dSIlpo Järvinen {
216bdf1ee5dSIlpo Järvinen 	if (tp->ecn_flags & TCP_ECN_OK)
217bdf1ee5dSIlpo Järvinen 		tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
218bdf1ee5dSIlpo Järvinen }
219bdf1ee5dSIlpo Järvinen 
220735d3831SFlorian Westphal static void tcp_ecn_accept_cwr(struct tcp_sock *tp, const struct sk_buff *skb)
221bdf1ee5dSIlpo Järvinen {
222bdf1ee5dSIlpo Järvinen 	if (tcp_hdr(skb)->cwr)
223bdf1ee5dSIlpo Järvinen 		tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
224bdf1ee5dSIlpo Järvinen }
225bdf1ee5dSIlpo Järvinen 
226735d3831SFlorian Westphal static void tcp_ecn_withdraw_cwr(struct tcp_sock *tp)
227bdf1ee5dSIlpo Järvinen {
228bdf1ee5dSIlpo Järvinen 	tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
229bdf1ee5dSIlpo Järvinen }
230bdf1ee5dSIlpo Järvinen 
231735d3831SFlorian Westphal static void __tcp_ecn_check_ce(struct tcp_sock *tp, const struct sk_buff *skb)
232bdf1ee5dSIlpo Järvinen {
233b82d1bb4SEric Dumazet 	switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
2347a269ffaSEric Dumazet 	case INET_ECN_NOT_ECT:
235bdf1ee5dSIlpo Järvinen 		/* Funny extension: if ECT is not set on a segment,
2367a269ffaSEric Dumazet 		 * and we already seen ECT on a previous segment,
2377a269ffaSEric Dumazet 		 * it is probably a retransmit.
2387a269ffaSEric Dumazet 		 */
2397a269ffaSEric Dumazet 		if (tp->ecn_flags & TCP_ECN_SEEN)
240bdf1ee5dSIlpo Järvinen 			tcp_enter_quickack_mode((struct sock *)tp);
2417a269ffaSEric Dumazet 		break;
2427a269ffaSEric Dumazet 	case INET_ECN_CE:
2439890092eSFlorian Westphal 		if (tcp_ca_needs_ecn((struct sock *)tp))
2449890092eSFlorian Westphal 			tcp_ca_event((struct sock *)tp, CA_EVENT_ECN_IS_CE);
2459890092eSFlorian Westphal 
246aae06bf5SEric Dumazet 		if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) {
247aae06bf5SEric Dumazet 			/* Better not delay acks, sender can have a very low cwnd */
248aae06bf5SEric Dumazet 			tcp_enter_quickack_mode((struct sock *)tp);
2497a269ffaSEric Dumazet 			tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
250aae06bf5SEric Dumazet 		}
2517a269ffaSEric Dumazet 		tp->ecn_flags |= TCP_ECN_SEEN;
2529890092eSFlorian Westphal 		break;
2539890092eSFlorian Westphal 	default:
2549890092eSFlorian Westphal 		if (tcp_ca_needs_ecn((struct sock *)tp))
2559890092eSFlorian Westphal 			tcp_ca_event((struct sock *)tp, CA_EVENT_ECN_NO_CE);
2569890092eSFlorian Westphal 		tp->ecn_flags |= TCP_ECN_SEEN;
2579890092eSFlorian Westphal 		break;
258bdf1ee5dSIlpo Järvinen 	}
259bdf1ee5dSIlpo Järvinen }
260bdf1ee5dSIlpo Järvinen 
261735d3831SFlorian Westphal static void tcp_ecn_check_ce(struct tcp_sock *tp, const struct sk_buff *skb)
262735d3831SFlorian Westphal {
263735d3831SFlorian Westphal 	if (tp->ecn_flags & TCP_ECN_OK)
264735d3831SFlorian Westphal 		__tcp_ecn_check_ce(tp, skb);
265735d3831SFlorian Westphal }
266735d3831SFlorian Westphal 
267735d3831SFlorian Westphal static void tcp_ecn_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)
268bdf1ee5dSIlpo Järvinen {
269bdf1ee5dSIlpo Järvinen 	if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
270bdf1ee5dSIlpo Järvinen 		tp->ecn_flags &= ~TCP_ECN_OK;
271bdf1ee5dSIlpo Järvinen }
272bdf1ee5dSIlpo Järvinen 
273735d3831SFlorian Westphal static void tcp_ecn_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)
274bdf1ee5dSIlpo Järvinen {
275bdf1ee5dSIlpo Järvinen 	if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
276bdf1ee5dSIlpo Järvinen 		tp->ecn_flags &= ~TCP_ECN_OK;
277bdf1ee5dSIlpo Järvinen }
278bdf1ee5dSIlpo Järvinen 
279735d3831SFlorian Westphal static bool tcp_ecn_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)
280bdf1ee5dSIlpo Järvinen {
281bdf1ee5dSIlpo Järvinen 	if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
282a2a385d6SEric Dumazet 		return true;
283a2a385d6SEric Dumazet 	return false;
284bdf1ee5dSIlpo Järvinen }
285bdf1ee5dSIlpo Järvinen 
2861da177e4SLinus Torvalds /* Buffer size and advertised window tuning.
2871da177e4SLinus Torvalds  *
2881da177e4SLinus Torvalds  * 1. Tuning sk->sk_sndbuf, when connection enters established state.
2891da177e4SLinus Torvalds  */
2901da177e4SLinus Torvalds 
2916ae70532SEric Dumazet static void tcp_sndbuf_expand(struct sock *sk)
2921da177e4SLinus Torvalds {
2936ae70532SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
2946ae70532SEric Dumazet 	int sndmem, per_mss;
2956ae70532SEric Dumazet 	u32 nr_segs;
2961da177e4SLinus Torvalds 
2976ae70532SEric Dumazet 	/* Worst case is non GSO/TSO : each frame consumes one skb
2986ae70532SEric Dumazet 	 * and skb->head is kmalloced using power of two area of memory
2996ae70532SEric Dumazet 	 */
3006ae70532SEric Dumazet 	per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) +
3016ae70532SEric Dumazet 		  MAX_TCP_HEADER +
3026ae70532SEric Dumazet 		  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3036ae70532SEric Dumazet 
3046ae70532SEric Dumazet 	per_mss = roundup_pow_of_two(per_mss) +
3056ae70532SEric Dumazet 		  SKB_DATA_ALIGN(sizeof(struct sk_buff));
3066ae70532SEric Dumazet 
3076ae70532SEric Dumazet 	nr_segs = max_t(u32, TCP_INIT_CWND, tp->snd_cwnd);
3086ae70532SEric Dumazet 	nr_segs = max_t(u32, nr_segs, tp->reordering + 1);
3096ae70532SEric Dumazet 
3106ae70532SEric Dumazet 	/* Fast Recovery (RFC 5681 3.2) :
3116ae70532SEric Dumazet 	 * Cubic needs 1.7 factor, rounded to 2 to include
3126ae70532SEric Dumazet 	 * extra cushion (application might react slowly to POLLOUT)
3136ae70532SEric Dumazet 	 */
3146ae70532SEric Dumazet 	sndmem = 2 * nr_segs * per_mss;
3156ae70532SEric Dumazet 
31606a59ecbSEric Dumazet 	if (sk->sk_sndbuf < sndmem)
31706a59ecbSEric Dumazet 		sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds /* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
3211da177e4SLinus Torvalds  *
3221da177e4SLinus Torvalds  * All tcp_full_space() is split to two parts: "network" buffer, allocated
3231da177e4SLinus Torvalds  * forward and advertised in receiver window (tp->rcv_wnd) and
3241da177e4SLinus Torvalds  * "application buffer", required to isolate scheduling/application
3251da177e4SLinus Torvalds  * latencies from network.
3261da177e4SLinus Torvalds  * window_clamp is maximal advertised window. It can be less than
3271da177e4SLinus Torvalds  * tcp_full_space(), in this case tcp_full_space() - window_clamp
3281da177e4SLinus Torvalds  * is reserved for "application" buffer. The less window_clamp is
3291da177e4SLinus Torvalds  * the smoother our behaviour from viewpoint of network, but the lower
3301da177e4SLinus Torvalds  * throughput and the higher sensitivity of the connection to losses. 8)
3311da177e4SLinus Torvalds  *
3321da177e4SLinus Torvalds  * rcv_ssthresh is more strict window_clamp used at "slow start"
3331da177e4SLinus Torvalds  * phase to predict further behaviour of this connection.
3341da177e4SLinus Torvalds  * It is used for two goals:
3351da177e4SLinus Torvalds  * - to enforce header prediction at sender, even when application
3361da177e4SLinus Torvalds  *   requires some significant "application buffer". It is check #1.
3371da177e4SLinus Torvalds  * - to prevent pruning of receive queue because of misprediction
3381da177e4SLinus Torvalds  *   of receiver window. Check #2.
3391da177e4SLinus Torvalds  *
3401da177e4SLinus Torvalds  * The scheme does not work when sender sends good segments opening
341caa20d9aSStephen Hemminger  * window and then starts to feed us spaghetti. But it should work
3421da177e4SLinus Torvalds  * in common situations. Otherwise, we have to rely on queue collapsing.
3431da177e4SLinus Torvalds  */
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds /* Slow part of check#2. */
3469e412ba7SIlpo Järvinen static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
3471da177e4SLinus Torvalds {
3489e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
3491da177e4SLinus Torvalds 	/* Optimize this! */
350dfd4f0aeSEric Dumazet 	int truesize = tcp_win_from_space(skb->truesize) >> 1;
351dfd4f0aeSEric Dumazet 	int window = tcp_win_from_space(sysctl_tcp_rmem[2]) >> 1;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	while (tp->rcv_ssthresh <= window) {
3541da177e4SLinus Torvalds 		if (truesize <= skb->len)
355463c84b9SArnaldo Carvalho de Melo 			return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 		truesize >>= 1;
3581da177e4SLinus Torvalds 		window >>= 1;
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 	return 0;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
363cf533ea5SEric Dumazet static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb)
3641da177e4SLinus Torvalds {
3659e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
3669e412ba7SIlpo Järvinen 
3671da177e4SLinus Torvalds 	/* Check #1 */
3681da177e4SLinus Torvalds 	if (tp->rcv_ssthresh < tp->window_clamp &&
3691da177e4SLinus Torvalds 	    (int)tp->rcv_ssthresh < tcp_space(sk) &&
370b8da51ebSEric Dumazet 	    !tcp_under_memory_pressure(sk)) {
3711da177e4SLinus Torvalds 		int incr;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 		/* Check #2. Increase window, if skb with such overhead
3741da177e4SLinus Torvalds 		 * will fit to rcvbuf in future.
3751da177e4SLinus Torvalds 		 */
3761da177e4SLinus Torvalds 		if (tcp_win_from_space(skb->truesize) <= skb->len)
3771da177e4SLinus Torvalds 			incr = 2 * tp->advmss;
3781da177e4SLinus Torvalds 		else
3799e412ba7SIlpo Järvinen 			incr = __tcp_grow_window(sk, skb);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 		if (incr) {
3824d846f02SEric Dumazet 			incr = max_t(int, incr, 2 * skb->len);
383056834d9SIlpo Järvinen 			tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr,
384056834d9SIlpo Järvinen 					       tp->window_clamp);
385463c84b9SArnaldo Carvalho de Melo 			inet_csk(sk)->icsk_ack.quick |= 1;
3861da177e4SLinus Torvalds 		}
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /* 3. Tuning rcvbuf, when connection enters established state. */
3911da177e4SLinus Torvalds static void tcp_fixup_rcvbuf(struct sock *sk)
3921da177e4SLinus Torvalds {
393e9266a02SEric Dumazet 	u32 mss = tcp_sk(sk)->advmss;
394e9266a02SEric Dumazet 	int rcvmem;
3951da177e4SLinus Torvalds 
39685f16525SYuchung Cheng 	rcvmem = 2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) *
39785f16525SYuchung Cheng 		 tcp_default_init_rwnd(mss);
398e9266a02SEric Dumazet 
399b0983d3cSEric Dumazet 	/* Dynamic Right Sizing (DRS) has 2 to 3 RTT latency
400b0983d3cSEric Dumazet 	 * Allow enough cushion so that sender is not limited by our window
401b0983d3cSEric Dumazet 	 */
402b0983d3cSEric Dumazet 	if (sysctl_tcp_moderate_rcvbuf)
403b0983d3cSEric Dumazet 		rcvmem <<= 2;
404b0983d3cSEric Dumazet 
405e9266a02SEric Dumazet 	if (sk->sk_rcvbuf < rcvmem)
406e9266a02SEric Dumazet 		sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]);
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
409caa20d9aSStephen Hemminger /* 4. Try to fixup all. It is made immediately after connection enters
4101da177e4SLinus Torvalds  *    established state.
4111da177e4SLinus Torvalds  */
41210467163SJerry Chu void tcp_init_buffer_space(struct sock *sk)
4131da177e4SLinus Torvalds {
4141da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
4151da177e4SLinus Torvalds 	int maxwin;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
4181da177e4SLinus Torvalds 		tcp_fixup_rcvbuf(sk);
4191da177e4SLinus Torvalds 	if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
4206ae70532SEric Dumazet 		tcp_sndbuf_expand(sk);
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	tp->rcvq_space.space = tp->rcv_wnd;
423b0983d3cSEric Dumazet 	tp->rcvq_space.time = tcp_time_stamp;
424b0983d3cSEric Dumazet 	tp->rcvq_space.seq = tp->copied_seq;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	maxwin = tcp_full_space(sk);
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	if (tp->window_clamp >= maxwin) {
4291da177e4SLinus Torvalds 		tp->window_clamp = maxwin;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 		if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
4321da177e4SLinus Torvalds 			tp->window_clamp = max(maxwin -
4331da177e4SLinus Torvalds 					       (maxwin >> sysctl_tcp_app_win),
4341da177e4SLinus Torvalds 					       4 * tp->advmss);
4351da177e4SLinus Torvalds 	}
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	/* Force reservation of one segment. */
4381da177e4SLinus Torvalds 	if (sysctl_tcp_app_win &&
4391da177e4SLinus Torvalds 	    tp->window_clamp > 2 * tp->advmss &&
4401da177e4SLinus Torvalds 	    tp->window_clamp + tp->advmss > maxwin)
4411da177e4SLinus Torvalds 		tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
4441da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /* 5. Recalculate window clamp after socket hit its memory bounds. */
4489e412ba7SIlpo Järvinen static void tcp_clamp_window(struct sock *sk)
4491da177e4SLinus Torvalds {
4509e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
4516687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
4521da177e4SLinus Torvalds 
4536687e988SArnaldo Carvalho de Melo 	icsk->icsk_ack.quick = 0;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
4561da177e4SLinus Torvalds 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
457b8da51ebSEric Dumazet 	    !tcp_under_memory_pressure(sk) &&
458180d8cd9SGlauber Costa 	    sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {
4591da177e4SLinus Torvalds 		sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
4601da177e4SLinus Torvalds 				    sysctl_tcp_rmem[2]);
4611da177e4SLinus Torvalds 	}
462326f36e9SJohn Heffner 	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
4631da177e4SLinus Torvalds 		tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
46640efc6faSStephen Hemminger /* Initialize RCV_MSS value.
46740efc6faSStephen Hemminger  * RCV_MSS is an our guess about MSS used by the peer.
46840efc6faSStephen Hemminger  * We haven't any direct information about the MSS.
46940efc6faSStephen Hemminger  * It's better to underestimate the RCV_MSS rather than overestimate.
47040efc6faSStephen Hemminger  * Overestimations make us ACKing less frequently than needed.
47140efc6faSStephen Hemminger  * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
47240efc6faSStephen Hemminger  */
47340efc6faSStephen Hemminger void tcp_initialize_rcv_mss(struct sock *sk)
47440efc6faSStephen Hemminger {
475cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
47640efc6faSStephen Hemminger 	unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
47740efc6faSStephen Hemminger 
47840efc6faSStephen Hemminger 	hint = min(hint, tp->rcv_wnd / 2);
479bee7ca9eSWilliam Allen Simpson 	hint = min(hint, TCP_MSS_DEFAULT);
48040efc6faSStephen Hemminger 	hint = max(hint, TCP_MIN_MSS);
48140efc6faSStephen Hemminger 
48240efc6faSStephen Hemminger 	inet_csk(sk)->icsk_ack.rcv_mss = hint;
48340efc6faSStephen Hemminger }
4844bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_initialize_rcv_mss);
48540efc6faSStephen Hemminger 
4861da177e4SLinus Torvalds /* Receiver "autotuning" code.
4871da177e4SLinus Torvalds  *
4881da177e4SLinus Torvalds  * The algorithm for RTT estimation w/o timestamps is based on
4891da177e4SLinus Torvalds  * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
490631dd1a8SJustin P. Mattock  * <http://public.lanl.gov/radiant/pubs.html#DRS>
4911da177e4SLinus Torvalds  *
4921da177e4SLinus Torvalds  * More detail on this code can be found at
493631dd1a8SJustin P. Mattock  * <http://staff.psc.edu/jheffner/>,
4941da177e4SLinus Torvalds  * though this reference is out of date.  A new paper
4951da177e4SLinus Torvalds  * is pending.
4961da177e4SLinus Torvalds  */
4971da177e4SLinus Torvalds static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	u32 new_sample = tp->rcv_rtt_est.rtt;
5001da177e4SLinus Torvalds 	long m = sample;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	if (m == 0)
5031da177e4SLinus Torvalds 		m = 1;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	if (new_sample != 0) {
5061da177e4SLinus Torvalds 		/* If we sample in larger samples in the non-timestamp
5071da177e4SLinus Torvalds 		 * case, we could grossly overestimate the RTT especially
5081da177e4SLinus Torvalds 		 * with chatty applications or bulk transfer apps which
5091da177e4SLinus Torvalds 		 * are stalled on filesystem I/O.
5101da177e4SLinus Torvalds 		 *
5111da177e4SLinus Torvalds 		 * Also, since we are only going for a minimum in the
51231f34269SStephen Hemminger 		 * non-timestamp case, we do not smooth things out
513caa20d9aSStephen Hemminger 		 * else with timestamps disabled convergence takes too
5141da177e4SLinus Torvalds 		 * long.
5151da177e4SLinus Torvalds 		 */
5161da177e4SLinus Torvalds 		if (!win_dep) {
5171da177e4SLinus Torvalds 			m -= (new_sample >> 3);
5181da177e4SLinus Torvalds 			new_sample += m;
51918a223e0SNeal Cardwell 		} else {
52018a223e0SNeal Cardwell 			m <<= 3;
52118a223e0SNeal Cardwell 			if (m < new_sample)
52218a223e0SNeal Cardwell 				new_sample = m;
52318a223e0SNeal Cardwell 		}
5241da177e4SLinus Torvalds 	} else {
525caa20d9aSStephen Hemminger 		/* No previous measure. */
5261da177e4SLinus Torvalds 		new_sample = m << 3;
5271da177e4SLinus Torvalds 	}
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	if (tp->rcv_rtt_est.rtt != new_sample)
5301da177e4SLinus Torvalds 		tp->rcv_rtt_est.rtt = new_sample;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
5341da177e4SLinus Torvalds {
5351da177e4SLinus Torvalds 	if (tp->rcv_rtt_est.time == 0)
5361da177e4SLinus Torvalds 		goto new_measure;
5371da177e4SLinus Torvalds 	if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
5381da177e4SLinus Torvalds 		return;
539651913ceSNeal Cardwell 	tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rcv_rtt_est.time, 1);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds new_measure:
5421da177e4SLinus Torvalds 	tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
5431da177e4SLinus Torvalds 	tp->rcv_rtt_est.time = tcp_time_stamp;
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
546056834d9SIlpo Järvinen static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
547056834d9SIlpo Järvinen 					  const struct sk_buff *skb)
5481da177e4SLinus Torvalds {
549463c84b9SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
5501da177e4SLinus Torvalds 	if (tp->rx_opt.rcv_tsecr &&
5511da177e4SLinus Torvalds 	    (TCP_SKB_CB(skb)->end_seq -
552463c84b9SArnaldo Carvalho de Melo 	     TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss))
5531da177e4SLinus Torvalds 		tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0);
5541da177e4SLinus Torvalds }
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds /*
5571da177e4SLinus Torvalds  * This function should be called every time data is copied to user space.
5581da177e4SLinus Torvalds  * It calculates the appropriate TCP receive buffer space.
5591da177e4SLinus Torvalds  */
5601da177e4SLinus Torvalds void tcp_rcv_space_adjust(struct sock *sk)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
5631da177e4SLinus Torvalds 	int time;
564b0983d3cSEric Dumazet 	int copied;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	time = tcp_time_stamp - tp->rcvq_space.time;
567056834d9SIlpo Järvinen 	if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)
5681da177e4SLinus Torvalds 		return;
5691da177e4SLinus Torvalds 
570b0983d3cSEric Dumazet 	/* Number of bytes copied to user in last RTT */
571b0983d3cSEric Dumazet 	copied = tp->copied_seq - tp->rcvq_space.seq;
572b0983d3cSEric Dumazet 	if (copied <= tp->rcvq_space.space)
573b0983d3cSEric Dumazet 		goto new_measure;
5741da177e4SLinus Torvalds 
575b0983d3cSEric Dumazet 	/* A bit of theory :
576b0983d3cSEric Dumazet 	 * copied = bytes received in previous RTT, our base window
577b0983d3cSEric Dumazet 	 * To cope with packet losses, we need a 2x factor
578b0983d3cSEric Dumazet 	 * To cope with slow start, and sender growing its cwin by 100 %
579b0983d3cSEric Dumazet 	 * every RTT, we need a 4x factor, because the ACK we are sending
580b0983d3cSEric Dumazet 	 * now is for the next RTT, not the current one :
581b0983d3cSEric Dumazet 	 * <prev RTT . ><current RTT .. ><next RTT .... >
582b0983d3cSEric Dumazet 	 */
5831da177e4SLinus Torvalds 
5846fcf9412SJohn Heffner 	if (sysctl_tcp_moderate_rcvbuf &&
5856fcf9412SJohn Heffner 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
586b0983d3cSEric Dumazet 		int rcvwin, rcvmem, rcvbuf;
5871da177e4SLinus Torvalds 
588b0983d3cSEric Dumazet 		/* minimal window to cope with packet losses, assuming
589b0983d3cSEric Dumazet 		 * steady state. Add some cushion because of small variations.
5901da177e4SLinus Torvalds 		 */
591b0983d3cSEric Dumazet 		rcvwin = (copied << 1) + 16 * tp->advmss;
592b0983d3cSEric Dumazet 
593b0983d3cSEric Dumazet 		/* If rate increased by 25%,
594b0983d3cSEric Dumazet 		 *	assume slow start, rcvwin = 3 * copied
595b0983d3cSEric Dumazet 		 * If rate increased by 50%,
596b0983d3cSEric Dumazet 		 *	assume sender can use 2x growth, rcvwin = 4 * copied
597b0983d3cSEric Dumazet 		 */
598b0983d3cSEric Dumazet 		if (copied >=
599b0983d3cSEric Dumazet 		    tp->rcvq_space.space + (tp->rcvq_space.space >> 2)) {
600b0983d3cSEric Dumazet 			if (copied >=
601b0983d3cSEric Dumazet 			    tp->rcvq_space.space + (tp->rcvq_space.space >> 1))
602b0983d3cSEric Dumazet 				rcvwin <<= 1;
603b0983d3cSEric Dumazet 			else
604b0983d3cSEric Dumazet 				rcvwin += (rcvwin >> 1);
605b0983d3cSEric Dumazet 		}
606b0983d3cSEric Dumazet 
60787fb4b7bSEric Dumazet 		rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
6081da177e4SLinus Torvalds 		while (tcp_win_from_space(rcvmem) < tp->advmss)
6091da177e4SLinus Torvalds 			rcvmem += 128;
610b0983d3cSEric Dumazet 
611b0983d3cSEric Dumazet 		rcvbuf = min(rcvwin / tp->advmss * rcvmem, sysctl_tcp_rmem[2]);
612b0983d3cSEric Dumazet 		if (rcvbuf > sk->sk_rcvbuf) {
613b0983d3cSEric Dumazet 			sk->sk_rcvbuf = rcvbuf;
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 			/* Make the window clamp follow along.  */
616b0983d3cSEric Dumazet 			tp->window_clamp = rcvwin;
6171da177e4SLinus Torvalds 		}
6181da177e4SLinus Torvalds 	}
619b0983d3cSEric Dumazet 	tp->rcvq_space.space = copied;
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds new_measure:
6221da177e4SLinus Torvalds 	tp->rcvq_space.seq = tp->copied_seq;
6231da177e4SLinus Torvalds 	tp->rcvq_space.time = tcp_time_stamp;
6241da177e4SLinus Torvalds }
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds /* There is something which you must keep in mind when you analyze the
6271da177e4SLinus Torvalds  * behavior of the tp->ato delayed ack timeout interval.  When a
6281da177e4SLinus Torvalds  * connection starts up, we want to ack as quickly as possible.  The
6291da177e4SLinus Torvalds  * problem is that "good" TCP's do slow start at the beginning of data
6301da177e4SLinus Torvalds  * transmission.  The means that until we send the first few ACK's the
6311da177e4SLinus Torvalds  * sender will sit on his end and only queue most of his data, because
6321da177e4SLinus Torvalds  * he can only send snd_cwnd unacked packets at any given time.  For
6331da177e4SLinus Torvalds  * each ACK we send, he increments snd_cwnd and transmits more of his
6341da177e4SLinus Torvalds  * queue.  -DaveM
6351da177e4SLinus Torvalds  */
6369e412ba7SIlpo Järvinen static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
6371da177e4SLinus Torvalds {
6389e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
639463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
6401da177e4SLinus Torvalds 	u32 now;
6411da177e4SLinus Torvalds 
642463c84b9SArnaldo Carvalho de Melo 	inet_csk_schedule_ack(sk);
6431da177e4SLinus Torvalds 
644463c84b9SArnaldo Carvalho de Melo 	tcp_measure_rcv_mss(sk, skb);
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	tcp_rcv_rtt_measure(tp);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	now = tcp_time_stamp;
6491da177e4SLinus Torvalds 
650463c84b9SArnaldo Carvalho de Melo 	if (!icsk->icsk_ack.ato) {
6511da177e4SLinus Torvalds 		/* The _first_ data packet received, initialize
6521da177e4SLinus Torvalds 		 * delayed ACK engine.
6531da177e4SLinus Torvalds 		 */
654463c84b9SArnaldo Carvalho de Melo 		tcp_incr_quickack(sk);
655463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.ato = TCP_ATO_MIN;
6561da177e4SLinus Torvalds 	} else {
657463c84b9SArnaldo Carvalho de Melo 		int m = now - icsk->icsk_ack.lrcvtime;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 		if (m <= TCP_ATO_MIN / 2) {
6601da177e4SLinus Torvalds 			/* The fastest case is the first. */
661463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
662463c84b9SArnaldo Carvalho de Melo 		} else if (m < icsk->icsk_ack.ato) {
663463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
664463c84b9SArnaldo Carvalho de Melo 			if (icsk->icsk_ack.ato > icsk->icsk_rto)
665463c84b9SArnaldo Carvalho de Melo 				icsk->icsk_ack.ato = icsk->icsk_rto;
666463c84b9SArnaldo Carvalho de Melo 		} else if (m > icsk->icsk_rto) {
667caa20d9aSStephen Hemminger 			/* Too long gap. Apparently sender failed to
6681da177e4SLinus Torvalds 			 * restart window, so that we send ACKs quickly.
6691da177e4SLinus Torvalds 			 */
670463c84b9SArnaldo Carvalho de Melo 			tcp_incr_quickack(sk);
6713ab224beSHideo Aoki 			sk_mem_reclaim(sk);
6721da177e4SLinus Torvalds 		}
6731da177e4SLinus Torvalds 	}
674463c84b9SArnaldo Carvalho de Melo 	icsk->icsk_ack.lrcvtime = now;
6751da177e4SLinus Torvalds 
676735d3831SFlorian Westphal 	tcp_ecn_check_ce(tp, skb);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	if (skb->len >= 128)
6799e412ba7SIlpo Järvinen 		tcp_grow_window(sk, skb);
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds /* Called to compute a smoothed rtt estimate. The data fed to this
6831da177e4SLinus Torvalds  * routine either comes from timestamps, or from segments that were
6841da177e4SLinus Torvalds  * known _not_ to have been retransmitted [see Karn/Partridge
6851da177e4SLinus Torvalds  * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
6861da177e4SLinus Torvalds  * piece by Van Jacobson.
6871da177e4SLinus Torvalds  * NOTE: the next three routines used to be one big routine.
6881da177e4SLinus Torvalds  * To save cycles in the RFC 1323 implementation it was better to break
6891da177e4SLinus Torvalds  * it up into three procedures. -- erics
6901da177e4SLinus Torvalds  */
691740b0f18SEric Dumazet static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
6921da177e4SLinus Torvalds {
6936687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
694740b0f18SEric Dumazet 	long m = mrtt_us; /* RTT */
695740b0f18SEric Dumazet 	u32 srtt = tp->srtt_us;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	/*	The following amusing code comes from Jacobson's
6981da177e4SLinus Torvalds 	 *	article in SIGCOMM '88.  Note that rtt and mdev
6991da177e4SLinus Torvalds 	 *	are scaled versions of rtt and mean deviation.
7001da177e4SLinus Torvalds 	 *	This is designed to be as fast as possible
7011da177e4SLinus Torvalds 	 *	m stands for "measurement".
7021da177e4SLinus Torvalds 	 *
7031da177e4SLinus Torvalds 	 *	On a 1990 paper the rto value is changed to:
7041da177e4SLinus Torvalds 	 *	RTO = rtt + 4 * mdev
7051da177e4SLinus Torvalds 	 *
7061da177e4SLinus Torvalds 	 * Funny. This algorithm seems to be very broken.
7071da177e4SLinus Torvalds 	 * These formulae increase RTO, when it should be decreased, increase
70831f34269SStephen Hemminger 	 * too slowly, when it should be increased quickly, decrease too quickly
7091da177e4SLinus Torvalds 	 * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
7101da177e4SLinus Torvalds 	 * does not matter how to _calculate_ it. Seems, it was trap
7111da177e4SLinus Torvalds 	 * that VJ failed to avoid. 8)
7121da177e4SLinus Torvalds 	 */
7134a5ab4e2SEric Dumazet 	if (srtt != 0) {
7144a5ab4e2SEric Dumazet 		m -= (srtt >> 3);	/* m is now error in rtt est */
7154a5ab4e2SEric Dumazet 		srtt += m;		/* rtt = 7/8 rtt + 1/8 new */
7161da177e4SLinus Torvalds 		if (m < 0) {
7171da177e4SLinus Torvalds 			m = -m;		/* m is now abs(error) */
718740b0f18SEric Dumazet 			m -= (tp->mdev_us >> 2);   /* similar update on mdev */
7191da177e4SLinus Torvalds 			/* This is similar to one of Eifel findings.
7201da177e4SLinus Torvalds 			 * Eifel blocks mdev updates when rtt decreases.
7211da177e4SLinus Torvalds 			 * This solution is a bit different: we use finer gain
7221da177e4SLinus Torvalds 			 * for mdev in this case (alpha*beta).
7231da177e4SLinus Torvalds 			 * Like Eifel it also prevents growth of rto,
7241da177e4SLinus Torvalds 			 * but also it limits too fast rto decreases,
7251da177e4SLinus Torvalds 			 * happening in pure Eifel.
7261da177e4SLinus Torvalds 			 */
7271da177e4SLinus Torvalds 			if (m > 0)
7281da177e4SLinus Torvalds 				m >>= 3;
7291da177e4SLinus Torvalds 		} else {
730740b0f18SEric Dumazet 			m -= (tp->mdev_us >> 2);   /* similar update on mdev */
7311da177e4SLinus Torvalds 		}
732740b0f18SEric Dumazet 		tp->mdev_us += m;		/* mdev = 3/4 mdev + 1/4 new */
733740b0f18SEric Dumazet 		if (tp->mdev_us > tp->mdev_max_us) {
734740b0f18SEric Dumazet 			tp->mdev_max_us = tp->mdev_us;
735740b0f18SEric Dumazet 			if (tp->mdev_max_us > tp->rttvar_us)
736740b0f18SEric Dumazet 				tp->rttvar_us = tp->mdev_max_us;
7371da177e4SLinus Torvalds 		}
7381da177e4SLinus Torvalds 		if (after(tp->snd_una, tp->rtt_seq)) {
739740b0f18SEric Dumazet 			if (tp->mdev_max_us < tp->rttvar_us)
740740b0f18SEric Dumazet 				tp->rttvar_us -= (tp->rttvar_us - tp->mdev_max_us) >> 2;
7411da177e4SLinus Torvalds 			tp->rtt_seq = tp->snd_nxt;
742740b0f18SEric Dumazet 			tp->mdev_max_us = tcp_rto_min_us(sk);
7431da177e4SLinus Torvalds 		}
7441da177e4SLinus Torvalds 	} else {
7451da177e4SLinus Torvalds 		/* no previous measure. */
7464a5ab4e2SEric Dumazet 		srtt = m << 3;		/* take the measured time to be rtt */
747740b0f18SEric Dumazet 		tp->mdev_us = m << 1;	/* make sure rto = 3*rtt */
748740b0f18SEric Dumazet 		tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk));
749740b0f18SEric Dumazet 		tp->mdev_max_us = tp->rttvar_us;
7501da177e4SLinus Torvalds 		tp->rtt_seq = tp->snd_nxt;
7511da177e4SLinus Torvalds 	}
752740b0f18SEric Dumazet 	tp->srtt_us = max(1U, srtt);
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
75595bd09ebSEric Dumazet /* Set the sk_pacing_rate to allow proper sizing of TSO packets.
75695bd09ebSEric Dumazet  * Note: TCP stack does not yet implement pacing.
75795bd09ebSEric Dumazet  * FQ packet scheduler can be used to implement cheap but effective
75895bd09ebSEric Dumazet  * TCP pacing, to smooth the burst on large writes when packets
75995bd09ebSEric Dumazet  * in flight is significantly lower than cwnd (or rwin)
76095bd09ebSEric Dumazet  */
76143e122b0SEric Dumazet int sysctl_tcp_pacing_ss_ratio __read_mostly = 200;
76243e122b0SEric Dumazet int sysctl_tcp_pacing_ca_ratio __read_mostly = 120;
76343e122b0SEric Dumazet 
76495bd09ebSEric Dumazet static void tcp_update_pacing_rate(struct sock *sk)
76595bd09ebSEric Dumazet {
76695bd09ebSEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
76795bd09ebSEric Dumazet 	u64 rate;
76895bd09ebSEric Dumazet 
76995bd09ebSEric Dumazet 	/* set sk_pacing_rate to 200 % of current rate (mss * cwnd / srtt) */
77043e122b0SEric Dumazet 	rate = (u64)tp->mss_cache * ((USEC_PER_SEC / 100) << 3);
77143e122b0SEric Dumazet 
77243e122b0SEric Dumazet 	/* current rate is (cwnd * mss) / srtt
77343e122b0SEric Dumazet 	 * In Slow Start [1], set sk_pacing_rate to 200 % the current rate.
77443e122b0SEric Dumazet 	 * In Congestion Avoidance phase, set it to 120 % the current rate.
77543e122b0SEric Dumazet 	 *
77643e122b0SEric Dumazet 	 * [1] : Normal Slow Start condition is (tp->snd_cwnd < tp->snd_ssthresh)
77743e122b0SEric Dumazet 	 *	 If snd_cwnd >= (tp->snd_ssthresh / 2), we are approaching
77843e122b0SEric Dumazet 	 *	 end of slow start and should slow down.
77943e122b0SEric Dumazet 	 */
78043e122b0SEric Dumazet 	if (tp->snd_cwnd < tp->snd_ssthresh / 2)
78143e122b0SEric Dumazet 		rate *= sysctl_tcp_pacing_ss_ratio;
78243e122b0SEric Dumazet 	else
78343e122b0SEric Dumazet 		rate *= sysctl_tcp_pacing_ca_ratio;
78495bd09ebSEric Dumazet 
78595bd09ebSEric Dumazet 	rate *= max(tp->snd_cwnd, tp->packets_out);
78695bd09ebSEric Dumazet 
787740b0f18SEric Dumazet 	if (likely(tp->srtt_us))
788740b0f18SEric Dumazet 		do_div(rate, tp->srtt_us);
78995bd09ebSEric Dumazet 
790ba537427SEric Dumazet 	/* ACCESS_ONCE() is needed because sch_fq fetches sk_pacing_rate
791ba537427SEric Dumazet 	 * without any lock. We want to make sure compiler wont store
792ba537427SEric Dumazet 	 * intermediate values in this location.
793ba537427SEric Dumazet 	 */
794ba537427SEric Dumazet 	ACCESS_ONCE(sk->sk_pacing_rate) = min_t(u64, rate,
795ba537427SEric Dumazet 						sk->sk_max_pacing_rate);
79695bd09ebSEric Dumazet }
79795bd09ebSEric Dumazet 
7981da177e4SLinus Torvalds /* Calculate rto without backoff.  This is the second half of Van Jacobson's
7991da177e4SLinus Torvalds  * routine referred to above.
8001da177e4SLinus Torvalds  */
801f7e56a76Sstephen hemminger static void tcp_set_rto(struct sock *sk)
8021da177e4SLinus Torvalds {
803463c84b9SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
8041da177e4SLinus Torvalds 	/* Old crap is replaced with new one. 8)
8051da177e4SLinus Torvalds 	 *
8061da177e4SLinus Torvalds 	 * More seriously:
8071da177e4SLinus Torvalds 	 * 1. If rtt variance happened to be less 50msec, it is hallucination.
8081da177e4SLinus Torvalds 	 *    It cannot be less due to utterly erratic ACK generation made
8091da177e4SLinus Torvalds 	 *    at least by solaris and freebsd. "Erratic ACKs" has _nothing_
8101da177e4SLinus Torvalds 	 *    to do with delayed acks, because at cwnd>2 true delack timeout
8111da177e4SLinus Torvalds 	 *    is invisible. Actually, Linux-2.4 also generates erratic
812caa20d9aSStephen Hemminger 	 *    ACKs in some circumstances.
8131da177e4SLinus Torvalds 	 */
814f1ecd5d9SDamian Lukowski 	inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	/* 2. Fixups made earlier cannot be right.
8171da177e4SLinus Torvalds 	 *    If we do not estimate RTO correctly without them,
8181da177e4SLinus Torvalds 	 *    all the algo is pure shit and should be replaced
819caa20d9aSStephen Hemminger 	 *    with correct one. It is exactly, which we pretend to do.
8201da177e4SLinus Torvalds 	 */
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds 	/* NOTE: clamping at TCP_RTO_MIN is not required, current algo
8231da177e4SLinus Torvalds 	 * guarantees that rto is higher.
8241da177e4SLinus Torvalds 	 */
825f1ecd5d9SDamian Lukowski 	tcp_bound_rto(sk);
8261da177e4SLinus Torvalds }
8271da177e4SLinus Torvalds 
828cf533ea5SEric Dumazet __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)
8291da177e4SLinus Torvalds {
8301da177e4SLinus Torvalds 	__u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
8311da177e4SLinus Torvalds 
83222b71c8fSGerrit Renker 	if (!cwnd)
833442b9635SDavid S. Miller 		cwnd = TCP_INIT_CWND;
8341da177e4SLinus Torvalds 	return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
8351da177e4SLinus Torvalds }
8361da177e4SLinus Torvalds 
837e60402d0SIlpo Järvinen /*
838e60402d0SIlpo Järvinen  * Packet counting of FACK is based on in-order assumptions, therefore TCP
839e60402d0SIlpo Järvinen  * disables it when reordering is detected
840e60402d0SIlpo Järvinen  */
8414aabd8efSDavid S. Miller void tcp_disable_fack(struct tcp_sock *tp)
842e60402d0SIlpo Järvinen {
84385cc391cSIlpo Järvinen 	/* RFC3517 uses different metric in lost marker => reset on change */
84485cc391cSIlpo Järvinen 	if (tcp_is_fack(tp))
84585cc391cSIlpo Järvinen 		tp->lost_skb_hint = NULL;
846ab56222aSVijay Subramanian 	tp->rx_opt.sack_ok &= ~TCP_FACK_ENABLED;
847e60402d0SIlpo Järvinen }
848e60402d0SIlpo Järvinen 
849564262c1SRyousei Takano /* Take a notice that peer is sending D-SACKs */
850e60402d0SIlpo Järvinen static void tcp_dsack_seen(struct tcp_sock *tp)
851e60402d0SIlpo Järvinen {
852ab56222aSVijay Subramanian 	tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;
853e60402d0SIlpo Järvinen }
854e60402d0SIlpo Järvinen 
8556687e988SArnaldo Carvalho de Melo static void tcp_update_reordering(struct sock *sk, const int metric,
8566687e988SArnaldo Carvalho de Melo 				  const int ts)
8571da177e4SLinus Torvalds {
8586687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
8591da177e4SLinus Torvalds 	if (metric > tp->reordering) {
86040b215e5SPavel Emelyanov 		int mib_idx;
86140b215e5SPavel Emelyanov 
862dca145ffSEric Dumazet 		tp->reordering = min(sysctl_tcp_max_reordering, metric);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 		/* This exciting event is worth to be remembered. 8) */
8651da177e4SLinus Torvalds 		if (ts)
86640b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPTSREORDER;
867e60402d0SIlpo Järvinen 		else if (tcp_is_reno(tp))
86840b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPRENOREORDER;
869e60402d0SIlpo Järvinen 		else if (tcp_is_fack(tp))
87040b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPFACKREORDER;
8711da177e4SLinus Torvalds 		else
87240b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPSACKREORDER;
87340b215e5SPavel Emelyanov 
874de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), mib_idx);
8751da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 1
87691df42beSJoe Perches 		pr_debug("Disorder%d %d %u f%u s%u rr%d\n",
8776687e988SArnaldo Carvalho de Melo 			 tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
8781da177e4SLinus Torvalds 			 tp->reordering,
8791da177e4SLinus Torvalds 			 tp->fackets_out,
8801da177e4SLinus Torvalds 			 tp->sacked_out,
8811da177e4SLinus Torvalds 			 tp->undo_marker ? tp->undo_retrans : 0);
8821da177e4SLinus Torvalds #endif
883e60402d0SIlpo Järvinen 		tcp_disable_fack(tp);
8841da177e4SLinus Torvalds 	}
885eed530b6SYuchung Cheng 
886eed530b6SYuchung Cheng 	if (metric > 0)
887eed530b6SYuchung Cheng 		tcp_disable_early_retrans(tp);
8884f41b1c5SYuchung Cheng 	tp->rack.reord = 1;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
891006f582cSIlpo Järvinen /* This must be called before lost_out is incremented */
892c8c213f2SIlpo Järvinen static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
893c8c213f2SIlpo Järvinen {
89451456b29SIan Morris 	if (!tp->retransmit_skb_hint ||
895c8c213f2SIlpo Järvinen 	    before(TCP_SKB_CB(skb)->seq,
896c8c213f2SIlpo Järvinen 		   TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
897006f582cSIlpo Järvinen 		tp->retransmit_skb_hint = skb;
898006f582cSIlpo Järvinen 
899006f582cSIlpo Järvinen 	if (!tp->lost_out ||
900006f582cSIlpo Järvinen 	    after(TCP_SKB_CB(skb)->end_seq, tp->retransmit_high))
901006f582cSIlpo Järvinen 		tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
902c8c213f2SIlpo Järvinen }
903c8c213f2SIlpo Järvinen 
90441ea36e3SIlpo Järvinen static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb)
90541ea36e3SIlpo Järvinen {
90641ea36e3SIlpo Järvinen 	if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
90741ea36e3SIlpo Järvinen 		tcp_verify_retransmit_hint(tp, skb);
90841ea36e3SIlpo Järvinen 
90941ea36e3SIlpo Järvinen 		tp->lost_out += tcp_skb_pcount(skb);
91041ea36e3SIlpo Järvinen 		TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
91141ea36e3SIlpo Järvinen 	}
91241ea36e3SIlpo Järvinen }
91341ea36e3SIlpo Järvinen 
9144f41b1c5SYuchung Cheng void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, struct sk_buff *skb)
915006f582cSIlpo Järvinen {
916006f582cSIlpo Järvinen 	tcp_verify_retransmit_hint(tp, skb);
917006f582cSIlpo Järvinen 
918006f582cSIlpo Järvinen 	if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
919006f582cSIlpo Järvinen 		tp->lost_out += tcp_skb_pcount(skb);
920006f582cSIlpo Järvinen 		TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
921006f582cSIlpo Järvinen 	}
922006f582cSIlpo Järvinen }
923006f582cSIlpo Järvinen 
9241da177e4SLinus Torvalds /* This procedure tags the retransmission queue when SACKs arrive.
9251da177e4SLinus Torvalds  *
9261da177e4SLinus Torvalds  * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
9271da177e4SLinus Torvalds  * Packets in queue with these bits set are counted in variables
9281da177e4SLinus Torvalds  * sacked_out, retrans_out and lost_out, correspondingly.
9291da177e4SLinus Torvalds  *
9301da177e4SLinus Torvalds  * Valid combinations are:
9311da177e4SLinus Torvalds  * Tag  InFlight	Description
9321da177e4SLinus Torvalds  * 0	1		- orig segment is in flight.
9331da177e4SLinus Torvalds  * S	0		- nothing flies, orig reached receiver.
9341da177e4SLinus Torvalds  * L	0		- nothing flies, orig lost by net.
9351da177e4SLinus Torvalds  * R	2		- both orig and retransmit are in flight.
9361da177e4SLinus Torvalds  * L|R	1		- orig is lost, retransmit is in flight.
9371da177e4SLinus Torvalds  * S|R  1		- orig reached receiver, retrans is still in flight.
9381da177e4SLinus Torvalds  * (L|S|R is logically valid, it could occur when L|R is sacked,
9391da177e4SLinus Torvalds  *  but it is equivalent to plain S and code short-curcuits it to S.
9401da177e4SLinus Torvalds  *  L|S is logically invalid, it would mean -1 packet in flight 8))
9411da177e4SLinus Torvalds  *
9421da177e4SLinus Torvalds  * These 6 states form finite state machine, controlled by the following events:
9431da177e4SLinus Torvalds  * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
9441da177e4SLinus Torvalds  * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
945974c1236SYuchung Cheng  * 3. Loss detection event of two flavors:
9461da177e4SLinus Torvalds  *	A. Scoreboard estimator decided the packet is lost.
9471da177e4SLinus Torvalds  *	   A'. Reno "three dupacks" marks head of queue lost.
948974c1236SYuchung Cheng  *	   A''. Its FACK modification, head until snd.fack is lost.
949974c1236SYuchung Cheng  *	B. SACK arrives sacking SND.NXT at the moment, when the
9501da177e4SLinus Torvalds  *	   segment was retransmitted.
9511da177e4SLinus Torvalds  * 4. D-SACK added new rule: D-SACK changes any tag to S.
9521da177e4SLinus Torvalds  *
9531da177e4SLinus Torvalds  * It is pleasant to note, that state diagram turns out to be commutative,
9541da177e4SLinus Torvalds  * so that we are allowed not to be bothered by order of our actions,
9551da177e4SLinus Torvalds  * when multiple events arrive simultaneously. (see the function below).
9561da177e4SLinus Torvalds  *
9571da177e4SLinus Torvalds  * Reordering detection.
9581da177e4SLinus Torvalds  * --------------------
9591da177e4SLinus Torvalds  * Reordering metric is maximal distance, which a packet can be displaced
9601da177e4SLinus Torvalds  * in packet stream. With SACKs we can estimate it:
9611da177e4SLinus Torvalds  *
9621da177e4SLinus Torvalds  * 1. SACK fills old hole and the corresponding segment was not
9631da177e4SLinus Torvalds  *    ever retransmitted -> reordering. Alas, we cannot use it
9641da177e4SLinus Torvalds  *    when segment was retransmitted.
9651da177e4SLinus Torvalds  * 2. The last flaw is solved with D-SACK. D-SACK arrives
9661da177e4SLinus Torvalds  *    for retransmitted and already SACKed segment -> reordering..
9671da177e4SLinus Torvalds  * Both of these heuristics are not used in Loss state, when we cannot
9681da177e4SLinus Torvalds  * account for retransmits accurately.
9695b3c9882SIlpo Järvinen  *
9705b3c9882SIlpo Järvinen  * SACK block validation.
9715b3c9882SIlpo Järvinen  * ----------------------
9725b3c9882SIlpo Järvinen  *
9735b3c9882SIlpo Järvinen  * SACK block range validation checks that the received SACK block fits to
9745b3c9882SIlpo Järvinen  * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
9755b3c9882SIlpo Järvinen  * Note that SND.UNA is not included to the range though being valid because
9760e835331SIlpo Järvinen  * it means that the receiver is rather inconsistent with itself reporting
9770e835331SIlpo Järvinen  * SACK reneging when it should advance SND.UNA. Such SACK block this is
9780e835331SIlpo Järvinen  * perfectly valid, however, in light of RFC2018 which explicitly states
9790e835331SIlpo Järvinen  * that "SACK block MUST reflect the newest segment.  Even if the newest
9800e835331SIlpo Järvinen  * segment is going to be discarded ...", not that it looks very clever
9810e835331SIlpo Järvinen  * in case of head skb. Due to potentional receiver driven attacks, we
9820e835331SIlpo Järvinen  * choose to avoid immediate execution of a walk in write queue due to
9830e835331SIlpo Järvinen  * reneging and defer head skb's loss recovery to standard loss recovery
9840e835331SIlpo Järvinen  * procedure that will eventually trigger (nothing forbids us doing this).
9855b3c9882SIlpo Järvinen  *
9865b3c9882SIlpo Järvinen  * Implements also blockage to start_seq wrap-around. Problem lies in the
9875b3c9882SIlpo Järvinen  * fact that though start_seq (s) is before end_seq (i.e., not reversed),
9885b3c9882SIlpo Järvinen  * there's no guarantee that it will be before snd_nxt (n). The problem
9895b3c9882SIlpo Järvinen  * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
9905b3c9882SIlpo Järvinen  * wrap (s_w):
9915b3c9882SIlpo Järvinen  *
9925b3c9882SIlpo Järvinen  *         <- outs wnd ->                          <- wrapzone ->
9935b3c9882SIlpo Järvinen  *         u     e      n                         u_w   e_w  s n_w
9945b3c9882SIlpo Järvinen  *         |     |      |                          |     |   |  |
9955b3c9882SIlpo Järvinen  * |<------------+------+----- TCP seqno space --------------+---------->|
9965b3c9882SIlpo Järvinen  * ...-- <2^31 ->|                                           |<--------...
9975b3c9882SIlpo Järvinen  * ...---- >2^31 ------>|                                    |<--------...
9985b3c9882SIlpo Järvinen  *
9995b3c9882SIlpo Järvinen  * Current code wouldn't be vulnerable but it's better still to discard such
10005b3c9882SIlpo Järvinen  * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
10015b3c9882SIlpo Järvinen  * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
10025b3c9882SIlpo Järvinen  * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
10035b3c9882SIlpo Järvinen  * equal to the ideal case (infinite seqno space without wrap caused issues).
10045b3c9882SIlpo Järvinen  *
10055b3c9882SIlpo Järvinen  * With D-SACK the lower bound is extended to cover sequence space below
10065b3c9882SIlpo Järvinen  * SND.UNA down to undo_marker, which is the last point of interest. Yet
1007564262c1SRyousei Takano  * again, D-SACK block must not to go across snd_una (for the same reason as
10085b3c9882SIlpo Järvinen  * for the normal SACK blocks, explained above). But there all simplicity
10095b3c9882SIlpo Järvinen  * ends, TCP might receive valid D-SACKs below that. As long as they reside
10105b3c9882SIlpo Järvinen  * fully below undo_marker they do not affect behavior in anyway and can
10115b3c9882SIlpo Järvinen  * therefore be safely ignored. In rare cases (which are more or less
10125b3c9882SIlpo Järvinen  * theoretical ones), the D-SACK will nicely cross that boundary due to skb
10135b3c9882SIlpo Järvinen  * fragmentation and packet reordering past skb's retransmission. To consider
10145b3c9882SIlpo Järvinen  * them correctly, the acceptable range must be extended even more though
10155b3c9882SIlpo Järvinen  * the exact amount is rather hard to quantify. However, tp->max_window can
10165b3c9882SIlpo Järvinen  * be used as an exaggerated estimate.
10171da177e4SLinus Torvalds  */
1018a2a385d6SEric Dumazet static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack,
10195b3c9882SIlpo Järvinen 				   u32 start_seq, u32 end_seq)
10205b3c9882SIlpo Järvinen {
10215b3c9882SIlpo Järvinen 	/* Too far in future, or reversed (interpretation is ambiguous) */
10225b3c9882SIlpo Järvinen 	if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
1023a2a385d6SEric Dumazet 		return false;
10245b3c9882SIlpo Järvinen 
10255b3c9882SIlpo Järvinen 	/* Nasty start_seq wrap-around check (see comments above) */
10265b3c9882SIlpo Järvinen 	if (!before(start_seq, tp->snd_nxt))
1027a2a385d6SEric Dumazet 		return false;
10285b3c9882SIlpo Järvinen 
1029564262c1SRyousei Takano 	/* In outstanding window? ...This is valid exit for D-SACKs too.
10305b3c9882SIlpo Järvinen 	 * start_seq == snd_una is non-sensical (see comments above)
10315b3c9882SIlpo Järvinen 	 */
10325b3c9882SIlpo Järvinen 	if (after(start_seq, tp->snd_una))
1033a2a385d6SEric Dumazet 		return true;
10345b3c9882SIlpo Järvinen 
10355b3c9882SIlpo Järvinen 	if (!is_dsack || !tp->undo_marker)
1036a2a385d6SEric Dumazet 		return false;
10375b3c9882SIlpo Järvinen 
10385b3c9882SIlpo Järvinen 	/* ...Then it's D-SACK, and must reside below snd_una completely */
1039f779b2d6SZheng Yan 	if (after(end_seq, tp->snd_una))
1040a2a385d6SEric Dumazet 		return false;
10415b3c9882SIlpo Järvinen 
10425b3c9882SIlpo Järvinen 	if (!before(start_seq, tp->undo_marker))
1043a2a385d6SEric Dumazet 		return true;
10445b3c9882SIlpo Järvinen 
10455b3c9882SIlpo Järvinen 	/* Too old */
10465b3c9882SIlpo Järvinen 	if (!after(end_seq, tp->undo_marker))
1047a2a385d6SEric Dumazet 		return false;
10485b3c9882SIlpo Järvinen 
10495b3c9882SIlpo Järvinen 	/* Undo_marker boundary crossing (overestimates a lot). Known already:
10505b3c9882SIlpo Järvinen 	 *   start_seq < undo_marker and end_seq >= undo_marker.
10515b3c9882SIlpo Järvinen 	 */
10525b3c9882SIlpo Järvinen 	return !before(start_seq, end_seq - tp->max_window);
10535b3c9882SIlpo Järvinen }
10545b3c9882SIlpo Järvinen 
1055a2a385d6SEric Dumazet static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb,
1056d06e021dSDavid S. Miller 			    struct tcp_sack_block_wire *sp, int num_sacks,
1057d06e021dSDavid S. Miller 			    u32 prior_snd_una)
1058d06e021dSDavid S. Miller {
10591ed83465SPavel Emelyanov 	struct tcp_sock *tp = tcp_sk(sk);
1060d3e2ce3bSHarvey Harrison 	u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
1061d3e2ce3bSHarvey Harrison 	u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
1062a2a385d6SEric Dumazet 	bool dup_sack = false;
1063d06e021dSDavid S. Miller 
1064d06e021dSDavid S. Miller 	if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
1065a2a385d6SEric Dumazet 		dup_sack = true;
1066e60402d0SIlpo Järvinen 		tcp_dsack_seen(tp);
1067de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
1068d06e021dSDavid S. Miller 	} else if (num_sacks > 1) {
1069d3e2ce3bSHarvey Harrison 		u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
1070d3e2ce3bSHarvey Harrison 		u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
1071d06e021dSDavid S. Miller 
1072d06e021dSDavid S. Miller 		if (!after(end_seq_0, end_seq_1) &&
1073d06e021dSDavid S. Miller 		    !before(start_seq_0, start_seq_1)) {
1074a2a385d6SEric Dumazet 			dup_sack = true;
1075e60402d0SIlpo Järvinen 			tcp_dsack_seen(tp);
1076de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk),
1077de0744afSPavel Emelyanov 					LINUX_MIB_TCPDSACKOFORECV);
1078d06e021dSDavid S. Miller 		}
1079d06e021dSDavid S. Miller 	}
1080d06e021dSDavid S. Miller 
1081d06e021dSDavid S. Miller 	/* D-SACK for already forgotten data... Do dumb counting. */
10826e08d5e3SYuchung Cheng 	if (dup_sack && tp->undo_marker && tp->undo_retrans > 0 &&
1083d06e021dSDavid S. Miller 	    !after(end_seq_0, prior_snd_una) &&
1084d06e021dSDavid S. Miller 	    after(end_seq_0, tp->undo_marker))
1085d06e021dSDavid S. Miller 		tp->undo_retrans--;
1086d06e021dSDavid S. Miller 
1087d06e021dSDavid S. Miller 	return dup_sack;
1088d06e021dSDavid S. Miller }
1089d06e021dSDavid S. Miller 
1090a1197f5aSIlpo Järvinen struct tcp_sacktag_state {
1091a1197f5aSIlpo Järvinen 	int	reord;
1092a1197f5aSIlpo Järvinen 	int	fack_count;
109331231a8aSKenneth Klette Jonassen 	/* Timestamps for earliest and latest never-retransmitted segment
109431231a8aSKenneth Klette Jonassen 	 * that was SACKed. RTO needs the earliest RTT to stay conservative,
109531231a8aSKenneth Klette Jonassen 	 * but congestion control should still get an accurate delay signal.
109631231a8aSKenneth Klette Jonassen 	 */
109731231a8aSKenneth Klette Jonassen 	struct skb_mstamp first_sackt;
109831231a8aSKenneth Klette Jonassen 	struct skb_mstamp last_sackt;
1099a1197f5aSIlpo Järvinen 	int	flag;
1100a1197f5aSIlpo Järvinen };
1101a1197f5aSIlpo Järvinen 
1102d1935942SIlpo Järvinen /* Check if skb is fully within the SACK block. In presence of GSO skbs,
1103d1935942SIlpo Järvinen  * the incoming SACK may not exactly match but we can find smaller MSS
1104d1935942SIlpo Järvinen  * aligned portion of it that matches. Therefore we might need to fragment
1105d1935942SIlpo Järvinen  * which may fail and creates some hassle (caller must handle error case
1106d1935942SIlpo Järvinen  * returns).
1107832d11c5SIlpo Järvinen  *
1108832d11c5SIlpo Järvinen  * FIXME: this could be merged to shift decision code
1109d1935942SIlpo Järvinen  */
11100f79efdcSAdrian Bunk static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
1111d1935942SIlpo Järvinen 				  u32 start_seq, u32 end_seq)
1112d1935942SIlpo Järvinen {
1113a2a385d6SEric Dumazet 	int err;
1114a2a385d6SEric Dumazet 	bool in_sack;
1115d1935942SIlpo Järvinen 	unsigned int pkt_len;
1116adb92db8SIlpo Järvinen 	unsigned int mss;
1117d1935942SIlpo Järvinen 
1118d1935942SIlpo Järvinen 	in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1119d1935942SIlpo Järvinen 		  !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1120d1935942SIlpo Järvinen 
1121d1935942SIlpo Järvinen 	if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1122d1935942SIlpo Järvinen 	    after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
1123adb92db8SIlpo Järvinen 		mss = tcp_skb_mss(skb);
1124d1935942SIlpo Järvinen 		in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1125d1935942SIlpo Järvinen 
1126adb92db8SIlpo Järvinen 		if (!in_sack) {
1127d1935942SIlpo Järvinen 			pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
1128adb92db8SIlpo Järvinen 			if (pkt_len < mss)
1129adb92db8SIlpo Järvinen 				pkt_len = mss;
1130adb92db8SIlpo Järvinen 		} else {
1131d1935942SIlpo Järvinen 			pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
1132adb92db8SIlpo Järvinen 			if (pkt_len < mss)
1133adb92db8SIlpo Järvinen 				return -EINVAL;
1134adb92db8SIlpo Järvinen 		}
1135adb92db8SIlpo Järvinen 
1136adb92db8SIlpo Järvinen 		/* Round if necessary so that SACKs cover only full MSSes
1137adb92db8SIlpo Järvinen 		 * and/or the remaining small portion (if present)
1138adb92db8SIlpo Järvinen 		 */
1139adb92db8SIlpo Järvinen 		if (pkt_len > mss) {
1140adb92db8SIlpo Järvinen 			unsigned int new_len = (pkt_len / mss) * mss;
1141adb92db8SIlpo Järvinen 			if (!in_sack && new_len < pkt_len) {
1142adb92db8SIlpo Järvinen 				new_len += mss;
11432cd0d743SNeal Cardwell 				if (new_len >= skb->len)
1144adb92db8SIlpo Järvinen 					return 0;
1145adb92db8SIlpo Järvinen 			}
1146adb92db8SIlpo Järvinen 			pkt_len = new_len;
1147adb92db8SIlpo Järvinen 		}
11486cc55e09SOctavian Purdila 		err = tcp_fragment(sk, skb, pkt_len, mss, GFP_ATOMIC);
1149d1935942SIlpo Järvinen 		if (err < 0)
1150d1935942SIlpo Järvinen 			return err;
1151d1935942SIlpo Järvinen 	}
1152d1935942SIlpo Järvinen 
1153d1935942SIlpo Järvinen 	return in_sack;
1154d1935942SIlpo Järvinen }
1155d1935942SIlpo Järvinen 
1156cc9a672eSNeal Cardwell /* Mark the given newly-SACKed range as such, adjusting counters and hints. */
1157cc9a672eSNeal Cardwell static u8 tcp_sacktag_one(struct sock *sk,
1158cc9a672eSNeal Cardwell 			  struct tcp_sacktag_state *state, u8 sacked,
1159cc9a672eSNeal Cardwell 			  u32 start_seq, u32 end_seq,
1160740b0f18SEric Dumazet 			  int dup_sack, int pcount,
1161740b0f18SEric Dumazet 			  const struct skb_mstamp *xmit_time)
11629e10c47cSIlpo Järvinen {
11636859d494SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
1164a1197f5aSIlpo Järvinen 	int fack_count = state->fack_count;
11659e10c47cSIlpo Järvinen 
11669e10c47cSIlpo Järvinen 	/* Account D-SACK for retransmitted packet. */
11679e10c47cSIlpo Järvinen 	if (dup_sack && (sacked & TCPCB_RETRANS)) {
11686e08d5e3SYuchung Cheng 		if (tp->undo_marker && tp->undo_retrans > 0 &&
1169cc9a672eSNeal Cardwell 		    after(end_seq, tp->undo_marker))
11709e10c47cSIlpo Järvinen 			tp->undo_retrans--;
1171ede9f3b1SIlpo Järvinen 		if (sacked & TCPCB_SACKED_ACKED)
1172a1197f5aSIlpo Järvinen 			state->reord = min(fack_count, state->reord);
11739e10c47cSIlpo Järvinen 	}
11749e10c47cSIlpo Järvinen 
11759e10c47cSIlpo Järvinen 	/* Nothing to do; acked frame is about to be dropped (was ACKed). */
1176cc9a672eSNeal Cardwell 	if (!after(end_seq, tp->snd_una))
1177a1197f5aSIlpo Järvinen 		return sacked;
11789e10c47cSIlpo Järvinen 
11799e10c47cSIlpo Järvinen 	if (!(sacked & TCPCB_SACKED_ACKED)) {
1180659a8ad5SYuchung Cheng 		tcp_rack_advance(tp, xmit_time, sacked);
1181659a8ad5SYuchung Cheng 
11829e10c47cSIlpo Järvinen 		if (sacked & TCPCB_SACKED_RETRANS) {
11839e10c47cSIlpo Järvinen 			/* If the segment is not tagged as lost,
11849e10c47cSIlpo Järvinen 			 * we do not clear RETRANS, believing
11859e10c47cSIlpo Järvinen 			 * that retransmission is still in flight.
11869e10c47cSIlpo Järvinen 			 */
11879e10c47cSIlpo Järvinen 			if (sacked & TCPCB_LOST) {
1188a1197f5aSIlpo Järvinen 				sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
1189f58b22fdSIlpo Järvinen 				tp->lost_out -= pcount;
1190f58b22fdSIlpo Järvinen 				tp->retrans_out -= pcount;
11919e10c47cSIlpo Järvinen 			}
11929e10c47cSIlpo Järvinen 		} else {
11939e10c47cSIlpo Järvinen 			if (!(sacked & TCPCB_RETRANS)) {
11949e10c47cSIlpo Järvinen 				/* New sack for not retransmitted frame,
11959e10c47cSIlpo Järvinen 				 * which was in hole. It is reordering.
11969e10c47cSIlpo Järvinen 				 */
1197cc9a672eSNeal Cardwell 				if (before(start_seq,
11989e10c47cSIlpo Järvinen 					   tcp_highest_sack_seq(tp)))
1199a1197f5aSIlpo Järvinen 					state->reord = min(fack_count,
1200a1197f5aSIlpo Järvinen 							   state->reord);
1201e33099f9SYuchung Cheng 				if (!after(end_seq, tp->high_seq))
1202e33099f9SYuchung Cheng 					state->flag |= FLAG_ORIG_SACK_ACKED;
120331231a8aSKenneth Klette Jonassen 				if (state->first_sackt.v64 == 0)
120431231a8aSKenneth Klette Jonassen 					state->first_sackt = *xmit_time;
120531231a8aSKenneth Klette Jonassen 				state->last_sackt = *xmit_time;
12069e10c47cSIlpo Järvinen 			}
12079e10c47cSIlpo Järvinen 
12089e10c47cSIlpo Järvinen 			if (sacked & TCPCB_LOST) {
1209a1197f5aSIlpo Järvinen 				sacked &= ~TCPCB_LOST;
1210f58b22fdSIlpo Järvinen 				tp->lost_out -= pcount;
12119e10c47cSIlpo Järvinen 			}
12129e10c47cSIlpo Järvinen 		}
12139e10c47cSIlpo Järvinen 
1214a1197f5aSIlpo Järvinen 		sacked |= TCPCB_SACKED_ACKED;
1215a1197f5aSIlpo Järvinen 		state->flag |= FLAG_DATA_SACKED;
1216f58b22fdSIlpo Järvinen 		tp->sacked_out += pcount;
1217ddf1af6fSYuchung Cheng 		tp->delivered += pcount;  /* Out-of-order packets delivered */
12189e10c47cSIlpo Järvinen 
1219f58b22fdSIlpo Järvinen 		fack_count += pcount;
12209e10c47cSIlpo Järvinen 
12219e10c47cSIlpo Järvinen 		/* Lost marker hint past SACKed? Tweak RFC3517 cnt */
122200db4124SIan Morris 		if (!tcp_is_fack(tp) && tp->lost_skb_hint &&
1223cc9a672eSNeal Cardwell 		    before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))
1224f58b22fdSIlpo Järvinen 			tp->lost_cnt_hint += pcount;
12259e10c47cSIlpo Järvinen 
12269e10c47cSIlpo Järvinen 		if (fack_count > tp->fackets_out)
12279e10c47cSIlpo Järvinen 			tp->fackets_out = fack_count;
12289e10c47cSIlpo Järvinen 	}
12299e10c47cSIlpo Järvinen 
12309e10c47cSIlpo Järvinen 	/* D-SACK. We can detect redundant retransmission in S|R and plain R
12319e10c47cSIlpo Järvinen 	 * frames and clear it. undo_retrans is decreased above, L|R frames
12329e10c47cSIlpo Järvinen 	 * are accounted above as well.
12339e10c47cSIlpo Järvinen 	 */
1234a1197f5aSIlpo Järvinen 	if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
1235a1197f5aSIlpo Järvinen 		sacked &= ~TCPCB_SACKED_RETRANS;
1236f58b22fdSIlpo Järvinen 		tp->retrans_out -= pcount;
12379e10c47cSIlpo Järvinen 	}
12389e10c47cSIlpo Järvinen 
1239a1197f5aSIlpo Järvinen 	return sacked;
12409e10c47cSIlpo Järvinen }
12419e10c47cSIlpo Järvinen 
1242daef52baSNeal Cardwell /* Shift newly-SACKed bytes from this skb to the immediately previous
1243daef52baSNeal Cardwell  * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
1244daef52baSNeal Cardwell  */
1245a2a385d6SEric Dumazet static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,
1246a1197f5aSIlpo Järvinen 			    struct tcp_sacktag_state *state,
12479ec06ff5SIlpo Järvinen 			    unsigned int pcount, int shifted, int mss,
1248a2a385d6SEric Dumazet 			    bool dup_sack)
1249832d11c5SIlpo Järvinen {
1250832d11c5SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
125150133161SIlpo Järvinen 	struct sk_buff *prev = tcp_write_queue_prev(sk, skb);
1252daef52baSNeal Cardwell 	u32 start_seq = TCP_SKB_CB(skb)->seq;	/* start of newly-SACKed */
1253daef52baSNeal Cardwell 	u32 end_seq = start_seq + shifted;	/* end of newly-SACKed */
1254832d11c5SIlpo Järvinen 
1255832d11c5SIlpo Järvinen 	BUG_ON(!pcount);
1256832d11c5SIlpo Järvinen 
12574c90d3b3SNeal Cardwell 	/* Adjust counters and hints for the newly sacked sequence
12584c90d3b3SNeal Cardwell 	 * range but discard the return value since prev is already
12594c90d3b3SNeal Cardwell 	 * marked. We must tag the range first because the seq
12604c90d3b3SNeal Cardwell 	 * advancement below implicitly advances
12614c90d3b3SNeal Cardwell 	 * tcp_highest_sack_seq() when skb is highest_sack.
12624c90d3b3SNeal Cardwell 	 */
12634c90d3b3SNeal Cardwell 	tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked,
126459c9af42SYuchung Cheng 			start_seq, end_seq, dup_sack, pcount,
1265740b0f18SEric Dumazet 			&skb->skb_mstamp);
12664c90d3b3SNeal Cardwell 
12674c90d3b3SNeal Cardwell 	if (skb == tp->lost_skb_hint)
12680af2a0d0SNeal Cardwell 		tp->lost_cnt_hint += pcount;
12690af2a0d0SNeal Cardwell 
1270832d11c5SIlpo Järvinen 	TCP_SKB_CB(prev)->end_seq += shifted;
1271832d11c5SIlpo Järvinen 	TCP_SKB_CB(skb)->seq += shifted;
1272832d11c5SIlpo Järvinen 
1273cd7d8498SEric Dumazet 	tcp_skb_pcount_add(prev, pcount);
1274cd7d8498SEric Dumazet 	BUG_ON(tcp_skb_pcount(skb) < pcount);
1275cd7d8498SEric Dumazet 	tcp_skb_pcount_add(skb, -pcount);
1276832d11c5SIlpo Järvinen 
1277832d11c5SIlpo Järvinen 	/* When we're adding to gso_segs == 1, gso_size will be zero,
1278832d11c5SIlpo Järvinen 	 * in theory this shouldn't be necessary but as long as DSACK
1279832d11c5SIlpo Järvinen 	 * code can come after this skb later on it's better to keep
1280832d11c5SIlpo Järvinen 	 * setting gso_size to something.
1281832d11c5SIlpo Järvinen 	 */
1282f69ad292SEric Dumazet 	if (!TCP_SKB_CB(prev)->tcp_gso_size)
1283f69ad292SEric Dumazet 		TCP_SKB_CB(prev)->tcp_gso_size = mss;
1284832d11c5SIlpo Järvinen 
1285832d11c5SIlpo Järvinen 	/* CHECKME: To clear or not to clear? Mimics normal skb currently */
128651466a75SEric Dumazet 	if (tcp_skb_pcount(skb) <= 1)
1287f69ad292SEric Dumazet 		TCP_SKB_CB(skb)->tcp_gso_size = 0;
1288832d11c5SIlpo Järvinen 
1289832d11c5SIlpo Järvinen 	/* Difference in this won't matter, both ACKed by the same cumul. ACK */
1290832d11c5SIlpo Järvinen 	TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1291832d11c5SIlpo Järvinen 
1292832d11c5SIlpo Järvinen 	if (skb->len > 0) {
1293832d11c5SIlpo Järvinen 		BUG_ON(!tcp_skb_pcount(skb));
1294111cc8b9SIlpo Järvinen 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTED);
1295a2a385d6SEric Dumazet 		return false;
1296832d11c5SIlpo Järvinen 	}
1297832d11c5SIlpo Järvinen 
1298832d11c5SIlpo Järvinen 	/* Whole SKB was eaten :-) */
1299832d11c5SIlpo Järvinen 
130092ee76b6SIlpo Järvinen 	if (skb == tp->retransmit_skb_hint)
130192ee76b6SIlpo Järvinen 		tp->retransmit_skb_hint = prev;
130292ee76b6SIlpo Järvinen 	if (skb == tp->lost_skb_hint) {
130392ee76b6SIlpo Järvinen 		tp->lost_skb_hint = prev;
130492ee76b6SIlpo Järvinen 		tp->lost_cnt_hint -= tcp_skb_pcount(prev);
130592ee76b6SIlpo Järvinen 	}
130692ee76b6SIlpo Järvinen 
13075e8a402fSEric Dumazet 	TCP_SKB_CB(prev)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
13085e8a402fSEric Dumazet 	if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
13095e8a402fSEric Dumazet 		TCP_SKB_CB(prev)->end_seq++;
13105e8a402fSEric Dumazet 
1311832d11c5SIlpo Järvinen 	if (skb == tcp_highest_sack(sk))
1312832d11c5SIlpo Järvinen 		tcp_advance_highest_sack(sk, skb);
1313832d11c5SIlpo Järvinen 
1314832d11c5SIlpo Järvinen 	tcp_unlink_write_queue(skb, sk);
1315832d11c5SIlpo Järvinen 	sk_wmem_free_skb(sk, skb);
1316832d11c5SIlpo Järvinen 
1317111cc8b9SIlpo Järvinen 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKMERGED);
1318111cc8b9SIlpo Järvinen 
1319a2a385d6SEric Dumazet 	return true;
1320832d11c5SIlpo Järvinen }
1321832d11c5SIlpo Järvinen 
1322832d11c5SIlpo Järvinen /* I wish gso_size would have a bit more sane initialization than
1323832d11c5SIlpo Järvinen  * something-or-zero which complicates things
1324832d11c5SIlpo Järvinen  */
1325cf533ea5SEric Dumazet static int tcp_skb_seglen(const struct sk_buff *skb)
1326832d11c5SIlpo Järvinen {
1327775ffabfSIlpo Järvinen 	return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
1328832d11c5SIlpo Järvinen }
1329832d11c5SIlpo Järvinen 
1330832d11c5SIlpo Järvinen /* Shifting pages past head area doesn't work */
1331cf533ea5SEric Dumazet static int skb_can_shift(const struct sk_buff *skb)
1332832d11c5SIlpo Järvinen {
1333832d11c5SIlpo Järvinen 	return !skb_headlen(skb) && skb_is_nonlinear(skb);
1334832d11c5SIlpo Järvinen }
1335832d11c5SIlpo Järvinen 
1336832d11c5SIlpo Järvinen /* Try collapsing SACK blocks spanning across multiple skbs to a single
1337832d11c5SIlpo Järvinen  * skb.
1338832d11c5SIlpo Järvinen  */
1339832d11c5SIlpo Järvinen static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
1340a1197f5aSIlpo Järvinen 					  struct tcp_sacktag_state *state,
1341832d11c5SIlpo Järvinen 					  u32 start_seq, u32 end_seq,
1342a2a385d6SEric Dumazet 					  bool dup_sack)
1343832d11c5SIlpo Järvinen {
1344832d11c5SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
1345832d11c5SIlpo Järvinen 	struct sk_buff *prev;
1346832d11c5SIlpo Järvinen 	int mss;
1347832d11c5SIlpo Järvinen 	int pcount = 0;
1348832d11c5SIlpo Järvinen 	int len;
1349832d11c5SIlpo Järvinen 	int in_sack;
1350832d11c5SIlpo Järvinen 
1351832d11c5SIlpo Järvinen 	if (!sk_can_gso(sk))
1352832d11c5SIlpo Järvinen 		goto fallback;
1353832d11c5SIlpo Järvinen 
1354832d11c5SIlpo Järvinen 	/* Normally R but no L won't result in plain S */
1355832d11c5SIlpo Järvinen 	if (!dup_sack &&
13569969ca5fSIlpo Järvinen 	    (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
1357832d11c5SIlpo Järvinen 		goto fallback;
1358832d11c5SIlpo Järvinen 	if (!skb_can_shift(skb))
1359832d11c5SIlpo Järvinen 		goto fallback;
1360832d11c5SIlpo Järvinen 	/* This frame is about to be dropped (was ACKed). */
1361832d11c5SIlpo Järvinen 	if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1362832d11c5SIlpo Järvinen 		goto fallback;
1363832d11c5SIlpo Järvinen 
1364832d11c5SIlpo Järvinen 	/* Can only happen with delayed DSACK + discard craziness */
1365832d11c5SIlpo Järvinen 	if (unlikely(skb == tcp_write_queue_head(sk)))
1366832d11c5SIlpo Järvinen 		goto fallback;
1367832d11c5SIlpo Järvinen 	prev = tcp_write_queue_prev(sk, skb);
1368832d11c5SIlpo Järvinen 
1369832d11c5SIlpo Järvinen 	if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1370832d11c5SIlpo Järvinen 		goto fallback;
1371832d11c5SIlpo Järvinen 
1372832d11c5SIlpo Järvinen 	in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1373832d11c5SIlpo Järvinen 		  !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1374832d11c5SIlpo Järvinen 
1375832d11c5SIlpo Järvinen 	if (in_sack) {
1376832d11c5SIlpo Järvinen 		len = skb->len;
1377832d11c5SIlpo Järvinen 		pcount = tcp_skb_pcount(skb);
1378775ffabfSIlpo Järvinen 		mss = tcp_skb_seglen(skb);
1379832d11c5SIlpo Järvinen 
1380832d11c5SIlpo Järvinen 		/* TODO: Fix DSACKs to not fragment already SACKed and we can
1381832d11c5SIlpo Järvinen 		 * drop this restriction as unnecessary
1382832d11c5SIlpo Järvinen 		 */
1383775ffabfSIlpo Järvinen 		if (mss != tcp_skb_seglen(prev))
1384832d11c5SIlpo Järvinen 			goto fallback;
1385832d11c5SIlpo Järvinen 	} else {
1386832d11c5SIlpo Järvinen 		if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1387832d11c5SIlpo Järvinen 			goto noop;
1388832d11c5SIlpo Järvinen 		/* CHECKME: This is non-MSS split case only?, this will
1389832d11c5SIlpo Järvinen 		 * cause skipped skbs due to advancing loop btw, original
1390832d11c5SIlpo Järvinen 		 * has that feature too
1391832d11c5SIlpo Järvinen 		 */
1392832d11c5SIlpo Järvinen 		if (tcp_skb_pcount(skb) <= 1)
1393832d11c5SIlpo Järvinen 			goto noop;
1394832d11c5SIlpo Järvinen 
1395832d11c5SIlpo Järvinen 		in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1396832d11c5SIlpo Järvinen 		if (!in_sack) {
1397832d11c5SIlpo Järvinen 			/* TODO: head merge to next could be attempted here
1398832d11c5SIlpo Järvinen 			 * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1399832d11c5SIlpo Järvinen 			 * though it might not be worth of the additional hassle
1400832d11c5SIlpo Järvinen 			 *
1401832d11c5SIlpo Järvinen 			 * ...we can probably just fallback to what was done
1402832d11c5SIlpo Järvinen 			 * previously. We could try merging non-SACKed ones
1403832d11c5SIlpo Järvinen 			 * as well but it probably isn't going to buy off
1404832d11c5SIlpo Järvinen 			 * because later SACKs might again split them, and
1405832d11c5SIlpo Järvinen 			 * it would make skb timestamp tracking considerably
1406832d11c5SIlpo Järvinen 			 * harder problem.
1407832d11c5SIlpo Järvinen 			 */
1408832d11c5SIlpo Järvinen 			goto fallback;
1409832d11c5SIlpo Järvinen 		}
1410832d11c5SIlpo Järvinen 
1411832d11c5SIlpo Järvinen 		len = end_seq - TCP_SKB_CB(skb)->seq;
1412832d11c5SIlpo Järvinen 		BUG_ON(len < 0);
1413832d11c5SIlpo Järvinen 		BUG_ON(len > skb->len);
1414832d11c5SIlpo Järvinen 
1415832d11c5SIlpo Järvinen 		/* MSS boundaries should be honoured or else pcount will
1416832d11c5SIlpo Järvinen 		 * severely break even though it makes things bit trickier.
1417832d11c5SIlpo Järvinen 		 * Optimize common case to avoid most of the divides
1418832d11c5SIlpo Järvinen 		 */
1419832d11c5SIlpo Järvinen 		mss = tcp_skb_mss(skb);
1420832d11c5SIlpo Järvinen 
1421832d11c5SIlpo Järvinen 		/* TODO: Fix DSACKs to not fragment already SACKed and we can
1422832d11c5SIlpo Järvinen 		 * drop this restriction as unnecessary
1423832d11c5SIlpo Järvinen 		 */
1424775ffabfSIlpo Järvinen 		if (mss != tcp_skb_seglen(prev))
1425832d11c5SIlpo Järvinen 			goto fallback;
1426832d11c5SIlpo Järvinen 
1427832d11c5SIlpo Järvinen 		if (len == mss) {
1428832d11c5SIlpo Järvinen 			pcount = 1;
1429832d11c5SIlpo Järvinen 		} else if (len < mss) {
1430832d11c5SIlpo Järvinen 			goto noop;
1431832d11c5SIlpo Järvinen 		} else {
1432832d11c5SIlpo Järvinen 			pcount = len / mss;
1433832d11c5SIlpo Järvinen 			len = pcount * mss;
1434832d11c5SIlpo Järvinen 		}
1435832d11c5SIlpo Järvinen 	}
1436832d11c5SIlpo Järvinen 
14374648dc97SNeal Cardwell 	/* tcp_sacktag_one() won't SACK-tag ranges below snd_una */
14384648dc97SNeal Cardwell 	if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
14394648dc97SNeal Cardwell 		goto fallback;
14404648dc97SNeal Cardwell 
1441832d11c5SIlpo Järvinen 	if (!skb_shift(prev, skb, len))
1442832d11c5SIlpo Järvinen 		goto fallback;
14439ec06ff5SIlpo Järvinen 	if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack))
1444832d11c5SIlpo Järvinen 		goto out;
1445832d11c5SIlpo Järvinen 
1446832d11c5SIlpo Järvinen 	/* Hole filled allows collapsing with the next as well, this is very
1447832d11c5SIlpo Järvinen 	 * useful when hole on every nth skb pattern happens
1448832d11c5SIlpo Järvinen 	 */
1449832d11c5SIlpo Järvinen 	if (prev == tcp_write_queue_tail(sk))
1450832d11c5SIlpo Järvinen 		goto out;
1451832d11c5SIlpo Järvinen 	skb = tcp_write_queue_next(sk, prev);
1452832d11c5SIlpo Järvinen 
1453f0bc52f3SIlpo Järvinen 	if (!skb_can_shift(skb) ||
1454f0bc52f3SIlpo Järvinen 	    (skb == tcp_send_head(sk)) ||
1455f0bc52f3SIlpo Järvinen 	    ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
1456775ffabfSIlpo Järvinen 	    (mss != tcp_skb_seglen(skb)))
1457832d11c5SIlpo Järvinen 		goto out;
1458832d11c5SIlpo Järvinen 
1459832d11c5SIlpo Järvinen 	len = skb->len;
1460832d11c5SIlpo Järvinen 	if (skb_shift(prev, skb, len)) {
1461832d11c5SIlpo Järvinen 		pcount += tcp_skb_pcount(skb);
14629ec06ff5SIlpo Järvinen 		tcp_shifted_skb(sk, skb, state, tcp_skb_pcount(skb), len, mss, 0);
1463832d11c5SIlpo Järvinen 	}
1464832d11c5SIlpo Järvinen 
1465832d11c5SIlpo Järvinen out:
1466a1197f5aSIlpo Järvinen 	state->fack_count += pcount;
1467832d11c5SIlpo Järvinen 	return prev;
1468832d11c5SIlpo Järvinen 
1469832d11c5SIlpo Järvinen noop:
1470832d11c5SIlpo Järvinen 	return skb;
1471832d11c5SIlpo Järvinen 
1472832d11c5SIlpo Järvinen fallback:
1473111cc8b9SIlpo Järvinen 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
1474832d11c5SIlpo Järvinen 	return NULL;
1475832d11c5SIlpo Järvinen }
1476832d11c5SIlpo Järvinen 
147768f8353bSIlpo Järvinen static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
147868f8353bSIlpo Järvinen 					struct tcp_sack_block *next_dup,
1479a1197f5aSIlpo Järvinen 					struct tcp_sacktag_state *state,
148068f8353bSIlpo Järvinen 					u32 start_seq, u32 end_seq,
1481a2a385d6SEric Dumazet 					bool dup_sack_in)
148268f8353bSIlpo Järvinen {
1483832d11c5SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
1484832d11c5SIlpo Järvinen 	struct sk_buff *tmp;
1485832d11c5SIlpo Järvinen 
148668f8353bSIlpo Järvinen 	tcp_for_write_queue_from(skb, sk) {
148768f8353bSIlpo Järvinen 		int in_sack = 0;
1488a2a385d6SEric Dumazet 		bool dup_sack = dup_sack_in;
148968f8353bSIlpo Järvinen 
149068f8353bSIlpo Järvinen 		if (skb == tcp_send_head(sk))
149168f8353bSIlpo Järvinen 			break;
149268f8353bSIlpo Järvinen 
149368f8353bSIlpo Järvinen 		/* queue is in-order => we can short-circuit the walk early */
149468f8353bSIlpo Järvinen 		if (!before(TCP_SKB_CB(skb)->seq, end_seq))
149568f8353bSIlpo Järvinen 			break;
149668f8353bSIlpo Järvinen 
149700db4124SIan Morris 		if (next_dup  &&
149868f8353bSIlpo Järvinen 		    before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
149968f8353bSIlpo Järvinen 			in_sack = tcp_match_skb_to_sack(sk, skb,
150068f8353bSIlpo Järvinen 							next_dup->start_seq,
150168f8353bSIlpo Järvinen 							next_dup->end_seq);
150268f8353bSIlpo Järvinen 			if (in_sack > 0)
1503a2a385d6SEric Dumazet 				dup_sack = true;
150468f8353bSIlpo Järvinen 		}
150568f8353bSIlpo Järvinen 
1506832d11c5SIlpo Järvinen 		/* skb reference here is a bit tricky to get right, since
1507832d11c5SIlpo Järvinen 		 * shifting can eat and free both this skb and the next,
1508832d11c5SIlpo Järvinen 		 * so not even _safe variant of the loop is enough.
1509832d11c5SIlpo Järvinen 		 */
1510832d11c5SIlpo Järvinen 		if (in_sack <= 0) {
1511a1197f5aSIlpo Järvinen 			tmp = tcp_shift_skb_data(sk, skb, state,
1512a1197f5aSIlpo Järvinen 						 start_seq, end_seq, dup_sack);
151300db4124SIan Morris 			if (tmp) {
1514832d11c5SIlpo Järvinen 				if (tmp != skb) {
1515832d11c5SIlpo Järvinen 					skb = tmp;
1516832d11c5SIlpo Järvinen 					continue;
1517832d11c5SIlpo Järvinen 				}
1518832d11c5SIlpo Järvinen 
1519832d11c5SIlpo Järvinen 				in_sack = 0;
1520832d11c5SIlpo Järvinen 			} else {
1521832d11c5SIlpo Järvinen 				in_sack = tcp_match_skb_to_sack(sk, skb,
1522832d11c5SIlpo Järvinen 								start_seq,
1523056834d9SIlpo Järvinen 								end_seq);
1524832d11c5SIlpo Järvinen 			}
1525832d11c5SIlpo Järvinen 		}
1526832d11c5SIlpo Järvinen 
152768f8353bSIlpo Järvinen 		if (unlikely(in_sack < 0))
152868f8353bSIlpo Järvinen 			break;
152968f8353bSIlpo Järvinen 
1530832d11c5SIlpo Järvinen 		if (in_sack) {
1531cc9a672eSNeal Cardwell 			TCP_SKB_CB(skb)->sacked =
1532cc9a672eSNeal Cardwell 				tcp_sacktag_one(sk,
1533a1197f5aSIlpo Järvinen 						state,
1534cc9a672eSNeal Cardwell 						TCP_SKB_CB(skb)->sacked,
1535cc9a672eSNeal Cardwell 						TCP_SKB_CB(skb)->seq,
1536cc9a672eSNeal Cardwell 						TCP_SKB_CB(skb)->end_seq,
1537a1197f5aSIlpo Järvinen 						dup_sack,
153859c9af42SYuchung Cheng 						tcp_skb_pcount(skb),
1539740b0f18SEric Dumazet 						&skb->skb_mstamp);
154068f8353bSIlpo Järvinen 
1541832d11c5SIlpo Järvinen 			if (!before(TCP_SKB_CB(skb)->seq,
1542832d11c5SIlpo Järvinen 				    tcp_highest_sack_seq(tp)))
1543832d11c5SIlpo Järvinen 				tcp_advance_highest_sack(sk, skb);
1544832d11c5SIlpo Järvinen 		}
1545832d11c5SIlpo Järvinen 
1546a1197f5aSIlpo Järvinen 		state->fack_count += tcp_skb_pcount(skb);
154768f8353bSIlpo Järvinen 	}
154868f8353bSIlpo Järvinen 	return skb;
154968f8353bSIlpo Järvinen }
155068f8353bSIlpo Järvinen 
155168f8353bSIlpo Järvinen /* Avoid all extra work that is being done by sacktag while walking in
155268f8353bSIlpo Järvinen  * a normal way
155368f8353bSIlpo Järvinen  */
155468f8353bSIlpo Järvinen static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
1555a1197f5aSIlpo Järvinen 					struct tcp_sacktag_state *state,
1556a1197f5aSIlpo Järvinen 					u32 skip_to_seq)
155768f8353bSIlpo Järvinen {
155868f8353bSIlpo Järvinen 	tcp_for_write_queue_from(skb, sk) {
155968f8353bSIlpo Järvinen 		if (skb == tcp_send_head(sk))
156068f8353bSIlpo Järvinen 			break;
156168f8353bSIlpo Järvinen 
1562e8bae275SIlpo Järvinen 		if (after(TCP_SKB_CB(skb)->end_seq, skip_to_seq))
156368f8353bSIlpo Järvinen 			break;
1564d152a7d8SIlpo Järvinen 
1565a1197f5aSIlpo Järvinen 		state->fack_count += tcp_skb_pcount(skb);
156668f8353bSIlpo Järvinen 	}
156768f8353bSIlpo Järvinen 	return skb;
156868f8353bSIlpo Järvinen }
156968f8353bSIlpo Järvinen 
157068f8353bSIlpo Järvinen static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
157168f8353bSIlpo Järvinen 						struct sock *sk,
157268f8353bSIlpo Järvinen 						struct tcp_sack_block *next_dup,
1573a1197f5aSIlpo Järvinen 						struct tcp_sacktag_state *state,
1574a1197f5aSIlpo Järvinen 						u32 skip_to_seq)
157568f8353bSIlpo Järvinen {
157651456b29SIan Morris 	if (!next_dup)
157768f8353bSIlpo Järvinen 		return skb;
157868f8353bSIlpo Järvinen 
157968f8353bSIlpo Järvinen 	if (before(next_dup->start_seq, skip_to_seq)) {
1580a1197f5aSIlpo Järvinen 		skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
1581a1197f5aSIlpo Järvinen 		skb = tcp_sacktag_walk(skb, sk, NULL, state,
158268f8353bSIlpo Järvinen 				       next_dup->start_seq, next_dup->end_seq,
1583a1197f5aSIlpo Järvinen 				       1);
158468f8353bSIlpo Järvinen 	}
158568f8353bSIlpo Järvinen 
158668f8353bSIlpo Järvinen 	return skb;
158768f8353bSIlpo Järvinen }
158868f8353bSIlpo Järvinen 
1589cf533ea5SEric Dumazet static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
159068f8353bSIlpo Järvinen {
159168f8353bSIlpo Järvinen 	return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
159268f8353bSIlpo Järvinen }
159368f8353bSIlpo Järvinen 
15941da177e4SLinus Torvalds static int
1595cf533ea5SEric Dumazet tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
1596196da974SKenneth Klette Jonassen 			u32 prior_snd_una, struct tcp_sacktag_state *state)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
1599cf533ea5SEric Dumazet 	const unsigned char *ptr = (skb_transport_header(ack_skb) +
16009c70220bSArnaldo Carvalho de Melo 				    TCP_SKB_CB(ack_skb)->sacked);
1601fd6dad61SIlpo Järvinen 	struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
16024389ddedSAdam Langley 	struct tcp_sack_block sp[TCP_NUM_SACKS];
160368f8353bSIlpo Järvinen 	struct tcp_sack_block *cache;
160468f8353bSIlpo Järvinen 	struct sk_buff *skb;
16054389ddedSAdam Langley 	int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
1606fd6dad61SIlpo Järvinen 	int used_sacks;
1607a2a385d6SEric Dumazet 	bool found_dup_sack = false;
160868f8353bSIlpo Järvinen 	int i, j;
1609fda03fbbSBaruch Even 	int first_sack_index;
16101da177e4SLinus Torvalds 
1611196da974SKenneth Klette Jonassen 	state->flag = 0;
1612196da974SKenneth Klette Jonassen 	state->reord = tp->packets_out;
1613a1197f5aSIlpo Järvinen 
1614d738cd8fSIlpo Järvinen 	if (!tp->sacked_out) {
1615de83c058SIlpo Järvinen 		if (WARN_ON(tp->fackets_out))
16161da177e4SLinus Torvalds 			tp->fackets_out = 0;
16176859d494SIlpo Järvinen 		tcp_highest_sack_reset(sk);
1618d738cd8fSIlpo Järvinen 	}
16191da177e4SLinus Torvalds 
16201ed83465SPavel Emelyanov 	found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
1621d06e021dSDavid S. Miller 					 num_sacks, prior_snd_una);
1622d06e021dSDavid S. Miller 	if (found_dup_sack)
1623196da974SKenneth Klette Jonassen 		state->flag |= FLAG_DSACKING_ACK;
16246f74651aSBaruch Even 
16256f74651aSBaruch Even 	/* Eliminate too old ACKs, but take into
16266f74651aSBaruch Even 	 * account more or less fresh ones, they can
16276f74651aSBaruch Even 	 * contain valid SACK info.
16286f74651aSBaruch Even 	 */
16296f74651aSBaruch Even 	if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
16306f74651aSBaruch Even 		return 0;
16316f74651aSBaruch Even 
163296a2d41aSIlpo Järvinen 	if (!tp->packets_out)
163396a2d41aSIlpo Järvinen 		goto out;
163496a2d41aSIlpo Järvinen 
1635fd6dad61SIlpo Järvinen 	used_sacks = 0;
1636fd6dad61SIlpo Järvinen 	first_sack_index = 0;
1637fd6dad61SIlpo Järvinen 	for (i = 0; i < num_sacks; i++) {
1638a2a385d6SEric Dumazet 		bool dup_sack = !i && found_dup_sack;
1639fd6dad61SIlpo Järvinen 
1640d3e2ce3bSHarvey Harrison 		sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq);
1641d3e2ce3bSHarvey Harrison 		sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq);
1642fd6dad61SIlpo Järvinen 
1643fd6dad61SIlpo Järvinen 		if (!tcp_is_sackblock_valid(tp, dup_sack,
1644fd6dad61SIlpo Järvinen 					    sp[used_sacks].start_seq,
1645fd6dad61SIlpo Järvinen 					    sp[used_sacks].end_seq)) {
164640b215e5SPavel Emelyanov 			int mib_idx;
164740b215e5SPavel Emelyanov 
1648fd6dad61SIlpo Järvinen 			if (dup_sack) {
1649fd6dad61SIlpo Järvinen 				if (!tp->undo_marker)
165040b215e5SPavel Emelyanov 					mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO;
1651fd6dad61SIlpo Järvinen 				else
165240b215e5SPavel Emelyanov 					mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD;
1653fd6dad61SIlpo Järvinen 			} else {
1654fd6dad61SIlpo Järvinen 				/* Don't count olds caused by ACK reordering */
1655fd6dad61SIlpo Järvinen 				if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) &&
1656fd6dad61SIlpo Järvinen 				    !after(sp[used_sacks].end_seq, tp->snd_una))
1657fd6dad61SIlpo Järvinen 					continue;
165840b215e5SPavel Emelyanov 				mib_idx = LINUX_MIB_TCPSACKDISCARD;
1659fd6dad61SIlpo Järvinen 			}
166040b215e5SPavel Emelyanov 
1661de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk), mib_idx);
1662fd6dad61SIlpo Järvinen 			if (i == 0)
1663fd6dad61SIlpo Järvinen 				first_sack_index = -1;
1664fd6dad61SIlpo Järvinen 			continue;
1665fd6dad61SIlpo Järvinen 		}
1666fd6dad61SIlpo Järvinen 
1667fd6dad61SIlpo Järvinen 		/* Ignore very old stuff early */
1668fd6dad61SIlpo Järvinen 		if (!after(sp[used_sacks].end_seq, prior_snd_una))
1669fd6dad61SIlpo Järvinen 			continue;
1670fd6dad61SIlpo Järvinen 
1671fd6dad61SIlpo Järvinen 		used_sacks++;
1672fd6dad61SIlpo Järvinen 	}
1673fd6dad61SIlpo Järvinen 
16746a438bbeSStephen Hemminger 	/* order SACK blocks to allow in order walk of the retrans queue */
1675fd6dad61SIlpo Järvinen 	for (i = used_sacks - 1; i > 0; i--) {
16766a438bbeSStephen Hemminger 		for (j = 0; j < i; j++) {
1677fd6dad61SIlpo Järvinen 			if (after(sp[j].start_seq, sp[j + 1].start_seq)) {
1678a0bffffcSIlpo Järvinen 				swap(sp[j], sp[j + 1]);
1679fda03fbbSBaruch Even 
1680fda03fbbSBaruch Even 				/* Track where the first SACK block goes to */
1681fda03fbbSBaruch Even 				if (j == first_sack_index)
1682fda03fbbSBaruch Even 					first_sack_index = j + 1;
16836a438bbeSStephen Hemminger 			}
16846a438bbeSStephen Hemminger 		}
16856a438bbeSStephen Hemminger 	}
16866a438bbeSStephen Hemminger 
168768f8353bSIlpo Järvinen 	skb = tcp_write_queue_head(sk);
1688196da974SKenneth Klette Jonassen 	state->fack_count = 0;
168968f8353bSIlpo Järvinen 	i = 0;
169068f8353bSIlpo Järvinen 
169168f8353bSIlpo Järvinen 	if (!tp->sacked_out) {
169268f8353bSIlpo Järvinen 		/* It's already past, so skip checking against it */
169368f8353bSIlpo Järvinen 		cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
169468f8353bSIlpo Järvinen 	} else {
169568f8353bSIlpo Järvinen 		cache = tp->recv_sack_cache;
169668f8353bSIlpo Järvinen 		/* Skip empty blocks in at head of the cache */
169768f8353bSIlpo Järvinen 		while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq &&
169868f8353bSIlpo Järvinen 		       !cache->end_seq)
169968f8353bSIlpo Järvinen 			cache++;
1700fda03fbbSBaruch Even 	}
1701fda03fbbSBaruch Even 
170268f8353bSIlpo Järvinen 	while (i < used_sacks) {
1703fd6dad61SIlpo Järvinen 		u32 start_seq = sp[i].start_seq;
1704fd6dad61SIlpo Järvinen 		u32 end_seq = sp[i].end_seq;
1705a2a385d6SEric Dumazet 		bool dup_sack = (found_dup_sack && (i == first_sack_index));
170668f8353bSIlpo Järvinen 		struct tcp_sack_block *next_dup = NULL;
1707e56d6cd6SIlpo Järvinen 
170868f8353bSIlpo Järvinen 		if (found_dup_sack && ((i + 1) == first_sack_index))
170968f8353bSIlpo Järvinen 			next_dup = &sp[i + 1];
17101da177e4SLinus Torvalds 
171168f8353bSIlpo Järvinen 		/* Skip too early cached blocks */
171268f8353bSIlpo Järvinen 		while (tcp_sack_cache_ok(tp, cache) &&
171368f8353bSIlpo Järvinen 		       !before(start_seq, cache->end_seq))
171468f8353bSIlpo Järvinen 			cache++;
17151da177e4SLinus Torvalds 
171668f8353bSIlpo Järvinen 		/* Can skip some work by looking recv_sack_cache? */
171768f8353bSIlpo Järvinen 		if (tcp_sack_cache_ok(tp, cache) && !dup_sack &&
171868f8353bSIlpo Järvinen 		    after(end_seq, cache->start_seq)) {
1719fe067e8aSDavid S. Miller 
172068f8353bSIlpo Järvinen 			/* Head todo? */
172168f8353bSIlpo Järvinen 			if (before(start_seq, cache->start_seq)) {
1722196da974SKenneth Klette Jonassen 				skb = tcp_sacktag_skip(skb, sk, state,
1723a1197f5aSIlpo Järvinen 						       start_seq);
1724056834d9SIlpo Järvinen 				skb = tcp_sacktag_walk(skb, sk, next_dup,
1725196da974SKenneth Klette Jonassen 						       state,
1726056834d9SIlpo Järvinen 						       start_seq,
1727056834d9SIlpo Järvinen 						       cache->start_seq,
1728a1197f5aSIlpo Järvinen 						       dup_sack);
1729fda03fbbSBaruch Even 			}
17306a438bbeSStephen Hemminger 
173168f8353bSIlpo Järvinen 			/* Rest of the block already fully processed? */
173220de20beSIlpo Järvinen 			if (!after(end_seq, cache->end_seq))
173320de20beSIlpo Järvinen 				goto advance_sp;
173420de20beSIlpo Järvinen 
1735056834d9SIlpo Järvinen 			skb = tcp_maybe_skipping_dsack(skb, sk, next_dup,
1736196da974SKenneth Klette Jonassen 						       state,
1737a1197f5aSIlpo Järvinen 						       cache->end_seq);
173868f8353bSIlpo Järvinen 
173968f8353bSIlpo Järvinen 			/* ...tail remains todo... */
17406859d494SIlpo Järvinen 			if (tcp_highest_sack_seq(tp) == cache->end_seq) {
174120de20beSIlpo Järvinen 				/* ...but better entrypoint exists! */
17426859d494SIlpo Järvinen 				skb = tcp_highest_sack(sk);
174351456b29SIan Morris 				if (!skb)
17446859d494SIlpo Järvinen 					break;
1745196da974SKenneth Klette Jonassen 				state->fack_count = tp->fackets_out;
174668f8353bSIlpo Järvinen 				cache++;
174768f8353bSIlpo Järvinen 				goto walk;
1748e56d6cd6SIlpo Järvinen 			}
1749e56d6cd6SIlpo Järvinen 
1750196da974SKenneth Klette Jonassen 			skb = tcp_sacktag_skip(skb, sk, state, cache->end_seq);
175168f8353bSIlpo Järvinen 			/* Check overlap against next cached too (past this one already) */
175268f8353bSIlpo Järvinen 			cache++;
175368f8353bSIlpo Järvinen 			continue;
17541da177e4SLinus Torvalds 		}
1755fbd52eb2SIlpo Järvinen 
17566859d494SIlpo Järvinen 		if (!before(start_seq, tcp_highest_sack_seq(tp))) {
17576859d494SIlpo Järvinen 			skb = tcp_highest_sack(sk);
175851456b29SIan Morris 			if (!skb)
17596859d494SIlpo Järvinen 				break;
1760196da974SKenneth Klette Jonassen 			state->fack_count = tp->fackets_out;
176168f8353bSIlpo Järvinen 		}
1762196da974SKenneth Klette Jonassen 		skb = tcp_sacktag_skip(skb, sk, state, start_seq);
176368f8353bSIlpo Järvinen 
176468f8353bSIlpo Järvinen walk:
1765196da974SKenneth Klette Jonassen 		skb = tcp_sacktag_walk(skb, sk, next_dup, state,
1766a1197f5aSIlpo Järvinen 				       start_seq, end_seq, dup_sack);
176768f8353bSIlpo Järvinen 
176868f8353bSIlpo Järvinen advance_sp:
176968f8353bSIlpo Järvinen 		i++;
17701da177e4SLinus Torvalds 	}
17711da177e4SLinus Torvalds 
177268f8353bSIlpo Järvinen 	/* Clear the head of the cache sack blocks so we can skip it next time */
177368f8353bSIlpo Järvinen 	for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) {
177468f8353bSIlpo Järvinen 		tp->recv_sack_cache[i].start_seq = 0;
177568f8353bSIlpo Järvinen 		tp->recv_sack_cache[i].end_seq = 0;
177668f8353bSIlpo Järvinen 	}
177768f8353bSIlpo Järvinen 	for (j = 0; j < used_sacks; j++)
177868f8353bSIlpo Järvinen 		tp->recv_sack_cache[i++] = sp[j];
177968f8353bSIlpo Järvinen 
1780196da974SKenneth Klette Jonassen 	if ((state->reord < tp->fackets_out) &&
17819b44190dSYuchung Cheng 	    ((inet_csk(sk)->icsk_ca_state != TCP_CA_Loss) || tp->undo_marker))
1782196da974SKenneth Klette Jonassen 		tcp_update_reordering(sk, tp->fackets_out - state->reord, 0);
17831da177e4SLinus Torvalds 
17849dac8835SYuchung Cheng 	tcp_verify_left_out(tp);
178596a2d41aSIlpo Järvinen out:
178696a2d41aSIlpo Järvinen 
17871da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0
1788547b792cSIlpo Järvinen 	WARN_ON((int)tp->sacked_out < 0);
1789547b792cSIlpo Järvinen 	WARN_ON((int)tp->lost_out < 0);
1790547b792cSIlpo Järvinen 	WARN_ON((int)tp->retrans_out < 0);
1791547b792cSIlpo Järvinen 	WARN_ON((int)tcp_packets_in_flight(tp) < 0);
17921da177e4SLinus Torvalds #endif
1793196da974SKenneth Klette Jonassen 	return state->flag;
17941da177e4SLinus Torvalds }
17951da177e4SLinus Torvalds 
1796882bebaaSIlpo Järvinen /* Limits sacked_out so that sum with lost_out isn't ever larger than
1797a2a385d6SEric Dumazet  * packets_out. Returns false if sacked_out adjustement wasn't necessary.
179830935cf4SIlpo Järvinen  */
1799a2a385d6SEric Dumazet static bool tcp_limit_reno_sacked(struct tcp_sock *tp)
18004ddf6676SIlpo Järvinen {
18014ddf6676SIlpo Järvinen 	u32 holes;
18024ddf6676SIlpo Järvinen 
18034ddf6676SIlpo Järvinen 	holes = max(tp->lost_out, 1U);
18044ddf6676SIlpo Järvinen 	holes = min(holes, tp->packets_out);
18054ddf6676SIlpo Järvinen 
18064ddf6676SIlpo Järvinen 	if ((tp->sacked_out + holes) > tp->packets_out) {
18074ddf6676SIlpo Järvinen 		tp->sacked_out = tp->packets_out - holes;
1808a2a385d6SEric Dumazet 		return true;
18094ddf6676SIlpo Järvinen 	}
1810a2a385d6SEric Dumazet 	return false;
1811882bebaaSIlpo Järvinen }
1812882bebaaSIlpo Järvinen 
1813882bebaaSIlpo Järvinen /* If we receive more dupacks than we expected counting segments
1814882bebaaSIlpo Järvinen  * in assumption of absent reordering, interpret this as reordering.
1815882bebaaSIlpo Järvinen  * The only another reason could be bug in receiver TCP.
1816882bebaaSIlpo Järvinen  */
1817882bebaaSIlpo Järvinen static void tcp_check_reno_reordering(struct sock *sk, const int addend)
1818882bebaaSIlpo Järvinen {
1819882bebaaSIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
1820882bebaaSIlpo Järvinen 	if (tcp_limit_reno_sacked(tp))
1821882bebaaSIlpo Järvinen 		tcp_update_reordering(sk, tp->packets_out + addend, 0);
18224ddf6676SIlpo Järvinen }
18234ddf6676SIlpo Järvinen 
18244ddf6676SIlpo Järvinen /* Emulate SACKs for SACKless connection: account for a new dupack. */
18254ddf6676SIlpo Järvinen 
18264ddf6676SIlpo Järvinen static void tcp_add_reno_sack(struct sock *sk)
18274ddf6676SIlpo Järvinen {
18284ddf6676SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
1829ddf1af6fSYuchung Cheng 	u32 prior_sacked = tp->sacked_out;
1830ddf1af6fSYuchung Cheng 
18314ddf6676SIlpo Järvinen 	tp->sacked_out++;
18324ddf6676SIlpo Järvinen 	tcp_check_reno_reordering(sk, 0);
1833ddf1af6fSYuchung Cheng 	if (tp->sacked_out > prior_sacked)
1834ddf1af6fSYuchung Cheng 		tp->delivered++; /* Some out-of-order packet is delivered */
1835005903bcSIlpo Järvinen 	tcp_verify_left_out(tp);
18364ddf6676SIlpo Järvinen }
18374ddf6676SIlpo Järvinen 
18384ddf6676SIlpo Järvinen /* Account for ACK, ACKing some data in Reno Recovery phase. */
18394ddf6676SIlpo Järvinen 
18404ddf6676SIlpo Järvinen static void tcp_remove_reno_sacks(struct sock *sk, int acked)
18414ddf6676SIlpo Järvinen {
18424ddf6676SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
18434ddf6676SIlpo Järvinen 
18444ddf6676SIlpo Järvinen 	if (acked > 0) {
18454ddf6676SIlpo Järvinen 		/* One ACK acked hole. The rest eat duplicate ACKs. */
1846ddf1af6fSYuchung Cheng 		tp->delivered += max_t(int, acked - tp->sacked_out, 1);
18474ddf6676SIlpo Järvinen 		if (acked - 1 >= tp->sacked_out)
18484ddf6676SIlpo Järvinen 			tp->sacked_out = 0;
18494ddf6676SIlpo Järvinen 		else
18504ddf6676SIlpo Järvinen 			tp->sacked_out -= acked - 1;
18514ddf6676SIlpo Järvinen 	}
18524ddf6676SIlpo Järvinen 	tcp_check_reno_reordering(sk, acked);
1853005903bcSIlpo Järvinen 	tcp_verify_left_out(tp);
18544ddf6676SIlpo Järvinen }
18554ddf6676SIlpo Järvinen 
18564ddf6676SIlpo Järvinen static inline void tcp_reset_reno_sack(struct tcp_sock *tp)
18574ddf6676SIlpo Järvinen {
18584ddf6676SIlpo Järvinen 	tp->sacked_out = 0;
18594ddf6676SIlpo Järvinen }
18604ddf6676SIlpo Järvinen 
1861989e04c5SYuchung Cheng void tcp_clear_retrans(struct tcp_sock *tp)
18621da177e4SLinus Torvalds {
18631da177e4SLinus Torvalds 	tp->retrans_out = 0;
18641da177e4SLinus Torvalds 	tp->lost_out = 0;
18651da177e4SLinus Torvalds 	tp->undo_marker = 0;
18666e08d5e3SYuchung Cheng 	tp->undo_retrans = -1;
18674cd82999SIlpo Järvinen 	tp->fackets_out = 0;
18684cd82999SIlpo Järvinen 	tp->sacked_out = 0;
18694cd82999SIlpo Järvinen }
18704cd82999SIlpo Järvinen 
1871989e04c5SYuchung Cheng static inline void tcp_init_undo(struct tcp_sock *tp)
1872989e04c5SYuchung Cheng {
1873989e04c5SYuchung Cheng 	tp->undo_marker = tp->snd_una;
1874989e04c5SYuchung Cheng 	/* Retransmission still in flight may cause DSACKs later. */
1875989e04c5SYuchung Cheng 	tp->undo_retrans = tp->retrans_out ? : -1;
1876989e04c5SYuchung Cheng }
1877989e04c5SYuchung Cheng 
18785ae344c9SNeal Cardwell /* Enter Loss state. If we detect SACK reneging, forget all SACK information
18791da177e4SLinus Torvalds  * and reset tags completely, otherwise preserve SACKs. If receiver
18801da177e4SLinus Torvalds  * dropped its ofo queue, we will know this due to reneging detection.
18811da177e4SLinus Torvalds  */
18825ae344c9SNeal Cardwell void tcp_enter_loss(struct sock *sk)
18831da177e4SLinus Torvalds {
18846687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
18851da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
18861da177e4SLinus Torvalds 	struct sk_buff *skb;
1887f82b681aSYuchung Cheng 	bool new_recovery = icsk->icsk_ca_state < TCP_CA_Recovery;
18885ae344c9SNeal Cardwell 	bool is_reneg;			/* is receiver reneging on SACKs? */
18891da177e4SLinus Torvalds 
18901da177e4SLinus Torvalds 	/* Reduce ssthresh if it has not yet been made inside this window. */
1891e33099f9SYuchung Cheng 	if (icsk->icsk_ca_state <= TCP_CA_Disorder ||
1892e33099f9SYuchung Cheng 	    !after(tp->high_seq, tp->snd_una) ||
18936687e988SArnaldo Carvalho de Melo 	    (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) {
18946687e988SArnaldo Carvalho de Melo 		tp->prior_ssthresh = tcp_current_ssthresh(sk);
18956687e988SArnaldo Carvalho de Melo 		tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
18966687e988SArnaldo Carvalho de Melo 		tcp_ca_event(sk, CA_EVENT_LOSS);
1897989e04c5SYuchung Cheng 		tcp_init_undo(tp);
18981da177e4SLinus Torvalds 	}
18991da177e4SLinus Torvalds 	tp->snd_cwnd	   = 1;
19001da177e4SLinus Torvalds 	tp->snd_cwnd_cnt   = 0;
19011da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
19021da177e4SLinus Torvalds 
1903989e04c5SYuchung Cheng 	tp->retrans_out = 0;
1904989e04c5SYuchung Cheng 	tp->lost_out = 0;
19054cd82999SIlpo Järvinen 
19064cd82999SIlpo Järvinen 	if (tcp_is_reno(tp))
19074cd82999SIlpo Järvinen 		tcp_reset_reno_sack(tp);
19081da177e4SLinus Torvalds 
19095ae344c9SNeal Cardwell 	skb = tcp_write_queue_head(sk);
19105ae344c9SNeal Cardwell 	is_reneg = skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED);
19115ae344c9SNeal Cardwell 	if (is_reneg) {
19125ae344c9SNeal Cardwell 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSACKRENEGING);
19134cd82999SIlpo Järvinen 		tp->sacked_out = 0;
19144cd82999SIlpo Järvinen 		tp->fackets_out = 0;
1915b7689205SIlpo Järvinen 	}
191664edc273SIlpo Järvinen 	tcp_clear_all_retrans_hints(tp);
19171da177e4SLinus Torvalds 
1918fe067e8aSDavid S. Miller 	tcp_for_write_queue(skb, sk) {
1919fe067e8aSDavid S. Miller 		if (skb == tcp_send_head(sk))
1920fe067e8aSDavid S. Miller 			break;
19214cd82999SIlpo Järvinen 
19221da177e4SLinus Torvalds 		TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED;
19235ae344c9SNeal Cardwell 		if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED) || is_reneg) {
19241da177e4SLinus Torvalds 			TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
19251da177e4SLinus Torvalds 			TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
19261da177e4SLinus Torvalds 			tp->lost_out += tcp_skb_pcount(skb);
1927006f582cSIlpo Järvinen 			tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
19281da177e4SLinus Torvalds 		}
19291da177e4SLinus Torvalds 	}
1930005903bcSIlpo Järvinen 	tcp_verify_left_out(tp);
19311da177e4SLinus Torvalds 
193274c181d5SYuchung Cheng 	/* Timeout in disordered state after receiving substantial DUPACKs
193374c181d5SYuchung Cheng 	 * suggests that the degree of reordering is over-estimated.
193474c181d5SYuchung Cheng 	 */
193574c181d5SYuchung Cheng 	if (icsk->icsk_ca_state <= TCP_CA_Disorder &&
193674c181d5SYuchung Cheng 	    tp->sacked_out >= sysctl_tcp_reordering)
19371da177e4SLinus Torvalds 		tp->reordering = min_t(unsigned int, tp->reordering,
19381da177e4SLinus Torvalds 				       sysctl_tcp_reordering);
19396687e988SArnaldo Carvalho de Melo 	tcp_set_ca_state(sk, TCP_CA_Loss);
19401da177e4SLinus Torvalds 	tp->high_seq = tp->snd_nxt;
1941735d3831SFlorian Westphal 	tcp_ecn_queue_cwr(tp);
1942e33099f9SYuchung Cheng 
1943e33099f9SYuchung Cheng 	/* F-RTO RFC5682 sec 3.1 step 1: retransmit SND.UNA if no previous
1944e33099f9SYuchung Cheng 	 * loss recovery is underway except recurring timeout(s) on
1945e33099f9SYuchung Cheng 	 * the same SND.UNA (sec 3.2). Disable F-RTO on path MTU probing
1946e33099f9SYuchung Cheng 	 */
1947e33099f9SYuchung Cheng 	tp->frto = sysctl_tcp_frto &&
1948e33099f9SYuchung Cheng 		   (new_recovery || icsk->icsk_retransmits) &&
1949e33099f9SYuchung Cheng 		   !inet_csk(sk)->icsk_mtup.probe_size;
19501da177e4SLinus Torvalds }
19511da177e4SLinus Torvalds 
1952cadbd031SIlpo Järvinen /* If ACK arrived pointing to a remembered SACK, it means that our
1953cadbd031SIlpo Järvinen  * remembered SACKs do not reflect real state of receiver i.e.
19541da177e4SLinus Torvalds  * receiver _host_ is heavily congested (or buggy).
1955cadbd031SIlpo Järvinen  *
19565ae344c9SNeal Cardwell  * To avoid big spurious retransmission bursts due to transient SACK
19575ae344c9SNeal Cardwell  * scoreboard oddities that look like reneging, we give the receiver a
19585ae344c9SNeal Cardwell  * little time (max(RTT/2, 10ms)) to send us some more ACKs that will
19595ae344c9SNeal Cardwell  * restore sanity to the SACK scoreboard. If the apparent reneging
19605ae344c9SNeal Cardwell  * persists until this RTO then we'll clear the SACK scoreboard.
19611da177e4SLinus Torvalds  */
1962a2a385d6SEric Dumazet static bool tcp_check_sack_reneging(struct sock *sk, int flag)
1963cadbd031SIlpo Järvinen {
1964cadbd031SIlpo Järvinen 	if (flag & FLAG_SACK_RENEGING) {
19655ae344c9SNeal Cardwell 		struct tcp_sock *tp = tcp_sk(sk);
19665ae344c9SNeal Cardwell 		unsigned long delay = max(usecs_to_jiffies(tp->srtt_us >> 4),
19675ae344c9SNeal Cardwell 					  msecs_to_jiffies(10));
19681da177e4SLinus Torvalds 
1969463c84b9SArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
19705ae344c9SNeal Cardwell 					  delay, TCP_RTO_MAX);
1971a2a385d6SEric Dumazet 		return true;
19721da177e4SLinus Torvalds 	}
1973a2a385d6SEric Dumazet 	return false;
19741da177e4SLinus Torvalds }
19751da177e4SLinus Torvalds 
1976cf533ea5SEric Dumazet static inline int tcp_fackets_out(const struct tcp_sock *tp)
19771da177e4SLinus Torvalds {
1978e60402d0SIlpo Järvinen 	return tcp_is_reno(tp) ? tp->sacked_out + 1 : tp->fackets_out;
19791da177e4SLinus Torvalds }
19801da177e4SLinus Torvalds 
198185cc391cSIlpo Järvinen /* Heurestics to calculate number of duplicate ACKs. There's no dupACKs
198285cc391cSIlpo Järvinen  * counter when SACK is enabled (without SACK, sacked_out is used for
198385cc391cSIlpo Järvinen  * that purpose).
198485cc391cSIlpo Järvinen  *
198585cc391cSIlpo Järvinen  * Instead, with FACK TCP uses fackets_out that includes both SACKed
198685cc391cSIlpo Järvinen  * segments up to the highest received SACK block so far and holes in
198785cc391cSIlpo Järvinen  * between them.
198885cc391cSIlpo Järvinen  *
198985cc391cSIlpo Järvinen  * With reordering, holes may still be in flight, so RFC3517 recovery
199085cc391cSIlpo Järvinen  * uses pure sacked_out (total number of SACKed segments) even though
199185cc391cSIlpo Järvinen  * it violates the RFC that uses duplicate ACKs, often these are equal
199285cc391cSIlpo Järvinen  * but when e.g. out-of-window ACKs or packet duplication occurs,
199385cc391cSIlpo Järvinen  * they differ. Since neither occurs due to loss, TCP should really
199485cc391cSIlpo Järvinen  * ignore them.
199585cc391cSIlpo Järvinen  */
1996cf533ea5SEric Dumazet static inline int tcp_dupack_heuristics(const struct tcp_sock *tp)
199785cc391cSIlpo Järvinen {
199885cc391cSIlpo Järvinen 	return tcp_is_fack(tp) ? tp->fackets_out : tp->sacked_out + 1;
199985cc391cSIlpo Järvinen }
200085cc391cSIlpo Järvinen 
2001750ea2baSYuchung Cheng static bool tcp_pause_early_retransmit(struct sock *sk, int flag)
2002750ea2baSYuchung Cheng {
2003750ea2baSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
2004750ea2baSYuchung Cheng 	unsigned long delay;
2005750ea2baSYuchung Cheng 
2006750ea2baSYuchung Cheng 	/* Delay early retransmit and entering fast recovery for
2007750ea2baSYuchung Cheng 	 * max(RTT/4, 2msec) unless ack has ECE mark, no RTT samples
2008750ea2baSYuchung Cheng 	 * available, or RTO is scheduled to fire first.
2009750ea2baSYuchung Cheng 	 */
20106ba8a3b1SNandita Dukkipati 	if (sysctl_tcp_early_retrans < 2 || sysctl_tcp_early_retrans > 3 ||
2011740b0f18SEric Dumazet 	    (flag & FLAG_ECE) || !tp->srtt_us)
2012750ea2baSYuchung Cheng 		return false;
2013750ea2baSYuchung Cheng 
2014740b0f18SEric Dumazet 	delay = max(usecs_to_jiffies(tp->srtt_us >> 5),
2015740b0f18SEric Dumazet 		    msecs_to_jiffies(2));
2016740b0f18SEric Dumazet 
2017750ea2baSYuchung Cheng 	if (!time_after(inet_csk(sk)->icsk_timeout, (jiffies + delay)))
2018750ea2baSYuchung Cheng 		return false;
2019750ea2baSYuchung Cheng 
20206ba8a3b1SNandita Dukkipati 	inet_csk_reset_xmit_timer(sk, ICSK_TIME_EARLY_RETRANS, delay,
20216ba8a3b1SNandita Dukkipati 				  TCP_RTO_MAX);
2022750ea2baSYuchung Cheng 	return true;
2023750ea2baSYuchung Cheng }
2024750ea2baSYuchung Cheng 
20251da177e4SLinus Torvalds /* Linux NewReno/SACK/FACK/ECN state machine.
20261da177e4SLinus Torvalds  * --------------------------------------
20271da177e4SLinus Torvalds  *
20281da177e4SLinus Torvalds  * "Open"	Normal state, no dubious events, fast path.
20291da177e4SLinus Torvalds  * "Disorder"   In all the respects it is "Open",
20301da177e4SLinus Torvalds  *		but requires a bit more attention. It is entered when
20311da177e4SLinus Torvalds  *		we see some SACKs or dupacks. It is split of "Open"
20321da177e4SLinus Torvalds  *		mainly to move some processing from fast path to slow one.
20331da177e4SLinus Torvalds  * "CWR"	CWND was reduced due to some Congestion Notification event.
20341da177e4SLinus Torvalds  *		It can be ECN, ICMP source quench, local device congestion.
20351da177e4SLinus Torvalds  * "Recovery"	CWND was reduced, we are fast-retransmitting.
20361da177e4SLinus Torvalds  * "Loss"	CWND was reduced due to RTO timeout or SACK reneging.
20371da177e4SLinus Torvalds  *
20381da177e4SLinus Torvalds  * tcp_fastretrans_alert() is entered:
20391da177e4SLinus Torvalds  * - each incoming ACK, if state is not "Open"
20401da177e4SLinus Torvalds  * - when arrived ACK is unusual, namely:
20411da177e4SLinus Torvalds  *	* SACK
20421da177e4SLinus Torvalds  *	* Duplicate ACK.
20431da177e4SLinus Torvalds  *	* ECN ECE.
20441da177e4SLinus Torvalds  *
20451da177e4SLinus Torvalds  * Counting packets in flight is pretty simple.
20461da177e4SLinus Torvalds  *
20471da177e4SLinus Torvalds  *	in_flight = packets_out - left_out + retrans_out
20481da177e4SLinus Torvalds  *
20491da177e4SLinus Torvalds  *	packets_out is SND.NXT-SND.UNA counted in packets.
20501da177e4SLinus Torvalds  *
20511da177e4SLinus Torvalds  *	retrans_out is number of retransmitted segments.
20521da177e4SLinus Torvalds  *
20531da177e4SLinus Torvalds  *	left_out is number of segments left network, but not ACKed yet.
20541da177e4SLinus Torvalds  *
20551da177e4SLinus Torvalds  *		left_out = sacked_out + lost_out
20561da177e4SLinus Torvalds  *
20571da177e4SLinus Torvalds  *     sacked_out: Packets, which arrived to receiver out of order
20581da177e4SLinus Torvalds  *		   and hence not ACKed. With SACKs this number is simply
20591da177e4SLinus Torvalds  *		   amount of SACKed data. Even without SACKs
20601da177e4SLinus Torvalds  *		   it is easy to give pretty reliable estimate of this number,
20611da177e4SLinus Torvalds  *		   counting duplicate ACKs.
20621da177e4SLinus Torvalds  *
20631da177e4SLinus Torvalds  *       lost_out: Packets lost by network. TCP has no explicit
20641da177e4SLinus Torvalds  *		   "loss notification" feedback from network (for now).
20651da177e4SLinus Torvalds  *		   It means that this number can be only _guessed_.
20661da177e4SLinus Torvalds  *		   Actually, it is the heuristics to predict lossage that
20671da177e4SLinus Torvalds  *		   distinguishes different algorithms.
20681da177e4SLinus Torvalds  *
20691da177e4SLinus Torvalds  *	F.e. after RTO, when all the queue is considered as lost,
20701da177e4SLinus Torvalds  *	lost_out = packets_out and in_flight = retrans_out.
20711da177e4SLinus Torvalds  *
20721da177e4SLinus Torvalds  *		Essentially, we have now two algorithms counting
20731da177e4SLinus Torvalds  *		lost packets.
20741da177e4SLinus Torvalds  *
20751da177e4SLinus Torvalds  *		FACK: It is the simplest heuristics. As soon as we decided
20761da177e4SLinus Torvalds  *		that something is lost, we decide that _all_ not SACKed
20771da177e4SLinus Torvalds  *		packets until the most forward SACK are lost. I.e.
20781da177e4SLinus Torvalds  *		lost_out = fackets_out - sacked_out and left_out = fackets_out.
20791da177e4SLinus Torvalds  *		It is absolutely correct estimate, if network does not reorder
20801da177e4SLinus Torvalds  *		packets. And it loses any connection to reality when reordering
20811da177e4SLinus Torvalds  *		takes place. We use FACK by default until reordering
20821da177e4SLinus Torvalds  *		is suspected on the path to this destination.
20831da177e4SLinus Torvalds  *
20841da177e4SLinus Torvalds  *		NewReno: when Recovery is entered, we assume that one segment
20851da177e4SLinus Torvalds  *		is lost (classic Reno). While we are in Recovery and
20861da177e4SLinus Torvalds  *		a partial ACK arrives, we assume that one more packet
20871da177e4SLinus Torvalds  *		is lost (NewReno). This heuristics are the same in NewReno
20881da177e4SLinus Torvalds  *		and SACK.
20891da177e4SLinus Torvalds  *
20901da177e4SLinus Torvalds  *  Imagine, that's all! Forget about all this shamanism about CWND inflation
20911da177e4SLinus Torvalds  *  deflation etc. CWND is real congestion window, never inflated, changes
20921da177e4SLinus Torvalds  *  only according to classic VJ rules.
20931da177e4SLinus Torvalds  *
20941da177e4SLinus Torvalds  * Really tricky (and requiring careful tuning) part of algorithm
20951da177e4SLinus Torvalds  * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue().
20961da177e4SLinus Torvalds  * The first determines the moment _when_ we should reduce CWND and,
20971da177e4SLinus Torvalds  * hence, slow down forward transmission. In fact, it determines the moment
20981da177e4SLinus Torvalds  * when we decide that hole is caused by loss, rather than by a reorder.
20991da177e4SLinus Torvalds  *
21001da177e4SLinus Torvalds  * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill
21011da177e4SLinus Torvalds  * holes, caused by lost packets.
21021da177e4SLinus Torvalds  *
21031da177e4SLinus Torvalds  * And the most logically complicated part of algorithm is undo
21041da177e4SLinus Torvalds  * heuristics. We detect false retransmits due to both too early
21051da177e4SLinus Torvalds  * fast retransmit (reordering) and underestimated RTO, analyzing
21061da177e4SLinus Torvalds  * timestamps and D-SACKs. When we detect that some segments were
21071da177e4SLinus Torvalds  * retransmitted by mistake and CWND reduction was wrong, we undo
21081da177e4SLinus Torvalds  * window reduction and abort recovery phase. This logic is hidden
21091da177e4SLinus Torvalds  * inside several functions named tcp_try_undo_<something>.
21101da177e4SLinus Torvalds  */
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds /* This function decides, when we should leave Disordered state
21131da177e4SLinus Torvalds  * and enter Recovery phase, reducing congestion window.
21141da177e4SLinus Torvalds  *
21151da177e4SLinus Torvalds  * Main question: may we further continue forward transmission
21161da177e4SLinus Torvalds  * with the same cwnd?
21171da177e4SLinus Torvalds  */
2118a2a385d6SEric Dumazet static bool tcp_time_to_recover(struct sock *sk, int flag)
21191da177e4SLinus Torvalds {
21209e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
21211da177e4SLinus Torvalds 	__u32 packets_out;
21221da177e4SLinus Torvalds 
21231da177e4SLinus Torvalds 	/* Trick#1: The loss is proven. */
21241da177e4SLinus Torvalds 	if (tp->lost_out)
2125a2a385d6SEric Dumazet 		return true;
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 	/* Not-A-Trick#2 : Classic rule... */
2128ea84e555SAndreas Petlund 	if (tcp_dupack_heuristics(tp) > tp->reordering)
2129a2a385d6SEric Dumazet 		return true;
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	/* Trick#4: It is still not OK... But will it be useful to delay
21321da177e4SLinus Torvalds 	 * recovery more?
21331da177e4SLinus Torvalds 	 */
21341da177e4SLinus Torvalds 	packets_out = tp->packets_out;
21351da177e4SLinus Torvalds 	if (packets_out <= tp->reordering &&
21361da177e4SLinus Torvalds 	    tp->sacked_out >= max_t(__u32, packets_out/2, sysctl_tcp_reordering) &&
21379e412ba7SIlpo Järvinen 	    !tcp_may_send_now(sk)) {
21381da177e4SLinus Torvalds 		/* We have nothing to send. This connection is limited
21391da177e4SLinus Torvalds 		 * either by receiver window or by application.
21401da177e4SLinus Torvalds 		 */
2141a2a385d6SEric Dumazet 		return true;
21421da177e4SLinus Torvalds 	}
21431da177e4SLinus Torvalds 
21447e380175SAndreas Petlund 	/* If a thin stream is detected, retransmit after first
21457e380175SAndreas Petlund 	 * received dupack. Employ only if SACK is supported in order
21467e380175SAndreas Petlund 	 * to avoid possible corner-case series of spurious retransmissions
21477e380175SAndreas Petlund 	 * Use only if there are no unsent data.
21487e380175SAndreas Petlund 	 */
21497e380175SAndreas Petlund 	if ((tp->thin_dupack || sysctl_tcp_thin_dupack) &&
21507e380175SAndreas Petlund 	    tcp_stream_is_thin(tp) && tcp_dupack_heuristics(tp) > 1 &&
21517e380175SAndreas Petlund 	    tcp_is_sack(tp) && !tcp_send_head(sk))
2152a2a385d6SEric Dumazet 		return true;
21537e380175SAndreas Petlund 
2154eed530b6SYuchung Cheng 	/* Trick#6: TCP early retransmit, per RFC5827.  To avoid spurious
2155eed530b6SYuchung Cheng 	 * retransmissions due to small network reorderings, we implement
2156eed530b6SYuchung Cheng 	 * Mitigation A.3 in the RFC and delay the retransmission for a short
2157eed530b6SYuchung Cheng 	 * interval if appropriate.
2158eed530b6SYuchung Cheng 	 */
2159eed530b6SYuchung Cheng 	if (tp->do_early_retrans && !tp->retrans_out && tp->sacked_out &&
21606ba8a3b1SNandita Dukkipati 	    (tp->packets_out >= (tp->sacked_out + 1) && tp->packets_out < 4) &&
2161eed530b6SYuchung Cheng 	    !tcp_may_send_now(sk))
2162750ea2baSYuchung Cheng 		return !tcp_pause_early_retransmit(sk, flag);
2163eed530b6SYuchung Cheng 
2164a2a385d6SEric Dumazet 	return false;
21651da177e4SLinus Torvalds }
21661da177e4SLinus Torvalds 
2167974c1236SYuchung Cheng /* Detect loss in event "A" above by marking head of queue up as lost.
2168974c1236SYuchung Cheng  * For FACK or non-SACK(Reno) senders, the first "packets" number of segments
2169974c1236SYuchung Cheng  * are considered lost. For RFC3517 SACK, a segment is considered lost if it
2170974c1236SYuchung Cheng  * has at least tp->reordering SACKed seqments above it; "packets" refers to
2171974c1236SYuchung Cheng  * the maximum SACKed segments to pass before reaching this limit.
217285cc391cSIlpo Järvinen  */
21731fdb9361SIlpo Järvinen static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)
21741da177e4SLinus Torvalds {
21759e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
21761da177e4SLinus Torvalds 	struct sk_buff *skb;
2177d88270eeSNeal Cardwell 	int cnt, oldcnt, lost;
2178c137f3ddSIlpo Järvinen 	unsigned int mss;
2179974c1236SYuchung Cheng 	/* Use SACK to deduce losses of new sequences sent during recovery */
2180974c1236SYuchung Cheng 	const u32 loss_high = tcp_is_sack(tp) ?  tp->snd_nxt : tp->high_seq;
21811da177e4SLinus Torvalds 
2182547b792cSIlpo Järvinen 	WARN_ON(packets > tp->packets_out);
21836a438bbeSStephen Hemminger 	if (tp->lost_skb_hint) {
21846a438bbeSStephen Hemminger 		skb = tp->lost_skb_hint;
21856a438bbeSStephen Hemminger 		cnt = tp->lost_cnt_hint;
21861fdb9361SIlpo Järvinen 		/* Head already handled? */
21871fdb9361SIlpo Järvinen 		if (mark_head && skb != tcp_write_queue_head(sk))
21881fdb9361SIlpo Järvinen 			return;
21896a438bbeSStephen Hemminger 	} else {
2190fe067e8aSDavid S. Miller 		skb = tcp_write_queue_head(sk);
21916a438bbeSStephen Hemminger 		cnt = 0;
21926a438bbeSStephen Hemminger 	}
21931da177e4SLinus Torvalds 
2194fe067e8aSDavid S. Miller 	tcp_for_write_queue_from(skb, sk) {
2195fe067e8aSDavid S. Miller 		if (skb == tcp_send_head(sk))
2196fe067e8aSDavid S. Miller 			break;
21976a438bbeSStephen Hemminger 		/* TODO: do this better */
21986a438bbeSStephen Hemminger 		/* this is not the most efficient way to do this... */
21996a438bbeSStephen Hemminger 		tp->lost_skb_hint = skb;
22006a438bbeSStephen Hemminger 		tp->lost_cnt_hint = cnt;
220185cc391cSIlpo Järvinen 
2202974c1236SYuchung Cheng 		if (after(TCP_SKB_CB(skb)->end_seq, loss_high))
2203c137f3ddSIlpo Järvinen 			break;
2204c137f3ddSIlpo Järvinen 
2205c137f3ddSIlpo Järvinen 		oldcnt = cnt;
2206ad1984e8SIlpo Järvinen 		if (tcp_is_fack(tp) || tcp_is_reno(tp) ||
220785cc391cSIlpo Järvinen 		    (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
22086a438bbeSStephen Hemminger 			cnt += tcp_skb_pcount(skb);
220985cc391cSIlpo Järvinen 
2210c137f3ddSIlpo Järvinen 		if (cnt > packets) {
2211b3de7559SYuchung Cheng 			if ((tcp_is_sack(tp) && !tcp_is_fack(tp)) ||
2212c0638c24SNeal Cardwell 			    (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||
2213b3de7559SYuchung Cheng 			    (oldcnt >= packets))
22141da177e4SLinus Torvalds 				break;
2215c137f3ddSIlpo Järvinen 
2216f69ad292SEric Dumazet 			mss = tcp_skb_mss(skb);
2217d88270eeSNeal Cardwell 			/* If needed, chop off the prefix to mark as lost. */
2218d88270eeSNeal Cardwell 			lost = (packets - oldcnt) * mss;
2219d88270eeSNeal Cardwell 			if (lost < skb->len &&
2220d88270eeSNeal Cardwell 			    tcp_fragment(sk, skb, lost, mss, GFP_ATOMIC) < 0)
2221c137f3ddSIlpo Järvinen 				break;
2222c137f3ddSIlpo Järvinen 			cnt = packets;
2223c137f3ddSIlpo Järvinen 		}
2224c137f3ddSIlpo Järvinen 
222541ea36e3SIlpo Järvinen 		tcp_skb_mark_lost(tp, skb);
22261fdb9361SIlpo Järvinen 
22271fdb9361SIlpo Järvinen 		if (mark_head)
22281fdb9361SIlpo Järvinen 			break;
22291da177e4SLinus Torvalds 	}
2230005903bcSIlpo Järvinen 	tcp_verify_left_out(tp);
22311da177e4SLinus Torvalds }
22321da177e4SLinus Torvalds 
22331da177e4SLinus Torvalds /* Account newly detected lost packet(s) */
22341da177e4SLinus Torvalds 
223585cc391cSIlpo Järvinen static void tcp_update_scoreboard(struct sock *sk, int fast_rexmit)
22361da177e4SLinus Torvalds {
22379e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
22389e412ba7SIlpo Järvinen 
223985cc391cSIlpo Järvinen 	if (tcp_is_reno(tp)) {
22401fdb9361SIlpo Järvinen 		tcp_mark_head_lost(sk, 1, 1);
224185cc391cSIlpo Järvinen 	} else if (tcp_is_fack(tp)) {
22421da177e4SLinus Torvalds 		int lost = tp->fackets_out - tp->reordering;
22431da177e4SLinus Torvalds 		if (lost <= 0)
22441da177e4SLinus Torvalds 			lost = 1;
22451fdb9361SIlpo Järvinen 		tcp_mark_head_lost(sk, lost, 0);
22461da177e4SLinus Torvalds 	} else {
224785cc391cSIlpo Järvinen 		int sacked_upto = tp->sacked_out - tp->reordering;
22481fdb9361SIlpo Järvinen 		if (sacked_upto >= 0)
22491fdb9361SIlpo Järvinen 			tcp_mark_head_lost(sk, sacked_upto, 0);
22501fdb9361SIlpo Järvinen 		else if (fast_rexmit)
22511fdb9361SIlpo Järvinen 			tcp_mark_head_lost(sk, 1, 1);
22521da177e4SLinus Torvalds 	}
22531da177e4SLinus Torvalds }
22541da177e4SLinus Torvalds 
22551da177e4SLinus Torvalds /* CWND moderation, preventing bursts due to too big ACKs
22561da177e4SLinus Torvalds  * in dubious situations.
22571da177e4SLinus Torvalds  */
22581da177e4SLinus Torvalds static inline void tcp_moderate_cwnd(struct tcp_sock *tp)
22591da177e4SLinus Torvalds {
22601da177e4SLinus Torvalds 	tp->snd_cwnd = min(tp->snd_cwnd,
22611da177e4SLinus Torvalds 			   tcp_packets_in_flight(tp) + tcp_max_burst(tp));
22621da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
22631da177e4SLinus Torvalds }
22641da177e4SLinus Torvalds 
226577c63127SYuchung Cheng static bool tcp_tsopt_ecr_before(const struct tcp_sock *tp, u32 when)
226677c63127SYuchung Cheng {
226777c63127SYuchung Cheng 	return tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
226877c63127SYuchung Cheng 	       before(tp->rx_opt.rcv_tsecr, when);
226977c63127SYuchung Cheng }
227077c63127SYuchung Cheng 
2271659a8ad5SYuchung Cheng /* skb is spurious retransmitted if the returned timestamp echo
2272659a8ad5SYuchung Cheng  * reply is prior to the skb transmission time
2273659a8ad5SYuchung Cheng  */
2274659a8ad5SYuchung Cheng static bool tcp_skb_spurious_retrans(const struct tcp_sock *tp,
2275659a8ad5SYuchung Cheng 				     const struct sk_buff *skb)
2276659a8ad5SYuchung Cheng {
2277659a8ad5SYuchung Cheng 	return (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS) &&
2278659a8ad5SYuchung Cheng 	       tcp_tsopt_ecr_before(tp, tcp_skb_timestamp(skb));
2279659a8ad5SYuchung Cheng }
2280659a8ad5SYuchung Cheng 
22811da177e4SLinus Torvalds /* Nothing was retransmitted or returned timestamp is less
22821da177e4SLinus Torvalds  * than timestamp of the first retransmission.
22831da177e4SLinus Torvalds  */
228467b95bd7SVijay Subramanian static inline bool tcp_packet_delayed(const struct tcp_sock *tp)
22851da177e4SLinus Torvalds {
22861da177e4SLinus Torvalds 	return !tp->retrans_stamp ||
228777c63127SYuchung Cheng 	       tcp_tsopt_ecr_before(tp, tp->retrans_stamp);
22881da177e4SLinus Torvalds }
22891da177e4SLinus Torvalds 
22901da177e4SLinus Torvalds /* Undo procedures. */
22911da177e4SLinus Torvalds 
22921f37bf87SMarcelo Leitner /* We can clear retrans_stamp when there are no retransmissions in the
22931f37bf87SMarcelo Leitner  * window. It would seem that it is trivially available for us in
22941f37bf87SMarcelo Leitner  * tp->retrans_out, however, that kind of assumptions doesn't consider
22951f37bf87SMarcelo Leitner  * what will happen if errors occur when sending retransmission for the
22961f37bf87SMarcelo Leitner  * second time. ...It could the that such segment has only
22971f37bf87SMarcelo Leitner  * TCPCB_EVER_RETRANS set at the present time. It seems that checking
22981f37bf87SMarcelo Leitner  * the head skb is enough except for some reneging corner cases that
22991f37bf87SMarcelo Leitner  * are not worth the effort.
23001f37bf87SMarcelo Leitner  *
23011f37bf87SMarcelo Leitner  * Main reason for all this complexity is the fact that connection dying
23021f37bf87SMarcelo Leitner  * time now depends on the validity of the retrans_stamp, in particular,
23031f37bf87SMarcelo Leitner  * that successive retransmissions of a segment must not advance
23041f37bf87SMarcelo Leitner  * retrans_stamp under any conditions.
23051f37bf87SMarcelo Leitner  */
23061f37bf87SMarcelo Leitner static bool tcp_any_retrans_done(const struct sock *sk)
23071f37bf87SMarcelo Leitner {
23081f37bf87SMarcelo Leitner 	const struct tcp_sock *tp = tcp_sk(sk);
23091f37bf87SMarcelo Leitner 	struct sk_buff *skb;
23101f37bf87SMarcelo Leitner 
23111f37bf87SMarcelo Leitner 	if (tp->retrans_out)
23121f37bf87SMarcelo Leitner 		return true;
23131f37bf87SMarcelo Leitner 
23141f37bf87SMarcelo Leitner 	skb = tcp_write_queue_head(sk);
23151f37bf87SMarcelo Leitner 	if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS))
23161f37bf87SMarcelo Leitner 		return true;
23171f37bf87SMarcelo Leitner 
23181f37bf87SMarcelo Leitner 	return false;
23191f37bf87SMarcelo Leitner }
23201f37bf87SMarcelo Leitner 
23211da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 1
23229e412ba7SIlpo Järvinen static void DBGUNDO(struct sock *sk, const char *msg)
23231da177e4SLinus Torvalds {
23249e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
23251da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
23269e412ba7SIlpo Järvinen 
2327569508c9SYOSHIFUJI Hideaki 	if (sk->sk_family == AF_INET) {
232891df42beSJoe Perches 		pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n",
23291da177e4SLinus Torvalds 			 msg,
2330288fcee8SJoe Perches 			 &inet->inet_daddr, ntohs(inet->inet_dport),
233183ae4088SIlpo Järvinen 			 tp->snd_cwnd, tcp_left_out(tp),
23321da177e4SLinus Torvalds 			 tp->snd_ssthresh, tp->prior_ssthresh,
23331da177e4SLinus Torvalds 			 tp->packets_out);
23341da177e4SLinus Torvalds 	}
2335dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
2336569508c9SYOSHIFUJI Hideaki 	else if (sk->sk_family == AF_INET6) {
2337569508c9SYOSHIFUJI Hideaki 		struct ipv6_pinfo *np = inet6_sk(sk);
233891df42beSJoe Perches 		pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n",
2339569508c9SYOSHIFUJI Hideaki 			 msg,
2340288fcee8SJoe Perches 			 &np->daddr, ntohs(inet->inet_dport),
2341569508c9SYOSHIFUJI Hideaki 			 tp->snd_cwnd, tcp_left_out(tp),
2342569508c9SYOSHIFUJI Hideaki 			 tp->snd_ssthresh, tp->prior_ssthresh,
2343569508c9SYOSHIFUJI Hideaki 			 tp->packets_out);
2344569508c9SYOSHIFUJI Hideaki 	}
2345569508c9SYOSHIFUJI Hideaki #endif
2346569508c9SYOSHIFUJI Hideaki }
23471da177e4SLinus Torvalds #else
23481da177e4SLinus Torvalds #define DBGUNDO(x...) do { } while (0)
23491da177e4SLinus Torvalds #endif
23501da177e4SLinus Torvalds 
23517026b912SYuchung Cheng static void tcp_undo_cwnd_reduction(struct sock *sk, bool unmark_loss)
23521da177e4SLinus Torvalds {
23536687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
23546687e988SArnaldo Carvalho de Melo 
23556a63df46SYuchung Cheng 	if (unmark_loss) {
23566a63df46SYuchung Cheng 		struct sk_buff *skb;
23576a63df46SYuchung Cheng 
23586a63df46SYuchung Cheng 		tcp_for_write_queue(skb, sk) {
23596a63df46SYuchung Cheng 			if (skb == tcp_send_head(sk))
23606a63df46SYuchung Cheng 				break;
23616a63df46SYuchung Cheng 			TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
23626a63df46SYuchung Cheng 		}
23636a63df46SYuchung Cheng 		tp->lost_out = 0;
23646a63df46SYuchung Cheng 		tcp_clear_all_retrans_hints(tp);
23656a63df46SYuchung Cheng 	}
23666a63df46SYuchung Cheng 
23671da177e4SLinus Torvalds 	if (tp->prior_ssthresh) {
23686687e988SArnaldo Carvalho de Melo 		const struct inet_connection_sock *icsk = inet_csk(sk);
23696687e988SArnaldo Carvalho de Melo 
23706687e988SArnaldo Carvalho de Melo 		if (icsk->icsk_ca_ops->undo_cwnd)
23716687e988SArnaldo Carvalho de Melo 			tp->snd_cwnd = icsk->icsk_ca_ops->undo_cwnd(sk);
23721da177e4SLinus Torvalds 		else
23731da177e4SLinus Torvalds 			tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh << 1);
23741da177e4SLinus Torvalds 
23757026b912SYuchung Cheng 		if (tp->prior_ssthresh > tp->snd_ssthresh) {
23761da177e4SLinus Torvalds 			tp->snd_ssthresh = tp->prior_ssthresh;
2377735d3831SFlorian Westphal 			tcp_ecn_withdraw_cwr(tp);
23781da177e4SLinus Torvalds 		}
23791da177e4SLinus Torvalds 	}
23801da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
23816a63df46SYuchung Cheng 	tp->undo_marker = 0;
23821da177e4SLinus Torvalds }
23831da177e4SLinus Torvalds 
238467b95bd7SVijay Subramanian static inline bool tcp_may_undo(const struct tcp_sock *tp)
23851da177e4SLinus Torvalds {
2386056834d9SIlpo Järvinen 	return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp));
23871da177e4SLinus Torvalds }
23881da177e4SLinus Torvalds 
23891da177e4SLinus Torvalds /* People celebrate: "We love our President!" */
2390a2a385d6SEric Dumazet static bool tcp_try_undo_recovery(struct sock *sk)
23911da177e4SLinus Torvalds {
23929e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
23939e412ba7SIlpo Järvinen 
23941da177e4SLinus Torvalds 	if (tcp_may_undo(tp)) {
239540b215e5SPavel Emelyanov 		int mib_idx;
239640b215e5SPavel Emelyanov 
23971da177e4SLinus Torvalds 		/* Happy end! We did not retransmit anything
23981da177e4SLinus Torvalds 		 * or our original transmission succeeded.
23991da177e4SLinus Torvalds 		 */
24009e412ba7SIlpo Järvinen 		DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans");
24017026b912SYuchung Cheng 		tcp_undo_cwnd_reduction(sk, false);
24026687e988SArnaldo Carvalho de Melo 		if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss)
240340b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPLOSSUNDO;
24041da177e4SLinus Torvalds 		else
240540b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPFULLUNDO;
240640b215e5SPavel Emelyanov 
2407de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), mib_idx);
24081da177e4SLinus Torvalds 	}
2409e60402d0SIlpo Järvinen 	if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) {
24101da177e4SLinus Torvalds 		/* Hold old state until something *above* high_seq
24111da177e4SLinus Torvalds 		 * is ACKed. For Reno it is MUST to prevent false
24121da177e4SLinus Torvalds 		 * fast retransmits (RFC2582). SACK TCP is safe. */
24131da177e4SLinus Torvalds 		tcp_moderate_cwnd(tp);
24141f37bf87SMarcelo Leitner 		if (!tcp_any_retrans_done(sk))
24151f37bf87SMarcelo Leitner 			tp->retrans_stamp = 0;
2416a2a385d6SEric Dumazet 		return true;
24171da177e4SLinus Torvalds 	}
24186687e988SArnaldo Carvalho de Melo 	tcp_set_ca_state(sk, TCP_CA_Open);
2419a2a385d6SEric Dumazet 	return false;
24201da177e4SLinus Torvalds }
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds /* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */
2423c7d9d6a1SYuchung Cheng static bool tcp_try_undo_dsack(struct sock *sk)
24241da177e4SLinus Torvalds {
24259e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
24269e412ba7SIlpo Järvinen 
24271da177e4SLinus Torvalds 	if (tp->undo_marker && !tp->undo_retrans) {
24289e412ba7SIlpo Järvinen 		DBGUNDO(sk, "D-SACK");
24297026b912SYuchung Cheng 		tcp_undo_cwnd_reduction(sk, false);
2430de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKUNDO);
2431c7d9d6a1SYuchung Cheng 		return true;
24321da177e4SLinus Torvalds 	}
2433c7d9d6a1SYuchung Cheng 	return false;
24341da177e4SLinus Torvalds }
24351da177e4SLinus Torvalds 
2436e33099f9SYuchung Cheng /* Undo during loss recovery after partial ACK or using F-RTO. */
2437e33099f9SYuchung Cheng static bool tcp_try_undo_loss(struct sock *sk, bool frto_undo)
24381da177e4SLinus Torvalds {
24399e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
24409e412ba7SIlpo Järvinen 
2441e33099f9SYuchung Cheng 	if (frto_undo || tcp_may_undo(tp)) {
24427026b912SYuchung Cheng 		tcp_undo_cwnd_reduction(sk, true);
24436a438bbeSStephen Hemminger 
24449e412ba7SIlpo Järvinen 		DBGUNDO(sk, "partial loss");
2445de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSSUNDO);
2446e33099f9SYuchung Cheng 		if (frto_undo)
2447e33099f9SYuchung Cheng 			NET_INC_STATS_BH(sock_net(sk),
2448e33099f9SYuchung Cheng 					 LINUX_MIB_TCPSPURIOUSRTOS);
2449463c84b9SArnaldo Carvalho de Melo 		inet_csk(sk)->icsk_retransmits = 0;
2450e33099f9SYuchung Cheng 		if (frto_undo || tcp_is_sack(tp))
24516687e988SArnaldo Carvalho de Melo 			tcp_set_ca_state(sk, TCP_CA_Open);
2452a2a385d6SEric Dumazet 		return true;
24531da177e4SLinus Torvalds 	}
2454a2a385d6SEric Dumazet 	return false;
24551da177e4SLinus Torvalds }
24561da177e4SLinus Torvalds 
24573759824dSYuchung Cheng /* The cwnd reduction in CWR and Recovery uses the PRR algorithm in RFC 6937.
2458fb4d3d1dSYuchung Cheng  * It computes the number of packets to send (sndcnt) based on packets newly
2459fb4d3d1dSYuchung Cheng  * delivered:
2460fb4d3d1dSYuchung Cheng  *   1) If the packets in flight is larger than ssthresh, PRR spreads the
2461fb4d3d1dSYuchung Cheng  *	cwnd reductions across a full RTT.
24623759824dSYuchung Cheng  *   2) Otherwise PRR uses packet conservation to send as much as delivered.
24633759824dSYuchung Cheng  *      But when the retransmits are acked without further losses, PRR
24643759824dSYuchung Cheng  *      slow starts cwnd up to ssthresh to speed up the recovery.
2465fb4d3d1dSYuchung Cheng  */
24665ee2c941SChristoph Paasch static void tcp_init_cwnd_reduction(struct sock *sk)
2467684bad11SYuchung Cheng {
2468684bad11SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
2469684bad11SYuchung Cheng 
2470684bad11SYuchung Cheng 	tp->high_seq = tp->snd_nxt;
24719b717a8dSNandita Dukkipati 	tp->tlp_high_seq = 0;
2472684bad11SYuchung Cheng 	tp->snd_cwnd_cnt = 0;
2473684bad11SYuchung Cheng 	tp->prior_cwnd = tp->snd_cwnd;
2474684bad11SYuchung Cheng 	tp->prr_delivered = 0;
2475684bad11SYuchung Cheng 	tp->prr_out = 0;
2476684bad11SYuchung Cheng 	tp->snd_ssthresh = inet_csk(sk)->icsk_ca_ops->ssthresh(sk);
2477735d3831SFlorian Westphal 	tcp_ecn_queue_cwr(tp);
2478684bad11SYuchung Cheng }
2479684bad11SYuchung Cheng 
248031ba0c10SYuchung Cheng static void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked,
248131ba0c10SYuchung Cheng 			       int flag)
2482fb4d3d1dSYuchung Cheng {
2483fb4d3d1dSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
2484fb4d3d1dSYuchung Cheng 	int sndcnt = 0;
2485fb4d3d1dSYuchung Cheng 	int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp);
2486fb4d3d1dSYuchung Cheng 
24878b8a321fSYuchung Cheng 	if (newly_acked_sacked <= 0 || WARN_ON_ONCE(!tp->prior_cwnd))
24888b8a321fSYuchung Cheng 		return;
24898b8a321fSYuchung Cheng 
2490684bad11SYuchung Cheng 	tp->prr_delivered += newly_acked_sacked;
24913759824dSYuchung Cheng 	if (delta < 0) {
2492fb4d3d1dSYuchung Cheng 		u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered +
2493fb4d3d1dSYuchung Cheng 			       tp->prior_cwnd - 1;
2494fb4d3d1dSYuchung Cheng 		sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out;
24953759824dSYuchung Cheng 	} else if ((flag & FLAG_RETRANS_DATA_ACKED) &&
24963759824dSYuchung Cheng 		   !(flag & FLAG_LOST_RETRANS)) {
2497fb4d3d1dSYuchung Cheng 		sndcnt = min_t(int, delta,
2498fb4d3d1dSYuchung Cheng 			       max_t(int, tp->prr_delivered - tp->prr_out,
2499fb4d3d1dSYuchung Cheng 				     newly_acked_sacked) + 1);
25003759824dSYuchung Cheng 	} else {
25013759824dSYuchung Cheng 		sndcnt = min(delta, newly_acked_sacked);
2502fb4d3d1dSYuchung Cheng 	}
250331ba0c10SYuchung Cheng 	/* Force a fast retransmit upon entering fast recovery */
250431ba0c10SYuchung Cheng 	sndcnt = max(sndcnt, (tp->prr_out ? 0 : 1));
2505fb4d3d1dSYuchung Cheng 	tp->snd_cwnd = tcp_packets_in_flight(tp) + sndcnt;
2506fb4d3d1dSYuchung Cheng }
2507fb4d3d1dSYuchung Cheng 
2508684bad11SYuchung Cheng static inline void tcp_end_cwnd_reduction(struct sock *sk)
25091da177e4SLinus Torvalds {
25106687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
2511a262f0cdSNandita Dukkipati 
2512684bad11SYuchung Cheng 	/* Reset cwnd to ssthresh in CWR or Recovery (unless it's undone) */
2513684bad11SYuchung Cheng 	if (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR ||
2514684bad11SYuchung Cheng 	    (tp->undo_marker && tp->snd_ssthresh < TCP_INFINITE_SSTHRESH)) {
251567d4120aSYuchung Cheng 		tp->snd_cwnd = tp->snd_ssthresh;
25161da177e4SLinus Torvalds 		tp->snd_cwnd_stamp = tcp_time_stamp;
251767d4120aSYuchung Cheng 	}
25186687e988SArnaldo Carvalho de Melo 	tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR);
25191da177e4SLinus Torvalds }
25201da177e4SLinus Torvalds 
2521684bad11SYuchung Cheng /* Enter CWR state. Disable cwnd undo since congestion is proven with ECN */
25225ee2c941SChristoph Paasch void tcp_enter_cwr(struct sock *sk)
252309484d1fSYuchung Cheng {
252409484d1fSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
252509484d1fSYuchung Cheng 
252609484d1fSYuchung Cheng 	tp->prior_ssthresh = 0;
2527684bad11SYuchung Cheng 	if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
252809484d1fSYuchung Cheng 		tp->undo_marker = 0;
25295ee2c941SChristoph Paasch 		tcp_init_cwnd_reduction(sk);
253009484d1fSYuchung Cheng 		tcp_set_ca_state(sk, TCP_CA_CWR);
253109484d1fSYuchung Cheng 	}
253209484d1fSYuchung Cheng }
25337782ad8bSKenneth Klette Jonassen EXPORT_SYMBOL(tcp_enter_cwr);
253409484d1fSYuchung Cheng 
25358aca6cb1SIlpo Järvinen static void tcp_try_keep_open(struct sock *sk)
25368aca6cb1SIlpo Järvinen {
25378aca6cb1SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
25388aca6cb1SIlpo Järvinen 	int state = TCP_CA_Open;
25398aca6cb1SIlpo Järvinen 
2540f698204bSNeal Cardwell 	if (tcp_left_out(tp) || tcp_any_retrans_done(sk))
25418aca6cb1SIlpo Järvinen 		state = TCP_CA_Disorder;
25428aca6cb1SIlpo Järvinen 
25438aca6cb1SIlpo Järvinen 	if (inet_csk(sk)->icsk_ca_state != state) {
25448aca6cb1SIlpo Järvinen 		tcp_set_ca_state(sk, state);
25458aca6cb1SIlpo Järvinen 		tp->high_seq = tp->snd_nxt;
25468aca6cb1SIlpo Järvinen 	}
25478aca6cb1SIlpo Järvinen }
25488aca6cb1SIlpo Järvinen 
254931ba0c10SYuchung Cheng static void tcp_try_to_open(struct sock *sk, int flag)
25501da177e4SLinus Torvalds {
25519e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
25529e412ba7SIlpo Järvinen 
255386426c22SIlpo Järvinen 	tcp_verify_left_out(tp);
255486426c22SIlpo Järvinen 
25559b44190dSYuchung Cheng 	if (!tcp_any_retrans_done(sk))
25561da177e4SLinus Torvalds 		tp->retrans_stamp = 0;
25571da177e4SLinus Torvalds 
25581da177e4SLinus Torvalds 	if (flag & FLAG_ECE)
25595ee2c941SChristoph Paasch 		tcp_enter_cwr(sk);
25601da177e4SLinus Torvalds 
25616687e988SArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) {
25628aca6cb1SIlpo Järvinen 		tcp_try_keep_open(sk);
25631da177e4SLinus Torvalds 	}
25641da177e4SLinus Torvalds }
25651da177e4SLinus Torvalds 
25665d424d5aSJohn Heffner static void tcp_mtup_probe_failed(struct sock *sk)
25675d424d5aSJohn Heffner {
25685d424d5aSJohn Heffner 	struct inet_connection_sock *icsk = inet_csk(sk);
25695d424d5aSJohn Heffner 
25705d424d5aSJohn Heffner 	icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1;
25715d424d5aSJohn Heffner 	icsk->icsk_mtup.probe_size = 0;
2572b56ea298SRick Jones 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMTUPFAIL);
25735d424d5aSJohn Heffner }
25745d424d5aSJohn Heffner 
257572211e90SIlpo Järvinen static void tcp_mtup_probe_success(struct sock *sk)
25765d424d5aSJohn Heffner {
25775d424d5aSJohn Heffner 	struct tcp_sock *tp = tcp_sk(sk);
25785d424d5aSJohn Heffner 	struct inet_connection_sock *icsk = inet_csk(sk);
25795d424d5aSJohn Heffner 
25805d424d5aSJohn Heffner 	/* FIXME: breaks with very large cwnd */
25815d424d5aSJohn Heffner 	tp->prior_ssthresh = tcp_current_ssthresh(sk);
25825d424d5aSJohn Heffner 	tp->snd_cwnd = tp->snd_cwnd *
25835d424d5aSJohn Heffner 		       tcp_mss_to_mtu(sk, tp->mss_cache) /
25845d424d5aSJohn Heffner 		       icsk->icsk_mtup.probe_size;
25855d424d5aSJohn Heffner 	tp->snd_cwnd_cnt = 0;
25865d424d5aSJohn Heffner 	tp->snd_cwnd_stamp = tcp_time_stamp;
25879c6d5e55SJohn Heffner 	tp->snd_ssthresh = tcp_current_ssthresh(sk);
25885d424d5aSJohn Heffner 
25895d424d5aSJohn Heffner 	icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size;
25905d424d5aSJohn Heffner 	icsk->icsk_mtup.probe_size = 0;
25915d424d5aSJohn Heffner 	tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
2592b56ea298SRick Jones 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMTUPSUCCESS);
25935d424d5aSJohn Heffner }
25945d424d5aSJohn Heffner 
2595e1aa680fSIlpo Järvinen /* Do a simple retransmit without using the backoff mechanisms in
2596e1aa680fSIlpo Järvinen  * tcp_timer. This is used for path mtu discovery.
2597e1aa680fSIlpo Järvinen  * The socket is already locked here.
2598e1aa680fSIlpo Järvinen  */
2599e1aa680fSIlpo Järvinen void tcp_simple_retransmit(struct sock *sk)
2600e1aa680fSIlpo Järvinen {
2601e1aa680fSIlpo Järvinen 	const struct inet_connection_sock *icsk = inet_csk(sk);
2602e1aa680fSIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
2603e1aa680fSIlpo Järvinen 	struct sk_buff *skb;
26040c54b85fSIlpo Järvinen 	unsigned int mss = tcp_current_mss(sk);
2605e1aa680fSIlpo Järvinen 	u32 prior_lost = tp->lost_out;
2606e1aa680fSIlpo Järvinen 
2607e1aa680fSIlpo Järvinen 	tcp_for_write_queue(skb, sk) {
2608e1aa680fSIlpo Järvinen 		if (skb == tcp_send_head(sk))
2609e1aa680fSIlpo Järvinen 			break;
2610775ffabfSIlpo Järvinen 		if (tcp_skb_seglen(skb) > mss &&
2611e1aa680fSIlpo Järvinen 		    !(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) {
2612e1aa680fSIlpo Järvinen 			if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
2613e1aa680fSIlpo Järvinen 				TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
2614e1aa680fSIlpo Järvinen 				tp->retrans_out -= tcp_skb_pcount(skb);
2615e1aa680fSIlpo Järvinen 			}
2616e1aa680fSIlpo Järvinen 			tcp_skb_mark_lost_uncond_verify(tp, skb);
2617e1aa680fSIlpo Järvinen 		}
2618e1aa680fSIlpo Järvinen 	}
2619e1aa680fSIlpo Järvinen 
2620e1aa680fSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
2621e1aa680fSIlpo Järvinen 
2622e1aa680fSIlpo Järvinen 	if (prior_lost == tp->lost_out)
2623e1aa680fSIlpo Järvinen 		return;
2624e1aa680fSIlpo Järvinen 
2625e1aa680fSIlpo Järvinen 	if (tcp_is_reno(tp))
2626e1aa680fSIlpo Järvinen 		tcp_limit_reno_sacked(tp);
2627e1aa680fSIlpo Järvinen 
2628e1aa680fSIlpo Järvinen 	tcp_verify_left_out(tp);
2629e1aa680fSIlpo Järvinen 
2630e1aa680fSIlpo Järvinen 	/* Don't muck with the congestion window here.
2631e1aa680fSIlpo Järvinen 	 * Reason is that we do not increase amount of _data_
2632e1aa680fSIlpo Järvinen 	 * in network, but units changed and effective
2633e1aa680fSIlpo Järvinen 	 * cwnd/ssthresh really reduced now.
2634e1aa680fSIlpo Järvinen 	 */
2635e1aa680fSIlpo Järvinen 	if (icsk->icsk_ca_state != TCP_CA_Loss) {
2636e1aa680fSIlpo Järvinen 		tp->high_seq = tp->snd_nxt;
2637e1aa680fSIlpo Järvinen 		tp->snd_ssthresh = tcp_current_ssthresh(sk);
2638e1aa680fSIlpo Järvinen 		tp->prior_ssthresh = 0;
2639e1aa680fSIlpo Järvinen 		tp->undo_marker = 0;
2640e1aa680fSIlpo Järvinen 		tcp_set_ca_state(sk, TCP_CA_Loss);
2641e1aa680fSIlpo Järvinen 	}
2642e1aa680fSIlpo Järvinen 	tcp_xmit_retransmit_queue(sk);
2643e1aa680fSIlpo Järvinen }
26444bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_simple_retransmit);
2645e1aa680fSIlpo Järvinen 
26461fbc3405SYuchung Cheng static void tcp_enter_recovery(struct sock *sk, bool ece_ack)
26471fbc3405SYuchung Cheng {
26481fbc3405SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
26491fbc3405SYuchung Cheng 	int mib_idx;
26501fbc3405SYuchung Cheng 
26511fbc3405SYuchung Cheng 	if (tcp_is_reno(tp))
26521fbc3405SYuchung Cheng 		mib_idx = LINUX_MIB_TCPRENORECOVERY;
26531fbc3405SYuchung Cheng 	else
26541fbc3405SYuchung Cheng 		mib_idx = LINUX_MIB_TCPSACKRECOVERY;
26551fbc3405SYuchung Cheng 
26561fbc3405SYuchung Cheng 	NET_INC_STATS_BH(sock_net(sk), mib_idx);
26571fbc3405SYuchung Cheng 
26581fbc3405SYuchung Cheng 	tp->prior_ssthresh = 0;
2659989e04c5SYuchung Cheng 	tcp_init_undo(tp);
26601fbc3405SYuchung Cheng 
2661291a00d1SYuchung Cheng 	if (!tcp_in_cwnd_reduction(sk)) {
26621fbc3405SYuchung Cheng 		if (!ece_ack)
26631fbc3405SYuchung Cheng 			tp->prior_ssthresh = tcp_current_ssthresh(sk);
26645ee2c941SChristoph Paasch 		tcp_init_cwnd_reduction(sk);
26651fbc3405SYuchung Cheng 	}
26661fbc3405SYuchung Cheng 	tcp_set_ca_state(sk, TCP_CA_Recovery);
26671fbc3405SYuchung Cheng }
26681fbc3405SYuchung Cheng 
2669ab42d9eeSYuchung Cheng /* Process an ACK in CA_Loss state. Move to CA_Open if lost data are
2670ab42d9eeSYuchung Cheng  * recovered or spurious. Otherwise retransmits more on partial ACKs.
2671ab42d9eeSYuchung Cheng  */
2672e662ca40SYuchung Cheng static void tcp_process_loss(struct sock *sk, int flag, bool is_dupack,
2673e662ca40SYuchung Cheng 			     int *rexmit)
2674ab42d9eeSYuchung Cheng {
2675ab42d9eeSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
2676e33099f9SYuchung Cheng 	bool recovered = !before(tp->snd_una, tp->high_seq);
2677ab42d9eeSYuchung Cheng 
2678da34ac76SYuchung Cheng 	if ((flag & FLAG_SND_UNA_ADVANCED) &&
2679da34ac76SYuchung Cheng 	    tcp_try_undo_loss(sk, false))
2680da34ac76SYuchung Cheng 		return;
2681da34ac76SYuchung Cheng 
2682e33099f9SYuchung Cheng 	if (tp->frto) { /* F-RTO RFC5682 sec 3.1 (sack enhanced version). */
2683e33099f9SYuchung Cheng 		/* Step 3.b. A timeout is spurious if not all data are
2684e33099f9SYuchung Cheng 		 * lost, i.e., never-retransmitted data are (s)acked.
2685e33099f9SYuchung Cheng 		 */
2686da34ac76SYuchung Cheng 		if ((flag & FLAG_ORIG_SACK_ACKED) &&
2687da34ac76SYuchung Cheng 		    tcp_try_undo_loss(sk, true))
2688e33099f9SYuchung Cheng 			return;
26890cfa5c07SYuchung Cheng 
2690b7b0ed91SYuchung Cheng 		if (after(tp->snd_nxt, tp->high_seq)) {
2691b7b0ed91SYuchung Cheng 			if (flag & FLAG_DATA_SACKED || is_dupack)
2692b7b0ed91SYuchung Cheng 				tp->frto = 0; /* Step 3.a. loss was real */
2693e33099f9SYuchung Cheng 		} else if (flag & FLAG_SND_UNA_ADVANCED && !recovered) {
2694e33099f9SYuchung Cheng 			tp->high_seq = tp->snd_nxt;
2695e662ca40SYuchung Cheng 			/* Step 2.b. Try send new data (but deferred until cwnd
2696e662ca40SYuchung Cheng 			 * is updated in tcp_ack()). Otherwise fall back to
2697e662ca40SYuchung Cheng 			 * the conventional recovery.
2698e662ca40SYuchung Cheng 			 */
2699e662ca40SYuchung Cheng 			if (tcp_send_head(sk) &&
2700e662ca40SYuchung Cheng 			    after(tcp_wnd_end(tp), tp->snd_nxt)) {
2701e662ca40SYuchung Cheng 				*rexmit = REXMIT_NEW;
2702e662ca40SYuchung Cheng 				return;
2703e662ca40SYuchung Cheng 			}
2704e33099f9SYuchung Cheng 			tp->frto = 0;
2705e33099f9SYuchung Cheng 		}
2706e33099f9SYuchung Cheng 	}
2707e33099f9SYuchung Cheng 
2708e33099f9SYuchung Cheng 	if (recovered) {
2709e33099f9SYuchung Cheng 		/* F-RTO RFC5682 sec 3.1 step 2.a and 1st part of step 3.a */
2710ab42d9eeSYuchung Cheng 		tcp_try_undo_recovery(sk);
2711ab42d9eeSYuchung Cheng 		return;
2712ab42d9eeSYuchung Cheng 	}
2713e33099f9SYuchung Cheng 	if (tcp_is_reno(tp)) {
2714e33099f9SYuchung Cheng 		/* A Reno DUPACK means new data in F-RTO step 2.b above are
2715e33099f9SYuchung Cheng 		 * delivered. Lower inflight to clock out (re)tranmissions.
2716e33099f9SYuchung Cheng 		 */
2717e33099f9SYuchung Cheng 		if (after(tp->snd_nxt, tp->high_seq) && is_dupack)
2718e33099f9SYuchung Cheng 			tcp_add_reno_sack(sk);
2719e33099f9SYuchung Cheng 		else if (flag & FLAG_SND_UNA_ADVANCED)
2720ab42d9eeSYuchung Cheng 			tcp_reset_reno_sack(tp);
2721e33099f9SYuchung Cheng 	}
2722e662ca40SYuchung Cheng 	*rexmit = REXMIT_LOST;
2723ab42d9eeSYuchung Cheng }
2724ab42d9eeSYuchung Cheng 
27256a63df46SYuchung Cheng /* Undo during fast recovery after partial ACK. */
272631ba0c10SYuchung Cheng static bool tcp_try_undo_partial(struct sock *sk, const int acked)
27276a63df46SYuchung Cheng {
27286a63df46SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
27296a63df46SYuchung Cheng 
27307026b912SYuchung Cheng 	if (tp->undo_marker && tcp_packet_delayed(tp)) {
27316a63df46SYuchung Cheng 		/* Plain luck! Hole if filled with delayed
27326a63df46SYuchung Cheng 		 * packet, rather than with a retransmit.
27336a63df46SYuchung Cheng 		 */
27347026b912SYuchung Cheng 		tcp_update_reordering(sk, tcp_fackets_out(tp) + acked, 1);
27357026b912SYuchung Cheng 
27367026b912SYuchung Cheng 		/* We are getting evidence that the reordering degree is higher
27377026b912SYuchung Cheng 		 * than we realized. If there are no retransmits out then we
27387026b912SYuchung Cheng 		 * can undo. Otherwise we clock out new packets but do not
27397026b912SYuchung Cheng 		 * mark more packets lost or retransmit more.
27407026b912SYuchung Cheng 		 */
274131ba0c10SYuchung Cheng 		if (tp->retrans_out)
27427026b912SYuchung Cheng 			return true;
27437026b912SYuchung Cheng 
27446a63df46SYuchung Cheng 		if (!tcp_any_retrans_done(sk))
27456a63df46SYuchung Cheng 			tp->retrans_stamp = 0;
27466a63df46SYuchung Cheng 
27477026b912SYuchung Cheng 		DBGUNDO(sk, "partial recovery");
27487026b912SYuchung Cheng 		tcp_undo_cwnd_reduction(sk, true);
27496a63df46SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO);
27507026b912SYuchung Cheng 		tcp_try_keep_open(sk);
27517026b912SYuchung Cheng 		return true;
27526a63df46SYuchung Cheng 	}
27537026b912SYuchung Cheng 	return false;
27546a63df46SYuchung Cheng }
27556a63df46SYuchung Cheng 
27561da177e4SLinus Torvalds /* Process an event, which can update packets-in-flight not trivially.
27571da177e4SLinus Torvalds  * Main goal of this function is to calculate new estimate for left_out,
27581da177e4SLinus Torvalds  * taking into account both packets sitting in receiver's buffer and
27591da177e4SLinus Torvalds  * packets lost by network.
27601da177e4SLinus Torvalds  *
276131ba0c10SYuchung Cheng  * Besides that it updates the congestion state when packet loss or ECN
276231ba0c10SYuchung Cheng  * is detected. But it does not reduce the cwnd, it is done by the
276331ba0c10SYuchung Cheng  * congestion control later.
27641da177e4SLinus Torvalds  *
27651da177e4SLinus Torvalds  * It does _not_ decide what to send, it is made in function
27661da177e4SLinus Torvalds  * tcp_xmit_retransmit_queue().
27671da177e4SLinus Torvalds  */
27686804973fSYuchung Cheng static void tcp_fastretrans_alert(struct sock *sk, const int acked,
276931ba0c10SYuchung Cheng 				  bool is_dupack, int *ack_flag, int *rexmit)
27701da177e4SLinus Torvalds {
27716687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
27721da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
277331ba0c10SYuchung Cheng 	int fast_rexmit = 0, flag = *ack_flag;
27746a63df46SYuchung Cheng 	bool do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) &&
277585cc391cSIlpo Järvinen 				    (tcp_fackets_out(tp) > tp->reordering));
27761da177e4SLinus Torvalds 
27773ccd3130SIlpo Järvinen 	if (WARN_ON(!tp->packets_out && tp->sacked_out))
27781da177e4SLinus Torvalds 		tp->sacked_out = 0;
277991fed7a1SIlpo Järvinen 	if (WARN_ON(!tp->sacked_out && tp->fackets_out))
27801da177e4SLinus Torvalds 		tp->fackets_out = 0;
27811da177e4SLinus Torvalds 
27821da177e4SLinus Torvalds 	/* Now state machine starts.
27831da177e4SLinus Torvalds 	 * A. ECE, hence prohibit cwnd undoing, the reduction is required. */
27841da177e4SLinus Torvalds 	if (flag & FLAG_ECE)
27851da177e4SLinus Torvalds 		tp->prior_ssthresh = 0;
27861da177e4SLinus Torvalds 
27871da177e4SLinus Torvalds 	/* B. In all the states check for reneging SACKs. */
2788cadbd031SIlpo Järvinen 	if (tcp_check_sack_reneging(sk, flag))
27891da177e4SLinus Torvalds 		return;
27901da177e4SLinus Torvalds 
2791974c1236SYuchung Cheng 	/* C. Check consistency of the current state. */
2792005903bcSIlpo Järvinen 	tcp_verify_left_out(tp);
27931da177e4SLinus Torvalds 
2794974c1236SYuchung Cheng 	/* D. Check state exit conditions. State can be terminated
27951da177e4SLinus Torvalds 	 *    when high_seq is ACKed. */
27966687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_state == TCP_CA_Open) {
2797547b792cSIlpo Järvinen 		WARN_ON(tp->retrans_out != 0);
27981da177e4SLinus Torvalds 		tp->retrans_stamp = 0;
27991da177e4SLinus Torvalds 	} else if (!before(tp->snd_una, tp->high_seq)) {
28006687e988SArnaldo Carvalho de Melo 		switch (icsk->icsk_ca_state) {
28011da177e4SLinus Torvalds 		case TCP_CA_CWR:
28021da177e4SLinus Torvalds 			/* CWR is to be held something *above* high_seq
28031da177e4SLinus Torvalds 			 * is ACKed for CWR bit to reach receiver. */
28041da177e4SLinus Torvalds 			if (tp->snd_una != tp->high_seq) {
2805684bad11SYuchung Cheng 				tcp_end_cwnd_reduction(sk);
28066687e988SArnaldo Carvalho de Melo 				tcp_set_ca_state(sk, TCP_CA_Open);
28071da177e4SLinus Torvalds 			}
28081da177e4SLinus Torvalds 			break;
28091da177e4SLinus Torvalds 
28101da177e4SLinus Torvalds 		case TCP_CA_Recovery:
2811e60402d0SIlpo Järvinen 			if (tcp_is_reno(tp))
28121da177e4SLinus Torvalds 				tcp_reset_reno_sack(tp);
28139e412ba7SIlpo Järvinen 			if (tcp_try_undo_recovery(sk))
28141da177e4SLinus Torvalds 				return;
2815684bad11SYuchung Cheng 			tcp_end_cwnd_reduction(sk);
28161da177e4SLinus Torvalds 			break;
28171da177e4SLinus Torvalds 		}
28181da177e4SLinus Torvalds 	}
28191da177e4SLinus Torvalds 
28204f41b1c5SYuchung Cheng 	/* Use RACK to detect loss */
28214f41b1c5SYuchung Cheng 	if (sysctl_tcp_recovery & TCP_RACK_LOST_RETRANS &&
282231ba0c10SYuchung Cheng 	    tcp_rack_mark_lost(sk)) {
28234f41b1c5SYuchung Cheng 		flag |= FLAG_LOST_RETRANS;
282431ba0c10SYuchung Cheng 		*ack_flag |= FLAG_LOST_RETRANS;
282531ba0c10SYuchung Cheng 	}
28264f41b1c5SYuchung Cheng 
2827974c1236SYuchung Cheng 	/* E. Process state. */
28286687e988SArnaldo Carvalho de Melo 	switch (icsk->icsk_ca_state) {
28291da177e4SLinus Torvalds 	case TCP_CA_Recovery:
28302e605294SIlpo Järvinen 		if (!(flag & FLAG_SND_UNA_ADVANCED)) {
2831e60402d0SIlpo Järvinen 			if (tcp_is_reno(tp) && is_dupack)
28326687e988SArnaldo Carvalho de Melo 				tcp_add_reno_sack(sk);
28337026b912SYuchung Cheng 		} else {
283431ba0c10SYuchung Cheng 			if (tcp_try_undo_partial(sk, acked))
28357026b912SYuchung Cheng 				return;
28367026b912SYuchung Cheng 			/* Partial ACK arrived. Force fast retransmit. */
28377026b912SYuchung Cheng 			do_lost = tcp_is_reno(tp) ||
28387026b912SYuchung Cheng 				  tcp_fackets_out(tp) > tp->reordering;
28397026b912SYuchung Cheng 		}
2840c7d9d6a1SYuchung Cheng 		if (tcp_try_undo_dsack(sk)) {
2841c7d9d6a1SYuchung Cheng 			tcp_try_keep_open(sk);
2842c7d9d6a1SYuchung Cheng 			return;
2843c7d9d6a1SYuchung Cheng 		}
28441da177e4SLinus Torvalds 		break;
28451da177e4SLinus Torvalds 	case TCP_CA_Loss:
2846e662ca40SYuchung Cheng 		tcp_process_loss(sk, flag, is_dupack, rexmit);
2847291a00d1SYuchung Cheng 		if (icsk->icsk_ca_state != TCP_CA_Open &&
2848291a00d1SYuchung Cheng 		    !(flag & FLAG_LOST_RETRANS))
28491da177e4SLinus Torvalds 			return;
2850291a00d1SYuchung Cheng 		/* Change state if cwnd is undone or retransmits are lost */
28511da177e4SLinus Torvalds 	default:
2852e60402d0SIlpo Järvinen 		if (tcp_is_reno(tp)) {
28532e605294SIlpo Järvinen 			if (flag & FLAG_SND_UNA_ADVANCED)
28541da177e4SLinus Torvalds 				tcp_reset_reno_sack(tp);
28551da177e4SLinus Torvalds 			if (is_dupack)
28566687e988SArnaldo Carvalho de Melo 				tcp_add_reno_sack(sk);
28571da177e4SLinus Torvalds 		}
28581da177e4SLinus Torvalds 
2859f698204bSNeal Cardwell 		if (icsk->icsk_ca_state <= TCP_CA_Disorder)
28609e412ba7SIlpo Järvinen 			tcp_try_undo_dsack(sk);
28611da177e4SLinus Torvalds 
2862750ea2baSYuchung Cheng 		if (!tcp_time_to_recover(sk, flag)) {
286331ba0c10SYuchung Cheng 			tcp_try_to_open(sk, flag);
28641da177e4SLinus Torvalds 			return;
28651da177e4SLinus Torvalds 		}
28661da177e4SLinus Torvalds 
28675d424d5aSJohn Heffner 		/* MTU probe failure: don't reduce cwnd */
28685d424d5aSJohn Heffner 		if (icsk->icsk_ca_state < TCP_CA_CWR &&
28695d424d5aSJohn Heffner 		    icsk->icsk_mtup.probe_size &&
28700e7b1368SJohn Heffner 		    tp->snd_una == tp->mtu_probe.probe_seq_start) {
28715d424d5aSJohn Heffner 			tcp_mtup_probe_failed(sk);
28725d424d5aSJohn Heffner 			/* Restores the reduction we did in tcp_mtup_probe() */
28735d424d5aSJohn Heffner 			tp->snd_cwnd++;
28745d424d5aSJohn Heffner 			tcp_simple_retransmit(sk);
28755d424d5aSJohn Heffner 			return;
28765d424d5aSJohn Heffner 		}
28775d424d5aSJohn Heffner 
28781da177e4SLinus Torvalds 		/* Otherwise enter Recovery state */
28791fbc3405SYuchung Cheng 		tcp_enter_recovery(sk, (flag & FLAG_ECE));
288085cc391cSIlpo Järvinen 		fast_rexmit = 1;
28811da177e4SLinus Torvalds 	}
28821da177e4SLinus Torvalds 
28833e59cb0dSYuchung Cheng 	if (do_lost)
288485cc391cSIlpo Järvinen 		tcp_update_scoreboard(sk, fast_rexmit);
2885e662ca40SYuchung Cheng 	*rexmit = REXMIT_LOST;
28861da177e4SLinus Torvalds }
28871da177e4SLinus Torvalds 
2888f6722583SYuchung Cheng /* Kathleen Nichols' algorithm for tracking the minimum value of
2889f6722583SYuchung Cheng  * a data stream over some fixed time interval. (E.g., the minimum
2890f6722583SYuchung Cheng  * RTT over the past five minutes.) It uses constant space and constant
2891f6722583SYuchung Cheng  * time per update yet almost always delivers the same minimum as an
2892f6722583SYuchung Cheng  * implementation that has to keep all the data in the window.
2893f6722583SYuchung Cheng  *
2894f6722583SYuchung Cheng  * The algorithm keeps track of the best, 2nd best & 3rd best min
2895f6722583SYuchung Cheng  * values, maintaining an invariant that the measurement time of the
2896f6722583SYuchung Cheng  * n'th best >= n-1'th best. It also makes sure that the three values
2897f6722583SYuchung Cheng  * are widely separated in the time window since that bounds the worse
2898f6722583SYuchung Cheng  * case error when that data is monotonically increasing over the window.
2899f6722583SYuchung Cheng  *
2900f6722583SYuchung Cheng  * Upon getting a new min, we can forget everything earlier because it
2901f6722583SYuchung Cheng  * has no value - the new min is <= everything else in the window by
2902f6722583SYuchung Cheng  * definition and it's the most recent. So we restart fresh on every new min
2903f6722583SYuchung Cheng  * and overwrites 2nd & 3rd choices. The same property holds for 2nd & 3rd
2904f6722583SYuchung Cheng  * best.
2905f6722583SYuchung Cheng  */
2906f6722583SYuchung Cheng static void tcp_update_rtt_min(struct sock *sk, u32 rtt_us)
2907f6722583SYuchung Cheng {
2908f6722583SYuchung Cheng 	const u32 now = tcp_time_stamp, wlen = sysctl_tcp_min_rtt_wlen * HZ;
2909f6722583SYuchung Cheng 	struct rtt_meas *m = tcp_sk(sk)->rtt_min;
2910f6722583SYuchung Cheng 	struct rtt_meas rttm = { .rtt = (rtt_us ? : 1), .ts = now };
2911f6722583SYuchung Cheng 	u32 elapsed;
2912f6722583SYuchung Cheng 
2913f6722583SYuchung Cheng 	/* Check if the new measurement updates the 1st, 2nd, or 3rd choices */
2914f6722583SYuchung Cheng 	if (unlikely(rttm.rtt <= m[0].rtt))
2915f6722583SYuchung Cheng 		m[0] = m[1] = m[2] = rttm;
2916f6722583SYuchung Cheng 	else if (rttm.rtt <= m[1].rtt)
2917f6722583SYuchung Cheng 		m[1] = m[2] = rttm;
2918f6722583SYuchung Cheng 	else if (rttm.rtt <= m[2].rtt)
2919f6722583SYuchung Cheng 		m[2] = rttm;
2920f6722583SYuchung Cheng 
2921f6722583SYuchung Cheng 	elapsed = now - m[0].ts;
2922f6722583SYuchung Cheng 	if (unlikely(elapsed > wlen)) {
2923f6722583SYuchung Cheng 		/* Passed entire window without a new min so make 2nd choice
2924f6722583SYuchung Cheng 		 * the new min & 3rd choice the new 2nd. So forth and so on.
2925f6722583SYuchung Cheng 		 */
2926f6722583SYuchung Cheng 		m[0] = m[1];
2927f6722583SYuchung Cheng 		m[1] = m[2];
2928f6722583SYuchung Cheng 		m[2] = rttm;
2929f6722583SYuchung Cheng 		if (now - m[0].ts > wlen) {
2930f6722583SYuchung Cheng 			m[0] = m[1];
2931f6722583SYuchung Cheng 			m[1] = rttm;
2932f6722583SYuchung Cheng 			if (now - m[0].ts > wlen)
2933f6722583SYuchung Cheng 				m[0] = rttm;
2934f6722583SYuchung Cheng 		}
2935f6722583SYuchung Cheng 	} else if (m[1].ts == m[0].ts && elapsed > wlen / 4) {
2936f6722583SYuchung Cheng 		/* Passed a quarter of the window without a new min so
2937f6722583SYuchung Cheng 		 * take 2nd choice from the 2nd quarter of the window.
2938f6722583SYuchung Cheng 		 */
2939f6722583SYuchung Cheng 		m[2] = m[1] = rttm;
2940f6722583SYuchung Cheng 	} else if (m[2].ts == m[1].ts && elapsed > wlen / 2) {
2941f6722583SYuchung Cheng 		/* Passed half the window without a new min so take the 3rd
2942f6722583SYuchung Cheng 		 * choice from the last half of the window.
2943f6722583SYuchung Cheng 		 */
2944f6722583SYuchung Cheng 		m[2] = rttm;
2945f6722583SYuchung Cheng 	}
2946f6722583SYuchung Cheng }
2947f6722583SYuchung Cheng 
2948ed08495cSYuchung Cheng static inline bool tcp_ack_update_rtt(struct sock *sk, const int flag,
2949f6722583SYuchung Cheng 				      long seq_rtt_us, long sack_rtt_us,
2950f6722583SYuchung Cheng 				      long ca_rtt_us)
295141834b73SIlpo Järvinen {
29525b08e47cSYuchung Cheng 	const struct tcp_sock *tp = tcp_sk(sk);
295341834b73SIlpo Järvinen 
29545b08e47cSYuchung Cheng 	/* Prefer RTT measured from ACK's timing to TS-ECR. This is because
29555b08e47cSYuchung Cheng 	 * broken middle-boxes or peers may corrupt TS-ECR fields. But
29565b08e47cSYuchung Cheng 	 * Karn's algorithm forbids taking RTT if some retransmitted data
29575b08e47cSYuchung Cheng 	 * is acked (RFC6298).
29581da177e4SLinus Torvalds 	 */
2959740b0f18SEric Dumazet 	if (seq_rtt_us < 0)
2960740b0f18SEric Dumazet 		seq_rtt_us = sack_rtt_us;
2961ed08495cSYuchung Cheng 
29621da177e4SLinus Torvalds 	/* RTTM Rule: A TSecr value received in a segment is used to
29631da177e4SLinus Torvalds 	 * update the averaged RTT measurement only if the segment
29641da177e4SLinus Torvalds 	 * acknowledges some new data, i.e., only if it advances the
29651da177e4SLinus Torvalds 	 * left edge of the send window.
29661da177e4SLinus Torvalds 	 * See draft-ietf-tcplw-high-performance-00, section 3.3.
29671da177e4SLinus Torvalds 	 */
2968740b0f18SEric Dumazet 	if (seq_rtt_us < 0 && tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
29692909d874SYuchung Cheng 	    flag & FLAG_ACKED)
2970f6722583SYuchung Cheng 		seq_rtt_us = ca_rtt_us = jiffies_to_usecs(tcp_time_stamp -
2971f6722583SYuchung Cheng 							  tp->rx_opt.rcv_tsecr);
2972740b0f18SEric Dumazet 	if (seq_rtt_us < 0)
2973ed08495cSYuchung Cheng 		return false;
29741da177e4SLinus Torvalds 
2975f6722583SYuchung Cheng 	/* ca_rtt_us >= 0 is counting on the invariant that ca_rtt_us is
2976f6722583SYuchung Cheng 	 * always taken together with ACK, SACK, or TS-opts. Any negative
2977f6722583SYuchung Cheng 	 * values will be skipped with the seq_rtt_us < 0 check above.
2978f6722583SYuchung Cheng 	 */
2979f6722583SYuchung Cheng 	tcp_update_rtt_min(sk, ca_rtt_us);
2980740b0f18SEric Dumazet 	tcp_rtt_estimator(sk, seq_rtt_us);
29815b08e47cSYuchung Cheng 	tcp_set_rto(sk);
29821da177e4SLinus Torvalds 
29835b08e47cSYuchung Cheng 	/* RFC6298: only reset backoff on valid RTT measurement. */
29845b08e47cSYuchung Cheng 	inet_csk(sk)->icsk_backoff = 0;
2985ed08495cSYuchung Cheng 	return true;
29861da177e4SLinus Torvalds }
29871da177e4SLinus Torvalds 
2988375fe02cSYuchung Cheng /* Compute time elapsed between (last) SYNACK and the ACK completing 3WHS. */
29890f1c28aeSYuchung Cheng void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req)
2990375fe02cSYuchung Cheng {
29910f1c28aeSYuchung Cheng 	long rtt_us = -1L;
2992375fe02cSYuchung Cheng 
29930f1c28aeSYuchung Cheng 	if (req && !req->num_retrans && tcp_rsk(req)->snt_synack.v64) {
29940f1c28aeSYuchung Cheng 		struct skb_mstamp now;
2995bc15afa3SYuchung Cheng 
29960f1c28aeSYuchung Cheng 		skb_mstamp_get(&now);
29970f1c28aeSYuchung Cheng 		rtt_us = skb_mstamp_us_delta(&now, &tcp_rsk(req)->snt_synack);
2998375fe02cSYuchung Cheng 	}
2999375fe02cSYuchung Cheng 
3000f6722583SYuchung Cheng 	tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, rtt_us, -1L, rtt_us);
30010f1c28aeSYuchung Cheng }
30020f1c28aeSYuchung Cheng 
30030f1c28aeSYuchung Cheng 
300424901551SEric Dumazet static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
30051da177e4SLinus Torvalds {
30066687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
300724901551SEric Dumazet 
300824901551SEric Dumazet 	icsk->icsk_ca_ops->cong_avoid(sk, ack, acked);
30096687e988SArnaldo Carvalho de Melo 	tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp;
30101da177e4SLinus Torvalds }
30111da177e4SLinus Torvalds 
30121da177e4SLinus Torvalds /* Restart timer after forward progress on connection.
30131da177e4SLinus Torvalds  * RFC2988 recommends to restart timer to now+rto.
30141da177e4SLinus Torvalds  */
3015750ea2baSYuchung Cheng void tcp_rearm_rto(struct sock *sk)
30161da177e4SLinus Torvalds {
30176ba8a3b1SNandita Dukkipati 	const struct inet_connection_sock *icsk = inet_csk(sk);
3018750ea2baSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
30199e412ba7SIlpo Järvinen 
3020168a8f58SJerry Chu 	/* If the retrans timer is currently being used by Fast Open
3021168a8f58SJerry Chu 	 * for SYN-ACK retrans purpose, stay put.
3022168a8f58SJerry Chu 	 */
3023168a8f58SJerry Chu 	if (tp->fastopen_rsk)
3024168a8f58SJerry Chu 		return;
3025168a8f58SJerry Chu 
30261da177e4SLinus Torvalds 	if (!tp->packets_out) {
3027463c84b9SArnaldo Carvalho de Melo 		inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
30281da177e4SLinus Torvalds 	} else {
3029750ea2baSYuchung Cheng 		u32 rto = inet_csk(sk)->icsk_rto;
3030750ea2baSYuchung Cheng 		/* Offset the time elapsed after installing regular RTO */
30316ba8a3b1SNandita Dukkipati 		if (icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
30326ba8a3b1SNandita Dukkipati 		    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
3033750ea2baSYuchung Cheng 			struct sk_buff *skb = tcp_write_queue_head(sk);
30347faee5c0SEric Dumazet 			const u32 rto_time_stamp =
30357faee5c0SEric Dumazet 				tcp_skb_timestamp(skb) + rto;
3036750ea2baSYuchung Cheng 			s32 delta = (s32)(rto_time_stamp - tcp_time_stamp);
3037750ea2baSYuchung Cheng 			/* delta may not be positive if the socket is locked
30386ba8a3b1SNandita Dukkipati 			 * when the retrans timer fires and is rescheduled.
3039750ea2baSYuchung Cheng 			 */
3040750ea2baSYuchung Cheng 			if (delta > 0)
3041750ea2baSYuchung Cheng 				rto = delta;
30421da177e4SLinus Torvalds 		}
3043750ea2baSYuchung Cheng 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto,
3044750ea2baSYuchung Cheng 					  TCP_RTO_MAX);
3045750ea2baSYuchung Cheng 	}
3046750ea2baSYuchung Cheng }
3047750ea2baSYuchung Cheng 
3048750ea2baSYuchung Cheng /* This function is called when the delayed ER timer fires. TCP enters
3049750ea2baSYuchung Cheng  * fast recovery and performs fast-retransmit.
3050750ea2baSYuchung Cheng  */
3051750ea2baSYuchung Cheng void tcp_resume_early_retransmit(struct sock *sk)
3052750ea2baSYuchung Cheng {
3053750ea2baSYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
3054750ea2baSYuchung Cheng 
3055750ea2baSYuchung Cheng 	tcp_rearm_rto(sk);
3056750ea2baSYuchung Cheng 
3057750ea2baSYuchung Cheng 	/* Stop if ER is disabled after the delayed ER timer is scheduled */
3058750ea2baSYuchung Cheng 	if (!tp->do_early_retrans)
3059750ea2baSYuchung Cheng 		return;
3060750ea2baSYuchung Cheng 
3061750ea2baSYuchung Cheng 	tcp_enter_recovery(sk, false);
3062750ea2baSYuchung Cheng 	tcp_update_scoreboard(sk, 1);
3063750ea2baSYuchung Cheng 	tcp_xmit_retransmit_queue(sk);
30641da177e4SLinus Torvalds }
30651da177e4SLinus Torvalds 
30667c46a03eSIlpo Järvinen /* If we get here, the whole TSO packet has not been acked. */
306713fcf850SIlpo Järvinen static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)
30681da177e4SLinus Torvalds {
30691da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
30707c46a03eSIlpo Järvinen 	u32 packets_acked;
30711da177e4SLinus Torvalds 
30727c46a03eSIlpo Järvinen 	BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una));
30731da177e4SLinus Torvalds 
30741da177e4SLinus Torvalds 	packets_acked = tcp_skb_pcount(skb);
30757c46a03eSIlpo Järvinen 	if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
30761da177e4SLinus Torvalds 		return 0;
30771da177e4SLinus Torvalds 	packets_acked -= tcp_skb_pcount(skb);
30781da177e4SLinus Torvalds 
30791da177e4SLinus Torvalds 	if (packets_acked) {
30801da177e4SLinus Torvalds 		BUG_ON(tcp_skb_pcount(skb) == 0);
30817c46a03eSIlpo Järvinen 		BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq));
30821da177e4SLinus Torvalds 	}
30831da177e4SLinus Torvalds 
308413fcf850SIlpo Järvinen 	return packets_acked;
30851da177e4SLinus Torvalds }
30861da177e4SLinus Torvalds 
3087ad971f61SEric Dumazet static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb,
3088ad971f61SEric Dumazet 			   u32 prior_snd_una)
3089ad971f61SEric Dumazet {
3090ad971f61SEric Dumazet 	const struct skb_shared_info *shinfo;
3091ad971f61SEric Dumazet 
3092ad971f61SEric Dumazet 	/* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */
3093ad971f61SEric Dumazet 	if (likely(!(sk->sk_tsflags & SOF_TIMESTAMPING_TX_ACK)))
3094ad971f61SEric Dumazet 		return;
3095ad971f61SEric Dumazet 
3096ad971f61SEric Dumazet 	shinfo = skb_shinfo(skb);
3097ad971f61SEric Dumazet 	if ((shinfo->tx_flags & SKBTX_ACK_TSTAMP) &&
3098ad971f61SEric Dumazet 	    between(shinfo->tskey, prior_snd_una, tcp_sk(sk)->snd_una - 1))
3099ad971f61SEric Dumazet 		__skb_tstamp_tx(skb, NULL, sk, SCM_TSTAMP_ACK);
3100ad971f61SEric Dumazet }
3101ad971f61SEric Dumazet 
31027c46a03eSIlpo Järvinen /* Remove acknowledged frames from the retransmission queue. If our packet
31037c46a03eSIlpo Järvinen  * is before the ack sequence we can discard it as it's confirmed to have
31047c46a03eSIlpo Järvinen  * arrived at the other end.
31057c46a03eSIlpo Järvinen  */
310633f5f57eSIlpo Järvinen static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
31073ebd8871SYuchung Cheng 			       u32 prior_snd_una, int *acked,
310831231a8aSKenneth Klette Jonassen 			       struct tcp_sacktag_state *sack)
31091da177e4SLinus Torvalds {
31102d2abbabSStephen Hemminger 	const struct inet_connection_sock *icsk = inet_csk(sk);
3111740b0f18SEric Dumazet 	struct skb_mstamp first_ackt, last_ackt, now;
3112740b0f18SEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
311390638a04SIlpo Järvinen 	u32 prior_sacked = tp->sacked_out;
3114740b0f18SEric Dumazet 	u32 reord = tp->packets_out;
3115740b0f18SEric Dumazet 	bool fully_acked = true;
311631231a8aSKenneth Klette Jonassen 	long sack_rtt_us = -1L;
3117740b0f18SEric Dumazet 	long seq_rtt_us = -1L;
311831231a8aSKenneth Klette Jonassen 	long ca_rtt_us = -1L;
3119740b0f18SEric Dumazet 	struct sk_buff *skb;
3120740b0f18SEric Dumazet 	u32 pkts_acked = 0;
31212f715c1dSYuchung Cheng 	bool rtt_update;
3122740b0f18SEric Dumazet 	int flag = 0;
3123740b0f18SEric Dumazet 
3124740b0f18SEric Dumazet 	first_ackt.v64 = 0;
31251da177e4SLinus Torvalds 
31267c46a03eSIlpo Järvinen 	while ((skb = tcp_write_queue_head(sk)) && skb != tcp_send_head(sk)) {
31271da177e4SLinus Torvalds 		struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
31287c46a03eSIlpo Järvinen 		u8 sacked = scb->sacked;
3129740b0f18SEric Dumazet 		u32 acked_pcount;
31301da177e4SLinus Torvalds 
3131ad971f61SEric Dumazet 		tcp_ack_tstamp(sk, skb, prior_snd_una);
3132712a7221SWillem de Bruijn 
31332072c228SGavin McCullagh 		/* Determine how many packets and what bytes were acked, tso and else */
31341da177e4SLinus Torvalds 		if (after(scb->end_seq, tp->snd_una)) {
313513fcf850SIlpo Järvinen 			if (tcp_skb_pcount(skb) == 1 ||
313613fcf850SIlpo Järvinen 			    !after(tp->snd_una, scb->seq))
31371da177e4SLinus Torvalds 				break;
313813fcf850SIlpo Järvinen 
313972018835SIlpo Järvinen 			acked_pcount = tcp_tso_acked(sk, skb);
314072018835SIlpo Järvinen 			if (!acked_pcount)
314113fcf850SIlpo Järvinen 				break;
314213fcf850SIlpo Järvinen 
3143a2a385d6SEric Dumazet 			fully_acked = false;
314413fcf850SIlpo Järvinen 		} else {
3145ad971f61SEric Dumazet 			/* Speedup tcp_unlink_write_queue() and next loop */
3146ad971f61SEric Dumazet 			prefetchw(skb->next);
314772018835SIlpo Järvinen 			acked_pcount = tcp_skb_pcount(skb);
31481da177e4SLinus Torvalds 		}
31491da177e4SLinus Torvalds 
3150ad971f61SEric Dumazet 		if (unlikely(sacked & TCPCB_RETRANS)) {
31511da177e4SLinus Torvalds 			if (sacked & TCPCB_SACKED_RETRANS)
315272018835SIlpo Järvinen 				tp->retrans_out -= acked_pcount;
31537c46a03eSIlpo Järvinen 			flag |= FLAG_RETRANS_DATA_ACKED;
31543d0d26c7SKenneth Klette Jonassen 		} else if (!(sacked & TCPCB_SACKED_ACKED)) {
3155740b0f18SEric Dumazet 			last_ackt = skb->skb_mstamp;
3156431a9124SEric Dumazet 			WARN_ON_ONCE(last_ackt.v64 == 0);
3157740b0f18SEric Dumazet 			if (!first_ackt.v64)
3158740b0f18SEric Dumazet 				first_ackt = last_ackt;
3159740b0f18SEric Dumazet 
316072018835SIlpo Järvinen 			reord = min(pkts_acked, reord);
3161e33099f9SYuchung Cheng 			if (!after(scb->end_seq, tp->high_seq))
3162e33099f9SYuchung Cheng 				flag |= FLAG_ORIG_SACK_ACKED;
3163c7caf8d3SIlpo Järvinen 		}
31647c46a03eSIlpo Järvinen 
3165ddf1af6fSYuchung Cheng 		if (sacked & TCPCB_SACKED_ACKED) {
316672018835SIlpo Järvinen 			tp->sacked_out -= acked_pcount;
3167ddf1af6fSYuchung Cheng 		} else if (tcp_is_sack(tp)) {
3168ddf1af6fSYuchung Cheng 			tp->delivered += acked_pcount;
3169ddf1af6fSYuchung Cheng 			if (!tcp_skb_spurious_retrans(tp, skb))
3170659a8ad5SYuchung Cheng 				tcp_rack_advance(tp, &skb->skb_mstamp, sacked);
3171ddf1af6fSYuchung Cheng 		}
31721da177e4SLinus Torvalds 		if (sacked & TCPCB_LOST)
317372018835SIlpo Järvinen 			tp->lost_out -= acked_pcount;
31747c46a03eSIlpo Järvinen 
317572018835SIlpo Järvinen 		tp->packets_out -= acked_pcount;
317672018835SIlpo Järvinen 		pkts_acked += acked_pcount;
317713fcf850SIlpo Järvinen 
3178009a2e3eSIlpo Järvinen 		/* Initial outgoing SYN's get put onto the write_queue
3179009a2e3eSIlpo Järvinen 		 * just like anything else we transmit.  It is not
3180009a2e3eSIlpo Järvinen 		 * true data, and if we misinform our callers that
3181009a2e3eSIlpo Järvinen 		 * this ACK acks real data, we will erroneously exit
3182009a2e3eSIlpo Järvinen 		 * connection startup slow start one packet too
3183009a2e3eSIlpo Järvinen 		 * quickly.  This is severely frowned upon behavior.
3184009a2e3eSIlpo Järvinen 		 */
3185ad971f61SEric Dumazet 		if (likely(!(scb->tcp_flags & TCPHDR_SYN))) {
3186009a2e3eSIlpo Järvinen 			flag |= FLAG_DATA_ACKED;
3187009a2e3eSIlpo Järvinen 		} else {
3188009a2e3eSIlpo Järvinen 			flag |= FLAG_SYN_ACKED;
3189009a2e3eSIlpo Järvinen 			tp->retrans_stamp = 0;
3190009a2e3eSIlpo Järvinen 		}
3191009a2e3eSIlpo Järvinen 
319213fcf850SIlpo Järvinen 		if (!fully_acked)
319313fcf850SIlpo Järvinen 			break;
319413fcf850SIlpo Järvinen 
3195fe067e8aSDavid S. Miller 		tcp_unlink_write_queue(skb, sk);
31963ab224beSHideo Aoki 		sk_wmem_free_skb(sk, skb);
3197ad971f61SEric Dumazet 		if (unlikely(skb == tp->retransmit_skb_hint))
3198ef9da47cSIlpo Järvinen 			tp->retransmit_skb_hint = NULL;
3199ad971f61SEric Dumazet 		if (unlikely(skb == tp->lost_skb_hint))
320090638a04SIlpo Järvinen 			tp->lost_skb_hint = NULL;
32011da177e4SLinus Torvalds 	}
32021da177e4SLinus Torvalds 
320333f5f57eSIlpo Järvinen 	if (likely(between(tp->snd_up, prior_snd_una, tp->snd_una)))
320433f5f57eSIlpo Järvinen 		tp->snd_up = tp->snd_una;
320533f5f57eSIlpo Järvinen 
3206cadbd031SIlpo Järvinen 	if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
3207cadbd031SIlpo Järvinen 		flag |= FLAG_SACK_RENEGING;
3208cadbd031SIlpo Järvinen 
3209740b0f18SEric Dumazet 	skb_mstamp_get(&now);
32109e45a3e3SYuchung Cheng 	if (likely(first_ackt.v64) && !(flag & FLAG_RETRANS_DATA_ACKED)) {
3211740b0f18SEric Dumazet 		seq_rtt_us = skb_mstamp_us_delta(&now, &first_ackt);
321231231a8aSKenneth Klette Jonassen 		ca_rtt_us = skb_mstamp_us_delta(&now, &last_ackt);
321331231a8aSKenneth Klette Jonassen 	}
321431231a8aSKenneth Klette Jonassen 	if (sack->first_sackt.v64) {
321531231a8aSKenneth Klette Jonassen 		sack_rtt_us = skb_mstamp_us_delta(&now, &sack->first_sackt);
321631231a8aSKenneth Klette Jonassen 		ca_rtt_us = skb_mstamp_us_delta(&now, &sack->last_sackt);
3217740b0f18SEric Dumazet 	}
3218740b0f18SEric Dumazet 
3219f6722583SYuchung Cheng 	rtt_update = tcp_ack_update_rtt(sk, flag, seq_rtt_us, sack_rtt_us,
3220f6722583SYuchung Cheng 					ca_rtt_us);
3221ed08495cSYuchung Cheng 
32227c46a03eSIlpo Järvinen 	if (flag & FLAG_ACKED) {
32232f715c1dSYuchung Cheng 		tcp_rearm_rto(sk);
322472211e90SIlpo Järvinen 		if (unlikely(icsk->icsk_mtup.probe_size &&
322572211e90SIlpo Järvinen 			     !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {
322672211e90SIlpo Järvinen 			tcp_mtup_probe_success(sk);
322772211e90SIlpo Järvinen 		}
322872211e90SIlpo Järvinen 
3229c7caf8d3SIlpo Järvinen 		if (tcp_is_reno(tp)) {
3230c7caf8d3SIlpo Järvinen 			tcp_remove_reno_sacks(sk, pkts_acked);
3231c7caf8d3SIlpo Järvinen 		} else {
323259a08cbaSIlpo Järvinen 			int delta;
323359a08cbaSIlpo Järvinen 
3234c7caf8d3SIlpo Järvinen 			/* Non-retransmitted hole got filled? That's reordering */
3235c7caf8d3SIlpo Järvinen 			if (reord < prior_fackets)
3236c7caf8d3SIlpo Järvinen 				tcp_update_reordering(sk, tp->fackets_out - reord, 0);
323790638a04SIlpo Järvinen 
323859a08cbaSIlpo Järvinen 			delta = tcp_is_fack(tp) ? pkts_acked :
323959a08cbaSIlpo Järvinen 						  prior_sacked - tp->sacked_out;
324059a08cbaSIlpo Järvinen 			tp->lost_cnt_hint -= min(tp->lost_cnt_hint, delta);
3241c7caf8d3SIlpo Järvinen 		}
3242c7caf8d3SIlpo Järvinen 
324391fed7a1SIlpo Järvinen 		tp->fackets_out -= min(pkts_acked, tp->fackets_out);
324468f8353bSIlpo Järvinen 
3245740b0f18SEric Dumazet 	} else if (skb && rtt_update && sack_rtt_us >= 0 &&
3246740b0f18SEric Dumazet 		   sack_rtt_us > skb_mstamp_us_delta(&now, &skb->skb_mstamp)) {
32472f715c1dSYuchung Cheng 		/* Do not re-arm RTO if the sack RTT is measured from data sent
32482f715c1dSYuchung Cheng 		 * after when the head was last (re)transmitted. Otherwise the
32492f715c1dSYuchung Cheng 		 * timeout may continue to extend in loss recovery.
32502f715c1dSYuchung Cheng 		 */
32512f715c1dSYuchung Cheng 		tcp_rearm_rto(sk);
32521da177e4SLinus Torvalds 	}
32531da177e4SLinus Torvalds 
3254138998fdSKenneth Klette Jonassen 	if (icsk->icsk_ca_ops->pkts_acked)
3255138998fdSKenneth Klette Jonassen 		icsk->icsk_ca_ops->pkts_acked(sk, pkts_acked, ca_rtt_us);
3256138998fdSKenneth Klette Jonassen 
32571da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0
3258547b792cSIlpo Järvinen 	WARN_ON((int)tp->sacked_out < 0);
3259547b792cSIlpo Järvinen 	WARN_ON((int)tp->lost_out < 0);
3260547b792cSIlpo Järvinen 	WARN_ON((int)tp->retrans_out < 0);
3261e60402d0SIlpo Järvinen 	if (!tp->packets_out && tcp_is_sack(tp)) {
3262cfcabdccSStephen Hemminger 		icsk = inet_csk(sk);
32631da177e4SLinus Torvalds 		if (tp->lost_out) {
326491df42beSJoe Perches 			pr_debug("Leak l=%u %d\n",
32656687e988SArnaldo Carvalho de Melo 				 tp->lost_out, icsk->icsk_ca_state);
32661da177e4SLinus Torvalds 			tp->lost_out = 0;
32671da177e4SLinus Torvalds 		}
32681da177e4SLinus Torvalds 		if (tp->sacked_out) {
326991df42beSJoe Perches 			pr_debug("Leak s=%u %d\n",
32706687e988SArnaldo Carvalho de Melo 				 tp->sacked_out, icsk->icsk_ca_state);
32711da177e4SLinus Torvalds 			tp->sacked_out = 0;
32721da177e4SLinus Torvalds 		}
32731da177e4SLinus Torvalds 		if (tp->retrans_out) {
327491df42beSJoe Perches 			pr_debug("Leak r=%u %d\n",
32756687e988SArnaldo Carvalho de Melo 				 tp->retrans_out, icsk->icsk_ca_state);
32761da177e4SLinus Torvalds 			tp->retrans_out = 0;
32771da177e4SLinus Torvalds 		}
32781da177e4SLinus Torvalds 	}
32791da177e4SLinus Torvalds #endif
32803ebd8871SYuchung Cheng 	*acked = pkts_acked;
32817c46a03eSIlpo Järvinen 	return flag;
32821da177e4SLinus Torvalds }
32831da177e4SLinus Torvalds 
32841da177e4SLinus Torvalds static void tcp_ack_probe(struct sock *sk)
32851da177e4SLinus Torvalds {
3286463c84b9SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
3287463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
32881da177e4SLinus Torvalds 
32891da177e4SLinus Torvalds 	/* Was it a usable window open? */
32901da177e4SLinus Torvalds 
329190840defSIlpo Järvinen 	if (!after(TCP_SKB_CB(tcp_send_head(sk))->end_seq, tcp_wnd_end(tp))) {
3292463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_backoff = 0;
3293463c84b9SArnaldo Carvalho de Melo 		inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0);
32941da177e4SLinus Torvalds 		/* Socket must be waked up by subsequent tcp_data_snd_check().
32951da177e4SLinus Torvalds 		 * This function is not for random using!
32961da177e4SLinus Torvalds 		 */
32971da177e4SLinus Torvalds 	} else {
329821c8fe99SEric Dumazet 		unsigned long when = tcp_probe0_when(sk, TCP_RTO_MAX);
3299fcdd1cf4SEric Dumazet 
3300463c84b9SArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
3301fcdd1cf4SEric Dumazet 					  when, TCP_RTO_MAX);
33021da177e4SLinus Torvalds 	}
33031da177e4SLinus Torvalds }
33041da177e4SLinus Torvalds 
330567b95bd7SVijay Subramanian static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag)
33061da177e4SLinus Torvalds {
3307a02cec21SEric Dumazet 	return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||
3308a02cec21SEric Dumazet 		inet_csk(sk)->icsk_ca_state != TCP_CA_Open;
33091da177e4SLinus Torvalds }
33101da177e4SLinus Torvalds 
33110f7cc9a3SYuchung Cheng /* Decide wheather to run the increase function of congestion control. */
331267b95bd7SVijay Subramanian static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
33131da177e4SLinus Torvalds {
33140f7cc9a3SYuchung Cheng 	/* If reordering is high then always grow cwnd whenever data is
33150f7cc9a3SYuchung Cheng 	 * delivered regardless of its ordering. Otherwise stay conservative
331616edfe7eSYuchung Cheng 	 * and only grow cwnd on in-order delivery (RFC5681). A stretched ACK w/
33170f7cc9a3SYuchung Cheng 	 * new SACK or ECE mark may first advance cwnd here and later reduce
33180f7cc9a3SYuchung Cheng 	 * cwnd in tcp_fastretrans_alert() based on more states.
33190f7cc9a3SYuchung Cheng 	 */
33200f7cc9a3SYuchung Cheng 	if (tcp_sk(sk)->reordering > sysctl_tcp_reordering)
33210f7cc9a3SYuchung Cheng 		return flag & FLAG_FORWARD_PROGRESS;
33220f7cc9a3SYuchung Cheng 
332316edfe7eSYuchung Cheng 	return flag & FLAG_DATA_ACKED;
33241da177e4SLinus Torvalds }
33251da177e4SLinus Torvalds 
3326d452e6caSYuchung Cheng /* The "ultimate" congestion control function that aims to replace the rigid
3327d452e6caSYuchung Cheng  * cwnd increase and decrease control (tcp_cong_avoid,tcp_*cwnd_reduction).
3328d452e6caSYuchung Cheng  * It's called toward the end of processing an ACK with precise rate
3329d452e6caSYuchung Cheng  * information. All transmission or retransmission are delayed afterwards.
3330d452e6caSYuchung Cheng  */
3331d452e6caSYuchung Cheng static void tcp_cong_control(struct sock *sk, u32 ack, u32 acked_sacked,
3332d452e6caSYuchung Cheng 			     int flag)
3333d452e6caSYuchung Cheng {
3334d452e6caSYuchung Cheng 	if (tcp_in_cwnd_reduction(sk)) {
3335d452e6caSYuchung Cheng 		/* Reduce cwnd if state mandates */
3336d452e6caSYuchung Cheng 		tcp_cwnd_reduction(sk, acked_sacked, flag);
3337d452e6caSYuchung Cheng 	} else if (tcp_may_raise_cwnd(sk, flag)) {
3338d452e6caSYuchung Cheng 		/* Advance cwnd if state allows */
3339d452e6caSYuchung Cheng 		tcp_cong_avoid(sk, ack, acked_sacked);
3340d452e6caSYuchung Cheng 	}
3341d452e6caSYuchung Cheng 	tcp_update_pacing_rate(sk);
3342d452e6caSYuchung Cheng }
3343d452e6caSYuchung Cheng 
33441da177e4SLinus Torvalds /* Check that window update is acceptable.
33451da177e4SLinus Torvalds  * The function assumes that snd_una<=ack<=snd_next.
33461da177e4SLinus Torvalds  */
334767b95bd7SVijay Subramanian static inline bool tcp_may_update_window(const struct tcp_sock *tp,
3348056834d9SIlpo Järvinen 					const u32 ack, const u32 ack_seq,
3349056834d9SIlpo Järvinen 					const u32 nwin)
33501da177e4SLinus Torvalds {
3351a02cec21SEric Dumazet 	return	after(ack, tp->snd_una) ||
33521da177e4SLinus Torvalds 		after(ack_seq, tp->snd_wl1) ||
3353a02cec21SEric Dumazet 		(ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd);
33541da177e4SLinus Torvalds }
33551da177e4SLinus Torvalds 
33560df48c26SEric Dumazet /* If we update tp->snd_una, also update tp->bytes_acked */
33570df48c26SEric Dumazet static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
33580df48c26SEric Dumazet {
33590df48c26SEric Dumazet 	u32 delta = ack - tp->snd_una;
33600df48c26SEric Dumazet 
3361d654976cSEric Dumazet 	u64_stats_update_begin(&tp->syncp);
33620df48c26SEric Dumazet 	tp->bytes_acked += delta;
3363d654976cSEric Dumazet 	u64_stats_update_end(&tp->syncp);
33640df48c26SEric Dumazet 	tp->snd_una = ack;
33650df48c26SEric Dumazet }
33660df48c26SEric Dumazet 
3367bdd1f9edSEric Dumazet /* If we update tp->rcv_nxt, also update tp->bytes_received */
3368bdd1f9edSEric Dumazet static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
3369bdd1f9edSEric Dumazet {
3370bdd1f9edSEric Dumazet 	u32 delta = seq - tp->rcv_nxt;
3371bdd1f9edSEric Dumazet 
3372d654976cSEric Dumazet 	u64_stats_update_begin(&tp->syncp);
3373bdd1f9edSEric Dumazet 	tp->bytes_received += delta;
3374d654976cSEric Dumazet 	u64_stats_update_end(&tp->syncp);
3375bdd1f9edSEric Dumazet 	tp->rcv_nxt = seq;
3376bdd1f9edSEric Dumazet }
3377bdd1f9edSEric Dumazet 
33781da177e4SLinus Torvalds /* Update our send window.
33791da177e4SLinus Torvalds  *
33801da177e4SLinus Torvalds  * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
33811da177e4SLinus Torvalds  * and in FreeBSD. NetBSD's one is even worse.) is wrong.
33821da177e4SLinus Torvalds  */
3383cf533ea5SEric Dumazet static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,
33849e412ba7SIlpo Järvinen 				 u32 ack_seq)
33851da177e4SLinus Torvalds {
33869e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
33871da177e4SLinus Torvalds 	int flag = 0;
3388aa8223c7SArnaldo Carvalho de Melo 	u32 nwin = ntohs(tcp_hdr(skb)->window);
33891da177e4SLinus Torvalds 
3390aa8223c7SArnaldo Carvalho de Melo 	if (likely(!tcp_hdr(skb)->syn))
33911da177e4SLinus Torvalds 		nwin <<= tp->rx_opt.snd_wscale;
33921da177e4SLinus Torvalds 
33931da177e4SLinus Torvalds 	if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {
33941da177e4SLinus Torvalds 		flag |= FLAG_WIN_UPDATE;
3395ee7537b6SHantzis Fotis 		tcp_update_wl(tp, ack_seq);
33961da177e4SLinus Torvalds 
33971da177e4SLinus Torvalds 		if (tp->snd_wnd != nwin) {
33981da177e4SLinus Torvalds 			tp->snd_wnd = nwin;
33991da177e4SLinus Torvalds 
34001da177e4SLinus Torvalds 			/* Note, it is the only place, where
34011da177e4SLinus Torvalds 			 * fast path is recovered for sending TCP.
34021da177e4SLinus Torvalds 			 */
34032ad41065SHerbert Xu 			tp->pred_flags = 0;
34049e412ba7SIlpo Järvinen 			tcp_fast_path_check(sk);
34051da177e4SLinus Torvalds 
34066f021c62SEric Dumazet 			if (tcp_send_head(sk))
34076f021c62SEric Dumazet 				tcp_slow_start_after_idle_check(sk);
34086f021c62SEric Dumazet 
34091da177e4SLinus Torvalds 			if (nwin > tp->max_window) {
34101da177e4SLinus Torvalds 				tp->max_window = nwin;
3411d83d8461SArnaldo Carvalho de Melo 				tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie);
34121da177e4SLinus Torvalds 			}
34131da177e4SLinus Torvalds 		}
34141da177e4SLinus Torvalds 	}
34151da177e4SLinus Torvalds 
34160df48c26SEric Dumazet 	tcp_snd_una_update(tp, ack);
34171da177e4SLinus Torvalds 
34181da177e4SLinus Torvalds 	return flag;
34191da177e4SLinus Torvalds }
34201da177e4SLinus Torvalds 
34217970ddc8SEric Dumazet /* Return true if we're currently rate-limiting out-of-window ACKs and
34227970ddc8SEric Dumazet  * thus shouldn't send a dupack right now. We rate-limit dupacks in
34237970ddc8SEric Dumazet  * response to out-of-window SYNs or ACKs to mitigate ACK loops or DoS
34247970ddc8SEric Dumazet  * attacks that send repeated SYNs or ACKs for the same connection. To
34257970ddc8SEric Dumazet  * do this, we do not send a duplicate SYNACK or ACK if the remote
34267970ddc8SEric Dumazet  * endpoint is sending out-of-window SYNs or pure ACKs at a high rate.
34277970ddc8SEric Dumazet  */
34287970ddc8SEric Dumazet bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
34297970ddc8SEric Dumazet 			  int mib_idx, u32 *last_oow_ack_time)
34307970ddc8SEric Dumazet {
34317970ddc8SEric Dumazet 	/* Data packets without SYNs are not likely part of an ACK loop. */
34327970ddc8SEric Dumazet 	if ((TCP_SKB_CB(skb)->seq != TCP_SKB_CB(skb)->end_seq) &&
34337970ddc8SEric Dumazet 	    !tcp_hdr(skb)->syn)
34347970ddc8SEric Dumazet 		goto not_rate_limited;
34357970ddc8SEric Dumazet 
34367970ddc8SEric Dumazet 	if (*last_oow_ack_time) {
34377970ddc8SEric Dumazet 		s32 elapsed = (s32)(tcp_time_stamp - *last_oow_ack_time);
34387970ddc8SEric Dumazet 
34397970ddc8SEric Dumazet 		if (0 <= elapsed && elapsed < sysctl_tcp_invalid_ratelimit) {
34407970ddc8SEric Dumazet 			NET_INC_STATS_BH(net, mib_idx);
34417970ddc8SEric Dumazet 			return true;	/* rate-limited: don't send yet! */
34427970ddc8SEric Dumazet 		}
34437970ddc8SEric Dumazet 	}
34447970ddc8SEric Dumazet 
34457970ddc8SEric Dumazet 	*last_oow_ack_time = tcp_time_stamp;
34467970ddc8SEric Dumazet 
34477970ddc8SEric Dumazet not_rate_limited:
34487970ddc8SEric Dumazet 	return false;	/* not rate-limited: go ahead, send dupack now! */
34497970ddc8SEric Dumazet }
34507970ddc8SEric Dumazet 
3451354e4aa3SEric Dumazet /* RFC 5961 7 [ACK Throttling] */
3452f2b2c582SNeal Cardwell static void tcp_send_challenge_ack(struct sock *sk, const struct sk_buff *skb)
3453354e4aa3SEric Dumazet {
3454354e4aa3SEric Dumazet 	/* unprotected vars, we dont care of overwrites */
3455354e4aa3SEric Dumazet 	static u32 challenge_timestamp;
3456354e4aa3SEric Dumazet 	static unsigned int challenge_count;
3457f2b2c582SNeal Cardwell 	struct tcp_sock *tp = tcp_sk(sk);
3458f2b2c582SNeal Cardwell 	u32 now;
3459354e4aa3SEric Dumazet 
3460f2b2c582SNeal Cardwell 	/* First check our per-socket dupack rate limit. */
3461f2b2c582SNeal Cardwell 	if (tcp_oow_rate_limited(sock_net(sk), skb,
3462f2b2c582SNeal Cardwell 				 LINUX_MIB_TCPACKSKIPPEDCHALLENGE,
3463f2b2c582SNeal Cardwell 				 &tp->last_oow_ack_time))
3464f2b2c582SNeal Cardwell 		return;
3465f2b2c582SNeal Cardwell 
3466f2b2c582SNeal Cardwell 	/* Then check the check host-wide RFC 5961 rate limit. */
3467f2b2c582SNeal Cardwell 	now = jiffies / HZ;
3468354e4aa3SEric Dumazet 	if (now != challenge_timestamp) {
3469354e4aa3SEric Dumazet 		challenge_timestamp = now;
3470354e4aa3SEric Dumazet 		challenge_count = 0;
3471354e4aa3SEric Dumazet 	}
3472354e4aa3SEric Dumazet 	if (++challenge_count <= sysctl_tcp_challenge_ack_limit) {
3473354e4aa3SEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPCHALLENGEACK);
3474354e4aa3SEric Dumazet 		tcp_send_ack(sk);
3475354e4aa3SEric Dumazet 	}
3476354e4aa3SEric Dumazet }
3477354e4aa3SEric Dumazet 
347812fb3dd9SEric Dumazet static void tcp_store_ts_recent(struct tcp_sock *tp)
347912fb3dd9SEric Dumazet {
348012fb3dd9SEric Dumazet 	tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
348112fb3dd9SEric Dumazet 	tp->rx_opt.ts_recent_stamp = get_seconds();
348212fb3dd9SEric Dumazet }
348312fb3dd9SEric Dumazet 
348412fb3dd9SEric Dumazet static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
348512fb3dd9SEric Dumazet {
348612fb3dd9SEric Dumazet 	if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) {
348712fb3dd9SEric Dumazet 		/* PAWS bug workaround wrt. ACK frames, the PAWS discard
348812fb3dd9SEric Dumazet 		 * extra check below makes sure this can only happen
348912fb3dd9SEric Dumazet 		 * for pure ACK frames.  -DaveM
349012fb3dd9SEric Dumazet 		 *
349112fb3dd9SEric Dumazet 		 * Not only, also it occurs for expired timestamps.
349212fb3dd9SEric Dumazet 		 */
349312fb3dd9SEric Dumazet 
349412fb3dd9SEric Dumazet 		if (tcp_paws_check(&tp->rx_opt, 0))
349512fb3dd9SEric Dumazet 			tcp_store_ts_recent(tp);
349612fb3dd9SEric Dumazet 	}
349712fb3dd9SEric Dumazet }
349812fb3dd9SEric Dumazet 
34999b717a8dSNandita Dukkipati /* This routine deals with acks during a TLP episode.
350008abdffaSSébastien Barré  * We mark the end of a TLP episode on receiving TLP dupack or when
350108abdffaSSébastien Barré  * ack is after tlp_high_seq.
35029b717a8dSNandita Dukkipati  * Ref: loss detection algorithm in draft-dukkipati-tcpm-tcp-loss-probe.
35039b717a8dSNandita Dukkipati  */
35049b717a8dSNandita Dukkipati static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
35059b717a8dSNandita Dukkipati {
35069b717a8dSNandita Dukkipati 	struct tcp_sock *tp = tcp_sk(sk);
35079b717a8dSNandita Dukkipati 
350808abdffaSSébastien Barré 	if (before(ack, tp->tlp_high_seq))
35099b717a8dSNandita Dukkipati 		return;
35109b717a8dSNandita Dukkipati 
351108abdffaSSébastien Barré 	if (flag & FLAG_DSACKING_ACK) {
351208abdffaSSébastien Barré 		/* This DSACK means original and TLP probe arrived; no loss */
35139b717a8dSNandita Dukkipati 		tp->tlp_high_seq = 0;
351408abdffaSSébastien Barré 	} else if (after(ack, tp->tlp_high_seq)) {
351508abdffaSSébastien Barré 		/* ACK advances: there was a loss, so reduce cwnd. Reset
351608abdffaSSébastien Barré 		 * tlp_high_seq in tcp_init_cwnd_reduction()
351708abdffaSSébastien Barré 		 */
35185ee2c941SChristoph Paasch 		tcp_init_cwnd_reduction(sk);
35199b717a8dSNandita Dukkipati 		tcp_set_ca_state(sk, TCP_CA_CWR);
35209b717a8dSNandita Dukkipati 		tcp_end_cwnd_reduction(sk);
3521031afe49SYuchung Cheng 		tcp_try_keep_open(sk);
35229b717a8dSNandita Dukkipati 		NET_INC_STATS_BH(sock_net(sk),
35239b717a8dSNandita Dukkipati 				 LINUX_MIB_TCPLOSSPROBERECOVERY);
352408abdffaSSébastien Barré 	} else if (!(flag & (FLAG_SND_UNA_ADVANCED |
352508abdffaSSébastien Barré 			     FLAG_NOT_DUP | FLAG_DATA_SACKED))) {
352608abdffaSSébastien Barré 		/* Pure dupack: original and TLP probe arrived; no loss */
352708abdffaSSébastien Barré 		tp->tlp_high_seq = 0;
35289b717a8dSNandita Dukkipati 	}
35299b717a8dSNandita Dukkipati }
35309b717a8dSNandita Dukkipati 
35317354c8c3SFlorian Westphal static inline void tcp_in_ack_event(struct sock *sk, u32 flags)
35327354c8c3SFlorian Westphal {
35337354c8c3SFlorian Westphal 	const struct inet_connection_sock *icsk = inet_csk(sk);
35347354c8c3SFlorian Westphal 
35357354c8c3SFlorian Westphal 	if (icsk->icsk_ca_ops->in_ack_event)
35367354c8c3SFlorian Westphal 		icsk->icsk_ca_ops->in_ack_event(sk, flags);
35377354c8c3SFlorian Westphal }
35387354c8c3SFlorian Westphal 
3539e662ca40SYuchung Cheng /* Congestion control has updated the cwnd already. So if we're in
3540e662ca40SYuchung Cheng  * loss recovery then now we do any new sends (for FRTO) or
3541e662ca40SYuchung Cheng  * retransmits (for CA_Loss or CA_recovery) that make sense.
3542e662ca40SYuchung Cheng  */
3543e662ca40SYuchung Cheng static void tcp_xmit_recovery(struct sock *sk, int rexmit)
3544e662ca40SYuchung Cheng {
3545e662ca40SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
3546e662ca40SYuchung Cheng 
3547e662ca40SYuchung Cheng 	if (rexmit == REXMIT_NONE)
3548e662ca40SYuchung Cheng 		return;
3549e662ca40SYuchung Cheng 
3550e662ca40SYuchung Cheng 	if (unlikely(rexmit == 2)) {
3551e662ca40SYuchung Cheng 		__tcp_push_pending_frames(sk, tcp_current_mss(sk),
3552e662ca40SYuchung Cheng 					  TCP_NAGLE_OFF);
3553e662ca40SYuchung Cheng 		if (after(tp->snd_nxt, tp->high_seq))
3554e662ca40SYuchung Cheng 			return;
3555e662ca40SYuchung Cheng 		tp->frto = 0;
3556e662ca40SYuchung Cheng 	}
3557e662ca40SYuchung Cheng 	tcp_xmit_retransmit_queue(sk);
3558e662ca40SYuchung Cheng }
3559e662ca40SYuchung Cheng 
35601da177e4SLinus Torvalds /* This routine deals with incoming acks, but not outgoing ones. */
3561cf533ea5SEric Dumazet static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
35621da177e4SLinus Torvalds {
35636687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
35641da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
3565196da974SKenneth Klette Jonassen 	struct tcp_sacktag_state sack_state;
35661da177e4SLinus Torvalds 	u32 prior_snd_una = tp->snd_una;
35671da177e4SLinus Torvalds 	u32 ack_seq = TCP_SKB_CB(skb)->seq;
35681da177e4SLinus Torvalds 	u32 ack = TCP_SKB_CB(skb)->ack_seq;
35697d2b55f8SNeal Cardwell 	bool is_dupack = false;
3570c7caf8d3SIlpo Järvinen 	u32 prior_fackets;
357135f079ebSNandita Dukkipati 	int prior_packets = tp->packets_out;
3572ddf1af6fSYuchung Cheng 	u32 prior_delivered = tp->delivered;
35736804973fSYuchung Cheng 	int acked = 0; /* Number of packets newly acked */
3574e662ca40SYuchung Cheng 	int rexmit = REXMIT_NONE; /* Flag to (re)transmit to recover losses */
3575196da974SKenneth Klette Jonassen 
357631231a8aSKenneth Klette Jonassen 	sack_state.first_sackt.v64 = 0;
35771da177e4SLinus Torvalds 
3578ad971f61SEric Dumazet 	/* We very likely will need to access write queue head. */
3579ad971f61SEric Dumazet 	prefetchw(sk->sk_write_queue.next);
3580ad971f61SEric Dumazet 
358196e0bf4bSJohn Dykstra 	/* If the ack is older than previous acks
35821da177e4SLinus Torvalds 	 * then we can probably ignore it.
35831da177e4SLinus Torvalds 	 */
3584354e4aa3SEric Dumazet 	if (before(ack, prior_snd_una)) {
3585354e4aa3SEric Dumazet 		/* RFC 5961 5.2 [Blind Data Injection Attack].[Mitigation] */
3586354e4aa3SEric Dumazet 		if (before(ack, prior_snd_una - tp->max_window)) {
3587f2b2c582SNeal Cardwell 			tcp_send_challenge_ack(sk, skb);
3588354e4aa3SEric Dumazet 			return -1;
3589354e4aa3SEric Dumazet 		}
35901da177e4SLinus Torvalds 		goto old_ack;
3591354e4aa3SEric Dumazet 	}
35921da177e4SLinus Torvalds 
359396e0bf4bSJohn Dykstra 	/* If the ack includes data we haven't sent yet, discard
359496e0bf4bSJohn Dykstra 	 * this segment (RFC793 Section 3.9).
359596e0bf4bSJohn Dykstra 	 */
359696e0bf4bSJohn Dykstra 	if (after(ack, tp->snd_nxt))
359796e0bf4bSJohn Dykstra 		goto invalid_ack;
359896e0bf4bSJohn Dykstra 
35996ba8a3b1SNandita Dukkipati 	if (icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
36006ba8a3b1SNandita Dukkipati 	    icsk->icsk_pending == ICSK_TIME_LOSS_PROBE)
3601750ea2baSYuchung Cheng 		tcp_rearm_rto(sk);
3602750ea2baSYuchung Cheng 
36030c9ab092SNeal Cardwell 	if (after(ack, prior_snd_una)) {
36042e605294SIlpo Järvinen 		flag |= FLAG_SND_UNA_ADVANCED;
36050c9ab092SNeal Cardwell 		icsk->icsk_retransmits = 0;
36060c9ab092SNeal Cardwell 	}
36072e605294SIlpo Järvinen 
3608c7caf8d3SIlpo Järvinen 	prior_fackets = tp->fackets_out;
3609c7caf8d3SIlpo Järvinen 
361012fb3dd9SEric Dumazet 	/* ts_recent update must be made after we are sure that the packet
361112fb3dd9SEric Dumazet 	 * is in window.
361212fb3dd9SEric Dumazet 	 */
361312fb3dd9SEric Dumazet 	if (flag & FLAG_UPDATE_TS_RECENT)
361412fb3dd9SEric Dumazet 		tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
361512fb3dd9SEric Dumazet 
36161da177e4SLinus Torvalds 	if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
36171da177e4SLinus Torvalds 		/* Window is constant, pure forward advance.
36181da177e4SLinus Torvalds 		 * No more checks are required.
36191da177e4SLinus Torvalds 		 * Note, we use the fact that SND.UNA>=SND.WL2.
36201da177e4SLinus Torvalds 		 */
3621ee7537b6SHantzis Fotis 		tcp_update_wl(tp, ack_seq);
36220df48c26SEric Dumazet 		tcp_snd_una_update(tp, ack);
36231da177e4SLinus Torvalds 		flag |= FLAG_WIN_UPDATE;
36241da177e4SLinus Torvalds 
36259890092eSFlorian Westphal 		tcp_in_ack_event(sk, CA_ACK_WIN_UPDATE);
3626317a76f9SStephen Hemminger 
3627de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPACKS);
36281da177e4SLinus Torvalds 	} else {
36299890092eSFlorian Westphal 		u32 ack_ev_flags = CA_ACK_SLOWPATH;
36309890092eSFlorian Westphal 
36311da177e4SLinus Torvalds 		if (ack_seq != TCP_SKB_CB(skb)->end_seq)
36321da177e4SLinus Torvalds 			flag |= FLAG_DATA;
36331da177e4SLinus Torvalds 		else
3634de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPPUREACKS);
36351da177e4SLinus Torvalds 
36369e412ba7SIlpo Järvinen 		flag |= tcp_ack_update_window(sk, skb, ack, ack_seq);
36371da177e4SLinus Torvalds 
36381da177e4SLinus Torvalds 		if (TCP_SKB_CB(skb)->sacked)
363959c9af42SYuchung Cheng 			flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
3640196da974SKenneth Klette Jonassen 							&sack_state);
36411da177e4SLinus Torvalds 
3642735d3831SFlorian Westphal 		if (tcp_ecn_rcv_ecn_echo(tp, tcp_hdr(skb))) {
36431da177e4SLinus Torvalds 			flag |= FLAG_ECE;
36449890092eSFlorian Westphal 			ack_ev_flags |= CA_ACK_ECE;
36459890092eSFlorian Westphal 		}
36461da177e4SLinus Torvalds 
36479890092eSFlorian Westphal 		if (flag & FLAG_WIN_UPDATE)
36489890092eSFlorian Westphal 			ack_ev_flags |= CA_ACK_WIN_UPDATE;
36499890092eSFlorian Westphal 
36509890092eSFlorian Westphal 		tcp_in_ack_event(sk, ack_ev_flags);
36511da177e4SLinus Torvalds 	}
36521da177e4SLinus Torvalds 
36531da177e4SLinus Torvalds 	/* We passed data and got it acked, remove any soft error
36541da177e4SLinus Torvalds 	 * log. Something worked...
36551da177e4SLinus Torvalds 	 */
36561da177e4SLinus Torvalds 	sk->sk_err_soft = 0;
36574b53fb67SDavid S. Miller 	icsk->icsk_probes_out = 0;
36581da177e4SLinus Torvalds 	tp->rcv_tstamp = tcp_time_stamp;
36591da177e4SLinus Torvalds 	if (!prior_packets)
36601da177e4SLinus Torvalds 		goto no_queue;
36611da177e4SLinus Torvalds 
36621da177e4SLinus Torvalds 	/* See if we can take anything off of the retransmit queue. */
36633ebd8871SYuchung Cheng 	flag |= tcp_clean_rtx_queue(sk, prior_fackets, prior_snd_una, &acked,
366431231a8aSKenneth Klette Jonassen 				    &sack_state);
3665a262f0cdSNandita Dukkipati 
36660f7cc9a3SYuchung Cheng 	if (tcp_ack_is_dubious(sk, flag)) {
36677d2b55f8SNeal Cardwell 		is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP));
366831ba0c10SYuchung Cheng 		tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
36691da177e4SLinus Torvalds 	}
36709b717a8dSNandita Dukkipati 	if (tp->tlp_high_seq)
36719b717a8dSNandita Dukkipati 		tcp_process_tlp_ack(sk, ack, flag);
36729b717a8dSNandita Dukkipati 
36735110effeSDavid S. Miller 	if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP)) {
36745110effeSDavid S. Miller 		struct dst_entry *dst = __sk_dst_get(sk);
36755110effeSDavid S. Miller 		if (dst)
36765110effeSDavid S. Miller 			dst_confirm(dst);
36775110effeSDavid S. Miller 	}
36786ba8a3b1SNandita Dukkipati 
36796ba8a3b1SNandita Dukkipati 	if (icsk->icsk_pending == ICSK_TIME_RETRANS)
36806ba8a3b1SNandita Dukkipati 		tcp_schedule_loss_probe(sk);
3681d452e6caSYuchung Cheng 	tcp_cong_control(sk, ack, tp->delivered - prior_delivered, flag);
3682e662ca40SYuchung Cheng 	tcp_xmit_recovery(sk, rexmit);
36831da177e4SLinus Torvalds 	return 1;
36841da177e4SLinus Torvalds 
36851da177e4SLinus Torvalds no_queue:
36865628adf1SNeal Cardwell 	/* If data was DSACKed, see if we can undo a cwnd reduction. */
36875628adf1SNeal Cardwell 	if (flag & FLAG_DSACKING_ACK)
368831ba0c10SYuchung Cheng 		tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
36891da177e4SLinus Torvalds 	/* If this ack opens up a zero window, clear backoff.  It was
36901da177e4SLinus Torvalds 	 * being used to time the probes, and is probably far higher than
36911da177e4SLinus Torvalds 	 * it needs to be for normal retransmission.
36921da177e4SLinus Torvalds 	 */
3693fe067e8aSDavid S. Miller 	if (tcp_send_head(sk))
36941da177e4SLinus Torvalds 		tcp_ack_probe(sk);
36959b717a8dSNandita Dukkipati 
36969b717a8dSNandita Dukkipati 	if (tp->tlp_high_seq)
36979b717a8dSNandita Dukkipati 		tcp_process_tlp_ack(sk, ack, flag);
36981da177e4SLinus Torvalds 	return 1;
36991da177e4SLinus Torvalds 
370096e0bf4bSJohn Dykstra invalid_ack:
370196e0bf4bSJohn Dykstra 	SOCK_DEBUG(sk, "Ack %u after %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
370296e0bf4bSJohn Dykstra 	return -1;
370396e0bf4bSJohn Dykstra 
37041da177e4SLinus Torvalds old_ack:
3705e95ae2f2SNeal Cardwell 	/* If data was SACKed, tag it and see if we should send more data.
3706e95ae2f2SNeal Cardwell 	 * If data was DSACKed, see if we can undo a cwnd reduction.
3707e95ae2f2SNeal Cardwell 	 */
37088aca6cb1SIlpo Järvinen 	if (TCP_SKB_CB(skb)->sacked) {
370959c9af42SYuchung Cheng 		flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
3710196da974SKenneth Klette Jonassen 						&sack_state);
371131ba0c10SYuchung Cheng 		tcp_fastretrans_alert(sk, acked, is_dupack, &flag, &rexmit);
3712e662ca40SYuchung Cheng 		tcp_xmit_recovery(sk, rexmit);
37138aca6cb1SIlpo Järvinen 	}
37141da177e4SLinus Torvalds 
371596e0bf4bSJohn Dykstra 	SOCK_DEBUG(sk, "Ack %u before %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
37161da177e4SLinus Torvalds 	return 0;
37171da177e4SLinus Torvalds }
37181da177e4SLinus Torvalds 
37197f9b838bSDaniel Lee static void tcp_parse_fastopen_option(int len, const unsigned char *cookie,
37207f9b838bSDaniel Lee 				      bool syn, struct tcp_fastopen_cookie *foc,
37217f9b838bSDaniel Lee 				      bool exp_opt)
37227f9b838bSDaniel Lee {
37237f9b838bSDaniel Lee 	/* Valid only in SYN or SYN-ACK with an even length.  */
37247f9b838bSDaniel Lee 	if (!foc || !syn || len < 0 || (len & 1))
37257f9b838bSDaniel Lee 		return;
37267f9b838bSDaniel Lee 
37277f9b838bSDaniel Lee 	if (len >= TCP_FASTOPEN_COOKIE_MIN &&
37287f9b838bSDaniel Lee 	    len <= TCP_FASTOPEN_COOKIE_MAX)
37297f9b838bSDaniel Lee 		memcpy(foc->val, cookie, len);
37307f9b838bSDaniel Lee 	else if (len != 0)
37317f9b838bSDaniel Lee 		len = -1;
37327f9b838bSDaniel Lee 	foc->len = len;
37337f9b838bSDaniel Lee 	foc->exp = exp_opt;
37347f9b838bSDaniel Lee }
37357f9b838bSDaniel Lee 
37361da177e4SLinus Torvalds /* Look for tcp options. Normally only called on SYN and SYNACK packets.
37371da177e4SLinus Torvalds  * But, this can also be called on packets in the established flow when
37381da177e4SLinus Torvalds  * the fast version below fails.
37391da177e4SLinus Torvalds  */
37401a2c6181SChristoph Paasch void tcp_parse_options(const struct sk_buff *skb,
37411a2c6181SChristoph Paasch 		       struct tcp_options_received *opt_rx, int estab,
37422100c8d2SYuchung Cheng 		       struct tcp_fastopen_cookie *foc)
37431da177e4SLinus Torvalds {
3744cf533ea5SEric Dumazet 	const unsigned char *ptr;
3745cf533ea5SEric Dumazet 	const struct tcphdr *th = tcp_hdr(skb);
37461da177e4SLinus Torvalds 	int length = (th->doff * 4) - sizeof(struct tcphdr);
37471da177e4SLinus Torvalds 
3748cf533ea5SEric Dumazet 	ptr = (const unsigned char *)(th + 1);
37491da177e4SLinus Torvalds 	opt_rx->saw_tstamp = 0;
37501da177e4SLinus Torvalds 
37511da177e4SLinus Torvalds 	while (length > 0) {
37521da177e4SLinus Torvalds 		int opcode = *ptr++;
37531da177e4SLinus Torvalds 		int opsize;
37541da177e4SLinus Torvalds 
37551da177e4SLinus Torvalds 		switch (opcode) {
37561da177e4SLinus Torvalds 		case TCPOPT_EOL:
37571da177e4SLinus Torvalds 			return;
37581da177e4SLinus Torvalds 		case TCPOPT_NOP:	/* Ref: RFC 793 section 3.1 */
37591da177e4SLinus Torvalds 			length--;
37601da177e4SLinus Torvalds 			continue;
37611da177e4SLinus Torvalds 		default:
37621da177e4SLinus Torvalds 			opsize = *ptr++;
37631da177e4SLinus Torvalds 			if (opsize < 2) /* "silly options" */
37641da177e4SLinus Torvalds 				return;
37651da177e4SLinus Torvalds 			if (opsize > length)
37661da177e4SLinus Torvalds 				return;	/* don't parse partial options */
37671da177e4SLinus Torvalds 			switch (opcode) {
37681da177e4SLinus Torvalds 			case TCPOPT_MSS:
37691da177e4SLinus Torvalds 				if (opsize == TCPOLEN_MSS && th->syn && !estab) {
3770d3e2ce3bSHarvey Harrison 					u16 in_mss = get_unaligned_be16(ptr);
37711da177e4SLinus Torvalds 					if (in_mss) {
3772f038ac8fSIlpo Järvinen 						if (opt_rx->user_mss &&
3773f038ac8fSIlpo Järvinen 						    opt_rx->user_mss < in_mss)
37741da177e4SLinus Torvalds 							in_mss = opt_rx->user_mss;
37751da177e4SLinus Torvalds 						opt_rx->mss_clamp = in_mss;
37761da177e4SLinus Torvalds 					}
37771da177e4SLinus Torvalds 				}
37781da177e4SLinus Torvalds 				break;
37791da177e4SLinus Torvalds 			case TCPOPT_WINDOW:
3780f038ac8fSIlpo Järvinen 				if (opsize == TCPOLEN_WINDOW && th->syn &&
3781bb5b7c11SDavid S. Miller 				    !estab && sysctl_tcp_window_scaling) {
37821da177e4SLinus Torvalds 					__u8 snd_wscale = *(__u8 *)ptr;
37831da177e4SLinus Torvalds 					opt_rx->wscale_ok = 1;
37841da177e4SLinus Torvalds 					if (snd_wscale > 14) {
3785e87cc472SJoe Perches 						net_info_ratelimited("%s: Illegal window scaling value %d >14 received\n",
3786058bd4d2SJoe Perches 								     __func__,
37871da177e4SLinus Torvalds 								     snd_wscale);
37881da177e4SLinus Torvalds 						snd_wscale = 14;
37891da177e4SLinus Torvalds 					}
37901da177e4SLinus Torvalds 					opt_rx->snd_wscale = snd_wscale;
37911da177e4SLinus Torvalds 				}
37921da177e4SLinus Torvalds 				break;
37931da177e4SLinus Torvalds 			case TCPOPT_TIMESTAMP:
3794f038ac8fSIlpo Järvinen 				if ((opsize == TCPOLEN_TIMESTAMP) &&
3795f038ac8fSIlpo Järvinen 				    ((estab && opt_rx->tstamp_ok) ||
3796bb5b7c11SDavid S. Miller 				     (!estab && sysctl_tcp_timestamps))) {
37971da177e4SLinus Torvalds 					opt_rx->saw_tstamp = 1;
3798d3e2ce3bSHarvey Harrison 					opt_rx->rcv_tsval = get_unaligned_be32(ptr);
3799d3e2ce3bSHarvey Harrison 					opt_rx->rcv_tsecr = get_unaligned_be32(ptr + 4);
38001da177e4SLinus Torvalds 				}
38011da177e4SLinus Torvalds 				break;
38021da177e4SLinus Torvalds 			case TCPOPT_SACK_PERM:
3803f038ac8fSIlpo Järvinen 				if (opsize == TCPOLEN_SACK_PERM && th->syn &&
3804bb5b7c11SDavid S. Miller 				    !estab && sysctl_tcp_sack) {
3805ab56222aSVijay Subramanian 					opt_rx->sack_ok = TCP_SACK_SEEN;
38061da177e4SLinus Torvalds 					tcp_sack_reset(opt_rx);
38071da177e4SLinus Torvalds 				}
38081da177e4SLinus Torvalds 				break;
38091da177e4SLinus Torvalds 
38101da177e4SLinus Torvalds 			case TCPOPT_SACK:
38111da177e4SLinus Torvalds 				if ((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) &&
38121da177e4SLinus Torvalds 				   !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) &&
38131da177e4SLinus Torvalds 				   opt_rx->sack_ok) {
38141da177e4SLinus Torvalds 					TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th;
38151da177e4SLinus Torvalds 				}
3816d7ea5b91SIlpo Järvinen 				break;
3817cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
3818cfb6eeb4SYOSHIFUJI Hideaki 			case TCPOPT_MD5SIG:
3819cfb6eeb4SYOSHIFUJI Hideaki 				/*
3820cfb6eeb4SYOSHIFUJI Hideaki 				 * The MD5 Hash has already been
3821cfb6eeb4SYOSHIFUJI Hideaki 				 * checked (see tcp_v{4,6}_do_rcv()).
3822cfb6eeb4SYOSHIFUJI Hideaki 				 */
3823cfb6eeb4SYOSHIFUJI Hideaki 				break;
3824cfb6eeb4SYOSHIFUJI Hideaki #endif
38257f9b838bSDaniel Lee 			case TCPOPT_FASTOPEN:
38267f9b838bSDaniel Lee 				tcp_parse_fastopen_option(
38277f9b838bSDaniel Lee 					opsize - TCPOLEN_FASTOPEN_BASE,
38287f9b838bSDaniel Lee 					ptr, th->syn, foc, false);
38297f9b838bSDaniel Lee 				break;
38307f9b838bSDaniel Lee 
38312100c8d2SYuchung Cheng 			case TCPOPT_EXP:
38322100c8d2SYuchung Cheng 				/* Fast Open option shares code 254 using a
38337f9b838bSDaniel Lee 				 * 16 bits magic number.
38342100c8d2SYuchung Cheng 				 */
38357f9b838bSDaniel Lee 				if (opsize >= TCPOLEN_EXP_FASTOPEN_BASE &&
38367f9b838bSDaniel Lee 				    get_unaligned_be16(ptr) ==
38377f9b838bSDaniel Lee 				    TCPOPT_FASTOPEN_MAGIC)
38387f9b838bSDaniel Lee 					tcp_parse_fastopen_option(opsize -
38397f9b838bSDaniel Lee 						TCPOLEN_EXP_FASTOPEN_BASE,
38407f9b838bSDaniel Lee 						ptr + 2, th->syn, foc, true);
38412100c8d2SYuchung Cheng 				break;
38422100c8d2SYuchung Cheng 
38432100c8d2SYuchung Cheng 			}
38441da177e4SLinus Torvalds 			ptr += opsize-2;
38451da177e4SLinus Torvalds 			length -= opsize;
38463ff50b79SStephen Hemminger 		}
38471da177e4SLinus Torvalds 	}
38481da177e4SLinus Torvalds }
38494bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_parse_options);
38501da177e4SLinus Torvalds 
3851a2a385d6SEric Dumazet static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr *th)
3852a4356b29SIlpo Järvinen {
3853cf533ea5SEric Dumazet 	const __be32 *ptr = (const __be32 *)(th + 1);
3854a4356b29SIlpo Järvinen 
3855a4356b29SIlpo Järvinen 	if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
3856a4356b29SIlpo Järvinen 			  | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
3857a4356b29SIlpo Järvinen 		tp->rx_opt.saw_tstamp = 1;
3858a4356b29SIlpo Järvinen 		++ptr;
3859a4356b29SIlpo Järvinen 		tp->rx_opt.rcv_tsval = ntohl(*ptr);
3860a4356b29SIlpo Järvinen 		++ptr;
3861e3e12028SAndrew Vagin 		if (*ptr)
3862ee684b6fSAndrey Vagin 			tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset;
3863e3e12028SAndrew Vagin 		else
3864e3e12028SAndrew Vagin 			tp->rx_opt.rcv_tsecr = 0;
3865a2a385d6SEric Dumazet 		return true;
3866a4356b29SIlpo Järvinen 	}
3867a2a385d6SEric Dumazet 	return false;
3868a4356b29SIlpo Järvinen }
3869a4356b29SIlpo Järvinen 
38701da177e4SLinus Torvalds /* Fast parse options. This hopes to only see timestamps.
38711da177e4SLinus Torvalds  * If it is wrong it falls back on tcp_parse_options().
38721da177e4SLinus Torvalds  */
3873a2a385d6SEric Dumazet static bool tcp_fast_parse_options(const struct sk_buff *skb,
38741a2c6181SChristoph Paasch 				   const struct tcphdr *th, struct tcp_sock *tp)
38751da177e4SLinus Torvalds {
38764957faadSWilliam Allen Simpson 	/* In the spirit of fast parsing, compare doff directly to constant
38774957faadSWilliam Allen Simpson 	 * values.  Because equality is used, short doff can be ignored here.
38784957faadSWilliam Allen Simpson 	 */
38794957faadSWilliam Allen Simpson 	if (th->doff == (sizeof(*th) / 4)) {
38801da177e4SLinus Torvalds 		tp->rx_opt.saw_tstamp = 0;
3881a2a385d6SEric Dumazet 		return false;
38821da177e4SLinus Torvalds 	} else if (tp->rx_opt.tstamp_ok &&
38834957faadSWilliam Allen Simpson 		   th->doff == ((sizeof(*th) + TCPOLEN_TSTAMP_ALIGNED) / 4)) {
3884a4356b29SIlpo Järvinen 		if (tcp_parse_aligned_timestamp(tp, th))
3885a2a385d6SEric Dumazet 			return true;
38861da177e4SLinus Torvalds 	}
3887ee684b6fSAndrey Vagin 
38881a2c6181SChristoph Paasch 	tcp_parse_options(skb, &tp->rx_opt, 1, NULL);
3889e3e12028SAndrew Vagin 	if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
3890ee684b6fSAndrey Vagin 		tp->rx_opt.rcv_tsecr -= tp->tsoffset;
3891ee684b6fSAndrey Vagin 
3892a2a385d6SEric Dumazet 	return true;
38931da177e4SLinus Torvalds }
38941da177e4SLinus Torvalds 
38957d5d5525SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
38967d5d5525SYOSHIFUJI Hideaki /*
38977d5d5525SYOSHIFUJI Hideaki  * Parse MD5 Signature option
38987d5d5525SYOSHIFUJI Hideaki  */
3899cf533ea5SEric Dumazet const u8 *tcp_parse_md5sig_option(const struct tcphdr *th)
39007d5d5525SYOSHIFUJI Hideaki {
39017d5d5525SYOSHIFUJI Hideaki 	int length = (th->doff << 2) - sizeof(*th);
3902cf533ea5SEric Dumazet 	const u8 *ptr = (const u8 *)(th + 1);
39037d5d5525SYOSHIFUJI Hideaki 
39047d5d5525SYOSHIFUJI Hideaki 	/* If the TCP option is too short, we can short cut */
39057d5d5525SYOSHIFUJI Hideaki 	if (length < TCPOLEN_MD5SIG)
39067d5d5525SYOSHIFUJI Hideaki 		return NULL;
39077d5d5525SYOSHIFUJI Hideaki 
39087d5d5525SYOSHIFUJI Hideaki 	while (length > 0) {
39097d5d5525SYOSHIFUJI Hideaki 		int opcode = *ptr++;
39107d5d5525SYOSHIFUJI Hideaki 		int opsize;
39117d5d5525SYOSHIFUJI Hideaki 
39127d5d5525SYOSHIFUJI Hideaki 		switch (opcode) {
39137d5d5525SYOSHIFUJI Hideaki 		case TCPOPT_EOL:
39147d5d5525SYOSHIFUJI Hideaki 			return NULL;
39157d5d5525SYOSHIFUJI Hideaki 		case TCPOPT_NOP:
39167d5d5525SYOSHIFUJI Hideaki 			length--;
39177d5d5525SYOSHIFUJI Hideaki 			continue;
39187d5d5525SYOSHIFUJI Hideaki 		default:
39197d5d5525SYOSHIFUJI Hideaki 			opsize = *ptr++;
39207d5d5525SYOSHIFUJI Hideaki 			if (opsize < 2 || opsize > length)
39217d5d5525SYOSHIFUJI Hideaki 				return NULL;
39227d5d5525SYOSHIFUJI Hideaki 			if (opcode == TCPOPT_MD5SIG)
3923ba78e2ddSDmitry Popov 				return opsize == TCPOLEN_MD5SIG ? ptr : NULL;
39247d5d5525SYOSHIFUJI Hideaki 		}
39257d5d5525SYOSHIFUJI Hideaki 		ptr += opsize - 2;
39267d5d5525SYOSHIFUJI Hideaki 		length -= opsize;
39277d5d5525SYOSHIFUJI Hideaki 	}
39287d5d5525SYOSHIFUJI Hideaki 	return NULL;
39297d5d5525SYOSHIFUJI Hideaki }
39304bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_parse_md5sig_option);
39317d5d5525SYOSHIFUJI Hideaki #endif
39327d5d5525SYOSHIFUJI Hideaki 
39331da177e4SLinus Torvalds /* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM
39341da177e4SLinus Torvalds  *
39351da177e4SLinus Torvalds  * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
39361da177e4SLinus Torvalds  * it can pass through stack. So, the following predicate verifies that
39371da177e4SLinus Torvalds  * this segment is not used for anything but congestion avoidance or
39381da177e4SLinus Torvalds  * fast retransmit. Moreover, we even are able to eliminate most of such
39391da177e4SLinus Torvalds  * second order effects, if we apply some small "replay" window (~RTO)
39401da177e4SLinus Torvalds  * to timestamp space.
39411da177e4SLinus Torvalds  *
39421da177e4SLinus Torvalds  * All these measures still do not guarantee that we reject wrapped ACKs
39431da177e4SLinus Torvalds  * on networks with high bandwidth, when sequence space is recycled fastly,
39441da177e4SLinus Torvalds  * but it guarantees that such events will be very rare and do not affect
39451da177e4SLinus Torvalds  * connection seriously. This doesn't look nice, but alas, PAWS is really
39461da177e4SLinus Torvalds  * buggy extension.
39471da177e4SLinus Torvalds  *
39481da177e4SLinus Torvalds  * [ Later note. Even worse! It is buggy for segments _with_ data. RFC
39491da177e4SLinus Torvalds  * states that events when retransmit arrives after original data are rare.
39501da177e4SLinus Torvalds  * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is
39511da177e4SLinus Torvalds  * the biggest problem on large power networks even with minor reordering.
39521da177e4SLinus Torvalds  * OK, let's give it small replay window. If peer clock is even 1hz, it is safe
39531da177e4SLinus Torvalds  * up to bandwidth of 18Gigabit/sec. 8) ]
39541da177e4SLinus Torvalds  */
39551da177e4SLinus Torvalds 
3956463c84b9SArnaldo Carvalho de Melo static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb)
39571da177e4SLinus Torvalds {
3958cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
3959cf533ea5SEric Dumazet 	const struct tcphdr *th = tcp_hdr(skb);
39601da177e4SLinus Torvalds 	u32 seq = TCP_SKB_CB(skb)->seq;
39611da177e4SLinus Torvalds 	u32 ack = TCP_SKB_CB(skb)->ack_seq;
39621da177e4SLinus Torvalds 
39631da177e4SLinus Torvalds 	return (/* 1. Pure ACK with correct sequence number. */
39641da177e4SLinus Torvalds 		(th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) &&
39651da177e4SLinus Torvalds 
39661da177e4SLinus Torvalds 		/* 2. ... and duplicate ACK. */
39671da177e4SLinus Torvalds 		ack == tp->snd_una &&
39681da177e4SLinus Torvalds 
39691da177e4SLinus Torvalds 		/* 3. ... and does not update window. */
39701da177e4SLinus Torvalds 		!tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) &&
39711da177e4SLinus Torvalds 
39721da177e4SLinus Torvalds 		/* 4. ... and sits in replay window. */
3973463c84b9SArnaldo Carvalho de Melo 		(s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) <= (inet_csk(sk)->icsk_rto * 1024) / HZ);
39741da177e4SLinus Torvalds }
39751da177e4SLinus Torvalds 
397667b95bd7SVijay Subramanian static inline bool tcp_paws_discard(const struct sock *sk,
3977056834d9SIlpo Järvinen 				   const struct sk_buff *skb)
39781da177e4SLinus Torvalds {
3979463c84b9SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
3980c887e6d2SIlpo Järvinen 
3981c887e6d2SIlpo Järvinen 	return !tcp_paws_check(&tp->rx_opt, TCP_PAWS_WINDOW) &&
3982c887e6d2SIlpo Järvinen 	       !tcp_disordered_ack(sk, skb);
39831da177e4SLinus Torvalds }
39841da177e4SLinus Torvalds 
39851da177e4SLinus Torvalds /* Check segment sequence number for validity.
39861da177e4SLinus Torvalds  *
39871da177e4SLinus Torvalds  * Segment controls are considered valid, if the segment
39881da177e4SLinus Torvalds  * fits to the window after truncation to the window. Acceptability
39891da177e4SLinus Torvalds  * of data (and SYN, FIN, of course) is checked separately.
39901da177e4SLinus Torvalds  * See tcp_data_queue(), for example.
39911da177e4SLinus Torvalds  *
39921da177e4SLinus Torvalds  * Also, controls (RST is main one) are accepted using RCV.WUP instead
39931da177e4SLinus Torvalds  * of RCV.NXT. Peer still did not advance his SND.UNA when we
39941da177e4SLinus Torvalds  * delayed ACK, so that hisSND.UNA<=ourRCV.WUP.
39951da177e4SLinus Torvalds  * (borrowed from freebsd)
39961da177e4SLinus Torvalds  */
39971da177e4SLinus Torvalds 
399867b95bd7SVijay Subramanian static inline bool tcp_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq)
39991da177e4SLinus Torvalds {
40001da177e4SLinus Torvalds 	return	!before(end_seq, tp->rcv_wup) &&
40011da177e4SLinus Torvalds 		!after(seq, tp->rcv_nxt + tcp_receive_window(tp));
40021da177e4SLinus Torvalds }
40031da177e4SLinus Torvalds 
40041da177e4SLinus Torvalds /* When we get a reset we do this. */
400510467163SJerry Chu void tcp_reset(struct sock *sk)
40061da177e4SLinus Torvalds {
40071da177e4SLinus Torvalds 	/* We want the right error as BSD sees it (and indeed as we do). */
40081da177e4SLinus Torvalds 	switch (sk->sk_state) {
40091da177e4SLinus Torvalds 	case TCP_SYN_SENT:
40101da177e4SLinus Torvalds 		sk->sk_err = ECONNREFUSED;
40111da177e4SLinus Torvalds 		break;
40121da177e4SLinus Torvalds 	case TCP_CLOSE_WAIT:
40131da177e4SLinus Torvalds 		sk->sk_err = EPIPE;
40141da177e4SLinus Torvalds 		break;
40151da177e4SLinus Torvalds 	case TCP_CLOSE:
40161da177e4SLinus Torvalds 		return;
40171da177e4SLinus Torvalds 	default:
40181da177e4SLinus Torvalds 		sk->sk_err = ECONNRESET;
40191da177e4SLinus Torvalds 	}
4020a4d25803STom Marshall 	/* This barrier is coupled with smp_rmb() in tcp_poll() */
4021a4d25803STom Marshall 	smp_wmb();
40221da177e4SLinus Torvalds 
40231da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD))
40241da177e4SLinus Torvalds 		sk->sk_error_report(sk);
40251da177e4SLinus Torvalds 
40261da177e4SLinus Torvalds 	tcp_done(sk);
40271da177e4SLinus Torvalds }
40281da177e4SLinus Torvalds 
40291da177e4SLinus Torvalds /*
40301da177e4SLinus Torvalds  * 	Process the FIN bit. This now behaves as it is supposed to work
40311da177e4SLinus Torvalds  *	and the FIN takes effect when it is validly part of sequence
40321da177e4SLinus Torvalds  *	space. Not before when we get holes.
40331da177e4SLinus Torvalds  *
40341da177e4SLinus Torvalds  *	If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
40351da177e4SLinus Torvalds  *	(and thence onto LAST-ACK and finally, CLOSE, we never enter
40361da177e4SLinus Torvalds  *	TIME-WAIT)
40371da177e4SLinus Torvalds  *
40381da177e4SLinus Torvalds  *	If we are in FINWAIT-1, a received FIN indicates simultaneous
40391da177e4SLinus Torvalds  *	close and we go into CLOSING (and later onto TIME-WAIT)
40401da177e4SLinus Torvalds  *
40411da177e4SLinus Torvalds  *	If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
40421da177e4SLinus Torvalds  */
4043e3e17b77SEric Dumazet void tcp_fin(struct sock *sk)
40441da177e4SLinus Torvalds {
40451da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
40461da177e4SLinus Torvalds 
4047463c84b9SArnaldo Carvalho de Melo 	inet_csk_schedule_ack(sk);
40481da177e4SLinus Torvalds 
40491da177e4SLinus Torvalds 	sk->sk_shutdown |= RCV_SHUTDOWN;
40501da177e4SLinus Torvalds 	sock_set_flag(sk, SOCK_DONE);
40511da177e4SLinus Torvalds 
40521da177e4SLinus Torvalds 	switch (sk->sk_state) {
40531da177e4SLinus Torvalds 	case TCP_SYN_RECV:
40541da177e4SLinus Torvalds 	case TCP_ESTABLISHED:
40551da177e4SLinus Torvalds 		/* Move to CLOSE_WAIT */
40561da177e4SLinus Torvalds 		tcp_set_state(sk, TCP_CLOSE_WAIT);
4057463c84b9SArnaldo Carvalho de Melo 		inet_csk(sk)->icsk_ack.pingpong = 1;
40581da177e4SLinus Torvalds 		break;
40591da177e4SLinus Torvalds 
40601da177e4SLinus Torvalds 	case TCP_CLOSE_WAIT:
40611da177e4SLinus Torvalds 	case TCP_CLOSING:
40621da177e4SLinus Torvalds 		/* Received a retransmission of the FIN, do
40631da177e4SLinus Torvalds 		 * nothing.
40641da177e4SLinus Torvalds 		 */
40651da177e4SLinus Torvalds 		break;
40661da177e4SLinus Torvalds 	case TCP_LAST_ACK:
40671da177e4SLinus Torvalds 		/* RFC793: Remain in the LAST-ACK state. */
40681da177e4SLinus Torvalds 		break;
40691da177e4SLinus Torvalds 
40701da177e4SLinus Torvalds 	case TCP_FIN_WAIT1:
40711da177e4SLinus Torvalds 		/* This case occurs when a simultaneous close
40721da177e4SLinus Torvalds 		 * happens, we must ack the received FIN and
40731da177e4SLinus Torvalds 		 * enter the CLOSING state.
40741da177e4SLinus Torvalds 		 */
40751da177e4SLinus Torvalds 		tcp_send_ack(sk);
40761da177e4SLinus Torvalds 		tcp_set_state(sk, TCP_CLOSING);
40771da177e4SLinus Torvalds 		break;
40781da177e4SLinus Torvalds 	case TCP_FIN_WAIT2:
40791da177e4SLinus Torvalds 		/* Received a FIN -- send ACK and enter TIME_WAIT. */
40801da177e4SLinus Torvalds 		tcp_send_ack(sk);
40811da177e4SLinus Torvalds 		tcp_time_wait(sk, TCP_TIME_WAIT, 0);
40821da177e4SLinus Torvalds 		break;
40831da177e4SLinus Torvalds 	default:
40841da177e4SLinus Torvalds 		/* Only TCP_LISTEN and TCP_CLOSE are left, in these
40851da177e4SLinus Torvalds 		 * cases we should never reach this piece of code.
40861da177e4SLinus Torvalds 		 */
4087058bd4d2SJoe Perches 		pr_err("%s: Impossible, sk->sk_state=%d\n",
40880dc47877SHarvey Harrison 		       __func__, sk->sk_state);
40891da177e4SLinus Torvalds 		break;
40903ff50b79SStephen Hemminger 	}
40911da177e4SLinus Torvalds 
40921da177e4SLinus Torvalds 	/* It _is_ possible, that we have something out-of-order _after_ FIN.
40931da177e4SLinus Torvalds 	 * Probably, we should reset in this case. For now drop them.
40941da177e4SLinus Torvalds 	 */
40951da177e4SLinus Torvalds 	__skb_queue_purge(&tp->out_of_order_queue);
4096e60402d0SIlpo Järvinen 	if (tcp_is_sack(tp))
40971da177e4SLinus Torvalds 		tcp_sack_reset(&tp->rx_opt);
40983ab224beSHideo Aoki 	sk_mem_reclaim(sk);
40991da177e4SLinus Torvalds 
41001da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD)) {
41011da177e4SLinus Torvalds 		sk->sk_state_change(sk);
41021da177e4SLinus Torvalds 
41031da177e4SLinus Torvalds 		/* Do not send POLL_HUP for half duplex close. */
41041da177e4SLinus Torvalds 		if (sk->sk_shutdown == SHUTDOWN_MASK ||
41051da177e4SLinus Torvalds 		    sk->sk_state == TCP_CLOSE)
41068d8ad9d7SPavel Emelyanov 			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
41071da177e4SLinus Torvalds 		else
41088d8ad9d7SPavel Emelyanov 			sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
41091da177e4SLinus Torvalds 	}
41101da177e4SLinus Torvalds }
41111da177e4SLinus Torvalds 
4112a2a385d6SEric Dumazet static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,
4113056834d9SIlpo Järvinen 				  u32 end_seq)
41141da177e4SLinus Torvalds {
41151da177e4SLinus Torvalds 	if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) {
41161da177e4SLinus Torvalds 		if (before(seq, sp->start_seq))
41171da177e4SLinus Torvalds 			sp->start_seq = seq;
41181da177e4SLinus Torvalds 		if (after(end_seq, sp->end_seq))
41191da177e4SLinus Torvalds 			sp->end_seq = end_seq;
4120a2a385d6SEric Dumazet 		return true;
41211da177e4SLinus Torvalds 	}
4122a2a385d6SEric Dumazet 	return false;
41231da177e4SLinus Torvalds }
41241da177e4SLinus Torvalds 
41251ed83465SPavel Emelyanov static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq)
41261da177e4SLinus Torvalds {
41271ed83465SPavel Emelyanov 	struct tcp_sock *tp = tcp_sk(sk);
41281ed83465SPavel Emelyanov 
4129bb5b7c11SDavid S. Miller 	if (tcp_is_sack(tp) && sysctl_tcp_dsack) {
413040b215e5SPavel Emelyanov 		int mib_idx;
413140b215e5SPavel Emelyanov 
41321da177e4SLinus Torvalds 		if (before(seq, tp->rcv_nxt))
413340b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPDSACKOLDSENT;
41341da177e4SLinus Torvalds 		else
413540b215e5SPavel Emelyanov 			mib_idx = LINUX_MIB_TCPDSACKOFOSENT;
413640b215e5SPavel Emelyanov 
4137de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), mib_idx);
41381da177e4SLinus Torvalds 
41391da177e4SLinus Torvalds 		tp->rx_opt.dsack = 1;
41401da177e4SLinus Torvalds 		tp->duplicate_sack[0].start_seq = seq;
41411da177e4SLinus Torvalds 		tp->duplicate_sack[0].end_seq = end_seq;
41421da177e4SLinus Torvalds 	}
41431da177e4SLinus Torvalds }
41441da177e4SLinus Torvalds 
41451ed83465SPavel Emelyanov static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq)
41461da177e4SLinus Torvalds {
41471ed83465SPavel Emelyanov 	struct tcp_sock *tp = tcp_sk(sk);
41481ed83465SPavel Emelyanov 
41491da177e4SLinus Torvalds 	if (!tp->rx_opt.dsack)
41501ed83465SPavel Emelyanov 		tcp_dsack_set(sk, seq, end_seq);
41511da177e4SLinus Torvalds 	else
41521da177e4SLinus Torvalds 		tcp_sack_extend(tp->duplicate_sack, seq, end_seq);
41531da177e4SLinus Torvalds }
41541da177e4SLinus Torvalds 
4155cf533ea5SEric Dumazet static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb)
41561da177e4SLinus Torvalds {
41571da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
41581da177e4SLinus Torvalds 
41591da177e4SLinus Torvalds 	if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
41601da177e4SLinus Torvalds 	    before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
4161de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
4162463c84b9SArnaldo Carvalho de Melo 		tcp_enter_quickack_mode(sk);
41631da177e4SLinus Torvalds 
4164bb5b7c11SDavid S. Miller 		if (tcp_is_sack(tp) && sysctl_tcp_dsack) {
41651da177e4SLinus Torvalds 			u32 end_seq = TCP_SKB_CB(skb)->end_seq;
41661da177e4SLinus Torvalds 
41671da177e4SLinus Torvalds 			if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))
41681da177e4SLinus Torvalds 				end_seq = tp->rcv_nxt;
41691ed83465SPavel Emelyanov 			tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, end_seq);
41701da177e4SLinus Torvalds 		}
41711da177e4SLinus Torvalds 	}
41721da177e4SLinus Torvalds 
41731da177e4SLinus Torvalds 	tcp_send_ack(sk);
41741da177e4SLinus Torvalds }
41751da177e4SLinus Torvalds 
41761da177e4SLinus Torvalds /* These routines update the SACK block as out-of-order packets arrive or
41771da177e4SLinus Torvalds  * in-order packets close up the sequence space.
41781da177e4SLinus Torvalds  */
41791da177e4SLinus Torvalds static void tcp_sack_maybe_coalesce(struct tcp_sock *tp)
41801da177e4SLinus Torvalds {
41811da177e4SLinus Torvalds 	int this_sack;
41821da177e4SLinus Torvalds 	struct tcp_sack_block *sp = &tp->selective_acks[0];
41831da177e4SLinus Torvalds 	struct tcp_sack_block *swalk = sp + 1;
41841da177e4SLinus Torvalds 
41851da177e4SLinus Torvalds 	/* See if the recent change to the first SACK eats into
41861da177e4SLinus Torvalds 	 * or hits the sequence space of other SACK blocks, if so coalesce.
41871da177e4SLinus Torvalds 	 */
41881da177e4SLinus Torvalds 	for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;) {
41891da177e4SLinus Torvalds 		if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) {
41901da177e4SLinus Torvalds 			int i;
41911da177e4SLinus Torvalds 
41921da177e4SLinus Torvalds 			/* Zap SWALK, by moving every further SACK up by one slot.
41931da177e4SLinus Torvalds 			 * Decrease num_sacks.
41941da177e4SLinus Torvalds 			 */
41951da177e4SLinus Torvalds 			tp->rx_opt.num_sacks--;
41961da177e4SLinus Torvalds 			for (i = this_sack; i < tp->rx_opt.num_sacks; i++)
41971da177e4SLinus Torvalds 				sp[i] = sp[i + 1];
41981da177e4SLinus Torvalds 			continue;
41991da177e4SLinus Torvalds 		}
42001da177e4SLinus Torvalds 		this_sack++, swalk++;
42011da177e4SLinus Torvalds 	}
42021da177e4SLinus Torvalds }
42031da177e4SLinus Torvalds 
42041da177e4SLinus Torvalds static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq)
42051da177e4SLinus Torvalds {
42061da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
42071da177e4SLinus Torvalds 	struct tcp_sack_block *sp = &tp->selective_acks[0];
42081da177e4SLinus Torvalds 	int cur_sacks = tp->rx_opt.num_sacks;
42091da177e4SLinus Torvalds 	int this_sack;
42101da177e4SLinus Torvalds 
42111da177e4SLinus Torvalds 	if (!cur_sacks)
42121da177e4SLinus Torvalds 		goto new_sack;
42131da177e4SLinus Torvalds 
42141da177e4SLinus Torvalds 	for (this_sack = 0; this_sack < cur_sacks; this_sack++, sp++) {
42151da177e4SLinus Torvalds 		if (tcp_sack_extend(sp, seq, end_seq)) {
42161da177e4SLinus Torvalds 			/* Rotate this_sack to the first one. */
42171da177e4SLinus Torvalds 			for (; this_sack > 0; this_sack--, sp--)
4218a0bffffcSIlpo Järvinen 				swap(*sp, *(sp - 1));
42191da177e4SLinus Torvalds 			if (cur_sacks > 1)
42201da177e4SLinus Torvalds 				tcp_sack_maybe_coalesce(tp);
42211da177e4SLinus Torvalds 			return;
42221da177e4SLinus Torvalds 		}
42231da177e4SLinus Torvalds 	}
42241da177e4SLinus Torvalds 
42251da177e4SLinus Torvalds 	/* Could not find an adjacent existing SACK, build a new one,
42261da177e4SLinus Torvalds 	 * put it at the front, and shift everyone else down.  We
42271da177e4SLinus Torvalds 	 * always know there is at least one SACK present already here.
42281da177e4SLinus Torvalds 	 *
42291da177e4SLinus Torvalds 	 * If the sack array is full, forget about the last one.
42301da177e4SLinus Torvalds 	 */
42314389ddedSAdam Langley 	if (this_sack >= TCP_NUM_SACKS) {
42321da177e4SLinus Torvalds 		this_sack--;
42331da177e4SLinus Torvalds 		tp->rx_opt.num_sacks--;
42341da177e4SLinus Torvalds 		sp--;
42351da177e4SLinus Torvalds 	}
42361da177e4SLinus Torvalds 	for (; this_sack > 0; this_sack--, sp--)
42371da177e4SLinus Torvalds 		*sp = *(sp - 1);
42381da177e4SLinus Torvalds 
42391da177e4SLinus Torvalds new_sack:
42401da177e4SLinus Torvalds 	/* Build the new head SACK, and we're done. */
42411da177e4SLinus Torvalds 	sp->start_seq = seq;
42421da177e4SLinus Torvalds 	sp->end_seq = end_seq;
42431da177e4SLinus Torvalds 	tp->rx_opt.num_sacks++;
42441da177e4SLinus Torvalds }
42451da177e4SLinus Torvalds 
42461da177e4SLinus Torvalds /* RCV.NXT advances, some SACKs should be eaten. */
42471da177e4SLinus Torvalds 
42481da177e4SLinus Torvalds static void tcp_sack_remove(struct tcp_sock *tp)
42491da177e4SLinus Torvalds {
42501da177e4SLinus Torvalds 	struct tcp_sack_block *sp = &tp->selective_acks[0];
42511da177e4SLinus Torvalds 	int num_sacks = tp->rx_opt.num_sacks;
42521da177e4SLinus Torvalds 	int this_sack;
42531da177e4SLinus Torvalds 
42541da177e4SLinus Torvalds 	/* Empty ofo queue, hence, all the SACKs are eaten. Clear. */
4255b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue)) {
42561da177e4SLinus Torvalds 		tp->rx_opt.num_sacks = 0;
42571da177e4SLinus Torvalds 		return;
42581da177e4SLinus Torvalds 	}
42591da177e4SLinus Torvalds 
42601da177e4SLinus Torvalds 	for (this_sack = 0; this_sack < num_sacks;) {
42611da177e4SLinus Torvalds 		/* Check if the start of the sack is covered by RCV.NXT. */
42621da177e4SLinus Torvalds 		if (!before(tp->rcv_nxt, sp->start_seq)) {
42631da177e4SLinus Torvalds 			int i;
42641da177e4SLinus Torvalds 
42651da177e4SLinus Torvalds 			/* RCV.NXT must cover all the block! */
4266547b792cSIlpo Järvinen 			WARN_ON(before(tp->rcv_nxt, sp->end_seq));
42671da177e4SLinus Torvalds 
42681da177e4SLinus Torvalds 			/* Zap this SACK, by moving forward any other SACKS. */
42691da177e4SLinus Torvalds 			for (i = this_sack+1; i < num_sacks; i++)
42701da177e4SLinus Torvalds 				tp->selective_acks[i-1] = tp->selective_acks[i];
42711da177e4SLinus Torvalds 			num_sacks--;
42721da177e4SLinus Torvalds 			continue;
42731da177e4SLinus Torvalds 		}
42741da177e4SLinus Torvalds 		this_sack++;
42751da177e4SLinus Torvalds 		sp++;
42761da177e4SLinus Torvalds 	}
42771da177e4SLinus Torvalds 	tp->rx_opt.num_sacks = num_sacks;
42781da177e4SLinus Torvalds }
42791da177e4SLinus Torvalds 
42801402d366SEric Dumazet /**
42811402d366SEric Dumazet  * tcp_try_coalesce - try to merge skb to prior one
42821402d366SEric Dumazet  * @sk: socket
42831402d366SEric Dumazet  * @to: prior buffer
42841402d366SEric Dumazet  * @from: buffer to add in queue
4285923dd347SEric Dumazet  * @fragstolen: pointer to boolean
42861402d366SEric Dumazet  *
42871402d366SEric Dumazet  * Before queueing skb @from after @to, try to merge them
42881402d366SEric Dumazet  * to reduce overall memory use and queue lengths, if cost is small.
42891402d366SEric Dumazet  * Packets in ofo or receive queues can stay a long time.
42901402d366SEric Dumazet  * Better try to coalesce them right now to avoid future collapses.
4291783c175fSEric Dumazet  * Returns true if caller should free @from instead of queueing it
42921402d366SEric Dumazet  */
4293783c175fSEric Dumazet static bool tcp_try_coalesce(struct sock *sk,
42941402d366SEric Dumazet 			     struct sk_buff *to,
4295329033f6SEric Dumazet 			     struct sk_buff *from,
4296329033f6SEric Dumazet 			     bool *fragstolen)
42971402d366SEric Dumazet {
4298bad43ca8SEric Dumazet 	int delta;
42991402d366SEric Dumazet 
4300329033f6SEric Dumazet 	*fragstolen = false;
430134a802a5SAlexander Duyck 
43021ca7ee30SEric Dumazet 	/* Its possible this segment overlaps with prior segment in queue */
43031ca7ee30SEric Dumazet 	if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
43041ca7ee30SEric Dumazet 		return false;
43051ca7ee30SEric Dumazet 
4306bad43ca8SEric Dumazet 	if (!skb_try_coalesce(to, from, fragstolen, &delta))
4307783c175fSEric Dumazet 		return false;
430834a802a5SAlexander Duyck 
43091402d366SEric Dumazet 	atomic_add(delta, &sk->sk_rmem_alloc);
43101402d366SEric Dumazet 	sk_mem_charge(sk, delta);
431134a802a5SAlexander Duyck 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE);
431234a802a5SAlexander Duyck 	TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
431334a802a5SAlexander Duyck 	TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
4314e93a0435SEric Dumazet 	TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags;
431534a802a5SAlexander Duyck 	return true;
43161402d366SEric Dumazet }
43171402d366SEric Dumazet 
43181da177e4SLinus Torvalds /* This one checks to see if we can put data from the
43191da177e4SLinus Torvalds  * out_of_order queue into the receive_queue.
43201da177e4SLinus Torvalds  */
43211da177e4SLinus Torvalds static void tcp_ofo_queue(struct sock *sk)
43221da177e4SLinus Torvalds {
43231da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
43241da177e4SLinus Torvalds 	__u32 dsack_high = tp->rcv_nxt;
4325bd1e75abSEric Dumazet 	struct sk_buff *skb, *tail;
4326bd1e75abSEric Dumazet 	bool fragstolen, eaten;
43271da177e4SLinus Torvalds 
43281da177e4SLinus Torvalds 	while ((skb = skb_peek(&tp->out_of_order_queue)) != NULL) {
43291da177e4SLinus Torvalds 		if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
43301da177e4SLinus Torvalds 			break;
43311da177e4SLinus Torvalds 
43321da177e4SLinus Torvalds 		if (before(TCP_SKB_CB(skb)->seq, dsack_high)) {
43331da177e4SLinus Torvalds 			__u32 dsack = dsack_high;
43341da177e4SLinus Torvalds 			if (before(TCP_SKB_CB(skb)->end_seq, dsack_high))
43351da177e4SLinus Torvalds 				dsack_high = TCP_SKB_CB(skb)->end_seq;
43361da177e4SLinus Torvalds 			tcp_dsack_extend(sk, TCP_SKB_CB(skb)->seq, dsack);
43371da177e4SLinus Torvalds 		}
43381da177e4SLinus Torvalds 
4339bd1e75abSEric Dumazet 		__skb_unlink(skb, &tp->out_of_order_queue);
43401da177e4SLinus Torvalds 		if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
43411da177e4SLinus Torvalds 			SOCK_DEBUG(sk, "ofo packet was already received\n");
43421da177e4SLinus Torvalds 			__kfree_skb(skb);
43431da177e4SLinus Torvalds 			continue;
43441da177e4SLinus Torvalds 		}
43451da177e4SLinus Torvalds 		SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n",
43461da177e4SLinus Torvalds 			   tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
43471da177e4SLinus Torvalds 			   TCP_SKB_CB(skb)->end_seq);
43481da177e4SLinus Torvalds 
4349bd1e75abSEric Dumazet 		tail = skb_peek_tail(&sk->sk_receive_queue);
4350bd1e75abSEric Dumazet 		eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen);
4351bdd1f9edSEric Dumazet 		tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
4352bd1e75abSEric Dumazet 		if (!eaten)
4353bd1e75abSEric Dumazet 			__skb_queue_tail(&sk->sk_receive_queue, skb);
4354bd1e75abSEric Dumazet 		if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
43551da177e4SLinus Torvalds 			tcp_fin(sk);
4356bd1e75abSEric Dumazet 		if (eaten)
4357bd1e75abSEric Dumazet 			kfree_skb_partial(skb, fragstolen);
43581da177e4SLinus Torvalds 	}
43591da177e4SLinus Torvalds }
43601da177e4SLinus Torvalds 
43611da177e4SLinus Torvalds static bool tcp_prune_ofo_queue(struct sock *sk);
43621da177e4SLinus Torvalds static int tcp_prune_queue(struct sock *sk);
43631da177e4SLinus Torvalds 
43641da177e4SLinus Torvalds static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb,
43651da177e4SLinus Torvalds 				 unsigned int size)
43661da177e4SLinus Torvalds {
43671da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
43681da177e4SLinus Torvalds 	    !sk_rmem_schedule(sk, skb, size)) {
43691da177e4SLinus Torvalds 
43701da177e4SLinus Torvalds 		if (tcp_prune_queue(sk) < 0)
43711da177e4SLinus Torvalds 			return -1;
43721ed83465SPavel Emelyanov 
43731da177e4SLinus Torvalds 		if (!sk_rmem_schedule(sk, skb, size)) {
43741da177e4SLinus Torvalds 			if (!tcp_prune_ofo_queue(sk))
43751da177e4SLinus Torvalds 				return -1;
43761da177e4SLinus Torvalds 
43778728b834SDavid S. Miller 			if (!sk_rmem_schedule(sk, skb, size))
43781da177e4SLinus Torvalds 				return -1;
43791da177e4SLinus Torvalds 		}
43801da177e4SLinus Torvalds 	}
43811da177e4SLinus Torvalds 	return 0;
43821da177e4SLinus Torvalds }
43831da177e4SLinus Torvalds 
4384e86b2919SEric Dumazet static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
4385e86b2919SEric Dumazet {
4386e86b2919SEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
4387e86b2919SEric Dumazet 	struct sk_buff *skb1;
4388e86b2919SEric Dumazet 	u32 seq, end_seq;
4389e86b2919SEric Dumazet 
4390735d3831SFlorian Westphal 	tcp_ecn_check_ce(tp, skb);
4391e86b2919SEric Dumazet 
4392c76562b6SMel Gorman 	if (unlikely(tcp_try_rmem_schedule(sk, skb, skb->truesize))) {
4393a6df1ae9SEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFODROP);
4394e86b2919SEric Dumazet 		__kfree_skb(skb);
4395e86b2919SEric Dumazet 		return;
4396e86b2919SEric Dumazet 	}
4397e86b2919SEric Dumazet 
4398e86b2919SEric Dumazet 	/* Disable header prediction. */
4399e86b2919SEric Dumazet 	tp->pred_flags = 0;
4400e86b2919SEric Dumazet 	inet_csk_schedule_ack(sk);
4401e86b2919SEric Dumazet 
4402a6df1ae9SEric Dumazet 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
4403e86b2919SEric Dumazet 	SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n",
4404e86b2919SEric Dumazet 		   tp->rcv_nxt, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
4405e86b2919SEric Dumazet 
4406e86b2919SEric Dumazet 	skb1 = skb_peek_tail(&tp->out_of_order_queue);
4407e86b2919SEric Dumazet 	if (!skb1) {
4408e86b2919SEric Dumazet 		/* Initial out of order segment, build 1 SACK. */
4409e86b2919SEric Dumazet 		if (tcp_is_sack(tp)) {
4410e86b2919SEric Dumazet 			tp->rx_opt.num_sacks = 1;
4411e86b2919SEric Dumazet 			tp->selective_acks[0].start_seq = TCP_SKB_CB(skb)->seq;
4412e86b2919SEric Dumazet 			tp->selective_acks[0].end_seq =
4413e86b2919SEric Dumazet 						TCP_SKB_CB(skb)->end_seq;
4414e86b2919SEric Dumazet 		}
4415e86b2919SEric Dumazet 		__skb_queue_head(&tp->out_of_order_queue, skb);
4416e86b2919SEric Dumazet 		goto end;
4417e86b2919SEric Dumazet 	}
4418e86b2919SEric Dumazet 
4419e86b2919SEric Dumazet 	seq = TCP_SKB_CB(skb)->seq;
4420e86b2919SEric Dumazet 	end_seq = TCP_SKB_CB(skb)->end_seq;
4421e86b2919SEric Dumazet 
4422e86b2919SEric Dumazet 	if (seq == TCP_SKB_CB(skb1)->end_seq) {
4423329033f6SEric Dumazet 		bool fragstolen;
4424329033f6SEric Dumazet 
4425329033f6SEric Dumazet 		if (!tcp_try_coalesce(sk, skb1, skb, &fragstolen)) {
44261402d366SEric Dumazet 			__skb_queue_after(&tp->out_of_order_queue, skb1, skb);
44271402d366SEric Dumazet 		} else {
44284e4f1fc2SEric Dumazet 			tcp_grow_window(sk, skb);
4429923dd347SEric Dumazet 			kfree_skb_partial(skb, fragstolen);
4430c8628155SEric Dumazet 			skb = NULL;
4431c8628155SEric Dumazet 		}
4432e86b2919SEric Dumazet 
4433e86b2919SEric Dumazet 		if (!tp->rx_opt.num_sacks ||
4434e86b2919SEric Dumazet 		    tp->selective_acks[0].end_seq != seq)
4435e86b2919SEric Dumazet 			goto add_sack;
4436e86b2919SEric Dumazet 
4437e86b2919SEric Dumazet 		/* Common case: data arrive in order after hole. */
4438e86b2919SEric Dumazet 		tp->selective_acks[0].end_seq = end_seq;
4439e86b2919SEric Dumazet 		goto end;
4440e86b2919SEric Dumazet 	}
4441e86b2919SEric Dumazet 
4442e86b2919SEric Dumazet 	/* Find place to insert this segment. */
4443e86b2919SEric Dumazet 	while (1) {
4444e86b2919SEric Dumazet 		if (!after(TCP_SKB_CB(skb1)->seq, seq))
4445e86b2919SEric Dumazet 			break;
4446e86b2919SEric Dumazet 		if (skb_queue_is_first(&tp->out_of_order_queue, skb1)) {
4447e86b2919SEric Dumazet 			skb1 = NULL;
4448e86b2919SEric Dumazet 			break;
4449e86b2919SEric Dumazet 		}
4450e86b2919SEric Dumazet 		skb1 = skb_queue_prev(&tp->out_of_order_queue, skb1);
4451e86b2919SEric Dumazet 	}
4452e86b2919SEric Dumazet 
4453e86b2919SEric Dumazet 	/* Do skb overlap to previous one? */
4454e86b2919SEric Dumazet 	if (skb1 && before(seq, TCP_SKB_CB(skb1)->end_seq)) {
4455e86b2919SEric Dumazet 		if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
4456e86b2919SEric Dumazet 			/* All the bits are present. Drop. */
4457a6df1ae9SEric Dumazet 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
4458e86b2919SEric Dumazet 			__kfree_skb(skb);
4459e86b2919SEric Dumazet 			skb = NULL;
4460e86b2919SEric Dumazet 			tcp_dsack_set(sk, seq, end_seq);
4461e86b2919SEric Dumazet 			goto add_sack;
4462e86b2919SEric Dumazet 		}
4463e86b2919SEric Dumazet 		if (after(seq, TCP_SKB_CB(skb1)->seq)) {
4464e86b2919SEric Dumazet 			/* Partial overlap. */
4465e86b2919SEric Dumazet 			tcp_dsack_set(sk, seq,
4466e86b2919SEric Dumazet 				      TCP_SKB_CB(skb1)->end_seq);
4467e86b2919SEric Dumazet 		} else {
4468e86b2919SEric Dumazet 			if (skb_queue_is_first(&tp->out_of_order_queue,
4469e86b2919SEric Dumazet 					       skb1))
4470e86b2919SEric Dumazet 				skb1 = NULL;
4471e86b2919SEric Dumazet 			else
4472e86b2919SEric Dumazet 				skb1 = skb_queue_prev(
4473e86b2919SEric Dumazet 					&tp->out_of_order_queue,
4474e86b2919SEric Dumazet 					skb1);
4475e86b2919SEric Dumazet 		}
4476e86b2919SEric Dumazet 	}
4477e86b2919SEric Dumazet 	if (!skb1)
4478e86b2919SEric Dumazet 		__skb_queue_head(&tp->out_of_order_queue, skb);
4479e86b2919SEric Dumazet 	else
4480e86b2919SEric Dumazet 		__skb_queue_after(&tp->out_of_order_queue, skb1, skb);
4481e86b2919SEric Dumazet 
4482e86b2919SEric Dumazet 	/* And clean segments covered by new one as whole. */
4483e86b2919SEric Dumazet 	while (!skb_queue_is_last(&tp->out_of_order_queue, skb)) {
4484e86b2919SEric Dumazet 		skb1 = skb_queue_next(&tp->out_of_order_queue, skb);
4485e86b2919SEric Dumazet 
4486e86b2919SEric Dumazet 		if (!after(end_seq, TCP_SKB_CB(skb1)->seq))
4487e86b2919SEric Dumazet 			break;
4488e86b2919SEric Dumazet 		if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
4489e86b2919SEric Dumazet 			tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
4490e86b2919SEric Dumazet 					 end_seq);
4491e86b2919SEric Dumazet 			break;
4492e86b2919SEric Dumazet 		}
4493e86b2919SEric Dumazet 		__skb_unlink(skb1, &tp->out_of_order_queue);
4494e86b2919SEric Dumazet 		tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
4495e86b2919SEric Dumazet 				 TCP_SKB_CB(skb1)->end_seq);
4496a6df1ae9SEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
4497e86b2919SEric Dumazet 		__kfree_skb(skb1);
4498e86b2919SEric Dumazet 	}
4499e86b2919SEric Dumazet 
4500e86b2919SEric Dumazet add_sack:
4501e86b2919SEric Dumazet 	if (tcp_is_sack(tp))
4502e86b2919SEric Dumazet 		tcp_sack_new_ofo_skb(sk, seq, end_seq);
4503e86b2919SEric Dumazet end:
45044e4f1fc2SEric Dumazet 	if (skb) {
45054e4f1fc2SEric Dumazet 		tcp_grow_window(sk, skb);
4506e86b2919SEric Dumazet 		skb_set_owner_r(skb, sk);
4507e86b2919SEric Dumazet 	}
45084e4f1fc2SEric Dumazet }
4509e86b2919SEric Dumazet 
4510292e8d8cSPavel Emelyanov static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int hdrlen,
4511b081f85cSEric Dumazet 		  bool *fragstolen)
4512b081f85cSEric Dumazet {
4513b081f85cSEric Dumazet 	int eaten;
4514b081f85cSEric Dumazet 	struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue);
4515b081f85cSEric Dumazet 
4516b081f85cSEric Dumazet 	__skb_pull(skb, hdrlen);
4517b081f85cSEric Dumazet 	eaten = (tail &&
4518b081f85cSEric Dumazet 		 tcp_try_coalesce(sk, tail, skb, fragstolen)) ? 1 : 0;
4519bdd1f9edSEric Dumazet 	tcp_rcv_nxt_update(tcp_sk(sk), TCP_SKB_CB(skb)->end_seq);
4520b081f85cSEric Dumazet 	if (!eaten) {
4521b081f85cSEric Dumazet 		__skb_queue_tail(&sk->sk_receive_queue, skb);
4522b081f85cSEric Dumazet 		skb_set_owner_r(skb, sk);
4523b081f85cSEric Dumazet 	}
4524b081f85cSEric Dumazet 	return eaten;
4525b081f85cSEric Dumazet }
4526e86b2919SEric Dumazet 
4527292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
4528292e8d8cSPavel Emelyanov {
4529cb93471aSEric Dumazet 	struct sk_buff *skb;
45305d4c9bfbSEric Dumazet 	int err = -ENOMEM;
45315d4c9bfbSEric Dumazet 	int data_len = 0;
4532292e8d8cSPavel Emelyanov 	bool fragstolen;
4533292e8d8cSPavel Emelyanov 
4534c454e611SPavel Emelyanov 	if (size == 0)
4535c454e611SPavel Emelyanov 		return 0;
4536c454e611SPavel Emelyanov 
45375d4c9bfbSEric Dumazet 	if (size > PAGE_SIZE) {
45385d4c9bfbSEric Dumazet 		int npages = min_t(size_t, size >> PAGE_SHIFT, MAX_SKB_FRAGS);
45395d4c9bfbSEric Dumazet 
45405d4c9bfbSEric Dumazet 		data_len = npages << PAGE_SHIFT;
45415d4c9bfbSEric Dumazet 		size = data_len + (size & ~PAGE_MASK);
45425d4c9bfbSEric Dumazet 	}
45435d4c9bfbSEric Dumazet 	skb = alloc_skb_with_frags(size - data_len, data_len,
45445d4c9bfbSEric Dumazet 				   PAGE_ALLOC_COSTLY_ORDER,
45455d4c9bfbSEric Dumazet 				   &err, sk->sk_allocation);
4546292e8d8cSPavel Emelyanov 	if (!skb)
4547292e8d8cSPavel Emelyanov 		goto err;
4548292e8d8cSPavel Emelyanov 
45495d4c9bfbSEric Dumazet 	skb_put(skb, size - data_len);
45505d4c9bfbSEric Dumazet 	skb->data_len = data_len;
45515d4c9bfbSEric Dumazet 	skb->len = size;
45525d4c9bfbSEric Dumazet 
4553cb93471aSEric Dumazet 	if (tcp_try_rmem_schedule(sk, skb, skb->truesize))
4554c76562b6SMel Gorman 		goto err_free;
4555c76562b6SMel Gorman 
45565d4c9bfbSEric Dumazet 	err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
45575d4c9bfbSEric Dumazet 	if (err)
4558292e8d8cSPavel Emelyanov 		goto err_free;
4559292e8d8cSPavel Emelyanov 
4560292e8d8cSPavel Emelyanov 	TCP_SKB_CB(skb)->seq = tcp_sk(sk)->rcv_nxt;
4561292e8d8cSPavel Emelyanov 	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size;
4562292e8d8cSPavel Emelyanov 	TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1;
4563292e8d8cSPavel Emelyanov 
4564cb93471aSEric Dumazet 	if (tcp_queue_rcv(sk, skb, 0, &fragstolen)) {
4565292e8d8cSPavel Emelyanov 		WARN_ON_ONCE(fragstolen); /* should not happen */
4566292e8d8cSPavel Emelyanov 		__kfree_skb(skb);
4567292e8d8cSPavel Emelyanov 	}
4568292e8d8cSPavel Emelyanov 	return size;
4569292e8d8cSPavel Emelyanov 
4570292e8d8cSPavel Emelyanov err_free:
4571292e8d8cSPavel Emelyanov 	kfree_skb(skb);
4572292e8d8cSPavel Emelyanov err:
45735d4c9bfbSEric Dumazet 	return err;
45745d4c9bfbSEric Dumazet 
4575292e8d8cSPavel Emelyanov }
4576292e8d8cSPavel Emelyanov 
45771da177e4SLinus Torvalds static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
45781da177e4SLinus Torvalds {
45791da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
45801da177e4SLinus Torvalds 	int eaten = -1;
4581329033f6SEric Dumazet 	bool fragstolen = false;
45821da177e4SLinus Torvalds 
45831da177e4SLinus Torvalds 	if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq)
45841da177e4SLinus Torvalds 		goto drop;
45851da177e4SLinus Torvalds 
4586f84af32cSEric Dumazet 	skb_dst_drop(skb);
4587155c6e1aSPeter Pan(潘卫平) 	__skb_pull(skb, tcp_hdr(skb)->doff * 4);
45881da177e4SLinus Torvalds 
4589735d3831SFlorian Westphal 	tcp_ecn_accept_cwr(tp, skb);
45901da177e4SLinus Torvalds 
45911da177e4SLinus Torvalds 	tp->rx_opt.dsack = 0;
45921da177e4SLinus Torvalds 
45931da177e4SLinus Torvalds 	/*  Queue data for delivery to the user.
45941da177e4SLinus Torvalds 	 *  Packets in sequence go to the receive queue.
45951da177e4SLinus Torvalds 	 *  Out of sequence packets to the out_of_order_queue.
45961da177e4SLinus Torvalds 	 */
45971da177e4SLinus Torvalds 	if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
45981da177e4SLinus Torvalds 		if (tcp_receive_window(tp) == 0)
45991da177e4SLinus Torvalds 			goto out_of_window;
46001da177e4SLinus Torvalds 
46011da177e4SLinus Torvalds 		/* Ok. In sequence. In window. */
46021da177e4SLinus Torvalds 		if (tp->ucopy.task == current &&
46031da177e4SLinus Torvalds 		    tp->copied_seq == tp->rcv_nxt && tp->ucopy.len &&
46041da177e4SLinus Torvalds 		    sock_owned_by_user(sk) && !tp->urg_data) {
46051da177e4SLinus Torvalds 			int chunk = min_t(unsigned int, skb->len,
46061da177e4SLinus Torvalds 					  tp->ucopy.len);
46071da177e4SLinus Torvalds 
46081da177e4SLinus Torvalds 			__set_current_state(TASK_RUNNING);
46091da177e4SLinus Torvalds 
46101da177e4SLinus Torvalds 			local_bh_enable();
4611f4362a2cSAl Viro 			if (!skb_copy_datagram_msg(skb, 0, tp->ucopy.msg, chunk)) {
46121da177e4SLinus Torvalds 				tp->ucopy.len -= chunk;
46131da177e4SLinus Torvalds 				tp->copied_seq += chunk;
461444f5324bSJerry Chu 				eaten = (chunk == skb->len);
46151da177e4SLinus Torvalds 				tcp_rcv_space_adjust(sk);
46161da177e4SLinus Torvalds 			}
46171da177e4SLinus Torvalds 			local_bh_disable();
46181da177e4SLinus Torvalds 		}
46191da177e4SLinus Torvalds 
46201da177e4SLinus Torvalds 		if (eaten <= 0) {
46211da177e4SLinus Torvalds queue_and_out:
462276dfa608SEric Dumazet 			if (eaten < 0) {
462376dfa608SEric Dumazet 				if (skb_queue_len(&sk->sk_receive_queue) == 0)
462476dfa608SEric Dumazet 					sk_forced_mem_schedule(sk, skb->truesize);
462576dfa608SEric Dumazet 				else if (tcp_try_rmem_schedule(sk, skb, skb->truesize))
46261da177e4SLinus Torvalds 					goto drop;
462776dfa608SEric Dumazet 			}
4628b081f85cSEric Dumazet 			eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen);
46291402d366SEric Dumazet 		}
4630bdd1f9edSEric Dumazet 		tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
46311da177e4SLinus Torvalds 		if (skb->len)
46329e412ba7SIlpo Järvinen 			tcp_event_data_recv(sk, skb);
4633155c6e1aSPeter Pan(潘卫平) 		if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
463420c4cb79SEric Dumazet 			tcp_fin(sk);
46351da177e4SLinus Torvalds 
4636b03efcfbSDavid S. Miller 		if (!skb_queue_empty(&tp->out_of_order_queue)) {
46371da177e4SLinus Torvalds 			tcp_ofo_queue(sk);
46381da177e4SLinus Torvalds 
46391da177e4SLinus Torvalds 			/* RFC2581. 4.2. SHOULD send immediate ACK, when
46401da177e4SLinus Torvalds 			 * gap in queue is filled.
46411da177e4SLinus Torvalds 			 */
4642b03efcfbSDavid S. Miller 			if (skb_queue_empty(&tp->out_of_order_queue))
4643463c84b9SArnaldo Carvalho de Melo 				inet_csk(sk)->icsk_ack.pingpong = 0;
46441da177e4SLinus Torvalds 		}
46451da177e4SLinus Torvalds 
46461da177e4SLinus Torvalds 		if (tp->rx_opt.num_sacks)
46471da177e4SLinus Torvalds 			tcp_sack_remove(tp);
46481da177e4SLinus Torvalds 
46499e412ba7SIlpo Järvinen 		tcp_fast_path_check(sk);
46501da177e4SLinus Torvalds 
4651923dd347SEric Dumazet 		if (eaten > 0)
4652923dd347SEric Dumazet 			kfree_skb_partial(skb, fragstolen);
46531d57f195SEric Dumazet 		if (!sock_flag(sk, SOCK_DEAD))
4654676d2369SDavid S. Miller 			sk->sk_data_ready(sk);
46551da177e4SLinus Torvalds 		return;
46561da177e4SLinus Torvalds 	}
46571da177e4SLinus Torvalds 
46581da177e4SLinus Torvalds 	if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
46591da177e4SLinus Torvalds 		/* A retransmit, 2nd most common case.  Force an immediate ack. */
4660de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
46611ed83465SPavel Emelyanov 		tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
46621da177e4SLinus Torvalds 
46631da177e4SLinus Torvalds out_of_window:
4664463c84b9SArnaldo Carvalho de Melo 		tcp_enter_quickack_mode(sk);
4665463c84b9SArnaldo Carvalho de Melo 		inet_csk_schedule_ack(sk);
46661da177e4SLinus Torvalds drop:
46671da177e4SLinus Torvalds 		__kfree_skb(skb);
46681da177e4SLinus Torvalds 		return;
46691da177e4SLinus Torvalds 	}
46701da177e4SLinus Torvalds 
46711da177e4SLinus Torvalds 	/* Out of window. F.e. zero window probe. */
46721da177e4SLinus Torvalds 	if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp)))
46731da177e4SLinus Torvalds 		goto out_of_window;
46741da177e4SLinus Torvalds 
4675463c84b9SArnaldo Carvalho de Melo 	tcp_enter_quickack_mode(sk);
46761da177e4SLinus Torvalds 
46771da177e4SLinus Torvalds 	if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
46781da177e4SLinus Torvalds 		/* Partial packet, seq < rcv_next < end_seq */
46791da177e4SLinus Torvalds 		SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n",
46801da177e4SLinus Torvalds 			   tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
46811da177e4SLinus Torvalds 			   TCP_SKB_CB(skb)->end_seq);
46821da177e4SLinus Torvalds 
46831ed83465SPavel Emelyanov 		tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
46841da177e4SLinus Torvalds 
46851da177e4SLinus Torvalds 		/* If window is closed, drop tail of packet. But after
46861da177e4SLinus Torvalds 		 * remembering D-SACK for its head made in previous line.
46871da177e4SLinus Torvalds 		 */
46881da177e4SLinus Torvalds 		if (!tcp_receive_window(tp))
46891da177e4SLinus Torvalds 			goto out_of_window;
46901da177e4SLinus Torvalds 		goto queue_and_out;
46911da177e4SLinus Torvalds 	}
46921da177e4SLinus Torvalds 
4693e86b2919SEric Dumazet 	tcp_data_queue_ofo(sk, skb);
46941da177e4SLinus Torvalds }
46951da177e4SLinus Torvalds 
46962cf46637SIlpo Järvinen static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
46972cf46637SIlpo Järvinen 					struct sk_buff_head *list)
46982cf46637SIlpo Järvinen {
469991521944SDavid S. Miller 	struct sk_buff *next = NULL;
470091521944SDavid S. Miller 
470191521944SDavid S. Miller 	if (!skb_queue_is_last(list, skb))
470291521944SDavid S. Miller 		next = skb_queue_next(list, skb);
47032cf46637SIlpo Järvinen 
47042cf46637SIlpo Järvinen 	__skb_unlink(skb, list);
47052cf46637SIlpo Järvinen 	__kfree_skb(skb);
47062cf46637SIlpo Järvinen 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRCVCOLLAPSED);
47072cf46637SIlpo Järvinen 
47082cf46637SIlpo Järvinen 	return next;
47092cf46637SIlpo Järvinen }
47102cf46637SIlpo Järvinen 
47111da177e4SLinus Torvalds /* Collapse contiguous sequence of skbs head..tail with
47121da177e4SLinus Torvalds  * sequence numbers start..end.
471391521944SDavid S. Miller  *
471491521944SDavid S. Miller  * If tail is NULL, this means until the end of the list.
471591521944SDavid S. Miller  *
47161da177e4SLinus Torvalds  * Segments with FIN/SYN are not collapsed (only because this
47171da177e4SLinus Torvalds  * simplifies code)
47181da177e4SLinus Torvalds  */
47191da177e4SLinus Torvalds static void
47208728b834SDavid S. Miller tcp_collapse(struct sock *sk, struct sk_buff_head *list,
47218728b834SDavid S. Miller 	     struct sk_buff *head, struct sk_buff *tail,
47228728b834SDavid S. Miller 	     u32 start, u32 end)
47231da177e4SLinus Torvalds {
472491521944SDavid S. Miller 	struct sk_buff *skb, *n;
472591521944SDavid S. Miller 	bool end_of_skbs;
47261da177e4SLinus Torvalds 
4727caa20d9aSStephen Hemminger 	/* First, check that queue is collapsible and find
47281da177e4SLinus Torvalds 	 * the point where collapsing can be useful. */
472991521944SDavid S. Miller 	skb = head;
473091521944SDavid S. Miller restart:
473191521944SDavid S. Miller 	end_of_skbs = true;
473291521944SDavid S. Miller 	skb_queue_walk_from_safe(list, skb, n) {
473391521944SDavid S. Miller 		if (skb == tail)
473491521944SDavid S. Miller 			break;
47351da177e4SLinus Torvalds 		/* No new bits? It is possible on ofo queue. */
47361da177e4SLinus Torvalds 		if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
47372cf46637SIlpo Järvinen 			skb = tcp_collapse_one(sk, skb, list);
473891521944SDavid S. Miller 			if (!skb)
473991521944SDavid S. Miller 				break;
474091521944SDavid S. Miller 			goto restart;
47411da177e4SLinus Torvalds 		}
47421da177e4SLinus Torvalds 
47431da177e4SLinus Torvalds 		/* The first skb to collapse is:
47441da177e4SLinus Torvalds 		 * - not SYN/FIN and
47451da177e4SLinus Torvalds 		 * - bloated or contains data before "start" or
47461da177e4SLinus Torvalds 		 *   overlaps to the next one.
47471da177e4SLinus Torvalds 		 */
4748e11ecddfSEric Dumazet 		if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) &&
47491da177e4SLinus Torvalds 		    (tcp_win_from_space(skb->truesize) > skb->len ||
475091521944SDavid S. Miller 		     before(TCP_SKB_CB(skb)->seq, start))) {
475191521944SDavid S. Miller 			end_of_skbs = false;
47521da177e4SLinus Torvalds 			break;
475391521944SDavid S. Miller 		}
475491521944SDavid S. Miller 
475591521944SDavid S. Miller 		if (!skb_queue_is_last(list, skb)) {
475691521944SDavid S. Miller 			struct sk_buff *next = skb_queue_next(list, skb);
475791521944SDavid S. Miller 			if (next != tail &&
475891521944SDavid S. Miller 			    TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(next)->seq) {
475991521944SDavid S. Miller 				end_of_skbs = false;
476091521944SDavid S. Miller 				break;
476191521944SDavid S. Miller 			}
476291521944SDavid S. Miller 		}
47631da177e4SLinus Torvalds 
47641da177e4SLinus Torvalds 		/* Decided to skip this, advance start seq. */
47651da177e4SLinus Torvalds 		start = TCP_SKB_CB(skb)->end_seq;
47661da177e4SLinus Torvalds 	}
4767e11ecddfSEric Dumazet 	if (end_of_skbs ||
4768e11ecddfSEric Dumazet 	    (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
47691da177e4SLinus Torvalds 		return;
47701da177e4SLinus Torvalds 
47711da177e4SLinus Torvalds 	while (before(start, end)) {
4772b3d6cb92SEric Dumazet 		int copy = min_t(int, SKB_MAX_ORDER(0, 0), end - start);
47731da177e4SLinus Torvalds 		struct sk_buff *nskb;
47741da177e4SLinus Torvalds 
4775b3d6cb92SEric Dumazet 		nskb = alloc_skb(copy, GFP_ATOMIC);
47761da177e4SLinus Torvalds 		if (!nskb)
47771da177e4SLinus Torvalds 			return;
4778c51957daSArnaldo Carvalho de Melo 
47791da177e4SLinus Torvalds 		memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
47801da177e4SLinus Torvalds 		TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
478143f59c89SDavid S. Miller 		__skb_queue_before(list, skb, nskb);
47823ab224beSHideo Aoki 		skb_set_owner_r(nskb, sk);
47831da177e4SLinus Torvalds 
47841da177e4SLinus Torvalds 		/* Copy data, releasing collapsed skbs. */
47851da177e4SLinus Torvalds 		while (copy > 0) {
47861da177e4SLinus Torvalds 			int offset = start - TCP_SKB_CB(skb)->seq;
47871da177e4SLinus Torvalds 			int size = TCP_SKB_CB(skb)->end_seq - start;
47881da177e4SLinus Torvalds 
478909a62660SKris Katterjohn 			BUG_ON(offset < 0);
47901da177e4SLinus Torvalds 			if (size > 0) {
47911da177e4SLinus Torvalds 				size = min(copy, size);
47921da177e4SLinus Torvalds 				if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
47931da177e4SLinus Torvalds 					BUG();
47941da177e4SLinus Torvalds 				TCP_SKB_CB(nskb)->end_seq += size;
47951da177e4SLinus Torvalds 				copy -= size;
47961da177e4SLinus Torvalds 				start += size;
47971da177e4SLinus Torvalds 			}
47981da177e4SLinus Torvalds 			if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
47992cf46637SIlpo Järvinen 				skb = tcp_collapse_one(sk, skb, list);
480091521944SDavid S. Miller 				if (!skb ||
480191521944SDavid S. Miller 				    skb == tail ||
4802e11ecddfSEric Dumazet 				    (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)))
48031da177e4SLinus Torvalds 					return;
48041da177e4SLinus Torvalds 			}
48051da177e4SLinus Torvalds 		}
48061da177e4SLinus Torvalds 	}
48071da177e4SLinus Torvalds }
48081da177e4SLinus Torvalds 
48091da177e4SLinus Torvalds /* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
48101da177e4SLinus Torvalds  * and tcp_collapse() them until all the queue is collapsed.
48111da177e4SLinus Torvalds  */
48121da177e4SLinus Torvalds static void tcp_collapse_ofo_queue(struct sock *sk)
48131da177e4SLinus Torvalds {
48141da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
48151da177e4SLinus Torvalds 	struct sk_buff *skb = skb_peek(&tp->out_of_order_queue);
48161da177e4SLinus Torvalds 	struct sk_buff *head;
48171da177e4SLinus Torvalds 	u32 start, end;
48181da177e4SLinus Torvalds 
481951456b29SIan Morris 	if (!skb)
48201da177e4SLinus Torvalds 		return;
48211da177e4SLinus Torvalds 
48221da177e4SLinus Torvalds 	start = TCP_SKB_CB(skb)->seq;
48231da177e4SLinus Torvalds 	end = TCP_SKB_CB(skb)->end_seq;
48241da177e4SLinus Torvalds 	head = skb;
48251da177e4SLinus Torvalds 
48261da177e4SLinus Torvalds 	for (;;) {
482791521944SDavid S. Miller 		struct sk_buff *next = NULL;
482891521944SDavid S. Miller 
482991521944SDavid S. Miller 		if (!skb_queue_is_last(&tp->out_of_order_queue, skb))
483091521944SDavid S. Miller 			next = skb_queue_next(&tp->out_of_order_queue, skb);
483191521944SDavid S. Miller 		skb = next;
48321da177e4SLinus Torvalds 
48331da177e4SLinus Torvalds 		/* Segment is terminated when we see gap or when
48341da177e4SLinus Torvalds 		 * we are at the end of all the queue. */
483591521944SDavid S. Miller 		if (!skb ||
48361da177e4SLinus Torvalds 		    after(TCP_SKB_CB(skb)->seq, end) ||
48371da177e4SLinus Torvalds 		    before(TCP_SKB_CB(skb)->end_seq, start)) {
48388728b834SDavid S. Miller 			tcp_collapse(sk, &tp->out_of_order_queue,
48398728b834SDavid S. Miller 				     head, skb, start, end);
48401da177e4SLinus Torvalds 			head = skb;
484191521944SDavid S. Miller 			if (!skb)
48421da177e4SLinus Torvalds 				break;
48431da177e4SLinus Torvalds 			/* Start new segment */
48441da177e4SLinus Torvalds 			start = TCP_SKB_CB(skb)->seq;
48451da177e4SLinus Torvalds 			end = TCP_SKB_CB(skb)->end_seq;
48461da177e4SLinus Torvalds 		} else {
48471da177e4SLinus Torvalds 			if (before(TCP_SKB_CB(skb)->seq, start))
48481da177e4SLinus Torvalds 				start = TCP_SKB_CB(skb)->seq;
48491da177e4SLinus Torvalds 			if (after(TCP_SKB_CB(skb)->end_seq, end))
48501da177e4SLinus Torvalds 				end = TCP_SKB_CB(skb)->end_seq;
48511da177e4SLinus Torvalds 		}
48521da177e4SLinus Torvalds 	}
48531da177e4SLinus Torvalds }
48541da177e4SLinus Torvalds 
4855b000cd37SVitaliy Gusev /*
4856b000cd37SVitaliy Gusev  * Purge the out-of-order queue.
485756f367bbSVitaliy Gusev  * Return true if queue was pruned.
4858b000cd37SVitaliy Gusev  */
4859a2a385d6SEric Dumazet static bool tcp_prune_ofo_queue(struct sock *sk)
4860b000cd37SVitaliy Gusev {
4861b000cd37SVitaliy Gusev 	struct tcp_sock *tp = tcp_sk(sk);
4862a2a385d6SEric Dumazet 	bool res = false;
4863b000cd37SVitaliy Gusev 
4864b000cd37SVitaliy Gusev 	if (!skb_queue_empty(&tp->out_of_order_queue)) {
4865de0744afSPavel Emelyanov 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_OFOPRUNED);
4866b000cd37SVitaliy Gusev 		__skb_queue_purge(&tp->out_of_order_queue);
4867b000cd37SVitaliy Gusev 
4868b000cd37SVitaliy Gusev 		/* Reset SACK state.  A conforming SACK implementation will
4869b000cd37SVitaliy Gusev 		 * do the same at a timeout based retransmit.  When a connection
4870b000cd37SVitaliy Gusev 		 * is in a sad state like this, we care only about integrity
4871b000cd37SVitaliy Gusev 		 * of the connection not performance.
4872b000cd37SVitaliy Gusev 		 */
4873b000cd37SVitaliy Gusev 		if (tp->rx_opt.sack_ok)
4874b000cd37SVitaliy Gusev 			tcp_sack_reset(&tp->rx_opt);
4875b000cd37SVitaliy Gusev 		sk_mem_reclaim(sk);
4876a2a385d6SEric Dumazet 		res = true;
4877b000cd37SVitaliy Gusev 	}
487856f367bbSVitaliy Gusev 	return res;
4879b000cd37SVitaliy Gusev }
4880b000cd37SVitaliy Gusev 
48811da177e4SLinus Torvalds /* Reduce allocated memory if we can, trying to get
48821da177e4SLinus Torvalds  * the socket within its memory limits again.
48831da177e4SLinus Torvalds  *
48841da177e4SLinus Torvalds  * Return less than zero if we should start dropping frames
48851da177e4SLinus Torvalds  * until the socket owning process reads some of the data
48861da177e4SLinus Torvalds  * to stabilize the situation.
48871da177e4SLinus Torvalds  */
48881da177e4SLinus Torvalds static int tcp_prune_queue(struct sock *sk)
48891da177e4SLinus Torvalds {
48901da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
48911da177e4SLinus Torvalds 
48921da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq);
48931da177e4SLinus Torvalds 
4894de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PRUNECALLED);
48951da177e4SLinus Torvalds 
48961da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
48979e412ba7SIlpo Järvinen 		tcp_clamp_window(sk);
4898b8da51ebSEric Dumazet 	else if (tcp_under_memory_pressure(sk))
48991da177e4SLinus Torvalds 		tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
49001da177e4SLinus Torvalds 
49011da177e4SLinus Torvalds 	tcp_collapse_ofo_queue(sk);
490291521944SDavid S. Miller 	if (!skb_queue_empty(&sk->sk_receive_queue))
49038728b834SDavid S. Miller 		tcp_collapse(sk, &sk->sk_receive_queue,
490491521944SDavid S. Miller 			     skb_peek(&sk->sk_receive_queue),
490591521944SDavid S. Miller 			     NULL,
49061da177e4SLinus Torvalds 			     tp->copied_seq, tp->rcv_nxt);
49073ab224beSHideo Aoki 	sk_mem_reclaim(sk);
49081da177e4SLinus Torvalds 
49091da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
49101da177e4SLinus Torvalds 		return 0;
49111da177e4SLinus Torvalds 
49121da177e4SLinus Torvalds 	/* Collapsing did not help, destructive actions follow.
49131da177e4SLinus Torvalds 	 * This must not ever occur. */
49141da177e4SLinus Torvalds 
4915b000cd37SVitaliy Gusev 	tcp_prune_ofo_queue(sk);
49161da177e4SLinus Torvalds 
49171da177e4SLinus Torvalds 	if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
49181da177e4SLinus Torvalds 		return 0;
49191da177e4SLinus Torvalds 
49201da177e4SLinus Torvalds 	/* If we are really being abused, tell the caller to silently
49211da177e4SLinus Torvalds 	 * drop receive data on the floor.  It will get retransmitted
49221da177e4SLinus Torvalds 	 * and hopefully then we'll have sufficient space.
49231da177e4SLinus Torvalds 	 */
4924de0744afSPavel Emelyanov 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_RCVPRUNED);
49251da177e4SLinus Torvalds 
49261da177e4SLinus Torvalds 	/* Massive buffer overcommit. */
49271da177e4SLinus Torvalds 	tp->pred_flags = 0;
49281da177e4SLinus Torvalds 	return -1;
49291da177e4SLinus Torvalds }
49301da177e4SLinus Torvalds 
4931a2a385d6SEric Dumazet static bool tcp_should_expand_sndbuf(const struct sock *sk)
49320d9901dfSDavid S. Miller {
4933cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
49349e412ba7SIlpo Järvinen 
49350d9901dfSDavid S. Miller 	/* If the user specified a specific send buffer setting, do
49360d9901dfSDavid S. Miller 	 * not modify it.
49370d9901dfSDavid S. Miller 	 */
49380d9901dfSDavid S. Miller 	if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
4939a2a385d6SEric Dumazet 		return false;
49400d9901dfSDavid S. Miller 
49410d9901dfSDavid S. Miller 	/* If we are under global TCP memory pressure, do not expand.  */
4942b8da51ebSEric Dumazet 	if (tcp_under_memory_pressure(sk))
4943a2a385d6SEric Dumazet 		return false;
49440d9901dfSDavid S. Miller 
49450d9901dfSDavid S. Miller 	/* If we are under soft global TCP memory pressure, do not expand.  */
4946180d8cd9SGlauber Costa 	if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0))
4947a2a385d6SEric Dumazet 		return false;
49480d9901dfSDavid S. Miller 
49490d9901dfSDavid S. Miller 	/* If we filled the congestion window, do not expand.  */
49506514890fSNeal Cardwell 	if (tcp_packets_in_flight(tp) >= tp->snd_cwnd)
4951a2a385d6SEric Dumazet 		return false;
49520d9901dfSDavid S. Miller 
4953a2a385d6SEric Dumazet 	return true;
49540d9901dfSDavid S. Miller }
49551da177e4SLinus Torvalds 
49561da177e4SLinus Torvalds /* When incoming ACK allowed to free some skb from write_queue,
49571da177e4SLinus Torvalds  * we remember this event in flag SOCK_QUEUE_SHRUNK and wake up socket
49581da177e4SLinus Torvalds  * on the exit from tcp input handler.
49591da177e4SLinus Torvalds  *
49601da177e4SLinus Torvalds  * PROBLEM: sndbuf expansion does not work well with largesend.
49611da177e4SLinus Torvalds  */
49621da177e4SLinus Torvalds static void tcp_new_space(struct sock *sk)
49631da177e4SLinus Torvalds {
49641da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
49651da177e4SLinus Torvalds 
49669e412ba7SIlpo Järvinen 	if (tcp_should_expand_sndbuf(sk)) {
49676ae70532SEric Dumazet 		tcp_sndbuf_expand(sk);
49681da177e4SLinus Torvalds 		tp->snd_cwnd_stamp = tcp_time_stamp;
49691da177e4SLinus Torvalds 	}
49701da177e4SLinus Torvalds 
49711da177e4SLinus Torvalds 	sk->sk_write_space(sk);
49721da177e4SLinus Torvalds }
49731da177e4SLinus Torvalds 
497440efc6faSStephen Hemminger static void tcp_check_space(struct sock *sk)
49751da177e4SLinus Torvalds {
49761da177e4SLinus Torvalds 	if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) {
49771da177e4SLinus Torvalds 		sock_reset_flag(sk, SOCK_QUEUE_SHRUNK);
49783c715127Sjbaron@akamai.com 		/* pairs with tcp_poll() */
49793c715127Sjbaron@akamai.com 		smp_mb__after_atomic();
49801da177e4SLinus Torvalds 		if (sk->sk_socket &&
49811da177e4SLinus Torvalds 		    test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
49821da177e4SLinus Torvalds 			tcp_new_space(sk);
49831da177e4SLinus Torvalds 	}
49841da177e4SLinus Torvalds }
49851da177e4SLinus Torvalds 
49869e412ba7SIlpo Järvinen static inline void tcp_data_snd_check(struct sock *sk)
49871da177e4SLinus Torvalds {
49889e412ba7SIlpo Järvinen 	tcp_push_pending_frames(sk);
49891da177e4SLinus Torvalds 	tcp_check_space(sk);
49901da177e4SLinus Torvalds }
49911da177e4SLinus Torvalds 
49921da177e4SLinus Torvalds /*
49931da177e4SLinus Torvalds  * Check if sending an ack is needed.
49941da177e4SLinus Torvalds  */
49951da177e4SLinus Torvalds static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
49961da177e4SLinus Torvalds {
49971da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
49981da177e4SLinus Torvalds 
49991da177e4SLinus Torvalds 	    /* More than one full frame received... */
50009d4fb27dSJoe Perches 	if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
50011da177e4SLinus Torvalds 	     /* ... and right edge of window advances far enough.
50021da177e4SLinus Torvalds 	      * (tcp_recvmsg() will send ACK otherwise). Or...
50031da177e4SLinus Torvalds 	      */
50049d4fb27dSJoe Perches 	     __tcp_select_window(sk) >= tp->rcv_wnd) ||
50051da177e4SLinus Torvalds 	    /* We ACK each frame or... */
5006463c84b9SArnaldo Carvalho de Melo 	    tcp_in_quickack_mode(sk) ||
50071da177e4SLinus Torvalds 	    /* We have out of order data. */
5008056834d9SIlpo Järvinen 	    (ofo_possible && skb_peek(&tp->out_of_order_queue))) {
50091da177e4SLinus Torvalds 		/* Then ack it now */
50101da177e4SLinus Torvalds 		tcp_send_ack(sk);
50111da177e4SLinus Torvalds 	} else {
50121da177e4SLinus Torvalds 		/* Else, send delayed ack. */
50131da177e4SLinus Torvalds 		tcp_send_delayed_ack(sk);
50141da177e4SLinus Torvalds 	}
50151da177e4SLinus Torvalds }
50161da177e4SLinus Torvalds 
501740efc6faSStephen Hemminger static inline void tcp_ack_snd_check(struct sock *sk)
50181da177e4SLinus Torvalds {
5019463c84b9SArnaldo Carvalho de Melo 	if (!inet_csk_ack_scheduled(sk)) {
50201da177e4SLinus Torvalds 		/* We sent a data segment already. */
50211da177e4SLinus Torvalds 		return;
50221da177e4SLinus Torvalds 	}
50231da177e4SLinus Torvalds 	__tcp_ack_snd_check(sk, 1);
50241da177e4SLinus Torvalds }
50251da177e4SLinus Torvalds 
50261da177e4SLinus Torvalds /*
50271da177e4SLinus Torvalds  *	This routine is only called when we have urgent data
5028caa20d9aSStephen Hemminger  *	signaled. Its the 'slow' part of tcp_urg. It could be
50291da177e4SLinus Torvalds  *	moved inline now as tcp_urg is only called from one
50301da177e4SLinus Torvalds  *	place. We handle URGent data wrong. We have to - as
50311da177e4SLinus Torvalds  *	BSD still doesn't use the correction from RFC961.
50321da177e4SLinus Torvalds  *	For 1003.1g we should support a new option TCP_STDURG to permit
50331da177e4SLinus Torvalds  *	either form (or just set the sysctl tcp_stdurg).
50341da177e4SLinus Torvalds  */
50351da177e4SLinus Torvalds 
5036cf533ea5SEric Dumazet static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
50371da177e4SLinus Torvalds {
50381da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
50391da177e4SLinus Torvalds 	u32 ptr = ntohs(th->urg_ptr);
50401da177e4SLinus Torvalds 
50411da177e4SLinus Torvalds 	if (ptr && !sysctl_tcp_stdurg)
50421da177e4SLinus Torvalds 		ptr--;
50431da177e4SLinus Torvalds 	ptr += ntohl(th->seq);
50441da177e4SLinus Torvalds 
50451da177e4SLinus Torvalds 	/* Ignore urgent data that we've already seen and read. */
50461da177e4SLinus Torvalds 	if (after(tp->copied_seq, ptr))
50471da177e4SLinus Torvalds 		return;
50481da177e4SLinus Torvalds 
50491da177e4SLinus Torvalds 	/* Do not replay urg ptr.
50501da177e4SLinus Torvalds 	 *
50511da177e4SLinus Torvalds 	 * NOTE: interesting situation not covered by specs.
50521da177e4SLinus Torvalds 	 * Misbehaving sender may send urg ptr, pointing to segment,
50531da177e4SLinus Torvalds 	 * which we already have in ofo queue. We are not able to fetch
50541da177e4SLinus Torvalds 	 * such data and will stay in TCP_URG_NOTYET until will be eaten
50551da177e4SLinus Torvalds 	 * by recvmsg(). Seems, we are not obliged to handle such wicked
50561da177e4SLinus Torvalds 	 * situations. But it is worth to think about possibility of some
50571da177e4SLinus Torvalds 	 * DoSes using some hypothetical application level deadlock.
50581da177e4SLinus Torvalds 	 */
50591da177e4SLinus Torvalds 	if (before(ptr, tp->rcv_nxt))
50601da177e4SLinus Torvalds 		return;
50611da177e4SLinus Torvalds 
50621da177e4SLinus Torvalds 	/* Do we already have a newer (or duplicate) urgent pointer? */
50631da177e4SLinus Torvalds 	if (tp->urg_data && !after(ptr, tp->urg_seq))
50641da177e4SLinus Torvalds 		return;
50651da177e4SLinus Torvalds 
50661da177e4SLinus Torvalds 	/* Tell the world about our new urgent pointer. */
50671da177e4SLinus Torvalds 	sk_send_sigurg(sk);
50681da177e4SLinus Torvalds 
50691da177e4SLinus Torvalds 	/* We may be adding urgent data when the last byte read was
50701da177e4SLinus Torvalds 	 * urgent. To do this requires some care. We cannot just ignore
50711da177e4SLinus Torvalds 	 * tp->copied_seq since we would read the last urgent byte again
50721da177e4SLinus Torvalds 	 * as data, nor can we alter copied_seq until this data arrives
5073caa20d9aSStephen Hemminger 	 * or we break the semantics of SIOCATMARK (and thus sockatmark())
50741da177e4SLinus Torvalds 	 *
50751da177e4SLinus Torvalds 	 * NOTE. Double Dutch. Rendering to plain English: author of comment
50761da177e4SLinus Torvalds 	 * above did something sort of 	send("A", MSG_OOB); send("B", MSG_OOB);
50771da177e4SLinus Torvalds 	 * and expect that both A and B disappear from stream. This is _wrong_.
50781da177e4SLinus Torvalds 	 * Though this happens in BSD with high probability, this is occasional.
50791da177e4SLinus Torvalds 	 * Any application relying on this is buggy. Note also, that fix "works"
50801da177e4SLinus Torvalds 	 * only in this artificial test. Insert some normal data between A and B and we will
50811da177e4SLinus Torvalds 	 * decline of BSD again. Verdict: it is better to remove to trap
50821da177e4SLinus Torvalds 	 * buggy users.
50831da177e4SLinus Torvalds 	 */
50841da177e4SLinus Torvalds 	if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
5085056834d9SIlpo Järvinen 	    !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
50861da177e4SLinus Torvalds 		struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
50871da177e4SLinus Torvalds 		tp->copied_seq++;
50881da177e4SLinus Torvalds 		if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
50898728b834SDavid S. Miller 			__skb_unlink(skb, &sk->sk_receive_queue);
50901da177e4SLinus Torvalds 			__kfree_skb(skb);
50911da177e4SLinus Torvalds 		}
50921da177e4SLinus Torvalds 	}
50931da177e4SLinus Torvalds 
50941da177e4SLinus Torvalds 	tp->urg_data = TCP_URG_NOTYET;
50951da177e4SLinus Torvalds 	tp->urg_seq = ptr;
50961da177e4SLinus Torvalds 
50971da177e4SLinus Torvalds 	/* Disable header prediction. */
50981da177e4SLinus Torvalds 	tp->pred_flags = 0;
50991da177e4SLinus Torvalds }
51001da177e4SLinus Torvalds 
51011da177e4SLinus Torvalds /* This is the 'fast' part of urgent handling. */
5102cf533ea5SEric Dumazet static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th)
51031da177e4SLinus Torvalds {
51041da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
51051da177e4SLinus Torvalds 
51061da177e4SLinus Torvalds 	/* Check if we get a new urgent pointer - normally not. */
51071da177e4SLinus Torvalds 	if (th->urg)
51081da177e4SLinus Torvalds 		tcp_check_urg(sk, th);
51091da177e4SLinus Torvalds 
51101da177e4SLinus Torvalds 	/* Do we wait for any urgent data? - normally not... */
51111da177e4SLinus Torvalds 	if (tp->urg_data == TCP_URG_NOTYET) {
51121da177e4SLinus Torvalds 		u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) -
51131da177e4SLinus Torvalds 			  th->syn;
51141da177e4SLinus Torvalds 
51151da177e4SLinus Torvalds 		/* Is the urgent pointer pointing into this packet? */
51161da177e4SLinus Torvalds 		if (ptr < skb->len) {
51171da177e4SLinus Torvalds 			u8 tmp;
51181da177e4SLinus Torvalds 			if (skb_copy_bits(skb, ptr, &tmp, 1))
51191da177e4SLinus Torvalds 				BUG();
51201da177e4SLinus Torvalds 			tp->urg_data = TCP_URG_VALID | tmp;
51211da177e4SLinus Torvalds 			if (!sock_flag(sk, SOCK_DEAD))
5122676d2369SDavid S. Miller 				sk->sk_data_ready(sk);
51231da177e4SLinus Torvalds 		}
51241da177e4SLinus Torvalds 	}
51251da177e4SLinus Torvalds }
51261da177e4SLinus Torvalds 
51271da177e4SLinus Torvalds static int tcp_copy_to_iovec(struct sock *sk, struct sk_buff *skb, int hlen)
51281da177e4SLinus Torvalds {
51291da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
51301da177e4SLinus Torvalds 	int chunk = skb->len - hlen;
51311da177e4SLinus Torvalds 	int err;
51321da177e4SLinus Torvalds 
51331da177e4SLinus Torvalds 	local_bh_enable();
513460476372SHerbert Xu 	if (skb_csum_unnecessary(skb))
5135f4362a2cSAl Viro 		err = skb_copy_datagram_msg(skb, hlen, tp->ucopy.msg, chunk);
51361da177e4SLinus Torvalds 	else
5137f4362a2cSAl Viro 		err = skb_copy_and_csum_datagram_msg(skb, hlen, tp->ucopy.msg);
51381da177e4SLinus Torvalds 
51391da177e4SLinus Torvalds 	if (!err) {
51401da177e4SLinus Torvalds 		tp->ucopy.len -= chunk;
51411da177e4SLinus Torvalds 		tp->copied_seq += chunk;
51421da177e4SLinus Torvalds 		tcp_rcv_space_adjust(sk);
51431da177e4SLinus Torvalds 	}
51441da177e4SLinus Torvalds 
51451da177e4SLinus Torvalds 	local_bh_disable();
51461da177e4SLinus Torvalds 	return err;
51471da177e4SLinus Torvalds }
51481da177e4SLinus Torvalds 
5149056834d9SIlpo Järvinen static __sum16 __tcp_checksum_complete_user(struct sock *sk,
5150056834d9SIlpo Järvinen 					    struct sk_buff *skb)
51511da177e4SLinus Torvalds {
5152b51655b9SAl Viro 	__sum16 result;
51531da177e4SLinus Torvalds 
51541da177e4SLinus Torvalds 	if (sock_owned_by_user(sk)) {
51551da177e4SLinus Torvalds 		local_bh_enable();
51561da177e4SLinus Torvalds 		result = __tcp_checksum_complete(skb);
51571da177e4SLinus Torvalds 		local_bh_disable();
51581da177e4SLinus Torvalds 	} else {
51591da177e4SLinus Torvalds 		result = __tcp_checksum_complete(skb);
51601da177e4SLinus Torvalds 	}
51611da177e4SLinus Torvalds 	return result;
51621da177e4SLinus Torvalds }
51631da177e4SLinus Torvalds 
516467b95bd7SVijay Subramanian static inline bool tcp_checksum_complete_user(struct sock *sk,
5165056834d9SIlpo Järvinen 					     struct sk_buff *skb)
51661da177e4SLinus Torvalds {
516760476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
51681da177e4SLinus Torvalds 	       __tcp_checksum_complete_user(sk, skb);
51691da177e4SLinus Torvalds }
51701da177e4SLinus Torvalds 
5171cbe2d128SIlpo Järvinen /* Does PAWS and seqno based validation of an incoming segment, flags will
5172cbe2d128SIlpo Järvinen  * play significant role here.
5173cbe2d128SIlpo Järvinen  */
51740c24604bSEric Dumazet static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
5175cf533ea5SEric Dumazet 				  const struct tcphdr *th, int syn_inerr)
5176cbe2d128SIlpo Järvinen {
5177cbe2d128SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
5178cbe2d128SIlpo Järvinen 
5179cbe2d128SIlpo Järvinen 	/* RFC1323: H1. Apply PAWS check first. */
51801a2c6181SChristoph Paasch 	if (tcp_fast_parse_options(skb, th, tp) && tp->rx_opt.saw_tstamp &&
5181cbe2d128SIlpo Järvinen 	    tcp_paws_discard(sk, skb)) {
5182cbe2d128SIlpo Järvinen 		if (!th->rst) {
5183cbe2d128SIlpo Järvinen 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
5184f2b2c582SNeal Cardwell 			if (!tcp_oow_rate_limited(sock_net(sk), skb,
5185f2b2c582SNeal Cardwell 						  LINUX_MIB_TCPACKSKIPPEDPAWS,
5186f2b2c582SNeal Cardwell 						  &tp->last_oow_ack_time))
5187cbe2d128SIlpo Järvinen 				tcp_send_dupack(sk, skb);
5188cbe2d128SIlpo Järvinen 			goto discard;
5189cbe2d128SIlpo Järvinen 		}
5190cbe2d128SIlpo Järvinen 		/* Reset is accepted even if it did not pass PAWS. */
5191cbe2d128SIlpo Järvinen 	}
5192cbe2d128SIlpo Järvinen 
5193cbe2d128SIlpo Järvinen 	/* Step 1: check sequence number */
5194cbe2d128SIlpo Järvinen 	if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {
5195cbe2d128SIlpo Järvinen 		/* RFC793, page 37: "In all states except SYN-SENT, all reset
5196cbe2d128SIlpo Järvinen 		 * (RST) segments are validated by checking their SEQ-fields."
5197cbe2d128SIlpo Järvinen 		 * And page 69: "If an incoming segment is not acceptable,
5198cbe2d128SIlpo Järvinen 		 * an acknowledgment should be sent in reply (unless the RST
5199cbe2d128SIlpo Järvinen 		 * bit is set, if so drop the segment and return)".
5200cbe2d128SIlpo Järvinen 		 */
5201e3715899SEric Dumazet 		if (!th->rst) {
5202e3715899SEric Dumazet 			if (th->syn)
5203e3715899SEric Dumazet 				goto syn_challenge;
5204f2b2c582SNeal Cardwell 			if (!tcp_oow_rate_limited(sock_net(sk), skb,
5205f2b2c582SNeal Cardwell 						  LINUX_MIB_TCPACKSKIPPEDSEQ,
5206f2b2c582SNeal Cardwell 						  &tp->last_oow_ack_time))
5207cbe2d128SIlpo Järvinen 				tcp_send_dupack(sk, skb);
5208e3715899SEric Dumazet 		}
5209cbe2d128SIlpo Järvinen 		goto discard;
5210cbe2d128SIlpo Järvinen 	}
5211cbe2d128SIlpo Järvinen 
5212cbe2d128SIlpo Järvinen 	/* Step 2: check RST bit */
5213cbe2d128SIlpo Järvinen 	if (th->rst) {
5214282f23c6SEric Dumazet 		/* RFC 5961 3.2 :
5215282f23c6SEric Dumazet 		 * If sequence number exactly matches RCV.NXT, then
5216282f23c6SEric Dumazet 		 *     RESET the connection
5217282f23c6SEric Dumazet 		 * else
5218282f23c6SEric Dumazet 		 *     Send a challenge ACK
5219282f23c6SEric Dumazet 		 */
5220282f23c6SEric Dumazet 		if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt)
5221cbe2d128SIlpo Järvinen 			tcp_reset(sk);
5222282f23c6SEric Dumazet 		else
5223f2b2c582SNeal Cardwell 			tcp_send_challenge_ack(sk, skb);
5224cbe2d128SIlpo Järvinen 		goto discard;
5225cbe2d128SIlpo Järvinen 	}
5226cbe2d128SIlpo Järvinen 
5227cbe2d128SIlpo Järvinen 	/* step 3: check security and precedence [ignored] */
5228cbe2d128SIlpo Järvinen 
52290c24604bSEric Dumazet 	/* step 4: Check for a SYN
5230cd214535SSowmini Varadhan 	 * RFC 5961 4.2 : Send a challenge ack
52310c24604bSEric Dumazet 	 */
52320c24604bSEric Dumazet 	if (th->syn) {
5233e3715899SEric Dumazet syn_challenge:
5234cbe2d128SIlpo Järvinen 		if (syn_inerr)
5235cbe2d128SIlpo Järvinen 			TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
52360c24604bSEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE);
5237f2b2c582SNeal Cardwell 		tcp_send_challenge_ack(sk, skb);
52380c24604bSEric Dumazet 		goto discard;
5239cbe2d128SIlpo Järvinen 	}
5240cbe2d128SIlpo Järvinen 
52410c24604bSEric Dumazet 	return true;
5242cbe2d128SIlpo Järvinen 
5243cbe2d128SIlpo Järvinen discard:
5244cbe2d128SIlpo Järvinen 	__kfree_skb(skb);
52450c24604bSEric Dumazet 	return false;
5246cbe2d128SIlpo Järvinen }
5247cbe2d128SIlpo Järvinen 
52481da177e4SLinus Torvalds /*
52491da177e4SLinus Torvalds  *	TCP receive function for the ESTABLISHED state.
52501da177e4SLinus Torvalds  *
52511da177e4SLinus Torvalds  *	It is split into a fast path and a slow path. The fast path is
52521da177e4SLinus Torvalds  * 	disabled when:
52531da177e4SLinus Torvalds  *	- A zero window was announced from us - zero window probing
52541da177e4SLinus Torvalds  *        is only handled properly in the slow path.
52551da177e4SLinus Torvalds  *	- Out of order segments arrived.
52561da177e4SLinus Torvalds  *	- Urgent data is expected.
52571da177e4SLinus Torvalds  *	- There is no buffer space left
52581da177e4SLinus Torvalds  *	- Unexpected TCP flags/window values/header lengths are received
52591da177e4SLinus Torvalds  *	  (detected by checking the TCP header against pred_flags)
52601da177e4SLinus Torvalds  *	- Data is sent in both directions. Fast path only supports pure senders
52611da177e4SLinus Torvalds  *	  or pure receivers (this means either the sequence number or the ack
52621da177e4SLinus Torvalds  *	  value must stay constant)
52631da177e4SLinus Torvalds  *	- Unexpected TCP option.
52641da177e4SLinus Torvalds  *
52651da177e4SLinus Torvalds  *	When these conditions are not satisfied it drops into a standard
52661da177e4SLinus Torvalds  *	receive procedure patterned after RFC793 to handle all cases.
52671da177e4SLinus Torvalds  *	The first three cases are guaranteed by proper pred_flags setting,
52681da177e4SLinus Torvalds  *	the rest is checked inline. Fast processing is turned on in
52691da177e4SLinus Torvalds  *	tcp_data_queue when everything is OK.
52701da177e4SLinus Torvalds  */
5271c995ae22SVijay Subramanian void tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
5272cf533ea5SEric Dumazet 			 const struct tcphdr *th, unsigned int len)
52731da177e4SLinus Torvalds {
52741da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
52751da177e4SLinus Torvalds 
527651456b29SIan Morris 	if (unlikely(!sk->sk_rx_dst))
52775d299f3dSEric Dumazet 		inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
52781da177e4SLinus Torvalds 	/*
52791da177e4SLinus Torvalds 	 *	Header prediction.
52801da177e4SLinus Torvalds 	 *	The code loosely follows the one in the famous
52811da177e4SLinus Torvalds 	 *	"30 instruction TCP receive" Van Jacobson mail.
52821da177e4SLinus Torvalds 	 *
52831da177e4SLinus Torvalds 	 *	Van's trick is to deposit buffers into socket queue
52841da177e4SLinus Torvalds 	 *	on a device interrupt, to call tcp_recv function
52851da177e4SLinus Torvalds 	 *	on the receive process context and checksum and copy
52861da177e4SLinus Torvalds 	 *	the buffer to user space. smart...
52871da177e4SLinus Torvalds 	 *
52881da177e4SLinus Torvalds 	 *	Our current scheme is not silly either but we take the
52891da177e4SLinus Torvalds 	 *	extra cost of the net_bh soft interrupt processing...
52901da177e4SLinus Torvalds 	 *	We do checksum and copy also but from device to kernel.
52911da177e4SLinus Torvalds 	 */
52921da177e4SLinus Torvalds 
52931da177e4SLinus Torvalds 	tp->rx_opt.saw_tstamp = 0;
52941da177e4SLinus Torvalds 
52951da177e4SLinus Torvalds 	/*	pred_flags is 0xS?10 << 16 + snd_wnd
5296caa20d9aSStephen Hemminger 	 *	if header_prediction is to be made
52971da177e4SLinus Torvalds 	 *	'S' will always be tp->tcp_header_len >> 2
52981da177e4SLinus Torvalds 	 *	'?' will be 0 for the fast path, otherwise pred_flags is 0 to
52991da177e4SLinus Torvalds 	 *  turn it off	(when there are holes in the receive
53001da177e4SLinus Torvalds 	 *	 space for instance)
53011da177e4SLinus Torvalds 	 *	PSH flag is ignored.
53021da177e4SLinus Torvalds 	 */
53031da177e4SLinus Torvalds 
53041da177e4SLinus Torvalds 	if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
530596e0bf4bSJohn Dykstra 	    TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&
530696e0bf4bSJohn Dykstra 	    !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
53071da177e4SLinus Torvalds 		int tcp_header_len = tp->tcp_header_len;
53081da177e4SLinus Torvalds 
53091da177e4SLinus Torvalds 		/* Timestamp header prediction: tcp_header_len
53101da177e4SLinus Torvalds 		 * is automatically equal to th->doff*4 due to pred_flags
53111da177e4SLinus Torvalds 		 * match.
53121da177e4SLinus Torvalds 		 */
53131da177e4SLinus Torvalds 
53141da177e4SLinus Torvalds 		/* Check timestamp */
53151da177e4SLinus Torvalds 		if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
53161da177e4SLinus Torvalds 			/* No? Slow path! */
5317a4356b29SIlpo Järvinen 			if (!tcp_parse_aligned_timestamp(tp, th))
53181da177e4SLinus Torvalds 				goto slow_path;
53191da177e4SLinus Torvalds 
53201da177e4SLinus Torvalds 			/* If PAWS failed, check it more carefully in slow path */
53211da177e4SLinus Torvalds 			if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0)
53221da177e4SLinus Torvalds 				goto slow_path;
53231da177e4SLinus Torvalds 
53241da177e4SLinus Torvalds 			/* DO NOT update ts_recent here, if checksum fails
53251da177e4SLinus Torvalds 			 * and timestamp was corrupted part, it will result
53261da177e4SLinus Torvalds 			 * in a hung connection since we will drop all
53271da177e4SLinus Torvalds 			 * future packets due to the PAWS test.
53281da177e4SLinus Torvalds 			 */
53291da177e4SLinus Torvalds 		}
53301da177e4SLinus Torvalds 
53311da177e4SLinus Torvalds 		if (len <= tcp_header_len) {
53321da177e4SLinus Torvalds 			/* Bulk data transfer: sender */
53331da177e4SLinus Torvalds 			if (len == tcp_header_len) {
53341da177e4SLinus Torvalds 				/* Predicted packet is in window by definition.
53351da177e4SLinus Torvalds 				 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
53361da177e4SLinus Torvalds 				 * Hence, check seq<=rcv_wup reduces to:
53371da177e4SLinus Torvalds 				 */
53381da177e4SLinus Torvalds 				if (tcp_header_len ==
53391da177e4SLinus Torvalds 				    (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
53401da177e4SLinus Torvalds 				    tp->rcv_nxt == tp->rcv_wup)
53411da177e4SLinus Torvalds 					tcp_store_ts_recent(tp);
53421da177e4SLinus Torvalds 
53431da177e4SLinus Torvalds 				/* We know that such packets are checksummed
53441da177e4SLinus Torvalds 				 * on entry.
53451da177e4SLinus Torvalds 				 */
53461da177e4SLinus Torvalds 				tcp_ack(sk, skb, 0);
53471da177e4SLinus Torvalds 				__kfree_skb(skb);
53489e412ba7SIlpo Järvinen 				tcp_data_snd_check(sk);
5349c995ae22SVijay Subramanian 				return;
53501da177e4SLinus Torvalds 			} else { /* Header too small */
535163231bddSPavel Emelyanov 				TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
53521da177e4SLinus Torvalds 				goto discard;
53531da177e4SLinus Torvalds 			}
53541da177e4SLinus Torvalds 		} else {
53551da177e4SLinus Torvalds 			int eaten = 0;
5356b081f85cSEric Dumazet 			bool fragstolen = false;
53571da177e4SLinus Torvalds 
535859ea33a6SJiri Kosina 			if (tp->ucopy.task == current &&
5359d27f9bc1SDan Williams 			    tp->copied_seq == tp->rcv_nxt &&
5360d27f9bc1SDan Williams 			    len - tcp_header_len <= tp->ucopy.len &&
5361d27f9bc1SDan Williams 			    sock_owned_by_user(sk)) {
53621da177e4SLinus Torvalds 				__set_current_state(TASK_RUNNING);
53631da177e4SLinus Torvalds 
5364d27f9bc1SDan Williams 				if (!tcp_copy_to_iovec(sk, skb, tcp_header_len)) {
53651da177e4SLinus Torvalds 					/* Predicted packet is in window by definition.
53661da177e4SLinus Torvalds 					 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
53671da177e4SLinus Torvalds 					 * Hence, check seq<=rcv_wup reduces to:
53681da177e4SLinus Torvalds 					 */
53691da177e4SLinus Torvalds 					if (tcp_header_len ==
53701da177e4SLinus Torvalds 					    (sizeof(struct tcphdr) +
53711da177e4SLinus Torvalds 					     TCPOLEN_TSTAMP_ALIGNED) &&
53721da177e4SLinus Torvalds 					    tp->rcv_nxt == tp->rcv_wup)
53731da177e4SLinus Torvalds 						tcp_store_ts_recent(tp);
53741da177e4SLinus Torvalds 
5375463c84b9SArnaldo Carvalho de Melo 					tcp_rcv_rtt_measure_ts(sk, skb);
53761da177e4SLinus Torvalds 
53771da177e4SLinus Torvalds 					__skb_pull(skb, tcp_header_len);
5378bdd1f9edSEric Dumazet 					tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
5379de0744afSPavel Emelyanov 					NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITSTOUSER);
5380d27f9bc1SDan Williams 					eaten = 1;
53811da177e4SLinus Torvalds 				}
53821da177e4SLinus Torvalds 			}
53831da177e4SLinus Torvalds 			if (!eaten) {
53841da177e4SLinus Torvalds 				if (tcp_checksum_complete_user(sk, skb))
53851da177e4SLinus Torvalds 					goto csum_error;
53861da177e4SLinus Torvalds 
5387aab2b4bfSNeal Cardwell 				if ((int)skb->truesize > sk->sk_forward_alloc)
5388aab2b4bfSNeal Cardwell 					goto step5;
5389aab2b4bfSNeal Cardwell 
53901da177e4SLinus Torvalds 				/* Predicted packet is in window by definition.
53911da177e4SLinus Torvalds 				 * seq == rcv_nxt and rcv_wup <= rcv_nxt.
53921da177e4SLinus Torvalds 				 * Hence, check seq<=rcv_wup reduces to:
53931da177e4SLinus Torvalds 				 */
53941da177e4SLinus Torvalds 				if (tcp_header_len ==
53951da177e4SLinus Torvalds 				    (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
53961da177e4SLinus Torvalds 				    tp->rcv_nxt == tp->rcv_wup)
53971da177e4SLinus Torvalds 					tcp_store_ts_recent(tp);
53981da177e4SLinus Torvalds 
5399463c84b9SArnaldo Carvalho de Melo 				tcp_rcv_rtt_measure_ts(sk, skb);
54001da177e4SLinus Torvalds 
5401de0744afSPavel Emelyanov 				NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITS);
54021da177e4SLinus Torvalds 
54031da177e4SLinus Torvalds 				/* Bulk data transfer: receiver */
5404b081f85cSEric Dumazet 				eaten = tcp_queue_rcv(sk, skb, tcp_header_len,
5405b081f85cSEric Dumazet 						      &fragstolen);
54061da177e4SLinus Torvalds 			}
54071da177e4SLinus Torvalds 
54089e412ba7SIlpo Järvinen 			tcp_event_data_recv(sk, skb);
54091da177e4SLinus Torvalds 
54101da177e4SLinus Torvalds 			if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
54111da177e4SLinus Torvalds 				/* Well, only one small jumplet in fast path... */
54121da177e4SLinus Torvalds 				tcp_ack(sk, skb, FLAG_DATA);
54139e412ba7SIlpo Järvinen 				tcp_data_snd_check(sk);
5414463c84b9SArnaldo Carvalho de Melo 				if (!inet_csk_ack_scheduled(sk))
54151da177e4SLinus Torvalds 					goto no_ack;
54161da177e4SLinus Torvalds 			}
54171da177e4SLinus Torvalds 
54181da177e4SLinus Torvalds 			__tcp_ack_snd_check(sk, 0);
54191da177e4SLinus Torvalds no_ack:
54201da177e4SLinus Torvalds 			if (eaten)
5421b081f85cSEric Dumazet 				kfree_skb_partial(skb, fragstolen);
5422676d2369SDavid S. Miller 			sk->sk_data_ready(sk);
5423c995ae22SVijay Subramanian 			return;
54241da177e4SLinus Torvalds 		}
54251da177e4SLinus Torvalds 	}
54261da177e4SLinus Torvalds 
54271da177e4SLinus Torvalds slow_path:
54281da177e4SLinus Torvalds 	if (len < (th->doff << 2) || tcp_checksum_complete_user(sk, skb))
54291da177e4SLinus Torvalds 		goto csum_error;
54301da177e4SLinus Torvalds 
54310c228e83SCalvin Owens 	if (!th->ack && !th->rst && !th->syn)
5432c3ae62afSEric Dumazet 		goto discard;
5433c3ae62afSEric Dumazet 
54341da177e4SLinus Torvalds 	/*
54351da177e4SLinus Torvalds 	 *	Standard slow path.
54361da177e4SLinus Torvalds 	 */
54371da177e4SLinus Torvalds 
54380c24604bSEric Dumazet 	if (!tcp_validate_incoming(sk, skb, th, 1))
5439c995ae22SVijay Subramanian 		return;
54401da177e4SLinus Torvalds 
54411da177e4SLinus Torvalds step5:
544212fb3dd9SEric Dumazet 	if (tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0)
544396e0bf4bSJohn Dykstra 		goto discard;
54441da177e4SLinus Torvalds 
5445463c84b9SArnaldo Carvalho de Melo 	tcp_rcv_rtt_measure_ts(sk, skb);
54461da177e4SLinus Torvalds 
54471da177e4SLinus Torvalds 	/* Process urgent data. */
54481da177e4SLinus Torvalds 	tcp_urg(sk, skb, th);
54491da177e4SLinus Torvalds 
54501da177e4SLinus Torvalds 	/* step 7: process the segment text */
54511da177e4SLinus Torvalds 	tcp_data_queue(sk, skb);
54521da177e4SLinus Torvalds 
54539e412ba7SIlpo Järvinen 	tcp_data_snd_check(sk);
54541da177e4SLinus Torvalds 	tcp_ack_snd_check(sk);
5455c995ae22SVijay Subramanian 	return;
54561da177e4SLinus Torvalds 
54571da177e4SLinus Torvalds csum_error:
54586a5dc9e5SEric Dumazet 	TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_CSUMERRORS);
545963231bddSPavel Emelyanov 	TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
54601da177e4SLinus Torvalds 
54611da177e4SLinus Torvalds discard:
54621da177e4SLinus Torvalds 	__kfree_skb(skb);
54631da177e4SLinus Torvalds }
54644bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_rcv_established);
54651da177e4SLinus Torvalds 
5466370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
5467370816aeSPavel Emelyanov {
5468370816aeSPavel Emelyanov 	struct tcp_sock *tp = tcp_sk(sk);
5469370816aeSPavel Emelyanov 	struct inet_connection_sock *icsk = inet_csk(sk);
5470370816aeSPavel Emelyanov 
5471370816aeSPavel Emelyanov 	tcp_set_state(sk, TCP_ESTABLISHED);
5472370816aeSPavel Emelyanov 
547300db4124SIan Morris 	if (skb) {
54745d299f3dSEric Dumazet 		icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
5475370816aeSPavel Emelyanov 		security_inet_conn_established(sk, skb);
547641063e9dSDavid S. Miller 	}
5477370816aeSPavel Emelyanov 
5478370816aeSPavel Emelyanov 	/* Make sure socket is routed, for correct metrics.  */
5479370816aeSPavel Emelyanov 	icsk->icsk_af_ops->rebuild_header(sk);
5480370816aeSPavel Emelyanov 
5481370816aeSPavel Emelyanov 	tcp_init_metrics(sk);
5482370816aeSPavel Emelyanov 
5483370816aeSPavel Emelyanov 	tcp_init_congestion_control(sk);
5484370816aeSPavel Emelyanov 
5485370816aeSPavel Emelyanov 	/* Prevent spurious tcp_cwnd_restart() on first data
5486370816aeSPavel Emelyanov 	 * packet.
5487370816aeSPavel Emelyanov 	 */
5488370816aeSPavel Emelyanov 	tp->lsndtime = tcp_time_stamp;
5489370816aeSPavel Emelyanov 
5490370816aeSPavel Emelyanov 	tcp_init_buffer_space(sk);
5491370816aeSPavel Emelyanov 
5492370816aeSPavel Emelyanov 	if (sock_flag(sk, SOCK_KEEPOPEN))
5493370816aeSPavel Emelyanov 		inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp));
5494370816aeSPavel Emelyanov 
5495370816aeSPavel Emelyanov 	if (!tp->rx_opt.snd_wscale)
5496370816aeSPavel Emelyanov 		__tcp_fast_path_on(tp, tp->snd_wnd);
5497370816aeSPavel Emelyanov 	else
5498370816aeSPavel Emelyanov 		tp->pred_flags = 0;
5499370816aeSPavel Emelyanov 
5500370816aeSPavel Emelyanov 	if (!sock_flag(sk, SOCK_DEAD)) {
5501370816aeSPavel Emelyanov 		sk->sk_state_change(sk);
5502370816aeSPavel Emelyanov 		sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
5503370816aeSPavel Emelyanov 	}
5504370816aeSPavel Emelyanov }
5505370816aeSPavel Emelyanov 
55068e4178c1SYuchung Cheng static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
55078e4178c1SYuchung Cheng 				    struct tcp_fastopen_cookie *cookie)
55088e4178c1SYuchung Cheng {
55098e4178c1SYuchung Cheng 	struct tcp_sock *tp = tcp_sk(sk);
551067da22d2SYuchung Cheng 	struct sk_buff *data = tp->syn_data ? tcp_write_queue_head(sk) : NULL;
55112646c831SDaniel Lee 	u16 mss = tp->rx_opt.mss_clamp, try_exp = 0;
55122646c831SDaniel Lee 	bool syn_drop = false;
55138e4178c1SYuchung Cheng 
55148e4178c1SYuchung Cheng 	if (mss == tp->rx_opt.user_mss) {
55158e4178c1SYuchung Cheng 		struct tcp_options_received opt;
55168e4178c1SYuchung Cheng 
55178e4178c1SYuchung Cheng 		/* Get original SYNACK MSS value if user MSS sets mss_clamp */
55188e4178c1SYuchung Cheng 		tcp_clear_options(&opt);
55198e4178c1SYuchung Cheng 		opt.user_mss = opt.mss_clamp = 0;
55201a2c6181SChristoph Paasch 		tcp_parse_options(synack, &opt, 0, NULL);
55218e4178c1SYuchung Cheng 		mss = opt.mss_clamp;
55228e4178c1SYuchung Cheng 	}
55238e4178c1SYuchung Cheng 
55242646c831SDaniel Lee 	if (!tp->syn_fastopen) {
55252646c831SDaniel Lee 		/* Ignore an unsolicited cookie */
552667da22d2SYuchung Cheng 		cookie->len = -1;
55272646c831SDaniel Lee 	} else if (tp->total_retrans) {
55282646c831SDaniel Lee 		/* SYN timed out and the SYN-ACK neither has a cookie nor
55292646c831SDaniel Lee 		 * acknowledges data. Presumably the remote received only
55302646c831SDaniel Lee 		 * the retransmitted (regular) SYNs: either the original
55312646c831SDaniel Lee 		 * SYN-data or the corresponding SYN-ACK was dropped.
5532aab48743SYuchung Cheng 		 */
55332646c831SDaniel Lee 		syn_drop = (cookie->len < 0 && data);
55342646c831SDaniel Lee 	} else if (cookie->len < 0 && !tp->syn_data) {
55352646c831SDaniel Lee 		/* We requested a cookie but didn't get it. If we did not use
55362646c831SDaniel Lee 		 * the (old) exp opt format then try so next time (try_exp=1).
55372646c831SDaniel Lee 		 * Otherwise we go back to use the RFC7413 opt (try_exp=2).
55382646c831SDaniel Lee 		 */
55392646c831SDaniel Lee 		try_exp = tp->syn_fastopen_exp ? 2 : 1;
55402646c831SDaniel Lee 	}
5541aab48743SYuchung Cheng 
55422646c831SDaniel Lee 	tcp_fastopen_cache_set(sk, mss, cookie, syn_drop, try_exp);
55438e4178c1SYuchung Cheng 
55448e4178c1SYuchung Cheng 	if (data) { /* Retransmit unacked data in SYN */
554593b174adSYuchung Cheng 		tcp_for_write_queue_from(data, sk) {
554693b174adSYuchung Cheng 			if (data == tcp_send_head(sk) ||
554793b174adSYuchung Cheng 			    __tcp_retransmit_skb(sk, data))
554893b174adSYuchung Cheng 				break;
554993b174adSYuchung Cheng 		}
55508e4178c1SYuchung Cheng 		tcp_rearm_rto(sk);
5551f19c29e3SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVEFAIL);
55528e4178c1SYuchung Cheng 		return true;
55538e4178c1SYuchung Cheng 	}
55546f73601eSYuchung Cheng 	tp->syn_data_acked = tp->syn_data;
5555f19c29e3SYuchung Cheng 	if (tp->syn_data_acked)
5556f19c29e3SYuchung Cheng 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVE);
555761d2bcaeSEric Dumazet 
555861d2bcaeSEric Dumazet 	tcp_fastopen_add_skb(sk, synack);
555961d2bcaeSEric Dumazet 
55608e4178c1SYuchung Cheng 	return false;
55618e4178c1SYuchung Cheng }
55628e4178c1SYuchung Cheng 
55631da177e4SLinus Torvalds static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
5564bda07a64SEric Dumazet 					 const struct tcphdr *th)
55651da177e4SLinus Torvalds {
5566d83d8461SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
55674957faadSWilliam Allen Simpson 	struct tcp_sock *tp = tcp_sk(sk);
55688e4178c1SYuchung Cheng 	struct tcp_fastopen_cookie foc = { .len = -1 };
55694957faadSWilliam Allen Simpson 	int saved_clamp = tp->rx_opt.mss_clamp;
55701da177e4SLinus Torvalds 
55711a2c6181SChristoph Paasch 	tcp_parse_options(skb, &tp->rx_opt, 0, &foc);
5572e3e12028SAndrew Vagin 	if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
5573ee684b6fSAndrey Vagin 		tp->rx_opt.rcv_tsecr -= tp->tsoffset;
55741da177e4SLinus Torvalds 
55751da177e4SLinus Torvalds 	if (th->ack) {
55761da177e4SLinus Torvalds 		/* rfc793:
55771da177e4SLinus Torvalds 		 * "If the state is SYN-SENT then
55781da177e4SLinus Torvalds 		 *    first check the ACK bit
55791da177e4SLinus Torvalds 		 *      If the ACK bit is set
55801da177e4SLinus Torvalds 		 *	  If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
55811da177e4SLinus Torvalds 		 *        a reset (unless the RST bit is set, if so drop
55821da177e4SLinus Torvalds 		 *        the segment and return)"
55831da177e4SLinus Torvalds 		 */
55848e4178c1SYuchung Cheng 		if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
55858e4178c1SYuchung Cheng 		    after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt))
55861da177e4SLinus Torvalds 			goto reset_and_undo;
55871da177e4SLinus Torvalds 
55881da177e4SLinus Torvalds 		if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
55891da177e4SLinus Torvalds 		    !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp,
55901da177e4SLinus Torvalds 			     tcp_time_stamp)) {
5591de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSACTIVEREJECTED);
55921da177e4SLinus Torvalds 			goto reset_and_undo;
55931da177e4SLinus Torvalds 		}
55941da177e4SLinus Torvalds 
55951da177e4SLinus Torvalds 		/* Now ACK is acceptable.
55961da177e4SLinus Torvalds 		 *
55971da177e4SLinus Torvalds 		 * "If the RST bit is set
55981da177e4SLinus Torvalds 		 *    If the ACK was acceptable then signal the user "error:
55991da177e4SLinus Torvalds 		 *    connection reset", drop the segment, enter CLOSED state,
56001da177e4SLinus Torvalds 		 *    delete TCB, and return."
56011da177e4SLinus Torvalds 		 */
56021da177e4SLinus Torvalds 
56031da177e4SLinus Torvalds 		if (th->rst) {
56041da177e4SLinus Torvalds 			tcp_reset(sk);
56051da177e4SLinus Torvalds 			goto discard;
56061da177e4SLinus Torvalds 		}
56071da177e4SLinus Torvalds 
56081da177e4SLinus Torvalds 		/* rfc793:
56091da177e4SLinus Torvalds 		 *   "fifth, if neither of the SYN or RST bits is set then
56101da177e4SLinus Torvalds 		 *    drop the segment and return."
56111da177e4SLinus Torvalds 		 *
56121da177e4SLinus Torvalds 		 *    See note below!
56131da177e4SLinus Torvalds 		 *                                        --ANK(990513)
56141da177e4SLinus Torvalds 		 */
56151da177e4SLinus Torvalds 		if (!th->syn)
56161da177e4SLinus Torvalds 			goto discard_and_undo;
56171da177e4SLinus Torvalds 
56181da177e4SLinus Torvalds 		/* rfc793:
56191da177e4SLinus Torvalds 		 *   "If the SYN bit is on ...
56201da177e4SLinus Torvalds 		 *    are acceptable then ...
56211da177e4SLinus Torvalds 		 *    (our SYN has been ACKed), change the connection
56221da177e4SLinus Torvalds 		 *    state to ESTABLISHED..."
56231da177e4SLinus Torvalds 		 */
56241da177e4SLinus Torvalds 
5625735d3831SFlorian Westphal 		tcp_ecn_rcv_synack(tp, th);
56261da177e4SLinus Torvalds 
56271b3a6926SRazvan Ghitulete 		tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
56281da177e4SLinus Torvalds 		tcp_ack(sk, skb, FLAG_SLOWPATH);
56291da177e4SLinus Torvalds 
56301da177e4SLinus Torvalds 		/* Ok.. it's good. Set up sequence numbers and
56311da177e4SLinus Torvalds 		 * move to established.
56321da177e4SLinus Torvalds 		 */
56331da177e4SLinus Torvalds 		tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
56341da177e4SLinus Torvalds 		tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
56351da177e4SLinus Torvalds 
56361da177e4SLinus Torvalds 		/* RFC1323: The window in SYN & SYN/ACK segments is
56371da177e4SLinus Torvalds 		 * never scaled.
56381da177e4SLinus Torvalds 		 */
56391da177e4SLinus Torvalds 		tp->snd_wnd = ntohs(th->window);
56401da177e4SLinus Torvalds 
56411da177e4SLinus Torvalds 		if (!tp->rx_opt.wscale_ok) {
56421da177e4SLinus Torvalds 			tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0;
56431da177e4SLinus Torvalds 			tp->window_clamp = min(tp->window_clamp, 65535U);
56441da177e4SLinus Torvalds 		}
56451da177e4SLinus Torvalds 
56461da177e4SLinus Torvalds 		if (tp->rx_opt.saw_tstamp) {
56471da177e4SLinus Torvalds 			tp->rx_opt.tstamp_ok	   = 1;
56481da177e4SLinus Torvalds 			tp->tcp_header_len =
56491da177e4SLinus Torvalds 				sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
56501da177e4SLinus Torvalds 			tp->advmss	    -= TCPOLEN_TSTAMP_ALIGNED;
56511da177e4SLinus Torvalds 			tcp_store_ts_recent(tp);
56521da177e4SLinus Torvalds 		} else {
56531da177e4SLinus Torvalds 			tp->tcp_header_len = sizeof(struct tcphdr);
56541da177e4SLinus Torvalds 		}
56551da177e4SLinus Torvalds 
5656e60402d0SIlpo Järvinen 		if (tcp_is_sack(tp) && sysctl_tcp_fack)
5657e60402d0SIlpo Järvinen 			tcp_enable_fack(tp);
56581da177e4SLinus Torvalds 
56595d424d5aSJohn Heffner 		tcp_mtup_init(sk);
5660d83d8461SArnaldo Carvalho de Melo 		tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
56611da177e4SLinus Torvalds 		tcp_initialize_rcv_mss(sk);
56621da177e4SLinus Torvalds 
56631da177e4SLinus Torvalds 		/* Remember, tcp_poll() does not lock socket!
56641da177e4SLinus Torvalds 		 * Change state from SYN-SENT only after copied_seq
56651da177e4SLinus Torvalds 		 * is initialized. */
56661da177e4SLinus Torvalds 		tp->copied_seq = tp->rcv_nxt;
56674957faadSWilliam Allen Simpson 
5668e16aa207SRalf Baechle 		smp_mb();
56691da177e4SLinus Torvalds 
5670370816aeSPavel Emelyanov 		tcp_finish_connect(sk, skb);
56711da177e4SLinus Torvalds 
567267da22d2SYuchung Cheng 		if ((tp->syn_fastopen || tp->syn_data) &&
567367da22d2SYuchung Cheng 		    tcp_rcv_fastopen_synack(sk, skb, &foc))
56748e4178c1SYuchung Cheng 			return -1;
56758e4178c1SYuchung Cheng 
5676295f7324SArnaldo Carvalho de Melo 		if (sk->sk_write_pending ||
5677295f7324SArnaldo Carvalho de Melo 		    icsk->icsk_accept_queue.rskq_defer_accept ||
5678295f7324SArnaldo Carvalho de Melo 		    icsk->icsk_ack.pingpong) {
56791da177e4SLinus Torvalds 			/* Save one ACK. Data will be ready after
56801da177e4SLinus Torvalds 			 * several ticks, if write_pending is set.
56811da177e4SLinus Torvalds 			 *
56821da177e4SLinus Torvalds 			 * It may be deleted, but with this feature tcpdumps
56831da177e4SLinus Torvalds 			 * look so _wonderfully_ clever, that I was not able
56841da177e4SLinus Torvalds 			 * to stand against the temptation 8)     --ANK
56851da177e4SLinus Torvalds 			 */
5686463c84b9SArnaldo Carvalho de Melo 			inet_csk_schedule_ack(sk);
5687295f7324SArnaldo Carvalho de Melo 			icsk->icsk_ack.lrcvtime = tcp_time_stamp;
5688463c84b9SArnaldo Carvalho de Melo 			tcp_enter_quickack_mode(sk);
56893f421baaSArnaldo Carvalho de Melo 			inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
56903f421baaSArnaldo Carvalho de Melo 						  TCP_DELACK_MAX, TCP_RTO_MAX);
56911da177e4SLinus Torvalds 
56921da177e4SLinus Torvalds discard:
56931da177e4SLinus Torvalds 			__kfree_skb(skb);
56941da177e4SLinus Torvalds 			return 0;
56951da177e4SLinus Torvalds 		} else {
56961da177e4SLinus Torvalds 			tcp_send_ack(sk);
56971da177e4SLinus Torvalds 		}
56981da177e4SLinus Torvalds 		return -1;
56991da177e4SLinus Torvalds 	}
57001da177e4SLinus Torvalds 
57011da177e4SLinus Torvalds 	/* No ACK in the segment */
57021da177e4SLinus Torvalds 
57031da177e4SLinus Torvalds 	if (th->rst) {
57041da177e4SLinus Torvalds 		/* rfc793:
57051da177e4SLinus Torvalds 		 * "If the RST bit is set
57061da177e4SLinus Torvalds 		 *
57071da177e4SLinus Torvalds 		 *      Otherwise (no ACK) drop the segment and return."
57081da177e4SLinus Torvalds 		 */
57091da177e4SLinus Torvalds 
57101da177e4SLinus Torvalds 		goto discard_and_undo;
57111da177e4SLinus Torvalds 	}
57121da177e4SLinus Torvalds 
57131da177e4SLinus Torvalds 	/* PAWS check. */
5714056834d9SIlpo Järvinen 	if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp &&
5715c887e6d2SIlpo Järvinen 	    tcp_paws_reject(&tp->rx_opt, 0))
57161da177e4SLinus Torvalds 		goto discard_and_undo;
57171da177e4SLinus Torvalds 
57181da177e4SLinus Torvalds 	if (th->syn) {
57191da177e4SLinus Torvalds 		/* We see SYN without ACK. It is attempt of
57201da177e4SLinus Torvalds 		 * simultaneous connect with crossed SYNs.
57211da177e4SLinus Torvalds 		 * Particularly, it can be connect to self.
57221da177e4SLinus Torvalds 		 */
57231da177e4SLinus Torvalds 		tcp_set_state(sk, TCP_SYN_RECV);
57241da177e4SLinus Torvalds 
57251da177e4SLinus Torvalds 		if (tp->rx_opt.saw_tstamp) {
57261da177e4SLinus Torvalds 			tp->rx_opt.tstamp_ok = 1;
57271da177e4SLinus Torvalds 			tcp_store_ts_recent(tp);
57281da177e4SLinus Torvalds 			tp->tcp_header_len =
57291da177e4SLinus Torvalds 				sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
57301da177e4SLinus Torvalds 		} else {
57311da177e4SLinus Torvalds 			tp->tcp_header_len = sizeof(struct tcphdr);
57321da177e4SLinus Torvalds 		}
57331da177e4SLinus Torvalds 
57341da177e4SLinus Torvalds 		tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
5735142a2e7eSEric Dumazet 		tp->copied_seq = tp->rcv_nxt;
57361da177e4SLinus Torvalds 		tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
57371da177e4SLinus Torvalds 
57381da177e4SLinus Torvalds 		/* RFC1323: The window in SYN & SYN/ACK segments is
57391da177e4SLinus Torvalds 		 * never scaled.
57401da177e4SLinus Torvalds 		 */
57411da177e4SLinus Torvalds 		tp->snd_wnd    = ntohs(th->window);
57421da177e4SLinus Torvalds 		tp->snd_wl1    = TCP_SKB_CB(skb)->seq;
57431da177e4SLinus Torvalds 		tp->max_window = tp->snd_wnd;
57441da177e4SLinus Torvalds 
5745735d3831SFlorian Westphal 		tcp_ecn_rcv_syn(tp, th);
57461da177e4SLinus Torvalds 
57475d424d5aSJohn Heffner 		tcp_mtup_init(sk);
5748d83d8461SArnaldo Carvalho de Melo 		tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
57491da177e4SLinus Torvalds 		tcp_initialize_rcv_mss(sk);
57501da177e4SLinus Torvalds 
57511da177e4SLinus Torvalds 		tcp_send_synack(sk);
57521da177e4SLinus Torvalds #if 0
57531da177e4SLinus Torvalds 		/* Note, we could accept data and URG from this segment.
5754168a8f58SJerry Chu 		 * There are no obstacles to make this (except that we must
5755168a8f58SJerry Chu 		 * either change tcp_recvmsg() to prevent it from returning data
5756168a8f58SJerry Chu 		 * before 3WHS completes per RFC793, or employ TCP Fast Open).
57571da177e4SLinus Torvalds 		 *
57581da177e4SLinus Torvalds 		 * However, if we ignore data in ACKless segments sometimes,
57591da177e4SLinus Torvalds 		 * we have no reasons to accept it sometimes.
57601da177e4SLinus Torvalds 		 * Also, seems the code doing it in step6 of tcp_rcv_state_process
57611da177e4SLinus Torvalds 		 * is not flawless. So, discard packet for sanity.
57621da177e4SLinus Torvalds 		 * Uncomment this return to process the data.
57631da177e4SLinus Torvalds 		 */
57641da177e4SLinus Torvalds 		return -1;
57651da177e4SLinus Torvalds #else
57661da177e4SLinus Torvalds 		goto discard;
57671da177e4SLinus Torvalds #endif
57681da177e4SLinus Torvalds 	}
57691da177e4SLinus Torvalds 	/* "fifth, if neither of the SYN or RST bits is set then
57701da177e4SLinus Torvalds 	 * drop the segment and return."
57711da177e4SLinus Torvalds 	 */
57721da177e4SLinus Torvalds 
57731da177e4SLinus Torvalds discard_and_undo:
57741da177e4SLinus Torvalds 	tcp_clear_options(&tp->rx_opt);
57751da177e4SLinus Torvalds 	tp->rx_opt.mss_clamp = saved_clamp;
57761da177e4SLinus Torvalds 	goto discard;
57771da177e4SLinus Torvalds 
57781da177e4SLinus Torvalds reset_and_undo:
57791da177e4SLinus Torvalds 	tcp_clear_options(&tp->rx_opt);
57801da177e4SLinus Torvalds 	tp->rx_opt.mss_clamp = saved_clamp;
57811da177e4SLinus Torvalds 	return 1;
57821da177e4SLinus Torvalds }
57831da177e4SLinus Torvalds 
57841da177e4SLinus Torvalds /*
57851da177e4SLinus Torvalds  *	This function implements the receiving procedure of RFC 793 for
57861da177e4SLinus Torvalds  *	all states except ESTABLISHED and TIME_WAIT.
57871da177e4SLinus Torvalds  *	It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
57881da177e4SLinus Torvalds  *	address independent.
57891da177e4SLinus Torvalds  */
57901da177e4SLinus Torvalds 
579172ab4a86SEric Dumazet int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
57921da177e4SLinus Torvalds {
57931da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
57948292a17aSArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
579572ab4a86SEric Dumazet 	const struct tcphdr *th = tcp_hdr(skb);
5796168a8f58SJerry Chu 	struct request_sock *req;
57971da177e4SLinus Torvalds 	int queued = 0;
57981f6afc81SEric Dumazet 	bool acceptable;
57991da177e4SLinus Torvalds 
58001da177e4SLinus Torvalds 	tp->rx_opt.saw_tstamp = 0;
58011da177e4SLinus Torvalds 
58021da177e4SLinus Torvalds 	switch (sk->sk_state) {
58031da177e4SLinus Torvalds 	case TCP_CLOSE:
58041da177e4SLinus Torvalds 		goto discard;
58051da177e4SLinus Torvalds 
58061da177e4SLinus Torvalds 	case TCP_LISTEN:
58071da177e4SLinus Torvalds 		if (th->ack)
58081da177e4SLinus Torvalds 			return 1;
58091da177e4SLinus Torvalds 
58101da177e4SLinus Torvalds 		if (th->rst)
58111da177e4SLinus Torvalds 			goto discard;
58121da177e4SLinus Torvalds 
58131da177e4SLinus Torvalds 		if (th->syn) {
5814fdf5af0dSEric Dumazet 			if (th->fin)
5815fdf5af0dSEric Dumazet 				goto discard;
58168292a17aSArnaldo Carvalho de Melo 			if (icsk->icsk_af_ops->conn_request(sk, skb) < 0)
58171da177e4SLinus Torvalds 				return 1;
58181da177e4SLinus Torvalds 
58191da177e4SLinus Torvalds 			/* Now we have several options: In theory there is
58201da177e4SLinus Torvalds 			 * nothing else in the frame. KA9Q has an option to
58211da177e4SLinus Torvalds 			 * send data with the syn, BSD accepts data with the
58221da177e4SLinus Torvalds 			 * syn up to the [to be] advertised window and
58231da177e4SLinus Torvalds 			 * Solaris 2.1 gives you a protocol error. For now
58241da177e4SLinus Torvalds 			 * we just ignore it, that fits the spec precisely
58251da177e4SLinus Torvalds 			 * and avoids incompatibilities. It would be nice in
58261da177e4SLinus Torvalds 			 * future to drop through and process the data.
58271da177e4SLinus Torvalds 			 *
58281da177e4SLinus Torvalds 			 * Now that TTCP is starting to be used we ought to
58291da177e4SLinus Torvalds 			 * queue this data.
58301da177e4SLinus Torvalds 			 * But, this leaves one open to an easy denial of
58311da177e4SLinus Torvalds 			 * service attack, and SYN cookies can't defend
58321da177e4SLinus Torvalds 			 * against this problem. So, we drop the data
5833fb7e2399SMasayuki Nakagawa 			 * in the interest of security over speed unless
5834fb7e2399SMasayuki Nakagawa 			 * it's still in use.
58351da177e4SLinus Torvalds 			 */
5836fb7e2399SMasayuki Nakagawa 			kfree_skb(skb);
5837fb7e2399SMasayuki Nakagawa 			return 0;
58381da177e4SLinus Torvalds 		}
58391da177e4SLinus Torvalds 		goto discard;
58401da177e4SLinus Torvalds 
58411da177e4SLinus Torvalds 	case TCP_SYN_SENT:
5842bda07a64SEric Dumazet 		queued = tcp_rcv_synsent_state_process(sk, skb, th);
58431da177e4SLinus Torvalds 		if (queued >= 0)
58441da177e4SLinus Torvalds 			return queued;
58451da177e4SLinus Torvalds 
58461da177e4SLinus Torvalds 		/* Do step6 onward by hand. */
58471da177e4SLinus Torvalds 		tcp_urg(sk, skb, th);
58481da177e4SLinus Torvalds 		__kfree_skb(skb);
58499e412ba7SIlpo Järvinen 		tcp_data_snd_check(sk);
58501da177e4SLinus Torvalds 		return 0;
58511da177e4SLinus Torvalds 	}
58521da177e4SLinus Torvalds 
5853168a8f58SJerry Chu 	req = tp->fastopen_rsk;
585400db4124SIan Morris 	if (req) {
585537561f68SJerry Chu 		WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
5856168a8f58SJerry Chu 		    sk->sk_state != TCP_FIN_WAIT1);
5857168a8f58SJerry Chu 
585851456b29SIan Morris 		if (!tcp_check_req(sk, skb, req, true))
5859168a8f58SJerry Chu 			goto discard;
5860e69bebdeSNeal Cardwell 	}
5861c3ae62afSEric Dumazet 
58620c228e83SCalvin Owens 	if (!th->ack && !th->rst && !th->syn)
5863c3ae62afSEric Dumazet 		goto discard;
5864c3ae62afSEric Dumazet 
5865e69bebdeSNeal Cardwell 	if (!tcp_validate_incoming(sk, skb, th, 0))
58660c24604bSEric Dumazet 		return 0;
58671da177e4SLinus Torvalds 
58681da177e4SLinus Torvalds 	/* step 5: check the ACK field */
58691f6afc81SEric Dumazet 	acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH |
587012fb3dd9SEric Dumazet 				      FLAG_UPDATE_TS_RECENT) > 0;
58711da177e4SLinus Torvalds 
58721da177e4SLinus Torvalds 	switch (sk->sk_state) {
58731da177e4SLinus Torvalds 	case TCP_SYN_RECV:
587461eb9003SJoe Perches 		if (!acceptable)
587561eb9003SJoe Perches 			return 1;
587661eb9003SJoe Perches 
58770f1c28aeSYuchung Cheng 		if (!tp->srtt_us)
58780f1c28aeSYuchung Cheng 			tcp_synack_rtt_meas(sk, req);
58790f1c28aeSYuchung Cheng 
588061eb9003SJoe Perches 		/* Once we leave TCP_SYN_RECV, we no longer need req
588161eb9003SJoe Perches 		 * so release it.
5882168a8f58SJerry Chu 		 */
5883168a8f58SJerry Chu 		if (req) {
5884e6c022a4SEric Dumazet 			tp->total_retrans = req->num_retrans;
5885168a8f58SJerry Chu 			reqsk_fastopen_remove(sk, req, false);
5886168a8f58SJerry Chu 		} else {
588761eb9003SJoe Perches 			/* Make sure socket is routed, for correct metrics. */
5888168a8f58SJerry Chu 			icsk->icsk_af_ops->rebuild_header(sk);
5889168a8f58SJerry Chu 			tcp_init_congestion_control(sk);
5890168a8f58SJerry Chu 
5891168a8f58SJerry Chu 			tcp_mtup_init(sk);
58921da177e4SLinus Torvalds 			tp->copied_seq = tp->rcv_nxt;
5893b0983d3cSEric Dumazet 			tcp_init_buffer_space(sk);
5894168a8f58SJerry Chu 		}
5895e16aa207SRalf Baechle 		smp_mb();
58961da177e4SLinus Torvalds 		tcp_set_state(sk, TCP_ESTABLISHED);
58971da177e4SLinus Torvalds 		sk->sk_state_change(sk);
58981da177e4SLinus Torvalds 
589961eb9003SJoe Perches 		/* Note, that this wakeup is only for marginal crossed SYN case.
590061eb9003SJoe Perches 		 * Passively open sockets are not waked up, because
590161eb9003SJoe Perches 		 * sk->sk_sleep == NULL and sk->sk_socket == NULL.
59021da177e4SLinus Torvalds 		 */
59038d8ad9d7SPavel Emelyanov 		if (sk->sk_socket)
59041f6afc81SEric Dumazet 			sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
59051da177e4SLinus Torvalds 
59061da177e4SLinus Torvalds 		tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
590761eb9003SJoe Perches 		tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
5908ee7537b6SHantzis Fotis 		tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
59091da177e4SLinus Torvalds 
59101da177e4SLinus Torvalds 		if (tp->rx_opt.tstamp_ok)
59111da177e4SLinus Torvalds 			tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
59121da177e4SLinus Torvalds 
5913168a8f58SJerry Chu 		if (req) {
591461eb9003SJoe Perches 			/* Re-arm the timer because data may have been sent out.
591561eb9003SJoe Perches 			 * This is similar to the regular data transmission case
5916168a8f58SJerry Chu 			 * when new data has just been ack'ed.
5917168a8f58SJerry Chu 			 *
591861eb9003SJoe Perches 			 * (TFO) - we could try to be more aggressive and
591961eb9003SJoe Perches 			 * retransmitting any data sooner based on when they
592061eb9003SJoe Perches 			 * are sent out.
59211da177e4SLinus Torvalds 			 */
5922168a8f58SJerry Chu 			tcp_rearm_rto(sk);
5923168a8f58SJerry Chu 		} else
59241da177e4SLinus Torvalds 			tcp_init_metrics(sk);
59251da177e4SLinus Torvalds 
592602cf4ebdSNeal Cardwell 		tcp_update_pacing_rate(sk);
592702cf4ebdSNeal Cardwell 
592861eb9003SJoe Perches 		/* Prevent spurious tcp_cwnd_restart() on first data packet */
59291da177e4SLinus Torvalds 		tp->lsndtime = tcp_time_stamp;
59301da177e4SLinus Torvalds 
59311da177e4SLinus Torvalds 		tcp_initialize_rcv_mss(sk);
59321da177e4SLinus Torvalds 		tcp_fast_path_on(tp);
59331da177e4SLinus Torvalds 		break;
59341da177e4SLinus Torvalds 
5935c48b22daSJoe Perches 	case TCP_FIN_WAIT1: {
5936c48b22daSJoe Perches 		struct dst_entry *dst;
5937c48b22daSJoe Perches 		int tmo;
5938c48b22daSJoe Perches 
5939168a8f58SJerry Chu 		/* If we enter the TCP_FIN_WAIT1 state and we are a
5940168a8f58SJerry Chu 		 * Fast Open socket and this is the first acceptable
5941168a8f58SJerry Chu 		 * ACK we have received, this would have acknowledged
5942168a8f58SJerry Chu 		 * our SYNACK so stop the SYNACK timer.
5943168a8f58SJerry Chu 		 */
594400db4124SIan Morris 		if (req) {
594537561f68SJerry Chu 			/* Return RST if ack_seq is invalid.
594637561f68SJerry Chu 			 * Note that RFC793 only says to generate a
594737561f68SJerry Chu 			 * DUPACK for it but for TCP Fast Open it seems
594837561f68SJerry Chu 			 * better to treat this case like TCP_SYN_RECV
594937561f68SJerry Chu 			 * above.
595037561f68SJerry Chu 			 */
595137561f68SJerry Chu 			if (!acceptable)
595237561f68SJerry Chu 				return 1;
5953168a8f58SJerry Chu 			/* We no longer need the request sock. */
5954168a8f58SJerry Chu 			reqsk_fastopen_remove(sk, req, false);
5955168a8f58SJerry Chu 			tcp_rearm_rto(sk);
5956168a8f58SJerry Chu 		}
5957c48b22daSJoe Perches 		if (tp->snd_una != tp->write_seq)
5958c48b22daSJoe Perches 			break;
59595110effeSDavid S. Miller 
59601da177e4SLinus Torvalds 		tcp_set_state(sk, TCP_FIN_WAIT2);
59611da177e4SLinus Torvalds 		sk->sk_shutdown |= SEND_SHUTDOWN;
59625110effeSDavid S. Miller 
59635110effeSDavid S. Miller 		dst = __sk_dst_get(sk);
59645110effeSDavid S. Miller 		if (dst)
59655110effeSDavid S. Miller 			dst_confirm(dst);
59661da177e4SLinus Torvalds 
59671f6afc81SEric Dumazet 		if (!sock_flag(sk, SOCK_DEAD)) {
59681da177e4SLinus Torvalds 			/* Wake up lingering close() */
59691da177e4SLinus Torvalds 			sk->sk_state_change(sk);
5970c48b22daSJoe Perches 			break;
5971c48b22daSJoe Perches 		}
59721da177e4SLinus Torvalds 
59731da177e4SLinus Torvalds 		if (tp->linger2 < 0 ||
59741da177e4SLinus Torvalds 		    (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
59751da177e4SLinus Torvalds 		     after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) {
59761da177e4SLinus Torvalds 			tcp_done(sk);
5977de0744afSPavel Emelyanov 			NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
59781da177e4SLinus Torvalds 			return 1;
59791da177e4SLinus Torvalds 		}
59801da177e4SLinus Torvalds 
5981463c84b9SArnaldo Carvalho de Melo 		tmo = tcp_fin_time(sk);
59821da177e4SLinus Torvalds 		if (tmo > TCP_TIMEWAIT_LEN) {
5983463c84b9SArnaldo Carvalho de Melo 			inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
59841da177e4SLinus Torvalds 		} else if (th->fin || sock_owned_by_user(sk)) {
59851da177e4SLinus Torvalds 			/* Bad case. We could lose such FIN otherwise.
59861da177e4SLinus Torvalds 			 * It is not a big problem, but it looks confusing
59871da177e4SLinus Torvalds 			 * and not so rare event. We still can lose it now,
59881da177e4SLinus Torvalds 			 * if it spins in bh_lock_sock(), but it is really
59891da177e4SLinus Torvalds 			 * marginal case.
59901da177e4SLinus Torvalds 			 */
5991463c84b9SArnaldo Carvalho de Melo 			inet_csk_reset_keepalive_timer(sk, tmo);
59921da177e4SLinus Torvalds 		} else {
59931da177e4SLinus Torvalds 			tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
59941da177e4SLinus Torvalds 			goto discard;
59951da177e4SLinus Torvalds 		}
59961da177e4SLinus Torvalds 		break;
5997c48b22daSJoe Perches 	}
59981da177e4SLinus Torvalds 
59991da177e4SLinus Torvalds 	case TCP_CLOSING:
60001da177e4SLinus Torvalds 		if (tp->snd_una == tp->write_seq) {
60011da177e4SLinus Torvalds 			tcp_time_wait(sk, TCP_TIME_WAIT, 0);
60021da177e4SLinus Torvalds 			goto discard;
60031da177e4SLinus Torvalds 		}
60041da177e4SLinus Torvalds 		break;
60051da177e4SLinus Torvalds 
60061da177e4SLinus Torvalds 	case TCP_LAST_ACK:
60071da177e4SLinus Torvalds 		if (tp->snd_una == tp->write_seq) {
60081da177e4SLinus Torvalds 			tcp_update_metrics(sk);
60091da177e4SLinus Torvalds 			tcp_done(sk);
60101da177e4SLinus Torvalds 			goto discard;
60111da177e4SLinus Torvalds 		}
60121da177e4SLinus Torvalds 		break;
60131da177e4SLinus Torvalds 	}
60141da177e4SLinus Torvalds 
60151da177e4SLinus Torvalds 	/* step 6: check the URG bit */
60161da177e4SLinus Torvalds 	tcp_urg(sk, skb, th);
60171da177e4SLinus Torvalds 
60181da177e4SLinus Torvalds 	/* step 7: process the segment text */
60191da177e4SLinus Torvalds 	switch (sk->sk_state) {
60201da177e4SLinus Torvalds 	case TCP_CLOSE_WAIT:
60211da177e4SLinus Torvalds 	case TCP_CLOSING:
60221da177e4SLinus Torvalds 	case TCP_LAST_ACK:
60231da177e4SLinus Torvalds 		if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
60241da177e4SLinus Torvalds 			break;
60251da177e4SLinus Torvalds 	case TCP_FIN_WAIT1:
60261da177e4SLinus Torvalds 	case TCP_FIN_WAIT2:
60271da177e4SLinus Torvalds 		/* RFC 793 says to queue data in these states,
60281da177e4SLinus Torvalds 		 * RFC 1122 says we MUST send a reset.
60291da177e4SLinus Torvalds 		 * BSD 4.4 also does reset.
60301da177e4SLinus Torvalds 		 */
60311da177e4SLinus Torvalds 		if (sk->sk_shutdown & RCV_SHUTDOWN) {
60321da177e4SLinus Torvalds 			if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
60331da177e4SLinus Torvalds 			    after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
6034de0744afSPavel Emelyanov 				NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
60351da177e4SLinus Torvalds 				tcp_reset(sk);
60361da177e4SLinus Torvalds 				return 1;
60371da177e4SLinus Torvalds 			}
60381da177e4SLinus Torvalds 		}
60391da177e4SLinus Torvalds 		/* Fall through */
60401da177e4SLinus Torvalds 	case TCP_ESTABLISHED:
60411da177e4SLinus Torvalds 		tcp_data_queue(sk, skb);
60421da177e4SLinus Torvalds 		queued = 1;
60431da177e4SLinus Torvalds 		break;
60441da177e4SLinus Torvalds 	}
60451da177e4SLinus Torvalds 
60461da177e4SLinus Torvalds 	/* tcp_data could move socket to TIME-WAIT */
60471da177e4SLinus Torvalds 	if (sk->sk_state != TCP_CLOSE) {
60489e412ba7SIlpo Järvinen 		tcp_data_snd_check(sk);
60491da177e4SLinus Torvalds 		tcp_ack_snd_check(sk);
60501da177e4SLinus Torvalds 	}
60511da177e4SLinus Torvalds 
60521da177e4SLinus Torvalds 	if (!queued) {
60531da177e4SLinus Torvalds discard:
60541da177e4SLinus Torvalds 		__kfree_skb(skb);
60551da177e4SLinus Torvalds 	}
60561da177e4SLinus Torvalds 	return 0;
60571da177e4SLinus Torvalds }
60581da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_rcv_state_process);
60591fb6f159SOctavian Purdila 
60601fb6f159SOctavian Purdila static inline void pr_drop_req(struct request_sock *req, __u16 port, int family)
60611fb6f159SOctavian Purdila {
60621fb6f159SOctavian Purdila 	struct inet_request_sock *ireq = inet_rsk(req);
60631fb6f159SOctavian Purdila 
60641fb6f159SOctavian Purdila 	if (family == AF_INET)
6065ba7a46f1SJoe Perches 		net_dbg_ratelimited("drop open request from %pI4/%u\n",
60661fb6f159SOctavian Purdila 				    &ireq->ir_rmt_addr, port);
60674135ab82SOctavian Purdila #if IS_ENABLED(CONFIG_IPV6)
60684135ab82SOctavian Purdila 	else if (family == AF_INET6)
6069ba7a46f1SJoe Perches 		net_dbg_ratelimited("drop open request from %pI6/%u\n",
60701fb6f159SOctavian Purdila 				    &ireq->ir_v6_rmt_addr, port);
60714135ab82SOctavian Purdila #endif
60721fb6f159SOctavian Purdila }
60731fb6f159SOctavian Purdila 
6074d82bd122SFlorian Westphal /* RFC3168 : 6.1.1 SYN packets must not have ECT/ECN bits set
6075d82bd122SFlorian Westphal  *
6076d82bd122SFlorian Westphal  * If we receive a SYN packet with these bits set, it means a
6077d82bd122SFlorian Westphal  * network is playing bad games with TOS bits. In order to
6078d82bd122SFlorian Westphal  * avoid possible false congestion notifications, we disable
6079f4e715c3Sstephen hemminger  * TCP ECN negotiation.
6080d82bd122SFlorian Westphal  *
6081d82bd122SFlorian Westphal  * Exception: tcp_ca wants ECN. This is required for DCTCP
6082843c2fdfSFlorian Westphal  * congestion control: Linux DCTCP asserts ECT on all packets,
6083843c2fdfSFlorian Westphal  * including SYN, which is most optimal solution; however,
6084843c2fdfSFlorian Westphal  * others, such as FreeBSD do not.
6085d82bd122SFlorian Westphal  */
6086d82bd122SFlorian Westphal static void tcp_ecn_create_request(struct request_sock *req,
6087d82bd122SFlorian Westphal 				   const struct sk_buff *skb,
6088f7b3bec6SFlorian Westphal 				   const struct sock *listen_sk,
6089f7b3bec6SFlorian Westphal 				   const struct dst_entry *dst)
6090d82bd122SFlorian Westphal {
6091d82bd122SFlorian Westphal 	const struct tcphdr *th = tcp_hdr(skb);
6092d82bd122SFlorian Westphal 	const struct net *net = sock_net(listen_sk);
6093d82bd122SFlorian Westphal 	bool th_ecn = th->ece && th->cwr;
6094843c2fdfSFlorian Westphal 	bool ect, ecn_ok;
6095c3a8d947SDaniel Borkmann 	u32 ecn_ok_dst;
6096d82bd122SFlorian Westphal 
6097d82bd122SFlorian Westphal 	if (!th_ecn)
6098d82bd122SFlorian Westphal 		return;
6099d82bd122SFlorian Westphal 
6100d82bd122SFlorian Westphal 	ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
6101c3a8d947SDaniel Borkmann 	ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
6102c3a8d947SDaniel Borkmann 	ecn_ok = net->ipv4.sysctl_tcp_ecn || ecn_ok_dst;
6103d82bd122SFlorian Westphal 
6104c3a8d947SDaniel Borkmann 	if ((!ect && ecn_ok) || tcp_ca_needs_ecn(listen_sk) ||
6105c3a8d947SDaniel Borkmann 	    (ecn_ok_dst & DST_FEATURE_ECN_CA))
6106d82bd122SFlorian Westphal 		inet_rsk(req)->ecn_ok = 1;
6107d82bd122SFlorian Westphal }
6108d82bd122SFlorian Westphal 
61091bfc4438SEric Dumazet static void tcp_openreq_init(struct request_sock *req,
61101bfc4438SEric Dumazet 			     const struct tcp_options_received *rx_opt,
61111bfc4438SEric Dumazet 			     struct sk_buff *skb, const struct sock *sk)
61121bfc4438SEric Dumazet {
61131bfc4438SEric Dumazet 	struct inet_request_sock *ireq = inet_rsk(req);
61141bfc4438SEric Dumazet 
6115ed53d0abSEric Dumazet 	req->rsk_rcv_wnd = 0;		/* So that tcp_send_synack() knows! */
61161bfc4438SEric Dumazet 	req->cookie_ts = 0;
61171bfc4438SEric Dumazet 	tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
61181bfc4438SEric Dumazet 	tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
61190f1c28aeSYuchung Cheng 	skb_mstamp_get(&tcp_rsk(req)->snt_synack);
61201bfc4438SEric Dumazet 	tcp_rsk(req)->last_oow_ack_time = 0;
61211bfc4438SEric Dumazet 	req->mss = rx_opt->mss_clamp;
61221bfc4438SEric Dumazet 	req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
61231bfc4438SEric Dumazet 	ireq->tstamp_ok = rx_opt->tstamp_ok;
61241bfc4438SEric Dumazet 	ireq->sack_ok = rx_opt->sack_ok;
61251bfc4438SEric Dumazet 	ireq->snd_wscale = rx_opt->snd_wscale;
61261bfc4438SEric Dumazet 	ireq->wscale_ok = rx_opt->wscale_ok;
61271bfc4438SEric Dumazet 	ireq->acked = 0;
61281bfc4438SEric Dumazet 	ireq->ecn_ok = 0;
61291bfc4438SEric Dumazet 	ireq->ir_rmt_port = tcp_hdr(skb)->source;
61301bfc4438SEric Dumazet 	ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
61311bfc4438SEric Dumazet 	ireq->ir_mark = inet_request_mark(sk, skb);
61321bfc4438SEric Dumazet }
61331bfc4438SEric Dumazet 
6134e49bb337SEric Dumazet struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
6135a1a5344dSEric Dumazet 				      struct sock *sk_listener,
6136a1a5344dSEric Dumazet 				      bool attach_listener)
6137e49bb337SEric Dumazet {
6138a1a5344dSEric Dumazet 	struct request_sock *req = reqsk_alloc(ops, sk_listener,
6139a1a5344dSEric Dumazet 					       attach_listener);
6140e49bb337SEric Dumazet 
6141e49bb337SEric Dumazet 	if (req) {
6142e49bb337SEric Dumazet 		struct inet_request_sock *ireq = inet_rsk(req);
6143e49bb337SEric Dumazet 
6144e49bb337SEric Dumazet 		kmemcheck_annotate_bitfield(ireq, flags);
6145e49bb337SEric Dumazet 		ireq->opt = NULL;
6146e49bb337SEric Dumazet 		atomic64_set(&ireq->ir_cookie, 0);
6147e49bb337SEric Dumazet 		ireq->ireq_state = TCP_NEW_SYN_RECV;
6148e49bb337SEric Dumazet 		write_pnet(&ireq->ireq_net, sock_net(sk_listener));
61490144a81cSEric Dumazet 		ireq->ireq_family = sk_listener->sk_family;
6150e49bb337SEric Dumazet 	}
6151e49bb337SEric Dumazet 
6152e49bb337SEric Dumazet 	return req;
6153e49bb337SEric Dumazet }
6154e49bb337SEric Dumazet EXPORT_SYMBOL(inet_reqsk_alloc);
6155e49bb337SEric Dumazet 
615641d25fe0SEric Dumazet /*
615741d25fe0SEric Dumazet  * Return true if a syncookie should be sent
615841d25fe0SEric Dumazet  */
61592985aaacSEric Dumazet static bool tcp_syn_flood_action(const struct sock *sk,
616041d25fe0SEric Dumazet 				 const struct sk_buff *skb,
616141d25fe0SEric Dumazet 				 const char *proto)
616241d25fe0SEric Dumazet {
61638d2675f1SEric Dumazet 	struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
616441d25fe0SEric Dumazet 	const char *msg = "Dropping request";
616541d25fe0SEric Dumazet 	bool want_cookie = false;
6166*12ed8244SNikolay Borisov 	struct net *net = sock_net(sk);
616741d25fe0SEric Dumazet 
616841d25fe0SEric Dumazet #ifdef CONFIG_SYN_COOKIES
6169*12ed8244SNikolay Borisov 	if (net->ipv4.sysctl_tcp_syncookies) {
617041d25fe0SEric Dumazet 		msg = "Sending cookies";
617141d25fe0SEric Dumazet 		want_cookie = true;
617241d25fe0SEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
617341d25fe0SEric Dumazet 	} else
617441d25fe0SEric Dumazet #endif
617541d25fe0SEric Dumazet 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
617641d25fe0SEric Dumazet 
61778d2675f1SEric Dumazet 	if (!queue->synflood_warned &&
6178*12ed8244SNikolay Borisov 	    net->ipv4.sysctl_tcp_syncookies != 2 &&
61798d2675f1SEric Dumazet 	    xchg(&queue->synflood_warned, 1) == 0)
618041d25fe0SEric Dumazet 		pr_info("%s: Possible SYN flooding on port %d. %s.  Check SNMP counters.\n",
618141d25fe0SEric Dumazet 			proto, ntohs(tcp_hdr(skb)->dest), msg);
61822985aaacSEric Dumazet 
618341d25fe0SEric Dumazet 	return want_cookie;
618441d25fe0SEric Dumazet }
618541d25fe0SEric Dumazet 
6186cd8ae852SEric Dumazet static void tcp_reqsk_record_syn(const struct sock *sk,
6187cd8ae852SEric Dumazet 				 struct request_sock *req,
6188cd8ae852SEric Dumazet 				 const struct sk_buff *skb)
6189cd8ae852SEric Dumazet {
6190cd8ae852SEric Dumazet 	if (tcp_sk(sk)->save_syn) {
6191cd8ae852SEric Dumazet 		u32 len = skb_network_header_len(skb) + tcp_hdrlen(skb);
6192cd8ae852SEric Dumazet 		u32 *copy;
6193cd8ae852SEric Dumazet 
6194cd8ae852SEric Dumazet 		copy = kmalloc(len + sizeof(u32), GFP_ATOMIC);
6195cd8ae852SEric Dumazet 		if (copy) {
6196cd8ae852SEric Dumazet 			copy[0] = len;
6197cd8ae852SEric Dumazet 			memcpy(&copy[1], skb_network_header(skb), len);
6198cd8ae852SEric Dumazet 			req->saved_syn = copy;
6199cd8ae852SEric Dumazet 		}
6200cd8ae852SEric Dumazet 	}
6201cd8ae852SEric Dumazet }
6202cd8ae852SEric Dumazet 
62031fb6f159SOctavian Purdila int tcp_conn_request(struct request_sock_ops *rsk_ops,
62041fb6f159SOctavian Purdila 		     const struct tcp_request_sock_ops *af_ops,
62051fb6f159SOctavian Purdila 		     struct sock *sk, struct sk_buff *skb)
62061fb6f159SOctavian Purdila {
62071fb6f159SOctavian Purdila 	struct tcp_fastopen_cookie foc = { .len = -1 };
62087c85af88SEric Dumazet 	__u32 isn = TCP_SKB_CB(skb)->tcp_tw_isn;
62097c85af88SEric Dumazet 	struct tcp_options_received tmp_opt;
62107c85af88SEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
6211*12ed8244SNikolay Borisov 	struct net *net = sock_net(sk);
62127c85af88SEric Dumazet 	struct sock *fastopen_sk = NULL;
62137c85af88SEric Dumazet 	struct dst_entry *dst = NULL;
62147c85af88SEric Dumazet 	struct request_sock *req;
62157c85af88SEric Dumazet 	bool want_cookie = false;
62167c85af88SEric Dumazet 	struct flowi fl;
62171fb6f159SOctavian Purdila 
62181fb6f159SOctavian Purdila 	/* TW buckets are converted to open requests without
62191fb6f159SOctavian Purdila 	 * limitations, they conserve resources and peer is
62201fb6f159SOctavian Purdila 	 * evidently real one.
62211fb6f159SOctavian Purdila 	 */
6222*12ed8244SNikolay Borisov 	if ((net->ipv4.sysctl_tcp_syncookies == 2 ||
62231fb6f159SOctavian Purdila 	     inet_csk_reqsk_queue_is_full(sk)) && !isn) {
62241fb6f159SOctavian Purdila 		want_cookie = tcp_syn_flood_action(sk, skb, rsk_ops->slab_name);
62251fb6f159SOctavian Purdila 		if (!want_cookie)
62261fb6f159SOctavian Purdila 			goto drop;
62271fb6f159SOctavian Purdila 	}
62281fb6f159SOctavian Purdila 
62291fb6f159SOctavian Purdila 
62301fb6f159SOctavian Purdila 	/* Accept backlog is full. If we have already queued enough
62311fb6f159SOctavian Purdila 	 * of warm entries in syn queue, drop request. It is better than
62321fb6f159SOctavian Purdila 	 * clogging syn queue with openreqs with exponentially increasing
62331fb6f159SOctavian Purdila 	 * timeout.
62341fb6f159SOctavian Purdila 	 */
62351fb6f159SOctavian Purdila 	if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) {
62361fb6f159SOctavian Purdila 		NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
62371fb6f159SOctavian Purdila 		goto drop;
62381fb6f159SOctavian Purdila 	}
62391fb6f159SOctavian Purdila 
6240a1a5344dSEric Dumazet 	req = inet_reqsk_alloc(rsk_ops, sk, !want_cookie);
62411fb6f159SOctavian Purdila 	if (!req)
62421fb6f159SOctavian Purdila 		goto drop;
62431fb6f159SOctavian Purdila 
62441fb6f159SOctavian Purdila 	tcp_rsk(req)->af_specific = af_ops;
62451fb6f159SOctavian Purdila 
62461fb6f159SOctavian Purdila 	tcp_clear_options(&tmp_opt);
62471fb6f159SOctavian Purdila 	tmp_opt.mss_clamp = af_ops->mss_clamp;
62481fb6f159SOctavian Purdila 	tmp_opt.user_mss  = tp->rx_opt.user_mss;
62491fb6f159SOctavian Purdila 	tcp_parse_options(skb, &tmp_opt, 0, want_cookie ? NULL : &foc);
62501fb6f159SOctavian Purdila 
62511fb6f159SOctavian Purdila 	if (want_cookie && !tmp_opt.saw_tstamp)
62521fb6f159SOctavian Purdila 		tcp_clear_options(&tmp_opt);
62531fb6f159SOctavian Purdila 
62541fb6f159SOctavian Purdila 	tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
62551fb6f159SOctavian Purdila 	tcp_openreq_init(req, &tmp_opt, skb, sk);
62561fb6f159SOctavian Purdila 
625716f86165SEric Dumazet 	/* Note: tcp_v6_init_req() might override ir_iif for link locals */
62586dd9a14eSDavid Ahern 	inet_rsk(req)->ir_iif = inet_request_bound_dev_if(sk, skb);
625916f86165SEric Dumazet 
62601fb6f159SOctavian Purdila 	af_ops->init_req(req, sk, skb);
62611fb6f159SOctavian Purdila 
62621fb6f159SOctavian Purdila 	if (security_inet_conn_request(sk, skb, req))
62631fb6f159SOctavian Purdila 		goto drop_and_free;
62641fb6f159SOctavian Purdila 
6265f7b3bec6SFlorian Westphal 	if (!want_cookie && !isn) {
62661fb6f159SOctavian Purdila 		/* VJ's idea. We save last timestamp seen
62671fb6f159SOctavian Purdila 		 * from the destination in peer table, when entering
62681fb6f159SOctavian Purdila 		 * state TIME-WAIT, and check against it before
62691fb6f159SOctavian Purdila 		 * accepting new connection request.
62701fb6f159SOctavian Purdila 		 *
62711fb6f159SOctavian Purdila 		 * If "isn" is not zero, this request hit alive
62721fb6f159SOctavian Purdila 		 * timewait bucket, so that all the necessary checks
62731fb6f159SOctavian Purdila 		 * are made in the function processing timewait state.
62741fb6f159SOctavian Purdila 		 */
6275a26552afSHannes Frederic Sowa 		if (tcp_death_row.sysctl_tw_recycle) {
62761fb6f159SOctavian Purdila 			bool strict;
62771fb6f159SOctavian Purdila 
62781fb6f159SOctavian Purdila 			dst = af_ops->route_req(sk, &fl, req, &strict);
6279a26552afSHannes Frederic Sowa 
62801fb6f159SOctavian Purdila 			if (dst && strict &&
6281a26552afSHannes Frederic Sowa 			    !tcp_peer_is_proven(req, dst, true,
6282a26552afSHannes Frederic Sowa 						tmp_opt.saw_tstamp)) {
62831fb6f159SOctavian Purdila 				NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
62841fb6f159SOctavian Purdila 				goto drop_and_release;
62851fb6f159SOctavian Purdila 			}
62861fb6f159SOctavian Purdila 		}
62871fb6f159SOctavian Purdila 		/* Kill the following clause, if you dislike this way. */
6288*12ed8244SNikolay Borisov 		else if (!net->ipv4.sysctl_tcp_syncookies &&
62891fb6f159SOctavian Purdila 			 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
62901fb6f159SOctavian Purdila 			  (sysctl_max_syn_backlog >> 2)) &&
6291a26552afSHannes Frederic Sowa 			 !tcp_peer_is_proven(req, dst, false,
6292a26552afSHannes Frederic Sowa 					     tmp_opt.saw_tstamp)) {
62931fb6f159SOctavian Purdila 			/* Without syncookies last quarter of
62941fb6f159SOctavian Purdila 			 * backlog is filled with destinations,
62951fb6f159SOctavian Purdila 			 * proven to be alive.
62961fb6f159SOctavian Purdila 			 * It means that we continue to communicate
62971fb6f159SOctavian Purdila 			 * to destinations, already remembered
62981fb6f159SOctavian Purdila 			 * to the moment of synflood.
62991fb6f159SOctavian Purdila 			 */
63001fb6f159SOctavian Purdila 			pr_drop_req(req, ntohs(tcp_hdr(skb)->source),
63011fb6f159SOctavian Purdila 				    rsk_ops->family);
63021fb6f159SOctavian Purdila 			goto drop_and_release;
63031fb6f159SOctavian Purdila 		}
63041fb6f159SOctavian Purdila 
63051fb6f159SOctavian Purdila 		isn = af_ops->init_seq(skb);
63061fb6f159SOctavian Purdila 	}
63071fb6f159SOctavian Purdila 	if (!dst) {
63081fb6f159SOctavian Purdila 		dst = af_ops->route_req(sk, &fl, req, NULL);
63091fb6f159SOctavian Purdila 		if (!dst)
63101fb6f159SOctavian Purdila 			goto drop_and_free;
63111fb6f159SOctavian Purdila 	}
63121fb6f159SOctavian Purdila 
6313f7b3bec6SFlorian Westphal 	tcp_ecn_create_request(req, skb, sk, dst);
6314f7b3bec6SFlorian Westphal 
6315f7b3bec6SFlorian Westphal 	if (want_cookie) {
6316f7b3bec6SFlorian Westphal 		isn = cookie_init_sequence(af_ops, sk, skb, &req->mss);
6317f7b3bec6SFlorian Westphal 		req->cookie_ts = tmp_opt.tstamp_ok;
6318f7b3bec6SFlorian Westphal 		if (!tmp_opt.tstamp_ok)
6319f7b3bec6SFlorian Westphal 			inet_rsk(req)->ecn_ok = 0;
6320f7b3bec6SFlorian Westphal 	}
6321f7b3bec6SFlorian Westphal 
63221fb6f159SOctavian Purdila 	tcp_rsk(req)->snt_isn = isn;
632358d607d3SEric Dumazet 	tcp_rsk(req)->txhash = net_tx_rndhash();
63241fb6f159SOctavian Purdila 	tcp_openreq_init_rwin(req, sk, dst);
6325ca6fb065SEric Dumazet 	if (!want_cookie) {
6326ca6fb065SEric Dumazet 		tcp_reqsk_record_syn(sk, req, skb);
63277656d842SEric Dumazet 		fastopen_sk = tcp_try_fastopen(sk, skb, req, &foc, dst);
6328ca6fb065SEric Dumazet 	}
63297c85af88SEric Dumazet 	if (fastopen_sk) {
6330ca6fb065SEric Dumazet 		af_ops->send_synack(fastopen_sk, dst, &fl, req,
6331dc6ef6beSEric Dumazet 				    &foc, false);
63327656d842SEric Dumazet 		/* Add the child socket directly into the accept queue */
63337656d842SEric Dumazet 		inet_csk_reqsk_queue_add(sk, req, fastopen_sk);
63347656d842SEric Dumazet 		sk->sk_data_ready(sk);
63357656d842SEric Dumazet 		bh_unlock_sock(fastopen_sk);
63367c85af88SEric Dumazet 		sock_put(fastopen_sk);
63377c85af88SEric Dumazet 	} else {
63389439ce00SEric Dumazet 		tcp_rsk(req)->tfo_listener = false;
6339ca6fb065SEric Dumazet 		if (!want_cookie)
6340079096f1SEric Dumazet 			inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
6341ca6fb065SEric Dumazet 		af_ops->send_synack(sk, dst, &fl, req,
6342dc6ef6beSEric Dumazet 				    &foc, !want_cookie);
6343ca6fb065SEric Dumazet 		if (want_cookie)
6344ca6fb065SEric Dumazet 			goto drop_and_free;
63451fb6f159SOctavian Purdila 	}
6346ca6fb065SEric Dumazet 	reqsk_put(req);
63471fb6f159SOctavian Purdila 	return 0;
63481fb6f159SOctavian Purdila 
63491fb6f159SOctavian Purdila drop_and_release:
63501fb6f159SOctavian Purdila 	dst_release(dst);
63511fb6f159SOctavian Purdila drop_and_free:
63521fb6f159SOctavian Purdila 	reqsk_free(req);
63531fb6f159SOctavian Purdila drop:
63541fb6f159SOctavian Purdila 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
63551fb6f159SOctavian Purdila 	return 0;
63561fb6f159SOctavian Purdila }
63571fb6f159SOctavian Purdila EXPORT_SYMBOL(tcp_conn_request);
6358