xref: /OK3568_Linux_fs/kernel/net/ipv4/tcp_output.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4*4882a593Smuzhiyun  *		operating system.  INET is implemented using the  BSD Socket
5*4882a593Smuzhiyun  *		interface as the means of communication with the user level.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *		Implementation of the Transmission Control Protocol(TCP).
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Authors:	Ross Biro
10*4882a593Smuzhiyun  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
11*4882a593Smuzhiyun  *		Mark Evans, <evansmp@uhura.aston.ac.uk>
12*4882a593Smuzhiyun  *		Corey Minyard <wf-rch!minyard@relay.EU.net>
13*4882a593Smuzhiyun  *		Florian La Roche, <flla@stud.uni-sb.de>
14*4882a593Smuzhiyun  *		Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
15*4882a593Smuzhiyun  *		Linus Torvalds, <torvalds@cs.helsinki.fi>
16*4882a593Smuzhiyun  *		Alan Cox, <gw4pts@gw4pts.ampr.org>
17*4882a593Smuzhiyun  *		Matthew Dillon, <dillon@apollo.west.oic.com>
18*4882a593Smuzhiyun  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
19*4882a593Smuzhiyun  *		Jorge Cwik, <jorge@laser.satlink.net>
20*4882a593Smuzhiyun  */
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun  * Changes:	Pedro Roque	:	Retransmit queue handled by TCP.
24*4882a593Smuzhiyun  *				:	Fragmentation on mtu decrease
25*4882a593Smuzhiyun  *				:	Segment collapse on retransmit
26*4882a593Smuzhiyun  *				:	AF independence
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  *		Linus Torvalds	:	send_delayed_ack
29*4882a593Smuzhiyun  *		David S. Miller	:	Charge memory using the right skb
30*4882a593Smuzhiyun  *					during syn/ack processing.
31*4882a593Smuzhiyun  *		David S. Miller :	Output engine completely rewritten.
32*4882a593Smuzhiyun  *		Andrea Arcangeli:	SYNACK carry ts_recent in tsecr.
33*4882a593Smuzhiyun  *		Cacophonix Gaul :	draft-minshall-nagle-01
34*4882a593Smuzhiyun  *		J Hadi Salim	:	ECN support
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun #define pr_fmt(fmt) "TCP: " fmt
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #include <net/tcp.h>
41*4882a593Smuzhiyun #include <net/mptcp.h>
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #include <linux/compiler.h>
44*4882a593Smuzhiyun #include <linux/gfp.h>
45*4882a593Smuzhiyun #include <linux/module.h>
46*4882a593Smuzhiyun #include <linux/static_key.h>
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <trace/events/tcp.h>
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /* Refresh clocks of a TCP socket,
51*4882a593Smuzhiyun  * ensuring monotically increasing values.
52*4882a593Smuzhiyun  */
tcp_mstamp_refresh(struct tcp_sock * tp)53*4882a593Smuzhiyun void tcp_mstamp_refresh(struct tcp_sock *tp)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun 	u64 val = tcp_clock_ns();
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	tp->tcp_clock_cache = val;
58*4882a593Smuzhiyun 	tp->tcp_mstamp = div_u64(val, NSEC_PER_USEC);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
62*4882a593Smuzhiyun 			   int push_one, gfp_t gfp);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun /* Account for new data that has been sent to the network. */
tcp_event_new_data_sent(struct sock * sk,struct sk_buff * skb)65*4882a593Smuzhiyun static void tcp_event_new_data_sent(struct sock *sk, struct sk_buff *skb)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
68*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
69*4882a593Smuzhiyun 	unsigned int prior_packets = tp->packets_out;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	WRITE_ONCE(tp->snd_nxt, TCP_SKB_CB(skb)->end_seq);
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	__skb_unlink(skb, &sk->sk_write_queue);
74*4882a593Smuzhiyun 	tcp_rbtree_insert(&sk->tcp_rtx_queue, skb);
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	if (tp->highest_sack == NULL)
77*4882a593Smuzhiyun 		tp->highest_sack = skb;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	tp->packets_out += tcp_skb_pcount(skb);
80*4882a593Smuzhiyun 	if (!prior_packets || icsk->icsk_pending == ICSK_TIME_LOSS_PROBE)
81*4882a593Smuzhiyun 		tcp_rearm_rto(sk);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPORIGDATASENT,
84*4882a593Smuzhiyun 		      tcp_skb_pcount(skb));
85*4882a593Smuzhiyun 	tcp_check_space(sk);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /* SND.NXT, if window was not shrunk or the amount of shrunk was less than one
89*4882a593Smuzhiyun  * window scaling factor due to loss of precision.
90*4882a593Smuzhiyun  * If window has been shrunk, what should we make? It is not clear at all.
91*4882a593Smuzhiyun  * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-(
92*4882a593Smuzhiyun  * Anything in between SND.UNA...SND.UNA+SND.WND also can be already
93*4882a593Smuzhiyun  * invalid. OK, let's make this for now:
94*4882a593Smuzhiyun  */
tcp_acceptable_seq(const struct sock * sk)95*4882a593Smuzhiyun static inline __u32 tcp_acceptable_seq(const struct sock *sk)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	const struct tcp_sock *tp = tcp_sk(sk);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	if (!before(tcp_wnd_end(tp), tp->snd_nxt) ||
100*4882a593Smuzhiyun 	    (tp->rx_opt.wscale_ok &&
101*4882a593Smuzhiyun 	     ((tp->snd_nxt - tcp_wnd_end(tp)) < (1 << tp->rx_opt.rcv_wscale))))
102*4882a593Smuzhiyun 		return tp->snd_nxt;
103*4882a593Smuzhiyun 	else
104*4882a593Smuzhiyun 		return tcp_wnd_end(tp);
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /* Calculate mss to advertise in SYN segment.
108*4882a593Smuzhiyun  * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that:
109*4882a593Smuzhiyun  *
110*4882a593Smuzhiyun  * 1. It is independent of path mtu.
111*4882a593Smuzhiyun  * 2. Ideally, it is maximal possible segment size i.e. 65535-40.
112*4882a593Smuzhiyun  * 3. For IPv4 it is reasonable to calculate it from maximal MTU of
113*4882a593Smuzhiyun  *    attached devices, because some buggy hosts are confused by
114*4882a593Smuzhiyun  *    large MSS.
115*4882a593Smuzhiyun  * 4. We do not make 3, we advertise MSS, calculated from first
116*4882a593Smuzhiyun  *    hop device mtu, but allow to raise it to ip_rt_min_advmss.
117*4882a593Smuzhiyun  *    This may be overridden via information stored in routing table.
118*4882a593Smuzhiyun  * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible,
119*4882a593Smuzhiyun  *    probably even Jumbo".
120*4882a593Smuzhiyun  */
tcp_advertise_mss(struct sock * sk)121*4882a593Smuzhiyun static __u16 tcp_advertise_mss(struct sock *sk)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
124*4882a593Smuzhiyun 	const struct dst_entry *dst = __sk_dst_get(sk);
125*4882a593Smuzhiyun 	int mss = tp->advmss;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	if (dst) {
128*4882a593Smuzhiyun 		unsigned int metric = dst_metric_advmss(dst);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 		if (metric < mss) {
131*4882a593Smuzhiyun 			mss = metric;
132*4882a593Smuzhiyun 			tp->advmss = mss;
133*4882a593Smuzhiyun 		}
134*4882a593Smuzhiyun 	}
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	return (__u16)mss;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /* RFC2861. Reset CWND after idle period longer RTO to "restart window".
140*4882a593Smuzhiyun  * This is the first part of cwnd validation mechanism.
141*4882a593Smuzhiyun  */
tcp_cwnd_restart(struct sock * sk,s32 delta)142*4882a593Smuzhiyun void tcp_cwnd_restart(struct sock *sk, s32 delta)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
145*4882a593Smuzhiyun 	u32 restart_cwnd = tcp_init_cwnd(tp, __sk_dst_get(sk));
146*4882a593Smuzhiyun 	u32 cwnd = tp->snd_cwnd;
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	tcp_ca_event(sk, CA_EVENT_CWND_RESTART);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	tp->snd_ssthresh = tcp_current_ssthresh(sk);
151*4882a593Smuzhiyun 	restart_cwnd = min(restart_cwnd, cwnd);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd)
154*4882a593Smuzhiyun 		cwnd >>= 1;
155*4882a593Smuzhiyun 	tp->snd_cwnd = max(cwnd, restart_cwnd);
156*4882a593Smuzhiyun 	tp->snd_cwnd_stamp = tcp_jiffies32;
157*4882a593Smuzhiyun 	tp->snd_cwnd_used = 0;
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun /* Congestion state accounting after a packet has been sent. */
tcp_event_data_sent(struct tcp_sock * tp,struct sock * sk)161*4882a593Smuzhiyun static void tcp_event_data_sent(struct tcp_sock *tp,
162*4882a593Smuzhiyun 				struct sock *sk)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
165*4882a593Smuzhiyun 	const u32 now = tcp_jiffies32;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	if (tcp_packets_in_flight(tp) == 0)
168*4882a593Smuzhiyun 		tcp_ca_event(sk, CA_EVENT_TX_START);
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	tp->lsndtime = now;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	/* If it is a reply for ato after last received
173*4882a593Smuzhiyun 	 * packet, enter pingpong mode.
174*4882a593Smuzhiyun 	 */
175*4882a593Smuzhiyun 	if ((u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato)
176*4882a593Smuzhiyun 		inet_csk_enter_pingpong_mode(sk);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /* Account for an ACK we sent. */
tcp_event_ack_sent(struct sock * sk,unsigned int pkts,u32 rcv_nxt)180*4882a593Smuzhiyun static inline void tcp_event_ack_sent(struct sock *sk, unsigned int pkts,
181*4882a593Smuzhiyun 				      u32 rcv_nxt)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	if (unlikely(tp->compressed_ack)) {
186*4882a593Smuzhiyun 		NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPACKCOMPRESSED,
187*4882a593Smuzhiyun 			      tp->compressed_ack);
188*4882a593Smuzhiyun 		tp->compressed_ack = 0;
189*4882a593Smuzhiyun 		if (hrtimer_try_to_cancel(&tp->compressed_ack_timer) == 1)
190*4882a593Smuzhiyun 			__sock_put(sk);
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	if (unlikely(rcv_nxt != tp->rcv_nxt))
194*4882a593Smuzhiyun 		return;  /* Special ACK sent by DCTCP to reflect ECN */
195*4882a593Smuzhiyun 	tcp_dec_quickack_mode(sk, pkts);
196*4882a593Smuzhiyun 	inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun /* Determine a window scaling and initial window to offer.
200*4882a593Smuzhiyun  * Based on the assumption that the given amount of space
201*4882a593Smuzhiyun  * will be offered. Store the results in the tp structure.
202*4882a593Smuzhiyun  * NOTE: for smooth operation initial space offering should
203*4882a593Smuzhiyun  * be a multiple of mss if possible. We assume here that mss >= 1.
204*4882a593Smuzhiyun  * This MUST be enforced by all callers.
205*4882a593Smuzhiyun  */
tcp_select_initial_window(const struct sock * sk,int __space,__u32 mss,__u32 * rcv_wnd,__u32 * window_clamp,int wscale_ok,__u8 * rcv_wscale,__u32 init_rcv_wnd)206*4882a593Smuzhiyun void tcp_select_initial_window(const struct sock *sk, int __space, __u32 mss,
207*4882a593Smuzhiyun 			       __u32 *rcv_wnd, __u32 *window_clamp,
208*4882a593Smuzhiyun 			       int wscale_ok, __u8 *rcv_wscale,
209*4882a593Smuzhiyun 			       __u32 init_rcv_wnd)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun 	unsigned int space = (__space < 0 ? 0 : __space);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/* If no clamp set the clamp to the max possible scaled window */
214*4882a593Smuzhiyun 	if (*window_clamp == 0)
215*4882a593Smuzhiyun 		(*window_clamp) = (U16_MAX << TCP_MAX_WSCALE);
216*4882a593Smuzhiyun 	space = min(*window_clamp, space);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	/* Quantize space offering to a multiple of mss if possible. */
219*4882a593Smuzhiyun 	if (space > mss)
220*4882a593Smuzhiyun 		space = rounddown(space, mss);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	/* NOTE: offering an initial window larger than 32767
223*4882a593Smuzhiyun 	 * will break some buggy TCP stacks. If the admin tells us
224*4882a593Smuzhiyun 	 * it is likely we could be speaking with such a buggy stack
225*4882a593Smuzhiyun 	 * we will truncate our initial window offering to 32K-1
226*4882a593Smuzhiyun 	 * unless the remote has sent us a window scaling option,
227*4882a593Smuzhiyun 	 * which we interpret as a sign the remote TCP is not
228*4882a593Smuzhiyun 	 * misinterpreting the window field as a signed quantity.
229*4882a593Smuzhiyun 	 */
230*4882a593Smuzhiyun 	if (sock_net(sk)->ipv4.sysctl_tcp_workaround_signed_windows)
231*4882a593Smuzhiyun 		(*rcv_wnd) = min(space, MAX_TCP_WINDOW);
232*4882a593Smuzhiyun 	else
233*4882a593Smuzhiyun 		(*rcv_wnd) = min_t(u32, space, U16_MAX);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	if (init_rcv_wnd)
236*4882a593Smuzhiyun 		*rcv_wnd = min(*rcv_wnd, init_rcv_wnd * mss);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	*rcv_wscale = 0;
239*4882a593Smuzhiyun 	if (wscale_ok) {
240*4882a593Smuzhiyun 		/* Set window scaling on max possible window */
241*4882a593Smuzhiyun 		space = max_t(u32, space, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
242*4882a593Smuzhiyun 		space = max_t(u32, space, READ_ONCE(sysctl_rmem_max));
243*4882a593Smuzhiyun 		space = min_t(u32, space, *window_clamp);
244*4882a593Smuzhiyun 		*rcv_wscale = clamp_t(int, ilog2(space) - 15,
245*4882a593Smuzhiyun 				      0, TCP_MAX_WSCALE);
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun 	/* Set the clamp no higher than max representable value */
248*4882a593Smuzhiyun 	(*window_clamp) = min_t(__u32, U16_MAX << (*rcv_wscale), *window_clamp);
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun EXPORT_SYMBOL(tcp_select_initial_window);
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun /* Chose a new window to advertise, update state in tcp_sock for the
253*4882a593Smuzhiyun  * socket, and return result with RFC1323 scaling applied.  The return
254*4882a593Smuzhiyun  * value can be stuffed directly into th->window for an outgoing
255*4882a593Smuzhiyun  * frame.
256*4882a593Smuzhiyun  */
tcp_select_window(struct sock * sk)257*4882a593Smuzhiyun static u16 tcp_select_window(struct sock *sk)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
260*4882a593Smuzhiyun 	u32 old_win = tp->rcv_wnd;
261*4882a593Smuzhiyun 	u32 cur_win = tcp_receive_window(tp);
262*4882a593Smuzhiyun 	u32 new_win = __tcp_select_window(sk);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	/* Never shrink the offered window */
265*4882a593Smuzhiyun 	if (new_win < cur_win) {
266*4882a593Smuzhiyun 		/* Danger Will Robinson!
267*4882a593Smuzhiyun 		 * Don't update rcv_wup/rcv_wnd here or else
268*4882a593Smuzhiyun 		 * we will not be able to advertise a zero
269*4882a593Smuzhiyun 		 * window in time.  --DaveM
270*4882a593Smuzhiyun 		 *
271*4882a593Smuzhiyun 		 * Relax Will Robinson.
272*4882a593Smuzhiyun 		 */
273*4882a593Smuzhiyun 		if (new_win == 0)
274*4882a593Smuzhiyun 			NET_INC_STATS(sock_net(sk),
275*4882a593Smuzhiyun 				      LINUX_MIB_TCPWANTZEROWINDOWADV);
276*4882a593Smuzhiyun 		new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale);
277*4882a593Smuzhiyun 	}
278*4882a593Smuzhiyun 	tp->rcv_wnd = new_win;
279*4882a593Smuzhiyun 	tp->rcv_wup = tp->rcv_nxt;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/* Make sure we do not exceed the maximum possible
282*4882a593Smuzhiyun 	 * scaled window.
283*4882a593Smuzhiyun 	 */
284*4882a593Smuzhiyun 	if (!tp->rx_opt.rcv_wscale &&
285*4882a593Smuzhiyun 	    sock_net(sk)->ipv4.sysctl_tcp_workaround_signed_windows)
286*4882a593Smuzhiyun 		new_win = min(new_win, MAX_TCP_WINDOW);
287*4882a593Smuzhiyun 	else
288*4882a593Smuzhiyun 		new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale));
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/* RFC1323 scaling applied */
291*4882a593Smuzhiyun 	new_win >>= tp->rx_opt.rcv_wscale;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	/* If we advertise zero window, disable fast path. */
294*4882a593Smuzhiyun 	if (new_win == 0) {
295*4882a593Smuzhiyun 		tp->pred_flags = 0;
296*4882a593Smuzhiyun 		if (old_win)
297*4882a593Smuzhiyun 			NET_INC_STATS(sock_net(sk),
298*4882a593Smuzhiyun 				      LINUX_MIB_TCPTOZEROWINDOWADV);
299*4882a593Smuzhiyun 	} else if (old_win == 0) {
300*4882a593Smuzhiyun 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFROMZEROWINDOWADV);
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	return new_win;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun /* Packet ECN state for a SYN-ACK */
tcp_ecn_send_synack(struct sock * sk,struct sk_buff * skb)307*4882a593Smuzhiyun static void tcp_ecn_send_synack(struct sock *sk, struct sk_buff *skb)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	const struct tcp_sock *tp = tcp_sk(sk);
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_CWR;
312*4882a593Smuzhiyun 	if (!(tp->ecn_flags & TCP_ECN_OK))
313*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_ECE;
314*4882a593Smuzhiyun 	else if (tcp_ca_needs_ecn(sk) ||
315*4882a593Smuzhiyun 		 tcp_bpf_ca_needs_ecn(sk))
316*4882a593Smuzhiyun 		INET_ECN_xmit(sk);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun /* Packet ECN state for a SYN.  */
tcp_ecn_send_syn(struct sock * sk,struct sk_buff * skb)320*4882a593Smuzhiyun static void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
323*4882a593Smuzhiyun 	bool bpf_needs_ecn = tcp_bpf_ca_needs_ecn(sk);
324*4882a593Smuzhiyun 	bool use_ecn = sock_net(sk)->ipv4.sysctl_tcp_ecn == 1 ||
325*4882a593Smuzhiyun 		tcp_ca_needs_ecn(sk) || bpf_needs_ecn;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	if (!use_ecn) {
328*4882a593Smuzhiyun 		const struct dst_entry *dst = __sk_dst_get(sk);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 		if (dst && dst_feature(dst, RTAX_FEATURE_ECN))
331*4882a593Smuzhiyun 			use_ecn = true;
332*4882a593Smuzhiyun 	}
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	tp->ecn_flags = 0;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	if (use_ecn) {
337*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ECE | TCPHDR_CWR;
338*4882a593Smuzhiyun 		tp->ecn_flags = TCP_ECN_OK;
339*4882a593Smuzhiyun 		if (tcp_ca_needs_ecn(sk) || bpf_needs_ecn)
340*4882a593Smuzhiyun 			INET_ECN_xmit(sk);
341*4882a593Smuzhiyun 	}
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
tcp_ecn_clear_syn(struct sock * sk,struct sk_buff * skb)344*4882a593Smuzhiyun static void tcp_ecn_clear_syn(struct sock *sk, struct sk_buff *skb)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	if (sock_net(sk)->ipv4.sysctl_tcp_ecn_fallback)
347*4882a593Smuzhiyun 		/* tp->ecn_flags are cleared at a later point in time when
348*4882a593Smuzhiyun 		 * SYN ACK is ultimatively being received.
349*4882a593Smuzhiyun 		 */
350*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->tcp_flags &= ~(TCPHDR_ECE | TCPHDR_CWR);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun static void
tcp_ecn_make_synack(const struct request_sock * req,struct tcphdr * th)354*4882a593Smuzhiyun tcp_ecn_make_synack(const struct request_sock *req, struct tcphdr *th)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun 	if (inet_rsk(req)->ecn_ok)
357*4882a593Smuzhiyun 		th->ece = 1;
358*4882a593Smuzhiyun }
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun /* Set up ECN state for a packet on a ESTABLISHED socket that is about to
361*4882a593Smuzhiyun  * be sent.
362*4882a593Smuzhiyun  */
tcp_ecn_send(struct sock * sk,struct sk_buff * skb,struct tcphdr * th,int tcp_header_len)363*4882a593Smuzhiyun static void tcp_ecn_send(struct sock *sk, struct sk_buff *skb,
364*4882a593Smuzhiyun 			 struct tcphdr *th, int tcp_header_len)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	if (tp->ecn_flags & TCP_ECN_OK) {
369*4882a593Smuzhiyun 		/* Not-retransmitted data segment: set ECT and inject CWR. */
370*4882a593Smuzhiyun 		if (skb->len != tcp_header_len &&
371*4882a593Smuzhiyun 		    !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) {
372*4882a593Smuzhiyun 			INET_ECN_xmit(sk);
373*4882a593Smuzhiyun 			if (tp->ecn_flags & TCP_ECN_QUEUE_CWR) {
374*4882a593Smuzhiyun 				tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR;
375*4882a593Smuzhiyun 				th->cwr = 1;
376*4882a593Smuzhiyun 				skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
377*4882a593Smuzhiyun 			}
378*4882a593Smuzhiyun 		} else if (!tcp_ca_needs_ecn(sk)) {
379*4882a593Smuzhiyun 			/* ACK or retransmitted segment: clear ECT|CE */
380*4882a593Smuzhiyun 			INET_ECN_dontxmit(sk);
381*4882a593Smuzhiyun 		}
382*4882a593Smuzhiyun 		if (tp->ecn_flags & TCP_ECN_DEMAND_CWR)
383*4882a593Smuzhiyun 			th->ece = 1;
384*4882a593Smuzhiyun 	}
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun /* Constructs common control bits of non-data skb. If SYN/FIN is present,
388*4882a593Smuzhiyun  * auto increment end seqno.
389*4882a593Smuzhiyun  */
tcp_init_nondata_skb(struct sk_buff * skb,u32 seq,u8 flags)390*4882a593Smuzhiyun static void tcp_init_nondata_skb(struct sk_buff *skb, u32 seq, u8 flags)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	skb->ip_summed = CHECKSUM_PARTIAL;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->tcp_flags = flags;
395*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->sacked = 0;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	tcp_skb_pcount_set(skb, 1);
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->seq = seq;
400*4882a593Smuzhiyun 	if (flags & (TCPHDR_SYN | TCPHDR_FIN))
401*4882a593Smuzhiyun 		seq++;
402*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->end_seq = seq;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun 
tcp_urg_mode(const struct tcp_sock * tp)405*4882a593Smuzhiyun static inline bool tcp_urg_mode(const struct tcp_sock *tp)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	return tp->snd_una != tp->snd_up;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun #define OPTION_SACK_ADVERTISE	(1 << 0)
411*4882a593Smuzhiyun #define OPTION_TS		(1 << 1)
412*4882a593Smuzhiyun #define OPTION_MD5		(1 << 2)
413*4882a593Smuzhiyun #define OPTION_WSCALE		(1 << 3)
414*4882a593Smuzhiyun #define OPTION_FAST_OPEN_COOKIE	(1 << 8)
415*4882a593Smuzhiyun #define OPTION_SMC		(1 << 9)
416*4882a593Smuzhiyun #define OPTION_MPTCP		(1 << 10)
417*4882a593Smuzhiyun 
smc_options_write(__be32 * ptr,u16 * options)418*4882a593Smuzhiyun static void smc_options_write(__be32 *ptr, u16 *options)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SMC)
421*4882a593Smuzhiyun 	if (static_branch_unlikely(&tcp_have_smc)) {
422*4882a593Smuzhiyun 		if (unlikely(OPTION_SMC & *options)) {
423*4882a593Smuzhiyun 			*ptr++ = htonl((TCPOPT_NOP  << 24) |
424*4882a593Smuzhiyun 				       (TCPOPT_NOP  << 16) |
425*4882a593Smuzhiyun 				       (TCPOPT_EXP <<  8) |
426*4882a593Smuzhiyun 				       (TCPOLEN_EXP_SMC_BASE));
427*4882a593Smuzhiyun 			*ptr++ = htonl(TCPOPT_SMC_MAGIC);
428*4882a593Smuzhiyun 		}
429*4882a593Smuzhiyun 	}
430*4882a593Smuzhiyun #endif
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun struct tcp_out_options {
434*4882a593Smuzhiyun 	u16 options;		/* bit field of OPTION_* */
435*4882a593Smuzhiyun 	u16 mss;		/* 0 to disable */
436*4882a593Smuzhiyun 	u8 ws;			/* window scale, 0 to disable */
437*4882a593Smuzhiyun 	u8 num_sack_blocks;	/* number of SACK blocks to include */
438*4882a593Smuzhiyun 	u8 hash_size;		/* bytes in hash_location */
439*4882a593Smuzhiyun 	u8 bpf_opt_len;		/* length of BPF hdr option */
440*4882a593Smuzhiyun 	__u8 *hash_location;	/* temporary pointer, overloaded */
441*4882a593Smuzhiyun 	__u32 tsval, tsecr;	/* need to include OPTION_TS */
442*4882a593Smuzhiyun 	struct tcp_fastopen_cookie *fastopen_cookie;	/* Fast open cookie */
443*4882a593Smuzhiyun 	struct mptcp_out_options mptcp;
444*4882a593Smuzhiyun };
445*4882a593Smuzhiyun 
mptcp_options_write(__be32 * ptr,struct tcp_out_options * opts)446*4882a593Smuzhiyun static void mptcp_options_write(__be32 *ptr, struct tcp_out_options *opts)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_MPTCP)
449*4882a593Smuzhiyun 	if (unlikely(OPTION_MPTCP & opts->options))
450*4882a593Smuzhiyun 		mptcp_write_options(ptr, &opts->mptcp);
451*4882a593Smuzhiyun #endif
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun #ifdef CONFIG_CGROUP_BPF
bpf_skops_write_hdr_opt_arg0(struct sk_buff * skb,enum tcp_synack_type synack_type)455*4882a593Smuzhiyun static int bpf_skops_write_hdr_opt_arg0(struct sk_buff *skb,
456*4882a593Smuzhiyun 					enum tcp_synack_type synack_type)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun 	if (unlikely(!skb))
459*4882a593Smuzhiyun 		return BPF_WRITE_HDR_TCP_CURRENT_MSS;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	if (unlikely(synack_type == TCP_SYNACK_COOKIE))
462*4882a593Smuzhiyun 		return BPF_WRITE_HDR_TCP_SYNACK_COOKIE;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	return 0;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun /* req, syn_skb and synack_type are used when writing synack */
bpf_skops_hdr_opt_len(struct sock * sk,struct sk_buff * skb,struct request_sock * req,struct sk_buff * syn_skb,enum tcp_synack_type synack_type,struct tcp_out_options * opts,unsigned int * remaining)468*4882a593Smuzhiyun static void bpf_skops_hdr_opt_len(struct sock *sk, struct sk_buff *skb,
469*4882a593Smuzhiyun 				  struct request_sock *req,
470*4882a593Smuzhiyun 				  struct sk_buff *syn_skb,
471*4882a593Smuzhiyun 				  enum tcp_synack_type synack_type,
472*4882a593Smuzhiyun 				  struct tcp_out_options *opts,
473*4882a593Smuzhiyun 				  unsigned int *remaining)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun 	struct bpf_sock_ops_kern sock_ops;
476*4882a593Smuzhiyun 	int err;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	if (likely(!BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk),
479*4882a593Smuzhiyun 					   BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG)) ||
480*4882a593Smuzhiyun 	    !*remaining)
481*4882a593Smuzhiyun 		return;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	/* *remaining has already been aligned to 4 bytes, so *remaining >= 4 */
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	/* init sock_ops */
486*4882a593Smuzhiyun 	memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	sock_ops.op = BPF_SOCK_OPS_HDR_OPT_LEN_CB;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	if (req) {
491*4882a593Smuzhiyun 		/* The listen "sk" cannot be passed here because
492*4882a593Smuzhiyun 		 * it is not locked.  It would not make too much
493*4882a593Smuzhiyun 		 * sense to do bpf_setsockopt(listen_sk) based
494*4882a593Smuzhiyun 		 * on individual connection request also.
495*4882a593Smuzhiyun 		 *
496*4882a593Smuzhiyun 		 * Thus, "req" is passed here and the cgroup-bpf-progs
497*4882a593Smuzhiyun 		 * of the listen "sk" will be run.
498*4882a593Smuzhiyun 		 *
499*4882a593Smuzhiyun 		 * "req" is also used here for fastopen even the "sk" here is
500*4882a593Smuzhiyun 		 * a fullsock "child" sk.  It is to keep the behavior
501*4882a593Smuzhiyun 		 * consistent between fastopen and non-fastopen on
502*4882a593Smuzhiyun 		 * the bpf programming side.
503*4882a593Smuzhiyun 		 */
504*4882a593Smuzhiyun 		sock_ops.sk = (struct sock *)req;
505*4882a593Smuzhiyun 		sock_ops.syn_skb = syn_skb;
506*4882a593Smuzhiyun 	} else {
507*4882a593Smuzhiyun 		sock_owned_by_me(sk);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 		sock_ops.is_fullsock = 1;
510*4882a593Smuzhiyun 		sock_ops.sk = sk;
511*4882a593Smuzhiyun 	}
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	sock_ops.args[0] = bpf_skops_write_hdr_opt_arg0(skb, synack_type);
514*4882a593Smuzhiyun 	sock_ops.remaining_opt_len = *remaining;
515*4882a593Smuzhiyun 	/* tcp_current_mss() does not pass a skb */
516*4882a593Smuzhiyun 	if (skb)
517*4882a593Smuzhiyun 		bpf_skops_init_skb(&sock_ops, skb, 0);
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	err = BPF_CGROUP_RUN_PROG_SOCK_OPS_SK(&sock_ops, sk);
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	if (err || sock_ops.remaining_opt_len == *remaining)
522*4882a593Smuzhiyun 		return;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	opts->bpf_opt_len = *remaining - sock_ops.remaining_opt_len;
525*4882a593Smuzhiyun 	/* round up to 4 bytes */
526*4882a593Smuzhiyun 	opts->bpf_opt_len = (opts->bpf_opt_len + 3) & ~3;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	*remaining -= opts->bpf_opt_len;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun 
bpf_skops_write_hdr_opt(struct sock * sk,struct sk_buff * skb,struct request_sock * req,struct sk_buff * syn_skb,enum tcp_synack_type synack_type,struct tcp_out_options * opts)531*4882a593Smuzhiyun static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,
532*4882a593Smuzhiyun 				    struct request_sock *req,
533*4882a593Smuzhiyun 				    struct sk_buff *syn_skb,
534*4882a593Smuzhiyun 				    enum tcp_synack_type synack_type,
535*4882a593Smuzhiyun 				    struct tcp_out_options *opts)
536*4882a593Smuzhiyun {
537*4882a593Smuzhiyun 	u8 first_opt_off, nr_written, max_opt_len = opts->bpf_opt_len;
538*4882a593Smuzhiyun 	struct bpf_sock_ops_kern sock_ops;
539*4882a593Smuzhiyun 	int err;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	if (likely(!max_opt_len))
542*4882a593Smuzhiyun 		return;
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
545*4882a593Smuzhiyun 
546*4882a593Smuzhiyun 	sock_ops.op = BPF_SOCK_OPS_WRITE_HDR_OPT_CB;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	if (req) {
549*4882a593Smuzhiyun 		sock_ops.sk = (struct sock *)req;
550*4882a593Smuzhiyun 		sock_ops.syn_skb = syn_skb;
551*4882a593Smuzhiyun 	} else {
552*4882a593Smuzhiyun 		sock_owned_by_me(sk);
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun 		sock_ops.is_fullsock = 1;
555*4882a593Smuzhiyun 		sock_ops.sk = sk;
556*4882a593Smuzhiyun 	}
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	sock_ops.args[0] = bpf_skops_write_hdr_opt_arg0(skb, synack_type);
559*4882a593Smuzhiyun 	sock_ops.remaining_opt_len = max_opt_len;
560*4882a593Smuzhiyun 	first_opt_off = tcp_hdrlen(skb) - max_opt_len;
561*4882a593Smuzhiyun 	bpf_skops_init_skb(&sock_ops, skb, first_opt_off);
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun 	err = BPF_CGROUP_RUN_PROG_SOCK_OPS_SK(&sock_ops, sk);
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	if (err)
566*4882a593Smuzhiyun 		nr_written = 0;
567*4882a593Smuzhiyun 	else
568*4882a593Smuzhiyun 		nr_written = max_opt_len - sock_ops.remaining_opt_len;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	if (nr_written < max_opt_len)
571*4882a593Smuzhiyun 		memset(skb->data + first_opt_off + nr_written, TCPOPT_NOP,
572*4882a593Smuzhiyun 		       max_opt_len - nr_written);
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun #else
bpf_skops_hdr_opt_len(struct sock * sk,struct sk_buff * skb,struct request_sock * req,struct sk_buff * syn_skb,enum tcp_synack_type synack_type,struct tcp_out_options * opts,unsigned int * remaining)575*4882a593Smuzhiyun static void bpf_skops_hdr_opt_len(struct sock *sk, struct sk_buff *skb,
576*4882a593Smuzhiyun 				  struct request_sock *req,
577*4882a593Smuzhiyun 				  struct sk_buff *syn_skb,
578*4882a593Smuzhiyun 				  enum tcp_synack_type synack_type,
579*4882a593Smuzhiyun 				  struct tcp_out_options *opts,
580*4882a593Smuzhiyun 				  unsigned int *remaining)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun 
bpf_skops_write_hdr_opt(struct sock * sk,struct sk_buff * skb,struct request_sock * req,struct sk_buff * syn_skb,enum tcp_synack_type synack_type,struct tcp_out_options * opts)584*4882a593Smuzhiyun static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb,
585*4882a593Smuzhiyun 				    struct request_sock *req,
586*4882a593Smuzhiyun 				    struct sk_buff *syn_skb,
587*4882a593Smuzhiyun 				    enum tcp_synack_type synack_type,
588*4882a593Smuzhiyun 				    struct tcp_out_options *opts)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun #endif
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun /* Write previously computed TCP options to the packet.
594*4882a593Smuzhiyun  *
595*4882a593Smuzhiyun  * Beware: Something in the Internet is very sensitive to the ordering of
596*4882a593Smuzhiyun  * TCP options, we learned this through the hard way, so be careful here.
597*4882a593Smuzhiyun  * Luckily we can at least blame others for their non-compliance but from
598*4882a593Smuzhiyun  * inter-operability perspective it seems that we're somewhat stuck with
599*4882a593Smuzhiyun  * the ordering which we have been using if we want to keep working with
600*4882a593Smuzhiyun  * those broken things (not that it currently hurts anybody as there isn't
601*4882a593Smuzhiyun  * particular reason why the ordering would need to be changed).
602*4882a593Smuzhiyun  *
603*4882a593Smuzhiyun  * At least SACK_PERM as the first option is known to lead to a disaster
604*4882a593Smuzhiyun  * (but it may well be that other scenarios fail similarly).
605*4882a593Smuzhiyun  */
tcp_options_write(__be32 * ptr,struct tcp_sock * tp,struct tcp_out_options * opts)606*4882a593Smuzhiyun static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
607*4882a593Smuzhiyun 			      struct tcp_out_options *opts)
608*4882a593Smuzhiyun {
609*4882a593Smuzhiyun 	u16 options = opts->options;	/* mungable copy */
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	if (unlikely(OPTION_MD5 & options)) {
612*4882a593Smuzhiyun 		*ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
613*4882a593Smuzhiyun 			       (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
614*4882a593Smuzhiyun 		/* overload cookie hash location */
615*4882a593Smuzhiyun 		opts->hash_location = (__u8 *)ptr;
616*4882a593Smuzhiyun 		ptr += 4;
617*4882a593Smuzhiyun 	}
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 	if (unlikely(opts->mss)) {
620*4882a593Smuzhiyun 		*ptr++ = htonl((TCPOPT_MSS << 24) |
621*4882a593Smuzhiyun 			       (TCPOLEN_MSS << 16) |
622*4882a593Smuzhiyun 			       opts->mss);
623*4882a593Smuzhiyun 	}
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 	if (likely(OPTION_TS & options)) {
626*4882a593Smuzhiyun 		if (unlikely(OPTION_SACK_ADVERTISE & options)) {
627*4882a593Smuzhiyun 			*ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
628*4882a593Smuzhiyun 				       (TCPOLEN_SACK_PERM << 16) |
629*4882a593Smuzhiyun 				       (TCPOPT_TIMESTAMP << 8) |
630*4882a593Smuzhiyun 				       TCPOLEN_TIMESTAMP);
631*4882a593Smuzhiyun 			options &= ~OPTION_SACK_ADVERTISE;
632*4882a593Smuzhiyun 		} else {
633*4882a593Smuzhiyun 			*ptr++ = htonl((TCPOPT_NOP << 24) |
634*4882a593Smuzhiyun 				       (TCPOPT_NOP << 16) |
635*4882a593Smuzhiyun 				       (TCPOPT_TIMESTAMP << 8) |
636*4882a593Smuzhiyun 				       TCPOLEN_TIMESTAMP);
637*4882a593Smuzhiyun 		}
638*4882a593Smuzhiyun 		*ptr++ = htonl(opts->tsval);
639*4882a593Smuzhiyun 		*ptr++ = htonl(opts->tsecr);
640*4882a593Smuzhiyun 	}
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 	if (unlikely(OPTION_SACK_ADVERTISE & options)) {
643*4882a593Smuzhiyun 		*ptr++ = htonl((TCPOPT_NOP << 24) |
644*4882a593Smuzhiyun 			       (TCPOPT_NOP << 16) |
645*4882a593Smuzhiyun 			       (TCPOPT_SACK_PERM << 8) |
646*4882a593Smuzhiyun 			       TCPOLEN_SACK_PERM);
647*4882a593Smuzhiyun 	}
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	if (unlikely(OPTION_WSCALE & options)) {
650*4882a593Smuzhiyun 		*ptr++ = htonl((TCPOPT_NOP << 24) |
651*4882a593Smuzhiyun 			       (TCPOPT_WINDOW << 16) |
652*4882a593Smuzhiyun 			       (TCPOLEN_WINDOW << 8) |
653*4882a593Smuzhiyun 			       opts->ws);
654*4882a593Smuzhiyun 	}
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	if (unlikely(opts->num_sack_blocks)) {
657*4882a593Smuzhiyun 		struct tcp_sack_block *sp = tp->rx_opt.dsack ?
658*4882a593Smuzhiyun 			tp->duplicate_sack : tp->selective_acks;
659*4882a593Smuzhiyun 		int this_sack;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 		*ptr++ = htonl((TCPOPT_NOP  << 24) |
662*4882a593Smuzhiyun 			       (TCPOPT_NOP  << 16) |
663*4882a593Smuzhiyun 			       (TCPOPT_SACK <<  8) |
664*4882a593Smuzhiyun 			       (TCPOLEN_SACK_BASE + (opts->num_sack_blocks *
665*4882a593Smuzhiyun 						     TCPOLEN_SACK_PERBLOCK)));
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 		for (this_sack = 0; this_sack < opts->num_sack_blocks;
668*4882a593Smuzhiyun 		     ++this_sack) {
669*4882a593Smuzhiyun 			*ptr++ = htonl(sp[this_sack].start_seq);
670*4882a593Smuzhiyun 			*ptr++ = htonl(sp[this_sack].end_seq);
671*4882a593Smuzhiyun 		}
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 		tp->rx_opt.dsack = 0;
674*4882a593Smuzhiyun 	}
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 	if (unlikely(OPTION_FAST_OPEN_COOKIE & options)) {
677*4882a593Smuzhiyun 		struct tcp_fastopen_cookie *foc = opts->fastopen_cookie;
678*4882a593Smuzhiyun 		u8 *p = (u8 *)ptr;
679*4882a593Smuzhiyun 		u32 len; /* Fast Open option length */
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 		if (foc->exp) {
682*4882a593Smuzhiyun 			len = TCPOLEN_EXP_FASTOPEN_BASE + foc->len;
683*4882a593Smuzhiyun 			*ptr = htonl((TCPOPT_EXP << 24) | (len << 16) |
684*4882a593Smuzhiyun 				     TCPOPT_FASTOPEN_MAGIC);
685*4882a593Smuzhiyun 			p += TCPOLEN_EXP_FASTOPEN_BASE;
686*4882a593Smuzhiyun 		} else {
687*4882a593Smuzhiyun 			len = TCPOLEN_FASTOPEN_BASE + foc->len;
688*4882a593Smuzhiyun 			*p++ = TCPOPT_FASTOPEN;
689*4882a593Smuzhiyun 			*p++ = len;
690*4882a593Smuzhiyun 		}
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun 		memcpy(p, foc->val, foc->len);
693*4882a593Smuzhiyun 		if ((len & 3) == 2) {
694*4882a593Smuzhiyun 			p[foc->len] = TCPOPT_NOP;
695*4882a593Smuzhiyun 			p[foc->len + 1] = TCPOPT_NOP;
696*4882a593Smuzhiyun 		}
697*4882a593Smuzhiyun 		ptr += (len + 3) >> 2;
698*4882a593Smuzhiyun 	}
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	smc_options_write(ptr, &options);
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	mptcp_options_write(ptr, opts);
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun 
smc_set_option(const struct tcp_sock * tp,struct tcp_out_options * opts,unsigned int * remaining)705*4882a593Smuzhiyun static void smc_set_option(const struct tcp_sock *tp,
706*4882a593Smuzhiyun 			   struct tcp_out_options *opts,
707*4882a593Smuzhiyun 			   unsigned int *remaining)
708*4882a593Smuzhiyun {
709*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SMC)
710*4882a593Smuzhiyun 	if (static_branch_unlikely(&tcp_have_smc)) {
711*4882a593Smuzhiyun 		if (tp->syn_smc) {
712*4882a593Smuzhiyun 			if (*remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) {
713*4882a593Smuzhiyun 				opts->options |= OPTION_SMC;
714*4882a593Smuzhiyun 				*remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED;
715*4882a593Smuzhiyun 			}
716*4882a593Smuzhiyun 		}
717*4882a593Smuzhiyun 	}
718*4882a593Smuzhiyun #endif
719*4882a593Smuzhiyun }
720*4882a593Smuzhiyun 
smc_set_option_cond(const struct tcp_sock * tp,const struct inet_request_sock * ireq,struct tcp_out_options * opts,unsigned int * remaining)721*4882a593Smuzhiyun static void smc_set_option_cond(const struct tcp_sock *tp,
722*4882a593Smuzhiyun 				const struct inet_request_sock *ireq,
723*4882a593Smuzhiyun 				struct tcp_out_options *opts,
724*4882a593Smuzhiyun 				unsigned int *remaining)
725*4882a593Smuzhiyun {
726*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SMC)
727*4882a593Smuzhiyun 	if (static_branch_unlikely(&tcp_have_smc)) {
728*4882a593Smuzhiyun 		if (tp->syn_smc && ireq->smc_ok) {
729*4882a593Smuzhiyun 			if (*remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) {
730*4882a593Smuzhiyun 				opts->options |= OPTION_SMC;
731*4882a593Smuzhiyun 				*remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED;
732*4882a593Smuzhiyun 			}
733*4882a593Smuzhiyun 		}
734*4882a593Smuzhiyun 	}
735*4882a593Smuzhiyun #endif
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun 
mptcp_set_option_cond(const struct request_sock * req,struct tcp_out_options * opts,unsigned int * remaining)738*4882a593Smuzhiyun static void mptcp_set_option_cond(const struct request_sock *req,
739*4882a593Smuzhiyun 				  struct tcp_out_options *opts,
740*4882a593Smuzhiyun 				  unsigned int *remaining)
741*4882a593Smuzhiyun {
742*4882a593Smuzhiyun 	if (rsk_is_mptcp(req)) {
743*4882a593Smuzhiyun 		unsigned int size;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 		if (mptcp_synack_options(req, &size, &opts->mptcp)) {
746*4882a593Smuzhiyun 			if (*remaining >= size) {
747*4882a593Smuzhiyun 				opts->options |= OPTION_MPTCP;
748*4882a593Smuzhiyun 				*remaining -= size;
749*4882a593Smuzhiyun 			}
750*4882a593Smuzhiyun 		}
751*4882a593Smuzhiyun 	}
752*4882a593Smuzhiyun }
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun /* Compute TCP options for SYN packets. This is not the final
755*4882a593Smuzhiyun  * network wire format yet.
756*4882a593Smuzhiyun  */
tcp_syn_options(struct sock * sk,struct sk_buff * skb,struct tcp_out_options * opts,struct tcp_md5sig_key ** md5)757*4882a593Smuzhiyun static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
758*4882a593Smuzhiyun 				struct tcp_out_options *opts,
759*4882a593Smuzhiyun 				struct tcp_md5sig_key **md5)
760*4882a593Smuzhiyun {
761*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
762*4882a593Smuzhiyun 	unsigned int remaining = MAX_TCP_OPTION_SPACE;
763*4882a593Smuzhiyun 	struct tcp_fastopen_request *fastopen = tp->fastopen_req;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	*md5 = NULL;
766*4882a593Smuzhiyun #ifdef CONFIG_TCP_MD5SIG
767*4882a593Smuzhiyun 	if (static_branch_unlikely(&tcp_md5_needed) &&
768*4882a593Smuzhiyun 	    rcu_access_pointer(tp->md5sig_info)) {
769*4882a593Smuzhiyun 		*md5 = tp->af_specific->md5_lookup(sk, sk);
770*4882a593Smuzhiyun 		if (*md5) {
771*4882a593Smuzhiyun 			opts->options |= OPTION_MD5;
772*4882a593Smuzhiyun 			remaining -= TCPOLEN_MD5SIG_ALIGNED;
773*4882a593Smuzhiyun 		}
774*4882a593Smuzhiyun 	}
775*4882a593Smuzhiyun #endif
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	/* We always get an MSS option.  The option bytes which will be seen in
778*4882a593Smuzhiyun 	 * normal data packets should timestamps be used, must be in the MSS
779*4882a593Smuzhiyun 	 * advertised.  But we subtract them from tp->mss_cache so that
780*4882a593Smuzhiyun 	 * calculations in tcp_sendmsg are simpler etc.  So account for this
781*4882a593Smuzhiyun 	 * fact here if necessary.  If we don't do this correctly, as a
782*4882a593Smuzhiyun 	 * receiver we won't recognize data packets as being full sized when we
783*4882a593Smuzhiyun 	 * should, and thus we won't abide by the delayed ACK rules correctly.
784*4882a593Smuzhiyun 	 * SACKs don't matter, we never delay an ACK when we have any of those
785*4882a593Smuzhiyun 	 * going out.  */
786*4882a593Smuzhiyun 	opts->mss = tcp_advertise_mss(sk);
787*4882a593Smuzhiyun 	remaining -= TCPOLEN_MSS_ALIGNED;
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	if (likely(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps) && !*md5)) {
790*4882a593Smuzhiyun 		opts->options |= OPTION_TS;
791*4882a593Smuzhiyun 		opts->tsval = tcp_skb_timestamp(skb) + tp->tsoffset;
792*4882a593Smuzhiyun 		opts->tsecr = tp->rx_opt.ts_recent;
793*4882a593Smuzhiyun 		remaining -= TCPOLEN_TSTAMP_ALIGNED;
794*4882a593Smuzhiyun 	}
795*4882a593Smuzhiyun 	if (likely(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_window_scaling))) {
796*4882a593Smuzhiyun 		opts->ws = tp->rx_opt.rcv_wscale;
797*4882a593Smuzhiyun 		opts->options |= OPTION_WSCALE;
798*4882a593Smuzhiyun 		remaining -= TCPOLEN_WSCALE_ALIGNED;
799*4882a593Smuzhiyun 	}
800*4882a593Smuzhiyun 	if (likely(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_sack))) {
801*4882a593Smuzhiyun 		opts->options |= OPTION_SACK_ADVERTISE;
802*4882a593Smuzhiyun 		if (unlikely(!(OPTION_TS & opts->options)))
803*4882a593Smuzhiyun 			remaining -= TCPOLEN_SACKPERM_ALIGNED;
804*4882a593Smuzhiyun 	}
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	if (fastopen && fastopen->cookie.len >= 0) {
807*4882a593Smuzhiyun 		u32 need = fastopen->cookie.len;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 		need += fastopen->cookie.exp ? TCPOLEN_EXP_FASTOPEN_BASE :
810*4882a593Smuzhiyun 					       TCPOLEN_FASTOPEN_BASE;
811*4882a593Smuzhiyun 		need = (need + 3) & ~3U;  /* Align to 32 bits */
812*4882a593Smuzhiyun 		if (remaining >= need) {
813*4882a593Smuzhiyun 			opts->options |= OPTION_FAST_OPEN_COOKIE;
814*4882a593Smuzhiyun 			opts->fastopen_cookie = &fastopen->cookie;
815*4882a593Smuzhiyun 			remaining -= need;
816*4882a593Smuzhiyun 			tp->syn_fastopen = 1;
817*4882a593Smuzhiyun 			tp->syn_fastopen_exp = fastopen->cookie.exp ? 1 : 0;
818*4882a593Smuzhiyun 		}
819*4882a593Smuzhiyun 	}
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun 	smc_set_option(tp, opts, &remaining);
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	if (sk_is_mptcp(sk)) {
824*4882a593Smuzhiyun 		unsigned int size;
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun 		if (mptcp_syn_options(sk, skb, &size, &opts->mptcp)) {
827*4882a593Smuzhiyun 			opts->options |= OPTION_MPTCP;
828*4882a593Smuzhiyun 			remaining -= size;
829*4882a593Smuzhiyun 		}
830*4882a593Smuzhiyun 	}
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts, &remaining);
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	return MAX_TCP_OPTION_SPACE - remaining;
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun /* Set up TCP options for SYN-ACKs. */
tcp_synack_options(const struct sock * sk,struct request_sock * req,unsigned int mss,struct sk_buff * skb,struct tcp_out_options * opts,const struct tcp_md5sig_key * md5,struct tcp_fastopen_cookie * foc,enum tcp_synack_type synack_type,struct sk_buff * syn_skb)838*4882a593Smuzhiyun static unsigned int tcp_synack_options(const struct sock *sk,
839*4882a593Smuzhiyun 				       struct request_sock *req,
840*4882a593Smuzhiyun 				       unsigned int mss, struct sk_buff *skb,
841*4882a593Smuzhiyun 				       struct tcp_out_options *opts,
842*4882a593Smuzhiyun 				       const struct tcp_md5sig_key *md5,
843*4882a593Smuzhiyun 				       struct tcp_fastopen_cookie *foc,
844*4882a593Smuzhiyun 				       enum tcp_synack_type synack_type,
845*4882a593Smuzhiyun 				       struct sk_buff *syn_skb)
846*4882a593Smuzhiyun {
847*4882a593Smuzhiyun 	struct inet_request_sock *ireq = inet_rsk(req);
848*4882a593Smuzhiyun 	unsigned int remaining = MAX_TCP_OPTION_SPACE;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun #ifdef CONFIG_TCP_MD5SIG
851*4882a593Smuzhiyun 	if (md5) {
852*4882a593Smuzhiyun 		opts->options |= OPTION_MD5;
853*4882a593Smuzhiyun 		remaining -= TCPOLEN_MD5SIG_ALIGNED;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun 		/* We can't fit any SACK blocks in a packet with MD5 + TS
856*4882a593Smuzhiyun 		 * options. There was discussion about disabling SACK
857*4882a593Smuzhiyun 		 * rather than TS in order to fit in better with old,
858*4882a593Smuzhiyun 		 * buggy kernels, but that was deemed to be unnecessary.
859*4882a593Smuzhiyun 		 */
860*4882a593Smuzhiyun 		if (synack_type != TCP_SYNACK_COOKIE)
861*4882a593Smuzhiyun 			ireq->tstamp_ok &= !ireq->sack_ok;
862*4882a593Smuzhiyun 	}
863*4882a593Smuzhiyun #endif
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 	/* We always send an MSS option. */
866*4882a593Smuzhiyun 	opts->mss = mss;
867*4882a593Smuzhiyun 	remaining -= TCPOLEN_MSS_ALIGNED;
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	if (likely(ireq->wscale_ok)) {
870*4882a593Smuzhiyun 		opts->ws = ireq->rcv_wscale;
871*4882a593Smuzhiyun 		opts->options |= OPTION_WSCALE;
872*4882a593Smuzhiyun 		remaining -= TCPOLEN_WSCALE_ALIGNED;
873*4882a593Smuzhiyun 	}
874*4882a593Smuzhiyun 	if (likely(ireq->tstamp_ok)) {
875*4882a593Smuzhiyun 		opts->options |= OPTION_TS;
876*4882a593Smuzhiyun 		opts->tsval = tcp_skb_timestamp(skb) + tcp_rsk(req)->ts_off;
877*4882a593Smuzhiyun 		opts->tsecr = req->ts_recent;
878*4882a593Smuzhiyun 		remaining -= TCPOLEN_TSTAMP_ALIGNED;
879*4882a593Smuzhiyun 	}
880*4882a593Smuzhiyun 	if (likely(ireq->sack_ok)) {
881*4882a593Smuzhiyun 		opts->options |= OPTION_SACK_ADVERTISE;
882*4882a593Smuzhiyun 		if (unlikely(!ireq->tstamp_ok))
883*4882a593Smuzhiyun 			remaining -= TCPOLEN_SACKPERM_ALIGNED;
884*4882a593Smuzhiyun 	}
885*4882a593Smuzhiyun 	if (foc != NULL && foc->len >= 0) {
886*4882a593Smuzhiyun 		u32 need = foc->len;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 		need += foc->exp ? TCPOLEN_EXP_FASTOPEN_BASE :
889*4882a593Smuzhiyun 				   TCPOLEN_FASTOPEN_BASE;
890*4882a593Smuzhiyun 		need = (need + 3) & ~3U;  /* Align to 32 bits */
891*4882a593Smuzhiyun 		if (remaining >= need) {
892*4882a593Smuzhiyun 			opts->options |= OPTION_FAST_OPEN_COOKIE;
893*4882a593Smuzhiyun 			opts->fastopen_cookie = foc;
894*4882a593Smuzhiyun 			remaining -= need;
895*4882a593Smuzhiyun 		}
896*4882a593Smuzhiyun 	}
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	mptcp_set_option_cond(req, opts, &remaining);
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	smc_set_option_cond(tcp_sk(sk), ireq, opts, &remaining);
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	bpf_skops_hdr_opt_len((struct sock *)sk, skb, req, syn_skb,
903*4882a593Smuzhiyun 			      synack_type, opts, &remaining);
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	return MAX_TCP_OPTION_SPACE - remaining;
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun /* Compute TCP options for ESTABLISHED sockets. This is not the
909*4882a593Smuzhiyun  * final wire format yet.
910*4882a593Smuzhiyun  */
tcp_established_options(struct sock * sk,struct sk_buff * skb,struct tcp_out_options * opts,struct tcp_md5sig_key ** md5)911*4882a593Smuzhiyun static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb,
912*4882a593Smuzhiyun 					struct tcp_out_options *opts,
913*4882a593Smuzhiyun 					struct tcp_md5sig_key **md5)
914*4882a593Smuzhiyun {
915*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
916*4882a593Smuzhiyun 	unsigned int size = 0;
917*4882a593Smuzhiyun 	unsigned int eff_sacks;
918*4882a593Smuzhiyun 
919*4882a593Smuzhiyun 	opts->options = 0;
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	*md5 = NULL;
922*4882a593Smuzhiyun #ifdef CONFIG_TCP_MD5SIG
923*4882a593Smuzhiyun 	if (static_branch_unlikely(&tcp_md5_needed) &&
924*4882a593Smuzhiyun 	    rcu_access_pointer(tp->md5sig_info)) {
925*4882a593Smuzhiyun 		*md5 = tp->af_specific->md5_lookup(sk, sk);
926*4882a593Smuzhiyun 		if (*md5) {
927*4882a593Smuzhiyun 			opts->options |= OPTION_MD5;
928*4882a593Smuzhiyun 			size += TCPOLEN_MD5SIG_ALIGNED;
929*4882a593Smuzhiyun 		}
930*4882a593Smuzhiyun 	}
931*4882a593Smuzhiyun #endif
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun 	if (likely(tp->rx_opt.tstamp_ok)) {
934*4882a593Smuzhiyun 		opts->options |= OPTION_TS;
935*4882a593Smuzhiyun 		opts->tsval = skb ? tcp_skb_timestamp(skb) + tp->tsoffset : 0;
936*4882a593Smuzhiyun 		opts->tsecr = tp->rx_opt.ts_recent;
937*4882a593Smuzhiyun 		size += TCPOLEN_TSTAMP_ALIGNED;
938*4882a593Smuzhiyun 	}
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	/* MPTCP options have precedence over SACK for the limited TCP
941*4882a593Smuzhiyun 	 * option space because a MPTCP connection would be forced to
942*4882a593Smuzhiyun 	 * fall back to regular TCP if a required multipath option is
943*4882a593Smuzhiyun 	 * missing. SACK still gets a chance to use whatever space is
944*4882a593Smuzhiyun 	 * left.
945*4882a593Smuzhiyun 	 */
946*4882a593Smuzhiyun 	if (sk_is_mptcp(sk)) {
947*4882a593Smuzhiyun 		unsigned int remaining = MAX_TCP_OPTION_SPACE - size;
948*4882a593Smuzhiyun 		unsigned int opt_size = 0;
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 		if (mptcp_established_options(sk, skb, &opt_size, remaining,
951*4882a593Smuzhiyun 					      &opts->mptcp)) {
952*4882a593Smuzhiyun 			opts->options |= OPTION_MPTCP;
953*4882a593Smuzhiyun 			size += opt_size;
954*4882a593Smuzhiyun 		}
955*4882a593Smuzhiyun 	}
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack;
958*4882a593Smuzhiyun 	if (unlikely(eff_sacks)) {
959*4882a593Smuzhiyun 		const unsigned int remaining = MAX_TCP_OPTION_SPACE - size;
960*4882a593Smuzhiyun 		if (unlikely(remaining < TCPOLEN_SACK_BASE_ALIGNED +
961*4882a593Smuzhiyun 					 TCPOLEN_SACK_PERBLOCK))
962*4882a593Smuzhiyun 			return size;
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 		opts->num_sack_blocks =
965*4882a593Smuzhiyun 			min_t(unsigned int, eff_sacks,
966*4882a593Smuzhiyun 			      (remaining - TCPOLEN_SACK_BASE_ALIGNED) /
967*4882a593Smuzhiyun 			      TCPOLEN_SACK_PERBLOCK);
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 		size += TCPOLEN_SACK_BASE_ALIGNED +
970*4882a593Smuzhiyun 			opts->num_sack_blocks * TCPOLEN_SACK_PERBLOCK;
971*4882a593Smuzhiyun 	}
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	if (unlikely(BPF_SOCK_OPS_TEST_FLAG(tp,
974*4882a593Smuzhiyun 					    BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG))) {
975*4882a593Smuzhiyun 		unsigned int remaining = MAX_TCP_OPTION_SPACE - size;
976*4882a593Smuzhiyun 
977*4882a593Smuzhiyun 		bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts, &remaining);
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun 		size = MAX_TCP_OPTION_SPACE - remaining;
980*4882a593Smuzhiyun 	}
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	return size;
983*4882a593Smuzhiyun }
984*4882a593Smuzhiyun 
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun /* TCP SMALL QUEUES (TSQ)
987*4882a593Smuzhiyun  *
988*4882a593Smuzhiyun  * TSQ goal is to keep small amount of skbs per tcp flow in tx queues (qdisc+dev)
989*4882a593Smuzhiyun  * to reduce RTT and bufferbloat.
990*4882a593Smuzhiyun  * We do this using a special skb destructor (tcp_wfree).
991*4882a593Smuzhiyun  *
992*4882a593Smuzhiyun  * Its important tcp_wfree() can be replaced by sock_wfree() in the event skb
993*4882a593Smuzhiyun  * needs to be reallocated in a driver.
994*4882a593Smuzhiyun  * The invariant being skb->truesize subtracted from sk->sk_wmem_alloc
995*4882a593Smuzhiyun  *
996*4882a593Smuzhiyun  * Since transmit from skb destructor is forbidden, we use a tasklet
997*4882a593Smuzhiyun  * to process all sockets that eventually need to send more skbs.
998*4882a593Smuzhiyun  * We use one tasklet per cpu, with its own queue of sockets.
999*4882a593Smuzhiyun  */
1000*4882a593Smuzhiyun struct tsq_tasklet {
1001*4882a593Smuzhiyun 	struct tasklet_struct	tasklet;
1002*4882a593Smuzhiyun 	struct list_head	head; /* queue of tcp sockets */
1003*4882a593Smuzhiyun };
1004*4882a593Smuzhiyun static DEFINE_PER_CPU(struct tsq_tasklet, tsq_tasklet);
1005*4882a593Smuzhiyun 
tcp_tsq_write(struct sock * sk)1006*4882a593Smuzhiyun static void tcp_tsq_write(struct sock *sk)
1007*4882a593Smuzhiyun {
1008*4882a593Smuzhiyun 	if ((1 << sk->sk_state) &
1009*4882a593Smuzhiyun 	    (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_CLOSING |
1010*4882a593Smuzhiyun 	     TCPF_CLOSE_WAIT  | TCPF_LAST_ACK)) {
1011*4882a593Smuzhiyun 		struct tcp_sock *tp = tcp_sk(sk);
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun 		if (tp->lost_out > tp->retrans_out &&
1014*4882a593Smuzhiyun 		    tp->snd_cwnd > tcp_packets_in_flight(tp)) {
1015*4882a593Smuzhiyun 			tcp_mstamp_refresh(tp);
1016*4882a593Smuzhiyun 			tcp_xmit_retransmit_queue(sk);
1017*4882a593Smuzhiyun 		}
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 		tcp_write_xmit(sk, tcp_current_mss(sk), tp->nonagle,
1020*4882a593Smuzhiyun 			       0, GFP_ATOMIC);
1021*4882a593Smuzhiyun 	}
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun 
tcp_tsq_handler(struct sock * sk)1024*4882a593Smuzhiyun static void tcp_tsq_handler(struct sock *sk)
1025*4882a593Smuzhiyun {
1026*4882a593Smuzhiyun 	bh_lock_sock(sk);
1027*4882a593Smuzhiyun 	if (!sock_owned_by_user(sk))
1028*4882a593Smuzhiyun 		tcp_tsq_write(sk);
1029*4882a593Smuzhiyun 	else if (!test_and_set_bit(TCP_TSQ_DEFERRED, &sk->sk_tsq_flags))
1030*4882a593Smuzhiyun 		sock_hold(sk);
1031*4882a593Smuzhiyun 	bh_unlock_sock(sk);
1032*4882a593Smuzhiyun }
1033*4882a593Smuzhiyun /*
1034*4882a593Smuzhiyun  * One tasklet per cpu tries to send more skbs.
1035*4882a593Smuzhiyun  * We run in tasklet context but need to disable irqs when
1036*4882a593Smuzhiyun  * transferring tsq->head because tcp_wfree() might
1037*4882a593Smuzhiyun  * interrupt us (non NAPI drivers)
1038*4882a593Smuzhiyun  */
tcp_tasklet_func(unsigned long data)1039*4882a593Smuzhiyun static void tcp_tasklet_func(unsigned long data)
1040*4882a593Smuzhiyun {
1041*4882a593Smuzhiyun 	struct tsq_tasklet *tsq = (struct tsq_tasklet *)data;
1042*4882a593Smuzhiyun 	LIST_HEAD(list);
1043*4882a593Smuzhiyun 	unsigned long flags;
1044*4882a593Smuzhiyun 	struct list_head *q, *n;
1045*4882a593Smuzhiyun 	struct tcp_sock *tp;
1046*4882a593Smuzhiyun 	struct sock *sk;
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	local_irq_save(flags);
1049*4882a593Smuzhiyun 	list_splice_init(&tsq->head, &list);
1050*4882a593Smuzhiyun 	local_irq_restore(flags);
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun 	list_for_each_safe(q, n, &list) {
1053*4882a593Smuzhiyun 		tp = list_entry(q, struct tcp_sock, tsq_node);
1054*4882a593Smuzhiyun 		list_del(&tp->tsq_node);
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 		sk = (struct sock *)tp;
1057*4882a593Smuzhiyun 		smp_mb__before_atomic();
1058*4882a593Smuzhiyun 		clear_bit(TSQ_QUEUED, &sk->sk_tsq_flags);
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 		tcp_tsq_handler(sk);
1061*4882a593Smuzhiyun 		sk_free(sk);
1062*4882a593Smuzhiyun 	}
1063*4882a593Smuzhiyun }
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun #define TCP_DEFERRED_ALL (TCPF_TSQ_DEFERRED |		\
1066*4882a593Smuzhiyun 			  TCPF_WRITE_TIMER_DEFERRED |	\
1067*4882a593Smuzhiyun 			  TCPF_DELACK_TIMER_DEFERRED |	\
1068*4882a593Smuzhiyun 			  TCPF_MTU_REDUCED_DEFERRED)
1069*4882a593Smuzhiyun /**
1070*4882a593Smuzhiyun  * tcp_release_cb - tcp release_sock() callback
1071*4882a593Smuzhiyun  * @sk: socket
1072*4882a593Smuzhiyun  *
1073*4882a593Smuzhiyun  * called from release_sock() to perform protocol dependent
1074*4882a593Smuzhiyun  * actions before socket release.
1075*4882a593Smuzhiyun  */
tcp_release_cb(struct sock * sk)1076*4882a593Smuzhiyun void tcp_release_cb(struct sock *sk)
1077*4882a593Smuzhiyun {
1078*4882a593Smuzhiyun 	unsigned long flags, nflags;
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun 	/* perform an atomic operation only if at least one flag is set */
1081*4882a593Smuzhiyun 	do {
1082*4882a593Smuzhiyun 		flags = sk->sk_tsq_flags;
1083*4882a593Smuzhiyun 		if (!(flags & TCP_DEFERRED_ALL))
1084*4882a593Smuzhiyun 			return;
1085*4882a593Smuzhiyun 		nflags = flags & ~TCP_DEFERRED_ALL;
1086*4882a593Smuzhiyun 	} while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags);
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 	if (flags & TCPF_TSQ_DEFERRED) {
1089*4882a593Smuzhiyun 		tcp_tsq_write(sk);
1090*4882a593Smuzhiyun 		__sock_put(sk);
1091*4882a593Smuzhiyun 	}
1092*4882a593Smuzhiyun 	/* Here begins the tricky part :
1093*4882a593Smuzhiyun 	 * We are called from release_sock() with :
1094*4882a593Smuzhiyun 	 * 1) BH disabled
1095*4882a593Smuzhiyun 	 * 2) sk_lock.slock spinlock held
1096*4882a593Smuzhiyun 	 * 3) socket owned by us (sk->sk_lock.owned == 1)
1097*4882a593Smuzhiyun 	 *
1098*4882a593Smuzhiyun 	 * But following code is meant to be called from BH handlers,
1099*4882a593Smuzhiyun 	 * so we should keep BH disabled, but early release socket ownership
1100*4882a593Smuzhiyun 	 */
1101*4882a593Smuzhiyun 	sock_release_ownership(sk);
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	if (flags & TCPF_WRITE_TIMER_DEFERRED) {
1104*4882a593Smuzhiyun 		tcp_write_timer_handler(sk);
1105*4882a593Smuzhiyun 		__sock_put(sk);
1106*4882a593Smuzhiyun 	}
1107*4882a593Smuzhiyun 	if (flags & TCPF_DELACK_TIMER_DEFERRED) {
1108*4882a593Smuzhiyun 		tcp_delack_timer_handler(sk);
1109*4882a593Smuzhiyun 		__sock_put(sk);
1110*4882a593Smuzhiyun 	}
1111*4882a593Smuzhiyun 	if (flags & TCPF_MTU_REDUCED_DEFERRED) {
1112*4882a593Smuzhiyun 		inet_csk(sk)->icsk_af_ops->mtu_reduced(sk);
1113*4882a593Smuzhiyun 		__sock_put(sk);
1114*4882a593Smuzhiyun 	}
1115*4882a593Smuzhiyun }
1116*4882a593Smuzhiyun EXPORT_SYMBOL(tcp_release_cb);
1117*4882a593Smuzhiyun 
tcp_tasklet_init(void)1118*4882a593Smuzhiyun void __init tcp_tasklet_init(void)
1119*4882a593Smuzhiyun {
1120*4882a593Smuzhiyun 	int i;
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 	for_each_possible_cpu(i) {
1123*4882a593Smuzhiyun 		struct tsq_tasklet *tsq = &per_cpu(tsq_tasklet, i);
1124*4882a593Smuzhiyun 
1125*4882a593Smuzhiyun 		INIT_LIST_HEAD(&tsq->head);
1126*4882a593Smuzhiyun 		tasklet_init(&tsq->tasklet,
1127*4882a593Smuzhiyun 			     tcp_tasklet_func,
1128*4882a593Smuzhiyun 			     (unsigned long)tsq);
1129*4882a593Smuzhiyun 	}
1130*4882a593Smuzhiyun }
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun /*
1133*4882a593Smuzhiyun  * Write buffer destructor automatically called from kfree_skb.
1134*4882a593Smuzhiyun  * We can't xmit new skbs from this context, as we might already
1135*4882a593Smuzhiyun  * hold qdisc lock.
1136*4882a593Smuzhiyun  */
tcp_wfree(struct sk_buff * skb)1137*4882a593Smuzhiyun void tcp_wfree(struct sk_buff *skb)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun 	struct sock *sk = skb->sk;
1140*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
1141*4882a593Smuzhiyun 	unsigned long flags, nval, oval;
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	/* Keep one reference on sk_wmem_alloc.
1144*4882a593Smuzhiyun 	 * Will be released by sk_free() from here or tcp_tasklet_func()
1145*4882a593Smuzhiyun 	 */
1146*4882a593Smuzhiyun 	WARN_ON(refcount_sub_and_test(skb->truesize - 1, &sk->sk_wmem_alloc));
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	/* If this softirq is serviced by ksoftirqd, we are likely under stress.
1149*4882a593Smuzhiyun 	 * Wait until our queues (qdisc + devices) are drained.
1150*4882a593Smuzhiyun 	 * This gives :
1151*4882a593Smuzhiyun 	 * - less callbacks to tcp_write_xmit(), reducing stress (batches)
1152*4882a593Smuzhiyun 	 * - chance for incoming ACK (processed by another cpu maybe)
1153*4882a593Smuzhiyun 	 *   to migrate this flow (skb->ooo_okay will be eventually set)
1154*4882a593Smuzhiyun 	 */
1155*4882a593Smuzhiyun 	if (refcount_read(&sk->sk_wmem_alloc) >= SKB_TRUESIZE(1) && this_cpu_ksoftirqd() == current)
1156*4882a593Smuzhiyun 		goto out;
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	for (oval = READ_ONCE(sk->sk_tsq_flags);; oval = nval) {
1159*4882a593Smuzhiyun 		struct tsq_tasklet *tsq;
1160*4882a593Smuzhiyun 		bool empty;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 		if (!(oval & TSQF_THROTTLED) || (oval & TSQF_QUEUED))
1163*4882a593Smuzhiyun 			goto out;
1164*4882a593Smuzhiyun 
1165*4882a593Smuzhiyun 		nval = (oval & ~TSQF_THROTTLED) | TSQF_QUEUED;
1166*4882a593Smuzhiyun 		nval = cmpxchg(&sk->sk_tsq_flags, oval, nval);
1167*4882a593Smuzhiyun 		if (nval != oval)
1168*4882a593Smuzhiyun 			continue;
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun 		/* queue this socket to tasklet queue */
1171*4882a593Smuzhiyun 		local_irq_save(flags);
1172*4882a593Smuzhiyun 		tsq = this_cpu_ptr(&tsq_tasklet);
1173*4882a593Smuzhiyun 		empty = list_empty(&tsq->head);
1174*4882a593Smuzhiyun 		list_add(&tp->tsq_node, &tsq->head);
1175*4882a593Smuzhiyun 		if (empty)
1176*4882a593Smuzhiyun 			tasklet_schedule(&tsq->tasklet);
1177*4882a593Smuzhiyun 		local_irq_restore(flags);
1178*4882a593Smuzhiyun 		return;
1179*4882a593Smuzhiyun 	}
1180*4882a593Smuzhiyun out:
1181*4882a593Smuzhiyun 	sk_free(sk);
1182*4882a593Smuzhiyun }
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun /* Note: Called under soft irq.
1185*4882a593Smuzhiyun  * We can call TCP stack right away, unless socket is owned by user.
1186*4882a593Smuzhiyun  */
tcp_pace_kick(struct hrtimer * timer)1187*4882a593Smuzhiyun enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer)
1188*4882a593Smuzhiyun {
1189*4882a593Smuzhiyun 	struct tcp_sock *tp = container_of(timer, struct tcp_sock, pacing_timer);
1190*4882a593Smuzhiyun 	struct sock *sk = (struct sock *)tp;
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun 	tcp_tsq_handler(sk);
1193*4882a593Smuzhiyun 	sock_put(sk);
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
1196*4882a593Smuzhiyun }
1197*4882a593Smuzhiyun 
tcp_update_skb_after_send(struct sock * sk,struct sk_buff * skb,u64 prior_wstamp)1198*4882a593Smuzhiyun static void tcp_update_skb_after_send(struct sock *sk, struct sk_buff *skb,
1199*4882a593Smuzhiyun 				      u64 prior_wstamp)
1200*4882a593Smuzhiyun {
1201*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 	if (sk->sk_pacing_status != SK_PACING_NONE) {
1204*4882a593Smuzhiyun 		unsigned long rate = sk->sk_pacing_rate;
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 		/* Original sch_fq does not pace first 10 MSS
1207*4882a593Smuzhiyun 		 * Note that tp->data_segs_out overflows after 2^32 packets,
1208*4882a593Smuzhiyun 		 * this is a minor annoyance.
1209*4882a593Smuzhiyun 		 */
1210*4882a593Smuzhiyun 		if (rate != ~0UL && rate && tp->data_segs_out >= 10) {
1211*4882a593Smuzhiyun 			u64 len_ns = div64_ul((u64)skb->len * NSEC_PER_SEC, rate);
1212*4882a593Smuzhiyun 			u64 credit = tp->tcp_wstamp_ns - prior_wstamp;
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 			/* take into account OS jitter */
1215*4882a593Smuzhiyun 			len_ns -= min_t(u64, len_ns / 2, credit);
1216*4882a593Smuzhiyun 			tp->tcp_wstamp_ns += len_ns;
1217*4882a593Smuzhiyun 		}
1218*4882a593Smuzhiyun 	}
1219*4882a593Smuzhiyun 	list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue);
1220*4882a593Smuzhiyun }
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun INDIRECT_CALLABLE_DECLARE(int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl));
1223*4882a593Smuzhiyun INDIRECT_CALLABLE_DECLARE(int inet6_csk_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl));
1224*4882a593Smuzhiyun INDIRECT_CALLABLE_DECLARE(void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb));
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun /* This routine actually transmits TCP packets queued in by
1227*4882a593Smuzhiyun  * tcp_do_sendmsg().  This is used by both the initial
1228*4882a593Smuzhiyun  * transmission and possible later retransmissions.
1229*4882a593Smuzhiyun  * All SKB's seen here are completely headerless.  It is our
1230*4882a593Smuzhiyun  * job to build the TCP header, and pass the packet down to
1231*4882a593Smuzhiyun  * IP so it can do the same plus pass the packet off to the
1232*4882a593Smuzhiyun  * device.
1233*4882a593Smuzhiyun  *
1234*4882a593Smuzhiyun  * We are working here with either a clone of the original
1235*4882a593Smuzhiyun  * SKB, or a fresh unique copy made by the retransmit engine.
1236*4882a593Smuzhiyun  */
__tcp_transmit_skb(struct sock * sk,struct sk_buff * skb,int clone_it,gfp_t gfp_mask,u32 rcv_nxt)1237*4882a593Smuzhiyun static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
1238*4882a593Smuzhiyun 			      int clone_it, gfp_t gfp_mask, u32 rcv_nxt)
1239*4882a593Smuzhiyun {
1240*4882a593Smuzhiyun 	const struct inet_connection_sock *icsk = inet_csk(sk);
1241*4882a593Smuzhiyun 	struct inet_sock *inet;
1242*4882a593Smuzhiyun 	struct tcp_sock *tp;
1243*4882a593Smuzhiyun 	struct tcp_skb_cb *tcb;
1244*4882a593Smuzhiyun 	struct tcp_out_options opts;
1245*4882a593Smuzhiyun 	unsigned int tcp_options_size, tcp_header_size;
1246*4882a593Smuzhiyun 	struct sk_buff *oskb = NULL;
1247*4882a593Smuzhiyun 	struct tcp_md5sig_key *md5;
1248*4882a593Smuzhiyun 	struct tcphdr *th;
1249*4882a593Smuzhiyun 	u64 prior_wstamp;
1250*4882a593Smuzhiyun 	int err;
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 	BUG_ON(!skb || !tcp_skb_pcount(skb));
1253*4882a593Smuzhiyun 	tp = tcp_sk(sk);
1254*4882a593Smuzhiyun 	prior_wstamp = tp->tcp_wstamp_ns;
1255*4882a593Smuzhiyun 	tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache);
1256*4882a593Smuzhiyun 	skb->skb_mstamp_ns = tp->tcp_wstamp_ns;
1257*4882a593Smuzhiyun 	if (clone_it) {
1258*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->tx.in_flight = TCP_SKB_CB(skb)->end_seq
1259*4882a593Smuzhiyun 			- tp->snd_una;
1260*4882a593Smuzhiyun 		oskb = skb;
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 		tcp_skb_tsorted_save(oskb) {
1263*4882a593Smuzhiyun 			if (unlikely(skb_cloned(oskb)))
1264*4882a593Smuzhiyun 				skb = pskb_copy(oskb, gfp_mask);
1265*4882a593Smuzhiyun 			else
1266*4882a593Smuzhiyun 				skb = skb_clone(oskb, gfp_mask);
1267*4882a593Smuzhiyun 		} tcp_skb_tsorted_restore(oskb);
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 		if (unlikely(!skb))
1270*4882a593Smuzhiyun 			return -ENOBUFS;
1271*4882a593Smuzhiyun 		/* retransmit skbs might have a non zero value in skb->dev
1272*4882a593Smuzhiyun 		 * because skb->dev is aliased with skb->rbnode.rb_left
1273*4882a593Smuzhiyun 		 */
1274*4882a593Smuzhiyun 		skb->dev = NULL;
1275*4882a593Smuzhiyun 	}
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 	inet = inet_sk(sk);
1278*4882a593Smuzhiyun 	tcb = TCP_SKB_CB(skb);
1279*4882a593Smuzhiyun 	memset(&opts, 0, sizeof(opts));
1280*4882a593Smuzhiyun 
1281*4882a593Smuzhiyun 	if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
1282*4882a593Smuzhiyun 		tcp_options_size = tcp_syn_options(sk, skb, &opts, &md5);
1283*4882a593Smuzhiyun 	} else {
1284*4882a593Smuzhiyun 		tcp_options_size = tcp_established_options(sk, skb, &opts,
1285*4882a593Smuzhiyun 							   &md5);
1286*4882a593Smuzhiyun 		/* Force a PSH flag on all (GSO) packets to expedite GRO flush
1287*4882a593Smuzhiyun 		 * at receiver : This slightly improve GRO performance.
1288*4882a593Smuzhiyun 		 * Note that we do not force the PSH flag for non GSO packets,
1289*4882a593Smuzhiyun 		 * because they might be sent under high congestion events,
1290*4882a593Smuzhiyun 		 * and in this case it is better to delay the delivery of 1-MSS
1291*4882a593Smuzhiyun 		 * packets and thus the corresponding ACK packet that would
1292*4882a593Smuzhiyun 		 * release the following packet.
1293*4882a593Smuzhiyun 		 */
1294*4882a593Smuzhiyun 		if (tcp_skb_pcount(skb) > 1)
1295*4882a593Smuzhiyun 			tcb->tcp_flags |= TCPHDR_PSH;
1296*4882a593Smuzhiyun 	}
1297*4882a593Smuzhiyun 	tcp_header_size = tcp_options_size + sizeof(struct tcphdr);
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun 	/* if no packet is in qdisc/device queue, then allow XPS to select
1300*4882a593Smuzhiyun 	 * another queue. We can be called from tcp_tsq_handler()
1301*4882a593Smuzhiyun 	 * which holds one reference to sk.
1302*4882a593Smuzhiyun 	 *
1303*4882a593Smuzhiyun 	 * TODO: Ideally, in-flight pure ACK packets should not matter here.
1304*4882a593Smuzhiyun 	 * One way to get this would be to set skb->truesize = 2 on them.
1305*4882a593Smuzhiyun 	 */
1306*4882a593Smuzhiyun 	skb->ooo_okay = sk_wmem_alloc_get(sk) < SKB_TRUESIZE(1);
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	/* If we had to use memory reserve to allocate this skb,
1309*4882a593Smuzhiyun 	 * this might cause drops if packet is looped back :
1310*4882a593Smuzhiyun 	 * Other socket might not have SOCK_MEMALLOC.
1311*4882a593Smuzhiyun 	 * Packets not looped back do not care about pfmemalloc.
1312*4882a593Smuzhiyun 	 */
1313*4882a593Smuzhiyun 	skb->pfmemalloc = 0;
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	skb_push(skb, tcp_header_size);
1316*4882a593Smuzhiyun 	skb_reset_transport_header(skb);
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun 	skb_orphan(skb);
1319*4882a593Smuzhiyun 	skb->sk = sk;
1320*4882a593Smuzhiyun 	skb->destructor = skb_is_tcp_pure_ack(skb) ? __sock_wfree : tcp_wfree;
1321*4882a593Smuzhiyun 	skb_set_hash_from_sk(skb, sk);
1322*4882a593Smuzhiyun 	refcount_add(skb->truesize, &sk->sk_wmem_alloc);
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 	skb_set_dst_pending_confirm(skb, sk->sk_dst_pending_confirm);
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	/* Build TCP header and checksum it. */
1327*4882a593Smuzhiyun 	th = (struct tcphdr *)skb->data;
1328*4882a593Smuzhiyun 	th->source		= inet->inet_sport;
1329*4882a593Smuzhiyun 	th->dest		= inet->inet_dport;
1330*4882a593Smuzhiyun 	th->seq			= htonl(tcb->seq);
1331*4882a593Smuzhiyun 	th->ack_seq		= htonl(rcv_nxt);
1332*4882a593Smuzhiyun 	*(((__be16 *)th) + 6)	= htons(((tcp_header_size >> 2) << 12) |
1333*4882a593Smuzhiyun 					tcb->tcp_flags);
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun 	th->check		= 0;
1336*4882a593Smuzhiyun 	th->urg_ptr		= 0;
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 	/* The urg_mode check is necessary during a below snd_una win probe */
1339*4882a593Smuzhiyun 	if (unlikely(tcp_urg_mode(tp) && before(tcb->seq, tp->snd_up))) {
1340*4882a593Smuzhiyun 		if (before(tp->snd_up, tcb->seq + 0x10000)) {
1341*4882a593Smuzhiyun 			th->urg_ptr = htons(tp->snd_up - tcb->seq);
1342*4882a593Smuzhiyun 			th->urg = 1;
1343*4882a593Smuzhiyun 		} else if (after(tcb->seq + 0xFFFF, tp->snd_nxt)) {
1344*4882a593Smuzhiyun 			th->urg_ptr = htons(0xFFFF);
1345*4882a593Smuzhiyun 			th->urg = 1;
1346*4882a593Smuzhiyun 		}
1347*4882a593Smuzhiyun 	}
1348*4882a593Smuzhiyun 
1349*4882a593Smuzhiyun 	tcp_options_write((__be32 *)(th + 1), tp, &opts);
1350*4882a593Smuzhiyun 	skb_shinfo(skb)->gso_type = sk->sk_gso_type;
1351*4882a593Smuzhiyun 	if (likely(!(tcb->tcp_flags & TCPHDR_SYN))) {
1352*4882a593Smuzhiyun 		th->window      = htons(tcp_select_window(sk));
1353*4882a593Smuzhiyun 		tcp_ecn_send(sk, skb, th, tcp_header_size);
1354*4882a593Smuzhiyun 	} else {
1355*4882a593Smuzhiyun 		/* RFC1323: The window in SYN & SYN/ACK segments
1356*4882a593Smuzhiyun 		 * is never scaled.
1357*4882a593Smuzhiyun 		 */
1358*4882a593Smuzhiyun 		th->window	= htons(min(tp->rcv_wnd, 65535U));
1359*4882a593Smuzhiyun 	}
1360*4882a593Smuzhiyun #ifdef CONFIG_TCP_MD5SIG
1361*4882a593Smuzhiyun 	/* Calculate the MD5 hash, as we have all we need now */
1362*4882a593Smuzhiyun 	if (md5) {
1363*4882a593Smuzhiyun 		sk_nocaps_add(sk, NETIF_F_GSO_MASK);
1364*4882a593Smuzhiyun 		tp->af_specific->calc_md5_hash(opts.hash_location,
1365*4882a593Smuzhiyun 					       md5, sk, skb);
1366*4882a593Smuzhiyun 	}
1367*4882a593Smuzhiyun #endif
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	/* BPF prog is the last one writing header option */
1370*4882a593Smuzhiyun 	bpf_skops_write_hdr_opt(sk, skb, NULL, NULL, 0, &opts);
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	INDIRECT_CALL_INET(icsk->icsk_af_ops->send_check,
1373*4882a593Smuzhiyun 			   tcp_v6_send_check, tcp_v4_send_check,
1374*4882a593Smuzhiyun 			   sk, skb);
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	if (likely(tcb->tcp_flags & TCPHDR_ACK))
1377*4882a593Smuzhiyun 		tcp_event_ack_sent(sk, tcp_skb_pcount(skb), rcv_nxt);
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun 	if (skb->len != tcp_header_size) {
1380*4882a593Smuzhiyun 		tcp_event_data_sent(tp, sk);
1381*4882a593Smuzhiyun 		tp->data_segs_out += tcp_skb_pcount(skb);
1382*4882a593Smuzhiyun 		tp->bytes_sent += skb->len - tcp_header_size;
1383*4882a593Smuzhiyun 	}
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq)
1386*4882a593Smuzhiyun 		TCP_ADD_STATS(sock_net(sk), TCP_MIB_OUTSEGS,
1387*4882a593Smuzhiyun 			      tcp_skb_pcount(skb));
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	tp->segs_out += tcp_skb_pcount(skb);
1390*4882a593Smuzhiyun 	/* OK, its time to fill skb_shinfo(skb)->gso_{segs|size} */
1391*4882a593Smuzhiyun 	skb_shinfo(skb)->gso_segs = tcp_skb_pcount(skb);
1392*4882a593Smuzhiyun 	skb_shinfo(skb)->gso_size = tcp_skb_mss(skb);
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	/* Leave earliest departure time in skb->tstamp (skb->skb_mstamp_ns) */
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	/* Cleanup our debris for IP stacks */
1397*4882a593Smuzhiyun 	memset(skb->cb, 0, max(sizeof(struct inet_skb_parm),
1398*4882a593Smuzhiyun 			       sizeof(struct inet6_skb_parm)));
1399*4882a593Smuzhiyun 
1400*4882a593Smuzhiyun 	tcp_add_tx_delay(skb, tp);
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun 	err = INDIRECT_CALL_INET(icsk->icsk_af_ops->queue_xmit,
1403*4882a593Smuzhiyun 				 inet6_csk_xmit, ip_queue_xmit,
1404*4882a593Smuzhiyun 				 sk, skb, &inet->cork.fl);
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	if (unlikely(err > 0)) {
1407*4882a593Smuzhiyun 		tcp_enter_cwr(sk);
1408*4882a593Smuzhiyun 		err = net_xmit_eval(err);
1409*4882a593Smuzhiyun 	}
1410*4882a593Smuzhiyun 	if (!err && oskb) {
1411*4882a593Smuzhiyun 		tcp_update_skb_after_send(sk, oskb, prior_wstamp);
1412*4882a593Smuzhiyun 		tcp_rate_skb_sent(sk, oskb);
1413*4882a593Smuzhiyun 	}
1414*4882a593Smuzhiyun 	return err;
1415*4882a593Smuzhiyun }
1416*4882a593Smuzhiyun 
tcp_transmit_skb(struct sock * sk,struct sk_buff * skb,int clone_it,gfp_t gfp_mask)1417*4882a593Smuzhiyun static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,
1418*4882a593Smuzhiyun 			    gfp_t gfp_mask)
1419*4882a593Smuzhiyun {
1420*4882a593Smuzhiyun 	return __tcp_transmit_skb(sk, skb, clone_it, gfp_mask,
1421*4882a593Smuzhiyun 				  tcp_sk(sk)->rcv_nxt);
1422*4882a593Smuzhiyun }
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun /* This routine just queues the buffer for sending.
1425*4882a593Smuzhiyun  *
1426*4882a593Smuzhiyun  * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames,
1427*4882a593Smuzhiyun  * otherwise socket can stall.
1428*4882a593Smuzhiyun  */
tcp_queue_skb(struct sock * sk,struct sk_buff * skb)1429*4882a593Smuzhiyun static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb)
1430*4882a593Smuzhiyun {
1431*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	/* Advance write_seq and place onto the write_queue. */
1434*4882a593Smuzhiyun 	WRITE_ONCE(tp->write_seq, TCP_SKB_CB(skb)->end_seq);
1435*4882a593Smuzhiyun 	__skb_header_release(skb);
1436*4882a593Smuzhiyun 	tcp_add_write_queue_tail(sk, skb);
1437*4882a593Smuzhiyun 	sk_wmem_queued_add(sk, skb->truesize);
1438*4882a593Smuzhiyun 	sk_mem_charge(sk, skb->truesize);
1439*4882a593Smuzhiyun }
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun /* Initialize TSO segments for a packet. */
tcp_set_skb_tso_segs(struct sk_buff * skb,unsigned int mss_now)1442*4882a593Smuzhiyun static void tcp_set_skb_tso_segs(struct sk_buff *skb, unsigned int mss_now)
1443*4882a593Smuzhiyun {
1444*4882a593Smuzhiyun 	if (skb->len <= mss_now) {
1445*4882a593Smuzhiyun 		/* Avoid the costly divide in the normal
1446*4882a593Smuzhiyun 		 * non-TSO case.
1447*4882a593Smuzhiyun 		 */
1448*4882a593Smuzhiyun 		tcp_skb_pcount_set(skb, 1);
1449*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->tcp_gso_size = 0;
1450*4882a593Smuzhiyun 	} else {
1451*4882a593Smuzhiyun 		tcp_skb_pcount_set(skb, DIV_ROUND_UP(skb->len, mss_now));
1452*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->tcp_gso_size = mss_now;
1453*4882a593Smuzhiyun 	}
1454*4882a593Smuzhiyun }
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun /* Pcount in the middle of the write queue got changed, we need to do various
1457*4882a593Smuzhiyun  * tweaks to fix counters
1458*4882a593Smuzhiyun  */
tcp_adjust_pcount(struct sock * sk,const struct sk_buff * skb,int decr)1459*4882a593Smuzhiyun static void tcp_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int decr)
1460*4882a593Smuzhiyun {
1461*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	tp->packets_out -= decr;
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 	if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
1466*4882a593Smuzhiyun 		tp->sacked_out -= decr;
1467*4882a593Smuzhiyun 	if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)
1468*4882a593Smuzhiyun 		tp->retrans_out -= decr;
1469*4882a593Smuzhiyun 	if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST)
1470*4882a593Smuzhiyun 		tp->lost_out -= decr;
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 	/* Reno case is special. Sigh... */
1473*4882a593Smuzhiyun 	if (tcp_is_reno(tp) && decr > 0)
1474*4882a593Smuzhiyun 		tp->sacked_out -= min_t(u32, tp->sacked_out, decr);
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun 	if (tp->lost_skb_hint &&
1477*4882a593Smuzhiyun 	    before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(tp->lost_skb_hint)->seq) &&
1478*4882a593Smuzhiyun 	    (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
1479*4882a593Smuzhiyun 		tp->lost_cnt_hint -= decr;
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun 	tcp_verify_left_out(tp);
1482*4882a593Smuzhiyun }
1483*4882a593Smuzhiyun 
tcp_has_tx_tstamp(const struct sk_buff * skb)1484*4882a593Smuzhiyun static bool tcp_has_tx_tstamp(const struct sk_buff *skb)
1485*4882a593Smuzhiyun {
1486*4882a593Smuzhiyun 	return TCP_SKB_CB(skb)->txstamp_ack ||
1487*4882a593Smuzhiyun 		(skb_shinfo(skb)->tx_flags & SKBTX_ANY_TSTAMP);
1488*4882a593Smuzhiyun }
1489*4882a593Smuzhiyun 
tcp_fragment_tstamp(struct sk_buff * skb,struct sk_buff * skb2)1490*4882a593Smuzhiyun static void tcp_fragment_tstamp(struct sk_buff *skb, struct sk_buff *skb2)
1491*4882a593Smuzhiyun {
1492*4882a593Smuzhiyun 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun 	if (unlikely(tcp_has_tx_tstamp(skb)) &&
1495*4882a593Smuzhiyun 	    !before(shinfo->tskey, TCP_SKB_CB(skb2)->seq)) {
1496*4882a593Smuzhiyun 		struct skb_shared_info *shinfo2 = skb_shinfo(skb2);
1497*4882a593Smuzhiyun 		u8 tsflags = shinfo->tx_flags & SKBTX_ANY_TSTAMP;
1498*4882a593Smuzhiyun 
1499*4882a593Smuzhiyun 		shinfo->tx_flags &= ~tsflags;
1500*4882a593Smuzhiyun 		shinfo2->tx_flags |= tsflags;
1501*4882a593Smuzhiyun 		swap(shinfo->tskey, shinfo2->tskey);
1502*4882a593Smuzhiyun 		TCP_SKB_CB(skb2)->txstamp_ack = TCP_SKB_CB(skb)->txstamp_ack;
1503*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->txstamp_ack = 0;
1504*4882a593Smuzhiyun 	}
1505*4882a593Smuzhiyun }
1506*4882a593Smuzhiyun 
tcp_skb_fragment_eor(struct sk_buff * skb,struct sk_buff * skb2)1507*4882a593Smuzhiyun static void tcp_skb_fragment_eor(struct sk_buff *skb, struct sk_buff *skb2)
1508*4882a593Smuzhiyun {
1509*4882a593Smuzhiyun 	TCP_SKB_CB(skb2)->eor = TCP_SKB_CB(skb)->eor;
1510*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->eor = 0;
1511*4882a593Smuzhiyun }
1512*4882a593Smuzhiyun 
1513*4882a593Smuzhiyun /* Insert buff after skb on the write or rtx queue of sk.  */
tcp_insert_write_queue_after(struct sk_buff * skb,struct sk_buff * buff,struct sock * sk,enum tcp_queue tcp_queue)1514*4882a593Smuzhiyun static void tcp_insert_write_queue_after(struct sk_buff *skb,
1515*4882a593Smuzhiyun 					 struct sk_buff *buff,
1516*4882a593Smuzhiyun 					 struct sock *sk,
1517*4882a593Smuzhiyun 					 enum tcp_queue tcp_queue)
1518*4882a593Smuzhiyun {
1519*4882a593Smuzhiyun 	if (tcp_queue == TCP_FRAG_IN_WRITE_QUEUE)
1520*4882a593Smuzhiyun 		__skb_queue_after(&sk->sk_write_queue, skb, buff);
1521*4882a593Smuzhiyun 	else
1522*4882a593Smuzhiyun 		tcp_rbtree_insert(&sk->tcp_rtx_queue, buff);
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun 
1525*4882a593Smuzhiyun /* Function to create two new TCP segments.  Shrinks the given segment
1526*4882a593Smuzhiyun  * to the specified size and appends a new segment with the rest of the
1527*4882a593Smuzhiyun  * packet to the list.  This won't be called frequently, I hope.
1528*4882a593Smuzhiyun  * Remember, these are still headerless SKBs at this point.
1529*4882a593Smuzhiyun  */
tcp_fragment(struct sock * sk,enum tcp_queue tcp_queue,struct sk_buff * skb,u32 len,unsigned int mss_now,gfp_t gfp)1530*4882a593Smuzhiyun int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
1531*4882a593Smuzhiyun 		 struct sk_buff *skb, u32 len,
1532*4882a593Smuzhiyun 		 unsigned int mss_now, gfp_t gfp)
1533*4882a593Smuzhiyun {
1534*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
1535*4882a593Smuzhiyun 	struct sk_buff *buff;
1536*4882a593Smuzhiyun 	int nsize, old_factor;
1537*4882a593Smuzhiyun 	long limit;
1538*4882a593Smuzhiyun 	int nlen;
1539*4882a593Smuzhiyun 	u8 flags;
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	if (WARN_ON(len > skb->len))
1542*4882a593Smuzhiyun 		return -EINVAL;
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun 	nsize = skb_headlen(skb) - len;
1545*4882a593Smuzhiyun 	if (nsize < 0)
1546*4882a593Smuzhiyun 		nsize = 0;
1547*4882a593Smuzhiyun 
1548*4882a593Smuzhiyun 	/* tcp_sendmsg() can overshoot sk_wmem_queued by one full size skb.
1549*4882a593Smuzhiyun 	 * We need some allowance to not penalize applications setting small
1550*4882a593Smuzhiyun 	 * SO_SNDBUF values.
1551*4882a593Smuzhiyun 	 * Also allow first and last skb in retransmit queue to be split.
1552*4882a593Smuzhiyun 	 */
1553*4882a593Smuzhiyun 	limit = sk->sk_sndbuf + 2 * SKB_TRUESIZE(GSO_MAX_SIZE);
1554*4882a593Smuzhiyun 	if (unlikely((sk->sk_wmem_queued >> 1) > limit &&
1555*4882a593Smuzhiyun 		     tcp_queue != TCP_FRAG_IN_WRITE_QUEUE &&
1556*4882a593Smuzhiyun 		     skb != tcp_rtx_queue_head(sk) &&
1557*4882a593Smuzhiyun 		     skb != tcp_rtx_queue_tail(sk))) {
1558*4882a593Smuzhiyun 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPWQUEUETOOBIG);
1559*4882a593Smuzhiyun 		return -ENOMEM;
1560*4882a593Smuzhiyun 	}
1561*4882a593Smuzhiyun 
1562*4882a593Smuzhiyun 	if (skb_unclone_keeptruesize(skb, gfp))
1563*4882a593Smuzhiyun 		return -ENOMEM;
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 	/* Get a new skb... force flag on. */
1566*4882a593Smuzhiyun 	buff = sk_stream_alloc_skb(sk, nsize, gfp, true);
1567*4882a593Smuzhiyun 	if (!buff)
1568*4882a593Smuzhiyun 		return -ENOMEM; /* We'll just try again later. */
1569*4882a593Smuzhiyun 	skb_copy_decrypted(buff, skb);
1570*4882a593Smuzhiyun 
1571*4882a593Smuzhiyun 	sk_wmem_queued_add(sk, buff->truesize);
1572*4882a593Smuzhiyun 	sk_mem_charge(sk, buff->truesize);
1573*4882a593Smuzhiyun 	nlen = skb->len - len - nsize;
1574*4882a593Smuzhiyun 	buff->truesize += nlen;
1575*4882a593Smuzhiyun 	skb->truesize -= nlen;
1576*4882a593Smuzhiyun 
1577*4882a593Smuzhiyun 	/* Correct the sequence numbers. */
1578*4882a593Smuzhiyun 	TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
1579*4882a593Smuzhiyun 	TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
1580*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun 	/* PSH and FIN should only be set in the second packet. */
1583*4882a593Smuzhiyun 	flags = TCP_SKB_CB(skb)->tcp_flags;
1584*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH);
1585*4882a593Smuzhiyun 	TCP_SKB_CB(buff)->tcp_flags = flags;
1586*4882a593Smuzhiyun 	TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked;
1587*4882a593Smuzhiyun 	tcp_skb_fragment_eor(skb, buff);
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	skb_split(skb, buff, len);
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	buff->ip_summed = CHECKSUM_PARTIAL;
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun 	buff->tstamp = skb->tstamp;
1594*4882a593Smuzhiyun 	tcp_fragment_tstamp(skb, buff);
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun 	old_factor = tcp_skb_pcount(skb);
1597*4882a593Smuzhiyun 
1598*4882a593Smuzhiyun 	/* Fix up tso_factor for both original and new SKB.  */
1599*4882a593Smuzhiyun 	tcp_set_skb_tso_segs(skb, mss_now);
1600*4882a593Smuzhiyun 	tcp_set_skb_tso_segs(buff, mss_now);
1601*4882a593Smuzhiyun 
1602*4882a593Smuzhiyun 	/* Update delivered info for the new segment */
1603*4882a593Smuzhiyun 	TCP_SKB_CB(buff)->tx = TCP_SKB_CB(skb)->tx;
1604*4882a593Smuzhiyun 
1605*4882a593Smuzhiyun 	/* If this packet has been sent out already, we must
1606*4882a593Smuzhiyun 	 * adjust the various packet counters.
1607*4882a593Smuzhiyun 	 */
1608*4882a593Smuzhiyun 	if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) {
1609*4882a593Smuzhiyun 		int diff = old_factor - tcp_skb_pcount(skb) -
1610*4882a593Smuzhiyun 			tcp_skb_pcount(buff);
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun 		if (diff)
1613*4882a593Smuzhiyun 			tcp_adjust_pcount(sk, skb, diff);
1614*4882a593Smuzhiyun 	}
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun 	/* Link BUFF into the send queue. */
1617*4882a593Smuzhiyun 	__skb_header_release(buff);
1618*4882a593Smuzhiyun 	tcp_insert_write_queue_after(skb, buff, sk, tcp_queue);
1619*4882a593Smuzhiyun 	if (tcp_queue == TCP_FRAG_IN_RTX_QUEUE)
1620*4882a593Smuzhiyun 		list_add(&buff->tcp_tsorted_anchor, &skb->tcp_tsorted_anchor);
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	return 0;
1623*4882a593Smuzhiyun }
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun /* This is similar to __pskb_pull_tail(). The difference is that pulled
1626*4882a593Smuzhiyun  * data is not copied, but immediately discarded.
1627*4882a593Smuzhiyun  */
__pskb_trim_head(struct sk_buff * skb,int len)1628*4882a593Smuzhiyun static int __pskb_trim_head(struct sk_buff *skb, int len)
1629*4882a593Smuzhiyun {
1630*4882a593Smuzhiyun 	struct skb_shared_info *shinfo;
1631*4882a593Smuzhiyun 	int i, k, eat;
1632*4882a593Smuzhiyun 
1633*4882a593Smuzhiyun 	eat = min_t(int, len, skb_headlen(skb));
1634*4882a593Smuzhiyun 	if (eat) {
1635*4882a593Smuzhiyun 		__skb_pull(skb, eat);
1636*4882a593Smuzhiyun 		len -= eat;
1637*4882a593Smuzhiyun 		if (!len)
1638*4882a593Smuzhiyun 			return 0;
1639*4882a593Smuzhiyun 	}
1640*4882a593Smuzhiyun 	eat = len;
1641*4882a593Smuzhiyun 	k = 0;
1642*4882a593Smuzhiyun 	shinfo = skb_shinfo(skb);
1643*4882a593Smuzhiyun 	for (i = 0; i < shinfo->nr_frags; i++) {
1644*4882a593Smuzhiyun 		int size = skb_frag_size(&shinfo->frags[i]);
1645*4882a593Smuzhiyun 
1646*4882a593Smuzhiyun 		if (size <= eat) {
1647*4882a593Smuzhiyun 			skb_frag_unref(skb, i);
1648*4882a593Smuzhiyun 			eat -= size;
1649*4882a593Smuzhiyun 		} else {
1650*4882a593Smuzhiyun 			shinfo->frags[k] = shinfo->frags[i];
1651*4882a593Smuzhiyun 			if (eat) {
1652*4882a593Smuzhiyun 				skb_frag_off_add(&shinfo->frags[k], eat);
1653*4882a593Smuzhiyun 				skb_frag_size_sub(&shinfo->frags[k], eat);
1654*4882a593Smuzhiyun 				eat = 0;
1655*4882a593Smuzhiyun 			}
1656*4882a593Smuzhiyun 			k++;
1657*4882a593Smuzhiyun 		}
1658*4882a593Smuzhiyun 	}
1659*4882a593Smuzhiyun 	shinfo->nr_frags = k;
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun 	skb->data_len -= len;
1662*4882a593Smuzhiyun 	skb->len = skb->data_len;
1663*4882a593Smuzhiyun 	return len;
1664*4882a593Smuzhiyun }
1665*4882a593Smuzhiyun 
1666*4882a593Smuzhiyun /* Remove acked data from a packet in the transmit queue. */
tcp_trim_head(struct sock * sk,struct sk_buff * skb,u32 len)1667*4882a593Smuzhiyun int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len)
1668*4882a593Smuzhiyun {
1669*4882a593Smuzhiyun 	u32 delta_truesize;
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun 	if (skb_unclone_keeptruesize(skb, GFP_ATOMIC))
1672*4882a593Smuzhiyun 		return -ENOMEM;
1673*4882a593Smuzhiyun 
1674*4882a593Smuzhiyun 	delta_truesize = __pskb_trim_head(skb, len);
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->seq += len;
1677*4882a593Smuzhiyun 	skb->ip_summed = CHECKSUM_PARTIAL;
1678*4882a593Smuzhiyun 
1679*4882a593Smuzhiyun 	if (delta_truesize) {
1680*4882a593Smuzhiyun 		skb->truesize	   -= delta_truesize;
1681*4882a593Smuzhiyun 		sk_wmem_queued_add(sk, -delta_truesize);
1682*4882a593Smuzhiyun 		sk_mem_uncharge(sk, delta_truesize);
1683*4882a593Smuzhiyun 	}
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun 	/* Any change of skb->len requires recalculation of tso factor. */
1686*4882a593Smuzhiyun 	if (tcp_skb_pcount(skb) > 1)
1687*4882a593Smuzhiyun 		tcp_set_skb_tso_segs(skb, tcp_skb_mss(skb));
1688*4882a593Smuzhiyun 
1689*4882a593Smuzhiyun 	return 0;
1690*4882a593Smuzhiyun }
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun /* Calculate MSS not accounting any TCP options.  */
__tcp_mtu_to_mss(struct sock * sk,int pmtu)1693*4882a593Smuzhiyun static inline int __tcp_mtu_to_mss(struct sock *sk, int pmtu)
1694*4882a593Smuzhiyun {
1695*4882a593Smuzhiyun 	const struct tcp_sock *tp = tcp_sk(sk);
1696*4882a593Smuzhiyun 	const struct inet_connection_sock *icsk = inet_csk(sk);
1697*4882a593Smuzhiyun 	int mss_now;
1698*4882a593Smuzhiyun 
1699*4882a593Smuzhiyun 	/* Calculate base mss without TCP options:
1700*4882a593Smuzhiyun 	   It is MMS_S - sizeof(tcphdr) of rfc1122
1701*4882a593Smuzhiyun 	 */
1702*4882a593Smuzhiyun 	mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr);
1703*4882a593Smuzhiyun 
1704*4882a593Smuzhiyun 	/* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */
1705*4882a593Smuzhiyun 	if (icsk->icsk_af_ops->net_frag_header_len) {
1706*4882a593Smuzhiyun 		const struct dst_entry *dst = __sk_dst_get(sk);
1707*4882a593Smuzhiyun 
1708*4882a593Smuzhiyun 		if (dst && dst_allfrag(dst))
1709*4882a593Smuzhiyun 			mss_now -= icsk->icsk_af_ops->net_frag_header_len;
1710*4882a593Smuzhiyun 	}
1711*4882a593Smuzhiyun 
1712*4882a593Smuzhiyun 	/* Clamp it (mss_clamp does not include tcp options) */
1713*4882a593Smuzhiyun 	if (mss_now > tp->rx_opt.mss_clamp)
1714*4882a593Smuzhiyun 		mss_now = tp->rx_opt.mss_clamp;
1715*4882a593Smuzhiyun 
1716*4882a593Smuzhiyun 	/* Now subtract optional transport overhead */
1717*4882a593Smuzhiyun 	mss_now -= icsk->icsk_ext_hdr_len;
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun 	/* Then reserve room for full set of TCP options and 8 bytes of data */
1720*4882a593Smuzhiyun 	mss_now = max(mss_now,
1721*4882a593Smuzhiyun 		      READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_snd_mss));
1722*4882a593Smuzhiyun 	return mss_now;
1723*4882a593Smuzhiyun }
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun /* Calculate MSS. Not accounting for SACKs here.  */
tcp_mtu_to_mss(struct sock * sk,int pmtu)1726*4882a593Smuzhiyun int tcp_mtu_to_mss(struct sock *sk, int pmtu)
1727*4882a593Smuzhiyun {
1728*4882a593Smuzhiyun 	/* Subtract TCP options size, not including SACKs */
1729*4882a593Smuzhiyun 	return __tcp_mtu_to_mss(sk, pmtu) -
1730*4882a593Smuzhiyun 	       (tcp_sk(sk)->tcp_header_len - sizeof(struct tcphdr));
1731*4882a593Smuzhiyun }
1732*4882a593Smuzhiyun EXPORT_SYMBOL(tcp_mtu_to_mss);
1733*4882a593Smuzhiyun 
1734*4882a593Smuzhiyun /* Inverse of above */
tcp_mss_to_mtu(struct sock * sk,int mss)1735*4882a593Smuzhiyun int tcp_mss_to_mtu(struct sock *sk, int mss)
1736*4882a593Smuzhiyun {
1737*4882a593Smuzhiyun 	const struct tcp_sock *tp = tcp_sk(sk);
1738*4882a593Smuzhiyun 	const struct inet_connection_sock *icsk = inet_csk(sk);
1739*4882a593Smuzhiyun 	int mtu;
1740*4882a593Smuzhiyun 
1741*4882a593Smuzhiyun 	mtu = mss +
1742*4882a593Smuzhiyun 	      tp->tcp_header_len +
1743*4882a593Smuzhiyun 	      icsk->icsk_ext_hdr_len +
1744*4882a593Smuzhiyun 	      icsk->icsk_af_ops->net_header_len;
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun 	/* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */
1747*4882a593Smuzhiyun 	if (icsk->icsk_af_ops->net_frag_header_len) {
1748*4882a593Smuzhiyun 		const struct dst_entry *dst = __sk_dst_get(sk);
1749*4882a593Smuzhiyun 
1750*4882a593Smuzhiyun 		if (dst && dst_allfrag(dst))
1751*4882a593Smuzhiyun 			mtu += icsk->icsk_af_ops->net_frag_header_len;
1752*4882a593Smuzhiyun 	}
1753*4882a593Smuzhiyun 	return mtu;
1754*4882a593Smuzhiyun }
1755*4882a593Smuzhiyun EXPORT_SYMBOL(tcp_mss_to_mtu);
1756*4882a593Smuzhiyun 
1757*4882a593Smuzhiyun /* MTU probing init per socket */
tcp_mtup_init(struct sock * sk)1758*4882a593Smuzhiyun void tcp_mtup_init(struct sock *sk)
1759*4882a593Smuzhiyun {
1760*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
1761*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
1762*4882a593Smuzhiyun 	struct net *net = sock_net(sk);
1763*4882a593Smuzhiyun 
1764*4882a593Smuzhiyun 	icsk->icsk_mtup.enabled = READ_ONCE(net->ipv4.sysctl_tcp_mtu_probing) > 1;
1765*4882a593Smuzhiyun 	icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) +
1766*4882a593Smuzhiyun 			       icsk->icsk_af_ops->net_header_len;
1767*4882a593Smuzhiyun 	icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, READ_ONCE(net->ipv4.sysctl_tcp_base_mss));
1768*4882a593Smuzhiyun 	icsk->icsk_mtup.probe_size = 0;
1769*4882a593Smuzhiyun 	if (icsk->icsk_mtup.enabled)
1770*4882a593Smuzhiyun 		icsk->icsk_mtup.probe_timestamp = tcp_jiffies32;
1771*4882a593Smuzhiyun }
1772*4882a593Smuzhiyun EXPORT_SYMBOL(tcp_mtup_init);
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun /* This function synchronize snd mss to current pmtu/exthdr set.
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun    tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts
1777*4882a593Smuzhiyun    for TCP options, but includes only bare TCP header.
1778*4882a593Smuzhiyun 
1779*4882a593Smuzhiyun    tp->rx_opt.mss_clamp is mss negotiated at connection setup.
1780*4882a593Smuzhiyun    It is minimum of user_mss and mss received with SYN.
1781*4882a593Smuzhiyun    It also does not include TCP options.
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun    inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function.
1784*4882a593Smuzhiyun 
1785*4882a593Smuzhiyun    tp->mss_cache is current effective sending mss, including
1786*4882a593Smuzhiyun    all tcp options except for SACKs. It is evaluated,
1787*4882a593Smuzhiyun    taking into account current pmtu, but never exceeds
1788*4882a593Smuzhiyun    tp->rx_opt.mss_clamp.
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun    NOTE1. rfc1122 clearly states that advertised MSS
1791*4882a593Smuzhiyun    DOES NOT include either tcp or ip options.
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun    NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache
1794*4882a593Smuzhiyun    are READ ONLY outside this function.		--ANK (980731)
1795*4882a593Smuzhiyun  */
tcp_sync_mss(struct sock * sk,u32 pmtu)1796*4882a593Smuzhiyun unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu)
1797*4882a593Smuzhiyun {
1798*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
1799*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
1800*4882a593Smuzhiyun 	int mss_now;
1801*4882a593Smuzhiyun 
1802*4882a593Smuzhiyun 	if (icsk->icsk_mtup.search_high > pmtu)
1803*4882a593Smuzhiyun 		icsk->icsk_mtup.search_high = pmtu;
1804*4882a593Smuzhiyun 
1805*4882a593Smuzhiyun 	mss_now = tcp_mtu_to_mss(sk, pmtu);
1806*4882a593Smuzhiyun 	mss_now = tcp_bound_to_half_wnd(tp, mss_now);
1807*4882a593Smuzhiyun 
1808*4882a593Smuzhiyun 	/* And store cached results */
1809*4882a593Smuzhiyun 	icsk->icsk_pmtu_cookie = pmtu;
1810*4882a593Smuzhiyun 	if (icsk->icsk_mtup.enabled)
1811*4882a593Smuzhiyun 		mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low));
1812*4882a593Smuzhiyun 	tp->mss_cache = mss_now;
1813*4882a593Smuzhiyun 
1814*4882a593Smuzhiyun 	return mss_now;
1815*4882a593Smuzhiyun }
1816*4882a593Smuzhiyun EXPORT_SYMBOL(tcp_sync_mss);
1817*4882a593Smuzhiyun 
1818*4882a593Smuzhiyun /* Compute the current effective MSS, taking SACKs and IP options,
1819*4882a593Smuzhiyun  * and even PMTU discovery events into account.
1820*4882a593Smuzhiyun  */
tcp_current_mss(struct sock * sk)1821*4882a593Smuzhiyun unsigned int tcp_current_mss(struct sock *sk)
1822*4882a593Smuzhiyun {
1823*4882a593Smuzhiyun 	const struct tcp_sock *tp = tcp_sk(sk);
1824*4882a593Smuzhiyun 	const struct dst_entry *dst = __sk_dst_get(sk);
1825*4882a593Smuzhiyun 	u32 mss_now;
1826*4882a593Smuzhiyun 	unsigned int header_len;
1827*4882a593Smuzhiyun 	struct tcp_out_options opts;
1828*4882a593Smuzhiyun 	struct tcp_md5sig_key *md5;
1829*4882a593Smuzhiyun 
1830*4882a593Smuzhiyun 	mss_now = tp->mss_cache;
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun 	if (dst) {
1833*4882a593Smuzhiyun 		u32 mtu = dst_mtu(dst);
1834*4882a593Smuzhiyun 		if (mtu != inet_csk(sk)->icsk_pmtu_cookie)
1835*4882a593Smuzhiyun 			mss_now = tcp_sync_mss(sk, mtu);
1836*4882a593Smuzhiyun 	}
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 	header_len = tcp_established_options(sk, NULL, &opts, &md5) +
1839*4882a593Smuzhiyun 		     sizeof(struct tcphdr);
1840*4882a593Smuzhiyun 	/* The mss_cache is sized based on tp->tcp_header_len, which assumes
1841*4882a593Smuzhiyun 	 * some common options. If this is an odd packet (because we have SACK
1842*4882a593Smuzhiyun 	 * blocks etc) then our calculated header_len will be different, and
1843*4882a593Smuzhiyun 	 * we have to adjust mss_now correspondingly */
1844*4882a593Smuzhiyun 	if (header_len != tp->tcp_header_len) {
1845*4882a593Smuzhiyun 		int delta = (int) header_len - tp->tcp_header_len;
1846*4882a593Smuzhiyun 		mss_now -= delta;
1847*4882a593Smuzhiyun 	}
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun 	return mss_now;
1850*4882a593Smuzhiyun }
1851*4882a593Smuzhiyun 
1852*4882a593Smuzhiyun /* RFC2861, slow part. Adjust cwnd, after it was not full during one rto.
1853*4882a593Smuzhiyun  * As additional protections, we do not touch cwnd in retransmission phases,
1854*4882a593Smuzhiyun  * and if application hit its sndbuf limit recently.
1855*4882a593Smuzhiyun  */
tcp_cwnd_application_limited(struct sock * sk)1856*4882a593Smuzhiyun static void tcp_cwnd_application_limited(struct sock *sk)
1857*4882a593Smuzhiyun {
1858*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	if (inet_csk(sk)->icsk_ca_state == TCP_CA_Open &&
1861*4882a593Smuzhiyun 	    sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1862*4882a593Smuzhiyun 		/* Limited by application or receiver window. */
1863*4882a593Smuzhiyun 		u32 init_win = tcp_init_cwnd(tp, __sk_dst_get(sk));
1864*4882a593Smuzhiyun 		u32 win_used = max(tp->snd_cwnd_used, init_win);
1865*4882a593Smuzhiyun 		if (win_used < tp->snd_cwnd) {
1866*4882a593Smuzhiyun 			tp->snd_ssthresh = tcp_current_ssthresh(sk);
1867*4882a593Smuzhiyun 			tp->snd_cwnd = (tp->snd_cwnd + win_used) >> 1;
1868*4882a593Smuzhiyun 		}
1869*4882a593Smuzhiyun 		tp->snd_cwnd_used = 0;
1870*4882a593Smuzhiyun 	}
1871*4882a593Smuzhiyun 	tp->snd_cwnd_stamp = tcp_jiffies32;
1872*4882a593Smuzhiyun }
1873*4882a593Smuzhiyun 
tcp_cwnd_validate(struct sock * sk,bool is_cwnd_limited)1874*4882a593Smuzhiyun static void tcp_cwnd_validate(struct sock *sk, bool is_cwnd_limited)
1875*4882a593Smuzhiyun {
1876*4882a593Smuzhiyun 	const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
1877*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
1878*4882a593Smuzhiyun 
1879*4882a593Smuzhiyun 	/* Track the strongest available signal of the degree to which the cwnd
1880*4882a593Smuzhiyun 	 * is fully utilized. If cwnd-limited then remember that fact for the
1881*4882a593Smuzhiyun 	 * current window. If not cwnd-limited then track the maximum number of
1882*4882a593Smuzhiyun 	 * outstanding packets in the current window. (If cwnd-limited then we
1883*4882a593Smuzhiyun 	 * chose to not update tp->max_packets_out to avoid an extra else
1884*4882a593Smuzhiyun 	 * clause with no functional impact.)
1885*4882a593Smuzhiyun 	 */
1886*4882a593Smuzhiyun 	if (!before(tp->snd_una, tp->cwnd_usage_seq) ||
1887*4882a593Smuzhiyun 	    is_cwnd_limited ||
1888*4882a593Smuzhiyun 	    (!tp->is_cwnd_limited &&
1889*4882a593Smuzhiyun 	     tp->packets_out > tp->max_packets_out)) {
1890*4882a593Smuzhiyun 		tp->is_cwnd_limited = is_cwnd_limited;
1891*4882a593Smuzhiyun 		tp->max_packets_out = tp->packets_out;
1892*4882a593Smuzhiyun 		tp->cwnd_usage_seq = tp->snd_nxt;
1893*4882a593Smuzhiyun 	}
1894*4882a593Smuzhiyun 
1895*4882a593Smuzhiyun 	if (tcp_is_cwnd_limited(sk)) {
1896*4882a593Smuzhiyun 		/* Network is feed fully. */
1897*4882a593Smuzhiyun 		tp->snd_cwnd_used = 0;
1898*4882a593Smuzhiyun 		tp->snd_cwnd_stamp = tcp_jiffies32;
1899*4882a593Smuzhiyun 	} else {
1900*4882a593Smuzhiyun 		/* Network starves. */
1901*4882a593Smuzhiyun 		if (tp->packets_out > tp->snd_cwnd_used)
1902*4882a593Smuzhiyun 			tp->snd_cwnd_used = tp->packets_out;
1903*4882a593Smuzhiyun 
1904*4882a593Smuzhiyun 		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle) &&
1905*4882a593Smuzhiyun 		    (s32)(tcp_jiffies32 - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto &&
1906*4882a593Smuzhiyun 		    !ca_ops->cong_control)
1907*4882a593Smuzhiyun 			tcp_cwnd_application_limited(sk);
1908*4882a593Smuzhiyun 
1909*4882a593Smuzhiyun 		/* The following conditions together indicate the starvation
1910*4882a593Smuzhiyun 		 * is caused by insufficient sender buffer:
1911*4882a593Smuzhiyun 		 * 1) just sent some data (see tcp_write_xmit)
1912*4882a593Smuzhiyun 		 * 2) not cwnd limited (this else condition)
1913*4882a593Smuzhiyun 		 * 3) no more data to send (tcp_write_queue_empty())
1914*4882a593Smuzhiyun 		 * 4) application is hitting buffer limit (SOCK_NOSPACE)
1915*4882a593Smuzhiyun 		 */
1916*4882a593Smuzhiyun 		if (tcp_write_queue_empty(sk) && sk->sk_socket &&
1917*4882a593Smuzhiyun 		    test_bit(SOCK_NOSPACE, &sk->sk_socket->flags) &&
1918*4882a593Smuzhiyun 		    (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT))
1919*4882a593Smuzhiyun 			tcp_chrono_start(sk, TCP_CHRONO_SNDBUF_LIMITED);
1920*4882a593Smuzhiyun 	}
1921*4882a593Smuzhiyun }
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun /* Minshall's variant of the Nagle send check. */
tcp_minshall_check(const struct tcp_sock * tp)1924*4882a593Smuzhiyun static bool tcp_minshall_check(const struct tcp_sock *tp)
1925*4882a593Smuzhiyun {
1926*4882a593Smuzhiyun 	return after(tp->snd_sml, tp->snd_una) &&
1927*4882a593Smuzhiyun 		!after(tp->snd_sml, tp->snd_nxt);
1928*4882a593Smuzhiyun }
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun /* Update snd_sml if this skb is under mss
1931*4882a593Smuzhiyun  * Note that a TSO packet might end with a sub-mss segment
1932*4882a593Smuzhiyun  * The test is really :
1933*4882a593Smuzhiyun  * if ((skb->len % mss) != 0)
1934*4882a593Smuzhiyun  *        tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
1935*4882a593Smuzhiyun  * But we can avoid doing the divide again given we already have
1936*4882a593Smuzhiyun  *  skb_pcount = skb->len / mss_now
1937*4882a593Smuzhiyun  */
tcp_minshall_update(struct tcp_sock * tp,unsigned int mss_now,const struct sk_buff * skb)1938*4882a593Smuzhiyun static void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss_now,
1939*4882a593Smuzhiyun 				const struct sk_buff *skb)
1940*4882a593Smuzhiyun {
1941*4882a593Smuzhiyun 	if (skb->len < tcp_skb_pcount(skb) * mss_now)
1942*4882a593Smuzhiyun 		tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
1943*4882a593Smuzhiyun }
1944*4882a593Smuzhiyun 
1945*4882a593Smuzhiyun /* Return false, if packet can be sent now without violation Nagle's rules:
1946*4882a593Smuzhiyun  * 1. It is full sized. (provided by caller in %partial bool)
1947*4882a593Smuzhiyun  * 2. Or it contains FIN. (already checked by caller)
1948*4882a593Smuzhiyun  * 3. Or TCP_CORK is not set, and TCP_NODELAY is set.
1949*4882a593Smuzhiyun  * 4. Or TCP_CORK is not set, and all sent packets are ACKed.
1950*4882a593Smuzhiyun  *    With Minshall's modification: all sent small packets are ACKed.
1951*4882a593Smuzhiyun  */
tcp_nagle_check(bool partial,const struct tcp_sock * tp,int nonagle)1952*4882a593Smuzhiyun static bool tcp_nagle_check(bool partial, const struct tcp_sock *tp,
1953*4882a593Smuzhiyun 			    int nonagle)
1954*4882a593Smuzhiyun {
1955*4882a593Smuzhiyun 	return partial &&
1956*4882a593Smuzhiyun 		((nonagle & TCP_NAGLE_CORK) ||
1957*4882a593Smuzhiyun 		 (!nonagle && tp->packets_out && tcp_minshall_check(tp)));
1958*4882a593Smuzhiyun }
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun /* Return how many segs we'd like on a TSO packet,
1961*4882a593Smuzhiyun  * to send one TSO packet per ms
1962*4882a593Smuzhiyun  */
tcp_tso_autosize(const struct sock * sk,unsigned int mss_now,int min_tso_segs)1963*4882a593Smuzhiyun static u32 tcp_tso_autosize(const struct sock *sk, unsigned int mss_now,
1964*4882a593Smuzhiyun 			    int min_tso_segs)
1965*4882a593Smuzhiyun {
1966*4882a593Smuzhiyun 	u32 bytes, segs;
1967*4882a593Smuzhiyun 
1968*4882a593Smuzhiyun 	bytes = min_t(unsigned long,
1969*4882a593Smuzhiyun 		      sk->sk_pacing_rate >> READ_ONCE(sk->sk_pacing_shift),
1970*4882a593Smuzhiyun 		      sk->sk_gso_max_size - 1 - MAX_TCP_HEADER);
1971*4882a593Smuzhiyun 
1972*4882a593Smuzhiyun 	/* Goal is to send at least one packet per ms,
1973*4882a593Smuzhiyun 	 * not one big TSO packet every 100 ms.
1974*4882a593Smuzhiyun 	 * This preserves ACK clocking and is consistent
1975*4882a593Smuzhiyun 	 * with tcp_tso_should_defer() heuristic.
1976*4882a593Smuzhiyun 	 */
1977*4882a593Smuzhiyun 	segs = max_t(u32, bytes / mss_now, min_tso_segs);
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun 	return segs;
1980*4882a593Smuzhiyun }
1981*4882a593Smuzhiyun 
1982*4882a593Smuzhiyun /* Return the number of segments we want in the skb we are transmitting.
1983*4882a593Smuzhiyun  * See if congestion control module wants to decide; otherwise, autosize.
1984*4882a593Smuzhiyun  */
tcp_tso_segs(struct sock * sk,unsigned int mss_now)1985*4882a593Smuzhiyun static u32 tcp_tso_segs(struct sock *sk, unsigned int mss_now)
1986*4882a593Smuzhiyun {
1987*4882a593Smuzhiyun 	const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
1988*4882a593Smuzhiyun 	u32 min_tso, tso_segs;
1989*4882a593Smuzhiyun 
1990*4882a593Smuzhiyun 	min_tso = ca_ops->min_tso_segs ?
1991*4882a593Smuzhiyun 			ca_ops->min_tso_segs(sk) :
1992*4882a593Smuzhiyun 			READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_tso_segs);
1993*4882a593Smuzhiyun 
1994*4882a593Smuzhiyun 	tso_segs = tcp_tso_autosize(sk, mss_now, min_tso);
1995*4882a593Smuzhiyun 	return min_t(u32, tso_segs, sk->sk_gso_max_segs);
1996*4882a593Smuzhiyun }
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun /* Returns the portion of skb which can be sent right away */
tcp_mss_split_point(const struct sock * sk,const struct sk_buff * skb,unsigned int mss_now,unsigned int max_segs,int nonagle)1999*4882a593Smuzhiyun static unsigned int tcp_mss_split_point(const struct sock *sk,
2000*4882a593Smuzhiyun 					const struct sk_buff *skb,
2001*4882a593Smuzhiyun 					unsigned int mss_now,
2002*4882a593Smuzhiyun 					unsigned int max_segs,
2003*4882a593Smuzhiyun 					int nonagle)
2004*4882a593Smuzhiyun {
2005*4882a593Smuzhiyun 	const struct tcp_sock *tp = tcp_sk(sk);
2006*4882a593Smuzhiyun 	u32 partial, needed, window, max_len;
2007*4882a593Smuzhiyun 
2008*4882a593Smuzhiyun 	window = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
2009*4882a593Smuzhiyun 	max_len = mss_now * max_segs;
2010*4882a593Smuzhiyun 
2011*4882a593Smuzhiyun 	if (likely(max_len <= window && skb != tcp_write_queue_tail(sk)))
2012*4882a593Smuzhiyun 		return max_len;
2013*4882a593Smuzhiyun 
2014*4882a593Smuzhiyun 	needed = min(skb->len, window);
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	if (max_len <= needed)
2017*4882a593Smuzhiyun 		return max_len;
2018*4882a593Smuzhiyun 
2019*4882a593Smuzhiyun 	partial = needed % mss_now;
2020*4882a593Smuzhiyun 	/* If last segment is not a full MSS, check if Nagle rules allow us
2021*4882a593Smuzhiyun 	 * to include this last segment in this skb.
2022*4882a593Smuzhiyun 	 * Otherwise, we'll split the skb at last MSS boundary
2023*4882a593Smuzhiyun 	 */
2024*4882a593Smuzhiyun 	if (tcp_nagle_check(partial != 0, tp, nonagle))
2025*4882a593Smuzhiyun 		return needed - partial;
2026*4882a593Smuzhiyun 
2027*4882a593Smuzhiyun 	return needed;
2028*4882a593Smuzhiyun }
2029*4882a593Smuzhiyun 
2030*4882a593Smuzhiyun /* Can at least one segment of SKB be sent right now, according to the
2031*4882a593Smuzhiyun  * congestion window rules?  If so, return how many segments are allowed.
2032*4882a593Smuzhiyun  */
tcp_cwnd_test(const struct tcp_sock * tp,const struct sk_buff * skb)2033*4882a593Smuzhiyun static inline unsigned int tcp_cwnd_test(const struct tcp_sock *tp,
2034*4882a593Smuzhiyun 					 const struct sk_buff *skb)
2035*4882a593Smuzhiyun {
2036*4882a593Smuzhiyun 	u32 in_flight, cwnd, halfcwnd;
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 	/* Don't be strict about the congestion window for the final FIN.  */
2039*4882a593Smuzhiyun 	if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) &&
2040*4882a593Smuzhiyun 	    tcp_skb_pcount(skb) == 1)
2041*4882a593Smuzhiyun 		return 1;
2042*4882a593Smuzhiyun 
2043*4882a593Smuzhiyun 	in_flight = tcp_packets_in_flight(tp);
2044*4882a593Smuzhiyun 	cwnd = tp->snd_cwnd;
2045*4882a593Smuzhiyun 	if (in_flight >= cwnd)
2046*4882a593Smuzhiyun 		return 0;
2047*4882a593Smuzhiyun 
2048*4882a593Smuzhiyun 	/* For better scheduling, ensure we have at least
2049*4882a593Smuzhiyun 	 * 2 GSO packets in flight.
2050*4882a593Smuzhiyun 	 */
2051*4882a593Smuzhiyun 	halfcwnd = max(cwnd >> 1, 1U);
2052*4882a593Smuzhiyun 	return min(halfcwnd, cwnd - in_flight);
2053*4882a593Smuzhiyun }
2054*4882a593Smuzhiyun 
2055*4882a593Smuzhiyun /* Initialize TSO state of a skb.
2056*4882a593Smuzhiyun  * This must be invoked the first time we consider transmitting
2057*4882a593Smuzhiyun  * SKB onto the wire.
2058*4882a593Smuzhiyun  */
tcp_init_tso_segs(struct sk_buff * skb,unsigned int mss_now)2059*4882a593Smuzhiyun static int tcp_init_tso_segs(struct sk_buff *skb, unsigned int mss_now)
2060*4882a593Smuzhiyun {
2061*4882a593Smuzhiyun 	int tso_segs = tcp_skb_pcount(skb);
2062*4882a593Smuzhiyun 
2063*4882a593Smuzhiyun 	if (!tso_segs || (tso_segs > 1 && tcp_skb_mss(skb) != mss_now)) {
2064*4882a593Smuzhiyun 		tcp_set_skb_tso_segs(skb, mss_now);
2065*4882a593Smuzhiyun 		tso_segs = tcp_skb_pcount(skb);
2066*4882a593Smuzhiyun 	}
2067*4882a593Smuzhiyun 	return tso_segs;
2068*4882a593Smuzhiyun }
2069*4882a593Smuzhiyun 
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun /* Return true if the Nagle test allows this packet to be
2072*4882a593Smuzhiyun  * sent now.
2073*4882a593Smuzhiyun  */
tcp_nagle_test(const struct tcp_sock * tp,const struct sk_buff * skb,unsigned int cur_mss,int nonagle)2074*4882a593Smuzhiyun static inline bool tcp_nagle_test(const struct tcp_sock *tp, const struct sk_buff *skb,
2075*4882a593Smuzhiyun 				  unsigned int cur_mss, int nonagle)
2076*4882a593Smuzhiyun {
2077*4882a593Smuzhiyun 	/* Nagle rule does not apply to frames, which sit in the middle of the
2078*4882a593Smuzhiyun 	 * write_queue (they have no chances to get new data).
2079*4882a593Smuzhiyun 	 *
2080*4882a593Smuzhiyun 	 * This is implemented in the callers, where they modify the 'nonagle'
2081*4882a593Smuzhiyun 	 * argument based upon the location of SKB in the send queue.
2082*4882a593Smuzhiyun 	 */
2083*4882a593Smuzhiyun 	if (nonagle & TCP_NAGLE_PUSH)
2084*4882a593Smuzhiyun 		return true;
2085*4882a593Smuzhiyun 
2086*4882a593Smuzhiyun 	/* Don't use the nagle rule for urgent data (or for the final FIN). */
2087*4882a593Smuzhiyun 	if (tcp_urg_mode(tp) || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
2088*4882a593Smuzhiyun 		return true;
2089*4882a593Smuzhiyun 
2090*4882a593Smuzhiyun 	if (!tcp_nagle_check(skb->len < cur_mss, tp, nonagle))
2091*4882a593Smuzhiyun 		return true;
2092*4882a593Smuzhiyun 
2093*4882a593Smuzhiyun 	return false;
2094*4882a593Smuzhiyun }
2095*4882a593Smuzhiyun 
2096*4882a593Smuzhiyun /* Does at least the first segment of SKB fit into the send window? */
tcp_snd_wnd_test(const struct tcp_sock * tp,const struct sk_buff * skb,unsigned int cur_mss)2097*4882a593Smuzhiyun static bool tcp_snd_wnd_test(const struct tcp_sock *tp,
2098*4882a593Smuzhiyun 			     const struct sk_buff *skb,
2099*4882a593Smuzhiyun 			     unsigned int cur_mss)
2100*4882a593Smuzhiyun {
2101*4882a593Smuzhiyun 	u32 end_seq = TCP_SKB_CB(skb)->end_seq;
2102*4882a593Smuzhiyun 
2103*4882a593Smuzhiyun 	if (skb->len > cur_mss)
2104*4882a593Smuzhiyun 		end_seq = TCP_SKB_CB(skb)->seq + cur_mss;
2105*4882a593Smuzhiyun 
2106*4882a593Smuzhiyun 	return !after(end_seq, tcp_wnd_end(tp));
2107*4882a593Smuzhiyun }
2108*4882a593Smuzhiyun 
2109*4882a593Smuzhiyun /* Trim TSO SKB to LEN bytes, put the remaining data into a new packet
2110*4882a593Smuzhiyun  * which is put after SKB on the list.  It is very much like
2111*4882a593Smuzhiyun  * tcp_fragment() except that it may make several kinds of assumptions
2112*4882a593Smuzhiyun  * in order to speed up the splitting operation.  In particular, we
2113*4882a593Smuzhiyun  * know that all the data is in scatter-gather pages, and that the
2114*4882a593Smuzhiyun  * packet has never been sent out before (and thus is not cloned).
2115*4882a593Smuzhiyun  */
tso_fragment(struct sock * sk,struct sk_buff * skb,unsigned int len,unsigned int mss_now,gfp_t gfp)2116*4882a593Smuzhiyun static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len,
2117*4882a593Smuzhiyun 			unsigned int mss_now, gfp_t gfp)
2118*4882a593Smuzhiyun {
2119*4882a593Smuzhiyun 	int nlen = skb->len - len;
2120*4882a593Smuzhiyun 	struct sk_buff *buff;
2121*4882a593Smuzhiyun 	u8 flags;
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun 	/* All of a TSO frame must be composed of paged data.  */
2124*4882a593Smuzhiyun 	if (skb->len != skb->data_len)
2125*4882a593Smuzhiyun 		return tcp_fragment(sk, TCP_FRAG_IN_WRITE_QUEUE,
2126*4882a593Smuzhiyun 				    skb, len, mss_now, gfp);
2127*4882a593Smuzhiyun 
2128*4882a593Smuzhiyun 	buff = sk_stream_alloc_skb(sk, 0, gfp, true);
2129*4882a593Smuzhiyun 	if (unlikely(!buff))
2130*4882a593Smuzhiyun 		return -ENOMEM;
2131*4882a593Smuzhiyun 	skb_copy_decrypted(buff, skb);
2132*4882a593Smuzhiyun 
2133*4882a593Smuzhiyun 	sk_wmem_queued_add(sk, buff->truesize);
2134*4882a593Smuzhiyun 	sk_mem_charge(sk, buff->truesize);
2135*4882a593Smuzhiyun 	buff->truesize += nlen;
2136*4882a593Smuzhiyun 	skb->truesize -= nlen;
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun 	/* Correct the sequence numbers. */
2139*4882a593Smuzhiyun 	TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len;
2140*4882a593Smuzhiyun 	TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq;
2141*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq;
2142*4882a593Smuzhiyun 
2143*4882a593Smuzhiyun 	/* PSH and FIN should only be set in the second packet. */
2144*4882a593Smuzhiyun 	flags = TCP_SKB_CB(skb)->tcp_flags;
2145*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH);
2146*4882a593Smuzhiyun 	TCP_SKB_CB(buff)->tcp_flags = flags;
2147*4882a593Smuzhiyun 
2148*4882a593Smuzhiyun 	/* This packet was never sent out yet, so no SACK bits. */
2149*4882a593Smuzhiyun 	TCP_SKB_CB(buff)->sacked = 0;
2150*4882a593Smuzhiyun 
2151*4882a593Smuzhiyun 	tcp_skb_fragment_eor(skb, buff);
2152*4882a593Smuzhiyun 
2153*4882a593Smuzhiyun 	buff->ip_summed = CHECKSUM_PARTIAL;
2154*4882a593Smuzhiyun 	skb_split(skb, buff, len);
2155*4882a593Smuzhiyun 	tcp_fragment_tstamp(skb, buff);
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun 	/* Fix up tso_factor for both original and new SKB.  */
2158*4882a593Smuzhiyun 	tcp_set_skb_tso_segs(skb, mss_now);
2159*4882a593Smuzhiyun 	tcp_set_skb_tso_segs(buff, mss_now);
2160*4882a593Smuzhiyun 
2161*4882a593Smuzhiyun 	/* Link BUFF into the send queue. */
2162*4882a593Smuzhiyun 	__skb_header_release(buff);
2163*4882a593Smuzhiyun 	tcp_insert_write_queue_after(skb, buff, sk, TCP_FRAG_IN_WRITE_QUEUE);
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun 	return 0;
2166*4882a593Smuzhiyun }
2167*4882a593Smuzhiyun 
2168*4882a593Smuzhiyun /* Try to defer sending, if possible, in order to minimize the amount
2169*4882a593Smuzhiyun  * of TSO splitting we do.  View it as a kind of TSO Nagle test.
2170*4882a593Smuzhiyun  *
2171*4882a593Smuzhiyun  * This algorithm is from John Heffner.
2172*4882a593Smuzhiyun  */
tcp_tso_should_defer(struct sock * sk,struct sk_buff * skb,bool * is_cwnd_limited,bool * is_rwnd_limited,u32 max_segs)2173*4882a593Smuzhiyun static bool tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb,
2174*4882a593Smuzhiyun 				 bool *is_cwnd_limited,
2175*4882a593Smuzhiyun 				 bool *is_rwnd_limited,
2176*4882a593Smuzhiyun 				 u32 max_segs)
2177*4882a593Smuzhiyun {
2178*4882a593Smuzhiyun 	const struct inet_connection_sock *icsk = inet_csk(sk);
2179*4882a593Smuzhiyun 	u32 send_win, cong_win, limit, in_flight;
2180*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
2181*4882a593Smuzhiyun 	struct sk_buff *head;
2182*4882a593Smuzhiyun 	int win_divisor;
2183*4882a593Smuzhiyun 	s64 delta;
2184*4882a593Smuzhiyun 
2185*4882a593Smuzhiyun 	if (icsk->icsk_ca_state >= TCP_CA_Recovery)
2186*4882a593Smuzhiyun 		goto send_now;
2187*4882a593Smuzhiyun 
2188*4882a593Smuzhiyun 	/* Avoid bursty behavior by allowing defer
2189*4882a593Smuzhiyun 	 * only if the last write was recent (1 ms).
2190*4882a593Smuzhiyun 	 * Note that tp->tcp_wstamp_ns can be in the future if we have
2191*4882a593Smuzhiyun 	 * packets waiting in a qdisc or device for EDT delivery.
2192*4882a593Smuzhiyun 	 */
2193*4882a593Smuzhiyun 	delta = tp->tcp_clock_cache - tp->tcp_wstamp_ns - NSEC_PER_MSEC;
2194*4882a593Smuzhiyun 	if (delta > 0)
2195*4882a593Smuzhiyun 		goto send_now;
2196*4882a593Smuzhiyun 
2197*4882a593Smuzhiyun 	in_flight = tcp_packets_in_flight(tp);
2198*4882a593Smuzhiyun 
2199*4882a593Smuzhiyun 	BUG_ON(tcp_skb_pcount(skb) <= 1);
2200*4882a593Smuzhiyun 	BUG_ON(tp->snd_cwnd <= in_flight);
2201*4882a593Smuzhiyun 
2202*4882a593Smuzhiyun 	send_win = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
2203*4882a593Smuzhiyun 
2204*4882a593Smuzhiyun 	/* From in_flight test above, we know that cwnd > in_flight.  */
2205*4882a593Smuzhiyun 	cong_win = (tp->snd_cwnd - in_flight) * tp->mss_cache;
2206*4882a593Smuzhiyun 
2207*4882a593Smuzhiyun 	limit = min(send_win, cong_win);
2208*4882a593Smuzhiyun 
2209*4882a593Smuzhiyun 	/* If a full-sized TSO skb can be sent, do it. */
2210*4882a593Smuzhiyun 	if (limit >= max_segs * tp->mss_cache)
2211*4882a593Smuzhiyun 		goto send_now;
2212*4882a593Smuzhiyun 
2213*4882a593Smuzhiyun 	/* Middle in queue won't get any more data, full sendable already? */
2214*4882a593Smuzhiyun 	if ((skb != tcp_write_queue_tail(sk)) && (limit >= skb->len))
2215*4882a593Smuzhiyun 		goto send_now;
2216*4882a593Smuzhiyun 
2217*4882a593Smuzhiyun 	win_divisor = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_tso_win_divisor);
2218*4882a593Smuzhiyun 	if (win_divisor) {
2219*4882a593Smuzhiyun 		u32 chunk = min(tp->snd_wnd, tp->snd_cwnd * tp->mss_cache);
2220*4882a593Smuzhiyun 
2221*4882a593Smuzhiyun 		/* If at least some fraction of a window is available,
2222*4882a593Smuzhiyun 		 * just use it.
2223*4882a593Smuzhiyun 		 */
2224*4882a593Smuzhiyun 		chunk /= win_divisor;
2225*4882a593Smuzhiyun 		if (limit >= chunk)
2226*4882a593Smuzhiyun 			goto send_now;
2227*4882a593Smuzhiyun 	} else {
2228*4882a593Smuzhiyun 		/* Different approach, try not to defer past a single
2229*4882a593Smuzhiyun 		 * ACK.  Receiver should ACK every other full sized
2230*4882a593Smuzhiyun 		 * frame, so if we have space for more than 3 frames
2231*4882a593Smuzhiyun 		 * then send now.
2232*4882a593Smuzhiyun 		 */
2233*4882a593Smuzhiyun 		if (limit > tcp_max_tso_deferred_mss(tp) * tp->mss_cache)
2234*4882a593Smuzhiyun 			goto send_now;
2235*4882a593Smuzhiyun 	}
2236*4882a593Smuzhiyun 
2237*4882a593Smuzhiyun 	/* TODO : use tsorted_sent_queue ? */
2238*4882a593Smuzhiyun 	head = tcp_rtx_queue_head(sk);
2239*4882a593Smuzhiyun 	if (!head)
2240*4882a593Smuzhiyun 		goto send_now;
2241*4882a593Smuzhiyun 	delta = tp->tcp_clock_cache - head->tstamp;
2242*4882a593Smuzhiyun 	/* If next ACK is likely to come too late (half srtt), do not defer */
2243*4882a593Smuzhiyun 	if ((s64)(delta - (u64)NSEC_PER_USEC * (tp->srtt_us >> 4)) < 0)
2244*4882a593Smuzhiyun 		goto send_now;
2245*4882a593Smuzhiyun 
2246*4882a593Smuzhiyun 	/* Ok, it looks like it is advisable to defer.
2247*4882a593Smuzhiyun 	 * Three cases are tracked :
2248*4882a593Smuzhiyun 	 * 1) We are cwnd-limited
2249*4882a593Smuzhiyun 	 * 2) We are rwnd-limited
2250*4882a593Smuzhiyun 	 * 3) We are application limited.
2251*4882a593Smuzhiyun 	 */
2252*4882a593Smuzhiyun 	if (cong_win < send_win) {
2253*4882a593Smuzhiyun 		if (cong_win <= skb->len) {
2254*4882a593Smuzhiyun 			*is_cwnd_limited = true;
2255*4882a593Smuzhiyun 			return true;
2256*4882a593Smuzhiyun 		}
2257*4882a593Smuzhiyun 	} else {
2258*4882a593Smuzhiyun 		if (send_win <= skb->len) {
2259*4882a593Smuzhiyun 			*is_rwnd_limited = true;
2260*4882a593Smuzhiyun 			return true;
2261*4882a593Smuzhiyun 		}
2262*4882a593Smuzhiyun 	}
2263*4882a593Smuzhiyun 
2264*4882a593Smuzhiyun 	/* If this packet won't get more data, do not wait. */
2265*4882a593Smuzhiyun 	if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) ||
2266*4882a593Smuzhiyun 	    TCP_SKB_CB(skb)->eor)
2267*4882a593Smuzhiyun 		goto send_now;
2268*4882a593Smuzhiyun 
2269*4882a593Smuzhiyun 	return true;
2270*4882a593Smuzhiyun 
2271*4882a593Smuzhiyun send_now:
2272*4882a593Smuzhiyun 	return false;
2273*4882a593Smuzhiyun }
2274*4882a593Smuzhiyun 
tcp_mtu_check_reprobe(struct sock * sk)2275*4882a593Smuzhiyun static inline void tcp_mtu_check_reprobe(struct sock *sk)
2276*4882a593Smuzhiyun {
2277*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
2278*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
2279*4882a593Smuzhiyun 	struct net *net = sock_net(sk);
2280*4882a593Smuzhiyun 	u32 interval;
2281*4882a593Smuzhiyun 	s32 delta;
2282*4882a593Smuzhiyun 
2283*4882a593Smuzhiyun 	interval = READ_ONCE(net->ipv4.sysctl_tcp_probe_interval);
2284*4882a593Smuzhiyun 	delta = tcp_jiffies32 - icsk->icsk_mtup.probe_timestamp;
2285*4882a593Smuzhiyun 	if (unlikely(delta >= interval * HZ)) {
2286*4882a593Smuzhiyun 		int mss = tcp_current_mss(sk);
2287*4882a593Smuzhiyun 
2288*4882a593Smuzhiyun 		/* Update current search range */
2289*4882a593Smuzhiyun 		icsk->icsk_mtup.probe_size = 0;
2290*4882a593Smuzhiyun 		icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp +
2291*4882a593Smuzhiyun 			sizeof(struct tcphdr) +
2292*4882a593Smuzhiyun 			icsk->icsk_af_ops->net_header_len;
2293*4882a593Smuzhiyun 		icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
2294*4882a593Smuzhiyun 
2295*4882a593Smuzhiyun 		/* Update probe time stamp */
2296*4882a593Smuzhiyun 		icsk->icsk_mtup.probe_timestamp = tcp_jiffies32;
2297*4882a593Smuzhiyun 	}
2298*4882a593Smuzhiyun }
2299*4882a593Smuzhiyun 
tcp_can_coalesce_send_queue_head(struct sock * sk,int len)2300*4882a593Smuzhiyun static bool tcp_can_coalesce_send_queue_head(struct sock *sk, int len)
2301*4882a593Smuzhiyun {
2302*4882a593Smuzhiyun 	struct sk_buff *skb, *next;
2303*4882a593Smuzhiyun 
2304*4882a593Smuzhiyun 	skb = tcp_send_head(sk);
2305*4882a593Smuzhiyun 	tcp_for_write_queue_from_safe(skb, next, sk) {
2306*4882a593Smuzhiyun 		if (len <= skb->len)
2307*4882a593Smuzhiyun 			break;
2308*4882a593Smuzhiyun 
2309*4882a593Smuzhiyun 		if (unlikely(TCP_SKB_CB(skb)->eor) || tcp_has_tx_tstamp(skb))
2310*4882a593Smuzhiyun 			return false;
2311*4882a593Smuzhiyun 
2312*4882a593Smuzhiyun 		len -= skb->len;
2313*4882a593Smuzhiyun 	}
2314*4882a593Smuzhiyun 
2315*4882a593Smuzhiyun 	return true;
2316*4882a593Smuzhiyun }
2317*4882a593Smuzhiyun 
2318*4882a593Smuzhiyun /* Create a new MTU probe if we are ready.
2319*4882a593Smuzhiyun  * MTU probe is regularly attempting to increase the path MTU by
2320*4882a593Smuzhiyun  * deliberately sending larger packets.  This discovers routing
2321*4882a593Smuzhiyun  * changes resulting in larger path MTUs.
2322*4882a593Smuzhiyun  *
2323*4882a593Smuzhiyun  * Returns 0 if we should wait to probe (no cwnd available),
2324*4882a593Smuzhiyun  *         1 if a probe was sent,
2325*4882a593Smuzhiyun  *         -1 otherwise
2326*4882a593Smuzhiyun  */
tcp_mtu_probe(struct sock * sk)2327*4882a593Smuzhiyun static int tcp_mtu_probe(struct sock *sk)
2328*4882a593Smuzhiyun {
2329*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
2330*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
2331*4882a593Smuzhiyun 	struct sk_buff *skb, *nskb, *next;
2332*4882a593Smuzhiyun 	struct net *net = sock_net(sk);
2333*4882a593Smuzhiyun 	int probe_size;
2334*4882a593Smuzhiyun 	int size_needed;
2335*4882a593Smuzhiyun 	int copy, len;
2336*4882a593Smuzhiyun 	int mss_now;
2337*4882a593Smuzhiyun 	int interval;
2338*4882a593Smuzhiyun 
2339*4882a593Smuzhiyun 	/* Not currently probing/verifying,
2340*4882a593Smuzhiyun 	 * not in recovery,
2341*4882a593Smuzhiyun 	 * have enough cwnd, and
2342*4882a593Smuzhiyun 	 * not SACKing (the variable headers throw things off)
2343*4882a593Smuzhiyun 	 */
2344*4882a593Smuzhiyun 	if (likely(!icsk->icsk_mtup.enabled ||
2345*4882a593Smuzhiyun 		   icsk->icsk_mtup.probe_size ||
2346*4882a593Smuzhiyun 		   inet_csk(sk)->icsk_ca_state != TCP_CA_Open ||
2347*4882a593Smuzhiyun 		   tp->snd_cwnd < 11 ||
2348*4882a593Smuzhiyun 		   tp->rx_opt.num_sacks || tp->rx_opt.dsack))
2349*4882a593Smuzhiyun 		return -1;
2350*4882a593Smuzhiyun 
2351*4882a593Smuzhiyun 	/* Use binary search for probe_size between tcp_mss_base,
2352*4882a593Smuzhiyun 	 * and current mss_clamp. if (search_high - search_low)
2353*4882a593Smuzhiyun 	 * smaller than a threshold, backoff from probing.
2354*4882a593Smuzhiyun 	 */
2355*4882a593Smuzhiyun 	mss_now = tcp_current_mss(sk);
2356*4882a593Smuzhiyun 	probe_size = tcp_mtu_to_mss(sk, (icsk->icsk_mtup.search_high +
2357*4882a593Smuzhiyun 				    icsk->icsk_mtup.search_low) >> 1);
2358*4882a593Smuzhiyun 	size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache;
2359*4882a593Smuzhiyun 	interval = icsk->icsk_mtup.search_high - icsk->icsk_mtup.search_low;
2360*4882a593Smuzhiyun 	/* When misfortune happens, we are reprobing actively,
2361*4882a593Smuzhiyun 	 * and then reprobe timer has expired. We stick with current
2362*4882a593Smuzhiyun 	 * probing process by not resetting search range to its orignal.
2363*4882a593Smuzhiyun 	 */
2364*4882a593Smuzhiyun 	if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high) ||
2365*4882a593Smuzhiyun 	    interval < READ_ONCE(net->ipv4.sysctl_tcp_probe_threshold)) {
2366*4882a593Smuzhiyun 		/* Check whether enough time has elaplased for
2367*4882a593Smuzhiyun 		 * another round of probing.
2368*4882a593Smuzhiyun 		 */
2369*4882a593Smuzhiyun 		tcp_mtu_check_reprobe(sk);
2370*4882a593Smuzhiyun 		return -1;
2371*4882a593Smuzhiyun 	}
2372*4882a593Smuzhiyun 
2373*4882a593Smuzhiyun 	/* Have enough data in the send queue to probe? */
2374*4882a593Smuzhiyun 	if (tp->write_seq - tp->snd_nxt < size_needed)
2375*4882a593Smuzhiyun 		return -1;
2376*4882a593Smuzhiyun 
2377*4882a593Smuzhiyun 	if (tp->snd_wnd < size_needed)
2378*4882a593Smuzhiyun 		return -1;
2379*4882a593Smuzhiyun 	if (after(tp->snd_nxt + size_needed, tcp_wnd_end(tp)))
2380*4882a593Smuzhiyun 		return 0;
2381*4882a593Smuzhiyun 
2382*4882a593Smuzhiyun 	/* Do we need to wait to drain cwnd? With none in flight, don't stall */
2383*4882a593Smuzhiyun 	if (tcp_packets_in_flight(tp) + 2 > tp->snd_cwnd) {
2384*4882a593Smuzhiyun 		if (!tcp_packets_in_flight(tp))
2385*4882a593Smuzhiyun 			return -1;
2386*4882a593Smuzhiyun 		else
2387*4882a593Smuzhiyun 			return 0;
2388*4882a593Smuzhiyun 	}
2389*4882a593Smuzhiyun 
2390*4882a593Smuzhiyun 	if (!tcp_can_coalesce_send_queue_head(sk, probe_size))
2391*4882a593Smuzhiyun 		return -1;
2392*4882a593Smuzhiyun 
2393*4882a593Smuzhiyun 	/* We're allowed to probe.  Build it now. */
2394*4882a593Smuzhiyun 	nskb = sk_stream_alloc_skb(sk, probe_size, GFP_ATOMIC, false);
2395*4882a593Smuzhiyun 	if (!nskb)
2396*4882a593Smuzhiyun 		return -1;
2397*4882a593Smuzhiyun 	sk_wmem_queued_add(sk, nskb->truesize);
2398*4882a593Smuzhiyun 	sk_mem_charge(sk, nskb->truesize);
2399*4882a593Smuzhiyun 
2400*4882a593Smuzhiyun 	skb = tcp_send_head(sk);
2401*4882a593Smuzhiyun 	skb_copy_decrypted(nskb, skb);
2402*4882a593Smuzhiyun 
2403*4882a593Smuzhiyun 	TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq;
2404*4882a593Smuzhiyun 	TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size;
2405*4882a593Smuzhiyun 	TCP_SKB_CB(nskb)->tcp_flags = TCPHDR_ACK;
2406*4882a593Smuzhiyun 	TCP_SKB_CB(nskb)->sacked = 0;
2407*4882a593Smuzhiyun 	nskb->csum = 0;
2408*4882a593Smuzhiyun 	nskb->ip_summed = CHECKSUM_PARTIAL;
2409*4882a593Smuzhiyun 
2410*4882a593Smuzhiyun 	tcp_insert_write_queue_before(nskb, skb, sk);
2411*4882a593Smuzhiyun 	tcp_highest_sack_replace(sk, skb, nskb);
2412*4882a593Smuzhiyun 
2413*4882a593Smuzhiyun 	len = 0;
2414*4882a593Smuzhiyun 	tcp_for_write_queue_from_safe(skb, next, sk) {
2415*4882a593Smuzhiyun 		copy = min_t(int, skb->len, probe_size - len);
2416*4882a593Smuzhiyun 		skb_copy_bits(skb, 0, skb_put(nskb, copy), copy);
2417*4882a593Smuzhiyun 
2418*4882a593Smuzhiyun 		if (skb->len <= copy) {
2419*4882a593Smuzhiyun 			/* We've eaten all the data from this skb.
2420*4882a593Smuzhiyun 			 * Throw it away. */
2421*4882a593Smuzhiyun 			TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
2422*4882a593Smuzhiyun 			/* If this is the last SKB we copy and eor is set
2423*4882a593Smuzhiyun 			 * we need to propagate it to the new skb.
2424*4882a593Smuzhiyun 			 */
2425*4882a593Smuzhiyun 			TCP_SKB_CB(nskb)->eor = TCP_SKB_CB(skb)->eor;
2426*4882a593Smuzhiyun 			tcp_skb_collapse_tstamp(nskb, skb);
2427*4882a593Smuzhiyun 			tcp_unlink_write_queue(skb, sk);
2428*4882a593Smuzhiyun 			sk_wmem_free_skb(sk, skb);
2429*4882a593Smuzhiyun 		} else {
2430*4882a593Smuzhiyun 			TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags &
2431*4882a593Smuzhiyun 						   ~(TCPHDR_FIN|TCPHDR_PSH);
2432*4882a593Smuzhiyun 			if (!skb_shinfo(skb)->nr_frags) {
2433*4882a593Smuzhiyun 				skb_pull(skb, copy);
2434*4882a593Smuzhiyun 			} else {
2435*4882a593Smuzhiyun 				__pskb_trim_head(skb, copy);
2436*4882a593Smuzhiyun 				tcp_set_skb_tso_segs(skb, mss_now);
2437*4882a593Smuzhiyun 			}
2438*4882a593Smuzhiyun 			TCP_SKB_CB(skb)->seq += copy;
2439*4882a593Smuzhiyun 		}
2440*4882a593Smuzhiyun 
2441*4882a593Smuzhiyun 		len += copy;
2442*4882a593Smuzhiyun 
2443*4882a593Smuzhiyun 		if (len >= probe_size)
2444*4882a593Smuzhiyun 			break;
2445*4882a593Smuzhiyun 	}
2446*4882a593Smuzhiyun 	tcp_init_tso_segs(nskb, nskb->len);
2447*4882a593Smuzhiyun 
2448*4882a593Smuzhiyun 	/* We're ready to send.  If this fails, the probe will
2449*4882a593Smuzhiyun 	 * be resegmented into mss-sized pieces by tcp_write_xmit().
2450*4882a593Smuzhiyun 	 */
2451*4882a593Smuzhiyun 	if (!tcp_transmit_skb(sk, nskb, 1, GFP_ATOMIC)) {
2452*4882a593Smuzhiyun 		/* Decrement cwnd here because we are sending
2453*4882a593Smuzhiyun 		 * effectively two packets. */
2454*4882a593Smuzhiyun 		tp->snd_cwnd--;
2455*4882a593Smuzhiyun 		tcp_event_new_data_sent(sk, nskb);
2456*4882a593Smuzhiyun 
2457*4882a593Smuzhiyun 		icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len);
2458*4882a593Smuzhiyun 		tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq;
2459*4882a593Smuzhiyun 		tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq;
2460*4882a593Smuzhiyun 
2461*4882a593Smuzhiyun 		return 1;
2462*4882a593Smuzhiyun 	}
2463*4882a593Smuzhiyun 
2464*4882a593Smuzhiyun 	return -1;
2465*4882a593Smuzhiyun }
2466*4882a593Smuzhiyun 
tcp_pacing_check(struct sock * sk)2467*4882a593Smuzhiyun static bool tcp_pacing_check(struct sock *sk)
2468*4882a593Smuzhiyun {
2469*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
2470*4882a593Smuzhiyun 
2471*4882a593Smuzhiyun 	if (!tcp_needs_internal_pacing(sk))
2472*4882a593Smuzhiyun 		return false;
2473*4882a593Smuzhiyun 
2474*4882a593Smuzhiyun 	if (tp->tcp_wstamp_ns <= tp->tcp_clock_cache)
2475*4882a593Smuzhiyun 		return false;
2476*4882a593Smuzhiyun 
2477*4882a593Smuzhiyun 	if (!hrtimer_is_queued(&tp->pacing_timer)) {
2478*4882a593Smuzhiyun 		hrtimer_start(&tp->pacing_timer,
2479*4882a593Smuzhiyun 			      ns_to_ktime(tp->tcp_wstamp_ns),
2480*4882a593Smuzhiyun 			      HRTIMER_MODE_ABS_PINNED_SOFT);
2481*4882a593Smuzhiyun 		sock_hold(sk);
2482*4882a593Smuzhiyun 	}
2483*4882a593Smuzhiyun 	return true;
2484*4882a593Smuzhiyun }
2485*4882a593Smuzhiyun 
2486*4882a593Smuzhiyun /* TCP Small Queues :
2487*4882a593Smuzhiyun  * Control number of packets in qdisc/devices to two packets / or ~1 ms.
2488*4882a593Smuzhiyun  * (These limits are doubled for retransmits)
2489*4882a593Smuzhiyun  * This allows for :
2490*4882a593Smuzhiyun  *  - better RTT estimation and ACK scheduling
2491*4882a593Smuzhiyun  *  - faster recovery
2492*4882a593Smuzhiyun  *  - high rates
2493*4882a593Smuzhiyun  * Alas, some drivers / subsystems require a fair amount
2494*4882a593Smuzhiyun  * of queued bytes to ensure line rate.
2495*4882a593Smuzhiyun  * One example is wifi aggregation (802.11 AMPDU)
2496*4882a593Smuzhiyun  */
tcp_small_queue_check(struct sock * sk,const struct sk_buff * skb,unsigned int factor)2497*4882a593Smuzhiyun static bool tcp_small_queue_check(struct sock *sk, const struct sk_buff *skb,
2498*4882a593Smuzhiyun 				  unsigned int factor)
2499*4882a593Smuzhiyun {
2500*4882a593Smuzhiyun 	unsigned long limit;
2501*4882a593Smuzhiyun 
2502*4882a593Smuzhiyun 	limit = max_t(unsigned long,
2503*4882a593Smuzhiyun 		      2 * skb->truesize,
2504*4882a593Smuzhiyun 		      sk->sk_pacing_rate >> READ_ONCE(sk->sk_pacing_shift));
2505*4882a593Smuzhiyun 	if (sk->sk_pacing_status == SK_PACING_NONE)
2506*4882a593Smuzhiyun 		limit = min_t(unsigned long, limit,
2507*4882a593Smuzhiyun 			      READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_limit_output_bytes));
2508*4882a593Smuzhiyun 	limit <<= factor;
2509*4882a593Smuzhiyun 
2510*4882a593Smuzhiyun 	if (static_branch_unlikely(&tcp_tx_delay_enabled) &&
2511*4882a593Smuzhiyun 	    tcp_sk(sk)->tcp_tx_delay) {
2512*4882a593Smuzhiyun 		u64 extra_bytes = (u64)sk->sk_pacing_rate * tcp_sk(sk)->tcp_tx_delay;
2513*4882a593Smuzhiyun 
2514*4882a593Smuzhiyun 		/* TSQ is based on skb truesize sum (sk_wmem_alloc), so we
2515*4882a593Smuzhiyun 		 * approximate our needs assuming an ~100% skb->truesize overhead.
2516*4882a593Smuzhiyun 		 * USEC_PER_SEC is approximated by 2^20.
2517*4882a593Smuzhiyun 		 * do_div(extra_bytes, USEC_PER_SEC/2) is replaced by a right shift.
2518*4882a593Smuzhiyun 		 */
2519*4882a593Smuzhiyun 		extra_bytes >>= (20 - 1);
2520*4882a593Smuzhiyun 		limit += extra_bytes;
2521*4882a593Smuzhiyun 	}
2522*4882a593Smuzhiyun 	if (refcount_read(&sk->sk_wmem_alloc) > limit) {
2523*4882a593Smuzhiyun 		/* Always send skb if rtx queue is empty.
2524*4882a593Smuzhiyun 		 * No need to wait for TX completion to call us back,
2525*4882a593Smuzhiyun 		 * after softirq/tasklet schedule.
2526*4882a593Smuzhiyun 		 * This helps when TX completions are delayed too much.
2527*4882a593Smuzhiyun 		 */
2528*4882a593Smuzhiyun 		if (tcp_rtx_queue_empty(sk))
2529*4882a593Smuzhiyun 			return false;
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun 		set_bit(TSQ_THROTTLED, &sk->sk_tsq_flags);
2532*4882a593Smuzhiyun 		/* It is possible TX completion already happened
2533*4882a593Smuzhiyun 		 * before we set TSQ_THROTTLED, so we must
2534*4882a593Smuzhiyun 		 * test again the condition.
2535*4882a593Smuzhiyun 		 */
2536*4882a593Smuzhiyun 		smp_mb__after_atomic();
2537*4882a593Smuzhiyun 		if (refcount_read(&sk->sk_wmem_alloc) > limit)
2538*4882a593Smuzhiyun 			return true;
2539*4882a593Smuzhiyun 	}
2540*4882a593Smuzhiyun 	return false;
2541*4882a593Smuzhiyun }
2542*4882a593Smuzhiyun 
tcp_chrono_set(struct tcp_sock * tp,const enum tcp_chrono new)2543*4882a593Smuzhiyun static void tcp_chrono_set(struct tcp_sock *tp, const enum tcp_chrono new)
2544*4882a593Smuzhiyun {
2545*4882a593Smuzhiyun 	const u32 now = tcp_jiffies32;
2546*4882a593Smuzhiyun 	enum tcp_chrono old = tp->chrono_type;
2547*4882a593Smuzhiyun 
2548*4882a593Smuzhiyun 	if (old > TCP_CHRONO_UNSPEC)
2549*4882a593Smuzhiyun 		tp->chrono_stat[old - 1] += now - tp->chrono_start;
2550*4882a593Smuzhiyun 	tp->chrono_start = now;
2551*4882a593Smuzhiyun 	tp->chrono_type = new;
2552*4882a593Smuzhiyun }
2553*4882a593Smuzhiyun 
tcp_chrono_start(struct sock * sk,const enum tcp_chrono type)2554*4882a593Smuzhiyun void tcp_chrono_start(struct sock *sk, const enum tcp_chrono type)
2555*4882a593Smuzhiyun {
2556*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
2557*4882a593Smuzhiyun 
2558*4882a593Smuzhiyun 	/* If there are multiple conditions worthy of tracking in a
2559*4882a593Smuzhiyun 	 * chronograph then the highest priority enum takes precedence
2560*4882a593Smuzhiyun 	 * over the other conditions. So that if something "more interesting"
2561*4882a593Smuzhiyun 	 * starts happening, stop the previous chrono and start a new one.
2562*4882a593Smuzhiyun 	 */
2563*4882a593Smuzhiyun 	if (type > tp->chrono_type)
2564*4882a593Smuzhiyun 		tcp_chrono_set(tp, type);
2565*4882a593Smuzhiyun }
2566*4882a593Smuzhiyun 
tcp_chrono_stop(struct sock * sk,const enum tcp_chrono type)2567*4882a593Smuzhiyun void tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type)
2568*4882a593Smuzhiyun {
2569*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
2570*4882a593Smuzhiyun 
2571*4882a593Smuzhiyun 
2572*4882a593Smuzhiyun 	/* There are multiple conditions worthy of tracking in a
2573*4882a593Smuzhiyun 	 * chronograph, so that the highest priority enum takes
2574*4882a593Smuzhiyun 	 * precedence over the other conditions (see tcp_chrono_start).
2575*4882a593Smuzhiyun 	 * If a condition stops, we only stop chrono tracking if
2576*4882a593Smuzhiyun 	 * it's the "most interesting" or current chrono we are
2577*4882a593Smuzhiyun 	 * tracking and starts busy chrono if we have pending data.
2578*4882a593Smuzhiyun 	 */
2579*4882a593Smuzhiyun 	if (tcp_rtx_and_write_queues_empty(sk))
2580*4882a593Smuzhiyun 		tcp_chrono_set(tp, TCP_CHRONO_UNSPEC);
2581*4882a593Smuzhiyun 	else if (type == tp->chrono_type)
2582*4882a593Smuzhiyun 		tcp_chrono_set(tp, TCP_CHRONO_BUSY);
2583*4882a593Smuzhiyun }
2584*4882a593Smuzhiyun 
2585*4882a593Smuzhiyun /* This routine writes packets to the network.  It advances the
2586*4882a593Smuzhiyun  * send_head.  This happens as incoming acks open up the remote
2587*4882a593Smuzhiyun  * window for us.
2588*4882a593Smuzhiyun  *
2589*4882a593Smuzhiyun  * LARGESEND note: !tcp_urg_mode is overkill, only frames between
2590*4882a593Smuzhiyun  * snd_up-64k-mss .. snd_up cannot be large. However, taking into
2591*4882a593Smuzhiyun  * account rare use of URG, this is not a big flaw.
2592*4882a593Smuzhiyun  *
2593*4882a593Smuzhiyun  * Send at most one packet when push_one > 0. Temporarily ignore
2594*4882a593Smuzhiyun  * cwnd limit to force at most one packet out when push_one == 2.
2595*4882a593Smuzhiyun 
2596*4882a593Smuzhiyun  * Returns true, if no segments are in flight and we have queued segments,
2597*4882a593Smuzhiyun  * but cannot send anything now because of SWS or another problem.
2598*4882a593Smuzhiyun  */
tcp_write_xmit(struct sock * sk,unsigned int mss_now,int nonagle,int push_one,gfp_t gfp)2599*4882a593Smuzhiyun static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
2600*4882a593Smuzhiyun 			   int push_one, gfp_t gfp)
2601*4882a593Smuzhiyun {
2602*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
2603*4882a593Smuzhiyun 	struct sk_buff *skb;
2604*4882a593Smuzhiyun 	unsigned int tso_segs, sent_pkts;
2605*4882a593Smuzhiyun 	int cwnd_quota;
2606*4882a593Smuzhiyun 	int result;
2607*4882a593Smuzhiyun 	bool is_cwnd_limited = false, is_rwnd_limited = false;
2608*4882a593Smuzhiyun 	u32 max_segs;
2609*4882a593Smuzhiyun 
2610*4882a593Smuzhiyun 	sent_pkts = 0;
2611*4882a593Smuzhiyun 
2612*4882a593Smuzhiyun 	tcp_mstamp_refresh(tp);
2613*4882a593Smuzhiyun 	if (!push_one) {
2614*4882a593Smuzhiyun 		/* Do MTU probing. */
2615*4882a593Smuzhiyun 		result = tcp_mtu_probe(sk);
2616*4882a593Smuzhiyun 		if (!result) {
2617*4882a593Smuzhiyun 			return false;
2618*4882a593Smuzhiyun 		} else if (result > 0) {
2619*4882a593Smuzhiyun 			sent_pkts = 1;
2620*4882a593Smuzhiyun 		}
2621*4882a593Smuzhiyun 	}
2622*4882a593Smuzhiyun 
2623*4882a593Smuzhiyun 	max_segs = tcp_tso_segs(sk, mss_now);
2624*4882a593Smuzhiyun 	while ((skb = tcp_send_head(sk))) {
2625*4882a593Smuzhiyun 		unsigned int limit;
2626*4882a593Smuzhiyun 
2627*4882a593Smuzhiyun 		if (unlikely(tp->repair) && tp->repair_queue == TCP_SEND_QUEUE) {
2628*4882a593Smuzhiyun 			/* "skb_mstamp_ns" is used as a start point for the retransmit timer */
2629*4882a593Smuzhiyun 			skb->skb_mstamp_ns = tp->tcp_wstamp_ns = tp->tcp_clock_cache;
2630*4882a593Smuzhiyun 			list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue);
2631*4882a593Smuzhiyun 			tcp_init_tso_segs(skb, mss_now);
2632*4882a593Smuzhiyun 			goto repair; /* Skip network transmission */
2633*4882a593Smuzhiyun 		}
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun 		if (tcp_pacing_check(sk))
2636*4882a593Smuzhiyun 			break;
2637*4882a593Smuzhiyun 
2638*4882a593Smuzhiyun 		tso_segs = tcp_init_tso_segs(skb, mss_now);
2639*4882a593Smuzhiyun 		BUG_ON(!tso_segs);
2640*4882a593Smuzhiyun 
2641*4882a593Smuzhiyun 		cwnd_quota = tcp_cwnd_test(tp, skb);
2642*4882a593Smuzhiyun 		if (!cwnd_quota) {
2643*4882a593Smuzhiyun 			if (push_one == 2)
2644*4882a593Smuzhiyun 				/* Force out a loss probe pkt. */
2645*4882a593Smuzhiyun 				cwnd_quota = 1;
2646*4882a593Smuzhiyun 			else
2647*4882a593Smuzhiyun 				break;
2648*4882a593Smuzhiyun 		}
2649*4882a593Smuzhiyun 
2650*4882a593Smuzhiyun 		if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now))) {
2651*4882a593Smuzhiyun 			is_rwnd_limited = true;
2652*4882a593Smuzhiyun 			break;
2653*4882a593Smuzhiyun 		}
2654*4882a593Smuzhiyun 
2655*4882a593Smuzhiyun 		if (tso_segs == 1) {
2656*4882a593Smuzhiyun 			if (unlikely(!tcp_nagle_test(tp, skb, mss_now,
2657*4882a593Smuzhiyun 						     (tcp_skb_is_last(sk, skb) ?
2658*4882a593Smuzhiyun 						      nonagle : TCP_NAGLE_PUSH))))
2659*4882a593Smuzhiyun 				break;
2660*4882a593Smuzhiyun 		} else {
2661*4882a593Smuzhiyun 			if (!push_one &&
2662*4882a593Smuzhiyun 			    tcp_tso_should_defer(sk, skb, &is_cwnd_limited,
2663*4882a593Smuzhiyun 						 &is_rwnd_limited, max_segs))
2664*4882a593Smuzhiyun 				break;
2665*4882a593Smuzhiyun 		}
2666*4882a593Smuzhiyun 
2667*4882a593Smuzhiyun 		limit = mss_now;
2668*4882a593Smuzhiyun 		if (tso_segs > 1 && !tcp_urg_mode(tp))
2669*4882a593Smuzhiyun 			limit = tcp_mss_split_point(sk, skb, mss_now,
2670*4882a593Smuzhiyun 						    min_t(unsigned int,
2671*4882a593Smuzhiyun 							  cwnd_quota,
2672*4882a593Smuzhiyun 							  max_segs),
2673*4882a593Smuzhiyun 						    nonagle);
2674*4882a593Smuzhiyun 
2675*4882a593Smuzhiyun 		if (skb->len > limit &&
2676*4882a593Smuzhiyun 		    unlikely(tso_fragment(sk, skb, limit, mss_now, gfp)))
2677*4882a593Smuzhiyun 			break;
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun 		if (tcp_small_queue_check(sk, skb, 0))
2680*4882a593Smuzhiyun 			break;
2681*4882a593Smuzhiyun 
2682*4882a593Smuzhiyun 		/* Argh, we hit an empty skb(), presumably a thread
2683*4882a593Smuzhiyun 		 * is sleeping in sendmsg()/sk_stream_wait_memory().
2684*4882a593Smuzhiyun 		 * We do not want to send a pure-ack packet and have
2685*4882a593Smuzhiyun 		 * a strange looking rtx queue with empty packet(s).
2686*4882a593Smuzhiyun 		 */
2687*4882a593Smuzhiyun 		if (TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq)
2688*4882a593Smuzhiyun 			break;
2689*4882a593Smuzhiyun 
2690*4882a593Smuzhiyun 		if (unlikely(tcp_transmit_skb(sk, skb, 1, gfp)))
2691*4882a593Smuzhiyun 			break;
2692*4882a593Smuzhiyun 
2693*4882a593Smuzhiyun repair:
2694*4882a593Smuzhiyun 		/* Advance the send_head.  This one is sent out.
2695*4882a593Smuzhiyun 		 * This call will increment packets_out.
2696*4882a593Smuzhiyun 		 */
2697*4882a593Smuzhiyun 		tcp_event_new_data_sent(sk, skb);
2698*4882a593Smuzhiyun 
2699*4882a593Smuzhiyun 		tcp_minshall_update(tp, mss_now, skb);
2700*4882a593Smuzhiyun 		sent_pkts += tcp_skb_pcount(skb);
2701*4882a593Smuzhiyun 
2702*4882a593Smuzhiyun 		if (push_one)
2703*4882a593Smuzhiyun 			break;
2704*4882a593Smuzhiyun 	}
2705*4882a593Smuzhiyun 
2706*4882a593Smuzhiyun 	if (is_rwnd_limited)
2707*4882a593Smuzhiyun 		tcp_chrono_start(sk, TCP_CHRONO_RWND_LIMITED);
2708*4882a593Smuzhiyun 	else
2709*4882a593Smuzhiyun 		tcp_chrono_stop(sk, TCP_CHRONO_RWND_LIMITED);
2710*4882a593Smuzhiyun 
2711*4882a593Smuzhiyun 	is_cwnd_limited |= (tcp_packets_in_flight(tp) >= tp->snd_cwnd);
2712*4882a593Smuzhiyun 	if (likely(sent_pkts || is_cwnd_limited))
2713*4882a593Smuzhiyun 		tcp_cwnd_validate(sk, is_cwnd_limited);
2714*4882a593Smuzhiyun 
2715*4882a593Smuzhiyun 	if (likely(sent_pkts)) {
2716*4882a593Smuzhiyun 		if (tcp_in_cwnd_reduction(sk))
2717*4882a593Smuzhiyun 			tp->prr_out += sent_pkts;
2718*4882a593Smuzhiyun 
2719*4882a593Smuzhiyun 		/* Send one loss probe per tail loss episode. */
2720*4882a593Smuzhiyun 		if (push_one != 2)
2721*4882a593Smuzhiyun 			tcp_schedule_loss_probe(sk, false);
2722*4882a593Smuzhiyun 		return false;
2723*4882a593Smuzhiyun 	}
2724*4882a593Smuzhiyun 	return !tp->packets_out && !tcp_write_queue_empty(sk);
2725*4882a593Smuzhiyun }
2726*4882a593Smuzhiyun 
tcp_schedule_loss_probe(struct sock * sk,bool advancing_rto)2727*4882a593Smuzhiyun bool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto)
2728*4882a593Smuzhiyun {
2729*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
2730*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
2731*4882a593Smuzhiyun 	u32 timeout, rto_delta_us;
2732*4882a593Smuzhiyun 	int early_retrans;
2733*4882a593Smuzhiyun 
2734*4882a593Smuzhiyun 	/* Don't do any loss probe on a Fast Open connection before 3WHS
2735*4882a593Smuzhiyun 	 * finishes.
2736*4882a593Smuzhiyun 	 */
2737*4882a593Smuzhiyun 	if (rcu_access_pointer(tp->fastopen_rsk))
2738*4882a593Smuzhiyun 		return false;
2739*4882a593Smuzhiyun 
2740*4882a593Smuzhiyun 	early_retrans = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_early_retrans);
2741*4882a593Smuzhiyun 	/* Schedule a loss probe in 2*RTT for SACK capable connections
2742*4882a593Smuzhiyun 	 * not in loss recovery, that are either limited by cwnd or application.
2743*4882a593Smuzhiyun 	 */
2744*4882a593Smuzhiyun 	if ((early_retrans != 3 && early_retrans != 4) ||
2745*4882a593Smuzhiyun 	    !tp->packets_out || !tcp_is_sack(tp) ||
2746*4882a593Smuzhiyun 	    (icsk->icsk_ca_state != TCP_CA_Open &&
2747*4882a593Smuzhiyun 	     icsk->icsk_ca_state != TCP_CA_CWR))
2748*4882a593Smuzhiyun 		return false;
2749*4882a593Smuzhiyun 
2750*4882a593Smuzhiyun 	/* Probe timeout is 2*rtt. Add minimum RTO to account
2751*4882a593Smuzhiyun 	 * for delayed ack when there's one outstanding packet. If no RTT
2752*4882a593Smuzhiyun 	 * sample is available then probe after TCP_TIMEOUT_INIT.
2753*4882a593Smuzhiyun 	 */
2754*4882a593Smuzhiyun 	if (tp->srtt_us) {
2755*4882a593Smuzhiyun 		timeout = usecs_to_jiffies(tp->srtt_us >> 2);
2756*4882a593Smuzhiyun 		if (tp->packets_out == 1)
2757*4882a593Smuzhiyun 			timeout += TCP_RTO_MIN;
2758*4882a593Smuzhiyun 		else
2759*4882a593Smuzhiyun 			timeout += TCP_TIMEOUT_MIN;
2760*4882a593Smuzhiyun 	} else {
2761*4882a593Smuzhiyun 		timeout = TCP_TIMEOUT_INIT;
2762*4882a593Smuzhiyun 	}
2763*4882a593Smuzhiyun 
2764*4882a593Smuzhiyun 	/* If the RTO formula yields an earlier time, then use that time. */
2765*4882a593Smuzhiyun 	rto_delta_us = advancing_rto ?
2766*4882a593Smuzhiyun 			jiffies_to_usecs(inet_csk(sk)->icsk_rto) :
2767*4882a593Smuzhiyun 			tcp_rto_delta_us(sk);  /* How far in future is RTO? */
2768*4882a593Smuzhiyun 	if (rto_delta_us > 0)
2769*4882a593Smuzhiyun 		timeout = min_t(u32, timeout, usecs_to_jiffies(rto_delta_us));
2770*4882a593Smuzhiyun 
2771*4882a593Smuzhiyun 	tcp_reset_xmit_timer(sk, ICSK_TIME_LOSS_PROBE, timeout, TCP_RTO_MAX);
2772*4882a593Smuzhiyun 	return true;
2773*4882a593Smuzhiyun }
2774*4882a593Smuzhiyun 
2775*4882a593Smuzhiyun /* Thanks to skb fast clones, we can detect if a prior transmit of
2776*4882a593Smuzhiyun  * a packet is still in a qdisc or driver queue.
2777*4882a593Smuzhiyun  * In this case, there is very little point doing a retransmit !
2778*4882a593Smuzhiyun  */
skb_still_in_host_queue(const struct sock * sk,const struct sk_buff * skb)2779*4882a593Smuzhiyun static bool skb_still_in_host_queue(const struct sock *sk,
2780*4882a593Smuzhiyun 				    const struct sk_buff *skb)
2781*4882a593Smuzhiyun {
2782*4882a593Smuzhiyun 	if (unlikely(skb_fclone_busy(sk, skb))) {
2783*4882a593Smuzhiyun 		NET_INC_STATS(sock_net(sk),
2784*4882a593Smuzhiyun 			      LINUX_MIB_TCPSPURIOUS_RTX_HOSTQUEUES);
2785*4882a593Smuzhiyun 		return true;
2786*4882a593Smuzhiyun 	}
2787*4882a593Smuzhiyun 	return false;
2788*4882a593Smuzhiyun }
2789*4882a593Smuzhiyun 
2790*4882a593Smuzhiyun /* When probe timeout (PTO) fires, try send a new segment if possible, else
2791*4882a593Smuzhiyun  * retransmit the last segment.
2792*4882a593Smuzhiyun  */
tcp_send_loss_probe(struct sock * sk)2793*4882a593Smuzhiyun void tcp_send_loss_probe(struct sock *sk)
2794*4882a593Smuzhiyun {
2795*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
2796*4882a593Smuzhiyun 	struct sk_buff *skb;
2797*4882a593Smuzhiyun 	int pcount;
2798*4882a593Smuzhiyun 	int mss = tcp_current_mss(sk);
2799*4882a593Smuzhiyun 
2800*4882a593Smuzhiyun 	/* At most one outstanding TLP */
2801*4882a593Smuzhiyun 	if (tp->tlp_high_seq)
2802*4882a593Smuzhiyun 		goto rearm_timer;
2803*4882a593Smuzhiyun 
2804*4882a593Smuzhiyun 	tp->tlp_retrans = 0;
2805*4882a593Smuzhiyun 	skb = tcp_send_head(sk);
2806*4882a593Smuzhiyun 	if (skb && tcp_snd_wnd_test(tp, skb, mss)) {
2807*4882a593Smuzhiyun 		pcount = tp->packets_out;
2808*4882a593Smuzhiyun 		tcp_write_xmit(sk, mss, TCP_NAGLE_OFF, 2, GFP_ATOMIC);
2809*4882a593Smuzhiyun 		if (tp->packets_out > pcount)
2810*4882a593Smuzhiyun 			goto probe_sent;
2811*4882a593Smuzhiyun 		goto rearm_timer;
2812*4882a593Smuzhiyun 	}
2813*4882a593Smuzhiyun 	skb = skb_rb_last(&sk->tcp_rtx_queue);
2814*4882a593Smuzhiyun 	if (unlikely(!skb)) {
2815*4882a593Smuzhiyun 		WARN_ONCE(tp->packets_out,
2816*4882a593Smuzhiyun 			  "invalid inflight: %u state %u cwnd %u mss %d\n",
2817*4882a593Smuzhiyun 			  tp->packets_out, sk->sk_state, tp->snd_cwnd, mss);
2818*4882a593Smuzhiyun 		inet_csk(sk)->icsk_pending = 0;
2819*4882a593Smuzhiyun 		return;
2820*4882a593Smuzhiyun 	}
2821*4882a593Smuzhiyun 
2822*4882a593Smuzhiyun 	if (skb_still_in_host_queue(sk, skb))
2823*4882a593Smuzhiyun 		goto rearm_timer;
2824*4882a593Smuzhiyun 
2825*4882a593Smuzhiyun 	pcount = tcp_skb_pcount(skb);
2826*4882a593Smuzhiyun 	if (WARN_ON(!pcount))
2827*4882a593Smuzhiyun 		goto rearm_timer;
2828*4882a593Smuzhiyun 
2829*4882a593Smuzhiyun 	if ((pcount > 1) && (skb->len > (pcount - 1) * mss)) {
2830*4882a593Smuzhiyun 		if (unlikely(tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb,
2831*4882a593Smuzhiyun 					  (pcount - 1) * mss, mss,
2832*4882a593Smuzhiyun 					  GFP_ATOMIC)))
2833*4882a593Smuzhiyun 			goto rearm_timer;
2834*4882a593Smuzhiyun 		skb = skb_rb_next(skb);
2835*4882a593Smuzhiyun 	}
2836*4882a593Smuzhiyun 
2837*4882a593Smuzhiyun 	if (WARN_ON(!skb || !tcp_skb_pcount(skb)))
2838*4882a593Smuzhiyun 		goto rearm_timer;
2839*4882a593Smuzhiyun 
2840*4882a593Smuzhiyun 	if (__tcp_retransmit_skb(sk, skb, 1))
2841*4882a593Smuzhiyun 		goto rearm_timer;
2842*4882a593Smuzhiyun 
2843*4882a593Smuzhiyun 	tp->tlp_retrans = 1;
2844*4882a593Smuzhiyun 
2845*4882a593Smuzhiyun probe_sent:
2846*4882a593Smuzhiyun 	/* Record snd_nxt for loss detection. */
2847*4882a593Smuzhiyun 	tp->tlp_high_seq = tp->snd_nxt;
2848*4882a593Smuzhiyun 
2849*4882a593Smuzhiyun 	NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSPROBES);
2850*4882a593Smuzhiyun 	/* Reset s.t. tcp_rearm_rto will restart timer from now */
2851*4882a593Smuzhiyun 	inet_csk(sk)->icsk_pending = 0;
2852*4882a593Smuzhiyun rearm_timer:
2853*4882a593Smuzhiyun 	tcp_rearm_rto(sk);
2854*4882a593Smuzhiyun }
2855*4882a593Smuzhiyun 
2856*4882a593Smuzhiyun /* Push out any pending frames which were held back due to
2857*4882a593Smuzhiyun  * TCP_CORK or attempt at coalescing tiny packets.
2858*4882a593Smuzhiyun  * The socket must be locked by the caller.
2859*4882a593Smuzhiyun  */
__tcp_push_pending_frames(struct sock * sk,unsigned int cur_mss,int nonagle)2860*4882a593Smuzhiyun void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
2861*4882a593Smuzhiyun 			       int nonagle)
2862*4882a593Smuzhiyun {
2863*4882a593Smuzhiyun 	/* If we are closed, the bytes will have to remain here.
2864*4882a593Smuzhiyun 	 * In time closedown will finish, we empty the write queue and
2865*4882a593Smuzhiyun 	 * all will be happy.
2866*4882a593Smuzhiyun 	 */
2867*4882a593Smuzhiyun 	if (unlikely(sk->sk_state == TCP_CLOSE))
2868*4882a593Smuzhiyun 		return;
2869*4882a593Smuzhiyun 
2870*4882a593Smuzhiyun 	if (tcp_write_xmit(sk, cur_mss, nonagle, 0,
2871*4882a593Smuzhiyun 			   sk_gfp_mask(sk, GFP_ATOMIC)))
2872*4882a593Smuzhiyun 		tcp_check_probe_timer(sk);
2873*4882a593Smuzhiyun }
2874*4882a593Smuzhiyun 
2875*4882a593Smuzhiyun /* Send _single_ skb sitting at the send head. This function requires
2876*4882a593Smuzhiyun  * true push pending frames to setup probe timer etc.
2877*4882a593Smuzhiyun  */
tcp_push_one(struct sock * sk,unsigned int mss_now)2878*4882a593Smuzhiyun void tcp_push_one(struct sock *sk, unsigned int mss_now)
2879*4882a593Smuzhiyun {
2880*4882a593Smuzhiyun 	struct sk_buff *skb = tcp_send_head(sk);
2881*4882a593Smuzhiyun 
2882*4882a593Smuzhiyun 	BUG_ON(!skb || skb->len < mss_now);
2883*4882a593Smuzhiyun 
2884*4882a593Smuzhiyun 	tcp_write_xmit(sk, mss_now, TCP_NAGLE_PUSH, 1, sk->sk_allocation);
2885*4882a593Smuzhiyun }
2886*4882a593Smuzhiyun 
2887*4882a593Smuzhiyun /* This function returns the amount that we can raise the
2888*4882a593Smuzhiyun  * usable window based on the following constraints
2889*4882a593Smuzhiyun  *
2890*4882a593Smuzhiyun  * 1. The window can never be shrunk once it is offered (RFC 793)
2891*4882a593Smuzhiyun  * 2. We limit memory per socket
2892*4882a593Smuzhiyun  *
2893*4882a593Smuzhiyun  * RFC 1122:
2894*4882a593Smuzhiyun  * "the suggested [SWS] avoidance algorithm for the receiver is to keep
2895*4882a593Smuzhiyun  *  RECV.NEXT + RCV.WIN fixed until:
2896*4882a593Smuzhiyun  *  RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)"
2897*4882a593Smuzhiyun  *
2898*4882a593Smuzhiyun  * i.e. don't raise the right edge of the window until you can raise
2899*4882a593Smuzhiyun  * it at least MSS bytes.
2900*4882a593Smuzhiyun  *
2901*4882a593Smuzhiyun  * Unfortunately, the recommended algorithm breaks header prediction,
2902*4882a593Smuzhiyun  * since header prediction assumes th->window stays fixed.
2903*4882a593Smuzhiyun  *
2904*4882a593Smuzhiyun  * Strictly speaking, keeping th->window fixed violates the receiver
2905*4882a593Smuzhiyun  * side SWS prevention criteria. The problem is that under this rule
2906*4882a593Smuzhiyun  * a stream of single byte packets will cause the right side of the
2907*4882a593Smuzhiyun  * window to always advance by a single byte.
2908*4882a593Smuzhiyun  *
2909*4882a593Smuzhiyun  * Of course, if the sender implements sender side SWS prevention
2910*4882a593Smuzhiyun  * then this will not be a problem.
2911*4882a593Smuzhiyun  *
2912*4882a593Smuzhiyun  * BSD seems to make the following compromise:
2913*4882a593Smuzhiyun  *
2914*4882a593Smuzhiyun  *	If the free space is less than the 1/4 of the maximum
2915*4882a593Smuzhiyun  *	space available and the free space is less than 1/2 mss,
2916*4882a593Smuzhiyun  *	then set the window to 0.
2917*4882a593Smuzhiyun  *	[ Actually, bsd uses MSS and 1/4 of maximal _window_ ]
2918*4882a593Smuzhiyun  *	Otherwise, just prevent the window from shrinking
2919*4882a593Smuzhiyun  *	and from being larger than the largest representable value.
2920*4882a593Smuzhiyun  *
2921*4882a593Smuzhiyun  * This prevents incremental opening of the window in the regime
2922*4882a593Smuzhiyun  * where TCP is limited by the speed of the reader side taking
2923*4882a593Smuzhiyun  * data out of the TCP receive queue. It does nothing about
2924*4882a593Smuzhiyun  * those cases where the window is constrained on the sender side
2925*4882a593Smuzhiyun  * because the pipeline is full.
2926*4882a593Smuzhiyun  *
2927*4882a593Smuzhiyun  * BSD also seems to "accidentally" limit itself to windows that are a
2928*4882a593Smuzhiyun  * multiple of MSS, at least until the free space gets quite small.
2929*4882a593Smuzhiyun  * This would appear to be a side effect of the mbuf implementation.
2930*4882a593Smuzhiyun  * Combining these two algorithms results in the observed behavior
2931*4882a593Smuzhiyun  * of having a fixed window size at almost all times.
2932*4882a593Smuzhiyun  *
2933*4882a593Smuzhiyun  * Below we obtain similar behavior by forcing the offered window to
2934*4882a593Smuzhiyun  * a multiple of the mss when it is feasible to do so.
2935*4882a593Smuzhiyun  *
2936*4882a593Smuzhiyun  * Note, we don't "adjust" for TIMESTAMP or SACK option bytes.
2937*4882a593Smuzhiyun  * Regular options like TIMESTAMP are taken into account.
2938*4882a593Smuzhiyun  */
__tcp_select_window(struct sock * sk)2939*4882a593Smuzhiyun u32 __tcp_select_window(struct sock *sk)
2940*4882a593Smuzhiyun {
2941*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
2942*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
2943*4882a593Smuzhiyun 	/* MSS for the peer's data.  Previous versions used mss_clamp
2944*4882a593Smuzhiyun 	 * here.  I don't know if the value based on our guesses
2945*4882a593Smuzhiyun 	 * of peer's MSS is better for the performance.  It's more correct
2946*4882a593Smuzhiyun 	 * but may be worse for the performance because of rcv_mss
2947*4882a593Smuzhiyun 	 * fluctuations.  --SAW  1998/11/1
2948*4882a593Smuzhiyun 	 */
2949*4882a593Smuzhiyun 	int mss = icsk->icsk_ack.rcv_mss;
2950*4882a593Smuzhiyun 	int free_space = tcp_space(sk);
2951*4882a593Smuzhiyun 	int allowed_space = tcp_full_space(sk);
2952*4882a593Smuzhiyun 	int full_space, window;
2953*4882a593Smuzhiyun 
2954*4882a593Smuzhiyun 	if (sk_is_mptcp(sk))
2955*4882a593Smuzhiyun 		mptcp_space(sk, &free_space, &allowed_space);
2956*4882a593Smuzhiyun 
2957*4882a593Smuzhiyun 	full_space = min_t(int, tp->window_clamp, allowed_space);
2958*4882a593Smuzhiyun 
2959*4882a593Smuzhiyun 	if (unlikely(mss > full_space)) {
2960*4882a593Smuzhiyun 		mss = full_space;
2961*4882a593Smuzhiyun 		if (mss <= 0)
2962*4882a593Smuzhiyun 			return 0;
2963*4882a593Smuzhiyun 	}
2964*4882a593Smuzhiyun 	if (free_space < (full_space >> 1)) {
2965*4882a593Smuzhiyun 		icsk->icsk_ack.quick = 0;
2966*4882a593Smuzhiyun 
2967*4882a593Smuzhiyun 		if (tcp_under_memory_pressure(sk))
2968*4882a593Smuzhiyun 			tp->rcv_ssthresh = min(tp->rcv_ssthresh,
2969*4882a593Smuzhiyun 					       4U * tp->advmss);
2970*4882a593Smuzhiyun 
2971*4882a593Smuzhiyun 		/* free_space might become our new window, make sure we don't
2972*4882a593Smuzhiyun 		 * increase it due to wscale.
2973*4882a593Smuzhiyun 		 */
2974*4882a593Smuzhiyun 		free_space = round_down(free_space, 1 << tp->rx_opt.rcv_wscale);
2975*4882a593Smuzhiyun 
2976*4882a593Smuzhiyun 		/* if free space is less than mss estimate, or is below 1/16th
2977*4882a593Smuzhiyun 		 * of the maximum allowed, try to move to zero-window, else
2978*4882a593Smuzhiyun 		 * tcp_clamp_window() will grow rcv buf up to tcp_rmem[2], and
2979*4882a593Smuzhiyun 		 * new incoming data is dropped due to memory limits.
2980*4882a593Smuzhiyun 		 * With large window, mss test triggers way too late in order
2981*4882a593Smuzhiyun 		 * to announce zero window in time before rmem limit kicks in.
2982*4882a593Smuzhiyun 		 */
2983*4882a593Smuzhiyun 		if (free_space < (allowed_space >> 4) || free_space < mss)
2984*4882a593Smuzhiyun 			return 0;
2985*4882a593Smuzhiyun 	}
2986*4882a593Smuzhiyun 
2987*4882a593Smuzhiyun 	if (free_space > tp->rcv_ssthresh)
2988*4882a593Smuzhiyun 		free_space = tp->rcv_ssthresh;
2989*4882a593Smuzhiyun 
2990*4882a593Smuzhiyun 	/* Don't do rounding if we are using window scaling, since the
2991*4882a593Smuzhiyun 	 * scaled window will not line up with the MSS boundary anyway.
2992*4882a593Smuzhiyun 	 */
2993*4882a593Smuzhiyun 	if (tp->rx_opt.rcv_wscale) {
2994*4882a593Smuzhiyun 		window = free_space;
2995*4882a593Smuzhiyun 
2996*4882a593Smuzhiyun 		/* Advertise enough space so that it won't get scaled away.
2997*4882a593Smuzhiyun 		 * Import case: prevent zero window announcement if
2998*4882a593Smuzhiyun 		 * 1<<rcv_wscale > mss.
2999*4882a593Smuzhiyun 		 */
3000*4882a593Smuzhiyun 		window = ALIGN(window, (1 << tp->rx_opt.rcv_wscale));
3001*4882a593Smuzhiyun 	} else {
3002*4882a593Smuzhiyun 		window = tp->rcv_wnd;
3003*4882a593Smuzhiyun 		/* Get the largest window that is a nice multiple of mss.
3004*4882a593Smuzhiyun 		 * Window clamp already applied above.
3005*4882a593Smuzhiyun 		 * If our current window offering is within 1 mss of the
3006*4882a593Smuzhiyun 		 * free space we just keep it. This prevents the divide
3007*4882a593Smuzhiyun 		 * and multiply from happening most of the time.
3008*4882a593Smuzhiyun 		 * We also don't do any window rounding when the free space
3009*4882a593Smuzhiyun 		 * is too small.
3010*4882a593Smuzhiyun 		 */
3011*4882a593Smuzhiyun 		if (window <= free_space - mss || window > free_space)
3012*4882a593Smuzhiyun 			window = rounddown(free_space, mss);
3013*4882a593Smuzhiyun 		else if (mss == full_space &&
3014*4882a593Smuzhiyun 			 free_space > window + (full_space >> 1))
3015*4882a593Smuzhiyun 			window = free_space;
3016*4882a593Smuzhiyun 	}
3017*4882a593Smuzhiyun 
3018*4882a593Smuzhiyun 	return window;
3019*4882a593Smuzhiyun }
3020*4882a593Smuzhiyun 
tcp_skb_collapse_tstamp(struct sk_buff * skb,const struct sk_buff * next_skb)3021*4882a593Smuzhiyun void tcp_skb_collapse_tstamp(struct sk_buff *skb,
3022*4882a593Smuzhiyun 			     const struct sk_buff *next_skb)
3023*4882a593Smuzhiyun {
3024*4882a593Smuzhiyun 	if (unlikely(tcp_has_tx_tstamp(next_skb))) {
3025*4882a593Smuzhiyun 		const struct skb_shared_info *next_shinfo =
3026*4882a593Smuzhiyun 			skb_shinfo(next_skb);
3027*4882a593Smuzhiyun 		struct skb_shared_info *shinfo = skb_shinfo(skb);
3028*4882a593Smuzhiyun 
3029*4882a593Smuzhiyun 		shinfo->tx_flags |= next_shinfo->tx_flags & SKBTX_ANY_TSTAMP;
3030*4882a593Smuzhiyun 		shinfo->tskey = next_shinfo->tskey;
3031*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->txstamp_ack |=
3032*4882a593Smuzhiyun 			TCP_SKB_CB(next_skb)->txstamp_ack;
3033*4882a593Smuzhiyun 	}
3034*4882a593Smuzhiyun }
3035*4882a593Smuzhiyun 
3036*4882a593Smuzhiyun /* Collapses two adjacent SKB's during retransmission. */
tcp_collapse_retrans(struct sock * sk,struct sk_buff * skb)3037*4882a593Smuzhiyun static bool tcp_collapse_retrans(struct sock *sk, struct sk_buff *skb)
3038*4882a593Smuzhiyun {
3039*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
3040*4882a593Smuzhiyun 	struct sk_buff *next_skb = skb_rb_next(skb);
3041*4882a593Smuzhiyun 	int next_skb_size;
3042*4882a593Smuzhiyun 
3043*4882a593Smuzhiyun 	next_skb_size = next_skb->len;
3044*4882a593Smuzhiyun 
3045*4882a593Smuzhiyun 	BUG_ON(tcp_skb_pcount(skb) != 1 || tcp_skb_pcount(next_skb) != 1);
3046*4882a593Smuzhiyun 
3047*4882a593Smuzhiyun 	if (next_skb_size) {
3048*4882a593Smuzhiyun 		if (next_skb_size <= skb_availroom(skb))
3049*4882a593Smuzhiyun 			skb_copy_bits(next_skb, 0, skb_put(skb, next_skb_size),
3050*4882a593Smuzhiyun 				      next_skb_size);
3051*4882a593Smuzhiyun 		else if (!tcp_skb_shift(skb, next_skb, 1, next_skb_size))
3052*4882a593Smuzhiyun 			return false;
3053*4882a593Smuzhiyun 	}
3054*4882a593Smuzhiyun 	tcp_highest_sack_replace(sk, next_skb, skb);
3055*4882a593Smuzhiyun 
3056*4882a593Smuzhiyun 	/* Update sequence range on original skb. */
3057*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq;
3058*4882a593Smuzhiyun 
3059*4882a593Smuzhiyun 	/* Merge over control information. This moves PSH/FIN etc. over */
3060*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(next_skb)->tcp_flags;
3061*4882a593Smuzhiyun 
3062*4882a593Smuzhiyun 	/* All done, get rid of second SKB and account for it so
3063*4882a593Smuzhiyun 	 * packet counting does not break.
3064*4882a593Smuzhiyun 	 */
3065*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked & TCPCB_EVER_RETRANS;
3066*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->eor = TCP_SKB_CB(next_skb)->eor;
3067*4882a593Smuzhiyun 
3068*4882a593Smuzhiyun 	/* changed transmit queue under us so clear hints */
3069*4882a593Smuzhiyun 	tcp_clear_retrans_hints_partial(tp);
3070*4882a593Smuzhiyun 	if (next_skb == tp->retransmit_skb_hint)
3071*4882a593Smuzhiyun 		tp->retransmit_skb_hint = skb;
3072*4882a593Smuzhiyun 
3073*4882a593Smuzhiyun 	tcp_adjust_pcount(sk, next_skb, tcp_skb_pcount(next_skb));
3074*4882a593Smuzhiyun 
3075*4882a593Smuzhiyun 	tcp_skb_collapse_tstamp(skb, next_skb);
3076*4882a593Smuzhiyun 
3077*4882a593Smuzhiyun 	tcp_rtx_queue_unlink_and_free(next_skb, sk);
3078*4882a593Smuzhiyun 	return true;
3079*4882a593Smuzhiyun }
3080*4882a593Smuzhiyun 
3081*4882a593Smuzhiyun /* Check if coalescing SKBs is legal. */
tcp_can_collapse(const struct sock * sk,const struct sk_buff * skb)3082*4882a593Smuzhiyun static bool tcp_can_collapse(const struct sock *sk, const struct sk_buff *skb)
3083*4882a593Smuzhiyun {
3084*4882a593Smuzhiyun 	if (tcp_skb_pcount(skb) > 1)
3085*4882a593Smuzhiyun 		return false;
3086*4882a593Smuzhiyun 	if (skb_cloned(skb))
3087*4882a593Smuzhiyun 		return false;
3088*4882a593Smuzhiyun 	/* Some heuristics for collapsing over SACK'd could be invented */
3089*4882a593Smuzhiyun 	if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
3090*4882a593Smuzhiyun 		return false;
3091*4882a593Smuzhiyun 
3092*4882a593Smuzhiyun 	return true;
3093*4882a593Smuzhiyun }
3094*4882a593Smuzhiyun 
3095*4882a593Smuzhiyun /* Collapse packets in the retransmit queue to make to create
3096*4882a593Smuzhiyun  * less packets on the wire. This is only done on retransmission.
3097*4882a593Smuzhiyun  */
tcp_retrans_try_collapse(struct sock * sk,struct sk_buff * to,int space)3098*4882a593Smuzhiyun static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *to,
3099*4882a593Smuzhiyun 				     int space)
3100*4882a593Smuzhiyun {
3101*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
3102*4882a593Smuzhiyun 	struct sk_buff *skb = to, *tmp;
3103*4882a593Smuzhiyun 	bool first = true;
3104*4882a593Smuzhiyun 
3105*4882a593Smuzhiyun 	if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_retrans_collapse))
3106*4882a593Smuzhiyun 		return;
3107*4882a593Smuzhiyun 	if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)
3108*4882a593Smuzhiyun 		return;
3109*4882a593Smuzhiyun 
3110*4882a593Smuzhiyun 	skb_rbtree_walk_from_safe(skb, tmp) {
3111*4882a593Smuzhiyun 		if (!tcp_can_collapse(sk, skb))
3112*4882a593Smuzhiyun 			break;
3113*4882a593Smuzhiyun 
3114*4882a593Smuzhiyun 		if (!tcp_skb_can_collapse(to, skb))
3115*4882a593Smuzhiyun 			break;
3116*4882a593Smuzhiyun 
3117*4882a593Smuzhiyun 		space -= skb->len;
3118*4882a593Smuzhiyun 
3119*4882a593Smuzhiyun 		if (first) {
3120*4882a593Smuzhiyun 			first = false;
3121*4882a593Smuzhiyun 			continue;
3122*4882a593Smuzhiyun 		}
3123*4882a593Smuzhiyun 
3124*4882a593Smuzhiyun 		if (space < 0)
3125*4882a593Smuzhiyun 			break;
3126*4882a593Smuzhiyun 
3127*4882a593Smuzhiyun 		if (after(TCP_SKB_CB(skb)->end_seq, tcp_wnd_end(tp)))
3128*4882a593Smuzhiyun 			break;
3129*4882a593Smuzhiyun 
3130*4882a593Smuzhiyun 		if (!tcp_collapse_retrans(sk, to))
3131*4882a593Smuzhiyun 			break;
3132*4882a593Smuzhiyun 	}
3133*4882a593Smuzhiyun }
3134*4882a593Smuzhiyun 
3135*4882a593Smuzhiyun /* This retransmits one SKB.  Policy decisions and retransmit queue
3136*4882a593Smuzhiyun  * state updates are done by the caller.  Returns non-zero if an
3137*4882a593Smuzhiyun  * error occurred which prevented the send.
3138*4882a593Smuzhiyun  */
__tcp_retransmit_skb(struct sock * sk,struct sk_buff * skb,int segs)3139*4882a593Smuzhiyun int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs)
3140*4882a593Smuzhiyun {
3141*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
3142*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
3143*4882a593Smuzhiyun 	unsigned int cur_mss;
3144*4882a593Smuzhiyun 	int diff, len, err;
3145*4882a593Smuzhiyun 	int avail_wnd;
3146*4882a593Smuzhiyun 
3147*4882a593Smuzhiyun 	/* Inconclusive MTU probe */
3148*4882a593Smuzhiyun 	if (icsk->icsk_mtup.probe_size)
3149*4882a593Smuzhiyun 		icsk->icsk_mtup.probe_size = 0;
3150*4882a593Smuzhiyun 
3151*4882a593Smuzhiyun 	/* Do not sent more than we queued. 1/4 is reserved for possible
3152*4882a593Smuzhiyun 	 * copying overhead: fragmentation, tunneling, mangling etc.
3153*4882a593Smuzhiyun 	 */
3154*4882a593Smuzhiyun 	if (refcount_read(&sk->sk_wmem_alloc) >
3155*4882a593Smuzhiyun 	    min_t(u32, sk->sk_wmem_queued + (sk->sk_wmem_queued >> 2),
3156*4882a593Smuzhiyun 		  sk->sk_sndbuf))
3157*4882a593Smuzhiyun 		return -EAGAIN;
3158*4882a593Smuzhiyun 
3159*4882a593Smuzhiyun 	if (skb_still_in_host_queue(sk, skb))
3160*4882a593Smuzhiyun 		return -EBUSY;
3161*4882a593Smuzhiyun 
3162*4882a593Smuzhiyun 	if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) {
3163*4882a593Smuzhiyun 		if (unlikely(before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))) {
3164*4882a593Smuzhiyun 			WARN_ON_ONCE(1);
3165*4882a593Smuzhiyun 			return -EINVAL;
3166*4882a593Smuzhiyun 		}
3167*4882a593Smuzhiyun 		if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
3168*4882a593Smuzhiyun 			return -ENOMEM;
3169*4882a593Smuzhiyun 	}
3170*4882a593Smuzhiyun 
3171*4882a593Smuzhiyun 	if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk))
3172*4882a593Smuzhiyun 		return -EHOSTUNREACH; /* Routing failure or similar. */
3173*4882a593Smuzhiyun 
3174*4882a593Smuzhiyun 	cur_mss = tcp_current_mss(sk);
3175*4882a593Smuzhiyun 	avail_wnd = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
3176*4882a593Smuzhiyun 
3177*4882a593Smuzhiyun 	/* If receiver has shrunk his window, and skb is out of
3178*4882a593Smuzhiyun 	 * new window, do not retransmit it. The exception is the
3179*4882a593Smuzhiyun 	 * case, when window is shrunk to zero. In this case
3180*4882a593Smuzhiyun 	 * our retransmit of one segment serves as a zero window probe.
3181*4882a593Smuzhiyun 	 */
3182*4882a593Smuzhiyun 	if (avail_wnd <= 0) {
3183*4882a593Smuzhiyun 		if (TCP_SKB_CB(skb)->seq != tp->snd_una)
3184*4882a593Smuzhiyun 			return -EAGAIN;
3185*4882a593Smuzhiyun 		avail_wnd = cur_mss;
3186*4882a593Smuzhiyun 	}
3187*4882a593Smuzhiyun 
3188*4882a593Smuzhiyun 	len = cur_mss * segs;
3189*4882a593Smuzhiyun 	if (len > avail_wnd) {
3190*4882a593Smuzhiyun 		len = rounddown(avail_wnd, cur_mss);
3191*4882a593Smuzhiyun 		if (!len)
3192*4882a593Smuzhiyun 			len = avail_wnd;
3193*4882a593Smuzhiyun 	}
3194*4882a593Smuzhiyun 	if (skb->len > len) {
3195*4882a593Smuzhiyun 		if (tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb, len,
3196*4882a593Smuzhiyun 				 cur_mss, GFP_ATOMIC))
3197*4882a593Smuzhiyun 			return -ENOMEM; /* We'll try again later. */
3198*4882a593Smuzhiyun 	} else {
3199*4882a593Smuzhiyun 		if (skb_unclone_keeptruesize(skb, GFP_ATOMIC))
3200*4882a593Smuzhiyun 			return -ENOMEM;
3201*4882a593Smuzhiyun 
3202*4882a593Smuzhiyun 		diff = tcp_skb_pcount(skb);
3203*4882a593Smuzhiyun 		tcp_set_skb_tso_segs(skb, cur_mss);
3204*4882a593Smuzhiyun 		diff -= tcp_skb_pcount(skb);
3205*4882a593Smuzhiyun 		if (diff)
3206*4882a593Smuzhiyun 			tcp_adjust_pcount(sk, skb, diff);
3207*4882a593Smuzhiyun 		avail_wnd = min_t(int, avail_wnd, cur_mss);
3208*4882a593Smuzhiyun 		if (skb->len < avail_wnd)
3209*4882a593Smuzhiyun 			tcp_retrans_try_collapse(sk, skb, avail_wnd);
3210*4882a593Smuzhiyun 	}
3211*4882a593Smuzhiyun 
3212*4882a593Smuzhiyun 	/* RFC3168, section 6.1.1.1. ECN fallback */
3213*4882a593Smuzhiyun 	if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN_ECN) == TCPHDR_SYN_ECN)
3214*4882a593Smuzhiyun 		tcp_ecn_clear_syn(sk, skb);
3215*4882a593Smuzhiyun 
3216*4882a593Smuzhiyun 	/* Update global and local TCP statistics. */
3217*4882a593Smuzhiyun 	segs = tcp_skb_pcount(skb);
3218*4882a593Smuzhiyun 	TCP_ADD_STATS(sock_net(sk), TCP_MIB_RETRANSSEGS, segs);
3219*4882a593Smuzhiyun 	if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)
3220*4882a593Smuzhiyun 		__NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNRETRANS);
3221*4882a593Smuzhiyun 	tp->total_retrans += segs;
3222*4882a593Smuzhiyun 	tp->bytes_retrans += skb->len;
3223*4882a593Smuzhiyun 
3224*4882a593Smuzhiyun 	/* make sure skb->data is aligned on arches that require it
3225*4882a593Smuzhiyun 	 * and check if ack-trimming & collapsing extended the headroom
3226*4882a593Smuzhiyun 	 * beyond what csum_start can cover.
3227*4882a593Smuzhiyun 	 */
3228*4882a593Smuzhiyun 	if (unlikely((NET_IP_ALIGN && ((unsigned long)skb->data & 3)) ||
3229*4882a593Smuzhiyun 		     skb_headroom(skb) >= 0xFFFF)) {
3230*4882a593Smuzhiyun 		struct sk_buff *nskb;
3231*4882a593Smuzhiyun 
3232*4882a593Smuzhiyun 		tcp_skb_tsorted_save(skb) {
3233*4882a593Smuzhiyun 			nskb = __pskb_copy(skb, MAX_TCP_HEADER, GFP_ATOMIC);
3234*4882a593Smuzhiyun 			if (nskb) {
3235*4882a593Smuzhiyun 				nskb->dev = NULL;
3236*4882a593Smuzhiyun 				err = tcp_transmit_skb(sk, nskb, 0, GFP_ATOMIC);
3237*4882a593Smuzhiyun 			} else {
3238*4882a593Smuzhiyun 				err = -ENOBUFS;
3239*4882a593Smuzhiyun 			}
3240*4882a593Smuzhiyun 		} tcp_skb_tsorted_restore(skb);
3241*4882a593Smuzhiyun 
3242*4882a593Smuzhiyun 		if (!err) {
3243*4882a593Smuzhiyun 			tcp_update_skb_after_send(sk, skb, tp->tcp_wstamp_ns);
3244*4882a593Smuzhiyun 			tcp_rate_skb_sent(sk, skb);
3245*4882a593Smuzhiyun 		}
3246*4882a593Smuzhiyun 	} else {
3247*4882a593Smuzhiyun 		err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
3248*4882a593Smuzhiyun 	}
3249*4882a593Smuzhiyun 
3250*4882a593Smuzhiyun 	/* To avoid taking spuriously low RTT samples based on a timestamp
3251*4882a593Smuzhiyun 	 * for a transmit that never happened, always mark EVER_RETRANS
3252*4882a593Smuzhiyun 	 */
3253*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->sacked |= TCPCB_EVER_RETRANS;
3254*4882a593Smuzhiyun 
3255*4882a593Smuzhiyun 	if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RETRANS_CB_FLAG))
3256*4882a593Smuzhiyun 		tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RETRANS_CB,
3257*4882a593Smuzhiyun 				  TCP_SKB_CB(skb)->seq, segs, err);
3258*4882a593Smuzhiyun 
3259*4882a593Smuzhiyun 	if (likely(!err)) {
3260*4882a593Smuzhiyun 		trace_tcp_retransmit_skb(sk, skb);
3261*4882a593Smuzhiyun 	} else if (err != -EBUSY) {
3262*4882a593Smuzhiyun 		NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPRETRANSFAIL, segs);
3263*4882a593Smuzhiyun 	}
3264*4882a593Smuzhiyun 	return err;
3265*4882a593Smuzhiyun }
3266*4882a593Smuzhiyun 
tcp_retransmit_skb(struct sock * sk,struct sk_buff * skb,int segs)3267*4882a593Smuzhiyun int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs)
3268*4882a593Smuzhiyun {
3269*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
3270*4882a593Smuzhiyun 	int err = __tcp_retransmit_skb(sk, skb, segs);
3271*4882a593Smuzhiyun 
3272*4882a593Smuzhiyun 	if (err == 0) {
3273*4882a593Smuzhiyun #if FASTRETRANS_DEBUG > 0
3274*4882a593Smuzhiyun 		if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
3275*4882a593Smuzhiyun 			net_dbg_ratelimited("retrans_out leaked\n");
3276*4882a593Smuzhiyun 		}
3277*4882a593Smuzhiyun #endif
3278*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS;
3279*4882a593Smuzhiyun 		tp->retrans_out += tcp_skb_pcount(skb);
3280*4882a593Smuzhiyun 	}
3281*4882a593Smuzhiyun 
3282*4882a593Smuzhiyun 	/* Save stamp of the first (attempted) retransmit. */
3283*4882a593Smuzhiyun 	if (!tp->retrans_stamp)
3284*4882a593Smuzhiyun 		tp->retrans_stamp = tcp_skb_timestamp(skb);
3285*4882a593Smuzhiyun 
3286*4882a593Smuzhiyun 	if (tp->undo_retrans < 0)
3287*4882a593Smuzhiyun 		tp->undo_retrans = 0;
3288*4882a593Smuzhiyun 	tp->undo_retrans += tcp_skb_pcount(skb);
3289*4882a593Smuzhiyun 	return err;
3290*4882a593Smuzhiyun }
3291*4882a593Smuzhiyun 
3292*4882a593Smuzhiyun /* This gets called after a retransmit timeout, and the initially
3293*4882a593Smuzhiyun  * retransmitted data is acknowledged.  It tries to continue
3294*4882a593Smuzhiyun  * resending the rest of the retransmit queue, until either
3295*4882a593Smuzhiyun  * we've sent it all or the congestion window limit is reached.
3296*4882a593Smuzhiyun  */
tcp_xmit_retransmit_queue(struct sock * sk)3297*4882a593Smuzhiyun void tcp_xmit_retransmit_queue(struct sock *sk)
3298*4882a593Smuzhiyun {
3299*4882a593Smuzhiyun 	const struct inet_connection_sock *icsk = inet_csk(sk);
3300*4882a593Smuzhiyun 	struct sk_buff *skb, *rtx_head, *hole = NULL;
3301*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
3302*4882a593Smuzhiyun 	bool rearm_timer = false;
3303*4882a593Smuzhiyun 	u32 max_segs;
3304*4882a593Smuzhiyun 	int mib_idx;
3305*4882a593Smuzhiyun 
3306*4882a593Smuzhiyun 	if (!tp->packets_out)
3307*4882a593Smuzhiyun 		return;
3308*4882a593Smuzhiyun 
3309*4882a593Smuzhiyun 	rtx_head = tcp_rtx_queue_head(sk);
3310*4882a593Smuzhiyun 	skb = tp->retransmit_skb_hint ?: rtx_head;
3311*4882a593Smuzhiyun 	max_segs = tcp_tso_segs(sk, tcp_current_mss(sk));
3312*4882a593Smuzhiyun 	skb_rbtree_walk_from(skb) {
3313*4882a593Smuzhiyun 		__u8 sacked;
3314*4882a593Smuzhiyun 		int segs;
3315*4882a593Smuzhiyun 
3316*4882a593Smuzhiyun 		if (tcp_pacing_check(sk))
3317*4882a593Smuzhiyun 			break;
3318*4882a593Smuzhiyun 
3319*4882a593Smuzhiyun 		/* we could do better than to assign each time */
3320*4882a593Smuzhiyun 		if (!hole)
3321*4882a593Smuzhiyun 			tp->retransmit_skb_hint = skb;
3322*4882a593Smuzhiyun 
3323*4882a593Smuzhiyun 		segs = tp->snd_cwnd - tcp_packets_in_flight(tp);
3324*4882a593Smuzhiyun 		if (segs <= 0)
3325*4882a593Smuzhiyun 			break;
3326*4882a593Smuzhiyun 		sacked = TCP_SKB_CB(skb)->sacked;
3327*4882a593Smuzhiyun 		/* In case tcp_shift_skb_data() have aggregated large skbs,
3328*4882a593Smuzhiyun 		 * we need to make sure not sending too bigs TSO packets
3329*4882a593Smuzhiyun 		 */
3330*4882a593Smuzhiyun 		segs = min_t(int, segs, max_segs);
3331*4882a593Smuzhiyun 
3332*4882a593Smuzhiyun 		if (tp->retrans_out >= tp->lost_out) {
3333*4882a593Smuzhiyun 			break;
3334*4882a593Smuzhiyun 		} else if (!(sacked & TCPCB_LOST)) {
3335*4882a593Smuzhiyun 			if (!hole && !(sacked & (TCPCB_SACKED_RETRANS|TCPCB_SACKED_ACKED)))
3336*4882a593Smuzhiyun 				hole = skb;
3337*4882a593Smuzhiyun 			continue;
3338*4882a593Smuzhiyun 
3339*4882a593Smuzhiyun 		} else {
3340*4882a593Smuzhiyun 			if (icsk->icsk_ca_state != TCP_CA_Loss)
3341*4882a593Smuzhiyun 				mib_idx = LINUX_MIB_TCPFASTRETRANS;
3342*4882a593Smuzhiyun 			else
3343*4882a593Smuzhiyun 				mib_idx = LINUX_MIB_TCPSLOWSTARTRETRANS;
3344*4882a593Smuzhiyun 		}
3345*4882a593Smuzhiyun 
3346*4882a593Smuzhiyun 		if (sacked & (TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))
3347*4882a593Smuzhiyun 			continue;
3348*4882a593Smuzhiyun 
3349*4882a593Smuzhiyun 		if (tcp_small_queue_check(sk, skb, 1))
3350*4882a593Smuzhiyun 			break;
3351*4882a593Smuzhiyun 
3352*4882a593Smuzhiyun 		if (tcp_retransmit_skb(sk, skb, segs))
3353*4882a593Smuzhiyun 			break;
3354*4882a593Smuzhiyun 
3355*4882a593Smuzhiyun 		NET_ADD_STATS(sock_net(sk), mib_idx, tcp_skb_pcount(skb));
3356*4882a593Smuzhiyun 
3357*4882a593Smuzhiyun 		if (tcp_in_cwnd_reduction(sk))
3358*4882a593Smuzhiyun 			tp->prr_out += tcp_skb_pcount(skb);
3359*4882a593Smuzhiyun 
3360*4882a593Smuzhiyun 		if (skb == rtx_head &&
3361*4882a593Smuzhiyun 		    icsk->icsk_pending != ICSK_TIME_REO_TIMEOUT)
3362*4882a593Smuzhiyun 			rearm_timer = true;
3363*4882a593Smuzhiyun 
3364*4882a593Smuzhiyun 	}
3365*4882a593Smuzhiyun 	if (rearm_timer)
3366*4882a593Smuzhiyun 		tcp_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
3367*4882a593Smuzhiyun 				     inet_csk(sk)->icsk_rto,
3368*4882a593Smuzhiyun 				     TCP_RTO_MAX);
3369*4882a593Smuzhiyun }
3370*4882a593Smuzhiyun 
3371*4882a593Smuzhiyun /* We allow to exceed memory limits for FIN packets to expedite
3372*4882a593Smuzhiyun  * connection tear down and (memory) recovery.
3373*4882a593Smuzhiyun  * Otherwise tcp_send_fin() could be tempted to either delay FIN
3374*4882a593Smuzhiyun  * or even be forced to close flow without any FIN.
3375*4882a593Smuzhiyun  * In general, we want to allow one skb per socket to avoid hangs
3376*4882a593Smuzhiyun  * with edge trigger epoll()
3377*4882a593Smuzhiyun  */
sk_forced_mem_schedule(struct sock * sk,int size)3378*4882a593Smuzhiyun void sk_forced_mem_schedule(struct sock *sk, int size)
3379*4882a593Smuzhiyun {
3380*4882a593Smuzhiyun 	int delta, amt;
3381*4882a593Smuzhiyun 
3382*4882a593Smuzhiyun 	delta = size - sk->sk_forward_alloc;
3383*4882a593Smuzhiyun 	if (delta <= 0)
3384*4882a593Smuzhiyun 		return;
3385*4882a593Smuzhiyun 	amt = sk_mem_pages(delta);
3386*4882a593Smuzhiyun 	sk->sk_forward_alloc += amt * SK_MEM_QUANTUM;
3387*4882a593Smuzhiyun 	sk_memory_allocated_add(sk, amt);
3388*4882a593Smuzhiyun 
3389*4882a593Smuzhiyun 	if (mem_cgroup_sockets_enabled && sk->sk_memcg)
3390*4882a593Smuzhiyun 		mem_cgroup_charge_skmem(sk->sk_memcg, amt);
3391*4882a593Smuzhiyun }
3392*4882a593Smuzhiyun 
3393*4882a593Smuzhiyun /* Send a FIN. The caller locks the socket for us.
3394*4882a593Smuzhiyun  * We should try to send a FIN packet really hard, but eventually give up.
3395*4882a593Smuzhiyun  */
tcp_send_fin(struct sock * sk)3396*4882a593Smuzhiyun void tcp_send_fin(struct sock *sk)
3397*4882a593Smuzhiyun {
3398*4882a593Smuzhiyun 	struct sk_buff *skb, *tskb, *tail = tcp_write_queue_tail(sk);
3399*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
3400*4882a593Smuzhiyun 
3401*4882a593Smuzhiyun 	/* Optimization, tack on the FIN if we have one skb in write queue and
3402*4882a593Smuzhiyun 	 * this skb was not yet sent, or we are under memory pressure.
3403*4882a593Smuzhiyun 	 * Note: in the latter case, FIN packet will be sent after a timeout,
3404*4882a593Smuzhiyun 	 * as TCP stack thinks it has already been transmitted.
3405*4882a593Smuzhiyun 	 */
3406*4882a593Smuzhiyun 	tskb = tail;
3407*4882a593Smuzhiyun 	if (!tskb && tcp_under_memory_pressure(sk))
3408*4882a593Smuzhiyun 		tskb = skb_rb_last(&sk->tcp_rtx_queue);
3409*4882a593Smuzhiyun 
3410*4882a593Smuzhiyun 	if (tskb) {
3411*4882a593Smuzhiyun 		TCP_SKB_CB(tskb)->tcp_flags |= TCPHDR_FIN;
3412*4882a593Smuzhiyun 		TCP_SKB_CB(tskb)->end_seq++;
3413*4882a593Smuzhiyun 		tp->write_seq++;
3414*4882a593Smuzhiyun 		if (!tail) {
3415*4882a593Smuzhiyun 			/* This means tskb was already sent.
3416*4882a593Smuzhiyun 			 * Pretend we included the FIN on previous transmit.
3417*4882a593Smuzhiyun 			 * We need to set tp->snd_nxt to the value it would have
3418*4882a593Smuzhiyun 			 * if FIN had been sent. This is because retransmit path
3419*4882a593Smuzhiyun 			 * does not change tp->snd_nxt.
3420*4882a593Smuzhiyun 			 */
3421*4882a593Smuzhiyun 			WRITE_ONCE(tp->snd_nxt, tp->snd_nxt + 1);
3422*4882a593Smuzhiyun 			return;
3423*4882a593Smuzhiyun 		}
3424*4882a593Smuzhiyun 	} else {
3425*4882a593Smuzhiyun 		skb = alloc_skb_fclone(MAX_TCP_HEADER, sk->sk_allocation);
3426*4882a593Smuzhiyun 		if (unlikely(!skb))
3427*4882a593Smuzhiyun 			return;
3428*4882a593Smuzhiyun 
3429*4882a593Smuzhiyun 		INIT_LIST_HEAD(&skb->tcp_tsorted_anchor);
3430*4882a593Smuzhiyun 		skb_reserve(skb, MAX_TCP_HEADER);
3431*4882a593Smuzhiyun 		sk_forced_mem_schedule(sk, skb->truesize);
3432*4882a593Smuzhiyun 		/* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */
3433*4882a593Smuzhiyun 		tcp_init_nondata_skb(skb, tp->write_seq,
3434*4882a593Smuzhiyun 				     TCPHDR_ACK | TCPHDR_FIN);
3435*4882a593Smuzhiyun 		tcp_queue_skb(sk, skb);
3436*4882a593Smuzhiyun 	}
3437*4882a593Smuzhiyun 	__tcp_push_pending_frames(sk, tcp_current_mss(sk), TCP_NAGLE_OFF);
3438*4882a593Smuzhiyun }
3439*4882a593Smuzhiyun 
3440*4882a593Smuzhiyun /* We get here when a process closes a file descriptor (either due to
3441*4882a593Smuzhiyun  * an explicit close() or as a byproduct of exit()'ing) and there
3442*4882a593Smuzhiyun  * was unread data in the receive queue.  This behavior is recommended
3443*4882a593Smuzhiyun  * by RFC 2525, section 2.17.  -DaveM
3444*4882a593Smuzhiyun  */
tcp_send_active_reset(struct sock * sk,gfp_t priority)3445*4882a593Smuzhiyun void tcp_send_active_reset(struct sock *sk, gfp_t priority)
3446*4882a593Smuzhiyun {
3447*4882a593Smuzhiyun 	struct sk_buff *skb;
3448*4882a593Smuzhiyun 
3449*4882a593Smuzhiyun 	TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTRSTS);
3450*4882a593Smuzhiyun 
3451*4882a593Smuzhiyun 	/* NOTE: No TCP options attached and we never retransmit this. */
3452*4882a593Smuzhiyun 	skb = alloc_skb(MAX_TCP_HEADER, priority);
3453*4882a593Smuzhiyun 	if (!skb) {
3454*4882a593Smuzhiyun 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED);
3455*4882a593Smuzhiyun 		return;
3456*4882a593Smuzhiyun 	}
3457*4882a593Smuzhiyun 
3458*4882a593Smuzhiyun 	/* Reserve space for headers and prepare control bits. */
3459*4882a593Smuzhiyun 	skb_reserve(skb, MAX_TCP_HEADER);
3460*4882a593Smuzhiyun 	tcp_init_nondata_skb(skb, tcp_acceptable_seq(sk),
3461*4882a593Smuzhiyun 			     TCPHDR_ACK | TCPHDR_RST);
3462*4882a593Smuzhiyun 	tcp_mstamp_refresh(tcp_sk(sk));
3463*4882a593Smuzhiyun 	/* Send it off. */
3464*4882a593Smuzhiyun 	if (tcp_transmit_skb(sk, skb, 0, priority))
3465*4882a593Smuzhiyun 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED);
3466*4882a593Smuzhiyun 
3467*4882a593Smuzhiyun 	/* skb of trace_tcp_send_reset() keeps the skb that caused RST,
3468*4882a593Smuzhiyun 	 * skb here is different to the troublesome skb, so use NULL
3469*4882a593Smuzhiyun 	 */
3470*4882a593Smuzhiyun 	trace_tcp_send_reset(sk, NULL);
3471*4882a593Smuzhiyun }
3472*4882a593Smuzhiyun 
3473*4882a593Smuzhiyun /* Send a crossed SYN-ACK during socket establishment.
3474*4882a593Smuzhiyun  * WARNING: This routine must only be called when we have already sent
3475*4882a593Smuzhiyun  * a SYN packet that crossed the incoming SYN that caused this routine
3476*4882a593Smuzhiyun  * to get called. If this assumption fails then the initial rcv_wnd
3477*4882a593Smuzhiyun  * and rcv_wscale values will not be correct.
3478*4882a593Smuzhiyun  */
tcp_send_synack(struct sock * sk)3479*4882a593Smuzhiyun int tcp_send_synack(struct sock *sk)
3480*4882a593Smuzhiyun {
3481*4882a593Smuzhiyun 	struct sk_buff *skb;
3482*4882a593Smuzhiyun 
3483*4882a593Smuzhiyun 	skb = tcp_rtx_queue_head(sk);
3484*4882a593Smuzhiyun 	if (!skb || !(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
3485*4882a593Smuzhiyun 		pr_err("%s: wrong queue state\n", __func__);
3486*4882a593Smuzhiyun 		return -EFAULT;
3487*4882a593Smuzhiyun 	}
3488*4882a593Smuzhiyun 	if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)) {
3489*4882a593Smuzhiyun 		if (skb_cloned(skb)) {
3490*4882a593Smuzhiyun 			struct sk_buff *nskb;
3491*4882a593Smuzhiyun 
3492*4882a593Smuzhiyun 			tcp_skb_tsorted_save(skb) {
3493*4882a593Smuzhiyun 				nskb = skb_copy(skb, GFP_ATOMIC);
3494*4882a593Smuzhiyun 			} tcp_skb_tsorted_restore(skb);
3495*4882a593Smuzhiyun 			if (!nskb)
3496*4882a593Smuzhiyun 				return -ENOMEM;
3497*4882a593Smuzhiyun 			INIT_LIST_HEAD(&nskb->tcp_tsorted_anchor);
3498*4882a593Smuzhiyun 			tcp_highest_sack_replace(sk, skb, nskb);
3499*4882a593Smuzhiyun 			tcp_rtx_queue_unlink_and_free(skb, sk);
3500*4882a593Smuzhiyun 			__skb_header_release(nskb);
3501*4882a593Smuzhiyun 			tcp_rbtree_insert(&sk->tcp_rtx_queue, nskb);
3502*4882a593Smuzhiyun 			sk_wmem_queued_add(sk, nskb->truesize);
3503*4882a593Smuzhiyun 			sk_mem_charge(sk, nskb->truesize);
3504*4882a593Smuzhiyun 			skb = nskb;
3505*4882a593Smuzhiyun 		}
3506*4882a593Smuzhiyun 
3507*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ACK;
3508*4882a593Smuzhiyun 		tcp_ecn_send_synack(sk, skb);
3509*4882a593Smuzhiyun 	}
3510*4882a593Smuzhiyun 	return tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
3511*4882a593Smuzhiyun }
3512*4882a593Smuzhiyun 
3513*4882a593Smuzhiyun /**
3514*4882a593Smuzhiyun  * tcp_make_synack - Allocate one skb and build a SYNACK packet.
3515*4882a593Smuzhiyun  * @sk: listener socket
3516*4882a593Smuzhiyun  * @dst: dst entry attached to the SYNACK. It is consumed and caller
3517*4882a593Smuzhiyun  *       should not use it again.
3518*4882a593Smuzhiyun  * @req: request_sock pointer
3519*4882a593Smuzhiyun  * @foc: cookie for tcp fast open
3520*4882a593Smuzhiyun  * @synack_type: Type of synack to prepare
3521*4882a593Smuzhiyun  * @syn_skb: SYN packet just received.  It could be NULL for rtx case.
3522*4882a593Smuzhiyun  */
tcp_make_synack(const struct sock * sk,struct dst_entry * dst,struct request_sock * req,struct tcp_fastopen_cookie * foc,enum tcp_synack_type synack_type,struct sk_buff * syn_skb)3523*4882a593Smuzhiyun struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
3524*4882a593Smuzhiyun 				struct request_sock *req,
3525*4882a593Smuzhiyun 				struct tcp_fastopen_cookie *foc,
3526*4882a593Smuzhiyun 				enum tcp_synack_type synack_type,
3527*4882a593Smuzhiyun 				struct sk_buff *syn_skb)
3528*4882a593Smuzhiyun {
3529*4882a593Smuzhiyun 	struct inet_request_sock *ireq = inet_rsk(req);
3530*4882a593Smuzhiyun 	const struct tcp_sock *tp = tcp_sk(sk);
3531*4882a593Smuzhiyun 	struct tcp_md5sig_key *md5 = NULL;
3532*4882a593Smuzhiyun 	struct tcp_out_options opts;
3533*4882a593Smuzhiyun 	struct sk_buff *skb;
3534*4882a593Smuzhiyun 	int tcp_header_size;
3535*4882a593Smuzhiyun 	struct tcphdr *th;
3536*4882a593Smuzhiyun 	int mss;
3537*4882a593Smuzhiyun 	u64 now;
3538*4882a593Smuzhiyun 
3539*4882a593Smuzhiyun 	skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
3540*4882a593Smuzhiyun 	if (unlikely(!skb)) {
3541*4882a593Smuzhiyun 		dst_release(dst);
3542*4882a593Smuzhiyun 		return NULL;
3543*4882a593Smuzhiyun 	}
3544*4882a593Smuzhiyun 	/* Reserve space for headers. */
3545*4882a593Smuzhiyun 	skb_reserve(skb, MAX_TCP_HEADER);
3546*4882a593Smuzhiyun 
3547*4882a593Smuzhiyun 	switch (synack_type) {
3548*4882a593Smuzhiyun 	case TCP_SYNACK_NORMAL:
3549*4882a593Smuzhiyun 		skb_set_owner_w(skb, req_to_sk(req));
3550*4882a593Smuzhiyun 		break;
3551*4882a593Smuzhiyun 	case TCP_SYNACK_COOKIE:
3552*4882a593Smuzhiyun 		/* Under synflood, we do not attach skb to a socket,
3553*4882a593Smuzhiyun 		 * to avoid false sharing.
3554*4882a593Smuzhiyun 		 */
3555*4882a593Smuzhiyun 		break;
3556*4882a593Smuzhiyun 	case TCP_SYNACK_FASTOPEN:
3557*4882a593Smuzhiyun 		/* sk is a const pointer, because we want to express multiple
3558*4882a593Smuzhiyun 		 * cpu might call us concurrently.
3559*4882a593Smuzhiyun 		 * sk->sk_wmem_alloc in an atomic, we can promote to rw.
3560*4882a593Smuzhiyun 		 */
3561*4882a593Smuzhiyun 		skb_set_owner_w(skb, (struct sock *)sk);
3562*4882a593Smuzhiyun 		break;
3563*4882a593Smuzhiyun 	}
3564*4882a593Smuzhiyun 	skb_dst_set(skb, dst);
3565*4882a593Smuzhiyun 
3566*4882a593Smuzhiyun 	mss = tcp_mss_clamp(tp, dst_metric_advmss(dst));
3567*4882a593Smuzhiyun 
3568*4882a593Smuzhiyun 	memset(&opts, 0, sizeof(opts));
3569*4882a593Smuzhiyun 	now = tcp_clock_ns();
3570*4882a593Smuzhiyun #ifdef CONFIG_SYN_COOKIES
3571*4882a593Smuzhiyun 	if (unlikely(synack_type == TCP_SYNACK_COOKIE && ireq->tstamp_ok))
3572*4882a593Smuzhiyun 		skb->skb_mstamp_ns = cookie_init_timestamp(req, now);
3573*4882a593Smuzhiyun 	else
3574*4882a593Smuzhiyun #endif
3575*4882a593Smuzhiyun 	{
3576*4882a593Smuzhiyun 		skb->skb_mstamp_ns = now;
3577*4882a593Smuzhiyun 		if (!tcp_rsk(req)->snt_synack) /* Timestamp first SYNACK */
3578*4882a593Smuzhiyun 			tcp_rsk(req)->snt_synack = tcp_skb_timestamp_us(skb);
3579*4882a593Smuzhiyun 	}
3580*4882a593Smuzhiyun 
3581*4882a593Smuzhiyun #ifdef CONFIG_TCP_MD5SIG
3582*4882a593Smuzhiyun 	rcu_read_lock();
3583*4882a593Smuzhiyun 	md5 = tcp_rsk(req)->af_specific->req_md5_lookup(sk, req_to_sk(req));
3584*4882a593Smuzhiyun #endif
3585*4882a593Smuzhiyun 	skb_set_hash(skb, tcp_rsk(req)->txhash, PKT_HASH_TYPE_L4);
3586*4882a593Smuzhiyun 	/* bpf program will be interested in the tcp_flags */
3587*4882a593Smuzhiyun 	TCP_SKB_CB(skb)->tcp_flags = TCPHDR_SYN | TCPHDR_ACK;
3588*4882a593Smuzhiyun 	tcp_header_size = tcp_synack_options(sk, req, mss, skb, &opts, md5,
3589*4882a593Smuzhiyun 					     foc, synack_type,
3590*4882a593Smuzhiyun 					     syn_skb) + sizeof(*th);
3591*4882a593Smuzhiyun 
3592*4882a593Smuzhiyun 	skb_push(skb, tcp_header_size);
3593*4882a593Smuzhiyun 	skb_reset_transport_header(skb);
3594*4882a593Smuzhiyun 
3595*4882a593Smuzhiyun 	th = (struct tcphdr *)skb->data;
3596*4882a593Smuzhiyun 	memset(th, 0, sizeof(struct tcphdr));
3597*4882a593Smuzhiyun 	th->syn = 1;
3598*4882a593Smuzhiyun 	th->ack = 1;
3599*4882a593Smuzhiyun 	tcp_ecn_make_synack(req, th);
3600*4882a593Smuzhiyun 	th->source = htons(ireq->ir_num);
3601*4882a593Smuzhiyun 	th->dest = ireq->ir_rmt_port;
3602*4882a593Smuzhiyun 	skb->mark = ireq->ir_mark;
3603*4882a593Smuzhiyun 	skb->ip_summed = CHECKSUM_PARTIAL;
3604*4882a593Smuzhiyun 	th->seq = htonl(tcp_rsk(req)->snt_isn);
3605*4882a593Smuzhiyun 	/* XXX data is queued and acked as is. No buffer/window check */
3606*4882a593Smuzhiyun 	th->ack_seq = htonl(tcp_rsk(req)->rcv_nxt);
3607*4882a593Smuzhiyun 
3608*4882a593Smuzhiyun 	/* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */
3609*4882a593Smuzhiyun 	th->window = htons(min(req->rsk_rcv_wnd, 65535U));
3610*4882a593Smuzhiyun 	tcp_options_write((__be32 *)(th + 1), NULL, &opts);
3611*4882a593Smuzhiyun 	th->doff = (tcp_header_size >> 2);
3612*4882a593Smuzhiyun 	__TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS);
3613*4882a593Smuzhiyun 
3614*4882a593Smuzhiyun #ifdef CONFIG_TCP_MD5SIG
3615*4882a593Smuzhiyun 	/* Okay, we have all we need - do the md5 hash if needed */
3616*4882a593Smuzhiyun 	if (md5)
3617*4882a593Smuzhiyun 		tcp_rsk(req)->af_specific->calc_md5_hash(opts.hash_location,
3618*4882a593Smuzhiyun 					       md5, req_to_sk(req), skb);
3619*4882a593Smuzhiyun 	rcu_read_unlock();
3620*4882a593Smuzhiyun #endif
3621*4882a593Smuzhiyun 
3622*4882a593Smuzhiyun 	bpf_skops_write_hdr_opt((struct sock *)sk, skb, req, syn_skb,
3623*4882a593Smuzhiyun 				synack_type, &opts);
3624*4882a593Smuzhiyun 
3625*4882a593Smuzhiyun 	skb->skb_mstamp_ns = now;
3626*4882a593Smuzhiyun 	tcp_add_tx_delay(skb, tp);
3627*4882a593Smuzhiyun 
3628*4882a593Smuzhiyun 	return skb;
3629*4882a593Smuzhiyun }
3630*4882a593Smuzhiyun EXPORT_SYMBOL(tcp_make_synack);
3631*4882a593Smuzhiyun 
tcp_ca_dst_init(struct sock * sk,const struct dst_entry * dst)3632*4882a593Smuzhiyun static void tcp_ca_dst_init(struct sock *sk, const struct dst_entry *dst)
3633*4882a593Smuzhiyun {
3634*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
3635*4882a593Smuzhiyun 	const struct tcp_congestion_ops *ca;
3636*4882a593Smuzhiyun 	u32 ca_key = dst_metric(dst, RTAX_CC_ALGO);
3637*4882a593Smuzhiyun 
3638*4882a593Smuzhiyun 	if (ca_key == TCP_CA_UNSPEC)
3639*4882a593Smuzhiyun 		return;
3640*4882a593Smuzhiyun 
3641*4882a593Smuzhiyun 	rcu_read_lock();
3642*4882a593Smuzhiyun 	ca = tcp_ca_find_key(ca_key);
3643*4882a593Smuzhiyun 	if (likely(ca && bpf_try_module_get(ca, ca->owner))) {
3644*4882a593Smuzhiyun 		bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner);
3645*4882a593Smuzhiyun 		icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst);
3646*4882a593Smuzhiyun 		icsk->icsk_ca_ops = ca;
3647*4882a593Smuzhiyun 	}
3648*4882a593Smuzhiyun 	rcu_read_unlock();
3649*4882a593Smuzhiyun }
3650*4882a593Smuzhiyun 
3651*4882a593Smuzhiyun /* Do all connect socket setups that can be done AF independent. */
tcp_connect_init(struct sock * sk)3652*4882a593Smuzhiyun static void tcp_connect_init(struct sock *sk)
3653*4882a593Smuzhiyun {
3654*4882a593Smuzhiyun 	const struct dst_entry *dst = __sk_dst_get(sk);
3655*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
3656*4882a593Smuzhiyun 	__u8 rcv_wscale;
3657*4882a593Smuzhiyun 	u32 rcv_wnd;
3658*4882a593Smuzhiyun 
3659*4882a593Smuzhiyun 	/* We'll fix this up when we get a response from the other end.
3660*4882a593Smuzhiyun 	 * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT.
3661*4882a593Smuzhiyun 	 */
3662*4882a593Smuzhiyun 	tp->tcp_header_len = sizeof(struct tcphdr);
3663*4882a593Smuzhiyun 	if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps))
3664*4882a593Smuzhiyun 		tp->tcp_header_len += TCPOLEN_TSTAMP_ALIGNED;
3665*4882a593Smuzhiyun 
3666*4882a593Smuzhiyun #ifdef CONFIG_TCP_MD5SIG
3667*4882a593Smuzhiyun 	if (tp->af_specific->md5_lookup(sk, sk))
3668*4882a593Smuzhiyun 		tp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
3669*4882a593Smuzhiyun #endif
3670*4882a593Smuzhiyun 
3671*4882a593Smuzhiyun 	/* If user gave his TCP_MAXSEG, record it to clamp */
3672*4882a593Smuzhiyun 	if (tp->rx_opt.user_mss)
3673*4882a593Smuzhiyun 		tp->rx_opt.mss_clamp = tp->rx_opt.user_mss;
3674*4882a593Smuzhiyun 	tp->max_window = 0;
3675*4882a593Smuzhiyun 	tcp_mtup_init(sk);
3676*4882a593Smuzhiyun 	tcp_sync_mss(sk, dst_mtu(dst));
3677*4882a593Smuzhiyun 
3678*4882a593Smuzhiyun 	tcp_ca_dst_init(sk, dst);
3679*4882a593Smuzhiyun 
3680*4882a593Smuzhiyun 	if (!tp->window_clamp)
3681*4882a593Smuzhiyun 		tp->window_clamp = dst_metric(dst, RTAX_WINDOW);
3682*4882a593Smuzhiyun 	tp->advmss = tcp_mss_clamp(tp, dst_metric_advmss(dst));
3683*4882a593Smuzhiyun 
3684*4882a593Smuzhiyun 	tcp_initialize_rcv_mss(sk);
3685*4882a593Smuzhiyun 
3686*4882a593Smuzhiyun 	/* limit the window selection if the user enforce a smaller rx buffer */
3687*4882a593Smuzhiyun 	if (sk->sk_userlocks & SOCK_RCVBUF_LOCK &&
3688*4882a593Smuzhiyun 	    (tp->window_clamp > tcp_full_space(sk) || tp->window_clamp == 0))
3689*4882a593Smuzhiyun 		tp->window_clamp = tcp_full_space(sk);
3690*4882a593Smuzhiyun 
3691*4882a593Smuzhiyun 	rcv_wnd = tcp_rwnd_init_bpf(sk);
3692*4882a593Smuzhiyun 	if (rcv_wnd == 0)
3693*4882a593Smuzhiyun 		rcv_wnd = dst_metric(dst, RTAX_INITRWND);
3694*4882a593Smuzhiyun 
3695*4882a593Smuzhiyun 	tcp_select_initial_window(sk, tcp_full_space(sk),
3696*4882a593Smuzhiyun 				  tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0),
3697*4882a593Smuzhiyun 				  &tp->rcv_wnd,
3698*4882a593Smuzhiyun 				  &tp->window_clamp,
3699*4882a593Smuzhiyun 				  READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_window_scaling),
3700*4882a593Smuzhiyun 				  &rcv_wscale,
3701*4882a593Smuzhiyun 				  rcv_wnd);
3702*4882a593Smuzhiyun 
3703*4882a593Smuzhiyun 	tp->rx_opt.rcv_wscale = rcv_wscale;
3704*4882a593Smuzhiyun 	tp->rcv_ssthresh = tp->rcv_wnd;
3705*4882a593Smuzhiyun 
3706*4882a593Smuzhiyun 	sk->sk_err = 0;
3707*4882a593Smuzhiyun 	sock_reset_flag(sk, SOCK_DONE);
3708*4882a593Smuzhiyun 	tp->snd_wnd = 0;
3709*4882a593Smuzhiyun 	tcp_init_wl(tp, 0);
3710*4882a593Smuzhiyun 	tcp_write_queue_purge(sk);
3711*4882a593Smuzhiyun 	tp->snd_una = tp->write_seq;
3712*4882a593Smuzhiyun 	tp->snd_sml = tp->write_seq;
3713*4882a593Smuzhiyun 	tp->snd_up = tp->write_seq;
3714*4882a593Smuzhiyun 	WRITE_ONCE(tp->snd_nxt, tp->write_seq);
3715*4882a593Smuzhiyun 
3716*4882a593Smuzhiyun 	if (likely(!tp->repair))
3717*4882a593Smuzhiyun 		tp->rcv_nxt = 0;
3718*4882a593Smuzhiyun 	else
3719*4882a593Smuzhiyun 		tp->rcv_tstamp = tcp_jiffies32;
3720*4882a593Smuzhiyun 	tp->rcv_wup = tp->rcv_nxt;
3721*4882a593Smuzhiyun 	WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
3722*4882a593Smuzhiyun 
3723*4882a593Smuzhiyun 	inet_csk(sk)->icsk_rto = tcp_timeout_init(sk);
3724*4882a593Smuzhiyun 	inet_csk(sk)->icsk_retransmits = 0;
3725*4882a593Smuzhiyun 	tcp_clear_retrans(tp);
3726*4882a593Smuzhiyun }
3727*4882a593Smuzhiyun 
tcp_connect_queue_skb(struct sock * sk,struct sk_buff * skb)3728*4882a593Smuzhiyun static void tcp_connect_queue_skb(struct sock *sk, struct sk_buff *skb)
3729*4882a593Smuzhiyun {
3730*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
3731*4882a593Smuzhiyun 	struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
3732*4882a593Smuzhiyun 
3733*4882a593Smuzhiyun 	tcb->end_seq += skb->len;
3734*4882a593Smuzhiyun 	__skb_header_release(skb);
3735*4882a593Smuzhiyun 	sk_wmem_queued_add(sk, skb->truesize);
3736*4882a593Smuzhiyun 	sk_mem_charge(sk, skb->truesize);
3737*4882a593Smuzhiyun 	WRITE_ONCE(tp->write_seq, tcb->end_seq);
3738*4882a593Smuzhiyun 	tp->packets_out += tcp_skb_pcount(skb);
3739*4882a593Smuzhiyun }
3740*4882a593Smuzhiyun 
3741*4882a593Smuzhiyun /* Build and send a SYN with data and (cached) Fast Open cookie. However,
3742*4882a593Smuzhiyun  * queue a data-only packet after the regular SYN, such that regular SYNs
3743*4882a593Smuzhiyun  * are retransmitted on timeouts. Also if the remote SYN-ACK acknowledges
3744*4882a593Smuzhiyun  * only the SYN sequence, the data are retransmitted in the first ACK.
3745*4882a593Smuzhiyun  * If cookie is not cached or other error occurs, falls back to send a
3746*4882a593Smuzhiyun  * regular SYN with Fast Open cookie request option.
3747*4882a593Smuzhiyun  */
tcp_send_syn_data(struct sock * sk,struct sk_buff * syn)3748*4882a593Smuzhiyun static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn)
3749*4882a593Smuzhiyun {
3750*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
3751*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
3752*4882a593Smuzhiyun 	struct tcp_fastopen_request *fo = tp->fastopen_req;
3753*4882a593Smuzhiyun 	int space, err = 0;
3754*4882a593Smuzhiyun 	struct sk_buff *syn_data;
3755*4882a593Smuzhiyun 
3756*4882a593Smuzhiyun 	tp->rx_opt.mss_clamp = tp->advmss;  /* If MSS is not cached */
3757*4882a593Smuzhiyun 	if (!tcp_fastopen_cookie_check(sk, &tp->rx_opt.mss_clamp, &fo->cookie))
3758*4882a593Smuzhiyun 		goto fallback;
3759*4882a593Smuzhiyun 
3760*4882a593Smuzhiyun 	/* MSS for SYN-data is based on cached MSS and bounded by PMTU and
3761*4882a593Smuzhiyun 	 * user-MSS. Reserve maximum option space for middleboxes that add
3762*4882a593Smuzhiyun 	 * private TCP options. The cost is reduced data space in SYN :(
3763*4882a593Smuzhiyun 	 */
3764*4882a593Smuzhiyun 	tp->rx_opt.mss_clamp = tcp_mss_clamp(tp, tp->rx_opt.mss_clamp);
3765*4882a593Smuzhiyun 	/* Sync mss_cache after updating the mss_clamp */
3766*4882a593Smuzhiyun 	tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
3767*4882a593Smuzhiyun 
3768*4882a593Smuzhiyun 	space = __tcp_mtu_to_mss(sk, icsk->icsk_pmtu_cookie) -
3769*4882a593Smuzhiyun 		MAX_TCP_OPTION_SPACE;
3770*4882a593Smuzhiyun 
3771*4882a593Smuzhiyun 	space = min_t(size_t, space, fo->size);
3772*4882a593Smuzhiyun 
3773*4882a593Smuzhiyun 	/* limit to order-0 allocations */
3774*4882a593Smuzhiyun 	space = min_t(size_t, space, SKB_MAX_HEAD(MAX_TCP_HEADER));
3775*4882a593Smuzhiyun 
3776*4882a593Smuzhiyun 	syn_data = sk_stream_alloc_skb(sk, space, sk->sk_allocation, false);
3777*4882a593Smuzhiyun 	if (!syn_data)
3778*4882a593Smuzhiyun 		goto fallback;
3779*4882a593Smuzhiyun 	syn_data->ip_summed = CHECKSUM_PARTIAL;
3780*4882a593Smuzhiyun 	memcpy(syn_data->cb, syn->cb, sizeof(syn->cb));
3781*4882a593Smuzhiyun 	if (space) {
3782*4882a593Smuzhiyun 		int copied = copy_from_iter(skb_put(syn_data, space), space,
3783*4882a593Smuzhiyun 					    &fo->data->msg_iter);
3784*4882a593Smuzhiyun 		if (unlikely(!copied)) {
3785*4882a593Smuzhiyun 			tcp_skb_tsorted_anchor_cleanup(syn_data);
3786*4882a593Smuzhiyun 			kfree_skb(syn_data);
3787*4882a593Smuzhiyun 			goto fallback;
3788*4882a593Smuzhiyun 		}
3789*4882a593Smuzhiyun 		if (copied != space) {
3790*4882a593Smuzhiyun 			skb_trim(syn_data, copied);
3791*4882a593Smuzhiyun 			space = copied;
3792*4882a593Smuzhiyun 		}
3793*4882a593Smuzhiyun 		skb_zcopy_set(syn_data, fo->uarg, NULL);
3794*4882a593Smuzhiyun 	}
3795*4882a593Smuzhiyun 	/* No more data pending in inet_wait_for_connect() */
3796*4882a593Smuzhiyun 	if (space == fo->size)
3797*4882a593Smuzhiyun 		fo->data = NULL;
3798*4882a593Smuzhiyun 	fo->copied = space;
3799*4882a593Smuzhiyun 
3800*4882a593Smuzhiyun 	tcp_connect_queue_skb(sk, syn_data);
3801*4882a593Smuzhiyun 	if (syn_data->len)
3802*4882a593Smuzhiyun 		tcp_chrono_start(sk, TCP_CHRONO_BUSY);
3803*4882a593Smuzhiyun 
3804*4882a593Smuzhiyun 	err = tcp_transmit_skb(sk, syn_data, 1, sk->sk_allocation);
3805*4882a593Smuzhiyun 
3806*4882a593Smuzhiyun 	syn->skb_mstamp_ns = syn_data->skb_mstamp_ns;
3807*4882a593Smuzhiyun 
3808*4882a593Smuzhiyun 	/* Now full SYN+DATA was cloned and sent (or not),
3809*4882a593Smuzhiyun 	 * remove the SYN from the original skb (syn_data)
3810*4882a593Smuzhiyun 	 * we keep in write queue in case of a retransmit, as we
3811*4882a593Smuzhiyun 	 * also have the SYN packet (with no data) in the same queue.
3812*4882a593Smuzhiyun 	 */
3813*4882a593Smuzhiyun 	TCP_SKB_CB(syn_data)->seq++;
3814*4882a593Smuzhiyun 	TCP_SKB_CB(syn_data)->tcp_flags = TCPHDR_ACK | TCPHDR_PSH;
3815*4882a593Smuzhiyun 	if (!err) {
3816*4882a593Smuzhiyun 		tp->syn_data = (fo->copied > 0);
3817*4882a593Smuzhiyun 		tcp_rbtree_insert(&sk->tcp_rtx_queue, syn_data);
3818*4882a593Smuzhiyun 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPORIGDATASENT);
3819*4882a593Smuzhiyun 		goto done;
3820*4882a593Smuzhiyun 	}
3821*4882a593Smuzhiyun 
3822*4882a593Smuzhiyun 	/* data was not sent, put it in write_queue */
3823*4882a593Smuzhiyun 	__skb_queue_tail(&sk->sk_write_queue, syn_data);
3824*4882a593Smuzhiyun 	tp->packets_out -= tcp_skb_pcount(syn_data);
3825*4882a593Smuzhiyun 
3826*4882a593Smuzhiyun fallback:
3827*4882a593Smuzhiyun 	/* Send a regular SYN with Fast Open cookie request option */
3828*4882a593Smuzhiyun 	if (fo->cookie.len > 0)
3829*4882a593Smuzhiyun 		fo->cookie.len = 0;
3830*4882a593Smuzhiyun 	err = tcp_transmit_skb(sk, syn, 1, sk->sk_allocation);
3831*4882a593Smuzhiyun 	if (err)
3832*4882a593Smuzhiyun 		tp->syn_fastopen = 0;
3833*4882a593Smuzhiyun done:
3834*4882a593Smuzhiyun 	fo->cookie.len = -1;  /* Exclude Fast Open option for SYN retries */
3835*4882a593Smuzhiyun 	return err;
3836*4882a593Smuzhiyun }
3837*4882a593Smuzhiyun 
3838*4882a593Smuzhiyun /* Build a SYN and send it off. */
tcp_connect(struct sock * sk)3839*4882a593Smuzhiyun int tcp_connect(struct sock *sk)
3840*4882a593Smuzhiyun {
3841*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
3842*4882a593Smuzhiyun 	struct sk_buff *buff;
3843*4882a593Smuzhiyun 	int err;
3844*4882a593Smuzhiyun 
3845*4882a593Smuzhiyun 	tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_CONNECT_CB, 0, NULL);
3846*4882a593Smuzhiyun 
3847*4882a593Smuzhiyun 	if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk))
3848*4882a593Smuzhiyun 		return -EHOSTUNREACH; /* Routing failure or similar. */
3849*4882a593Smuzhiyun 
3850*4882a593Smuzhiyun 	tcp_connect_init(sk);
3851*4882a593Smuzhiyun 
3852*4882a593Smuzhiyun 	if (unlikely(tp->repair)) {
3853*4882a593Smuzhiyun 		tcp_finish_connect(sk, NULL);
3854*4882a593Smuzhiyun 		return 0;
3855*4882a593Smuzhiyun 	}
3856*4882a593Smuzhiyun 
3857*4882a593Smuzhiyun 	buff = sk_stream_alloc_skb(sk, 0, sk->sk_allocation, true);
3858*4882a593Smuzhiyun 	if (unlikely(!buff))
3859*4882a593Smuzhiyun 		return -ENOBUFS;
3860*4882a593Smuzhiyun 
3861*4882a593Smuzhiyun 	tcp_init_nondata_skb(buff, tp->write_seq++, TCPHDR_SYN);
3862*4882a593Smuzhiyun 	tcp_mstamp_refresh(tp);
3863*4882a593Smuzhiyun 	tp->retrans_stamp = tcp_time_stamp(tp);
3864*4882a593Smuzhiyun 	tcp_connect_queue_skb(sk, buff);
3865*4882a593Smuzhiyun 	tcp_ecn_send_syn(sk, buff);
3866*4882a593Smuzhiyun 	tcp_rbtree_insert(&sk->tcp_rtx_queue, buff);
3867*4882a593Smuzhiyun 
3868*4882a593Smuzhiyun 	/* Send off SYN; include data in Fast Open. */
3869*4882a593Smuzhiyun 	err = tp->fastopen_req ? tcp_send_syn_data(sk, buff) :
3870*4882a593Smuzhiyun 	      tcp_transmit_skb(sk, buff, 1, sk->sk_allocation);
3871*4882a593Smuzhiyun 	if (err == -ECONNREFUSED)
3872*4882a593Smuzhiyun 		return err;
3873*4882a593Smuzhiyun 
3874*4882a593Smuzhiyun 	/* We change tp->snd_nxt after the tcp_transmit_skb() call
3875*4882a593Smuzhiyun 	 * in order to make this packet get counted in tcpOutSegs.
3876*4882a593Smuzhiyun 	 */
3877*4882a593Smuzhiyun 	WRITE_ONCE(tp->snd_nxt, tp->write_seq);
3878*4882a593Smuzhiyun 	tp->pushed_seq = tp->write_seq;
3879*4882a593Smuzhiyun 	buff = tcp_send_head(sk);
3880*4882a593Smuzhiyun 	if (unlikely(buff)) {
3881*4882a593Smuzhiyun 		WRITE_ONCE(tp->snd_nxt, TCP_SKB_CB(buff)->seq);
3882*4882a593Smuzhiyun 		tp->pushed_seq	= TCP_SKB_CB(buff)->seq;
3883*4882a593Smuzhiyun 	}
3884*4882a593Smuzhiyun 	TCP_INC_STATS(sock_net(sk), TCP_MIB_ACTIVEOPENS);
3885*4882a593Smuzhiyun 
3886*4882a593Smuzhiyun 	/* Timer for repeating the SYN until an answer. */
3887*4882a593Smuzhiyun 	inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
3888*4882a593Smuzhiyun 				  inet_csk(sk)->icsk_rto, TCP_RTO_MAX);
3889*4882a593Smuzhiyun 	return 0;
3890*4882a593Smuzhiyun }
3891*4882a593Smuzhiyun EXPORT_SYMBOL(tcp_connect);
3892*4882a593Smuzhiyun 
3893*4882a593Smuzhiyun /* Send out a delayed ack, the caller does the policy checking
3894*4882a593Smuzhiyun  * to see if we should even be here.  See tcp_input.c:tcp_ack_snd_check()
3895*4882a593Smuzhiyun  * for details.
3896*4882a593Smuzhiyun  */
tcp_send_delayed_ack(struct sock * sk)3897*4882a593Smuzhiyun void tcp_send_delayed_ack(struct sock *sk)
3898*4882a593Smuzhiyun {
3899*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
3900*4882a593Smuzhiyun 	int ato = icsk->icsk_ack.ato;
3901*4882a593Smuzhiyun 	unsigned long timeout;
3902*4882a593Smuzhiyun 
3903*4882a593Smuzhiyun 	if (ato > TCP_DELACK_MIN) {
3904*4882a593Smuzhiyun 		const struct tcp_sock *tp = tcp_sk(sk);
3905*4882a593Smuzhiyun 		int max_ato = HZ / 2;
3906*4882a593Smuzhiyun 
3907*4882a593Smuzhiyun 		if (inet_csk_in_pingpong_mode(sk) ||
3908*4882a593Smuzhiyun 		    (icsk->icsk_ack.pending & ICSK_ACK_PUSHED))
3909*4882a593Smuzhiyun 			max_ato = TCP_DELACK_MAX;
3910*4882a593Smuzhiyun 
3911*4882a593Smuzhiyun 		/* Slow path, intersegment interval is "high". */
3912*4882a593Smuzhiyun 
3913*4882a593Smuzhiyun 		/* If some rtt estimate is known, use it to bound delayed ack.
3914*4882a593Smuzhiyun 		 * Do not use inet_csk(sk)->icsk_rto here, use results of rtt measurements
3915*4882a593Smuzhiyun 		 * directly.
3916*4882a593Smuzhiyun 		 */
3917*4882a593Smuzhiyun 		if (tp->srtt_us) {
3918*4882a593Smuzhiyun 			int rtt = max_t(int, usecs_to_jiffies(tp->srtt_us >> 3),
3919*4882a593Smuzhiyun 					TCP_DELACK_MIN);
3920*4882a593Smuzhiyun 
3921*4882a593Smuzhiyun 			if (rtt < max_ato)
3922*4882a593Smuzhiyun 				max_ato = rtt;
3923*4882a593Smuzhiyun 		}
3924*4882a593Smuzhiyun 
3925*4882a593Smuzhiyun 		ato = min(ato, max_ato);
3926*4882a593Smuzhiyun 	}
3927*4882a593Smuzhiyun 
3928*4882a593Smuzhiyun 	ato = min_t(u32, ato, inet_csk(sk)->icsk_delack_max);
3929*4882a593Smuzhiyun 
3930*4882a593Smuzhiyun 	/* Stay within the limit we were given */
3931*4882a593Smuzhiyun 	timeout = jiffies + ato;
3932*4882a593Smuzhiyun 
3933*4882a593Smuzhiyun 	/* Use new timeout only if there wasn't a older one earlier. */
3934*4882a593Smuzhiyun 	if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
3935*4882a593Smuzhiyun 		/* If delack timer is about to expire, send ACK now. */
3936*4882a593Smuzhiyun 		if (time_before_eq(icsk->icsk_ack.timeout, jiffies + (ato >> 2))) {
3937*4882a593Smuzhiyun 			tcp_send_ack(sk);
3938*4882a593Smuzhiyun 			return;
3939*4882a593Smuzhiyun 		}
3940*4882a593Smuzhiyun 
3941*4882a593Smuzhiyun 		if (!time_before(timeout, icsk->icsk_ack.timeout))
3942*4882a593Smuzhiyun 			timeout = icsk->icsk_ack.timeout;
3943*4882a593Smuzhiyun 	}
3944*4882a593Smuzhiyun 	icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
3945*4882a593Smuzhiyun 	icsk->icsk_ack.timeout = timeout;
3946*4882a593Smuzhiyun 	sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
3947*4882a593Smuzhiyun }
3948*4882a593Smuzhiyun 
3949*4882a593Smuzhiyun /* This routine sends an ack and also updates the window. */
__tcp_send_ack(struct sock * sk,u32 rcv_nxt)3950*4882a593Smuzhiyun void __tcp_send_ack(struct sock *sk, u32 rcv_nxt)
3951*4882a593Smuzhiyun {
3952*4882a593Smuzhiyun 	struct sk_buff *buff;
3953*4882a593Smuzhiyun 
3954*4882a593Smuzhiyun 	/* If we have been reset, we may not send again. */
3955*4882a593Smuzhiyun 	if (sk->sk_state == TCP_CLOSE)
3956*4882a593Smuzhiyun 		return;
3957*4882a593Smuzhiyun 
3958*4882a593Smuzhiyun 	/* We are not putting this on the write queue, so
3959*4882a593Smuzhiyun 	 * tcp_transmit_skb() will set the ownership to this
3960*4882a593Smuzhiyun 	 * sock.
3961*4882a593Smuzhiyun 	 */
3962*4882a593Smuzhiyun 	buff = alloc_skb(MAX_TCP_HEADER,
3963*4882a593Smuzhiyun 			 sk_gfp_mask(sk, GFP_ATOMIC | __GFP_NOWARN));
3964*4882a593Smuzhiyun 	if (unlikely(!buff)) {
3965*4882a593Smuzhiyun 		struct inet_connection_sock *icsk = inet_csk(sk);
3966*4882a593Smuzhiyun 		unsigned long delay;
3967*4882a593Smuzhiyun 
3968*4882a593Smuzhiyun 		delay = TCP_DELACK_MAX << icsk->icsk_ack.retry;
3969*4882a593Smuzhiyun 		if (delay < TCP_RTO_MAX)
3970*4882a593Smuzhiyun 			icsk->icsk_ack.retry++;
3971*4882a593Smuzhiyun 		inet_csk_schedule_ack(sk);
3972*4882a593Smuzhiyun 		icsk->icsk_ack.ato = TCP_ATO_MIN;
3973*4882a593Smuzhiyun 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, delay, TCP_RTO_MAX);
3974*4882a593Smuzhiyun 		return;
3975*4882a593Smuzhiyun 	}
3976*4882a593Smuzhiyun 
3977*4882a593Smuzhiyun 	/* Reserve space for headers and prepare control bits. */
3978*4882a593Smuzhiyun 	skb_reserve(buff, MAX_TCP_HEADER);
3979*4882a593Smuzhiyun 	tcp_init_nondata_skb(buff, tcp_acceptable_seq(sk), TCPHDR_ACK);
3980*4882a593Smuzhiyun 
3981*4882a593Smuzhiyun 	/* We do not want pure acks influencing TCP Small Queues or fq/pacing
3982*4882a593Smuzhiyun 	 * too much.
3983*4882a593Smuzhiyun 	 * SKB_TRUESIZE(max(1 .. 66, MAX_TCP_HEADER)) is unfortunately ~784
3984*4882a593Smuzhiyun 	 */
3985*4882a593Smuzhiyun 	skb_set_tcp_pure_ack(buff);
3986*4882a593Smuzhiyun 
3987*4882a593Smuzhiyun 	/* Send it off, this clears delayed acks for us. */
3988*4882a593Smuzhiyun 	__tcp_transmit_skb(sk, buff, 0, (__force gfp_t)0, rcv_nxt);
3989*4882a593Smuzhiyun }
3990*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__tcp_send_ack);
3991*4882a593Smuzhiyun 
tcp_send_ack(struct sock * sk)3992*4882a593Smuzhiyun void tcp_send_ack(struct sock *sk)
3993*4882a593Smuzhiyun {
3994*4882a593Smuzhiyun 	__tcp_send_ack(sk, tcp_sk(sk)->rcv_nxt);
3995*4882a593Smuzhiyun }
3996*4882a593Smuzhiyun 
3997*4882a593Smuzhiyun /* This routine sends a packet with an out of date sequence
3998*4882a593Smuzhiyun  * number. It assumes the other end will try to ack it.
3999*4882a593Smuzhiyun  *
4000*4882a593Smuzhiyun  * Question: what should we make while urgent mode?
4001*4882a593Smuzhiyun  * 4.4BSD forces sending single byte of data. We cannot send
4002*4882a593Smuzhiyun  * out of window data, because we have SND.NXT==SND.MAX...
4003*4882a593Smuzhiyun  *
4004*4882a593Smuzhiyun  * Current solution: to send TWO zero-length segments in urgent mode:
4005*4882a593Smuzhiyun  * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is
4006*4882a593Smuzhiyun  * out-of-date with SND.UNA-1 to probe window.
4007*4882a593Smuzhiyun  */
tcp_xmit_probe_skb(struct sock * sk,int urgent,int mib)4008*4882a593Smuzhiyun static int tcp_xmit_probe_skb(struct sock *sk, int urgent, int mib)
4009*4882a593Smuzhiyun {
4010*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
4011*4882a593Smuzhiyun 	struct sk_buff *skb;
4012*4882a593Smuzhiyun 
4013*4882a593Smuzhiyun 	/* We don't queue it, tcp_transmit_skb() sets ownership. */
4014*4882a593Smuzhiyun 	skb = alloc_skb(MAX_TCP_HEADER,
4015*4882a593Smuzhiyun 			sk_gfp_mask(sk, GFP_ATOMIC | __GFP_NOWARN));
4016*4882a593Smuzhiyun 	if (!skb)
4017*4882a593Smuzhiyun 		return -1;
4018*4882a593Smuzhiyun 
4019*4882a593Smuzhiyun 	/* Reserve space for headers and set control bits. */
4020*4882a593Smuzhiyun 	skb_reserve(skb, MAX_TCP_HEADER);
4021*4882a593Smuzhiyun 	/* Use a previous sequence.  This should cause the other
4022*4882a593Smuzhiyun 	 * end to send an ack.  Don't queue or clone SKB, just
4023*4882a593Smuzhiyun 	 * send it.
4024*4882a593Smuzhiyun 	 */
4025*4882a593Smuzhiyun 	tcp_init_nondata_skb(skb, tp->snd_una - !urgent, TCPHDR_ACK);
4026*4882a593Smuzhiyun 	NET_INC_STATS(sock_net(sk), mib);
4027*4882a593Smuzhiyun 	return tcp_transmit_skb(sk, skb, 0, (__force gfp_t)0);
4028*4882a593Smuzhiyun }
4029*4882a593Smuzhiyun 
4030*4882a593Smuzhiyun /* Called from setsockopt( ... TCP_REPAIR ) */
tcp_send_window_probe(struct sock * sk)4031*4882a593Smuzhiyun void tcp_send_window_probe(struct sock *sk)
4032*4882a593Smuzhiyun {
4033*4882a593Smuzhiyun 	if (sk->sk_state == TCP_ESTABLISHED) {
4034*4882a593Smuzhiyun 		tcp_sk(sk)->snd_wl1 = tcp_sk(sk)->rcv_nxt - 1;
4035*4882a593Smuzhiyun 		tcp_mstamp_refresh(tcp_sk(sk));
4036*4882a593Smuzhiyun 		tcp_xmit_probe_skb(sk, 0, LINUX_MIB_TCPWINPROBE);
4037*4882a593Smuzhiyun 	}
4038*4882a593Smuzhiyun }
4039*4882a593Smuzhiyun 
4040*4882a593Smuzhiyun /* Initiate keepalive or window probe from timer. */
tcp_write_wakeup(struct sock * sk,int mib)4041*4882a593Smuzhiyun int tcp_write_wakeup(struct sock *sk, int mib)
4042*4882a593Smuzhiyun {
4043*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
4044*4882a593Smuzhiyun 	struct sk_buff *skb;
4045*4882a593Smuzhiyun 
4046*4882a593Smuzhiyun 	if (sk->sk_state == TCP_CLOSE)
4047*4882a593Smuzhiyun 		return -1;
4048*4882a593Smuzhiyun 
4049*4882a593Smuzhiyun 	skb = tcp_send_head(sk);
4050*4882a593Smuzhiyun 	if (skb && before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) {
4051*4882a593Smuzhiyun 		int err;
4052*4882a593Smuzhiyun 		unsigned int mss = tcp_current_mss(sk);
4053*4882a593Smuzhiyun 		unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
4054*4882a593Smuzhiyun 
4055*4882a593Smuzhiyun 		if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq))
4056*4882a593Smuzhiyun 			tp->pushed_seq = TCP_SKB_CB(skb)->end_seq;
4057*4882a593Smuzhiyun 
4058*4882a593Smuzhiyun 		/* We are probing the opening of a window
4059*4882a593Smuzhiyun 		 * but the window size is != 0
4060*4882a593Smuzhiyun 		 * must have been a result SWS avoidance ( sender )
4061*4882a593Smuzhiyun 		 */
4062*4882a593Smuzhiyun 		if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq ||
4063*4882a593Smuzhiyun 		    skb->len > mss) {
4064*4882a593Smuzhiyun 			seg_size = min(seg_size, mss);
4065*4882a593Smuzhiyun 			TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH;
4066*4882a593Smuzhiyun 			if (tcp_fragment(sk, TCP_FRAG_IN_WRITE_QUEUE,
4067*4882a593Smuzhiyun 					 skb, seg_size, mss, GFP_ATOMIC))
4068*4882a593Smuzhiyun 				return -1;
4069*4882a593Smuzhiyun 		} else if (!tcp_skb_pcount(skb))
4070*4882a593Smuzhiyun 			tcp_set_skb_tso_segs(skb, mss);
4071*4882a593Smuzhiyun 
4072*4882a593Smuzhiyun 		TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH;
4073*4882a593Smuzhiyun 		err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC);
4074*4882a593Smuzhiyun 		if (!err)
4075*4882a593Smuzhiyun 			tcp_event_new_data_sent(sk, skb);
4076*4882a593Smuzhiyun 		return err;
4077*4882a593Smuzhiyun 	} else {
4078*4882a593Smuzhiyun 		if (between(tp->snd_up, tp->snd_una + 1, tp->snd_una + 0xFFFF))
4079*4882a593Smuzhiyun 			tcp_xmit_probe_skb(sk, 1, mib);
4080*4882a593Smuzhiyun 		return tcp_xmit_probe_skb(sk, 0, mib);
4081*4882a593Smuzhiyun 	}
4082*4882a593Smuzhiyun }
4083*4882a593Smuzhiyun 
4084*4882a593Smuzhiyun /* A window probe timeout has occurred.  If window is not closed send
4085*4882a593Smuzhiyun  * a partial packet else a zero probe.
4086*4882a593Smuzhiyun  */
tcp_send_probe0(struct sock * sk)4087*4882a593Smuzhiyun void tcp_send_probe0(struct sock *sk)
4088*4882a593Smuzhiyun {
4089*4882a593Smuzhiyun 	struct inet_connection_sock *icsk = inet_csk(sk);
4090*4882a593Smuzhiyun 	struct tcp_sock *tp = tcp_sk(sk);
4091*4882a593Smuzhiyun 	struct net *net = sock_net(sk);
4092*4882a593Smuzhiyun 	unsigned long timeout;
4093*4882a593Smuzhiyun 	int err;
4094*4882a593Smuzhiyun 
4095*4882a593Smuzhiyun 	err = tcp_write_wakeup(sk, LINUX_MIB_TCPWINPROBE);
4096*4882a593Smuzhiyun 
4097*4882a593Smuzhiyun 	if (tp->packets_out || tcp_write_queue_empty(sk)) {
4098*4882a593Smuzhiyun 		/* Cancel probe timer, if it is not required. */
4099*4882a593Smuzhiyun 		icsk->icsk_probes_out = 0;
4100*4882a593Smuzhiyun 		icsk->icsk_backoff = 0;
4101*4882a593Smuzhiyun 		icsk->icsk_probes_tstamp = 0;
4102*4882a593Smuzhiyun 		return;
4103*4882a593Smuzhiyun 	}
4104*4882a593Smuzhiyun 
4105*4882a593Smuzhiyun 	icsk->icsk_probes_out++;
4106*4882a593Smuzhiyun 	if (err <= 0) {
4107*4882a593Smuzhiyun 		if (icsk->icsk_backoff < READ_ONCE(net->ipv4.sysctl_tcp_retries2))
4108*4882a593Smuzhiyun 			icsk->icsk_backoff++;
4109*4882a593Smuzhiyun 		timeout = tcp_probe0_when(sk, TCP_RTO_MAX);
4110*4882a593Smuzhiyun 	} else {
4111*4882a593Smuzhiyun 		/* If packet was not sent due to local congestion,
4112*4882a593Smuzhiyun 		 * Let senders fight for local resources conservatively.
4113*4882a593Smuzhiyun 		 */
4114*4882a593Smuzhiyun 		timeout = TCP_RESOURCE_PROBE_INTERVAL;
4115*4882a593Smuzhiyun 	}
4116*4882a593Smuzhiyun 
4117*4882a593Smuzhiyun 	timeout = tcp_clamp_probe0_to_user_timeout(sk, timeout);
4118*4882a593Smuzhiyun 	tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0, timeout, TCP_RTO_MAX);
4119*4882a593Smuzhiyun }
4120*4882a593Smuzhiyun 
tcp_rtx_synack(const struct sock * sk,struct request_sock * req)4121*4882a593Smuzhiyun int tcp_rtx_synack(const struct sock *sk, struct request_sock *req)
4122*4882a593Smuzhiyun {
4123*4882a593Smuzhiyun 	const struct tcp_request_sock_ops *af_ops = tcp_rsk(req)->af_specific;
4124*4882a593Smuzhiyun 	struct flowi fl;
4125*4882a593Smuzhiyun 	int res;
4126*4882a593Smuzhiyun 
4127*4882a593Smuzhiyun 	tcp_rsk(req)->txhash = net_tx_rndhash();
4128*4882a593Smuzhiyun 	res = af_ops->send_synack(sk, NULL, &fl, req, NULL, TCP_SYNACK_NORMAL,
4129*4882a593Smuzhiyun 				  NULL);
4130*4882a593Smuzhiyun 	if (!res) {
4131*4882a593Smuzhiyun 		TCP_INC_STATS(sock_net(sk), TCP_MIB_RETRANSSEGS);
4132*4882a593Smuzhiyun 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNRETRANS);
4133*4882a593Smuzhiyun 		if (unlikely(tcp_passive_fastopen(sk)))
4134*4882a593Smuzhiyun 			tcp_sk(sk)->total_retrans++;
4135*4882a593Smuzhiyun 		trace_tcp_retransmit_synack(sk, req);
4136*4882a593Smuzhiyun 	}
4137*4882a593Smuzhiyun 	return res;
4138*4882a593Smuzhiyun }
4139*4882a593Smuzhiyun EXPORT_SYMBOL(tcp_rtx_synack);
4140