xref: /linux/include/net/tcp.h (revision a351d6087bf7d3d8440d58d3bf244ec64b89394a)
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_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1671da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1681da177e4SLinus Torvalds 					 * after this time. It should be equal
1691da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1701da177e4SLinus Torvalds 					 * to provide reliability equal to one
1711da177e4SLinus Torvalds 					 * provided by timewait state.
1721da177e4SLinus Torvalds 					 */
1731da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1741da177e4SLinus Torvalds 					 * timestamps. It must be less than
1751da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1761da177e4SLinus Torvalds 					 */
1771da177e4SLinus Torvalds /*
1781da177e4SLinus Torvalds  *	TCP option
1791da177e4SLinus Torvalds  */
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1821da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1831da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1841da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1851da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1861da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1871da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
188cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
189c74a39c8SMat Martineau #define TCPOPT_MPTCP		30	/* Multipath TCP (RFC6824) */
1907f9b838bSDaniel Lee #define TCPOPT_FASTOPEN		34	/* Fast open (RFC7413) */
1912100c8d2SYuchung Cheng #define TCPOPT_EXP		254	/* Experimental */
1922100c8d2SYuchung Cheng /* Magic number to be after the option value for sharing TCP
1932100c8d2SYuchung Cheng  * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
1942100c8d2SYuchung Cheng  */
1952100c8d2SYuchung Cheng #define TCPOPT_FASTOPEN_MAGIC	0xF989
19660e2a778SUrsula Braun #define TCPOPT_SMC_MAGIC	0xE2D4C3D9
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /*
1991da177e4SLinus Torvalds  *     TCP option lengths
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds #define TCPOLEN_MSS            4
2031da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
2041da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
2051da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
206cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG         18
2077f9b838bSDaniel Lee #define TCPOLEN_FASTOPEN_BASE  2
2082100c8d2SYuchung Cheng #define TCPOLEN_EXP_FASTOPEN_BASE  4
20960e2a778SUrsula Braun #define TCPOLEN_EXP_SMC_BASE   6
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* But this is what stacks really send out. */
2121da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
2131da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
2141da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
2151da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
2161da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
2171da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
218cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED		20
21933ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED		4
22060e2a778SUrsula Braun #define TCPOLEN_EXP_SMC_BASE_ALIGNED	8
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /* Flags in tp->nonagle */
2231da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
2241da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
225caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
2261da177e4SLinus Torvalds 
22736e31b0aSAndreas Petlund /* TCP thin-stream limits */
22836e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
22936e31b0aSAndreas Petlund 
23021603fc4SJörg Thalheim /* TCP initial congestion window as per rfc6928 */
231442b9635SDavid S. Miller #define TCP_INIT_CWND		10
232442b9635SDavid S. Miller 
233cf60af03SYuchung Cheng /* Bit Flags for sysctl_tcp_fastopen */
234cf60af03SYuchung Cheng #define	TFO_CLIENT_ENABLE	1
23510467163SJerry Chu #define	TFO_SERVER_ENABLE	2
23667da22d2SYuchung Cheng #define	TFO_CLIENT_NO_COOKIE	4	/* Data in SYN w/o cookie option */
237cf60af03SYuchung Cheng 
23810467163SJerry Chu /* Accept SYN data w/o any cookie option */
23910467163SJerry Chu #define	TFO_SERVER_COOKIE_NOT_REQD	0x200
24010467163SJerry Chu 
24110467163SJerry Chu /* Force enable TFO on all listeners, i.e., not requiring the
242cebc5cbaSYuchung Cheng  * TCP_FASTOPEN socket option.
24310467163SJerry Chu  */
24410467163SJerry Chu #define	TFO_SERVER_WO_SOCKOPT1	0x400
24510467163SJerry Chu 
246295ff7edSArnaldo Carvalho de Melo 
2471da177e4SLinus Torvalds /* sysctl variables for tcp */
2481da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
249a4fe34bfSEric W. Biederman extern long sysctl_tcp_mem[3];
250e20223f1SEric Dumazet 
251a0370b3fSYuchung Cheng #define TCP_RACK_LOSS_DETECTION  0x1 /* Use RACK to detect losses */
2521f255691SPriyaranjan Jha #define TCP_RACK_STATIC_REO_WND  0x2 /* Use static RACK reo wnd */
25320b654dfSYuchung Cheng #define TCP_RACK_NO_DUPTHRESH    0x4 /* Do not use DUPACK threshold in RACK */
254a0370b3fSYuchung Cheng 
2558d987e5cSEric Dumazet extern atomic_long_t tcp_memory_allocated;
2560defbb0aSEric Dumazet DECLARE_PER_CPU(int, tcp_memory_per_cpu_fw_alloc);
2570defbb0aSEric Dumazet 
2581748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated;
25906044751SEric Dumazet extern unsigned long tcp_memory_pressure;
2601da177e4SLinus Torvalds 
261b8da51ebSEric Dumazet /* optimized version of sk_under_memory_pressure() for TCP sockets */
262b8da51ebSEric Dumazet static inline bool tcp_under_memory_pressure(const struct sock *sk)
263b8da51ebSEric Dumazet {
264baac50bbSJohannes Weiner 	if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
265baac50bbSJohannes Weiner 	    mem_cgroup_under_socket_pressure(sk->sk_memcg))
266e805605cSJohannes Weiner 		return true;
267b8da51ebSEric Dumazet 
2681f142c17SEric Dumazet 	return READ_ONCE(tcp_memory_pressure);
269b8da51ebSEric Dumazet }
2701da177e4SLinus Torvalds /*
2711da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
2721da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
2731da177e4SLinus Torvalds  */
2741da177e4SLinus Torvalds 
275a2a385d6SEric Dumazet static inline bool before(__u32 seq1, __u32 seq2)
2761da177e4SLinus Torvalds {
2770d630cc0SGerrit Renker         return (__s32)(seq1-seq2) < 0;
2781da177e4SLinus Torvalds }
2799a036b9cSGerrit Renker #define after(seq2, seq1) 	before(seq1, seq2)
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
282a2a385d6SEric Dumazet static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
287efcdbf24SArun Sharma static inline bool tcp_out_of_memory(struct sock *sk)
288efcdbf24SArun Sharma {
289efcdbf24SArun Sharma 	if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
290efcdbf24SArun Sharma 	    sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
291efcdbf24SArun Sharma 		return true;
292efcdbf24SArun Sharma 	return false;
293efcdbf24SArun Sharma }
294efcdbf24SArun Sharma 
29503271f3aSTalal Ahmad static inline void tcp_wmem_free_skb(struct sock *sk, struct sk_buff *skb)
29603271f3aSTalal Ahmad {
29703271f3aSTalal Ahmad 	sk_wmem_queued_add(sk, -skb->truesize);
2989b65b17dSTalal Ahmad 	if (!skb_zcopy_pure(skb))
29903271f3aSTalal Ahmad 		sk_mem_uncharge(sk, skb->truesize);
3009b65b17dSTalal Ahmad 	else
3019b65b17dSTalal Ahmad 		sk_mem_uncharge(sk, SKB_TRUESIZE(skb_end_offset(skb)));
30203271f3aSTalal Ahmad 	__kfree_skb(skb);
30303271f3aSTalal Ahmad }
30403271f3aSTalal Ahmad 
305a6c5ea4cSEric Dumazet void sk_forced_mem_schedule(struct sock *sk, int size);
306a6c5ea4cSEric Dumazet 
3075c9f3023SJoe Perches bool tcp_check_oom(struct sock *sk, int shift);
308efcdbf24SArun Sharma 
309a0f82f64SFlorian Westphal 
3101da177e4SLinus Torvalds extern struct proto tcp_prot;
3111da177e4SLinus Torvalds 
31257ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
31313415e46SEric Dumazet #define __TCP_INC_STATS(net, field)	__SNMP_INC_STATS((net)->mib.tcp_statistics, field)
31457ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
315aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
3161da177e4SLinus Torvalds 
3175c9f3023SJoe Perches void tcp_tasklet_init(void);
31846d3ceabSEric Dumazet 
31932bbd879SStefano Brivio int tcp_v4_err(struct sk_buff *skb, u32);
3201da177e4SLinus Torvalds 
3215c9f3023SJoe Perches void tcp_shutdown(struct sock *sk, int how);
3221da177e4SLinus Torvalds 
3237487449cSPaolo Abeni int tcp_v4_early_demux(struct sk_buff *skb);
3245c9f3023SJoe Perches int tcp_v4_rcv(struct sk_buff *skb);
3251da177e4SLinus Torvalds 
32627728ba8SEric Dumazet void tcp_remove_empty_skb(struct sock *sk);
3275c9f3023SJoe Perches int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
3281b784140SYing Xue int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
329306b13ebSTom Herbert int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size);
3303242abebSBenjamin Hesmans int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, int *copied,
3313242abebSBenjamin Hesmans 			 size_t size, struct ubuf_info *uarg);
3325c9f3023SJoe Perches int tcp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
3335c9f3023SJoe Perches 		 int flags);
334306b13ebSTom Herbert int tcp_sendpage_locked(struct sock *sk, struct page *page, int offset,
335306b13ebSTom Herbert 			size_t size, int flags);
336e3b5616aSDave Watson ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
337e3b5616aSDave Watson 		 size_t size, int flags);
33835b2c321SMat Martineau int tcp_send_mss(struct sock *sk, int *size_goal, int flags);
33935b2c321SMat Martineau void tcp_push(struct sock *sk, int flags, int mss_now, int nonagle,
34035b2c321SMat Martineau 	      int size_goal);
3415c9f3023SJoe Perches void tcp_release_cb(struct sock *sk);
3425c9f3023SJoe Perches void tcp_wfree(struct sk_buff *skb);
3435c9f3023SJoe Perches void tcp_write_timer_handler(struct sock *sk);
3445c9f3023SJoe Perches void tcp_delack_timer_handler(struct sock *sk);
3455c9f3023SJoe Perches int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
34672ab4a86SEric Dumazet int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb);
3473d97d88eSYafang Shao void tcp_rcv_established(struct sock *sk, struct sk_buff *skb);
3485c9f3023SJoe Perches void tcp_rcv_space_adjust(struct sock *sk);
3495c9f3023SJoe Perches int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
3505c9f3023SJoe Perches void tcp_twsk_destructor(struct sock *sk);
351edc12f03SKuniyuki Iwashima void tcp_twsk_purge(struct list_head *net_exit_list, int family);
3525c9f3023SJoe Perches ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
35353d3176bSChangli Gao 			struct pipe_inode_info *pipe, size_t len,
35453d3176bSChangli Gao 			unsigned int flags);
355f8dd3b8dSEric Dumazet struct sk_buff *tcp_stream_alloc_skb(struct sock *sk, int size, gfp_t gfp,
356f8dd3b8dSEric Dumazet 				     bool force_schedule);
3579c55e01cSJens Axboe 
358a0496ef2SYuchung Cheng void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks);
359463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
360463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3611da177e4SLinus Torvalds {
362463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
363fc6415bcSDavid S. Miller 
364463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
365463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
366463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3671da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
368463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
369fc6415bcSDavid S. Miller 		} else
370463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
374bdf1ee5dSIlpo Järvinen #define	TCP_ECN_OK		1
375bdf1ee5dSIlpo Järvinen #define	TCP_ECN_QUEUE_CWR	2
376bdf1ee5dSIlpo Järvinen #define	TCP_ECN_DEMAND_CWR	4
3777a269ffaSEric Dumazet #define	TCP_ECN_SEEN		8
378bdf1ee5dSIlpo Järvinen 
379fd2c3ef7SEric Dumazet enum tcp_tw_status {
3801da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
3811da177e4SLinus Torvalds 	TCP_TW_RST = 1,
3821da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
3831da177e4SLinus Torvalds 	TCP_TW_SYN = 3
3841da177e4SLinus Torvalds };
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 
3875c9f3023SJoe Perches enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
3881da177e4SLinus Torvalds 					      struct sk_buff *skb,
3898feaf0c0SArnaldo Carvalho de Melo 					      const struct tcphdr *th);
3905c9f3023SJoe Perches struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
391e0f9759fSEric Dumazet 			   struct request_sock *req, bool fastopen,
392e0f9759fSEric Dumazet 			   bool *lost_race);
3935c9f3023SJoe Perches int tcp_child_process(struct sock *parent, struct sock *child,
3941da177e4SLinus Torvalds 		      struct sk_buff *skb);
3955ae344c9SNeal Cardwell void tcp_enter_loss(struct sock *sk);
3967e901ee7SYuchung Cheng void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int newly_lost, int flag);
3975c9f3023SJoe Perches void tcp_clear_retrans(struct tcp_sock *tp);
3985c9f3023SJoe Perches void tcp_update_metrics(struct sock *sk);
3995c9f3023SJoe Perches void tcp_init_metrics(struct sock *sk);
4005c9f3023SJoe Perches void tcp_metrics_init(void);
401d82bae12SSoheil Hassas Yeganeh bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst);
40277c3c956SPaolo Abeni void __tcp_close(struct sock *sk, long timeout);
4035c9f3023SJoe Perches void tcp_close(struct sock *sk, long timeout);
4045c9f3023SJoe Perches void tcp_init_sock(struct sock *sk);
40572be0fe6SMartin KaFai Lau void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb);
406a11e1d43SLinus Torvalds __poll_t tcp_poll(struct file *file, struct socket *sock,
407a11e1d43SLinus Torvalds 		      struct poll_table_struct *wait);
408273b7f0fSMartin KaFai Lau int do_tcp_getsockopt(struct sock *sk, int level,
409273b7f0fSMartin KaFai Lau 		      int optname, sockptr_t optval, sockptr_t optlen);
4105c9f3023SJoe Perches int tcp_getsockopt(struct sock *sk, int level, int optname,
4113fdadf7dSDmitry Mishin 		   char __user *optval, int __user *optlen);
4129cacf81fSStanislav Fomichev bool tcp_bpf_bypass_getsockopt(int level, int optname);
4130c751f70SMartin KaFai Lau int do_tcp_setsockopt(struct sock *sk, int level, int optname,
4140c751f70SMartin KaFai Lau 		      sockptr_t optval, unsigned int optlen);
415a7b75c5aSChristoph Hellwig int tcp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
416a7b75c5aSChristoph Hellwig 		   unsigned int optlen);
4175c9f3023SJoe Perches void tcp_set_keepalive(struct sock *sk, int val);
41842cb80a2SEric Dumazet void tcp_syn_ack_timeout(const struct request_sock *req);
419ec095263SOliver Hartkopp int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
4201b784140SYing Xue 		int flags, int *addr_len);
421d1361840SEric Dumazet int tcp_set_rcvlowat(struct sock *sk, int val);
422cb811109SPrankur gupta int tcp_set_window_clamp(struct sock *sk, int val);
423892bfd3dSFlorian Westphal void tcp_update_recv_tstamps(struct sk_buff *skb,
424892bfd3dSFlorian Westphal 			     struct scm_timestamping_internal *tss);
425892bfd3dSFlorian Westphal void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
426892bfd3dSFlorian Westphal 			struct scm_timestamping_internal *tss);
42703f45c88SEric Dumazet void tcp_data_ready(struct sock *sk);
428340a6f3dSYafang Shao #ifdef CONFIG_MMU
42993ab6cc6SEric Dumazet int tcp_mmap(struct file *file, struct socket *sock,
43093ab6cc6SEric Dumazet 	     struct vm_area_struct *vma);
431340a6f3dSYafang Shao #endif
432eed29f17SEric Dumazet void tcp_parse_options(const struct net *net, const struct sk_buff *skb,
4331a2c6181SChristoph Paasch 		       struct tcp_options_received *opt_rx,
4342100c8d2SYuchung Cheng 		       int estab, struct tcp_fastopen_cookie *foc);
4355c9f3023SJoe Perches const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
4367d5d5525SYOSHIFUJI Hideaki 
4371da177e4SLinus Torvalds /*
4389349d600SPetar Penkov  *	BPF SKB-less helpers
4399349d600SPetar Penkov  */
4409349d600SPetar Penkov u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph,
4419349d600SPetar Penkov 			 struct tcphdr *th, u32 *cookie);
4429349d600SPetar Penkov u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph,
4439349d600SPetar Penkov 			 struct tcphdr *th, u32 *cookie);
44433bf9885SMaxim Mikityanskiy u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss);
4459349d600SPetar Penkov u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
4469349d600SPetar Penkov 			  const struct tcp_request_sock_ops *af_ops,
4479349d600SPetar Penkov 			  struct sock *sk, struct tcphdr *th);
4489349d600SPetar Penkov /*
4491da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4501da177e4SLinus Torvalds  */
4511da177e4SLinus Torvalds 
4525c9f3023SJoe Perches void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
4534fab9071SNeal Cardwell void tcp_v4_mtu_reduced(struct sock *sk);
4549cf74903SEric Dumazet void tcp_req_err(struct sock *sk, u32 seq, bool abort);
455d2924569SEric Dumazet void tcp_ld_RTO_revert(struct sock *sk, u32 seq);
4565c9f3023SJoe Perches int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
457c28c6f04SEric Dumazet struct sock *tcp_create_openreq_child(const struct sock *sk,
45860236fddSArnaldo Carvalho de Melo 				      struct request_sock *req,
4591da177e4SLinus Torvalds 				      struct sk_buff *skb);
46081164413SDaniel Borkmann void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst);
4610c27171eSEric Dumazet struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
46260236fddSArnaldo Carvalho de Melo 				  struct request_sock *req,
4635e0724d0SEric Dumazet 				  struct dst_entry *dst,
4645e0724d0SEric Dumazet 				  struct request_sock *req_unhash,
4655e0724d0SEric Dumazet 				  bool *own_req);
4665c9f3023SJoe Perches int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
4675c9f3023SJoe Perches int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
4685c9f3023SJoe Perches int tcp_connect(struct sock *sk);
469b3d05147SEric Dumazet enum tcp_synack_type {
470b3d05147SEric Dumazet 	TCP_SYNACK_NORMAL,
471b3d05147SEric Dumazet 	TCP_SYNACK_FASTOPEN,
472b3d05147SEric Dumazet 	TCP_SYNACK_COOKIE,
473b3d05147SEric Dumazet };
4745d062de7SEric Dumazet struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
475e6b4d113SWilliam Allen Simpson 				struct request_sock *req,
476ca6fb065SEric Dumazet 				struct tcp_fastopen_cookie *foc,
477331fca43SMartin KaFai Lau 				enum tcp_synack_type synack_type,
478331fca43SMartin KaFai Lau 				struct sk_buff *syn_skb);
4795c9f3023SJoe Perches int tcp_disconnect(struct sock *sk, int flags);
4801da177e4SLinus Torvalds 
481370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
482292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
48363d02d15SEric Dumazet void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /* From syncookies.c */
486b80c0e78SEric Dumazet struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
487b80c0e78SEric Dumazet 				 struct request_sock *req,
48884b114b9SEric Dumazet 				 struct dst_entry *dst, u32 tsoff);
4895c9f3023SJoe Perches int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
4900198230bSPatrick McHardy 		      u32 cookie);
491461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
4926fc8c827SFlorian Westphal struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
493ba5a4fddSEric Dumazet 					    const struct tcp_request_sock_ops *af_ops,
4946fc8c827SFlorian Westphal 					    struct sock *sk, struct sk_buff *skb);
495e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
4968c27bd75SFlorian Westphal 
49763262315SEric Dumazet /* Syncookies use a monotonic timer which increments every 60 seconds.
4988c27bd75SFlorian Westphal  * This counter is used both as a hash input and partially encoded into
4998c27bd75SFlorian Westphal  * the cookie value.  A cookie is only validated further if the delta
5008c27bd75SFlorian Westphal  * between the current counter value and the encoded one is less than this,
50163262315SEric Dumazet  * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
5028c27bd75SFlorian Westphal  * the counter advances immediately after a cookie is generated).
5038c27bd75SFlorian Westphal  */
5048c27bd75SFlorian Westphal #define MAX_SYNCOOKIE_AGE	2
505264ea103SEric Dumazet #define TCP_SYNCOOKIE_PERIOD	(60 * HZ)
506264ea103SEric Dumazet #define TCP_SYNCOOKIE_VALID	(MAX_SYNCOOKIE_AGE * TCP_SYNCOOKIE_PERIOD)
507264ea103SEric Dumazet 
508264ea103SEric Dumazet /* syncookies: remember time of last synqueue overflow
509264ea103SEric Dumazet  * But do not dirty this field too often (once per second is enough)
5103f684b4bSEric Dumazet  * It is racy as we do not hold a lock, but race is very minor.
511264ea103SEric Dumazet  */
5123f684b4bSEric Dumazet static inline void tcp_synq_overflow(const struct sock *sk)
513264ea103SEric Dumazet {
51440a1227eSMartin KaFai Lau 	unsigned int last_overflow;
515cca9bab1SArnd Bergmann 	unsigned int now = jiffies;
516264ea103SEric Dumazet 
51740a1227eSMartin KaFai Lau 	if (sk->sk_reuseport) {
51840a1227eSMartin KaFai Lau 		struct sock_reuseport *reuse;
51940a1227eSMartin KaFai Lau 
52040a1227eSMartin KaFai Lau 		reuse = rcu_dereference(sk->sk_reuseport_cb);
52140a1227eSMartin KaFai Lau 		if (likely(reuse)) {
52240a1227eSMartin KaFai Lau 			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
52304d26e7bSGuillaume Nault 			if (!time_between32(now, last_overflow,
52404d26e7bSGuillaume Nault 					    last_overflow + HZ))
52540a1227eSMartin KaFai Lau 				WRITE_ONCE(reuse->synq_overflow_ts, now);
52640a1227eSMartin KaFai Lau 			return;
52740a1227eSMartin KaFai Lau 		}
52840a1227eSMartin KaFai Lau 	}
52940a1227eSMartin KaFai Lau 
530721c8dafSGuillaume Nault 	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
53104d26e7bSGuillaume Nault 	if (!time_between32(now, last_overflow, last_overflow + HZ))
532721c8dafSGuillaume Nault 		WRITE_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp, now);
533264ea103SEric Dumazet }
534264ea103SEric Dumazet 
535264ea103SEric Dumazet /* syncookies: no recent synqueue overflow on this listening socket? */
536264ea103SEric Dumazet static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
537264ea103SEric Dumazet {
53840a1227eSMartin KaFai Lau 	unsigned int last_overflow;
539cca9bab1SArnd Bergmann 	unsigned int now = jiffies;
540264ea103SEric Dumazet 
54140a1227eSMartin KaFai Lau 	if (sk->sk_reuseport) {
54240a1227eSMartin KaFai Lau 		struct sock_reuseport *reuse;
54340a1227eSMartin KaFai Lau 
54440a1227eSMartin KaFai Lau 		reuse = rcu_dereference(sk->sk_reuseport_cb);
54540a1227eSMartin KaFai Lau 		if (likely(reuse)) {
54640a1227eSMartin KaFai Lau 			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
547cb44a08fSGuillaume Nault 			return !time_between32(now, last_overflow - HZ,
548cb44a08fSGuillaume Nault 					       last_overflow +
54940a1227eSMartin KaFai Lau 					       TCP_SYNCOOKIE_VALID);
55040a1227eSMartin KaFai Lau 		}
55140a1227eSMartin KaFai Lau 	}
55240a1227eSMartin KaFai Lau 
553721c8dafSGuillaume Nault 	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
554cb44a08fSGuillaume Nault 
555cb44a08fSGuillaume Nault 	/* If last_overflow <= jiffies <= last_overflow + TCP_SYNCOOKIE_VALID,
556cb44a08fSGuillaume Nault 	 * then we're under synflood. However, we have to use
557cb44a08fSGuillaume Nault 	 * 'last_overflow - HZ' as lower bound. That's because a concurrent
558cb44a08fSGuillaume Nault 	 * tcp_synq_overflow() could update .ts_recent_stamp after we read
559cb44a08fSGuillaume Nault 	 * jiffies but before we store .ts_recent_stamp into last_overflow,
560cb44a08fSGuillaume Nault 	 * which could lead to rejecting a valid syncookie.
561cb44a08fSGuillaume Nault 	 */
562cb44a08fSGuillaume Nault 	return !time_between32(now, last_overflow - HZ,
563cb44a08fSGuillaume Nault 			       last_overflow + TCP_SYNCOOKIE_VALID);
564264ea103SEric Dumazet }
5658c27bd75SFlorian Westphal 
5668c27bd75SFlorian Westphal static inline u32 tcp_cookie_time(void)
5678c27bd75SFlorian Westphal {
56863262315SEric Dumazet 	u64 val = get_jiffies_64();
56963262315SEric Dumazet 
570264ea103SEric Dumazet 	do_div(val, TCP_SYNCOOKIE_PERIOD);
57163262315SEric Dumazet 	return val;
5728c27bd75SFlorian Westphal }
5738c27bd75SFlorian Westphal 
5745c9f3023SJoe Perches u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
5755c9f3023SJoe Perches 			      u16 *mssp);
5763f684b4bSEric Dumazet __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss);
577200ecef6SEric Dumazet u64 cookie_init_timestamp(struct request_sock *req, u64 now);
578f9301034SEric Dumazet bool cookie_timestamp_decode(const struct net *net,
579f9301034SEric Dumazet 			     struct tcp_options_received *opt);
580f1673381SFlorian Westphal bool cookie_ecn_ok(const struct tcp_options_received *opt,
581f7b3bec6SFlorian Westphal 		   const struct net *net, const struct dst_entry *dst);
5824dfc2817SFlorian Westphal 
583c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */
5845c9f3023SJoe Perches int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
58581eb6a14SPatrick McHardy 		      u32 cookie);
5865c9f3023SJoe Perches struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
587f1673381SFlorian Westphal 
5885c9f3023SJoe Perches u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
58981eb6a14SPatrick McHardy 			      const struct tcphdr *th, u16 *mssp);
5903f684b4bSEric Dumazet __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mss);
591e05c82d3SEric Dumazet #endif
5921da177e4SLinus Torvalds /* tcp_output.c */
5931da177e4SLinus Torvalds 
59404d8825cSPaolo Abeni void tcp_skb_entail(struct sock *sk, struct sk_buff *skb);
59504d8825cSPaolo Abeni void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb);
5965c9f3023SJoe Perches void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
5979e412ba7SIlpo Järvinen 			       int nonagle);
59810d3be56SEric Dumazet int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
59910d3be56SEric Dumazet int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
6005c9f3023SJoe Perches void tcp_retransmit_timer(struct sock *sk);
6015c9f3023SJoe Perches void tcp_xmit_retransmit_queue(struct sock *);
6025c9f3023SJoe Perches void tcp_simple_retransmit(struct sock *);
60357dde7f7SYuchung Cheng void tcp_enter_recovery(struct sock *sk, bool ece_ack);
6045c9f3023SJoe Perches int tcp_trim_head(struct sock *, struct sk_buff *, u32);
60575c119afSEric Dumazet enum tcp_queue {
60675c119afSEric Dumazet 	TCP_FRAG_IN_WRITE_QUEUE,
60775c119afSEric Dumazet 	TCP_FRAG_IN_RTX_QUEUE,
60875c119afSEric Dumazet };
60975c119afSEric Dumazet int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
61075c119afSEric Dumazet 		 struct sk_buff *skb, u32 len,
61175c119afSEric Dumazet 		 unsigned int mss_now, gfp_t gfp);
6121da177e4SLinus Torvalds 
6135c9f3023SJoe Perches void tcp_send_probe0(struct sock *);
6145c9f3023SJoe Perches void tcp_send_partial(struct sock *);
615e520af48SEric Dumazet int tcp_write_wakeup(struct sock *, int mib);
6165c9f3023SJoe Perches void tcp_send_fin(struct sock *sk);
6175c9f3023SJoe Perches void tcp_send_active_reset(struct sock *sk, gfp_t priority);
6185c9f3023SJoe Perches int tcp_send_synack(struct sock *);
6195c9f3023SJoe Perches void tcp_push_one(struct sock *, unsigned int mss_now);
62027cde44aSYuchung Cheng void __tcp_send_ack(struct sock *sk, u32 rcv_nxt);
6215c9f3023SJoe Perches void tcp_send_ack(struct sock *sk);
6225c9f3023SJoe Perches void tcp_send_delayed_ack(struct sock *sk);
6235c9f3023SJoe Perches void tcp_send_loss_probe(struct sock *sk);
624ed66dfafSNeal Cardwell bool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto);
625cfea5a68SMartin KaFai Lau void tcp_skb_collapse_tstamp(struct sk_buff *skb,
626cfea5a68SMartin KaFai Lau 			     const struct sk_buff *next_skb);
6271da177e4SLinus Torvalds 
628a762a980SDavid S. Miller /* tcp_input.c */
6295c9f3023SJoe Perches void tcp_rearm_rto(struct sock *sk);
6300f1c28aeSYuchung Cheng void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req);
631049fe386SFlorian Westphal void tcp_reset(struct sock *sk, struct sk_buff *skb);
6324f41b1c5SYuchung Cheng void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, struct sk_buff *skb);
633e3e17b77SEric Dumazet void tcp_fin(struct sock *sk);
6344bfe744fSEric Dumazet void tcp_check_space(struct sock *sk);
635a762a980SDavid S. Miller 
6361da177e4SLinus Torvalds /* tcp_timer.c */
6375c9f3023SJoe Perches void tcp_init_xmit_timers(struct sock *);
638463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
639463c84b9SArnaldo Carvalho de Melo {
64073a6bab5SEric Dumazet 	if (hrtimer_try_to_cancel(&tcp_sk(sk)->pacing_timer) == 1)
641cf0dd203SEric Dumazet 		__sock_put(sk);
64273a6bab5SEric Dumazet 
6435d9f4262SEric Dumazet 	if (hrtimer_try_to_cancel(&tcp_sk(sk)->compressed_ack_timer) == 1)
6445d9f4262SEric Dumazet 		__sock_put(sk);
6455d9f4262SEric Dumazet 
646463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
647463c84b9SArnaldo Carvalho de Melo }
6481da177e4SLinus Torvalds 
6495c9f3023SJoe Perches unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
6505c9f3023SJoe Perches unsigned int tcp_current_mss(struct sock *sk);
651344db93aSEnke Chen u32 tcp_clamp_probe0_to_user_timeout(const struct sock *sk, u32 when);
6520c54b85fSIlpo Järvinen 
6530c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */
6540c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
6550c54b85fSIlpo Järvinen {
65601f83d69SAlexey Kuznetsov 	int cutoff;
65701f83d69SAlexey Kuznetsov 
65801f83d69SAlexey Kuznetsov 	/* When peer uses tiny windows, there is no use in packetizing
65901f83d69SAlexey Kuznetsov 	 * to sub-MSS pieces for the sake of SWS or making sure there
66001f83d69SAlexey Kuznetsov 	 * are enough packets in the pipe for fast recovery.
66101f83d69SAlexey Kuznetsov 	 *
66201f83d69SAlexey Kuznetsov 	 * On the other hand, for extremely large MSS devices, handling
66301f83d69SAlexey Kuznetsov 	 * smaller than MSS windows in this way does make sense.
66401f83d69SAlexey Kuznetsov 	 */
6652631b79fSSeymour, Shane M 	if (tp->max_window > TCP_MSS_DEFAULT)
66601f83d69SAlexey Kuznetsov 		cutoff = (tp->max_window >> 1);
66701f83d69SAlexey Kuznetsov 	else
66801f83d69SAlexey Kuznetsov 		cutoff = tp->max_window;
66901f83d69SAlexey Kuznetsov 
67001f83d69SAlexey Kuznetsov 	if (cutoff && pktsize > cutoff)
67101f83d69SAlexey Kuznetsov 		return max_t(int, cutoff, 68U - tp->tcp_header_len);
6720c54b85fSIlpo Järvinen 	else
6730c54b85fSIlpo Järvinen 		return pktsize;
6740c54b85fSIlpo Järvinen }
6751da177e4SLinus Torvalds 
67617b085eaSArnaldo Carvalho de Melo /* tcp.c */
6770df48c26SEric Dumazet void tcp_get_info(struct sock *, struct tcp_info *);
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
6805c9f3023SJoe Perches int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
6811da177e4SLinus Torvalds 		  sk_read_actor_t recv_actor);
682965b57b4SCong Wang int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor);
6833f92a64eSJakub Kicinski struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off);
6843f92a64eSJakub Kicinski void tcp_read_done(struct sock *sk, size_t len);
6851da177e4SLinus Torvalds 
6865c9f3023SJoe Perches void tcp_initialize_rcv_mss(struct sock *sk);
6871da177e4SLinus Torvalds 
6885c9f3023SJoe Perches int tcp_mtu_to_mss(struct sock *sk, int pmtu);
6895c9f3023SJoe Perches int tcp_mss_to_mtu(struct sock *sk, int mss);
6905c9f3023SJoe Perches void tcp_mtup_init(struct sock *sk);
6915d424d5aSJohn Heffner 
692f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk)
693f1ecd5d9SDamian Lukowski {
694f1ecd5d9SDamian Lukowski 	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
695f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
696f1ecd5d9SDamian Lukowski }
697f1ecd5d9SDamian Lukowski 
698f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
699f1ecd5d9SDamian Lukowski {
700740b0f18SEric Dumazet 	return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
701f1ecd5d9SDamian Lukowski }
702f1ecd5d9SDamian Lukowski 
70331770e34SFlorian Westphal static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
70431770e34SFlorian Westphal {
70571158bb1SPaolo Abeni 	/* mptcp hooks are only on the slow path */
70671158bb1SPaolo Abeni 	if (sk_is_mptcp((struct sock *)tp))
70771158bb1SPaolo Abeni 		return;
70871158bb1SPaolo Abeni 
70931770e34SFlorian Westphal 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
71031770e34SFlorian Westphal 			       ntohl(TCP_FLAG_ACK) |
71131770e34SFlorian Westphal 			       snd_wnd);
71231770e34SFlorian Westphal }
71331770e34SFlorian Westphal 
71431770e34SFlorian Westphal static inline void tcp_fast_path_on(struct tcp_sock *tp)
71531770e34SFlorian Westphal {
71631770e34SFlorian Westphal 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
71731770e34SFlorian Westphal }
71831770e34SFlorian Westphal 
71931770e34SFlorian Westphal static inline void tcp_fast_path_check(struct sock *sk)
72031770e34SFlorian Westphal {
72131770e34SFlorian Westphal 	struct tcp_sock *tp = tcp_sk(sk);
72231770e34SFlorian Westphal 
72331770e34SFlorian Westphal 	if (RB_EMPTY_ROOT(&tp->out_of_order_queue) &&
72431770e34SFlorian Westphal 	    tp->rcv_wnd &&
72531770e34SFlorian Westphal 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
72631770e34SFlorian Westphal 	    !tp->urg_data)
72731770e34SFlorian Westphal 		tcp_fast_path_on(tp);
72831770e34SFlorian Westphal }
72931770e34SFlorian Westphal 
7300c266898SSatoru SATOH /* Compute the actual rto_min value */
7310c266898SSatoru SATOH static inline u32 tcp_rto_min(struct sock *sk)
7320c266898SSatoru SATOH {
733cf533ea5SEric Dumazet 	const struct dst_entry *dst = __sk_dst_get(sk);
734ca584ba0SMartin KaFai Lau 	u32 rto_min = inet_csk(sk)->icsk_rto_min;
7350c266898SSatoru SATOH 
7360c266898SSatoru SATOH 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
7370c266898SSatoru SATOH 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
7380c266898SSatoru SATOH 	return rto_min;
7390c266898SSatoru SATOH }
7400c266898SSatoru SATOH 
741740b0f18SEric Dumazet static inline u32 tcp_rto_min_us(struct sock *sk)
742740b0f18SEric Dumazet {
743740b0f18SEric Dumazet 	return jiffies_to_usecs(tcp_rto_min(sk));
744740b0f18SEric Dumazet }
745740b0f18SEric Dumazet 
74681164413SDaniel Borkmann static inline bool tcp_ca_dst_locked(const struct dst_entry *dst)
74781164413SDaniel Borkmann {
74881164413SDaniel Borkmann 	return dst_metric_locked(dst, RTAX_CC_ALGO);
74981164413SDaniel Borkmann }
75081164413SDaniel Borkmann 
751f6722583SYuchung Cheng /* Minimum RTT in usec. ~0 means not available. */
752f6722583SYuchung Cheng static inline u32 tcp_min_rtt(const struct tcp_sock *tp)
753f6722583SYuchung Cheng {
75464033892SNeal Cardwell 	return minmax_get(&tp->rtt_min);
755f6722583SYuchung Cheng }
756f6722583SYuchung Cheng 
7571da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
7581da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
7591da177e4SLinus Torvalds  * than the offered window.
7601da177e4SLinus Torvalds  */
76140efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
7621da177e4SLinus Torvalds {
7631da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	if (win < 0)
7661da177e4SLinus Torvalds 		win = 0;
7671da177e4SLinus Torvalds 	return (u32) win;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
7711da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
7721da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
7731da177e4SLinus Torvalds  */
7745c9f3023SJoe Perches u32 __tcp_select_window(struct sock *sk);
7751da177e4SLinus Torvalds 
776ee995283SPavel Emelyanov void tcp_send_window_probe(struct sock *sk);
777ee995283SPavel Emelyanov 
778ec66eda8SEric Dumazet /* TCP uses 32bit jiffies to save some space.
779ec66eda8SEric Dumazet  * Note that this is different from tcp_time_stamp, which
780ec66eda8SEric Dumazet  * historically has been the same until linux-4.13.
781ec66eda8SEric Dumazet  */
782ec66eda8SEric Dumazet #define tcp_jiffies32 ((u32)jiffies)
783ec66eda8SEric Dumazet 
7849a568de4SEric Dumazet /*
7859a568de4SEric Dumazet  * Deliver a 32bit value for TCP timestamp option (RFC 7323)
7869a568de4SEric Dumazet  * It is no longer tied to jiffies, but to 1 ms clock.
7879a568de4SEric Dumazet  * Note: double check if you want to use tcp_jiffies32 instead of this.
7881da177e4SLinus Torvalds  */
7899a568de4SEric Dumazet #define TCP_TS_HZ	1000
7909a568de4SEric Dumazet 
7919a568de4SEric Dumazet static inline u64 tcp_clock_ns(void)
7929a568de4SEric Dumazet {
793fb420d5dSEric Dumazet 	return ktime_get_ns();
7949a568de4SEric Dumazet }
7959a568de4SEric Dumazet 
7969a568de4SEric Dumazet static inline u64 tcp_clock_us(void)
7979a568de4SEric Dumazet {
7989a568de4SEric Dumazet 	return div_u64(tcp_clock_ns(), NSEC_PER_USEC);
7999a568de4SEric Dumazet }
8009a568de4SEric Dumazet 
8019a568de4SEric Dumazet /* This should only be used in contexts where tp->tcp_mstamp is up to date */
8029a568de4SEric Dumazet static inline u32 tcp_time_stamp(const struct tcp_sock *tp)
8039a568de4SEric Dumazet {
8049a568de4SEric Dumazet 	return div_u64(tp->tcp_mstamp, USEC_PER_SEC / TCP_TS_HZ);
8059a568de4SEric Dumazet }
8069a568de4SEric Dumazet 
807200ecef6SEric Dumazet /* Convert a nsec timestamp into TCP TSval timestamp (ms based currently) */
808200ecef6SEric Dumazet static inline u32 tcp_ns_to_ts(u64 ns)
809200ecef6SEric Dumazet {
810200ecef6SEric Dumazet 	return div_u64(ns, NSEC_PER_SEC / TCP_TS_HZ);
811200ecef6SEric Dumazet }
812200ecef6SEric Dumazet 
8139a568de4SEric Dumazet /* Could use tcp_clock_us() / 1000, but this version uses a single divide */
8149a568de4SEric Dumazet static inline u32 tcp_time_stamp_raw(void)
8159a568de4SEric Dumazet {
816200ecef6SEric Dumazet 	return tcp_ns_to_ts(tcp_clock_ns());
8179a568de4SEric Dumazet }
8189a568de4SEric Dumazet 
8199799ccb0SEric Dumazet void tcp_mstamp_refresh(struct tcp_sock *tp);
8209a568de4SEric Dumazet 
8219a568de4SEric Dumazet static inline u32 tcp_stamp_us_delta(u64 t1, u64 t0)
8229a568de4SEric Dumazet {
8239a568de4SEric Dumazet 	return max_t(s64, t1 - t0, 0);
8249a568de4SEric Dumazet }
8251da177e4SLinus Torvalds 
8267faee5c0SEric Dumazet static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
8277faee5c0SEric Dumazet {
828200ecef6SEric Dumazet 	return tcp_ns_to_ts(skb->skb_mstamp_ns);
8297faee5c0SEric Dumazet }
8307faee5c0SEric Dumazet 
8312fd66ffbSEric Dumazet /* provide the departure time in us unit */
8322fd66ffbSEric Dumazet static inline u64 tcp_skb_timestamp_us(const struct sk_buff *skb)
8332fd66ffbSEric Dumazet {
834d3edd06eSEric Dumazet 	return div_u64(skb->skb_mstamp_ns, NSEC_PER_USEC);
8352fd66ffbSEric Dumazet }
8362fd66ffbSEric Dumazet 
8377faee5c0SEric Dumazet 
838a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
839a3433f35SChangli Gao 
840a3433f35SChangli Gao #define TCPHDR_FIN 0x01
841a3433f35SChangli Gao #define TCPHDR_SYN 0x02
842a3433f35SChangli Gao #define TCPHDR_RST 0x04
843a3433f35SChangli Gao #define TCPHDR_PSH 0x08
844a3433f35SChangli Gao #define TCPHDR_ACK 0x10
845a3433f35SChangli Gao #define TCPHDR_URG 0x20
846a3433f35SChangli Gao #define TCPHDR_ECE 0x40
847a3433f35SChangli Gao #define TCPHDR_CWR 0x80
848a3433f35SChangli Gao 
84949213555SDaniel Borkmann #define TCPHDR_SYN_ECN	(TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)
85049213555SDaniel Borkmann 
851caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
852f86586faSEric Dumazet  * TCP per-packet control information to the transmission code.
853f86586faSEric Dumazet  * We also store the host-order sequence numbers in here too.
854f86586faSEric Dumazet  * This is 44 bytes if IPV6 is enabled.
855f86586faSEric Dumazet  * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
8561da177e4SLinus Torvalds  */
8571da177e4SLinus Torvalds struct tcp_skb_cb {
8581da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
8591da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
860cd7d8498SEric Dumazet 	union {
861cd7d8498SEric Dumazet 		/* Note : tcp_tw_isn is used in input path only
862cd7d8498SEric Dumazet 		 *	  (isn chosen by tcp_timewait_state_process())
863cd7d8498SEric Dumazet 		 *
864f69ad292SEric Dumazet 		 * 	  tcp_gso_segs/size are used in write queue only,
865f69ad292SEric Dumazet 		 *	  cf tcp_skb_pcount()/tcp_skb_mss()
866cd7d8498SEric Dumazet 		 */
867cd7d8498SEric Dumazet 		__u32		tcp_tw_isn;
868f69ad292SEric Dumazet 		struct {
869f69ad292SEric Dumazet 			u16	tcp_gso_segs;
870f69ad292SEric Dumazet 			u16	tcp_gso_size;
871f69ad292SEric Dumazet 		};
872cd7d8498SEric Dumazet 	};
8734de075e0SEric Dumazet 	__u8		tcp_flags;	/* TCP header flags. (tcp[13])	*/
874f4f9f6e7SNeal Cardwell 
875713bafeaSYuchung Cheng 	__u8		sacked;		/* State flags for SACK.	*/
8761da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
8771da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
8781da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
8791da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
880d3edd06eSEric Dumazet #define TCPCB_REPAIRED		0x10	/* SKB repaired (no skb_mstamp_ns)	*/
8811da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
8829d186cacSAndrey Vagin #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
8839d186cacSAndrey Vagin 				TCPCB_REPAIRED)
8841da177e4SLinus Torvalds 
885f4f9f6e7SNeal Cardwell 	__u8		ip_dsfield;	/* IPv4 tos or IPv6 dsfield	*/
8866b084928SSoheil Hassas Yeganeh 	__u8		txstamp_ack:1,	/* Record TX timestamp for ack? */
887c134ecb8SMartin KaFai Lau 			eor:1,		/* Is skb MSG_EOR marked? */
88898aaa913SMike Maloney 			has_rxtstamp:1,	/* SKB has a RX timestamp	*/
88998aaa913SMike Maloney 			unused:5;
8901da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
891971f10ecSEric Dumazet 	union {
892b75803d5SLawrence Brakmo 		struct {
89340bc6063SYuchung Cheng #define TCPCB_DELIVERED_CE_MASK ((1U<<20) - 1)
894b9f64820SYuchung Cheng 			/* There is space for up to 24 bytes */
89540bc6063SYuchung Cheng 			__u32 is_app_limited:1, /* cwnd not fully used? */
89640bc6063SYuchung Cheng 			      delivered_ce:20,
89740bc6063SYuchung Cheng 			      unused:11;
898b9f64820SYuchung Cheng 			/* pkts S/ACKed so far upon tx of skb, incl retrans: */
899b9f64820SYuchung Cheng 			__u32 delivered;
900b9f64820SYuchung Cheng 			/* start of send pipeline phase */
9019a568de4SEric Dumazet 			u64 first_tx_mstamp;
902b9f64820SYuchung Cheng 			/* when we reached the "delivered" count */
9039a568de4SEric Dumazet 			u64 delivered_mstamp;
904b75803d5SLawrence Brakmo 		} tx;   /* only used for outgoing skbs */
905b75803d5SLawrence Brakmo 		union {
906971f10ecSEric Dumazet 			struct inet_skb_parm	h4;
907971f10ecSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
908971f10ecSEric Dumazet 			struct inet6_skb_parm	h6;
909971f10ecSEric Dumazet #endif
910b75803d5SLawrence Brakmo 		} header;	/* For incoming skbs */
911b75803d5SLawrence Brakmo 	};
9121da177e4SLinus Torvalds };
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
9151da177e4SLinus Torvalds 
9169b9e2f25SEric Dumazet extern const struct inet_connection_sock_af_ops ipv4_specific;
9179b9e2f25SEric Dumazet 
918815afe17SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
919870c3151SEric Dumazet /* This is the variant of inet6_iif() that must be used by TCP,
920870c3151SEric Dumazet  * as TCP moves IP6CB into a different location in skb->cb[]
921870c3151SEric Dumazet  */
922870c3151SEric Dumazet static inline int tcp_v6_iif(const struct sk_buff *skb)
923870c3151SEric Dumazet {
92424b711edSDavid Ahern 	return TCP_SKB_CB(skb)->header.h6.iif;
92524b711edSDavid Ahern }
92624b711edSDavid Ahern 
92724b711edSDavid Ahern static inline int tcp_v6_iif_l3_slave(const struct sk_buff *skb)
92824b711edSDavid Ahern {
929a04a480dSDavid Ahern 	bool l3_slave = ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags);
93074b20582SDavid Ahern 
93174b20582SDavid Ahern 	return l3_slave ? skb->skb_iif : TCP_SKB_CB(skb)->header.h6.iif;
932870c3151SEric Dumazet }
9334297a0efSDavid Ahern 
9344297a0efSDavid Ahern /* TCP_SKB_CB reference means this can not be used from early demux */
9354297a0efSDavid Ahern static inline int tcp_v6_sdif(const struct sk_buff *skb)
9364297a0efSDavid Ahern {
9374297a0efSDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9384297a0efSDavid Ahern 	if (skb && ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags))
9394297a0efSDavid Ahern 		return TCP_SKB_CB(skb)->header.h6.iif;
9404297a0efSDavid Ahern #endif
9414297a0efSDavid Ahern 	return 0;
9424297a0efSDavid Ahern }
943dd2e0b86SEric Dumazet 
944b03d2142SEric Dumazet extern const struct inet_connection_sock_af_ops ipv6_specific;
945b03d2142SEric Dumazet 
946dd2e0b86SEric Dumazet INDIRECT_CALLABLE_DECLARE(void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb));
947243600eeSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *skb));
94811052589SKuniyuki Iwashima void tcp_v6_early_demux(struct sk_buff *skb);
949dd2e0b86SEric Dumazet 
950815afe17SEric Dumazet #endif
951870c3151SEric Dumazet 
9523fa6f616SDavid Ahern /* TCP_SKB_CB reference means this can not be used from early demux */
9533fa6f616SDavid Ahern static inline int tcp_v4_sdif(struct sk_buff *skb)
9543fa6f616SDavid Ahern {
9553fa6f616SDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9563fa6f616SDavid Ahern 	if (skb && ipv4_l3mdev_skb(TCP_SKB_CB(skb)->header.h4.flags))
9573fa6f616SDavid Ahern 		return TCP_SKB_CB(skb)->header.h4.iif;
9583fa6f616SDavid Ahern #endif
9593fa6f616SDavid Ahern 	return 0;
9603fa6f616SDavid Ahern }
9613fa6f616SDavid Ahern 
9621da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
9631da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
9641da177e4SLinus Torvalds  */
9651da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
9661da177e4SLinus Torvalds {
967cd7d8498SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_segs;
968cd7d8498SEric Dumazet }
969cd7d8498SEric Dumazet 
970cd7d8498SEric Dumazet static inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs)
971cd7d8498SEric Dumazet {
972cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs = segs;
973cd7d8498SEric Dumazet }
974cd7d8498SEric Dumazet 
975cd7d8498SEric Dumazet static inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs)
976cd7d8498SEric Dumazet {
977cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs += segs;
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
980f69ad292SEric Dumazet /* This is valid iff skb is in write queue and tcp_skb_pcount() > 1. */
9811da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
9821da177e4SLinus Torvalds {
983f69ad292SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_size;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
986c134ecb8SMartin KaFai Lau static inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb)
987c134ecb8SMartin KaFai Lau {
988c134ecb8SMartin KaFai Lau 	return likely(!TCP_SKB_CB(skb)->eor);
989c134ecb8SMartin KaFai Lau }
990c134ecb8SMartin KaFai Lau 
99185712484SMat Martineau static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
99285712484SMat Martineau 					const struct sk_buff *from)
99385712484SMat Martineau {
99485712484SMat Martineau 	return likely(tcp_skb_can_collapse_to(to) &&
9959b65b17dSTalal Ahmad 		      mptcp_skb_can_collapse(to, from) &&
9969b65b17dSTalal Ahmad 		      skb_pure_zcopy_same(to, from));
99785712484SMat Martineau }
99885712484SMat Martineau 
999317a76f9SStephen Hemminger /* Events passed to congestion control interface */
1000317a76f9SStephen Hemminger enum tcp_ca_event {
1001317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
1002317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
1003317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
1004317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
10059890092eSFlorian Westphal 	CA_EVENT_ECN_NO_CE,	/* ECT set, but not CE marked */
10069890092eSFlorian Westphal 	CA_EVENT_ECN_IS_CE,	/* received CE marked IP packet */
10077354c8c3SFlorian Westphal };
10087354c8c3SFlorian Westphal 
10099890092eSFlorian Westphal /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
10107354c8c3SFlorian Westphal enum tcp_ca_ack_event_flags {
1011c1d2b4c3SFlorian Westphal 	CA_ACK_SLOWPATH		= (1 << 0),	/* In slow path processing */
1012c1d2b4c3SFlorian Westphal 	CA_ACK_WIN_UPDATE	= (1 << 1),	/* ACK updated window */
1013c1d2b4c3SFlorian Westphal 	CA_ACK_ECE		= (1 << 2),	/* ECE bit is set on ack */
1014317a76f9SStephen Hemminger };
1015317a76f9SStephen Hemminger 
1016317a76f9SStephen Hemminger /*
1017317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
1018317a76f9SStephen Hemminger  */
1019317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
10203ff825b2SStephen Hemminger #define TCP_CA_MAX	128
10213ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
10223ff825b2SStephen Hemminger 
1023c5c6a8abSDaniel Borkmann #define TCP_CA_UNSPEC	0
1024c5c6a8abSDaniel Borkmann 
102530e502a3SDaniel Borkmann /* Algorithm can be set on socket without CAP_NET_ADMIN privileges */
1026164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1
102730e502a3SDaniel Borkmann /* Requires ECN/ECT set on all packets */
102830e502a3SDaniel Borkmann #define TCP_CONG_NEEDS_ECN	0x2
10290baf26b0SMartin KaFai Lau #define TCP_CONG_MASK	(TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN)
1030164891aaSStephen Hemminger 
103164f40ff5SEric Dumazet union tcp_cc_info;
103264f40ff5SEric Dumazet 
1033756ee172SLawrence Brakmo struct ack_sample {
1034756ee172SLawrence Brakmo 	u32 pkts_acked;
1035756ee172SLawrence Brakmo 	s32 rtt_us;
10366f094b9eSLawrence Brakmo 	u32 in_flight;
1037756ee172SLawrence Brakmo };
1038756ee172SLawrence Brakmo 
1039b9f64820SYuchung Cheng /* A rate sample measures the number of (original/retransmitted) data
1040b9f64820SYuchung Cheng  * packets delivered "delivered" over an interval of time "interval_us".
1041b9f64820SYuchung Cheng  * The tcp_rate.c code fills in the rate sample, and congestion
1042b9f64820SYuchung Cheng  * control modules that define a cong_control function to run at the end
1043b9f64820SYuchung Cheng  * of ACK processing can optionally chose to consult this sample when
1044b9f64820SYuchung Cheng  * setting cwnd and pacing rate.
1045b9f64820SYuchung Cheng  * A sample is invalid if "delivered" or "interval_us" is negative.
1046b9f64820SYuchung Cheng  */
1047b9f64820SYuchung Cheng struct rate_sample {
10489a568de4SEric Dumazet 	u64  prior_mstamp; /* starting timestamp for interval */
1049b9f64820SYuchung Cheng 	u32  prior_delivered;	/* tp->delivered at "prior_mstamp" */
105040bc6063SYuchung Cheng 	u32  prior_delivered_ce;/* tp->delivered_ce at "prior_mstamp" */
1051b9f64820SYuchung Cheng 	s32  delivered;		/* number of packets delivered over interval */
105240bc6063SYuchung Cheng 	s32  delivered_ce;	/* number of packets delivered w/ CE marks*/
1053b9f64820SYuchung Cheng 	long interval_us;	/* time for tp->delivered to incr "delivered" */
10544929c942SDeepti Raghavan 	u32 snd_interval_us;	/* snd interval for delivered packets */
10554929c942SDeepti Raghavan 	u32 rcv_interval_us;	/* rcv interval for delivered packets */
1056b9f64820SYuchung Cheng 	long rtt_us;		/* RTT of last (S)ACKed packet (or -1) */
1057b9f64820SYuchung Cheng 	int  losses;		/* number of packets marked lost upon ACK */
1058b9f64820SYuchung Cheng 	u32  acked_sacked;	/* number of packets newly (S)ACKed upon ACK */
1059b9f64820SYuchung Cheng 	u32  prior_in_flight;	/* in flight before this ACK */
1060b253a068SPengcheng Yang 	u32  last_end_seq;	/* end_seq of most recently ACKed packet */
1061d7722e85SSoheil Hassas Yeganeh 	bool is_app_limited;	/* is sample from packet with bubble in pipe? */
1062b9f64820SYuchung Cheng 	bool is_retrans;	/* is sample from retransmission? */
1063e4286603SYuchung Cheng 	bool is_ack_delayed;	/* is this (likely) a delayed ACK? */
1064b9f64820SYuchung Cheng };
1065b9f64820SYuchung Cheng 
1066317a76f9SStephen Hemminger struct tcp_congestion_ops {
106782506665SEric Dumazet /* fast path fields are put first to fill one cache line */
106882506665SEric Dumazet 
106982506665SEric Dumazet 	/* return slow start threshold (required) */
107082506665SEric Dumazet 	u32 (*ssthresh)(struct sock *sk);
107182506665SEric Dumazet 
107282506665SEric Dumazet 	/* do new cwnd calculation (required) */
107382506665SEric Dumazet 	void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked);
107482506665SEric Dumazet 
107582506665SEric Dumazet 	/* call before changing ca_state (optional) */
107682506665SEric Dumazet 	void (*set_state)(struct sock *sk, u8 new_state);
107782506665SEric Dumazet 
107882506665SEric Dumazet 	/* call when cwnd event occurs (optional) */
107982506665SEric Dumazet 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
108082506665SEric Dumazet 
108182506665SEric Dumazet 	/* call when ack arrives (optional) */
108282506665SEric Dumazet 	void (*in_ack_event)(struct sock *sk, u32 flags);
108382506665SEric Dumazet 
108482506665SEric Dumazet 	/* hook for packet ack accounting (optional) */
108582506665SEric Dumazet 	void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample);
108682506665SEric Dumazet 
108782506665SEric Dumazet 	/* override sysctl_tcp_min_tso_segs */
108882506665SEric Dumazet 	u32 (*min_tso_segs)(struct sock *sk);
108982506665SEric Dumazet 
109082506665SEric Dumazet 	/* call when packets are delivered to update cwnd and pacing rate,
109182506665SEric Dumazet 	 * after all the ca_state processing. (optional)
109282506665SEric Dumazet 	 */
109382506665SEric Dumazet 	void (*cong_control)(struct sock *sk, const struct rate_sample *rs);
109482506665SEric Dumazet 
109582506665SEric Dumazet 
109682506665SEric Dumazet 	/* new value of cwnd after loss (required) */
109782506665SEric Dumazet 	u32  (*undo_cwnd)(struct sock *sk);
109882506665SEric Dumazet 	/* returns the multiplier used in tcp_sndbuf_expand (optional) */
109982506665SEric Dumazet 	u32 (*sndbuf_expand)(struct sock *sk);
110082506665SEric Dumazet 
110182506665SEric Dumazet /* control/slow paths put last */
110282506665SEric Dumazet 	/* get info for inet_diag (optional) */
110382506665SEric Dumazet 	size_t (*get_info)(struct sock *sk, u32 ext, int *attr,
110482506665SEric Dumazet 			   union tcp_cc_info *info);
110582506665SEric Dumazet 
110682506665SEric Dumazet 	char 			name[TCP_CA_NAME_MAX];
110782506665SEric Dumazet 	struct module		*owner;
1108317a76f9SStephen Hemminger 	struct list_head	list;
1109c5c6a8abSDaniel Borkmann 	u32			key;
1110c5c6a8abSDaniel Borkmann 	u32			flags;
1111317a76f9SStephen Hemminger 
1112317a76f9SStephen Hemminger 	/* initialize private data (optional) */
11136687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
1114317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
11156687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
111682506665SEric Dumazet } ____cacheline_aligned_in_smp;
1117317a76f9SStephen Hemminger 
11185c9f3023SJoe Perches int tcp_register_congestion_control(struct tcp_congestion_ops *type);
11195c9f3023SJoe Perches void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
1120317a76f9SStephen Hemminger 
112155d8694fSFlorian Westphal void tcp_assign_congestion_control(struct sock *sk);
11225c9f3023SJoe Perches void tcp_init_congestion_control(struct sock *sk);
11235c9f3023SJoe Perches void tcp_cleanup_congestion_control(struct sock *sk);
11246670e152SStephen Hemminger int tcp_set_default_congestion_control(struct net *net, const char *name);
11256670e152SStephen Hemminger void tcp_get_default_congestion_control(struct net *net, char *name);
11265c9f3023SJoe Perches void tcp_get_available_congestion_control(char *buf, size_t len);
11275c9f3023SJoe Perches void tcp_get_allowed_congestion_control(char *buf, size_t len);
11285c9f3023SJoe Perches int tcp_set_allowed_congestion_control(char *allowed);
11298d650cdeSEric Dumazet int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
113029a94932SNeal Cardwell 			       bool cap_net_admin);
1131e73ebb08SNeal Cardwell u32 tcp_slow_start(struct tcp_sock *tp, u32 acked);
1132e73ebb08SNeal Cardwell void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked);
1133317a76f9SStephen Hemminger 
11345c9f3023SJoe Perches u32 tcp_reno_ssthresh(struct sock *sk);
1135e9799183SFlorian Westphal u32 tcp_reno_undo_cwnd(struct sock *sk);
113624901551SEric Dumazet void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked);
1137a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
1138317a76f9SStephen Hemminger 
11390baf26b0SMartin KaFai Lau struct tcp_congestion_ops *tcp_ca_find(const char *name);
1140c5c6a8abSDaniel Borkmann struct tcp_congestion_ops *tcp_ca_find_key(u32 key);
11416670e152SStephen Hemminger u32 tcp_ca_get_key_by_name(struct net *net, const char *name, bool *ecn_ca);
1142ea697639SDaniel Borkmann #ifdef CONFIG_INET
1143c5c6a8abSDaniel Borkmann char *tcp_ca_get_name_by_key(u32 key, char *buffer);
1144ea697639SDaniel Borkmann #else
1145ea697639SDaniel Borkmann static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
1146ea697639SDaniel Borkmann {
1147ea697639SDaniel Borkmann 	return NULL;
1148ea697639SDaniel Borkmann }
1149ea697639SDaniel Borkmann #endif
1150c5c6a8abSDaniel Borkmann 
115130e502a3SDaniel Borkmann static inline bool tcp_ca_needs_ecn(const struct sock *sk)
115230e502a3SDaniel Borkmann {
115330e502a3SDaniel Borkmann 	const struct inet_connection_sock *icsk = inet_csk(sk);
115430e502a3SDaniel Borkmann 
115530e502a3SDaniel Borkmann 	return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN;
115630e502a3SDaniel Borkmann }
115730e502a3SDaniel Borkmann 
11586687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
1159317a76f9SStephen Hemminger {
11606687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
11616687e988SArnaldo Carvalho de Melo 
11626687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
11636687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
1164317a76f9SStephen Hemminger }
1165317a76f9SStephen Hemminger 
116615fcdf6aSPing Gan /* From tcp_cong.c */
116715fcdf6aSPing Gan void tcp_set_ca_state(struct sock *sk, const u8 ca_state);
116815fcdf6aSPing Gan 
1169b9f64820SYuchung Cheng /* From tcp_rate.c */
1170b9f64820SYuchung Cheng void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb);
1171b9f64820SYuchung Cheng void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
1172b9f64820SYuchung Cheng 			    struct rate_sample *rs);
1173b9f64820SYuchung Cheng void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
1174d4761754SYousuk Seung 		  bool is_sack_reneg, struct rate_sample *rs);
1175d7722e85SSoheil Hassas Yeganeh void tcp_rate_check_app_limited(struct sock *sk);
1176b9f64820SYuchung Cheng 
1177b253a068SPengcheng Yang static inline bool tcp_skb_sent_after(u64 t1, u64 t2, u32 seq1, u32 seq2)
1178b253a068SPengcheng Yang {
1179b253a068SPengcheng Yang 	return t1 > t2 || (t1 == t2 && after(seq1, seq2));
1180b253a068SPengcheng Yang }
1181b253a068SPengcheng Yang 
1182e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK
1183e60402d0SIlpo Järvinen  * handling. SACK is negotiated with the peer, and therefore it can vary
1184e60402d0SIlpo Järvinen  * between different flows.
1185e60402d0SIlpo Järvinen  *
1186e60402d0SIlpo Järvinen  * tcp_is_sack - SACK enabled
1187e60402d0SIlpo Järvinen  * tcp_is_reno - No SACK
1188e60402d0SIlpo Järvinen  */
1189e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp)
1190e60402d0SIlpo Järvinen {
1191ebeef4bcSEric Dumazet 	return likely(tp->rx_opt.sack_ok);
1192e60402d0SIlpo Järvinen }
1193e60402d0SIlpo Järvinen 
1194a2a385d6SEric Dumazet static inline bool tcp_is_reno(const struct tcp_sock *tp)
1195e60402d0SIlpo Järvinen {
1196e60402d0SIlpo Järvinen 	return !tcp_is_sack(tp);
1197e60402d0SIlpo Järvinen }
1198e60402d0SIlpo Järvinen 
119983ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
120083ae4088SIlpo Järvinen {
120183ae4088SIlpo Järvinen 	return tp->sacked_out + tp->lost_out;
120283ae4088SIlpo Järvinen }
120383ae4088SIlpo Järvinen 
12041da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
12051da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
12061da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
12071da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
12081da177e4SLinus Torvalds  *
12091da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
12101da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
12111da177e4SLinus Torvalds  *
12121da177e4SLinus Torvalds  * Read this equation as:
12131da177e4SLinus Torvalds  *
12141da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
12151da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
12161da177e4SLinus Torvalds  *	"Packets fast retransmitted"
12171da177e4SLinus Torvalds  */
121840efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
12191da177e4SLinus Torvalds {
122083ae4088SIlpo Järvinen 	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
12211da177e4SLinus Torvalds }
12221da177e4SLinus Torvalds 
12230b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH	0x7fffffff
12240b6a05c1SIlpo Järvinen 
122540570375SEric Dumazet static inline u32 tcp_snd_cwnd(const struct tcp_sock *tp)
122640570375SEric Dumazet {
122740570375SEric Dumazet 	return tp->snd_cwnd;
122840570375SEric Dumazet }
122940570375SEric Dumazet 
123040570375SEric Dumazet static inline void tcp_snd_cwnd_set(struct tcp_sock *tp, u32 val)
123140570375SEric Dumazet {
123240570375SEric Dumazet 	WARN_ON_ONCE((int)val <= 0);
123340570375SEric Dumazet 	tp->snd_cwnd = val;
123440570375SEric Dumazet }
123540570375SEric Dumazet 
1236071d5080SYuchung Cheng static inline bool tcp_in_slow_start(const struct tcp_sock *tp)
1237071d5080SYuchung Cheng {
123840570375SEric Dumazet 	return tcp_snd_cwnd(tp) < tp->snd_ssthresh;
1239071d5080SYuchung Cheng }
1240071d5080SYuchung Cheng 
12410b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
12420b6a05c1SIlpo Järvinen {
12430b6a05c1SIlpo Järvinen 	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
12440b6a05c1SIlpo Järvinen }
12450b6a05c1SIlpo Järvinen 
1246684bad11SYuchung Cheng static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
1247684bad11SYuchung Cheng {
1248684bad11SYuchung Cheng 	return (TCPF_CA_CWR | TCPF_CA_Recovery) &
1249684bad11SYuchung Cheng 	       (1 << inet_csk(sk)->icsk_ca_state);
1250684bad11SYuchung Cheng }
1251684bad11SYuchung Cheng 
12521da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
1253684bad11SYuchung Cheng  * The exception is cwnd reduction phase, when cwnd is decreasing towards
12541da177e4SLinus Torvalds  * ssthresh.
12551da177e4SLinus Torvalds  */
12566687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
12571da177e4SLinus Torvalds {
12586687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
1259cf533ea5SEric Dumazet 
1260684bad11SYuchung Cheng 	if (tcp_in_cwnd_reduction(sk))
12611da177e4SLinus Torvalds 		return tp->snd_ssthresh;
12621da177e4SLinus Torvalds 	else
12631da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
126440570375SEric Dumazet 			   ((tcp_snd_cwnd(tp) >> 1) +
126540570375SEric Dumazet 			    (tcp_snd_cwnd(tp) >> 2)));
12661da177e4SLinus Torvalds }
12671da177e4SLinus Torvalds 
1268b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */
1269b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
12701da177e4SLinus Torvalds 
12715ee2c941SChristoph Paasch void tcp_enter_cwr(struct sock *sk);
12725c9f3023SJoe Perches __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
12731da177e4SLinus Torvalds 
12746b5a5c0dSNeal Cardwell /* The maximum number of MSS of available cwnd for which TSO defers
12756b5a5c0dSNeal Cardwell  * sending if not using sysctl_tcp_tso_win_divisor.
12766b5a5c0dSNeal Cardwell  */
12776b5a5c0dSNeal Cardwell static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
12786b5a5c0dSNeal Cardwell {
12796b5a5c0dSNeal Cardwell 	return 3;
12806b5a5c0dSNeal Cardwell }
12816b5a5c0dSNeal Cardwell 
128290840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */
128390840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
128490840defSIlpo Järvinen {
128590840defSIlpo Järvinen 	return tp->snd_una + tp->snd_wnd;
128690840defSIlpo Järvinen }
1287e114a710SEric Dumazet 
1288e114a710SEric Dumazet /* We follow the spirit of RFC2861 to validate cwnd but implement a more
1289e114a710SEric Dumazet  * flexible approach. The RFC suggests cwnd should not be raised unless
1290ca8a2263SNeal Cardwell  * it was fully used previously. And that's exactly what we do in
1291ca8a2263SNeal Cardwell  * congestion avoidance mode. But in slow start we allow cwnd to grow
1292ca8a2263SNeal Cardwell  * as long as the application has used half the cwnd.
1293e114a710SEric Dumazet  * Example :
1294e114a710SEric Dumazet  *    cwnd is 10 (IW10), but application sends 9 frames.
1295e114a710SEric Dumazet  *    We allow cwnd to reach 18 when all frames are ACKed.
1296e114a710SEric Dumazet  * This check is safe because it's as aggressive as slow start which already
1297e114a710SEric Dumazet  * risks 100% overshoot. The advantage is that we discourage application to
1298e114a710SEric Dumazet  * either send more filler packets or data to artificially blow up the cwnd
1299e114a710SEric Dumazet  * usage, and allow application-limited process to probe bw more aggressively.
1300e114a710SEric Dumazet  */
130124901551SEric Dumazet static inline bool tcp_is_cwnd_limited(const struct sock *sk)
1302e114a710SEric Dumazet {
1303e114a710SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1304e114a710SEric Dumazet 
1305f4ce91ceSNeal Cardwell 	if (tp->is_cwnd_limited)
1306f4ce91ceSNeal Cardwell 		return true;
1307f4ce91ceSNeal Cardwell 
1308ca8a2263SNeal Cardwell 	/* If in slow start, ensure cwnd grows to twice what was ACKed. */
1309071d5080SYuchung Cheng 	if (tcp_in_slow_start(tp))
131040570375SEric Dumazet 		return tcp_snd_cwnd(tp) < 2 * tp->max_packets_out;
1311ca8a2263SNeal Cardwell 
1312f4ce91ceSNeal Cardwell 	return false;
1313e114a710SEric Dumazet }
1314f4805edeSStephen Hemminger 
1315cadefe5fSEric Dumazet /* BBR congestion control needs pacing.
1316cadefe5fSEric Dumazet  * Same remark for SO_MAX_PACING_RATE.
1317cadefe5fSEric Dumazet  * sch_fq packet scheduler is efficiently handling pacing,
1318cadefe5fSEric Dumazet  * but is not always installed/used.
1319cadefe5fSEric Dumazet  * Return true if TCP stack should pace packets itself.
1320cadefe5fSEric Dumazet  */
1321cadefe5fSEric Dumazet static inline bool tcp_needs_internal_pacing(const struct sock *sk)
1322cadefe5fSEric Dumazet {
1323cadefe5fSEric Dumazet 	return smp_load_acquire(&sk->sk_pacing_status) == SK_PACING_NEEDED;
1324cadefe5fSEric Dumazet }
1325cadefe5fSEric Dumazet 
13268dc242adSEric Dumazet /* Estimates in how many jiffies next packet for this flow can be sent.
13278dc242adSEric Dumazet  * Scheduling a retransmit timer too early would be silly.
13283f80e08fSEric Dumazet  */
13298dc242adSEric Dumazet static inline unsigned long tcp_pacing_delay(const struct sock *sk)
13303f80e08fSEric Dumazet {
13318dc242adSEric Dumazet 	s64 delay = tcp_sk(sk)->tcp_wstamp_ns - tcp_sk(sk)->tcp_clock_cache;
13323f80e08fSEric Dumazet 
13338dc242adSEric Dumazet 	return delay > 0 ? nsecs_to_jiffies(delay) : 0;
13343f80e08fSEric Dumazet }
13353f80e08fSEric Dumazet 
13363f80e08fSEric Dumazet static inline void tcp_reset_xmit_timer(struct sock *sk,
13373f80e08fSEric Dumazet 					const int what,
13383f80e08fSEric Dumazet 					unsigned long when,
13398dc242adSEric Dumazet 					const unsigned long max_when)
13403f80e08fSEric Dumazet {
13418dc242adSEric Dumazet 	inet_csk_reset_xmit_timer(sk, what, when + tcp_pacing_delay(sk),
13423f80e08fSEric Dumazet 				  max_when);
13433f80e08fSEric Dumazet }
13443f80e08fSEric Dumazet 
134521c8fe99SEric Dumazet /* Something is really bad, we could not queue an additional packet,
13463f80e08fSEric Dumazet  * because qdisc is full or receiver sent a 0 window, or we are paced.
134721c8fe99SEric Dumazet  * We do not want to add fuel to the fire, or abort too early,
134821c8fe99SEric Dumazet  * so make sure the timer we arm now is at least 200ms in the future,
134921c8fe99SEric Dumazet  * regardless of current icsk_rto value (as it could be ~2ms)
135021c8fe99SEric Dumazet  */
135121c8fe99SEric Dumazet static inline unsigned long tcp_probe0_base(const struct sock *sk)
135221c8fe99SEric Dumazet {
135321c8fe99SEric Dumazet 	return max_t(unsigned long, inet_csk(sk)->icsk_rto, TCP_RTO_MIN);
135421c8fe99SEric Dumazet }
135521c8fe99SEric Dumazet 
135621c8fe99SEric Dumazet /* Variant of inet_csk_rto_backoff() used for zero window probes */
135721c8fe99SEric Dumazet static inline unsigned long tcp_probe0_when(const struct sock *sk,
135821c8fe99SEric Dumazet 					    unsigned long max_when)
135921c8fe99SEric Dumazet {
13606d4634d1SCambda Zhu 	u8 backoff = min_t(u8, ilog2(TCP_RTO_MAX / TCP_RTO_MIN) + 1,
13616d4634d1SCambda Zhu 			   inet_csk(sk)->icsk_backoff);
13626d4634d1SCambda Zhu 	u64 when = (u64)tcp_probe0_base(sk) << backoff;
136321c8fe99SEric Dumazet 
136421c8fe99SEric Dumazet 	return (unsigned long)min_t(u64, when, max_when);
136521c8fe99SEric Dumazet }
136621c8fe99SEric Dumazet 
13679e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk)
13681da177e4SLinus Torvalds {
136921c8fe99SEric Dumazet 	if (!tcp_sk(sk)->packets_out && !inet_csk(sk)->icsk_pending)
13703f80e08fSEric Dumazet 		tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
13718dc242adSEric Dumazet 				     tcp_probe0_base(sk), TCP_RTO_MAX);
13721da177e4SLinus Torvalds }
13731da177e4SLinus Torvalds 
1374ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
13751da177e4SLinus Torvalds {
13761da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
13771da177e4SLinus Torvalds }
13781da177e4SLinus Torvalds 
1379ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
13801da177e4SLinus Torvalds {
13811da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds /*
13851da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
13861da177e4SLinus Torvalds  */
1387ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr,
1388ba7808eaSFrederik Deweerdt 				   __be32 daddr, __wsum base)
13891da177e4SLinus Torvalds {
13901da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_TCP, base);
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
1393a2a385d6SEric Dumazet static inline bool tcp_checksum_complete(struct sk_buff *skb)
13941da177e4SLinus Torvalds {
139560476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
13966ab6dfa6SCong Wang 		__skb_checksum_complete(skb);
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds 
13997a26dc9eSMenglong Dong bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb,
14007a26dc9eSMenglong Dong 		     enum skb_drop_reason *reason);
1401f35f8219SEric Dumazet 
1402f35f8219SEric Dumazet 
1403ac6e7800SEric Dumazet int tcp_filter(struct sock *sk, struct sk_buff *skb);
14045c9f3023SJoe Perches void tcp_set_state(struct sock *sk, int state);
14055c9f3023SJoe Perches void tcp_done(struct sock *sk);
1406c1e64e29SLorenzo Colitti int tcp_abort(struct sock *sk, int err);
1407c1e64e29SLorenzo Colitti 
140840efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
14091da177e4SLinus Torvalds {
14101da177e4SLinus Torvalds 	rx_opt->dsack = 0;
14111da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
14121da177e4SLinus Torvalds }
14131da177e4SLinus Torvalds 
14146f021c62SEric Dumazet void tcp_cwnd_restart(struct sock *sk, s32 delta);
14156f021c62SEric Dumazet 
14166f021c62SEric Dumazet static inline void tcp_slow_start_after_idle_check(struct sock *sk)
14176f021c62SEric Dumazet {
14181b1fc3fdSWei Wang 	const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
14196f021c62SEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
14206f021c62SEric Dumazet 	s32 delta;
14216f021c62SEric Dumazet 
14224845b571SKuniyuki Iwashima 	if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle) ||
14234845b571SKuniyuki Iwashima 	    tp->packets_out || ca_ops->cong_control)
14246f021c62SEric Dumazet 		return;
1425d635fbe2SEric Dumazet 	delta = tcp_jiffies32 - tp->lsndtime;
14266f021c62SEric Dumazet 	if (delta > inet_csk(sk)->icsk_rto)
14276f021c62SEric Dumazet 		tcp_cwnd_restart(sk, delta);
14286f021c62SEric Dumazet }
142985f16525SYuchung Cheng 
14301da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
1431ceef9ab6SEric Dumazet void tcp_select_initial_window(const struct sock *sk, int __space,
1432ceef9ab6SEric Dumazet 			       __u32 mss, __u32 *rcv_wnd,
14335c9f3023SJoe Perches 			       __u32 *window_clamp, int wscale_ok,
14345c9f3023SJoe Perches 			       __u8 *rcv_wscale, __u32 init_rcv_wnd);
14351da177e4SLinus Torvalds 
143694f0893eSEric Dumazet static inline int tcp_win_from_space(const struct sock *sk, int space)
14371da177e4SLinus Torvalds {
143836eeee75SKuniyuki Iwashima 	int tcp_adv_win_scale = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_adv_win_scale);
1439c4836742SGao Feng 
1440c4836742SGao Feng 	return tcp_adv_win_scale <= 0 ?
1441c4836742SGao Feng 		(space>>(-tcp_adv_win_scale)) :
1442c4836742SGao Feng 		space - (space>>tcp_adv_win_scale);
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
14461da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
14471da177e4SLinus Torvalds {
1448ebb3b78dSEric Dumazet 	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) -
144970c26558SEric Dumazet 				  READ_ONCE(sk->sk_backlog.len) -
14501da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
14531da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
14541da177e4SLinus Torvalds {
1455ebb3b78dSEric Dumazet 	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
1458053f3684SWei Wang static inline void tcp_adjust_rcv_ssthresh(struct sock *sk)
1459053f3684SWei Wang {
1460053f3684SWei Wang 	int unused_mem = sk_unused_reserved_mem(sk);
1461053f3684SWei Wang 	struct tcp_sock *tp = tcp_sk(sk);
1462053f3684SWei Wang 
1463053f3684SWei Wang 	tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
1464053f3684SWei Wang 	if (unused_mem)
1465053f3684SWei Wang 		tp->rcv_ssthresh = max_t(u32, tp->rcv_ssthresh,
1466053f3684SWei Wang 					 tcp_win_from_space(sk, unused_mem));
1467053f3684SWei Wang }
1468053f3684SWei Wang 
1469c76c6956SPaolo Abeni void tcp_cleanup_rbuf(struct sock *sk, int copied);
1470c76c6956SPaolo Abeni 
147124adbc16SEric Dumazet /* We provision sk_rcvbuf around 200% of sk_rcvlowat.
147224adbc16SEric Dumazet  * If 87.5 % (7/8) of the space has been consumed, we want to override
147324adbc16SEric Dumazet  * SO_RCVLOWAT constraint, since we are receiving skbs with too small
147424adbc16SEric Dumazet  * len/truesize ratio.
147524adbc16SEric Dumazet  */
147624adbc16SEric Dumazet static inline bool tcp_rmem_pressure(const struct sock *sk)
147724adbc16SEric Dumazet {
1478f969dc5aSEric Dumazet 	int rcvbuf, threshold;
1479f969dc5aSEric Dumazet 
1480f969dc5aSEric Dumazet 	if (tcp_under_memory_pressure(sk))
1481f969dc5aSEric Dumazet 		return true;
1482f969dc5aSEric Dumazet 
1483f969dc5aSEric Dumazet 	rcvbuf = READ_ONCE(sk->sk_rcvbuf);
1484f969dc5aSEric Dumazet 	threshold = rcvbuf - (rcvbuf >> 3);
148524adbc16SEric Dumazet 
148624adbc16SEric Dumazet 	return atomic_read(&sk->sk_rmem_alloc) > threshold;
148724adbc16SEric Dumazet }
148824adbc16SEric Dumazet 
148905dc72abSEric Dumazet static inline bool tcp_epollin_ready(const struct sock *sk, int target)
149005dc72abSEric Dumazet {
149105dc72abSEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
149205dc72abSEric Dumazet 	int avail = READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->copied_seq);
149305dc72abSEric Dumazet 
149405dc72abSEric Dumazet 	if (avail <= 0)
149505dc72abSEric Dumazet 		return false;
149605dc72abSEric Dumazet 
149705dc72abSEric Dumazet 	return (avail >= target) || tcp_rmem_pressure(sk) ||
149805dc72abSEric Dumazet 	       (tcp_receive_window(tp) <= inet_csk(sk)->icsk_ack.rcv_mss);
149905dc72abSEric Dumazet }
150005dc72abSEric Dumazet 
1501843f4a55SYuchung Cheng extern void tcp_openreq_init_rwin(struct request_sock *req,
1502b1964b5fSEric Dumazet 				  const struct sock *sk_listener,
1503b1964b5fSEric Dumazet 				  const struct dst_entry *dst);
1504843f4a55SYuchung Cheng 
15055c9f3023SJoe Perches void tcp_enter_memory_pressure(struct sock *sk);
150606044751SEric Dumazet void tcp_leave_memory_pressure(struct sock *sk);
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
15091da177e4SLinus Torvalds {
1510b840d15dSNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
1511b840d15dSNikolay Borisov 
1512f2f316e2SKuniyuki Iwashima 	return tp->keepalive_intvl ? :
1513f2f316e2SKuniyuki Iwashima 		READ_ONCE(net->ipv4.sysctl_tcp_keepalive_intvl);
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds 
15161da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
15171da177e4SLinus Torvalds {
151813b287e8SNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
151913b287e8SNikolay Borisov 
1520f2f316e2SKuniyuki Iwashima 	return tp->keepalive_time ? :
1521f2f316e2SKuniyuki Iwashima 		READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time);
15221da177e4SLinus Torvalds }
15231da177e4SLinus Torvalds 
1524df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp)
1525df19a626SEric Dumazet {
15269bd6861bSNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
15279bd6861bSNikolay Borisov 
1528f2f316e2SKuniyuki Iwashima 	return tp->keepalive_probes ? :
1529f2f316e2SKuniyuki Iwashima 		READ_ONCE(net->ipv4.sysctl_tcp_keepalive_probes);
1530df19a626SEric Dumazet }
1531df19a626SEric Dumazet 
15326c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
15336c37e5deSFlavio Leitner {
15346c37e5deSFlavio Leitner 	const struct inet_connection_sock *icsk = &tp->inet_conn;
15356c37e5deSFlavio Leitner 
153670eabf0eSEric Dumazet 	return min_t(u32, tcp_jiffies32 - icsk->icsk_ack.lrcvtime,
153770eabf0eSEric Dumazet 			  tcp_jiffies32 - tp->rcv_tstamp);
15386c37e5deSFlavio Leitner }
15396c37e5deSFlavio Leitner 
1540463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
15411da177e4SLinus Torvalds {
154239e24435SKuniyuki Iwashima 	int fin_timeout = tcp_sk(sk)->linger2 ? :
154339e24435SKuniyuki Iwashima 		READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fin_timeout);
1544463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
15451da177e4SLinus Torvalds 
1546463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1547463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
15481da177e4SLinus Torvalds 
15491da177e4SLinus Torvalds 	return fin_timeout;
15501da177e4SLinus Torvalds }
15511da177e4SLinus Torvalds 
1552a2a385d6SEric Dumazet static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
1553c887e6d2SIlpo Järvinen 				  int paws_win)
15541da177e4SLinus Torvalds {
1555c887e6d2SIlpo Järvinen 	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1556a2a385d6SEric Dumazet 		return true;
1557cca9bab1SArnd Bergmann 	if (unlikely(!time_before32(ktime_get_seconds(),
1558cca9bab1SArnd Bergmann 				    rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)))
1559a2a385d6SEric Dumazet 		return true;
1560bc2ce894SEric Dumazet 	/*
1561bc2ce894SEric Dumazet 	 * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
1562bc2ce894SEric Dumazet 	 * then following tcp messages have valid values. Ignore 0 value,
1563bc2ce894SEric Dumazet 	 * or else 'negative' tsval might forbid us to accept their packets.
1564bc2ce894SEric Dumazet 	 */
1565bc2ce894SEric Dumazet 	if (!rx_opt->ts_recent)
1566a2a385d6SEric Dumazet 		return true;
1567a2a385d6SEric Dumazet 	return false;
1568c887e6d2SIlpo Järvinen }
1569c887e6d2SIlpo Järvinen 
1570a2a385d6SEric Dumazet static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
1571c887e6d2SIlpo Järvinen 				   int rst)
1572c887e6d2SIlpo Järvinen {
1573c887e6d2SIlpo Järvinen 	if (tcp_paws_check(rx_opt, 0))
1574a2a385d6SEric Dumazet 		return false;
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
15771da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
15781da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
15791da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
15801da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
15811da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
15821da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
15831da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
15841da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
15871da177e4SLinus Torvalds 	 */
1588cca9bab1SArnd Bergmann 	if (rst && !time_before32(ktime_get_seconds(),
1589cca9bab1SArnd Bergmann 				  rx_opt->ts_recent_stamp + TCP_PAWS_MSL))
1590a2a385d6SEric Dumazet 		return false;
1591a2a385d6SEric Dumazet 	return true;
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
15947970ddc8SEric Dumazet bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
15957970ddc8SEric Dumazet 			  int mib_idx, u32 *last_oow_ack_time);
1596032ee423SNeal Cardwell 
1597a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net)
15981da177e4SLinus Torvalds {
15991da177e4SLinus Torvalds 	/* See RFC 2012 */
16006aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOALGORITHM, 1);
16016aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
16026aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
16036aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_MAXCONN, -1);
16041da177e4SLinus Torvalds }
16051da177e4SLinus Torvalds 
16066a438bbeSStephen Hemminger /* from STCP */
1607ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
16080800f170SDavid S. Miller {
16096a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
1610ef9da47cSIlpo Järvinen }
1611ef9da47cSIlpo Järvinen 
1612ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
1613ef9da47cSIlpo Järvinen {
1614ef9da47cSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
16156a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
1616b7689205SIlpo Järvinen }
1617b7689205SIlpo Järvinen 
1618a915da9bSEric Dumazet union tcp_md5_addr {
1619a915da9bSEric Dumazet 	struct in_addr  a4;
1620a915da9bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1621a915da9bSEric Dumazet 	struct in6_addr	a6;
1622a915da9bSEric Dumazet #endif
1623a915da9bSEric Dumazet };
1624a915da9bSEric Dumazet 
1625cfb6eeb4SYOSHIFUJI Hideaki /* - key database */
1626cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key {
1627a915da9bSEric Dumazet 	struct hlist_node	node;
1628cfb6eeb4SYOSHIFUJI Hideaki 	u8			keylen;
1629a915da9bSEric Dumazet 	u8			family; /* AF_INET or AF_INET6 */
16306797318eSIvan Delalande 	u8			prefixlen;
1631a76c2315SLeonard Crestez 	u8			flags;
1632dea53bb8SDavid Ahern 	union tcp_md5_addr	addr;
1633dea53bb8SDavid Ahern 	int			l3index; /* set if key added with L3 scope */
1634a915da9bSEric Dumazet 	u8			key[TCP_MD5SIG_MAXKEYLEN];
1635a915da9bSEric Dumazet 	struct rcu_head		rcu;
1636cfb6eeb4SYOSHIFUJI Hideaki };
1637cfb6eeb4SYOSHIFUJI Hideaki 
1638cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */
1639cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info {
1640a915da9bSEric Dumazet 	struct hlist_head	head;
1641a8afca03SEric Dumazet 	struct rcu_head		rcu;
1642cfb6eeb4SYOSHIFUJI Hideaki };
1643cfb6eeb4SYOSHIFUJI Hideaki 
1644cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */
1645cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr {
1646cfb6eeb4SYOSHIFUJI Hideaki 	__be32		saddr;
1647cfb6eeb4SYOSHIFUJI Hideaki 	__be32		daddr;
1648cfb6eeb4SYOSHIFUJI Hideaki 	__u8		pad;
1649cfb6eeb4SYOSHIFUJI Hideaki 	__u8		protocol;
1650cfb6eeb4SYOSHIFUJI Hideaki 	__be16		len;
1651cfb6eeb4SYOSHIFUJI Hideaki };
1652cfb6eeb4SYOSHIFUJI Hideaki 
1653cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr {
1654cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr	saddr;
1655cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr daddr;
1656cfb6eeb4SYOSHIFUJI Hideaki 	__be32		len;
1657cfb6eeb4SYOSHIFUJI Hideaki 	__be32		protocol;	/* including padding */
1658cfb6eeb4SYOSHIFUJI Hideaki };
1659cfb6eeb4SYOSHIFUJI Hideaki 
1660cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block {
1661cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr ip4;
1662dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1663cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_pseudohdr ip6;
1664cfb6eeb4SYOSHIFUJI Hideaki #endif
1665cfb6eeb4SYOSHIFUJI Hideaki };
1666cfb6eeb4SYOSHIFUJI Hideaki 
1667cfb6eeb4SYOSHIFUJI Hideaki /* - pool: digest algorithm, hash description and scratch buffer */
1668cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool {
1669cf80e0e4SHerbert Xu 	struct ahash_request	*md5_req;
167019689e38SEric Dumazet 	void			*scratch;
1671cfb6eeb4SYOSHIFUJI Hideaki };
1672cfb6eeb4SYOSHIFUJI Hideaki 
1673cfb6eeb4SYOSHIFUJI Hideaki /* - functions */
167439f8e58eSEric Dumazet int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
167539f8e58eSEric Dumazet 			const struct sock *sk, const struct sk_buff *skb);
16765c9f3023SJoe Perches int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
1677a76c2315SLeonard Crestez 		   int family, u8 prefixlen, int l3index, u8 flags,
1678dea53bb8SDavid Ahern 		   const u8 *newkey, u8 newkeylen, gfp_t gfp);
16795c9f3023SJoe Perches int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
1680a76c2315SLeonard Crestez 		   int family, u8 prefixlen, int l3index, u8 flags);
1681b83e3debSEric Dumazet struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
1682fd3a154aSEric Dumazet 					 const struct sock *addr_sk);
1683cfb6eeb4SYOSHIFUJI Hideaki 
16849501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
16856015c71eSEric Dumazet #include <linux/jump_label.h>
1686921f9a0fSEric Dumazet extern struct static_key_false tcp_md5_needed;
1687dea53bb8SDavid Ahern struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index,
16885c9f3023SJoe Perches 					   const union tcp_md5_addr *addr,
16895c9f3023SJoe Perches 					   int family);
16906015c71eSEric Dumazet static inline struct tcp_md5sig_key *
1691dea53bb8SDavid Ahern tcp_md5_do_lookup(const struct sock *sk, int l3index,
1692dea53bb8SDavid Ahern 		  const union tcp_md5_addr *addr, int family)
16936015c71eSEric Dumazet {
1694921f9a0fSEric Dumazet 	if (!static_branch_unlikely(&tcp_md5_needed))
16956015c71eSEric Dumazet 		return NULL;
1696dea53bb8SDavid Ahern 	return __tcp_md5_do_lookup(sk, l3index, addr, family);
16976015c71eSEric Dumazet }
16981330b6efSJakub Kicinski 
16991330b6efSJakub Kicinski enum skb_drop_reason
17001330b6efSJakub Kicinski tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb,
17017bbb765bSDmitry Safonov 		     const void *saddr, const void *daddr,
17027bbb765bSDmitry Safonov 		     int family, int dif, int sdif);
17037bbb765bSDmitry Safonov 
17046015c71eSEric Dumazet 
1705a915da9bSEric Dumazet #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_key)
17069501f972SYOSHIFUJI Hideaki #else
1707dea53bb8SDavid Ahern static inline struct tcp_md5sig_key *
1708dea53bb8SDavid Ahern tcp_md5_do_lookup(const struct sock *sk, int l3index,
1709dea53bb8SDavid Ahern 		  const union tcp_md5_addr *addr, int family)
1710a915da9bSEric Dumazet {
1711a915da9bSEric Dumazet 	return NULL;
1712a915da9bSEric Dumazet }
17131330b6efSJakub Kicinski 
17141330b6efSJakub Kicinski static inline enum skb_drop_reason
17151330b6efSJakub Kicinski tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb,
17167bbb765bSDmitry Safonov 		     const void *saddr, const void *daddr,
171724055bb8SVladimir Oltean 		     int family, int dif, int sdif)
17187bbb765bSDmitry Safonov {
17191330b6efSJakub Kicinski 	return SKB_NOT_DROPPED_YET;
17207bbb765bSDmitry Safonov }
17219501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	NULL
17229501f972SYOSHIFUJI Hideaki #endif
17239501f972SYOSHIFUJI Hideaki 
17245c9f3023SJoe Perches bool tcp_alloc_md5sig_pool(void);
1725cfb6eeb4SYOSHIFUJI Hideaki 
17265c9f3023SJoe Perches struct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
172771cea17eSEric Dumazet static inline void tcp_put_md5sig_pool(void)
172871cea17eSEric Dumazet {
172971cea17eSEric Dumazet 	local_bh_enable();
173071cea17eSEric Dumazet }
173135790c04SEric Dumazet 
17325c9f3023SJoe Perches int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
173395c96174SEric Dumazet 			  unsigned int header_len);
17345c9f3023SJoe Perches int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
1735cf533ea5SEric Dumazet 		     const struct tcp_md5sig_key *key);
1736cfb6eeb4SYOSHIFUJI Hideaki 
173710467163SJerry Chu /* From tcp_fastopen.c */
17385c9f3023SJoe Perches void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
17397268586bSYuchung Cheng 			    struct tcp_fastopen_cookie *cookie);
17405c9f3023SJoe Perches void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
17412646c831SDaniel Lee 			    struct tcp_fastopen_cookie *cookie, bool syn_lost,
17422646c831SDaniel Lee 			    u16 try_exp);
1743783237e8SYuchung Cheng struct tcp_fastopen_request {
1744783237e8SYuchung Cheng 	/* Fast Open cookie. Size 0 means a cookie request */
1745783237e8SYuchung Cheng 	struct tcp_fastopen_cookie	cookie;
1746783237e8SYuchung Cheng 	struct msghdr			*data;  /* data in MSG_FASTOPEN */
1747f5ddcbbbSEric Dumazet 	size_t				size;
1748f5ddcbbbSEric Dumazet 	int				copied;	/* queued in tcp_connect() */
1749f859a448SWillem de Bruijn 	struct ubuf_info		*uarg;
1750783237e8SYuchung Cheng };
1751783237e8SYuchung Cheng void tcp_free_fastopen_req(struct tcp_sock *tp);
17521fba70e5SYuchung Cheng void tcp_fastopen_destroy_cipher(struct sock *sk);
175343713848SHaishuang Yan void tcp_fastopen_ctx_destroy(struct net *net);
17541fba70e5SYuchung Cheng int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
1755438ac880SArd Biesheuvel 			      void *primary_key, void *backup_key);
1756f19008e6SJason Baron int tcp_fastopen_get_cipher(struct net *net, struct inet_connection_sock *icsk,
1757f19008e6SJason Baron 			    u64 *key);
175861d2bcaeSEric Dumazet void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb);
17597c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
17605b7ed089SYuchung Cheng 			      struct request_sock *req,
176171c02379SChristoph Paasch 			      struct tcp_fastopen_cookie *foc,
176271c02379SChristoph Paasch 			      const struct dst_entry *dst);
176343713848SHaishuang Yan void tcp_fastopen_init_key_once(struct net *net);
1764065263f4SWei Wang bool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss,
1765065263f4SWei Wang 			     struct tcp_fastopen_cookie *cookie);
176619f6d3f3SWei Wang bool tcp_fastopen_defer_connect(struct sock *sk, int *err);
1767438ac880SArd Biesheuvel #define TCP_FASTOPEN_KEY_LENGTH sizeof(siphash_key_t)
17689092a76dSJason Baron #define TCP_FASTOPEN_KEY_MAX 2
17699092a76dSJason Baron #define TCP_FASTOPEN_KEY_BUF_LENGTH \
17709092a76dSJason Baron 	(TCP_FASTOPEN_KEY_LENGTH * TCP_FASTOPEN_KEY_MAX)
177110467163SJerry Chu 
177210467163SJerry Chu /* Fastopen key context */
177310467163SJerry Chu struct tcp_fastopen_context {
1774438ac880SArd Biesheuvel 	siphash_key_t	key[TCP_FASTOPEN_KEY_MAX];
1775c681edaeSArd Biesheuvel 	int		num;
177610467163SJerry Chu 	struct rcu_head	rcu;
177710467163SJerry Chu };
177810467163SJerry Chu 
177946c2fa39SWei Wang void tcp_fastopen_active_disable(struct sock *sk);
1780cf1ef3f0SWei Wang bool tcp_fastopen_active_should_disable(struct sock *sk);
1781cf1ef3f0SWei Wang void tcp_fastopen_active_disable_ofo_check(struct sock *sk);
17827268586bSYuchung Cheng void tcp_fastopen_active_detect_blackhole(struct sock *sk, bool expired);
1783cf1ef3f0SWei Wang 
17849092a76dSJason Baron /* Caller needs to wrap with rcu_read_(un)lock() */
17859092a76dSJason Baron static inline
17869092a76dSJason Baron struct tcp_fastopen_context *tcp_fastopen_get_ctx(const struct sock *sk)
17879092a76dSJason Baron {
17889092a76dSJason Baron 	struct tcp_fastopen_context *ctx;
17899092a76dSJason Baron 
17909092a76dSJason Baron 	ctx = rcu_dereference(inet_csk(sk)->icsk_accept_queue.fastopenq.ctx);
17919092a76dSJason Baron 	if (!ctx)
17929092a76dSJason Baron 		ctx = rcu_dereference(sock_net(sk)->ipv4.tcp_fastopen_ctx);
17939092a76dSJason Baron 	return ctx;
17949092a76dSJason Baron }
17959092a76dSJason Baron 
17969092a76dSJason Baron static inline
17979092a76dSJason Baron bool tcp_fastopen_cookie_match(const struct tcp_fastopen_cookie *foc,
17989092a76dSJason Baron 			       const struct tcp_fastopen_cookie *orig)
17999092a76dSJason Baron {
18009092a76dSJason Baron 	if (orig->len == TCP_FASTOPEN_COOKIE_SIZE &&
18019092a76dSJason Baron 	    orig->len == foc->len &&
18029092a76dSJason Baron 	    !memcmp(orig->val, foc->val, foc->len))
18039092a76dSJason Baron 		return true;
18049092a76dSJason Baron 	return false;
18059092a76dSJason Baron }
18069092a76dSJason Baron 
18079092a76dSJason Baron static inline
18089092a76dSJason Baron int tcp_fastopen_context_len(const struct tcp_fastopen_context *ctx)
18099092a76dSJason Baron {
1810c681edaeSArd Biesheuvel 	return ctx->num;
18119092a76dSJason Baron }
18129092a76dSJason Baron 
181305b055e8SFrancis Yan /* Latencies incurred by various limits for a sender. They are
181405b055e8SFrancis Yan  * chronograph-like stats that are mutually exclusive.
181505b055e8SFrancis Yan  */
181605b055e8SFrancis Yan enum tcp_chrono {
181705b055e8SFrancis Yan 	TCP_CHRONO_UNSPEC,
181805b055e8SFrancis Yan 	TCP_CHRONO_BUSY, /* Actively sending data (non-empty write queue) */
181905b055e8SFrancis Yan 	TCP_CHRONO_RWND_LIMITED, /* Stalled by insufficient receive window */
182005b055e8SFrancis Yan 	TCP_CHRONO_SNDBUF_LIMITED, /* Stalled by insufficient send buffer */
182105b055e8SFrancis Yan 	__TCP_CHRONO_MAX,
182205b055e8SFrancis Yan };
182305b055e8SFrancis Yan 
182405b055e8SFrancis Yan void tcp_chrono_start(struct sock *sk, const enum tcp_chrono type);
182505b055e8SFrancis Yan void tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type);
182605b055e8SFrancis Yan 
1827e2080072SEric Dumazet /* This helper is needed, because skb->tcp_tsorted_anchor uses
1828e2080072SEric Dumazet  * the same memory storage than skb->destructor/_skb_refdst
1829e2080072SEric Dumazet  */
1830e2080072SEric Dumazet static inline void tcp_skb_tsorted_anchor_cleanup(struct sk_buff *skb)
1831e2080072SEric Dumazet {
1832e2080072SEric Dumazet 	skb->destructor = NULL;
1833e2080072SEric Dumazet 	skb->_skb_refdst = 0UL;
1834e2080072SEric Dumazet }
1835e2080072SEric Dumazet 
1836e2080072SEric Dumazet #define tcp_skb_tsorted_save(skb) {		\
1837e2080072SEric Dumazet 	unsigned long _save = skb->_skb_refdst;	\
1838e2080072SEric Dumazet 	skb->_skb_refdst = 0UL;
1839e2080072SEric Dumazet 
1840e2080072SEric Dumazet #define tcp_skb_tsorted_restore(skb)		\
1841e2080072SEric Dumazet 	skb->_skb_refdst = _save;		\
1842e2080072SEric Dumazet }
1843e2080072SEric Dumazet 
1844ac3f09baSEric Dumazet void tcp_write_queue_purge(struct sock *sk);
1845fe067e8aSDavid S. Miller 
184675c119afSEric Dumazet static inline struct sk_buff *tcp_rtx_queue_head(const struct sock *sk)
184775c119afSEric Dumazet {
184875c119afSEric Dumazet 	return skb_rb_first(&sk->tcp_rtx_queue);
184975c119afSEric Dumazet }
185075c119afSEric Dumazet 
1851b617158dSEric Dumazet static inline struct sk_buff *tcp_rtx_queue_tail(const struct sock *sk)
1852b617158dSEric Dumazet {
1853b617158dSEric Dumazet 	return skb_rb_last(&sk->tcp_rtx_queue);
1854b617158dSEric Dumazet }
1855b617158dSEric Dumazet 
1856cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
1857fe067e8aSDavid S. Miller {
1858cd07a8eaSDavid S. Miller 	return skb_peek_tail(&sk->sk_write_queue);
1859fe067e8aSDavid S. Miller }
1860fe067e8aSDavid S. Miller 
1861234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
1862cd07a8eaSDavid S. Miller 	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1863234b6860SIlpo Järvinen 
1864cf533ea5SEric Dumazet static inline struct sk_buff *tcp_send_head(const struct sock *sk)
1865fe067e8aSDavid S. Miller {
186675c119afSEric Dumazet 	return skb_peek(&sk->sk_write_queue);
1867fe067e8aSDavid S. Miller }
1868fe067e8aSDavid S. Miller 
1869cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk,
1870cd07a8eaSDavid S. Miller 				   const struct sk_buff *skb)
1871cd07a8eaSDavid S. Miller {
1872cd07a8eaSDavid S. Miller 	return skb_queue_is_last(&sk->sk_write_queue, skb);
1873cd07a8eaSDavid S. Miller }
1874cd07a8eaSDavid S. Miller 
1875ee2aabd3SEric Dumazet /**
1876ee2aabd3SEric Dumazet  * tcp_write_queue_empty - test if any payload (or FIN) is available in write queue
1877ee2aabd3SEric Dumazet  * @sk: socket
1878ee2aabd3SEric Dumazet  *
1879ee2aabd3SEric Dumazet  * Since the write queue can have a temporary empty skb in it,
1880ee2aabd3SEric Dumazet  * we must not use "return skb_queue_empty(&sk->sk_write_queue)"
1881ee2aabd3SEric Dumazet  */
188275c119afSEric Dumazet static inline bool tcp_write_queue_empty(const struct sock *sk)
1883fe067e8aSDavid S. Miller {
1884ee2aabd3SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1885ee2aabd3SEric Dumazet 
1886ee2aabd3SEric Dumazet 	return tp->write_seq == tp->snd_nxt;
188775c119afSEric Dumazet }
188875c119afSEric Dumazet 
188975c119afSEric Dumazet static inline bool tcp_rtx_queue_empty(const struct sock *sk)
189075c119afSEric Dumazet {
189175c119afSEric Dumazet 	return RB_EMPTY_ROOT(&sk->tcp_rtx_queue);
189275c119afSEric Dumazet }
189375c119afSEric Dumazet 
189475c119afSEric Dumazet static inline bool tcp_rtx_and_write_queues_empty(const struct sock *sk)
189575c119afSEric Dumazet {
189675c119afSEric Dumazet 	return tcp_rtx_queue_empty(sk) && tcp_write_queue_empty(sk);
1897fe067e8aSDavid S. Miller }
1898fe067e8aSDavid S. Miller 
1899fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1900fe067e8aSDavid S. Miller {
1901a43e052bSEric Dumazet 	__skb_queue_tail(&sk->sk_write_queue, skb);
1902fe067e8aSDavid S. Miller 
1903fe067e8aSDavid S. Miller 	/* Queue it, remembering where we must start sending. */
190450895b9dSEric Dumazet 	if (sk->sk_write_queue.next == skb)
19050f87230dSFrancis Yan 		tcp_chrono_start(sk, TCP_CHRONO_BUSY);
1906fe067e8aSDavid S. Miller }
1907fe067e8aSDavid S. Miller 
190843f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk.  */
1909fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new,
1910fe067e8aSDavid S. Miller 						  struct sk_buff *skb,
1911fe067e8aSDavid S. Miller 						  struct sock *sk)
1912fe067e8aSDavid S. Miller {
191343f59c89SDavid S. Miller 	__skb_queue_before(&sk->sk_write_queue, skb, new);
1914fe067e8aSDavid S. Miller }
1915fe067e8aSDavid S. Miller 
1916fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
1917fe067e8aSDavid S. Miller {
19184a269818SEric Dumazet 	tcp_skb_tsorted_anchor_cleanup(skb);
1919fe067e8aSDavid S. Miller 	__skb_unlink(skb, &sk->sk_write_queue);
1920fe067e8aSDavid S. Miller }
1921fe067e8aSDavid S. Miller 
192275c119afSEric Dumazet void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb);
192375c119afSEric Dumazet 
192475c119afSEric Dumazet static inline void tcp_rtx_queue_unlink(struct sk_buff *skb, struct sock *sk)
1925fe067e8aSDavid S. Miller {
192675c119afSEric Dumazet 	tcp_skb_tsorted_anchor_cleanup(skb);
192775c119afSEric Dumazet 	rb_erase(&skb->rbnode, &sk->tcp_rtx_queue);
192875c119afSEric Dumazet }
192975c119afSEric Dumazet 
193075c119afSEric Dumazet static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct sock *sk)
193175c119afSEric Dumazet {
193275c119afSEric Dumazet 	list_del(&skb->tcp_tsorted_anchor);
193375c119afSEric Dumazet 	tcp_rtx_queue_unlink(skb, sk);
193403271f3aSTalal Ahmad 	tcp_wmem_free_skb(sk, skb);
1935fe067e8aSDavid S. Miller }
1936fe067e8aSDavid S. Miller 
193712d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk)
193812d50c46SKrishna Kumar {
193912d50c46SKrishna Kumar 	if (tcp_send_head(sk)) {
194012d50c46SKrishna Kumar 		struct tcp_sock *tp = tcp_sk(sk);
194112d50c46SKrishna Kumar 
194212d50c46SKrishna Kumar 		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
194312d50c46SKrishna Kumar 	}
194412d50c46SKrishna Kumar }
194512d50c46SKrishna Kumar 
1946ecb97192SNeal Cardwell /* Start sequence of the skb just after the highest skb with SACKed
1947ecb97192SNeal Cardwell  * bit, valid only if sacked_out > 0 or when the caller has ensured
1948ecb97192SNeal Cardwell  * validity by itself.
1949a47e5a98SIlpo Järvinen  */
1950a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
1951a47e5a98SIlpo Järvinen {
1952a47e5a98SIlpo Järvinen 	if (!tp->sacked_out)
1953a47e5a98SIlpo Järvinen 		return tp->snd_una;
19546859d494SIlpo Järvinen 
19556859d494SIlpo Järvinen 	if (tp->highest_sack == NULL)
19566859d494SIlpo Järvinen 		return tp->snd_nxt;
19576859d494SIlpo Järvinen 
1958a47e5a98SIlpo Järvinen 	return TCP_SKB_CB(tp->highest_sack)->seq;
1959a47e5a98SIlpo Järvinen }
1960a47e5a98SIlpo Järvinen 
19616859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
19626859d494SIlpo Järvinen {
196350895b9dSEric Dumazet 	tcp_sk(sk)->highest_sack = skb_rb_next(skb);
19646859d494SIlpo Järvinen }
19656859d494SIlpo Järvinen 
19666859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
19676859d494SIlpo Järvinen {
19686859d494SIlpo Järvinen 	return tcp_sk(sk)->highest_sack;
19696859d494SIlpo Järvinen }
19706859d494SIlpo Järvinen 
19716859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk)
19726859d494SIlpo Järvinen {
197350895b9dSEric Dumazet 	tcp_sk(sk)->highest_sack = tcp_rtx_queue_head(sk);
19746859d494SIlpo Järvinen }
19756859d494SIlpo Järvinen 
19762b7cda9cSEric Dumazet /* Called when old skb is about to be deleted and replaced by new skb */
19772b7cda9cSEric Dumazet static inline void tcp_highest_sack_replace(struct sock *sk,
19786859d494SIlpo Järvinen 					    struct sk_buff *old,
19796859d494SIlpo Järvinen 					    struct sk_buff *new)
19806859d494SIlpo Järvinen {
19812b7cda9cSEric Dumazet 	if (old == tcp_highest_sack(sk))
19826859d494SIlpo Järvinen 		tcp_sk(sk)->highest_sack = new;
19836859d494SIlpo Järvinen }
19846859d494SIlpo Järvinen 
1985b1f0a0e9SFlorian Westphal /* This helper checks if socket has IP_TRANSPARENT set */
1986b1f0a0e9SFlorian Westphal static inline bool inet_sk_transparent(const struct sock *sk)
1987b1f0a0e9SFlorian Westphal {
1988b1f0a0e9SFlorian Westphal 	switch (sk->sk_state) {
1989b1f0a0e9SFlorian Westphal 	case TCP_TIME_WAIT:
1990b1f0a0e9SFlorian Westphal 		return inet_twsk(sk)->tw_transparent;
1991b1f0a0e9SFlorian Westphal 	case TCP_NEW_SYN_RECV:
1992b1f0a0e9SFlorian Westphal 		return inet_rsk(inet_reqsk(sk))->no_srccheck;
1993b1f0a0e9SFlorian Westphal 	}
1994b1f0a0e9SFlorian Westphal 	return inet_sk(sk)->transparent;
1995b1f0a0e9SFlorian Westphal }
1996b1f0a0e9SFlorian Westphal 
19975aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from
19985aa4b32fSAndreas Petlund  * increased latency). Used to trigger latency-reducing mechanisms.
19995aa4b32fSAndreas Petlund  */
2000a2a385d6SEric Dumazet static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
20015aa4b32fSAndreas Petlund {
20025aa4b32fSAndreas Petlund 	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
20035aa4b32fSAndreas Petlund }
20045aa4b32fSAndreas Petlund 
20051da177e4SLinus Torvalds /* /proc */
20061da177e4SLinus Torvalds enum tcp_seq_states {
20071da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
20081da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
20091da177e4SLinus Torvalds };
20101da177e4SLinus Torvalds 
201137d849bbSChristoph Hellwig void *tcp_seq_start(struct seq_file *seq, loff_t *pos);
201237d849bbSChristoph Hellwig void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
201337d849bbSChristoph Hellwig void tcp_seq_stop(struct seq_file *seq, void *v);
201473cb88ecSArjan van de Ven 
20151da177e4SLinus Torvalds struct tcp_seq_afinfo {
20161da177e4SLinus Torvalds 	sa_family_t			family;
20171da177e4SLinus Torvalds };
20181da177e4SLinus Torvalds 
20191da177e4SLinus Torvalds struct tcp_iter_state {
2020a4146b1bSDenis V. Lunev 	struct seq_net_private	p;
20211da177e4SLinus Torvalds 	enum tcp_seq_states	state;
20221da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
2023a7cb5a49SEric W. Biederman 	int			bucket, offset, sbucket, num;
2024a8b690f9STom Herbert 	loff_t			last_pos;
20251da177e4SLinus Torvalds };
20261da177e4SLinus Torvalds 
202720380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
2028c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops;
202920380731SArnaldo Carvalho de Melo 
20305c9f3023SJoe Perches void tcp_v4_destroy_sock(struct sock *sk);
203120380731SArnaldo Carvalho de Melo 
203228be6e07SEric Dumazet struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
2033c8f44affSMichał Mirosław 				netdev_features_t features);
2034d4546c25SDavid Miller struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb);
20355521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp4_gro_complete(struct sk_buff *skb, int thoff));
20365521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb));
20375521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp6_gro_complete(struct sk_buff *skb, int thoff));
20385521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp6_gro_receive(struct list_head *head, struct sk_buff *skb));
20395c9f3023SJoe Perches int tcp_gro_complete(struct sk_buff *skb);
204028850dc7SDaniel Borkmann 
20415c9f3023SJoe Perches void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
2042f4c50d99SHerbert Xu 
2043c9bee3b7SEric Dumazet static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
2044c9bee3b7SEric Dumazet {
20454979f2d9SNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
204655be8736SKuniyuki Iwashima 	return tp->notsent_lowat ?: READ_ONCE(net->ipv4.sysctl_tcp_notsent_lowat);
2047c9bee3b7SEric Dumazet }
2048c9bee3b7SEric Dumazet 
2049d3cd4924SEric Dumazet bool tcp_stream_memory_free(const struct sock *sk, int wake);
2050c9bee3b7SEric Dumazet 
205120380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
20525c9f3023SJoe Perches int tcp4_proc_init(void);
20535c9f3023SJoe Perches void tcp4_proc_exit(void);
205420380731SArnaldo Carvalho de Melo #endif
205520380731SArnaldo Carvalho de Melo 
2056ea3bea3aSEric Dumazet int tcp_rtx_synack(const struct sock *sk, struct request_sock *req);
20571fb6f159SOctavian Purdila int tcp_conn_request(struct request_sock_ops *rsk_ops,
20581fb6f159SOctavian Purdila 		     const struct tcp_request_sock_ops *af_ops,
20591fb6f159SOctavian Purdila 		     struct sock *sk, struct sk_buff *skb);
20605db92c99SOctavian Purdila 
2061cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */
2062cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops {
2063cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
2064b83e3debSEric Dumazet 	struct tcp_md5sig_key	*(*md5_lookup) (const struct sock *sk,
2065fd3a154aSEric Dumazet 						const struct sock *addr_sk);
2066cfb6eeb4SYOSHIFUJI Hideaki 	int		(*calc_md5_hash)(char *location,
206739f8e58eSEric Dumazet 					 const struct tcp_md5sig_key *md5,
2068318cf7aaSEric Dumazet 					 const struct sock *sk,
2069318cf7aaSEric Dumazet 					 const struct sk_buff *skb);
2070cfb6eeb4SYOSHIFUJI Hideaki 	int		(*md5_parse)(struct sock *sk,
20718917a777SIvan Delalande 				     int optname,
2072d4c19c49SChristoph Hellwig 				     sockptr_t optval,
2073cfb6eeb4SYOSHIFUJI Hideaki 				     int optlen);
2074cfb6eeb4SYOSHIFUJI Hideaki #endif
2075cfb6eeb4SYOSHIFUJI Hideaki };
2076cfb6eeb4SYOSHIFUJI Hideaki 
2077cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops {
20782aec4a29SOctavian Purdila 	u16 mss_clamp;
2079cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
2080b83e3debSEric Dumazet 	struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk,
2081fd3a154aSEric Dumazet 						 const struct sock *addr_sk);
2082e3afe7b7SJohn Dykstra 	int		(*calc_md5_hash) (char *location,
208339f8e58eSEric Dumazet 					  const struct tcp_md5sig_key *md5,
2084318cf7aaSEric Dumazet 					  const struct sock *sk,
2085318cf7aaSEric Dumazet 					  const struct sk_buff *skb);
2086cfb6eeb4SYOSHIFUJI Hideaki #endif
2087fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
20883f684b4bSEric Dumazet 	__u32 (*cookie_init_seq)(const struct sk_buff *skb,
2089fb7b37a7SOctavian Purdila 				 __u16 *mss);
2090fb7b37a7SOctavian Purdila #endif
20917ea851d1SFlorian Westphal 	struct dst_entry *(*route_req)(const struct sock *sk,
20927ea851d1SFlorian Westphal 				       struct sk_buff *skb,
20937ea851d1SFlorian Westphal 				       struct flowi *fl,
20947ea851d1SFlorian Westphal 				       struct request_sock *req);
209584b114b9SEric Dumazet 	u32 (*init_seq)(const struct sk_buff *skb);
20965d2ed052SEric Dumazet 	u32 (*init_ts_off)(const struct net *net, const struct sk_buff *skb);
20970f935dbeSEric Dumazet 	int (*send_synack)(const struct sock *sk, struct dst_entry *dst,
2098d6274bd8SOctavian Purdila 			   struct flowi *fl, struct request_sock *req,
2099dc6ef6beSEric Dumazet 			   struct tcp_fastopen_cookie *foc,
2100331fca43SMartin KaFai Lau 			   enum tcp_synack_type synack_type,
2101331fca43SMartin KaFai Lau 			   struct sk_buff *syn_skb);
2102cfb6eeb4SYOSHIFUJI Hideaki };
2103cfb6eeb4SYOSHIFUJI Hideaki 
210435b2c321SMat Martineau extern const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops;
210535b2c321SMat Martineau #if IS_ENABLED(CONFIG_IPV6)
210635b2c321SMat Martineau extern const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops;
210735b2c321SMat Martineau #endif
210835b2c321SMat Martineau 
2109fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
2110fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
21113f684b4bSEric Dumazet 					 const struct sock *sk, struct sk_buff *skb,
2112fb7b37a7SOctavian Purdila 					 __u16 *mss)
2113fb7b37a7SOctavian Purdila {
21143f684b4bSEric Dumazet 	tcp_synq_overflow(sk);
211502a1d6e7SEric Dumazet 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
21163f684b4bSEric Dumazet 	return ops->cookie_init_seq(skb, mss);
2117fb7b37a7SOctavian Purdila }
2118fb7b37a7SOctavian Purdila #else
2119fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
21203f684b4bSEric Dumazet 					 const struct sock *sk, struct sk_buff *skb,
2121fb7b37a7SOctavian Purdila 					 __u16 *mss)
2122fb7b37a7SOctavian Purdila {
2123fb7b37a7SOctavian Purdila 	return 0;
2124fb7b37a7SOctavian Purdila }
2125fb7b37a7SOctavian Purdila #endif
2126fb7b37a7SOctavian Purdila 
21275c9f3023SJoe Perches int tcpv4_offload_init(void);
212828850dc7SDaniel Borkmann 
21295c9f3023SJoe Perches void tcp_v4_init(void);
21305c9f3023SJoe Perches void tcp_init(void);
213120380731SArnaldo Carvalho de Melo 
2132659a8ad5SYuchung Cheng /* tcp_recovery.c */
2133d716bfdbSYuchung Cheng void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb);
21346ac06ecdSYuchung Cheng void tcp_newreno_mark_lost(struct sock *sk, bool snd_una_advanced);
2135b8fef65aSYuchung Cheng extern s32 tcp_rack_skb_timeout(struct tcp_sock *tp, struct sk_buff *skb,
2136b8fef65aSYuchung Cheng 				u32 reo_wnd);
213762d9f1a6SPengcheng Yang extern bool tcp_rack_mark_lost(struct sock *sk);
21381d0833dfSYuchung Cheng extern void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq,
21399a568de4SEric Dumazet 			     u64 xmit_time);
214057dde7f7SYuchung Cheng extern void tcp_rack_reo_timeout(struct sock *sk);
21411f255691SPriyaranjan Jha extern void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs);
2142659a8ad5SYuchung Cheng 
21431a91bb7cSMubashir Adnan Qureshi /* tcp_plb.c */
21441a91bb7cSMubashir Adnan Qureshi 
21451a91bb7cSMubashir Adnan Qureshi /*
21461a91bb7cSMubashir Adnan Qureshi  * Scaling factor for fractions in PLB. For example, tcp_plb_update_state
21471a91bb7cSMubashir Adnan Qureshi  * expects cong_ratio which represents fraction of traffic that experienced
21481a91bb7cSMubashir Adnan Qureshi  * congestion over a single RTT. In order to avoid floating point operations,
21491a91bb7cSMubashir Adnan Qureshi  * this fraction should be mapped to (1 << TCP_PLB_SCALE) and passed in.
21501a91bb7cSMubashir Adnan Qureshi  */
21511a91bb7cSMubashir Adnan Qureshi #define TCP_PLB_SCALE 8
21521a91bb7cSMubashir Adnan Qureshi 
21531a91bb7cSMubashir Adnan Qureshi /* State for PLB (Protective Load Balancing) for a single TCP connection. */
21541a91bb7cSMubashir Adnan Qureshi struct tcp_plb_state {
21551a91bb7cSMubashir Adnan Qureshi 	u8	consec_cong_rounds:5, /* consecutive congested rounds */
21561a91bb7cSMubashir Adnan Qureshi 		unused:3;
21571a91bb7cSMubashir Adnan Qureshi 	u32	pause_until; /* jiffies32 when PLB can resume rerouting */
21581a91bb7cSMubashir Adnan Qureshi };
21591a91bb7cSMubashir Adnan Qureshi 
21601a91bb7cSMubashir Adnan Qureshi static inline void tcp_plb_init(const struct sock *sk,
21611a91bb7cSMubashir Adnan Qureshi 				struct tcp_plb_state *plb)
21621a91bb7cSMubashir Adnan Qureshi {
21631a91bb7cSMubashir Adnan Qureshi 	plb->consec_cong_rounds = 0;
21641a91bb7cSMubashir Adnan Qureshi 	plb->pause_until = 0;
21651a91bb7cSMubashir Adnan Qureshi }
21661a91bb7cSMubashir Adnan Qureshi void tcp_plb_update_state(const struct sock *sk, struct tcp_plb_state *plb,
21671a91bb7cSMubashir Adnan Qureshi 			  const int cong_ratio);
21681a91bb7cSMubashir Adnan Qureshi void tcp_plb_check_rehash(struct sock *sk, struct tcp_plb_state *plb);
21691a91bb7cSMubashir Adnan Qureshi void tcp_plb_update_state_upon_rto(struct sock *sk, struct tcp_plb_state *plb);
21701a91bb7cSMubashir Adnan Qureshi 
2171e1a10ef7SNeal Cardwell /* At how many usecs into the future should the RTO fire? */
2172e1a10ef7SNeal Cardwell static inline s64 tcp_rto_delta_us(const struct sock *sk)
2173e1a10ef7SNeal Cardwell {
217475c119afSEric Dumazet 	const struct sk_buff *skb = tcp_rtx_queue_head(sk);
2175e1a10ef7SNeal Cardwell 	u32 rto = inet_csk(sk)->icsk_rto;
21762fd66ffbSEric Dumazet 	u64 rto_time_stamp_us = tcp_skb_timestamp_us(skb) + jiffies_to_usecs(rto);
2177e1a10ef7SNeal Cardwell 
2178e1a10ef7SNeal Cardwell 	return rto_time_stamp_us - tcp_sk(sk)->tcp_mstamp;
2179e1a10ef7SNeal Cardwell }
2180e1a10ef7SNeal Cardwell 
2181e25f866fSCong Wang /*
2182e25f866fSCong Wang  * Save and compile IPv4 options, return a pointer to it
2183e25f866fSCong Wang  */
218491ed1e66SPaolo Abeni static inline struct ip_options_rcu *tcp_v4_save_options(struct net *net,
218591ed1e66SPaolo Abeni 							 struct sk_buff *skb)
2186e25f866fSCong Wang {
2187e25f866fSCong Wang 	const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
2188e25f866fSCong Wang 	struct ip_options_rcu *dopt = NULL;
2189e25f866fSCong Wang 
2190461b74c3SCong Wang 	if (opt->optlen) {
2191e25f866fSCong Wang 		int opt_size = sizeof(*dopt) + opt->optlen;
2192e25f866fSCong Wang 
2193e25f866fSCong Wang 		dopt = kmalloc(opt_size, GFP_ATOMIC);
219491ed1e66SPaolo Abeni 		if (dopt && __ip_options_echo(net, &dopt->opt, skb, opt)) {
2195e25f866fSCong Wang 			kfree(dopt);
2196e25f866fSCong Wang 			dopt = NULL;
2197e25f866fSCong Wang 		}
2198e25f866fSCong Wang 	}
2199e25f866fSCong Wang 	return dopt;
2200e25f866fSCong Wang }
2201e25f866fSCong Wang 
220298781965SEric Dumazet /* locally generated TCP pure ACKs have skb->truesize == 2
220398781965SEric Dumazet  * (check tcp_send_ack() in net/ipv4/tcp_output.c )
220498781965SEric Dumazet  * This is much faster than dissecting the packet to find out.
220598781965SEric Dumazet  * (Think of GRE encapsulations, IPv4, IPv6, ...)
220698781965SEric Dumazet  */
220798781965SEric Dumazet static inline bool skb_is_tcp_pure_ack(const struct sk_buff *skb)
220898781965SEric Dumazet {
220998781965SEric Dumazet 	return skb->truesize == 2;
221098781965SEric Dumazet }
221198781965SEric Dumazet 
221298781965SEric Dumazet static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
221398781965SEric Dumazet {
221498781965SEric Dumazet 	skb->truesize = 2;
221598781965SEric Dumazet }
221698781965SEric Dumazet 
2217473bd239STom Herbert static inline int tcp_inq(struct sock *sk)
2218473bd239STom Herbert {
2219473bd239STom Herbert 	struct tcp_sock *tp = tcp_sk(sk);
2220473bd239STom Herbert 	int answ;
2221473bd239STom Herbert 
2222473bd239STom Herbert 	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
2223473bd239STom Herbert 		answ = 0;
2224473bd239STom Herbert 	} else if (sock_flag(sk, SOCK_URGINLINE) ||
2225473bd239STom Herbert 		   !tp->urg_data ||
2226473bd239STom Herbert 		   before(tp->urg_seq, tp->copied_seq) ||
2227473bd239STom Herbert 		   !before(tp->urg_seq, tp->rcv_nxt)) {
2228473bd239STom Herbert 
2229473bd239STom Herbert 		answ = tp->rcv_nxt - tp->copied_seq;
2230473bd239STom Herbert 
2231473bd239STom Herbert 		/* Subtract 1, if FIN was received */
2232473bd239STom Herbert 		if (answ && sock_flag(sk, SOCK_DONE))
2233473bd239STom Herbert 			answ--;
2234473bd239STom Herbert 	} else {
2235473bd239STom Herbert 		answ = tp->urg_seq - tp->copied_seq;
2236473bd239STom Herbert 	}
2237473bd239STom Herbert 
2238473bd239STom Herbert 	return answ;
2239473bd239STom Herbert }
2240473bd239STom Herbert 
224132035585STom Herbert int tcp_peek_len(struct socket *sock);
224232035585STom Herbert 
2243a44d6eacSMartin KaFai Lau static inline void tcp_segs_in(struct tcp_sock *tp, const struct sk_buff *skb)
2244a44d6eacSMartin KaFai Lau {
2245a44d6eacSMartin KaFai Lau 	u16 segs_in;
2246a44d6eacSMartin KaFai Lau 
2247a44d6eacSMartin KaFai Lau 	segs_in = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
22480307a0b7SEric Dumazet 
22490307a0b7SEric Dumazet 	/* We update these fields while other threads might
22500307a0b7SEric Dumazet 	 * read them from tcp_get_info()
22510307a0b7SEric Dumazet 	 */
22520307a0b7SEric Dumazet 	WRITE_ONCE(tp->segs_in, tp->segs_in + segs_in);
2253a44d6eacSMartin KaFai Lau 	if (skb->len > tcp_hdrlen(skb))
22540307a0b7SEric Dumazet 		WRITE_ONCE(tp->data_segs_in, tp->data_segs_in + segs_in);
2255a44d6eacSMartin KaFai Lau }
2256a44d6eacSMartin KaFai Lau 
22579caad864SEric Dumazet /*
22589caad864SEric Dumazet  * TCP listen path runs lockless.
22599caad864SEric Dumazet  * We forced "struct sock" to be const qualified to make sure
22609caad864SEric Dumazet  * we don't modify one of its field by mistake.
22619caad864SEric Dumazet  * Here, we increment sk_drops which is an atomic_t, so we can safely
22629caad864SEric Dumazet  * make sock writable again.
22639caad864SEric Dumazet  */
22649caad864SEric Dumazet static inline void tcp_listendrop(const struct sock *sk)
22659caad864SEric Dumazet {
22669caad864SEric Dumazet 	atomic_inc(&((struct sock *)sk)->sk_drops);
226702a1d6e7SEric Dumazet 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
22689caad864SEric Dumazet }
22699caad864SEric Dumazet 
2270218af599SEric Dumazet enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer);
2271218af599SEric Dumazet 
2272734942ccSDave Watson /*
2273734942ccSDave Watson  * Interface for adding Upper Level Protocols over TCP
2274734942ccSDave Watson  */
2275734942ccSDave Watson 
2276734942ccSDave Watson #define TCP_ULP_NAME_MAX	16
2277734942ccSDave Watson #define TCP_ULP_MAX		128
2278734942ccSDave Watson #define TCP_ULP_BUF_MAX		(TCP_ULP_NAME_MAX*TCP_ULP_MAX)
2279734942ccSDave Watson 
2280734942ccSDave Watson struct tcp_ulp_ops {
2281734942ccSDave Watson 	struct list_head	list;
2282734942ccSDave Watson 
2283734942ccSDave Watson 	/* initialize ulp */
2284734942ccSDave Watson 	int (*init)(struct sock *sk);
228595fa1454SJohn Fastabend 	/* update ulp */
228633bfe20dSJohn Fastabend 	void (*update)(struct sock *sk, struct proto *p,
228733bfe20dSJohn Fastabend 		       void (*write_space)(struct sock *sk));
2288734942ccSDave Watson 	/* cleanup ulp */
2289734942ccSDave Watson 	void (*release)(struct sock *sk);
229061723b39SDavide Caratti 	/* diagnostic */
229161723b39SDavide Caratti 	int (*get_info)(const struct sock *sk, struct sk_buff *skb);
229261723b39SDavide Caratti 	size_t (*get_info_size)(const struct sock *sk);
229313230593SMat Martineau 	/* clone ulp */
229413230593SMat Martineau 	void (*clone)(const struct request_sock *req, struct sock *newsk,
229513230593SMat Martineau 		      const gfp_t priority);
2296734942ccSDave Watson 
2297734942ccSDave Watson 	char		name[TCP_ULP_NAME_MAX];
2298734942ccSDave Watson 	struct module	*owner;
2299734942ccSDave Watson };
2300734942ccSDave Watson int tcp_register_ulp(struct tcp_ulp_ops *type);
2301734942ccSDave Watson void tcp_unregister_ulp(struct tcp_ulp_ops *type);
2302734942ccSDave Watson int tcp_set_ulp(struct sock *sk, const char *name);
2303734942ccSDave Watson void tcp_get_available_ulp(char *buf, size_t len);
2304734942ccSDave Watson void tcp_cleanup_ulp(struct sock *sk);
230533bfe20dSJohn Fastabend void tcp_update_ulp(struct sock *sk, struct proto *p,
230633bfe20dSJohn Fastabend 		    void (*write_space)(struct sock *sk));
2307734942ccSDave Watson 
2308037b0b86SDaniel Borkmann #define MODULE_ALIAS_TCP_ULP(name)				\
2309037b0b86SDaniel Borkmann 	__MODULE_INFO(alias, alias_userspace, name);		\
2310037b0b86SDaniel Borkmann 	__MODULE_INFO(alias, alias_tcp_ulp, "tcp-ulp-" name)
2311037b0b86SDaniel Borkmann 
231288759609SCong Wang #ifdef CONFIG_NET_SOCK_MSG
2313604326b4SDaniel Borkmann struct sk_msg;
2314604326b4SDaniel Borkmann struct sk_psock;
2315604326b4SDaniel Borkmann 
231688759609SCong Wang #ifdef CONFIG_BPF_SYSCALL
2317f747632bSLorenz Bauer struct proto *tcp_bpf_get_proto(struct sock *sk, struct sk_psock *psock);
231851e0158aSCong Wang int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
2319f747632bSLorenz Bauer void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
232088759609SCong Wang #endif /* CONFIG_BPF_SYSCALL */
2321f747632bSLorenz Bauer 
2322*a351d608SPengcheng Yang int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
2323*a351d608SPengcheng Yang 			  struct sk_msg *msg, u32 bytes, int flags);
23245da00404SLorenz Bauer #endif /* CONFIG_NET_SOCK_MSG */
2325604326b4SDaniel Borkmann 
232688759609SCong Wang #if !defined(CONFIG_BPF_SYSCALL) || !defined(CONFIG_NET_SOCK_MSG)
232788759609SCong Wang static inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
232888759609SCong Wang {
232988759609SCong Wang }
233088759609SCong Wang #endif
233188759609SCong Wang 
23320813a841SMartin KaFai Lau #ifdef CONFIG_CGROUP_BPF
23330813a841SMartin KaFai Lau static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
23340813a841SMartin KaFai Lau 				      struct sk_buff *skb,
23350813a841SMartin KaFai Lau 				      unsigned int end_offset)
23360813a841SMartin KaFai Lau {
23370813a841SMartin KaFai Lau 	skops->skb = skb;
23380813a841SMartin KaFai Lau 	skops->skb_data_end = skb->data + end_offset;
23390813a841SMartin KaFai Lau }
23400813a841SMartin KaFai Lau #else
23410813a841SMartin KaFai Lau static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
23420813a841SMartin KaFai Lau 				      struct sk_buff *skb,
23430813a841SMartin KaFai Lau 				      unsigned int end_offset)
23440813a841SMartin KaFai Lau {
23450813a841SMartin KaFai Lau }
23460813a841SMartin KaFai Lau #endif
23470813a841SMartin KaFai Lau 
234840304b2aSLawrence Brakmo /* Call BPF_SOCK_OPS program that returns an int. If the return value
234940304b2aSLawrence Brakmo  * is < 0, then the BPF op failed (for example if the loaded BPF
235040304b2aSLawrence Brakmo  * program does not support the chosen operation or there is no BPF
235140304b2aSLawrence Brakmo  * program loaded).
235240304b2aSLawrence Brakmo  */
235340304b2aSLawrence Brakmo #ifdef CONFIG_BPF
2354de525be2SLawrence Brakmo static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
235540304b2aSLawrence Brakmo {
235640304b2aSLawrence Brakmo 	struct bpf_sock_ops_kern sock_ops;
235740304b2aSLawrence Brakmo 	int ret;
235840304b2aSLawrence Brakmo 
2359b73042b8SLawrence Brakmo 	memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
2360f19397a5SLawrence Brakmo 	if (sk_fullsock(sk)) {
2361f19397a5SLawrence Brakmo 		sock_ops.is_fullsock = 1;
236240304b2aSLawrence Brakmo 		sock_owned_by_me(sk);
2363f19397a5SLawrence Brakmo 	}
236440304b2aSLawrence Brakmo 
236540304b2aSLawrence Brakmo 	sock_ops.sk = sk;
236640304b2aSLawrence Brakmo 	sock_ops.op = op;
2367de525be2SLawrence Brakmo 	if (nargs > 0)
2368de525be2SLawrence Brakmo 		memcpy(sock_ops.args, args, nargs * sizeof(*args));
236940304b2aSLawrence Brakmo 
237040304b2aSLawrence Brakmo 	ret = BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
237140304b2aSLawrence Brakmo 	if (ret == 0)
237240304b2aSLawrence Brakmo 		ret = sock_ops.reply;
237340304b2aSLawrence Brakmo 	else
237440304b2aSLawrence Brakmo 		ret = -1;
237540304b2aSLawrence Brakmo 	return ret;
237640304b2aSLawrence Brakmo }
2377de525be2SLawrence Brakmo 
2378de525be2SLawrence Brakmo static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
2379de525be2SLawrence Brakmo {
2380de525be2SLawrence Brakmo 	u32 args[2] = {arg1, arg2};
2381de525be2SLawrence Brakmo 
2382de525be2SLawrence Brakmo 	return tcp_call_bpf(sk, op, 2, args);
2383de525be2SLawrence Brakmo }
2384de525be2SLawrence Brakmo 
2385de525be2SLawrence Brakmo static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
2386de525be2SLawrence Brakmo 				    u32 arg3)
2387de525be2SLawrence Brakmo {
2388de525be2SLawrence Brakmo 	u32 args[3] = {arg1, arg2, arg3};
2389de525be2SLawrence Brakmo 
2390de525be2SLawrence Brakmo 	return tcp_call_bpf(sk, op, 3, args);
2391de525be2SLawrence Brakmo }
2392de525be2SLawrence Brakmo 
239340304b2aSLawrence Brakmo #else
2394de525be2SLawrence Brakmo static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
239540304b2aSLawrence Brakmo {
239640304b2aSLawrence Brakmo 	return -EPERM;
239740304b2aSLawrence Brakmo }
2398de525be2SLawrence Brakmo 
2399de525be2SLawrence Brakmo static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
2400de525be2SLawrence Brakmo {
2401de525be2SLawrence Brakmo 	return -EPERM;
2402de525be2SLawrence Brakmo }
2403de525be2SLawrence Brakmo 
2404de525be2SLawrence Brakmo static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
2405de525be2SLawrence Brakmo 				    u32 arg3)
2406de525be2SLawrence Brakmo {
2407de525be2SLawrence Brakmo 	return -EPERM;
2408de525be2SLawrence Brakmo }
2409de525be2SLawrence Brakmo 
241040304b2aSLawrence Brakmo #endif
241140304b2aSLawrence Brakmo 
24128550f328SLawrence Brakmo static inline u32 tcp_timeout_init(struct sock *sk)
24138550f328SLawrence Brakmo {
24148550f328SLawrence Brakmo 	int timeout;
24158550f328SLawrence Brakmo 
2416de525be2SLawrence Brakmo 	timeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL);
24178550f328SLawrence Brakmo 
24188550f328SLawrence Brakmo 	if (timeout <= 0)
24198550f328SLawrence Brakmo 		timeout = TCP_TIMEOUT_INIT;
24205903123fSAkhmat Karakotov 	return min_t(int, timeout, TCP_RTO_MAX);
24218550f328SLawrence Brakmo }
24228550f328SLawrence Brakmo 
242313d3b1ebSLawrence Brakmo static inline u32 tcp_rwnd_init_bpf(struct sock *sk)
242413d3b1ebSLawrence Brakmo {
242513d3b1ebSLawrence Brakmo 	int rwnd;
242613d3b1ebSLawrence Brakmo 
2427de525be2SLawrence Brakmo 	rwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL);
242813d3b1ebSLawrence Brakmo 
242913d3b1ebSLawrence Brakmo 	if (rwnd < 0)
243013d3b1ebSLawrence Brakmo 		rwnd = 0;
243113d3b1ebSLawrence Brakmo 	return rwnd;
243213d3b1ebSLawrence Brakmo }
243391b5b21cSLawrence Brakmo 
243491b5b21cSLawrence Brakmo static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk)
243591b5b21cSLawrence Brakmo {
2436de525be2SLawrence Brakmo 	return (tcp_call_bpf(sk, BPF_SOCK_OPS_NEEDS_ECN, 0, NULL) == 1);
243791b5b21cSLawrence Brakmo }
243860e2a778SUrsula Braun 
243923729ff2SStanislav Fomichev static inline void tcp_bpf_rtt(struct sock *sk)
244023729ff2SStanislav Fomichev {
2441bef8e263SArnd Bergmann 	if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_RTT_CB_FLAG))
244223729ff2SStanislav Fomichev 		tcp_call_bpf(sk, BPF_SOCK_OPS_RTT_CB, 0, NULL);
244323729ff2SStanislav Fomichev }
244423729ff2SStanislav Fomichev 
244560e2a778SUrsula Braun #if IS_ENABLED(CONFIG_SMC)
244660e2a778SUrsula Braun extern struct static_key_false tcp_have_smc;
244760e2a778SUrsula Braun #endif
24486dac1523SIlya Lesokhin 
24496dac1523SIlya Lesokhin #if IS_ENABLED(CONFIG_TLS_DEVICE)
24506dac1523SIlya Lesokhin void clean_acked_data_enable(struct inet_connection_sock *icsk,
24516dac1523SIlya Lesokhin 			     void (*cad)(struct sock *sk, u32 ack_seq));
24526dac1523SIlya Lesokhin void clean_acked_data_disable(struct inet_connection_sock *icsk);
2453494bc1d2SJakub Kicinski void clean_acked_data_flush(void);
24546dac1523SIlya Lesokhin #endif
24556dac1523SIlya Lesokhin 
2456a842fe14SEric Dumazet DECLARE_STATIC_KEY_FALSE(tcp_tx_delay_enabled);
2457a842fe14SEric Dumazet static inline void tcp_add_tx_delay(struct sk_buff *skb,
2458a842fe14SEric Dumazet 				    const struct tcp_sock *tp)
2459a842fe14SEric Dumazet {
2460a842fe14SEric Dumazet 	if (static_branch_unlikely(&tcp_tx_delay_enabled))
2461a842fe14SEric Dumazet 		skb->skb_mstamp_ns += (u64)tp->tcp_tx_delay * NSEC_PER_USEC;
2462a842fe14SEric Dumazet }
2463a842fe14SEric Dumazet 
2464d6fb396cSEric Dumazet /* Compute Earliest Departure Time for some control packets
2465d6fb396cSEric Dumazet  * like ACK or RST for TIME_WAIT or non ESTABLISHED sockets.
2466d6fb396cSEric Dumazet  */
2467d6fb396cSEric Dumazet static inline u64 tcp_transmit_time(const struct sock *sk)
2468a842fe14SEric Dumazet {
2469a842fe14SEric Dumazet 	if (static_branch_unlikely(&tcp_tx_delay_enabled)) {
2470a842fe14SEric Dumazet 		u32 delay = (sk->sk_state == TCP_TIME_WAIT) ?
2471a842fe14SEric Dumazet 			tcp_twsk(sk)->tw_tx_delay : tcp_sk(sk)->tcp_tx_delay;
2472a842fe14SEric Dumazet 
2473d6fb396cSEric Dumazet 		return tcp_clock_ns() + (u64)delay * NSEC_PER_USEC;
2474a842fe14SEric Dumazet 	}
2475d6fb396cSEric Dumazet 	return 0;
2476a842fe14SEric Dumazet }
2477a842fe14SEric Dumazet 
24781da177e4SLinus Torvalds #endif	/* _TCP_H */
2479