xref: /linux/include/net/tcp.h (revision 059217c18be6757b95bfd77ba53fb50b48b8a816)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
51da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *		Definitions for the TCP module.
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Version:	@(#)tcp.h	1.0.5	05/23/93
101da177e4SLinus Torvalds  *
1102c30a84SJesper Juhl  * Authors:	Ross Biro
121da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds #ifndef _TCP_H
151da177e4SLinus Torvalds #define _TCP_H
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #define FASTRETRANS_DEBUG 1
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <linux/list.h>
201da177e4SLinus Torvalds #include <linux/tcp.h>
21187f1882SPaul Gortmaker #include <linux/bug.h>
221da177e4SLinus Torvalds #include <linux/slab.h>
231da177e4SLinus Torvalds #include <linux/cache.h>
241da177e4SLinus Torvalds #include <linux/percpu.h>
25fb286bb2SHerbert Xu #include <linux/skbuff.h>
26435cf559SWilliam Allen Simpson #include <linux/kref.h>
27740b0f18SEric Dumazet #include <linux/ktime.h>
2805e22e83SEric Dumazet #include <linux/indirect_call_wrapper.h>
293f421baaSArnaldo Carvalho de Melo 
303f421baaSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
31295ff7edSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h>
3277d8bf9cSArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
331da177e4SLinus Torvalds #include <net/checksum.h>
342e6599cbSArnaldo Carvalho de Melo #include <net/request_sock.h>
3540a1227eSMartin KaFai Lau #include <net/sock_reuseport.h>
361da177e4SLinus Torvalds #include <net/sock.h>
371da177e4SLinus Torvalds #include <net/snmp.h>
381da177e4SLinus Torvalds #include <net/ip.h>
39c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
40bdf1ee5dSIlpo Järvinen #include <net/inet_ecn.h>
410c266898SSatoru SATOH #include <net/dst.h>
4285712484SMat Martineau #include <net/mptcp.h>
43c752f073SArnaldo Carvalho de Melo 
441da177e4SLinus Torvalds #include <linux/seq_file.h>
45180d8cd9SGlauber Costa #include <linux/memcontrol.h>
4640304b2aSLawrence Brakmo #include <linux/bpf-cgroup.h>
47438ac880SArd Biesheuvel #include <linux/siphash.h>
4840304b2aSLawrence Brakmo 
490f7ff927SArnaldo Carvalho de Melo extern struct inet_hashinfo tcp_hashinfo;
501da177e4SLinus Torvalds 
5119757cebSEric Dumazet DECLARE_PER_CPU(unsigned int, tcp_orphan_count);
5219757cebSEric Dumazet int tcp_orphan_count_sum(void);
5319757cebSEric Dumazet 
545c9f3023SJoe Perches void tcp_time_wait(struct sock *sk, int state, int timeo);
551da177e4SLinus Torvalds 
569bacd256SEric Dumazet #define MAX_TCP_HEADER	L1_CACHE_ALIGN(128 + MAX_HEADER)
5733ad798cSAdam Langley #define MAX_TCP_OPTION_SPACE 40
583b4929f6SEric Dumazet #define TCP_MIN_SND_MSS		48
593b4929f6SEric Dumazet #define TCP_MIN_GSO_SIZE	(TCP_MIN_SND_MSS - MAX_TCP_OPTION_SPACE)
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /*
621da177e4SLinus Torvalds  * Never offer a window over 32767 without using window scaling. Some
631da177e4SLinus Torvalds  * poor stacks do signed 16bit maths!
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds #define MAX_TCP_WINDOW		32767U
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
681da177e4SLinus Torvalds #define TCP_MIN_MSS		88U
691da177e4SLinus Torvalds 
701555e6fdSJosh Hunt /* The initial MTU to use for probing */
71dcd8fb85SFan Du #define TCP_BASE_MSS		1024
725d424d5aSJohn Heffner 
7305cbc0dbSFan Du /* probing interval, default to 10 minutes as per RFC4821 */
7405cbc0dbSFan Du #define TCP_PROBE_INTERVAL	600
7505cbc0dbSFan Du 
766b58e0a5SFan Du /* Specify interval when tcp mtu probing will stop */
776b58e0a5SFan Du #define TCP_PROBE_THRESHOLD	8
786b58e0a5SFan Du 
791da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
801da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
831da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
841da177e4SLinus Torvalds 
85589c49cbSGao Feng /* Maximal number of window scale according to RFC1323 */
86589c49cbSGao Feng #define TCP_MAX_WSCALE		14U
87589c49cbSGao Feng 
881da177e4SLinus Torvalds /* urg_data states */
891da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
901da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
911da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #define TCP_RETR1	3	/*
941da177e4SLinus Torvalds 				 * This is how many retries it does before it
951da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
961da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
971da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
981da177e4SLinus Torvalds 				 */
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #define TCP_RETR2	15	/*
1011da177e4SLinus Torvalds 				 * This should take at least
1021da177e4SLinus Torvalds 				 * 90 minutes to time out.
1031da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
1041da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
1051da177e4SLinus Torvalds 				 */
1061da177e4SLinus Torvalds 
1076c9ff979SAlex Bergmann #define TCP_SYN_RETRIES	 6	/* This is how many retries are done
1086c9ff979SAlex Bergmann 				 * when active opening a connection.
1096c9ff979SAlex Bergmann 				 * RFC1122 says the minimum retry MUST
1106c9ff979SAlex Bergmann 				 * be at least 180secs.  Nevertheless
1116c9ff979SAlex Bergmann 				 * this value is corresponding to
1126c9ff979SAlex Bergmann 				 * 63secs of retransmission with the
1136c9ff979SAlex Bergmann 				 * current initial RTO.
1146c9ff979SAlex Bergmann 				 */
1151da177e4SLinus Torvalds 
1166c9ff979SAlex Bergmann #define TCP_SYNACK_RETRIES 5	/* This is how may retries are done
1176c9ff979SAlex Bergmann 				 * when passive opening a connection.
1186c9ff979SAlex Bergmann 				 * This is corresponding to 31secs of
1196c9ff979SAlex Bergmann 				 * retransmission with the current
1206c9ff979SAlex Bergmann 				 * initial RTO.
1216c9ff979SAlex Bergmann 				 */
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1241da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1251da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1261da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1271da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1281da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1291da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1301da177e4SLinus Torvalds 				  */
131f0628c52SCambda Zhu #define TCP_FIN_TIMEOUT_MAX (120 * HZ) /* max TCP_LINGER2 value (two minutes) */
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1341da177e4SLinus Torvalds #if HZ >= 100
1351da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1361da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1371da177e4SLinus Torvalds #else
1381da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1391da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1401da177e4SLinus Torvalds #endif
1411da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1421da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
143bb4d991aSYuchung Cheng #define TCP_TIMEOUT_MIN	(2U) /* Min timeout for TCP timers in jiffies */
144fd4f2ceaSEric Dumazet #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))	/* RFC6298 2.1 initial RTO value	*/
1459ad7c049SJerry Chu #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value, now
1469ad7c049SJerry Chu 						 * used as a fallback RTO for the
1479ad7c049SJerry Chu 						 * initial data transmission if no
1489ad7c049SJerry Chu 						 * valid RTT sample has been acquired,
1499ad7c049SJerry Chu 						 * most likely due to retrans in 3WHS.
1509ad7c049SJerry Chu 						 */
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1531da177e4SLinus Torvalds 					                 * for local resources.
1541da177e4SLinus Torvalds 					                 */
1551da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1561da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1571da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1601da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1611da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1621da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1651da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1661da177e4SLinus Torvalds 					 * after this time. It should be equal
1671da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1681da177e4SLinus Torvalds 					 * to provide reliability equal to one
1691da177e4SLinus Torvalds 					 * provided by timewait state.
1701da177e4SLinus Torvalds 					 */
1711da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1721da177e4SLinus Torvalds 					 * timestamps. It must be less than
1731da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1741da177e4SLinus Torvalds 					 */
1751da177e4SLinus Torvalds /*
1761da177e4SLinus Torvalds  *	TCP option
1771da177e4SLinus Torvalds  */
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1801da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1811da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1821da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1831da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1841da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1851da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
186cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
187c74a39c8SMat Martineau #define TCPOPT_MPTCP		30	/* Multipath TCP (RFC6824) */
1887f9b838bSDaniel Lee #define TCPOPT_FASTOPEN		34	/* Fast open (RFC7413) */
1892100c8d2SYuchung Cheng #define TCPOPT_EXP		254	/* Experimental */
1902100c8d2SYuchung Cheng /* Magic number to be after the option value for sharing TCP
1912100c8d2SYuchung Cheng  * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
1922100c8d2SYuchung Cheng  */
1932100c8d2SYuchung Cheng #define TCPOPT_FASTOPEN_MAGIC	0xF989
19460e2a778SUrsula Braun #define TCPOPT_SMC_MAGIC	0xE2D4C3D9
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /*
1971da177e4SLinus Torvalds  *     TCP option lengths
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds #define TCPOLEN_MSS            4
2011da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
2021da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
2031da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
204cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG         18
2057f9b838bSDaniel Lee #define TCPOLEN_FASTOPEN_BASE  2
2062100c8d2SYuchung Cheng #define TCPOLEN_EXP_FASTOPEN_BASE  4
20760e2a778SUrsula Braun #define TCPOLEN_EXP_SMC_BASE   6
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds /* But this is what stacks really send out. */
2101da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
2111da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
2121da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
2131da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
2141da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
2151da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
216cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED		20
21733ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED		4
21860e2a778SUrsula Braun #define TCPOLEN_EXP_SMC_BASE_ALIGNED	8
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds /* Flags in tp->nonagle */
2211da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
2221da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
223caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
2241da177e4SLinus Torvalds 
22536e31b0aSAndreas Petlund /* TCP thin-stream limits */
22636e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
22736e31b0aSAndreas Petlund 
22821603fc4SJörg Thalheim /* TCP initial congestion window as per rfc6928 */
229442b9635SDavid S. Miller #define TCP_INIT_CWND		10
230442b9635SDavid S. Miller 
231cf60af03SYuchung Cheng /* Bit Flags for sysctl_tcp_fastopen */
232cf60af03SYuchung Cheng #define	TFO_CLIENT_ENABLE	1
23310467163SJerry Chu #define	TFO_SERVER_ENABLE	2
23467da22d2SYuchung Cheng #define	TFO_CLIENT_NO_COOKIE	4	/* Data in SYN w/o cookie option */
235cf60af03SYuchung Cheng 
23610467163SJerry Chu /* Accept SYN data w/o any cookie option */
23710467163SJerry Chu #define	TFO_SERVER_COOKIE_NOT_REQD	0x200
23810467163SJerry Chu 
23910467163SJerry Chu /* Force enable TFO on all listeners, i.e., not requiring the
240cebc5cbaSYuchung Cheng  * TCP_FASTOPEN socket option.
24110467163SJerry Chu  */
24210467163SJerry Chu #define	TFO_SERVER_WO_SOCKOPT1	0x400
24310467163SJerry Chu 
244295ff7edSArnaldo Carvalho de Melo 
2451da177e4SLinus Torvalds /* sysctl variables for tcp */
2461da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
247a4fe34bfSEric W. Biederman extern long sysctl_tcp_mem[3];
248e20223f1SEric Dumazet 
249a0370b3fSYuchung Cheng #define TCP_RACK_LOSS_DETECTION  0x1 /* Use RACK to detect losses */
2501f255691SPriyaranjan Jha #define TCP_RACK_STATIC_REO_WND  0x2 /* Use static RACK reo wnd */
25120b654dfSYuchung Cheng #define TCP_RACK_NO_DUPTHRESH    0x4 /* Do not use DUPACK threshold in RACK */
252a0370b3fSYuchung Cheng 
2538d987e5cSEric Dumazet extern atomic_long_t tcp_memory_allocated;
2540defbb0aSEric Dumazet DECLARE_PER_CPU(int, tcp_memory_per_cpu_fw_alloc);
2550defbb0aSEric Dumazet 
2561748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated;
25706044751SEric Dumazet extern unsigned long tcp_memory_pressure;
2581da177e4SLinus Torvalds 
259b8da51ebSEric Dumazet /* optimized version of sk_under_memory_pressure() for TCP sockets */
260b8da51ebSEric Dumazet static inline bool tcp_under_memory_pressure(const struct sock *sk)
261b8da51ebSEric Dumazet {
262baac50bbSJohannes Weiner 	if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
263baac50bbSJohannes Weiner 	    mem_cgroup_under_socket_pressure(sk->sk_memcg))
264e805605cSJohannes Weiner 		return true;
265b8da51ebSEric Dumazet 
2661f142c17SEric Dumazet 	return READ_ONCE(tcp_memory_pressure);
267b8da51ebSEric Dumazet }
2681da177e4SLinus Torvalds /*
2691da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
2701da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
2711da177e4SLinus Torvalds  */
2721da177e4SLinus Torvalds 
273a2a385d6SEric Dumazet static inline bool before(__u32 seq1, __u32 seq2)
2741da177e4SLinus Torvalds {
2750d630cc0SGerrit Renker         return (__s32)(seq1-seq2) < 0;
2761da177e4SLinus Torvalds }
2779a036b9cSGerrit Renker #define after(seq2, seq1) 	before(seq1, seq2)
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
280a2a385d6SEric Dumazet static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
285efcdbf24SArun Sharma static inline bool tcp_out_of_memory(struct sock *sk)
286efcdbf24SArun Sharma {
287efcdbf24SArun Sharma 	if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
288efcdbf24SArun Sharma 	    sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
289efcdbf24SArun Sharma 		return true;
290efcdbf24SArun Sharma 	return false;
291efcdbf24SArun Sharma }
292efcdbf24SArun Sharma 
29303271f3aSTalal Ahmad static inline void tcp_wmem_free_skb(struct sock *sk, struct sk_buff *skb)
29403271f3aSTalal Ahmad {
29503271f3aSTalal Ahmad 	sk_wmem_queued_add(sk, -skb->truesize);
2969b65b17dSTalal Ahmad 	if (!skb_zcopy_pure(skb))
29703271f3aSTalal Ahmad 		sk_mem_uncharge(sk, skb->truesize);
2989b65b17dSTalal Ahmad 	else
2999b65b17dSTalal Ahmad 		sk_mem_uncharge(sk, SKB_TRUESIZE(skb_end_offset(skb)));
30003271f3aSTalal Ahmad 	__kfree_skb(skb);
30103271f3aSTalal Ahmad }
30203271f3aSTalal Ahmad 
303a6c5ea4cSEric Dumazet void sk_forced_mem_schedule(struct sock *sk, int size);
304a6c5ea4cSEric Dumazet 
3055c9f3023SJoe Perches bool tcp_check_oom(struct sock *sk, int shift);
306efcdbf24SArun Sharma 
307a0f82f64SFlorian Westphal 
3081da177e4SLinus Torvalds extern struct proto tcp_prot;
3091da177e4SLinus Torvalds 
31057ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
31113415e46SEric Dumazet #define __TCP_INC_STATS(net, field)	__SNMP_INC_STATS((net)->mib.tcp_statistics, field)
31257ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
313aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
3141da177e4SLinus Torvalds 
3155c9f3023SJoe Perches void tcp_tasklet_init(void);
31646d3ceabSEric Dumazet 
31732bbd879SStefano Brivio int tcp_v4_err(struct sk_buff *skb, u32);
3181da177e4SLinus Torvalds 
3195c9f3023SJoe Perches void tcp_shutdown(struct sock *sk, int how);
3201da177e4SLinus Torvalds 
3217487449cSPaolo Abeni int tcp_v4_early_demux(struct sk_buff *skb);
3225c9f3023SJoe Perches int tcp_v4_rcv(struct sk_buff *skb);
3231da177e4SLinus Torvalds 
32427728ba8SEric Dumazet void tcp_remove_empty_skb(struct sock *sk);
3251b784140SYing Xue int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
326306b13ebSTom Herbert int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size);
3273242abebSBenjamin Hesmans int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, int *copied,
3283242abebSBenjamin Hesmans 			 size_t size, struct ubuf_info *uarg);
3291d7e4538SDavid Howells void tcp_splice_eof(struct socket *sock);
33035b2c321SMat Martineau int tcp_send_mss(struct sock *sk, int *size_goal, int flags);
331fbf93406SEric Dumazet int tcp_wmem_schedule(struct sock *sk, int copy);
33235b2c321SMat Martineau void tcp_push(struct sock *sk, int flags, int mss_now, int nonagle,
33335b2c321SMat Martineau 	      int size_goal);
3345c9f3023SJoe Perches void tcp_release_cb(struct sock *sk);
3355c9f3023SJoe Perches void tcp_wfree(struct sk_buff *skb);
3365c9f3023SJoe Perches void tcp_write_timer_handler(struct sock *sk);
3375c9f3023SJoe Perches void tcp_delack_timer_handler(struct sock *sk);
338e1d001faSBreno Leitao int tcp_ioctl(struct sock *sk, int cmd, int *karg);
33972ab4a86SEric Dumazet int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb);
3403d97d88eSYafang Shao void tcp_rcv_established(struct sock *sk, struct sk_buff *skb);
3415c9f3023SJoe Perches void tcp_rcv_space_adjust(struct sock *sk);
3425c9f3023SJoe Perches int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
3435c9f3023SJoe Perches void tcp_twsk_destructor(struct sock *sk);
344edc12f03SKuniyuki Iwashima void tcp_twsk_purge(struct list_head *net_exit_list, int family);
3455c9f3023SJoe Perches ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
34653d3176bSChangli Gao 			struct pipe_inode_info *pipe, size_t len,
34753d3176bSChangli Gao 			unsigned int flags);
3485882efffSEric Dumazet struct sk_buff *tcp_stream_alloc_skb(struct sock *sk, gfp_t gfp,
349f8dd3b8dSEric Dumazet 				     bool force_schedule);
3509c55e01cSJens Axboe 
351*059217c1SNeal Cardwell static inline void tcp_dec_quickack_mode(struct sock *sk)
3521da177e4SLinus Torvalds {
353463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
354fc6415bcSDavid S. Miller 
355463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
356*059217c1SNeal Cardwell 		/* How many ACKs S/ACKing new data have we sent? */
357*059217c1SNeal Cardwell 		const unsigned int pkts = inet_csk_ack_scheduled(sk) ? 1 : 0;
358*059217c1SNeal Cardwell 
359463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
360463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3611da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
362463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
363fc6415bcSDavid S. Miller 		} else
364463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
368bdf1ee5dSIlpo Järvinen #define	TCP_ECN_OK		1
369bdf1ee5dSIlpo Järvinen #define	TCP_ECN_QUEUE_CWR	2
370bdf1ee5dSIlpo Järvinen #define	TCP_ECN_DEMAND_CWR	4
3717a269ffaSEric Dumazet #define	TCP_ECN_SEEN		8
372bdf1ee5dSIlpo Järvinen 
373fd2c3ef7SEric Dumazet enum tcp_tw_status {
3741da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
3751da177e4SLinus Torvalds 	TCP_TW_RST = 1,
3761da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
3771da177e4SLinus Torvalds 	TCP_TW_SYN = 3
3781da177e4SLinus Torvalds };
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 
3815c9f3023SJoe Perches enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
3821da177e4SLinus Torvalds 					      struct sk_buff *skb,
3838feaf0c0SArnaldo Carvalho de Melo 					      const struct tcphdr *th);
3845c9f3023SJoe Perches struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
385e0f9759fSEric Dumazet 			   struct request_sock *req, bool fastopen,
386e0f9759fSEric Dumazet 			   bool *lost_race);
3875c9f3023SJoe Perches int tcp_child_process(struct sock *parent, struct sock *child,
3881da177e4SLinus Torvalds 		      struct sk_buff *skb);
3895ae344c9SNeal Cardwell void tcp_enter_loss(struct sock *sk);
3907e901ee7SYuchung Cheng void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int newly_lost, int flag);
3915c9f3023SJoe Perches void tcp_clear_retrans(struct tcp_sock *tp);
3925c9f3023SJoe Perches void tcp_update_metrics(struct sock *sk);
3935c9f3023SJoe Perches void tcp_init_metrics(struct sock *sk);
3945c9f3023SJoe Perches void tcp_metrics_init(void);
395d82bae12SSoheil Hassas Yeganeh bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst);
39677c3c956SPaolo Abeni void __tcp_close(struct sock *sk, long timeout);
3975c9f3023SJoe Perches void tcp_close(struct sock *sk, long timeout);
3985c9f3023SJoe Perches void tcp_init_sock(struct sock *sk);
39972be0fe6SMartin KaFai Lau void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb);
400a11e1d43SLinus Torvalds __poll_t tcp_poll(struct file *file, struct socket *sock,
401a11e1d43SLinus Torvalds 		      struct poll_table_struct *wait);
402273b7f0fSMartin KaFai Lau int do_tcp_getsockopt(struct sock *sk, int level,
403273b7f0fSMartin KaFai Lau 		      int optname, sockptr_t optval, sockptr_t optlen);
4045c9f3023SJoe Perches int tcp_getsockopt(struct sock *sk, int level, int optname,
4053fdadf7dSDmitry Mishin 		   char __user *optval, int __user *optlen);
4069cacf81fSStanislav Fomichev bool tcp_bpf_bypass_getsockopt(int level, int optname);
4070c751f70SMartin KaFai Lau int do_tcp_setsockopt(struct sock *sk, int level, int optname,
4080c751f70SMartin KaFai Lau 		      sockptr_t optval, unsigned int optlen);
409a7b75c5aSChristoph Hellwig int tcp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
410a7b75c5aSChristoph Hellwig 		   unsigned int optlen);
4115c9f3023SJoe Perches void tcp_set_keepalive(struct sock *sk, int val);
41242cb80a2SEric Dumazet void tcp_syn_ack_timeout(const struct request_sock *req);
413ec095263SOliver Hartkopp int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
4141b784140SYing Xue 		int flags, int *addr_len);
415d1361840SEric Dumazet int tcp_set_rcvlowat(struct sock *sk, int val);
416cb811109SPrankur gupta int tcp_set_window_clamp(struct sock *sk, int val);
417892bfd3dSFlorian Westphal void tcp_update_recv_tstamps(struct sk_buff *skb,
418892bfd3dSFlorian Westphal 			     struct scm_timestamping_internal *tss);
419892bfd3dSFlorian Westphal void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
420892bfd3dSFlorian Westphal 			struct scm_timestamping_internal *tss);
42103f45c88SEric Dumazet void tcp_data_ready(struct sock *sk);
422340a6f3dSYafang Shao #ifdef CONFIG_MMU
42393ab6cc6SEric Dumazet int tcp_mmap(struct file *file, struct socket *sock,
42493ab6cc6SEric Dumazet 	     struct vm_area_struct *vma);
425340a6f3dSYafang Shao #endif
426eed29f17SEric Dumazet void tcp_parse_options(const struct net *net, const struct sk_buff *skb,
4271a2c6181SChristoph Paasch 		       struct tcp_options_received *opt_rx,
4282100c8d2SYuchung Cheng 		       int estab, struct tcp_fastopen_cookie *foc);
4295c9f3023SJoe Perches const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
4307d5d5525SYOSHIFUJI Hideaki 
4311da177e4SLinus Torvalds /*
4329349d600SPetar Penkov  *	BPF SKB-less helpers
4339349d600SPetar Penkov  */
4349349d600SPetar Penkov u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph,
4359349d600SPetar Penkov 			 struct tcphdr *th, u32 *cookie);
4369349d600SPetar Penkov u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph,
4379349d600SPetar Penkov 			 struct tcphdr *th, u32 *cookie);
43833bf9885SMaxim Mikityanskiy u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss);
4399349d600SPetar Penkov u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
4409349d600SPetar Penkov 			  const struct tcp_request_sock_ops *af_ops,
4419349d600SPetar Penkov 			  struct sock *sk, struct tcphdr *th);
4429349d600SPetar Penkov /*
4431da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4441da177e4SLinus Torvalds  */
4451da177e4SLinus Torvalds 
4465c9f3023SJoe Perches void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
4474fab9071SNeal Cardwell void tcp_v4_mtu_reduced(struct sock *sk);
4489cf74903SEric Dumazet void tcp_req_err(struct sock *sk, u32 seq, bool abort);
449d2924569SEric Dumazet void tcp_ld_RTO_revert(struct sock *sk, u32 seq);
4505c9f3023SJoe Perches int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
451c28c6f04SEric Dumazet struct sock *tcp_create_openreq_child(const struct sock *sk,
45260236fddSArnaldo Carvalho de Melo 				      struct request_sock *req,
4531da177e4SLinus Torvalds 				      struct sk_buff *skb);
45481164413SDaniel Borkmann void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst);
4550c27171eSEric Dumazet struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
45660236fddSArnaldo Carvalho de Melo 				  struct request_sock *req,
4575e0724d0SEric Dumazet 				  struct dst_entry *dst,
4585e0724d0SEric Dumazet 				  struct request_sock *req_unhash,
4595e0724d0SEric Dumazet 				  bool *own_req);
4605c9f3023SJoe Perches int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
4615c9f3023SJoe Perches int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
4625c9f3023SJoe Perches int tcp_connect(struct sock *sk);
463b3d05147SEric Dumazet enum tcp_synack_type {
464b3d05147SEric Dumazet 	TCP_SYNACK_NORMAL,
465b3d05147SEric Dumazet 	TCP_SYNACK_FASTOPEN,
466b3d05147SEric Dumazet 	TCP_SYNACK_COOKIE,
467b3d05147SEric Dumazet };
4685d062de7SEric Dumazet struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
469e6b4d113SWilliam Allen Simpson 				struct request_sock *req,
470ca6fb065SEric Dumazet 				struct tcp_fastopen_cookie *foc,
471331fca43SMartin KaFai Lau 				enum tcp_synack_type synack_type,
472331fca43SMartin KaFai Lau 				struct sk_buff *syn_skb);
4735c9f3023SJoe Perches int tcp_disconnect(struct sock *sk, int flags);
4741da177e4SLinus Torvalds 
475370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
476292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
47763d02d15SEric Dumazet void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds /* From syncookies.c */
480b80c0e78SEric Dumazet struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
481b80c0e78SEric Dumazet 				 struct request_sock *req,
48284b114b9SEric Dumazet 				 struct dst_entry *dst, u32 tsoff);
4835c9f3023SJoe Perches int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
4840198230bSPatrick McHardy 		      u32 cookie);
485461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
4866fc8c827SFlorian Westphal struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
487ba5a4fddSEric Dumazet 					    const struct tcp_request_sock_ops *af_ops,
4886fc8c827SFlorian Westphal 					    struct sock *sk, struct sk_buff *skb);
489e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
4908c27bd75SFlorian Westphal 
49163262315SEric Dumazet /* Syncookies use a monotonic timer which increments every 60 seconds.
4928c27bd75SFlorian Westphal  * This counter is used both as a hash input and partially encoded into
4938c27bd75SFlorian Westphal  * the cookie value.  A cookie is only validated further if the delta
4948c27bd75SFlorian Westphal  * between the current counter value and the encoded one is less than this,
49563262315SEric Dumazet  * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
4968c27bd75SFlorian Westphal  * the counter advances immediately after a cookie is generated).
4978c27bd75SFlorian Westphal  */
4988c27bd75SFlorian Westphal #define MAX_SYNCOOKIE_AGE	2
499264ea103SEric Dumazet #define TCP_SYNCOOKIE_PERIOD	(60 * HZ)
500264ea103SEric Dumazet #define TCP_SYNCOOKIE_VALID	(MAX_SYNCOOKIE_AGE * TCP_SYNCOOKIE_PERIOD)
501264ea103SEric Dumazet 
502264ea103SEric Dumazet /* syncookies: remember time of last synqueue overflow
503264ea103SEric Dumazet  * But do not dirty this field too often (once per second is enough)
5043f684b4bSEric Dumazet  * It is racy as we do not hold a lock, but race is very minor.
505264ea103SEric Dumazet  */
5063f684b4bSEric Dumazet static inline void tcp_synq_overflow(const struct sock *sk)
507264ea103SEric Dumazet {
50840a1227eSMartin KaFai Lau 	unsigned int last_overflow;
509cca9bab1SArnd Bergmann 	unsigned int now = jiffies;
510264ea103SEric Dumazet 
51140a1227eSMartin KaFai Lau 	if (sk->sk_reuseport) {
51240a1227eSMartin KaFai Lau 		struct sock_reuseport *reuse;
51340a1227eSMartin KaFai Lau 
51440a1227eSMartin KaFai Lau 		reuse = rcu_dereference(sk->sk_reuseport_cb);
51540a1227eSMartin KaFai Lau 		if (likely(reuse)) {
51640a1227eSMartin KaFai Lau 			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
51704d26e7bSGuillaume Nault 			if (!time_between32(now, last_overflow,
51804d26e7bSGuillaume Nault 					    last_overflow + HZ))
51940a1227eSMartin KaFai Lau 				WRITE_ONCE(reuse->synq_overflow_ts, now);
52040a1227eSMartin KaFai Lau 			return;
52140a1227eSMartin KaFai Lau 		}
52240a1227eSMartin KaFai Lau 	}
52340a1227eSMartin KaFai Lau 
524721c8dafSGuillaume Nault 	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
52504d26e7bSGuillaume Nault 	if (!time_between32(now, last_overflow, last_overflow + HZ))
526e9d9da91SEric Dumazet 		WRITE_ONCE(tcp_sk_rw(sk)->rx_opt.ts_recent_stamp, now);
527264ea103SEric Dumazet }
528264ea103SEric Dumazet 
529264ea103SEric Dumazet /* syncookies: no recent synqueue overflow on this listening socket? */
530264ea103SEric Dumazet static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
531264ea103SEric Dumazet {
53240a1227eSMartin KaFai Lau 	unsigned int last_overflow;
533cca9bab1SArnd Bergmann 	unsigned int now = jiffies;
534264ea103SEric Dumazet 
53540a1227eSMartin KaFai Lau 	if (sk->sk_reuseport) {
53640a1227eSMartin KaFai Lau 		struct sock_reuseport *reuse;
53740a1227eSMartin KaFai Lau 
53840a1227eSMartin KaFai Lau 		reuse = rcu_dereference(sk->sk_reuseport_cb);
53940a1227eSMartin KaFai Lau 		if (likely(reuse)) {
54040a1227eSMartin KaFai Lau 			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
541cb44a08fSGuillaume Nault 			return !time_between32(now, last_overflow - HZ,
542cb44a08fSGuillaume Nault 					       last_overflow +
54340a1227eSMartin KaFai Lau 					       TCP_SYNCOOKIE_VALID);
54440a1227eSMartin KaFai Lau 		}
54540a1227eSMartin KaFai Lau 	}
54640a1227eSMartin KaFai Lau 
547721c8dafSGuillaume Nault 	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
548cb44a08fSGuillaume Nault 
549cb44a08fSGuillaume Nault 	/* If last_overflow <= jiffies <= last_overflow + TCP_SYNCOOKIE_VALID,
550cb44a08fSGuillaume Nault 	 * then we're under synflood. However, we have to use
551cb44a08fSGuillaume Nault 	 * 'last_overflow - HZ' as lower bound. That's because a concurrent
552cb44a08fSGuillaume Nault 	 * tcp_synq_overflow() could update .ts_recent_stamp after we read
553cb44a08fSGuillaume Nault 	 * jiffies but before we store .ts_recent_stamp into last_overflow,
554cb44a08fSGuillaume Nault 	 * which could lead to rejecting a valid syncookie.
555cb44a08fSGuillaume Nault 	 */
556cb44a08fSGuillaume Nault 	return !time_between32(now, last_overflow - HZ,
557cb44a08fSGuillaume Nault 			       last_overflow + TCP_SYNCOOKIE_VALID);
558264ea103SEric Dumazet }
5598c27bd75SFlorian Westphal 
5608c27bd75SFlorian Westphal static inline u32 tcp_cookie_time(void)
5618c27bd75SFlorian Westphal {
56263262315SEric Dumazet 	u64 val = get_jiffies_64();
56363262315SEric Dumazet 
564264ea103SEric Dumazet 	do_div(val, TCP_SYNCOOKIE_PERIOD);
56563262315SEric Dumazet 	return val;
5668c27bd75SFlorian Westphal }
5678c27bd75SFlorian Westphal 
5685c9f3023SJoe Perches u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
5695c9f3023SJoe Perches 			      u16 *mssp);
5703f684b4bSEric Dumazet __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss);
571200ecef6SEric Dumazet u64 cookie_init_timestamp(struct request_sock *req, u64 now);
572f9301034SEric Dumazet bool cookie_timestamp_decode(const struct net *net,
573f9301034SEric Dumazet 			     struct tcp_options_received *opt);
574f1673381SFlorian Westphal bool cookie_ecn_ok(const struct tcp_options_received *opt,
575f7b3bec6SFlorian Westphal 		   const struct net *net, const struct dst_entry *dst);
5764dfc2817SFlorian Westphal 
577c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */
5785c9f3023SJoe Perches int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
57981eb6a14SPatrick McHardy 		      u32 cookie);
5805c9f3023SJoe Perches struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
581f1673381SFlorian Westphal 
5825c9f3023SJoe Perches u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
58381eb6a14SPatrick McHardy 			      const struct tcphdr *th, u16 *mssp);
5843f684b4bSEric Dumazet __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mss);
585e05c82d3SEric Dumazet #endif
5861da177e4SLinus Torvalds /* tcp_output.c */
5871da177e4SLinus Torvalds 
58804d8825cSPaolo Abeni void tcp_skb_entail(struct sock *sk, struct sk_buff *skb);
58904d8825cSPaolo Abeni void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb);
5905c9f3023SJoe Perches void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
5919e412ba7SIlpo Järvinen 			       int nonagle);
59210d3be56SEric Dumazet int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
59310d3be56SEric Dumazet int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
5945c9f3023SJoe Perches void tcp_retransmit_timer(struct sock *sk);
5955c9f3023SJoe Perches void tcp_xmit_retransmit_queue(struct sock *);
5965c9f3023SJoe Perches void tcp_simple_retransmit(struct sock *);
59757dde7f7SYuchung Cheng void tcp_enter_recovery(struct sock *sk, bool ece_ack);
5985c9f3023SJoe Perches int tcp_trim_head(struct sock *, struct sk_buff *, u32);
59975c119afSEric Dumazet enum tcp_queue {
60075c119afSEric Dumazet 	TCP_FRAG_IN_WRITE_QUEUE,
60175c119afSEric Dumazet 	TCP_FRAG_IN_RTX_QUEUE,
60275c119afSEric Dumazet };
60375c119afSEric Dumazet int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
60475c119afSEric Dumazet 		 struct sk_buff *skb, u32 len,
60575c119afSEric Dumazet 		 unsigned int mss_now, gfp_t gfp);
6061da177e4SLinus Torvalds 
6075c9f3023SJoe Perches void tcp_send_probe0(struct sock *);
608e520af48SEric Dumazet int tcp_write_wakeup(struct sock *, int mib);
6095c9f3023SJoe Perches void tcp_send_fin(struct sock *sk);
6105c9f3023SJoe Perches void tcp_send_active_reset(struct sock *sk, gfp_t priority);
6115c9f3023SJoe Perches int tcp_send_synack(struct sock *);
6125c9f3023SJoe Perches void tcp_push_one(struct sock *, unsigned int mss_now);
61327cde44aSYuchung Cheng void __tcp_send_ack(struct sock *sk, u32 rcv_nxt);
6145c9f3023SJoe Perches void tcp_send_ack(struct sock *sk);
6155c9f3023SJoe Perches void tcp_send_delayed_ack(struct sock *sk);
6165c9f3023SJoe Perches void tcp_send_loss_probe(struct sock *sk);
617ed66dfafSNeal Cardwell bool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto);
618cfea5a68SMartin KaFai Lau void tcp_skb_collapse_tstamp(struct sk_buff *skb,
619cfea5a68SMartin KaFai Lau 			     const struct sk_buff *next_skb);
6201da177e4SLinus Torvalds 
621a762a980SDavid S. Miller /* tcp_input.c */
6225c9f3023SJoe Perches void tcp_rearm_rto(struct sock *sk);
6230f1c28aeSYuchung Cheng void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req);
624049fe386SFlorian Westphal void tcp_reset(struct sock *sk, struct sk_buff *skb);
625e3e17b77SEric Dumazet void tcp_fin(struct sock *sk);
6264bfe744fSEric Dumazet void tcp_check_space(struct sock *sk);
62730c6f0bfSfuyuanli void tcp_sack_compress_send_ack(struct sock *sk);
628a762a980SDavid S. Miller 
6291da177e4SLinus Torvalds /* tcp_timer.c */
6305c9f3023SJoe Perches void tcp_init_xmit_timers(struct sock *);
631463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
632463c84b9SArnaldo Carvalho de Melo {
63373a6bab5SEric Dumazet 	if (hrtimer_try_to_cancel(&tcp_sk(sk)->pacing_timer) == 1)
634cf0dd203SEric Dumazet 		__sock_put(sk);
63573a6bab5SEric Dumazet 
6365d9f4262SEric Dumazet 	if (hrtimer_try_to_cancel(&tcp_sk(sk)->compressed_ack_timer) == 1)
6375d9f4262SEric Dumazet 		__sock_put(sk);
6385d9f4262SEric Dumazet 
639463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
640463c84b9SArnaldo Carvalho de Melo }
6411da177e4SLinus Torvalds 
6425c9f3023SJoe Perches unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
6435c9f3023SJoe Perches unsigned int tcp_current_mss(struct sock *sk);
644344db93aSEnke Chen u32 tcp_clamp_probe0_to_user_timeout(const struct sock *sk, u32 when);
6450c54b85fSIlpo Järvinen 
6460c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */
6470c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
6480c54b85fSIlpo Järvinen {
64901f83d69SAlexey Kuznetsov 	int cutoff;
65001f83d69SAlexey Kuznetsov 
65101f83d69SAlexey Kuznetsov 	/* When peer uses tiny windows, there is no use in packetizing
65201f83d69SAlexey Kuznetsov 	 * to sub-MSS pieces for the sake of SWS or making sure there
65301f83d69SAlexey Kuznetsov 	 * are enough packets in the pipe for fast recovery.
65401f83d69SAlexey Kuznetsov 	 *
65501f83d69SAlexey Kuznetsov 	 * On the other hand, for extremely large MSS devices, handling
65601f83d69SAlexey Kuznetsov 	 * smaller than MSS windows in this way does make sense.
65701f83d69SAlexey Kuznetsov 	 */
6582631b79fSSeymour, Shane M 	if (tp->max_window > TCP_MSS_DEFAULT)
65901f83d69SAlexey Kuznetsov 		cutoff = (tp->max_window >> 1);
66001f83d69SAlexey Kuznetsov 	else
66101f83d69SAlexey Kuznetsov 		cutoff = tp->max_window;
66201f83d69SAlexey Kuznetsov 
66301f83d69SAlexey Kuznetsov 	if (cutoff && pktsize > cutoff)
66401f83d69SAlexey Kuznetsov 		return max_t(int, cutoff, 68U - tp->tcp_header_len);
6650c54b85fSIlpo Järvinen 	else
6660c54b85fSIlpo Järvinen 		return pktsize;
6670c54b85fSIlpo Järvinen }
6681da177e4SLinus Torvalds 
66917b085eaSArnaldo Carvalho de Melo /* tcp.c */
6700df48c26SEric Dumazet void tcp_get_info(struct sock *, struct tcp_info *);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
6735c9f3023SJoe Perches int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
6741da177e4SLinus Torvalds 		  sk_read_actor_t recv_actor);
675965b57b4SCong Wang int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor);
6763f92a64eSJakub Kicinski struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off);
6773f92a64eSJakub Kicinski void tcp_read_done(struct sock *sk, size_t len);
6781da177e4SLinus Torvalds 
6795c9f3023SJoe Perches void tcp_initialize_rcv_mss(struct sock *sk);
6801da177e4SLinus Torvalds 
6815c9f3023SJoe Perches int tcp_mtu_to_mss(struct sock *sk, int pmtu);
6825c9f3023SJoe Perches int tcp_mss_to_mtu(struct sock *sk, int mss);
6835c9f3023SJoe Perches void tcp_mtup_init(struct sock *sk);
6845d424d5aSJohn Heffner 
685f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk)
686f1ecd5d9SDamian Lukowski {
687f1ecd5d9SDamian Lukowski 	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
688f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
689f1ecd5d9SDamian Lukowski }
690f1ecd5d9SDamian Lukowski 
691f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
692f1ecd5d9SDamian Lukowski {
693740b0f18SEric Dumazet 	return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
694f1ecd5d9SDamian Lukowski }
695f1ecd5d9SDamian Lukowski 
69631770e34SFlorian Westphal static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
69731770e34SFlorian Westphal {
69871158bb1SPaolo Abeni 	/* mptcp hooks are only on the slow path */
69971158bb1SPaolo Abeni 	if (sk_is_mptcp((struct sock *)tp))
70071158bb1SPaolo Abeni 		return;
70171158bb1SPaolo Abeni 
70231770e34SFlorian Westphal 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
70331770e34SFlorian Westphal 			       ntohl(TCP_FLAG_ACK) |
70431770e34SFlorian Westphal 			       snd_wnd);
70531770e34SFlorian Westphal }
70631770e34SFlorian Westphal 
70731770e34SFlorian Westphal static inline void tcp_fast_path_on(struct tcp_sock *tp)
70831770e34SFlorian Westphal {
70931770e34SFlorian Westphal 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
71031770e34SFlorian Westphal }
71131770e34SFlorian Westphal 
71231770e34SFlorian Westphal static inline void tcp_fast_path_check(struct sock *sk)
71331770e34SFlorian Westphal {
71431770e34SFlorian Westphal 	struct tcp_sock *tp = tcp_sk(sk);
71531770e34SFlorian Westphal 
71631770e34SFlorian Westphal 	if (RB_EMPTY_ROOT(&tp->out_of_order_queue) &&
71731770e34SFlorian Westphal 	    tp->rcv_wnd &&
71831770e34SFlorian Westphal 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
71931770e34SFlorian Westphal 	    !tp->urg_data)
72031770e34SFlorian Westphal 		tcp_fast_path_on(tp);
72131770e34SFlorian Westphal }
72231770e34SFlorian Westphal 
7230c266898SSatoru SATOH /* Compute the actual rto_min value */
7240c266898SSatoru SATOH static inline u32 tcp_rto_min(struct sock *sk)
7250c266898SSatoru SATOH {
726cf533ea5SEric Dumazet 	const struct dst_entry *dst = __sk_dst_get(sk);
727ca584ba0SMartin KaFai Lau 	u32 rto_min = inet_csk(sk)->icsk_rto_min;
7280c266898SSatoru SATOH 
7290c266898SSatoru SATOH 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
7300c266898SSatoru SATOH 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
7310c266898SSatoru SATOH 	return rto_min;
7320c266898SSatoru SATOH }
7330c266898SSatoru SATOH 
734740b0f18SEric Dumazet static inline u32 tcp_rto_min_us(struct sock *sk)
735740b0f18SEric Dumazet {
736740b0f18SEric Dumazet 	return jiffies_to_usecs(tcp_rto_min(sk));
737740b0f18SEric Dumazet }
738740b0f18SEric Dumazet 
73981164413SDaniel Borkmann static inline bool tcp_ca_dst_locked(const struct dst_entry *dst)
74081164413SDaniel Borkmann {
74181164413SDaniel Borkmann 	return dst_metric_locked(dst, RTAX_CC_ALGO);
74281164413SDaniel Borkmann }
74381164413SDaniel Borkmann 
744f6722583SYuchung Cheng /* Minimum RTT in usec. ~0 means not available. */
745f6722583SYuchung Cheng static inline u32 tcp_min_rtt(const struct tcp_sock *tp)
746f6722583SYuchung Cheng {
74764033892SNeal Cardwell 	return minmax_get(&tp->rtt_min);
748f6722583SYuchung Cheng }
749f6722583SYuchung Cheng 
7501da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
7511da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
7521da177e4SLinus Torvalds  * than the offered window.
7531da177e4SLinus Torvalds  */
75440efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
7551da177e4SLinus Torvalds {
7561da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	if (win < 0)
7591da177e4SLinus Torvalds 		win = 0;
7601da177e4SLinus Torvalds 	return (u32) win;
7611da177e4SLinus Torvalds }
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
7641da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
7651da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
7661da177e4SLinus Torvalds  */
7675c9f3023SJoe Perches u32 __tcp_select_window(struct sock *sk);
7681da177e4SLinus Torvalds 
769ee995283SPavel Emelyanov void tcp_send_window_probe(struct sock *sk);
770ee995283SPavel Emelyanov 
771ec66eda8SEric Dumazet /* TCP uses 32bit jiffies to save some space.
772ec66eda8SEric Dumazet  * Note that this is different from tcp_time_stamp, which
773ec66eda8SEric Dumazet  * historically has been the same until linux-4.13.
774ec66eda8SEric Dumazet  */
775ec66eda8SEric Dumazet #define tcp_jiffies32 ((u32)jiffies)
776ec66eda8SEric Dumazet 
7779a568de4SEric Dumazet /*
7789a568de4SEric Dumazet  * Deliver a 32bit value for TCP timestamp option (RFC 7323)
7799a568de4SEric Dumazet  * It is no longer tied to jiffies, but to 1 ms clock.
7809a568de4SEric Dumazet  * Note: double check if you want to use tcp_jiffies32 instead of this.
7811da177e4SLinus Torvalds  */
7829a568de4SEric Dumazet #define TCP_TS_HZ	1000
7839a568de4SEric Dumazet 
7849a568de4SEric Dumazet static inline u64 tcp_clock_ns(void)
7859a568de4SEric Dumazet {
786fb420d5dSEric Dumazet 	return ktime_get_ns();
7879a568de4SEric Dumazet }
7889a568de4SEric Dumazet 
7899a568de4SEric Dumazet static inline u64 tcp_clock_us(void)
7909a568de4SEric Dumazet {
7919a568de4SEric Dumazet 	return div_u64(tcp_clock_ns(), NSEC_PER_USEC);
7929a568de4SEric Dumazet }
7939a568de4SEric Dumazet 
7949a568de4SEric Dumazet /* This should only be used in contexts where tp->tcp_mstamp is up to date */
7959a568de4SEric Dumazet static inline u32 tcp_time_stamp(const struct tcp_sock *tp)
7969a568de4SEric Dumazet {
7979a568de4SEric Dumazet 	return div_u64(tp->tcp_mstamp, USEC_PER_SEC / TCP_TS_HZ);
7989a568de4SEric Dumazet }
7999a568de4SEric Dumazet 
800200ecef6SEric Dumazet /* Convert a nsec timestamp into TCP TSval timestamp (ms based currently) */
801200ecef6SEric Dumazet static inline u32 tcp_ns_to_ts(u64 ns)
802200ecef6SEric Dumazet {
803200ecef6SEric Dumazet 	return div_u64(ns, NSEC_PER_SEC / TCP_TS_HZ);
804200ecef6SEric Dumazet }
805200ecef6SEric Dumazet 
8069a568de4SEric Dumazet /* Could use tcp_clock_us() / 1000, but this version uses a single divide */
8079a568de4SEric Dumazet static inline u32 tcp_time_stamp_raw(void)
8089a568de4SEric Dumazet {
809200ecef6SEric Dumazet 	return tcp_ns_to_ts(tcp_clock_ns());
8109a568de4SEric Dumazet }
8119a568de4SEric Dumazet 
8129799ccb0SEric Dumazet void tcp_mstamp_refresh(struct tcp_sock *tp);
8139a568de4SEric Dumazet 
8149a568de4SEric Dumazet static inline u32 tcp_stamp_us_delta(u64 t1, u64 t0)
8159a568de4SEric Dumazet {
8169a568de4SEric Dumazet 	return max_t(s64, t1 - t0, 0);
8179a568de4SEric Dumazet }
8181da177e4SLinus Torvalds 
8197faee5c0SEric Dumazet static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
8207faee5c0SEric Dumazet {
821200ecef6SEric Dumazet 	return tcp_ns_to_ts(skb->skb_mstamp_ns);
8227faee5c0SEric Dumazet }
8237faee5c0SEric Dumazet 
8242fd66ffbSEric Dumazet /* provide the departure time in us unit */
8252fd66ffbSEric Dumazet static inline u64 tcp_skb_timestamp_us(const struct sk_buff *skb)
8262fd66ffbSEric Dumazet {
827d3edd06eSEric Dumazet 	return div_u64(skb->skb_mstamp_ns, NSEC_PER_USEC);
8282fd66ffbSEric Dumazet }
8292fd66ffbSEric Dumazet 
8307faee5c0SEric Dumazet 
831a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
832a3433f35SChangli Gao 
833a3433f35SChangli Gao #define TCPHDR_FIN 0x01
834a3433f35SChangli Gao #define TCPHDR_SYN 0x02
835a3433f35SChangli Gao #define TCPHDR_RST 0x04
836a3433f35SChangli Gao #define TCPHDR_PSH 0x08
837a3433f35SChangli Gao #define TCPHDR_ACK 0x10
838a3433f35SChangli Gao #define TCPHDR_URG 0x20
839a3433f35SChangli Gao #define TCPHDR_ECE 0x40
840a3433f35SChangli Gao #define TCPHDR_CWR 0x80
841a3433f35SChangli Gao 
84249213555SDaniel Borkmann #define TCPHDR_SYN_ECN	(TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)
84349213555SDaniel Borkmann 
844caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
845f86586faSEric Dumazet  * TCP per-packet control information to the transmission code.
846f86586faSEric Dumazet  * We also store the host-order sequence numbers in here too.
847f86586faSEric Dumazet  * This is 44 bytes if IPV6 is enabled.
848f86586faSEric Dumazet  * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
8491da177e4SLinus Torvalds  */
8501da177e4SLinus Torvalds struct tcp_skb_cb {
8511da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
8521da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
853cd7d8498SEric Dumazet 	union {
854cd7d8498SEric Dumazet 		/* Note : tcp_tw_isn is used in input path only
855cd7d8498SEric Dumazet 		 *	  (isn chosen by tcp_timewait_state_process())
856cd7d8498SEric Dumazet 		 *
857f69ad292SEric Dumazet 		 * 	  tcp_gso_segs/size are used in write queue only,
858f69ad292SEric Dumazet 		 *	  cf tcp_skb_pcount()/tcp_skb_mss()
859cd7d8498SEric Dumazet 		 */
860cd7d8498SEric Dumazet 		__u32		tcp_tw_isn;
861f69ad292SEric Dumazet 		struct {
862f69ad292SEric Dumazet 			u16	tcp_gso_segs;
863f69ad292SEric Dumazet 			u16	tcp_gso_size;
864f69ad292SEric Dumazet 		};
865cd7d8498SEric Dumazet 	};
8664de075e0SEric Dumazet 	__u8		tcp_flags;	/* TCP header flags. (tcp[13])	*/
867f4f9f6e7SNeal Cardwell 
868713bafeaSYuchung Cheng 	__u8		sacked;		/* State flags for SACK.	*/
8691da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
8701da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
8711da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
8721da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
873d3edd06eSEric Dumazet #define TCPCB_REPAIRED		0x10	/* SKB repaired (no skb_mstamp_ns)	*/
8741da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
8759d186cacSAndrey Vagin #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
8769d186cacSAndrey Vagin 				TCPCB_REPAIRED)
8771da177e4SLinus Torvalds 
878f4f9f6e7SNeal Cardwell 	__u8		ip_dsfield;	/* IPv4 tos or IPv6 dsfield	*/
8796b084928SSoheil Hassas Yeganeh 	__u8		txstamp_ack:1,	/* Record TX timestamp for ack? */
880c134ecb8SMartin KaFai Lau 			eor:1,		/* Is skb MSG_EOR marked? */
88198aaa913SMike Maloney 			has_rxtstamp:1,	/* SKB has a RX timestamp	*/
88298aaa913SMike Maloney 			unused:5;
8831da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
884971f10ecSEric Dumazet 	union {
885b75803d5SLawrence Brakmo 		struct {
88640bc6063SYuchung Cheng #define TCPCB_DELIVERED_CE_MASK ((1U<<20) - 1)
887b9f64820SYuchung Cheng 			/* There is space for up to 24 bytes */
88840bc6063SYuchung Cheng 			__u32 is_app_limited:1, /* cwnd not fully used? */
88940bc6063SYuchung Cheng 			      delivered_ce:20,
89040bc6063SYuchung Cheng 			      unused:11;
891b9f64820SYuchung Cheng 			/* pkts S/ACKed so far upon tx of skb, incl retrans: */
892b9f64820SYuchung Cheng 			__u32 delivered;
893b9f64820SYuchung Cheng 			/* start of send pipeline phase */
8949a568de4SEric Dumazet 			u64 first_tx_mstamp;
895b9f64820SYuchung Cheng 			/* when we reached the "delivered" count */
8969a568de4SEric Dumazet 			u64 delivered_mstamp;
897b75803d5SLawrence Brakmo 		} tx;   /* only used for outgoing skbs */
898b75803d5SLawrence Brakmo 		union {
899971f10ecSEric Dumazet 			struct inet_skb_parm	h4;
900971f10ecSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
901971f10ecSEric Dumazet 			struct inet6_skb_parm	h6;
902971f10ecSEric Dumazet #endif
903b75803d5SLawrence Brakmo 		} header;	/* For incoming skbs */
904b75803d5SLawrence Brakmo 	};
9051da177e4SLinus Torvalds };
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
9081da177e4SLinus Torvalds 
9099b9e2f25SEric Dumazet extern const struct inet_connection_sock_af_ops ipv4_specific;
9109b9e2f25SEric Dumazet 
911815afe17SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
912870c3151SEric Dumazet /* This is the variant of inet6_iif() that must be used by TCP,
913870c3151SEric Dumazet  * as TCP moves IP6CB into a different location in skb->cb[]
914870c3151SEric Dumazet  */
915870c3151SEric Dumazet static inline int tcp_v6_iif(const struct sk_buff *skb)
916870c3151SEric Dumazet {
91724b711edSDavid Ahern 	return TCP_SKB_CB(skb)->header.h6.iif;
91824b711edSDavid Ahern }
91924b711edSDavid Ahern 
92024b711edSDavid Ahern static inline int tcp_v6_iif_l3_slave(const struct sk_buff *skb)
92124b711edSDavid Ahern {
922a04a480dSDavid Ahern 	bool l3_slave = ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags);
92374b20582SDavid Ahern 
92474b20582SDavid Ahern 	return l3_slave ? skb->skb_iif : TCP_SKB_CB(skb)->header.h6.iif;
925870c3151SEric Dumazet }
9264297a0efSDavid Ahern 
9274297a0efSDavid Ahern /* TCP_SKB_CB reference means this can not be used from early demux */
9284297a0efSDavid Ahern static inline int tcp_v6_sdif(const struct sk_buff *skb)
9294297a0efSDavid Ahern {
9304297a0efSDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9314297a0efSDavid Ahern 	if (skb && ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags))
9324297a0efSDavid Ahern 		return TCP_SKB_CB(skb)->header.h6.iif;
9334297a0efSDavid Ahern #endif
9344297a0efSDavid Ahern 	return 0;
9354297a0efSDavid Ahern }
936dd2e0b86SEric Dumazet 
937b03d2142SEric Dumazet extern const struct inet_connection_sock_af_ops ipv6_specific;
938b03d2142SEric Dumazet 
939dd2e0b86SEric Dumazet INDIRECT_CALLABLE_DECLARE(void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb));
940243600eeSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *skb));
94111052589SKuniyuki Iwashima void tcp_v6_early_demux(struct sk_buff *skb);
942dd2e0b86SEric Dumazet 
943815afe17SEric Dumazet #endif
944870c3151SEric Dumazet 
9453fa6f616SDavid Ahern /* TCP_SKB_CB reference means this can not be used from early demux */
9463fa6f616SDavid Ahern static inline int tcp_v4_sdif(struct sk_buff *skb)
9473fa6f616SDavid Ahern {
9483fa6f616SDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9493fa6f616SDavid Ahern 	if (skb && ipv4_l3mdev_skb(TCP_SKB_CB(skb)->header.h4.flags))
9503fa6f616SDavid Ahern 		return TCP_SKB_CB(skb)->header.h4.iif;
9513fa6f616SDavid Ahern #endif
9523fa6f616SDavid Ahern 	return 0;
9533fa6f616SDavid Ahern }
9543fa6f616SDavid Ahern 
9551da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
9561da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
9571da177e4SLinus Torvalds  */
9581da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
9591da177e4SLinus Torvalds {
960cd7d8498SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_segs;
961cd7d8498SEric Dumazet }
962cd7d8498SEric Dumazet 
963cd7d8498SEric Dumazet static inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs)
964cd7d8498SEric Dumazet {
965cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs = segs;
966cd7d8498SEric Dumazet }
967cd7d8498SEric Dumazet 
968cd7d8498SEric Dumazet static inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs)
969cd7d8498SEric Dumazet {
970cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs += segs;
9711da177e4SLinus Torvalds }
9721da177e4SLinus Torvalds 
973f69ad292SEric Dumazet /* This is valid iff skb is in write queue and tcp_skb_pcount() > 1. */
9741da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
9751da177e4SLinus Torvalds {
976f69ad292SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_size;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
979c134ecb8SMartin KaFai Lau static inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb)
980c134ecb8SMartin KaFai Lau {
981c134ecb8SMartin KaFai Lau 	return likely(!TCP_SKB_CB(skb)->eor);
982c134ecb8SMartin KaFai Lau }
983c134ecb8SMartin KaFai Lau 
98485712484SMat Martineau static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
98585712484SMat Martineau 					const struct sk_buff *from)
98685712484SMat Martineau {
98785712484SMat Martineau 	return likely(tcp_skb_can_collapse_to(to) &&
9889b65b17dSTalal Ahmad 		      mptcp_skb_can_collapse(to, from) &&
9899b65b17dSTalal Ahmad 		      skb_pure_zcopy_same(to, from));
99085712484SMat Martineau }
99185712484SMat Martineau 
992317a76f9SStephen Hemminger /* Events passed to congestion control interface */
993317a76f9SStephen Hemminger enum tcp_ca_event {
994317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
995317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
996317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
997317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
9989890092eSFlorian Westphal 	CA_EVENT_ECN_NO_CE,	/* ECT set, but not CE marked */
9999890092eSFlorian Westphal 	CA_EVENT_ECN_IS_CE,	/* received CE marked IP packet */
10007354c8c3SFlorian Westphal };
10017354c8c3SFlorian Westphal 
10029890092eSFlorian Westphal /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
10037354c8c3SFlorian Westphal enum tcp_ca_ack_event_flags {
1004c1d2b4c3SFlorian Westphal 	CA_ACK_SLOWPATH		= (1 << 0),	/* In slow path processing */
1005c1d2b4c3SFlorian Westphal 	CA_ACK_WIN_UPDATE	= (1 << 1),	/* ACK updated window */
1006c1d2b4c3SFlorian Westphal 	CA_ACK_ECE		= (1 << 2),	/* ECE bit is set on ack */
1007317a76f9SStephen Hemminger };
1008317a76f9SStephen Hemminger 
1009317a76f9SStephen Hemminger /*
1010317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
1011317a76f9SStephen Hemminger  */
1012317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
10133ff825b2SStephen Hemminger #define TCP_CA_MAX	128
10143ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
10153ff825b2SStephen Hemminger 
1016c5c6a8abSDaniel Borkmann #define TCP_CA_UNSPEC	0
1017c5c6a8abSDaniel Borkmann 
101830e502a3SDaniel Borkmann /* Algorithm can be set on socket without CAP_NET_ADMIN privileges */
1019164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1
102030e502a3SDaniel Borkmann /* Requires ECN/ECT set on all packets */
102130e502a3SDaniel Borkmann #define TCP_CONG_NEEDS_ECN	0x2
10220baf26b0SMartin KaFai Lau #define TCP_CONG_MASK	(TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN)
1023164891aaSStephen Hemminger 
102464f40ff5SEric Dumazet union tcp_cc_info;
102564f40ff5SEric Dumazet 
1026756ee172SLawrence Brakmo struct ack_sample {
1027756ee172SLawrence Brakmo 	u32 pkts_acked;
1028756ee172SLawrence Brakmo 	s32 rtt_us;
10296f094b9eSLawrence Brakmo 	u32 in_flight;
1030756ee172SLawrence Brakmo };
1031756ee172SLawrence Brakmo 
1032b9f64820SYuchung Cheng /* A rate sample measures the number of (original/retransmitted) data
1033b9f64820SYuchung Cheng  * packets delivered "delivered" over an interval of time "interval_us".
1034b9f64820SYuchung Cheng  * The tcp_rate.c code fills in the rate sample, and congestion
1035b9f64820SYuchung Cheng  * control modules that define a cong_control function to run at the end
1036b9f64820SYuchung Cheng  * of ACK processing can optionally chose to consult this sample when
1037b9f64820SYuchung Cheng  * setting cwnd and pacing rate.
1038b9f64820SYuchung Cheng  * A sample is invalid if "delivered" or "interval_us" is negative.
1039b9f64820SYuchung Cheng  */
1040b9f64820SYuchung Cheng struct rate_sample {
10419a568de4SEric Dumazet 	u64  prior_mstamp; /* starting timestamp for interval */
1042b9f64820SYuchung Cheng 	u32  prior_delivered;	/* tp->delivered at "prior_mstamp" */
104340bc6063SYuchung Cheng 	u32  prior_delivered_ce;/* tp->delivered_ce at "prior_mstamp" */
1044b9f64820SYuchung Cheng 	s32  delivered;		/* number of packets delivered over interval */
104540bc6063SYuchung Cheng 	s32  delivered_ce;	/* number of packets delivered w/ CE marks*/
1046b9f64820SYuchung Cheng 	long interval_us;	/* time for tp->delivered to incr "delivered" */
10474929c942SDeepti Raghavan 	u32 snd_interval_us;	/* snd interval for delivered packets */
10484929c942SDeepti Raghavan 	u32 rcv_interval_us;	/* rcv interval for delivered packets */
1049b9f64820SYuchung Cheng 	long rtt_us;		/* RTT of last (S)ACKed packet (or -1) */
1050b9f64820SYuchung Cheng 	int  losses;		/* number of packets marked lost upon ACK */
1051b9f64820SYuchung Cheng 	u32  acked_sacked;	/* number of packets newly (S)ACKed upon ACK */
1052b9f64820SYuchung Cheng 	u32  prior_in_flight;	/* in flight before this ACK */
1053b253a068SPengcheng Yang 	u32  last_end_seq;	/* end_seq of most recently ACKed packet */
1054d7722e85SSoheil Hassas Yeganeh 	bool is_app_limited;	/* is sample from packet with bubble in pipe? */
1055b9f64820SYuchung Cheng 	bool is_retrans;	/* is sample from retransmission? */
1056e4286603SYuchung Cheng 	bool is_ack_delayed;	/* is this (likely) a delayed ACK? */
1057b9f64820SYuchung Cheng };
1058b9f64820SYuchung Cheng 
1059317a76f9SStephen Hemminger struct tcp_congestion_ops {
106082506665SEric Dumazet /* fast path fields are put first to fill one cache line */
106182506665SEric Dumazet 
106282506665SEric Dumazet 	/* return slow start threshold (required) */
106382506665SEric Dumazet 	u32 (*ssthresh)(struct sock *sk);
106482506665SEric Dumazet 
106582506665SEric Dumazet 	/* do new cwnd calculation (required) */
106682506665SEric Dumazet 	void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked);
106782506665SEric Dumazet 
106882506665SEric Dumazet 	/* call before changing ca_state (optional) */
106982506665SEric Dumazet 	void (*set_state)(struct sock *sk, u8 new_state);
107082506665SEric Dumazet 
107182506665SEric Dumazet 	/* call when cwnd event occurs (optional) */
107282506665SEric Dumazet 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
107382506665SEric Dumazet 
107482506665SEric Dumazet 	/* call when ack arrives (optional) */
107582506665SEric Dumazet 	void (*in_ack_event)(struct sock *sk, u32 flags);
107682506665SEric Dumazet 
107782506665SEric Dumazet 	/* hook for packet ack accounting (optional) */
107882506665SEric Dumazet 	void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample);
107982506665SEric Dumazet 
108082506665SEric Dumazet 	/* override sysctl_tcp_min_tso_segs */
108182506665SEric Dumazet 	u32 (*min_tso_segs)(struct sock *sk);
108282506665SEric Dumazet 
108382506665SEric Dumazet 	/* call when packets are delivered to update cwnd and pacing rate,
108482506665SEric Dumazet 	 * after all the ca_state processing. (optional)
108582506665SEric Dumazet 	 */
108682506665SEric Dumazet 	void (*cong_control)(struct sock *sk, const struct rate_sample *rs);
108782506665SEric Dumazet 
108882506665SEric Dumazet 
108982506665SEric Dumazet 	/* new value of cwnd after loss (required) */
109082506665SEric Dumazet 	u32  (*undo_cwnd)(struct sock *sk);
109182506665SEric Dumazet 	/* returns the multiplier used in tcp_sndbuf_expand (optional) */
109282506665SEric Dumazet 	u32 (*sndbuf_expand)(struct sock *sk);
109382506665SEric Dumazet 
109482506665SEric Dumazet /* control/slow paths put last */
109582506665SEric Dumazet 	/* get info for inet_diag (optional) */
109682506665SEric Dumazet 	size_t (*get_info)(struct sock *sk, u32 ext, int *attr,
109782506665SEric Dumazet 			   union tcp_cc_info *info);
109882506665SEric Dumazet 
109982506665SEric Dumazet 	char 			name[TCP_CA_NAME_MAX];
110082506665SEric Dumazet 	struct module		*owner;
1101317a76f9SStephen Hemminger 	struct list_head	list;
1102c5c6a8abSDaniel Borkmann 	u32			key;
1103c5c6a8abSDaniel Borkmann 	u32			flags;
1104317a76f9SStephen Hemminger 
1105317a76f9SStephen Hemminger 	/* initialize private data (optional) */
11066687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
1107317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
11086687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
110982506665SEric Dumazet } ____cacheline_aligned_in_smp;
1110317a76f9SStephen Hemminger 
11115c9f3023SJoe Perches int tcp_register_congestion_control(struct tcp_congestion_ops *type);
11125c9f3023SJoe Perches void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
11138fb1a76aSKui-Feng Lee int tcp_update_congestion_control(struct tcp_congestion_ops *type,
11148fb1a76aSKui-Feng Lee 				  struct tcp_congestion_ops *old_type);
11158fb1a76aSKui-Feng Lee int tcp_validate_congestion_control(struct tcp_congestion_ops *ca);
1116317a76f9SStephen Hemminger 
111755d8694fSFlorian Westphal void tcp_assign_congestion_control(struct sock *sk);
11185c9f3023SJoe Perches void tcp_init_congestion_control(struct sock *sk);
11195c9f3023SJoe Perches void tcp_cleanup_congestion_control(struct sock *sk);
11206670e152SStephen Hemminger int tcp_set_default_congestion_control(struct net *net, const char *name);
11216670e152SStephen Hemminger void tcp_get_default_congestion_control(struct net *net, char *name);
11225c9f3023SJoe Perches void tcp_get_available_congestion_control(char *buf, size_t len);
11235c9f3023SJoe Perches void tcp_get_allowed_congestion_control(char *buf, size_t len);
11245c9f3023SJoe Perches int tcp_set_allowed_congestion_control(char *allowed);
11258d650cdeSEric Dumazet int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
112629a94932SNeal Cardwell 			       bool cap_net_admin);
1127e73ebb08SNeal Cardwell u32 tcp_slow_start(struct tcp_sock *tp, u32 acked);
1128e73ebb08SNeal Cardwell void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked);
1129317a76f9SStephen Hemminger 
11305c9f3023SJoe Perches u32 tcp_reno_ssthresh(struct sock *sk);
1131e9799183SFlorian Westphal u32 tcp_reno_undo_cwnd(struct sock *sk);
113224901551SEric Dumazet void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked);
1133a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
1134317a76f9SStephen Hemminger 
11350baf26b0SMartin KaFai Lau struct tcp_congestion_ops *tcp_ca_find(const char *name);
1136c5c6a8abSDaniel Borkmann struct tcp_congestion_ops *tcp_ca_find_key(u32 key);
11376670e152SStephen Hemminger u32 tcp_ca_get_key_by_name(struct net *net, const char *name, bool *ecn_ca);
1138ea697639SDaniel Borkmann #ifdef CONFIG_INET
1139c5c6a8abSDaniel Borkmann char *tcp_ca_get_name_by_key(u32 key, char *buffer);
1140ea697639SDaniel Borkmann #else
1141ea697639SDaniel Borkmann static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
1142ea697639SDaniel Borkmann {
1143ea697639SDaniel Borkmann 	return NULL;
1144ea697639SDaniel Borkmann }
1145ea697639SDaniel Borkmann #endif
1146c5c6a8abSDaniel Borkmann 
114730e502a3SDaniel Borkmann static inline bool tcp_ca_needs_ecn(const struct sock *sk)
114830e502a3SDaniel Borkmann {
114930e502a3SDaniel Borkmann 	const struct inet_connection_sock *icsk = inet_csk(sk);
115030e502a3SDaniel Borkmann 
115130e502a3SDaniel Borkmann 	return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN;
115230e502a3SDaniel Borkmann }
115330e502a3SDaniel Borkmann 
11546687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
1155317a76f9SStephen Hemminger {
11566687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
11576687e988SArnaldo Carvalho de Melo 
11586687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
11596687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
1160317a76f9SStephen Hemminger }
1161317a76f9SStephen Hemminger 
116215fcdf6aSPing Gan /* From tcp_cong.c */
116315fcdf6aSPing Gan void tcp_set_ca_state(struct sock *sk, const u8 ca_state);
116415fcdf6aSPing Gan 
1165b9f64820SYuchung Cheng /* From tcp_rate.c */
1166b9f64820SYuchung Cheng void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb);
1167b9f64820SYuchung Cheng void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
1168b9f64820SYuchung Cheng 			    struct rate_sample *rs);
1169b9f64820SYuchung Cheng void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
1170d4761754SYousuk Seung 		  bool is_sack_reneg, struct rate_sample *rs);
1171d7722e85SSoheil Hassas Yeganeh void tcp_rate_check_app_limited(struct sock *sk);
1172b9f64820SYuchung Cheng 
1173b253a068SPengcheng Yang static inline bool tcp_skb_sent_after(u64 t1, u64 t2, u32 seq1, u32 seq2)
1174b253a068SPengcheng Yang {
1175b253a068SPengcheng Yang 	return t1 > t2 || (t1 == t2 && after(seq1, seq2));
1176b253a068SPengcheng Yang }
1177b253a068SPengcheng Yang 
1178e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK
1179e60402d0SIlpo Järvinen  * handling. SACK is negotiated with the peer, and therefore it can vary
1180e60402d0SIlpo Järvinen  * between different flows.
1181e60402d0SIlpo Järvinen  *
1182e60402d0SIlpo Järvinen  * tcp_is_sack - SACK enabled
1183e60402d0SIlpo Järvinen  * tcp_is_reno - No SACK
1184e60402d0SIlpo Järvinen  */
1185e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp)
1186e60402d0SIlpo Järvinen {
1187ebeef4bcSEric Dumazet 	return likely(tp->rx_opt.sack_ok);
1188e60402d0SIlpo Järvinen }
1189e60402d0SIlpo Järvinen 
1190a2a385d6SEric Dumazet static inline bool tcp_is_reno(const struct tcp_sock *tp)
1191e60402d0SIlpo Järvinen {
1192e60402d0SIlpo Järvinen 	return !tcp_is_sack(tp);
1193e60402d0SIlpo Järvinen }
1194e60402d0SIlpo Järvinen 
119583ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
119683ae4088SIlpo Järvinen {
119783ae4088SIlpo Järvinen 	return tp->sacked_out + tp->lost_out;
119883ae4088SIlpo Järvinen }
119983ae4088SIlpo Järvinen 
12001da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
12011da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
12021da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
12031da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
12041da177e4SLinus Torvalds  *
12051da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
12061da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
12071da177e4SLinus Torvalds  *
12081da177e4SLinus Torvalds  * Read this equation as:
12091da177e4SLinus Torvalds  *
12101da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
12111da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
12121da177e4SLinus Torvalds  *	"Packets fast retransmitted"
12131da177e4SLinus Torvalds  */
121440efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
12151da177e4SLinus Torvalds {
121683ae4088SIlpo Järvinen 	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
12171da177e4SLinus Torvalds }
12181da177e4SLinus Torvalds 
12190b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH	0x7fffffff
12200b6a05c1SIlpo Järvinen 
122140570375SEric Dumazet static inline u32 tcp_snd_cwnd(const struct tcp_sock *tp)
122240570375SEric Dumazet {
122340570375SEric Dumazet 	return tp->snd_cwnd;
122440570375SEric Dumazet }
122540570375SEric Dumazet 
122640570375SEric Dumazet static inline void tcp_snd_cwnd_set(struct tcp_sock *tp, u32 val)
122740570375SEric Dumazet {
122840570375SEric Dumazet 	WARN_ON_ONCE((int)val <= 0);
122940570375SEric Dumazet 	tp->snd_cwnd = val;
123040570375SEric Dumazet }
123140570375SEric Dumazet 
1232071d5080SYuchung Cheng static inline bool tcp_in_slow_start(const struct tcp_sock *tp)
1233071d5080SYuchung Cheng {
123440570375SEric Dumazet 	return tcp_snd_cwnd(tp) < tp->snd_ssthresh;
1235071d5080SYuchung Cheng }
1236071d5080SYuchung Cheng 
12370b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
12380b6a05c1SIlpo Järvinen {
12390b6a05c1SIlpo Järvinen 	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
12400b6a05c1SIlpo Järvinen }
12410b6a05c1SIlpo Järvinen 
1242684bad11SYuchung Cheng static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
1243684bad11SYuchung Cheng {
1244684bad11SYuchung Cheng 	return (TCPF_CA_CWR | TCPF_CA_Recovery) &
1245684bad11SYuchung Cheng 	       (1 << inet_csk(sk)->icsk_ca_state);
1246684bad11SYuchung Cheng }
1247684bad11SYuchung Cheng 
12481da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
1249684bad11SYuchung Cheng  * The exception is cwnd reduction phase, when cwnd is decreasing towards
12501da177e4SLinus Torvalds  * ssthresh.
12511da177e4SLinus Torvalds  */
12526687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
12531da177e4SLinus Torvalds {
12546687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
1255cf533ea5SEric Dumazet 
1256684bad11SYuchung Cheng 	if (tcp_in_cwnd_reduction(sk))
12571da177e4SLinus Torvalds 		return tp->snd_ssthresh;
12581da177e4SLinus Torvalds 	else
12591da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
126040570375SEric Dumazet 			   ((tcp_snd_cwnd(tp) >> 1) +
126140570375SEric Dumazet 			    (tcp_snd_cwnd(tp) >> 2)));
12621da177e4SLinus Torvalds }
12631da177e4SLinus Torvalds 
1264b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */
1265b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
12661da177e4SLinus Torvalds 
12675ee2c941SChristoph Paasch void tcp_enter_cwr(struct sock *sk);
12685c9f3023SJoe Perches __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
12691da177e4SLinus Torvalds 
12706b5a5c0dSNeal Cardwell /* The maximum number of MSS of available cwnd for which TSO defers
12716b5a5c0dSNeal Cardwell  * sending if not using sysctl_tcp_tso_win_divisor.
12726b5a5c0dSNeal Cardwell  */
12736b5a5c0dSNeal Cardwell static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
12746b5a5c0dSNeal Cardwell {
12756b5a5c0dSNeal Cardwell 	return 3;
12766b5a5c0dSNeal Cardwell }
12776b5a5c0dSNeal Cardwell 
127890840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */
127990840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
128090840defSIlpo Järvinen {
128190840defSIlpo Järvinen 	return tp->snd_una + tp->snd_wnd;
128290840defSIlpo Järvinen }
1283e114a710SEric Dumazet 
1284e114a710SEric Dumazet /* We follow the spirit of RFC2861 to validate cwnd but implement a more
1285e114a710SEric Dumazet  * flexible approach. The RFC suggests cwnd should not be raised unless
1286ca8a2263SNeal Cardwell  * it was fully used previously. And that's exactly what we do in
1287ca8a2263SNeal Cardwell  * congestion avoidance mode. But in slow start we allow cwnd to grow
1288ca8a2263SNeal Cardwell  * as long as the application has used half the cwnd.
1289e114a710SEric Dumazet  * Example :
1290e114a710SEric Dumazet  *    cwnd is 10 (IW10), but application sends 9 frames.
1291e114a710SEric Dumazet  *    We allow cwnd to reach 18 when all frames are ACKed.
1292e114a710SEric Dumazet  * This check is safe because it's as aggressive as slow start which already
1293e114a710SEric Dumazet  * risks 100% overshoot. The advantage is that we discourage application to
1294e114a710SEric Dumazet  * either send more filler packets or data to artificially blow up the cwnd
1295e114a710SEric Dumazet  * usage, and allow application-limited process to probe bw more aggressively.
1296e114a710SEric Dumazet  */
129724901551SEric Dumazet static inline bool tcp_is_cwnd_limited(const struct sock *sk)
1298e114a710SEric Dumazet {
1299e114a710SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1300e114a710SEric Dumazet 
1301f4ce91ceSNeal Cardwell 	if (tp->is_cwnd_limited)
1302f4ce91ceSNeal Cardwell 		return true;
1303f4ce91ceSNeal Cardwell 
1304ca8a2263SNeal Cardwell 	/* If in slow start, ensure cwnd grows to twice what was ACKed. */
1305071d5080SYuchung Cheng 	if (tcp_in_slow_start(tp))
130640570375SEric Dumazet 		return tcp_snd_cwnd(tp) < 2 * tp->max_packets_out;
1307ca8a2263SNeal Cardwell 
1308f4ce91ceSNeal Cardwell 	return false;
1309e114a710SEric Dumazet }
1310f4805edeSStephen Hemminger 
1311cadefe5fSEric Dumazet /* BBR congestion control needs pacing.
1312cadefe5fSEric Dumazet  * Same remark for SO_MAX_PACING_RATE.
1313cadefe5fSEric Dumazet  * sch_fq packet scheduler is efficiently handling pacing,
1314cadefe5fSEric Dumazet  * but is not always installed/used.
1315cadefe5fSEric Dumazet  * Return true if TCP stack should pace packets itself.
1316cadefe5fSEric Dumazet  */
1317cadefe5fSEric Dumazet static inline bool tcp_needs_internal_pacing(const struct sock *sk)
1318cadefe5fSEric Dumazet {
1319cadefe5fSEric Dumazet 	return smp_load_acquire(&sk->sk_pacing_status) == SK_PACING_NEEDED;
1320cadefe5fSEric Dumazet }
1321cadefe5fSEric Dumazet 
13228dc242adSEric Dumazet /* Estimates in how many jiffies next packet for this flow can be sent.
13238dc242adSEric Dumazet  * Scheduling a retransmit timer too early would be silly.
13243f80e08fSEric Dumazet  */
13258dc242adSEric Dumazet static inline unsigned long tcp_pacing_delay(const struct sock *sk)
13263f80e08fSEric Dumazet {
13278dc242adSEric Dumazet 	s64 delay = tcp_sk(sk)->tcp_wstamp_ns - tcp_sk(sk)->tcp_clock_cache;
13283f80e08fSEric Dumazet 
13298dc242adSEric Dumazet 	return delay > 0 ? nsecs_to_jiffies(delay) : 0;
13303f80e08fSEric Dumazet }
13313f80e08fSEric Dumazet 
13323f80e08fSEric Dumazet static inline void tcp_reset_xmit_timer(struct sock *sk,
13333f80e08fSEric Dumazet 					const int what,
13343f80e08fSEric Dumazet 					unsigned long when,
13358dc242adSEric Dumazet 					const unsigned long max_when)
13363f80e08fSEric Dumazet {
13378dc242adSEric Dumazet 	inet_csk_reset_xmit_timer(sk, what, when + tcp_pacing_delay(sk),
13383f80e08fSEric Dumazet 				  max_when);
13393f80e08fSEric Dumazet }
13403f80e08fSEric Dumazet 
134121c8fe99SEric Dumazet /* Something is really bad, we could not queue an additional packet,
13423f80e08fSEric Dumazet  * because qdisc is full or receiver sent a 0 window, or we are paced.
134321c8fe99SEric Dumazet  * We do not want to add fuel to the fire, or abort too early,
134421c8fe99SEric Dumazet  * so make sure the timer we arm now is at least 200ms in the future,
134521c8fe99SEric Dumazet  * regardless of current icsk_rto value (as it could be ~2ms)
134621c8fe99SEric Dumazet  */
134721c8fe99SEric Dumazet static inline unsigned long tcp_probe0_base(const struct sock *sk)
134821c8fe99SEric Dumazet {
134921c8fe99SEric Dumazet 	return max_t(unsigned long, inet_csk(sk)->icsk_rto, TCP_RTO_MIN);
135021c8fe99SEric Dumazet }
135121c8fe99SEric Dumazet 
135221c8fe99SEric Dumazet /* Variant of inet_csk_rto_backoff() used for zero window probes */
135321c8fe99SEric Dumazet static inline unsigned long tcp_probe0_when(const struct sock *sk,
135421c8fe99SEric Dumazet 					    unsigned long max_when)
135521c8fe99SEric Dumazet {
13566d4634d1SCambda Zhu 	u8 backoff = min_t(u8, ilog2(TCP_RTO_MAX / TCP_RTO_MIN) + 1,
13576d4634d1SCambda Zhu 			   inet_csk(sk)->icsk_backoff);
13586d4634d1SCambda Zhu 	u64 when = (u64)tcp_probe0_base(sk) << backoff;
135921c8fe99SEric Dumazet 
136021c8fe99SEric Dumazet 	return (unsigned long)min_t(u64, when, max_when);
136121c8fe99SEric Dumazet }
136221c8fe99SEric Dumazet 
13639e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk)
13641da177e4SLinus Torvalds {
136521c8fe99SEric Dumazet 	if (!tcp_sk(sk)->packets_out && !inet_csk(sk)->icsk_pending)
13663f80e08fSEric Dumazet 		tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
13678dc242adSEric Dumazet 				     tcp_probe0_base(sk), TCP_RTO_MAX);
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
1370ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
13711da177e4SLinus Torvalds {
13721da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds 
1375ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
13761da177e4SLinus Torvalds {
13771da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
13781da177e4SLinus Torvalds }
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds /*
13811da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
13821da177e4SLinus Torvalds  */
1383ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr,
1384ba7808eaSFrederik Deweerdt 				   __be32 daddr, __wsum base)
13851da177e4SLinus Torvalds {
13861da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_TCP, base);
13871da177e4SLinus Torvalds }
13881da177e4SLinus Torvalds 
1389a2a385d6SEric Dumazet static inline bool tcp_checksum_complete(struct sk_buff *skb)
13901da177e4SLinus Torvalds {
139160476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
13926ab6dfa6SCong Wang 		__skb_checksum_complete(skb);
13931da177e4SLinus Torvalds }
13941da177e4SLinus Torvalds 
13957a26dc9eSMenglong Dong bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb,
13967a26dc9eSMenglong Dong 		     enum skb_drop_reason *reason);
1397f35f8219SEric Dumazet 
1398f35f8219SEric Dumazet 
1399ac6e7800SEric Dumazet int tcp_filter(struct sock *sk, struct sk_buff *skb);
14005c9f3023SJoe Perches void tcp_set_state(struct sock *sk, int state);
14015c9f3023SJoe Perches void tcp_done(struct sock *sk);
1402c1e64e29SLorenzo Colitti int tcp_abort(struct sock *sk, int err);
1403c1e64e29SLorenzo Colitti 
140440efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
14051da177e4SLinus Torvalds {
14061da177e4SLinus Torvalds 	rx_opt->dsack = 0;
14071da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14106f021c62SEric Dumazet void tcp_cwnd_restart(struct sock *sk, s32 delta);
14116f021c62SEric Dumazet 
14126f021c62SEric Dumazet static inline void tcp_slow_start_after_idle_check(struct sock *sk)
14136f021c62SEric Dumazet {
14141b1fc3fdSWei Wang 	const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
14156f021c62SEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
14166f021c62SEric Dumazet 	s32 delta;
14176f021c62SEric Dumazet 
14184845b571SKuniyuki Iwashima 	if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle) ||
14194845b571SKuniyuki Iwashima 	    tp->packets_out || ca_ops->cong_control)
14206f021c62SEric Dumazet 		return;
1421d635fbe2SEric Dumazet 	delta = tcp_jiffies32 - tp->lsndtime;
14226f021c62SEric Dumazet 	if (delta > inet_csk(sk)->icsk_rto)
14236f021c62SEric Dumazet 		tcp_cwnd_restart(sk, delta);
14246f021c62SEric Dumazet }
142585f16525SYuchung Cheng 
14261da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
1427ceef9ab6SEric Dumazet void tcp_select_initial_window(const struct sock *sk, int __space,
1428ceef9ab6SEric Dumazet 			       __u32 mss, __u32 *rcv_wnd,
14295c9f3023SJoe Perches 			       __u32 *window_clamp, int wscale_ok,
14305c9f3023SJoe Perches 			       __u8 *rcv_wscale, __u32 init_rcv_wnd);
14311da177e4SLinus Torvalds 
1432b8dc6d6cSPaolo Abeni static inline int __tcp_win_from_space(u8 scaling_ratio, int space)
14331da177e4SLinus Torvalds {
1434b8dc6d6cSPaolo Abeni 	s64 scaled_space = (s64)space * scaling_ratio;
1435c4836742SGao Feng 
1436dfa2f048SEric Dumazet 	return scaled_space >> TCP_RMEM_TO_WIN_SCALE;
1437dfa2f048SEric Dumazet }
1438dfa2f048SEric Dumazet 
1439b8dc6d6cSPaolo Abeni static inline int tcp_win_from_space(const struct sock *sk, int space)
1440b8dc6d6cSPaolo Abeni {
1441b8dc6d6cSPaolo Abeni 	return __tcp_win_from_space(tcp_sk(sk)->scaling_ratio, space);
1442b8dc6d6cSPaolo Abeni }
1443b8dc6d6cSPaolo Abeni 
1444b8dc6d6cSPaolo Abeni /* inverse of __tcp_win_from_space() */
1445b8dc6d6cSPaolo Abeni static inline int __tcp_space_from_win(u8 scaling_ratio, int win)
1446dfa2f048SEric Dumazet {
1447dfa2f048SEric Dumazet 	u64 val = (u64)win << TCP_RMEM_TO_WIN_SCALE;
1448dfa2f048SEric Dumazet 
1449b8dc6d6cSPaolo Abeni 	do_div(val, scaling_ratio);
1450dfa2f048SEric Dumazet 	return val;
1451dfa2f048SEric Dumazet }
1452dfa2f048SEric Dumazet 
1453b8dc6d6cSPaolo Abeni static inline int tcp_space_from_win(const struct sock *sk, int win)
1454b8dc6d6cSPaolo Abeni {
1455b8dc6d6cSPaolo Abeni 	return __tcp_space_from_win(tcp_sk(sk)->scaling_ratio, win);
1456b8dc6d6cSPaolo Abeni }
1457b8dc6d6cSPaolo Abeni 
1458dfa2f048SEric Dumazet static inline void tcp_scaling_ratio_init(struct sock *sk)
1459dfa2f048SEric Dumazet {
1460dfa2f048SEric Dumazet 	/* Assume a conservative default of 1200 bytes of payload per 4K page.
1461dfa2f048SEric Dumazet 	 * This may be adjusted later in tcp_measure_rcv_mss().
1462dfa2f048SEric Dumazet 	 */
1463dfa2f048SEric Dumazet 	tcp_sk(sk)->scaling_ratio = (1200 << TCP_RMEM_TO_WIN_SCALE) /
1464dfa2f048SEric Dumazet 				    SKB_TRUESIZE(4096);
14651da177e4SLinus Torvalds }
14661da177e4SLinus Torvalds 
14671da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
14681da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
14691da177e4SLinus Torvalds {
1470ebb3b78dSEric Dumazet 	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) -
147170c26558SEric Dumazet 				  READ_ONCE(sk->sk_backlog.len) -
14721da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
14761da177e4SLinus Torvalds {
1477ebb3b78dSEric Dumazet 	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
14781da177e4SLinus Torvalds }
14791da177e4SLinus Torvalds 
1480053f3684SWei Wang static inline void tcp_adjust_rcv_ssthresh(struct sock *sk)
1481053f3684SWei Wang {
1482053f3684SWei Wang 	int unused_mem = sk_unused_reserved_mem(sk);
1483053f3684SWei Wang 	struct tcp_sock *tp = tcp_sk(sk);
1484053f3684SWei Wang 
1485053f3684SWei Wang 	tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
1486053f3684SWei Wang 	if (unused_mem)
1487053f3684SWei Wang 		tp->rcv_ssthresh = max_t(u32, tp->rcv_ssthresh,
1488053f3684SWei Wang 					 tcp_win_from_space(sk, unused_mem));
1489053f3684SWei Wang }
1490053f3684SWei Wang 
1491c76c6956SPaolo Abeni void tcp_cleanup_rbuf(struct sock *sk, int copied);
1492e5c6de5fSJohn Fastabend void __tcp_cleanup_rbuf(struct sock *sk, int copied);
1493e5c6de5fSJohn Fastabend 
1494c76c6956SPaolo Abeni 
149524adbc16SEric Dumazet /* We provision sk_rcvbuf around 200% of sk_rcvlowat.
149624adbc16SEric Dumazet  * If 87.5 % (7/8) of the space has been consumed, we want to override
149724adbc16SEric Dumazet  * SO_RCVLOWAT constraint, since we are receiving skbs with too small
149824adbc16SEric Dumazet  * len/truesize ratio.
149924adbc16SEric Dumazet  */
150024adbc16SEric Dumazet static inline bool tcp_rmem_pressure(const struct sock *sk)
150124adbc16SEric Dumazet {
1502f969dc5aSEric Dumazet 	int rcvbuf, threshold;
1503f969dc5aSEric Dumazet 
1504f969dc5aSEric Dumazet 	if (tcp_under_memory_pressure(sk))
1505f969dc5aSEric Dumazet 		return true;
1506f969dc5aSEric Dumazet 
1507f969dc5aSEric Dumazet 	rcvbuf = READ_ONCE(sk->sk_rcvbuf);
1508f969dc5aSEric Dumazet 	threshold = rcvbuf - (rcvbuf >> 3);
150924adbc16SEric Dumazet 
151024adbc16SEric Dumazet 	return atomic_read(&sk->sk_rmem_alloc) > threshold;
151124adbc16SEric Dumazet }
151224adbc16SEric Dumazet 
151305dc72abSEric Dumazet static inline bool tcp_epollin_ready(const struct sock *sk, int target)
151405dc72abSEric Dumazet {
151505dc72abSEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
151605dc72abSEric Dumazet 	int avail = READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->copied_seq);
151705dc72abSEric Dumazet 
151805dc72abSEric Dumazet 	if (avail <= 0)
151905dc72abSEric Dumazet 		return false;
152005dc72abSEric Dumazet 
152105dc72abSEric Dumazet 	return (avail >= target) || tcp_rmem_pressure(sk) ||
152205dc72abSEric Dumazet 	       (tcp_receive_window(tp) <= inet_csk(sk)->icsk_ack.rcv_mss);
152305dc72abSEric Dumazet }
152405dc72abSEric Dumazet 
1525843f4a55SYuchung Cheng extern void tcp_openreq_init_rwin(struct request_sock *req,
1526b1964b5fSEric Dumazet 				  const struct sock *sk_listener,
1527b1964b5fSEric Dumazet 				  const struct dst_entry *dst);
1528843f4a55SYuchung Cheng 
15295c9f3023SJoe Perches void tcp_enter_memory_pressure(struct sock *sk);
153006044751SEric Dumazet void tcp_leave_memory_pressure(struct sock *sk);
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
15331da177e4SLinus Torvalds {
1534b840d15dSNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
15355ecf9d4fSEric Dumazet 	int val;
1536b840d15dSNikolay Borisov 
15375ecf9d4fSEric Dumazet 	/* Paired with WRITE_ONCE() in tcp_sock_set_keepintvl()
15385ecf9d4fSEric Dumazet 	 * and do_tcp_setsockopt().
15395ecf9d4fSEric Dumazet 	 */
15405ecf9d4fSEric Dumazet 	val = READ_ONCE(tp->keepalive_intvl);
15415ecf9d4fSEric Dumazet 
15425ecf9d4fSEric Dumazet 	return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_intvl);
15431da177e4SLinus Torvalds }
15441da177e4SLinus Torvalds 
15451da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
15461da177e4SLinus Torvalds {
154713b287e8SNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
15484164245cSEric Dumazet 	int val;
154913b287e8SNikolay Borisov 
15504164245cSEric Dumazet 	/* Paired with WRITE_ONCE() in tcp_sock_set_keepidle_locked() */
15514164245cSEric Dumazet 	val = READ_ONCE(tp->keepalive_time);
15524164245cSEric Dumazet 
15534164245cSEric Dumazet 	return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time);
15541da177e4SLinus Torvalds }
15551da177e4SLinus Torvalds 
1556df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp)
1557df19a626SEric Dumazet {
15589bd6861bSNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
15596e5e1de6SEric Dumazet 	int val;
15609bd6861bSNikolay Borisov 
15616e5e1de6SEric Dumazet 	/* Paired with WRITE_ONCE() in tcp_sock_set_keepcnt()
15626e5e1de6SEric Dumazet 	 * and do_tcp_setsockopt().
15636e5e1de6SEric Dumazet 	 */
15646e5e1de6SEric Dumazet 	val = READ_ONCE(tp->keepalive_probes);
15656e5e1de6SEric Dumazet 
15666e5e1de6SEric Dumazet 	return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_probes);
1567df19a626SEric Dumazet }
1568df19a626SEric Dumazet 
15696c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
15706c37e5deSFlavio Leitner {
15716c37e5deSFlavio Leitner 	const struct inet_connection_sock *icsk = &tp->inet_conn;
15726c37e5deSFlavio Leitner 
157370eabf0eSEric Dumazet 	return min_t(u32, tcp_jiffies32 - icsk->icsk_ack.lrcvtime,
157470eabf0eSEric Dumazet 			  tcp_jiffies32 - tp->rcv_tstamp);
15756c37e5deSFlavio Leitner }
15766c37e5deSFlavio Leitner 
1577463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
15781da177e4SLinus Torvalds {
157939e24435SKuniyuki Iwashima 	int fin_timeout = tcp_sk(sk)->linger2 ? :
158039e24435SKuniyuki Iwashima 		READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fin_timeout);
1581463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
15821da177e4SLinus Torvalds 
1583463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1584463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	return fin_timeout;
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds 
1589a2a385d6SEric Dumazet static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
1590c887e6d2SIlpo Järvinen 				  int paws_win)
15911da177e4SLinus Torvalds {
1592c887e6d2SIlpo Järvinen 	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1593a2a385d6SEric Dumazet 		return true;
1594cca9bab1SArnd Bergmann 	if (unlikely(!time_before32(ktime_get_seconds(),
1595cca9bab1SArnd Bergmann 				    rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)))
1596a2a385d6SEric Dumazet 		return true;
1597bc2ce894SEric Dumazet 	/*
1598bc2ce894SEric Dumazet 	 * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
1599bc2ce894SEric Dumazet 	 * then following tcp messages have valid values. Ignore 0 value,
1600bc2ce894SEric Dumazet 	 * or else 'negative' tsval might forbid us to accept their packets.
1601bc2ce894SEric Dumazet 	 */
1602bc2ce894SEric Dumazet 	if (!rx_opt->ts_recent)
1603a2a385d6SEric Dumazet 		return true;
1604a2a385d6SEric Dumazet 	return false;
1605c887e6d2SIlpo Järvinen }
1606c887e6d2SIlpo Järvinen 
1607a2a385d6SEric Dumazet static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
1608c887e6d2SIlpo Järvinen 				   int rst)
1609c887e6d2SIlpo Järvinen {
1610c887e6d2SIlpo Järvinen 	if (tcp_paws_check(rx_opt, 0))
1611a2a385d6SEric Dumazet 		return false;
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
16141da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
16151da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
16161da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
16171da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
16181da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
16191da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
16201da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
16211da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
16241da177e4SLinus Torvalds 	 */
1625cca9bab1SArnd Bergmann 	if (rst && !time_before32(ktime_get_seconds(),
1626cca9bab1SArnd Bergmann 				  rx_opt->ts_recent_stamp + TCP_PAWS_MSL))
1627a2a385d6SEric Dumazet 		return false;
1628a2a385d6SEric Dumazet 	return true;
16291da177e4SLinus Torvalds }
16301da177e4SLinus Torvalds 
16317970ddc8SEric Dumazet bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
16327970ddc8SEric Dumazet 			  int mib_idx, u32 *last_oow_ack_time);
1633032ee423SNeal Cardwell 
1634a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net)
16351da177e4SLinus Torvalds {
16361da177e4SLinus Torvalds 	/* See RFC 2012 */
16376aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOALGORITHM, 1);
16386aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
16396aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
16406aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_MAXCONN, -1);
16411da177e4SLinus Torvalds }
16421da177e4SLinus Torvalds 
16436a438bbeSStephen Hemminger /* from STCP */
1644ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
16450800f170SDavid S. Miller {
16466a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
1647ef9da47cSIlpo Järvinen }
1648ef9da47cSIlpo Järvinen 
1649ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
1650ef9da47cSIlpo Järvinen {
1651ef9da47cSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
16526a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
1653b7689205SIlpo Järvinen }
1654b7689205SIlpo Järvinen 
1655a915da9bSEric Dumazet union tcp_md5_addr {
1656a915da9bSEric Dumazet 	struct in_addr  a4;
1657a915da9bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1658a915da9bSEric Dumazet 	struct in6_addr	a6;
1659a915da9bSEric Dumazet #endif
1660a915da9bSEric Dumazet };
1661a915da9bSEric Dumazet 
1662cfb6eeb4SYOSHIFUJI Hideaki /* - key database */
1663cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key {
1664a915da9bSEric Dumazet 	struct hlist_node	node;
1665cfb6eeb4SYOSHIFUJI Hideaki 	u8			keylen;
1666a915da9bSEric Dumazet 	u8			family; /* AF_INET or AF_INET6 */
16676797318eSIvan Delalande 	u8			prefixlen;
1668a76c2315SLeonard Crestez 	u8			flags;
1669dea53bb8SDavid Ahern 	union tcp_md5_addr	addr;
1670dea53bb8SDavid Ahern 	int			l3index; /* set if key added with L3 scope */
1671a915da9bSEric Dumazet 	u8			key[TCP_MD5SIG_MAXKEYLEN];
1672a915da9bSEric Dumazet 	struct rcu_head		rcu;
1673cfb6eeb4SYOSHIFUJI Hideaki };
1674cfb6eeb4SYOSHIFUJI Hideaki 
1675cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */
1676cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info {
1677a915da9bSEric Dumazet 	struct hlist_head	head;
1678a8afca03SEric Dumazet 	struct rcu_head		rcu;
1679cfb6eeb4SYOSHIFUJI Hideaki };
1680cfb6eeb4SYOSHIFUJI Hideaki 
1681cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */
1682cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr {
1683cfb6eeb4SYOSHIFUJI Hideaki 	__be32		saddr;
1684cfb6eeb4SYOSHIFUJI Hideaki 	__be32		daddr;
1685cfb6eeb4SYOSHIFUJI Hideaki 	__u8		pad;
1686cfb6eeb4SYOSHIFUJI Hideaki 	__u8		protocol;
1687cfb6eeb4SYOSHIFUJI Hideaki 	__be16		len;
1688cfb6eeb4SYOSHIFUJI Hideaki };
1689cfb6eeb4SYOSHIFUJI Hideaki 
1690cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr {
1691cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr	saddr;
1692cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr daddr;
1693cfb6eeb4SYOSHIFUJI Hideaki 	__be32		len;
1694cfb6eeb4SYOSHIFUJI Hideaki 	__be32		protocol;	/* including padding */
1695cfb6eeb4SYOSHIFUJI Hideaki };
1696cfb6eeb4SYOSHIFUJI Hideaki 
1697cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block {
1698cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr ip4;
1699dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1700cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_pseudohdr ip6;
1701cfb6eeb4SYOSHIFUJI Hideaki #endif
1702cfb6eeb4SYOSHIFUJI Hideaki };
1703cfb6eeb4SYOSHIFUJI Hideaki 
1704cfb6eeb4SYOSHIFUJI Hideaki /* - pool: digest algorithm, hash description and scratch buffer */
1705cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool {
1706cf80e0e4SHerbert Xu 	struct ahash_request	*md5_req;
170719689e38SEric Dumazet 	void			*scratch;
1708cfb6eeb4SYOSHIFUJI Hideaki };
1709cfb6eeb4SYOSHIFUJI Hideaki 
1710cfb6eeb4SYOSHIFUJI Hideaki /* - functions */
171139f8e58eSEric Dumazet int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
171239f8e58eSEric Dumazet 			const struct sock *sk, const struct sk_buff *skb);
17135c9f3023SJoe Perches int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
1714a76c2315SLeonard Crestez 		   int family, u8 prefixlen, int l3index, u8 flags,
1715459837b5SDmitry Safonov 		   const u8 *newkey, u8 newkeylen);
1716459837b5SDmitry Safonov int tcp_md5_key_copy(struct sock *sk, const union tcp_md5_addr *addr,
1717459837b5SDmitry Safonov 		     int family, u8 prefixlen, int l3index,
1718459837b5SDmitry Safonov 		     struct tcp_md5sig_key *key);
1719459837b5SDmitry Safonov 
17205c9f3023SJoe Perches int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
1721a76c2315SLeonard Crestez 		   int family, u8 prefixlen, int l3index, u8 flags);
1722b83e3debSEric Dumazet struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
1723fd3a154aSEric Dumazet 					 const struct sock *addr_sk);
1724cfb6eeb4SYOSHIFUJI Hideaki 
17259501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
17266015c71eSEric Dumazet #include <linux/jump_label.h>
1727459837b5SDmitry Safonov extern struct static_key_false_deferred tcp_md5_needed;
1728dea53bb8SDavid Ahern struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index,
17295c9f3023SJoe Perches 					   const union tcp_md5_addr *addr,
17305c9f3023SJoe Perches 					   int family);
17316015c71eSEric Dumazet static inline struct tcp_md5sig_key *
1732dea53bb8SDavid Ahern tcp_md5_do_lookup(const struct sock *sk, int l3index,
1733dea53bb8SDavid Ahern 		  const union tcp_md5_addr *addr, int family)
17346015c71eSEric Dumazet {
1735459837b5SDmitry Safonov 	if (!static_branch_unlikely(&tcp_md5_needed.key))
17366015c71eSEric Dumazet 		return NULL;
1737dea53bb8SDavid Ahern 	return __tcp_md5_do_lookup(sk, l3index, addr, family);
17386015c71eSEric Dumazet }
17391330b6efSJakub Kicinski 
17401330b6efSJakub Kicinski enum skb_drop_reason
17411330b6efSJakub Kicinski tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb,
17427bbb765bSDmitry Safonov 		     const void *saddr, const void *daddr,
17437bbb765bSDmitry Safonov 		     int family, int dif, int sdif);
17447bbb765bSDmitry Safonov 
17456015c71eSEric Dumazet 
1746a915da9bSEric Dumazet #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_key)
17479501f972SYOSHIFUJI Hideaki #else
1748dea53bb8SDavid Ahern static inline struct tcp_md5sig_key *
1749dea53bb8SDavid Ahern tcp_md5_do_lookup(const struct sock *sk, int l3index,
1750dea53bb8SDavid Ahern 		  const union tcp_md5_addr *addr, int family)
1751a915da9bSEric Dumazet {
1752a915da9bSEric Dumazet 	return NULL;
1753a915da9bSEric Dumazet }
17541330b6efSJakub Kicinski 
17551330b6efSJakub Kicinski static inline enum skb_drop_reason
17561330b6efSJakub Kicinski tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb,
17577bbb765bSDmitry Safonov 		     const void *saddr, const void *daddr,
175824055bb8SVladimir Oltean 		     int family, int dif, int sdif)
17597bbb765bSDmitry Safonov {
17601330b6efSJakub Kicinski 	return SKB_NOT_DROPPED_YET;
17617bbb765bSDmitry Safonov }
17629501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	NULL
17639501f972SYOSHIFUJI Hideaki #endif
17649501f972SYOSHIFUJI Hideaki 
17655c9f3023SJoe Perches bool tcp_alloc_md5sig_pool(void);
1766cfb6eeb4SYOSHIFUJI Hideaki 
17675c9f3023SJoe Perches struct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
176871cea17eSEric Dumazet static inline void tcp_put_md5sig_pool(void)
176971cea17eSEric Dumazet {
177071cea17eSEric Dumazet 	local_bh_enable();
177171cea17eSEric Dumazet }
177235790c04SEric Dumazet 
17735c9f3023SJoe Perches int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
177495c96174SEric Dumazet 			  unsigned int header_len);
17755c9f3023SJoe Perches int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
1776cf533ea5SEric Dumazet 		     const struct tcp_md5sig_key *key);
1777cfb6eeb4SYOSHIFUJI Hideaki 
177810467163SJerry Chu /* From tcp_fastopen.c */
17795c9f3023SJoe Perches void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
17807268586bSYuchung Cheng 			    struct tcp_fastopen_cookie *cookie);
17815c9f3023SJoe Perches void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
17822646c831SDaniel Lee 			    struct tcp_fastopen_cookie *cookie, bool syn_lost,
17832646c831SDaniel Lee 			    u16 try_exp);
1784783237e8SYuchung Cheng struct tcp_fastopen_request {
1785783237e8SYuchung Cheng 	/* Fast Open cookie. Size 0 means a cookie request */
1786783237e8SYuchung Cheng 	struct tcp_fastopen_cookie	cookie;
1787783237e8SYuchung Cheng 	struct msghdr			*data;  /* data in MSG_FASTOPEN */
1788f5ddcbbbSEric Dumazet 	size_t				size;
1789f5ddcbbbSEric Dumazet 	int				copied;	/* queued in tcp_connect() */
1790f859a448SWillem de Bruijn 	struct ubuf_info		*uarg;
1791783237e8SYuchung Cheng };
1792783237e8SYuchung Cheng void tcp_free_fastopen_req(struct tcp_sock *tp);
17931fba70e5SYuchung Cheng void tcp_fastopen_destroy_cipher(struct sock *sk);
179443713848SHaishuang Yan void tcp_fastopen_ctx_destroy(struct net *net);
17951fba70e5SYuchung Cheng int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
1796438ac880SArd Biesheuvel 			      void *primary_key, void *backup_key);
1797f19008e6SJason Baron int tcp_fastopen_get_cipher(struct net *net, struct inet_connection_sock *icsk,
1798f19008e6SJason Baron 			    u64 *key);
179961d2bcaeSEric Dumazet void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb);
18007c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
18015b7ed089SYuchung Cheng 			      struct request_sock *req,
180271c02379SChristoph Paasch 			      struct tcp_fastopen_cookie *foc,
180371c02379SChristoph Paasch 			      const struct dst_entry *dst);
180443713848SHaishuang Yan void tcp_fastopen_init_key_once(struct net *net);
1805065263f4SWei Wang bool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss,
1806065263f4SWei Wang 			     struct tcp_fastopen_cookie *cookie);
180719f6d3f3SWei Wang bool tcp_fastopen_defer_connect(struct sock *sk, int *err);
1808438ac880SArd Biesheuvel #define TCP_FASTOPEN_KEY_LENGTH sizeof(siphash_key_t)
18099092a76dSJason Baron #define TCP_FASTOPEN_KEY_MAX 2
18109092a76dSJason Baron #define TCP_FASTOPEN_KEY_BUF_LENGTH \
18119092a76dSJason Baron 	(TCP_FASTOPEN_KEY_LENGTH * TCP_FASTOPEN_KEY_MAX)
181210467163SJerry Chu 
181310467163SJerry Chu /* Fastopen key context */
181410467163SJerry Chu struct tcp_fastopen_context {
1815438ac880SArd Biesheuvel 	siphash_key_t	key[TCP_FASTOPEN_KEY_MAX];
1816c681edaeSArd Biesheuvel 	int		num;
181710467163SJerry Chu 	struct rcu_head	rcu;
181810467163SJerry Chu };
181910467163SJerry Chu 
182046c2fa39SWei Wang void tcp_fastopen_active_disable(struct sock *sk);
1821cf1ef3f0SWei Wang bool tcp_fastopen_active_should_disable(struct sock *sk);
1822cf1ef3f0SWei Wang void tcp_fastopen_active_disable_ofo_check(struct sock *sk);
18237268586bSYuchung Cheng void tcp_fastopen_active_detect_blackhole(struct sock *sk, bool expired);
1824cf1ef3f0SWei Wang 
18259092a76dSJason Baron /* Caller needs to wrap with rcu_read_(un)lock() */
18269092a76dSJason Baron static inline
18279092a76dSJason Baron struct tcp_fastopen_context *tcp_fastopen_get_ctx(const struct sock *sk)
18289092a76dSJason Baron {
18299092a76dSJason Baron 	struct tcp_fastopen_context *ctx;
18309092a76dSJason Baron 
18319092a76dSJason Baron 	ctx = rcu_dereference(inet_csk(sk)->icsk_accept_queue.fastopenq.ctx);
18329092a76dSJason Baron 	if (!ctx)
18339092a76dSJason Baron 		ctx = rcu_dereference(sock_net(sk)->ipv4.tcp_fastopen_ctx);
18349092a76dSJason Baron 	return ctx;
18359092a76dSJason Baron }
18369092a76dSJason Baron 
18379092a76dSJason Baron static inline
18389092a76dSJason Baron bool tcp_fastopen_cookie_match(const struct tcp_fastopen_cookie *foc,
18399092a76dSJason Baron 			       const struct tcp_fastopen_cookie *orig)
18409092a76dSJason Baron {
18419092a76dSJason Baron 	if (orig->len == TCP_FASTOPEN_COOKIE_SIZE &&
18429092a76dSJason Baron 	    orig->len == foc->len &&
18439092a76dSJason Baron 	    !memcmp(orig->val, foc->val, foc->len))
18449092a76dSJason Baron 		return true;
18459092a76dSJason Baron 	return false;
18469092a76dSJason Baron }
18479092a76dSJason Baron 
18489092a76dSJason Baron static inline
18499092a76dSJason Baron int tcp_fastopen_context_len(const struct tcp_fastopen_context *ctx)
18509092a76dSJason Baron {
1851c681edaeSArd Biesheuvel 	return ctx->num;
18529092a76dSJason Baron }
18539092a76dSJason Baron 
185405b055e8SFrancis Yan /* Latencies incurred by various limits for a sender. They are
185505b055e8SFrancis Yan  * chronograph-like stats that are mutually exclusive.
185605b055e8SFrancis Yan  */
185705b055e8SFrancis Yan enum tcp_chrono {
185805b055e8SFrancis Yan 	TCP_CHRONO_UNSPEC,
185905b055e8SFrancis Yan 	TCP_CHRONO_BUSY, /* Actively sending data (non-empty write queue) */
186005b055e8SFrancis Yan 	TCP_CHRONO_RWND_LIMITED, /* Stalled by insufficient receive window */
186105b055e8SFrancis Yan 	TCP_CHRONO_SNDBUF_LIMITED, /* Stalled by insufficient send buffer */
186205b055e8SFrancis Yan 	__TCP_CHRONO_MAX,
186305b055e8SFrancis Yan };
186405b055e8SFrancis Yan 
186505b055e8SFrancis Yan void tcp_chrono_start(struct sock *sk, const enum tcp_chrono type);
186605b055e8SFrancis Yan void tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type);
186705b055e8SFrancis Yan 
1868e2080072SEric Dumazet /* This helper is needed, because skb->tcp_tsorted_anchor uses
1869e2080072SEric Dumazet  * the same memory storage than skb->destructor/_skb_refdst
1870e2080072SEric Dumazet  */
1871e2080072SEric Dumazet static inline void tcp_skb_tsorted_anchor_cleanup(struct sk_buff *skb)
1872e2080072SEric Dumazet {
1873e2080072SEric Dumazet 	skb->destructor = NULL;
1874e2080072SEric Dumazet 	skb->_skb_refdst = 0UL;
1875e2080072SEric Dumazet }
1876e2080072SEric Dumazet 
1877e2080072SEric Dumazet #define tcp_skb_tsorted_save(skb) {		\
1878e2080072SEric Dumazet 	unsigned long _save = skb->_skb_refdst;	\
1879e2080072SEric Dumazet 	skb->_skb_refdst = 0UL;
1880e2080072SEric Dumazet 
1881e2080072SEric Dumazet #define tcp_skb_tsorted_restore(skb)		\
1882e2080072SEric Dumazet 	skb->_skb_refdst = _save;		\
1883e2080072SEric Dumazet }
1884e2080072SEric Dumazet 
1885ac3f09baSEric Dumazet void tcp_write_queue_purge(struct sock *sk);
1886fe067e8aSDavid S. Miller 
188775c119afSEric Dumazet static inline struct sk_buff *tcp_rtx_queue_head(const struct sock *sk)
188875c119afSEric Dumazet {
188975c119afSEric Dumazet 	return skb_rb_first(&sk->tcp_rtx_queue);
189075c119afSEric Dumazet }
189175c119afSEric Dumazet 
1892b617158dSEric Dumazet static inline struct sk_buff *tcp_rtx_queue_tail(const struct sock *sk)
1893b617158dSEric Dumazet {
1894b617158dSEric Dumazet 	return skb_rb_last(&sk->tcp_rtx_queue);
1895b617158dSEric Dumazet }
1896b617158dSEric Dumazet 
1897cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
1898fe067e8aSDavid S. Miller {
1899cd07a8eaSDavid S. Miller 	return skb_peek_tail(&sk->sk_write_queue);
1900fe067e8aSDavid S. Miller }
1901fe067e8aSDavid S. Miller 
1902234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
1903cd07a8eaSDavid S. Miller 	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1904234b6860SIlpo Järvinen 
1905cf533ea5SEric Dumazet static inline struct sk_buff *tcp_send_head(const struct sock *sk)
1906fe067e8aSDavid S. Miller {
190775c119afSEric Dumazet 	return skb_peek(&sk->sk_write_queue);
1908fe067e8aSDavid S. Miller }
1909fe067e8aSDavid S. Miller 
1910cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk,
1911cd07a8eaSDavid S. Miller 				   const struct sk_buff *skb)
1912cd07a8eaSDavid S. Miller {
1913cd07a8eaSDavid S. Miller 	return skb_queue_is_last(&sk->sk_write_queue, skb);
1914cd07a8eaSDavid S. Miller }
1915cd07a8eaSDavid S. Miller 
1916ee2aabd3SEric Dumazet /**
1917ee2aabd3SEric Dumazet  * tcp_write_queue_empty - test if any payload (or FIN) is available in write queue
1918ee2aabd3SEric Dumazet  * @sk: socket
1919ee2aabd3SEric Dumazet  *
1920ee2aabd3SEric Dumazet  * Since the write queue can have a temporary empty skb in it,
1921ee2aabd3SEric Dumazet  * we must not use "return skb_queue_empty(&sk->sk_write_queue)"
1922ee2aabd3SEric Dumazet  */
192375c119afSEric Dumazet static inline bool tcp_write_queue_empty(const struct sock *sk)
1924fe067e8aSDavid S. Miller {
1925ee2aabd3SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1926ee2aabd3SEric Dumazet 
1927ee2aabd3SEric Dumazet 	return tp->write_seq == tp->snd_nxt;
192875c119afSEric Dumazet }
192975c119afSEric Dumazet 
193075c119afSEric Dumazet static inline bool tcp_rtx_queue_empty(const struct sock *sk)
193175c119afSEric Dumazet {
193275c119afSEric Dumazet 	return RB_EMPTY_ROOT(&sk->tcp_rtx_queue);
193375c119afSEric Dumazet }
193475c119afSEric Dumazet 
193575c119afSEric Dumazet static inline bool tcp_rtx_and_write_queues_empty(const struct sock *sk)
193675c119afSEric Dumazet {
193775c119afSEric Dumazet 	return tcp_rtx_queue_empty(sk) && tcp_write_queue_empty(sk);
1938fe067e8aSDavid S. Miller }
1939fe067e8aSDavid S. Miller 
1940fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1941fe067e8aSDavid S. Miller {
1942a43e052bSEric Dumazet 	__skb_queue_tail(&sk->sk_write_queue, skb);
1943fe067e8aSDavid S. Miller 
1944fe067e8aSDavid S. Miller 	/* Queue it, remembering where we must start sending. */
194550895b9dSEric Dumazet 	if (sk->sk_write_queue.next == skb)
19460f87230dSFrancis Yan 		tcp_chrono_start(sk, TCP_CHRONO_BUSY);
1947fe067e8aSDavid S. Miller }
1948fe067e8aSDavid S. Miller 
194943f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk.  */
1950fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new,
1951fe067e8aSDavid S. Miller 						  struct sk_buff *skb,
1952fe067e8aSDavid S. Miller 						  struct sock *sk)
1953fe067e8aSDavid S. Miller {
195443f59c89SDavid S. Miller 	__skb_queue_before(&sk->sk_write_queue, skb, new);
1955fe067e8aSDavid S. Miller }
1956fe067e8aSDavid S. Miller 
1957fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
1958fe067e8aSDavid S. Miller {
19594a269818SEric Dumazet 	tcp_skb_tsorted_anchor_cleanup(skb);
1960fe067e8aSDavid S. Miller 	__skb_unlink(skb, &sk->sk_write_queue);
1961fe067e8aSDavid S. Miller }
1962fe067e8aSDavid S. Miller 
196375c119afSEric Dumazet void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb);
196475c119afSEric Dumazet 
196575c119afSEric Dumazet static inline void tcp_rtx_queue_unlink(struct sk_buff *skb, struct sock *sk)
1966fe067e8aSDavid S. Miller {
196775c119afSEric Dumazet 	tcp_skb_tsorted_anchor_cleanup(skb);
196875c119afSEric Dumazet 	rb_erase(&skb->rbnode, &sk->tcp_rtx_queue);
196975c119afSEric Dumazet }
197075c119afSEric Dumazet 
197175c119afSEric Dumazet static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct sock *sk)
197275c119afSEric Dumazet {
197375c119afSEric Dumazet 	list_del(&skb->tcp_tsorted_anchor);
197475c119afSEric Dumazet 	tcp_rtx_queue_unlink(skb, sk);
197503271f3aSTalal Ahmad 	tcp_wmem_free_skb(sk, skb);
1976fe067e8aSDavid S. Miller }
1977fe067e8aSDavid S. Miller 
197812d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk)
197912d50c46SKrishna Kumar {
198012d50c46SKrishna Kumar 	if (tcp_send_head(sk)) {
198112d50c46SKrishna Kumar 		struct tcp_sock *tp = tcp_sk(sk);
198212d50c46SKrishna Kumar 
198312d50c46SKrishna Kumar 		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
198412d50c46SKrishna Kumar 	}
198512d50c46SKrishna Kumar }
198612d50c46SKrishna Kumar 
1987ecb97192SNeal Cardwell /* Start sequence of the skb just after the highest skb with SACKed
1988ecb97192SNeal Cardwell  * bit, valid only if sacked_out > 0 or when the caller has ensured
1989ecb97192SNeal Cardwell  * validity by itself.
1990a47e5a98SIlpo Järvinen  */
1991a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
1992a47e5a98SIlpo Järvinen {
1993a47e5a98SIlpo Järvinen 	if (!tp->sacked_out)
1994a47e5a98SIlpo Järvinen 		return tp->snd_una;
19956859d494SIlpo Järvinen 
19966859d494SIlpo Järvinen 	if (tp->highest_sack == NULL)
19976859d494SIlpo Järvinen 		return tp->snd_nxt;
19986859d494SIlpo Järvinen 
1999a47e5a98SIlpo Järvinen 	return TCP_SKB_CB(tp->highest_sack)->seq;
2000a47e5a98SIlpo Järvinen }
2001a47e5a98SIlpo Järvinen 
20026859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
20036859d494SIlpo Järvinen {
200450895b9dSEric Dumazet 	tcp_sk(sk)->highest_sack = skb_rb_next(skb);
20056859d494SIlpo Järvinen }
20066859d494SIlpo Järvinen 
20076859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
20086859d494SIlpo Järvinen {
20096859d494SIlpo Järvinen 	return tcp_sk(sk)->highest_sack;
20106859d494SIlpo Järvinen }
20116859d494SIlpo Järvinen 
20126859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk)
20136859d494SIlpo Järvinen {
201450895b9dSEric Dumazet 	tcp_sk(sk)->highest_sack = tcp_rtx_queue_head(sk);
20156859d494SIlpo Järvinen }
20166859d494SIlpo Järvinen 
20172b7cda9cSEric Dumazet /* Called when old skb is about to be deleted and replaced by new skb */
20182b7cda9cSEric Dumazet static inline void tcp_highest_sack_replace(struct sock *sk,
20196859d494SIlpo Järvinen 					    struct sk_buff *old,
20206859d494SIlpo Järvinen 					    struct sk_buff *new)
20216859d494SIlpo Järvinen {
20222b7cda9cSEric Dumazet 	if (old == tcp_highest_sack(sk))
20236859d494SIlpo Järvinen 		tcp_sk(sk)->highest_sack = new;
20246859d494SIlpo Järvinen }
20256859d494SIlpo Järvinen 
2026b1f0a0e9SFlorian Westphal /* This helper checks if socket has IP_TRANSPARENT set */
2027b1f0a0e9SFlorian Westphal static inline bool inet_sk_transparent(const struct sock *sk)
2028b1f0a0e9SFlorian Westphal {
2029b1f0a0e9SFlorian Westphal 	switch (sk->sk_state) {
2030b1f0a0e9SFlorian Westphal 	case TCP_TIME_WAIT:
2031b1f0a0e9SFlorian Westphal 		return inet_twsk(sk)->tw_transparent;
2032b1f0a0e9SFlorian Westphal 	case TCP_NEW_SYN_RECV:
2033b1f0a0e9SFlorian Westphal 		return inet_rsk(inet_reqsk(sk))->no_srccheck;
2034b1f0a0e9SFlorian Westphal 	}
20354bd0623fSEric Dumazet 	return inet_test_bit(TRANSPARENT, sk);
2036b1f0a0e9SFlorian Westphal }
2037b1f0a0e9SFlorian Westphal 
20385aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from
20395aa4b32fSAndreas Petlund  * increased latency). Used to trigger latency-reducing mechanisms.
20405aa4b32fSAndreas Petlund  */
2041a2a385d6SEric Dumazet static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
20425aa4b32fSAndreas Petlund {
20435aa4b32fSAndreas Petlund 	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
20445aa4b32fSAndreas Petlund }
20455aa4b32fSAndreas Petlund 
20461da177e4SLinus Torvalds /* /proc */
20471da177e4SLinus Torvalds enum tcp_seq_states {
20481da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
20491da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
20501da177e4SLinus Torvalds };
20511da177e4SLinus Torvalds 
205237d849bbSChristoph Hellwig void *tcp_seq_start(struct seq_file *seq, loff_t *pos);
205337d849bbSChristoph Hellwig void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
205437d849bbSChristoph Hellwig void tcp_seq_stop(struct seq_file *seq, void *v);
205573cb88ecSArjan van de Ven 
20561da177e4SLinus Torvalds struct tcp_seq_afinfo {
20571da177e4SLinus Torvalds 	sa_family_t			family;
20581da177e4SLinus Torvalds };
20591da177e4SLinus Torvalds 
20601da177e4SLinus Torvalds struct tcp_iter_state {
2061a4146b1bSDenis V. Lunev 	struct seq_net_private	p;
20621da177e4SLinus Torvalds 	enum tcp_seq_states	state;
20631da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
2064a7cb5a49SEric W. Biederman 	int			bucket, offset, sbucket, num;
2065a8b690f9STom Herbert 	loff_t			last_pos;
20661da177e4SLinus Torvalds };
20671da177e4SLinus Torvalds 
206820380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
2069c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops;
207020380731SArnaldo Carvalho de Melo 
20715c9f3023SJoe Perches void tcp_v4_destroy_sock(struct sock *sk);
207220380731SArnaldo Carvalho de Melo 
207328be6e07SEric Dumazet struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
2074c8f44affSMichał Mirosław 				netdev_features_t features);
2075d4546c25SDavid Miller struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb);
20765521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp4_gro_complete(struct sk_buff *skb, int thoff));
20775521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb));
20785521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp6_gro_complete(struct sk_buff *skb, int thoff));
20795521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp6_gro_receive(struct list_head *head, struct sk_buff *skb));
2080b1f2abcfSParav Pandit void tcp_gro_complete(struct sk_buff *skb);
208128850dc7SDaniel Borkmann 
20825c9f3023SJoe Perches void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
2083f4c50d99SHerbert Xu 
2084c9bee3b7SEric Dumazet static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
2085c9bee3b7SEric Dumazet {
20864979f2d9SNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
20871aeb87bcSEric Dumazet 	u32 val;
20881aeb87bcSEric Dumazet 
20891aeb87bcSEric Dumazet 	val = READ_ONCE(tp->notsent_lowat);
20901aeb87bcSEric Dumazet 
20911aeb87bcSEric Dumazet 	return val ?: READ_ONCE(net->ipv4.sysctl_tcp_notsent_lowat);
2092c9bee3b7SEric Dumazet }
2093c9bee3b7SEric Dumazet 
2094d3cd4924SEric Dumazet bool tcp_stream_memory_free(const struct sock *sk, int wake);
2095c9bee3b7SEric Dumazet 
209620380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
20975c9f3023SJoe Perches int tcp4_proc_init(void);
20985c9f3023SJoe Perches void tcp4_proc_exit(void);
209920380731SArnaldo Carvalho de Melo #endif
210020380731SArnaldo Carvalho de Melo 
2101ea3bea3aSEric Dumazet int tcp_rtx_synack(const struct sock *sk, struct request_sock *req);
21021fb6f159SOctavian Purdila int tcp_conn_request(struct request_sock_ops *rsk_ops,
21031fb6f159SOctavian Purdila 		     const struct tcp_request_sock_ops *af_ops,
21041fb6f159SOctavian Purdila 		     struct sock *sk, struct sk_buff *skb);
21055db92c99SOctavian Purdila 
2106cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */
2107cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops {
2108cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
2109b83e3debSEric Dumazet 	struct tcp_md5sig_key	*(*md5_lookup) (const struct sock *sk,
2110fd3a154aSEric Dumazet 						const struct sock *addr_sk);
2111cfb6eeb4SYOSHIFUJI Hideaki 	int		(*calc_md5_hash)(char *location,
211239f8e58eSEric Dumazet 					 const struct tcp_md5sig_key *md5,
2113318cf7aaSEric Dumazet 					 const struct sock *sk,
2114318cf7aaSEric Dumazet 					 const struct sk_buff *skb);
2115cfb6eeb4SYOSHIFUJI Hideaki 	int		(*md5_parse)(struct sock *sk,
21168917a777SIvan Delalande 				     int optname,
2117d4c19c49SChristoph Hellwig 				     sockptr_t optval,
2118cfb6eeb4SYOSHIFUJI Hideaki 				     int optlen);
2119cfb6eeb4SYOSHIFUJI Hideaki #endif
2120cfb6eeb4SYOSHIFUJI Hideaki };
2121cfb6eeb4SYOSHIFUJI Hideaki 
2122cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops {
21232aec4a29SOctavian Purdila 	u16 mss_clamp;
2124cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
2125b83e3debSEric Dumazet 	struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk,
2126fd3a154aSEric Dumazet 						 const struct sock *addr_sk);
2127e3afe7b7SJohn Dykstra 	int		(*calc_md5_hash) (char *location,
212839f8e58eSEric Dumazet 					  const struct tcp_md5sig_key *md5,
2129318cf7aaSEric Dumazet 					  const struct sock *sk,
2130318cf7aaSEric Dumazet 					  const struct sk_buff *skb);
2131cfb6eeb4SYOSHIFUJI Hideaki #endif
2132fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
21333f684b4bSEric Dumazet 	__u32 (*cookie_init_seq)(const struct sk_buff *skb,
2134fb7b37a7SOctavian Purdila 				 __u16 *mss);
2135fb7b37a7SOctavian Purdila #endif
21367ea851d1SFlorian Westphal 	struct dst_entry *(*route_req)(const struct sock *sk,
21377ea851d1SFlorian Westphal 				       struct sk_buff *skb,
21387ea851d1SFlorian Westphal 				       struct flowi *fl,
21397ea851d1SFlorian Westphal 				       struct request_sock *req);
214084b114b9SEric Dumazet 	u32 (*init_seq)(const struct sk_buff *skb);
21415d2ed052SEric Dumazet 	u32 (*init_ts_off)(const struct net *net, const struct sk_buff *skb);
21420f935dbeSEric Dumazet 	int (*send_synack)(const struct sock *sk, struct dst_entry *dst,
2143d6274bd8SOctavian Purdila 			   struct flowi *fl, struct request_sock *req,
2144dc6ef6beSEric Dumazet 			   struct tcp_fastopen_cookie *foc,
2145331fca43SMartin KaFai Lau 			   enum tcp_synack_type synack_type,
2146331fca43SMartin KaFai Lau 			   struct sk_buff *syn_skb);
2147cfb6eeb4SYOSHIFUJI Hideaki };
2148cfb6eeb4SYOSHIFUJI Hideaki 
214935b2c321SMat Martineau extern const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops;
215035b2c321SMat Martineau #if IS_ENABLED(CONFIG_IPV6)
215135b2c321SMat Martineau extern const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops;
215235b2c321SMat Martineau #endif
215335b2c321SMat Martineau 
2154fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
2155fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
21563f684b4bSEric Dumazet 					 const struct sock *sk, struct sk_buff *skb,
2157fb7b37a7SOctavian Purdila 					 __u16 *mss)
2158fb7b37a7SOctavian Purdila {
21593f684b4bSEric Dumazet 	tcp_synq_overflow(sk);
216002a1d6e7SEric Dumazet 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
21613f684b4bSEric Dumazet 	return ops->cookie_init_seq(skb, mss);
2162fb7b37a7SOctavian Purdila }
2163fb7b37a7SOctavian Purdila #else
2164fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
21653f684b4bSEric Dumazet 					 const struct sock *sk, struct sk_buff *skb,
2166fb7b37a7SOctavian Purdila 					 __u16 *mss)
2167fb7b37a7SOctavian Purdila {
2168fb7b37a7SOctavian Purdila 	return 0;
2169fb7b37a7SOctavian Purdila }
2170fb7b37a7SOctavian Purdila #endif
2171fb7b37a7SOctavian Purdila 
21725c9f3023SJoe Perches int tcpv4_offload_init(void);
217328850dc7SDaniel Borkmann 
21745c9f3023SJoe Perches void tcp_v4_init(void);
21755c9f3023SJoe Perches void tcp_init(void);
217620380731SArnaldo Carvalho de Melo 
2177659a8ad5SYuchung Cheng /* tcp_recovery.c */
2178d716bfdbSYuchung Cheng void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb);
21796ac06ecdSYuchung Cheng void tcp_newreno_mark_lost(struct sock *sk, bool snd_una_advanced);
2180b8fef65aSYuchung Cheng extern s32 tcp_rack_skb_timeout(struct tcp_sock *tp, struct sk_buff *skb,
2181b8fef65aSYuchung Cheng 				u32 reo_wnd);
218262d9f1a6SPengcheng Yang extern bool tcp_rack_mark_lost(struct sock *sk);
21831d0833dfSYuchung Cheng extern void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq,
21849a568de4SEric Dumazet 			     u64 xmit_time);
218557dde7f7SYuchung Cheng extern void tcp_rack_reo_timeout(struct sock *sk);
21861f255691SPriyaranjan Jha extern void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs);
2187659a8ad5SYuchung Cheng 
21881a91bb7cSMubashir Adnan Qureshi /* tcp_plb.c */
21891a91bb7cSMubashir Adnan Qureshi 
21901a91bb7cSMubashir Adnan Qureshi /*
21911a91bb7cSMubashir Adnan Qureshi  * Scaling factor for fractions in PLB. For example, tcp_plb_update_state
21921a91bb7cSMubashir Adnan Qureshi  * expects cong_ratio which represents fraction of traffic that experienced
21931a91bb7cSMubashir Adnan Qureshi  * congestion over a single RTT. In order to avoid floating point operations,
21941a91bb7cSMubashir Adnan Qureshi  * this fraction should be mapped to (1 << TCP_PLB_SCALE) and passed in.
21951a91bb7cSMubashir Adnan Qureshi  */
21961a91bb7cSMubashir Adnan Qureshi #define TCP_PLB_SCALE 8
21971a91bb7cSMubashir Adnan Qureshi 
21981a91bb7cSMubashir Adnan Qureshi /* State for PLB (Protective Load Balancing) for a single TCP connection. */
21991a91bb7cSMubashir Adnan Qureshi struct tcp_plb_state {
22001a91bb7cSMubashir Adnan Qureshi 	u8	consec_cong_rounds:5, /* consecutive congested rounds */
22011a91bb7cSMubashir Adnan Qureshi 		unused:3;
22021a91bb7cSMubashir Adnan Qureshi 	u32	pause_until; /* jiffies32 when PLB can resume rerouting */
22031a91bb7cSMubashir Adnan Qureshi };
22041a91bb7cSMubashir Adnan Qureshi 
22051a91bb7cSMubashir Adnan Qureshi static inline void tcp_plb_init(const struct sock *sk,
22061a91bb7cSMubashir Adnan Qureshi 				struct tcp_plb_state *plb)
22071a91bb7cSMubashir Adnan Qureshi {
22081a91bb7cSMubashir Adnan Qureshi 	plb->consec_cong_rounds = 0;
22091a91bb7cSMubashir Adnan Qureshi 	plb->pause_until = 0;
22101a91bb7cSMubashir Adnan Qureshi }
22111a91bb7cSMubashir Adnan Qureshi void tcp_plb_update_state(const struct sock *sk, struct tcp_plb_state *plb,
22121a91bb7cSMubashir Adnan Qureshi 			  const int cong_ratio);
22131a91bb7cSMubashir Adnan Qureshi void tcp_plb_check_rehash(struct sock *sk, struct tcp_plb_state *plb);
22141a91bb7cSMubashir Adnan Qureshi void tcp_plb_update_state_upon_rto(struct sock *sk, struct tcp_plb_state *plb);
22151a91bb7cSMubashir Adnan Qureshi 
2216e1a10ef7SNeal Cardwell /* At how many usecs into the future should the RTO fire? */
2217e1a10ef7SNeal Cardwell static inline s64 tcp_rto_delta_us(const struct sock *sk)
2218e1a10ef7SNeal Cardwell {
221975c119afSEric Dumazet 	const struct sk_buff *skb = tcp_rtx_queue_head(sk);
2220e1a10ef7SNeal Cardwell 	u32 rto = inet_csk(sk)->icsk_rto;
22212fd66ffbSEric Dumazet 	u64 rto_time_stamp_us = tcp_skb_timestamp_us(skb) + jiffies_to_usecs(rto);
2222e1a10ef7SNeal Cardwell 
2223e1a10ef7SNeal Cardwell 	return rto_time_stamp_us - tcp_sk(sk)->tcp_mstamp;
2224e1a10ef7SNeal Cardwell }
2225e1a10ef7SNeal Cardwell 
2226e25f866fSCong Wang /*
2227e25f866fSCong Wang  * Save and compile IPv4 options, return a pointer to it
2228e25f866fSCong Wang  */
222991ed1e66SPaolo Abeni static inline struct ip_options_rcu *tcp_v4_save_options(struct net *net,
223091ed1e66SPaolo Abeni 							 struct sk_buff *skb)
2231e25f866fSCong Wang {
2232e25f866fSCong Wang 	const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
2233e25f866fSCong Wang 	struct ip_options_rcu *dopt = NULL;
2234e25f866fSCong Wang 
2235461b74c3SCong Wang 	if (opt->optlen) {
2236e25f866fSCong Wang 		int opt_size = sizeof(*dopt) + opt->optlen;
2237e25f866fSCong Wang 
2238e25f866fSCong Wang 		dopt = kmalloc(opt_size, GFP_ATOMIC);
223991ed1e66SPaolo Abeni 		if (dopt && __ip_options_echo(net, &dopt->opt, skb, opt)) {
2240e25f866fSCong Wang 			kfree(dopt);
2241e25f866fSCong Wang 			dopt = NULL;
2242e25f866fSCong Wang 		}
2243e25f866fSCong Wang 	}
2244e25f866fSCong Wang 	return dopt;
2245e25f866fSCong Wang }
2246e25f866fSCong Wang 
224798781965SEric Dumazet /* locally generated TCP pure ACKs have skb->truesize == 2
224898781965SEric Dumazet  * (check tcp_send_ack() in net/ipv4/tcp_output.c )
224998781965SEric Dumazet  * This is much faster than dissecting the packet to find out.
225098781965SEric Dumazet  * (Think of GRE encapsulations, IPv4, IPv6, ...)
225198781965SEric Dumazet  */
225298781965SEric Dumazet static inline bool skb_is_tcp_pure_ack(const struct sk_buff *skb)
225398781965SEric Dumazet {
225498781965SEric Dumazet 	return skb->truesize == 2;
225598781965SEric Dumazet }
225698781965SEric Dumazet 
225798781965SEric Dumazet static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
225898781965SEric Dumazet {
225998781965SEric Dumazet 	skb->truesize = 2;
226098781965SEric Dumazet }
226198781965SEric Dumazet 
2262473bd239STom Herbert static inline int tcp_inq(struct sock *sk)
2263473bd239STom Herbert {
2264473bd239STom Herbert 	struct tcp_sock *tp = tcp_sk(sk);
2265473bd239STom Herbert 	int answ;
2266473bd239STom Herbert 
2267473bd239STom Herbert 	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
2268473bd239STom Herbert 		answ = 0;
2269473bd239STom Herbert 	} else if (sock_flag(sk, SOCK_URGINLINE) ||
2270473bd239STom Herbert 		   !tp->urg_data ||
2271473bd239STom Herbert 		   before(tp->urg_seq, tp->copied_seq) ||
2272473bd239STom Herbert 		   !before(tp->urg_seq, tp->rcv_nxt)) {
2273473bd239STom Herbert 
2274473bd239STom Herbert 		answ = tp->rcv_nxt - tp->copied_seq;
2275473bd239STom Herbert 
2276473bd239STom Herbert 		/* Subtract 1, if FIN was received */
2277473bd239STom Herbert 		if (answ && sock_flag(sk, SOCK_DONE))
2278473bd239STom Herbert 			answ--;
2279473bd239STom Herbert 	} else {
2280473bd239STom Herbert 		answ = tp->urg_seq - tp->copied_seq;
2281473bd239STom Herbert 	}
2282473bd239STom Herbert 
2283473bd239STom Herbert 	return answ;
2284473bd239STom Herbert }
2285473bd239STom Herbert 
228632035585STom Herbert int tcp_peek_len(struct socket *sock);
228732035585STom Herbert 
2288a44d6eacSMartin KaFai Lau static inline void tcp_segs_in(struct tcp_sock *tp, const struct sk_buff *skb)
2289a44d6eacSMartin KaFai Lau {
2290a44d6eacSMartin KaFai Lau 	u16 segs_in;
2291a44d6eacSMartin KaFai Lau 
2292a44d6eacSMartin KaFai Lau 	segs_in = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
22930307a0b7SEric Dumazet 
22940307a0b7SEric Dumazet 	/* We update these fields while other threads might
22950307a0b7SEric Dumazet 	 * read them from tcp_get_info()
22960307a0b7SEric Dumazet 	 */
22970307a0b7SEric Dumazet 	WRITE_ONCE(tp->segs_in, tp->segs_in + segs_in);
2298a44d6eacSMartin KaFai Lau 	if (skb->len > tcp_hdrlen(skb))
22990307a0b7SEric Dumazet 		WRITE_ONCE(tp->data_segs_in, tp->data_segs_in + segs_in);
2300a44d6eacSMartin KaFai Lau }
2301a44d6eacSMartin KaFai Lau 
23029caad864SEric Dumazet /*
23039caad864SEric Dumazet  * TCP listen path runs lockless.
23049caad864SEric Dumazet  * We forced "struct sock" to be const qualified to make sure
23059caad864SEric Dumazet  * we don't modify one of its field by mistake.
23069caad864SEric Dumazet  * Here, we increment sk_drops which is an atomic_t, so we can safely
23079caad864SEric Dumazet  * make sock writable again.
23089caad864SEric Dumazet  */
23099caad864SEric Dumazet static inline void tcp_listendrop(const struct sock *sk)
23109caad864SEric Dumazet {
23119caad864SEric Dumazet 	atomic_inc(&((struct sock *)sk)->sk_drops);
231202a1d6e7SEric Dumazet 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
23139caad864SEric Dumazet }
23149caad864SEric Dumazet 
2315218af599SEric Dumazet enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer);
2316218af599SEric Dumazet 
2317734942ccSDave Watson /*
2318734942ccSDave Watson  * Interface for adding Upper Level Protocols over TCP
2319734942ccSDave Watson  */
2320734942ccSDave Watson 
2321734942ccSDave Watson #define TCP_ULP_NAME_MAX	16
2322734942ccSDave Watson #define TCP_ULP_MAX		128
2323734942ccSDave Watson #define TCP_ULP_BUF_MAX		(TCP_ULP_NAME_MAX*TCP_ULP_MAX)
2324734942ccSDave Watson 
2325734942ccSDave Watson struct tcp_ulp_ops {
2326734942ccSDave Watson 	struct list_head	list;
2327734942ccSDave Watson 
2328734942ccSDave Watson 	/* initialize ulp */
2329734942ccSDave Watson 	int (*init)(struct sock *sk);
233095fa1454SJohn Fastabend 	/* update ulp */
233133bfe20dSJohn Fastabend 	void (*update)(struct sock *sk, struct proto *p,
233233bfe20dSJohn Fastabend 		       void (*write_space)(struct sock *sk));
2333734942ccSDave Watson 	/* cleanup ulp */
2334734942ccSDave Watson 	void (*release)(struct sock *sk);
233561723b39SDavide Caratti 	/* diagnostic */
233661723b39SDavide Caratti 	int (*get_info)(const struct sock *sk, struct sk_buff *skb);
233761723b39SDavide Caratti 	size_t (*get_info_size)(const struct sock *sk);
233813230593SMat Martineau 	/* clone ulp */
233913230593SMat Martineau 	void (*clone)(const struct request_sock *req, struct sock *newsk,
234013230593SMat Martineau 		      const gfp_t priority);
2341734942ccSDave Watson 
2342734942ccSDave Watson 	char		name[TCP_ULP_NAME_MAX];
2343734942ccSDave Watson 	struct module	*owner;
2344734942ccSDave Watson };
2345734942ccSDave Watson int tcp_register_ulp(struct tcp_ulp_ops *type);
2346734942ccSDave Watson void tcp_unregister_ulp(struct tcp_ulp_ops *type);
2347734942ccSDave Watson int tcp_set_ulp(struct sock *sk, const char *name);
2348734942ccSDave Watson void tcp_get_available_ulp(char *buf, size_t len);
2349734942ccSDave Watson void tcp_cleanup_ulp(struct sock *sk);
235033bfe20dSJohn Fastabend void tcp_update_ulp(struct sock *sk, struct proto *p,
235133bfe20dSJohn Fastabend 		    void (*write_space)(struct sock *sk));
2352734942ccSDave Watson 
2353037b0b86SDaniel Borkmann #define MODULE_ALIAS_TCP_ULP(name)				\
2354037b0b86SDaniel Borkmann 	__MODULE_INFO(alias, alias_userspace, name);		\
2355037b0b86SDaniel Borkmann 	__MODULE_INFO(alias, alias_tcp_ulp, "tcp-ulp-" name)
2356037b0b86SDaniel Borkmann 
235788759609SCong Wang #ifdef CONFIG_NET_SOCK_MSG
2358604326b4SDaniel Borkmann struct sk_msg;
2359604326b4SDaniel Borkmann struct sk_psock;
2360604326b4SDaniel Borkmann 
236188759609SCong Wang #ifdef CONFIG_BPF_SYSCALL
236251e0158aSCong Wang int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
2363f747632bSLorenz Bauer void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
236488759609SCong Wang #endif /* CONFIG_BPF_SYSCALL */
2365f747632bSLorenz Bauer 
2366e5c6de5fSJohn Fastabend #ifdef CONFIG_INET
2367e5c6de5fSJohn Fastabend void tcp_eat_skb(struct sock *sk, struct sk_buff *skb);
2368e5c6de5fSJohn Fastabend #else
2369e5c6de5fSJohn Fastabend static inline void tcp_eat_skb(struct sock *sk, struct sk_buff *skb)
2370e5c6de5fSJohn Fastabend {
2371e5c6de5fSJohn Fastabend }
2372e5c6de5fSJohn Fastabend #endif
2373e5c6de5fSJohn Fastabend 
2374a351d608SPengcheng Yang int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
2375a351d608SPengcheng Yang 			  struct sk_msg *msg, u32 bytes, int flags);
23765da00404SLorenz Bauer #endif /* CONFIG_NET_SOCK_MSG */
2377604326b4SDaniel Borkmann 
237888759609SCong Wang #if !defined(CONFIG_BPF_SYSCALL) || !defined(CONFIG_NET_SOCK_MSG)
237988759609SCong Wang static inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
238088759609SCong Wang {
238188759609SCong Wang }
238288759609SCong Wang #endif
238388759609SCong Wang 
23840813a841SMartin KaFai Lau #ifdef CONFIG_CGROUP_BPF
23850813a841SMartin KaFai Lau static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
23860813a841SMartin KaFai Lau 				      struct sk_buff *skb,
23870813a841SMartin KaFai Lau 				      unsigned int end_offset)
23880813a841SMartin KaFai Lau {
23890813a841SMartin KaFai Lau 	skops->skb = skb;
23900813a841SMartin KaFai Lau 	skops->skb_data_end = skb->data + end_offset;
23910813a841SMartin KaFai Lau }
23920813a841SMartin KaFai Lau #else
23930813a841SMartin KaFai Lau static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
23940813a841SMartin KaFai Lau 				      struct sk_buff *skb,
23950813a841SMartin KaFai Lau 				      unsigned int end_offset)
23960813a841SMartin KaFai Lau {
23970813a841SMartin KaFai Lau }
23980813a841SMartin KaFai Lau #endif
23990813a841SMartin KaFai Lau 
240040304b2aSLawrence Brakmo /* Call BPF_SOCK_OPS program that returns an int. If the return value
240140304b2aSLawrence Brakmo  * is < 0, then the BPF op failed (for example if the loaded BPF
240240304b2aSLawrence Brakmo  * program does not support the chosen operation or there is no BPF
240340304b2aSLawrence Brakmo  * program loaded).
240440304b2aSLawrence Brakmo  */
240540304b2aSLawrence Brakmo #ifdef CONFIG_BPF
2406de525be2SLawrence Brakmo static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
240740304b2aSLawrence Brakmo {
240840304b2aSLawrence Brakmo 	struct bpf_sock_ops_kern sock_ops;
240940304b2aSLawrence Brakmo 	int ret;
241040304b2aSLawrence Brakmo 
2411b73042b8SLawrence Brakmo 	memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
2412f19397a5SLawrence Brakmo 	if (sk_fullsock(sk)) {
2413f19397a5SLawrence Brakmo 		sock_ops.is_fullsock = 1;
241440304b2aSLawrence Brakmo 		sock_owned_by_me(sk);
2415f19397a5SLawrence Brakmo 	}
241640304b2aSLawrence Brakmo 
241740304b2aSLawrence Brakmo 	sock_ops.sk = sk;
241840304b2aSLawrence Brakmo 	sock_ops.op = op;
2419de525be2SLawrence Brakmo 	if (nargs > 0)
2420de525be2SLawrence Brakmo 		memcpy(sock_ops.args, args, nargs * sizeof(*args));
242140304b2aSLawrence Brakmo 
242240304b2aSLawrence Brakmo 	ret = BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
242340304b2aSLawrence Brakmo 	if (ret == 0)
242440304b2aSLawrence Brakmo 		ret = sock_ops.reply;
242540304b2aSLawrence Brakmo 	else
242640304b2aSLawrence Brakmo 		ret = -1;
242740304b2aSLawrence Brakmo 	return ret;
242840304b2aSLawrence Brakmo }
2429de525be2SLawrence Brakmo 
2430de525be2SLawrence Brakmo static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
2431de525be2SLawrence Brakmo {
2432de525be2SLawrence Brakmo 	u32 args[2] = {arg1, arg2};
2433de525be2SLawrence Brakmo 
2434de525be2SLawrence Brakmo 	return tcp_call_bpf(sk, op, 2, args);
2435de525be2SLawrence Brakmo }
2436de525be2SLawrence Brakmo 
2437de525be2SLawrence Brakmo static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
2438de525be2SLawrence Brakmo 				    u32 arg3)
2439de525be2SLawrence Brakmo {
2440de525be2SLawrence Brakmo 	u32 args[3] = {arg1, arg2, arg3};
2441de525be2SLawrence Brakmo 
2442de525be2SLawrence Brakmo 	return tcp_call_bpf(sk, op, 3, args);
2443de525be2SLawrence Brakmo }
2444de525be2SLawrence Brakmo 
244540304b2aSLawrence Brakmo #else
2446de525be2SLawrence Brakmo static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
244740304b2aSLawrence Brakmo {
244840304b2aSLawrence Brakmo 	return -EPERM;
244940304b2aSLawrence Brakmo }
2450de525be2SLawrence Brakmo 
2451de525be2SLawrence Brakmo static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
2452de525be2SLawrence Brakmo {
2453de525be2SLawrence Brakmo 	return -EPERM;
2454de525be2SLawrence Brakmo }
2455de525be2SLawrence Brakmo 
2456de525be2SLawrence Brakmo static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
2457de525be2SLawrence Brakmo 				    u32 arg3)
2458de525be2SLawrence Brakmo {
2459de525be2SLawrence Brakmo 	return -EPERM;
2460de525be2SLawrence Brakmo }
2461de525be2SLawrence Brakmo 
246240304b2aSLawrence Brakmo #endif
246340304b2aSLawrence Brakmo 
24648550f328SLawrence Brakmo static inline u32 tcp_timeout_init(struct sock *sk)
24658550f328SLawrence Brakmo {
24668550f328SLawrence Brakmo 	int timeout;
24678550f328SLawrence Brakmo 
2468de525be2SLawrence Brakmo 	timeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL);
24698550f328SLawrence Brakmo 
24708550f328SLawrence Brakmo 	if (timeout <= 0)
24718550f328SLawrence Brakmo 		timeout = TCP_TIMEOUT_INIT;
24725903123fSAkhmat Karakotov 	return min_t(int, timeout, TCP_RTO_MAX);
24738550f328SLawrence Brakmo }
24748550f328SLawrence Brakmo 
247513d3b1ebSLawrence Brakmo static inline u32 tcp_rwnd_init_bpf(struct sock *sk)
247613d3b1ebSLawrence Brakmo {
247713d3b1ebSLawrence Brakmo 	int rwnd;
247813d3b1ebSLawrence Brakmo 
2479de525be2SLawrence Brakmo 	rwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL);
248013d3b1ebSLawrence Brakmo 
248113d3b1ebSLawrence Brakmo 	if (rwnd < 0)
248213d3b1ebSLawrence Brakmo 		rwnd = 0;
248313d3b1ebSLawrence Brakmo 	return rwnd;
248413d3b1ebSLawrence Brakmo }
248591b5b21cSLawrence Brakmo 
248691b5b21cSLawrence Brakmo static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk)
248791b5b21cSLawrence Brakmo {
2488de525be2SLawrence Brakmo 	return (tcp_call_bpf(sk, BPF_SOCK_OPS_NEEDS_ECN, 0, NULL) == 1);
248991b5b21cSLawrence Brakmo }
249060e2a778SUrsula Braun 
249123729ff2SStanislav Fomichev static inline void tcp_bpf_rtt(struct sock *sk)
249223729ff2SStanislav Fomichev {
2493bef8e263SArnd Bergmann 	if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_RTT_CB_FLAG))
249423729ff2SStanislav Fomichev 		tcp_call_bpf(sk, BPF_SOCK_OPS_RTT_CB, 0, NULL);
249523729ff2SStanislav Fomichev }
249623729ff2SStanislav Fomichev 
249760e2a778SUrsula Braun #if IS_ENABLED(CONFIG_SMC)
249860e2a778SUrsula Braun extern struct static_key_false tcp_have_smc;
249960e2a778SUrsula Braun #endif
25006dac1523SIlya Lesokhin 
25016dac1523SIlya Lesokhin #if IS_ENABLED(CONFIG_TLS_DEVICE)
25026dac1523SIlya Lesokhin void clean_acked_data_enable(struct inet_connection_sock *icsk,
25036dac1523SIlya Lesokhin 			     void (*cad)(struct sock *sk, u32 ack_seq));
25046dac1523SIlya Lesokhin void clean_acked_data_disable(struct inet_connection_sock *icsk);
2505494bc1d2SJakub Kicinski void clean_acked_data_flush(void);
25066dac1523SIlya Lesokhin #endif
25076dac1523SIlya Lesokhin 
2508a842fe14SEric Dumazet DECLARE_STATIC_KEY_FALSE(tcp_tx_delay_enabled);
2509a842fe14SEric Dumazet static inline void tcp_add_tx_delay(struct sk_buff *skb,
2510a842fe14SEric Dumazet 				    const struct tcp_sock *tp)
2511a842fe14SEric Dumazet {
2512a842fe14SEric Dumazet 	if (static_branch_unlikely(&tcp_tx_delay_enabled))
2513a842fe14SEric Dumazet 		skb->skb_mstamp_ns += (u64)tp->tcp_tx_delay * NSEC_PER_USEC;
2514a842fe14SEric Dumazet }
2515a842fe14SEric Dumazet 
2516d6fb396cSEric Dumazet /* Compute Earliest Departure Time for some control packets
2517d6fb396cSEric Dumazet  * like ACK or RST for TIME_WAIT or non ESTABLISHED sockets.
2518d6fb396cSEric Dumazet  */
2519d6fb396cSEric Dumazet static inline u64 tcp_transmit_time(const struct sock *sk)
2520a842fe14SEric Dumazet {
2521a842fe14SEric Dumazet 	if (static_branch_unlikely(&tcp_tx_delay_enabled)) {
2522a842fe14SEric Dumazet 		u32 delay = (sk->sk_state == TCP_TIME_WAIT) ?
2523a842fe14SEric Dumazet 			tcp_twsk(sk)->tw_tx_delay : tcp_sk(sk)->tcp_tx_delay;
2524a842fe14SEric Dumazet 
2525d6fb396cSEric Dumazet 		return tcp_clock_ns() + (u64)delay * NSEC_PER_USEC;
2526a842fe14SEric Dumazet 	}
2527d6fb396cSEric Dumazet 	return 0;
2528a842fe14SEric Dumazet }
2529a842fe14SEric Dumazet 
25301da177e4SLinus Torvalds #endif	/* _TCP_H */
2531