xref: /linux/include/net/tcp.h (revision 659a8ad56f490279f0efee43a62ffa1ac914a4e0)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Definitions for the TCP module.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Version:	@(#)tcp.h	1.0.5	05/23/93
91da177e4SLinus Torvalds  *
1002c30a84SJesper Juhl  * Authors:	Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
141da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
151da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
161da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds #ifndef _TCP_H
191da177e4SLinus Torvalds #define _TCP_H
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #define FASTRETRANS_DEBUG 1
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/list.h>
241da177e4SLinus Torvalds #include <linux/tcp.h>
25187f1882SPaul Gortmaker #include <linux/bug.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/cache.h>
281da177e4SLinus Torvalds #include <linux/percpu.h>
29fb286bb2SHerbert Xu #include <linux/skbuff.h>
30cfb6eeb4SYOSHIFUJI Hideaki #include <linux/crypto.h>
31c6aefafbSGlenn Griffin #include <linux/cryptohash.h>
32435cf559SWilliam Allen Simpson #include <linux/kref.h>
33740b0f18SEric Dumazet #include <linux/ktime.h>
343f421baaSArnaldo Carvalho de Melo 
353f421baaSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
36295ff7edSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h>
3777d8bf9cSArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
381da177e4SLinus Torvalds #include <net/checksum.h>
392e6599cbSArnaldo Carvalho de Melo #include <net/request_sock.h>
401da177e4SLinus Torvalds #include <net/sock.h>
411da177e4SLinus Torvalds #include <net/snmp.h>
421da177e4SLinus Torvalds #include <net/ip.h>
43c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
44bdf1ee5dSIlpo Järvinen #include <net/inet_ecn.h>
450c266898SSatoru SATOH #include <net/dst.h>
46c752f073SArnaldo Carvalho de Melo 
471da177e4SLinus Torvalds #include <linux/seq_file.h>
48180d8cd9SGlauber Costa #include <linux/memcontrol.h>
491da177e4SLinus Torvalds 
500f7ff927SArnaldo Carvalho de Melo extern struct inet_hashinfo tcp_hashinfo;
511da177e4SLinus Torvalds 
52dd24c001SEric Dumazet extern struct percpu_counter tcp_orphan_count;
535c9f3023SJoe Perches void tcp_time_wait(struct sock *sk, int state, int timeo);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds #define MAX_TCP_HEADER	(128 + MAX_HEADER)
5633ad798cSAdam Langley #define MAX_TCP_OPTION_SPACE 40
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /*
591da177e4SLinus Torvalds  * Never offer a window over 32767 without using window scaling. Some
601da177e4SLinus Torvalds  * poor stacks do signed 16bit maths!
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds #define MAX_TCP_WINDOW		32767U
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
651da177e4SLinus Torvalds #define TCP_MIN_MSS		88U
661da177e4SLinus Torvalds 
675d424d5aSJohn Heffner /* The least MTU to use for probing */
68dcd8fb85SFan Du #define TCP_BASE_MSS		1024
695d424d5aSJohn Heffner 
7005cbc0dbSFan Du /* probing interval, default to 10 minutes as per RFC4821 */
7105cbc0dbSFan Du #define TCP_PROBE_INTERVAL	600
7205cbc0dbSFan Du 
736b58e0a5SFan Du /* Specify interval when tcp mtu probing will stop */
746b58e0a5SFan Du #define TCP_PROBE_THRESHOLD	8
756b58e0a5SFan Du 
761da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
771da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
801da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /* urg_data states */
831da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
841da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
851da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #define TCP_RETR1	3	/*
881da177e4SLinus Torvalds 				 * This is how many retries it does before it
891da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
901da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
911da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
921da177e4SLinus Torvalds 				 */
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #define TCP_RETR2	15	/*
951da177e4SLinus Torvalds 				 * This should take at least
961da177e4SLinus Torvalds 				 * 90 minutes to time out.
971da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
981da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
991da177e4SLinus Torvalds 				 */
1001da177e4SLinus Torvalds 
1016c9ff979SAlex Bergmann #define TCP_SYN_RETRIES	 6	/* This is how many retries are done
1026c9ff979SAlex Bergmann 				 * when active opening a connection.
1036c9ff979SAlex Bergmann 				 * RFC1122 says the minimum retry MUST
1046c9ff979SAlex Bergmann 				 * be at least 180secs.  Nevertheless
1056c9ff979SAlex Bergmann 				 * this value is corresponding to
1066c9ff979SAlex Bergmann 				 * 63secs of retransmission with the
1076c9ff979SAlex Bergmann 				 * current initial RTO.
1086c9ff979SAlex Bergmann 				 */
1091da177e4SLinus Torvalds 
1106c9ff979SAlex Bergmann #define TCP_SYNACK_RETRIES 5	/* This is how may retries are done
1116c9ff979SAlex Bergmann 				 * when passive opening a connection.
1126c9ff979SAlex Bergmann 				 * This is corresponding to 31secs of
1136c9ff979SAlex Bergmann 				 * retransmission with the current
1146c9ff979SAlex Bergmann 				 * initial RTO.
1156c9ff979SAlex Bergmann 				 */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1181da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1191da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1201da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1211da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1221da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1231da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1241da177e4SLinus Torvalds 				  */
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1271da177e4SLinus Torvalds #if HZ >= 100
1281da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1291da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1301da177e4SLinus Torvalds #else
1311da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1321da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1331da177e4SLinus Torvalds #endif
1341da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1351da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
136fd4f2ceaSEric Dumazet #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))	/* RFC6298 2.1 initial RTO value	*/
1379ad7c049SJerry Chu #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value, now
1389ad7c049SJerry Chu 						 * used as a fallback RTO for the
1399ad7c049SJerry Chu 						 * initial data transmission if no
1409ad7c049SJerry Chu 						 * valid RTT sample has been acquired,
1419ad7c049SJerry Chu 						 * most likely due to retrans in 3WHS.
1429ad7c049SJerry Chu 						 */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1451da177e4SLinus Torvalds 					                 * for local resources.
1461da177e4SLinus Torvalds 					                 */
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1491da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1501da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1531da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1541da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1551da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1601da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1611da177e4SLinus Torvalds 					 * after this time. It should be equal
1621da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1631da177e4SLinus Torvalds 					 * to provide reliability equal to one
1641da177e4SLinus Torvalds 					 * provided by timewait state.
1651da177e4SLinus Torvalds 					 */
1661da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1671da177e4SLinus Torvalds 					 * timestamps. It must be less than
1681da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1691da177e4SLinus Torvalds 					 */
1701da177e4SLinus Torvalds /*
1711da177e4SLinus Torvalds  *	TCP option
1721da177e4SLinus Torvalds  */
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1751da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1761da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1771da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1781da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1791da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1801da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
181cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
1827f9b838bSDaniel Lee #define TCPOPT_FASTOPEN		34	/* Fast open (RFC7413) */
1832100c8d2SYuchung Cheng #define TCPOPT_EXP		254	/* Experimental */
1842100c8d2SYuchung Cheng /* Magic number to be after the option value for sharing TCP
1852100c8d2SYuchung Cheng  * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
1862100c8d2SYuchung Cheng  */
1872100c8d2SYuchung Cheng #define TCPOPT_FASTOPEN_MAGIC	0xF989
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
1901da177e4SLinus Torvalds  *     TCP option lengths
1911da177e4SLinus Torvalds  */
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds #define TCPOLEN_MSS            4
1941da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
1951da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
1961da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
197cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG         18
1987f9b838bSDaniel Lee #define TCPOLEN_FASTOPEN_BASE  2
1992100c8d2SYuchung Cheng #define TCPOLEN_EXP_FASTOPEN_BASE  4
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /* But this is what stacks really send out. */
2021da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
2031da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
2041da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
2051da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
2061da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
2071da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
208cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED		20
20933ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED		4
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* Flags in tp->nonagle */
2121da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
2131da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
214caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
2151da177e4SLinus Torvalds 
21636e31b0aSAndreas Petlund /* TCP thin-stream limits */
21736e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
21836e31b0aSAndreas Petlund 
2197eb38527SDavid S. Miller /* TCP initial congestion window as per draft-hkchu-tcpm-initcwnd-01 */
220442b9635SDavid S. Miller #define TCP_INIT_CWND		10
221442b9635SDavid S. Miller 
222cf60af03SYuchung Cheng /* Bit Flags for sysctl_tcp_fastopen */
223cf60af03SYuchung Cheng #define	TFO_CLIENT_ENABLE	1
22410467163SJerry Chu #define	TFO_SERVER_ENABLE	2
22567da22d2SYuchung Cheng #define	TFO_CLIENT_NO_COOKIE	4	/* Data in SYN w/o cookie option */
226cf60af03SYuchung Cheng 
22710467163SJerry Chu /* Accept SYN data w/o any cookie option */
22810467163SJerry Chu #define	TFO_SERVER_COOKIE_NOT_REQD	0x200
22910467163SJerry Chu 
23010467163SJerry Chu /* Force enable TFO on all listeners, i.e., not requiring the
23110467163SJerry Chu  * TCP_FASTOPEN socket option. SOCKOPT1/2 determine how to set max_qlen.
23210467163SJerry Chu  */
23310467163SJerry Chu #define	TFO_SERVER_WO_SOCKOPT1	0x400
23410467163SJerry Chu #define	TFO_SERVER_WO_SOCKOPT2	0x800
23510467163SJerry Chu 
236295ff7edSArnaldo Carvalho de Melo extern struct inet_timewait_death_row tcp_death_row;
237295ff7edSArnaldo Carvalho de Melo 
2381da177e4SLinus Torvalds /* sysctl variables for tcp */
2391da177e4SLinus Torvalds extern int sysctl_tcp_timestamps;
2401da177e4SLinus Torvalds extern int sysctl_tcp_window_scaling;
2411da177e4SLinus Torvalds extern int sysctl_tcp_sack;
2421da177e4SLinus Torvalds extern int sysctl_tcp_fin_timeout;
2431da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_time;
2441da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_probes;
2451da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_intvl;
2461da177e4SLinus Torvalds extern int sysctl_tcp_syn_retries;
2471da177e4SLinus Torvalds extern int sysctl_tcp_synack_retries;
2481da177e4SLinus Torvalds extern int sysctl_tcp_retries1;
2491da177e4SLinus Torvalds extern int sysctl_tcp_retries2;
2501da177e4SLinus Torvalds extern int sysctl_tcp_orphan_retries;
2511da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
2522100c8d2SYuchung Cheng extern int sysctl_tcp_fastopen;
2531da177e4SLinus Torvalds extern int sysctl_tcp_retrans_collapse;
2541da177e4SLinus Torvalds extern int sysctl_tcp_stdurg;
2551da177e4SLinus Torvalds extern int sysctl_tcp_rfc1337;
2561da177e4SLinus Torvalds extern int sysctl_tcp_abort_on_overflow;
2571da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
2581da177e4SLinus Torvalds extern int sysctl_tcp_fack;
2591da177e4SLinus Torvalds extern int sysctl_tcp_reordering;
260dca145ffSEric Dumazet extern int sysctl_tcp_max_reordering;
2611da177e4SLinus Torvalds extern int sysctl_tcp_dsack;
262a4fe34bfSEric W. Biederman extern long sysctl_tcp_mem[3];
2631da177e4SLinus Torvalds extern int sysctl_tcp_wmem[3];
2641da177e4SLinus Torvalds extern int sysctl_tcp_rmem[3];
2651da177e4SLinus Torvalds extern int sysctl_tcp_app_win;
2661da177e4SLinus Torvalds extern int sysctl_tcp_adv_win_scale;
2671da177e4SLinus Torvalds extern int sysctl_tcp_tw_reuse;
2681da177e4SLinus Torvalds extern int sysctl_tcp_frto;
2691da177e4SLinus Torvalds extern int sysctl_tcp_low_latency;
2701da177e4SLinus Torvalds extern int sysctl_tcp_nometrics_save;
2711da177e4SLinus Torvalds extern int sysctl_tcp_moderate_rcvbuf;
2721da177e4SLinus Torvalds extern int sysctl_tcp_tso_win_divisor;
27315d99e02SRick Jones extern int sysctl_tcp_workaround_signed_windows;
27435089bb2SDavid S. Miller extern int sysctl_tcp_slow_start_after_idle;
27536e31b0aSAndreas Petlund extern int sysctl_tcp_thin_linear_timeouts;
2767e380175SAndreas Petlund extern int sysctl_tcp_thin_dupack;
277eed530b6SYuchung Cheng extern int sysctl_tcp_early_retrans;
27846d3ceabSEric Dumazet extern int sysctl_tcp_limit_output_bytes;
279282f23c6SEric Dumazet extern int sysctl_tcp_challenge_ack_limit;
280c9bee3b7SEric Dumazet extern unsigned int sysctl_tcp_notsent_lowat;
28195bd09ebSEric Dumazet extern int sysctl_tcp_min_tso_segs;
282f6722583SYuchung Cheng extern int sysctl_tcp_min_rtt_wlen;
283f54b3111SEric Dumazet extern int sysctl_tcp_autocorking;
284032ee423SNeal Cardwell extern int sysctl_tcp_invalid_ratelimit;
28543e122b0SEric Dumazet extern int sysctl_tcp_pacing_ss_ratio;
28643e122b0SEric Dumazet extern int sysctl_tcp_pacing_ca_ratio;
2871da177e4SLinus Torvalds 
2888d987e5cSEric Dumazet extern atomic_long_t tcp_memory_allocated;
2891748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated;
2901da177e4SLinus Torvalds extern int tcp_memory_pressure;
2911da177e4SLinus Torvalds 
292b8da51ebSEric Dumazet /* optimized version of sk_under_memory_pressure() for TCP sockets */
293b8da51ebSEric Dumazet static inline bool tcp_under_memory_pressure(const struct sock *sk)
294b8da51ebSEric Dumazet {
295b8da51ebSEric Dumazet 	if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
296b8da51ebSEric Dumazet 		return !!sk->sk_cgrp->memory_pressure;
297b8da51ebSEric Dumazet 
298b8da51ebSEric Dumazet 	return tcp_memory_pressure;
299b8da51ebSEric Dumazet }
3001da177e4SLinus Torvalds /*
3011da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
3021da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
3031da177e4SLinus Torvalds  */
3041da177e4SLinus Torvalds 
305a2a385d6SEric Dumazet static inline bool before(__u32 seq1, __u32 seq2)
3061da177e4SLinus Torvalds {
3070d630cc0SGerrit Renker         return (__s32)(seq1-seq2) < 0;
3081da177e4SLinus Torvalds }
3099a036b9cSGerrit Renker #define after(seq2, seq1) 	before(seq1, seq2)
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
312a2a385d6SEric Dumazet static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
317efcdbf24SArun Sharma static inline bool tcp_out_of_memory(struct sock *sk)
318efcdbf24SArun Sharma {
319efcdbf24SArun Sharma 	if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
320efcdbf24SArun Sharma 	    sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
321efcdbf24SArun Sharma 		return true;
322efcdbf24SArun Sharma 	return false;
323efcdbf24SArun Sharma }
324efcdbf24SArun Sharma 
325a6c5ea4cSEric Dumazet void sk_forced_mem_schedule(struct sock *sk, int size);
326a6c5ea4cSEric Dumazet 
327ad1af0feSDavid S. Miller static inline bool tcp_too_many_orphans(struct sock *sk, int shift)
328e4fd5da3SPavel Emelianov {
329ad1af0feSDavid S. Miller 	struct percpu_counter *ocp = sk->sk_prot->orphan_count;
330ad1af0feSDavid S. Miller 	int orphans = percpu_counter_read_positive(ocp);
331ad1af0feSDavid S. Miller 
332ad1af0feSDavid S. Miller 	if (orphans << shift > sysctl_tcp_max_orphans) {
333ad1af0feSDavid S. Miller 		orphans = percpu_counter_sum_positive(ocp);
334ad1af0feSDavid S. Miller 		if (orphans << shift > sysctl_tcp_max_orphans)
335ad1af0feSDavid S. Miller 			return true;
336ad1af0feSDavid S. Miller 	}
337ad1af0feSDavid S. Miller 	return false;
338e4fd5da3SPavel Emelianov }
3391da177e4SLinus Torvalds 
3405c9f3023SJoe Perches bool tcp_check_oom(struct sock *sk, int shift);
341efcdbf24SArun Sharma 
342a0f82f64SFlorian Westphal 
3431da177e4SLinus Torvalds extern struct proto tcp_prot;
3441da177e4SLinus Torvalds 
34557ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
34657ef42d5SPavel Emelyanov #define TCP_INC_STATS_BH(net, field)	SNMP_INC_STATS_BH((net)->mib.tcp_statistics, field)
34757ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
34857ef42d5SPavel Emelyanov #define TCP_ADD_STATS_USER(net, field, val) SNMP_ADD_STATS_USER((net)->mib.tcp_statistics, field, val)
349aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
3501da177e4SLinus Torvalds 
3515c9f3023SJoe Perches void tcp_tasklet_init(void);
35246d3ceabSEric Dumazet 
3535c9f3023SJoe Perches void tcp_v4_err(struct sk_buff *skb, u32);
3541da177e4SLinus Torvalds 
3555c9f3023SJoe Perches void tcp_shutdown(struct sock *sk, int how);
3561da177e4SLinus Torvalds 
3575c9f3023SJoe Perches void tcp_v4_early_demux(struct sk_buff *skb);
3585c9f3023SJoe Perches int tcp_v4_rcv(struct sk_buff *skb);
3591da177e4SLinus Torvalds 
3605c9f3023SJoe Perches int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
3611b784140SYing Xue int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
3625c9f3023SJoe Perches int tcp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
3635c9f3023SJoe Perches 		 int flags);
3645c9f3023SJoe Perches void tcp_release_cb(struct sock *sk);
3655c9f3023SJoe Perches void tcp_wfree(struct sk_buff *skb);
3665c9f3023SJoe Perches void tcp_write_timer_handler(struct sock *sk);
3675c9f3023SJoe Perches void tcp_delack_timer_handler(struct sock *sk);
3685c9f3023SJoe Perches int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
36972ab4a86SEric Dumazet int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb);
3705c9f3023SJoe Perches void tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
371cf533ea5SEric Dumazet 			 const struct tcphdr *th, unsigned int len);
3725c9f3023SJoe Perches void tcp_rcv_space_adjust(struct sock *sk);
3735c9f3023SJoe Perches int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
3745c9f3023SJoe Perches void tcp_twsk_destructor(struct sock *sk);
3755c9f3023SJoe Perches ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
37653d3176bSChangli Gao 			struct pipe_inode_info *pipe, size_t len,
37753d3176bSChangli Gao 			unsigned int flags);
3789c55e01cSJens Axboe 
379463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
380463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3811da177e4SLinus Torvalds {
382463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
383fc6415bcSDavid S. Miller 
384463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
385463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
386463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3871da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
388463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
389fc6415bcSDavid S. Miller 		} else
390463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
394bdf1ee5dSIlpo Järvinen #define	TCP_ECN_OK		1
395bdf1ee5dSIlpo Järvinen #define	TCP_ECN_QUEUE_CWR	2
396bdf1ee5dSIlpo Järvinen #define	TCP_ECN_DEMAND_CWR	4
3977a269ffaSEric Dumazet #define	TCP_ECN_SEEN		8
398bdf1ee5dSIlpo Järvinen 
399fd2c3ef7SEric Dumazet enum tcp_tw_status {
4001da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
4011da177e4SLinus Torvalds 	TCP_TW_RST = 1,
4021da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
4031da177e4SLinus Torvalds 	TCP_TW_SYN = 3
4041da177e4SLinus Torvalds };
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 
4075c9f3023SJoe Perches enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
4081da177e4SLinus Torvalds 					      struct sk_buff *skb,
4098feaf0c0SArnaldo Carvalho de Melo 					      const struct tcphdr *th);
4105c9f3023SJoe Perches struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
41152452c54SEric Dumazet 			   struct request_sock *req, bool fastopen);
4125c9f3023SJoe Perches int tcp_child_process(struct sock *parent, struct sock *child,
4131da177e4SLinus Torvalds 		      struct sk_buff *skb);
4145ae344c9SNeal Cardwell void tcp_enter_loss(struct sock *sk);
4155c9f3023SJoe Perches void tcp_clear_retrans(struct tcp_sock *tp);
4165c9f3023SJoe Perches void tcp_update_metrics(struct sock *sk);
4175c9f3023SJoe Perches void tcp_init_metrics(struct sock *sk);
4185c9f3023SJoe Perches void tcp_metrics_init(void);
4195c9f3023SJoe Perches bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
420a26552afSHannes Frederic Sowa 			bool paws_check, bool timestamps);
4215c9f3023SJoe Perches bool tcp_remember_stamp(struct sock *sk);
4225c9f3023SJoe Perches bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw);
4235c9f3023SJoe Perches void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst);
4245c9f3023SJoe Perches void tcp_disable_fack(struct tcp_sock *tp);
4255c9f3023SJoe Perches void tcp_close(struct sock *sk, long timeout);
4265c9f3023SJoe Perches void tcp_init_sock(struct sock *sk);
4275c9f3023SJoe Perches unsigned int tcp_poll(struct file *file, struct socket *sock,
42853d3176bSChangli Gao 		      struct poll_table_struct *wait);
4295c9f3023SJoe Perches int tcp_getsockopt(struct sock *sk, int level, int optname,
4303fdadf7dSDmitry Mishin 		   char __user *optval, int __user *optlen);
4315c9f3023SJoe Perches int tcp_setsockopt(struct sock *sk, int level, int optname,
43253d3176bSChangli Gao 		   char __user *optval, unsigned int optlen);
4335c9f3023SJoe Perches int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
43453d3176bSChangli Gao 			  char __user *optval, int __user *optlen);
4355c9f3023SJoe Perches int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
436b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen);
4375c9f3023SJoe Perches void tcp_set_keepalive(struct sock *sk, int val);
43842cb80a2SEric Dumazet void tcp_syn_ack_timeout(const struct request_sock *req);
4391b784140SYing Xue int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
4401b784140SYing Xue 		int flags, int *addr_len);
4415c9f3023SJoe Perches void tcp_parse_options(const struct sk_buff *skb,
4421a2c6181SChristoph Paasch 		       struct tcp_options_received *opt_rx,
4432100c8d2SYuchung Cheng 		       int estab, struct tcp_fastopen_cookie *foc);
4445c9f3023SJoe Perches const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
4457d5d5525SYOSHIFUJI Hideaki 
4461da177e4SLinus Torvalds /*
4471da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4481da177e4SLinus Torvalds  */
4491da177e4SLinus Torvalds 
4505c9f3023SJoe Perches void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
4514fab9071SNeal Cardwell void tcp_v4_mtu_reduced(struct sock *sk);
45226e37360SEric Dumazet void tcp_req_err(struct sock *sk, u32 seq);
4535c9f3023SJoe Perches int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
454c28c6f04SEric Dumazet struct sock *tcp_create_openreq_child(const struct sock *sk,
45560236fddSArnaldo Carvalho de Melo 				      struct request_sock *req,
4561da177e4SLinus Torvalds 				      struct sk_buff *skb);
45781164413SDaniel Borkmann void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst);
4580c27171eSEric Dumazet struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
45960236fddSArnaldo Carvalho de Melo 				  struct request_sock *req,
4601da177e4SLinus Torvalds 				  struct dst_entry *dst);
4615c9f3023SJoe Perches int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
4625c9f3023SJoe Perches int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
4635c9f3023SJoe Perches int tcp_connect(struct sock *sk);
4645d062de7SEric Dumazet struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst,
465e6b4d113SWilliam Allen Simpson 				struct request_sock *req,
466ca6fb065SEric Dumazet 				struct tcp_fastopen_cookie *foc,
467ca6fb065SEric Dumazet 				bool attach_req);
4685c9f3023SJoe Perches int tcp_disconnect(struct sock *sk, int flags);
4691da177e4SLinus Torvalds 
470370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
471292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
47263d02d15SEric Dumazet void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds /* From syncookies.c */
475b80c0e78SEric Dumazet struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
476b80c0e78SEric Dumazet 				 struct request_sock *req,
477b80c0e78SEric Dumazet 				 struct dst_entry *dst);
4785c9f3023SJoe Perches int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
4790198230bSPatrick McHardy 		      u32 cookie);
480461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
481e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
4828c27bd75SFlorian Westphal 
48363262315SEric Dumazet /* Syncookies use a monotonic timer which increments every 60 seconds.
4848c27bd75SFlorian Westphal  * This counter is used both as a hash input and partially encoded into
4858c27bd75SFlorian Westphal  * the cookie value.  A cookie is only validated further if the delta
4868c27bd75SFlorian Westphal  * between the current counter value and the encoded one is less than this,
48763262315SEric Dumazet  * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
4888c27bd75SFlorian Westphal  * the counter advances immediately after a cookie is generated).
4898c27bd75SFlorian Westphal  */
4908c27bd75SFlorian Westphal #define MAX_SYNCOOKIE_AGE	2
491264ea103SEric Dumazet #define TCP_SYNCOOKIE_PERIOD	(60 * HZ)
492264ea103SEric Dumazet #define TCP_SYNCOOKIE_VALID	(MAX_SYNCOOKIE_AGE * TCP_SYNCOOKIE_PERIOD)
493264ea103SEric Dumazet 
494264ea103SEric Dumazet /* syncookies: remember time of last synqueue overflow
495264ea103SEric Dumazet  * But do not dirty this field too often (once per second is enough)
4963f684b4bSEric Dumazet  * It is racy as we do not hold a lock, but race is very minor.
497264ea103SEric Dumazet  */
4983f684b4bSEric Dumazet static inline void tcp_synq_overflow(const struct sock *sk)
499264ea103SEric Dumazet {
500264ea103SEric Dumazet 	unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
501264ea103SEric Dumazet 	unsigned long now = jiffies;
502264ea103SEric Dumazet 
503264ea103SEric Dumazet 	if (time_after(now, last_overflow + HZ))
504264ea103SEric Dumazet 		tcp_sk(sk)->rx_opt.ts_recent_stamp = now;
505264ea103SEric Dumazet }
506264ea103SEric Dumazet 
507264ea103SEric Dumazet /* syncookies: no recent synqueue overflow on this listening socket? */
508264ea103SEric Dumazet static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
509264ea103SEric Dumazet {
510264ea103SEric Dumazet 	unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
511264ea103SEric Dumazet 
512264ea103SEric Dumazet 	return time_after(jiffies, last_overflow + TCP_SYNCOOKIE_VALID);
513264ea103SEric Dumazet }
5148c27bd75SFlorian Westphal 
5158c27bd75SFlorian Westphal static inline u32 tcp_cookie_time(void)
5168c27bd75SFlorian Westphal {
51763262315SEric Dumazet 	u64 val = get_jiffies_64();
51863262315SEric Dumazet 
519264ea103SEric Dumazet 	do_div(val, TCP_SYNCOOKIE_PERIOD);
52063262315SEric Dumazet 	return val;
5218c27bd75SFlorian Westphal }
5228c27bd75SFlorian Westphal 
5235c9f3023SJoe Perches u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
5245c9f3023SJoe Perches 			      u16 *mssp);
5253f684b4bSEric Dumazet __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss);
5265c9f3023SJoe Perches __u32 cookie_init_timestamp(struct request_sock *req);
527f1673381SFlorian Westphal bool cookie_timestamp_decode(struct tcp_options_received *opt);
528f1673381SFlorian Westphal bool cookie_ecn_ok(const struct tcp_options_received *opt,
529f7b3bec6SFlorian Westphal 		   const struct net *net, const struct dst_entry *dst);
5304dfc2817SFlorian Westphal 
531c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */
5325c9f3023SJoe Perches int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
53381eb6a14SPatrick McHardy 		      u32 cookie);
5345c9f3023SJoe Perches struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
535f1673381SFlorian Westphal 
5365c9f3023SJoe Perches u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
53781eb6a14SPatrick McHardy 			      const struct tcphdr *th, u16 *mssp);
5383f684b4bSEric Dumazet __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mss);
539e05c82d3SEric Dumazet #endif
5401da177e4SLinus Torvalds /* tcp_output.c */
5411da177e4SLinus Torvalds 
5425c9f3023SJoe Perches void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
5439e412ba7SIlpo Järvinen 			       int nonagle);
5445c9f3023SJoe Perches bool tcp_may_send_now(struct sock *sk);
5455c9f3023SJoe Perches int __tcp_retransmit_skb(struct sock *, struct sk_buff *);
5465c9f3023SJoe Perches int tcp_retransmit_skb(struct sock *, struct sk_buff *);
5475c9f3023SJoe Perches void tcp_retransmit_timer(struct sock *sk);
5485c9f3023SJoe Perches void tcp_xmit_retransmit_queue(struct sock *);
5495c9f3023SJoe Perches void tcp_simple_retransmit(struct sock *);
5505c9f3023SJoe Perches int tcp_trim_head(struct sock *, struct sk_buff *, u32);
5516cc55e09SOctavian Purdila int tcp_fragment(struct sock *, struct sk_buff *, u32, unsigned int, gfp_t);
5521da177e4SLinus Torvalds 
5535c9f3023SJoe Perches void tcp_send_probe0(struct sock *);
5545c9f3023SJoe Perches void tcp_send_partial(struct sock *);
555e520af48SEric Dumazet int tcp_write_wakeup(struct sock *, int mib);
5565c9f3023SJoe Perches void tcp_send_fin(struct sock *sk);
5575c9f3023SJoe Perches void tcp_send_active_reset(struct sock *sk, gfp_t priority);
5585c9f3023SJoe Perches int tcp_send_synack(struct sock *);
5595c9f3023SJoe Perches void tcp_push_one(struct sock *, unsigned int mss_now);
5605c9f3023SJoe Perches void tcp_send_ack(struct sock *sk);
5615c9f3023SJoe Perches void tcp_send_delayed_ack(struct sock *sk);
5625c9f3023SJoe Perches void tcp_send_loss_probe(struct sock *sk);
5635c9f3023SJoe Perches bool tcp_schedule_loss_probe(struct sock *sk);
5641da177e4SLinus Torvalds 
565a762a980SDavid S. Miller /* tcp_input.c */
5665c9f3023SJoe Perches void tcp_resume_early_retransmit(struct sock *sk);
5675c9f3023SJoe Perches void tcp_rearm_rto(struct sock *sk);
5680f1c28aeSYuchung Cheng void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req);
5695c9f3023SJoe Perches void tcp_reset(struct sock *sk);
570a762a980SDavid S. Miller 
5711da177e4SLinus Torvalds /* tcp_timer.c */
5725c9f3023SJoe Perches void tcp_init_xmit_timers(struct sock *);
573463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
574463c84b9SArnaldo Carvalho de Melo {
575463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
576463c84b9SArnaldo Carvalho de Melo }
5771da177e4SLinus Torvalds 
5785c9f3023SJoe Perches unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
5795c9f3023SJoe Perches unsigned int tcp_current_mss(struct sock *sk);
5800c54b85fSIlpo Järvinen 
5810c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */
5820c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
5830c54b85fSIlpo Järvinen {
58401f83d69SAlexey Kuznetsov 	int cutoff;
58501f83d69SAlexey Kuznetsov 
58601f83d69SAlexey Kuznetsov 	/* When peer uses tiny windows, there is no use in packetizing
58701f83d69SAlexey Kuznetsov 	 * to sub-MSS pieces for the sake of SWS or making sure there
58801f83d69SAlexey Kuznetsov 	 * are enough packets in the pipe for fast recovery.
58901f83d69SAlexey Kuznetsov 	 *
59001f83d69SAlexey Kuznetsov 	 * On the other hand, for extremely large MSS devices, handling
59101f83d69SAlexey Kuznetsov 	 * smaller than MSS windows in this way does make sense.
59201f83d69SAlexey Kuznetsov 	 */
59301f83d69SAlexey Kuznetsov 	if (tp->max_window >= 512)
59401f83d69SAlexey Kuznetsov 		cutoff = (tp->max_window >> 1);
59501f83d69SAlexey Kuznetsov 	else
59601f83d69SAlexey Kuznetsov 		cutoff = tp->max_window;
59701f83d69SAlexey Kuznetsov 
59801f83d69SAlexey Kuznetsov 	if (cutoff && pktsize > cutoff)
59901f83d69SAlexey Kuznetsov 		return max_t(int, cutoff, 68U - tp->tcp_header_len);
6000c54b85fSIlpo Järvinen 	else
6010c54b85fSIlpo Järvinen 		return pktsize;
6020c54b85fSIlpo Järvinen }
6031da177e4SLinus Torvalds 
60417b085eaSArnaldo Carvalho de Melo /* tcp.c */
6050df48c26SEric Dumazet void tcp_get_info(struct sock *, struct tcp_info *);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
6081da177e4SLinus Torvalds typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
6091da177e4SLinus Torvalds 				unsigned int, size_t);
6105c9f3023SJoe Perches int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
6111da177e4SLinus Torvalds 		  sk_read_actor_t recv_actor);
6121da177e4SLinus Torvalds 
6135c9f3023SJoe Perches void tcp_initialize_rcv_mss(struct sock *sk);
6141da177e4SLinus Torvalds 
6155c9f3023SJoe Perches int tcp_mtu_to_mss(struct sock *sk, int pmtu);
6165c9f3023SJoe Perches int tcp_mss_to_mtu(struct sock *sk, int mss);
6175c9f3023SJoe Perches void tcp_mtup_init(struct sock *sk);
6185c9f3023SJoe Perches void tcp_init_buffer_space(struct sock *sk);
6195d424d5aSJohn Heffner 
620f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk)
621f1ecd5d9SDamian Lukowski {
622f1ecd5d9SDamian Lukowski 	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
623f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
624f1ecd5d9SDamian Lukowski }
625f1ecd5d9SDamian Lukowski 
626f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
627f1ecd5d9SDamian Lukowski {
628740b0f18SEric Dumazet 	return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
629f1ecd5d9SDamian Lukowski }
630f1ecd5d9SDamian Lukowski 
63140efc6faSStephen Hemminger static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
6321da177e4SLinus Torvalds {
6331da177e4SLinus Torvalds 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
6341da177e4SLinus Torvalds 			       ntohl(TCP_FLAG_ACK) |
6351da177e4SLinus Torvalds 			       snd_wnd);
6361da177e4SLinus Torvalds }
6371da177e4SLinus Torvalds 
63840efc6faSStephen Hemminger static inline void tcp_fast_path_on(struct tcp_sock *tp)
6391da177e4SLinus Torvalds {
6401da177e4SLinus Torvalds 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds 
6439e412ba7SIlpo Järvinen static inline void tcp_fast_path_check(struct sock *sk)
6441da177e4SLinus Torvalds {
6459e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
6469e412ba7SIlpo Järvinen 
647b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue) &&
6481da177e4SLinus Torvalds 	    tp->rcv_wnd &&
6491da177e4SLinus Torvalds 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
6501da177e4SLinus Torvalds 	    !tp->urg_data)
6511da177e4SLinus Torvalds 		tcp_fast_path_on(tp);
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
6540c266898SSatoru SATOH /* Compute the actual rto_min value */
6550c266898SSatoru SATOH static inline u32 tcp_rto_min(struct sock *sk)
6560c266898SSatoru SATOH {
657cf533ea5SEric Dumazet 	const struct dst_entry *dst = __sk_dst_get(sk);
6580c266898SSatoru SATOH 	u32 rto_min = TCP_RTO_MIN;
6590c266898SSatoru SATOH 
6600c266898SSatoru SATOH 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
6610c266898SSatoru SATOH 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
6620c266898SSatoru SATOH 	return rto_min;
6630c266898SSatoru SATOH }
6640c266898SSatoru SATOH 
665740b0f18SEric Dumazet static inline u32 tcp_rto_min_us(struct sock *sk)
666740b0f18SEric Dumazet {
667740b0f18SEric Dumazet 	return jiffies_to_usecs(tcp_rto_min(sk));
668740b0f18SEric Dumazet }
669740b0f18SEric Dumazet 
67081164413SDaniel Borkmann static inline bool tcp_ca_dst_locked(const struct dst_entry *dst)
67181164413SDaniel Borkmann {
67281164413SDaniel Borkmann 	return dst_metric_locked(dst, RTAX_CC_ALGO);
67381164413SDaniel Borkmann }
67481164413SDaniel Borkmann 
675f6722583SYuchung Cheng /* Minimum RTT in usec. ~0 means not available. */
676f6722583SYuchung Cheng static inline u32 tcp_min_rtt(const struct tcp_sock *tp)
677f6722583SYuchung Cheng {
678f6722583SYuchung Cheng 	return tp->rtt_min[0].rtt;
679f6722583SYuchung Cheng }
680f6722583SYuchung Cheng 
6811da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
6821da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
6831da177e4SLinus Torvalds  * than the offered window.
6841da177e4SLinus Torvalds  */
68540efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
6861da177e4SLinus Torvalds {
6871da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 	if (win < 0)
6901da177e4SLinus Torvalds 		win = 0;
6911da177e4SLinus Torvalds 	return (u32) win;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
6951da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
6961da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
6971da177e4SLinus Torvalds  */
6985c9f3023SJoe Perches u32 __tcp_select_window(struct sock *sk);
6991da177e4SLinus Torvalds 
700ee995283SPavel Emelyanov void tcp_send_window_probe(struct sock *sk);
701ee995283SPavel Emelyanov 
7021da177e4SLinus Torvalds /* TCP timestamps are only 32-bits, this causes a slight
7031da177e4SLinus Torvalds  * complication on 64-bit systems since we store a snapshot
70431f34269SStephen Hemminger  * of jiffies in the buffer control blocks below.  We decided
70531f34269SStephen Hemminger  * to use only the low 32-bits of jiffies and hide the ugly
7061da177e4SLinus Torvalds  * casts with the following macro.
7071da177e4SLinus Torvalds  */
7081da177e4SLinus Torvalds #define tcp_time_stamp		((__u32)(jiffies))
7091da177e4SLinus Torvalds 
7107faee5c0SEric Dumazet static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
7117faee5c0SEric Dumazet {
7127faee5c0SEric Dumazet 	return skb->skb_mstamp.stamp_jiffies;
7137faee5c0SEric Dumazet }
7147faee5c0SEric Dumazet 
7157faee5c0SEric Dumazet 
716a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
717a3433f35SChangli Gao 
718a3433f35SChangli Gao #define TCPHDR_FIN 0x01
719a3433f35SChangli Gao #define TCPHDR_SYN 0x02
720a3433f35SChangli Gao #define TCPHDR_RST 0x04
721a3433f35SChangli Gao #define TCPHDR_PSH 0x08
722a3433f35SChangli Gao #define TCPHDR_ACK 0x10
723a3433f35SChangli Gao #define TCPHDR_URG 0x20
724a3433f35SChangli Gao #define TCPHDR_ECE 0x40
725a3433f35SChangli Gao #define TCPHDR_CWR 0x80
726a3433f35SChangli Gao 
72749213555SDaniel Borkmann #define TCPHDR_SYN_ECN	(TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR)
72849213555SDaniel Borkmann 
729caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
730f86586faSEric Dumazet  * TCP per-packet control information to the transmission code.
731f86586faSEric Dumazet  * We also store the host-order sequence numbers in here too.
732f86586faSEric Dumazet  * This is 44 bytes if IPV6 is enabled.
733f86586faSEric Dumazet  * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
7341da177e4SLinus Torvalds  */
7351da177e4SLinus Torvalds struct tcp_skb_cb {
7361da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
7371da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
738cd7d8498SEric Dumazet 	union {
739cd7d8498SEric Dumazet 		/* Note : tcp_tw_isn is used in input path only
740cd7d8498SEric Dumazet 		 *	  (isn chosen by tcp_timewait_state_process())
741cd7d8498SEric Dumazet 		 *
742f69ad292SEric Dumazet 		 * 	  tcp_gso_segs/size are used in write queue only,
743f69ad292SEric Dumazet 		 *	  cf tcp_skb_pcount()/tcp_skb_mss()
744cd7d8498SEric Dumazet 		 */
745cd7d8498SEric Dumazet 		__u32		tcp_tw_isn;
746f69ad292SEric Dumazet 		struct {
747f69ad292SEric Dumazet 			u16	tcp_gso_segs;
748f69ad292SEric Dumazet 			u16	tcp_gso_size;
749f69ad292SEric Dumazet 		};
750cd7d8498SEric Dumazet 	};
7514de075e0SEric Dumazet 	__u8		tcp_flags;	/* TCP header flags. (tcp[13])	*/
752f4f9f6e7SNeal Cardwell 
7531da177e4SLinus Torvalds 	__u8		sacked;		/* State flags for SACK/FACK.	*/
7541da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
7551da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
7561da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
7571da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
7589d186cacSAndrey Vagin #define TCPCB_REPAIRED		0x10	/* SKB repaired (no skb_mstamp)	*/
7591da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
7609d186cacSAndrey Vagin #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
7619d186cacSAndrey Vagin 				TCPCB_REPAIRED)
7621da177e4SLinus Torvalds 
763f4f9f6e7SNeal Cardwell 	__u8		ip_dsfield;	/* IPv4 tos or IPv6 dsfield	*/
764f4f9f6e7SNeal Cardwell 	/* 1 byte hole */
7651da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
766971f10ecSEric Dumazet 	union {
767971f10ecSEric Dumazet 		struct inet_skb_parm	h4;
768971f10ecSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
769971f10ecSEric Dumazet 		struct inet6_skb_parm	h6;
770971f10ecSEric Dumazet #endif
771971f10ecSEric Dumazet 	} header;	/* For incoming frames		*/
7721da177e4SLinus Torvalds };
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
7751da177e4SLinus Torvalds 
776870c3151SEric Dumazet 
777815afe17SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
778870c3151SEric Dumazet /* This is the variant of inet6_iif() that must be used by TCP,
779870c3151SEric Dumazet  * as TCP moves IP6CB into a different location in skb->cb[]
780870c3151SEric Dumazet  */
781870c3151SEric Dumazet static inline int tcp_v6_iif(const struct sk_buff *skb)
782870c3151SEric Dumazet {
783870c3151SEric Dumazet 	return TCP_SKB_CB(skb)->header.h6.iif;
784870c3151SEric Dumazet }
785815afe17SEric Dumazet #endif
786870c3151SEric Dumazet 
7871da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
7881da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
7891da177e4SLinus Torvalds  */
7901da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
7911da177e4SLinus Torvalds {
792cd7d8498SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_segs;
793cd7d8498SEric Dumazet }
794cd7d8498SEric Dumazet 
795cd7d8498SEric Dumazet static inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs)
796cd7d8498SEric Dumazet {
797cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs = segs;
798cd7d8498SEric Dumazet }
799cd7d8498SEric Dumazet 
800cd7d8498SEric Dumazet static inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs)
801cd7d8498SEric Dumazet {
802cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs += segs;
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
805f69ad292SEric Dumazet /* This is valid iff skb is in write queue and tcp_skb_pcount() > 1. */
8061da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
8071da177e4SLinus Torvalds {
808f69ad292SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_size;
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
811317a76f9SStephen Hemminger /* Events passed to congestion control interface */
812317a76f9SStephen Hemminger enum tcp_ca_event {
813317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
814317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
815317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
816317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
8179890092eSFlorian Westphal 	CA_EVENT_ECN_NO_CE,	/* ECT set, but not CE marked */
8189890092eSFlorian Westphal 	CA_EVENT_ECN_IS_CE,	/* received CE marked IP packet */
8199890092eSFlorian Westphal 	CA_EVENT_DELAYED_ACK,	/* Delayed ack is sent */
8209890092eSFlorian Westphal 	CA_EVENT_NON_DELAYED_ACK,
8217354c8c3SFlorian Westphal };
8227354c8c3SFlorian Westphal 
8239890092eSFlorian Westphal /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
8247354c8c3SFlorian Westphal enum tcp_ca_ack_event_flags {
8259890092eSFlorian Westphal 	CA_ACK_SLOWPATH		= (1 << 0),	/* In slow path processing */
8269890092eSFlorian Westphal 	CA_ACK_WIN_UPDATE	= (1 << 1),	/* ACK updated window */
8279890092eSFlorian Westphal 	CA_ACK_ECE		= (1 << 2),	/* ECE bit is set on ack */
828317a76f9SStephen Hemminger };
829317a76f9SStephen Hemminger 
830317a76f9SStephen Hemminger /*
831317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
832317a76f9SStephen Hemminger  */
833317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
8343ff825b2SStephen Hemminger #define TCP_CA_MAX	128
8353ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
8363ff825b2SStephen Hemminger 
837c5c6a8abSDaniel Borkmann #define TCP_CA_UNSPEC	0
838c5c6a8abSDaniel Borkmann 
83930e502a3SDaniel Borkmann /* Algorithm can be set on socket without CAP_NET_ADMIN privileges */
840164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1
84130e502a3SDaniel Borkmann /* Requires ECN/ECT set on all packets */
84230e502a3SDaniel Borkmann #define TCP_CONG_NEEDS_ECN	0x2
843164891aaSStephen Hemminger 
84464f40ff5SEric Dumazet union tcp_cc_info;
84564f40ff5SEric Dumazet 
846317a76f9SStephen Hemminger struct tcp_congestion_ops {
847317a76f9SStephen Hemminger 	struct list_head	list;
848c5c6a8abSDaniel Borkmann 	u32 key;
849c5c6a8abSDaniel Borkmann 	u32 flags;
850317a76f9SStephen Hemminger 
851317a76f9SStephen Hemminger 	/* initialize private data (optional) */
8526687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
853317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
8546687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
855317a76f9SStephen Hemminger 
856317a76f9SStephen Hemminger 	/* return slow start threshold (required) */
8576687e988SArnaldo Carvalho de Melo 	u32 (*ssthresh)(struct sock *sk);
858317a76f9SStephen Hemminger 	/* do new cwnd calculation (required) */
85924901551SEric Dumazet 	void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked);
860317a76f9SStephen Hemminger 	/* call before changing ca_state (optional) */
8616687e988SArnaldo Carvalho de Melo 	void (*set_state)(struct sock *sk, u8 new_state);
862317a76f9SStephen Hemminger 	/* call when cwnd event occurs (optional) */
8636687e988SArnaldo Carvalho de Melo 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
8647354c8c3SFlorian Westphal 	/* call when ack arrives (optional) */
8657354c8c3SFlorian Westphal 	void (*in_ack_event)(struct sock *sk, u32 flags);
866317a76f9SStephen Hemminger 	/* new value of cwnd after loss (optional) */
8676687e988SArnaldo Carvalho de Melo 	u32  (*undo_cwnd)(struct sock *sk);
868317a76f9SStephen Hemminger 	/* hook for packet ack accounting (optional) */
86930cfd0baSStephen Hemminger 	void (*pkts_acked)(struct sock *sk, u32 num_acked, s32 rtt_us);
87073c1f4a0SArnaldo Carvalho de Melo 	/* get info for inet_diag (optional) */
87164f40ff5SEric Dumazet 	size_t (*get_info)(struct sock *sk, u32 ext, int *attr,
87264f40ff5SEric Dumazet 			   union tcp_cc_info *info);
873317a76f9SStephen Hemminger 
874317a76f9SStephen Hemminger 	char 		name[TCP_CA_NAME_MAX];
875317a76f9SStephen Hemminger 	struct module 	*owner;
876317a76f9SStephen Hemminger };
877317a76f9SStephen Hemminger 
8785c9f3023SJoe Perches int tcp_register_congestion_control(struct tcp_congestion_ops *type);
8795c9f3023SJoe Perches void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
880317a76f9SStephen Hemminger 
88155d8694fSFlorian Westphal void tcp_assign_congestion_control(struct sock *sk);
8825c9f3023SJoe Perches void tcp_init_congestion_control(struct sock *sk);
8835c9f3023SJoe Perches void tcp_cleanup_congestion_control(struct sock *sk);
8845c9f3023SJoe Perches int tcp_set_default_congestion_control(const char *name);
8855c9f3023SJoe Perches void tcp_get_default_congestion_control(char *name);
8865c9f3023SJoe Perches void tcp_get_available_congestion_control(char *buf, size_t len);
8875c9f3023SJoe Perches void tcp_get_allowed_congestion_control(char *buf, size_t len);
8885c9f3023SJoe Perches int tcp_set_allowed_congestion_control(char *allowed);
8895c9f3023SJoe Perches int tcp_set_congestion_control(struct sock *sk, const char *name);
890e73ebb08SNeal Cardwell u32 tcp_slow_start(struct tcp_sock *tp, u32 acked);
891e73ebb08SNeal Cardwell void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked);
892317a76f9SStephen Hemminger 
8935c9f3023SJoe Perches u32 tcp_reno_ssthresh(struct sock *sk);
89424901551SEric Dumazet void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked);
895a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
896317a76f9SStephen Hemminger 
897c5c6a8abSDaniel Borkmann struct tcp_congestion_ops *tcp_ca_find_key(u32 key);
898c3a8d947SDaniel Borkmann u32 tcp_ca_get_key_by_name(const char *name, bool *ecn_ca);
899ea697639SDaniel Borkmann #ifdef CONFIG_INET
900c5c6a8abSDaniel Borkmann char *tcp_ca_get_name_by_key(u32 key, char *buffer);
901ea697639SDaniel Borkmann #else
902ea697639SDaniel Borkmann static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer)
903ea697639SDaniel Borkmann {
904ea697639SDaniel Borkmann 	return NULL;
905ea697639SDaniel Borkmann }
906ea697639SDaniel Borkmann #endif
907c5c6a8abSDaniel Borkmann 
90830e502a3SDaniel Borkmann static inline bool tcp_ca_needs_ecn(const struct sock *sk)
90930e502a3SDaniel Borkmann {
91030e502a3SDaniel Borkmann 	const struct inet_connection_sock *icsk = inet_csk(sk);
91130e502a3SDaniel Borkmann 
91230e502a3SDaniel Borkmann 	return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN;
91330e502a3SDaniel Borkmann }
91430e502a3SDaniel Borkmann 
9156687e988SArnaldo Carvalho de Melo static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
916317a76f9SStephen Hemminger {
9176687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
9186687e988SArnaldo Carvalho de Melo 
9196687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->set_state)
9206687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->set_state(sk, ca_state);
9216687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_state = ca_state;
922317a76f9SStephen Hemminger }
923317a76f9SStephen Hemminger 
9246687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
925317a76f9SStephen Hemminger {
9266687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
9276687e988SArnaldo Carvalho de Melo 
9286687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
9296687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
930317a76f9SStephen Hemminger }
931317a76f9SStephen Hemminger 
932e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK
933e60402d0SIlpo Järvinen  * handling. SACK is negotiated with the peer, and therefore it can vary
934e60402d0SIlpo Järvinen  * between different flows.
935e60402d0SIlpo Järvinen  *
936e60402d0SIlpo Järvinen  * tcp_is_sack - SACK enabled
937e60402d0SIlpo Järvinen  * tcp_is_reno - No SACK
938e60402d0SIlpo Järvinen  * tcp_is_fack - FACK enabled, implies SACK enabled
939e60402d0SIlpo Järvinen  */
940e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp)
941e60402d0SIlpo Järvinen {
942e60402d0SIlpo Järvinen 	return tp->rx_opt.sack_ok;
943e60402d0SIlpo Järvinen }
944e60402d0SIlpo Järvinen 
945a2a385d6SEric Dumazet static inline bool tcp_is_reno(const struct tcp_sock *tp)
946e60402d0SIlpo Järvinen {
947e60402d0SIlpo Järvinen 	return !tcp_is_sack(tp);
948e60402d0SIlpo Järvinen }
949e60402d0SIlpo Järvinen 
950a2a385d6SEric Dumazet static inline bool tcp_is_fack(const struct tcp_sock *tp)
951e60402d0SIlpo Järvinen {
952ab56222aSVijay Subramanian 	return tp->rx_opt.sack_ok & TCP_FACK_ENABLED;
953e60402d0SIlpo Järvinen }
954e60402d0SIlpo Järvinen 
955e60402d0SIlpo Järvinen static inline void tcp_enable_fack(struct tcp_sock *tp)
956e60402d0SIlpo Järvinen {
957ab56222aSVijay Subramanian 	tp->rx_opt.sack_ok |= TCP_FACK_ENABLED;
958e60402d0SIlpo Järvinen }
959e60402d0SIlpo Järvinen 
960eed530b6SYuchung Cheng /* TCP early-retransmit (ER) is similar to but more conservative than
961eed530b6SYuchung Cheng  * the thin-dupack feature.  Enable ER only if thin-dupack is disabled.
962eed530b6SYuchung Cheng  */
963eed530b6SYuchung Cheng static inline void tcp_enable_early_retrans(struct tcp_sock *tp)
964eed530b6SYuchung Cheng {
965eed530b6SYuchung Cheng 	tp->do_early_retrans = sysctl_tcp_early_retrans &&
9666ba8a3b1SNandita Dukkipati 		sysctl_tcp_early_retrans < 4 && !sysctl_tcp_thin_dupack &&
9676ba8a3b1SNandita Dukkipati 		sysctl_tcp_reordering == 3;
968eed530b6SYuchung Cheng }
969eed530b6SYuchung Cheng 
970eed530b6SYuchung Cheng static inline void tcp_disable_early_retrans(struct tcp_sock *tp)
971eed530b6SYuchung Cheng {
972eed530b6SYuchung Cheng 	tp->do_early_retrans = 0;
973eed530b6SYuchung Cheng }
974eed530b6SYuchung Cheng 
97583ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
97683ae4088SIlpo Järvinen {
97783ae4088SIlpo Järvinen 	return tp->sacked_out + tp->lost_out;
97883ae4088SIlpo Järvinen }
97983ae4088SIlpo Järvinen 
9801da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
9811da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
9821da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
9831da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
9841da177e4SLinus Torvalds  *
9851da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
9861da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
9871da177e4SLinus Torvalds  *
9881da177e4SLinus Torvalds  * Read this equation as:
9891da177e4SLinus Torvalds  *
9901da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
9911da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
9921da177e4SLinus Torvalds  *	"Packets fast retransmitted"
9931da177e4SLinus Torvalds  */
99440efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
9951da177e4SLinus Torvalds {
99683ae4088SIlpo Järvinen 	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
9971da177e4SLinus Torvalds }
9981da177e4SLinus Torvalds 
9990b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH	0x7fffffff
10000b6a05c1SIlpo Järvinen 
1001071d5080SYuchung Cheng static inline bool tcp_in_slow_start(const struct tcp_sock *tp)
1002071d5080SYuchung Cheng {
100376174004SYuchung Cheng 	return tp->snd_cwnd < tp->snd_ssthresh;
1004071d5080SYuchung Cheng }
1005071d5080SYuchung Cheng 
10060b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
10070b6a05c1SIlpo Järvinen {
10080b6a05c1SIlpo Järvinen 	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
10090b6a05c1SIlpo Järvinen }
10100b6a05c1SIlpo Järvinen 
1011684bad11SYuchung Cheng static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
1012684bad11SYuchung Cheng {
1013684bad11SYuchung Cheng 	return (TCPF_CA_CWR | TCPF_CA_Recovery) &
1014684bad11SYuchung Cheng 	       (1 << inet_csk(sk)->icsk_ca_state);
1015684bad11SYuchung Cheng }
1016684bad11SYuchung Cheng 
10171da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
1018684bad11SYuchung Cheng  * The exception is cwnd reduction phase, when cwnd is decreasing towards
10191da177e4SLinus Torvalds  * ssthresh.
10201da177e4SLinus Torvalds  */
10216687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
10221da177e4SLinus Torvalds {
10236687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
1024cf533ea5SEric Dumazet 
1025684bad11SYuchung Cheng 	if (tcp_in_cwnd_reduction(sk))
10261da177e4SLinus Torvalds 		return tp->snd_ssthresh;
10271da177e4SLinus Torvalds 	else
10281da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
10291da177e4SLinus Torvalds 			   ((tp->snd_cwnd >> 1) +
10301da177e4SLinus Torvalds 			    (tp->snd_cwnd >> 2)));
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
1033b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */
1034b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
10351da177e4SLinus Torvalds 
10365ee2c941SChristoph Paasch void tcp_enter_cwr(struct sock *sk);
10375c9f3023SJoe Perches __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
10381da177e4SLinus Torvalds 
10396b5a5c0dSNeal Cardwell /* The maximum number of MSS of available cwnd for which TSO defers
10406b5a5c0dSNeal Cardwell  * sending if not using sysctl_tcp_tso_win_divisor.
10416b5a5c0dSNeal Cardwell  */
10426b5a5c0dSNeal Cardwell static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
10436b5a5c0dSNeal Cardwell {
10446b5a5c0dSNeal Cardwell 	return 3;
10456b5a5c0dSNeal Cardwell }
10466b5a5c0dSNeal Cardwell 
10471da177e4SLinus Torvalds /* Slow start with delack produces 3 packets of burst, so that
1048dd9e0ddaSJohn Heffner  * it is safe "de facto".  This will be the default - same as
1049dd9e0ddaSJohn Heffner  * the default reordering threshold - but if reordering increases,
1050dd9e0ddaSJohn Heffner  * we must be able to allow cwnd to burst at least this much in order
1051dd9e0ddaSJohn Heffner  * to not pull it back when holes are filled.
10521da177e4SLinus Torvalds  */
10531da177e4SLinus Torvalds static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
10541da177e4SLinus Torvalds {
1055dd9e0ddaSJohn Heffner 	return tp->reordering;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
105890840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */
105990840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
106090840defSIlpo Järvinen {
106190840defSIlpo Järvinen 	return tp->snd_una + tp->snd_wnd;
106290840defSIlpo Järvinen }
1063e114a710SEric Dumazet 
1064e114a710SEric Dumazet /* We follow the spirit of RFC2861 to validate cwnd but implement a more
1065e114a710SEric Dumazet  * flexible approach. The RFC suggests cwnd should not be raised unless
1066ca8a2263SNeal Cardwell  * it was fully used previously. And that's exactly what we do in
1067ca8a2263SNeal Cardwell  * congestion avoidance mode. But in slow start we allow cwnd to grow
1068ca8a2263SNeal Cardwell  * as long as the application has used half the cwnd.
1069e114a710SEric Dumazet  * Example :
1070e114a710SEric Dumazet  *    cwnd is 10 (IW10), but application sends 9 frames.
1071e114a710SEric Dumazet  *    We allow cwnd to reach 18 when all frames are ACKed.
1072e114a710SEric Dumazet  * This check is safe because it's as aggressive as slow start which already
1073e114a710SEric Dumazet  * risks 100% overshoot. The advantage is that we discourage application to
1074e114a710SEric Dumazet  * either send more filler packets or data to artificially blow up the cwnd
1075e114a710SEric Dumazet  * usage, and allow application-limited process to probe bw more aggressively.
1076e114a710SEric Dumazet  */
107724901551SEric Dumazet static inline bool tcp_is_cwnd_limited(const struct sock *sk)
1078e114a710SEric Dumazet {
1079e114a710SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1080e114a710SEric Dumazet 
1081ca8a2263SNeal Cardwell 	/* If in slow start, ensure cwnd grows to twice what was ACKed. */
1082071d5080SYuchung Cheng 	if (tcp_in_slow_start(tp))
1083ca8a2263SNeal Cardwell 		return tp->snd_cwnd < 2 * tp->max_packets_out;
1084ca8a2263SNeal Cardwell 
1085ca8a2263SNeal Cardwell 	return tp->is_cwnd_limited;
1086e114a710SEric Dumazet }
1087f4805edeSStephen Hemminger 
108821c8fe99SEric Dumazet /* Something is really bad, we could not queue an additional packet,
108921c8fe99SEric Dumazet  * because qdisc is full or receiver sent a 0 window.
109021c8fe99SEric Dumazet  * We do not want to add fuel to the fire, or abort too early,
109121c8fe99SEric Dumazet  * so make sure the timer we arm now is at least 200ms in the future,
109221c8fe99SEric Dumazet  * regardless of current icsk_rto value (as it could be ~2ms)
109321c8fe99SEric Dumazet  */
109421c8fe99SEric Dumazet static inline unsigned long tcp_probe0_base(const struct sock *sk)
109521c8fe99SEric Dumazet {
109621c8fe99SEric Dumazet 	return max_t(unsigned long, inet_csk(sk)->icsk_rto, TCP_RTO_MIN);
109721c8fe99SEric Dumazet }
109821c8fe99SEric Dumazet 
109921c8fe99SEric Dumazet /* Variant of inet_csk_rto_backoff() used for zero window probes */
110021c8fe99SEric Dumazet static inline unsigned long tcp_probe0_when(const struct sock *sk,
110121c8fe99SEric Dumazet 					    unsigned long max_when)
110221c8fe99SEric Dumazet {
110321c8fe99SEric Dumazet 	u64 when = (u64)tcp_probe0_base(sk) << inet_csk(sk)->icsk_backoff;
110421c8fe99SEric Dumazet 
110521c8fe99SEric Dumazet 	return (unsigned long)min_t(u64, when, max_when);
110621c8fe99SEric Dumazet }
110721c8fe99SEric Dumazet 
11089e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk)
11091da177e4SLinus Torvalds {
111021c8fe99SEric Dumazet 	if (!tcp_sk(sk)->packets_out && !inet_csk(sk)->icsk_pending)
11113f421baaSArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
111221c8fe99SEric Dumazet 					  tcp_probe0_base(sk), TCP_RTO_MAX);
11131da177e4SLinus Torvalds }
11141da177e4SLinus Torvalds 
1115ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
11161da177e4SLinus Torvalds {
11171da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds 
1120ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
11211da177e4SLinus Torvalds {
11221da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
11231da177e4SLinus Torvalds }
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds /*
11261da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
11271da177e4SLinus Torvalds  */
1128ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr,
1129ba7808eaSFrederik Deweerdt 				   __be32 daddr, __wsum base)
11301da177e4SLinus Torvalds {
11311da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
11321da177e4SLinus Torvalds }
11331da177e4SLinus Torvalds 
1134b51655b9SAl Viro static inline __sum16 __tcp_checksum_complete(struct sk_buff *skb)
11351da177e4SLinus Torvalds {
1136fb286bb2SHerbert Xu 	return __skb_checksum_complete(skb);
11371da177e4SLinus Torvalds }
11381da177e4SLinus Torvalds 
1139a2a385d6SEric Dumazet static inline bool tcp_checksum_complete(struct sk_buff *skb)
11401da177e4SLinus Torvalds {
114160476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
11421da177e4SLinus Torvalds 		__tcp_checksum_complete(skb);
11431da177e4SLinus Torvalds }
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds /* Prequeue for VJ style copy to user, combined with checksumming. */
11461da177e4SLinus Torvalds 
114740efc6faSStephen Hemminger static inline void tcp_prequeue_init(struct tcp_sock *tp)
11481da177e4SLinus Torvalds {
11491da177e4SLinus Torvalds 	tp->ucopy.task = NULL;
11501da177e4SLinus Torvalds 	tp->ucopy.len = 0;
11511da177e4SLinus Torvalds 	tp->ucopy.memory = 0;
11521da177e4SLinus Torvalds 	skb_queue_head_init(&tp->ucopy.prequeue);
11531da177e4SLinus Torvalds }
11541da177e4SLinus Torvalds 
11555c9f3023SJoe Perches bool tcp_prequeue(struct sock *sk, struct sk_buff *skb);
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds #undef STATE_TRACE
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds #ifdef STATE_TRACE
11601da177e4SLinus Torvalds static const char *statename[]={
11611da177e4SLinus Torvalds 	"Unused","Established","Syn Sent","Syn Recv",
11621da177e4SLinus Torvalds 	"Fin Wait 1","Fin Wait 2","Time Wait", "Close",
11631da177e4SLinus Torvalds 	"Close Wait","Last ACK","Listen","Closing"
11641da177e4SLinus Torvalds };
11651da177e4SLinus Torvalds #endif
11665c9f3023SJoe Perches void tcp_set_state(struct sock *sk, int state);
11671da177e4SLinus Torvalds 
11685c9f3023SJoe Perches void tcp_done(struct sock *sk);
11691da177e4SLinus Torvalds 
117040efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
11711da177e4SLinus Torvalds {
11721da177e4SLinus Torvalds 	rx_opt->dsack = 0;
11731da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
11741da177e4SLinus Torvalds }
11751da177e4SLinus Torvalds 
11765c9f3023SJoe Perches u32 tcp_default_init_rwnd(u32 mss);
11776f021c62SEric Dumazet void tcp_cwnd_restart(struct sock *sk, s32 delta);
11786f021c62SEric Dumazet 
11796f021c62SEric Dumazet static inline void tcp_slow_start_after_idle_check(struct sock *sk)
11806f021c62SEric Dumazet {
11816f021c62SEric Dumazet 	struct tcp_sock *tp = tcp_sk(sk);
11826f021c62SEric Dumazet 	s32 delta;
11836f021c62SEric Dumazet 
11846f021c62SEric Dumazet 	if (!sysctl_tcp_slow_start_after_idle || tp->packets_out)
11856f021c62SEric Dumazet 		return;
11866f021c62SEric Dumazet 	delta = tcp_time_stamp - tp->lsndtime;
11876f021c62SEric Dumazet 	if (delta > inet_csk(sk)->icsk_rto)
11886f021c62SEric Dumazet 		tcp_cwnd_restart(sk, delta);
11896f021c62SEric Dumazet }
119085f16525SYuchung Cheng 
11911da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
11925c9f3023SJoe Perches void tcp_select_initial_window(int __space, __u32 mss, __u32 *rcv_wnd,
11935c9f3023SJoe Perches 			       __u32 *window_clamp, int wscale_ok,
11945c9f3023SJoe Perches 			       __u8 *rcv_wscale, __u32 init_rcv_wnd);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds static inline int tcp_win_from_space(int space)
11971da177e4SLinus Torvalds {
11981da177e4SLinus Torvalds 	return sysctl_tcp_adv_win_scale<=0 ?
11991da177e4SLinus Torvalds 		(space>>(-sysctl_tcp_adv_win_scale)) :
12001da177e4SLinus Torvalds 		space - (space>>sysctl_tcp_adv_win_scale);
12011da177e4SLinus Torvalds }
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
12041da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
12051da177e4SLinus Torvalds {
12061da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf -
12071da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
12111da177e4SLinus Torvalds {
12121da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf);
12131da177e4SLinus Torvalds }
12141da177e4SLinus Torvalds 
1215843f4a55SYuchung Cheng extern void tcp_openreq_init_rwin(struct request_sock *req,
1216b1964b5fSEric Dumazet 				  const struct sock *sk_listener,
1217b1964b5fSEric Dumazet 				  const struct dst_entry *dst);
1218843f4a55SYuchung Cheng 
12195c9f3023SJoe Perches void tcp_enter_memory_pressure(struct sock *sk);
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
12221da177e4SLinus Torvalds {
12231da177e4SLinus Torvalds 	return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
12271da177e4SLinus Torvalds {
12281da177e4SLinus Torvalds 	return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
1231df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp)
1232df19a626SEric Dumazet {
1233df19a626SEric Dumazet 	return tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
1234df19a626SEric Dumazet }
1235df19a626SEric Dumazet 
12366c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
12376c37e5deSFlavio Leitner {
12386c37e5deSFlavio Leitner 	const struct inet_connection_sock *icsk = &tp->inet_conn;
12396c37e5deSFlavio Leitner 
12406c37e5deSFlavio Leitner 	return min_t(u32, tcp_time_stamp - icsk->icsk_ack.lrcvtime,
12416c37e5deSFlavio Leitner 			  tcp_time_stamp - tp->rcv_tstamp);
12426c37e5deSFlavio Leitner }
12436c37e5deSFlavio Leitner 
1244463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
12451da177e4SLinus Torvalds {
1246463c84b9SArnaldo Carvalho de Melo 	int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
1247463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
12481da177e4SLinus Torvalds 
1249463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1250463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	return fin_timeout;
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
1255a2a385d6SEric Dumazet static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
1256c887e6d2SIlpo Järvinen 				  int paws_win)
12571da177e4SLinus Torvalds {
1258c887e6d2SIlpo Järvinen 	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1259a2a385d6SEric Dumazet 		return true;
1260c887e6d2SIlpo Järvinen 	if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS))
1261a2a385d6SEric Dumazet 		return true;
1262bc2ce894SEric Dumazet 	/*
1263bc2ce894SEric Dumazet 	 * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
1264bc2ce894SEric Dumazet 	 * then following tcp messages have valid values. Ignore 0 value,
1265bc2ce894SEric Dumazet 	 * or else 'negative' tsval might forbid us to accept their packets.
1266bc2ce894SEric Dumazet 	 */
1267bc2ce894SEric Dumazet 	if (!rx_opt->ts_recent)
1268a2a385d6SEric Dumazet 		return true;
1269a2a385d6SEric Dumazet 	return false;
1270c887e6d2SIlpo Järvinen }
1271c887e6d2SIlpo Järvinen 
1272a2a385d6SEric Dumazet static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
1273c887e6d2SIlpo Järvinen 				   int rst)
1274c887e6d2SIlpo Järvinen {
1275c887e6d2SIlpo Järvinen 	if (tcp_paws_check(rx_opt, 0))
1276a2a385d6SEric Dumazet 		return false;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
12791da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
12801da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
12811da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
12821da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
12831da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
12841da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
12851da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
12861da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
12891da177e4SLinus Torvalds 	 */
12909d729f72SJames Morris 	if (rst && get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
1291a2a385d6SEric Dumazet 		return false;
1292a2a385d6SEric Dumazet 	return true;
12931da177e4SLinus Torvalds }
12941da177e4SLinus Torvalds 
12957970ddc8SEric Dumazet bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
12967970ddc8SEric Dumazet 			  int mib_idx, u32 *last_oow_ack_time);
1297032ee423SNeal Cardwell 
1298a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net)
12991da177e4SLinus Torvalds {
13001da177e4SLinus Torvalds 	/* See RFC 2012 */
1301cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOALGORITHM, 1);
1302cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
1303cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
1304cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_MAXCONN, -1);
13051da177e4SLinus Torvalds }
13061da177e4SLinus Torvalds 
13076a438bbeSStephen Hemminger /* from STCP */
1308ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
13090800f170SDavid S. Miller {
13106a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
1311ef9da47cSIlpo Järvinen }
1312ef9da47cSIlpo Järvinen 
1313ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
1314ef9da47cSIlpo Järvinen {
1315ef9da47cSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
13166a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
1317b7689205SIlpo Järvinen }
1318b7689205SIlpo Järvinen 
1319cfb6eeb4SYOSHIFUJI Hideaki /* MD5 Signature */
1320cfb6eeb4SYOSHIFUJI Hideaki struct crypto_hash;
1321cfb6eeb4SYOSHIFUJI Hideaki 
1322a915da9bSEric Dumazet union tcp_md5_addr {
1323a915da9bSEric Dumazet 	struct in_addr  a4;
1324a915da9bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1325a915da9bSEric Dumazet 	struct in6_addr	a6;
1326a915da9bSEric Dumazet #endif
1327a915da9bSEric Dumazet };
1328a915da9bSEric Dumazet 
1329cfb6eeb4SYOSHIFUJI Hideaki /* - key database */
1330cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key {
1331a915da9bSEric Dumazet 	struct hlist_node	node;
1332cfb6eeb4SYOSHIFUJI Hideaki 	u8			keylen;
1333a915da9bSEric Dumazet 	u8			family; /* AF_INET or AF_INET6 */
1334a915da9bSEric Dumazet 	union tcp_md5_addr	addr;
1335a915da9bSEric Dumazet 	u8			key[TCP_MD5SIG_MAXKEYLEN];
1336a915da9bSEric Dumazet 	struct rcu_head		rcu;
1337cfb6eeb4SYOSHIFUJI Hideaki };
1338cfb6eeb4SYOSHIFUJI Hideaki 
1339cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */
1340cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info {
1341a915da9bSEric Dumazet 	struct hlist_head	head;
1342a8afca03SEric Dumazet 	struct rcu_head		rcu;
1343cfb6eeb4SYOSHIFUJI Hideaki };
1344cfb6eeb4SYOSHIFUJI Hideaki 
1345cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */
1346cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr {
1347cfb6eeb4SYOSHIFUJI Hideaki 	__be32		saddr;
1348cfb6eeb4SYOSHIFUJI Hideaki 	__be32		daddr;
1349cfb6eeb4SYOSHIFUJI Hideaki 	__u8		pad;
1350cfb6eeb4SYOSHIFUJI Hideaki 	__u8		protocol;
1351cfb6eeb4SYOSHIFUJI Hideaki 	__be16		len;
1352cfb6eeb4SYOSHIFUJI Hideaki };
1353cfb6eeb4SYOSHIFUJI Hideaki 
1354cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr {
1355cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr	saddr;
1356cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr daddr;
1357cfb6eeb4SYOSHIFUJI Hideaki 	__be32		len;
1358cfb6eeb4SYOSHIFUJI Hideaki 	__be32		protocol;	/* including padding */
1359cfb6eeb4SYOSHIFUJI Hideaki };
1360cfb6eeb4SYOSHIFUJI Hideaki 
1361cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block {
1362cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr ip4;
1363dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1364cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_pseudohdr ip6;
1365cfb6eeb4SYOSHIFUJI Hideaki #endif
1366cfb6eeb4SYOSHIFUJI Hideaki };
1367cfb6eeb4SYOSHIFUJI Hideaki 
1368cfb6eeb4SYOSHIFUJI Hideaki /* - pool: digest algorithm, hash description and scratch buffer */
1369cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool {
1370cfb6eeb4SYOSHIFUJI Hideaki 	struct hash_desc	md5_desc;
1371cfb6eeb4SYOSHIFUJI Hideaki 	union tcp_md5sum_block	md5_blk;
1372cfb6eeb4SYOSHIFUJI Hideaki };
1373cfb6eeb4SYOSHIFUJI Hideaki 
1374cfb6eeb4SYOSHIFUJI Hideaki /* - functions */
137539f8e58eSEric Dumazet int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
137639f8e58eSEric Dumazet 			const struct sock *sk, const struct sk_buff *skb);
13775c9f3023SJoe Perches int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
13785c9f3023SJoe Perches 		   int family, const u8 *newkey, u8 newkeylen, gfp_t gfp);
13795c9f3023SJoe Perches int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
1380a915da9bSEric Dumazet 		   int family);
1381b83e3debSEric Dumazet struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
1382fd3a154aSEric Dumazet 					 const struct sock *addr_sk);
1383cfb6eeb4SYOSHIFUJI Hideaki 
13849501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1385b83e3debSEric Dumazet struct tcp_md5sig_key *tcp_md5_do_lookup(const struct sock *sk,
13865c9f3023SJoe Perches 					 const union tcp_md5_addr *addr,
13875c9f3023SJoe Perches 					 int family);
1388a915da9bSEric Dumazet #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_key)
13899501f972SYOSHIFUJI Hideaki #else
1390b83e3debSEric Dumazet static inline struct tcp_md5sig_key *tcp_md5_do_lookup(const struct sock *sk,
1391a915da9bSEric Dumazet 					 const union tcp_md5_addr *addr,
1392a915da9bSEric Dumazet 					 int family)
1393a915da9bSEric Dumazet {
1394a915da9bSEric Dumazet 	return NULL;
1395a915da9bSEric Dumazet }
13969501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	NULL
13979501f972SYOSHIFUJI Hideaki #endif
13989501f972SYOSHIFUJI Hideaki 
13995c9f3023SJoe Perches bool tcp_alloc_md5sig_pool(void);
1400cfb6eeb4SYOSHIFUJI Hideaki 
14015c9f3023SJoe Perches struct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
140271cea17eSEric Dumazet static inline void tcp_put_md5sig_pool(void)
140371cea17eSEric Dumazet {
140471cea17eSEric Dumazet 	local_bh_enable();
140571cea17eSEric Dumazet }
140635790c04SEric Dumazet 
14075c9f3023SJoe Perches int tcp_md5_hash_header(struct tcp_md5sig_pool *, const struct tcphdr *);
14085c9f3023SJoe Perches int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
140995c96174SEric Dumazet 			  unsigned int header_len);
14105c9f3023SJoe Perches int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
1411cf533ea5SEric Dumazet 		     const struct tcp_md5sig_key *key);
1412cfb6eeb4SYOSHIFUJI Hideaki 
141310467163SJerry Chu /* From tcp_fastopen.c */
14145c9f3023SJoe Perches void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
14155c9f3023SJoe Perches 			    struct tcp_fastopen_cookie *cookie, int *syn_loss,
14165c9f3023SJoe Perches 			    unsigned long *last_syn_loss);
14175c9f3023SJoe Perches void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
14182646c831SDaniel Lee 			    struct tcp_fastopen_cookie *cookie, bool syn_lost,
14192646c831SDaniel Lee 			    u16 try_exp);
1420783237e8SYuchung Cheng struct tcp_fastopen_request {
1421783237e8SYuchung Cheng 	/* Fast Open cookie. Size 0 means a cookie request */
1422783237e8SYuchung Cheng 	struct tcp_fastopen_cookie	cookie;
1423783237e8SYuchung Cheng 	struct msghdr			*data;  /* data in MSG_FASTOPEN */
1424f5ddcbbbSEric Dumazet 	size_t				size;
1425f5ddcbbbSEric Dumazet 	int				copied;	/* queued in tcp_connect() */
1426783237e8SYuchung Cheng };
1427783237e8SYuchung Cheng void tcp_free_fastopen_req(struct tcp_sock *tp);
1428783237e8SYuchung Cheng 
142910467163SJerry Chu extern struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
143010467163SJerry Chu int tcp_fastopen_reset_cipher(void *key, unsigned int len);
14317c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
14325b7ed089SYuchung Cheng 			      struct request_sock *req,
1433843f4a55SYuchung Cheng 			      struct tcp_fastopen_cookie *foc,
1434843f4a55SYuchung Cheng 			      struct dst_entry *dst);
1435222e83d2SHannes Frederic Sowa void tcp_fastopen_init_key_once(bool publish);
143610467163SJerry Chu #define TCP_FASTOPEN_KEY_LENGTH 16
143710467163SJerry Chu 
143810467163SJerry Chu /* Fastopen key context */
143910467163SJerry Chu struct tcp_fastopen_context {
14407ae8639cSEric Dumazet 	struct crypto_cipher	*tfm;
144110467163SJerry Chu 	__u8			key[TCP_FASTOPEN_KEY_LENGTH];
144210467163SJerry Chu 	struct rcu_head		rcu;
144310467163SJerry Chu };
144410467163SJerry Chu 
1445fe067e8aSDavid S. Miller /* write queue abstraction */
1446fe067e8aSDavid S. Miller static inline void tcp_write_queue_purge(struct sock *sk)
1447fe067e8aSDavid S. Miller {
1448fe067e8aSDavid S. Miller 	struct sk_buff *skb;
1449fe067e8aSDavid S. Miller 
1450fe067e8aSDavid S. Miller 	while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL)
14513ab224beSHideo Aoki 		sk_wmem_free_skb(sk, skb);
14523ab224beSHideo Aoki 	sk_mem_reclaim(sk);
14538818a9d8SIlpo Järvinen 	tcp_clear_all_retrans_hints(tcp_sk(sk));
1454fe067e8aSDavid S. Miller }
1455fe067e8aSDavid S. Miller 
1456cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
1457fe067e8aSDavid S. Miller {
1458cd07a8eaSDavid S. Miller 	return skb_peek(&sk->sk_write_queue);
1459fe067e8aSDavid S. Miller }
1460fe067e8aSDavid S. Miller 
1461cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
1462fe067e8aSDavid S. Miller {
1463cd07a8eaSDavid S. Miller 	return skb_peek_tail(&sk->sk_write_queue);
1464fe067e8aSDavid S. Miller }
1465fe067e8aSDavid S. Miller 
1466cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_next(const struct sock *sk,
1467cf533ea5SEric Dumazet 						   const struct sk_buff *skb)
1468fe067e8aSDavid S. Miller {
1469cd07a8eaSDavid S. Miller 	return skb_queue_next(&sk->sk_write_queue, skb);
1470fe067e8aSDavid S. Miller }
1471fe067e8aSDavid S. Miller 
1472cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_prev(const struct sock *sk,
1473cf533ea5SEric Dumazet 						   const struct sk_buff *skb)
1474832d11c5SIlpo Järvinen {
1475832d11c5SIlpo Järvinen 	return skb_queue_prev(&sk->sk_write_queue, skb);
1476832d11c5SIlpo Järvinen }
1477832d11c5SIlpo Järvinen 
1478fe067e8aSDavid S. Miller #define tcp_for_write_queue(skb, sk)					\
1479cd07a8eaSDavid S. Miller 	skb_queue_walk(&(sk)->sk_write_queue, skb)
1480fe067e8aSDavid S. Miller 
1481fe067e8aSDavid S. Miller #define tcp_for_write_queue_from(skb, sk)				\
1482cd07a8eaSDavid S. Miller 	skb_queue_walk_from(&(sk)->sk_write_queue, skb)
1483fe067e8aSDavid S. Miller 
1484234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
1485cd07a8eaSDavid S. Miller 	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1486234b6860SIlpo Järvinen 
1487cf533ea5SEric Dumazet static inline struct sk_buff *tcp_send_head(const struct sock *sk)
1488fe067e8aSDavid S. Miller {
1489fe067e8aSDavid S. Miller 	return sk->sk_send_head;
1490fe067e8aSDavid S. Miller }
1491fe067e8aSDavid S. Miller 
1492cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk,
1493cd07a8eaSDavid S. Miller 				   const struct sk_buff *skb)
1494cd07a8eaSDavid S. Miller {
1495cd07a8eaSDavid S. Miller 	return skb_queue_is_last(&sk->sk_write_queue, skb);
1496cd07a8eaSDavid S. Miller }
1497cd07a8eaSDavid S. Miller 
1498cf533ea5SEric Dumazet static inline void tcp_advance_send_head(struct sock *sk, const struct sk_buff *skb)
1499fe067e8aSDavid S. Miller {
1500cd07a8eaSDavid S. Miller 	if (tcp_skb_is_last(sk, skb))
1501fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1502cd07a8eaSDavid S. Miller 	else
1503cd07a8eaSDavid S. Miller 		sk->sk_send_head = tcp_write_queue_next(sk, skb);
1504fe067e8aSDavid S. Miller }
1505fe067e8aSDavid S. Miller 
1506fe067e8aSDavid S. Miller static inline void tcp_check_send_head(struct sock *sk, struct sk_buff *skb_unlinked)
1507fe067e8aSDavid S. Miller {
1508fe067e8aSDavid S. Miller 	if (sk->sk_send_head == skb_unlinked)
1509fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1510fe067e8aSDavid S. Miller }
1511fe067e8aSDavid S. Miller 
1512fe067e8aSDavid S. Miller static inline void tcp_init_send_head(struct sock *sk)
1513fe067e8aSDavid S. Miller {
1514fe067e8aSDavid S. Miller 	sk->sk_send_head = NULL;
1515fe067e8aSDavid S. Miller }
1516fe067e8aSDavid S. Miller 
1517fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1518fe067e8aSDavid S. Miller {
1519fe067e8aSDavid S. Miller 	__skb_queue_tail(&sk->sk_write_queue, skb);
1520fe067e8aSDavid S. Miller }
1521fe067e8aSDavid S. Miller 
1522fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1523fe067e8aSDavid S. Miller {
1524fe067e8aSDavid S. Miller 	__tcp_add_write_queue_tail(sk, skb);
1525fe067e8aSDavid S. Miller 
1526fe067e8aSDavid S. Miller 	/* Queue it, remembering where we must start sending. */
15276859d494SIlpo Järvinen 	if (sk->sk_send_head == NULL) {
1528fe067e8aSDavid S. Miller 		sk->sk_send_head = skb;
15296859d494SIlpo Järvinen 
15306859d494SIlpo Järvinen 		if (tcp_sk(sk)->highest_sack == NULL)
15316859d494SIlpo Järvinen 			tcp_sk(sk)->highest_sack = skb;
15326859d494SIlpo Järvinen 	}
1533fe067e8aSDavid S. Miller }
1534fe067e8aSDavid S. Miller 
1535fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_head(struct sock *sk, struct sk_buff *skb)
1536fe067e8aSDavid S. Miller {
1537fe067e8aSDavid S. Miller 	__skb_queue_head(&sk->sk_write_queue, skb);
1538fe067e8aSDavid S. Miller }
1539fe067e8aSDavid S. Miller 
1540fe067e8aSDavid S. Miller /* Insert buff after skb on the write queue of sk.  */
1541fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_after(struct sk_buff *skb,
1542fe067e8aSDavid S. Miller 						struct sk_buff *buff,
1543fe067e8aSDavid S. Miller 						struct sock *sk)
1544fe067e8aSDavid S. Miller {
15457de6c033SGerrit Renker 	__skb_queue_after(&sk->sk_write_queue, skb, buff);
1546fe067e8aSDavid S. Miller }
1547fe067e8aSDavid S. Miller 
154843f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk.  */
1549fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new,
1550fe067e8aSDavid S. Miller 						  struct sk_buff *skb,
1551fe067e8aSDavid S. Miller 						  struct sock *sk)
1552fe067e8aSDavid S. Miller {
155343f59c89SDavid S. Miller 	__skb_queue_before(&sk->sk_write_queue, skb, new);
15546e421410SIlpo Järvinen 
15556e421410SIlpo Järvinen 	if (sk->sk_send_head == skb)
15566e421410SIlpo Järvinen 		sk->sk_send_head = new;
1557fe067e8aSDavid S. Miller }
1558fe067e8aSDavid S. Miller 
1559fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
1560fe067e8aSDavid S. Miller {
1561fe067e8aSDavid S. Miller 	__skb_unlink(skb, &sk->sk_write_queue);
1562fe067e8aSDavid S. Miller }
1563fe067e8aSDavid S. Miller 
1564a2a385d6SEric Dumazet static inline bool tcp_write_queue_empty(struct sock *sk)
1565fe067e8aSDavid S. Miller {
1566fe067e8aSDavid S. Miller 	return skb_queue_empty(&sk->sk_write_queue);
1567fe067e8aSDavid S. Miller }
1568fe067e8aSDavid S. Miller 
156912d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk)
157012d50c46SKrishna Kumar {
157112d50c46SKrishna Kumar 	if (tcp_send_head(sk)) {
157212d50c46SKrishna Kumar 		struct tcp_sock *tp = tcp_sk(sk);
157312d50c46SKrishna Kumar 
157412d50c46SKrishna Kumar 		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
157512d50c46SKrishna Kumar 	}
157612d50c46SKrishna Kumar }
157712d50c46SKrishna Kumar 
1578ecb97192SNeal Cardwell /* Start sequence of the skb just after the highest skb with SACKed
1579ecb97192SNeal Cardwell  * bit, valid only if sacked_out > 0 or when the caller has ensured
1580ecb97192SNeal Cardwell  * validity by itself.
1581a47e5a98SIlpo Järvinen  */
1582a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
1583a47e5a98SIlpo Järvinen {
1584a47e5a98SIlpo Järvinen 	if (!tp->sacked_out)
1585a47e5a98SIlpo Järvinen 		return tp->snd_una;
15866859d494SIlpo Järvinen 
15876859d494SIlpo Järvinen 	if (tp->highest_sack == NULL)
15886859d494SIlpo Järvinen 		return tp->snd_nxt;
15896859d494SIlpo Järvinen 
1590a47e5a98SIlpo Järvinen 	return TCP_SKB_CB(tp->highest_sack)->seq;
1591a47e5a98SIlpo Järvinen }
1592a47e5a98SIlpo Järvinen 
15936859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
15946859d494SIlpo Järvinen {
15956859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_skb_is_last(sk, skb) ? NULL :
15966859d494SIlpo Järvinen 						tcp_write_queue_next(sk, skb);
15976859d494SIlpo Järvinen }
15986859d494SIlpo Järvinen 
15996859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
16006859d494SIlpo Järvinen {
16016859d494SIlpo Järvinen 	return tcp_sk(sk)->highest_sack;
16026859d494SIlpo Järvinen }
16036859d494SIlpo Järvinen 
16046859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk)
16056859d494SIlpo Järvinen {
16066859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_write_queue_head(sk);
16076859d494SIlpo Järvinen }
16086859d494SIlpo Järvinen 
16096859d494SIlpo Järvinen /* Called when old skb is about to be deleted (to be combined with new skb) */
16106859d494SIlpo Järvinen static inline void tcp_highest_sack_combine(struct sock *sk,
16116859d494SIlpo Järvinen 					    struct sk_buff *old,
16126859d494SIlpo Järvinen 					    struct sk_buff *new)
16136859d494SIlpo Järvinen {
16146859d494SIlpo Järvinen 	if (tcp_sk(sk)->sacked_out && (old == tcp_sk(sk)->highest_sack))
16156859d494SIlpo Järvinen 		tcp_sk(sk)->highest_sack = new;
16166859d494SIlpo Järvinen }
16176859d494SIlpo Järvinen 
16185aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from
16195aa4b32fSAndreas Petlund  * increased latency). Used to trigger latency-reducing mechanisms.
16205aa4b32fSAndreas Petlund  */
1621a2a385d6SEric Dumazet static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
16225aa4b32fSAndreas Petlund {
16235aa4b32fSAndreas Petlund 	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
16245aa4b32fSAndreas Petlund }
16255aa4b32fSAndreas Petlund 
16261da177e4SLinus Torvalds /* /proc */
16271da177e4SLinus Torvalds enum tcp_seq_states {
16281da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
16291da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
16301da177e4SLinus Torvalds };
16311da177e4SLinus Torvalds 
163273cb88ecSArjan van de Ven int tcp_seq_open(struct inode *inode, struct file *file);
163373cb88ecSArjan van de Ven 
16341da177e4SLinus Torvalds struct tcp_seq_afinfo {
16351da177e4SLinus Torvalds 	char				*name;
16361da177e4SLinus Torvalds 	sa_family_t			family;
163773cb88ecSArjan van de Ven 	const struct file_operations	*seq_fops;
16389427c4b3SDenis V. Lunev 	struct seq_operations		seq_ops;
16391da177e4SLinus Torvalds };
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds struct tcp_iter_state {
1642a4146b1bSDenis V. Lunev 	struct seq_net_private	p;
16431da177e4SLinus Torvalds 	sa_family_t		family;
16441da177e4SLinus Torvalds 	enum tcp_seq_states	state;
16451da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
1646a7cb5a49SEric W. Biederman 	int			bucket, offset, sbucket, num;
1647a8b690f9STom Herbert 	loff_t			last_pos;
16481da177e4SLinus Torvalds };
16491da177e4SLinus Torvalds 
16505c9f3023SJoe Perches int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo);
16515c9f3023SJoe Perches void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo);
16521da177e4SLinus Torvalds 
165320380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
1654c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops;
165520380731SArnaldo Carvalho de Melo 
16565c9f3023SJoe Perches void tcp_v4_destroy_sock(struct sock *sk);
165720380731SArnaldo Carvalho de Melo 
165828be6e07SEric Dumazet struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
1659c8f44affSMichał Mirosław 				netdev_features_t features);
16605c9f3023SJoe Perches struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb);
16615c9f3023SJoe Perches int tcp_gro_complete(struct sk_buff *skb);
166228850dc7SDaniel Borkmann 
16635c9f3023SJoe Perches void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
1664f4c50d99SHerbert Xu 
1665c9bee3b7SEric Dumazet static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
1666c9bee3b7SEric Dumazet {
1667c9bee3b7SEric Dumazet 	return tp->notsent_lowat ?: sysctl_tcp_notsent_lowat;
1668c9bee3b7SEric Dumazet }
1669c9bee3b7SEric Dumazet 
1670c9bee3b7SEric Dumazet static inline bool tcp_stream_memory_free(const struct sock *sk)
1671c9bee3b7SEric Dumazet {
1672c9bee3b7SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1673c9bee3b7SEric Dumazet 	u32 notsent_bytes = tp->write_seq - tp->snd_nxt;
1674c9bee3b7SEric Dumazet 
1675c9bee3b7SEric Dumazet 	return notsent_bytes < tcp_notsent_lowat(tp);
1676c9bee3b7SEric Dumazet }
1677c9bee3b7SEric Dumazet 
167820380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
16795c9f3023SJoe Perches int tcp4_proc_init(void);
16805c9f3023SJoe Perches void tcp4_proc_exit(void);
168120380731SArnaldo Carvalho de Melo #endif
168220380731SArnaldo Carvalho de Melo 
1683ea3bea3aSEric Dumazet int tcp_rtx_synack(const struct sock *sk, struct request_sock *req);
16841fb6f159SOctavian Purdila int tcp_conn_request(struct request_sock_ops *rsk_ops,
16851fb6f159SOctavian Purdila 		     const struct tcp_request_sock_ops *af_ops,
16861fb6f159SOctavian Purdila 		     struct sock *sk, struct sk_buff *skb);
16875db92c99SOctavian Purdila 
1688cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */
1689cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops {
1690cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1691b83e3debSEric Dumazet 	struct tcp_md5sig_key	*(*md5_lookup) (const struct sock *sk,
1692fd3a154aSEric Dumazet 						const struct sock *addr_sk);
1693cfb6eeb4SYOSHIFUJI Hideaki 	int		(*calc_md5_hash)(char *location,
169439f8e58eSEric Dumazet 					 const struct tcp_md5sig_key *md5,
1695318cf7aaSEric Dumazet 					 const struct sock *sk,
1696318cf7aaSEric Dumazet 					 const struct sk_buff *skb);
1697cfb6eeb4SYOSHIFUJI Hideaki 	int		(*md5_parse)(struct sock *sk,
1698cfb6eeb4SYOSHIFUJI Hideaki 				     char __user *optval,
1699cfb6eeb4SYOSHIFUJI Hideaki 				     int optlen);
1700cfb6eeb4SYOSHIFUJI Hideaki #endif
1701cfb6eeb4SYOSHIFUJI Hideaki };
1702cfb6eeb4SYOSHIFUJI Hideaki 
1703cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops {
17042aec4a29SOctavian Purdila 	u16 mss_clamp;
1705cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1706b83e3debSEric Dumazet 	struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk,
1707fd3a154aSEric Dumazet 						 const struct sock *addr_sk);
1708e3afe7b7SJohn Dykstra 	int		(*calc_md5_hash) (char *location,
170939f8e58eSEric Dumazet 					  const struct tcp_md5sig_key *md5,
1710318cf7aaSEric Dumazet 					  const struct sock *sk,
1711318cf7aaSEric Dumazet 					  const struct sk_buff *skb);
1712cfb6eeb4SYOSHIFUJI Hideaki #endif
1713b40cf18eSEric Dumazet 	void (*init_req)(struct request_sock *req,
1714b40cf18eSEric Dumazet 			 const struct sock *sk_listener,
171516bea70aSOctavian Purdila 			 struct sk_buff *skb);
1716fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
17173f684b4bSEric Dumazet 	__u32 (*cookie_init_seq)(const struct sk_buff *skb,
1718fb7b37a7SOctavian Purdila 				 __u16 *mss);
1719fb7b37a7SOctavian Purdila #endif
1720f964629eSEric Dumazet 	struct dst_entry *(*route_req)(const struct sock *sk, struct flowi *fl,
1721d94e0417SOctavian Purdila 				       const struct request_sock *req,
1722d94e0417SOctavian Purdila 				       bool *strict);
1723936b8bdbSOctavian Purdila 	__u32 (*init_seq)(const struct sk_buff *skb);
17240f935dbeSEric Dumazet 	int (*send_synack)(const struct sock *sk, struct dst_entry *dst,
1725d6274bd8SOctavian Purdila 			   struct flowi *fl, struct request_sock *req,
1726dc6ef6beSEric Dumazet 			   struct tcp_fastopen_cookie *foc,
1727ca6fb065SEric Dumazet 			   bool attach_req);
1728cfb6eeb4SYOSHIFUJI Hideaki };
1729cfb6eeb4SYOSHIFUJI Hideaki 
1730fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
1731fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
17323f684b4bSEric Dumazet 					 const struct sock *sk, struct sk_buff *skb,
1733fb7b37a7SOctavian Purdila 					 __u16 *mss)
1734fb7b37a7SOctavian Purdila {
17353f684b4bSEric Dumazet 	tcp_synq_overflow(sk);
17363f684b4bSEric Dumazet 	NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT);
17373f684b4bSEric Dumazet 	return ops->cookie_init_seq(skb, mss);
1738fb7b37a7SOctavian Purdila }
1739fb7b37a7SOctavian Purdila #else
1740fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
17413f684b4bSEric Dumazet 					 const struct sock *sk, struct sk_buff *skb,
1742fb7b37a7SOctavian Purdila 					 __u16 *mss)
1743fb7b37a7SOctavian Purdila {
1744fb7b37a7SOctavian Purdila 	return 0;
1745fb7b37a7SOctavian Purdila }
1746fb7b37a7SOctavian Purdila #endif
1747fb7b37a7SOctavian Purdila 
17485c9f3023SJoe Perches int tcpv4_offload_init(void);
174928850dc7SDaniel Borkmann 
17505c9f3023SJoe Perches void tcp_v4_init(void);
17515c9f3023SJoe Perches void tcp_init(void);
175220380731SArnaldo Carvalho de Melo 
1753*659a8ad5SYuchung Cheng /* tcp_recovery.c */
1754*659a8ad5SYuchung Cheng 
1755*659a8ad5SYuchung Cheng extern void tcp_rack_advance(struct tcp_sock *tp,
1756*659a8ad5SYuchung Cheng 			     const struct skb_mstamp *xmit_time, u8 sacked);
1757*659a8ad5SYuchung Cheng 
1758e25f866fSCong Wang /*
1759e25f866fSCong Wang  * Save and compile IPv4 options, return a pointer to it
1760e25f866fSCong Wang  */
1761e25f866fSCong Wang static inline struct ip_options_rcu *tcp_v4_save_options(struct sk_buff *skb)
1762e25f866fSCong Wang {
1763e25f866fSCong Wang 	const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
1764e25f866fSCong Wang 	struct ip_options_rcu *dopt = NULL;
1765e25f866fSCong Wang 
1766461b74c3SCong Wang 	if (opt->optlen) {
1767e25f866fSCong Wang 		int opt_size = sizeof(*dopt) + opt->optlen;
1768e25f866fSCong Wang 
1769e25f866fSCong Wang 		dopt = kmalloc(opt_size, GFP_ATOMIC);
1770e25f866fSCong Wang 		if (dopt && __ip_options_echo(&dopt->opt, skb, opt)) {
1771e25f866fSCong Wang 			kfree(dopt);
1772e25f866fSCong Wang 			dopt = NULL;
1773e25f866fSCong Wang 		}
1774e25f866fSCong Wang 	}
1775e25f866fSCong Wang 	return dopt;
1776e25f866fSCong Wang }
1777e25f866fSCong Wang 
177898781965SEric Dumazet /* locally generated TCP pure ACKs have skb->truesize == 2
177998781965SEric Dumazet  * (check tcp_send_ack() in net/ipv4/tcp_output.c )
178098781965SEric Dumazet  * This is much faster than dissecting the packet to find out.
178198781965SEric Dumazet  * (Think of GRE encapsulations, IPv4, IPv6, ...)
178298781965SEric Dumazet  */
178398781965SEric Dumazet static inline bool skb_is_tcp_pure_ack(const struct sk_buff *skb)
178498781965SEric Dumazet {
178598781965SEric Dumazet 	return skb->truesize == 2;
178698781965SEric Dumazet }
178798781965SEric Dumazet 
178898781965SEric Dumazet static inline void skb_set_tcp_pure_ack(struct sk_buff *skb)
178998781965SEric Dumazet {
179098781965SEric Dumazet 	skb->truesize = 2;
179198781965SEric Dumazet }
179298781965SEric Dumazet 
17931da177e4SLinus Torvalds #endif	/* _TCP_H */
1794