xref: /kernel/linux/linux-5.10/include/net/tcp.h (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * INET		An implementation of the TCP/IP protocol suite for the LINUX
48c2ecf20Sopenharmony_ci *		operating system.  INET is implemented using the  BSD Socket
58c2ecf20Sopenharmony_ci *		interface as the means of communication with the user level.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *		Definitions for the TCP module.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Version:	@(#)tcp.h	1.0.5	05/23/93
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Authors:	Ross Biro
128c2ecf20Sopenharmony_ci *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci#ifndef _TCP_H
158c2ecf20Sopenharmony_ci#define _TCP_H
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define FASTRETRANS_DEBUG 1
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/list.h>
208c2ecf20Sopenharmony_ci#include <linux/tcp.h>
218c2ecf20Sopenharmony_ci#include <linux/bug.h>
228c2ecf20Sopenharmony_ci#include <linux/slab.h>
238c2ecf20Sopenharmony_ci#include <linux/cache.h>
248c2ecf20Sopenharmony_ci#include <linux/percpu.h>
258c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
268c2ecf20Sopenharmony_ci#include <linux/kref.h>
278c2ecf20Sopenharmony_ci#include <linux/ktime.h>
288c2ecf20Sopenharmony_ci#include <linux/indirect_call_wrapper.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <net/inet_connection_sock.h>
318c2ecf20Sopenharmony_ci#include <net/inet_timewait_sock.h>
328c2ecf20Sopenharmony_ci#include <net/inet_hashtables.h>
338c2ecf20Sopenharmony_ci#include <net/checksum.h>
348c2ecf20Sopenharmony_ci#include <net/request_sock.h>
358c2ecf20Sopenharmony_ci#include <net/sock_reuseport.h>
368c2ecf20Sopenharmony_ci#include <net/sock.h>
378c2ecf20Sopenharmony_ci#include <net/snmp.h>
388c2ecf20Sopenharmony_ci#include <net/ip.h>
398c2ecf20Sopenharmony_ci#include <net/tcp_states.h>
408c2ecf20Sopenharmony_ci#include <net/inet_ecn.h>
418c2ecf20Sopenharmony_ci#include <net/dst.h>
428c2ecf20Sopenharmony_ci#include <net/mptcp.h>
438c2ecf20Sopenharmony_ci#ifdef CONFIG_NEWIP
448c2ecf20Sopenharmony_ci#include <linux/nip.h> /* NIP */
458c2ecf20Sopenharmony_ci#endif
468c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
478c2ecf20Sopenharmony_ci#include <linux/memcontrol.h>
488c2ecf20Sopenharmony_ci#include <linux/bpf-cgroup.h>
498c2ecf20Sopenharmony_ci#include <linux/siphash.h>
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciextern struct inet_hashinfo tcp_hashinfo;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciDECLARE_PER_CPU(unsigned int, tcp_orphan_count);
548c2ecf20Sopenharmony_ciint tcp_orphan_count_sum(void);
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_civoid tcp_time_wait(struct sock *sk, int state, int timeo);
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define MAX_TCP_HEADER	L1_CACHE_ALIGN(128 + MAX_HEADER)
598c2ecf20Sopenharmony_ci#define MAX_TCP_OPTION_SPACE 40
608c2ecf20Sopenharmony_ci#define TCP_MIN_SND_MSS		48
618c2ecf20Sopenharmony_ci#define TCP_MIN_GSO_SIZE	(TCP_MIN_SND_MSS - MAX_TCP_OPTION_SPACE)
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci/*
648c2ecf20Sopenharmony_ci * Never offer a window over 32767 without using window scaling. Some
658c2ecf20Sopenharmony_ci * poor stacks do signed 16bit maths!
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_ci#define MAX_TCP_WINDOW		32767U
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* Minimal accepted MSS. It is (60+60+8) - (20+20). */
708c2ecf20Sopenharmony_ci#define TCP_MIN_MSS		88U
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/* The initial MTU to use for probing */
738c2ecf20Sopenharmony_ci#define TCP_BASE_MSS		1024
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* probing interval, default to 10 minutes as per RFC4821 */
768c2ecf20Sopenharmony_ci#define TCP_PROBE_INTERVAL	600
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/* Specify interval when tcp mtu probing will stop */
798c2ecf20Sopenharmony_ci#define TCP_PROBE_THRESHOLD	8
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/* After receiving this amount of duplicate ACKs fast retransmit starts. */
828c2ecf20Sopenharmony_ci#define TCP_FASTRETRANS_THRESH 3
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* Maximal number of ACKs sent quickly to accelerate slow-start. */
858c2ecf20Sopenharmony_ci#define TCP_MAX_QUICKACKS	16U
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* Maximal number of window scale according to RFC1323 */
888c2ecf20Sopenharmony_ci#define TCP_MAX_WSCALE		14U
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/* urg_data states */
918c2ecf20Sopenharmony_ci#define TCP_URG_VALID	0x0100
928c2ecf20Sopenharmony_ci#define TCP_URG_NOTYET	0x0200
938c2ecf20Sopenharmony_ci#define TCP_URG_READ	0x0400
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define TCP_RETR1	3	/*
968c2ecf20Sopenharmony_ci				 * This is how many retries it does before it
978c2ecf20Sopenharmony_ci				 * tries to figure out if the gateway is
988c2ecf20Sopenharmony_ci				 * down. Minimal RFC value is 3; it corresponds
998c2ecf20Sopenharmony_ci				 * to ~3sec-8min depending on RTO.
1008c2ecf20Sopenharmony_ci				 */
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#define TCP_RETR2	15	/*
1038c2ecf20Sopenharmony_ci				 * This should take at least
1048c2ecf20Sopenharmony_ci				 * 90 minutes to time out.
1058c2ecf20Sopenharmony_ci				 * RFC1122 says that the limit is 100 sec.
1068c2ecf20Sopenharmony_ci				 * 15 is ~13-30min depending on RTO.
1078c2ecf20Sopenharmony_ci				 */
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define TCP_SYN_RETRIES	 6	/* This is how many retries are done
1108c2ecf20Sopenharmony_ci				 * when active opening a connection.
1118c2ecf20Sopenharmony_ci				 * RFC1122 says the minimum retry MUST
1128c2ecf20Sopenharmony_ci				 * be at least 180secs.  Nevertheless
1138c2ecf20Sopenharmony_ci				 * this value is corresponding to
1148c2ecf20Sopenharmony_ci				 * 63secs of retransmission with the
1158c2ecf20Sopenharmony_ci				 * current initial RTO.
1168c2ecf20Sopenharmony_ci				 */
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define TCP_SYNACK_RETRIES 5	/* This is how may retries are done
1198c2ecf20Sopenharmony_ci				 * when passive opening a connection.
1208c2ecf20Sopenharmony_ci				 * This is corresponding to 31secs of
1218c2ecf20Sopenharmony_ci				 * retransmission with the current
1228c2ecf20Sopenharmony_ci				 * initial RTO.
1238c2ecf20Sopenharmony_ci				 */
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1268c2ecf20Sopenharmony_ci				  * state, about 60 seconds	*/
1278c2ecf20Sopenharmony_ci#define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1288c2ecf20Sopenharmony_ci                                 /* BSD style FIN_WAIT2 deadlock breaker.
1298c2ecf20Sopenharmony_ci				  * It used to be 3min, new value is 60sec,
1308c2ecf20Sopenharmony_ci				  * to combine FIN-WAIT-2 timeout with
1318c2ecf20Sopenharmony_ci				  * TIME-WAIT timer.
1328c2ecf20Sopenharmony_ci				  */
1338c2ecf20Sopenharmony_ci#define TCP_FIN_TIMEOUT_MAX (120 * HZ) /* max TCP_LINGER2 value (two minutes) */
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci#define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1368c2ecf20Sopenharmony_ci#if HZ >= 100
1378c2ecf20Sopenharmony_ci#define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1388c2ecf20Sopenharmony_ci#define TCP_ATO_MIN	((unsigned)(HZ/25))
1398c2ecf20Sopenharmony_ci#else
1408c2ecf20Sopenharmony_ci#define TCP_DELACK_MIN	4U
1418c2ecf20Sopenharmony_ci#define TCP_ATO_MIN	4U
1428c2ecf20Sopenharmony_ci#endif
1438c2ecf20Sopenharmony_ci#define TCP_RTO_MAX	((unsigned)(120*HZ))
1448c2ecf20Sopenharmony_ci#define TCP_RTO_MIN	((unsigned)(HZ/5))
1458c2ecf20Sopenharmony_ci#define TCP_TIMEOUT_MIN	(2U) /* Min timeout for TCP timers in jiffies */
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci#define TCP_TIMEOUT_MIN_US (2*USEC_PER_MSEC) /* Min TCP timeout in microsecs */
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci#define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))	/* RFC6298 2.1 initial RTO value	*/
1508c2ecf20Sopenharmony_ci#define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value, now
1518c2ecf20Sopenharmony_ci						 * used as a fallback RTO for the
1528c2ecf20Sopenharmony_ci						 * initial data transmission if no
1538c2ecf20Sopenharmony_ci						 * valid RTT sample has been acquired,
1548c2ecf20Sopenharmony_ci						 * most likely due to retrans in 3WHS.
1558c2ecf20Sopenharmony_ci						 */
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci#define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1588c2ecf20Sopenharmony_ci					                 * for local resources.
1598c2ecf20Sopenharmony_ci					                 */
1608c2ecf20Sopenharmony_ci#define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1618c2ecf20Sopenharmony_ci#define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1628c2ecf20Sopenharmony_ci#define TCP_KEEPALIVE_INTVL	(75*HZ)
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#define MAX_TCP_KEEPIDLE	32767
1658c2ecf20Sopenharmony_ci#define MAX_TCP_KEEPINTVL	32767
1668c2ecf20Sopenharmony_ci#define MAX_TCP_KEEPCNT		127
1678c2ecf20Sopenharmony_ci#define MAX_TCP_SYNCNT		127
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci#define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci#define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1728c2ecf20Sopenharmony_ci#define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1738c2ecf20Sopenharmony_ci					 * after this time. It should be equal
1748c2ecf20Sopenharmony_ci					 * (or greater than) TCP_TIMEWAIT_LEN
1758c2ecf20Sopenharmony_ci					 * to provide reliability equal to one
1768c2ecf20Sopenharmony_ci					 * provided by timewait state.
1778c2ecf20Sopenharmony_ci					 */
1788c2ecf20Sopenharmony_ci#define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1798c2ecf20Sopenharmony_ci					 * timestamps. It must be less than
1808c2ecf20Sopenharmony_ci					 * minimal timewait lifetime.
1818c2ecf20Sopenharmony_ci					 */
1828c2ecf20Sopenharmony_ci/*
1838c2ecf20Sopenharmony_ci *	TCP option
1848c2ecf20Sopenharmony_ci */
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci#define TCPOPT_NOP		1	/* Padding */
1878c2ecf20Sopenharmony_ci#define TCPOPT_EOL		0	/* End of options */
1888c2ecf20Sopenharmony_ci#define TCPOPT_MSS		2	/* Segment size negotiating */
1898c2ecf20Sopenharmony_ci#define TCPOPT_WINDOW		3	/* Window scaling */
1908c2ecf20Sopenharmony_ci#define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1918c2ecf20Sopenharmony_ci#define TCPOPT_SACK             5       /* SACK Block */
1928c2ecf20Sopenharmony_ci#define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
1938c2ecf20Sopenharmony_ci#define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
1948c2ecf20Sopenharmony_ci#define TCPOPT_MPTCP		30	/* Multipath TCP (RFC6824) */
1958c2ecf20Sopenharmony_ci#define TCPOPT_FASTOPEN		34	/* Fast open (RFC7413) */
1968c2ecf20Sopenharmony_ci#define TCPOPT_EXP		254	/* Experimental */
1978c2ecf20Sopenharmony_ci/* Magic number to be after the option value for sharing TCP
1988c2ecf20Sopenharmony_ci * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_ci#define TCPOPT_FASTOPEN_MAGIC	0xF989
2018c2ecf20Sopenharmony_ci#define TCPOPT_SMC_MAGIC	0xE2D4C3D9
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci/*
2048c2ecf20Sopenharmony_ci *     TCP option lengths
2058c2ecf20Sopenharmony_ci */
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci#define TCPOLEN_MSS            4
2088c2ecf20Sopenharmony_ci#define TCPOLEN_WINDOW         3
2098c2ecf20Sopenharmony_ci#define TCPOLEN_SACK_PERM      2
2108c2ecf20Sopenharmony_ci#define TCPOLEN_TIMESTAMP      10
2118c2ecf20Sopenharmony_ci#define TCPOLEN_MD5SIG         18
2128c2ecf20Sopenharmony_ci#define TCPOLEN_FASTOPEN_BASE  2
2138c2ecf20Sopenharmony_ci#define TCPOLEN_EXP_FASTOPEN_BASE  4
2148c2ecf20Sopenharmony_ci#define TCPOLEN_EXP_SMC_BASE   6
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci/* But this is what stacks really send out. */
2178c2ecf20Sopenharmony_ci#define TCPOLEN_TSTAMP_ALIGNED		12
2188c2ecf20Sopenharmony_ci#define TCPOLEN_WSCALE_ALIGNED		4
2198c2ecf20Sopenharmony_ci#define TCPOLEN_SACKPERM_ALIGNED	4
2208c2ecf20Sopenharmony_ci#define TCPOLEN_SACK_BASE		2
2218c2ecf20Sopenharmony_ci#define TCPOLEN_SACK_BASE_ALIGNED	4
2228c2ecf20Sopenharmony_ci#define TCPOLEN_SACK_PERBLOCK		8
2238c2ecf20Sopenharmony_ci#define TCPOLEN_MD5SIG_ALIGNED		20
2248c2ecf20Sopenharmony_ci#define TCPOLEN_MSS_ALIGNED		4
2258c2ecf20Sopenharmony_ci#define TCPOLEN_EXP_SMC_BASE_ALIGNED	8
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci/* Flags in tp->nonagle */
2288c2ecf20Sopenharmony_ci#define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
2298c2ecf20Sopenharmony_ci#define TCP_NAGLE_CORK		2	/* Socket is corked	    */
2308c2ecf20Sopenharmony_ci#define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci/* TCP thin-stream limits */
2338c2ecf20Sopenharmony_ci#define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci/* TCP initial congestion window as per rfc6928 */
2368c2ecf20Sopenharmony_ci#define TCP_INIT_CWND		10
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci/* Bit Flags for sysctl_tcp_fastopen */
2398c2ecf20Sopenharmony_ci#define	TFO_CLIENT_ENABLE	1
2408c2ecf20Sopenharmony_ci#define	TFO_SERVER_ENABLE	2
2418c2ecf20Sopenharmony_ci#define	TFO_CLIENT_NO_COOKIE	4	/* Data in SYN w/o cookie option */
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci/* Accept SYN data w/o any cookie option */
2448c2ecf20Sopenharmony_ci#define	TFO_SERVER_COOKIE_NOT_REQD	0x200
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/* Force enable TFO on all listeners, i.e., not requiring the
2478c2ecf20Sopenharmony_ci * TCP_FASTOPEN socket option.
2488c2ecf20Sopenharmony_ci */
2498c2ecf20Sopenharmony_ci#define	TFO_SERVER_WO_SOCKOPT1	0x400
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci/* sysctl variables for tcp */
2538c2ecf20Sopenharmony_ciextern int sysctl_tcp_max_orphans;
2548c2ecf20Sopenharmony_ciextern long sysctl_tcp_mem[3];
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci#define TCP_RACK_LOSS_DETECTION  0x1 /* Use RACK to detect losses */
2578c2ecf20Sopenharmony_ci#define TCP_RACK_STATIC_REO_WND  0x2 /* Use static RACK reo wnd */
2588c2ecf20Sopenharmony_ci#define TCP_RACK_NO_DUPTHRESH    0x4 /* Do not use DUPACK threshold in RACK */
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ciextern atomic_long_t tcp_memory_allocated;
2618c2ecf20Sopenharmony_ciextern struct percpu_counter tcp_sockets_allocated;
2628c2ecf20Sopenharmony_ciextern unsigned long tcp_memory_pressure;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci/* optimized version of sk_under_memory_pressure() for TCP sockets */
2658c2ecf20Sopenharmony_cistatic inline bool tcp_under_memory_pressure(const struct sock *sk)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	if (mem_cgroup_sockets_enabled && sk->sk_memcg &&
2688c2ecf20Sopenharmony_ci	    mem_cgroup_under_socket_pressure(sk->sk_memcg))
2698c2ecf20Sopenharmony_ci		return true;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	return READ_ONCE(tcp_memory_pressure);
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci/*
2748c2ecf20Sopenharmony_ci * The next routines deal with comparing 32 bit unsigned ints
2758c2ecf20Sopenharmony_ci * and worry about wraparound (automatic with unsigned arithmetic).
2768c2ecf20Sopenharmony_ci */
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_cistatic inline bool before(__u32 seq1, __u32 seq2)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci        return (__s32)(seq1-seq2) < 0;
2818c2ecf20Sopenharmony_ci}
2828c2ecf20Sopenharmony_ci#define after(seq2, seq1) 	before(seq1, seq2)
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci/* is s2<=s1<=s3 ? */
2858c2ecf20Sopenharmony_cistatic inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	return seq3 - seq2 >= seq1 - seq2;
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistatic inline bool tcp_out_of_memory(struct sock *sk)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
2938c2ecf20Sopenharmony_ci	    sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
2948c2ecf20Sopenharmony_ci		return true;
2958c2ecf20Sopenharmony_ci	return false;
2968c2ecf20Sopenharmony_ci}
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_civoid sk_forced_mem_schedule(struct sock *sk, int size);
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cibool tcp_check_oom(struct sock *sk, int shift);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ciextern struct proto tcp_prot;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci#define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
3068c2ecf20Sopenharmony_ci#define __TCP_INC_STATS(net, field)	__SNMP_INC_STATS((net)->mib.tcp_statistics, field)
3078c2ecf20Sopenharmony_ci#define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
3088c2ecf20Sopenharmony_ci#define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_civoid tcp_tasklet_init(void);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ciint tcp_v4_err(struct sk_buff *skb, u32);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_civoid tcp_shutdown(struct sock *sk, int how);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ciint tcp_v4_early_demux(struct sk_buff *skb);
3178c2ecf20Sopenharmony_ciint tcp_v4_rcv(struct sk_buff *skb);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ciint tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
3208c2ecf20Sopenharmony_ciint tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
3218c2ecf20Sopenharmony_ciint tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size);
3228c2ecf20Sopenharmony_ciint tcp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
3238c2ecf20Sopenharmony_ci		 int flags);
3248c2ecf20Sopenharmony_ciint tcp_sendpage_locked(struct sock *sk, struct page *page, int offset,
3258c2ecf20Sopenharmony_ci			size_t size, int flags);
3268c2ecf20Sopenharmony_cissize_t do_tcp_sendpages(struct sock *sk, struct page *page, int offset,
3278c2ecf20Sopenharmony_ci		 size_t size, int flags);
3288c2ecf20Sopenharmony_ciint tcp_send_mss(struct sock *sk, int *size_goal, int flags);
3298c2ecf20Sopenharmony_civoid tcp_push(struct sock *sk, int flags, int mss_now, int nonagle,
3308c2ecf20Sopenharmony_ci	      int size_goal);
3318c2ecf20Sopenharmony_civoid tcp_release_cb(struct sock *sk);
3328c2ecf20Sopenharmony_civoid tcp_wfree(struct sk_buff *skb);
3338c2ecf20Sopenharmony_civoid tcp_write_timer_handler(struct sock *sk);
3348c2ecf20Sopenharmony_civoid tcp_delack_timer_handler(struct sock *sk);
3358c2ecf20Sopenharmony_ciint tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
3368c2ecf20Sopenharmony_ciint tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb);
3378c2ecf20Sopenharmony_civoid tcp_rcv_established(struct sock *sk, struct sk_buff *skb);
3388c2ecf20Sopenharmony_civoid tcp_rcv_space_adjust(struct sock *sk);
3398c2ecf20Sopenharmony_ciint tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
3408c2ecf20Sopenharmony_civoid tcp_twsk_destructor(struct sock *sk);
3418c2ecf20Sopenharmony_cissize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
3428c2ecf20Sopenharmony_ci			struct pipe_inode_info *pipe, size_t len,
3438c2ecf20Sopenharmony_ci			unsigned int flags);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic inline void tcp_dec_quickack_mode(struct sock *sk)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	struct inet_connection_sock *icsk = inet_csk(sk);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	if (icsk->icsk_ack.quick) {
3508c2ecf20Sopenharmony_ci		/* How many ACKs S/ACKing new data have we sent? */
3518c2ecf20Sopenharmony_ci		const unsigned int pkts = inet_csk_ack_scheduled(sk) ? 1 : 0;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci		if (pkts >= icsk->icsk_ack.quick) {
3548c2ecf20Sopenharmony_ci			icsk->icsk_ack.quick = 0;
3558c2ecf20Sopenharmony_ci			/* Leaving quickack mode we deflate ATO. */
3568c2ecf20Sopenharmony_ci			icsk->icsk_ack.ato   = TCP_ATO_MIN;
3578c2ecf20Sopenharmony_ci		} else
3588c2ecf20Sopenharmony_ci			icsk->icsk_ack.quick -= pkts;
3598c2ecf20Sopenharmony_ci	}
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci#define	TCP_ECN_OK		1
3638c2ecf20Sopenharmony_ci#define	TCP_ECN_QUEUE_CWR	2
3648c2ecf20Sopenharmony_ci#define	TCP_ECN_DEMAND_CWR	4
3658c2ecf20Sopenharmony_ci#define	TCP_ECN_SEEN		8
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_cienum tcp_tw_status {
3688c2ecf20Sopenharmony_ci	TCP_TW_SUCCESS = 0,
3698c2ecf20Sopenharmony_ci	TCP_TW_RST = 1,
3708c2ecf20Sopenharmony_ci	TCP_TW_ACK = 2,
3718c2ecf20Sopenharmony_ci	TCP_TW_SYN = 3
3728c2ecf20Sopenharmony_ci};
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cienum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
3768c2ecf20Sopenharmony_ci					      struct sk_buff *skb,
3778c2ecf20Sopenharmony_ci					      const struct tcphdr *th);
3788c2ecf20Sopenharmony_cistruct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
3798c2ecf20Sopenharmony_ci			   struct request_sock *req, bool fastopen,
3808c2ecf20Sopenharmony_ci			   bool *lost_race);
3818c2ecf20Sopenharmony_ciint tcp_child_process(struct sock *parent, struct sock *child,
3828c2ecf20Sopenharmony_ci		      struct sk_buff *skb);
3838c2ecf20Sopenharmony_civoid tcp_enter_loss(struct sock *sk);
3848c2ecf20Sopenharmony_civoid tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int flag);
3858c2ecf20Sopenharmony_civoid tcp_clear_retrans(struct tcp_sock *tp);
3868c2ecf20Sopenharmony_civoid tcp_update_metrics(struct sock *sk);
3878c2ecf20Sopenharmony_civoid tcp_init_metrics(struct sock *sk);
3888c2ecf20Sopenharmony_civoid tcp_metrics_init(void);
3898c2ecf20Sopenharmony_cibool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst);
3908c2ecf20Sopenharmony_civoid __tcp_close(struct sock *sk, long timeout);
3918c2ecf20Sopenharmony_civoid tcp_close(struct sock *sk, long timeout);
3928c2ecf20Sopenharmony_civoid tcp_init_sock(struct sock *sk);
3938c2ecf20Sopenharmony_civoid tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb);
3948c2ecf20Sopenharmony_ci__poll_t tcp_poll(struct file *file, struct socket *sock,
3958c2ecf20Sopenharmony_ci		      struct poll_table_struct *wait);
3968c2ecf20Sopenharmony_ciint tcp_getsockopt(struct sock *sk, int level, int optname,
3978c2ecf20Sopenharmony_ci		   char __user *optval, int __user *optlen);
3988c2ecf20Sopenharmony_cibool tcp_bpf_bypass_getsockopt(int level, int optname);
3998c2ecf20Sopenharmony_ciint tcp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
4008c2ecf20Sopenharmony_ci		   unsigned int optlen);
4018c2ecf20Sopenharmony_civoid tcp_set_keepalive(struct sock *sk, int val);
4028c2ecf20Sopenharmony_civoid tcp_syn_ack_timeout(const struct request_sock *req);
4038c2ecf20Sopenharmony_ciint tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
4048c2ecf20Sopenharmony_ci		int flags, int *addr_len);
4058c2ecf20Sopenharmony_ciint tcp_set_rcvlowat(struct sock *sk, int val);
4068c2ecf20Sopenharmony_civoid tcp_data_ready(struct sock *sk);
4078c2ecf20Sopenharmony_ci#ifdef CONFIG_MMU
4088c2ecf20Sopenharmony_ciint tcp_mmap(struct file *file, struct socket *sock,
4098c2ecf20Sopenharmony_ci	     struct vm_area_struct *vma);
4108c2ecf20Sopenharmony_ci#endif
4118c2ecf20Sopenharmony_civoid tcp_parse_options(const struct net *net, const struct sk_buff *skb,
4128c2ecf20Sopenharmony_ci		       struct tcp_options_received *opt_rx,
4138c2ecf20Sopenharmony_ci		       int estab, struct tcp_fastopen_cookie *foc);
4148c2ecf20Sopenharmony_ciconst u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci/*
4178c2ecf20Sopenharmony_ci *	BPF SKB-less helpers
4188c2ecf20Sopenharmony_ci */
4198c2ecf20Sopenharmony_ciu16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph,
4208c2ecf20Sopenharmony_ci			 struct tcphdr *th, u32 *cookie);
4218c2ecf20Sopenharmony_ciu16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph,
4228c2ecf20Sopenharmony_ci			 struct tcphdr *th, u32 *cookie);
4238c2ecf20Sopenharmony_ciu16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
4248c2ecf20Sopenharmony_ci			  const struct tcp_request_sock_ops *af_ops,
4258c2ecf20Sopenharmony_ci			  struct sock *sk, struct tcphdr *th);
4268c2ecf20Sopenharmony_ci/*
4278c2ecf20Sopenharmony_ci *	TCP v4 functions exported for the inet6 API
4288c2ecf20Sopenharmony_ci */
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_civoid tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
4318c2ecf20Sopenharmony_civoid tcp_v4_mtu_reduced(struct sock *sk);
4328c2ecf20Sopenharmony_civoid tcp_req_err(struct sock *sk, u32 seq, bool abort);
4338c2ecf20Sopenharmony_civoid tcp_ld_RTO_revert(struct sock *sk, u32 seq);
4348c2ecf20Sopenharmony_ciint tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
4358c2ecf20Sopenharmony_cistruct sock *tcp_create_openreq_child(const struct sock *sk,
4368c2ecf20Sopenharmony_ci				      struct request_sock *req,
4378c2ecf20Sopenharmony_ci				      struct sk_buff *skb);
4388c2ecf20Sopenharmony_civoid tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst);
4398c2ecf20Sopenharmony_cistruct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
4408c2ecf20Sopenharmony_ci				  struct request_sock *req,
4418c2ecf20Sopenharmony_ci				  struct dst_entry *dst,
4428c2ecf20Sopenharmony_ci				  struct request_sock *req_unhash,
4438c2ecf20Sopenharmony_ci				  bool *own_req);
4448c2ecf20Sopenharmony_ciint tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
4458c2ecf20Sopenharmony_ciint tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
4468c2ecf20Sopenharmony_ciint tcp_connect(struct sock *sk);
4478c2ecf20Sopenharmony_cienum tcp_synack_type {
4488c2ecf20Sopenharmony_ci	TCP_SYNACK_NORMAL,
4498c2ecf20Sopenharmony_ci	TCP_SYNACK_FASTOPEN,
4508c2ecf20Sopenharmony_ci	TCP_SYNACK_COOKIE,
4518c2ecf20Sopenharmony_ci};
4528c2ecf20Sopenharmony_cistruct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
4538c2ecf20Sopenharmony_ci				struct request_sock *req,
4548c2ecf20Sopenharmony_ci				struct tcp_fastopen_cookie *foc,
4558c2ecf20Sopenharmony_ci				enum tcp_synack_type synack_type,
4568c2ecf20Sopenharmony_ci				struct sk_buff *syn_skb);
4578c2ecf20Sopenharmony_ciint tcp_disconnect(struct sock *sk, int flags);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_civoid tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
4608c2ecf20Sopenharmony_ciint tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
4618c2ecf20Sopenharmony_civoid inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci/* From syncookies.c */
4648c2ecf20Sopenharmony_cistruct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
4658c2ecf20Sopenharmony_ci				 struct request_sock *req,
4668c2ecf20Sopenharmony_ci				 struct dst_entry *dst, u32 tsoff);
4678c2ecf20Sopenharmony_ciint __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
4688c2ecf20Sopenharmony_ci		      u32 cookie);
4698c2ecf20Sopenharmony_cistruct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
4708c2ecf20Sopenharmony_cistruct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops,
4718c2ecf20Sopenharmony_ci					    const struct tcp_request_sock_ops *af_ops,
4728c2ecf20Sopenharmony_ci					    struct sock *sk, struct sk_buff *skb);
4738c2ecf20Sopenharmony_ci#ifdef CONFIG_SYN_COOKIES
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci/* Syncookies use a monotonic timer which increments every 60 seconds.
4768c2ecf20Sopenharmony_ci * This counter is used both as a hash input and partially encoded into
4778c2ecf20Sopenharmony_ci * the cookie value.  A cookie is only validated further if the delta
4788c2ecf20Sopenharmony_ci * between the current counter value and the encoded one is less than this,
4798c2ecf20Sopenharmony_ci * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
4808c2ecf20Sopenharmony_ci * the counter advances immediately after a cookie is generated).
4818c2ecf20Sopenharmony_ci */
4828c2ecf20Sopenharmony_ci#define MAX_SYNCOOKIE_AGE	2
4838c2ecf20Sopenharmony_ci#define TCP_SYNCOOKIE_PERIOD	(60 * HZ)
4848c2ecf20Sopenharmony_ci#define TCP_SYNCOOKIE_VALID	(MAX_SYNCOOKIE_AGE * TCP_SYNCOOKIE_PERIOD)
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci/* syncookies: remember time of last synqueue overflow
4878c2ecf20Sopenharmony_ci * But do not dirty this field too often (once per second is enough)
4888c2ecf20Sopenharmony_ci * It is racy as we do not hold a lock, but race is very minor.
4898c2ecf20Sopenharmony_ci */
4908c2ecf20Sopenharmony_cistatic inline void tcp_synq_overflow(const struct sock *sk)
4918c2ecf20Sopenharmony_ci{
4928c2ecf20Sopenharmony_ci	unsigned int last_overflow;
4938c2ecf20Sopenharmony_ci	unsigned int now = jiffies;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	if (sk->sk_reuseport) {
4968c2ecf20Sopenharmony_ci		struct sock_reuseport *reuse;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci		reuse = rcu_dereference(sk->sk_reuseport_cb);
4998c2ecf20Sopenharmony_ci		if (likely(reuse)) {
5008c2ecf20Sopenharmony_ci			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
5018c2ecf20Sopenharmony_ci			if (!time_between32(now, last_overflow,
5028c2ecf20Sopenharmony_ci					    last_overflow + HZ))
5038c2ecf20Sopenharmony_ci				WRITE_ONCE(reuse->synq_overflow_ts, now);
5048c2ecf20Sopenharmony_ci			return;
5058c2ecf20Sopenharmony_ci		}
5068c2ecf20Sopenharmony_ci	}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
5098c2ecf20Sopenharmony_ci	if (!time_between32(now, last_overflow, last_overflow + HZ))
5108c2ecf20Sopenharmony_ci		WRITE_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp, now);
5118c2ecf20Sopenharmony_ci}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci/* syncookies: no recent synqueue overflow on this listening socket? */
5148c2ecf20Sopenharmony_cistatic inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	unsigned int last_overflow;
5178c2ecf20Sopenharmony_ci	unsigned int now = jiffies;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	if (sk->sk_reuseport) {
5208c2ecf20Sopenharmony_ci		struct sock_reuseport *reuse;
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci		reuse = rcu_dereference(sk->sk_reuseport_cb);
5238c2ecf20Sopenharmony_ci		if (likely(reuse)) {
5248c2ecf20Sopenharmony_ci			last_overflow = READ_ONCE(reuse->synq_overflow_ts);
5258c2ecf20Sopenharmony_ci			return !time_between32(now, last_overflow - HZ,
5268c2ecf20Sopenharmony_ci					       last_overflow +
5278c2ecf20Sopenharmony_ci					       TCP_SYNCOOKIE_VALID);
5288c2ecf20Sopenharmony_ci		}
5298c2ecf20Sopenharmony_ci	}
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	/* If last_overflow <= jiffies <= last_overflow + TCP_SYNCOOKIE_VALID,
5348c2ecf20Sopenharmony_ci	 * then we're under synflood. However, we have to use
5358c2ecf20Sopenharmony_ci	 * 'last_overflow - HZ' as lower bound. That's because a concurrent
5368c2ecf20Sopenharmony_ci	 * tcp_synq_overflow() could update .ts_recent_stamp after we read
5378c2ecf20Sopenharmony_ci	 * jiffies but before we store .ts_recent_stamp into last_overflow,
5388c2ecf20Sopenharmony_ci	 * which could lead to rejecting a valid syncookie.
5398c2ecf20Sopenharmony_ci	 */
5408c2ecf20Sopenharmony_ci	return !time_between32(now, last_overflow - HZ,
5418c2ecf20Sopenharmony_ci			       last_overflow + TCP_SYNCOOKIE_VALID);
5428c2ecf20Sopenharmony_ci}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_cistatic inline u32 tcp_cookie_time(void)
5458c2ecf20Sopenharmony_ci{
5468c2ecf20Sopenharmony_ci	u64 val = get_jiffies_64();
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	do_div(val, TCP_SYNCOOKIE_PERIOD);
5498c2ecf20Sopenharmony_ci	return val;
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ciu32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
5538c2ecf20Sopenharmony_ci			      u16 *mssp);
5548c2ecf20Sopenharmony_ci__u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss);
5558c2ecf20Sopenharmony_ciu64 cookie_init_timestamp(struct request_sock *req, u64 now);
5568c2ecf20Sopenharmony_cibool cookie_timestamp_decode(const struct net *net,
5578c2ecf20Sopenharmony_ci			     struct tcp_options_received *opt);
5588c2ecf20Sopenharmony_cibool cookie_ecn_ok(const struct tcp_options_received *opt,
5598c2ecf20Sopenharmony_ci		   const struct net *net, const struct dst_entry *dst);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci/* From net/ipv6/syncookies.c */
5628c2ecf20Sopenharmony_ciint __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
5638c2ecf20Sopenharmony_ci		      u32 cookie);
5648c2ecf20Sopenharmony_cistruct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ciu32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
5678c2ecf20Sopenharmony_ci			      const struct tcphdr *th, u16 *mssp);
5688c2ecf20Sopenharmony_ci__u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mss);
5698c2ecf20Sopenharmony_ci#endif
5708c2ecf20Sopenharmony_ci/* tcp_output.c */
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_civoid __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
5738c2ecf20Sopenharmony_ci			       int nonagle);
5748c2ecf20Sopenharmony_ciint __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
5758c2ecf20Sopenharmony_ciint tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs);
5768c2ecf20Sopenharmony_civoid tcp_retransmit_timer(struct sock *sk);
5778c2ecf20Sopenharmony_civoid tcp_xmit_retransmit_queue(struct sock *);
5788c2ecf20Sopenharmony_civoid tcp_simple_retransmit(struct sock *);
5798c2ecf20Sopenharmony_civoid tcp_enter_recovery(struct sock *sk, bool ece_ack);
5808c2ecf20Sopenharmony_ciint tcp_trim_head(struct sock *, struct sk_buff *, u32);
5818c2ecf20Sopenharmony_cienum tcp_queue {
5828c2ecf20Sopenharmony_ci	TCP_FRAG_IN_WRITE_QUEUE,
5838c2ecf20Sopenharmony_ci	TCP_FRAG_IN_RTX_QUEUE,
5848c2ecf20Sopenharmony_ci};
5858c2ecf20Sopenharmony_ciint tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,
5868c2ecf20Sopenharmony_ci		 struct sk_buff *skb, u32 len,
5878c2ecf20Sopenharmony_ci		 unsigned int mss_now, gfp_t gfp);
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_civoid tcp_send_probe0(struct sock *);
5908c2ecf20Sopenharmony_civoid tcp_send_partial(struct sock *);
5918c2ecf20Sopenharmony_ciint tcp_write_wakeup(struct sock *, int mib);
5928c2ecf20Sopenharmony_civoid tcp_send_fin(struct sock *sk);
5938c2ecf20Sopenharmony_civoid tcp_send_active_reset(struct sock *sk, gfp_t priority);
5948c2ecf20Sopenharmony_ciint tcp_send_synack(struct sock *);
5958c2ecf20Sopenharmony_civoid tcp_push_one(struct sock *, unsigned int mss_now);
5968c2ecf20Sopenharmony_civoid __tcp_send_ack(struct sock *sk, u32 rcv_nxt);
5978c2ecf20Sopenharmony_civoid tcp_send_ack(struct sock *sk);
5988c2ecf20Sopenharmony_civoid tcp_send_delayed_ack(struct sock *sk);
5998c2ecf20Sopenharmony_civoid tcp_send_loss_probe(struct sock *sk);
6008c2ecf20Sopenharmony_cibool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto);
6018c2ecf20Sopenharmony_civoid tcp_skb_collapse_tstamp(struct sk_buff *skb,
6028c2ecf20Sopenharmony_ci			     const struct sk_buff *next_skb);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci/* tcp_input.c */
6058c2ecf20Sopenharmony_civoid tcp_rearm_rto(struct sock *sk);
6068c2ecf20Sopenharmony_civoid tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req);
6078c2ecf20Sopenharmony_civoid tcp_reset(struct sock *sk);
6088c2ecf20Sopenharmony_civoid tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, struct sk_buff *skb);
6098c2ecf20Sopenharmony_civoid tcp_fin(struct sock *sk);
6108c2ecf20Sopenharmony_civoid tcp_check_space(struct sock *sk);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci/* tcp_timer.c */
6138c2ecf20Sopenharmony_civoid tcp_init_xmit_timers(struct sock *);
6148c2ecf20Sopenharmony_cistatic inline void tcp_clear_xmit_timers(struct sock *sk)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	if (hrtimer_try_to_cancel(&tcp_sk(sk)->pacing_timer) == 1)
6178c2ecf20Sopenharmony_ci		__sock_put(sk);
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	if (hrtimer_try_to_cancel(&tcp_sk(sk)->compressed_ack_timer) == 1)
6208c2ecf20Sopenharmony_ci		__sock_put(sk);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	inet_csk_clear_xmit_timers(sk);
6238c2ecf20Sopenharmony_ci}
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ciunsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
6268c2ecf20Sopenharmony_ciunsigned int tcp_current_mss(struct sock *sk);
6278c2ecf20Sopenharmony_ciu32 tcp_clamp_probe0_to_user_timeout(const struct sock *sk, u32 when);
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci/* Bound MSS / TSO packet size with the half of the window */
6308c2ecf20Sopenharmony_cistatic inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	int cutoff;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	/* When peer uses tiny windows, there is no use in packetizing
6358c2ecf20Sopenharmony_ci	 * to sub-MSS pieces for the sake of SWS or making sure there
6368c2ecf20Sopenharmony_ci	 * are enough packets in the pipe for fast recovery.
6378c2ecf20Sopenharmony_ci	 *
6388c2ecf20Sopenharmony_ci	 * On the other hand, for extremely large MSS devices, handling
6398c2ecf20Sopenharmony_ci	 * smaller than MSS windows in this way does make sense.
6408c2ecf20Sopenharmony_ci	 */
6418c2ecf20Sopenharmony_ci	if (tp->max_window > TCP_MSS_DEFAULT)
6428c2ecf20Sopenharmony_ci		cutoff = (tp->max_window >> 1);
6438c2ecf20Sopenharmony_ci	else
6448c2ecf20Sopenharmony_ci		cutoff = tp->max_window;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	if (cutoff && pktsize > cutoff)
6478c2ecf20Sopenharmony_ci		return max_t(int, cutoff, 68U - tp->tcp_header_len);
6488c2ecf20Sopenharmony_ci	else
6498c2ecf20Sopenharmony_ci		return pktsize;
6508c2ecf20Sopenharmony_ci}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci/* tcp.c */
6538c2ecf20Sopenharmony_civoid tcp_get_info(struct sock *, struct tcp_info *);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci/* Read 'sendfile()'-style from a TCP socket */
6568c2ecf20Sopenharmony_ciint tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
6578c2ecf20Sopenharmony_ci		  sk_read_actor_t recv_actor);
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_civoid tcp_initialize_rcv_mss(struct sock *sk);
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ciint tcp_mtu_to_mss(struct sock *sk, int pmtu);
6628c2ecf20Sopenharmony_ciint tcp_mss_to_mtu(struct sock *sk, int mss);
6638c2ecf20Sopenharmony_civoid tcp_mtup_init(struct sock *sk);
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_cistatic inline void tcp_bound_rto(const struct sock *sk)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
6688c2ecf20Sopenharmony_ci		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
6698c2ecf20Sopenharmony_ci}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_cistatic inline u32 __tcp_set_rto(const struct tcp_sock *tp)
6728c2ecf20Sopenharmony_ci{
6738c2ecf20Sopenharmony_ci	return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
6748c2ecf20Sopenharmony_ci}
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_cistatic inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
6778c2ecf20Sopenharmony_ci{
6788c2ecf20Sopenharmony_ci	/* mptcp hooks are only on the slow path */
6798c2ecf20Sopenharmony_ci	if (sk_is_mptcp((struct sock *)tp))
6808c2ecf20Sopenharmony_ci		return;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
6838c2ecf20Sopenharmony_ci			       ntohl(TCP_FLAG_ACK) |
6848c2ecf20Sopenharmony_ci			       snd_wnd);
6858c2ecf20Sopenharmony_ci}
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_cistatic inline void tcp_fast_path_on(struct tcp_sock *tp)
6888c2ecf20Sopenharmony_ci{
6898c2ecf20Sopenharmony_ci	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
6908c2ecf20Sopenharmony_ci}
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_cistatic inline void tcp_fast_path_check(struct sock *sk)
6938c2ecf20Sopenharmony_ci{
6948c2ecf20Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	if (RB_EMPTY_ROOT(&tp->out_of_order_queue) &&
6978c2ecf20Sopenharmony_ci	    tp->rcv_wnd &&
6988c2ecf20Sopenharmony_ci	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
6998c2ecf20Sopenharmony_ci	    !tp->urg_data)
7008c2ecf20Sopenharmony_ci		tcp_fast_path_on(tp);
7018c2ecf20Sopenharmony_ci}
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci/* Compute the actual rto_min value */
7048c2ecf20Sopenharmony_cistatic inline u32 tcp_rto_min(struct sock *sk)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	const struct dst_entry *dst = __sk_dst_get(sk);
7078c2ecf20Sopenharmony_ci	u32 rto_min = inet_csk(sk)->icsk_rto_min;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
7108c2ecf20Sopenharmony_ci		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
7118c2ecf20Sopenharmony_ci	return rto_min;
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic inline u32 tcp_rto_min_us(struct sock *sk)
7158c2ecf20Sopenharmony_ci{
7168c2ecf20Sopenharmony_ci	return jiffies_to_usecs(tcp_rto_min(sk));
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cistatic inline bool tcp_ca_dst_locked(const struct dst_entry *dst)
7208c2ecf20Sopenharmony_ci{
7218c2ecf20Sopenharmony_ci	return dst_metric_locked(dst, RTAX_CC_ALGO);
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci/* Minimum RTT in usec. ~0 means not available. */
7258c2ecf20Sopenharmony_cistatic inline u32 tcp_min_rtt(const struct tcp_sock *tp)
7268c2ecf20Sopenharmony_ci{
7278c2ecf20Sopenharmony_ci	return minmax_get(&tp->rtt_min);
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci/* Compute the actual receive window we are currently advertising.
7318c2ecf20Sopenharmony_ci * Rcv_nxt can be after the window if our peer push more data
7328c2ecf20Sopenharmony_ci * than the offered window.
7338c2ecf20Sopenharmony_ci */
7348c2ecf20Sopenharmony_cistatic inline u32 tcp_receive_window(const struct tcp_sock *tp)
7358c2ecf20Sopenharmony_ci{
7368c2ecf20Sopenharmony_ci	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	if (win < 0)
7398c2ecf20Sopenharmony_ci		win = 0;
7408c2ecf20Sopenharmony_ci	return (u32) win;
7418c2ecf20Sopenharmony_ci}
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci/* Choose a new window, without checks for shrinking, and without
7448c2ecf20Sopenharmony_ci * scaling applied to the result.  The caller does these things
7458c2ecf20Sopenharmony_ci * if necessary.  This is a "raw" window selection.
7468c2ecf20Sopenharmony_ci */
7478c2ecf20Sopenharmony_ciu32 __tcp_select_window(struct sock *sk);
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_civoid tcp_send_window_probe(struct sock *sk);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci/* TCP uses 32bit jiffies to save some space.
7528c2ecf20Sopenharmony_ci * Note that this is different from tcp_time_stamp, which
7538c2ecf20Sopenharmony_ci * historically has been the same until linux-4.13.
7548c2ecf20Sopenharmony_ci */
7558c2ecf20Sopenharmony_ci#define tcp_jiffies32 ((u32)jiffies)
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci/*
7588c2ecf20Sopenharmony_ci * Deliver a 32bit value for TCP timestamp option (RFC 7323)
7598c2ecf20Sopenharmony_ci * It is no longer tied to jiffies, but to 1 ms clock.
7608c2ecf20Sopenharmony_ci * Note: double check if you want to use tcp_jiffies32 instead of this.
7618c2ecf20Sopenharmony_ci */
7628c2ecf20Sopenharmony_ci#define TCP_TS_HZ	1000
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_cistatic inline u64 tcp_clock_ns(void)
7658c2ecf20Sopenharmony_ci{
7668c2ecf20Sopenharmony_ci	return ktime_get_ns();
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic inline u64 tcp_clock_us(void)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	return div_u64(tcp_clock_ns(), NSEC_PER_USEC);
7728c2ecf20Sopenharmony_ci}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci/* This should only be used in contexts where tp->tcp_mstamp is up to date */
7758c2ecf20Sopenharmony_cistatic inline u32 tcp_time_stamp(const struct tcp_sock *tp)
7768c2ecf20Sopenharmony_ci{
7778c2ecf20Sopenharmony_ci	return div_u64(tp->tcp_mstamp, USEC_PER_SEC / TCP_TS_HZ);
7788c2ecf20Sopenharmony_ci}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci/* Convert a nsec timestamp into TCP TSval timestamp (ms based currently) */
7818c2ecf20Sopenharmony_cistatic inline u64 tcp_ns_to_ts(u64 ns)
7828c2ecf20Sopenharmony_ci{
7838c2ecf20Sopenharmony_ci	return div_u64(ns, NSEC_PER_SEC / TCP_TS_HZ);
7848c2ecf20Sopenharmony_ci}
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci/* Could use tcp_clock_us() / 1000, but this version uses a single divide */
7878c2ecf20Sopenharmony_cistatic inline u32 tcp_time_stamp_raw(void)
7888c2ecf20Sopenharmony_ci{
7898c2ecf20Sopenharmony_ci	return tcp_ns_to_ts(tcp_clock_ns());
7908c2ecf20Sopenharmony_ci}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_civoid tcp_mstamp_refresh(struct tcp_sock *tp);
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_cistatic inline u32 tcp_stamp_us_delta(u64 t1, u64 t0)
7958c2ecf20Sopenharmony_ci{
7968c2ecf20Sopenharmony_ci	return max_t(s64, t1 - t0, 0);
7978c2ecf20Sopenharmony_ci}
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_cistatic inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
8008c2ecf20Sopenharmony_ci{
8018c2ecf20Sopenharmony_ci	return tcp_ns_to_ts(skb->skb_mstamp_ns);
8028c2ecf20Sopenharmony_ci}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci/* provide the departure time in us unit */
8058c2ecf20Sopenharmony_cistatic inline u64 tcp_skb_timestamp_us(const struct sk_buff *skb)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	return div_u64(skb->skb_mstamp_ns, NSEC_PER_USEC);
8088c2ecf20Sopenharmony_ci}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci#define tcp_flag_byte(th) (((u_int8_t *)th)[13])
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci#define TCPHDR_FIN 0x01
8148c2ecf20Sopenharmony_ci#define TCPHDR_SYN 0x02
8158c2ecf20Sopenharmony_ci#define TCPHDR_RST 0x04
8168c2ecf20Sopenharmony_ci#define TCPHDR_PSH 0x08
8178c2ecf20Sopenharmony_ci#define TCPHDR_ACK 0x10
8188c2ecf20Sopenharmony_ci#define TCPHDR_URG 0x20
8198c2ecf20Sopenharmony_ci#define TCPHDR_ECE 0x40
8208c2ecf20Sopenharmony_ci#define TCPHDR_CWR 0x80
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci#define TCPHDR_SYN_ECN	(TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci/* This is what the send packet queuing engine uses to pass
8258c2ecf20Sopenharmony_ci * TCP per-packet control information to the transmission code.
8268c2ecf20Sopenharmony_ci * We also store the host-order sequence numbers in here too.
8278c2ecf20Sopenharmony_ci * This is 44 bytes if IPV6 is enabled.
8288c2ecf20Sopenharmony_ci * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
8298c2ecf20Sopenharmony_ci */
8308c2ecf20Sopenharmony_cistruct tcp_skb_cb {
8318c2ecf20Sopenharmony_ci	__u32		seq;		/* Starting sequence number	*/
8328c2ecf20Sopenharmony_ci	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
8338c2ecf20Sopenharmony_ci	union {
8348c2ecf20Sopenharmony_ci		/* Note : tcp_tw_isn is used in input path only
8358c2ecf20Sopenharmony_ci		 *	  (isn chosen by tcp_timewait_state_process())
8368c2ecf20Sopenharmony_ci		 *
8378c2ecf20Sopenharmony_ci		 * 	  tcp_gso_segs/size are used in write queue only,
8388c2ecf20Sopenharmony_ci		 *	  cf tcp_skb_pcount()/tcp_skb_mss()
8398c2ecf20Sopenharmony_ci		 */
8408c2ecf20Sopenharmony_ci		__u32		tcp_tw_isn;
8418c2ecf20Sopenharmony_ci		struct {
8428c2ecf20Sopenharmony_ci			u16	tcp_gso_segs;
8438c2ecf20Sopenharmony_ci			u16	tcp_gso_size;
8448c2ecf20Sopenharmony_ci		};
8458c2ecf20Sopenharmony_ci	};
8468c2ecf20Sopenharmony_ci	__u8		tcp_flags;	/* TCP header flags. (tcp[13])	*/
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	__u8		sacked;		/* State flags for SACK.	*/
8498c2ecf20Sopenharmony_ci#define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
8508c2ecf20Sopenharmony_ci#define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
8518c2ecf20Sopenharmony_ci#define TCPCB_LOST		0x04	/* SKB is lost			*/
8528c2ecf20Sopenharmony_ci#define TCPCB_TAGBITS		0x07	/* All tag bits			*/
8538c2ecf20Sopenharmony_ci#define TCPCB_REPAIRED		0x10	/* SKB repaired (no skb_mstamp_ns)	*/
8548c2ecf20Sopenharmony_ci#define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
8558c2ecf20Sopenharmony_ci#define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
8568c2ecf20Sopenharmony_ci				TCPCB_REPAIRED)
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ci	__u8		ip_dsfield;	/* IPv4 tos or IPv6 dsfield	*/
8598c2ecf20Sopenharmony_ci	__u8		txstamp_ack:1,	/* Record TX timestamp for ack? */
8608c2ecf20Sopenharmony_ci			eor:1,		/* Is skb MSG_EOR marked? */
8618c2ecf20Sopenharmony_ci			has_rxtstamp:1,	/* SKB has a RX timestamp	*/
8628c2ecf20Sopenharmony_ci			unused:5;
8638c2ecf20Sopenharmony_ci	__u32		ack_seq;	/* Sequence number ACK'd	*/
8648c2ecf20Sopenharmony_ci	union {
8658c2ecf20Sopenharmony_ci		struct {
8668c2ecf20Sopenharmony_ci			/* There is space for up to 24 bytes */
8678c2ecf20Sopenharmony_ci			__u32 in_flight:30,/* Bytes in flight at transmit */
8688c2ecf20Sopenharmony_ci			      is_app_limited:1, /* cwnd not fully used? */
8698c2ecf20Sopenharmony_ci			      unused:1;
8708c2ecf20Sopenharmony_ci			/* pkts S/ACKed so far upon tx of skb, incl retrans: */
8718c2ecf20Sopenharmony_ci			__u32 delivered;
8728c2ecf20Sopenharmony_ci			/* start of send pipeline phase */
8738c2ecf20Sopenharmony_ci			u64 first_tx_mstamp;
8748c2ecf20Sopenharmony_ci			/* when we reached the "delivered" count */
8758c2ecf20Sopenharmony_ci			u64 delivered_mstamp;
8768c2ecf20Sopenharmony_ci		} tx;   /* only used for outgoing skbs */
8778c2ecf20Sopenharmony_ci		union {
8788c2ecf20Sopenharmony_ci			struct inet_skb_parm	h4;
8798c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
8808c2ecf20Sopenharmony_ci			struct inet6_skb_parm	h6;
8818c2ecf20Sopenharmony_ci#endif
8828c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_NEWIP)
8838c2ecf20Sopenharmony_ci			struct ninet_skb_parm	hnip; /* NIP */
8848c2ecf20Sopenharmony_ci#endif
8858c2ecf20Sopenharmony_ci		} header;	/* For incoming skbs */
8868c2ecf20Sopenharmony_ci		struct {
8878c2ecf20Sopenharmony_ci			__u32 flags;
8888c2ecf20Sopenharmony_ci			struct sock *sk_redir;
8898c2ecf20Sopenharmony_ci			void *data_end;
8908c2ecf20Sopenharmony_ci		} bpf;
8918c2ecf20Sopenharmony_ci	};
8928c2ecf20Sopenharmony_ci};
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci#define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_cistatic inline void bpf_compute_data_end_sk_skb(struct sk_buff *skb)
8978c2ecf20Sopenharmony_ci{
8988c2ecf20Sopenharmony_ci	TCP_SKB_CB(skb)->bpf.data_end = skb->data + skb_headlen(skb);
8998c2ecf20Sopenharmony_ci}
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_cistatic inline bool tcp_skb_bpf_ingress(const struct sk_buff *skb)
9028c2ecf20Sopenharmony_ci{
9038c2ecf20Sopenharmony_ci	return TCP_SKB_CB(skb)->bpf.flags & BPF_F_INGRESS;
9048c2ecf20Sopenharmony_ci}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_cistatic inline struct sock *tcp_skb_bpf_redirect_fetch(struct sk_buff *skb)
9078c2ecf20Sopenharmony_ci{
9088c2ecf20Sopenharmony_ci	return TCP_SKB_CB(skb)->bpf.sk_redir;
9098c2ecf20Sopenharmony_ci}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_cistatic inline void tcp_skb_bpf_redirect_clear(struct sk_buff *skb)
9128c2ecf20Sopenharmony_ci{
9138c2ecf20Sopenharmony_ci	TCP_SKB_CB(skb)->bpf.sk_redir = NULL;
9148c2ecf20Sopenharmony_ci}
9158c2ecf20Sopenharmony_ci
9168c2ecf20Sopenharmony_ciextern const struct inet_connection_sock_af_ops ipv4_specific;
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
9198c2ecf20Sopenharmony_ci/* This is the variant of inet6_iif() that must be used by TCP,
9208c2ecf20Sopenharmony_ci * as TCP moves IP6CB into a different location in skb->cb[]
9218c2ecf20Sopenharmony_ci */
9228c2ecf20Sopenharmony_cistatic inline int tcp_v6_iif(const struct sk_buff *skb)
9238c2ecf20Sopenharmony_ci{
9248c2ecf20Sopenharmony_ci	return TCP_SKB_CB(skb)->header.h6.iif;
9258c2ecf20Sopenharmony_ci}
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_cistatic inline int tcp_v6_iif_l3_slave(const struct sk_buff *skb)
9288c2ecf20Sopenharmony_ci{
9298c2ecf20Sopenharmony_ci	bool l3_slave = ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags);
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci	return l3_slave ? skb->skb_iif : TCP_SKB_CB(skb)->header.h6.iif;
9328c2ecf20Sopenharmony_ci}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci/* TCP_SKB_CB reference means this can not be used from early demux */
9358c2ecf20Sopenharmony_cistatic inline int tcp_v6_sdif(const struct sk_buff *skb)
9368c2ecf20Sopenharmony_ci{
9378c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9388c2ecf20Sopenharmony_ci	if (skb && ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags))
9398c2ecf20Sopenharmony_ci		return TCP_SKB_CB(skb)->header.h6.iif;
9408c2ecf20Sopenharmony_ci#endif
9418c2ecf20Sopenharmony_ci	return 0;
9428c2ecf20Sopenharmony_ci}
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ciextern const struct inet_connection_sock_af_ops ipv6_specific;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb));
9478c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *skb));
9488c2ecf20Sopenharmony_civoid tcp_v6_early_demux(struct sk_buff *skb);
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci#endif
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci/* TCP_SKB_CB reference means this can not be used from early demux */
9538c2ecf20Sopenharmony_cistatic inline int tcp_v4_sdif(struct sk_buff *skb)
9548c2ecf20Sopenharmony_ci{
9558c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
9568c2ecf20Sopenharmony_ci	if (skb && ipv4_l3mdev_skb(TCP_SKB_CB(skb)->header.h4.flags))
9578c2ecf20Sopenharmony_ci		return TCP_SKB_CB(skb)->header.h4.iif;
9588c2ecf20Sopenharmony_ci#endif
9598c2ecf20Sopenharmony_ci	return 0;
9608c2ecf20Sopenharmony_ci}
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci/* Due to TSO, an SKB can be composed of multiple actual
9638c2ecf20Sopenharmony_ci * packets.  To keep these tracked properly, we use this.
9648c2ecf20Sopenharmony_ci */
9658c2ecf20Sopenharmony_cistatic inline int tcp_skb_pcount(const struct sk_buff *skb)
9668c2ecf20Sopenharmony_ci{
9678c2ecf20Sopenharmony_ci	return TCP_SKB_CB(skb)->tcp_gso_segs;
9688c2ecf20Sopenharmony_ci}
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_cistatic inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs)
9718c2ecf20Sopenharmony_ci{
9728c2ecf20Sopenharmony_ci	TCP_SKB_CB(skb)->tcp_gso_segs = segs;
9738c2ecf20Sopenharmony_ci}
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_cistatic inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs)
9768c2ecf20Sopenharmony_ci{
9778c2ecf20Sopenharmony_ci	TCP_SKB_CB(skb)->tcp_gso_segs += segs;
9788c2ecf20Sopenharmony_ci}
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci/* This is valid iff skb is in write queue and tcp_skb_pcount() > 1. */
9818c2ecf20Sopenharmony_cistatic inline int tcp_skb_mss(const struct sk_buff *skb)
9828c2ecf20Sopenharmony_ci{
9838c2ecf20Sopenharmony_ci	return TCP_SKB_CB(skb)->tcp_gso_size;
9848c2ecf20Sopenharmony_ci}
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_cistatic inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb)
9878c2ecf20Sopenharmony_ci{
9888c2ecf20Sopenharmony_ci	return likely(!TCP_SKB_CB(skb)->eor);
9898c2ecf20Sopenharmony_ci}
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_cistatic inline bool tcp_skb_can_collapse(const struct sk_buff *to,
9928c2ecf20Sopenharmony_ci					const struct sk_buff *from)
9938c2ecf20Sopenharmony_ci{
9948c2ecf20Sopenharmony_ci	return likely(tcp_skb_can_collapse_to(to) &&
9958c2ecf20Sopenharmony_ci		      mptcp_skb_can_collapse(to, from));
9968c2ecf20Sopenharmony_ci}
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci/* Events passed to congestion control interface */
9998c2ecf20Sopenharmony_cienum tcp_ca_event {
10008c2ecf20Sopenharmony_ci	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
10018c2ecf20Sopenharmony_ci	CA_EVENT_CWND_RESTART,	/* congestion window restart */
10028c2ecf20Sopenharmony_ci	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
10038c2ecf20Sopenharmony_ci	CA_EVENT_LOSS,		/* loss timeout */
10048c2ecf20Sopenharmony_ci	CA_EVENT_ECN_NO_CE,	/* ECT set, but not CE marked */
10058c2ecf20Sopenharmony_ci	CA_EVENT_ECN_IS_CE,	/* received CE marked IP packet */
10068c2ecf20Sopenharmony_ci};
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci/* Information about inbound ACK, passed to cong_ops->in_ack_event() */
10098c2ecf20Sopenharmony_cienum tcp_ca_ack_event_flags {
10108c2ecf20Sopenharmony_ci	CA_ACK_SLOWPATH		= (1 << 0),	/* In slow path processing */
10118c2ecf20Sopenharmony_ci	CA_ACK_WIN_UPDATE	= (1 << 1),	/* ACK updated window */
10128c2ecf20Sopenharmony_ci	CA_ACK_ECE		= (1 << 2),	/* ECE bit is set on ack */
10138c2ecf20Sopenharmony_ci};
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci/*
10168c2ecf20Sopenharmony_ci * Interface for adding new TCP congestion control handlers
10178c2ecf20Sopenharmony_ci */
10188c2ecf20Sopenharmony_ci#define TCP_CA_NAME_MAX	16
10198c2ecf20Sopenharmony_ci#define TCP_CA_MAX	128
10208c2ecf20Sopenharmony_ci#define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci#define TCP_CA_UNSPEC	0
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci/* Algorithm can be set on socket without CAP_NET_ADMIN privileges */
10258c2ecf20Sopenharmony_ci#define TCP_CONG_NON_RESTRICTED 0x1
10268c2ecf20Sopenharmony_ci/* Requires ECN/ECT set on all packets */
10278c2ecf20Sopenharmony_ci#define TCP_CONG_NEEDS_ECN	0x2
10288c2ecf20Sopenharmony_ci#define TCP_CONG_MASK	(TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN)
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ciunion tcp_cc_info;
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_cistruct ack_sample {
10338c2ecf20Sopenharmony_ci	u32 pkts_acked;
10348c2ecf20Sopenharmony_ci	s32 rtt_us;
10358c2ecf20Sopenharmony_ci	u32 in_flight;
10368c2ecf20Sopenharmony_ci};
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci/* A rate sample measures the number of (original/retransmitted) data
10398c2ecf20Sopenharmony_ci * packets delivered "delivered" over an interval of time "interval_us".
10408c2ecf20Sopenharmony_ci * The tcp_rate.c code fills in the rate sample, and congestion
10418c2ecf20Sopenharmony_ci * control modules that define a cong_control function to run at the end
10428c2ecf20Sopenharmony_ci * of ACK processing can optionally chose to consult this sample when
10438c2ecf20Sopenharmony_ci * setting cwnd and pacing rate.
10448c2ecf20Sopenharmony_ci * A sample is invalid if "delivered" or "interval_us" is negative.
10458c2ecf20Sopenharmony_ci */
10468c2ecf20Sopenharmony_cistruct rate_sample {
10478c2ecf20Sopenharmony_ci	u64  prior_mstamp; /* starting timestamp for interval */
10488c2ecf20Sopenharmony_ci	u32  prior_delivered;	/* tp->delivered at "prior_mstamp" */
10498c2ecf20Sopenharmony_ci	s32  delivered;		/* number of packets delivered over interval */
10508c2ecf20Sopenharmony_ci	long interval_us;	/* time for tp->delivered to incr "delivered" */
10518c2ecf20Sopenharmony_ci	u32 snd_interval_us;	/* snd interval for delivered packets */
10528c2ecf20Sopenharmony_ci	u32 rcv_interval_us;	/* rcv interval for delivered packets */
10538c2ecf20Sopenharmony_ci	long rtt_us;		/* RTT of last (S)ACKed packet (or -1) */
10548c2ecf20Sopenharmony_ci	int  losses;		/* number of packets marked lost upon ACK */
10558c2ecf20Sopenharmony_ci	u32  acked_sacked;	/* number of packets newly (S)ACKed upon ACK */
10568c2ecf20Sopenharmony_ci	u32  prior_in_flight;	/* in flight before this ACK */
10578c2ecf20Sopenharmony_ci	u32  last_end_seq;	/* end_seq of most recently ACKed packet */
10588c2ecf20Sopenharmony_ci	bool is_app_limited;	/* is sample from packet with bubble in pipe? */
10598c2ecf20Sopenharmony_ci	bool is_retrans;	/* is sample from retransmission? */
10608c2ecf20Sopenharmony_ci	bool is_ack_delayed;	/* is this (likely) a delayed ACK? */
10618c2ecf20Sopenharmony_ci};
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_cistruct tcp_congestion_ops {
10648c2ecf20Sopenharmony_ci	struct list_head	list;
10658c2ecf20Sopenharmony_ci	u32 key;
10668c2ecf20Sopenharmony_ci	u32 flags;
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	/* initialize private data (optional) */
10698c2ecf20Sopenharmony_ci	void (*init)(struct sock *sk);
10708c2ecf20Sopenharmony_ci	/* cleanup private data  (optional) */
10718c2ecf20Sopenharmony_ci	void (*release)(struct sock *sk);
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci	/* return slow start threshold (required) */
10748c2ecf20Sopenharmony_ci	u32 (*ssthresh)(struct sock *sk);
10758c2ecf20Sopenharmony_ci	/* do new cwnd calculation (required) */
10768c2ecf20Sopenharmony_ci	void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked);
10778c2ecf20Sopenharmony_ci	/* call before changing ca_state (optional) */
10788c2ecf20Sopenharmony_ci	void (*set_state)(struct sock *sk, u8 new_state);
10798c2ecf20Sopenharmony_ci	/* call when cwnd event occurs (optional) */
10808c2ecf20Sopenharmony_ci	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
10818c2ecf20Sopenharmony_ci	/* call when ack arrives (optional) */
10828c2ecf20Sopenharmony_ci	void (*in_ack_event)(struct sock *sk, u32 flags);
10838c2ecf20Sopenharmony_ci	/* new value of cwnd after loss (required) */
10848c2ecf20Sopenharmony_ci	u32  (*undo_cwnd)(struct sock *sk);
10858c2ecf20Sopenharmony_ci	/* hook for packet ack accounting (optional) */
10868c2ecf20Sopenharmony_ci	void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample);
10878c2ecf20Sopenharmony_ci	/* override sysctl_tcp_min_tso_segs */
10888c2ecf20Sopenharmony_ci	u32 (*min_tso_segs)(struct sock *sk);
10898c2ecf20Sopenharmony_ci	/* returns the multiplier used in tcp_sndbuf_expand (optional) */
10908c2ecf20Sopenharmony_ci	u32 (*sndbuf_expand)(struct sock *sk);
10918c2ecf20Sopenharmony_ci	/* call when packets are delivered to update cwnd and pacing rate,
10928c2ecf20Sopenharmony_ci	 * after all the ca_state processing. (optional)
10938c2ecf20Sopenharmony_ci	 */
10948c2ecf20Sopenharmony_ci	void (*cong_control)(struct sock *sk, const struct rate_sample *rs);
10958c2ecf20Sopenharmony_ci	/* get info for inet_diag (optional) */
10968c2ecf20Sopenharmony_ci	size_t (*get_info)(struct sock *sk, u32 ext, int *attr,
10978c2ecf20Sopenharmony_ci			   union tcp_cc_info *info);
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	char 		name[TCP_CA_NAME_MAX];
11008c2ecf20Sopenharmony_ci	struct module 	*owner;
11018c2ecf20Sopenharmony_ci};
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ciint tcp_register_congestion_control(struct tcp_congestion_ops *type);
11048c2ecf20Sopenharmony_civoid tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_civoid tcp_assign_congestion_control(struct sock *sk);
11078c2ecf20Sopenharmony_civoid tcp_init_congestion_control(struct sock *sk);
11088c2ecf20Sopenharmony_civoid tcp_cleanup_congestion_control(struct sock *sk);
11098c2ecf20Sopenharmony_ciint tcp_set_default_congestion_control(struct net *net, const char *name);
11108c2ecf20Sopenharmony_civoid tcp_get_default_congestion_control(struct net *net, char *name);
11118c2ecf20Sopenharmony_civoid tcp_get_available_congestion_control(char *buf, size_t len);
11128c2ecf20Sopenharmony_civoid tcp_get_allowed_congestion_control(char *buf, size_t len);
11138c2ecf20Sopenharmony_ciint tcp_set_allowed_congestion_control(char *allowed);
11148c2ecf20Sopenharmony_ciint tcp_set_congestion_control(struct sock *sk, const char *name, bool load,
11158c2ecf20Sopenharmony_ci			       bool cap_net_admin);
11168c2ecf20Sopenharmony_ciu32 tcp_slow_start(struct tcp_sock *tp, u32 acked);
11178c2ecf20Sopenharmony_civoid tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ciu32 tcp_reno_ssthresh(struct sock *sk);
11208c2ecf20Sopenharmony_ciu32 tcp_reno_undo_cwnd(struct sock *sk);
11218c2ecf20Sopenharmony_civoid tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked);
11228c2ecf20Sopenharmony_ciextern struct tcp_congestion_ops tcp_reno;
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_cistruct tcp_congestion_ops *tcp_ca_find(const char *name);
11258c2ecf20Sopenharmony_cistruct tcp_congestion_ops *tcp_ca_find_key(u32 key);
11268c2ecf20Sopenharmony_ciu32 tcp_ca_get_key_by_name(struct net *net, const char *name, bool *ecn_ca);
11278c2ecf20Sopenharmony_ci#ifdef CONFIG_INET
11288c2ecf20Sopenharmony_cichar *tcp_ca_get_name_by_key(u32 key, char *buffer);
11298c2ecf20Sopenharmony_ci#else
11308c2ecf20Sopenharmony_cistatic inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
11318c2ecf20Sopenharmony_ci{
11328c2ecf20Sopenharmony_ci	return NULL;
11338c2ecf20Sopenharmony_ci}
11348c2ecf20Sopenharmony_ci#endif
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_cistatic inline bool tcp_ca_needs_ecn(const struct sock *sk)
11378c2ecf20Sopenharmony_ci{
11388c2ecf20Sopenharmony_ci	const struct inet_connection_sock *icsk = inet_csk(sk);
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN;
11418c2ecf20Sopenharmony_ci}
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_cistatic inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
11448c2ecf20Sopenharmony_ci{
11458c2ecf20Sopenharmony_ci	struct inet_connection_sock *icsk = inet_csk(sk);
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	if (icsk->icsk_ca_ops->set_state)
11488c2ecf20Sopenharmony_ci		icsk->icsk_ca_ops->set_state(sk, ca_state);
11498c2ecf20Sopenharmony_ci	icsk->icsk_ca_state = ca_state;
11508c2ecf20Sopenharmony_ci}
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_cistatic inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
11538c2ecf20Sopenharmony_ci{
11548c2ecf20Sopenharmony_ci	const struct inet_connection_sock *icsk = inet_csk(sk);
11558c2ecf20Sopenharmony_ci
11568c2ecf20Sopenharmony_ci	if (icsk->icsk_ca_ops->cwnd_event)
11578c2ecf20Sopenharmony_ci		icsk->icsk_ca_ops->cwnd_event(sk, event);
11588c2ecf20Sopenharmony_ci}
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci/* From tcp_rate.c */
11618c2ecf20Sopenharmony_civoid tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb);
11628c2ecf20Sopenharmony_civoid tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
11638c2ecf20Sopenharmony_ci			    struct rate_sample *rs);
11648c2ecf20Sopenharmony_civoid tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
11658c2ecf20Sopenharmony_ci		  bool is_sack_reneg, struct rate_sample *rs);
11668c2ecf20Sopenharmony_civoid tcp_rate_check_app_limited(struct sock *sk);
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_cistatic inline bool tcp_skb_sent_after(u64 t1, u64 t2, u32 seq1, u32 seq2)
11698c2ecf20Sopenharmony_ci{
11708c2ecf20Sopenharmony_ci	return t1 > t2 || (t1 == t2 && after(seq1, seq2));
11718c2ecf20Sopenharmony_ci}
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci/* These functions determine how the current flow behaves in respect of SACK
11748c2ecf20Sopenharmony_ci * handling. SACK is negotiated with the peer, and therefore it can vary
11758c2ecf20Sopenharmony_ci * between different flows.
11768c2ecf20Sopenharmony_ci *
11778c2ecf20Sopenharmony_ci * tcp_is_sack - SACK enabled
11788c2ecf20Sopenharmony_ci * tcp_is_reno - No SACK
11798c2ecf20Sopenharmony_ci */
11808c2ecf20Sopenharmony_cistatic inline int tcp_is_sack(const struct tcp_sock *tp)
11818c2ecf20Sopenharmony_ci{
11828c2ecf20Sopenharmony_ci	return likely(tp->rx_opt.sack_ok);
11838c2ecf20Sopenharmony_ci}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_cistatic inline bool tcp_is_reno(const struct tcp_sock *tp)
11868c2ecf20Sopenharmony_ci{
11878c2ecf20Sopenharmony_ci	return !tcp_is_sack(tp);
11888c2ecf20Sopenharmony_ci}
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_cistatic inline unsigned int tcp_left_out(const struct tcp_sock *tp)
11918c2ecf20Sopenharmony_ci{
11928c2ecf20Sopenharmony_ci	return tp->sacked_out + tp->lost_out;
11938c2ecf20Sopenharmony_ci}
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci/* This determines how many packets are "in the network" to the best
11968c2ecf20Sopenharmony_ci * of our knowledge.  In many cases it is conservative, but where
11978c2ecf20Sopenharmony_ci * detailed information is available from the receiver (via SACK
11988c2ecf20Sopenharmony_ci * blocks etc.) we can make more aggressive calculations.
11998c2ecf20Sopenharmony_ci *
12008c2ecf20Sopenharmony_ci * Use this for decisions involving congestion control, use just
12018c2ecf20Sopenharmony_ci * tp->packets_out to determine if the send queue is empty or not.
12028c2ecf20Sopenharmony_ci *
12038c2ecf20Sopenharmony_ci * Read this equation as:
12048c2ecf20Sopenharmony_ci *
12058c2ecf20Sopenharmony_ci *	"Packets sent once on transmission queue" MINUS
12068c2ecf20Sopenharmony_ci *	"Packets left network, but not honestly ACKed yet" PLUS
12078c2ecf20Sopenharmony_ci *	"Packets fast retransmitted"
12088c2ecf20Sopenharmony_ci */
12098c2ecf20Sopenharmony_cistatic inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
12108c2ecf20Sopenharmony_ci{
12118c2ecf20Sopenharmony_ci	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
12128c2ecf20Sopenharmony_ci}
12138c2ecf20Sopenharmony_ci
12148c2ecf20Sopenharmony_ci#define TCP_INFINITE_SSTHRESH	0x7fffffff
12158c2ecf20Sopenharmony_ci
12168c2ecf20Sopenharmony_cistatic inline bool tcp_in_slow_start(const struct tcp_sock *tp)
12178c2ecf20Sopenharmony_ci{
12188c2ecf20Sopenharmony_ci	return tp->snd_cwnd < tp->snd_ssthresh;
12198c2ecf20Sopenharmony_ci}
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_cistatic inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
12228c2ecf20Sopenharmony_ci{
12238c2ecf20Sopenharmony_ci	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
12248c2ecf20Sopenharmony_ci}
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_cistatic inline bool tcp_in_cwnd_reduction(const struct sock *sk)
12278c2ecf20Sopenharmony_ci{
12288c2ecf20Sopenharmony_ci	return (TCPF_CA_CWR | TCPF_CA_Recovery) &
12298c2ecf20Sopenharmony_ci	       (1 << inet_csk(sk)->icsk_ca_state);
12308c2ecf20Sopenharmony_ci}
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci/* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
12338c2ecf20Sopenharmony_ci * The exception is cwnd reduction phase, when cwnd is decreasing towards
12348c2ecf20Sopenharmony_ci * ssthresh.
12358c2ecf20Sopenharmony_ci */
12368c2ecf20Sopenharmony_cistatic inline __u32 tcp_current_ssthresh(const struct sock *sk)
12378c2ecf20Sopenharmony_ci{
12388c2ecf20Sopenharmony_ci	const struct tcp_sock *tp = tcp_sk(sk);
12398c2ecf20Sopenharmony_ci
12408c2ecf20Sopenharmony_ci	if (tcp_in_cwnd_reduction(sk))
12418c2ecf20Sopenharmony_ci		return tp->snd_ssthresh;
12428c2ecf20Sopenharmony_ci	else
12438c2ecf20Sopenharmony_ci		return max(tp->snd_ssthresh,
12448c2ecf20Sopenharmony_ci			   ((tp->snd_cwnd >> 1) +
12458c2ecf20Sopenharmony_ci			    (tp->snd_cwnd >> 2)));
12468c2ecf20Sopenharmony_ci}
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci/* Use define here intentionally to get WARN_ON location shown at the caller */
12498c2ecf20Sopenharmony_ci#define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_civoid tcp_enter_cwr(struct sock *sk);
12528c2ecf20Sopenharmony_ci__u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci/* The maximum number of MSS of available cwnd for which TSO defers
12558c2ecf20Sopenharmony_ci * sending if not using sysctl_tcp_tso_win_divisor.
12568c2ecf20Sopenharmony_ci */
12578c2ecf20Sopenharmony_cistatic inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
12588c2ecf20Sopenharmony_ci{
12598c2ecf20Sopenharmony_ci	return 3;
12608c2ecf20Sopenharmony_ci}
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci/* Returns end sequence number of the receiver's advertised window */
12638c2ecf20Sopenharmony_cistatic inline u32 tcp_wnd_end(const struct tcp_sock *tp)
12648c2ecf20Sopenharmony_ci{
12658c2ecf20Sopenharmony_ci	return tp->snd_una + tp->snd_wnd;
12668c2ecf20Sopenharmony_ci}
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci/* We follow the spirit of RFC2861 to validate cwnd but implement a more
12698c2ecf20Sopenharmony_ci * flexible approach. The RFC suggests cwnd should not be raised unless
12708c2ecf20Sopenharmony_ci * it was fully used previously. And that's exactly what we do in
12718c2ecf20Sopenharmony_ci * congestion avoidance mode. But in slow start we allow cwnd to grow
12728c2ecf20Sopenharmony_ci * as long as the application has used half the cwnd.
12738c2ecf20Sopenharmony_ci * Example :
12748c2ecf20Sopenharmony_ci *    cwnd is 10 (IW10), but application sends 9 frames.
12758c2ecf20Sopenharmony_ci *    We allow cwnd to reach 18 when all frames are ACKed.
12768c2ecf20Sopenharmony_ci * This check is safe because it's as aggressive as slow start which already
12778c2ecf20Sopenharmony_ci * risks 100% overshoot. The advantage is that we discourage application to
12788c2ecf20Sopenharmony_ci * either send more filler packets or data to artificially blow up the cwnd
12798c2ecf20Sopenharmony_ci * usage, and allow application-limited process to probe bw more aggressively.
12808c2ecf20Sopenharmony_ci */
12818c2ecf20Sopenharmony_cistatic inline bool tcp_is_cwnd_limited(const struct sock *sk)
12828c2ecf20Sopenharmony_ci{
12838c2ecf20Sopenharmony_ci	const struct tcp_sock *tp = tcp_sk(sk);
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	if (tp->is_cwnd_limited)
12868c2ecf20Sopenharmony_ci		return true;
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci	/* If in slow start, ensure cwnd grows to twice what was ACKed. */
12898c2ecf20Sopenharmony_ci	if (tcp_in_slow_start(tp))
12908c2ecf20Sopenharmony_ci		return tp->snd_cwnd < 2 * tp->max_packets_out;
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	return false;
12938c2ecf20Sopenharmony_ci}
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_ci/* BBR congestion control needs pacing.
12968c2ecf20Sopenharmony_ci * Same remark for SO_MAX_PACING_RATE.
12978c2ecf20Sopenharmony_ci * sch_fq packet scheduler is efficiently handling pacing,
12988c2ecf20Sopenharmony_ci * but is not always installed/used.
12998c2ecf20Sopenharmony_ci * Return true if TCP stack should pace packets itself.
13008c2ecf20Sopenharmony_ci */
13018c2ecf20Sopenharmony_cistatic inline bool tcp_needs_internal_pacing(const struct sock *sk)
13028c2ecf20Sopenharmony_ci{
13038c2ecf20Sopenharmony_ci	return smp_load_acquire(&sk->sk_pacing_status) == SK_PACING_NEEDED;
13048c2ecf20Sopenharmony_ci}
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ci/* Estimates in how many jiffies next packet for this flow can be sent.
13078c2ecf20Sopenharmony_ci * Scheduling a retransmit timer too early would be silly.
13088c2ecf20Sopenharmony_ci */
13098c2ecf20Sopenharmony_cistatic inline unsigned long tcp_pacing_delay(const struct sock *sk)
13108c2ecf20Sopenharmony_ci{
13118c2ecf20Sopenharmony_ci	s64 delay = tcp_sk(sk)->tcp_wstamp_ns - tcp_sk(sk)->tcp_clock_cache;
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ci	return delay > 0 ? nsecs_to_jiffies(delay) : 0;
13148c2ecf20Sopenharmony_ci}
13158c2ecf20Sopenharmony_ci
13168c2ecf20Sopenharmony_cistatic inline void tcp_reset_xmit_timer(struct sock *sk,
13178c2ecf20Sopenharmony_ci					const int what,
13188c2ecf20Sopenharmony_ci					unsigned long when,
13198c2ecf20Sopenharmony_ci					const unsigned long max_when)
13208c2ecf20Sopenharmony_ci{
13218c2ecf20Sopenharmony_ci	inet_csk_reset_xmit_timer(sk, what, when + tcp_pacing_delay(sk),
13228c2ecf20Sopenharmony_ci				  max_when);
13238c2ecf20Sopenharmony_ci}
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci/* Something is really bad, we could not queue an additional packet,
13268c2ecf20Sopenharmony_ci * because qdisc is full or receiver sent a 0 window, or we are paced.
13278c2ecf20Sopenharmony_ci * We do not want to add fuel to the fire, or abort too early,
13288c2ecf20Sopenharmony_ci * so make sure the timer we arm now is at least 200ms in the future,
13298c2ecf20Sopenharmony_ci * regardless of current icsk_rto value (as it could be ~2ms)
13308c2ecf20Sopenharmony_ci */
13318c2ecf20Sopenharmony_cistatic inline unsigned long tcp_probe0_base(const struct sock *sk)
13328c2ecf20Sopenharmony_ci{
13338c2ecf20Sopenharmony_ci	return max_t(unsigned long, inet_csk(sk)->icsk_rto, TCP_RTO_MIN);
13348c2ecf20Sopenharmony_ci}
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci/* Variant of inet_csk_rto_backoff() used for zero window probes */
13378c2ecf20Sopenharmony_cistatic inline unsigned long tcp_probe0_when(const struct sock *sk,
13388c2ecf20Sopenharmony_ci					    unsigned long max_when)
13398c2ecf20Sopenharmony_ci{
13408c2ecf20Sopenharmony_ci	u64 when = (u64)tcp_probe0_base(sk) << inet_csk(sk)->icsk_backoff;
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	return (unsigned long)min_t(u64, when, max_when);
13438c2ecf20Sopenharmony_ci}
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_cistatic inline void tcp_check_probe_timer(struct sock *sk)
13468c2ecf20Sopenharmony_ci{
13478c2ecf20Sopenharmony_ci	if (!tcp_sk(sk)->packets_out && !inet_csk(sk)->icsk_pending)
13488c2ecf20Sopenharmony_ci		tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
13498c2ecf20Sopenharmony_ci				     tcp_probe0_base(sk), TCP_RTO_MAX);
13508c2ecf20Sopenharmony_ci}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_cistatic inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
13538c2ecf20Sopenharmony_ci{
13548c2ecf20Sopenharmony_ci	tp->snd_wl1 = seq;
13558c2ecf20Sopenharmony_ci}
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_cistatic inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
13588c2ecf20Sopenharmony_ci{
13598c2ecf20Sopenharmony_ci	tp->snd_wl1 = seq;
13608c2ecf20Sopenharmony_ci}
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ci/*
13638c2ecf20Sopenharmony_ci * Calculate(/check) TCP checksum
13648c2ecf20Sopenharmony_ci */
13658c2ecf20Sopenharmony_cistatic inline __sum16 tcp_v4_check(int len, __be32 saddr,
13668c2ecf20Sopenharmony_ci				   __be32 daddr, __wsum base)
13678c2ecf20Sopenharmony_ci{
13688c2ecf20Sopenharmony_ci	return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_TCP, base);
13698c2ecf20Sopenharmony_ci}
13708c2ecf20Sopenharmony_ci
13718c2ecf20Sopenharmony_cistatic inline bool tcp_checksum_complete(struct sk_buff *skb)
13728c2ecf20Sopenharmony_ci{
13738c2ecf20Sopenharmony_ci	return !skb_csum_unnecessary(skb) &&
13748c2ecf20Sopenharmony_ci		__skb_checksum_complete(skb);
13758c2ecf20Sopenharmony_ci}
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_cibool tcp_add_backlog(struct sock *sk, struct sk_buff *skb);
13788c2ecf20Sopenharmony_ciint tcp_filter(struct sock *sk, struct sk_buff *skb);
13798c2ecf20Sopenharmony_civoid tcp_set_state(struct sock *sk, int state);
13808c2ecf20Sopenharmony_civoid tcp_done(struct sock *sk);
13818c2ecf20Sopenharmony_ciint tcp_abort(struct sock *sk, int err);
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_cistatic inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
13848c2ecf20Sopenharmony_ci{
13858c2ecf20Sopenharmony_ci	rx_opt->dsack = 0;
13868c2ecf20Sopenharmony_ci	rx_opt->num_sacks = 0;
13878c2ecf20Sopenharmony_ci}
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_civoid tcp_cwnd_restart(struct sock *sk, s32 delta);
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_cistatic inline void tcp_slow_start_after_idle_check(struct sock *sk)
13928c2ecf20Sopenharmony_ci{
13938c2ecf20Sopenharmony_ci	const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
13948c2ecf20Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
13958c2ecf20Sopenharmony_ci	s32 delta;
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci	if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle) ||
13988c2ecf20Sopenharmony_ci	    tp->packets_out || ca_ops->cong_control)
13998c2ecf20Sopenharmony_ci		return;
14008c2ecf20Sopenharmony_ci	delta = tcp_jiffies32 - tp->lsndtime;
14018c2ecf20Sopenharmony_ci	if (delta > inet_csk(sk)->icsk_rto)
14028c2ecf20Sopenharmony_ci		tcp_cwnd_restart(sk, delta);
14038c2ecf20Sopenharmony_ci}
14048c2ecf20Sopenharmony_ci
14058c2ecf20Sopenharmony_ci/* Determine a window scaling and initial window to offer. */
14068c2ecf20Sopenharmony_civoid tcp_select_initial_window(const struct sock *sk, int __space,
14078c2ecf20Sopenharmony_ci			       __u32 mss, __u32 *rcv_wnd,
14088c2ecf20Sopenharmony_ci			       __u32 *window_clamp, int wscale_ok,
14098c2ecf20Sopenharmony_ci			       __u8 *rcv_wscale, __u32 init_rcv_wnd);
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_cistatic inline int tcp_win_from_space(const struct sock *sk, int space)
14128c2ecf20Sopenharmony_ci{
14138c2ecf20Sopenharmony_ci	int tcp_adv_win_scale = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_adv_win_scale);
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ci	return tcp_adv_win_scale <= 0 ?
14168c2ecf20Sopenharmony_ci		(space>>(-tcp_adv_win_scale)) :
14178c2ecf20Sopenharmony_ci		space - (space>>tcp_adv_win_scale);
14188c2ecf20Sopenharmony_ci}
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci/* Note: caller must be prepared to deal with negative returns */
14218c2ecf20Sopenharmony_cistatic inline int tcp_space(const struct sock *sk)
14228c2ecf20Sopenharmony_ci{
14238c2ecf20Sopenharmony_ci	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) -
14248c2ecf20Sopenharmony_ci				  READ_ONCE(sk->sk_backlog.len) -
14258c2ecf20Sopenharmony_ci				  atomic_read(&sk->sk_rmem_alloc));
14268c2ecf20Sopenharmony_ci}
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_cistatic inline int tcp_full_space(const struct sock *sk)
14298c2ecf20Sopenharmony_ci{
14308c2ecf20Sopenharmony_ci	return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf));
14318c2ecf20Sopenharmony_ci}
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_civoid tcp_cleanup_rbuf(struct sock *sk, int copied);
14348c2ecf20Sopenharmony_ci
14358c2ecf20Sopenharmony_ci/* We provision sk_rcvbuf around 200% of sk_rcvlowat.
14368c2ecf20Sopenharmony_ci * If 87.5 % (7/8) of the space has been consumed, we want to override
14378c2ecf20Sopenharmony_ci * SO_RCVLOWAT constraint, since we are receiving skbs with too small
14388c2ecf20Sopenharmony_ci * len/truesize ratio.
14398c2ecf20Sopenharmony_ci */
14408c2ecf20Sopenharmony_cistatic inline bool tcp_rmem_pressure(const struct sock *sk)
14418c2ecf20Sopenharmony_ci{
14428c2ecf20Sopenharmony_ci	int rcvbuf, threshold;
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci	if (tcp_under_memory_pressure(sk))
14458c2ecf20Sopenharmony_ci		return true;
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci	rcvbuf = READ_ONCE(sk->sk_rcvbuf);
14488c2ecf20Sopenharmony_ci	threshold = rcvbuf - (rcvbuf >> 3);
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	return atomic_read(&sk->sk_rmem_alloc) > threshold;
14518c2ecf20Sopenharmony_ci}
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ciextern void tcp_openreq_init_rwin(struct request_sock *req,
14548c2ecf20Sopenharmony_ci				  const struct sock *sk_listener,
14558c2ecf20Sopenharmony_ci				  const struct dst_entry *dst);
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_civoid tcp_enter_memory_pressure(struct sock *sk);
14588c2ecf20Sopenharmony_civoid tcp_leave_memory_pressure(struct sock *sk);
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_cistatic inline int keepalive_intvl_when(const struct tcp_sock *tp)
14618c2ecf20Sopenharmony_ci{
14628c2ecf20Sopenharmony_ci	struct net *net = sock_net((struct sock *)tp);
14638c2ecf20Sopenharmony_ci	int val;
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	/* Paired with WRITE_ONCE() in tcp_sock_set_keepintvl()
14668c2ecf20Sopenharmony_ci	 * and do_tcp_setsockopt().
14678c2ecf20Sopenharmony_ci	 */
14688c2ecf20Sopenharmony_ci	val = READ_ONCE(tp->keepalive_intvl);
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_intvl);
14718c2ecf20Sopenharmony_ci}
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_cistatic inline int keepalive_time_when(const struct tcp_sock *tp)
14748c2ecf20Sopenharmony_ci{
14758c2ecf20Sopenharmony_ci	struct net *net = sock_net((struct sock *)tp);
14768c2ecf20Sopenharmony_ci	int val;
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci	/* Paired with WRITE_ONCE() in tcp_sock_set_keepidle_locked() */
14798c2ecf20Sopenharmony_ci	val = READ_ONCE(tp->keepalive_time);
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci	return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time);
14828c2ecf20Sopenharmony_ci}
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_cistatic inline int keepalive_probes(const struct tcp_sock *tp)
14858c2ecf20Sopenharmony_ci{
14868c2ecf20Sopenharmony_ci	struct net *net = sock_net((struct sock *)tp);
14878c2ecf20Sopenharmony_ci	int val;
14888c2ecf20Sopenharmony_ci
14898c2ecf20Sopenharmony_ci	/* Paired with WRITE_ONCE() in tcp_sock_set_keepcnt()
14908c2ecf20Sopenharmony_ci	 * and do_tcp_setsockopt().
14918c2ecf20Sopenharmony_ci	 */
14928c2ecf20Sopenharmony_ci	val = READ_ONCE(tp->keepalive_probes);
14938c2ecf20Sopenharmony_ci
14948c2ecf20Sopenharmony_ci	return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_probes);
14958c2ecf20Sopenharmony_ci}
14968c2ecf20Sopenharmony_ci
14978c2ecf20Sopenharmony_cistatic inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
14988c2ecf20Sopenharmony_ci{
14998c2ecf20Sopenharmony_ci	const struct inet_connection_sock *icsk = &tp->inet_conn;
15008c2ecf20Sopenharmony_ci
15018c2ecf20Sopenharmony_ci	return min_t(u32, tcp_jiffies32 - icsk->icsk_ack.lrcvtime,
15028c2ecf20Sopenharmony_ci			  tcp_jiffies32 - tp->rcv_tstamp);
15038c2ecf20Sopenharmony_ci}
15048c2ecf20Sopenharmony_ci
15058c2ecf20Sopenharmony_cistatic inline int tcp_fin_time(const struct sock *sk)
15068c2ecf20Sopenharmony_ci{
15078c2ecf20Sopenharmony_ci	int fin_timeout = tcp_sk(sk)->linger2 ? :
15088c2ecf20Sopenharmony_ci		READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fin_timeout);
15098c2ecf20Sopenharmony_ci	const int rto = inet_csk(sk)->icsk_rto;
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci	if (fin_timeout < (rto << 2) - (rto >> 1))
15128c2ecf20Sopenharmony_ci		fin_timeout = (rto << 2) - (rto >> 1);
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci	return fin_timeout;
15158c2ecf20Sopenharmony_ci}
15168c2ecf20Sopenharmony_ci
15178c2ecf20Sopenharmony_cistatic inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
15188c2ecf20Sopenharmony_ci				  int paws_win)
15198c2ecf20Sopenharmony_ci{
15208c2ecf20Sopenharmony_ci	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
15218c2ecf20Sopenharmony_ci		return true;
15228c2ecf20Sopenharmony_ci	if (unlikely(!time_before32(ktime_get_seconds(),
15238c2ecf20Sopenharmony_ci				    rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)))
15248c2ecf20Sopenharmony_ci		return true;
15258c2ecf20Sopenharmony_ci	/*
15268c2ecf20Sopenharmony_ci	 * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
15278c2ecf20Sopenharmony_ci	 * then following tcp messages have valid values. Ignore 0 value,
15288c2ecf20Sopenharmony_ci	 * or else 'negative' tsval might forbid us to accept their packets.
15298c2ecf20Sopenharmony_ci	 */
15308c2ecf20Sopenharmony_ci	if (!rx_opt->ts_recent)
15318c2ecf20Sopenharmony_ci		return true;
15328c2ecf20Sopenharmony_ci	return false;
15338c2ecf20Sopenharmony_ci}
15348c2ecf20Sopenharmony_ci
15358c2ecf20Sopenharmony_cistatic inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
15368c2ecf20Sopenharmony_ci				   int rst)
15378c2ecf20Sopenharmony_ci{
15388c2ecf20Sopenharmony_ci	if (tcp_paws_check(rx_opt, 0))
15398c2ecf20Sopenharmony_ci		return false;
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci	/* RST segments are not recommended to carry timestamp,
15428c2ecf20Sopenharmony_ci	   and, if they do, it is recommended to ignore PAWS because
15438c2ecf20Sopenharmony_ci	   "their cleanup function should take precedence over timestamps."
15448c2ecf20Sopenharmony_ci	   Certainly, it is mistake. It is necessary to understand the reasons
15458c2ecf20Sopenharmony_ci	   of this constraint to relax it: if peer reboots, clock may go
15468c2ecf20Sopenharmony_ci	   out-of-sync and half-open connections will not be reset.
15478c2ecf20Sopenharmony_ci	   Actually, the problem would be not existing if all
15488c2ecf20Sopenharmony_ci	   the implementations followed draft about maintaining clock
15498c2ecf20Sopenharmony_ci	   via reboots. Linux-2.2 DOES NOT!
15508c2ecf20Sopenharmony_ci
15518c2ecf20Sopenharmony_ci	   However, we can relax time bounds for RST segments to MSL.
15528c2ecf20Sopenharmony_ci	 */
15538c2ecf20Sopenharmony_ci	if (rst && !time_before32(ktime_get_seconds(),
15548c2ecf20Sopenharmony_ci				  rx_opt->ts_recent_stamp + TCP_PAWS_MSL))
15558c2ecf20Sopenharmony_ci		return false;
15568c2ecf20Sopenharmony_ci	return true;
15578c2ecf20Sopenharmony_ci}
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_cibool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
15608c2ecf20Sopenharmony_ci			  int mib_idx, u32 *last_oow_ack_time);
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_cistatic inline void tcp_mib_init(struct net *net)
15638c2ecf20Sopenharmony_ci{
15648c2ecf20Sopenharmony_ci	/* See RFC 2012 */
15658c2ecf20Sopenharmony_ci	TCP_ADD_STATS(net, TCP_MIB_RTOALGORITHM, 1);
15668c2ecf20Sopenharmony_ci	TCP_ADD_STATS(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
15678c2ecf20Sopenharmony_ci	TCP_ADD_STATS(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
15688c2ecf20Sopenharmony_ci	TCP_ADD_STATS(net, TCP_MIB_MAXCONN, -1);
15698c2ecf20Sopenharmony_ci}
15708c2ecf20Sopenharmony_ci
15718c2ecf20Sopenharmony_ci/* from STCP */
15728c2ecf20Sopenharmony_cistatic inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
15738c2ecf20Sopenharmony_ci{
15748c2ecf20Sopenharmony_ci	tp->lost_skb_hint = NULL;
15758c2ecf20Sopenharmony_ci}
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_cistatic inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
15788c2ecf20Sopenharmony_ci{
15798c2ecf20Sopenharmony_ci	tcp_clear_retrans_hints_partial(tp);
15808c2ecf20Sopenharmony_ci	tp->retransmit_skb_hint = NULL;
15818c2ecf20Sopenharmony_ci}
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ciunion tcp_md5_addr {
15848c2ecf20Sopenharmony_ci	struct in_addr  a4;
15858c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
15868c2ecf20Sopenharmony_ci	struct in6_addr	a6;
15878c2ecf20Sopenharmony_ci#endif
15888c2ecf20Sopenharmony_ci};
15898c2ecf20Sopenharmony_ci
15908c2ecf20Sopenharmony_ci/* - key database */
15918c2ecf20Sopenharmony_cistruct tcp_md5sig_key {
15928c2ecf20Sopenharmony_ci	struct hlist_node	node;
15938c2ecf20Sopenharmony_ci	u8			keylen;
15948c2ecf20Sopenharmony_ci	u8			family; /* AF_INET or AF_INET6 */
15958c2ecf20Sopenharmony_ci	u8			prefixlen;
15968c2ecf20Sopenharmony_ci	union tcp_md5_addr	addr;
15978c2ecf20Sopenharmony_ci	int			l3index; /* set if key added with L3 scope */
15988c2ecf20Sopenharmony_ci	u8			key[TCP_MD5SIG_MAXKEYLEN];
15998c2ecf20Sopenharmony_ci	struct rcu_head		rcu;
16008c2ecf20Sopenharmony_ci};
16018c2ecf20Sopenharmony_ci
16028c2ecf20Sopenharmony_ci/* - sock block */
16038c2ecf20Sopenharmony_cistruct tcp_md5sig_info {
16048c2ecf20Sopenharmony_ci	struct hlist_head	head;
16058c2ecf20Sopenharmony_ci	struct rcu_head		rcu;
16068c2ecf20Sopenharmony_ci};
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci/* - pseudo header */
16098c2ecf20Sopenharmony_cistruct tcp4_pseudohdr {
16108c2ecf20Sopenharmony_ci	__be32		saddr;
16118c2ecf20Sopenharmony_ci	__be32		daddr;
16128c2ecf20Sopenharmony_ci	__u8		pad;
16138c2ecf20Sopenharmony_ci	__u8		protocol;
16148c2ecf20Sopenharmony_ci	__be16		len;
16158c2ecf20Sopenharmony_ci};
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_cistruct tcp6_pseudohdr {
16188c2ecf20Sopenharmony_ci	struct in6_addr	saddr;
16198c2ecf20Sopenharmony_ci	struct in6_addr daddr;
16208c2ecf20Sopenharmony_ci	__be32		len;
16218c2ecf20Sopenharmony_ci	__be32		protocol;	/* including padding */
16228c2ecf20Sopenharmony_ci};
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_ciunion tcp_md5sum_block {
16258c2ecf20Sopenharmony_ci	struct tcp4_pseudohdr ip4;
16268c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
16278c2ecf20Sopenharmony_ci	struct tcp6_pseudohdr ip6;
16288c2ecf20Sopenharmony_ci#endif
16298c2ecf20Sopenharmony_ci};
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci/* - pool: digest algorithm, hash description and scratch buffer */
16328c2ecf20Sopenharmony_cistruct tcp_md5sig_pool {
16338c2ecf20Sopenharmony_ci	struct ahash_request	*md5_req;
16348c2ecf20Sopenharmony_ci	void			*scratch;
16358c2ecf20Sopenharmony_ci};
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_ci/* - functions */
16388c2ecf20Sopenharmony_ciint tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
16398c2ecf20Sopenharmony_ci			const struct sock *sk, const struct sk_buff *skb);
16408c2ecf20Sopenharmony_ciint tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
16418c2ecf20Sopenharmony_ci		   int family, u8 prefixlen, int l3index,
16428c2ecf20Sopenharmony_ci		   const u8 *newkey, u8 newkeylen, gfp_t gfp);
16438c2ecf20Sopenharmony_ciint tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
16448c2ecf20Sopenharmony_ci		   int family, u8 prefixlen, int l3index);
16458c2ecf20Sopenharmony_cistruct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
16468c2ecf20Sopenharmony_ci					 const struct sock *addr_sk);
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_ci#ifdef CONFIG_TCP_MD5SIG
16498c2ecf20Sopenharmony_ci#include <linux/jump_label.h>
16508c2ecf20Sopenharmony_ciextern struct static_key_false tcp_md5_needed;
16518c2ecf20Sopenharmony_cistruct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index,
16528c2ecf20Sopenharmony_ci					   const union tcp_md5_addr *addr,
16538c2ecf20Sopenharmony_ci					   int family);
16548c2ecf20Sopenharmony_cistatic inline struct tcp_md5sig_key *
16558c2ecf20Sopenharmony_citcp_md5_do_lookup(const struct sock *sk, int l3index,
16568c2ecf20Sopenharmony_ci		  const union tcp_md5_addr *addr, int family)
16578c2ecf20Sopenharmony_ci{
16588c2ecf20Sopenharmony_ci	if (!static_branch_unlikely(&tcp_md5_needed))
16598c2ecf20Sopenharmony_ci		return NULL;
16608c2ecf20Sopenharmony_ci	return __tcp_md5_do_lookup(sk, l3index, addr, family);
16618c2ecf20Sopenharmony_ci}
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_ci#define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_key)
16648c2ecf20Sopenharmony_ci#else
16658c2ecf20Sopenharmony_cistatic inline struct tcp_md5sig_key *
16668c2ecf20Sopenharmony_citcp_md5_do_lookup(const struct sock *sk, int l3index,
16678c2ecf20Sopenharmony_ci		  const union tcp_md5_addr *addr, int family)
16688c2ecf20Sopenharmony_ci{
16698c2ecf20Sopenharmony_ci	return NULL;
16708c2ecf20Sopenharmony_ci}
16718c2ecf20Sopenharmony_ci#define tcp_twsk_md5_key(twsk)	NULL
16728c2ecf20Sopenharmony_ci#endif
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_cibool tcp_alloc_md5sig_pool(void);
16758c2ecf20Sopenharmony_ci
16768c2ecf20Sopenharmony_cistruct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
16778c2ecf20Sopenharmony_cistatic inline void tcp_put_md5sig_pool(void)
16788c2ecf20Sopenharmony_ci{
16798c2ecf20Sopenharmony_ci	local_bh_enable();
16808c2ecf20Sopenharmony_ci}
16818c2ecf20Sopenharmony_ci
16828c2ecf20Sopenharmony_ciint tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
16838c2ecf20Sopenharmony_ci			  unsigned int header_len);
16848c2ecf20Sopenharmony_ciint tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
16858c2ecf20Sopenharmony_ci		     const struct tcp_md5sig_key *key);
16868c2ecf20Sopenharmony_ci
16878c2ecf20Sopenharmony_ci/* From tcp_fastopen.c */
16888c2ecf20Sopenharmony_civoid tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
16898c2ecf20Sopenharmony_ci			    struct tcp_fastopen_cookie *cookie);
16908c2ecf20Sopenharmony_civoid tcp_fastopen_cache_set(struct sock *sk, u16 mss,
16918c2ecf20Sopenharmony_ci			    struct tcp_fastopen_cookie *cookie, bool syn_lost,
16928c2ecf20Sopenharmony_ci			    u16 try_exp);
16938c2ecf20Sopenharmony_cistruct tcp_fastopen_request {
16948c2ecf20Sopenharmony_ci	/* Fast Open cookie. Size 0 means a cookie request */
16958c2ecf20Sopenharmony_ci	struct tcp_fastopen_cookie	cookie;
16968c2ecf20Sopenharmony_ci	struct msghdr			*data;  /* data in MSG_FASTOPEN */
16978c2ecf20Sopenharmony_ci	size_t				size;
16988c2ecf20Sopenharmony_ci	int				copied;	/* queued in tcp_connect() */
16998c2ecf20Sopenharmony_ci	struct ubuf_info		*uarg;
17008c2ecf20Sopenharmony_ci};
17018c2ecf20Sopenharmony_civoid tcp_free_fastopen_req(struct tcp_sock *tp);
17028c2ecf20Sopenharmony_civoid tcp_fastopen_destroy_cipher(struct sock *sk);
17038c2ecf20Sopenharmony_civoid tcp_fastopen_ctx_destroy(struct net *net);
17048c2ecf20Sopenharmony_ciint tcp_fastopen_reset_cipher(struct net *net, struct sock *sk,
17058c2ecf20Sopenharmony_ci			      void *primary_key, void *backup_key);
17068c2ecf20Sopenharmony_ciint tcp_fastopen_get_cipher(struct net *net, struct inet_connection_sock *icsk,
17078c2ecf20Sopenharmony_ci			    u64 *key);
17088c2ecf20Sopenharmony_civoid tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb);
17098c2ecf20Sopenharmony_cistruct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
17108c2ecf20Sopenharmony_ci			      struct request_sock *req,
17118c2ecf20Sopenharmony_ci			      struct tcp_fastopen_cookie *foc,
17128c2ecf20Sopenharmony_ci			      const struct dst_entry *dst);
17138c2ecf20Sopenharmony_civoid tcp_fastopen_init_key_once(struct net *net);
17148c2ecf20Sopenharmony_cibool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss,
17158c2ecf20Sopenharmony_ci			     struct tcp_fastopen_cookie *cookie);
17168c2ecf20Sopenharmony_cibool tcp_fastopen_defer_connect(struct sock *sk, int *err);
17178c2ecf20Sopenharmony_ci#define TCP_FASTOPEN_KEY_LENGTH sizeof(siphash_key_t)
17188c2ecf20Sopenharmony_ci#define TCP_FASTOPEN_KEY_MAX 2
17198c2ecf20Sopenharmony_ci#define TCP_FASTOPEN_KEY_BUF_LENGTH \
17208c2ecf20Sopenharmony_ci	(TCP_FASTOPEN_KEY_LENGTH * TCP_FASTOPEN_KEY_MAX)
17218c2ecf20Sopenharmony_ci
17228c2ecf20Sopenharmony_ci/* Fastopen key context */
17238c2ecf20Sopenharmony_cistruct tcp_fastopen_context {
17248c2ecf20Sopenharmony_ci	siphash_key_t	key[TCP_FASTOPEN_KEY_MAX];
17258c2ecf20Sopenharmony_ci	int		num;
17268c2ecf20Sopenharmony_ci	struct rcu_head	rcu;
17278c2ecf20Sopenharmony_ci};
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_ciextern unsigned int sysctl_tcp_fastopen_blackhole_timeout;
17308c2ecf20Sopenharmony_civoid tcp_fastopen_active_disable(struct sock *sk);
17318c2ecf20Sopenharmony_cibool tcp_fastopen_active_should_disable(struct sock *sk);
17328c2ecf20Sopenharmony_civoid tcp_fastopen_active_disable_ofo_check(struct sock *sk);
17338c2ecf20Sopenharmony_civoid tcp_fastopen_active_detect_blackhole(struct sock *sk, bool expired);
17348c2ecf20Sopenharmony_ci
17358c2ecf20Sopenharmony_ci/* Caller needs to wrap with rcu_read_(un)lock() */
17368c2ecf20Sopenharmony_cistatic inline
17378c2ecf20Sopenharmony_cistruct tcp_fastopen_context *tcp_fastopen_get_ctx(const struct sock *sk)
17388c2ecf20Sopenharmony_ci{
17398c2ecf20Sopenharmony_ci	struct tcp_fastopen_context *ctx;
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci	ctx = rcu_dereference(inet_csk(sk)->icsk_accept_queue.fastopenq.ctx);
17428c2ecf20Sopenharmony_ci	if (!ctx)
17438c2ecf20Sopenharmony_ci		ctx = rcu_dereference(sock_net(sk)->ipv4.tcp_fastopen_ctx);
17448c2ecf20Sopenharmony_ci	return ctx;
17458c2ecf20Sopenharmony_ci}
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_cistatic inline
17488c2ecf20Sopenharmony_cibool tcp_fastopen_cookie_match(const struct tcp_fastopen_cookie *foc,
17498c2ecf20Sopenharmony_ci			       const struct tcp_fastopen_cookie *orig)
17508c2ecf20Sopenharmony_ci{
17518c2ecf20Sopenharmony_ci	if (orig->len == TCP_FASTOPEN_COOKIE_SIZE &&
17528c2ecf20Sopenharmony_ci	    orig->len == foc->len &&
17538c2ecf20Sopenharmony_ci	    !memcmp(orig->val, foc->val, foc->len))
17548c2ecf20Sopenharmony_ci		return true;
17558c2ecf20Sopenharmony_ci	return false;
17568c2ecf20Sopenharmony_ci}
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_cistatic inline
17598c2ecf20Sopenharmony_ciint tcp_fastopen_context_len(const struct tcp_fastopen_context *ctx)
17608c2ecf20Sopenharmony_ci{
17618c2ecf20Sopenharmony_ci	return ctx->num;
17628c2ecf20Sopenharmony_ci}
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci/* Latencies incurred by various limits for a sender. They are
17658c2ecf20Sopenharmony_ci * chronograph-like stats that are mutually exclusive.
17668c2ecf20Sopenharmony_ci */
17678c2ecf20Sopenharmony_cienum tcp_chrono {
17688c2ecf20Sopenharmony_ci	TCP_CHRONO_UNSPEC,
17698c2ecf20Sopenharmony_ci	TCP_CHRONO_BUSY, /* Actively sending data (non-empty write queue) */
17708c2ecf20Sopenharmony_ci	TCP_CHRONO_RWND_LIMITED, /* Stalled by insufficient receive window */
17718c2ecf20Sopenharmony_ci	TCP_CHRONO_SNDBUF_LIMITED, /* Stalled by insufficient send buffer */
17728c2ecf20Sopenharmony_ci	__TCP_CHRONO_MAX,
17738c2ecf20Sopenharmony_ci};
17748c2ecf20Sopenharmony_ci
17758c2ecf20Sopenharmony_civoid tcp_chrono_start(struct sock *sk, const enum tcp_chrono type);
17768c2ecf20Sopenharmony_civoid tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type);
17778c2ecf20Sopenharmony_ci
17788c2ecf20Sopenharmony_ci/* This helper is needed, because skb->tcp_tsorted_anchor uses
17798c2ecf20Sopenharmony_ci * the same memory storage than skb->destructor/_skb_refdst
17808c2ecf20Sopenharmony_ci */
17818c2ecf20Sopenharmony_cistatic inline void tcp_skb_tsorted_anchor_cleanup(struct sk_buff *skb)
17828c2ecf20Sopenharmony_ci{
17838c2ecf20Sopenharmony_ci	skb->destructor = NULL;
17848c2ecf20Sopenharmony_ci	skb->_skb_refdst = 0UL;
17858c2ecf20Sopenharmony_ci}
17868c2ecf20Sopenharmony_ci
17878c2ecf20Sopenharmony_ci#define tcp_skb_tsorted_save(skb) {		\
17888c2ecf20Sopenharmony_ci	unsigned long _save = skb->_skb_refdst;	\
17898c2ecf20Sopenharmony_ci	skb->_skb_refdst = 0UL;
17908c2ecf20Sopenharmony_ci
17918c2ecf20Sopenharmony_ci#define tcp_skb_tsorted_restore(skb)		\
17928c2ecf20Sopenharmony_ci	skb->_skb_refdst = _save;		\
17938c2ecf20Sopenharmony_ci}
17948c2ecf20Sopenharmony_ci
17958c2ecf20Sopenharmony_civoid tcp_write_queue_purge(struct sock *sk);
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_cistatic inline struct sk_buff *tcp_rtx_queue_head(const struct sock *sk)
17988c2ecf20Sopenharmony_ci{
17998c2ecf20Sopenharmony_ci	return skb_rb_first(&sk->tcp_rtx_queue);
18008c2ecf20Sopenharmony_ci}
18018c2ecf20Sopenharmony_ci
18028c2ecf20Sopenharmony_cistatic inline struct sk_buff *tcp_rtx_queue_tail(const struct sock *sk)
18038c2ecf20Sopenharmony_ci{
18048c2ecf20Sopenharmony_ci	return skb_rb_last(&sk->tcp_rtx_queue);
18058c2ecf20Sopenharmony_ci}
18068c2ecf20Sopenharmony_ci
18078c2ecf20Sopenharmony_cistatic inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
18088c2ecf20Sopenharmony_ci{
18098c2ecf20Sopenharmony_ci	return skb_peek(&sk->sk_write_queue);
18108c2ecf20Sopenharmony_ci}
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_cistatic inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
18138c2ecf20Sopenharmony_ci{
18148c2ecf20Sopenharmony_ci	return skb_peek_tail(&sk->sk_write_queue);
18158c2ecf20Sopenharmony_ci}
18168c2ecf20Sopenharmony_ci
18178c2ecf20Sopenharmony_ci#define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
18188c2ecf20Sopenharmony_ci	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
18198c2ecf20Sopenharmony_ci
18208c2ecf20Sopenharmony_cistatic inline struct sk_buff *tcp_send_head(const struct sock *sk)
18218c2ecf20Sopenharmony_ci{
18228c2ecf20Sopenharmony_ci	return skb_peek(&sk->sk_write_queue);
18238c2ecf20Sopenharmony_ci}
18248c2ecf20Sopenharmony_ci
18258c2ecf20Sopenharmony_cistatic inline bool tcp_skb_is_last(const struct sock *sk,
18268c2ecf20Sopenharmony_ci				   const struct sk_buff *skb)
18278c2ecf20Sopenharmony_ci{
18288c2ecf20Sopenharmony_ci	return skb_queue_is_last(&sk->sk_write_queue, skb);
18298c2ecf20Sopenharmony_ci}
18308c2ecf20Sopenharmony_ci
18318c2ecf20Sopenharmony_ci/**
18328c2ecf20Sopenharmony_ci * tcp_write_queue_empty - test if any payload (or FIN) is available in write queue
18338c2ecf20Sopenharmony_ci * @sk: socket
18348c2ecf20Sopenharmony_ci *
18358c2ecf20Sopenharmony_ci * Since the write queue can have a temporary empty skb in it,
18368c2ecf20Sopenharmony_ci * we must not use "return skb_queue_empty(&sk->sk_write_queue)"
18378c2ecf20Sopenharmony_ci */
18388c2ecf20Sopenharmony_cistatic inline bool tcp_write_queue_empty(const struct sock *sk)
18398c2ecf20Sopenharmony_ci{
18408c2ecf20Sopenharmony_ci	const struct tcp_sock *tp = tcp_sk(sk);
18418c2ecf20Sopenharmony_ci
18428c2ecf20Sopenharmony_ci	return tp->write_seq == tp->snd_nxt;
18438c2ecf20Sopenharmony_ci}
18448c2ecf20Sopenharmony_ci
18458c2ecf20Sopenharmony_cistatic inline bool tcp_rtx_queue_empty(const struct sock *sk)
18468c2ecf20Sopenharmony_ci{
18478c2ecf20Sopenharmony_ci	return RB_EMPTY_ROOT(&sk->tcp_rtx_queue);
18488c2ecf20Sopenharmony_ci}
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_cistatic inline bool tcp_rtx_and_write_queues_empty(const struct sock *sk)
18518c2ecf20Sopenharmony_ci{
18528c2ecf20Sopenharmony_ci	return tcp_rtx_queue_empty(sk) && tcp_write_queue_empty(sk);
18538c2ecf20Sopenharmony_ci}
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_cistatic inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
18568c2ecf20Sopenharmony_ci{
18578c2ecf20Sopenharmony_ci	__skb_queue_tail(&sk->sk_write_queue, skb);
18588c2ecf20Sopenharmony_ci
18598c2ecf20Sopenharmony_ci	/* Queue it, remembering where we must start sending. */
18608c2ecf20Sopenharmony_ci	if (sk->sk_write_queue.next == skb)
18618c2ecf20Sopenharmony_ci		tcp_chrono_start(sk, TCP_CHRONO_BUSY);
18628c2ecf20Sopenharmony_ci}
18638c2ecf20Sopenharmony_ci
18648c2ecf20Sopenharmony_ci/* Insert new before skb on the write queue of sk.  */
18658c2ecf20Sopenharmony_cistatic inline void tcp_insert_write_queue_before(struct sk_buff *new,
18668c2ecf20Sopenharmony_ci						  struct sk_buff *skb,
18678c2ecf20Sopenharmony_ci						  struct sock *sk)
18688c2ecf20Sopenharmony_ci{
18698c2ecf20Sopenharmony_ci	__skb_queue_before(&sk->sk_write_queue, skb, new);
18708c2ecf20Sopenharmony_ci}
18718c2ecf20Sopenharmony_ci
18728c2ecf20Sopenharmony_cistatic inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
18738c2ecf20Sopenharmony_ci{
18748c2ecf20Sopenharmony_ci	tcp_skb_tsorted_anchor_cleanup(skb);
18758c2ecf20Sopenharmony_ci	__skb_unlink(skb, &sk->sk_write_queue);
18768c2ecf20Sopenharmony_ci}
18778c2ecf20Sopenharmony_ci
18788c2ecf20Sopenharmony_civoid tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb);
18798c2ecf20Sopenharmony_ci
18808c2ecf20Sopenharmony_cistatic inline void tcp_rtx_queue_unlink(struct sk_buff *skb, struct sock *sk)
18818c2ecf20Sopenharmony_ci{
18828c2ecf20Sopenharmony_ci	tcp_skb_tsorted_anchor_cleanup(skb);
18838c2ecf20Sopenharmony_ci	rb_erase(&skb->rbnode, &sk->tcp_rtx_queue);
18848c2ecf20Sopenharmony_ci}
18858c2ecf20Sopenharmony_ci
18868c2ecf20Sopenharmony_cistatic inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct sock *sk)
18878c2ecf20Sopenharmony_ci{
18888c2ecf20Sopenharmony_ci	list_del(&skb->tcp_tsorted_anchor);
18898c2ecf20Sopenharmony_ci	tcp_rtx_queue_unlink(skb, sk);
18908c2ecf20Sopenharmony_ci	sk_wmem_free_skb(sk, skb);
18918c2ecf20Sopenharmony_ci}
18928c2ecf20Sopenharmony_ci
18938c2ecf20Sopenharmony_cistatic inline void tcp_push_pending_frames(struct sock *sk)
18948c2ecf20Sopenharmony_ci{
18958c2ecf20Sopenharmony_ci	if (tcp_send_head(sk)) {
18968c2ecf20Sopenharmony_ci		struct tcp_sock *tp = tcp_sk(sk);
18978c2ecf20Sopenharmony_ci
18988c2ecf20Sopenharmony_ci		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
18998c2ecf20Sopenharmony_ci	}
19008c2ecf20Sopenharmony_ci}
19018c2ecf20Sopenharmony_ci
19028c2ecf20Sopenharmony_ci/* Start sequence of the skb just after the highest skb with SACKed
19038c2ecf20Sopenharmony_ci * bit, valid only if sacked_out > 0 or when the caller has ensured
19048c2ecf20Sopenharmony_ci * validity by itself.
19058c2ecf20Sopenharmony_ci */
19068c2ecf20Sopenharmony_cistatic inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
19078c2ecf20Sopenharmony_ci{
19088c2ecf20Sopenharmony_ci	if (!tp->sacked_out)
19098c2ecf20Sopenharmony_ci		return tp->snd_una;
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci	if (tp->highest_sack == NULL)
19128c2ecf20Sopenharmony_ci		return tp->snd_nxt;
19138c2ecf20Sopenharmony_ci
19148c2ecf20Sopenharmony_ci	return TCP_SKB_CB(tp->highest_sack)->seq;
19158c2ecf20Sopenharmony_ci}
19168c2ecf20Sopenharmony_ci
19178c2ecf20Sopenharmony_cistatic inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
19188c2ecf20Sopenharmony_ci{
19198c2ecf20Sopenharmony_ci	tcp_sk(sk)->highest_sack = skb_rb_next(skb);
19208c2ecf20Sopenharmony_ci}
19218c2ecf20Sopenharmony_ci
19228c2ecf20Sopenharmony_cistatic inline struct sk_buff *tcp_highest_sack(struct sock *sk)
19238c2ecf20Sopenharmony_ci{
19248c2ecf20Sopenharmony_ci	return tcp_sk(sk)->highest_sack;
19258c2ecf20Sopenharmony_ci}
19268c2ecf20Sopenharmony_ci
19278c2ecf20Sopenharmony_cistatic inline void tcp_highest_sack_reset(struct sock *sk)
19288c2ecf20Sopenharmony_ci{
19298c2ecf20Sopenharmony_ci	tcp_sk(sk)->highest_sack = tcp_rtx_queue_head(sk);
19308c2ecf20Sopenharmony_ci}
19318c2ecf20Sopenharmony_ci
19328c2ecf20Sopenharmony_ci/* Called when old skb is about to be deleted and replaced by new skb */
19338c2ecf20Sopenharmony_cistatic inline void tcp_highest_sack_replace(struct sock *sk,
19348c2ecf20Sopenharmony_ci					    struct sk_buff *old,
19358c2ecf20Sopenharmony_ci					    struct sk_buff *new)
19368c2ecf20Sopenharmony_ci{
19378c2ecf20Sopenharmony_ci	if (old == tcp_highest_sack(sk))
19388c2ecf20Sopenharmony_ci		tcp_sk(sk)->highest_sack = new;
19398c2ecf20Sopenharmony_ci}
19408c2ecf20Sopenharmony_ci
19418c2ecf20Sopenharmony_ci/* This helper checks if socket has IP_TRANSPARENT set */
19428c2ecf20Sopenharmony_cistatic inline bool inet_sk_transparent(const struct sock *sk)
19438c2ecf20Sopenharmony_ci{
19448c2ecf20Sopenharmony_ci	switch (sk->sk_state) {
19458c2ecf20Sopenharmony_ci	case TCP_TIME_WAIT:
19468c2ecf20Sopenharmony_ci		return inet_twsk(sk)->tw_transparent;
19478c2ecf20Sopenharmony_ci	case TCP_NEW_SYN_RECV:
19488c2ecf20Sopenharmony_ci		return inet_rsk(inet_reqsk(sk))->no_srccheck;
19498c2ecf20Sopenharmony_ci	}
19508c2ecf20Sopenharmony_ci	return inet_sk(sk)->transparent;
19518c2ecf20Sopenharmony_ci}
19528c2ecf20Sopenharmony_ci
19538c2ecf20Sopenharmony_ci/* Determines whether this is a thin stream (which may suffer from
19548c2ecf20Sopenharmony_ci * increased latency). Used to trigger latency-reducing mechanisms.
19558c2ecf20Sopenharmony_ci */
19568c2ecf20Sopenharmony_cistatic inline bool tcp_stream_is_thin(struct tcp_sock *tp)
19578c2ecf20Sopenharmony_ci{
19588c2ecf20Sopenharmony_ci	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
19598c2ecf20Sopenharmony_ci}
19608c2ecf20Sopenharmony_ci
19618c2ecf20Sopenharmony_ci/* /proc */
19628c2ecf20Sopenharmony_cienum tcp_seq_states {
19638c2ecf20Sopenharmony_ci	TCP_SEQ_STATE_LISTENING,
19648c2ecf20Sopenharmony_ci	TCP_SEQ_STATE_ESTABLISHED,
19658c2ecf20Sopenharmony_ci};
19668c2ecf20Sopenharmony_ci
19678c2ecf20Sopenharmony_civoid *tcp_seq_start(struct seq_file *seq, loff_t *pos);
19688c2ecf20Sopenharmony_civoid *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos);
19698c2ecf20Sopenharmony_civoid tcp_seq_stop(struct seq_file *seq, void *v);
19708c2ecf20Sopenharmony_ci
19718c2ecf20Sopenharmony_cistruct tcp_seq_afinfo {
19728c2ecf20Sopenharmony_ci	sa_family_t			family;
19738c2ecf20Sopenharmony_ci};
19748c2ecf20Sopenharmony_ci
19758c2ecf20Sopenharmony_cistruct tcp_iter_state {
19768c2ecf20Sopenharmony_ci	struct seq_net_private	p;
19778c2ecf20Sopenharmony_ci	enum tcp_seq_states	state;
19788c2ecf20Sopenharmony_ci	struct sock		*syn_wait_sk;
19798c2ecf20Sopenharmony_ci	struct tcp_seq_afinfo	*bpf_seq_afinfo;
19808c2ecf20Sopenharmony_ci	int			bucket, offset, sbucket, num;
19818c2ecf20Sopenharmony_ci	loff_t			last_pos;
19828c2ecf20Sopenharmony_ci};
19838c2ecf20Sopenharmony_ci
19848c2ecf20Sopenharmony_ciextern struct request_sock_ops tcp_request_sock_ops;
19858c2ecf20Sopenharmony_ciextern struct request_sock_ops tcp6_request_sock_ops;
19868c2ecf20Sopenharmony_ci
19878c2ecf20Sopenharmony_civoid tcp_v4_destroy_sock(struct sock *sk);
19888c2ecf20Sopenharmony_ci
19898c2ecf20Sopenharmony_cistruct sk_buff *tcp_gso_segment(struct sk_buff *skb,
19908c2ecf20Sopenharmony_ci				netdev_features_t features);
19918c2ecf20Sopenharmony_cistruct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb);
19928c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(int tcp4_gro_complete(struct sk_buff *skb, int thoff));
19938c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb));
19948c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(int tcp6_gro_complete(struct sk_buff *skb, int thoff));
19958c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp6_gro_receive(struct list_head *head, struct sk_buff *skb));
19968c2ecf20Sopenharmony_ciint tcp_gro_complete(struct sk_buff *skb);
19978c2ecf20Sopenharmony_ci
19988c2ecf20Sopenharmony_civoid __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
19998c2ecf20Sopenharmony_ci
20008c2ecf20Sopenharmony_cistatic inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
20018c2ecf20Sopenharmony_ci{
20028c2ecf20Sopenharmony_ci	struct net *net = sock_net((struct sock *)tp);
20038c2ecf20Sopenharmony_ci	u32 val;
20048c2ecf20Sopenharmony_ci
20058c2ecf20Sopenharmony_ci	val = READ_ONCE(tp->notsent_lowat);
20068c2ecf20Sopenharmony_ci
20078c2ecf20Sopenharmony_ci	return val ?: READ_ONCE(net->ipv4.sysctl_tcp_notsent_lowat);
20088c2ecf20Sopenharmony_ci}
20098c2ecf20Sopenharmony_ci
20108c2ecf20Sopenharmony_ci/* @wake is one when sk_stream_write_space() calls us.
20118c2ecf20Sopenharmony_ci * This sends EPOLLOUT only if notsent_bytes is half the limit.
20128c2ecf20Sopenharmony_ci * This mimics the strategy used in sock_def_write_space().
20138c2ecf20Sopenharmony_ci */
20148c2ecf20Sopenharmony_cistatic inline bool tcp_stream_memory_free(const struct sock *sk, int wake)
20158c2ecf20Sopenharmony_ci{
20168c2ecf20Sopenharmony_ci	const struct tcp_sock *tp = tcp_sk(sk);
20178c2ecf20Sopenharmony_ci	u32 notsent_bytes = READ_ONCE(tp->write_seq) -
20188c2ecf20Sopenharmony_ci			    READ_ONCE(tp->snd_nxt);
20198c2ecf20Sopenharmony_ci
20208c2ecf20Sopenharmony_ci	return (notsent_bytes << wake) < tcp_notsent_lowat(tp);
20218c2ecf20Sopenharmony_ci}
20228c2ecf20Sopenharmony_ci
20238c2ecf20Sopenharmony_ci#ifdef CONFIG_PROC_FS
20248c2ecf20Sopenharmony_ciint tcp4_proc_init(void);
20258c2ecf20Sopenharmony_civoid tcp4_proc_exit(void);
20268c2ecf20Sopenharmony_ci#endif
20278c2ecf20Sopenharmony_ci
20288c2ecf20Sopenharmony_ciint tcp_rtx_synack(const struct sock *sk, struct request_sock *req);
20298c2ecf20Sopenharmony_ciint tcp_conn_request(struct request_sock_ops *rsk_ops,
20308c2ecf20Sopenharmony_ci		     const struct tcp_request_sock_ops *af_ops,
20318c2ecf20Sopenharmony_ci		     struct sock *sk, struct sk_buff *skb);
20328c2ecf20Sopenharmony_ci
20338c2ecf20Sopenharmony_ci/* TCP af-specific functions */
20348c2ecf20Sopenharmony_cistruct tcp_sock_af_ops {
20358c2ecf20Sopenharmony_ci#ifdef CONFIG_TCP_MD5SIG
20368c2ecf20Sopenharmony_ci	struct tcp_md5sig_key	*(*md5_lookup) (const struct sock *sk,
20378c2ecf20Sopenharmony_ci						const struct sock *addr_sk);
20388c2ecf20Sopenharmony_ci	int		(*calc_md5_hash)(char *location,
20398c2ecf20Sopenharmony_ci					 const struct tcp_md5sig_key *md5,
20408c2ecf20Sopenharmony_ci					 const struct sock *sk,
20418c2ecf20Sopenharmony_ci					 const struct sk_buff *skb);
20428c2ecf20Sopenharmony_ci	int		(*md5_parse)(struct sock *sk,
20438c2ecf20Sopenharmony_ci				     int optname,
20448c2ecf20Sopenharmony_ci				     sockptr_t optval,
20458c2ecf20Sopenharmony_ci				     int optlen);
20468c2ecf20Sopenharmony_ci#endif
20478c2ecf20Sopenharmony_ci};
20488c2ecf20Sopenharmony_ci
20498c2ecf20Sopenharmony_cistruct tcp_request_sock_ops {
20508c2ecf20Sopenharmony_ci	u16 mss_clamp;
20518c2ecf20Sopenharmony_ci#ifdef CONFIG_TCP_MD5SIG
20528c2ecf20Sopenharmony_ci	struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk,
20538c2ecf20Sopenharmony_ci						 const struct sock *addr_sk);
20548c2ecf20Sopenharmony_ci	int		(*calc_md5_hash) (char *location,
20558c2ecf20Sopenharmony_ci					  const struct tcp_md5sig_key *md5,
20568c2ecf20Sopenharmony_ci					  const struct sock *sk,
20578c2ecf20Sopenharmony_ci					  const struct sk_buff *skb);
20588c2ecf20Sopenharmony_ci#endif
20598c2ecf20Sopenharmony_ci	void (*init_req)(struct request_sock *req,
20608c2ecf20Sopenharmony_ci			 const struct sock *sk_listener,
20618c2ecf20Sopenharmony_ci			 struct sk_buff *skb);
20628c2ecf20Sopenharmony_ci#ifdef CONFIG_SYN_COOKIES
20638c2ecf20Sopenharmony_ci	__u32 (*cookie_init_seq)(const struct sk_buff *skb,
20648c2ecf20Sopenharmony_ci				 __u16 *mss);
20658c2ecf20Sopenharmony_ci#endif
20668c2ecf20Sopenharmony_ci	struct dst_entry *(*route_req)(const struct sock *sk, struct flowi *fl,
20678c2ecf20Sopenharmony_ci				       const struct request_sock *req);
20688c2ecf20Sopenharmony_ci	u32 (*init_seq)(const struct sk_buff *skb);
20698c2ecf20Sopenharmony_ci	u32 (*init_ts_off)(const struct net *net, const struct sk_buff *skb);
20708c2ecf20Sopenharmony_ci	int (*send_synack)(const struct sock *sk, struct dst_entry *dst,
20718c2ecf20Sopenharmony_ci			   struct flowi *fl, struct request_sock *req,
20728c2ecf20Sopenharmony_ci			   struct tcp_fastopen_cookie *foc,
20738c2ecf20Sopenharmony_ci			   enum tcp_synack_type synack_type,
20748c2ecf20Sopenharmony_ci			   struct sk_buff *syn_skb);
20758c2ecf20Sopenharmony_ci};
20768c2ecf20Sopenharmony_ci
20778c2ecf20Sopenharmony_ciextern const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops;
20788c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6)
20798c2ecf20Sopenharmony_ciextern const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops;
20808c2ecf20Sopenharmony_ci#endif
20818c2ecf20Sopenharmony_ci
20828c2ecf20Sopenharmony_ci#ifdef CONFIG_SYN_COOKIES
20838c2ecf20Sopenharmony_cistatic inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
20848c2ecf20Sopenharmony_ci					 const struct sock *sk, struct sk_buff *skb,
20858c2ecf20Sopenharmony_ci					 __u16 *mss)
20868c2ecf20Sopenharmony_ci{
20878c2ecf20Sopenharmony_ci	tcp_synq_overflow(sk);
20888c2ecf20Sopenharmony_ci	__NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
20898c2ecf20Sopenharmony_ci	return ops->cookie_init_seq(skb, mss);
20908c2ecf20Sopenharmony_ci}
20918c2ecf20Sopenharmony_ci#else
20928c2ecf20Sopenharmony_cistatic inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
20938c2ecf20Sopenharmony_ci					 const struct sock *sk, struct sk_buff *skb,
20948c2ecf20Sopenharmony_ci					 __u16 *mss)
20958c2ecf20Sopenharmony_ci{
20968c2ecf20Sopenharmony_ci	return 0;
20978c2ecf20Sopenharmony_ci}
20988c2ecf20Sopenharmony_ci#endif
20998c2ecf20Sopenharmony_ci
21008c2ecf20Sopenharmony_ciint tcpv4_offload_init(void);
21018c2ecf20Sopenharmony_ci
21028c2ecf20Sopenharmony_civoid tcp_v4_init(void);
21038c2ecf20Sopenharmony_civoid tcp_init(void);
21048c2ecf20Sopenharmony_ci
21058c2ecf20Sopenharmony_ci/* tcp_recovery.c */
21068c2ecf20Sopenharmony_civoid tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb);
21078c2ecf20Sopenharmony_civoid tcp_newreno_mark_lost(struct sock *sk, bool snd_una_advanced);
21088c2ecf20Sopenharmony_ciextern s32 tcp_rack_skb_timeout(struct tcp_sock *tp, struct sk_buff *skb,
21098c2ecf20Sopenharmony_ci				u32 reo_wnd);
21108c2ecf20Sopenharmony_ciextern bool tcp_rack_mark_lost(struct sock *sk);
21118c2ecf20Sopenharmony_ciextern void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq,
21128c2ecf20Sopenharmony_ci			     u64 xmit_time);
21138c2ecf20Sopenharmony_ciextern void tcp_rack_reo_timeout(struct sock *sk);
21148c2ecf20Sopenharmony_ciextern void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs);
21158c2ecf20Sopenharmony_ci
21168c2ecf20Sopenharmony_ci/* At how many usecs into the future should the RTO fire? */
21178c2ecf20Sopenharmony_cistatic inline s64 tcp_rto_delta_us(const struct sock *sk)
21188c2ecf20Sopenharmony_ci{
21198c2ecf20Sopenharmony_ci	const struct sk_buff *skb = tcp_rtx_queue_head(sk);
21208c2ecf20Sopenharmony_ci	u32 rto = inet_csk(sk)->icsk_rto;
21218c2ecf20Sopenharmony_ci	u64 rto_time_stamp_us = tcp_skb_timestamp_us(skb) + jiffies_to_usecs(rto);
21228c2ecf20Sopenharmony_ci
21238c2ecf20Sopenharmony_ci	return rto_time_stamp_us - tcp_sk(sk)->tcp_mstamp;
21248c2ecf20Sopenharmony_ci}
21258c2ecf20Sopenharmony_ci
21268c2ecf20Sopenharmony_ci/*
21278c2ecf20Sopenharmony_ci * Save and compile IPv4 options, return a pointer to it
21288c2ecf20Sopenharmony_ci */
21298c2ecf20Sopenharmony_cistatic inline struct ip_options_rcu *tcp_v4_save_options(struct net *net,
21308c2ecf20Sopenharmony_ci							 struct sk_buff *skb)
21318c2ecf20Sopenharmony_ci{
21328c2ecf20Sopenharmony_ci	const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
21338c2ecf20Sopenharmony_ci	struct ip_options_rcu *dopt = NULL;
21348c2ecf20Sopenharmony_ci
21358c2ecf20Sopenharmony_ci	if (opt->optlen) {
21368c2ecf20Sopenharmony_ci		int opt_size = sizeof(*dopt) + opt->optlen;
21378c2ecf20Sopenharmony_ci
21388c2ecf20Sopenharmony_ci		dopt = kmalloc(opt_size, GFP_ATOMIC);
21398c2ecf20Sopenharmony_ci		if (dopt && __ip_options_echo(net, &dopt->opt, skb, opt)) {
21408c2ecf20Sopenharmony_ci			kfree(dopt);
21418c2ecf20Sopenharmony_ci			dopt = NULL;
21428c2ecf20Sopenharmony_ci		}
21438c2ecf20Sopenharmony_ci	}
21448c2ecf20Sopenharmony_ci	return dopt;
21458c2ecf20Sopenharmony_ci}
21468c2ecf20Sopenharmony_ci
21478c2ecf20Sopenharmony_ci/* locally generated TCP pure ACKs have skb->truesize == 2
21488c2ecf20Sopenharmony_ci * (check tcp_send_ack() in net/ipv4/tcp_output.c )
21498c2ecf20Sopenharmony_ci * This is much faster than dissecting the packet to find out.
21508c2ecf20Sopenharmony_ci * (Think of GRE encapsulations, IPv4, IPv6, ...)
21518c2ecf20Sopenharmony_ci */
21528c2ecf20Sopenharmony_cistatic inline bool skb_is_tcp_pure_ack(const struct sk_buff *skb)
21538c2ecf20Sopenharmony_ci{
21548c2ecf20Sopenharmony_ci	return skb->truesize == 2;
21558c2ecf20Sopenharmony_ci}
21568c2ecf20Sopenharmony_ci
21578c2ecf20Sopenharmony_cistatic inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
21588c2ecf20Sopenharmony_ci{
21598c2ecf20Sopenharmony_ci	skb->truesize = 2;
21608c2ecf20Sopenharmony_ci}
21618c2ecf20Sopenharmony_ci
21628c2ecf20Sopenharmony_cistatic inline int tcp_inq(struct sock *sk)
21638c2ecf20Sopenharmony_ci{
21648c2ecf20Sopenharmony_ci	struct tcp_sock *tp = tcp_sk(sk);
21658c2ecf20Sopenharmony_ci	int answ;
21668c2ecf20Sopenharmony_ci
21678c2ecf20Sopenharmony_ci	if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
21688c2ecf20Sopenharmony_ci		answ = 0;
21698c2ecf20Sopenharmony_ci	} else if (sock_flag(sk, SOCK_URGINLINE) ||
21708c2ecf20Sopenharmony_ci		   !tp->urg_data ||
21718c2ecf20Sopenharmony_ci		   before(tp->urg_seq, tp->copied_seq) ||
21728c2ecf20Sopenharmony_ci		   !before(tp->urg_seq, tp->rcv_nxt)) {
21738c2ecf20Sopenharmony_ci
21748c2ecf20Sopenharmony_ci		answ = tp->rcv_nxt - tp->copied_seq;
21758c2ecf20Sopenharmony_ci
21768c2ecf20Sopenharmony_ci		/* Subtract 1, if FIN was received */
21778c2ecf20Sopenharmony_ci		if (answ && sock_flag(sk, SOCK_DONE))
21788c2ecf20Sopenharmony_ci			answ--;
21798c2ecf20Sopenharmony_ci	} else {
21808c2ecf20Sopenharmony_ci		answ = tp->urg_seq - tp->copied_seq;
21818c2ecf20Sopenharmony_ci	}
21828c2ecf20Sopenharmony_ci
21838c2ecf20Sopenharmony_ci	return answ;
21848c2ecf20Sopenharmony_ci}
21858c2ecf20Sopenharmony_ci
21868c2ecf20Sopenharmony_ciint tcp_peek_len(struct socket *sock);
21878c2ecf20Sopenharmony_ci
21888c2ecf20Sopenharmony_cistatic inline void tcp_segs_in(struct tcp_sock *tp, const struct sk_buff *skb)
21898c2ecf20Sopenharmony_ci{
21908c2ecf20Sopenharmony_ci	u16 segs_in;
21918c2ecf20Sopenharmony_ci
21928c2ecf20Sopenharmony_ci	segs_in = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
21938c2ecf20Sopenharmony_ci	tp->segs_in += segs_in;
21948c2ecf20Sopenharmony_ci	if (skb->len > tcp_hdrlen(skb))
21958c2ecf20Sopenharmony_ci		tp->data_segs_in += segs_in;
21968c2ecf20Sopenharmony_ci}
21978c2ecf20Sopenharmony_ci
21988c2ecf20Sopenharmony_ci/*
21998c2ecf20Sopenharmony_ci * TCP listen path runs lockless.
22008c2ecf20Sopenharmony_ci * We forced "struct sock" to be const qualified to make sure
22018c2ecf20Sopenharmony_ci * we don't modify one of its field by mistake.
22028c2ecf20Sopenharmony_ci * Here, we increment sk_drops which is an atomic_t, so we can safely
22038c2ecf20Sopenharmony_ci * make sock writable again.
22048c2ecf20Sopenharmony_ci */
22058c2ecf20Sopenharmony_cistatic inline void tcp_listendrop(const struct sock *sk)
22068c2ecf20Sopenharmony_ci{
22078c2ecf20Sopenharmony_ci	atomic_inc(&((struct sock *)sk)->sk_drops);
22088c2ecf20Sopenharmony_ci	__NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
22098c2ecf20Sopenharmony_ci}
22108c2ecf20Sopenharmony_ci
22118c2ecf20Sopenharmony_cienum hrtimer_restart tcp_pace_kick(struct hrtimer *timer);
22128c2ecf20Sopenharmony_ci
22138c2ecf20Sopenharmony_ci/*
22148c2ecf20Sopenharmony_ci * Interface for adding Upper Level Protocols over TCP
22158c2ecf20Sopenharmony_ci */
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_ci#define TCP_ULP_NAME_MAX	16
22188c2ecf20Sopenharmony_ci#define TCP_ULP_MAX		128
22198c2ecf20Sopenharmony_ci#define TCP_ULP_BUF_MAX		(TCP_ULP_NAME_MAX*TCP_ULP_MAX)
22208c2ecf20Sopenharmony_ci
22218c2ecf20Sopenharmony_cistruct tcp_ulp_ops {
22228c2ecf20Sopenharmony_ci	struct list_head	list;
22238c2ecf20Sopenharmony_ci
22248c2ecf20Sopenharmony_ci	/* initialize ulp */
22258c2ecf20Sopenharmony_ci	int (*init)(struct sock *sk);
22268c2ecf20Sopenharmony_ci	/* update ulp */
22278c2ecf20Sopenharmony_ci	void (*update)(struct sock *sk, struct proto *p,
22288c2ecf20Sopenharmony_ci		       void (*write_space)(struct sock *sk));
22298c2ecf20Sopenharmony_ci	/* cleanup ulp */
22308c2ecf20Sopenharmony_ci	void (*release)(struct sock *sk);
22318c2ecf20Sopenharmony_ci	/* diagnostic */
22328c2ecf20Sopenharmony_ci	int (*get_info)(const struct sock *sk, struct sk_buff *skb);
22338c2ecf20Sopenharmony_ci	size_t (*get_info_size)(const struct sock *sk);
22348c2ecf20Sopenharmony_ci	/* clone ulp */
22358c2ecf20Sopenharmony_ci	void (*clone)(const struct request_sock *req, struct sock *newsk,
22368c2ecf20Sopenharmony_ci		      const gfp_t priority);
22378c2ecf20Sopenharmony_ci
22388c2ecf20Sopenharmony_ci	char		name[TCP_ULP_NAME_MAX];
22398c2ecf20Sopenharmony_ci	struct module	*owner;
22408c2ecf20Sopenharmony_ci};
22418c2ecf20Sopenharmony_ciint tcp_register_ulp(struct tcp_ulp_ops *type);
22428c2ecf20Sopenharmony_civoid tcp_unregister_ulp(struct tcp_ulp_ops *type);
22438c2ecf20Sopenharmony_ciint tcp_set_ulp(struct sock *sk, const char *name);
22448c2ecf20Sopenharmony_civoid tcp_get_available_ulp(char *buf, size_t len);
22458c2ecf20Sopenharmony_civoid tcp_cleanup_ulp(struct sock *sk);
22468c2ecf20Sopenharmony_civoid tcp_update_ulp(struct sock *sk, struct proto *p,
22478c2ecf20Sopenharmony_ci		    void (*write_space)(struct sock *sk));
22488c2ecf20Sopenharmony_ci
22498c2ecf20Sopenharmony_ci#define MODULE_ALIAS_TCP_ULP(name)				\
22508c2ecf20Sopenharmony_ci	__MODULE_INFO(alias, alias_userspace, name);		\
22518c2ecf20Sopenharmony_ci	__MODULE_INFO(alias, alias_tcp_ulp, "tcp-ulp-" name)
22528c2ecf20Sopenharmony_ci
22538c2ecf20Sopenharmony_cistruct sk_msg;
22548c2ecf20Sopenharmony_cistruct sk_psock;
22558c2ecf20Sopenharmony_ci
22568c2ecf20Sopenharmony_ci#ifdef CONFIG_BPF_STREAM_PARSER
22578c2ecf20Sopenharmony_cistruct proto *tcp_bpf_get_proto(struct sock *sk, struct sk_psock *psock);
22588c2ecf20Sopenharmony_civoid tcp_bpf_clone(const struct sock *sk, struct sock *newsk);
22598c2ecf20Sopenharmony_ci#else
22608c2ecf20Sopenharmony_cistatic inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk)
22618c2ecf20Sopenharmony_ci{
22628c2ecf20Sopenharmony_ci}
22638c2ecf20Sopenharmony_ci#endif /* CONFIG_BPF_STREAM_PARSER */
22648c2ecf20Sopenharmony_ci
22658c2ecf20Sopenharmony_ci#ifdef CONFIG_NET_SOCK_MSG
22668c2ecf20Sopenharmony_ciint tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg, u32 bytes,
22678c2ecf20Sopenharmony_ci			  int flags);
22688c2ecf20Sopenharmony_ciint __tcp_bpf_recvmsg(struct sock *sk, struct sk_psock *psock,
22698c2ecf20Sopenharmony_ci		      struct msghdr *msg, int len, int flags);
22708c2ecf20Sopenharmony_ci#endif /* CONFIG_NET_SOCK_MSG */
22718c2ecf20Sopenharmony_ci
22728c2ecf20Sopenharmony_ci#ifdef CONFIG_CGROUP_BPF
22738c2ecf20Sopenharmony_cistatic inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
22748c2ecf20Sopenharmony_ci				      struct sk_buff *skb,
22758c2ecf20Sopenharmony_ci				      unsigned int end_offset)
22768c2ecf20Sopenharmony_ci{
22778c2ecf20Sopenharmony_ci	skops->skb = skb;
22788c2ecf20Sopenharmony_ci	skops->skb_data_end = skb->data + end_offset;
22798c2ecf20Sopenharmony_ci}
22808c2ecf20Sopenharmony_ci#else
22818c2ecf20Sopenharmony_cistatic inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops,
22828c2ecf20Sopenharmony_ci				      struct sk_buff *skb,
22838c2ecf20Sopenharmony_ci				      unsigned int end_offset)
22848c2ecf20Sopenharmony_ci{
22858c2ecf20Sopenharmony_ci}
22868c2ecf20Sopenharmony_ci#endif
22878c2ecf20Sopenharmony_ci
22888c2ecf20Sopenharmony_ci/* Call BPF_SOCK_OPS program that returns an int. If the return value
22898c2ecf20Sopenharmony_ci * is < 0, then the BPF op failed (for example if the loaded BPF
22908c2ecf20Sopenharmony_ci * program does not support the chosen operation or there is no BPF
22918c2ecf20Sopenharmony_ci * program loaded).
22928c2ecf20Sopenharmony_ci */
22938c2ecf20Sopenharmony_ci#ifdef CONFIG_BPF
22948c2ecf20Sopenharmony_cistatic inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
22958c2ecf20Sopenharmony_ci{
22968c2ecf20Sopenharmony_ci	struct bpf_sock_ops_kern sock_ops;
22978c2ecf20Sopenharmony_ci	int ret;
22988c2ecf20Sopenharmony_ci
22998c2ecf20Sopenharmony_ci	memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
23008c2ecf20Sopenharmony_ci	if (sk_fullsock(sk)) {
23018c2ecf20Sopenharmony_ci		sock_ops.is_fullsock = 1;
23028c2ecf20Sopenharmony_ci		sock_owned_by_me(sk);
23038c2ecf20Sopenharmony_ci	}
23048c2ecf20Sopenharmony_ci
23058c2ecf20Sopenharmony_ci	sock_ops.sk = sk;
23068c2ecf20Sopenharmony_ci	sock_ops.op = op;
23078c2ecf20Sopenharmony_ci	if (nargs > 0)
23088c2ecf20Sopenharmony_ci		memcpy(sock_ops.args, args, nargs * sizeof(*args));
23098c2ecf20Sopenharmony_ci
23108c2ecf20Sopenharmony_ci	ret = BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
23118c2ecf20Sopenharmony_ci	if (ret == 0)
23128c2ecf20Sopenharmony_ci		ret = sock_ops.reply;
23138c2ecf20Sopenharmony_ci	else
23148c2ecf20Sopenharmony_ci		ret = -1;
23158c2ecf20Sopenharmony_ci	return ret;
23168c2ecf20Sopenharmony_ci}
23178c2ecf20Sopenharmony_ci
23188c2ecf20Sopenharmony_cistatic inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
23198c2ecf20Sopenharmony_ci{
23208c2ecf20Sopenharmony_ci	u32 args[2] = {arg1, arg2};
23218c2ecf20Sopenharmony_ci
23228c2ecf20Sopenharmony_ci	return tcp_call_bpf(sk, op, 2, args);
23238c2ecf20Sopenharmony_ci}
23248c2ecf20Sopenharmony_ci
23258c2ecf20Sopenharmony_cistatic inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
23268c2ecf20Sopenharmony_ci				    u32 arg3)
23278c2ecf20Sopenharmony_ci{
23288c2ecf20Sopenharmony_ci	u32 args[3] = {arg1, arg2, arg3};
23298c2ecf20Sopenharmony_ci
23308c2ecf20Sopenharmony_ci	return tcp_call_bpf(sk, op, 3, args);
23318c2ecf20Sopenharmony_ci}
23328c2ecf20Sopenharmony_ci
23338c2ecf20Sopenharmony_ci#else
23348c2ecf20Sopenharmony_cistatic inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args)
23358c2ecf20Sopenharmony_ci{
23368c2ecf20Sopenharmony_ci	return -EPERM;
23378c2ecf20Sopenharmony_ci}
23388c2ecf20Sopenharmony_ci
23398c2ecf20Sopenharmony_cistatic inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2)
23408c2ecf20Sopenharmony_ci{
23418c2ecf20Sopenharmony_ci	return -EPERM;
23428c2ecf20Sopenharmony_ci}
23438c2ecf20Sopenharmony_ci
23448c2ecf20Sopenharmony_cistatic inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2,
23458c2ecf20Sopenharmony_ci				    u32 arg3)
23468c2ecf20Sopenharmony_ci{
23478c2ecf20Sopenharmony_ci	return -EPERM;
23488c2ecf20Sopenharmony_ci}
23498c2ecf20Sopenharmony_ci
23508c2ecf20Sopenharmony_ci#endif
23518c2ecf20Sopenharmony_ci
23528c2ecf20Sopenharmony_cistatic inline u32 tcp_timeout_init(struct sock *sk)
23538c2ecf20Sopenharmony_ci{
23548c2ecf20Sopenharmony_ci	int timeout;
23558c2ecf20Sopenharmony_ci
23568c2ecf20Sopenharmony_ci	timeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL);
23578c2ecf20Sopenharmony_ci
23588c2ecf20Sopenharmony_ci	if (timeout <= 0)
23598c2ecf20Sopenharmony_ci		timeout = TCP_TIMEOUT_INIT;
23608c2ecf20Sopenharmony_ci	return timeout;
23618c2ecf20Sopenharmony_ci}
23628c2ecf20Sopenharmony_ci
23638c2ecf20Sopenharmony_cistatic inline u32 tcp_rwnd_init_bpf(struct sock *sk)
23648c2ecf20Sopenharmony_ci{
23658c2ecf20Sopenharmony_ci	int rwnd;
23668c2ecf20Sopenharmony_ci
23678c2ecf20Sopenharmony_ci	rwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL);
23688c2ecf20Sopenharmony_ci
23698c2ecf20Sopenharmony_ci	if (rwnd < 0)
23708c2ecf20Sopenharmony_ci		rwnd = 0;
23718c2ecf20Sopenharmony_ci	return rwnd;
23728c2ecf20Sopenharmony_ci}
23738c2ecf20Sopenharmony_ci
23748c2ecf20Sopenharmony_cistatic inline bool tcp_bpf_ca_needs_ecn(struct sock *sk)
23758c2ecf20Sopenharmony_ci{
23768c2ecf20Sopenharmony_ci	return (tcp_call_bpf(sk, BPF_SOCK_OPS_NEEDS_ECN, 0, NULL) == 1);
23778c2ecf20Sopenharmony_ci}
23788c2ecf20Sopenharmony_ci
23798c2ecf20Sopenharmony_cistatic inline void tcp_bpf_rtt(struct sock *sk)
23808c2ecf20Sopenharmony_ci{
23818c2ecf20Sopenharmony_ci	if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_RTT_CB_FLAG))
23828c2ecf20Sopenharmony_ci		tcp_call_bpf(sk, BPF_SOCK_OPS_RTT_CB, 0, NULL);
23838c2ecf20Sopenharmony_ci}
23848c2ecf20Sopenharmony_ci
23858c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_SMC)
23868c2ecf20Sopenharmony_ciextern struct static_key_false tcp_have_smc;
23878c2ecf20Sopenharmony_ci#endif
23888c2ecf20Sopenharmony_ci
23898c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_TLS_DEVICE)
23908c2ecf20Sopenharmony_civoid clean_acked_data_enable(struct inet_connection_sock *icsk,
23918c2ecf20Sopenharmony_ci			     void (*cad)(struct sock *sk, u32 ack_seq));
23928c2ecf20Sopenharmony_civoid clean_acked_data_disable(struct inet_connection_sock *icsk);
23938c2ecf20Sopenharmony_civoid clean_acked_data_flush(void);
23948c2ecf20Sopenharmony_ci#endif
23958c2ecf20Sopenharmony_ci
23968c2ecf20Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(tcp_tx_delay_enabled);
23978c2ecf20Sopenharmony_cistatic inline void tcp_add_tx_delay(struct sk_buff *skb,
23988c2ecf20Sopenharmony_ci				    const struct tcp_sock *tp)
23998c2ecf20Sopenharmony_ci{
24008c2ecf20Sopenharmony_ci	if (static_branch_unlikely(&tcp_tx_delay_enabled))
24018c2ecf20Sopenharmony_ci		skb->skb_mstamp_ns += (u64)tp->tcp_tx_delay * NSEC_PER_USEC;
24028c2ecf20Sopenharmony_ci}
24038c2ecf20Sopenharmony_ci
24048c2ecf20Sopenharmony_ci/* Compute Earliest Departure Time for some control packets
24058c2ecf20Sopenharmony_ci * like ACK or RST for TIME_WAIT or non ESTABLISHED sockets.
24068c2ecf20Sopenharmony_ci */
24078c2ecf20Sopenharmony_cistatic inline u64 tcp_transmit_time(const struct sock *sk)
24088c2ecf20Sopenharmony_ci{
24098c2ecf20Sopenharmony_ci	if (static_branch_unlikely(&tcp_tx_delay_enabled)) {
24108c2ecf20Sopenharmony_ci		u32 delay = (sk->sk_state == TCP_TIME_WAIT) ?
24118c2ecf20Sopenharmony_ci			tcp_twsk(sk)->tw_tx_delay : tcp_sk(sk)->tcp_tx_delay;
24128c2ecf20Sopenharmony_ci
24138c2ecf20Sopenharmony_ci		return tcp_clock_ns() + (u64)delay * NSEC_PER_USEC;
24148c2ecf20Sopenharmony_ci	}
24158c2ecf20Sopenharmony_ci	return 0;
24168c2ecf20Sopenharmony_ci}
24178c2ecf20Sopenharmony_ci
24188c2ecf20Sopenharmony_ci#endif	/* _TCP_H */
2419