xref: /linux/include/net/tcp.h (revision 05e22e8395058745bd0312bc488b522197852aff)
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>
28*05e22e83SEric Dumazet #include <linux/indirect_call_wrapper.h>
293f421baaSArnaldo Carvalho de Melo 
303f421baaSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
31295ff7edSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h>
3277d8bf9cSArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
331da177e4SLinus Torvalds #include <net/checksum.h>
342e6599cbSArnaldo Carvalho de Melo #include <net/request_sock.h>
3540a1227eSMartin KaFai Lau #include <net/sock_reuseport.h>
361da177e4SLinus Torvalds #include <net/sock.h>
371da177e4SLinus Torvalds #include <net/snmp.h>
381da177e4SLinus Torvalds #include <net/ip.h>
39c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
40bdf1ee5dSIlpo Järvinen #include <net/inet_ecn.h>
410c266898SSatoru SATOH #include <net/dst.h>
4285712484SMat Martineau #include <net/mptcp.h>
43c752f073SArnaldo Carvalho de Melo 
441da177e4SLinus Torvalds #include <linux/seq_file.h>
45180d8cd9SGlauber Costa #include <linux/memcontrol.h>
4640304b2aSLawrence Brakmo #include <linux/bpf-cgroup.h>
47438ac880SArd Biesheuvel #include <linux/siphash.h>
4840304b2aSLawrence Brakmo 
490f7ff927SArnaldo Carvalho de Melo extern struct inet_hashinfo tcp_hashinfo;
501da177e4SLinus Torvalds 
51dd24c001SEric Dumazet extern struct percpu_counter tcp_orphan_count;
525c9f3023SJoe Perches void tcp_time_wait(struct sock *sk, int state, int timeo);
531da177e4SLinus Torvalds 
549bacd256SEric Dumazet #define MAX_TCP_HEADER	L1_CACHE_ALIGN(128 + MAX_HEADER)
5533ad798cSAdam Langley #define MAX_TCP_OPTION_SPACE 40
563b4929f6SEric Dumazet #define TCP_MIN_SND_MSS		48
573b4929f6SEric Dumazet #define TCP_MIN_GSO_SIZE	(TCP_MIN_SND_MSS - MAX_TCP_OPTION_SPACE)
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds  * Never offer a window over 32767 without using window scaling. Some
611da177e4SLinus Torvalds  * poor stacks do signed 16bit maths!
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds #define MAX_TCP_WINDOW		32767U
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
661da177e4SLinus Torvalds #define TCP_MIN_MSS		88U
671da177e4SLinus Torvalds 
681555e6fdSJosh Hunt /* The initial MTU to use for probing */
69dcd8fb85SFan Du #define TCP_BASE_MSS		1024
705d424d5aSJohn Heffner 
7105cbc0dbSFan Du /* probing interval, default to 10 minutes as per RFC4821 */
7205cbc0dbSFan Du #define TCP_PROBE_INTERVAL	600
7305cbc0dbSFan Du 
746b58e0a5SFan Du /* Specify interval when tcp mtu probing will stop */
756b58e0a5SFan Du #define TCP_PROBE_THRESHOLD	8
766b58e0a5SFan Du 
771da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
781da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
811da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
821da177e4SLinus Torvalds 
83589c49cbSGao Feng /* Maximal number of window scale according to RFC1323 */
84589c49cbSGao Feng #define TCP_MAX_WSCALE		14U
85589c49cbSGao Feng 
861da177e4SLinus Torvalds /* urg_data states */
871da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
881da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
891da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #define TCP_RETR1	3	/*
921da177e4SLinus Torvalds 				 * This is how many retries it does before it
931da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
941da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
951da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
961da177e4SLinus Torvalds 				 */
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds #define TCP_RETR2	15	/*
991da177e4SLinus Torvalds 				 * This should take at least
1001da177e4SLinus Torvalds 				 * 90 minutes to time out.
1011da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
1021da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
1031da177e4SLinus Torvalds 				 */
1041da177e4SLinus Torvalds 
1056c9ff979SAlex Bergmann #define TCP_SYN_RETRIES	 6	/* This is how many retries are done
1066c9ff979SAlex Bergmann 				 * when active opening a connection.
1076c9ff979SAlex Bergmann 				 * RFC1122 says the minimum retry MUST
1086c9ff979SAlex Bergmann 				 * be at least 180secs.  Nevertheless
1096c9ff979SAlex Bergmann 				 * this value is corresponding to
1106c9ff979SAlex Bergmann 				 * 63secs of retransmission with the
1116c9ff979SAlex Bergmann 				 * current initial RTO.
1126c9ff979SAlex Bergmann 				 */
1131da177e4SLinus Torvalds 
1146c9ff979SAlex Bergmann #define TCP_SYNACK_RETRIES 5	/* This is how may retries are done
1156c9ff979SAlex Bergmann 				 * when passive opening a connection.
1166c9ff979SAlex Bergmann 				 * This is corresponding to 31secs of
1176c9ff979SAlex Bergmann 				 * retransmission with the current
1186c9ff979SAlex Bergmann 				 * initial RTO.
1196c9ff979SAlex Bergmann 				 */
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1221da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1231da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1241da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1251da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1261da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1271da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1281da177e4SLinus Torvalds 				  */
129f0628c52SCambda Zhu #define TCP_FIN_TIMEOUT_MAX (120 * HZ) /* max TCP_LINGER2 value (two minutes) */
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1321da177e4SLinus Torvalds #if HZ >= 100
1331da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1341da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1351da177e4SLinus Torvalds #else
1361da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1371da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1381da177e4SLinus Torvalds #endif
1391da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1401da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
141bb4d991aSYuchung Cheng #define TCP_TIMEOUT_MIN	(2U) /* Min timeout for TCP timers in jiffies */
142fd4f2ceaSEric Dumazet #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))	/* RFC6298 2.1 initial RTO value	*/
1439ad7c049SJerry Chu #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value, now
1449ad7c049SJerry Chu 						 * used as a fallback RTO for the
1459ad7c049SJerry Chu 						 * initial data transmission if no
1469ad7c049SJerry Chu 						 * valid RTT sample has been acquired,
1479ad7c049SJerry Chu 						 * most likely due to retrans in 3WHS.
1489ad7c049SJerry Chu 						 */
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1511da177e4SLinus Torvalds 					                 * for local resources.
1521da177e4SLinus Torvalds 					                 */
1531da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1541da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1551da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1581da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1591da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1601da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1651da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1661da177e4SLinus Torvalds 					 * after this time. It should be equal
1671da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1681da177e4SLinus Torvalds 					 * to provide reliability equal to one
1691da177e4SLinus Torvalds 					 * provided by timewait state.
1701da177e4SLinus Torvalds 					 */
1711da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1721da177e4SLinus Torvalds 					 * timestamps. It must be less than
1731da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1741da177e4SLinus Torvalds 					 */
1751da177e4SLinus Torvalds /*
1761da177e4SLinus Torvalds  *	TCP option
1771da177e4SLinus Torvalds  */
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1801da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1811da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1821da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1831da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1841da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1851da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
186cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
187c74a39c8SMat Martineau #define TCPOPT_MPTCP		30	/* Multipath TCP (RFC6824) */
1887f9b838bSDaniel Lee #define TCPOPT_FASTOPEN		34	/* Fast open (RFC7413) */
1892100c8d2SYuchung Cheng #define TCPOPT_EXP		254	/* Experimental */
1902100c8d2SYuchung Cheng /* Magic number to be after the option value for sharing TCP
1912100c8d2SYuchung Cheng  * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
1922100c8d2SYuchung Cheng  */
1932100c8d2SYuchung Cheng #define TCPOPT_FASTOPEN_MAGIC	0xF989
19460e2a778SUrsula Braun #define TCPOPT_SMC_MAGIC	0xE2D4C3D9
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /*
1971da177e4SLinus Torvalds  *     TCP option lengths
1981da177e4SLinus Torvalds  */
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds #define TCPOLEN_MSS            4
2011da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
2021da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
2031da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
204cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG         18
2057f9b838bSDaniel Lee #define TCPOLEN_FASTOPEN_BASE  2
2062100c8d2SYuchung Cheng #define TCPOLEN_EXP_FASTOPEN_BASE  4
20760e2a778SUrsula Braun #define TCPOLEN_EXP_SMC_BASE   6
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds /* But this is what stacks really send out. */
2101da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
2111da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
2121da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
2131da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
2141da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
2151da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
216cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED		20
21733ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED		4
21860e2a778SUrsula Braun #define TCPOLEN_EXP_SMC_BASE_ALIGNED	8
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds /* Flags in tp->nonagle */
2211da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
2221da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
223caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
2241da177e4SLinus Torvalds 
22536e31b0aSAndreas Petlund /* TCP thin-stream limits */
22636e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
22736e31b0aSAndreas Petlund 
22821603fc4SJörg Thalheim /* TCP initial congestion window as per rfc6928 */
229442b9635SDavid S. Miller #define TCP_INIT_CWND		10
230442b9635SDavid S. Miller 
231cf60af03SYuchung Cheng /* Bit Flags for sysctl_tcp_fastopen */
232cf60af03SYuchung Cheng #define	TFO_CLIENT_ENABLE	1
23310467163SJerry Chu #define	TFO_SERVER_ENABLE	2
23467da22d2SYuchung Cheng #define	TFO_CLIENT_NO_COOKIE	4	/* Data in SYN w/o cookie option */
235cf60af03SYuchung Cheng 
23610467163SJerry Chu /* Accept SYN data w/o any cookie option */
23710467163SJerry Chu #define	TFO_SERVER_COOKIE_NOT_REQD	0x200
23810467163SJerry Chu 
23910467163SJerry Chu /* Force enable TFO on all listeners, i.e., not requiring the
240cebc5cbaSYuchung Cheng  * TCP_FASTOPEN socket option.
24110467163SJerry Chu  */
24210467163SJerry Chu #define	TFO_SERVER_WO_SOCKOPT1	0x400
24310467163SJerry Chu 
244295ff7edSArnaldo Carvalho de Melo 
2451da177e4SLinus Torvalds /* sysctl variables for tcp */
2461da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
247a4fe34bfSEric W. Biederman extern long sysctl_tcp_mem[3];
248e20223f1SEric Dumazet 
249a0370b3fSYuchung Cheng #define TCP_RACK_LOSS_DETECTION  0x1 /* Use RACK to detect losses */
2501f255691SPriyaranjan Jha #define TCP_RACK_STATIC_REO_WND  0x2 /* Use static RACK reo wnd */
25120b654dfSYuchung Cheng #define TCP_RACK_NO_DUPTHRESH    0x4 /* Do not use DUPACK threshold in RACK */
252a0370b3fSYuchung Cheng 
2538d987e5cSEric Dumazet extern atomic_long_t tcp_memory_allocated;
2541748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated;
25506044751SEric Dumazet extern unsigned long tcp_memory_pressure;
2561da177e4SLinus Torvalds 
257b8da51ebSEric Dumazet /* optimized version of sk_under_memory_pressure() for TCP sockets */
258b8da51ebSEric Dumazet static inline bool tcp_under_memory_pressure(const struct sock *sk)
259b8da51ebSEric Dumazet {
260baac50bbSJohannes Weiner 	if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
261baac50bbSJohannes Weiner 	    mem_cgroup_under_socket_pressure(sk->sk_memcg))
262e805605cSJohannes Weiner 		return true;
263b8da51ebSEric Dumazet 
2641f142c17SEric Dumazet 	return READ_ONCE(tcp_memory_pressure);
265b8da51ebSEric Dumazet }
2661da177e4SLinus Torvalds /*
2671da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
2681da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
2691da177e4SLinus Torvalds  */
2701da177e4SLinus Torvalds 
271a2a385d6SEric Dumazet static inline bool before(__u32 seq1, __u32 seq2)
2721da177e4SLinus Torvalds {
2730d630cc0SGerrit Renker         return (__s32)(seq1-seq2) < 0;
2741da177e4SLinus Torvalds }
2759a036b9cSGerrit Renker #define after(seq2, seq1) 	before(seq1, seq2)
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
278a2a385d6SEric Dumazet static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
283efcdbf24SArun Sharma static inline bool tcp_out_of_memory(struct sock *sk)
284efcdbf24SArun Sharma {
285efcdbf24SArun Sharma 	if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
286efcdbf24SArun Sharma 	    sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
287efcdbf24SArun Sharma 		return true;
288efcdbf24SArun Sharma 	return false;
289efcdbf24SArun Sharma }
290efcdbf24SArun Sharma 
291a6c5ea4cSEric Dumazet void sk_forced_mem_schedule(struct sock *sk, int size);
292a6c5ea4cSEric Dumazet 
293ad1af0feSDavid S. Miller static inline bool tcp_too_many_orphans(struct sock *sk, int shift)
294e4fd5da3SPavel Emelianov {
295ad1af0feSDavid S. Miller 	struct percpu_counter *ocp = sk->sk_prot->orphan_count;
296ad1af0feSDavid S. Miller 	int orphans = percpu_counter_read_positive(ocp);
297ad1af0feSDavid S. Miller 
298ad1af0feSDavid S. Miller 	if (orphans << shift > sysctl_tcp_max_orphans) {
299ad1af0feSDavid S. Miller 		orphans = percpu_counter_sum_positive(ocp);
300ad1af0feSDavid S. Miller 		if (orphans << shift > sysctl_tcp_max_orphans)
301ad1af0feSDavid S. Miller 			return true;
302ad1af0feSDavid S. Miller 	}
303ad1af0feSDavid S. Miller 	return false;
304e4fd5da3SPavel Emelianov }
3051da177e4SLinus Torvalds 
3065c9f3023SJoe Perches bool tcp_check_oom(struct sock *sk, int shift);
307efcdbf24SArun Sharma 
308a0f82f64SFlorian Westphal 
3091da177e4SLinus Torvalds extern struct proto tcp_prot;
3101da177e4SLinus Torvalds 
31157ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
31213415e46SEric Dumazet #define __TCP_INC_STATS(net, field)	__SNMP_INC_STATS((net)->mib.tcp_statistics, field)
31357ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
314aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
3151da177e4SLinus Torvalds 
3165c9f3023SJoe Perches void tcp_tasklet_init(void);
31746d3ceabSEric Dumazet 
31832bbd879SStefano Brivio int tcp_v4_err(struct sk_buff *skb, u32);
3191da177e4SLinus Torvalds 
3205c9f3023SJoe Perches void tcp_shutdown(struct sock *sk, int how);
3211da177e4SLinus Torvalds 
3227487449cSPaolo Abeni int tcp_v4_early_demux(struct sk_buff *skb);
3235c9f3023SJoe Perches int tcp_v4_rcv(struct sk_buff *skb);
3241da177e4SLinus Torvalds 
3255c9f3023SJoe Perches int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
3261b784140SYing Xue int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
327306b13ebSTom Herbert int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size);
3285c9f3023SJoe Perches int tcp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
3295c9f3023SJoe Perches 		 int flags);
330306b13ebSTom Herbert int tcp_sendpage_locked(struct sock *sk, struct page *page, int offset,
331306b13ebSTom Herbert 			size_t size, int flags);
332e3b5616aSDave Watson ssize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
333e3b5616aSDave Watson 		 size_t size, int flags);
33435b2c321SMat Martineau int tcp_send_mss(struct sock *sk, int *size_goal, int flags);
33535b2c321SMat Martineau void tcp_push(struct sock *sk, int flags, int mss_now, int nonagle,
33635b2c321SMat Martineau 	      int size_goal);
3375c9f3023SJoe Perches void tcp_release_cb(struct sock *sk);
3385c9f3023SJoe Perches void tcp_wfree(struct sk_buff *skb);
3395c9f3023SJoe Perches void tcp_write_timer_handler(struct sock *sk);
3405c9f3023SJoe Perches void tcp_delack_timer_handler(struct sock *sk);
3415c9f3023SJoe Perches int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
34272ab4a86SEric Dumazet int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb);
3433d97d88eSYafang Shao void tcp_rcv_established(struct sock *sk, struct sk_buff *skb);
3445c9f3023SJoe Perches void tcp_rcv_space_adjust(struct sock *sk);
3455c9f3023SJoe Perches int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
3465c9f3023SJoe Perches void tcp_twsk_destructor(struct sock *sk);
3475c9f3023SJoe Perches ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
34853d3176bSChangli Gao 			struct pipe_inode_info *pipe, size_t len,
34953d3176bSChangli Gao 			unsigned int flags);
3509c55e01cSJens Axboe 
351a0496ef2SYuchung Cheng void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks);
352463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
353463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3541da177e4SLinus Torvalds {
355463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
356fc6415bcSDavid S. Miller 
357463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
358463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
359463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3601da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
361463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
362fc6415bcSDavid S. Miller 		} else
363463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
367bdf1ee5dSIlpo Järvinen #define	TCP_ECN_OK		1
368bdf1ee5dSIlpo Järvinen #define	TCP_ECN_QUEUE_CWR	2
369bdf1ee5dSIlpo Järvinen #define	TCP_ECN_DEMAND_CWR	4
3707a269ffaSEric Dumazet #define	TCP_ECN_SEEN		8
371bdf1ee5dSIlpo Järvinen 
372fd2c3ef7SEric Dumazet enum tcp_tw_status {
3731da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
3741da177e4SLinus Torvalds 	TCP_TW_RST = 1,
3751da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
3761da177e4SLinus Torvalds 	TCP_TW_SYN = 3
3771da177e4SLinus Torvalds };
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 
3805c9f3023SJoe Perches enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
3811da177e4SLinus Torvalds 					      struct sk_buff *skb,
3828feaf0c0SArnaldo Carvalho de Melo 					      const struct tcphdr *th);
3835c9f3023SJoe Perches struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
384e0f9759fSEric Dumazet 			   struct request_sock *req, bool fastopen,
385e0f9759fSEric Dumazet 			   bool *lost_race);
3865c9f3023SJoe Perches int tcp_child_process(struct sock *parent, struct sock *child,
3871da177e4SLinus Torvalds 		      struct sk_buff *skb);
3885ae344c9SNeal Cardwell void tcp_enter_loss(struct sock *sk);
38957dde7f7SYuchung Cheng void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int flag);
3905c9f3023SJoe Perches void tcp_clear_retrans(struct tcp_sock *tp);
3915c9f3023SJoe Perches void tcp_update_metrics(struct sock *sk);
3925c9f3023SJoe Perches void tcp_init_metrics(struct sock *sk);
3935c9f3023SJoe Perches void tcp_metrics_init(void);
394d82bae12SSoheil Hassas Yeganeh bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst);
3955c9f3023SJoe Perches void tcp_close(struct sock *sk, long timeout);
3965c9f3023SJoe Perches void tcp_init_sock(struct sock *sk);
39727204aaaSWei Wang void tcp_init_transfer(struct sock *sk, int bpf_op);
398a11e1d43SLinus Torvalds __poll_t tcp_poll(struct file *file, struct socket *sock,
399a11e1d43SLinus Torvalds 		      struct poll_table_struct *wait);
4005c9f3023SJoe Perches int tcp_getsockopt(struct sock *sk, int level, int optname,
4013fdadf7dSDmitry Mishin 		   char __user *optval, int __user *optlen);
4025c9f3023SJoe Perches int tcp_setsockopt(struct sock *sk, int level, int optname,
40353d3176bSChangli Gao 		   char __user *optval, unsigned int optlen);
4045c9f3023SJoe Perches int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
40553d3176bSChangli Gao 			  char __user *optval, int __user *optlen);
4065c9f3023SJoe Perches int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
407b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen);
4085c9f3023SJoe Perches void tcp_set_keepalive(struct sock *sk, int val);
40942cb80a2SEric Dumazet void tcp_syn_ack_timeout(const struct request_sock *req);
4101b784140SYing Xue int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
4111b784140SYing Xue 		int flags, int *addr_len);
412d1361840SEric Dumazet int tcp_set_rcvlowat(struct sock *sk, int val);
41303f45c88SEric Dumazet void tcp_data_ready(struct sock *sk);
414340a6f3dSYafang Shao #ifdef CONFIG_MMU
41593ab6cc6SEric Dumazet int tcp_mmap(struct file *file, struct socket *sock,
41693ab6cc6SEric Dumazet 	     struct vm_area_struct *vma);
417340a6f3dSYafang Shao #endif
418eed29f17SEric Dumazet void tcp_parse_options(const struct net *net, const struct sk_buff *skb,
4191a2c6181SChristoph Paasch 		       struct tcp_options_received *opt_rx,
4202100c8d2SYuchung Cheng 		       int estab, struct tcp_fastopen_cookie *foc);
4215c9f3023SJoe Perches const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
4227d5d5525SYOSHIFUJI Hideaki 
4231da177e4SLinus Torvalds /*
4249349d600SPetar Penkov  *	BPF SKB-less helpers
4259349d600SPetar Penkov  */
4269349d600SPetar Penkov u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph,
4279349d600SPetar Penkov 			 struct tcphdr *th, u32 *cookie);
4289349d600SPetar Penkov u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph,
4299349d600SPetar Penkov 			 struct tcphdr *th, u32 *cookie);
4309349d600SPetar Penkov u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
4319349d600SPetar Penkov 			  const struct tcp_request_sock_ops *af_ops,
4329349d600SPetar Penkov 			  struct sock *sk, struct tcphdr *th);
4339349d600SPetar Penkov /*
4341da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4351da177e4SLinus Torvalds  */
4361da177e4SLinus Torvalds 
4375c9f3023SJoe Perches void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
4384fab9071SNeal Cardwell void tcp_v4_mtu_reduced(struct sock *sk);
4399cf74903SEric Dumazet void tcp_req_err(struct sock *sk, u32 seq, bool abort);
440d2924569SEric Dumazet void tcp_ld_RTO_revert(struct sock *sk, u32 seq);
4415c9f3023SJoe Perches int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
442c28c6f04SEric Dumazet struct sock *tcp_create_openreq_child(const struct sock *sk,
44360236fddSArnaldo Carvalho de Melo 				      struct request_sock *req,
4441da177e4SLinus Torvalds 				      struct sk_buff *skb);
44581164413SDaniel Borkmann void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst);
4460c27171eSEric Dumazet struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
44760236fddSArnaldo Carvalho de Melo 				  struct request_sock *req,
4485e0724d0SEric Dumazet 				  struct dst_entry *dst,
4495e0724d0SEric Dumazet 				  struct request_sock *req_unhash,
4505e0724d0SEric Dumazet 				  bool *own_req);
4515c9f3023SJoe Perches int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
4525c9f3023SJoe Perches int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
4535c9f3023SJoe Perches int tcp_connect(struct sock *sk);
454b3d05147SEric Dumazet enum tcp_synack_type {
455b3d05147SEric Dumazet 	TCP_SYNACK_NORMAL,
456b3d05147SEric Dumazet 	TCP_SYNACK_FASTOPEN,
457b3d05147SEric Dumazet 	TCP_SYNACK_COOKIE,
458b3d05147SEric Dumazet };
4595d062de7SEric Dumazet struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
460e6b4d113SWilliam Allen Simpson 				struct request_sock *req,
461ca6fb065SEric Dumazet 				struct tcp_fastopen_cookie *foc,
462b3d05147SEric Dumazet 				enum tcp_synack_type synack_type);
4635c9f3023SJoe Perches int tcp_disconnect(struct sock *sk, int flags);
4641da177e4SLinus Torvalds 
465370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
466292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
46763d02d15SEric Dumazet void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /* From syncookies.c */
470b80c0e78SEric Dumazet struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
471b80c0e78SEric Dumazet 				 struct request_sock *req,
47284b114b9SEric Dumazet 				 struct dst_entry *dst, u32 tsoff);
4735c9f3023SJoe Perches int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
4740198230bSPatrick McHardy 		      u32 cookie);
475461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
476e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
4778c27bd75SFlorian Westphal 
47863262315SEric Dumazet /* Syncookies use a monotonic timer which increments every 60 seconds.
4798c27bd75SFlorian Westphal  * This counter is used both as a hash input and partially encoded into
4808c27bd75SFlorian Westphal  * the cookie value.  A cookie is only validated further if the delta
4818c27bd75SFlorian Westphal  * between the current counter value and the encoded one is less than this,
48263262315SEric Dumazet  * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
4838c27bd75SFlorian Westphal  * the counter advances immediately after a cookie is generated).
4848c27bd75SFlorian Westphal  */
4858c27bd75SFlorian Westphal #define MAX_SYNCOOKIE_AGE	2
486264ea103SEric Dumazet #define TCP_SYNCOOKIE_PERIOD	(60 * HZ)
487264ea103SEric Dumazet #define TCP_SYNCOOKIE_VALID	(MAX_SYNCOOKIE_AGE * TCP_SYNCOOKIE_PERIOD)
488264ea103SEric Dumazet 
489264ea103SEric Dumazet /* syncookies: remember time of last synqueue overflow
490264ea103SEric Dumazet  * But do not dirty this field too often (once per second is enough)
4913f684b4bSEric Dumazet  * It is racy as we do not hold a lock, but race is very minor.
492264ea103SEric Dumazet  */
4933f684b4bSEric Dumazet static inline void tcp_synq_overflow(const struct sock *sk)
494264ea103SEric Dumazet {
49540a1227eSMartin KaFai Lau 	unsigned int last_overflow;
496cca9bab1SArnd Bergmann 	unsigned int now = jiffies;
497264ea103SEric Dumazet 
49840a1227eSMartin KaFai Lau 	if (sk->sk_reuseport) {
49940a1227eSMartin KaFai Lau 		struct sock_reuseport *reuse;
50040a1227eSMartin KaFai Lau 
50140a1227eSMartin KaFai Lau 		reuse = rcu_dereference(sk->sk_reuseport_cb);
50240a1227eSMartin KaFai Lau 		if (likely(reuse)) {
50340a1227eSMartin KaFai Lau 			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
50404d26e7bSGuillaume Nault 			if (!time_between32(now, last_overflow,
50504d26e7bSGuillaume Nault 					    last_overflow + HZ))
50640a1227eSMartin KaFai Lau 				WRITE_ONCE(reuse->synq_overflow_ts, now);
50740a1227eSMartin KaFai Lau 			return;
50840a1227eSMartin KaFai Lau 		}
50940a1227eSMartin KaFai Lau 	}
51040a1227eSMartin KaFai Lau 
511721c8dafSGuillaume Nault 	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
51204d26e7bSGuillaume Nault 	if (!time_between32(now, last_overflow, last_overflow + HZ))
513721c8dafSGuillaume Nault 		WRITE_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp, now);
514264ea103SEric Dumazet }
515264ea103SEric Dumazet 
516264ea103SEric Dumazet /* syncookies: no recent synqueue overflow on this listening socket? */
517264ea103SEric Dumazet static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
518264ea103SEric Dumazet {
51940a1227eSMartin KaFai Lau 	unsigned int last_overflow;
520cca9bab1SArnd Bergmann 	unsigned int now = jiffies;
521264ea103SEric Dumazet 
52240a1227eSMartin KaFai Lau 	if (sk->sk_reuseport) {
52340a1227eSMartin KaFai Lau 		struct sock_reuseport *reuse;
52440a1227eSMartin KaFai Lau 
52540a1227eSMartin KaFai Lau 		reuse = rcu_dereference(sk->sk_reuseport_cb);
52640a1227eSMartin KaFai Lau 		if (likely(reuse)) {
52740a1227eSMartin KaFai Lau 			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
528cb44a08fSGuillaume Nault 			return !time_between32(now, last_overflow - HZ,
529cb44a08fSGuillaume Nault 					       last_overflow +
53040a1227eSMartin KaFai Lau 					       TCP_SYNCOOKIE_VALID);
53140a1227eSMartin KaFai Lau 		}
53240a1227eSMartin KaFai Lau 	}
53340a1227eSMartin KaFai Lau 
534721c8dafSGuillaume Nault 	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
535cb44a08fSGuillaume Nault 
536cb44a08fSGuillaume Nault 	/* If last_overflow <= jiffies <= last_overflow + TCP_SYNCOOKIE_VALID,
537cb44a08fSGuillaume Nault 	 * then we're under synflood. However, we have to use
538cb44a08fSGuillaume Nault 	 * 'last_overflow - HZ' as lower bound. That's because a concurrent
539cb44a08fSGuillaume Nault 	 * tcp_synq_overflow() could update .ts_recent_stamp after we read
540cb44a08fSGuillaume Nault 	 * jiffies but before we store .ts_recent_stamp into last_overflow,
541cb44a08fSGuillaume Nault 	 * which could lead to rejecting a valid syncookie.
542cb44a08fSGuillaume Nault 	 */
543cb44a08fSGuillaume Nault 	return !time_between32(now, last_overflow - HZ,
544cb44a08fSGuillaume Nault 			       last_overflow + TCP_SYNCOOKIE_VALID);
545264ea103SEric Dumazet }
5468c27bd75SFlorian Westphal 
5478c27bd75SFlorian Westphal static inline u32 tcp_cookie_time(void)
5488c27bd75SFlorian Westphal {
54963262315SEric Dumazet 	u64 val = get_jiffies_64();
55063262315SEric Dumazet 
551264ea103SEric Dumazet 	do_div(val, TCP_SYNCOOKIE_PERIOD);
55263262315SEric Dumazet 	return val;
5538c27bd75SFlorian Westphal }
5548c27bd75SFlorian Westphal 
5555c9f3023SJoe Perches u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
5565c9f3023SJoe Perches 			      u16 *mssp);
5573f684b4bSEric Dumazet __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss);
558200ecef6SEric Dumazet u64 cookie_init_timestamp(struct request_sock *req, u64 now);
559f9301034SEric Dumazet bool cookie_timestamp_decode(const struct net *net,
560f9301034SEric Dumazet 			     struct tcp_options_received *opt);
561f1673381SFlorian Westphal bool cookie_ecn_ok(const struct tcp_options_received *opt,
562f7b3bec6SFlorian Westphal 		   const struct net *net, const struct dst_entry *dst);
5634dfc2817SFlorian Westphal 
564c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */
5655c9f3023SJoe Perches int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
56681eb6a14SPatrick McHardy 		      u32 cookie);
5675c9f3023SJoe Perches struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
568f1673381SFlorian Westphal 
5695c9f3023SJoe Perches u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
57081eb6a14SPatrick McHardy 			      const struct tcphdr *th, u16 *mssp);
5713f684b4bSEric Dumazet __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mss);
572e05c82d3SEric Dumazet #endif
5731da177e4SLinus Torvalds /* tcp_output.c */
5741da177e4SLinus Torvalds 
5755c9f3023SJoe Perches void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
5769e412ba7SIlpo Järvinen 			       int nonagle);
57710d3be56SEric Dumazet int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
57810d3be56SEric Dumazet int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
5795c9f3023SJoe Perches void tcp_retransmit_timer(struct sock *sk);
5805c9f3023SJoe Perches void tcp_xmit_retransmit_queue(struct sock *);
5815c9f3023SJoe Perches void tcp_simple_retransmit(struct sock *);
58257dde7f7SYuchung Cheng void tcp_enter_recovery(struct sock *sk, bool ece_ack);
5835c9f3023SJoe Perches int tcp_trim_head(struct sock *, struct sk_buff *, u32);
58475c119afSEric Dumazet enum tcp_queue {
58575c119afSEric Dumazet 	TCP_FRAG_IN_WRITE_QUEUE,
58675c119afSEric Dumazet 	TCP_FRAG_IN_RTX_QUEUE,
58775c119afSEric Dumazet };
58875c119afSEric Dumazet int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
58975c119afSEric Dumazet 		 struct sk_buff *skb, u32 len,
59075c119afSEric Dumazet 		 unsigned int mss_now, gfp_t gfp);
5911da177e4SLinus Torvalds 
5925c9f3023SJoe Perches void tcp_send_probe0(struct sock *);
5935c9f3023SJoe Perches void tcp_send_partial(struct sock *);
594e520af48SEric Dumazet int tcp_write_wakeup(struct sock *, int mib);
5955c9f3023SJoe Perches void tcp_send_fin(struct sock *sk);
5965c9f3023SJoe Perches void tcp_send_active_reset(struct sock *sk, gfp_t priority);
5975c9f3023SJoe Perches int tcp_send_synack(struct sock *);
5985c9f3023SJoe Perches void tcp_push_one(struct sock *, unsigned int mss_now);
59927cde44aSYuchung Cheng void __tcp_send_ack(struct sock *sk, u32 rcv_nxt);
6005c9f3023SJoe Perches void tcp_send_ack(struct sock *sk);
6015c9f3023SJoe Perches void tcp_send_delayed_ack(struct sock *sk);
6025c9f3023SJoe Perches void tcp_send_loss_probe(struct sock *sk);
603ed66dfafSNeal Cardwell bool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto);
604cfea5a68SMartin KaFai Lau void tcp_skb_collapse_tstamp(struct sk_buff *skb,
605cfea5a68SMartin KaFai Lau 			     const struct sk_buff *next_skb);
6061da177e4SLinus Torvalds 
607a762a980SDavid S. Miller /* tcp_input.c */
6085c9f3023SJoe Perches void tcp_rearm_rto(struct sock *sk);
6090f1c28aeSYuchung Cheng void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req);
6105c9f3023SJoe Perches void tcp_reset(struct sock *sk);
6114f41b1c5SYuchung Cheng void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, struct sk_buff *skb);
612e3e17b77SEric Dumazet void tcp_fin(struct sock *sk);
613a762a980SDavid S. Miller 
6141da177e4SLinus Torvalds /* tcp_timer.c */
6155c9f3023SJoe Perches void tcp_init_xmit_timers(struct sock *);
616463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
617463c84b9SArnaldo Carvalho de Melo {
61873a6bab5SEric Dumazet 	if (hrtimer_try_to_cancel(&tcp_sk(sk)->pacing_timer) == 1)
619cf0dd203SEric Dumazet 		__sock_put(sk);
62073a6bab5SEric Dumazet 
6215d9f4262SEric Dumazet 	if (hrtimer_try_to_cancel(&tcp_sk(sk)->compressed_ack_timer) == 1)
6225d9f4262SEric Dumazet 		__sock_put(sk);
6235d9f4262SEric Dumazet 
624463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
625463c84b9SArnaldo Carvalho de Melo }
6261da177e4SLinus Torvalds 
6275c9f3023SJoe Perches unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
6285c9f3023SJoe Perches unsigned int tcp_current_mss(struct sock *sk);
6290c54b85fSIlpo Järvinen 
6300c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */
6310c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
6320c54b85fSIlpo Järvinen {
63301f83d69SAlexey Kuznetsov 	int cutoff;
63401f83d69SAlexey Kuznetsov 
63501f83d69SAlexey Kuznetsov 	/* When peer uses tiny windows, there is no use in packetizing
63601f83d69SAlexey Kuznetsov 	 * to sub-MSS pieces for the sake of SWS or making sure there
63701f83d69SAlexey Kuznetsov 	 * are enough packets in the pipe for fast recovery.
63801f83d69SAlexey Kuznetsov 	 *
63901f83d69SAlexey Kuznetsov 	 * On the other hand, for extremely large MSS devices, handling
64001f83d69SAlexey Kuznetsov 	 * smaller than MSS windows in this way does make sense.
64101f83d69SAlexey Kuznetsov 	 */
6422631b79fSSeymour, Shane M 	if (tp->max_window > TCP_MSS_DEFAULT)
64301f83d69SAlexey Kuznetsov 		cutoff = (tp->max_window >> 1);
64401f83d69SAlexey Kuznetsov 	else
64501f83d69SAlexey Kuznetsov 		cutoff = tp->max_window;
64601f83d69SAlexey Kuznetsov 
64701f83d69SAlexey Kuznetsov 	if (cutoff && pktsize > cutoff)
64801f83d69SAlexey Kuznetsov 		return max_t(int, cutoff, 68U - tp->tcp_header_len);
6490c54b85fSIlpo Järvinen 	else
6500c54b85fSIlpo Järvinen 		return pktsize;
6510c54b85fSIlpo Järvinen }
6521da177e4SLinus Torvalds 
65317b085eaSArnaldo Carvalho de Melo /* tcp.c */
6540df48c26SEric Dumazet void tcp_get_info(struct sock *, struct tcp_info *);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
6575c9f3023SJoe Perches int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
6581da177e4SLinus Torvalds 		  sk_read_actor_t recv_actor);
6591da177e4SLinus Torvalds 
6605c9f3023SJoe Perches void tcp_initialize_rcv_mss(struct sock *sk);
6611da177e4SLinus Torvalds 
6625c9f3023SJoe Perches int tcp_mtu_to_mss(struct sock *sk, int pmtu);
6635c9f3023SJoe Perches int tcp_mss_to_mtu(struct sock *sk, int mss);
6645c9f3023SJoe Perches void tcp_mtup_init(struct sock *sk);
6655d424d5aSJohn Heffner 
666f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk)
667f1ecd5d9SDamian Lukowski {
668f1ecd5d9SDamian Lukowski 	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
669f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
670f1ecd5d9SDamian Lukowski }
671f1ecd5d9SDamian Lukowski 
672f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
673f1ecd5d9SDamian Lukowski {
674740b0f18SEric Dumazet 	return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
675f1ecd5d9SDamian Lukowski }
676f1ecd5d9SDamian Lukowski 
67731770e34SFlorian Westphal static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
67831770e34SFlorian Westphal {
67931770e34SFlorian Westphal 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
68031770e34SFlorian Westphal 			       ntohl(TCP_FLAG_ACK) |
68131770e34SFlorian Westphal 			       snd_wnd);
68231770e34SFlorian Westphal }
68331770e34SFlorian Westphal 
68431770e34SFlorian Westphal static inline void tcp_fast_path_on(struct tcp_sock *tp)
68531770e34SFlorian Westphal {
68631770e34SFlorian Westphal 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
68731770e34SFlorian Westphal }
68831770e34SFlorian Westphal 
68931770e34SFlorian Westphal static inline void tcp_fast_path_check(struct sock *sk)
69031770e34SFlorian Westphal {
69131770e34SFlorian Westphal 	struct tcp_sock *tp = tcp_sk(sk);
69231770e34SFlorian Westphal 
69331770e34SFlorian Westphal 	if (RB_EMPTY_ROOT(&tp->out_of_order_queue) &&
69431770e34SFlorian Westphal 	    tp->rcv_wnd &&
69531770e34SFlorian Westphal 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
69631770e34SFlorian Westphal 	    !tp->urg_data)
69731770e34SFlorian Westphal 		tcp_fast_path_on(tp);
69831770e34SFlorian Westphal }
69931770e34SFlorian Westphal 
7000c266898SSatoru SATOH /* Compute the actual rto_min value */
7010c266898SSatoru SATOH static inline u32 tcp_rto_min(struct sock *sk)
7020c266898SSatoru SATOH {
703cf533ea5SEric Dumazet 	const struct dst_entry *dst = __sk_dst_get(sk);
7040c266898SSatoru SATOH 	u32 rto_min = TCP_RTO_MIN;
7050c266898SSatoru SATOH 
7060c266898SSatoru SATOH 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
7070c266898SSatoru SATOH 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
7080c266898SSatoru SATOH 	return rto_min;
7090c266898SSatoru SATOH }
7100c266898SSatoru SATOH 
711740b0f18SEric Dumazet static inline u32 tcp_rto_min_us(struct sock *sk)
712740b0f18SEric Dumazet {
713740b0f18SEric Dumazet 	return jiffies_to_usecs(tcp_rto_min(sk));
714740b0f18SEric Dumazet }
715740b0f18SEric Dumazet 
71681164413SDaniel Borkmann static inline bool tcp_ca_dst_locked(const struct dst_entry *dst)
71781164413SDaniel Borkmann {
71881164413SDaniel Borkmann 	return dst_metric_locked(dst, RTAX_CC_ALGO);
71981164413SDaniel Borkmann }
72081164413SDaniel Borkmann 
721f6722583SYuchung Cheng /* Minimum RTT in usec. ~0 means not available. */
722f6722583SYuchung Cheng static inline u32 tcp_min_rtt(const struct tcp_sock *tp)
723f6722583SYuchung Cheng {
72464033892SNeal Cardwell 	return minmax_get(&tp->rtt_min);
725f6722583SYuchung Cheng }
726f6722583SYuchung Cheng 
7271da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
7281da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
7291da177e4SLinus Torvalds  * than the offered window.
7301da177e4SLinus Torvalds  */
73140efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
7321da177e4SLinus Torvalds {
7331da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	if (win < 0)
7361da177e4SLinus Torvalds 		win = 0;
7371da177e4SLinus Torvalds 	return (u32) win;
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
7411da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
7421da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
7431da177e4SLinus Torvalds  */
7445c9f3023SJoe Perches u32 __tcp_select_window(struct sock *sk);
7451da177e4SLinus Torvalds 
746ee995283SPavel Emelyanov void tcp_send_window_probe(struct sock *sk);
747ee995283SPavel Emelyanov 
748ec66eda8SEric Dumazet /* TCP uses 32bit jiffies to save some space.
749ec66eda8SEric Dumazet  * Note that this is different from tcp_time_stamp, which
750ec66eda8SEric Dumazet  * historically has been the same until linux-4.13.
751ec66eda8SEric Dumazet  */
752ec66eda8SEric Dumazet #define tcp_jiffies32 ((u32)jiffies)
753ec66eda8SEric Dumazet 
7549a568de4SEric Dumazet /*
7559a568de4SEric Dumazet  * Deliver a 32bit value for TCP timestamp option (RFC 7323)
7569a568de4SEric Dumazet  * It is no longer tied to jiffies, but to 1 ms clock.
7579a568de4SEric Dumazet  * Note: double check if you want to use tcp_jiffies32 instead of this.
7581da177e4SLinus Torvalds  */
7599a568de4SEric Dumazet #define TCP_TS_HZ	1000
7609a568de4SEric Dumazet 
7619a568de4SEric Dumazet static inline u64 tcp_clock_ns(void)
7629a568de4SEric Dumazet {
763fb420d5dSEric Dumazet 	return ktime_get_ns();
7649a568de4SEric Dumazet }
7659a568de4SEric Dumazet 
7669a568de4SEric Dumazet static inline u64 tcp_clock_us(void)
7679a568de4SEric Dumazet {
7689a568de4SEric Dumazet 	return div_u64(tcp_clock_ns(), NSEC_PER_USEC);
7699a568de4SEric Dumazet }
7709a568de4SEric Dumazet 
7719a568de4SEric Dumazet /* This should only be used in contexts where tp->tcp_mstamp is up to date */
7729a568de4SEric Dumazet static inline u32 tcp_time_stamp(const struct tcp_sock *tp)
7739a568de4SEric Dumazet {
7749a568de4SEric Dumazet 	return div_u64(tp->tcp_mstamp, USEC_PER_SEC / TCP_TS_HZ);
7759a568de4SEric Dumazet }
7769a568de4SEric Dumazet 
777200ecef6SEric Dumazet /* Convert a nsec timestamp into TCP TSval timestamp (ms based currently) */
778200ecef6SEric Dumazet static inline u32 tcp_ns_to_ts(u64 ns)
779200ecef6SEric Dumazet {
780200ecef6SEric Dumazet 	return div_u64(ns, NSEC_PER_SEC / TCP_TS_HZ);
781200ecef6SEric Dumazet }
782200ecef6SEric Dumazet 
7839a568de4SEric Dumazet /* Could use tcp_clock_us() / 1000, but this version uses a single divide */
7849a568de4SEric Dumazet static inline u32 tcp_time_stamp_raw(void)
7859a568de4SEric Dumazet {
786200ecef6SEric Dumazet 	return tcp_ns_to_ts(tcp_clock_ns());
7879a568de4SEric Dumazet }
7889a568de4SEric Dumazet 
7899799ccb0SEric Dumazet void tcp_mstamp_refresh(struct tcp_sock *tp);
7909a568de4SEric Dumazet 
7919a568de4SEric Dumazet static inline u32 tcp_stamp_us_delta(u64 t1, u64 t0)
7929a568de4SEric Dumazet {
7939a568de4SEric Dumazet 	return max_t(s64, t1 - t0, 0);
7949a568de4SEric Dumazet }
7951da177e4SLinus Torvalds 
7967faee5c0SEric Dumazet static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
7977faee5c0SEric Dumazet {
798200ecef6SEric Dumazet 	return tcp_ns_to_ts(skb->skb_mstamp_ns);
7997faee5c0SEric Dumazet }
8007faee5c0SEric Dumazet 
8012fd66ffbSEric Dumazet /* provide the departure time in us unit */
8022fd66ffbSEric Dumazet static inline u64 tcp_skb_timestamp_us(const struct sk_buff *skb)
8032fd66ffbSEric Dumazet {
804d3edd06eSEric Dumazet 	return div_u64(skb->skb_mstamp_ns, NSEC_PER_USEC);
8052fd66ffbSEric Dumazet }
8062fd66ffbSEric Dumazet 
8077faee5c0SEric Dumazet 
808a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
809a3433f35SChangli Gao 
810a3433f35SChangli Gao #define TCPHDR_FIN 0x01
811a3433f35SChangli Gao #define TCPHDR_SYN 0x02
812a3433f35SChangli Gao #define TCPHDR_RST 0x04
813a3433f35SChangli Gao #define TCPHDR_PSH 0x08
814a3433f35SChangli Gao #define TCPHDR_ACK 0x10
815a3433f35SChangli Gao #define TCPHDR_URG 0x20
816a3433f35SChangli Gao #define TCPHDR_ECE 0x40
817a3433f35SChangli Gao #define TCPHDR_CWR 0x80
818a3433f35SChangli Gao 
81949213555SDaniel Borkmann #define TCPHDR_SYN_ECN	(TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)
82049213555SDaniel Borkmann 
821caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
822f86586faSEric Dumazet  * TCP per-packet control information to the transmission code.
823f86586faSEric Dumazet  * We also store the host-order sequence numbers in here too.
824f86586faSEric Dumazet  * This is 44 bytes if IPV6 is enabled.
825f86586faSEric Dumazet  * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
8261da177e4SLinus Torvalds  */
8271da177e4SLinus Torvalds struct tcp_skb_cb {
8281da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
8291da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
830cd7d8498SEric Dumazet 	union {
831cd7d8498SEric Dumazet 		/* Note : tcp_tw_isn is used in input path only
832cd7d8498SEric Dumazet 		 *	  (isn chosen by tcp_timewait_state_process())
833cd7d8498SEric Dumazet 		 *
834f69ad292SEric Dumazet 		 * 	  tcp_gso_segs/size are used in write queue only,
835f69ad292SEric Dumazet 		 *	  cf tcp_skb_pcount()/tcp_skb_mss()
836cd7d8498SEric Dumazet 		 */
837cd7d8498SEric Dumazet 		__u32		tcp_tw_isn;
838f69ad292SEric Dumazet 		struct {
839f69ad292SEric Dumazet 			u16	tcp_gso_segs;
840f69ad292SEric Dumazet 			u16	tcp_gso_size;
841f69ad292SEric Dumazet 		};
842cd7d8498SEric Dumazet 	};
8434de075e0SEric Dumazet 	__u8		tcp_flags;	/* TCP header flags. (tcp[13])	*/
844f4f9f6e7SNeal Cardwell 
845713bafeaSYuchung Cheng 	__u8		sacked;		/* State flags for SACK.	*/
8461da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
8471da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
8481da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
8491da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
850d3edd06eSEric Dumazet #define TCPCB_REPAIRED		0x10	/* SKB repaired (no skb_mstamp_ns)	*/
8511da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
8529d186cacSAndrey Vagin #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
8539d186cacSAndrey Vagin 				TCPCB_REPAIRED)
8541da177e4SLinus Torvalds 
855f4f9f6e7SNeal Cardwell 	__u8		ip_dsfield;	/* IPv4 tos or IPv6 dsfield	*/
8566b084928SSoheil Hassas Yeganeh 	__u8		txstamp_ack:1,	/* Record TX timestamp for ack? */
857c134ecb8SMartin KaFai Lau 			eor:1,		/* Is skb MSG_EOR marked? */
85898aaa913SMike Maloney 			has_rxtstamp:1,	/* SKB has a RX timestamp	*/
85998aaa913SMike Maloney 			unused:5;
8601da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
861971f10ecSEric Dumazet 	union {
862b75803d5SLawrence Brakmo 		struct {
863b9f64820SYuchung Cheng 			/* There is space for up to 24 bytes */
864d7722e85SSoheil Hassas Yeganeh 			__u32 in_flight:30,/* Bytes in flight at transmit */
865d7722e85SSoheil Hassas Yeganeh 			      is_app_limited:1, /* cwnd not fully used? */
866d7722e85SSoheil Hassas Yeganeh 			      unused:1;
867b9f64820SYuchung Cheng 			/* pkts S/ACKed so far upon tx of skb, incl retrans: */
868b9f64820SYuchung Cheng 			__u32 delivered;
869b9f64820SYuchung Cheng 			/* start of send pipeline phase */
8709a568de4SEric Dumazet 			u64 first_tx_mstamp;
871b9f64820SYuchung Cheng 			/* when we reached the "delivered" count */
8729a568de4SEric Dumazet 			u64 delivered_mstamp;
873b75803d5SLawrence Brakmo 		} tx;   /* only used for outgoing skbs */
874b75803d5SLawrence Brakmo 		union {
875971f10ecSEric Dumazet 			struct inet_skb_parm	h4;
876971f10ecSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
877971f10ecSEric Dumazet 			struct inet6_skb_parm	h6;
878971f10ecSEric Dumazet #endif
879b75803d5SLawrence Brakmo 		} header;	/* For incoming skbs */
88034f79502SJohn Fastabend 		struct {
88134f79502SJohn Fastabend 			__u32 flags;
882e5cd3abcSJohn Fastabend 			struct sock *sk_redir;
8838108a775SJohn Fastabend 			void *data_end;
88434f79502SJohn Fastabend 		} bpf;
885b75803d5SLawrence Brakmo 	};
8861da177e4SLinus Torvalds };
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
8891da177e4SLinus Torvalds 
8900ea488ffSJohn Fastabend static inline void bpf_compute_data_end_sk_skb(struct sk_buff *skb)
8910ea488ffSJohn Fastabend {
8920ea488ffSJohn Fastabend 	TCP_SKB_CB(skb)->bpf.data_end = skb->data + skb_headlen(skb);
8930ea488ffSJohn Fastabend }
894870c3151SEric Dumazet 
895604326b4SDaniel Borkmann static inline bool tcp_skb_bpf_ingress(const struct sk_buff *skb)
896604326b4SDaniel Borkmann {
897604326b4SDaniel Borkmann 	return TCP_SKB_CB(skb)->bpf.flags & BPF_F_INGRESS;
898604326b4SDaniel Borkmann }
899604326b4SDaniel Borkmann 
900604326b4SDaniel Borkmann static inline struct sock *tcp_skb_bpf_redirect_fetch(struct sk_buff *skb)
901604326b4SDaniel Borkmann {
902604326b4SDaniel Borkmann 	return TCP_SKB_CB(skb)->bpf.sk_redir;
903604326b4SDaniel Borkmann }
904604326b4SDaniel Borkmann 
905604326b4SDaniel Borkmann static inline void tcp_skb_bpf_redirect_clear(struct sk_buff *skb)
906604326b4SDaniel Borkmann {
907604326b4SDaniel Borkmann 	TCP_SKB_CB(skb)->bpf.sk_redir = NULL;
908604326b4SDaniel Borkmann }
909604326b4SDaniel Borkmann 
910815afe17SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
911870c3151SEric Dumazet /* This is the variant of inet6_iif() that must be used by TCP,
912870c3151SEric Dumazet  * as TCP moves IP6CB into a different location in skb->cb[]
913870c3151SEric Dumazet  */
914870c3151SEric Dumazet static inline int tcp_v6_iif(const struct sk_buff *skb)
915870c3151SEric Dumazet {
91624b711edSDavid Ahern 	return TCP_SKB_CB(skb)->header.h6.iif;
91724b711edSDavid Ahern }
91824b711edSDavid Ahern 
91924b711edSDavid Ahern static inline int tcp_v6_iif_l3_slave(const struct sk_buff *skb)
92024b711edSDavid Ahern {
921a04a480dSDavid Ahern 	bool l3_slave = ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags);
92274b20582SDavid Ahern 
92374b20582SDavid Ahern 	return l3_slave ? skb->skb_iif : TCP_SKB_CB(skb)->header.h6.iif;
924870c3151SEric Dumazet }
9254297a0efSDavid Ahern 
9264297a0efSDavid Ahern /* TCP_SKB_CB reference means this can not be used from early demux */
9274297a0efSDavid Ahern static inline int tcp_v6_sdif(const struct sk_buff *skb)
9284297a0efSDavid Ahern {
9294297a0efSDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9304297a0efSDavid Ahern 	if (skb && ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags))
9314297a0efSDavid Ahern 		return TCP_SKB_CB(skb)->header.h6.iif;
9324297a0efSDavid Ahern #endif
9334297a0efSDavid Ahern 	return 0;
9344297a0efSDavid Ahern }
935815afe17SEric Dumazet #endif
936870c3151SEric Dumazet 
937a04a480dSDavid Ahern static inline bool inet_exact_dif_match(struct net *net, struct sk_buff *skb)
938a04a480dSDavid Ahern {
939a04a480dSDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
940a04a480dSDavid Ahern 	if (!net->ipv4.sysctl_tcp_l3mdev_accept &&
941b4d1605aSDavid Ahern 	    skb && ipv4_l3mdev_skb(IPCB(skb)->flags))
942a04a480dSDavid Ahern 		return true;
943a04a480dSDavid Ahern #endif
944a04a480dSDavid Ahern 	return false;
945a04a480dSDavid Ahern }
946a04a480dSDavid Ahern 
9473fa6f616SDavid Ahern /* TCP_SKB_CB reference means this can not be used from early demux */
9483fa6f616SDavid Ahern static inline int tcp_v4_sdif(struct sk_buff *skb)
9493fa6f616SDavid Ahern {
9503fa6f616SDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9513fa6f616SDavid Ahern 	if (skb && ipv4_l3mdev_skb(TCP_SKB_CB(skb)->header.h4.flags))
9523fa6f616SDavid Ahern 		return TCP_SKB_CB(skb)->header.h4.iif;
9533fa6f616SDavid Ahern #endif
9543fa6f616SDavid Ahern 	return 0;
9553fa6f616SDavid Ahern }
9563fa6f616SDavid Ahern 
9571da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
9581da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
9591da177e4SLinus Torvalds  */
9601da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
9611da177e4SLinus Torvalds {
962cd7d8498SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_segs;
963cd7d8498SEric Dumazet }
964cd7d8498SEric Dumazet 
965cd7d8498SEric Dumazet static inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs)
966cd7d8498SEric Dumazet {
967cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs = segs;
968cd7d8498SEric Dumazet }
969cd7d8498SEric Dumazet 
970cd7d8498SEric Dumazet static inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs)
971cd7d8498SEric Dumazet {
972cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs += segs;
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
975f69ad292SEric Dumazet /* This is valid iff skb is in write queue and tcp_skb_pcount() > 1. */
9761da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
9771da177e4SLinus Torvalds {
978f69ad292SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_size;
9791da177e4SLinus Torvalds }
9801da177e4SLinus Torvalds 
981c134ecb8SMartin KaFai Lau static inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb)
982c134ecb8SMartin KaFai Lau {
983c134ecb8SMartin KaFai Lau 	return likely(!TCP_SKB_CB(skb)->eor);
984c134ecb8SMartin KaFai Lau }
985c134ecb8SMartin KaFai Lau 
98685712484SMat Martineau static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
98785712484SMat Martineau 					const struct sk_buff *from)
98885712484SMat Martineau {
98985712484SMat Martineau 	return likely(tcp_skb_can_collapse_to(to) &&
99085712484SMat Martineau 		      mptcp_skb_can_collapse(to, from));
99185712484SMat Martineau }
99285712484SMat Martineau 
993317a76f9SStephen Hemminger /* Events passed to congestion control interface */
994317a76f9SStephen Hemminger enum tcp_ca_event {
995317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
996317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
997317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
998317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
9999890092eSFlorian Westphal 	CA_EVENT_ECN_NO_CE,	/* ECT set, but not CE marked */
10009890092eSFlorian Westphal 	CA_EVENT_ECN_IS_CE,	/* received CE marked IP packet */
10017354c8c3SFlorian Westphal };
10027354c8c3SFlorian Westphal 
10039890092eSFlorian Westphal /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
10047354c8c3SFlorian Westphal enum tcp_ca_ack_event_flags {
1005c1d2b4c3SFlorian Westphal 	CA_ACK_SLOWPATH		= (1 << 0),	/* In slow path processing */
1006c1d2b4c3SFlorian Westphal 	CA_ACK_WIN_UPDATE	= (1 << 1),	/* ACK updated window */
1007c1d2b4c3SFlorian Westphal 	CA_ACK_ECE		= (1 << 2),	/* ECE bit is set on ack */
1008317a76f9SStephen Hemminger };
1009317a76f9SStephen Hemminger 
1010317a76f9SStephen Hemminger /*
1011317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
1012317a76f9SStephen Hemminger  */
1013317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
10143ff825b2SStephen Hemminger #define TCP_CA_MAX	128
10153ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
10163ff825b2SStephen Hemminger 
1017c5c6a8abSDaniel Borkmann #define TCP_CA_UNSPEC	0
1018c5c6a8abSDaniel Borkmann 
101930e502a3SDaniel Borkmann /* Algorithm can be set on socket without CAP_NET_ADMIN privileges */
1020164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1
102130e502a3SDaniel Borkmann /* Requires ECN/ECT set on all packets */
102230e502a3SDaniel Borkmann #define TCP_CONG_NEEDS_ECN	0x2
10230baf26b0SMartin KaFai Lau #define TCP_CONG_MASK	(TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN)
1024164891aaSStephen Hemminger 
102564f40ff5SEric Dumazet union tcp_cc_info;
102664f40ff5SEric Dumazet 
1027756ee172SLawrence Brakmo struct ack_sample {
1028756ee172SLawrence Brakmo 	u32 pkts_acked;
1029756ee172SLawrence Brakmo 	s32 rtt_us;
10306f094b9eSLawrence Brakmo 	u32 in_flight;
1031756ee172SLawrence Brakmo };
1032756ee172SLawrence Brakmo 
1033b9f64820SYuchung Cheng /* A rate sample measures the number of (original/retransmitted) data
1034b9f64820SYuchung Cheng  * packets delivered "delivered" over an interval of time "interval_us".
1035b9f64820SYuchung Cheng  * The tcp_rate.c code fills in the rate sample, and congestion
1036b9f64820SYuchung Cheng  * control modules that define a cong_control function to run at the end
1037b9f64820SYuchung Cheng  * of ACK processing can optionally chose to consult this sample when
1038b9f64820SYuchung Cheng  * setting cwnd and pacing rate.
1039b9f64820SYuchung Cheng  * A sample is invalid if "delivered" or "interval_us" is negative.
1040b9f64820SYuchung Cheng  */
1041b9f64820SYuchung Cheng struct rate_sample {
10429a568de4SEric Dumazet 	u64  prior_mstamp; /* starting timestamp for interval */
1043b9f64820SYuchung Cheng 	u32  prior_delivered;	/* tp->delivered at "prior_mstamp" */
1044b9f64820SYuchung Cheng 	s32  delivered;		/* number of packets delivered over interval */
1045b9f64820SYuchung Cheng 	long interval_us;	/* time for tp->delivered to incr "delivered" */
10464929c942SDeepti Raghavan 	u32 snd_interval_us;	/* snd interval for delivered packets */
10474929c942SDeepti Raghavan 	u32 rcv_interval_us;	/* rcv interval for delivered packets */
1048b9f64820SYuchung Cheng 	long rtt_us;		/* RTT of last (S)ACKed packet (or -1) */
1049b9f64820SYuchung Cheng 	int  losses;		/* number of packets marked lost upon ACK */
1050b9f64820SYuchung Cheng 	u32  acked_sacked;	/* number of packets newly (S)ACKed upon ACK */
1051b9f64820SYuchung Cheng 	u32  prior_in_flight;	/* in flight before this ACK */
1052d7722e85SSoheil Hassas Yeganeh 	bool is_app_limited;	/* is sample from packet with bubble in pipe? */
1053b9f64820SYuchung Cheng 	bool is_retrans;	/* is sample from retransmission? */
1054e4286603SYuchung Cheng 	bool is_ack_delayed;	/* is this (likely) a delayed ACK? */
1055b9f64820SYuchung Cheng };
1056b9f64820SYuchung Cheng 
1057317a76f9SStephen Hemminger struct tcp_congestion_ops {
1058317a76f9SStephen Hemminger 	struct list_head	list;
1059c5c6a8abSDaniel Borkmann 	u32 key;
1060c5c6a8abSDaniel Borkmann 	u32 flags;
1061317a76f9SStephen Hemminger 
1062317a76f9SStephen Hemminger 	/* initialize private data (optional) */
10636687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
1064317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
10656687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
1066317a76f9SStephen Hemminger 
1067317a76f9SStephen Hemminger 	/* return slow start threshold (required) */
10686687e988SArnaldo Carvalho de Melo 	u32 (*ssthresh)(struct sock *sk);
1069317a76f9SStephen Hemminger 	/* do new cwnd calculation (required) */
107024901551SEric Dumazet 	void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked);
1071317a76f9SStephen Hemminger 	/* call before changing ca_state (optional) */
10726687e988SArnaldo Carvalho de Melo 	void (*set_state)(struct sock *sk, u8 new_state);
1073317a76f9SStephen Hemminger 	/* call when cwnd event occurs (optional) */
10746687e988SArnaldo Carvalho de Melo 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
10757354c8c3SFlorian Westphal 	/* call when ack arrives (optional) */
10767354c8c3SFlorian Westphal 	void (*in_ack_event)(struct sock *sk, u32 flags);
10771e0ce2a1SAnmol Sarma 	/* new value of cwnd after loss (required) */
10786687e988SArnaldo Carvalho de Melo 	u32  (*undo_cwnd)(struct sock *sk);
1079317a76f9SStephen Hemminger 	/* hook for packet ack accounting (optional) */
1080756ee172SLawrence Brakmo 	void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample);
1081dcb8c9b4SEric Dumazet 	/* override sysctl_tcp_min_tso_segs */
1082dcb8c9b4SEric Dumazet 	u32 (*min_tso_segs)(struct sock *sk);
108377bfc174SYuchung Cheng 	/* returns the multiplier used in tcp_sndbuf_expand (optional) */
108477bfc174SYuchung Cheng 	u32 (*sndbuf_expand)(struct sock *sk);
1085c0402760SYuchung Cheng 	/* call when packets are delivered to update cwnd and pacing rate,
1086c0402760SYuchung Cheng 	 * after all the ca_state processing. (optional)
1087c0402760SYuchung Cheng 	 */
1088c0402760SYuchung Cheng 	void (*cong_control)(struct sock *sk, const struct rate_sample *rs);
108973c1f4a0SArnaldo Carvalho de Melo 	/* get info for inet_diag (optional) */
109064f40ff5SEric Dumazet 	size_t (*get_info)(struct sock *sk, u32 ext, int *attr,
109164f40ff5SEric Dumazet 			   union tcp_cc_info *info);
1092317a76f9SStephen Hemminger 
1093317a76f9SStephen Hemminger 	char 		name[TCP_CA_NAME_MAX];
1094317a76f9SStephen Hemminger 	struct module 	*owner;
1095317a76f9SStephen Hemminger };
1096317a76f9SStephen Hemminger 
10975c9f3023SJoe Perches int tcp_register_congestion_control(struct tcp_congestion_ops *type);
10985c9f3023SJoe Perches void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
1099317a76f9SStephen Hemminger 
110055d8694fSFlorian Westphal void tcp_assign_congestion_control(struct sock *sk);
11015c9f3023SJoe Perches void tcp_init_congestion_control(struct sock *sk);
11025c9f3023SJoe Perches void tcp_cleanup_congestion_control(struct sock *sk);
11036670e152SStephen Hemminger int tcp_set_default_congestion_control(struct net *net, const char *name);
11046670e152SStephen Hemminger void tcp_get_default_congestion_control(struct net *net, char *name);
11055c9f3023SJoe Perches void tcp_get_available_congestion_control(char *buf, size_t len);
11065c9f3023SJoe Perches void tcp_get_allowed_congestion_control(char *buf, size_t len);
11075c9f3023SJoe Perches int tcp_set_allowed_congestion_control(char *allowed);
11088d650cdeSEric Dumazet int tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
11098d650cdeSEric Dumazet 			       bool reinit, bool cap_net_admin);
1110e73ebb08SNeal Cardwell u32 tcp_slow_start(struct tcp_sock *tp, u32 acked);
1111e73ebb08SNeal Cardwell void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked);
1112317a76f9SStephen Hemminger 
11135c9f3023SJoe Perches u32 tcp_reno_ssthresh(struct sock *sk);
1114e9799183SFlorian Westphal u32 tcp_reno_undo_cwnd(struct sock *sk);
111524901551SEric Dumazet void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked);
1116a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
1117317a76f9SStephen Hemminger 
11180baf26b0SMartin KaFai Lau struct tcp_congestion_ops *tcp_ca_find(const char *name);
1119c5c6a8abSDaniel Borkmann struct tcp_congestion_ops *tcp_ca_find_key(u32 key);
11206670e152SStephen Hemminger u32 tcp_ca_get_key_by_name(struct net *net, const char *name, bool *ecn_ca);
1121ea697639SDaniel Borkmann #ifdef CONFIG_INET
1122c5c6a8abSDaniel Borkmann char *tcp_ca_get_name_by_key(u32 key, char *buffer);
1123ea697639SDaniel Borkmann #else
1124ea697639SDaniel Borkmann static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
1125ea697639SDaniel Borkmann {
1126ea697639SDaniel Borkmann 	return NULL;
1127ea697639SDaniel Borkmann }
1128ea697639SDaniel Borkmann #endif
1129c5c6a8abSDaniel Borkmann 
113030e502a3SDaniel Borkmann static inline bool tcp_ca_needs_ecn(const struct sock *sk)
113130e502a3SDaniel Borkmann {
113230e502a3SDaniel Borkmann 	const struct inet_connection_sock *icsk = inet_csk(sk);
113330e502a3SDaniel Borkmann 
113430e502a3SDaniel Borkmann 	return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN;
113530e502a3SDaniel Borkmann }
113630e502a3SDaniel Borkmann 
11376687e988SArnaldo Carvalho de Melo static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
1138317a76f9SStephen Hemminger {
11396687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
11406687e988SArnaldo Carvalho de Melo 
11416687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->set_state)
11426687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->set_state(sk, ca_state);
11436687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_state = ca_state;
1144317a76f9SStephen Hemminger }
1145317a76f9SStephen Hemminger 
11466687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
1147317a76f9SStephen Hemminger {
11486687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
11496687e988SArnaldo Carvalho de Melo 
11506687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
11516687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
1152317a76f9SStephen Hemminger }
1153317a76f9SStephen Hemminger 
1154b9f64820SYuchung Cheng /* From tcp_rate.c */
1155b9f64820SYuchung Cheng void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb);
1156b9f64820SYuchung Cheng void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
1157b9f64820SYuchung Cheng 			    struct rate_sample *rs);
1158b9f64820SYuchung Cheng void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
1159d4761754SYousuk Seung 		  bool is_sack_reneg, struct rate_sample *rs);
1160d7722e85SSoheil Hassas Yeganeh void tcp_rate_check_app_limited(struct sock *sk);
1161b9f64820SYuchung Cheng 
1162e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK
1163e60402d0SIlpo Järvinen  * handling. SACK is negotiated with the peer, and therefore it can vary
1164e60402d0SIlpo Järvinen  * between different flows.
1165e60402d0SIlpo Järvinen  *
1166e60402d0SIlpo Järvinen  * tcp_is_sack - SACK enabled
1167e60402d0SIlpo Järvinen  * tcp_is_reno - No SACK
1168e60402d0SIlpo Järvinen  */
1169e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp)
1170e60402d0SIlpo Järvinen {
1171ebeef4bcSEric Dumazet 	return likely(tp->rx_opt.sack_ok);
1172e60402d0SIlpo Järvinen }
1173e60402d0SIlpo Järvinen 
1174a2a385d6SEric Dumazet static inline bool tcp_is_reno(const struct tcp_sock *tp)
1175e60402d0SIlpo Järvinen {
1176e60402d0SIlpo Järvinen 	return !tcp_is_sack(tp);
1177e60402d0SIlpo Järvinen }
1178e60402d0SIlpo Järvinen 
117983ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
118083ae4088SIlpo Järvinen {
118183ae4088SIlpo Järvinen 	return tp->sacked_out + tp->lost_out;
118283ae4088SIlpo Järvinen }
118383ae4088SIlpo Järvinen 
11841da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
11851da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
11861da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
11871da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
11881da177e4SLinus Torvalds  *
11891da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
11901da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
11911da177e4SLinus Torvalds  *
11921da177e4SLinus Torvalds  * Read this equation as:
11931da177e4SLinus Torvalds  *
11941da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
11951da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
11961da177e4SLinus Torvalds  *	"Packets fast retransmitted"
11971da177e4SLinus Torvalds  */
119840efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
11991da177e4SLinus Torvalds {
120083ae4088SIlpo Järvinen 	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12030b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH	0x7fffffff
12040b6a05c1SIlpo Järvinen 
1205071d5080SYuchung Cheng static inline bool tcp_in_slow_start(const struct tcp_sock *tp)
1206071d5080SYuchung Cheng {
120776174004SYuchung Cheng 	return tp->snd_cwnd < tp->snd_ssthresh;
1208071d5080SYuchung Cheng }
1209071d5080SYuchung Cheng 
12100b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
12110b6a05c1SIlpo Järvinen {
12120b6a05c1SIlpo Järvinen 	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
12130b6a05c1SIlpo Järvinen }
12140b6a05c1SIlpo Järvinen 
1215684bad11SYuchung Cheng static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
1216684bad11SYuchung Cheng {
1217684bad11SYuchung Cheng 	return (TCPF_CA_CWR | TCPF_CA_Recovery) &
1218684bad11SYuchung Cheng 	       (1 << inet_csk(sk)->icsk_ca_state);
1219684bad11SYuchung Cheng }
1220684bad11SYuchung Cheng 
12211da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
1222684bad11SYuchung Cheng  * The exception is cwnd reduction phase, when cwnd is decreasing towards
12231da177e4SLinus Torvalds  * ssthresh.
12241da177e4SLinus Torvalds  */
12256687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
12261da177e4SLinus Torvalds {
12276687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
1228cf533ea5SEric Dumazet 
1229684bad11SYuchung Cheng 	if (tcp_in_cwnd_reduction(sk))
12301da177e4SLinus Torvalds 		return tp->snd_ssthresh;
12311da177e4SLinus Torvalds 	else
12321da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
12331da177e4SLinus Torvalds 			   ((tp->snd_cwnd >> 1) +
12341da177e4SLinus Torvalds 			    (tp->snd_cwnd >> 2)));
12351da177e4SLinus Torvalds }
12361da177e4SLinus Torvalds 
1237b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */
1238b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
12391da177e4SLinus Torvalds 
12405ee2c941SChristoph Paasch void tcp_enter_cwr(struct sock *sk);
12415c9f3023SJoe Perches __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
12421da177e4SLinus Torvalds 
12436b5a5c0dSNeal Cardwell /* The maximum number of MSS of available cwnd for which TSO defers
12446b5a5c0dSNeal Cardwell  * sending if not using sysctl_tcp_tso_win_divisor.
12456b5a5c0dSNeal Cardwell  */
12466b5a5c0dSNeal Cardwell static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
12476b5a5c0dSNeal Cardwell {
12486b5a5c0dSNeal Cardwell 	return 3;
12496b5a5c0dSNeal Cardwell }
12506b5a5c0dSNeal Cardwell 
125190840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */
125290840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
125390840defSIlpo Järvinen {
125490840defSIlpo Järvinen 	return tp->snd_una + tp->snd_wnd;
125590840defSIlpo Järvinen }
1256e114a710SEric Dumazet 
1257e114a710SEric Dumazet /* We follow the spirit of RFC2861 to validate cwnd but implement a more
1258e114a710SEric Dumazet  * flexible approach. The RFC suggests cwnd should not be raised unless
1259ca8a2263SNeal Cardwell  * it was fully used previously. And that's exactly what we do in
1260ca8a2263SNeal Cardwell  * congestion avoidance mode. But in slow start we allow cwnd to grow
1261ca8a2263SNeal Cardwell  * as long as the application has used half the cwnd.
1262e114a710SEric Dumazet  * Example :
1263e114a710SEric Dumazet  *    cwnd is 10 (IW10), but application sends 9 frames.
1264e114a710SEric Dumazet  *    We allow cwnd to reach 18 when all frames are ACKed.
1265e114a710SEric Dumazet  * This check is safe because it's as aggressive as slow start which already
1266e114a710SEric Dumazet  * risks 100% overshoot. The advantage is that we discourage application to
1267e114a710SEric Dumazet  * either send more filler packets or data to artificially blow up the cwnd
1268e114a710SEric Dumazet  * usage, and allow application-limited process to probe bw more aggressively.
1269e114a710SEric Dumazet  */
127024901551SEric Dumazet static inline bool tcp_is_cwnd_limited(const struct sock *sk)
1271e114a710SEric Dumazet {
1272e114a710SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1273e114a710SEric Dumazet 
1274ca8a2263SNeal Cardwell 	/* If in slow start, ensure cwnd grows to twice what was ACKed. */
1275071d5080SYuchung Cheng 	if (tcp_in_slow_start(tp))
1276ca8a2263SNeal Cardwell 		return tp->snd_cwnd < 2 * tp->max_packets_out;
1277ca8a2263SNeal Cardwell 
1278ca8a2263SNeal Cardwell 	return tp->is_cwnd_limited;
1279e114a710SEric Dumazet }
1280f4805edeSStephen Hemminger 
1281cadefe5fSEric Dumazet /* BBR congestion control needs pacing.
1282cadefe5fSEric Dumazet  * Same remark for SO_MAX_PACING_RATE.
1283cadefe5fSEric Dumazet  * sch_fq packet scheduler is efficiently handling pacing,
1284cadefe5fSEric Dumazet  * but is not always installed/used.
1285cadefe5fSEric Dumazet  * Return true if TCP stack should pace packets itself.
1286cadefe5fSEric Dumazet  */
1287cadefe5fSEric Dumazet static inline bool tcp_needs_internal_pacing(const struct sock *sk)
1288cadefe5fSEric Dumazet {
1289cadefe5fSEric Dumazet 	return smp_load_acquire(&sk->sk_pacing_status) == SK_PACING_NEEDED;
1290cadefe5fSEric Dumazet }
1291cadefe5fSEric Dumazet 
12928dc242adSEric Dumazet /* Estimates in how many jiffies next packet for this flow can be sent.
12938dc242adSEric Dumazet  * Scheduling a retransmit timer too early would be silly.
12943f80e08fSEric Dumazet  */
12958dc242adSEric Dumazet static inline unsigned long tcp_pacing_delay(const struct sock *sk)
12963f80e08fSEric Dumazet {
12978dc242adSEric Dumazet 	s64 delay = tcp_sk(sk)->tcp_wstamp_ns - tcp_sk(sk)->tcp_clock_cache;
12983f80e08fSEric Dumazet 
12998dc242adSEric Dumazet 	return delay > 0 ? nsecs_to_jiffies(delay) : 0;
13003f80e08fSEric Dumazet }
13013f80e08fSEric Dumazet 
13023f80e08fSEric Dumazet static inline void tcp_reset_xmit_timer(struct sock *sk,
13033f80e08fSEric Dumazet 					const int what,
13043f80e08fSEric Dumazet 					unsigned long when,
13058dc242adSEric Dumazet 					const unsigned long max_when)
13063f80e08fSEric Dumazet {
13078dc242adSEric Dumazet 	inet_csk_reset_xmit_timer(sk, what, when + tcp_pacing_delay(sk),
13083f80e08fSEric Dumazet 				  max_when);
13093f80e08fSEric Dumazet }
13103f80e08fSEric Dumazet 
131121c8fe99SEric Dumazet /* Something is really bad, we could not queue an additional packet,
13123f80e08fSEric Dumazet  * because qdisc is full or receiver sent a 0 window, or we are paced.
131321c8fe99SEric Dumazet  * We do not want to add fuel to the fire, or abort too early,
131421c8fe99SEric Dumazet  * so make sure the timer we arm now is at least 200ms in the future,
131521c8fe99SEric Dumazet  * regardless of current icsk_rto value (as it could be ~2ms)
131621c8fe99SEric Dumazet  */
131721c8fe99SEric Dumazet static inline unsigned long tcp_probe0_base(const struct sock *sk)
131821c8fe99SEric Dumazet {
131921c8fe99SEric Dumazet 	return max_t(unsigned long, inet_csk(sk)->icsk_rto, TCP_RTO_MIN);
132021c8fe99SEric Dumazet }
132121c8fe99SEric Dumazet 
132221c8fe99SEric Dumazet /* Variant of inet_csk_rto_backoff() used for zero window probes */
132321c8fe99SEric Dumazet static inline unsigned long tcp_probe0_when(const struct sock *sk,
132421c8fe99SEric Dumazet 					    unsigned long max_when)
132521c8fe99SEric Dumazet {
132621c8fe99SEric Dumazet 	u64 when = (u64)tcp_probe0_base(sk) << inet_csk(sk)->icsk_backoff;
132721c8fe99SEric Dumazet 
132821c8fe99SEric Dumazet 	return (unsigned long)min_t(u64, when, max_when);
132921c8fe99SEric Dumazet }
133021c8fe99SEric Dumazet 
13319e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk)
13321da177e4SLinus Torvalds {
133321c8fe99SEric Dumazet 	if (!tcp_sk(sk)->packets_out && !inet_csk(sk)->icsk_pending)
13343f80e08fSEric Dumazet 		tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
13358dc242adSEric Dumazet 				     tcp_probe0_base(sk), TCP_RTO_MAX);
13361da177e4SLinus Torvalds }
13371da177e4SLinus Torvalds 
1338ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
13391da177e4SLinus Torvalds {
13401da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
13411da177e4SLinus Torvalds }
13421da177e4SLinus Torvalds 
1343ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
13441da177e4SLinus Torvalds {
13451da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
13461da177e4SLinus Torvalds }
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds /*
13491da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
13501da177e4SLinus Torvalds  */
1351ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr,
1352ba7808eaSFrederik Deweerdt 				   __be32 daddr, __wsum base)
13531da177e4SLinus Torvalds {
13541da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_TCP, base);
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
1357a2a385d6SEric Dumazet static inline bool tcp_checksum_complete(struct sk_buff *skb)
13581da177e4SLinus Torvalds {
135960476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
13606ab6dfa6SCong Wang 		__skb_checksum_complete(skb);
13611da177e4SLinus Torvalds }
13621da177e4SLinus Torvalds 
1363c9c33212SEric Dumazet bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb);
1364ac6e7800SEric Dumazet int tcp_filter(struct sock *sk, struct sk_buff *skb);
13655c9f3023SJoe Perches void tcp_set_state(struct sock *sk, int state);
13665c9f3023SJoe Perches void tcp_done(struct sock *sk);
1367c1e64e29SLorenzo Colitti int tcp_abort(struct sock *sk, int err);
1368c1e64e29SLorenzo Colitti 
136940efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
13701da177e4SLinus Torvalds {
13711da177e4SLinus Torvalds 	rx_opt->dsack = 0;
13721da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
13731da177e4SLinus Torvalds }
13741da177e4SLinus Torvalds 
13756f021c62SEric Dumazet void tcp_cwnd_restart(struct sock *sk, s32 delta);
13766f021c62SEric Dumazet 
13776f021c62SEric Dumazet static inline void tcp_slow_start_after_idle_check(struct sock *sk)
13786f021c62SEric Dumazet {
13791b1fc3fdSWei Wang 	const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
13806f021c62SEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
13816f021c62SEric Dumazet 	s32 delta;
13826f021c62SEric Dumazet 
1383b510f0d2SEric Dumazet 	if (!sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle || tp->packets_out ||
13841b1fc3fdSWei Wang 	    ca_ops->cong_control)
13856f021c62SEric Dumazet 		return;
1386d635fbe2SEric Dumazet 	delta = tcp_jiffies32 - tp->lsndtime;
13876f021c62SEric Dumazet 	if (delta > inet_csk(sk)->icsk_rto)
13886f021c62SEric Dumazet 		tcp_cwnd_restart(sk, delta);
13896f021c62SEric Dumazet }
139085f16525SYuchung Cheng 
13911da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
1392ceef9ab6SEric Dumazet void tcp_select_initial_window(const struct sock *sk, int __space,
1393ceef9ab6SEric Dumazet 			       __u32 mss, __u32 *rcv_wnd,
13945c9f3023SJoe Perches 			       __u32 *window_clamp, int wscale_ok,
13955c9f3023SJoe Perches 			       __u8 *rcv_wscale, __u32 init_rcv_wnd);
13961da177e4SLinus Torvalds 
139794f0893eSEric Dumazet static inline int tcp_win_from_space(const struct sock *sk, int space)
13981da177e4SLinus Torvalds {
139994f0893eSEric Dumazet 	int tcp_adv_win_scale = sock_net(sk)->ipv4.sysctl_tcp_adv_win_scale;
1400c4836742SGao Feng 
1401c4836742SGao Feng 	return tcp_adv_win_scale <= 0 ?
1402c4836742SGao Feng 		(space>>(-tcp_adv_win_scale)) :
1403c4836742SGao Feng 		space - (space>>tcp_adv_win_scale);
14041da177e4SLinus Torvalds }
14051da177e4SLinus Torvalds 
14061da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
14071da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
14081da177e4SLinus Torvalds {
1409ebb3b78dSEric Dumazet 	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) -
141070c26558SEric Dumazet 				  READ_ONCE(sk->sk_backlog.len) -
14111da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
14121da177e4SLinus Torvalds }
14131da177e4SLinus Torvalds 
14141da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
14151da177e4SLinus Torvalds {
1416ebb3b78dSEric Dumazet 	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
141924adbc16SEric Dumazet /* We provision sk_rcvbuf around 200% of sk_rcvlowat.
142024adbc16SEric Dumazet  * If 87.5 % (7/8) of the space has been consumed, we want to override
142124adbc16SEric Dumazet  * SO_RCVLOWAT constraint, since we are receiving skbs with too small
142224adbc16SEric Dumazet  * len/truesize ratio.
142324adbc16SEric Dumazet  */
142424adbc16SEric Dumazet static inline bool tcp_rmem_pressure(const struct sock *sk)
142524adbc16SEric Dumazet {
142624adbc16SEric Dumazet 	int rcvbuf = READ_ONCE(sk->sk_rcvbuf);
142724adbc16SEric Dumazet 	int threshold = rcvbuf - (rcvbuf >> 3);
142824adbc16SEric Dumazet 
142924adbc16SEric Dumazet 	return atomic_read(&sk->sk_rmem_alloc) > threshold;
143024adbc16SEric Dumazet }
143124adbc16SEric Dumazet 
1432843f4a55SYuchung Cheng extern void tcp_openreq_init_rwin(struct request_sock *req,
1433b1964b5fSEric Dumazet 				  const struct sock *sk_listener,
1434b1964b5fSEric Dumazet 				  const struct dst_entry *dst);
1435843f4a55SYuchung Cheng 
14365c9f3023SJoe Perches void tcp_enter_memory_pressure(struct sock *sk);
143706044751SEric Dumazet void tcp_leave_memory_pressure(struct sock *sk);
14381da177e4SLinus Torvalds 
14391da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
14401da177e4SLinus Torvalds {
1441b840d15dSNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
1442b840d15dSNikolay Borisov 
1443b840d15dSNikolay Borisov 	return tp->keepalive_intvl ? : net->ipv4.sysctl_tcp_keepalive_intvl;
14441da177e4SLinus Torvalds }
14451da177e4SLinus Torvalds 
14461da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
14471da177e4SLinus Torvalds {
144813b287e8SNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
144913b287e8SNikolay Borisov 
145013b287e8SNikolay Borisov 	return tp->keepalive_time ? : net->ipv4.sysctl_tcp_keepalive_time;
14511da177e4SLinus Torvalds }
14521da177e4SLinus Torvalds 
1453df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp)
1454df19a626SEric Dumazet {
14559bd6861bSNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
14569bd6861bSNikolay Borisov 
14579bd6861bSNikolay Borisov 	return tp->keepalive_probes ? : net->ipv4.sysctl_tcp_keepalive_probes;
1458df19a626SEric Dumazet }
1459df19a626SEric Dumazet 
14606c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
14616c37e5deSFlavio Leitner {
14626c37e5deSFlavio Leitner 	const struct inet_connection_sock *icsk = &tp->inet_conn;
14636c37e5deSFlavio Leitner 
146470eabf0eSEric Dumazet 	return min_t(u32, tcp_jiffies32 - icsk->icsk_ack.lrcvtime,
146570eabf0eSEric Dumazet 			  tcp_jiffies32 - tp->rcv_tstamp);
14666c37e5deSFlavio Leitner }
14676c37e5deSFlavio Leitner 
1468463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
14691da177e4SLinus Torvalds {
14701e579caaSNikolay Borisov 	int fin_timeout = tcp_sk(sk)->linger2 ? : sock_net(sk)->ipv4.sysctl_tcp_fin_timeout;
1471463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
14721da177e4SLinus Torvalds 
1473463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1474463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 	return fin_timeout;
14771da177e4SLinus Torvalds }
14781da177e4SLinus Torvalds 
1479a2a385d6SEric Dumazet static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
1480c887e6d2SIlpo Järvinen 				  int paws_win)
14811da177e4SLinus Torvalds {
1482c887e6d2SIlpo Järvinen 	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1483a2a385d6SEric Dumazet 		return true;
1484cca9bab1SArnd Bergmann 	if (unlikely(!time_before32(ktime_get_seconds(),
1485cca9bab1SArnd Bergmann 				    rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)))
1486a2a385d6SEric Dumazet 		return true;
1487bc2ce894SEric Dumazet 	/*
1488bc2ce894SEric Dumazet 	 * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
1489bc2ce894SEric Dumazet 	 * then following tcp messages have valid values. Ignore 0 value,
1490bc2ce894SEric Dumazet 	 * or else 'negative' tsval might forbid us to accept their packets.
1491bc2ce894SEric Dumazet 	 */
1492bc2ce894SEric Dumazet 	if (!rx_opt->ts_recent)
1493a2a385d6SEric Dumazet 		return true;
1494a2a385d6SEric Dumazet 	return false;
1495c887e6d2SIlpo Järvinen }
1496c887e6d2SIlpo Järvinen 
1497a2a385d6SEric Dumazet static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
1498c887e6d2SIlpo Järvinen 				   int rst)
1499c887e6d2SIlpo Järvinen {
1500c887e6d2SIlpo Järvinen 	if (tcp_paws_check(rx_opt, 0))
1501a2a385d6SEric Dumazet 		return false;
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
15041da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
15051da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
15061da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
15071da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
15081da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
15091da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
15101da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
15111da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
15141da177e4SLinus Torvalds 	 */
1515cca9bab1SArnd Bergmann 	if (rst && !time_before32(ktime_get_seconds(),
1516cca9bab1SArnd Bergmann 				  rx_opt->ts_recent_stamp + TCP_PAWS_MSL))
1517a2a385d6SEric Dumazet 		return false;
1518a2a385d6SEric Dumazet 	return true;
15191da177e4SLinus Torvalds }
15201da177e4SLinus Torvalds 
15217970ddc8SEric Dumazet bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
15227970ddc8SEric Dumazet 			  int mib_idx, u32 *last_oow_ack_time);
1523032ee423SNeal Cardwell 
1524a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net)
15251da177e4SLinus Torvalds {
15261da177e4SLinus Torvalds 	/* See RFC 2012 */
15276aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOALGORITHM, 1);
15286aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
15296aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
15306aef70a8SEric Dumazet 	TCP_ADD_STATS(net, TCP_MIB_MAXCONN, -1);
15311da177e4SLinus Torvalds }
15321da177e4SLinus Torvalds 
15336a438bbeSStephen Hemminger /* from STCP */
1534ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
15350800f170SDavid S. Miller {
15366a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
1537ef9da47cSIlpo Järvinen }
1538ef9da47cSIlpo Järvinen 
1539ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
1540ef9da47cSIlpo Järvinen {
1541ef9da47cSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
15426a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
1543b7689205SIlpo Järvinen }
1544b7689205SIlpo Järvinen 
1545a915da9bSEric Dumazet union tcp_md5_addr {
1546a915da9bSEric Dumazet 	struct in_addr  a4;
1547a915da9bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1548a915da9bSEric Dumazet 	struct in6_addr	a6;
1549a915da9bSEric Dumazet #endif
1550a915da9bSEric Dumazet };
1551a915da9bSEric Dumazet 
1552cfb6eeb4SYOSHIFUJI Hideaki /* - key database */
1553cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key {
1554a915da9bSEric Dumazet 	struct hlist_node	node;
1555cfb6eeb4SYOSHIFUJI Hideaki 	u8			keylen;
1556a915da9bSEric Dumazet 	u8			family; /* AF_INET or AF_INET6 */
15576797318eSIvan Delalande 	u8			prefixlen;
1558dea53bb8SDavid Ahern 	union tcp_md5_addr	addr;
1559dea53bb8SDavid Ahern 	int			l3index; /* set if key added with L3 scope */
1560a915da9bSEric Dumazet 	u8			key[TCP_MD5SIG_MAXKEYLEN];
1561a915da9bSEric Dumazet 	struct rcu_head		rcu;
1562cfb6eeb4SYOSHIFUJI Hideaki };
1563cfb6eeb4SYOSHIFUJI Hideaki 
1564cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */
1565cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info {
1566a915da9bSEric Dumazet 	struct hlist_head	head;
1567a8afca03SEric Dumazet 	struct rcu_head		rcu;
1568cfb6eeb4SYOSHIFUJI Hideaki };
1569cfb6eeb4SYOSHIFUJI Hideaki 
1570cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */
1571cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr {
1572cfb6eeb4SYOSHIFUJI Hideaki 	__be32		saddr;
1573cfb6eeb4SYOSHIFUJI Hideaki 	__be32		daddr;
1574cfb6eeb4SYOSHIFUJI Hideaki 	__u8		pad;
1575cfb6eeb4SYOSHIFUJI Hideaki 	__u8		protocol;
1576cfb6eeb4SYOSHIFUJI Hideaki 	__be16		len;
1577cfb6eeb4SYOSHIFUJI Hideaki };
1578cfb6eeb4SYOSHIFUJI Hideaki 
1579cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr {
1580cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr	saddr;
1581cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr daddr;
1582cfb6eeb4SYOSHIFUJI Hideaki 	__be32		len;
1583cfb6eeb4SYOSHIFUJI Hideaki 	__be32		protocol;	/* including padding */
1584cfb6eeb4SYOSHIFUJI Hideaki };
1585cfb6eeb4SYOSHIFUJI Hideaki 
1586cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block {
1587cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr ip4;
1588dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1589cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_pseudohdr ip6;
1590cfb6eeb4SYOSHIFUJI Hideaki #endif
1591cfb6eeb4SYOSHIFUJI Hideaki };
1592cfb6eeb4SYOSHIFUJI Hideaki 
1593cfb6eeb4SYOSHIFUJI Hideaki /* - pool: digest algorithm, hash description and scratch buffer */
1594cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool {
1595cf80e0e4SHerbert Xu 	struct ahash_request	*md5_req;
159619689e38SEric Dumazet 	void			*scratch;
1597cfb6eeb4SYOSHIFUJI Hideaki };
1598cfb6eeb4SYOSHIFUJI Hideaki 
1599cfb6eeb4SYOSHIFUJI Hideaki /* - functions */
160039f8e58eSEric Dumazet int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
160139f8e58eSEric Dumazet 			const struct sock *sk, const struct sk_buff *skb);
16025c9f3023SJoe Perches int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
1603dea53bb8SDavid Ahern 		   int family, u8 prefixlen, int l3index,
1604dea53bb8SDavid Ahern 		   const u8 *newkey, u8 newkeylen, gfp_t gfp);
16055c9f3023SJoe Perches int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
1606dea53bb8SDavid Ahern 		   int family, u8 prefixlen, int l3index);
1607b83e3debSEric Dumazet struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
1608fd3a154aSEric Dumazet 					 const struct sock *addr_sk);
1609cfb6eeb4SYOSHIFUJI Hideaki 
16109501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
16116015c71eSEric Dumazet #include <linux/jump_label.h>
1612921f9a0fSEric Dumazet extern struct static_key_false tcp_md5_needed;
1613dea53bb8SDavid Ahern struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index,
16145c9f3023SJoe Perches 					   const union tcp_md5_addr *addr,
16155c9f3023SJoe Perches 					   int family);
16166015c71eSEric Dumazet static inline struct tcp_md5sig_key *
1617dea53bb8SDavid Ahern tcp_md5_do_lookup(const struct sock *sk, int l3index,
1618dea53bb8SDavid Ahern 		  const union tcp_md5_addr *addr, int family)
16196015c71eSEric Dumazet {
1620921f9a0fSEric Dumazet 	if (!static_branch_unlikely(&tcp_md5_needed))
16216015c71eSEric Dumazet 		return NULL;
1622dea53bb8SDavid Ahern 	return __tcp_md5_do_lookup(sk, l3index, addr, family);
16236015c71eSEric Dumazet }
16246015c71eSEric Dumazet 
1625a915da9bSEric Dumazet #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_key)
16269501f972SYOSHIFUJI Hideaki #else
1627dea53bb8SDavid Ahern static inline struct tcp_md5sig_key *
1628dea53bb8SDavid Ahern tcp_md5_do_lookup(const struct sock *sk, int l3index,
1629dea53bb8SDavid Ahern 		  const union tcp_md5_addr *addr, int family)
1630a915da9bSEric Dumazet {
1631a915da9bSEric Dumazet 	return NULL;
1632a915da9bSEric Dumazet }
16339501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	NULL
16349501f972SYOSHIFUJI Hideaki #endif
16359501f972SYOSHIFUJI Hideaki 
16365c9f3023SJoe Perches bool tcp_alloc_md5sig_pool(void);
1637cfb6eeb4SYOSHIFUJI Hideaki 
16385c9f3023SJoe Perches struct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
163971cea17eSEric Dumazet static inline void tcp_put_md5sig_pool(void)
164071cea17eSEric Dumazet {
164171cea17eSEric Dumazet 	local_bh_enable();
164271cea17eSEric Dumazet }
164335790c04SEric Dumazet 
16445c9f3023SJoe Perches int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
164595c96174SEric Dumazet 			  unsigned int header_len);
16465c9f3023SJoe Perches int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
1647cf533ea5SEric Dumazet 		     const struct tcp_md5sig_key *key);
1648cfb6eeb4SYOSHIFUJI Hideaki 
164910467163SJerry Chu /* From tcp_fastopen.c */
16505c9f3023SJoe Perches void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
16517268586bSYuchung Cheng 			    struct tcp_fastopen_cookie *cookie);
16525c9f3023SJoe Perches void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
16532646c831SDaniel Lee 			    struct tcp_fastopen_cookie *cookie, bool syn_lost,
16542646c831SDaniel Lee 			    u16 try_exp);
1655783237e8SYuchung Cheng struct tcp_fastopen_request {
1656783237e8SYuchung Cheng 	/* Fast Open cookie. Size 0 means a cookie request */
1657783237e8SYuchung Cheng 	struct tcp_fastopen_cookie	cookie;
1658783237e8SYuchung Cheng 	struct msghdr			*data;  /* data in MSG_FASTOPEN */
1659f5ddcbbbSEric Dumazet 	size_t				size;
1660f5ddcbbbSEric Dumazet 	int				copied;	/* queued in tcp_connect() */
1661f859a448SWillem de Bruijn 	struct ubuf_info		*uarg;
1662783237e8SYuchung Cheng };
1663783237e8SYuchung Cheng void tcp_free_fastopen_req(struct tcp_sock *tp);
16641fba70e5SYuchung Cheng void tcp_fastopen_destroy_cipher(struct sock *sk);
166543713848SHaishuang Yan void tcp_fastopen_ctx_destroy(struct net *net);
16661fba70e5SYuchung Cheng int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
1667438ac880SArd Biesheuvel 			      void *primary_key, void *backup_key);
166861d2bcaeSEric Dumazet void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb);
16697c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
16705b7ed089SYuchung Cheng 			      struct request_sock *req,
167171c02379SChristoph Paasch 			      struct tcp_fastopen_cookie *foc,
167271c02379SChristoph Paasch 			      const struct dst_entry *dst);
167343713848SHaishuang Yan void tcp_fastopen_init_key_once(struct net *net);
1674065263f4SWei Wang bool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss,
1675065263f4SWei Wang 			     struct tcp_fastopen_cookie *cookie);
167619f6d3f3SWei Wang bool tcp_fastopen_defer_connect(struct sock *sk, int *err);
1677438ac880SArd Biesheuvel #define TCP_FASTOPEN_KEY_LENGTH sizeof(siphash_key_t)
16789092a76dSJason Baron #define TCP_FASTOPEN_KEY_MAX 2
16799092a76dSJason Baron #define TCP_FASTOPEN_KEY_BUF_LENGTH \
16809092a76dSJason Baron 	(TCP_FASTOPEN_KEY_LENGTH * TCP_FASTOPEN_KEY_MAX)
168110467163SJerry Chu 
168210467163SJerry Chu /* Fastopen key context */
168310467163SJerry Chu struct tcp_fastopen_context {
1684438ac880SArd Biesheuvel 	siphash_key_t	key[TCP_FASTOPEN_KEY_MAX];
1685c681edaeSArd Biesheuvel 	int		num;
168610467163SJerry Chu 	struct rcu_head	rcu;
168710467163SJerry Chu };
168810467163SJerry Chu 
1689cf1ef3f0SWei Wang extern unsigned int sysctl_tcp_fastopen_blackhole_timeout;
169046c2fa39SWei Wang void tcp_fastopen_active_disable(struct sock *sk);
1691cf1ef3f0SWei Wang bool tcp_fastopen_active_should_disable(struct sock *sk);
1692cf1ef3f0SWei Wang void tcp_fastopen_active_disable_ofo_check(struct sock *sk);
16937268586bSYuchung Cheng void tcp_fastopen_active_detect_blackhole(struct sock *sk, bool expired);
1694cf1ef3f0SWei Wang 
16959092a76dSJason Baron /* Caller needs to wrap with rcu_read_(un)lock() */
16969092a76dSJason Baron static inline
16979092a76dSJason Baron struct tcp_fastopen_context *tcp_fastopen_get_ctx(const struct sock *sk)
16989092a76dSJason Baron {
16999092a76dSJason Baron 	struct tcp_fastopen_context *ctx;
17009092a76dSJason Baron 
17019092a76dSJason Baron 	ctx = rcu_dereference(inet_csk(sk)->icsk_accept_queue.fastopenq.ctx);
17029092a76dSJason Baron 	if (!ctx)
17039092a76dSJason Baron 		ctx = rcu_dereference(sock_net(sk)->ipv4.tcp_fastopen_ctx);
17049092a76dSJason Baron 	return ctx;
17059092a76dSJason Baron }
17069092a76dSJason Baron 
17079092a76dSJason Baron static inline
17089092a76dSJason Baron bool tcp_fastopen_cookie_match(const struct tcp_fastopen_cookie *foc,
17099092a76dSJason Baron 			       const struct tcp_fastopen_cookie *orig)
17109092a76dSJason Baron {
17119092a76dSJason Baron 	if (orig->len == TCP_FASTOPEN_COOKIE_SIZE &&
17129092a76dSJason Baron 	    orig->len == foc->len &&
17139092a76dSJason Baron 	    !memcmp(orig->val, foc->val, foc->len))
17149092a76dSJason Baron 		return true;
17159092a76dSJason Baron 	return false;
17169092a76dSJason Baron }
17179092a76dSJason Baron 
17189092a76dSJason Baron static inline
17199092a76dSJason Baron int tcp_fastopen_context_len(const struct tcp_fastopen_context *ctx)
17209092a76dSJason Baron {
1721c681edaeSArd Biesheuvel 	return ctx->num;
17229092a76dSJason Baron }
17239092a76dSJason Baron 
172405b055e8SFrancis Yan /* Latencies incurred by various limits for a sender. They are
172505b055e8SFrancis Yan  * chronograph-like stats that are mutually exclusive.
172605b055e8SFrancis Yan  */
172705b055e8SFrancis Yan enum tcp_chrono {
172805b055e8SFrancis Yan 	TCP_CHRONO_UNSPEC,
172905b055e8SFrancis Yan 	TCP_CHRONO_BUSY, /* Actively sending data (non-empty write queue) */
173005b055e8SFrancis Yan 	TCP_CHRONO_RWND_LIMITED, /* Stalled by insufficient receive window */
173105b055e8SFrancis Yan 	TCP_CHRONO_SNDBUF_LIMITED, /* Stalled by insufficient send buffer */
173205b055e8SFrancis Yan 	__TCP_CHRONO_MAX,
173305b055e8SFrancis Yan };
173405b055e8SFrancis Yan 
173505b055e8SFrancis Yan void tcp_chrono_start(struct sock *sk, const enum tcp_chrono type);
173605b055e8SFrancis Yan void tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type);
173705b055e8SFrancis Yan 
1738e2080072SEric Dumazet /* This helper is needed, because skb->tcp_tsorted_anchor uses
1739e2080072SEric Dumazet  * the same memory storage than skb->destructor/_skb_refdst
1740e2080072SEric Dumazet  */
1741e2080072SEric Dumazet static inline void tcp_skb_tsorted_anchor_cleanup(struct sk_buff *skb)
1742e2080072SEric Dumazet {
1743e2080072SEric Dumazet 	skb->destructor = NULL;
1744e2080072SEric Dumazet 	skb->_skb_refdst = 0UL;
1745e2080072SEric Dumazet }
1746e2080072SEric Dumazet 
1747e2080072SEric Dumazet #define tcp_skb_tsorted_save(skb) {		\
1748e2080072SEric Dumazet 	unsigned long _save = skb->_skb_refdst;	\
1749e2080072SEric Dumazet 	skb->_skb_refdst = 0UL;
1750e2080072SEric Dumazet 
1751e2080072SEric Dumazet #define tcp_skb_tsorted_restore(skb)		\
1752e2080072SEric Dumazet 	skb->_skb_refdst = _save;		\
1753e2080072SEric Dumazet }
1754e2080072SEric Dumazet 
1755ac3f09baSEric Dumazet void tcp_write_queue_purge(struct sock *sk);
1756fe067e8aSDavid S. Miller 
175775c119afSEric Dumazet static inline struct sk_buff *tcp_rtx_queue_head(const struct sock *sk)
175875c119afSEric Dumazet {
175975c119afSEric Dumazet 	return skb_rb_first(&sk->tcp_rtx_queue);
176075c119afSEric Dumazet }
176175c119afSEric Dumazet 
1762b617158dSEric Dumazet static inline struct sk_buff *tcp_rtx_queue_tail(const struct sock *sk)
1763b617158dSEric Dumazet {
1764b617158dSEric Dumazet 	return skb_rb_last(&sk->tcp_rtx_queue);
1765b617158dSEric Dumazet }
1766b617158dSEric Dumazet 
1767cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
1768fe067e8aSDavid S. Miller {
1769cd07a8eaSDavid S. Miller 	return skb_peek(&sk->sk_write_queue);
1770fe067e8aSDavid S. Miller }
1771fe067e8aSDavid S. Miller 
1772cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
1773fe067e8aSDavid S. Miller {
1774cd07a8eaSDavid S. Miller 	return skb_peek_tail(&sk->sk_write_queue);
1775fe067e8aSDavid S. Miller }
1776fe067e8aSDavid S. Miller 
1777234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
1778cd07a8eaSDavid S. Miller 	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1779234b6860SIlpo Järvinen 
1780cf533ea5SEric Dumazet static inline struct sk_buff *tcp_send_head(const struct sock *sk)
1781fe067e8aSDavid S. Miller {
178275c119afSEric Dumazet 	return skb_peek(&sk->sk_write_queue);
1783fe067e8aSDavid S. Miller }
1784fe067e8aSDavid S. Miller 
1785cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk,
1786cd07a8eaSDavid S. Miller 				   const struct sk_buff *skb)
1787cd07a8eaSDavid S. Miller {
1788cd07a8eaSDavid S. Miller 	return skb_queue_is_last(&sk->sk_write_queue, skb);
1789cd07a8eaSDavid S. Miller }
1790cd07a8eaSDavid S. Miller 
1791ee2aabd3SEric Dumazet /**
1792ee2aabd3SEric Dumazet  * tcp_write_queue_empty - test if any payload (or FIN) is available in write queue
1793ee2aabd3SEric Dumazet  * @sk: socket
1794ee2aabd3SEric Dumazet  *
1795ee2aabd3SEric Dumazet  * Since the write queue can have a temporary empty skb in it,
1796ee2aabd3SEric Dumazet  * we must not use "return skb_queue_empty(&sk->sk_write_queue)"
1797ee2aabd3SEric Dumazet  */
179875c119afSEric Dumazet static inline bool tcp_write_queue_empty(const struct sock *sk)
1799fe067e8aSDavid S. Miller {
1800ee2aabd3SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1801ee2aabd3SEric Dumazet 
1802ee2aabd3SEric Dumazet 	return tp->write_seq == tp->snd_nxt;
180375c119afSEric Dumazet }
180475c119afSEric Dumazet 
180575c119afSEric Dumazet static inline bool tcp_rtx_queue_empty(const struct sock *sk)
180675c119afSEric Dumazet {
180775c119afSEric Dumazet 	return RB_EMPTY_ROOT(&sk->tcp_rtx_queue);
180875c119afSEric Dumazet }
180975c119afSEric Dumazet 
181075c119afSEric Dumazet static inline bool tcp_rtx_and_write_queues_empty(const struct sock *sk)
181175c119afSEric Dumazet {
181275c119afSEric Dumazet 	return tcp_rtx_queue_empty(sk) && tcp_write_queue_empty(sk);
1813fe067e8aSDavid S. Miller }
1814fe067e8aSDavid S. Miller 
1815fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1816fe067e8aSDavid S. Miller {
1817a43e052bSEric Dumazet 	__skb_queue_tail(&sk->sk_write_queue, skb);
1818fe067e8aSDavid S. Miller 
1819fe067e8aSDavid S. Miller 	/* Queue it, remembering where we must start sending. */
182050895b9dSEric Dumazet 	if (sk->sk_write_queue.next == skb)
18210f87230dSFrancis Yan 		tcp_chrono_start(sk, TCP_CHRONO_BUSY);
1822fe067e8aSDavid S. Miller }
1823fe067e8aSDavid S. Miller 
182443f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk.  */
1825fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new,
1826fe067e8aSDavid S. Miller 						  struct sk_buff *skb,
1827fe067e8aSDavid S. Miller 						  struct sock *sk)
1828fe067e8aSDavid S. Miller {
182943f59c89SDavid S. Miller 	__skb_queue_before(&sk->sk_write_queue, skb, new);
1830fe067e8aSDavid S. Miller }
1831fe067e8aSDavid S. Miller 
1832fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
1833fe067e8aSDavid S. Miller {
18344a269818SEric Dumazet 	tcp_skb_tsorted_anchor_cleanup(skb);
1835fe067e8aSDavid S. Miller 	__skb_unlink(skb, &sk->sk_write_queue);
1836fe067e8aSDavid S. Miller }
1837fe067e8aSDavid S. Miller 
183875c119afSEric Dumazet void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb);
183975c119afSEric Dumazet 
184075c119afSEric Dumazet static inline void tcp_rtx_queue_unlink(struct sk_buff *skb, struct sock *sk)
1841fe067e8aSDavid S. Miller {
184275c119afSEric Dumazet 	tcp_skb_tsorted_anchor_cleanup(skb);
184375c119afSEric Dumazet 	rb_erase(&skb->rbnode, &sk->tcp_rtx_queue);
184475c119afSEric Dumazet }
184575c119afSEric Dumazet 
184675c119afSEric Dumazet static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct sock *sk)
184775c119afSEric Dumazet {
184875c119afSEric Dumazet 	list_del(&skb->tcp_tsorted_anchor);
184975c119afSEric Dumazet 	tcp_rtx_queue_unlink(skb, sk);
185075c119afSEric Dumazet 	sk_wmem_free_skb(sk, skb);
1851fe067e8aSDavid S. Miller }
1852fe067e8aSDavid S. Miller 
185312d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk)
185412d50c46SKrishna Kumar {
185512d50c46SKrishna Kumar 	if (tcp_send_head(sk)) {
185612d50c46SKrishna Kumar 		struct tcp_sock *tp = tcp_sk(sk);
185712d50c46SKrishna Kumar 
185812d50c46SKrishna Kumar 		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
185912d50c46SKrishna Kumar 	}
186012d50c46SKrishna Kumar }
186112d50c46SKrishna Kumar 
1862ecb97192SNeal Cardwell /* Start sequence of the skb just after the highest skb with SACKed
1863ecb97192SNeal Cardwell  * bit, valid only if sacked_out > 0 or when the caller has ensured
1864ecb97192SNeal Cardwell  * validity by itself.
1865a47e5a98SIlpo Järvinen  */
1866a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
1867a47e5a98SIlpo Järvinen {
1868a47e5a98SIlpo Järvinen 	if (!tp->sacked_out)
1869a47e5a98SIlpo Järvinen 		return tp->snd_una;
18706859d494SIlpo Järvinen 
18716859d494SIlpo Järvinen 	if (tp->highest_sack == NULL)
18726859d494SIlpo Järvinen 		return tp->snd_nxt;
18736859d494SIlpo Järvinen 
1874a47e5a98SIlpo Järvinen 	return TCP_SKB_CB(tp->highest_sack)->seq;
1875a47e5a98SIlpo Järvinen }
1876a47e5a98SIlpo Järvinen 
18776859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
18786859d494SIlpo Järvinen {
187950895b9dSEric Dumazet 	tcp_sk(sk)->highest_sack = skb_rb_next(skb);
18806859d494SIlpo Järvinen }
18816859d494SIlpo Järvinen 
18826859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
18836859d494SIlpo Järvinen {
18846859d494SIlpo Järvinen 	return tcp_sk(sk)->highest_sack;
18856859d494SIlpo Järvinen }
18866859d494SIlpo Järvinen 
18876859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk)
18886859d494SIlpo Järvinen {
188950895b9dSEric Dumazet 	tcp_sk(sk)->highest_sack = tcp_rtx_queue_head(sk);
18906859d494SIlpo Järvinen }
18916859d494SIlpo Järvinen 
18922b7cda9cSEric Dumazet /* Called when old skb is about to be deleted and replaced by new skb */
18932b7cda9cSEric Dumazet static inline void tcp_highest_sack_replace(struct sock *sk,
18946859d494SIlpo Järvinen 					    struct sk_buff *old,
18956859d494SIlpo Järvinen 					    struct sk_buff *new)
18966859d494SIlpo Järvinen {
18972b7cda9cSEric Dumazet 	if (old == tcp_highest_sack(sk))
18986859d494SIlpo Järvinen 		tcp_sk(sk)->highest_sack = new;
18996859d494SIlpo Järvinen }
19006859d494SIlpo Järvinen 
1901b1f0a0e9SFlorian Westphal /* This helper checks if socket has IP_TRANSPARENT set */
1902b1f0a0e9SFlorian Westphal static inline bool inet_sk_transparent(const struct sock *sk)
1903b1f0a0e9SFlorian Westphal {
1904b1f0a0e9SFlorian Westphal 	switch (sk->sk_state) {
1905b1f0a0e9SFlorian Westphal 	case TCP_TIME_WAIT:
1906b1f0a0e9SFlorian Westphal 		return inet_twsk(sk)->tw_transparent;
1907b1f0a0e9SFlorian Westphal 	case TCP_NEW_SYN_RECV:
1908b1f0a0e9SFlorian Westphal 		return inet_rsk(inet_reqsk(sk))->no_srccheck;
1909b1f0a0e9SFlorian Westphal 	}
1910b1f0a0e9SFlorian Westphal 	return inet_sk(sk)->transparent;
1911b1f0a0e9SFlorian Westphal }
1912b1f0a0e9SFlorian Westphal 
19135aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from
19145aa4b32fSAndreas Petlund  * increased latency). Used to trigger latency-reducing mechanisms.
19155aa4b32fSAndreas Petlund  */
1916a2a385d6SEric Dumazet static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
19175aa4b32fSAndreas Petlund {
19185aa4b32fSAndreas Petlund 	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
19195aa4b32fSAndreas Petlund }
19205aa4b32fSAndreas Petlund 
19211da177e4SLinus Torvalds /* /proc */
19221da177e4SLinus Torvalds enum tcp_seq_states {
19231da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
19241da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
19251da177e4SLinus Torvalds };
19261da177e4SLinus Torvalds 
192737d849bbSChristoph Hellwig void *tcp_seq_start(struct seq_file *seq, loff_t *pos);
192837d849bbSChristoph Hellwig void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
192937d849bbSChristoph Hellwig void tcp_seq_stop(struct seq_file *seq, void *v);
193073cb88ecSArjan van de Ven 
19311da177e4SLinus Torvalds struct tcp_seq_afinfo {
19321da177e4SLinus Torvalds 	sa_family_t			family;
19331da177e4SLinus Torvalds };
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds struct tcp_iter_state {
1936a4146b1bSDenis V. Lunev 	struct seq_net_private	p;
19371da177e4SLinus Torvalds 	enum tcp_seq_states	state;
19381da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
1939a7cb5a49SEric W. Biederman 	int			bucket, offset, sbucket, num;
1940a8b690f9STom Herbert 	loff_t			last_pos;
19411da177e4SLinus Torvalds };
19421da177e4SLinus Torvalds 
194320380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
1944c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops;
194520380731SArnaldo Carvalho de Melo 
19465c9f3023SJoe Perches void tcp_v4_destroy_sock(struct sock *sk);
194720380731SArnaldo Carvalho de Melo 
194828be6e07SEric Dumazet struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
1949c8f44affSMichał Mirosław 				netdev_features_t features);
1950d4546c25SDavid Miller struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb);
19515c9f3023SJoe Perches int tcp_gro_complete(struct sk_buff *skb);
195228850dc7SDaniel Borkmann 
19535c9f3023SJoe Perches void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
1954f4c50d99SHerbert Xu 
1955c9bee3b7SEric Dumazet static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
1956c9bee3b7SEric Dumazet {
19574979f2d9SNikolay Borisov 	struct net *net = sock_net((struct sock *)tp);
19584979f2d9SNikolay Borisov 	return tp->notsent_lowat ?: net->ipv4.sysctl_tcp_notsent_lowat;
1959c9bee3b7SEric Dumazet }
1960c9bee3b7SEric Dumazet 
1961a74f0fa0SEric Dumazet /* @wake is one when sk_stream_write_space() calls us.
1962a74f0fa0SEric Dumazet  * This sends EPOLLOUT only if notsent_bytes is half the limit.
1963a74f0fa0SEric Dumazet  * This mimics the strategy used in sock_def_write_space().
1964a74f0fa0SEric Dumazet  */
1965a74f0fa0SEric Dumazet static inline bool tcp_stream_memory_free(const struct sock *sk, int wake)
1966c9bee3b7SEric Dumazet {
1967c9bee3b7SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1968e0d694d6SEric Dumazet 	u32 notsent_bytes = READ_ONCE(tp->write_seq) -
1969e0d694d6SEric Dumazet 			    READ_ONCE(tp->snd_nxt);
1970c9bee3b7SEric Dumazet 
1971a74f0fa0SEric Dumazet 	return (notsent_bytes << wake) < tcp_notsent_lowat(tp);
1972c9bee3b7SEric Dumazet }
1973c9bee3b7SEric Dumazet 
197420380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
19755c9f3023SJoe Perches int tcp4_proc_init(void);
19765c9f3023SJoe Perches void tcp4_proc_exit(void);
197720380731SArnaldo Carvalho de Melo #endif
197820380731SArnaldo Carvalho de Melo 
1979ea3bea3aSEric Dumazet int tcp_rtx_synack(const struct sock *sk, struct request_sock *req);
19801fb6f159SOctavian Purdila int tcp_conn_request(struct request_sock_ops *rsk_ops,
19811fb6f159SOctavian Purdila 		     const struct tcp_request_sock_ops *af_ops,
19821fb6f159SOctavian Purdila 		     struct sock *sk, struct sk_buff *skb);
19835db92c99SOctavian Purdila 
1984cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */
1985cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops {
1986cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1987b83e3debSEric Dumazet 	struct tcp_md5sig_key	*(*md5_lookup) (const struct sock *sk,
1988fd3a154aSEric Dumazet 						const struct sock *addr_sk);
1989cfb6eeb4SYOSHIFUJI Hideaki 	int		(*calc_md5_hash)(char *location,
199039f8e58eSEric Dumazet 					 const struct tcp_md5sig_key *md5,
1991318cf7aaSEric Dumazet 					 const struct sock *sk,
1992318cf7aaSEric Dumazet 					 const struct sk_buff *skb);
1993cfb6eeb4SYOSHIFUJI Hideaki 	int		(*md5_parse)(struct sock *sk,
19948917a777SIvan Delalande 				     int optname,
1995cfb6eeb4SYOSHIFUJI Hideaki 				     char __user *optval,
1996cfb6eeb4SYOSHIFUJI Hideaki 				     int optlen);
1997cfb6eeb4SYOSHIFUJI Hideaki #endif
1998cfb6eeb4SYOSHIFUJI Hideaki };
1999cfb6eeb4SYOSHIFUJI Hideaki 
2000cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops {
20012aec4a29SOctavian Purdila 	u16 mss_clamp;
2002cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
2003b83e3debSEric Dumazet 	struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk,
2004fd3a154aSEric Dumazet 						 const struct sock *addr_sk);
2005e3afe7b7SJohn Dykstra 	int		(*calc_md5_hash) (char *location,
200639f8e58eSEric Dumazet 					  const struct tcp_md5sig_key *md5,
2007318cf7aaSEric Dumazet 					  const struct sock *sk,
2008318cf7aaSEric Dumazet 					  const struct sk_buff *skb);
2009cfb6eeb4SYOSHIFUJI Hideaki #endif
2010b40cf18eSEric Dumazet 	void (*init_req)(struct request_sock *req,
2011b40cf18eSEric Dumazet 			 const struct sock *sk_listener,
201216bea70aSOctavian Purdila 			 struct sk_buff *skb);
2013fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
20143f684b4bSEric Dumazet 	__u32 (*cookie_init_seq)(const struct sk_buff *skb,
2015fb7b37a7SOctavian Purdila 				 __u16 *mss);
2016fb7b37a7SOctavian Purdila #endif
2017f964629eSEric Dumazet 	struct dst_entry *(*route_req)(const struct sock *sk, struct flowi *fl,
20184396e461SSoheil Hassas Yeganeh 				       const struct request_sock *req);
201984b114b9SEric Dumazet 	u32 (*init_seq)(const struct sk_buff *skb);
20205d2ed052SEric Dumazet 	u32 (*init_ts_off)(const struct net *net, const struct sk_buff *skb);
20210f935dbeSEric Dumazet 	int (*send_synack)(const struct sock *sk, struct dst_entry *dst,
2022d6274bd8SOctavian Purdila 			   struct flowi *fl, struct request_sock *req,
2023dc6ef6beSEric Dumazet 			   struct tcp_fastopen_cookie *foc,
2024b3d05147SEric Dumazet 			   enum tcp_synack_type synack_type);
2025cfb6eeb4SYOSHIFUJI Hideaki };
2026cfb6eeb4SYOSHIFUJI Hideaki 
202735b2c321SMat Martineau extern const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops;
202835b2c321SMat Martineau #if IS_ENABLED(CONFIG_IPV6)
202935b2c321SMat Martineau extern const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops;
203035b2c321SMat Martineau #endif
203135b2c321SMat Martineau 
2032fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
2033fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
20343f684b4bSEric Dumazet 					 const struct sock *sk, struct sk_buff *skb,
2035fb7b37a7SOctavian Purdila 					 __u16 *mss)
2036fb7b37a7SOctavian Purdila {
20373f684b4bSEric Dumazet 	tcp_synq_overflow(sk);
203802a1d6e7SEric Dumazet 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
20393f684b4bSEric Dumazet 	return ops->cookie_init_seq(skb, mss);
2040fb7b37a7SOctavian Purdila }
2041fb7b37a7SOctavian Purdila #else
2042fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
20433f684b4bSEric Dumazet 					 const struct sock *sk, struct sk_buff *skb,
2044fb7b37a7SOctavian Purdila 					 __u16 *mss)
2045fb7b37a7SOctavian Purdila {
2046fb7b37a7SOctavian Purdila 	return 0;
2047fb7b37a7SOctavian Purdila }
2048fb7b37a7SOctavian Purdila #endif
2049fb7b37a7SOctavian Purdila 
20505c9f3023SJoe Perches int tcpv4_offload_init(void);
205128850dc7SDaniel Borkmann 
20525c9f3023SJoe Perches void tcp_v4_init(void);
20535c9f3023SJoe Perches void tcp_init(void);
205420380731SArnaldo Carvalho de Melo 
2055659a8ad5SYuchung Cheng /* tcp_recovery.c */
2056d716bfdbSYuchung Cheng void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb);
20576ac06ecdSYuchung Cheng void tcp_newreno_mark_lost(struct sock *sk, bool snd_una_advanced);
2058b8fef65aSYuchung Cheng extern s32 tcp_rack_skb_timeout(struct tcp_sock *tp, struct sk_buff *skb,
2059b8fef65aSYuchung Cheng 				u32 reo_wnd);
2060128eda86SEric Dumazet extern void tcp_rack_mark_lost(struct sock *sk);
20611d0833dfSYuchung Cheng extern void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq,
20629a568de4SEric Dumazet 			     u64 xmit_time);
206357dde7f7SYuchung Cheng extern void tcp_rack_reo_timeout(struct sock *sk);
20641f255691SPriyaranjan Jha extern void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs);
2065659a8ad5SYuchung Cheng 
2066e1a10ef7SNeal Cardwell /* At how many usecs into the future should the RTO fire? */
2067e1a10ef7SNeal Cardwell static inline s64 tcp_rto_delta_us(const struct sock *sk)
2068e1a10ef7SNeal Cardwell {
206975c119afSEric Dumazet 	const struct sk_buff *skb = tcp_rtx_queue_head(sk);
2070e1a10ef7SNeal Cardwell 	u32 rto = inet_csk(sk)->icsk_rto;
20712fd66ffbSEric Dumazet 	u64 rto_time_stamp_us = tcp_skb_timestamp_us(skb) + jiffies_to_usecs(rto);
2072e1a10ef7SNeal Cardwell 
2073e1a10ef7SNeal Cardwell 	return rto_time_stamp_us - tcp_sk(sk)->tcp_mstamp;
2074e1a10ef7SNeal Cardwell }
2075e1a10ef7SNeal Cardwell 
2076e25f866fSCong Wang /*
2077e25f866fSCong Wang  * Save and compile IPv4 options, return a pointer to it
2078e25f866fSCong Wang  */
207991ed1e66SPaolo Abeni static inline struct ip_options_rcu *tcp_v4_save_options(struct net *net,
208091ed1e66SPaolo Abeni 							 struct sk_buff *skb)
2081e25f866fSCong Wang {
2082e25f866fSCong Wang 	const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
2083e25f866fSCong Wang 	struct ip_options_rcu *dopt = NULL;
2084e25f866fSCong Wang 
2085461b74c3SCong Wang 	if (opt->optlen) {
2086e25f866fSCong Wang 		int opt_size = sizeof(*dopt) + opt->optlen;
2087e25f866fSCong Wang 
2088e25f866fSCong Wang 		dopt = kmalloc(opt_size, GFP_ATOMIC);
208991ed1e66SPaolo Abeni 		if (dopt && __ip_options_echo(net, &dopt->opt, skb, opt)) {
2090e25f866fSCong Wang 			kfree(dopt);
2091e25f866fSCong Wang 			dopt = NULL;
2092e25f866fSCong Wang 		}
2093e25f866fSCong Wang 	}
2094e25f866fSCong Wang 	return dopt;
2095e25f866fSCong Wang }
2096e25f866fSCong Wang 
209798781965SEric Dumazet /* locally generated TCP pure ACKs have skb->truesize == 2
209898781965SEric Dumazet  * (check tcp_send_ack() in net/ipv4/tcp_output.c )
209998781965SEric Dumazet  * This is much faster than dissecting the packet to find out.
210098781965SEric Dumazet  * (Think of GRE encapsulations, IPv4, IPv6, ...)
210198781965SEric Dumazet  */
210298781965SEric Dumazet static inline bool skb_is_tcp_pure_ack(const struct sk_buff *skb)
210398781965SEric Dumazet {
210498781965SEric Dumazet 	return skb->truesize == 2;
210598781965SEric Dumazet }
210698781965SEric Dumazet 
210798781965SEric Dumazet static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
210898781965SEric Dumazet {
210998781965SEric Dumazet 	skb->truesize = 2;
211098781965SEric Dumazet }
211198781965SEric Dumazet 
2112473bd239STom Herbert static inline int tcp_inq(struct sock *sk)
2113473bd239STom Herbert {
2114473bd239STom Herbert 	struct tcp_sock *tp = tcp_sk(sk);
2115473bd239STom Herbert 	int answ;
2116473bd239STom Herbert 
2117473bd239STom Herbert 	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
2118473bd239STom Herbert 		answ = 0;
2119473bd239STom Herbert 	} else if (sock_flag(sk, SOCK_URGINLINE) ||
2120473bd239STom Herbert 		   !tp->urg_data ||
2121473bd239STom Herbert 		   before(tp->urg_seq, tp->copied_seq) ||
2122473bd239STom Herbert 		   !before(tp->urg_seq, tp->rcv_nxt)) {
2123473bd239STom Herbert 
2124473bd239STom Herbert 		answ = tp->rcv_nxt - tp->copied_seq;
2125473bd239STom Herbert 
2126473bd239STom Herbert 		/* Subtract 1, if FIN was received */
2127473bd239STom Herbert 		if (answ && sock_flag(sk, SOCK_DONE))
2128473bd239STom Herbert 			answ--;
2129473bd239STom Herbert 	} else {
2130473bd239STom Herbert 		answ = tp->urg_seq - tp->copied_seq;
2131473bd239STom Herbert 	}
2132473bd239STom Herbert 
2133473bd239STom Herbert 	return answ;
2134473bd239STom Herbert }
2135473bd239STom Herbert 
213632035585STom Herbert int tcp_peek_len(struct socket *sock);
213732035585STom Herbert 
2138a44d6eacSMartin KaFai Lau static inline void tcp_segs_in(struct tcp_sock *tp, const struct sk_buff *skb)
2139a44d6eacSMartin KaFai Lau {
2140a44d6eacSMartin KaFai Lau 	u16 segs_in;
2141a44d6eacSMartin KaFai Lau 
2142a44d6eacSMartin KaFai Lau 	segs_in = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
2143a44d6eacSMartin KaFai Lau 	tp->segs_in += segs_in;
2144a44d6eacSMartin KaFai Lau 	if (skb->len > tcp_hdrlen(skb))
2145a44d6eacSMartin KaFai Lau 		tp->data_segs_in += segs_in;
2146a44d6eacSMartin KaFai Lau }
2147a44d6eacSMartin KaFai Lau 
21489caad864SEric Dumazet /*
21499caad864SEric Dumazet  * TCP listen path runs lockless.
21509caad864SEric Dumazet  * We forced "struct sock" to be const qualified to make sure
21519caad864SEric Dumazet  * we don't modify one of its field by mistake.
21529caad864SEric Dumazet  * Here, we increment sk_drops which is an atomic_t, so we can safely
21539caad864SEric Dumazet  * make sock writable again.
21549caad864SEric Dumazet  */
21559caad864SEric Dumazet static inline void tcp_listendrop(const struct sock *sk)
21569caad864SEric Dumazet {
21579caad864SEric Dumazet 	atomic_inc(&((struct sock *)sk)->sk_drops);
215802a1d6e7SEric Dumazet 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
21599caad864SEric Dumazet }
21609caad864SEric Dumazet 
2161218af599SEric Dumazet enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer);
2162218af599SEric Dumazet 
2163734942ccSDave Watson /*
2164734942ccSDave Watson  * Interface for adding Upper Level Protocols over TCP
2165734942ccSDave Watson  */
2166734942ccSDave Watson 
2167734942ccSDave Watson #define TCP_ULP_NAME_MAX	16
2168734942ccSDave Watson #define TCP_ULP_MAX		128
2169734942ccSDave Watson #define TCP_ULP_BUF_MAX		(TCP_ULP_NAME_MAX*TCP_ULP_MAX)
2170734942ccSDave Watson 
2171734942ccSDave Watson struct tcp_ulp_ops {
2172734942ccSDave Watson 	struct list_head	list;
2173734942ccSDave Watson 
2174734942ccSDave Watson 	/* initialize ulp */
2175734942ccSDave Watson 	int (*init)(struct sock *sk);
217695fa1454SJohn Fastabend 	/* update ulp */
217733bfe20dSJohn Fastabend 	void (*update)(struct sock *sk, struct proto *p,
217833bfe20dSJohn Fastabend 		       void (*write_space)(struct sock *sk));
2179734942ccSDave Watson 	/* cleanup ulp */
2180734942ccSDave Watson 	void (*release)(struct sock *sk);
218161723b39SDavide Caratti 	/* diagnostic */
218261723b39SDavide Caratti 	int (*get_info)(const struct sock *sk, struct sk_buff *skb);
218361723b39SDavide Caratti 	size_t (*get_info_size)(const struct sock *sk);
218413230593SMat Martineau 	/* clone ulp */
218513230593SMat Martineau 	void (*clone)(const struct request_sock *req, struct sock *newsk,
218613230593SMat Martineau 		      const gfp_t priority);
2187734942ccSDave Watson 
2188734942ccSDave Watson 	char		name[TCP_ULP_NAME_MAX];
2189734942ccSDave Watson 	struct module	*owner;
2190734942ccSDave Watson };
2191734942ccSDave Watson int tcp_register_ulp(struct tcp_ulp_ops *type);
2192734942ccSDave Watson void tcp_unregister_ulp(struct tcp_ulp_ops *type);
2193734942ccSDave Watson int tcp_set_ulp(struct sock *sk, const char *name);
2194734942ccSDave Watson void tcp_get_available_ulp(char *buf, size_t len);
2195734942ccSDave Watson void tcp_cleanup_ulp(struct sock *sk);
219633bfe20dSJohn Fastabend void tcp_update_ulp(struct sock *sk, struct proto *p,
219733bfe20dSJohn Fastabend 		    void (*write_space)(struct sock *sk));
2198734942ccSDave Watson 
2199037b0b86SDaniel Borkmann #define MODULE_ALIAS_TCP_ULP(name)				\
2200037b0b86SDaniel Borkmann 	__MODULE_INFO(alias, alias_userspace, name);		\
2201037b0b86SDaniel Borkmann 	__MODULE_INFO(alias, alias_tcp_ulp, "tcp-ulp-" name)
2202037b0b86SDaniel Borkmann 
2203604326b4SDaniel Borkmann struct sk_msg;
2204604326b4SDaniel Borkmann struct sk_psock;
2205604326b4SDaniel Borkmann 
2206f747632bSLorenz Bauer #ifdef CONFIG_BPF_STREAM_PARSER
2207f747632bSLorenz Bauer struct proto *tcp_bpf_get_proto(struct sock *sk, struct sk_psock *psock);
2208f747632bSLorenz Bauer void tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
2209f747632bSLorenz Bauer #else
2210f747632bSLorenz Bauer static inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
2211f747632bSLorenz Bauer {
2212f747632bSLorenz Bauer }
2213f747632bSLorenz Bauer #endif /* CONFIG_BPF_STREAM_PARSER */
2214f747632bSLorenz Bauer 
22155da00404SLorenz Bauer #ifdef CONFIG_NET_SOCK_MSG
2216604326b4SDaniel Borkmann int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg, u32 bytes,
2217604326b4SDaniel Borkmann 			  int flags);
2218604326b4SDaniel Borkmann int __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
221902c558b2SJohn Fastabend 		      struct msghdr *msg, int len, int flags);
22205da00404SLorenz Bauer #endif /* CONFIG_NET_SOCK_MSG */
2221604326b4SDaniel Borkmann 
222240304b2aSLawrence Brakmo /* Call BPF_SOCK_OPS program that returns an int. If the return value
222340304b2aSLawrence Brakmo  * is < 0, then the BPF op failed (for example if the loaded BPF
222440304b2aSLawrence Brakmo  * program does not support the chosen operation or there is no BPF
222540304b2aSLawrence Brakmo  * program loaded).
222640304b2aSLawrence Brakmo  */
222740304b2aSLawrence Brakmo #ifdef CONFIG_BPF
2228de525be2SLawrence Brakmo static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
222940304b2aSLawrence Brakmo {
223040304b2aSLawrence Brakmo 	struct bpf_sock_ops_kern sock_ops;
223140304b2aSLawrence Brakmo 	int ret;
223240304b2aSLawrence Brakmo 
2233b73042b8SLawrence Brakmo 	memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
2234f19397a5SLawrence Brakmo 	if (sk_fullsock(sk)) {
2235f19397a5SLawrence Brakmo 		sock_ops.is_fullsock = 1;
223640304b2aSLawrence Brakmo 		sock_owned_by_me(sk);
2237f19397a5SLawrence Brakmo 	}
223840304b2aSLawrence Brakmo 
223940304b2aSLawrence Brakmo 	sock_ops.sk = sk;
224040304b2aSLawrence Brakmo 	sock_ops.op = op;
2241de525be2SLawrence Brakmo 	if (nargs > 0)
2242de525be2SLawrence Brakmo 		memcpy(sock_ops.args, args, nargs * sizeof(*args));
224340304b2aSLawrence Brakmo 
224440304b2aSLawrence Brakmo 	ret = BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
224540304b2aSLawrence Brakmo 	if (ret == 0)
224640304b2aSLawrence Brakmo 		ret = sock_ops.reply;
224740304b2aSLawrence Brakmo 	else
224840304b2aSLawrence Brakmo 		ret = -1;
224940304b2aSLawrence Brakmo 	return ret;
225040304b2aSLawrence Brakmo }
2251de525be2SLawrence Brakmo 
2252de525be2SLawrence Brakmo static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
2253de525be2SLawrence Brakmo {
2254de525be2SLawrence Brakmo 	u32 args[2] = {arg1, arg2};
2255de525be2SLawrence Brakmo 
2256de525be2SLawrence Brakmo 	return tcp_call_bpf(sk, op, 2, args);
2257de525be2SLawrence Brakmo }
2258de525be2SLawrence Brakmo 
2259de525be2SLawrence Brakmo static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
2260de525be2SLawrence Brakmo 				    u32 arg3)
2261de525be2SLawrence Brakmo {
2262de525be2SLawrence Brakmo 	u32 args[3] = {arg1, arg2, arg3};
2263de525be2SLawrence Brakmo 
2264de525be2SLawrence Brakmo 	return tcp_call_bpf(sk, op, 3, args);
2265de525be2SLawrence Brakmo }
2266de525be2SLawrence Brakmo 
226740304b2aSLawrence Brakmo #else
2268de525be2SLawrence Brakmo static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
226940304b2aSLawrence Brakmo {
227040304b2aSLawrence Brakmo 	return -EPERM;
227140304b2aSLawrence Brakmo }
2272de525be2SLawrence Brakmo 
2273de525be2SLawrence Brakmo static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
2274de525be2SLawrence Brakmo {
2275de525be2SLawrence Brakmo 	return -EPERM;
2276de525be2SLawrence Brakmo }
2277de525be2SLawrence Brakmo 
2278de525be2SLawrence Brakmo static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
2279de525be2SLawrence Brakmo 				    u32 arg3)
2280de525be2SLawrence Brakmo {
2281de525be2SLawrence Brakmo 	return -EPERM;
2282de525be2SLawrence Brakmo }
2283de525be2SLawrence Brakmo 
228440304b2aSLawrence Brakmo #endif
228540304b2aSLawrence Brakmo 
22868550f328SLawrence Brakmo static inline u32 tcp_timeout_init(struct sock *sk)
22878550f328SLawrence Brakmo {
22888550f328SLawrence Brakmo 	int timeout;
22898550f328SLawrence Brakmo 
2290de525be2SLawrence Brakmo 	timeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL);
22918550f328SLawrence Brakmo 
22928550f328SLawrence Brakmo 	if (timeout <= 0)
22938550f328SLawrence Brakmo 		timeout = TCP_TIMEOUT_INIT;
22948550f328SLawrence Brakmo 	return timeout;
22958550f328SLawrence Brakmo }
22968550f328SLawrence Brakmo 
229713d3b1ebSLawrence Brakmo static inline u32 tcp_rwnd_init_bpf(struct sock *sk)
229813d3b1ebSLawrence Brakmo {
229913d3b1ebSLawrence Brakmo 	int rwnd;
230013d3b1ebSLawrence Brakmo 
2301de525be2SLawrence Brakmo 	rwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL);
230213d3b1ebSLawrence Brakmo 
230313d3b1ebSLawrence Brakmo 	if (rwnd < 0)
230413d3b1ebSLawrence Brakmo 		rwnd = 0;
230513d3b1ebSLawrence Brakmo 	return rwnd;
230613d3b1ebSLawrence Brakmo }
230791b5b21cSLawrence Brakmo 
230891b5b21cSLawrence Brakmo static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk)
230991b5b21cSLawrence Brakmo {
2310de525be2SLawrence Brakmo 	return (tcp_call_bpf(sk, BPF_SOCK_OPS_NEEDS_ECN, 0, NULL) == 1);
231191b5b21cSLawrence Brakmo }
231260e2a778SUrsula Braun 
231323729ff2SStanislav Fomichev static inline void tcp_bpf_rtt(struct sock *sk)
231423729ff2SStanislav Fomichev {
2315bef8e263SArnd Bergmann 	if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_RTT_CB_FLAG))
231623729ff2SStanislav Fomichev 		tcp_call_bpf(sk, BPF_SOCK_OPS_RTT_CB, 0, NULL);
231723729ff2SStanislav Fomichev }
231823729ff2SStanislav Fomichev 
231960e2a778SUrsula Braun #if IS_ENABLED(CONFIG_SMC)
232060e2a778SUrsula Braun extern struct static_key_false tcp_have_smc;
232160e2a778SUrsula Braun #endif
23226dac1523SIlya Lesokhin 
23236dac1523SIlya Lesokhin #if IS_ENABLED(CONFIG_TLS_DEVICE)
23246dac1523SIlya Lesokhin void clean_acked_data_enable(struct inet_connection_sock *icsk,
23256dac1523SIlya Lesokhin 			     void (*cad)(struct sock *sk, u32 ack_seq));
23266dac1523SIlya Lesokhin void clean_acked_data_disable(struct inet_connection_sock *icsk);
2327494bc1d2SJakub Kicinski void clean_acked_data_flush(void);
23286dac1523SIlya Lesokhin #endif
23296dac1523SIlya Lesokhin 
2330a842fe14SEric Dumazet DECLARE_STATIC_KEY_FALSE(tcp_tx_delay_enabled);
2331a842fe14SEric Dumazet static inline void tcp_add_tx_delay(struct sk_buff *skb,
2332a842fe14SEric Dumazet 				    const struct tcp_sock *tp)
2333a842fe14SEric Dumazet {
2334a842fe14SEric Dumazet 	if (static_branch_unlikely(&tcp_tx_delay_enabled))
2335a842fe14SEric Dumazet 		skb->skb_mstamp_ns += (u64)tp->tcp_tx_delay * NSEC_PER_USEC;
2336a842fe14SEric Dumazet }
2337a842fe14SEric Dumazet 
2338d6fb396cSEric Dumazet /* Compute Earliest Departure Time for some control packets
2339d6fb396cSEric Dumazet  * like ACK or RST for TIME_WAIT or non ESTABLISHED sockets.
2340d6fb396cSEric Dumazet  */
2341d6fb396cSEric Dumazet static inline u64 tcp_transmit_time(const struct sock *sk)
2342a842fe14SEric Dumazet {
2343a842fe14SEric Dumazet 	if (static_branch_unlikely(&tcp_tx_delay_enabled)) {
2344a842fe14SEric Dumazet 		u32 delay = (sk->sk_state == TCP_TIME_WAIT) ?
2345a842fe14SEric Dumazet 			tcp_twsk(sk)->tw_tx_delay : tcp_sk(sk)->tcp_tx_delay;
2346a842fe14SEric Dumazet 
2347d6fb396cSEric Dumazet 		return tcp_clock_ns() + (u64)delay * NSEC_PER_USEC;
2348a842fe14SEric Dumazet 	}
2349d6fb396cSEric Dumazet 	return 0;
2350a842fe14SEric Dumazet }
2351a842fe14SEric Dumazet 
23521da177e4SLinus Torvalds #endif	/* _TCP_H */
2353