xref: /linux/include/net/tcp.h (revision 1e03d32bea8e782b7d31769c25a5fae8a5044488)
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>
40c845f5f3SDmitry Safonov #include <net/tcp_ao.h>
41bdf1ee5dSIlpo Järvinen #include <net/inet_ecn.h>
420c266898SSatoru SATOH #include <net/dst.h>
4385712484SMat Martineau #include <net/mptcp.h>
44c752f073SArnaldo Carvalho de Melo 
451da177e4SLinus Torvalds #include <linux/seq_file.h>
46180d8cd9SGlauber Costa #include <linux/memcontrol.h>
4740304b2aSLawrence Brakmo #include <linux/bpf-cgroup.h>
48438ac880SArd Biesheuvel #include <linux/siphash.h>
4940304b2aSLawrence Brakmo 
500f7ff927SArnaldo Carvalho de Melo extern struct inet_hashinfo tcp_hashinfo;
511da177e4SLinus Torvalds 
5219757cebSEric Dumazet DECLARE_PER_CPU(unsigned int, tcp_orphan_count);
5319757cebSEric Dumazet int tcp_orphan_count_sum(void);
5419757cebSEric Dumazet 
555c9f3023SJoe Perches void tcp_time_wait(struct sock *sk, int state, int timeo);
561da177e4SLinus Torvalds 
579bacd256SEric Dumazet #define MAX_TCP_HEADER	L1_CACHE_ALIGN(128 + MAX_HEADER)
5833ad798cSAdam Langley #define MAX_TCP_OPTION_SPACE 40
593b4929f6SEric Dumazet #define TCP_MIN_SND_MSS		48
603b4929f6SEric Dumazet #define TCP_MIN_GSO_SIZE	(TCP_MIN_SND_MSS - MAX_TCP_OPTION_SPACE)
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /*
631da177e4SLinus Torvalds  * Never offer a window over 32767 without using window scaling. Some
641da177e4SLinus Torvalds  * poor stacks do signed 16bit maths!
651da177e4SLinus Torvalds  */
661da177e4SLinus Torvalds #define MAX_TCP_WINDOW		32767U
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
691da177e4SLinus Torvalds #define TCP_MIN_MSS		88U
701da177e4SLinus Torvalds 
711555e6fdSJosh Hunt /* The initial MTU to use for probing */
72dcd8fb85SFan Du #define TCP_BASE_MSS		1024
735d424d5aSJohn Heffner 
7405cbc0dbSFan Du /* probing interval, default to 10 minutes as per RFC4821 */
7505cbc0dbSFan Du #define TCP_PROBE_INTERVAL	600
7605cbc0dbSFan Du 
776b58e0a5SFan Du /* Specify interval when tcp mtu probing will stop */
786b58e0a5SFan Du #define TCP_PROBE_THRESHOLD	8
796b58e0a5SFan Du 
801da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
811da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
841da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
851da177e4SLinus Torvalds 
86589c49cbSGao Feng /* Maximal number of window scale according to RFC1323 */
87589c49cbSGao Feng #define TCP_MAX_WSCALE		14U
88589c49cbSGao Feng 
891da177e4SLinus Torvalds /* urg_data states */
901da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
911da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
921da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #define TCP_RETR1	3	/*
951da177e4SLinus Torvalds 				 * This is how many retries it does before it
961da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
971da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
981da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
991da177e4SLinus Torvalds 				 */
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds #define TCP_RETR2	15	/*
1021da177e4SLinus Torvalds 				 * This should take at least
1031da177e4SLinus Torvalds 				 * 90 minutes to time out.
1041da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
1051da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
1061da177e4SLinus Torvalds 				 */
1071da177e4SLinus Torvalds 
1086c9ff979SAlex Bergmann #define TCP_SYN_RETRIES	 6	/* This is how many retries are done
1096c9ff979SAlex Bergmann 				 * when active opening a connection.
1106c9ff979SAlex Bergmann 				 * RFC1122 says the minimum retry MUST
1116c9ff979SAlex Bergmann 				 * be at least 180secs.  Nevertheless
1126c9ff979SAlex Bergmann 				 * this value is corresponding to
1136c9ff979SAlex Bergmann 				 * 63secs of retransmission with the
1146c9ff979SAlex Bergmann 				 * current initial RTO.
1156c9ff979SAlex Bergmann 				 */
1161da177e4SLinus Torvalds 
1176c9ff979SAlex Bergmann #define TCP_SYNACK_RETRIES 5	/* This is how may retries are done
1186c9ff979SAlex Bergmann 				 * when passive opening a connection.
1196c9ff979SAlex Bergmann 				 * This is corresponding to 31secs of
1206c9ff979SAlex Bergmann 				 * retransmission with the current
1216c9ff979SAlex Bergmann 				 * initial RTO.
1226c9ff979SAlex Bergmann 				 */
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1251da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1261da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1271da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1281da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1291da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1301da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1311da177e4SLinus Torvalds 				  */
132f0628c52SCambda Zhu #define TCP_FIN_TIMEOUT_MAX (120 * HZ) /* max TCP_LINGER2 value (two minutes) */
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
13595b9a87cSDavid Morley static_assert((1 << ATO_BITS) > TCP_DELACK_MAX);
13695b9a87cSDavid Morley 
1371da177e4SLinus Torvalds #if HZ >= 100
1381da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1391da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1401da177e4SLinus Torvalds #else
1411da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1421da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1431da177e4SLinus Torvalds #endif
1441da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1451da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
146bb4d991aSYuchung Cheng #define TCP_TIMEOUT_MIN	(2U) /* Min timeout for TCP timers in jiffies */
1471c2709cfSNeal Cardwell 
1481c2709cfSNeal Cardwell #define TCP_TIMEOUT_MIN_US (2*USEC_PER_MSEC) /* Min TCP timeout in microsecs */
1491c2709cfSNeal Cardwell 
150fd4f2ceaSEric Dumazet #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))	/* RFC6298 2.1 initial RTO value	*/
1519ad7c049SJerry Chu #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value, now
1529ad7c049SJerry Chu 						 * used as a fallback RTO for the
1539ad7c049SJerry Chu 						 * initial data transmission if no
1549ad7c049SJerry Chu 						 * valid RTT sample has been acquired,
1559ad7c049SJerry Chu 						 * most likely due to retrans in 3WHS.
1569ad7c049SJerry Chu 						 */
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1591da177e4SLinus Torvalds 					                 * for local resources.
1601da177e4SLinus Torvalds 					                 */
1611da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1621da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1631da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1661da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1671da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1681da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1691da177e4SLinus Torvalds 
170af772144SEric Dumazet /* Ensure that TCP PAWS checks are relaxed after ~2147 seconds
171af772144SEric Dumazet  * to avoid overflows. This assumes a clock smaller than 1 Mhz.
172af772144SEric Dumazet  * Default clock is 1 Khz, tcp_usec_ts uses 1 Mhz.
173af772144SEric Dumazet  */
174af772144SEric Dumazet #define TCP_PAWS_WRAP (INT_MAX / USEC_PER_SEC)
175af772144SEric Dumazet 
1761da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1771da177e4SLinus Torvalds 					 * after this time. It should be equal
1781da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1791da177e4SLinus Torvalds 					 * to provide reliability equal to one
1801da177e4SLinus Torvalds 					 * provided by timewait state.
1811da177e4SLinus Torvalds 					 */
1821da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1831da177e4SLinus Torvalds 					 * timestamps. It must be less than
1841da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1851da177e4SLinus Torvalds 					 */
1861da177e4SLinus Torvalds /*
1871da177e4SLinus Torvalds  *	TCP option
1881da177e4SLinus Torvalds  */
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1911da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1921da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1931da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1941da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1951da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1961da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
197cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
198*1e03d32bSDmitry Safonov #define TCPOPT_AO		29	/* Authentication Option (RFC5925) */
199c74a39c8SMat Martineau #define TCPOPT_MPTCP		30	/* Multipath TCP (RFC6824) */
2007f9b838bSDaniel Lee #define TCPOPT_FASTOPEN		34	/* Fast open (RFC7413) */
2012100c8d2SYuchung Cheng #define TCPOPT_EXP		254	/* Experimental */
2022100c8d2SYuchung Cheng /* Magic number to be after the option value for sharing TCP
2032100c8d2SYuchung Cheng  * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
2042100c8d2SYuchung Cheng  */
2052100c8d2SYuchung Cheng #define TCPOPT_FASTOPEN_MAGIC	0xF989
20660e2a778SUrsula Braun #define TCPOPT_SMC_MAGIC	0xE2D4C3D9
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds /*
2091da177e4SLinus Torvalds  *     TCP option lengths
2101da177e4SLinus Torvalds  */
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds #define TCPOLEN_MSS            4
2131da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
2141da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
2151da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
216cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG         18
2177f9b838bSDaniel Lee #define TCPOLEN_FASTOPEN_BASE  2
2182100c8d2SYuchung Cheng #define TCPOLEN_EXP_FASTOPEN_BASE  4
21960e2a778SUrsula Braun #define TCPOLEN_EXP_SMC_BASE   6
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds /* But this is what stacks really send out. */
2221da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
2231da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
2241da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
2251da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
2261da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
2271da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
228cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED		20
22933ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED		4
23060e2a778SUrsula Braun #define TCPOLEN_EXP_SMC_BASE_ALIGNED	8
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds /* Flags in tp->nonagle */
2331da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
2341da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
235caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
2361da177e4SLinus Torvalds 
23736e31b0aSAndreas Petlund /* TCP thin-stream limits */
23836e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
23936e31b0aSAndreas Petlund 
24021603fc4SJörg Thalheim /* TCP initial congestion window as per rfc6928 */
241442b9635SDavid S. Miller #define TCP_INIT_CWND		10
242442b9635SDavid S. Miller 
243cf60af03SYuchung Cheng /* Bit Flags for sysctl_tcp_fastopen */
244cf60af03SYuchung Cheng #define	TFO_CLIENT_ENABLE	1
24510467163SJerry Chu #define	TFO_SERVER_ENABLE	2
24667da22d2SYuchung Cheng #define	TFO_CLIENT_NO_COOKIE	4	/* Data in SYN w/o cookie option */
247cf60af03SYuchung Cheng 
24810467163SJerry Chu /* Accept SYN data w/o any cookie option */
24910467163SJerry Chu #define	TFO_SERVER_COOKIE_NOT_REQD	0x200
25010467163SJerry Chu 
25110467163SJerry Chu /* Force enable TFO on all listeners, i.e., not requiring the
252cebc5cbaSYuchung Cheng  * TCP_FASTOPEN socket option.
25310467163SJerry Chu  */
25410467163SJerry Chu #define	TFO_SERVER_WO_SOCKOPT1	0x400
25510467163SJerry Chu 
256295ff7edSArnaldo Carvalho de Melo 
2571da177e4SLinus Torvalds /* sysctl variables for tcp */
2581da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
259a4fe34bfSEric W. Biederman extern long sysctl_tcp_mem[3];
260e20223f1SEric Dumazet 
261a0370b3fSYuchung Cheng #define TCP_RACK_LOSS_DETECTION  0x1 /* Use RACK to detect losses */
2621f255691SPriyaranjan Jha #define TCP_RACK_STATIC_REO_WND  0x2 /* Use static RACK reo wnd */
26320b654dfSYuchung Cheng #define TCP_RACK_NO_DUPTHRESH    0x4 /* Do not use DUPACK threshold in RACK */
264a0370b3fSYuchung Cheng 
2658d987e5cSEric Dumazet extern atomic_long_t tcp_memory_allocated;
2660defbb0aSEric Dumazet DECLARE_PER_CPU(int, tcp_memory_per_cpu_fw_alloc);
2670defbb0aSEric Dumazet 
2681748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated;
26906044751SEric Dumazet extern unsigned long tcp_memory_pressure;
2701da177e4SLinus Torvalds 
271b8da51ebSEric Dumazet /* optimized version of sk_under_memory_pressure() for TCP sockets */
272b8da51ebSEric Dumazet static inline bool tcp_under_memory_pressure(const struct sock *sk)
273b8da51ebSEric Dumazet {
274baac50bbSJohannes Weiner 	if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
275baac50bbSJohannes Weiner 	    mem_cgroup_under_socket_pressure(sk->sk_memcg))
276e805605cSJohannes Weiner 		return true;
277b8da51ebSEric Dumazet 
2781f142c17SEric Dumazet 	return READ_ONCE(tcp_memory_pressure);
279b8da51ebSEric Dumazet }
2801da177e4SLinus Torvalds /*
2811da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
2821da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
2831da177e4SLinus Torvalds  */
2841da177e4SLinus Torvalds 
285a2a385d6SEric Dumazet static inline bool before(__u32 seq1, __u32 seq2)
2861da177e4SLinus Torvalds {
2870d630cc0SGerrit Renker         return (__s32)(seq1-seq2) < 0;
2881da177e4SLinus Torvalds }
2899a036b9cSGerrit Renker #define after(seq2, seq1) 	before(seq1, seq2)
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
292a2a385d6SEric Dumazet static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
2931da177e4SLinus Torvalds {
2941da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds 
297efcdbf24SArun Sharma static inline bool tcp_out_of_memory(struct sock *sk)
298efcdbf24SArun Sharma {
299efcdbf24SArun Sharma 	if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
300efcdbf24SArun Sharma 	    sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
301efcdbf24SArun Sharma 		return true;
302efcdbf24SArun Sharma 	return false;
303efcdbf24SArun Sharma }
304efcdbf24SArun Sharma 
30503271f3aSTalal Ahmad static inline void tcp_wmem_free_skb(struct sock *sk, struct sk_buff *skb)
30603271f3aSTalal Ahmad {
30703271f3aSTalal Ahmad 	sk_wmem_queued_add(sk, -skb->truesize);
3089b65b17dSTalal Ahmad 	if (!skb_zcopy_pure(skb))
30903271f3aSTalal Ahmad 		sk_mem_uncharge(sk, skb->truesize);
3109b65b17dSTalal Ahmad 	else
3119b65b17dSTalal Ahmad 		sk_mem_uncharge(sk, SKB_TRUESIZE(skb_end_offset(skb)));
31203271f3aSTalal Ahmad 	__kfree_skb(skb);
31303271f3aSTalal Ahmad }
31403271f3aSTalal Ahmad 
315a6c5ea4cSEric Dumazet void sk_forced_mem_schedule(struct sock *sk, int size);
316a6c5ea4cSEric Dumazet 
3175c9f3023SJoe Perches bool tcp_check_oom(struct sock *sk, int shift);
318efcdbf24SArun Sharma 
319a0f82f64SFlorian Westphal 
3201da177e4SLinus Torvalds extern struct proto tcp_prot;
3211da177e4SLinus Torvalds 
32257ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
32313415e46SEric Dumazet #define __TCP_INC_STATS(net, field)	__SNMP_INC_STATS((net)->mib.tcp_statistics, field)
32457ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
325aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
3261da177e4SLinus Torvalds 
3275c9f3023SJoe Perches void tcp_tasklet_init(void);
32846d3ceabSEric Dumazet 
32932bbd879SStefano Brivio int tcp_v4_err(struct sk_buff *skb, u32);
3301da177e4SLinus Torvalds 
3315c9f3023SJoe Perches void tcp_shutdown(struct sock *sk, int how);
3321da177e4SLinus Torvalds 
3337487449cSPaolo Abeni int tcp_v4_early_demux(struct sk_buff *skb);
3345c9f3023SJoe Perches int tcp_v4_rcv(struct sk_buff *skb);
3351da177e4SLinus Torvalds 
33627728ba8SEric Dumazet void tcp_remove_empty_skb(struct sock *sk);
3371b784140SYing Xue int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
338306b13ebSTom Herbert int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size);
3393242abebSBenjamin Hesmans int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, int *copied,
3403242abebSBenjamin Hesmans 			 size_t size, struct ubuf_info *uarg);
3411d7e4538SDavid Howells void tcp_splice_eof(struct socket *sock);
34235b2c321SMat Martineau int tcp_send_mss(struct sock *sk, int *size_goal, int flags);
343fbf93406SEric Dumazet int tcp_wmem_schedule(struct sock *sk, int copy);
34435b2c321SMat Martineau void tcp_push(struct sock *sk, int flags, int mss_now, int nonagle,
34535b2c321SMat Martineau 	      int size_goal);
3465c9f3023SJoe Perches void tcp_release_cb(struct sock *sk);
3475c9f3023SJoe Perches void tcp_wfree(struct sk_buff *skb);
3485c9f3023SJoe Perches void tcp_write_timer_handler(struct sock *sk);
3495c9f3023SJoe Perches void tcp_delack_timer_handler(struct sock *sk);
350e1d001faSBreno Leitao int tcp_ioctl(struct sock *sk, int cmd, int *karg);
35172ab4a86SEric Dumazet int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb);
3523d97d88eSYafang Shao void tcp_rcv_established(struct sock *sk, struct sk_buff *skb);
3535c9f3023SJoe Perches void tcp_rcv_space_adjust(struct sock *sk);
3545c9f3023SJoe Perches int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
3555c9f3023SJoe Perches void tcp_twsk_destructor(struct sock *sk);
356edc12f03SKuniyuki Iwashima void tcp_twsk_purge(struct list_head *net_exit_list, int family);
3575c9f3023SJoe Perches ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
35853d3176bSChangli Gao 			struct pipe_inode_info *pipe, size_t len,
35953d3176bSChangli Gao 			unsigned int flags);
3605882efffSEric Dumazet struct sk_buff *tcp_stream_alloc_skb(struct sock *sk, gfp_t gfp,
361f8dd3b8dSEric Dumazet 				     bool force_schedule);
3629c55e01cSJens Axboe 
363059217c1SNeal Cardwell static inline void tcp_dec_quickack_mode(struct sock *sk)
3641da177e4SLinus Torvalds {
365463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
366fc6415bcSDavid S. Miller 
367463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
368059217c1SNeal Cardwell 		/* How many ACKs S/ACKing new data have we sent? */
369059217c1SNeal Cardwell 		const unsigned int pkts = inet_csk_ack_scheduled(sk) ? 1 : 0;
370059217c1SNeal Cardwell 
371463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
372463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3731da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
374463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
375fc6415bcSDavid S. Miller 		} else
376463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3771da177e4SLinus Torvalds 	}
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
380bdf1ee5dSIlpo Järvinen #define	TCP_ECN_OK		1
381bdf1ee5dSIlpo Järvinen #define	TCP_ECN_QUEUE_CWR	2
382bdf1ee5dSIlpo Järvinen #define	TCP_ECN_DEMAND_CWR	4
3837a269ffaSEric Dumazet #define	TCP_ECN_SEEN		8
384bdf1ee5dSIlpo Järvinen 
385fd2c3ef7SEric Dumazet enum tcp_tw_status {
3861da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
3871da177e4SLinus Torvalds 	TCP_TW_RST = 1,
3881da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
3891da177e4SLinus Torvalds 	TCP_TW_SYN = 3
3901da177e4SLinus Torvalds };
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds 
3935c9f3023SJoe Perches enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
3941da177e4SLinus Torvalds 					      struct sk_buff *skb,
3958feaf0c0SArnaldo Carvalho de Melo 					      const struct tcphdr *th);
3965c9f3023SJoe Perches struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
397e0f9759fSEric Dumazet 			   struct request_sock *req, bool fastopen,
398e0f9759fSEric Dumazet 			   bool *lost_race);
3995c9f3023SJoe Perches int tcp_child_process(struct sock *parent, struct sock *child,
4001da177e4SLinus Torvalds 		      struct sk_buff *skb);
4015ae344c9SNeal Cardwell void tcp_enter_loss(struct sock *sk);
4027e901ee7SYuchung Cheng void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int newly_lost, int flag);
4035c9f3023SJoe Perches void tcp_clear_retrans(struct tcp_sock *tp);
4045c9f3023SJoe Perches void tcp_update_metrics(struct sock *sk);
4055c9f3023SJoe Perches void tcp_init_metrics(struct sock *sk);
4065c9f3023SJoe Perches void tcp_metrics_init(void);
407d82bae12SSoheil Hassas Yeganeh bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst);
40877c3c956SPaolo Abeni void __tcp_close(struct sock *sk, long timeout);
4095c9f3023SJoe Perches void tcp_close(struct sock *sk, long timeout);
4105c9f3023SJoe Perches void tcp_init_sock(struct sock *sk);
41172be0fe6SMartin KaFai Lau void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb);
412a11e1d43SLinus Torvalds __poll_t tcp_poll(struct file *file, struct socket *sock,
413a11e1d43SLinus Torvalds 		      struct poll_table_struct *wait);
414273b7f0fSMartin KaFai Lau int do_tcp_getsockopt(struct sock *sk, int level,
415273b7f0fSMartin KaFai Lau 		      int optname, sockptr_t optval, sockptr_t optlen);
4165c9f3023SJoe Perches int tcp_getsockopt(struct sock *sk, int level, int optname,
4173fdadf7dSDmitry Mishin 		   char __user *optval, int __user *optlen);
4189cacf81fSStanislav Fomichev bool tcp_bpf_bypass_getsockopt(int level, int optname);
4190c751f70SMartin KaFai Lau int do_tcp_setsockopt(struct sock *sk, int level, int optname,
4200c751f70SMartin KaFai Lau 		      sockptr_t optval, unsigned int optlen);
421a7b75c5aSChristoph Hellwig int tcp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
422a7b75c5aSChristoph Hellwig 		   unsigned int optlen);
4235c9f3023SJoe Perches void tcp_set_keepalive(struct sock *sk, int val);
42442cb80a2SEric Dumazet void tcp_syn_ack_timeout(const struct request_sock *req);
425ec095263SOliver Hartkopp int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
4261b784140SYing Xue 		int flags, int *addr_len);
427d1361840SEric Dumazet int tcp_set_rcvlowat(struct sock *sk, int val);
428cb811109SPrankur gupta int tcp_set_window_clamp(struct sock *sk, int val);
429892bfd3dSFlorian Westphal void tcp_update_recv_tstamps(struct sk_buff *skb,
430892bfd3dSFlorian Westphal 			     struct scm_timestamping_internal *tss);
431892bfd3dSFlorian Westphal void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk,
432892bfd3dSFlorian Westphal 			struct scm_timestamping_internal *tss);
43303f45c88SEric Dumazet void tcp_data_ready(struct sock *sk);
434340a6f3dSYafang Shao #ifdef CONFIG_MMU
43593ab6cc6SEric Dumazet int tcp_mmap(struct file *file, struct socket *sock,
43693ab6cc6SEric Dumazet 	     struct vm_area_struct *vma);
437340a6f3dSYafang Shao #endif
438eed29f17SEric Dumazet void tcp_parse_options(const struct net *net, const struct sk_buff *skb,
4391a2c6181SChristoph Paasch 		       struct tcp_options_received *opt_rx,
4402100c8d2SYuchung Cheng 		       int estab, struct tcp_fastopen_cookie *foc);
4415c9f3023SJoe Perches const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
4427d5d5525SYOSHIFUJI Hideaki 
4431da177e4SLinus Torvalds /*
4449349d600SPetar Penkov  *	BPF SKB-less helpers
4459349d600SPetar Penkov  */
4469349d600SPetar Penkov u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph,
4479349d600SPetar Penkov 			 struct tcphdr *th, u32 *cookie);
4489349d600SPetar Penkov u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph,
4499349d600SPetar Penkov 			 struct tcphdr *th, u32 *cookie);
45033bf9885SMaxim Mikityanskiy u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss);
4519349d600SPetar Penkov u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
4529349d600SPetar Penkov 			  const struct tcp_request_sock_ops *af_ops,
4539349d600SPetar Penkov 			  struct sock *sk, struct tcphdr *th);
4549349d600SPetar Penkov /*
4551da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4561da177e4SLinus Torvalds  */
4571da177e4SLinus Torvalds 
4585c9f3023SJoe Perches void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
4594fab9071SNeal Cardwell void tcp_v4_mtu_reduced(struct sock *sk);
4609cf74903SEric Dumazet void tcp_req_err(struct sock *sk, u32 seq, bool abort);
461d2924569SEric Dumazet void tcp_ld_RTO_revert(struct sock *sk, u32 seq);
4625c9f3023SJoe Perches int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
463c28c6f04SEric Dumazet struct sock *tcp_create_openreq_child(const struct sock *sk,
46460236fddSArnaldo Carvalho de Melo 				      struct request_sock *req,
4651da177e4SLinus Torvalds 				      struct sk_buff *skb);
46681164413SDaniel Borkmann void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst);
4670c27171eSEric Dumazet struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
46860236fddSArnaldo Carvalho de Melo 				  struct request_sock *req,
4695e0724d0SEric Dumazet 				  struct dst_entry *dst,
4705e0724d0SEric Dumazet 				  struct request_sock *req_unhash,
4715e0724d0SEric Dumazet 				  bool *own_req);
4725c9f3023SJoe Perches int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
4735c9f3023SJoe Perches int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
4745c9f3023SJoe Perches int tcp_connect(struct sock *sk);
475b3d05147SEric Dumazet enum tcp_synack_type {
476b3d05147SEric Dumazet 	TCP_SYNACK_NORMAL,
477b3d05147SEric Dumazet 	TCP_SYNACK_FASTOPEN,
478b3d05147SEric Dumazet 	TCP_SYNACK_COOKIE,
479b3d05147SEric Dumazet };
4805d062de7SEric Dumazet struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
481e6b4d113SWilliam Allen Simpson 				struct request_sock *req,
482ca6fb065SEric Dumazet 				struct tcp_fastopen_cookie *foc,
483331fca43SMartin KaFai Lau 				enum tcp_synack_type synack_type,
484331fca43SMartin KaFai Lau 				struct sk_buff *syn_skb);
4855c9f3023SJoe Perches int tcp_disconnect(struct sock *sk, int flags);
4861da177e4SLinus Torvalds 
487370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
488292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
48963d02d15SEric Dumazet void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /* From syncookies.c */
492b80c0e78SEric Dumazet struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
493b80c0e78SEric Dumazet 				 struct request_sock *req,
49484b114b9SEric Dumazet 				 struct dst_entry *dst, u32 tsoff);
4955c9f3023SJoe Perches int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
4960198230bSPatrick McHardy 		      u32 cookie);
497461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
4986fc8c827SFlorian Westphal struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
499ba5a4fddSEric Dumazet 					    const struct tcp_request_sock_ops *af_ops,
5006fc8c827SFlorian Westphal 					    struct sock *sk, struct sk_buff *skb);
501e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
5028c27bd75SFlorian Westphal 
50363262315SEric Dumazet /* Syncookies use a monotonic timer which increments every 60 seconds.
5048c27bd75SFlorian Westphal  * This counter is used both as a hash input and partially encoded into
5058c27bd75SFlorian Westphal  * the cookie value.  A cookie is only validated further if the delta
5068c27bd75SFlorian Westphal  * between the current counter value and the encoded one is less than this,
50763262315SEric Dumazet  * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
5088c27bd75SFlorian Westphal  * the counter advances immediately after a cookie is generated).
5098c27bd75SFlorian Westphal  */
5108c27bd75SFlorian Westphal #define MAX_SYNCOOKIE_AGE	2
511264ea103SEric Dumazet #define TCP_SYNCOOKIE_PERIOD	(60 * HZ)
512264ea103SEric Dumazet #define TCP_SYNCOOKIE_VALID	(MAX_SYNCOOKIE_AGE * TCP_SYNCOOKIE_PERIOD)
513264ea103SEric Dumazet 
514264ea103SEric Dumazet /* syncookies: remember time of last synqueue overflow
515264ea103SEric Dumazet  * But do not dirty this field too often (once per second is enough)
5163f684b4bSEric Dumazet  * It is racy as we do not hold a lock, but race is very minor.
517264ea103SEric Dumazet  */
5183f684b4bSEric Dumazet static inline void tcp_synq_overflow(const struct sock *sk)
519264ea103SEric Dumazet {
52040a1227eSMartin KaFai Lau 	unsigned int last_overflow;
521cca9bab1SArnd Bergmann 	unsigned int now = jiffies;
522264ea103SEric Dumazet 
52340a1227eSMartin KaFai Lau 	if (sk->sk_reuseport) {
52440a1227eSMartin KaFai Lau 		struct sock_reuseport *reuse;
52540a1227eSMartin KaFai Lau 
52640a1227eSMartin KaFai Lau 		reuse = rcu_dereference(sk->sk_reuseport_cb);
52740a1227eSMartin KaFai Lau 		if (likely(reuse)) {
52840a1227eSMartin KaFai Lau 			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
52904d26e7bSGuillaume Nault 			if (!time_between32(now, last_overflow,
53004d26e7bSGuillaume Nault 					    last_overflow + HZ))
53140a1227eSMartin KaFai Lau 				WRITE_ONCE(reuse->synq_overflow_ts, now);
53240a1227eSMartin KaFai Lau 			return;
53340a1227eSMartin KaFai Lau 		}
53440a1227eSMartin KaFai Lau 	}
53540a1227eSMartin KaFai Lau 
536721c8dafSGuillaume Nault 	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
53704d26e7bSGuillaume Nault 	if (!time_between32(now, last_overflow, last_overflow + HZ))
538e9d9da91SEric Dumazet 		WRITE_ONCE(tcp_sk_rw(sk)->rx_opt.ts_recent_stamp, now);
539264ea103SEric Dumazet }
540264ea103SEric Dumazet 
541264ea103SEric Dumazet /* syncookies: no recent synqueue overflow on this listening socket? */
542264ea103SEric Dumazet static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
543264ea103SEric Dumazet {
54440a1227eSMartin KaFai Lau 	unsigned int last_overflow;
545cca9bab1SArnd Bergmann 	unsigned int now = jiffies;
546264ea103SEric Dumazet 
54740a1227eSMartin KaFai Lau 	if (sk->sk_reuseport) {
54840a1227eSMartin KaFai Lau 		struct sock_reuseport *reuse;
54940a1227eSMartin KaFai Lau 
55040a1227eSMartin KaFai Lau 		reuse = rcu_dereference(sk->sk_reuseport_cb);
55140a1227eSMartin KaFai Lau 		if (likely(reuse)) {
55240a1227eSMartin KaFai Lau 			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
553cb44a08fSGuillaume Nault 			return !time_between32(now, last_overflow - HZ,
554cb44a08fSGuillaume Nault 					       last_overflow +
55540a1227eSMartin KaFai Lau 					       TCP_SYNCOOKIE_VALID);
55640a1227eSMartin KaFai Lau 		}
55740a1227eSMartin KaFai Lau 	}
55840a1227eSMartin KaFai Lau 
559721c8dafSGuillaume Nault 	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
560cb44a08fSGuillaume Nault 
561cb44a08fSGuillaume Nault 	/* If last_overflow <= jiffies <= last_overflow + TCP_SYNCOOKIE_VALID,
562cb44a08fSGuillaume Nault 	 * then we're under synflood. However, we have to use
563cb44a08fSGuillaume Nault 	 * 'last_overflow - HZ' as lower bound. That's because a concurrent
564cb44a08fSGuillaume Nault 	 * tcp_synq_overflow() could update .ts_recent_stamp after we read
565cb44a08fSGuillaume Nault 	 * jiffies but before we store .ts_recent_stamp into last_overflow,
566cb44a08fSGuillaume Nault 	 * which could lead to rejecting a valid syncookie.
567cb44a08fSGuillaume Nault 	 */
568cb44a08fSGuillaume Nault 	return !time_between32(now, last_overflow - HZ,
569cb44a08fSGuillaume Nault 			       last_overflow + TCP_SYNCOOKIE_VALID);
570264ea103SEric Dumazet }
5718c27bd75SFlorian Westphal 
5728c27bd75SFlorian Westphal static inline u32 tcp_cookie_time(void)
5738c27bd75SFlorian Westphal {
57463262315SEric Dumazet 	u64 val = get_jiffies_64();
57563262315SEric Dumazet 
576264ea103SEric Dumazet 	do_div(val, TCP_SYNCOOKIE_PERIOD);
57763262315SEric Dumazet 	return val;
5788c27bd75SFlorian Westphal }
5798c27bd75SFlorian Westphal 
5805c9f3023SJoe Perches u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
5815c9f3023SJoe Perches 			      u16 *mssp);
5823f684b4bSEric Dumazet __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss);
583200ecef6SEric Dumazet u64 cookie_init_timestamp(struct request_sock *req, u64 now);
584f9301034SEric Dumazet bool cookie_timestamp_decode(const struct net *net,
585f9301034SEric Dumazet 			     struct tcp_options_received *opt);
586f1673381SFlorian Westphal bool cookie_ecn_ok(const struct tcp_options_received *opt,
587f7b3bec6SFlorian Westphal 		   const struct net *net, const struct dst_entry *dst);
5884dfc2817SFlorian Westphal 
589c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */
5905c9f3023SJoe Perches int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
59181eb6a14SPatrick McHardy 		      u32 cookie);
5925c9f3023SJoe Perches struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
593f1673381SFlorian Westphal 
5945c9f3023SJoe Perches u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
59581eb6a14SPatrick McHardy 			      const struct tcphdr *th, u16 *mssp);
5963f684b4bSEric Dumazet __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mss);
597e05c82d3SEric Dumazet #endif
5981da177e4SLinus Torvalds /* tcp_output.c */
5991da177e4SLinus Torvalds 
60004d8825cSPaolo Abeni void tcp_skb_entail(struct sock *sk, struct sk_buff *skb);
60104d8825cSPaolo Abeni void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb);
6025c9f3023SJoe Perches void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
6039e412ba7SIlpo Järvinen 			       int nonagle);
60410d3be56SEric Dumazet int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
60510d3be56SEric Dumazet int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
6065c9f3023SJoe Perches void tcp_retransmit_timer(struct sock *sk);
6075c9f3023SJoe Perches void tcp_xmit_retransmit_queue(struct sock *);
6085c9f3023SJoe Perches void tcp_simple_retransmit(struct sock *);
60957dde7f7SYuchung Cheng void tcp_enter_recovery(struct sock *sk, bool ece_ack);
6105c9f3023SJoe Perches int tcp_trim_head(struct sock *, struct sk_buff *, u32);
61175c119afSEric Dumazet enum tcp_queue {
61275c119afSEric Dumazet 	TCP_FRAG_IN_WRITE_QUEUE,
61375c119afSEric Dumazet 	TCP_FRAG_IN_RTX_QUEUE,
61475c119afSEric Dumazet };
61575c119afSEric Dumazet int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
61675c119afSEric Dumazet 		 struct sk_buff *skb, u32 len,
61775c119afSEric Dumazet 		 unsigned int mss_now, gfp_t gfp);
6181da177e4SLinus Torvalds 
6195c9f3023SJoe Perches void tcp_send_probe0(struct sock *);
620e520af48SEric Dumazet int tcp_write_wakeup(struct sock *, int mib);
6215c9f3023SJoe Perches void tcp_send_fin(struct sock *sk);
6225c9f3023SJoe Perches void tcp_send_active_reset(struct sock *sk, gfp_t priority);
6235c9f3023SJoe Perches int tcp_send_synack(struct sock *);
6245c9f3023SJoe Perches void tcp_push_one(struct sock *, unsigned int mss_now);
62527cde44aSYuchung Cheng void __tcp_send_ack(struct sock *sk, u32 rcv_nxt);
6265c9f3023SJoe Perches void tcp_send_ack(struct sock *sk);
6275c9f3023SJoe Perches void tcp_send_delayed_ack(struct sock *sk);
6285c9f3023SJoe Perches void tcp_send_loss_probe(struct sock *sk);
629ed66dfafSNeal Cardwell bool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto);
630cfea5a68SMartin KaFai Lau void tcp_skb_collapse_tstamp(struct sk_buff *skb,
631cfea5a68SMartin KaFai Lau 			     const struct sk_buff *next_skb);
6321da177e4SLinus Torvalds 
633a762a980SDavid S. Miller /* tcp_input.c */
6345c9f3023SJoe Perches void tcp_rearm_rto(struct sock *sk);
6350f1c28aeSYuchung Cheng void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req);
636049fe386SFlorian Westphal void tcp_reset(struct sock *sk, struct sk_buff *skb);
637e3e17b77SEric Dumazet void tcp_fin(struct sock *sk);
6384bfe744fSEric Dumazet void tcp_check_space(struct sock *sk);
63930c6f0bfSfuyuanli void tcp_sack_compress_send_ack(struct sock *sk);
640a762a980SDavid S. Miller 
6411da177e4SLinus Torvalds /* tcp_timer.c */
6425c9f3023SJoe Perches void tcp_init_xmit_timers(struct sock *);
643463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
644463c84b9SArnaldo Carvalho de Melo {
64573a6bab5SEric Dumazet 	if (hrtimer_try_to_cancel(&tcp_sk(sk)->pacing_timer) == 1)
646cf0dd203SEric Dumazet 		__sock_put(sk);
64773a6bab5SEric Dumazet 
6485d9f4262SEric Dumazet 	if (hrtimer_try_to_cancel(&tcp_sk(sk)->compressed_ack_timer) == 1)
6495d9f4262SEric Dumazet 		__sock_put(sk);
6505d9f4262SEric Dumazet 
651463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
652463c84b9SArnaldo Carvalho de Melo }
6531da177e4SLinus Torvalds 
6545c9f3023SJoe Perches unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
6555c9f3023SJoe Perches unsigned int tcp_current_mss(struct sock *sk);
656344db93aSEnke Chen u32 tcp_clamp_probe0_to_user_timeout(const struct sock *sk, u32 when);
6570c54b85fSIlpo Järvinen 
6580c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */
6590c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
6600c54b85fSIlpo Järvinen {
66101f83d69SAlexey Kuznetsov 	int cutoff;
66201f83d69SAlexey Kuznetsov 
66301f83d69SAlexey Kuznetsov 	/* When peer uses tiny windows, there is no use in packetizing
66401f83d69SAlexey Kuznetsov 	 * to sub-MSS pieces for the sake of SWS or making sure there
66501f83d69SAlexey Kuznetsov 	 * are enough packets in the pipe for fast recovery.
66601f83d69SAlexey Kuznetsov 	 *
66701f83d69SAlexey Kuznetsov 	 * On the other hand, for extremely large MSS devices, handling
66801f83d69SAlexey Kuznetsov 	 * smaller than MSS windows in this way does make sense.
66901f83d69SAlexey Kuznetsov 	 */
6702631b79fSSeymour, Shane M 	if (tp->max_window > TCP_MSS_DEFAULT)
67101f83d69SAlexey Kuznetsov 		cutoff = (tp->max_window >> 1);
67201f83d69SAlexey Kuznetsov 	else
67301f83d69SAlexey Kuznetsov 		cutoff = tp->max_window;
67401f83d69SAlexey Kuznetsov 
67501f83d69SAlexey Kuznetsov 	if (cutoff && pktsize > cutoff)
67601f83d69SAlexey Kuznetsov 		return max_t(int, cutoff, 68U - tp->tcp_header_len);
6770c54b85fSIlpo Järvinen 	else
6780c54b85fSIlpo Järvinen 		return pktsize;
6790c54b85fSIlpo Järvinen }
6801da177e4SLinus Torvalds 
68117b085eaSArnaldo Carvalho de Melo /* tcp.c */
6820df48c26SEric Dumazet void tcp_get_info(struct sock *, struct tcp_info *);
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
6855c9f3023SJoe Perches int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
6861da177e4SLinus Torvalds 		  sk_read_actor_t recv_actor);
687965b57b4SCong Wang int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor);
6883f92a64eSJakub Kicinski struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off);
6893f92a64eSJakub Kicinski void tcp_read_done(struct sock *sk, size_t len);
6901da177e4SLinus Torvalds 
6915c9f3023SJoe Perches void tcp_initialize_rcv_mss(struct sock *sk);
6921da177e4SLinus Torvalds 
6935c9f3023SJoe Perches int tcp_mtu_to_mss(struct sock *sk, int pmtu);
6945c9f3023SJoe Perches int tcp_mss_to_mtu(struct sock *sk, int mss);
6955c9f3023SJoe Perches void tcp_mtup_init(struct sock *sk);
6965d424d5aSJohn Heffner 
697f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk)
698f1ecd5d9SDamian Lukowski {
699f1ecd5d9SDamian Lukowski 	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
700f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
701f1ecd5d9SDamian Lukowski }
702f1ecd5d9SDamian Lukowski 
703f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
704f1ecd5d9SDamian Lukowski {
705740b0f18SEric Dumazet 	return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
706f1ecd5d9SDamian Lukowski }
707f1ecd5d9SDamian Lukowski 
70831770e34SFlorian Westphal static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
70931770e34SFlorian Westphal {
71071158bb1SPaolo Abeni 	/* mptcp hooks are only on the slow path */
71171158bb1SPaolo Abeni 	if (sk_is_mptcp((struct sock *)tp))
71271158bb1SPaolo Abeni 		return;
71371158bb1SPaolo Abeni 
71431770e34SFlorian Westphal 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
71531770e34SFlorian Westphal 			       ntohl(TCP_FLAG_ACK) |
71631770e34SFlorian Westphal 			       snd_wnd);
71731770e34SFlorian Westphal }
71831770e34SFlorian Westphal 
71931770e34SFlorian Westphal static inline void tcp_fast_path_on(struct tcp_sock *tp)
72031770e34SFlorian Westphal {
72131770e34SFlorian Westphal 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
72231770e34SFlorian Westphal }
72331770e34SFlorian Westphal 
72431770e34SFlorian Westphal static inline void tcp_fast_path_check(struct sock *sk)
72531770e34SFlorian Westphal {
72631770e34SFlorian Westphal 	struct tcp_sock *tp = tcp_sk(sk);
72731770e34SFlorian Westphal 
72831770e34SFlorian Westphal 	if (RB_EMPTY_ROOT(&tp->out_of_order_queue) &&
72931770e34SFlorian Westphal 	    tp->rcv_wnd &&
73031770e34SFlorian Westphal 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
73131770e34SFlorian Westphal 	    !tp->urg_data)
73231770e34SFlorian Westphal 		tcp_fast_path_on(tp);
73331770e34SFlorian Westphal }
73431770e34SFlorian Westphal 
735bbf80d71SEric Dumazet u32 tcp_delack_max(const struct sock *sk);
736bbf80d71SEric Dumazet 
7370c266898SSatoru SATOH /* Compute the actual rto_min value */
738f68a181fSEric Dumazet static inline u32 tcp_rto_min(const struct sock *sk)
7390c266898SSatoru SATOH {
740cf533ea5SEric Dumazet 	const struct dst_entry *dst = __sk_dst_get(sk);
741ca584ba0SMartin KaFai Lau 	u32 rto_min = inet_csk(sk)->icsk_rto_min;
7420c266898SSatoru SATOH 
7430c266898SSatoru SATOH 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
7440c266898SSatoru SATOH 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
7450c266898SSatoru SATOH 	return rto_min;
7460c266898SSatoru SATOH }
7470c266898SSatoru SATOH 
748f68a181fSEric Dumazet static inline u32 tcp_rto_min_us(const struct sock *sk)
749740b0f18SEric Dumazet {
750740b0f18SEric Dumazet 	return jiffies_to_usecs(tcp_rto_min(sk));
751740b0f18SEric Dumazet }
752740b0f18SEric Dumazet 
75381164413SDaniel Borkmann static inline bool tcp_ca_dst_locked(const struct dst_entry *dst)
75481164413SDaniel Borkmann {
75581164413SDaniel Borkmann 	return dst_metric_locked(dst, RTAX_CC_ALGO);
75681164413SDaniel Borkmann }
75781164413SDaniel Borkmann 
758f6722583SYuchung Cheng /* Minimum RTT in usec. ~0 means not available. */
759f6722583SYuchung Cheng static inline u32 tcp_min_rtt(const struct tcp_sock *tp)
760f6722583SYuchung Cheng {
76164033892SNeal Cardwell 	return minmax_get(&tp->rtt_min);
762f6722583SYuchung Cheng }
763f6722583SYuchung Cheng 
7641da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
7651da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
7661da177e4SLinus Torvalds  * than the offered window.
7671da177e4SLinus Torvalds  */
76840efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	if (win < 0)
7731da177e4SLinus Torvalds 		win = 0;
7741da177e4SLinus Torvalds 	return (u32) win;
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
7781da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
7791da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
7801da177e4SLinus Torvalds  */
7815c9f3023SJoe Perches u32 __tcp_select_window(struct sock *sk);
7821da177e4SLinus Torvalds 
783ee995283SPavel Emelyanov void tcp_send_window_probe(struct sock *sk);
784ee995283SPavel Emelyanov 
785ec66eda8SEric Dumazet /* TCP uses 32bit jiffies to save some space.
786ec66eda8SEric Dumazet  * Note that this is different from tcp_time_stamp, which
787ec66eda8SEric Dumazet  * historically has been the same until linux-4.13.
788ec66eda8SEric Dumazet  */
789ec66eda8SEric Dumazet #define tcp_jiffies32 ((u32)jiffies)
790ec66eda8SEric Dumazet 
7919a568de4SEric Dumazet /*
7929a568de4SEric Dumazet  * Deliver a 32bit value for TCP timestamp option (RFC 7323)
7939a568de4SEric Dumazet  * It is no longer tied to jiffies, but to 1 ms clock.
7949a568de4SEric Dumazet  * Note: double check if you want to use tcp_jiffies32 instead of this.
7951da177e4SLinus Torvalds  */
7969a568de4SEric Dumazet #define TCP_TS_HZ	1000
7979a568de4SEric Dumazet 
7989a568de4SEric Dumazet static inline u64 tcp_clock_ns(void)
7999a568de4SEric Dumazet {
800fb420d5dSEric Dumazet 	return ktime_get_ns();
8019a568de4SEric Dumazet }
8029a568de4SEric Dumazet 
8039a568de4SEric Dumazet static inline u64 tcp_clock_us(void)
8049a568de4SEric Dumazet {
8059a568de4SEric Dumazet 	return div_u64(tcp_clock_ns(), NSEC_PER_USEC);
8069a568de4SEric Dumazet }
8079a568de4SEric Dumazet 
8082a7c8d29SEric Dumazet static inline u64 tcp_clock_ms(void)
8092a7c8d29SEric Dumazet {
8102a7c8d29SEric Dumazet 	return div_u64(tcp_clock_ns(), NSEC_PER_MSEC);
8112a7c8d29SEric Dumazet }
8122a7c8d29SEric Dumazet 
81316cf6477SEric Dumazet /* TCP Timestamp included in TS option (RFC 1323) can either use ms
81416cf6477SEric Dumazet  * or usec resolution. Each socket carries a flag to select one or other
81516cf6477SEric Dumazet  * resolution, as the route attribute could change anytime.
81616cf6477SEric Dumazet  * Each flow must stick to initial resolution.
81716cf6477SEric Dumazet  */
81816cf6477SEric Dumazet static inline u32 tcp_clock_ts(bool usec_ts)
81916cf6477SEric Dumazet {
82016cf6477SEric Dumazet 	return usec_ts ? tcp_clock_us() : tcp_clock_ms();
82116cf6477SEric Dumazet }
82216cf6477SEric Dumazet 
82399d67955SEric Dumazet static inline u32 tcp_time_stamp_ms(const struct tcp_sock *tp)
82499d67955SEric Dumazet {
82599d67955SEric Dumazet 	return div_u64(tp->tcp_mstamp, USEC_PER_MSEC);
82699d67955SEric Dumazet }
82799d67955SEric Dumazet 
8289d0c00f5SEric Dumazet static inline u32 tcp_time_stamp_ts(const struct tcp_sock *tp)
8299d0c00f5SEric Dumazet {
830614e8316SEric Dumazet 	if (tp->tcp_usec_ts)
831614e8316SEric Dumazet 		return tp->tcp_mstamp;
8329d0c00f5SEric Dumazet 	return tcp_time_stamp_ms(tp);
8339d0c00f5SEric Dumazet }
8349d0c00f5SEric Dumazet 
8359799ccb0SEric Dumazet void tcp_mstamp_refresh(struct tcp_sock *tp);
8369a568de4SEric Dumazet 
8379a568de4SEric Dumazet static inline u32 tcp_stamp_us_delta(u64 t1, u64 t0)
8389a568de4SEric Dumazet {
8399a568de4SEric Dumazet 	return max_t(s64, t1 - t0, 0);
8409a568de4SEric Dumazet }
8411da177e4SLinus Torvalds 
8422fd66ffbSEric Dumazet /* provide the departure time in us unit */
8432fd66ffbSEric Dumazet static inline u64 tcp_skb_timestamp_us(const struct sk_buff *skb)
8442fd66ffbSEric Dumazet {
845d3edd06eSEric Dumazet 	return div_u64(skb->skb_mstamp_ns, NSEC_PER_USEC);
8462fd66ffbSEric Dumazet }
8472fd66ffbSEric Dumazet 
848d1a02ed6SEric Dumazet /* Provide skb TSval in usec or ms unit */
849d1a02ed6SEric Dumazet static inline u32 tcp_skb_timestamp_ts(bool usec_ts, const struct sk_buff *skb)
850d1a02ed6SEric Dumazet {
851d1a02ed6SEric Dumazet 	if (usec_ts)
852d1a02ed6SEric Dumazet 		return tcp_skb_timestamp_us(skb);
853d1a02ed6SEric Dumazet 
854d1a02ed6SEric Dumazet 	return div_u64(skb->skb_mstamp_ns, NSEC_PER_MSEC);
855d1a02ed6SEric Dumazet }
856d1a02ed6SEric Dumazet 
85716cf6477SEric Dumazet static inline u32 tcp_tw_tsval(const struct tcp_timewait_sock *tcptw)
85816cf6477SEric Dumazet {
859614e8316SEric Dumazet 	return tcp_clock_ts(tcptw->tw_sk.tw_usec_ts) + tcptw->tw_ts_offset;
86016cf6477SEric Dumazet }
86116cf6477SEric Dumazet 
86216cf6477SEric Dumazet static inline u32 tcp_rsk_tsval(const struct tcp_request_sock *treq)
86316cf6477SEric Dumazet {
864614e8316SEric Dumazet 	return tcp_clock_ts(treq->req_usec_ts) + treq->ts_off;
86516cf6477SEric Dumazet }
8667faee5c0SEric Dumazet 
867a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
868a3433f35SChangli Gao 
869a3433f35SChangli Gao #define TCPHDR_FIN 0x01
870a3433f35SChangli Gao #define TCPHDR_SYN 0x02
871a3433f35SChangli Gao #define TCPHDR_RST 0x04
872a3433f35SChangli Gao #define TCPHDR_PSH 0x08
873a3433f35SChangli Gao #define TCPHDR_ACK 0x10
874a3433f35SChangli Gao #define TCPHDR_URG 0x20
875a3433f35SChangli Gao #define TCPHDR_ECE 0x40
876a3433f35SChangli Gao #define TCPHDR_CWR 0x80
877a3433f35SChangli Gao 
87849213555SDaniel Borkmann #define TCPHDR_SYN_ECN	(TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)
87949213555SDaniel Borkmann 
880caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
881f86586faSEric Dumazet  * TCP per-packet control information to the transmission code.
882f86586faSEric Dumazet  * We also store the host-order sequence numbers in here too.
883f86586faSEric Dumazet  * This is 44 bytes if IPV6 is enabled.
884f86586faSEric Dumazet  * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
8851da177e4SLinus Torvalds  */
8861da177e4SLinus Torvalds struct tcp_skb_cb {
8871da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
8881da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
889cd7d8498SEric Dumazet 	union {
890cd7d8498SEric Dumazet 		/* Note : tcp_tw_isn is used in input path only
891cd7d8498SEric Dumazet 		 *	  (isn chosen by tcp_timewait_state_process())
892cd7d8498SEric Dumazet 		 *
893f69ad292SEric Dumazet 		 * 	  tcp_gso_segs/size are used in write queue only,
894f69ad292SEric Dumazet 		 *	  cf tcp_skb_pcount()/tcp_skb_mss()
895cd7d8498SEric Dumazet 		 */
896cd7d8498SEric Dumazet 		__u32		tcp_tw_isn;
897f69ad292SEric Dumazet 		struct {
898f69ad292SEric Dumazet 			u16	tcp_gso_segs;
899f69ad292SEric Dumazet 			u16	tcp_gso_size;
900f69ad292SEric Dumazet 		};
901cd7d8498SEric Dumazet 	};
9024de075e0SEric Dumazet 	__u8		tcp_flags;	/* TCP header flags. (tcp[13])	*/
903f4f9f6e7SNeal Cardwell 
904713bafeaSYuchung Cheng 	__u8		sacked;		/* State flags for SACK.	*/
9051da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
9061da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
9071da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
9081da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
909d3edd06eSEric Dumazet #define TCPCB_REPAIRED		0x10	/* SKB repaired (no skb_mstamp_ns)	*/
9101da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
9119d186cacSAndrey Vagin #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
9129d186cacSAndrey Vagin 				TCPCB_REPAIRED)
9131da177e4SLinus Torvalds 
914f4f9f6e7SNeal Cardwell 	__u8		ip_dsfield;	/* IPv4 tos or IPv6 dsfield	*/
9156b084928SSoheil Hassas Yeganeh 	__u8		txstamp_ack:1,	/* Record TX timestamp for ack? */
916c134ecb8SMartin KaFai Lau 			eor:1,		/* Is skb MSG_EOR marked? */
91798aaa913SMike Maloney 			has_rxtstamp:1,	/* SKB has a RX timestamp	*/
91898aaa913SMike Maloney 			unused:5;
9191da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
920971f10ecSEric Dumazet 	union {
921b75803d5SLawrence Brakmo 		struct {
92240bc6063SYuchung Cheng #define TCPCB_DELIVERED_CE_MASK ((1U<<20) - 1)
923b9f64820SYuchung Cheng 			/* There is space for up to 24 bytes */
92440bc6063SYuchung Cheng 			__u32 is_app_limited:1, /* cwnd not fully used? */
92540bc6063SYuchung Cheng 			      delivered_ce:20,
92640bc6063SYuchung Cheng 			      unused:11;
927b9f64820SYuchung Cheng 			/* pkts S/ACKed so far upon tx of skb, incl retrans: */
928b9f64820SYuchung Cheng 			__u32 delivered;
929b9f64820SYuchung Cheng 			/* start of send pipeline phase */
9309a568de4SEric Dumazet 			u64 first_tx_mstamp;
931b9f64820SYuchung Cheng 			/* when we reached the "delivered" count */
9329a568de4SEric Dumazet 			u64 delivered_mstamp;
933b75803d5SLawrence Brakmo 		} tx;   /* only used for outgoing skbs */
934b75803d5SLawrence Brakmo 		union {
935971f10ecSEric Dumazet 			struct inet_skb_parm	h4;
936971f10ecSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
937971f10ecSEric Dumazet 			struct inet6_skb_parm	h6;
938971f10ecSEric Dumazet #endif
939b75803d5SLawrence Brakmo 		} header;	/* For incoming skbs */
940b75803d5SLawrence Brakmo 	};
9411da177e4SLinus Torvalds };
9421da177e4SLinus Torvalds 
9431da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
9441da177e4SLinus Torvalds 
9459b9e2f25SEric Dumazet extern const struct inet_connection_sock_af_ops ipv4_specific;
9469b9e2f25SEric Dumazet 
947815afe17SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
948870c3151SEric Dumazet /* This is the variant of inet6_iif() that must be used by TCP,
949870c3151SEric Dumazet  * as TCP moves IP6CB into a different location in skb->cb[]
950870c3151SEric Dumazet  */
951870c3151SEric Dumazet static inline int tcp_v6_iif(const struct sk_buff *skb)
952870c3151SEric Dumazet {
95324b711edSDavid Ahern 	return TCP_SKB_CB(skb)->header.h6.iif;
95424b711edSDavid Ahern }
95524b711edSDavid Ahern 
95624b711edSDavid Ahern static inline int tcp_v6_iif_l3_slave(const struct sk_buff *skb)
95724b711edSDavid Ahern {
958a04a480dSDavid Ahern 	bool l3_slave = ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags);
95974b20582SDavid Ahern 
96074b20582SDavid Ahern 	return l3_slave ? skb->skb_iif : TCP_SKB_CB(skb)->header.h6.iif;
961870c3151SEric Dumazet }
9624297a0efSDavid Ahern 
9634297a0efSDavid Ahern /* TCP_SKB_CB reference means this can not be used from early demux */
9644297a0efSDavid Ahern static inline int tcp_v6_sdif(const struct sk_buff *skb)
9654297a0efSDavid Ahern {
9664297a0efSDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9674297a0efSDavid Ahern 	if (skb && ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags))
9684297a0efSDavid Ahern 		return TCP_SKB_CB(skb)->header.h6.iif;
9694297a0efSDavid Ahern #endif
9704297a0efSDavid Ahern 	return 0;
9714297a0efSDavid Ahern }
972dd2e0b86SEric Dumazet 
973b03d2142SEric Dumazet extern const struct inet_connection_sock_af_ops ipv6_specific;
974b03d2142SEric Dumazet 
975dd2e0b86SEric Dumazet INDIRECT_CALLABLE_DECLARE(void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb));
976243600eeSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *skb));
97711052589SKuniyuki Iwashima void tcp_v6_early_demux(struct sk_buff *skb);
978dd2e0b86SEric Dumazet 
979815afe17SEric Dumazet #endif
980870c3151SEric Dumazet 
9813fa6f616SDavid Ahern /* TCP_SKB_CB reference means this can not be used from early demux */
9823fa6f616SDavid Ahern static inline int tcp_v4_sdif(struct sk_buff *skb)
9833fa6f616SDavid Ahern {
9843fa6f616SDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9853fa6f616SDavid Ahern 	if (skb && ipv4_l3mdev_skb(TCP_SKB_CB(skb)->header.h4.flags))
9863fa6f616SDavid Ahern 		return TCP_SKB_CB(skb)->header.h4.iif;
9873fa6f616SDavid Ahern #endif
9883fa6f616SDavid Ahern 	return 0;
9893fa6f616SDavid Ahern }
9903fa6f616SDavid Ahern 
9911da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
9921da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
9931da177e4SLinus Torvalds  */
9941da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
9951da177e4SLinus Torvalds {
996cd7d8498SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_segs;
997cd7d8498SEric Dumazet }
998cd7d8498SEric Dumazet 
999cd7d8498SEric Dumazet static inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs)
1000cd7d8498SEric Dumazet {
1001cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs = segs;
1002cd7d8498SEric Dumazet }
1003cd7d8498SEric Dumazet 
1004cd7d8498SEric Dumazet static inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs)
1005cd7d8498SEric Dumazet {
1006cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs += segs;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
1009f69ad292SEric Dumazet /* This is valid iff skb is in write queue and tcp_skb_pcount() > 1. */
10101da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
10111da177e4SLinus Torvalds {
1012f69ad292SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_size;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
1015c134ecb8SMartin KaFai Lau static inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb)
1016c134ecb8SMartin KaFai Lau {
1017c134ecb8SMartin KaFai Lau 	return likely(!TCP_SKB_CB(skb)->eor);
1018c134ecb8SMartin KaFai Lau }
1019c134ecb8SMartin KaFai Lau 
102085712484SMat Martineau static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
102185712484SMat Martineau 					const struct sk_buff *from)
102285712484SMat Martineau {
102385712484SMat Martineau 	return likely(tcp_skb_can_collapse_to(to) &&
10249b65b17dSTalal Ahmad 		      mptcp_skb_can_collapse(to, from) &&
10259b65b17dSTalal Ahmad 		      skb_pure_zcopy_same(to, from));
102685712484SMat Martineau }
102785712484SMat Martineau 
1028317a76f9SStephen Hemminger /* Events passed to congestion control interface */
1029317a76f9SStephen Hemminger enum tcp_ca_event {
1030317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
1031317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
1032317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
1033317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
10349890092eSFlorian Westphal 	CA_EVENT_ECN_NO_CE,	/* ECT set, but not CE marked */
10359890092eSFlorian Westphal 	CA_EVENT_ECN_IS_CE,	/* received CE marked IP packet */
10367354c8c3SFlorian Westphal };
10377354c8c3SFlorian Westphal 
10389890092eSFlorian Westphal /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
10397354c8c3SFlorian Westphal enum tcp_ca_ack_event_flags {
1040c1d2b4c3SFlorian Westphal 	CA_ACK_SLOWPATH		= (1 << 0),	/* In slow path processing */
1041c1d2b4c3SFlorian Westphal 	CA_ACK_WIN_UPDATE	= (1 << 1),	/* ACK updated window */
1042c1d2b4c3SFlorian Westphal 	CA_ACK_ECE		= (1 << 2),	/* ECE bit is set on ack */
1043317a76f9SStephen Hemminger };
1044317a76f9SStephen Hemminger 
1045317a76f9SStephen Hemminger /*
1046317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
1047317a76f9SStephen Hemminger  */
1048317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
10493ff825b2SStephen Hemminger #define TCP_CA_MAX	128
10503ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
10513ff825b2SStephen Hemminger 
1052c5c6a8abSDaniel Borkmann #define TCP_CA_UNSPEC	0
1053c5c6a8abSDaniel Borkmann 
105430e502a3SDaniel Borkmann /* Algorithm can be set on socket without CAP_NET_ADMIN privileges */
1055164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1
105630e502a3SDaniel Borkmann /* Requires ECN/ECT set on all packets */
105730e502a3SDaniel Borkmann #define TCP_CONG_NEEDS_ECN	0x2
10580baf26b0SMartin KaFai Lau #define TCP_CONG_MASK	(TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN)
1059164891aaSStephen Hemminger 
106064f40ff5SEric Dumazet union tcp_cc_info;
106164f40ff5SEric Dumazet 
1062756ee172SLawrence Brakmo struct ack_sample {
1063756ee172SLawrence Brakmo 	u32 pkts_acked;
1064756ee172SLawrence Brakmo 	s32 rtt_us;
10656f094b9eSLawrence Brakmo 	u32 in_flight;
1066756ee172SLawrence Brakmo };
1067756ee172SLawrence Brakmo 
1068b9f64820SYuchung Cheng /* A rate sample measures the number of (original/retransmitted) data
1069b9f64820SYuchung Cheng  * packets delivered "delivered" over an interval of time "interval_us".
1070b9f64820SYuchung Cheng  * The tcp_rate.c code fills in the rate sample, and congestion
1071b9f64820SYuchung Cheng  * control modules that define a cong_control function to run at the end
1072b9f64820SYuchung Cheng  * of ACK processing can optionally chose to consult this sample when
1073b9f64820SYuchung Cheng  * setting cwnd and pacing rate.
1074b9f64820SYuchung Cheng  * A sample is invalid if "delivered" or "interval_us" is negative.
1075b9f64820SYuchung Cheng  */
1076b9f64820SYuchung Cheng struct rate_sample {
10779a568de4SEric Dumazet 	u64  prior_mstamp; /* starting timestamp for interval */
1078b9f64820SYuchung Cheng 	u32  prior_delivered;	/* tp->delivered at "prior_mstamp" */
107940bc6063SYuchung Cheng 	u32  prior_delivered_ce;/* tp->delivered_ce at "prior_mstamp" */
1080b9f64820SYuchung Cheng 	s32  delivered;		/* number of packets delivered over interval */
108140bc6063SYuchung Cheng 	s32  delivered_ce;	/* number of packets delivered w/ CE marks*/
1082b9f64820SYuchung Cheng 	long interval_us;	/* time for tp->delivered to incr "delivered" */
10834929c942SDeepti Raghavan 	u32 snd_interval_us;	/* snd interval for delivered packets */
10844929c942SDeepti Raghavan 	u32 rcv_interval_us;	/* rcv interval for delivered packets */
1085b9f64820SYuchung Cheng 	long rtt_us;		/* RTT of last (S)ACKed packet (or -1) */
1086b9f64820SYuchung Cheng 	int  losses;		/* number of packets marked lost upon ACK */
1087b9f64820SYuchung Cheng 	u32  acked_sacked;	/* number of packets newly (S)ACKed upon ACK */
1088b9f64820SYuchung Cheng 	u32  prior_in_flight;	/* in flight before this ACK */
1089b253a068SPengcheng Yang 	u32  last_end_seq;	/* end_seq of most recently ACKed packet */
1090d7722e85SSoheil Hassas Yeganeh 	bool is_app_limited;	/* is sample from packet with bubble in pipe? */
1091b9f64820SYuchung Cheng 	bool is_retrans;	/* is sample from retransmission? */
1092e4286603SYuchung Cheng 	bool is_ack_delayed;	/* is this (likely) a delayed ACK? */
1093b9f64820SYuchung Cheng };
1094b9f64820SYuchung Cheng 
1095317a76f9SStephen Hemminger struct tcp_congestion_ops {
109682506665SEric Dumazet /* fast path fields are put first to fill one cache line */
109782506665SEric Dumazet 
109882506665SEric Dumazet 	/* return slow start threshold (required) */
109982506665SEric Dumazet 	u32 (*ssthresh)(struct sock *sk);
110082506665SEric Dumazet 
110182506665SEric Dumazet 	/* do new cwnd calculation (required) */
110282506665SEric Dumazet 	void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked);
110382506665SEric Dumazet 
110482506665SEric Dumazet 	/* call before changing ca_state (optional) */
110582506665SEric Dumazet 	void (*set_state)(struct sock *sk, u8 new_state);
110682506665SEric Dumazet 
110782506665SEric Dumazet 	/* call when cwnd event occurs (optional) */
110882506665SEric Dumazet 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
110982506665SEric Dumazet 
111082506665SEric Dumazet 	/* call when ack arrives (optional) */
111182506665SEric Dumazet 	void (*in_ack_event)(struct sock *sk, u32 flags);
111282506665SEric Dumazet 
111382506665SEric Dumazet 	/* hook for packet ack accounting (optional) */
111482506665SEric Dumazet 	void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample);
111582506665SEric Dumazet 
111682506665SEric Dumazet 	/* override sysctl_tcp_min_tso_segs */
111782506665SEric Dumazet 	u32 (*min_tso_segs)(struct sock *sk);
111882506665SEric Dumazet 
111982506665SEric Dumazet 	/* call when packets are delivered to update cwnd and pacing rate,
112082506665SEric Dumazet 	 * after all the ca_state processing. (optional)
112182506665SEric Dumazet 	 */
112282506665SEric Dumazet 	void (*cong_control)(struct sock *sk, const struct rate_sample *rs);
112382506665SEric Dumazet 
112482506665SEric Dumazet 
112582506665SEric Dumazet 	/* new value of cwnd after loss (required) */
112682506665SEric Dumazet 	u32  (*undo_cwnd)(struct sock *sk);
112782506665SEric Dumazet 	/* returns the multiplier used in tcp_sndbuf_expand (optional) */
112882506665SEric Dumazet 	u32 (*sndbuf_expand)(struct sock *sk);
112982506665SEric Dumazet 
113082506665SEric Dumazet /* control/slow paths put last */
113182506665SEric Dumazet 	/* get info for inet_diag (optional) */
113282506665SEric Dumazet 	size_t (*get_info)(struct sock *sk, u32 ext, int *attr,
113382506665SEric Dumazet 			   union tcp_cc_info *info);
113482506665SEric Dumazet 
113582506665SEric Dumazet 	char 			name[TCP_CA_NAME_MAX];
113682506665SEric Dumazet 	struct module		*owner;
1137317a76f9SStephen Hemminger 	struct list_head	list;
1138c5c6a8abSDaniel Borkmann 	u32			key;
1139c5c6a8abSDaniel Borkmann 	u32			flags;
1140317a76f9SStephen Hemminger 
1141317a76f9SStephen Hemminger 	/* initialize private data (optional) */
11426687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
1143317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
11446687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
114582506665SEric Dumazet } ____cacheline_aligned_in_smp;
1146317a76f9SStephen Hemminger 
11475c9f3023SJoe Perches int tcp_register_congestion_control(struct tcp_congestion_ops *type);
11485c9f3023SJoe Perches void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
11498fb1a76aSKui-Feng Lee int tcp_update_congestion_control(struct tcp_congestion_ops *type,
11508fb1a76aSKui-Feng Lee 				  struct tcp_congestion_ops *old_type);
11518fb1a76aSKui-Feng Lee int tcp_validate_congestion_control(struct tcp_congestion_ops *ca);
1152317a76f9SStephen Hemminger 
115355d8694fSFlorian Westphal void tcp_assign_congestion_control(struct sock *sk);
11545c9f3023SJoe Perches void tcp_init_congestion_control(struct sock *sk);
11555c9f3023SJoe Perches void tcp_cleanup_congestion_control(struct sock *sk);
11566670e152SStephen Hemminger int tcp_set_default_congestion_control(struct net *net, const char *name);
11576670e152SStephen Hemminger void tcp_get_default_congestion_control(struct net *net, char *name);
11585c9f3023SJoe Perches void tcp_get_available_congestion_control(char *buf, size_t len);
11595c9f3023SJoe Perches void tcp_get_allowed_congestion_control(char *buf, size_t len);
11605c9f3023SJoe Perches int tcp_set_allowed_congestion_control(char *allowed);
11618d650cdeSEric Dumazet int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
116229a94932SNeal Cardwell 			       bool cap_net_admin);
1163e73ebb08SNeal Cardwell u32 tcp_slow_start(struct tcp_sock *tp, u32 acked);
1164e73ebb08SNeal Cardwell void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked);
1165317a76f9SStephen Hemminger 
11665c9f3023SJoe Perches u32 tcp_reno_ssthresh(struct sock *sk);
1167e9799183SFlorian Westphal u32 tcp_reno_undo_cwnd(struct sock *sk);
116824901551SEric Dumazet void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked);
1169a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
1170317a76f9SStephen Hemminger 
11710baf26b0SMartin KaFai Lau struct tcp_congestion_ops *tcp_ca_find(const char *name);
1172c5c6a8abSDaniel Borkmann struct tcp_congestion_ops *tcp_ca_find_key(u32 key);
11736670e152SStephen Hemminger u32 tcp_ca_get_key_by_name(struct net *net, const char *name, bool *ecn_ca);
1174ea697639SDaniel Borkmann #ifdef CONFIG_INET
1175c5c6a8abSDaniel Borkmann char *tcp_ca_get_name_by_key(u32 key, char *buffer);
1176ea697639SDaniel Borkmann #else
1177ea697639SDaniel Borkmann static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
1178ea697639SDaniel Borkmann {
1179ea697639SDaniel Borkmann 	return NULL;
1180ea697639SDaniel Borkmann }
1181ea697639SDaniel Borkmann #endif
1182c5c6a8abSDaniel Borkmann 
118330e502a3SDaniel Borkmann static inline bool tcp_ca_needs_ecn(const struct sock *sk)
118430e502a3SDaniel Borkmann {
118530e502a3SDaniel Borkmann 	const struct inet_connection_sock *icsk = inet_csk(sk);
118630e502a3SDaniel Borkmann 
118730e502a3SDaniel Borkmann 	return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN;
118830e502a3SDaniel Borkmann }
118930e502a3SDaniel Borkmann 
11906687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
1191317a76f9SStephen Hemminger {
11926687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
11936687e988SArnaldo Carvalho de Melo 
11946687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
11956687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
1196317a76f9SStephen Hemminger }
1197317a76f9SStephen Hemminger 
119815fcdf6aSPing Gan /* From tcp_cong.c */
119915fcdf6aSPing Gan void tcp_set_ca_state(struct sock *sk, const u8 ca_state);
120015fcdf6aSPing Gan 
1201b9f64820SYuchung Cheng /* From tcp_rate.c */
1202b9f64820SYuchung Cheng void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb);
1203b9f64820SYuchung Cheng void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
1204b9f64820SYuchung Cheng 			    struct rate_sample *rs);
1205b9f64820SYuchung Cheng void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
1206d4761754SYousuk Seung 		  bool is_sack_reneg, struct rate_sample *rs);
1207d7722e85SSoheil Hassas Yeganeh void tcp_rate_check_app_limited(struct sock *sk);
1208b9f64820SYuchung Cheng 
1209b253a068SPengcheng Yang static inline bool tcp_skb_sent_after(u64 t1, u64 t2, u32 seq1, u32 seq2)
1210b253a068SPengcheng Yang {
1211b253a068SPengcheng Yang 	return t1 > t2 || (t1 == t2 && after(seq1, seq2));
1212b253a068SPengcheng Yang }
1213b253a068SPengcheng Yang 
1214e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK
1215e60402d0SIlpo Järvinen  * handling. SACK is negotiated with the peer, and therefore it can vary
1216e60402d0SIlpo Järvinen  * between different flows.
1217e60402d0SIlpo Järvinen  *
1218e60402d0SIlpo Järvinen  * tcp_is_sack - SACK enabled
1219e60402d0SIlpo Järvinen  * tcp_is_reno - No SACK
1220e60402d0SIlpo Järvinen  */
1221e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp)
1222e60402d0SIlpo Järvinen {
1223ebeef4bcSEric Dumazet 	return likely(tp->rx_opt.sack_ok);
1224e60402d0SIlpo Järvinen }
1225e60402d0SIlpo Järvinen 
1226a2a385d6SEric Dumazet static inline bool tcp_is_reno(const struct tcp_sock *tp)
1227e60402d0SIlpo Järvinen {
1228e60402d0SIlpo Järvinen 	return !tcp_is_sack(tp);
1229e60402d0SIlpo Järvinen }
1230e60402d0SIlpo Järvinen 
123183ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
123283ae4088SIlpo Järvinen {
123383ae4088SIlpo Järvinen 	return tp->sacked_out + tp->lost_out;
123483ae4088SIlpo Järvinen }
123583ae4088SIlpo Järvinen 
12361da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
12371da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
12381da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
12391da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
12401da177e4SLinus Torvalds  *
12411da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
12421da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
12431da177e4SLinus Torvalds  *
12441da177e4SLinus Torvalds  * Read this equation as:
12451da177e4SLinus Torvalds  *
12461da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
12471da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
12481da177e4SLinus Torvalds  *	"Packets fast retransmitted"
12491da177e4SLinus Torvalds  */
125040efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
12511da177e4SLinus Torvalds {
125283ae4088SIlpo Järvinen 	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
12550b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH	0x7fffffff
12560b6a05c1SIlpo Järvinen 
125740570375SEric Dumazet static inline u32 tcp_snd_cwnd(const struct tcp_sock *tp)
125840570375SEric Dumazet {
125940570375SEric Dumazet 	return tp->snd_cwnd;
126040570375SEric Dumazet }
126140570375SEric Dumazet 
126240570375SEric Dumazet static inline void tcp_snd_cwnd_set(struct tcp_sock *tp, u32 val)
126340570375SEric Dumazet {
126440570375SEric Dumazet 	WARN_ON_ONCE((int)val <= 0);
126540570375SEric Dumazet 	tp->snd_cwnd = val;
126640570375SEric Dumazet }
126740570375SEric Dumazet 
1268071d5080SYuchung Cheng static inline bool tcp_in_slow_start(const struct tcp_sock *tp)
1269071d5080SYuchung Cheng {
127040570375SEric Dumazet 	return tcp_snd_cwnd(tp) < tp->snd_ssthresh;
1271071d5080SYuchung Cheng }
1272071d5080SYuchung Cheng 
12730b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
12740b6a05c1SIlpo Järvinen {
12750b6a05c1SIlpo Järvinen 	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
12760b6a05c1SIlpo Järvinen }
12770b6a05c1SIlpo Järvinen 
1278684bad11SYuchung Cheng static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
1279684bad11SYuchung Cheng {
1280684bad11SYuchung Cheng 	return (TCPF_CA_CWR | TCPF_CA_Recovery) &
1281684bad11SYuchung Cheng 	       (1 << inet_csk(sk)->icsk_ca_state);
1282684bad11SYuchung Cheng }
1283684bad11SYuchung Cheng 
12841da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
1285684bad11SYuchung Cheng  * The exception is cwnd reduction phase, when cwnd is decreasing towards
12861da177e4SLinus Torvalds  * ssthresh.
12871da177e4SLinus Torvalds  */
12886687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
12891da177e4SLinus Torvalds {
12906687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
1291cf533ea5SEric Dumazet 
1292684bad11SYuchung Cheng 	if (tcp_in_cwnd_reduction(sk))
12931da177e4SLinus Torvalds 		return tp->snd_ssthresh;
12941da177e4SLinus Torvalds 	else
12951da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
129640570375SEric Dumazet 			   ((tcp_snd_cwnd(tp) >> 1) +
129740570375SEric Dumazet 			    (tcp_snd_cwnd(tp) >> 2)));
12981da177e4SLinus Torvalds }
12991da177e4SLinus Torvalds 
1300b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */
1301b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
13021da177e4SLinus Torvalds 
13035ee2c941SChristoph Paasch void tcp_enter_cwr(struct sock *sk);
13045c9f3023SJoe Perches __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
13051da177e4SLinus Torvalds 
13066b5a5c0dSNeal Cardwell /* The maximum number of MSS of available cwnd for which TSO defers
13076b5a5c0dSNeal Cardwell  * sending if not using sysctl_tcp_tso_win_divisor.
13086b5a5c0dSNeal Cardwell  */
13096b5a5c0dSNeal Cardwell static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
13106b5a5c0dSNeal Cardwell {
13116b5a5c0dSNeal Cardwell 	return 3;
13126b5a5c0dSNeal Cardwell }
13136b5a5c0dSNeal Cardwell 
131490840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */
131590840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
131690840defSIlpo Järvinen {
131790840defSIlpo Järvinen 	return tp->snd_una + tp->snd_wnd;
131890840defSIlpo Järvinen }
1319e114a710SEric Dumazet 
1320e114a710SEric Dumazet /* We follow the spirit of RFC2861 to validate cwnd but implement a more
1321e114a710SEric Dumazet  * flexible approach. The RFC suggests cwnd should not be raised unless
1322ca8a2263SNeal Cardwell  * it was fully used previously. And that's exactly what we do in
1323ca8a2263SNeal Cardwell  * congestion avoidance mode. But in slow start we allow cwnd to grow
1324ca8a2263SNeal Cardwell  * as long as the application has used half the cwnd.
1325e114a710SEric Dumazet  * Example :
1326e114a710SEric Dumazet  *    cwnd is 10 (IW10), but application sends 9 frames.
1327e114a710SEric Dumazet  *    We allow cwnd to reach 18 when all frames are ACKed.
1328e114a710SEric Dumazet  * This check is safe because it's as aggressive as slow start which already
1329e114a710SEric Dumazet  * risks 100% overshoot. The advantage is that we discourage application to
1330e114a710SEric Dumazet  * either send more filler packets or data to artificially blow up the cwnd
1331e114a710SEric Dumazet  * usage, and allow application-limited process to probe bw more aggressively.
1332e114a710SEric Dumazet  */
133324901551SEric Dumazet static inline bool tcp_is_cwnd_limited(const struct sock *sk)
1334e114a710SEric Dumazet {
1335e114a710SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1336e114a710SEric Dumazet 
1337f4ce91ceSNeal Cardwell 	if (tp->is_cwnd_limited)
1338f4ce91ceSNeal Cardwell 		return true;
1339f4ce91ceSNeal Cardwell 
1340ca8a2263SNeal Cardwell 	/* If in slow start, ensure cwnd grows to twice what was ACKed. */
1341071d5080SYuchung Cheng 	if (tcp_in_slow_start(tp))
134240570375SEric Dumazet 		return tcp_snd_cwnd(tp) < 2 * tp->max_packets_out;
1343ca8a2263SNeal Cardwell 
1344f4ce91ceSNeal Cardwell 	return false;
1345e114a710SEric Dumazet }
1346f4805edeSStephen Hemminger 
1347cadefe5fSEric Dumazet /* BBR congestion control needs pacing.
1348cadefe5fSEric Dumazet  * Same remark for SO_MAX_PACING_RATE.
1349cadefe5fSEric Dumazet  * sch_fq packet scheduler is efficiently handling pacing,
1350cadefe5fSEric Dumazet  * but is not always installed/used.
1351cadefe5fSEric Dumazet  * Return true if TCP stack should pace packets itself.
1352cadefe5fSEric Dumazet  */
1353cadefe5fSEric Dumazet static inline bool tcp_needs_internal_pacing(const struct sock *sk)
1354cadefe5fSEric Dumazet {
1355cadefe5fSEric Dumazet 	return smp_load_acquire(&sk->sk_pacing_status) == SK_PACING_NEEDED;
1356cadefe5fSEric Dumazet }
1357cadefe5fSEric Dumazet 
13588dc242adSEric Dumazet /* Estimates in how many jiffies next packet for this flow can be sent.
13598dc242adSEric Dumazet  * Scheduling a retransmit timer too early would be silly.
13603f80e08fSEric Dumazet  */
13618dc242adSEric Dumazet static inline unsigned long tcp_pacing_delay(const struct sock *sk)
13623f80e08fSEric Dumazet {
13638dc242adSEric Dumazet 	s64 delay = tcp_sk(sk)->tcp_wstamp_ns - tcp_sk(sk)->tcp_clock_cache;
13643f80e08fSEric Dumazet 
13658dc242adSEric Dumazet 	return delay > 0 ? nsecs_to_jiffies(delay) : 0;
13663f80e08fSEric Dumazet }
13673f80e08fSEric Dumazet 
13683f80e08fSEric Dumazet static inline void tcp_reset_xmit_timer(struct sock *sk,
13693f80e08fSEric Dumazet 					const int what,
13703f80e08fSEric Dumazet 					unsigned long when,
13718dc242adSEric Dumazet 					const unsigned long max_when)
13723f80e08fSEric Dumazet {
13738dc242adSEric Dumazet 	inet_csk_reset_xmit_timer(sk, what, when + tcp_pacing_delay(sk),
13743f80e08fSEric Dumazet 				  max_when);
13753f80e08fSEric Dumazet }
13763f80e08fSEric Dumazet 
137721c8fe99SEric Dumazet /* Something is really bad, we could not queue an additional packet,
13783f80e08fSEric Dumazet  * because qdisc is full or receiver sent a 0 window, or we are paced.
137921c8fe99SEric Dumazet  * We do not want to add fuel to the fire, or abort too early,
138021c8fe99SEric Dumazet  * so make sure the timer we arm now is at least 200ms in the future,
138121c8fe99SEric Dumazet  * regardless of current icsk_rto value (as it could be ~2ms)
138221c8fe99SEric Dumazet  */
138321c8fe99SEric Dumazet static inline unsigned long tcp_probe0_base(const struct sock *sk)
138421c8fe99SEric Dumazet {
138521c8fe99SEric Dumazet 	return max_t(unsigned long, inet_csk(sk)->icsk_rto, TCP_RTO_MIN);
138621c8fe99SEric Dumazet }
138721c8fe99SEric Dumazet 
138821c8fe99SEric Dumazet /* Variant of inet_csk_rto_backoff() used for zero window probes */
138921c8fe99SEric Dumazet static inline unsigned long tcp_probe0_when(const struct sock *sk,
139021c8fe99SEric Dumazet 					    unsigned long max_when)
139121c8fe99SEric Dumazet {
13926d4634d1SCambda Zhu 	u8 backoff = min_t(u8, ilog2(TCP_RTO_MAX / TCP_RTO_MIN) + 1,
13936d4634d1SCambda Zhu 			   inet_csk(sk)->icsk_backoff);
13946d4634d1SCambda Zhu 	u64 when = (u64)tcp_probe0_base(sk) << backoff;
139521c8fe99SEric Dumazet 
139621c8fe99SEric Dumazet 	return (unsigned long)min_t(u64, when, max_when);
139721c8fe99SEric Dumazet }
139821c8fe99SEric Dumazet 
13999e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk)
14001da177e4SLinus Torvalds {
140121c8fe99SEric Dumazet 	if (!tcp_sk(sk)->packets_out && !inet_csk(sk)->icsk_pending)
14023f80e08fSEric Dumazet 		tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
14038dc242adSEric Dumazet 				     tcp_probe0_base(sk), TCP_RTO_MAX);
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
1406ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
14071da177e4SLinus Torvalds {
14081da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds 
1411ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
14121da177e4SLinus Torvalds {
14131da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
14141da177e4SLinus Torvalds }
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds /*
14171da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
14181da177e4SLinus Torvalds  */
1419ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr,
1420ba7808eaSFrederik Deweerdt 				   __be32 daddr, __wsum base)
14211da177e4SLinus Torvalds {
14221da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_TCP, base);
14231da177e4SLinus Torvalds }
14241da177e4SLinus Torvalds 
1425a2a385d6SEric Dumazet static inline bool tcp_checksum_complete(struct sk_buff *skb)
14261da177e4SLinus Torvalds {
142760476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
14286ab6dfa6SCong Wang 		__skb_checksum_complete(skb);
14291da177e4SLinus Torvalds }
14301da177e4SLinus Torvalds 
14317a26dc9eSMenglong Dong bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb,
14327a26dc9eSMenglong Dong 		     enum skb_drop_reason *reason);
1433f35f8219SEric Dumazet 
1434f35f8219SEric Dumazet 
1435ac6e7800SEric Dumazet int tcp_filter(struct sock *sk, struct sk_buff *skb);
14365c9f3023SJoe Perches void tcp_set_state(struct sock *sk, int state);
14375c9f3023SJoe Perches void tcp_done(struct sock *sk);
1438c1e64e29SLorenzo Colitti int tcp_abort(struct sock *sk, int err);
1439c1e64e29SLorenzo Colitti 
144040efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
14411da177e4SLinus Torvalds {
14421da177e4SLinus Torvalds 	rx_opt->dsack = 0;
14431da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
14441da177e4SLinus Torvalds }
14451da177e4SLinus Torvalds 
14466f021c62SEric Dumazet void tcp_cwnd_restart(struct sock *sk, s32 delta);
14476f021c62SEric Dumazet 
14486f021c62SEric Dumazet static inline void tcp_slow_start_after_idle_check(struct sock *sk)
14496f021c62SEric Dumazet {
14501b1fc3fdSWei Wang 	const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
14516f021c62SEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
14526f021c62SEric Dumazet 	s32 delta;
14536f021c62SEric Dumazet 
14544845b571SKuniyuki Iwashima 	if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle) ||
14554845b571SKuniyuki Iwashima 	    tp->packets_out || ca_ops->cong_control)
14566f021c62SEric Dumazet 		return;
1457d635fbe2SEric Dumazet 	delta = tcp_jiffies32 - tp->lsndtime;
14586f021c62SEric Dumazet 	if (delta > inet_csk(sk)->icsk_rto)
14596f021c62SEric Dumazet 		tcp_cwnd_restart(sk, delta);
14606f021c62SEric Dumazet }
146185f16525SYuchung Cheng 
14621da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
1463ceef9ab6SEric Dumazet void tcp_select_initial_window(const struct sock *sk, int __space,
1464ceef9ab6SEric Dumazet 			       __u32 mss, __u32 *rcv_wnd,
14655c9f3023SJoe Perches 			       __u32 *window_clamp, int wscale_ok,
14665c9f3023SJoe Perches 			       __u8 *rcv_wscale, __u32 init_rcv_wnd);
14671da177e4SLinus Torvalds 
1468b8dc6d6cSPaolo Abeni static inline int __tcp_win_from_space(u8 scaling_ratio, int space)
14691da177e4SLinus Torvalds {
1470b8dc6d6cSPaolo Abeni 	s64 scaled_space = (s64)space * scaling_ratio;
1471c4836742SGao Feng 
1472dfa2f048SEric Dumazet 	return scaled_space >> TCP_RMEM_TO_WIN_SCALE;
1473dfa2f048SEric Dumazet }
1474dfa2f048SEric Dumazet 
1475b8dc6d6cSPaolo Abeni static inline int tcp_win_from_space(const struct sock *sk, int space)
1476b8dc6d6cSPaolo Abeni {
1477b8dc6d6cSPaolo Abeni 	return __tcp_win_from_space(tcp_sk(sk)->scaling_ratio, space);
1478b8dc6d6cSPaolo Abeni }
1479b8dc6d6cSPaolo Abeni 
1480b8dc6d6cSPaolo Abeni /* inverse of __tcp_win_from_space() */
1481b8dc6d6cSPaolo Abeni static inline int __tcp_space_from_win(u8 scaling_ratio, int win)
1482dfa2f048SEric Dumazet {
1483dfa2f048SEric Dumazet 	u64 val = (u64)win << TCP_RMEM_TO_WIN_SCALE;
1484dfa2f048SEric Dumazet 
1485b8dc6d6cSPaolo Abeni 	do_div(val, scaling_ratio);
1486dfa2f048SEric Dumazet 	return val;
1487dfa2f048SEric Dumazet }
1488dfa2f048SEric Dumazet 
1489b8dc6d6cSPaolo Abeni static inline int tcp_space_from_win(const struct sock *sk, int win)
1490b8dc6d6cSPaolo Abeni {
1491b8dc6d6cSPaolo Abeni 	return __tcp_space_from_win(tcp_sk(sk)->scaling_ratio, win);
1492b8dc6d6cSPaolo Abeni }
1493b8dc6d6cSPaolo Abeni 
1494dfa2f048SEric Dumazet /* Assume a conservative default of 1200 bytes of payload per 4K page.
1495dfa2f048SEric Dumazet  * This may be adjusted later in tcp_measure_rcv_mss().
1496dfa2f048SEric Dumazet  */
1497849ee75aSPaolo Abeni #define TCP_DEFAULT_SCALING_RATIO ((1200 << TCP_RMEM_TO_WIN_SCALE) / \
1498849ee75aSPaolo Abeni 				   SKB_TRUESIZE(4096))
1499849ee75aSPaolo Abeni 
1500849ee75aSPaolo Abeni static inline void tcp_scaling_ratio_init(struct sock *sk)
1501849ee75aSPaolo Abeni {
1502849ee75aSPaolo Abeni 	tcp_sk(sk)->scaling_ratio = TCP_DEFAULT_SCALING_RATIO;
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
15051da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
15061da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
15071da177e4SLinus Torvalds {
1508ebb3b78dSEric Dumazet 	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) -
150970c26558SEric Dumazet 				  READ_ONCE(sk->sk_backlog.len) -
15101da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
15111da177e4SLinus Torvalds }
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
15141da177e4SLinus Torvalds {
1515ebb3b78dSEric Dumazet 	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
15161da177e4SLinus Torvalds }
15171da177e4SLinus Torvalds 
1518053f3684SWei Wang static inline void tcp_adjust_rcv_ssthresh(struct sock *sk)
1519053f3684SWei Wang {
1520053f3684SWei Wang 	int unused_mem = sk_unused_reserved_mem(sk);
1521053f3684SWei Wang 	struct tcp_sock *tp = tcp_sk(sk);
1522053f3684SWei Wang 
1523053f3684SWei Wang 	tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
1524053f3684SWei Wang 	if (unused_mem)
1525053f3684SWei Wang 		tp->rcv_ssthresh = max_t(u32, tp->rcv_ssthresh,
1526053f3684SWei Wang 					 tcp_win_from_space(sk, unused_mem));
1527053f3684SWei Wang }
1528053f3684SWei Wang 
1529c76c6956SPaolo Abeni void tcp_cleanup_rbuf(struct sock *sk, int copied);
1530e5c6de5fSJohn Fastabend void __tcp_cleanup_rbuf(struct sock *sk, int copied);
1531e5c6de5fSJohn Fastabend 
1532c76c6956SPaolo Abeni 
153324adbc16SEric Dumazet /* We provision sk_rcvbuf around 200% of sk_rcvlowat.
153424adbc16SEric Dumazet  * If 87.5 % (7/8) of the space has been consumed, we want to override
153524adbc16SEric Dumazet  * SO_RCVLOWAT constraint, since we are receiving skbs with too small
153624adbc16SEric Dumazet  * len/truesize ratio.
153724adbc16SEric Dumazet  */
153824adbc16SEric Dumazet static inline bool tcp_rmem_pressure(const struct sock *sk)
153924adbc16SEric Dumazet {
1540f969dc5aSEric Dumazet 	int rcvbuf, threshold;
1541f969dc5aSEric Dumazet 
1542f969dc5aSEric Dumazet 	if (tcp_under_memory_pressure(sk))
1543f969dc5aSEric Dumazet 		return true;
1544f969dc5aSEric Dumazet 
1545f969dc5aSEric Dumazet 	rcvbuf = READ_ONCE(sk->sk_rcvbuf);
1546f969dc5aSEric Dumazet 	threshold = rcvbuf - (rcvbuf >> 3);
154724adbc16SEric Dumazet 
154824adbc16SEric Dumazet 	return atomic_read(&sk->sk_rmem_alloc) > threshold;
154924adbc16SEric Dumazet }
155024adbc16SEric Dumazet 
155105dc72abSEric Dumazet static inline bool tcp_epollin_ready(const struct sock *sk, int target)
155205dc72abSEric Dumazet {
155305dc72abSEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
155405dc72abSEric Dumazet 	int avail = READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->copied_seq);
155505dc72abSEric Dumazet 
155605dc72abSEric Dumazet 	if (avail <= 0)
155705dc72abSEric Dumazet 		return false;
155805dc72abSEric Dumazet 
155905dc72abSEric Dumazet 	return (avail >= target) || tcp_rmem_pressure(sk) ||
156005dc72abSEric Dumazet 	       (tcp_receive_window(tp) <= inet_csk(sk)->icsk_ack.rcv_mss);
156105dc72abSEric Dumazet }
156205dc72abSEric Dumazet 
1563843f4a55SYuchung Cheng extern void tcp_openreq_init_rwin(struct request_sock *req,
1564b1964b5fSEric Dumazet 				  const struct sock *sk_listener,
1565b1964b5fSEric Dumazet 				  const struct dst_entry *dst);
1566843f4a55SYuchung Cheng 
15675c9f3023SJoe Perches void tcp_enter_memory_pressure(struct sock *sk);
156806044751SEric Dumazet void tcp_leave_memory_pressure(struct sock *sk);
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
15711da177e4SLinus Torvalds {
1572b840d15dSNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
15735ecf9d4fSEric Dumazet 	int val;
1574b840d15dSNikolay Borisov 
15755ecf9d4fSEric Dumazet 	/* Paired with WRITE_ONCE() in tcp_sock_set_keepintvl()
15765ecf9d4fSEric Dumazet 	 * and do_tcp_setsockopt().
15775ecf9d4fSEric Dumazet 	 */
15785ecf9d4fSEric Dumazet 	val = READ_ONCE(tp->keepalive_intvl);
15795ecf9d4fSEric Dumazet 
15805ecf9d4fSEric Dumazet 	return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_intvl);
15811da177e4SLinus Torvalds }
15821da177e4SLinus Torvalds 
15831da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
15841da177e4SLinus Torvalds {
158513b287e8SNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
15864164245cSEric Dumazet 	int val;
158713b287e8SNikolay Borisov 
15884164245cSEric Dumazet 	/* Paired with WRITE_ONCE() in tcp_sock_set_keepidle_locked() */
15894164245cSEric Dumazet 	val = READ_ONCE(tp->keepalive_time);
15904164245cSEric Dumazet 
15914164245cSEric Dumazet 	return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time);
15921da177e4SLinus Torvalds }
15931da177e4SLinus Torvalds 
1594df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp)
1595df19a626SEric Dumazet {
15969bd6861bSNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
15976e5e1de6SEric Dumazet 	int val;
15989bd6861bSNikolay Borisov 
15996e5e1de6SEric Dumazet 	/* Paired with WRITE_ONCE() in tcp_sock_set_keepcnt()
16006e5e1de6SEric Dumazet 	 * and do_tcp_setsockopt().
16016e5e1de6SEric Dumazet 	 */
16026e5e1de6SEric Dumazet 	val = READ_ONCE(tp->keepalive_probes);
16036e5e1de6SEric Dumazet 
16046e5e1de6SEric Dumazet 	return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_probes);
1605df19a626SEric Dumazet }
1606df19a626SEric Dumazet 
16076c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
16086c37e5deSFlavio Leitner {
16096c37e5deSFlavio Leitner 	const struct inet_connection_sock *icsk = &tp->inet_conn;
16106c37e5deSFlavio Leitner 
161170eabf0eSEric Dumazet 	return min_t(u32, tcp_jiffies32 - icsk->icsk_ack.lrcvtime,
161270eabf0eSEric Dumazet 			  tcp_jiffies32 - tp->rcv_tstamp);
16136c37e5deSFlavio Leitner }
16146c37e5deSFlavio Leitner 
1615463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
16161da177e4SLinus Torvalds {
161739e24435SKuniyuki Iwashima 	int fin_timeout = tcp_sk(sk)->linger2 ? :
161839e24435SKuniyuki Iwashima 		READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fin_timeout);
1619463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
16201da177e4SLinus Torvalds 
1621463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1622463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 	return fin_timeout;
16251da177e4SLinus Torvalds }
16261da177e4SLinus Torvalds 
1627a2a385d6SEric Dumazet static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
1628c887e6d2SIlpo Järvinen 				  int paws_win)
16291da177e4SLinus Torvalds {
1630c887e6d2SIlpo Järvinen 	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1631a2a385d6SEric Dumazet 		return true;
1632cca9bab1SArnd Bergmann 	if (unlikely(!time_before32(ktime_get_seconds(),
1633af772144SEric Dumazet 				    rx_opt->ts_recent_stamp + TCP_PAWS_WRAP)))
1634a2a385d6SEric Dumazet 		return true;
1635bc2ce894SEric Dumazet 	/*
1636bc2ce894SEric Dumazet 	 * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
1637bc2ce894SEric Dumazet 	 * then following tcp messages have valid values. Ignore 0 value,
1638bc2ce894SEric Dumazet 	 * or else 'negative' tsval might forbid us to accept their packets.
1639bc2ce894SEric Dumazet 	 */
1640bc2ce894SEric Dumazet 	if (!rx_opt->ts_recent)
1641a2a385d6SEric Dumazet 		return true;
1642a2a385d6SEric Dumazet 	return false;
1643c887e6d2SIlpo Järvinen }
1644c887e6d2SIlpo Järvinen 
1645a2a385d6SEric Dumazet static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
1646c887e6d2SIlpo Järvinen 				   int rst)
1647c887e6d2SIlpo Järvinen {
1648c887e6d2SIlpo Järvinen 	if (tcp_paws_check(rx_opt, 0))
1649a2a385d6SEric Dumazet 		return false;
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
16521da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
16531da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
16541da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
16551da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
16561da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
16571da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
16581da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
16591da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
16621da177e4SLinus Torvalds 	 */
1663cca9bab1SArnd Bergmann 	if (rst && !time_before32(ktime_get_seconds(),
1664cca9bab1SArnd Bergmann 				  rx_opt->ts_recent_stamp + TCP_PAWS_MSL))
1665a2a385d6SEric Dumazet 		return false;
1666a2a385d6SEric Dumazet 	return true;
16671da177e4SLinus Torvalds }
16681da177e4SLinus Torvalds 
16697970ddc8SEric Dumazet bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
16707970ddc8SEric Dumazet 			  int mib_idx, u32 *last_oow_ack_time);
1671032ee423SNeal Cardwell 
1672a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net)
16731da177e4SLinus Torvalds {
16741da177e4SLinus Torvalds 	/* See RFC 2012 */
16756aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOALGORITHM, 1);
16766aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
16776aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
16786aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_MAXCONN, -1);
16791da177e4SLinus Torvalds }
16801da177e4SLinus Torvalds 
16816a438bbeSStephen Hemminger /* from STCP */
1682ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
16830800f170SDavid S. Miller {
16846a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
1685ef9da47cSIlpo Järvinen }
1686ef9da47cSIlpo Järvinen 
1687ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
1688ef9da47cSIlpo Järvinen {
1689ef9da47cSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
16906a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
1691b7689205SIlpo Järvinen }
1692b7689205SIlpo Järvinen 
1693c845f5f3SDmitry Safonov #define tcp_md5_addr tcp_ao_addr
1694a915da9bSEric Dumazet 
1695cfb6eeb4SYOSHIFUJI Hideaki /* - key database */
1696cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key {
1697a915da9bSEric Dumazet 	struct hlist_node	node;
1698cfb6eeb4SYOSHIFUJI Hideaki 	u8			keylen;
1699a915da9bSEric Dumazet 	u8			family; /* AF_INET or AF_INET6 */
17006797318eSIvan Delalande 	u8			prefixlen;
1701a76c2315SLeonard Crestez 	u8			flags;
1702dea53bb8SDavid Ahern 	union tcp_md5_addr	addr;
1703dea53bb8SDavid Ahern 	int			l3index; /* set if key added with L3 scope */
1704a915da9bSEric Dumazet 	u8			key[TCP_MD5SIG_MAXKEYLEN];
1705a915da9bSEric Dumazet 	struct rcu_head		rcu;
1706cfb6eeb4SYOSHIFUJI Hideaki };
1707cfb6eeb4SYOSHIFUJI Hideaki 
1708cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */
1709cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info {
1710a915da9bSEric Dumazet 	struct hlist_head	head;
1711a8afca03SEric Dumazet 	struct rcu_head		rcu;
1712cfb6eeb4SYOSHIFUJI Hideaki };
1713cfb6eeb4SYOSHIFUJI Hideaki 
1714cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */
1715cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr {
1716cfb6eeb4SYOSHIFUJI Hideaki 	__be32		saddr;
1717cfb6eeb4SYOSHIFUJI Hideaki 	__be32		daddr;
1718cfb6eeb4SYOSHIFUJI Hideaki 	__u8		pad;
1719cfb6eeb4SYOSHIFUJI Hideaki 	__u8		protocol;
1720cfb6eeb4SYOSHIFUJI Hideaki 	__be16		len;
1721cfb6eeb4SYOSHIFUJI Hideaki };
1722cfb6eeb4SYOSHIFUJI Hideaki 
1723cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr {
1724cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr	saddr;
1725cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr daddr;
1726cfb6eeb4SYOSHIFUJI Hideaki 	__be32		len;
1727cfb6eeb4SYOSHIFUJI Hideaki 	__be32		protocol;	/* including padding */
1728cfb6eeb4SYOSHIFUJI Hideaki };
1729cfb6eeb4SYOSHIFUJI Hideaki 
1730cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block {
1731cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr ip4;
1732dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1733cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_pseudohdr ip6;
1734cfb6eeb4SYOSHIFUJI Hideaki #endif
1735cfb6eeb4SYOSHIFUJI Hideaki };
1736cfb6eeb4SYOSHIFUJI Hideaki 
17378c73b263SDmitry Safonov /*
17388c73b263SDmitry Safonov  * struct tcp_sigpool - per-CPU pool of ahash_requests
17398c73b263SDmitry Safonov  * @scratch: per-CPU temporary area, that can be used between
17408c73b263SDmitry Safonov  *	     tcp_sigpool_start() and tcp_sigpool_end() to perform
17418c73b263SDmitry Safonov  *	     crypto request
17428c73b263SDmitry Safonov  * @req: pre-allocated ahash request
17438c73b263SDmitry Safonov  */
17448c73b263SDmitry Safonov struct tcp_sigpool {
174519689e38SEric Dumazet 	void *scratch;
17468c73b263SDmitry Safonov 	struct ahash_request *req;
1747cfb6eeb4SYOSHIFUJI Hideaki };
1748cfb6eeb4SYOSHIFUJI Hideaki 
17498c73b263SDmitry Safonov int tcp_sigpool_alloc_ahash(const char *alg, size_t scratch_size);
17508c73b263SDmitry Safonov void tcp_sigpool_get(unsigned int id);
17518c73b263SDmitry Safonov void tcp_sigpool_release(unsigned int id);
17528c73b263SDmitry Safonov int tcp_sigpool_hash_skb_data(struct tcp_sigpool *hp,
17538c73b263SDmitry Safonov 			      const struct sk_buff *skb,
17548c73b263SDmitry Safonov 			      unsigned int header_len);
17558c73b263SDmitry Safonov 
17568c73b263SDmitry Safonov /**
17578c73b263SDmitry Safonov  * tcp_sigpool_start - disable bh and start using tcp_sigpool_ahash
17588c73b263SDmitry Safonov  * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash()
17598c73b263SDmitry Safonov  * @c: returned tcp_sigpool for usage (uninitialized on failure)
17608c73b263SDmitry Safonov  *
17618c73b263SDmitry Safonov  * Returns 0 on success, error otherwise.
17628c73b263SDmitry Safonov  */
17638c73b263SDmitry Safonov int tcp_sigpool_start(unsigned int id, struct tcp_sigpool *c);
17648c73b263SDmitry Safonov /**
17658c73b263SDmitry Safonov  * tcp_sigpool_end - enable bh and stop using tcp_sigpool
17668c73b263SDmitry Safonov  * @c: tcp_sigpool context that was returned by tcp_sigpool_start()
17678c73b263SDmitry Safonov  */
17688c73b263SDmitry Safonov void tcp_sigpool_end(struct tcp_sigpool *c);
17698c73b263SDmitry Safonov size_t tcp_sigpool_algo(unsigned int id, char *buf, size_t buf_len);
1770cfb6eeb4SYOSHIFUJI Hideaki /* - functions */
177139f8e58eSEric Dumazet int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
177239f8e58eSEric Dumazet 			const struct sock *sk, const struct sk_buff *skb);
17735c9f3023SJoe Perches int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
1774a76c2315SLeonard Crestez 		   int family, u8 prefixlen, int l3index, u8 flags,
1775459837b5SDmitry Safonov 		   const u8 *newkey, u8 newkeylen);
1776459837b5SDmitry Safonov int tcp_md5_key_copy(struct sock *sk, const union tcp_md5_addr *addr,
1777459837b5SDmitry Safonov 		     int family, u8 prefixlen, int l3index,
1778459837b5SDmitry Safonov 		     struct tcp_md5sig_key *key);
1779459837b5SDmitry Safonov 
17805c9f3023SJoe Perches int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
1781a76c2315SLeonard Crestez 		   int family, u8 prefixlen, int l3index, u8 flags);
17820aadc739SDmitry Safonov void tcp_clear_md5_list(struct sock *sk);
1783b83e3debSEric Dumazet struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
1784fd3a154aSEric Dumazet 					 const struct sock *addr_sk);
1785cfb6eeb4SYOSHIFUJI Hideaki 
17869501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
17876015c71eSEric Dumazet #include <linux/jump_label.h>
1788459837b5SDmitry Safonov extern struct static_key_false_deferred tcp_md5_needed;
1789dea53bb8SDavid Ahern struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index,
17905c9f3023SJoe Perches 					   const union tcp_md5_addr *addr,
17910aadc739SDmitry Safonov 					   int family, bool any_l3index);
17926015c71eSEric Dumazet static inline struct tcp_md5sig_key *
1793dea53bb8SDavid Ahern tcp_md5_do_lookup(const struct sock *sk, int l3index,
1794dea53bb8SDavid Ahern 		  const union tcp_md5_addr *addr, int family)
17956015c71eSEric Dumazet {
1796459837b5SDmitry Safonov 	if (!static_branch_unlikely(&tcp_md5_needed.key))
17976015c71eSEric Dumazet 		return NULL;
17980aadc739SDmitry Safonov 	return __tcp_md5_do_lookup(sk, l3index, addr, family, false);
17990aadc739SDmitry Safonov }
18000aadc739SDmitry Safonov 
18010aadc739SDmitry Safonov static inline struct tcp_md5sig_key *
18020aadc739SDmitry Safonov tcp_md5_do_lookup_any_l3index(const struct sock *sk,
18030aadc739SDmitry Safonov 			      const union tcp_md5_addr *addr, int family)
18040aadc739SDmitry Safonov {
18050aadc739SDmitry Safonov 	if (!static_branch_unlikely(&tcp_md5_needed.key))
18060aadc739SDmitry Safonov 		return NULL;
18070aadc739SDmitry Safonov 	return __tcp_md5_do_lookup(sk, 0, addr, family, true);
18086015c71eSEric Dumazet }
18091330b6efSJakub Kicinski 
18101330b6efSJakub Kicinski enum skb_drop_reason
18111330b6efSJakub Kicinski tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb,
18127bbb765bSDmitry Safonov 		     const void *saddr, const void *daddr,
18137bbb765bSDmitry Safonov 		     int family, int dif, int sdif);
18147bbb765bSDmitry Safonov 
18156015c71eSEric Dumazet 
1816a915da9bSEric Dumazet #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_key)
18179501f972SYOSHIFUJI Hideaki #else
1818dea53bb8SDavid Ahern static inline struct tcp_md5sig_key *
1819dea53bb8SDavid Ahern tcp_md5_do_lookup(const struct sock *sk, int l3index,
1820dea53bb8SDavid Ahern 		  const union tcp_md5_addr *addr, int family)
1821a915da9bSEric Dumazet {
1822a915da9bSEric Dumazet 	return NULL;
1823a915da9bSEric Dumazet }
18241330b6efSJakub Kicinski 
18250aadc739SDmitry Safonov static inline struct tcp_md5sig_key *
18260aadc739SDmitry Safonov tcp_md5_do_lookup_any_l3index(const struct sock *sk,
18270aadc739SDmitry Safonov 			      const union tcp_md5_addr *addr, int family)
18280aadc739SDmitry Safonov {
18290aadc739SDmitry Safonov 	return NULL;
18300aadc739SDmitry Safonov }
18310aadc739SDmitry Safonov 
18321330b6efSJakub Kicinski static inline enum skb_drop_reason
18331330b6efSJakub Kicinski tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb,
18347bbb765bSDmitry Safonov 		     const void *saddr, const void *daddr,
183524055bb8SVladimir Oltean 		     int family, int dif, int sdif)
18367bbb765bSDmitry Safonov {
18371330b6efSJakub Kicinski 	return SKB_NOT_DROPPED_YET;
18387bbb765bSDmitry Safonov }
18399501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	NULL
18409501f972SYOSHIFUJI Hideaki #endif
18419501f972SYOSHIFUJI Hideaki 
18428c73b263SDmitry Safonov int tcp_md5_alloc_sigpool(void);
18438c73b263SDmitry Safonov void tcp_md5_release_sigpool(void);
18448c73b263SDmitry Safonov void tcp_md5_add_sigpool(void);
18458c73b263SDmitry Safonov extern int tcp_md5_sigpool_id;
1846cfb6eeb4SYOSHIFUJI Hideaki 
18478c73b263SDmitry Safonov int tcp_md5_hash_key(struct tcp_sigpool *hp,
1848cf533ea5SEric Dumazet 		     const struct tcp_md5sig_key *key);
1849cfb6eeb4SYOSHIFUJI Hideaki 
185010467163SJerry Chu /* From tcp_fastopen.c */
18515c9f3023SJoe Perches void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
18527268586bSYuchung Cheng 			    struct tcp_fastopen_cookie *cookie);
18535c9f3023SJoe Perches void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
18542646c831SDaniel Lee 			    struct tcp_fastopen_cookie *cookie, bool syn_lost,
18552646c831SDaniel Lee 			    u16 try_exp);
1856783237e8SYuchung Cheng struct tcp_fastopen_request {
1857783237e8SYuchung Cheng 	/* Fast Open cookie. Size 0 means a cookie request */
1858783237e8SYuchung Cheng 	struct tcp_fastopen_cookie	cookie;
1859783237e8SYuchung Cheng 	struct msghdr			*data;  /* data in MSG_FASTOPEN */
1860f5ddcbbbSEric Dumazet 	size_t				size;
1861f5ddcbbbSEric Dumazet 	int				copied;	/* queued in tcp_connect() */
1862f859a448SWillem de Bruijn 	struct ubuf_info		*uarg;
1863783237e8SYuchung Cheng };
1864783237e8SYuchung Cheng void tcp_free_fastopen_req(struct tcp_sock *tp);
18651fba70e5SYuchung Cheng void tcp_fastopen_destroy_cipher(struct sock *sk);
186643713848SHaishuang Yan void tcp_fastopen_ctx_destroy(struct net *net);
18671fba70e5SYuchung Cheng int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
1868438ac880SArd Biesheuvel 			      void *primary_key, void *backup_key);
1869f19008e6SJason Baron int tcp_fastopen_get_cipher(struct net *net, struct inet_connection_sock *icsk,
1870f19008e6SJason Baron 			    u64 *key);
187161d2bcaeSEric Dumazet void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb);
18727c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
18735b7ed089SYuchung Cheng 			      struct request_sock *req,
187471c02379SChristoph Paasch 			      struct tcp_fastopen_cookie *foc,
187571c02379SChristoph Paasch 			      const struct dst_entry *dst);
187643713848SHaishuang Yan void tcp_fastopen_init_key_once(struct net *net);
1877065263f4SWei Wang bool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss,
1878065263f4SWei Wang 			     struct tcp_fastopen_cookie *cookie);
187919f6d3f3SWei Wang bool tcp_fastopen_defer_connect(struct sock *sk, int *err);
1880438ac880SArd Biesheuvel #define TCP_FASTOPEN_KEY_LENGTH sizeof(siphash_key_t)
18819092a76dSJason Baron #define TCP_FASTOPEN_KEY_MAX 2
18829092a76dSJason Baron #define TCP_FASTOPEN_KEY_BUF_LENGTH \
18839092a76dSJason Baron 	(TCP_FASTOPEN_KEY_LENGTH * TCP_FASTOPEN_KEY_MAX)
188410467163SJerry Chu 
188510467163SJerry Chu /* Fastopen key context */
188610467163SJerry Chu struct tcp_fastopen_context {
1887438ac880SArd Biesheuvel 	siphash_key_t	key[TCP_FASTOPEN_KEY_MAX];
1888c681edaeSArd Biesheuvel 	int		num;
188910467163SJerry Chu 	struct rcu_head	rcu;
189010467163SJerry Chu };
189110467163SJerry Chu 
189246c2fa39SWei Wang void tcp_fastopen_active_disable(struct sock *sk);
1893cf1ef3f0SWei Wang bool tcp_fastopen_active_should_disable(struct sock *sk);
1894cf1ef3f0SWei Wang void tcp_fastopen_active_disable_ofo_check(struct sock *sk);
18957268586bSYuchung Cheng void tcp_fastopen_active_detect_blackhole(struct sock *sk, bool expired);
1896cf1ef3f0SWei Wang 
18979092a76dSJason Baron /* Caller needs to wrap with rcu_read_(un)lock() */
18989092a76dSJason Baron static inline
18999092a76dSJason Baron struct tcp_fastopen_context *tcp_fastopen_get_ctx(const struct sock *sk)
19009092a76dSJason Baron {
19019092a76dSJason Baron 	struct tcp_fastopen_context *ctx;
19029092a76dSJason Baron 
19039092a76dSJason Baron 	ctx = rcu_dereference(inet_csk(sk)->icsk_accept_queue.fastopenq.ctx);
19049092a76dSJason Baron 	if (!ctx)
19059092a76dSJason Baron 		ctx = rcu_dereference(sock_net(sk)->ipv4.tcp_fastopen_ctx);
19069092a76dSJason Baron 	return ctx;
19079092a76dSJason Baron }
19089092a76dSJason Baron 
19099092a76dSJason Baron static inline
19109092a76dSJason Baron bool tcp_fastopen_cookie_match(const struct tcp_fastopen_cookie *foc,
19119092a76dSJason Baron 			       const struct tcp_fastopen_cookie *orig)
19129092a76dSJason Baron {
19139092a76dSJason Baron 	if (orig->len == TCP_FASTOPEN_COOKIE_SIZE &&
19149092a76dSJason Baron 	    orig->len == foc->len &&
19159092a76dSJason Baron 	    !memcmp(orig->val, foc->val, foc->len))
19169092a76dSJason Baron 		return true;
19179092a76dSJason Baron 	return false;
19189092a76dSJason Baron }
19199092a76dSJason Baron 
19209092a76dSJason Baron static inline
19219092a76dSJason Baron int tcp_fastopen_context_len(const struct tcp_fastopen_context *ctx)
19229092a76dSJason Baron {
1923c681edaeSArd Biesheuvel 	return ctx->num;
19249092a76dSJason Baron }
19259092a76dSJason Baron 
192605b055e8SFrancis Yan /* Latencies incurred by various limits for a sender. They are
192705b055e8SFrancis Yan  * chronograph-like stats that are mutually exclusive.
192805b055e8SFrancis Yan  */
192905b055e8SFrancis Yan enum tcp_chrono {
193005b055e8SFrancis Yan 	TCP_CHRONO_UNSPEC,
193105b055e8SFrancis Yan 	TCP_CHRONO_BUSY, /* Actively sending data (non-empty write queue) */
193205b055e8SFrancis Yan 	TCP_CHRONO_RWND_LIMITED, /* Stalled by insufficient receive window */
193305b055e8SFrancis Yan 	TCP_CHRONO_SNDBUF_LIMITED, /* Stalled by insufficient send buffer */
193405b055e8SFrancis Yan 	__TCP_CHRONO_MAX,
193505b055e8SFrancis Yan };
193605b055e8SFrancis Yan 
193705b055e8SFrancis Yan void tcp_chrono_start(struct sock *sk, const enum tcp_chrono type);
193805b055e8SFrancis Yan void tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type);
193905b055e8SFrancis Yan 
1940e2080072SEric Dumazet /* This helper is needed, because skb->tcp_tsorted_anchor uses
1941e2080072SEric Dumazet  * the same memory storage than skb->destructor/_skb_refdst
1942e2080072SEric Dumazet  */
1943e2080072SEric Dumazet static inline void tcp_skb_tsorted_anchor_cleanup(struct sk_buff *skb)
1944e2080072SEric Dumazet {
1945e2080072SEric Dumazet 	skb->destructor = NULL;
1946e2080072SEric Dumazet 	skb->_skb_refdst = 0UL;
1947e2080072SEric Dumazet }
1948e2080072SEric Dumazet 
1949e2080072SEric Dumazet #define tcp_skb_tsorted_save(skb) {		\
1950e2080072SEric Dumazet 	unsigned long _save = skb->_skb_refdst;	\
1951e2080072SEric Dumazet 	skb->_skb_refdst = 0UL;
1952e2080072SEric Dumazet 
1953e2080072SEric Dumazet #define tcp_skb_tsorted_restore(skb)		\
1954e2080072SEric Dumazet 	skb->_skb_refdst = _save;		\
1955e2080072SEric Dumazet }
1956e2080072SEric Dumazet 
1957ac3f09baSEric Dumazet void tcp_write_queue_purge(struct sock *sk);
1958fe067e8aSDavid S. Miller 
195975c119afSEric Dumazet static inline struct sk_buff *tcp_rtx_queue_head(const struct sock *sk)
196075c119afSEric Dumazet {
196175c119afSEric Dumazet 	return skb_rb_first(&sk->tcp_rtx_queue);
196275c119afSEric Dumazet }
196375c119afSEric Dumazet 
1964b617158dSEric Dumazet static inline struct sk_buff *tcp_rtx_queue_tail(const struct sock *sk)
1965b617158dSEric Dumazet {
1966b617158dSEric Dumazet 	return skb_rb_last(&sk->tcp_rtx_queue);
1967b617158dSEric Dumazet }
1968b617158dSEric Dumazet 
1969cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
1970fe067e8aSDavid S. Miller {
1971cd07a8eaSDavid S. Miller 	return skb_peek_tail(&sk->sk_write_queue);
1972fe067e8aSDavid S. Miller }
1973fe067e8aSDavid S. Miller 
1974234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
1975cd07a8eaSDavid S. Miller 	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1976234b6860SIlpo Järvinen 
1977cf533ea5SEric Dumazet static inline struct sk_buff *tcp_send_head(const struct sock *sk)
1978fe067e8aSDavid S. Miller {
197975c119afSEric Dumazet 	return skb_peek(&sk->sk_write_queue);
1980fe067e8aSDavid S. Miller }
1981fe067e8aSDavid S. Miller 
1982cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk,
1983cd07a8eaSDavid S. Miller 				   const struct sk_buff *skb)
1984cd07a8eaSDavid S. Miller {
1985cd07a8eaSDavid S. Miller 	return skb_queue_is_last(&sk->sk_write_queue, skb);
1986cd07a8eaSDavid S. Miller }
1987cd07a8eaSDavid S. Miller 
1988ee2aabd3SEric Dumazet /**
1989ee2aabd3SEric Dumazet  * tcp_write_queue_empty - test if any payload (or FIN) is available in write queue
1990ee2aabd3SEric Dumazet  * @sk: socket
1991ee2aabd3SEric Dumazet  *
1992ee2aabd3SEric Dumazet  * Since the write queue can have a temporary empty skb in it,
1993ee2aabd3SEric Dumazet  * we must not use "return skb_queue_empty(&sk->sk_write_queue)"
1994ee2aabd3SEric Dumazet  */
199575c119afSEric Dumazet static inline bool tcp_write_queue_empty(const struct sock *sk)
1996fe067e8aSDavid S. Miller {
1997ee2aabd3SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1998ee2aabd3SEric Dumazet 
1999ee2aabd3SEric Dumazet 	return tp->write_seq == tp->snd_nxt;
200075c119afSEric Dumazet }
200175c119afSEric Dumazet 
200275c119afSEric Dumazet static inline bool tcp_rtx_queue_empty(const struct sock *sk)
200375c119afSEric Dumazet {
200475c119afSEric Dumazet 	return RB_EMPTY_ROOT(&sk->tcp_rtx_queue);
200575c119afSEric Dumazet }
200675c119afSEric Dumazet 
200775c119afSEric Dumazet static inline bool tcp_rtx_and_write_queues_empty(const struct sock *sk)
200875c119afSEric Dumazet {
200975c119afSEric Dumazet 	return tcp_rtx_queue_empty(sk) && tcp_write_queue_empty(sk);
2010fe067e8aSDavid S. Miller }
2011fe067e8aSDavid S. Miller 
2012fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
2013fe067e8aSDavid S. Miller {
2014a43e052bSEric Dumazet 	__skb_queue_tail(&sk->sk_write_queue, skb);
2015fe067e8aSDavid S. Miller 
2016fe067e8aSDavid S. Miller 	/* Queue it, remembering where we must start sending. */
201750895b9dSEric Dumazet 	if (sk->sk_write_queue.next == skb)
20180f87230dSFrancis Yan 		tcp_chrono_start(sk, TCP_CHRONO_BUSY);
2019fe067e8aSDavid S. Miller }
2020fe067e8aSDavid S. Miller 
202143f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk.  */
2022fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new,
2023fe067e8aSDavid S. Miller 						  struct sk_buff *skb,
2024fe067e8aSDavid S. Miller 						  struct sock *sk)
2025fe067e8aSDavid S. Miller {
202643f59c89SDavid S. Miller 	__skb_queue_before(&sk->sk_write_queue, skb, new);
2027fe067e8aSDavid S. Miller }
2028fe067e8aSDavid S. Miller 
2029fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
2030fe067e8aSDavid S. Miller {
20314a269818SEric Dumazet 	tcp_skb_tsorted_anchor_cleanup(skb);
2032fe067e8aSDavid S. Miller 	__skb_unlink(skb, &sk->sk_write_queue);
2033fe067e8aSDavid S. Miller }
2034fe067e8aSDavid S. Miller 
203575c119afSEric Dumazet void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb);
203675c119afSEric Dumazet 
203775c119afSEric Dumazet static inline void tcp_rtx_queue_unlink(struct sk_buff *skb, struct sock *sk)
2038fe067e8aSDavid S. Miller {
203975c119afSEric Dumazet 	tcp_skb_tsorted_anchor_cleanup(skb);
204075c119afSEric Dumazet 	rb_erase(&skb->rbnode, &sk->tcp_rtx_queue);
204175c119afSEric Dumazet }
204275c119afSEric Dumazet 
204375c119afSEric Dumazet static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct sock *sk)
204475c119afSEric Dumazet {
204575c119afSEric Dumazet 	list_del(&skb->tcp_tsorted_anchor);
204675c119afSEric Dumazet 	tcp_rtx_queue_unlink(skb, sk);
204703271f3aSTalal Ahmad 	tcp_wmem_free_skb(sk, skb);
2048fe067e8aSDavid S. Miller }
2049fe067e8aSDavid S. Miller 
205012d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk)
205112d50c46SKrishna Kumar {
205212d50c46SKrishna Kumar 	if (tcp_send_head(sk)) {
205312d50c46SKrishna Kumar 		struct tcp_sock *tp = tcp_sk(sk);
205412d50c46SKrishna Kumar 
205512d50c46SKrishna Kumar 		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
205612d50c46SKrishna Kumar 	}
205712d50c46SKrishna Kumar }
205812d50c46SKrishna Kumar 
2059ecb97192SNeal Cardwell /* Start sequence of the skb just after the highest skb with SACKed
2060ecb97192SNeal Cardwell  * bit, valid only if sacked_out > 0 or when the caller has ensured
2061ecb97192SNeal Cardwell  * validity by itself.
2062a47e5a98SIlpo Järvinen  */
2063a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
2064a47e5a98SIlpo Järvinen {
2065a47e5a98SIlpo Järvinen 	if (!tp->sacked_out)
2066a47e5a98SIlpo Järvinen 		return tp->snd_una;
20676859d494SIlpo Järvinen 
20686859d494SIlpo Järvinen 	if (tp->highest_sack == NULL)
20696859d494SIlpo Järvinen 		return tp->snd_nxt;
20706859d494SIlpo Järvinen 
2071a47e5a98SIlpo Järvinen 	return TCP_SKB_CB(tp->highest_sack)->seq;
2072a47e5a98SIlpo Järvinen }
2073a47e5a98SIlpo Järvinen 
20746859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
20756859d494SIlpo Järvinen {
207650895b9dSEric Dumazet 	tcp_sk(sk)->highest_sack = skb_rb_next(skb);
20776859d494SIlpo Järvinen }
20786859d494SIlpo Järvinen 
20796859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
20806859d494SIlpo Järvinen {
20816859d494SIlpo Järvinen 	return tcp_sk(sk)->highest_sack;
20826859d494SIlpo Järvinen }
20836859d494SIlpo Järvinen 
20846859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk)
20856859d494SIlpo Järvinen {
208650895b9dSEric Dumazet 	tcp_sk(sk)->highest_sack = tcp_rtx_queue_head(sk);
20876859d494SIlpo Järvinen }
20886859d494SIlpo Järvinen 
20892b7cda9cSEric Dumazet /* Called when old skb is about to be deleted and replaced by new skb */
20902b7cda9cSEric Dumazet static inline void tcp_highest_sack_replace(struct sock *sk,
20916859d494SIlpo Järvinen 					    struct sk_buff *old,
20926859d494SIlpo Järvinen 					    struct sk_buff *new)
20936859d494SIlpo Järvinen {
20942b7cda9cSEric Dumazet 	if (old == tcp_highest_sack(sk))
20956859d494SIlpo Järvinen 		tcp_sk(sk)->highest_sack = new;
20966859d494SIlpo Järvinen }
20976859d494SIlpo Järvinen 
2098b1f0a0e9SFlorian Westphal /* This helper checks if socket has IP_TRANSPARENT set */
2099b1f0a0e9SFlorian Westphal static inline bool inet_sk_transparent(const struct sock *sk)
2100b1f0a0e9SFlorian Westphal {
2101b1f0a0e9SFlorian Westphal 	switch (sk->sk_state) {
2102b1f0a0e9SFlorian Westphal 	case TCP_TIME_WAIT:
2103b1f0a0e9SFlorian Westphal 		return inet_twsk(sk)->tw_transparent;
2104b1f0a0e9SFlorian Westphal 	case TCP_NEW_SYN_RECV:
2105b1f0a0e9SFlorian Westphal 		return inet_rsk(inet_reqsk(sk))->no_srccheck;
2106b1f0a0e9SFlorian Westphal 	}
21074bd0623fSEric Dumazet 	return inet_test_bit(TRANSPARENT, sk);
2108b1f0a0e9SFlorian Westphal }
2109b1f0a0e9SFlorian Westphal 
21105aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from
21115aa4b32fSAndreas Petlund  * increased latency). Used to trigger latency-reducing mechanisms.
21125aa4b32fSAndreas Petlund  */
2113a2a385d6SEric Dumazet static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
21145aa4b32fSAndreas Petlund {
21155aa4b32fSAndreas Petlund 	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
21165aa4b32fSAndreas Petlund }
21175aa4b32fSAndreas Petlund 
21181da177e4SLinus Torvalds /* /proc */
21191da177e4SLinus Torvalds enum tcp_seq_states {
21201da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
21211da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
21221da177e4SLinus Torvalds };
21231da177e4SLinus Torvalds 
212437d849bbSChristoph Hellwig void *tcp_seq_start(struct seq_file *seq, loff_t *pos);
212537d849bbSChristoph Hellwig void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
212637d849bbSChristoph Hellwig void tcp_seq_stop(struct seq_file *seq, void *v);
212773cb88ecSArjan van de Ven 
21281da177e4SLinus Torvalds struct tcp_seq_afinfo {
21291da177e4SLinus Torvalds 	sa_family_t			family;
21301da177e4SLinus Torvalds };
21311da177e4SLinus Torvalds 
21321da177e4SLinus Torvalds struct tcp_iter_state {
2133a4146b1bSDenis V. Lunev 	struct seq_net_private	p;
21341da177e4SLinus Torvalds 	enum tcp_seq_states	state;
21351da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
2136a7cb5a49SEric W. Biederman 	int			bucket, offset, sbucket, num;
2137a8b690f9STom Herbert 	loff_t			last_pos;
21381da177e4SLinus Torvalds };
21391da177e4SLinus Torvalds 
214020380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
2141c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops;
214220380731SArnaldo Carvalho de Melo 
21435c9f3023SJoe Perches void tcp_v4_destroy_sock(struct sock *sk);
214420380731SArnaldo Carvalho de Melo 
214528be6e07SEric Dumazet struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
2146c8f44affSMichał Mirosław 				netdev_features_t features);
2147d4546c25SDavid Miller struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb);
21485521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp4_gro_complete(struct sk_buff *skb, int thoff));
21495521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb));
21505521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp6_gro_complete(struct sk_buff *skb, int thoff));
21515521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp6_gro_receive(struct list_head *head, struct sk_buff *skb));
2152e411a8e3SJacob Keller #ifdef CONFIG_INET
2153b1f2abcfSParav Pandit void tcp_gro_complete(struct sk_buff *skb);
2154e411a8e3SJacob Keller #else
2155e411a8e3SJacob Keller static inline void tcp_gro_complete(struct sk_buff *skb) { }
2156e411a8e3SJacob Keller #endif
215728850dc7SDaniel Borkmann 
21585c9f3023SJoe Perches void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
2159f4c50d99SHerbert Xu 
2160c9bee3b7SEric Dumazet static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
2161c9bee3b7SEric Dumazet {
21624979f2d9SNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
21631aeb87bcSEric Dumazet 	u32 val;
21641aeb87bcSEric Dumazet 
21651aeb87bcSEric Dumazet 	val = READ_ONCE(tp->notsent_lowat);
21661aeb87bcSEric Dumazet 
21671aeb87bcSEric Dumazet 	return val ?: READ_ONCE(net->ipv4.sysctl_tcp_notsent_lowat);
2168c9bee3b7SEric Dumazet }
2169c9bee3b7SEric Dumazet 
2170d3cd4924SEric Dumazet bool tcp_stream_memory_free(const struct sock *sk, int wake);
2171c9bee3b7SEric Dumazet 
217220380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
21735c9f3023SJoe Perches int tcp4_proc_init(void);
21745c9f3023SJoe Perches void tcp4_proc_exit(void);
217520380731SArnaldo Carvalho de Melo #endif
217620380731SArnaldo Carvalho de Melo 
2177ea3bea3aSEric Dumazet int tcp_rtx_synack(const struct sock *sk, struct request_sock *req);
21781fb6f159SOctavian Purdila int tcp_conn_request(struct request_sock_ops *rsk_ops,
21791fb6f159SOctavian Purdila 		     const struct tcp_request_sock_ops *af_ops,
21801fb6f159SOctavian Purdila 		     struct sock *sk, struct sk_buff *skb);
21815db92c99SOctavian Purdila 
2182cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */
2183cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops {
2184cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
2185b83e3debSEric Dumazet 	struct tcp_md5sig_key	*(*md5_lookup) (const struct sock *sk,
2186fd3a154aSEric Dumazet 						const struct sock *addr_sk);
2187cfb6eeb4SYOSHIFUJI Hideaki 	int		(*calc_md5_hash)(char *location,
218839f8e58eSEric Dumazet 					 const struct tcp_md5sig_key *md5,
2189318cf7aaSEric Dumazet 					 const struct sock *sk,
2190318cf7aaSEric Dumazet 					 const struct sk_buff *skb);
2191cfb6eeb4SYOSHIFUJI Hideaki 	int		(*md5_parse)(struct sock *sk,
21928917a777SIvan Delalande 				     int optname,
2193d4c19c49SChristoph Hellwig 				     sockptr_t optval,
2194cfb6eeb4SYOSHIFUJI Hideaki 				     int optlen);
2195cfb6eeb4SYOSHIFUJI Hideaki #endif
21964954f17dSDmitry Safonov #ifdef CONFIG_TCP_AO
21974954f17dSDmitry Safonov 	int (*ao_parse)(struct sock *sk, int optname, sockptr_t optval, int optlen);
21980aadc739SDmitry Safonov 	struct tcp_ao_key *(*ao_lookup)(const struct sock *sk,
21990aadc739SDmitry Safonov 					struct sock *addr_sk,
22000aadc739SDmitry Safonov 					int sndid, int rcvid);
22017c2ffaf2SDmitry Safonov 	int (*ao_calc_key_sk)(struct tcp_ao_key *mkt, u8 *key,
22027c2ffaf2SDmitry Safonov 			      const struct sock *sk,
22037c2ffaf2SDmitry Safonov 			      __be32 sisn, __be32 disn, bool send);
2204*1e03d32bSDmitry Safonov 	int (*calc_ao_hash)(char *location, struct tcp_ao_key *ao,
2205*1e03d32bSDmitry Safonov 			    const struct sock *sk, const struct sk_buff *skb,
2206*1e03d32bSDmitry Safonov 			    const u8 *tkey, int hash_offset, u32 sne);
22074954f17dSDmitry Safonov #endif
2208cfb6eeb4SYOSHIFUJI Hideaki };
2209cfb6eeb4SYOSHIFUJI Hideaki 
2210cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops {
22112aec4a29SOctavian Purdila 	u16 mss_clamp;
2212cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
2213b83e3debSEric Dumazet 	struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk,
2214fd3a154aSEric Dumazet 						 const struct sock *addr_sk);
2215e3afe7b7SJohn Dykstra 	int		(*calc_md5_hash) (char *location,
221639f8e58eSEric Dumazet 					  const struct tcp_md5sig_key *md5,
2217318cf7aaSEric Dumazet 					  const struct sock *sk,
2218318cf7aaSEric Dumazet 					  const struct sk_buff *skb);
2219cfb6eeb4SYOSHIFUJI Hideaki #endif
2220fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
22213f684b4bSEric Dumazet 	__u32 (*cookie_init_seq)(const struct sk_buff *skb,
2222fb7b37a7SOctavian Purdila 				 __u16 *mss);
2223fb7b37a7SOctavian Purdila #endif
22247ea851d1SFlorian Westphal 	struct dst_entry *(*route_req)(const struct sock *sk,
22257ea851d1SFlorian Westphal 				       struct sk_buff *skb,
22267ea851d1SFlorian Westphal 				       struct flowi *fl,
22277ea851d1SFlorian Westphal 				       struct request_sock *req);
222884b114b9SEric Dumazet 	u32 (*init_seq)(const struct sk_buff *skb);
22295d2ed052SEric Dumazet 	u32 (*init_ts_off)(const struct net *net, const struct sk_buff *skb);
22300f935dbeSEric Dumazet 	int (*send_synack)(const struct sock *sk, struct dst_entry *dst,
2231d6274bd8SOctavian Purdila 			   struct flowi *fl, struct request_sock *req,
2232dc6ef6beSEric Dumazet 			   struct tcp_fastopen_cookie *foc,
2233331fca43SMartin KaFai Lau 			   enum tcp_synack_type synack_type,
2234331fca43SMartin KaFai Lau 			   struct sk_buff *syn_skb);
2235cfb6eeb4SYOSHIFUJI Hideaki };
2236cfb6eeb4SYOSHIFUJI Hideaki 
223735b2c321SMat Martineau extern const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops;
223835b2c321SMat Martineau #if IS_ENABLED(CONFIG_IPV6)
223935b2c321SMat Martineau extern const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops;
224035b2c321SMat Martineau #endif
224135b2c321SMat Martineau 
2242fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
2243fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
22443f684b4bSEric Dumazet 					 const struct sock *sk, struct sk_buff *skb,
2245fb7b37a7SOctavian Purdila 					 __u16 *mss)
2246fb7b37a7SOctavian Purdila {
22473f684b4bSEric Dumazet 	tcp_synq_overflow(sk);
224802a1d6e7SEric Dumazet 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
22493f684b4bSEric Dumazet 	return ops->cookie_init_seq(skb, mss);
2250fb7b37a7SOctavian Purdila }
2251fb7b37a7SOctavian Purdila #else
2252fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
22533f684b4bSEric Dumazet 					 const struct sock *sk, struct sk_buff *skb,
2254fb7b37a7SOctavian Purdila 					 __u16 *mss)
2255fb7b37a7SOctavian Purdila {
2256fb7b37a7SOctavian Purdila 	return 0;
2257fb7b37a7SOctavian Purdila }
2258fb7b37a7SOctavian Purdila #endif
2259fb7b37a7SOctavian Purdila 
2260*1e03d32bSDmitry Safonov struct tcp_key {
2261*1e03d32bSDmitry Safonov 	union {
2262*1e03d32bSDmitry Safonov 		struct tcp_ao_key *ao_key;
2263*1e03d32bSDmitry Safonov 		struct tcp_md5sig_key *md5_key;
2264*1e03d32bSDmitry Safonov 	};
2265*1e03d32bSDmitry Safonov 	enum {
2266*1e03d32bSDmitry Safonov 		TCP_KEY_NONE = 0,
2267*1e03d32bSDmitry Safonov 		TCP_KEY_MD5,
2268*1e03d32bSDmitry Safonov 		TCP_KEY_AO,
2269*1e03d32bSDmitry Safonov 	} type;
2270*1e03d32bSDmitry Safonov };
2271*1e03d32bSDmitry Safonov 
2272*1e03d32bSDmitry Safonov static inline void tcp_get_current_key(const struct sock *sk,
2273*1e03d32bSDmitry Safonov 				       struct tcp_key *out)
2274*1e03d32bSDmitry Safonov {
2275*1e03d32bSDmitry Safonov #if defined(CONFIG_TCP_AO) || defined(CONFIG_TCP_MD5SIG)
2276*1e03d32bSDmitry Safonov 	const struct tcp_sock *tp = tcp_sk(sk);
2277*1e03d32bSDmitry Safonov #endif
2278*1e03d32bSDmitry Safonov #ifdef CONFIG_TCP_AO
2279*1e03d32bSDmitry Safonov 	struct tcp_ao_info *ao;
2280*1e03d32bSDmitry Safonov 
2281*1e03d32bSDmitry Safonov 	ao = rcu_dereference_protected(tp->ao_info, lockdep_sock_is_held(sk));
2282*1e03d32bSDmitry Safonov 	if (ao) {
2283*1e03d32bSDmitry Safonov 		out->ao_key = READ_ONCE(ao->current_key);
2284*1e03d32bSDmitry Safonov 		out->type = TCP_KEY_AO;
2285*1e03d32bSDmitry Safonov 		return;
2286*1e03d32bSDmitry Safonov 	}
2287*1e03d32bSDmitry Safonov #endif
2288*1e03d32bSDmitry Safonov #ifdef CONFIG_TCP_MD5SIG
2289*1e03d32bSDmitry Safonov 	if (static_branch_unlikely(&tcp_md5_needed.key) &&
2290*1e03d32bSDmitry Safonov 	    rcu_access_pointer(tp->md5sig_info)) {
2291*1e03d32bSDmitry Safonov 		out->md5_key = tp->af_specific->md5_lookup(sk, sk);
2292*1e03d32bSDmitry Safonov 		if (out->md5_key) {
2293*1e03d32bSDmitry Safonov 			out->type = TCP_KEY_MD5;
2294*1e03d32bSDmitry Safonov 			return;
2295*1e03d32bSDmitry Safonov 		}
2296*1e03d32bSDmitry Safonov 	}
2297*1e03d32bSDmitry Safonov #endif
2298*1e03d32bSDmitry Safonov 	out->type = TCP_KEY_NONE;
2299*1e03d32bSDmitry Safonov }
2300*1e03d32bSDmitry Safonov 
2301*1e03d32bSDmitry Safonov static inline bool tcp_key_is_md5(const struct tcp_key *key)
2302*1e03d32bSDmitry Safonov {
2303*1e03d32bSDmitry Safonov #ifdef CONFIG_TCP_MD5SIG
2304*1e03d32bSDmitry Safonov 	if (static_branch_unlikely(&tcp_md5_needed.key) &&
2305*1e03d32bSDmitry Safonov 	    key->type == TCP_KEY_MD5)
2306*1e03d32bSDmitry Safonov 		return true;
2307*1e03d32bSDmitry Safonov #endif
2308*1e03d32bSDmitry Safonov 	return false;
2309*1e03d32bSDmitry Safonov }
2310*1e03d32bSDmitry Safonov 
2311*1e03d32bSDmitry Safonov static inline bool tcp_key_is_ao(const struct tcp_key *key)
2312*1e03d32bSDmitry Safonov {
2313*1e03d32bSDmitry Safonov #ifdef CONFIG_TCP_AO
2314*1e03d32bSDmitry Safonov 	if (key->type == TCP_KEY_AO)
2315*1e03d32bSDmitry Safonov 		return true;
2316*1e03d32bSDmitry Safonov #endif
2317*1e03d32bSDmitry Safonov 	return false;
2318*1e03d32bSDmitry Safonov }
2319*1e03d32bSDmitry Safonov 
23205c9f3023SJoe Perches int tcpv4_offload_init(void);
232128850dc7SDaniel Borkmann 
23225c9f3023SJoe Perches void tcp_v4_init(void);
23235c9f3023SJoe Perches void tcp_init(void);
232420380731SArnaldo Carvalho de Melo 
2325659a8ad5SYuchung Cheng /* tcp_recovery.c */
2326d716bfdbSYuchung Cheng void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb);
23276ac06ecdSYuchung Cheng void tcp_newreno_mark_lost(struct sock *sk, bool snd_una_advanced);
2328b8fef65aSYuchung Cheng extern s32 tcp_rack_skb_timeout(struct tcp_sock *tp, struct sk_buff *skb,
2329b8fef65aSYuchung Cheng 				u32 reo_wnd);
233062d9f1a6SPengcheng Yang extern bool tcp_rack_mark_lost(struct sock *sk);
23311d0833dfSYuchung Cheng extern void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq,
23329a568de4SEric Dumazet 			     u64 xmit_time);
233357dde7f7SYuchung Cheng extern void tcp_rack_reo_timeout(struct sock *sk);
23341f255691SPriyaranjan Jha extern void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs);
2335659a8ad5SYuchung Cheng 
23361a91bb7cSMubashir Adnan Qureshi /* tcp_plb.c */
23371a91bb7cSMubashir Adnan Qureshi 
23381a91bb7cSMubashir Adnan Qureshi /*
23391a91bb7cSMubashir Adnan Qureshi  * Scaling factor for fractions in PLB. For example, tcp_plb_update_state
23401a91bb7cSMubashir Adnan Qureshi  * expects cong_ratio which represents fraction of traffic that experienced
23411a91bb7cSMubashir Adnan Qureshi  * congestion over a single RTT. In order to avoid floating point operations,
23421a91bb7cSMubashir Adnan Qureshi  * this fraction should be mapped to (1 << TCP_PLB_SCALE) and passed in.
23431a91bb7cSMubashir Adnan Qureshi  */
23441a91bb7cSMubashir Adnan Qureshi #define TCP_PLB_SCALE 8
23451a91bb7cSMubashir Adnan Qureshi 
23461a91bb7cSMubashir Adnan Qureshi /* State for PLB (Protective Load Balancing) for a single TCP connection. */
23471a91bb7cSMubashir Adnan Qureshi struct tcp_plb_state {
23481a91bb7cSMubashir Adnan Qureshi 	u8	consec_cong_rounds:5, /* consecutive congested rounds */
23491a91bb7cSMubashir Adnan Qureshi 		unused:3;
23501a91bb7cSMubashir Adnan Qureshi 	u32	pause_until; /* jiffies32 when PLB can resume rerouting */
23511a91bb7cSMubashir Adnan Qureshi };
23521a91bb7cSMubashir Adnan Qureshi 
23531a91bb7cSMubashir Adnan Qureshi static inline void tcp_plb_init(const struct sock *sk,
23541a91bb7cSMubashir Adnan Qureshi 				struct tcp_plb_state *plb)
23551a91bb7cSMubashir Adnan Qureshi {
23561a91bb7cSMubashir Adnan Qureshi 	plb->consec_cong_rounds = 0;
23571a91bb7cSMubashir Adnan Qureshi 	plb->pause_until = 0;
23581a91bb7cSMubashir Adnan Qureshi }
23591a91bb7cSMubashir Adnan Qureshi void tcp_plb_update_state(const struct sock *sk, struct tcp_plb_state *plb,
23601a91bb7cSMubashir Adnan Qureshi 			  const int cong_ratio);
23611a91bb7cSMubashir Adnan Qureshi void tcp_plb_check_rehash(struct sock *sk, struct tcp_plb_state *plb);
23621a91bb7cSMubashir Adnan Qureshi void tcp_plb_update_state_upon_rto(struct sock *sk, struct tcp_plb_state *plb);
23631a91bb7cSMubashir Adnan Qureshi 
2364e1a10ef7SNeal Cardwell /* At how many usecs into the future should the RTO fire? */
2365e1a10ef7SNeal Cardwell static inline s64 tcp_rto_delta_us(const struct sock *sk)
2366e1a10ef7SNeal Cardwell {
236775c119afSEric Dumazet 	const struct sk_buff *skb = tcp_rtx_queue_head(sk);
2368e1a10ef7SNeal Cardwell 	u32 rto = inet_csk(sk)->icsk_rto;
23692fd66ffbSEric Dumazet 	u64 rto_time_stamp_us = tcp_skb_timestamp_us(skb) + jiffies_to_usecs(rto);
2370e1a10ef7SNeal Cardwell 
2371e1a10ef7SNeal Cardwell 	return rto_time_stamp_us - tcp_sk(sk)->tcp_mstamp;
2372e1a10ef7SNeal Cardwell }
2373e1a10ef7SNeal Cardwell 
2374e25f866fSCong Wang /*
2375e25f866fSCong Wang  * Save and compile IPv4 options, return a pointer to it
2376e25f866fSCong Wang  */
237791ed1e66SPaolo Abeni static inline struct ip_options_rcu *tcp_v4_save_options(struct net *net,
237891ed1e66SPaolo Abeni 							 struct sk_buff *skb)
2379e25f866fSCong Wang {
2380e25f866fSCong Wang 	const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
2381e25f866fSCong Wang 	struct ip_options_rcu *dopt = NULL;
2382e25f866fSCong Wang 
2383461b74c3SCong Wang 	if (opt->optlen) {
2384e25f866fSCong Wang 		int opt_size = sizeof(*dopt) + opt->optlen;
2385e25f866fSCong Wang 
2386e25f866fSCong Wang 		dopt = kmalloc(opt_size, GFP_ATOMIC);
238791ed1e66SPaolo Abeni 		if (dopt && __ip_options_echo(net, &dopt->opt, skb, opt)) {
2388e25f866fSCong Wang 			kfree(dopt);
2389e25f866fSCong Wang 			dopt = NULL;
2390e25f866fSCong Wang 		}
2391e25f866fSCong Wang 	}
2392e25f866fSCong Wang 	return dopt;
2393e25f866fSCong Wang }
2394e25f866fSCong Wang 
239598781965SEric Dumazet /* locally generated TCP pure ACKs have skb->truesize == 2
239698781965SEric Dumazet  * (check tcp_send_ack() in net/ipv4/tcp_output.c )
239798781965SEric Dumazet  * This is much faster than dissecting the packet to find out.
239898781965SEric Dumazet  * (Think of GRE encapsulations, IPv4, IPv6, ...)
239998781965SEric Dumazet  */
240098781965SEric Dumazet static inline bool skb_is_tcp_pure_ack(const struct sk_buff *skb)
240198781965SEric Dumazet {
240298781965SEric Dumazet 	return skb->truesize == 2;
240398781965SEric Dumazet }
240498781965SEric Dumazet 
240598781965SEric Dumazet static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
240698781965SEric Dumazet {
240798781965SEric Dumazet 	skb->truesize = 2;
240898781965SEric Dumazet }
240998781965SEric Dumazet 
2410473bd239STom Herbert static inline int tcp_inq(struct sock *sk)
2411473bd239STom Herbert {
2412473bd239STom Herbert 	struct tcp_sock *tp = tcp_sk(sk);
2413473bd239STom Herbert 	int answ;
2414473bd239STom Herbert 
2415473bd239STom Herbert 	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
2416473bd239STom Herbert 		answ = 0;
2417473bd239STom Herbert 	} else if (sock_flag(sk, SOCK_URGINLINE) ||
2418473bd239STom Herbert 		   !tp->urg_data ||
2419473bd239STom Herbert 		   before(tp->urg_seq, tp->copied_seq) ||
2420473bd239STom Herbert 		   !before(tp->urg_seq, tp->rcv_nxt)) {
2421473bd239STom Herbert 
2422473bd239STom Herbert 		answ = tp->rcv_nxt - tp->copied_seq;
2423473bd239STom Herbert 
2424473bd239STom Herbert 		/* Subtract 1, if FIN was received */
2425473bd239STom Herbert 		if (answ && sock_flag(sk, SOCK_DONE))
2426473bd239STom Herbert 			answ--;
2427473bd239STom Herbert 	} else {
2428473bd239STom Herbert 		answ = tp->urg_seq - tp->copied_seq;
2429473bd239STom Herbert 	}
2430473bd239STom Herbert 
2431473bd239STom Herbert 	return answ;
2432473bd239STom Herbert }
2433473bd239STom Herbert 
243432035585STom Herbert int tcp_peek_len(struct socket *sock);
243532035585STom Herbert 
2436a44d6eacSMartin KaFai Lau static inline void tcp_segs_in(struct tcp_sock *tp, const struct sk_buff *skb)
2437a44d6eacSMartin KaFai Lau {
2438a44d6eacSMartin KaFai Lau 	u16 segs_in;
2439a44d6eacSMartin KaFai Lau 
2440a44d6eacSMartin KaFai Lau 	segs_in = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
24410307a0b7SEric Dumazet 
24420307a0b7SEric Dumazet 	/* We update these fields while other threads might
24430307a0b7SEric Dumazet 	 * read them from tcp_get_info()
24440307a0b7SEric Dumazet 	 */
24450307a0b7SEric Dumazet 	WRITE_ONCE(tp->segs_in, tp->segs_in + segs_in);
2446a44d6eacSMartin KaFai Lau 	if (skb->len > tcp_hdrlen(skb))
24470307a0b7SEric Dumazet 		WRITE_ONCE(tp->data_segs_in, tp->data_segs_in + segs_in);
2448a44d6eacSMartin KaFai Lau }
2449a44d6eacSMartin KaFai Lau 
24509caad864SEric Dumazet /*
24519caad864SEric Dumazet  * TCP listen path runs lockless.
24529caad864SEric Dumazet  * We forced "struct sock" to be const qualified to make sure
24539caad864SEric Dumazet  * we don't modify one of its field by mistake.
24549caad864SEric Dumazet  * Here, we increment sk_drops which is an atomic_t, so we can safely
24559caad864SEric Dumazet  * make sock writable again.
24569caad864SEric Dumazet  */
24579caad864SEric Dumazet static inline void tcp_listendrop(const struct sock *sk)
24589caad864SEric Dumazet {
24599caad864SEric Dumazet 	atomic_inc(&((struct sock *)sk)->sk_drops);
246002a1d6e7SEric Dumazet 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
24619caad864SEric Dumazet }
24629caad864SEric Dumazet 
2463218af599SEric Dumazet enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer);
2464218af599SEric Dumazet 
2465734942ccSDave Watson /*
2466734942ccSDave Watson  * Interface for adding Upper Level Protocols over TCP
2467734942ccSDave Watson  */
2468734942ccSDave Watson 
2469734942ccSDave Watson #define TCP_ULP_NAME_MAX	16
2470734942ccSDave Watson #define TCP_ULP_MAX		128
2471734942ccSDave Watson #define TCP_ULP_BUF_MAX		(TCP_ULP_NAME_MAX*TCP_ULP_MAX)
2472734942ccSDave Watson 
2473734942ccSDave Watson struct tcp_ulp_ops {
2474734942ccSDave Watson 	struct list_head	list;
2475734942ccSDave Watson 
2476734942ccSDave Watson 	/* initialize ulp */
2477734942ccSDave Watson 	int (*init)(struct sock *sk);
247895fa1454SJohn Fastabend 	/* update ulp */
247933bfe20dSJohn Fastabend 	void (*update)(struct sock *sk, struct proto *p,
248033bfe20dSJohn Fastabend 		       void (*write_space)(struct sock *sk));
2481734942ccSDave Watson 	/* cleanup ulp */
2482734942ccSDave Watson 	void (*release)(struct sock *sk);
248361723b39SDavide Caratti 	/* diagnostic */
248461723b39SDavide Caratti 	int (*get_info)(const struct sock *sk, struct sk_buff *skb);
248561723b39SDavide Caratti 	size_t (*get_info_size)(const struct sock *sk);
248613230593SMat Martineau 	/* clone ulp */
248713230593SMat Martineau 	void (*clone)(const struct request_sock *req, struct sock *newsk,
248813230593SMat Martineau 		      const gfp_t priority);
2489734942ccSDave Watson 
2490734942ccSDave Watson 	char		name[TCP_ULP_NAME_MAX];
2491734942ccSDave Watson 	struct module	*owner;
2492734942ccSDave Watson };
2493734942ccSDave Watson int tcp_register_ulp(struct tcp_ulp_ops *type);
2494734942ccSDave Watson void tcp_unregister_ulp(struct tcp_ulp_ops *type);
2495734942ccSDave Watson int tcp_set_ulp(struct sock *sk, const char *name);
2496734942ccSDave Watson void tcp_get_available_ulp(char *buf, size_t len);
2497734942ccSDave Watson void tcp_cleanup_ulp(struct sock *sk);
249833bfe20dSJohn Fastabend void tcp_update_ulp(struct sock *sk, struct proto *p,
249933bfe20dSJohn Fastabend 		    void (*write_space)(struct sock *sk));
2500734942ccSDave Watson 
2501037b0b86SDaniel Borkmann #define MODULE_ALIAS_TCP_ULP(name)				\
2502037b0b86SDaniel Borkmann 	__MODULE_INFO(alias, alias_userspace, name);		\
2503037b0b86SDaniel Borkmann 	__MODULE_INFO(alias, alias_tcp_ulp, "tcp-ulp-" name)
2504037b0b86SDaniel Borkmann 
250588759609SCong Wang #ifdef CONFIG_NET_SOCK_MSG
2506604326b4SDaniel Borkmann struct sk_msg;
2507604326b4SDaniel Borkmann struct sk_psock;
2508604326b4SDaniel Borkmann 
250988759609SCong Wang #ifdef CONFIG_BPF_SYSCALL
251051e0158aSCong Wang int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore);
2511f747632bSLorenz Bauer void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
251288759609SCong Wang #endif /* CONFIG_BPF_SYSCALL */
2513f747632bSLorenz Bauer 
2514e5c6de5fSJohn Fastabend #ifdef CONFIG_INET
2515e5c6de5fSJohn Fastabend void tcp_eat_skb(struct sock *sk, struct sk_buff *skb);
2516e5c6de5fSJohn Fastabend #else
2517e5c6de5fSJohn Fastabend static inline void tcp_eat_skb(struct sock *sk, struct sk_buff *skb)
2518e5c6de5fSJohn Fastabend {
2519e5c6de5fSJohn Fastabend }
2520e5c6de5fSJohn Fastabend #endif
2521e5c6de5fSJohn Fastabend 
2522a351d608SPengcheng Yang int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress,
2523a351d608SPengcheng Yang 			  struct sk_msg *msg, u32 bytes, int flags);
25245da00404SLorenz Bauer #endif /* CONFIG_NET_SOCK_MSG */
2525604326b4SDaniel Borkmann 
252688759609SCong Wang #if !defined(CONFIG_BPF_SYSCALL) || !defined(CONFIG_NET_SOCK_MSG)
252788759609SCong Wang static inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
252888759609SCong Wang {
252988759609SCong Wang }
253088759609SCong Wang #endif
253188759609SCong Wang 
25320813a841SMartin KaFai Lau #ifdef CONFIG_CGROUP_BPF
25330813a841SMartin KaFai Lau static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
25340813a841SMartin KaFai Lau 				      struct sk_buff *skb,
25350813a841SMartin KaFai Lau 				      unsigned int end_offset)
25360813a841SMartin KaFai Lau {
25370813a841SMartin KaFai Lau 	skops->skb = skb;
25380813a841SMartin KaFai Lau 	skops->skb_data_end = skb->data + end_offset;
25390813a841SMartin KaFai Lau }
25400813a841SMartin KaFai Lau #else
25410813a841SMartin KaFai Lau static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
25420813a841SMartin KaFai Lau 				      struct sk_buff *skb,
25430813a841SMartin KaFai Lau 				      unsigned int end_offset)
25440813a841SMartin KaFai Lau {
25450813a841SMartin KaFai Lau }
25460813a841SMartin KaFai Lau #endif
25470813a841SMartin KaFai Lau 
254840304b2aSLawrence Brakmo /* Call BPF_SOCK_OPS program that returns an int. If the return value
254940304b2aSLawrence Brakmo  * is < 0, then the BPF op failed (for example if the loaded BPF
255040304b2aSLawrence Brakmo  * program does not support the chosen operation or there is no BPF
255140304b2aSLawrence Brakmo  * program loaded).
255240304b2aSLawrence Brakmo  */
255340304b2aSLawrence Brakmo #ifdef CONFIG_BPF
2554de525be2SLawrence Brakmo static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
255540304b2aSLawrence Brakmo {
255640304b2aSLawrence Brakmo 	struct bpf_sock_ops_kern sock_ops;
255740304b2aSLawrence Brakmo 	int ret;
255840304b2aSLawrence Brakmo 
2559b73042b8SLawrence Brakmo 	memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
2560f19397a5SLawrence Brakmo 	if (sk_fullsock(sk)) {
2561f19397a5SLawrence Brakmo 		sock_ops.is_fullsock = 1;
256240304b2aSLawrence Brakmo 		sock_owned_by_me(sk);
2563f19397a5SLawrence Brakmo 	}
256440304b2aSLawrence Brakmo 
256540304b2aSLawrence Brakmo 	sock_ops.sk = sk;
256640304b2aSLawrence Brakmo 	sock_ops.op = op;
2567de525be2SLawrence Brakmo 	if (nargs > 0)
2568de525be2SLawrence Brakmo 		memcpy(sock_ops.args, args, nargs * sizeof(*args));
256940304b2aSLawrence Brakmo 
257040304b2aSLawrence Brakmo 	ret = BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
257140304b2aSLawrence Brakmo 	if (ret == 0)
257240304b2aSLawrence Brakmo 		ret = sock_ops.reply;
257340304b2aSLawrence Brakmo 	else
257440304b2aSLawrence Brakmo 		ret = -1;
257540304b2aSLawrence Brakmo 	return ret;
257640304b2aSLawrence Brakmo }
2577de525be2SLawrence Brakmo 
2578de525be2SLawrence Brakmo static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
2579de525be2SLawrence Brakmo {
2580de525be2SLawrence Brakmo 	u32 args[2] = {arg1, arg2};
2581de525be2SLawrence Brakmo 
2582de525be2SLawrence Brakmo 	return tcp_call_bpf(sk, op, 2, args);
2583de525be2SLawrence Brakmo }
2584de525be2SLawrence Brakmo 
2585de525be2SLawrence Brakmo static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
2586de525be2SLawrence Brakmo 				    u32 arg3)
2587de525be2SLawrence Brakmo {
2588de525be2SLawrence Brakmo 	u32 args[3] = {arg1, arg2, arg3};
2589de525be2SLawrence Brakmo 
2590de525be2SLawrence Brakmo 	return tcp_call_bpf(sk, op, 3, args);
2591de525be2SLawrence Brakmo }
2592de525be2SLawrence Brakmo 
259340304b2aSLawrence Brakmo #else
2594de525be2SLawrence Brakmo static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
259540304b2aSLawrence Brakmo {
259640304b2aSLawrence Brakmo 	return -EPERM;
259740304b2aSLawrence Brakmo }
2598de525be2SLawrence Brakmo 
2599de525be2SLawrence Brakmo static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
2600de525be2SLawrence Brakmo {
2601de525be2SLawrence Brakmo 	return -EPERM;
2602de525be2SLawrence Brakmo }
2603de525be2SLawrence Brakmo 
2604de525be2SLawrence Brakmo static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
2605de525be2SLawrence Brakmo 				    u32 arg3)
2606de525be2SLawrence Brakmo {
2607de525be2SLawrence Brakmo 	return -EPERM;
2608de525be2SLawrence Brakmo }
2609de525be2SLawrence Brakmo 
261040304b2aSLawrence Brakmo #endif
261140304b2aSLawrence Brakmo 
26128550f328SLawrence Brakmo static inline u32 tcp_timeout_init(struct sock *sk)
26138550f328SLawrence Brakmo {
26148550f328SLawrence Brakmo 	int timeout;
26158550f328SLawrence Brakmo 
2616de525be2SLawrence Brakmo 	timeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL);
26178550f328SLawrence Brakmo 
26188550f328SLawrence Brakmo 	if (timeout <= 0)
26198550f328SLawrence Brakmo 		timeout = TCP_TIMEOUT_INIT;
26205903123fSAkhmat Karakotov 	return min_t(int, timeout, TCP_RTO_MAX);
26218550f328SLawrence Brakmo }
26228550f328SLawrence Brakmo 
262313d3b1ebSLawrence Brakmo static inline u32 tcp_rwnd_init_bpf(struct sock *sk)
262413d3b1ebSLawrence Brakmo {
262513d3b1ebSLawrence Brakmo 	int rwnd;
262613d3b1ebSLawrence Brakmo 
2627de525be2SLawrence Brakmo 	rwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL);
262813d3b1ebSLawrence Brakmo 
262913d3b1ebSLawrence Brakmo 	if (rwnd < 0)
263013d3b1ebSLawrence Brakmo 		rwnd = 0;
263113d3b1ebSLawrence Brakmo 	return rwnd;
263213d3b1ebSLawrence Brakmo }
263391b5b21cSLawrence Brakmo 
263491b5b21cSLawrence Brakmo static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk)
263591b5b21cSLawrence Brakmo {
2636de525be2SLawrence Brakmo 	return (tcp_call_bpf(sk, BPF_SOCK_OPS_NEEDS_ECN, 0, NULL) == 1);
263791b5b21cSLawrence Brakmo }
263860e2a778SUrsula Braun 
263923729ff2SStanislav Fomichev static inline void tcp_bpf_rtt(struct sock *sk)
264023729ff2SStanislav Fomichev {
2641bef8e263SArnd Bergmann 	if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_RTT_CB_FLAG))
264223729ff2SStanislav Fomichev 		tcp_call_bpf(sk, BPF_SOCK_OPS_RTT_CB, 0, NULL);
264323729ff2SStanislav Fomichev }
264423729ff2SStanislav Fomichev 
264560e2a778SUrsula Braun #if IS_ENABLED(CONFIG_SMC)
264660e2a778SUrsula Braun extern struct static_key_false tcp_have_smc;
264760e2a778SUrsula Braun #endif
26486dac1523SIlya Lesokhin 
26496dac1523SIlya Lesokhin #if IS_ENABLED(CONFIG_TLS_DEVICE)
26506dac1523SIlya Lesokhin void clean_acked_data_enable(struct inet_connection_sock *icsk,
26516dac1523SIlya Lesokhin 			     void (*cad)(struct sock *sk, u32 ack_seq));
26526dac1523SIlya Lesokhin void clean_acked_data_disable(struct inet_connection_sock *icsk);
2653494bc1d2SJakub Kicinski void clean_acked_data_flush(void);
26546dac1523SIlya Lesokhin #endif
26556dac1523SIlya Lesokhin 
2656a842fe14SEric Dumazet DECLARE_STATIC_KEY_FALSE(tcp_tx_delay_enabled);
2657a842fe14SEric Dumazet static inline void tcp_add_tx_delay(struct sk_buff *skb,
2658a842fe14SEric Dumazet 				    const struct tcp_sock *tp)
2659a842fe14SEric Dumazet {
2660a842fe14SEric Dumazet 	if (static_branch_unlikely(&tcp_tx_delay_enabled))
2661a842fe14SEric Dumazet 		skb->skb_mstamp_ns += (u64)tp->tcp_tx_delay * NSEC_PER_USEC;
2662a842fe14SEric Dumazet }
2663a842fe14SEric Dumazet 
2664d6fb396cSEric Dumazet /* Compute Earliest Departure Time for some control packets
2665d6fb396cSEric Dumazet  * like ACK or RST for TIME_WAIT or non ESTABLISHED sockets.
2666d6fb396cSEric Dumazet  */
2667d6fb396cSEric Dumazet static inline u64 tcp_transmit_time(const struct sock *sk)
2668a842fe14SEric Dumazet {
2669a842fe14SEric Dumazet 	if (static_branch_unlikely(&tcp_tx_delay_enabled)) {
2670a842fe14SEric Dumazet 		u32 delay = (sk->sk_state == TCP_TIME_WAIT) ?
2671a842fe14SEric Dumazet 			tcp_twsk(sk)->tw_tx_delay : tcp_sk(sk)->tcp_tx_delay;
2672a842fe14SEric Dumazet 
2673d6fb396cSEric Dumazet 		return tcp_clock_ns() + (u64)delay * NSEC_PER_USEC;
2674a842fe14SEric Dumazet 	}
2675d6fb396cSEric Dumazet 	return 0;
2676a842fe14SEric Dumazet }
2677a842fe14SEric Dumazet 
26780aadc739SDmitry Safonov static inline bool tcp_ao_required(struct sock *sk, const void *saddr,
26790aadc739SDmitry Safonov 				   int family)
26800aadc739SDmitry Safonov {
26810aadc739SDmitry Safonov #ifdef CONFIG_TCP_AO
26820aadc739SDmitry Safonov 	struct tcp_ao_info *ao_info;
26830aadc739SDmitry Safonov 	struct tcp_ao_key *ao_key;
26840aadc739SDmitry Safonov 
26850aadc739SDmitry Safonov 	ao_info = rcu_dereference_check(tcp_sk(sk)->ao_info,
26860aadc739SDmitry Safonov 					lockdep_sock_is_held(sk));
26870aadc739SDmitry Safonov 	if (!ao_info)
26880aadc739SDmitry Safonov 		return false;
26890aadc739SDmitry Safonov 
26900aadc739SDmitry Safonov 	ao_key = tcp_ao_do_lookup(sk, saddr, family, -1, -1);
26910aadc739SDmitry Safonov 	if (ao_info->ao_required || ao_key)
26920aadc739SDmitry Safonov 		return true;
26930aadc739SDmitry Safonov #endif
26940aadc739SDmitry Safonov 	return false;
26950aadc739SDmitry Safonov }
26960aadc739SDmitry Safonov 
26971da177e4SLinus Torvalds #endif	/* _TCP_H */
2698