xref: /linux/include/net/tcp.h (revision dca145ffaa8d39ea1904491ac81b92b7049372c0)
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 */
685d424d5aSJohn Heffner #define TCP_BASE_MSS		512
695d424d5aSJohn Heffner 
701da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
711da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
741da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /* urg_data states */
771da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
781da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
791da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds #define TCP_RETR1	3	/*
821da177e4SLinus Torvalds 				 * This is how many retries it does before it
831da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
841da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
851da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
861da177e4SLinus Torvalds 				 */
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds #define TCP_RETR2	15	/*
891da177e4SLinus Torvalds 				 * This should take at least
901da177e4SLinus Torvalds 				 * 90 minutes to time out.
911da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
921da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
931da177e4SLinus Torvalds 				 */
941da177e4SLinus Torvalds 
956c9ff979SAlex Bergmann #define TCP_SYN_RETRIES	 6	/* This is how many retries are done
966c9ff979SAlex Bergmann 				 * when active opening a connection.
976c9ff979SAlex Bergmann 				 * RFC1122 says the minimum retry MUST
986c9ff979SAlex Bergmann 				 * be at least 180secs.  Nevertheless
996c9ff979SAlex Bergmann 				 * this value is corresponding to
1006c9ff979SAlex Bergmann 				 * 63secs of retransmission with the
1016c9ff979SAlex Bergmann 				 * current initial RTO.
1026c9ff979SAlex Bergmann 				 */
1031da177e4SLinus Torvalds 
1046c9ff979SAlex Bergmann #define TCP_SYNACK_RETRIES 5	/* This is how may retries are done
1056c9ff979SAlex Bergmann 				 * when passive opening a connection.
1066c9ff979SAlex Bergmann 				 * This is corresponding to 31secs of
1076c9ff979SAlex Bergmann 				 * retransmission with the current
1086c9ff979SAlex Bergmann 				 * initial RTO.
1096c9ff979SAlex Bergmann 				 */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1121da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1131da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1141da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1151da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1161da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1171da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1181da177e4SLinus Torvalds 				  */
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1211da177e4SLinus Torvalds #if HZ >= 100
1221da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1231da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1241da177e4SLinus Torvalds #else
1251da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1261da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1271da177e4SLinus Torvalds #endif
1281da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1291da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
130fd4f2ceaSEric Dumazet #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))	/* RFC6298 2.1 initial RTO value	*/
1319ad7c049SJerry Chu #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value, now
1329ad7c049SJerry Chu 						 * used as a fallback RTO for the
1339ad7c049SJerry Chu 						 * initial data transmission if no
1349ad7c049SJerry Chu 						 * valid RTT sample has been acquired,
1359ad7c049SJerry Chu 						 * most likely due to retrans in 3WHS.
1369ad7c049SJerry Chu 						 */
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1391da177e4SLinus Torvalds 					                 * for local resources.
1401da177e4SLinus Torvalds 					                 */
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1431da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1441da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1471da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1481da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1491da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1541da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1551da177e4SLinus Torvalds 					 * after this time. It should be equal
1561da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1571da177e4SLinus Torvalds 					 * to provide reliability equal to one
1581da177e4SLinus Torvalds 					 * provided by timewait state.
1591da177e4SLinus Torvalds 					 */
1601da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1611da177e4SLinus Torvalds 					 * timestamps. It must be less than
1621da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1631da177e4SLinus Torvalds 					 */
1641da177e4SLinus Torvalds /*
1651da177e4SLinus Torvalds  *	TCP option
1661da177e4SLinus Torvalds  */
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1691da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1701da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1711da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1721da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1731da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1741da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
175cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
1762100c8d2SYuchung Cheng #define TCPOPT_EXP		254	/* Experimental */
1772100c8d2SYuchung Cheng /* Magic number to be after the option value for sharing TCP
1782100c8d2SYuchung Cheng  * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
1792100c8d2SYuchung Cheng  */
1802100c8d2SYuchung Cheng #define TCPOPT_FASTOPEN_MAGIC	0xF989
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds /*
1831da177e4SLinus Torvalds  *     TCP option lengths
1841da177e4SLinus Torvalds  */
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds #define TCPOLEN_MSS            4
1871da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
1881da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
1891da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
190cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG         18
1912100c8d2SYuchung Cheng #define TCPOLEN_EXP_FASTOPEN_BASE  4
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds /* But this is what stacks really send out. */
1941da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
1951da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
1961da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
1971da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
1981da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
1991da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
200cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED		20
20133ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED		4
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds /* Flags in tp->nonagle */
2041da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
2051da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
206caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
2071da177e4SLinus Torvalds 
20836e31b0aSAndreas Petlund /* TCP thin-stream limits */
20936e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
21036e31b0aSAndreas Petlund 
2117eb38527SDavid S. Miller /* TCP initial congestion window as per draft-hkchu-tcpm-initcwnd-01 */
212442b9635SDavid S. Miller #define TCP_INIT_CWND		10
213442b9635SDavid S. Miller 
214cf60af03SYuchung Cheng /* Bit Flags for sysctl_tcp_fastopen */
215cf60af03SYuchung Cheng #define	TFO_CLIENT_ENABLE	1
21610467163SJerry Chu #define	TFO_SERVER_ENABLE	2
21767da22d2SYuchung Cheng #define	TFO_CLIENT_NO_COOKIE	4	/* Data in SYN w/o cookie option */
218cf60af03SYuchung Cheng 
21910467163SJerry Chu /* Accept SYN data w/o any cookie option */
22010467163SJerry Chu #define	TFO_SERVER_COOKIE_NOT_REQD	0x200
22110467163SJerry Chu 
22210467163SJerry Chu /* Force enable TFO on all listeners, i.e., not requiring the
22310467163SJerry Chu  * TCP_FASTOPEN socket option. SOCKOPT1/2 determine how to set max_qlen.
22410467163SJerry Chu  */
22510467163SJerry Chu #define	TFO_SERVER_WO_SOCKOPT1	0x400
22610467163SJerry Chu #define	TFO_SERVER_WO_SOCKOPT2	0x800
22710467163SJerry Chu 
228295ff7edSArnaldo Carvalho de Melo extern struct inet_timewait_death_row tcp_death_row;
229295ff7edSArnaldo Carvalho de Melo 
2301da177e4SLinus Torvalds /* sysctl variables for tcp */
2311da177e4SLinus Torvalds extern int sysctl_tcp_timestamps;
2321da177e4SLinus Torvalds extern int sysctl_tcp_window_scaling;
2331da177e4SLinus Torvalds extern int sysctl_tcp_sack;
2341da177e4SLinus Torvalds extern int sysctl_tcp_fin_timeout;
2351da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_time;
2361da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_probes;
2371da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_intvl;
2381da177e4SLinus Torvalds extern int sysctl_tcp_syn_retries;
2391da177e4SLinus Torvalds extern int sysctl_tcp_synack_retries;
2401da177e4SLinus Torvalds extern int sysctl_tcp_retries1;
2411da177e4SLinus Torvalds extern int sysctl_tcp_retries2;
2421da177e4SLinus Torvalds extern int sysctl_tcp_orphan_retries;
2431da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
2442100c8d2SYuchung Cheng extern int sysctl_tcp_fastopen;
2451da177e4SLinus Torvalds extern int sysctl_tcp_retrans_collapse;
2461da177e4SLinus Torvalds extern int sysctl_tcp_stdurg;
2471da177e4SLinus Torvalds extern int sysctl_tcp_rfc1337;
2481da177e4SLinus Torvalds extern int sysctl_tcp_abort_on_overflow;
2491da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
2501da177e4SLinus Torvalds extern int sysctl_tcp_fack;
2511da177e4SLinus Torvalds extern int sysctl_tcp_reordering;
252*dca145ffSEric Dumazet extern int sysctl_tcp_max_reordering;
2531da177e4SLinus Torvalds extern int sysctl_tcp_dsack;
254a4fe34bfSEric W. Biederman extern long sysctl_tcp_mem[3];
2551da177e4SLinus Torvalds extern int sysctl_tcp_wmem[3];
2561da177e4SLinus Torvalds extern int sysctl_tcp_rmem[3];
2571da177e4SLinus Torvalds extern int sysctl_tcp_app_win;
2581da177e4SLinus Torvalds extern int sysctl_tcp_adv_win_scale;
2591da177e4SLinus Torvalds extern int sysctl_tcp_tw_reuse;
2601da177e4SLinus Torvalds extern int sysctl_tcp_frto;
2611da177e4SLinus Torvalds extern int sysctl_tcp_low_latency;
2621da177e4SLinus Torvalds extern int sysctl_tcp_nometrics_save;
2631da177e4SLinus Torvalds extern int sysctl_tcp_moderate_rcvbuf;
2641da177e4SLinus Torvalds extern int sysctl_tcp_tso_win_divisor;
2655d424d5aSJohn Heffner extern int sysctl_tcp_mtu_probing;
2665d424d5aSJohn Heffner extern int sysctl_tcp_base_mss;
26715d99e02SRick Jones extern int sysctl_tcp_workaround_signed_windows;
26835089bb2SDavid S. Miller extern int sysctl_tcp_slow_start_after_idle;
26936e31b0aSAndreas Petlund extern int sysctl_tcp_thin_linear_timeouts;
2707e380175SAndreas Petlund extern int sysctl_tcp_thin_dupack;
271eed530b6SYuchung Cheng extern int sysctl_tcp_early_retrans;
27246d3ceabSEric Dumazet extern int sysctl_tcp_limit_output_bytes;
273282f23c6SEric Dumazet extern int sysctl_tcp_challenge_ack_limit;
274c9bee3b7SEric Dumazet extern unsigned int sysctl_tcp_notsent_lowat;
27595bd09ebSEric Dumazet extern int sysctl_tcp_min_tso_segs;
276f54b3111SEric Dumazet extern int sysctl_tcp_autocorking;
2771da177e4SLinus Torvalds 
2788d987e5cSEric Dumazet extern atomic_long_t tcp_memory_allocated;
2791748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated;
2801da177e4SLinus Torvalds extern int tcp_memory_pressure;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds /*
2831da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
2841da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
2851da177e4SLinus Torvalds  */
2861da177e4SLinus Torvalds 
287a2a385d6SEric Dumazet static inline bool before(__u32 seq1, __u32 seq2)
2881da177e4SLinus Torvalds {
2890d630cc0SGerrit Renker         return (__s32)(seq1-seq2) < 0;
2901da177e4SLinus Torvalds }
2919a036b9cSGerrit Renker #define after(seq2, seq1) 	before(seq1, seq2)
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
294a2a385d6SEric Dumazet static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
299efcdbf24SArun Sharma static inline bool tcp_out_of_memory(struct sock *sk)
300efcdbf24SArun Sharma {
301efcdbf24SArun Sharma 	if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
302efcdbf24SArun Sharma 	    sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
303efcdbf24SArun Sharma 		return true;
304efcdbf24SArun Sharma 	return false;
305efcdbf24SArun Sharma }
306efcdbf24SArun Sharma 
307ad1af0feSDavid S. Miller static inline bool tcp_too_many_orphans(struct sock *sk, int shift)
308e4fd5da3SPavel Emelianov {
309ad1af0feSDavid S. Miller 	struct percpu_counter *ocp = sk->sk_prot->orphan_count;
310ad1af0feSDavid S. Miller 	int orphans = percpu_counter_read_positive(ocp);
311ad1af0feSDavid S. Miller 
312ad1af0feSDavid S. Miller 	if (orphans << shift > sysctl_tcp_max_orphans) {
313ad1af0feSDavid S. Miller 		orphans = percpu_counter_sum_positive(ocp);
314ad1af0feSDavid S. Miller 		if (orphans << shift > sysctl_tcp_max_orphans)
315ad1af0feSDavid S. Miller 			return true;
316ad1af0feSDavid S. Miller 	}
317ad1af0feSDavid S. Miller 	return false;
318e4fd5da3SPavel Emelianov }
3191da177e4SLinus Torvalds 
3205c9f3023SJoe Perches bool tcp_check_oom(struct sock *sk, int shift);
321efcdbf24SArun Sharma 
322a0f82f64SFlorian Westphal /* syncookies: remember time of last synqueue overflow */
323a0f82f64SFlorian Westphal static inline void tcp_synq_overflow(struct sock *sk)
324a0f82f64SFlorian Westphal {
325a0f82f64SFlorian Westphal 	tcp_sk(sk)->rx_opt.ts_recent_stamp = jiffies;
326a0f82f64SFlorian Westphal }
327a0f82f64SFlorian Westphal 
328a0f82f64SFlorian Westphal /* syncookies: no recent synqueue overflow on this listening socket? */
329a2a385d6SEric Dumazet static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
330a0f82f64SFlorian Westphal {
331a0f82f64SFlorian Westphal 	unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
3329ad7c049SJerry Chu 	return time_after(jiffies, last_overflow + TCP_TIMEOUT_FALLBACK);
333a0f82f64SFlorian Westphal }
334a0f82f64SFlorian Westphal 
3351da177e4SLinus Torvalds extern struct proto tcp_prot;
3361da177e4SLinus Torvalds 
33757ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
33857ef42d5SPavel Emelyanov #define TCP_INC_STATS_BH(net, field)	SNMP_INC_STATS_BH((net)->mib.tcp_statistics, field)
33957ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
34057ef42d5SPavel Emelyanov #define TCP_ADD_STATS_USER(net, field, val) SNMP_ADD_STATS_USER((net)->mib.tcp_statistics, field, val)
341aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
3421da177e4SLinus Torvalds 
3435c9f3023SJoe Perches void tcp_tasklet_init(void);
34446d3ceabSEric Dumazet 
3455c9f3023SJoe Perches void tcp_v4_err(struct sk_buff *skb, u32);
3461da177e4SLinus Torvalds 
3475c9f3023SJoe Perches void tcp_shutdown(struct sock *sk, int how);
3481da177e4SLinus Torvalds 
3495c9f3023SJoe Perches void tcp_v4_early_demux(struct sk_buff *skb);
3505c9f3023SJoe Perches int tcp_v4_rcv(struct sk_buff *skb);
3511da177e4SLinus Torvalds 
3525c9f3023SJoe Perches int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
3535c9f3023SJoe Perches int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
3547ba42910SChangli Gao 		size_t size);
3555c9f3023SJoe Perches int tcp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
3565c9f3023SJoe Perches 		 int flags);
3575c9f3023SJoe Perches void tcp_release_cb(struct sock *sk);
3585c9f3023SJoe Perches void tcp_wfree(struct sk_buff *skb);
3595c9f3023SJoe Perches void tcp_write_timer_handler(struct sock *sk);
3605c9f3023SJoe Perches void tcp_delack_timer_handler(struct sock *sk);
3615c9f3023SJoe Perches int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
3625c9f3023SJoe Perches int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
363cf533ea5SEric Dumazet 			  const struct tcphdr *th, unsigned int len);
3645c9f3023SJoe Perches void tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
365cf533ea5SEric Dumazet 			 const struct tcphdr *th, unsigned int len);
3665c9f3023SJoe Perches void tcp_rcv_space_adjust(struct sock *sk);
3675c9f3023SJoe Perches int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
3685c9f3023SJoe Perches void tcp_twsk_destructor(struct sock *sk);
3695c9f3023SJoe Perches ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
37053d3176bSChangli Gao 			struct pipe_inode_info *pipe, size_t len,
37153d3176bSChangli Gao 			unsigned int flags);
3729c55e01cSJens Axboe 
373463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
374463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3751da177e4SLinus Torvalds {
376463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
377fc6415bcSDavid S. Miller 
378463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
379463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
380463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3811da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
382463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
383fc6415bcSDavid S. Miller 		} else
384463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
388bdf1ee5dSIlpo Järvinen #define	TCP_ECN_OK		1
389bdf1ee5dSIlpo Järvinen #define	TCP_ECN_QUEUE_CWR	2
390bdf1ee5dSIlpo Järvinen #define	TCP_ECN_DEMAND_CWR	4
3917a269ffaSEric Dumazet #define	TCP_ECN_SEEN		8
392bdf1ee5dSIlpo Järvinen 
393fd2c3ef7SEric Dumazet enum tcp_tw_status {
3941da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
3951da177e4SLinus Torvalds 	TCP_TW_RST = 1,
3961da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
3971da177e4SLinus Torvalds 	TCP_TW_SYN = 3
3981da177e4SLinus Torvalds };
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 
4015c9f3023SJoe Perches enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
4021da177e4SLinus Torvalds 					      struct sk_buff *skb,
4038feaf0c0SArnaldo Carvalho de Melo 					      const struct tcphdr *th);
4045c9f3023SJoe Perches struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
4055c9f3023SJoe Perches 			   struct request_sock *req, struct request_sock **prev,
4068336886fSJerry Chu 			   bool fastopen);
4075c9f3023SJoe Perches int tcp_child_process(struct sock *parent, struct sock *child,
4081da177e4SLinus Torvalds 		      struct sk_buff *skb);
4095ae344c9SNeal Cardwell void tcp_enter_loss(struct sock *sk);
4105c9f3023SJoe Perches void tcp_clear_retrans(struct tcp_sock *tp);
4115c9f3023SJoe Perches void tcp_update_metrics(struct sock *sk);
4125c9f3023SJoe Perches void tcp_init_metrics(struct sock *sk);
4135c9f3023SJoe Perches void tcp_metrics_init(void);
4145c9f3023SJoe Perches bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst,
415a26552afSHannes Frederic Sowa 			bool paws_check, bool timestamps);
4165c9f3023SJoe Perches bool tcp_remember_stamp(struct sock *sk);
4175c9f3023SJoe Perches bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw);
4185c9f3023SJoe Perches void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst);
4195c9f3023SJoe Perches void tcp_disable_fack(struct tcp_sock *tp);
4205c9f3023SJoe Perches void tcp_close(struct sock *sk, long timeout);
4215c9f3023SJoe Perches void tcp_init_sock(struct sock *sk);
4225c9f3023SJoe Perches unsigned int tcp_poll(struct file *file, struct socket *sock,
42353d3176bSChangli Gao 		      struct poll_table_struct *wait);
4245c9f3023SJoe Perches int tcp_getsockopt(struct sock *sk, int level, int optname,
4253fdadf7dSDmitry Mishin 		   char __user *optval, int __user *optlen);
4265c9f3023SJoe Perches int tcp_setsockopt(struct sock *sk, int level, int optname,
42753d3176bSChangli Gao 		   char __user *optval, unsigned int optlen);
4285c9f3023SJoe Perches int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
42953d3176bSChangli Gao 			  char __user *optval, int __user *optlen);
4305c9f3023SJoe Perches int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
431b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen);
4325c9f3023SJoe Perches void tcp_set_keepalive(struct sock *sk, int val);
4335c9f3023SJoe Perches void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req);
4345c9f3023SJoe Perches int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
43553d3176bSChangli Gao 		size_t len, int nonblock, int flags, int *addr_len);
4365c9f3023SJoe Perches void tcp_parse_options(const struct sk_buff *skb,
4371a2c6181SChristoph Paasch 		       struct tcp_options_received *opt_rx,
4382100c8d2SYuchung Cheng 		       int estab, struct tcp_fastopen_cookie *foc);
4395c9f3023SJoe Perches const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
4407d5d5525SYOSHIFUJI Hideaki 
4411da177e4SLinus Torvalds /*
4421da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4431da177e4SLinus Torvalds  */
4441da177e4SLinus Torvalds 
4455c9f3023SJoe Perches void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
4464fab9071SNeal Cardwell void tcp_v4_mtu_reduced(struct sock *sk);
4475c9f3023SJoe Perches int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
4485c9f3023SJoe Perches struct sock *tcp_create_openreq_child(struct sock *sk,
44960236fddSArnaldo Carvalho de Melo 				      struct request_sock *req,
4501da177e4SLinus Torvalds 				      struct sk_buff *skb);
4515c9f3023SJoe Perches struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
45260236fddSArnaldo Carvalho de Melo 				  struct request_sock *req,
4531da177e4SLinus Torvalds 				  struct dst_entry *dst);
4545c9f3023SJoe Perches int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
4555c9f3023SJoe Perches int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
4565c9f3023SJoe Perches int tcp_connect(struct sock *sk);
4575c9f3023SJoe Perches struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
458e6b4d113SWilliam Allen Simpson 				struct request_sock *req,
4598336886fSJerry Chu 				struct tcp_fastopen_cookie *foc);
4605c9f3023SJoe Perches int tcp_disconnect(struct sock *sk, int flags);
4611da177e4SLinus Torvalds 
462370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
463292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
46463d02d15SEric Dumazet void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds /* From syncookies.c */
4675c9f3023SJoe Perches int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
4680198230bSPatrick McHardy 		      u32 cookie);
469461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb);
470e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
4718c27bd75SFlorian Westphal 
47263262315SEric Dumazet /* Syncookies use a monotonic timer which increments every 60 seconds.
4738c27bd75SFlorian Westphal  * This counter is used both as a hash input and partially encoded into
4748c27bd75SFlorian Westphal  * the cookie value.  A cookie is only validated further if the delta
4758c27bd75SFlorian Westphal  * between the current counter value and the encoded one is less than this,
47663262315SEric Dumazet  * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if
4778c27bd75SFlorian Westphal  * the counter advances immediately after a cookie is generated).
4788c27bd75SFlorian Westphal  */
4798c27bd75SFlorian Westphal #define MAX_SYNCOOKIE_AGE 2
4808c27bd75SFlorian Westphal 
4818c27bd75SFlorian Westphal static inline u32 tcp_cookie_time(void)
4828c27bd75SFlorian Westphal {
48363262315SEric Dumazet 	u64 val = get_jiffies_64();
48463262315SEric Dumazet 
48563262315SEric Dumazet 	do_div(val, 60 * HZ);
48663262315SEric Dumazet 	return val;
4878c27bd75SFlorian Westphal }
4888c27bd75SFlorian Westphal 
4895c9f3023SJoe Perches u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
4905c9f3023SJoe Perches 			      u16 *mssp);
49157b47553SOctavian Purdila __u32 cookie_v4_init_sequence(struct sock *sk, const struct sk_buff *skb,
49257b47553SOctavian Purdila 			      __u16 *mss);
493e05c82d3SEric Dumazet #endif
4941da177e4SLinus Torvalds 
4955c9f3023SJoe Perches __u32 cookie_init_timestamp(struct request_sock *req);
4965c9f3023SJoe Perches bool cookie_check_timestamp(struct tcp_options_received *opt, struct net *net,
4975c9f3023SJoe Perches 			    bool *ecn_ok);
4984dfc2817SFlorian Westphal 
499c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */
5005c9f3023SJoe Perches int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
50181eb6a14SPatrick McHardy 		      u32 cookie);
5025c9f3023SJoe Perches struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
503e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
5045c9f3023SJoe Perches u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
50581eb6a14SPatrick McHardy 			      const struct tcphdr *th, u16 *mssp);
5065c9f3023SJoe Perches __u32 cookie_v6_init_sequence(struct sock *sk, const struct sk_buff *skb,
507c6aefafbSGlenn Griffin 			      __u16 *mss);
508e05c82d3SEric Dumazet #endif
5091da177e4SLinus Torvalds /* tcp_output.c */
5101da177e4SLinus Torvalds 
5115c9f3023SJoe Perches void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
5129e412ba7SIlpo Järvinen 			       int nonagle);
5135c9f3023SJoe Perches bool tcp_may_send_now(struct sock *sk);
5145c9f3023SJoe Perches int __tcp_retransmit_skb(struct sock *, struct sk_buff *);
5155c9f3023SJoe Perches int tcp_retransmit_skb(struct sock *, struct sk_buff *);
5165c9f3023SJoe Perches void tcp_retransmit_timer(struct sock *sk);
5175c9f3023SJoe Perches void tcp_xmit_retransmit_queue(struct sock *);
5185c9f3023SJoe Perches void tcp_simple_retransmit(struct sock *);
5195c9f3023SJoe Perches int tcp_trim_head(struct sock *, struct sk_buff *, u32);
5206cc55e09SOctavian Purdila int tcp_fragment(struct sock *, struct sk_buff *, u32, unsigned int, gfp_t);
5211da177e4SLinus Torvalds 
5225c9f3023SJoe Perches void tcp_send_probe0(struct sock *);
5235c9f3023SJoe Perches void tcp_send_partial(struct sock *);
5245c9f3023SJoe Perches int tcp_write_wakeup(struct sock *);
5255c9f3023SJoe Perches void tcp_send_fin(struct sock *sk);
5265c9f3023SJoe Perches void tcp_send_active_reset(struct sock *sk, gfp_t priority);
5275c9f3023SJoe Perches int tcp_send_synack(struct sock *);
5285c9f3023SJoe Perches bool tcp_syn_flood_action(struct sock *sk, const struct sk_buff *skb,
529946cedccSEric Dumazet 			  const char *proto);
5305c9f3023SJoe Perches void tcp_push_one(struct sock *, unsigned int mss_now);
5315c9f3023SJoe Perches void tcp_send_ack(struct sock *sk);
5325c9f3023SJoe Perches void tcp_send_delayed_ack(struct sock *sk);
5335c9f3023SJoe Perches void tcp_send_loss_probe(struct sock *sk);
5345c9f3023SJoe Perches bool tcp_schedule_loss_probe(struct sock *sk);
5351da177e4SLinus Torvalds 
536a762a980SDavid S. Miller /* tcp_input.c */
5375c9f3023SJoe Perches void tcp_resume_early_retransmit(struct sock *sk);
5385c9f3023SJoe Perches void tcp_rearm_rto(struct sock *sk);
5395c9f3023SJoe Perches void tcp_reset(struct sock *sk);
540a762a980SDavid S. Miller 
5411da177e4SLinus Torvalds /* tcp_timer.c */
5425c9f3023SJoe Perches void tcp_init_xmit_timers(struct sock *);
543463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
544463c84b9SArnaldo Carvalho de Melo {
545463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
546463c84b9SArnaldo Carvalho de Melo }
5471da177e4SLinus Torvalds 
5485c9f3023SJoe Perches unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
5495c9f3023SJoe Perches unsigned int tcp_current_mss(struct sock *sk);
5500c54b85fSIlpo Järvinen 
5510c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */
5520c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
5530c54b85fSIlpo Järvinen {
55401f83d69SAlexey Kuznetsov 	int cutoff;
55501f83d69SAlexey Kuznetsov 
55601f83d69SAlexey Kuznetsov 	/* When peer uses tiny windows, there is no use in packetizing
55701f83d69SAlexey Kuznetsov 	 * to sub-MSS pieces for the sake of SWS or making sure there
55801f83d69SAlexey Kuznetsov 	 * are enough packets in the pipe for fast recovery.
55901f83d69SAlexey Kuznetsov 	 *
56001f83d69SAlexey Kuznetsov 	 * On the other hand, for extremely large MSS devices, handling
56101f83d69SAlexey Kuznetsov 	 * smaller than MSS windows in this way does make sense.
56201f83d69SAlexey Kuznetsov 	 */
56301f83d69SAlexey Kuznetsov 	if (tp->max_window >= 512)
56401f83d69SAlexey Kuznetsov 		cutoff = (tp->max_window >> 1);
56501f83d69SAlexey Kuznetsov 	else
56601f83d69SAlexey Kuznetsov 		cutoff = tp->max_window;
56701f83d69SAlexey Kuznetsov 
56801f83d69SAlexey Kuznetsov 	if (cutoff && pktsize > cutoff)
56901f83d69SAlexey Kuznetsov 		return max_t(int, cutoff, 68U - tp->tcp_header_len);
5700c54b85fSIlpo Järvinen 	else
5710c54b85fSIlpo Järvinen 		return pktsize;
5720c54b85fSIlpo Järvinen }
5731da177e4SLinus Torvalds 
57417b085eaSArnaldo Carvalho de Melo /* tcp.c */
5755c9f3023SJoe Perches void tcp_get_info(const struct sock *, struct tcp_info *);
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
5781da177e4SLinus Torvalds typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
5791da177e4SLinus Torvalds 				unsigned int, size_t);
5805c9f3023SJoe Perches int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
5811da177e4SLinus Torvalds 		  sk_read_actor_t recv_actor);
5821da177e4SLinus Torvalds 
5835c9f3023SJoe Perches void tcp_initialize_rcv_mss(struct sock *sk);
5841da177e4SLinus Torvalds 
5855c9f3023SJoe Perches int tcp_mtu_to_mss(struct sock *sk, int pmtu);
5865c9f3023SJoe Perches int tcp_mss_to_mtu(struct sock *sk, int mss);
5875c9f3023SJoe Perches void tcp_mtup_init(struct sock *sk);
5885c9f3023SJoe Perches void tcp_init_buffer_space(struct sock *sk);
5895d424d5aSJohn Heffner 
590f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk)
591f1ecd5d9SDamian Lukowski {
592f1ecd5d9SDamian Lukowski 	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
593f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
594f1ecd5d9SDamian Lukowski }
595f1ecd5d9SDamian Lukowski 
596f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
597f1ecd5d9SDamian Lukowski {
598740b0f18SEric Dumazet 	return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us);
599f1ecd5d9SDamian Lukowski }
600f1ecd5d9SDamian Lukowski 
60140efc6faSStephen Hemminger static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
6021da177e4SLinus Torvalds {
6031da177e4SLinus Torvalds 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
6041da177e4SLinus Torvalds 			       ntohl(TCP_FLAG_ACK) |
6051da177e4SLinus Torvalds 			       snd_wnd);
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
60840efc6faSStephen Hemminger static inline void tcp_fast_path_on(struct tcp_sock *tp)
6091da177e4SLinus Torvalds {
6101da177e4SLinus Torvalds 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6139e412ba7SIlpo Järvinen static inline void tcp_fast_path_check(struct sock *sk)
6141da177e4SLinus Torvalds {
6159e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
6169e412ba7SIlpo Järvinen 
617b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue) &&
6181da177e4SLinus Torvalds 	    tp->rcv_wnd &&
6191da177e4SLinus Torvalds 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
6201da177e4SLinus Torvalds 	    !tp->urg_data)
6211da177e4SLinus Torvalds 		tcp_fast_path_on(tp);
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
6240c266898SSatoru SATOH /* Compute the actual rto_min value */
6250c266898SSatoru SATOH static inline u32 tcp_rto_min(struct sock *sk)
6260c266898SSatoru SATOH {
627cf533ea5SEric Dumazet 	const struct dst_entry *dst = __sk_dst_get(sk);
6280c266898SSatoru SATOH 	u32 rto_min = TCP_RTO_MIN;
6290c266898SSatoru SATOH 
6300c266898SSatoru SATOH 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
6310c266898SSatoru SATOH 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
6320c266898SSatoru SATOH 	return rto_min;
6330c266898SSatoru SATOH }
6340c266898SSatoru SATOH 
635740b0f18SEric Dumazet static inline u32 tcp_rto_min_us(struct sock *sk)
636740b0f18SEric Dumazet {
637740b0f18SEric Dumazet 	return jiffies_to_usecs(tcp_rto_min(sk));
638740b0f18SEric Dumazet }
639740b0f18SEric Dumazet 
6401da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
6411da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
6421da177e4SLinus Torvalds  * than the offered window.
6431da177e4SLinus Torvalds  */
64440efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds 	if (win < 0)
6491da177e4SLinus Torvalds 		win = 0;
6501da177e4SLinus Torvalds 	return (u32) win;
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
6541da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
6551da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
6561da177e4SLinus Torvalds  */
6575c9f3023SJoe Perches u32 __tcp_select_window(struct sock *sk);
6581da177e4SLinus Torvalds 
659ee995283SPavel Emelyanov void tcp_send_window_probe(struct sock *sk);
660ee995283SPavel Emelyanov 
6611da177e4SLinus Torvalds /* TCP timestamps are only 32-bits, this causes a slight
6621da177e4SLinus Torvalds  * complication on 64-bit systems since we store a snapshot
66331f34269SStephen Hemminger  * of jiffies in the buffer control blocks below.  We decided
66431f34269SStephen Hemminger  * to use only the low 32-bits of jiffies and hide the ugly
6651da177e4SLinus Torvalds  * casts with the following macro.
6661da177e4SLinus Torvalds  */
6671da177e4SLinus Torvalds #define tcp_time_stamp		((__u32)(jiffies))
6681da177e4SLinus Torvalds 
6697faee5c0SEric Dumazet static inline u32 tcp_skb_timestamp(const struct sk_buff *skb)
6707faee5c0SEric Dumazet {
6717faee5c0SEric Dumazet 	return skb->skb_mstamp.stamp_jiffies;
6727faee5c0SEric Dumazet }
6737faee5c0SEric Dumazet 
6747faee5c0SEric Dumazet 
675a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
676a3433f35SChangli Gao 
677a3433f35SChangli Gao #define TCPHDR_FIN 0x01
678a3433f35SChangli Gao #define TCPHDR_SYN 0x02
679a3433f35SChangli Gao #define TCPHDR_RST 0x04
680a3433f35SChangli Gao #define TCPHDR_PSH 0x08
681a3433f35SChangli Gao #define TCPHDR_ACK 0x10
682a3433f35SChangli Gao #define TCPHDR_URG 0x20
683a3433f35SChangli Gao #define TCPHDR_ECE 0x40
684a3433f35SChangli Gao #define TCPHDR_CWR 0x80
685a3433f35SChangli Gao 
686caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
687f86586faSEric Dumazet  * TCP per-packet control information to the transmission code.
688f86586faSEric Dumazet  * We also store the host-order sequence numbers in here too.
689f86586faSEric Dumazet  * This is 44 bytes if IPV6 is enabled.
690f86586faSEric Dumazet  * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
6911da177e4SLinus Torvalds  */
6921da177e4SLinus Torvalds struct tcp_skb_cb {
6931da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
6941da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
695cd7d8498SEric Dumazet 	union {
696cd7d8498SEric Dumazet 		/* Note : tcp_tw_isn is used in input path only
697cd7d8498SEric Dumazet 		 *	  (isn chosen by tcp_timewait_state_process())
698cd7d8498SEric Dumazet 		 *
699cd7d8498SEric Dumazet 		 * 	  tcp_gso_segs is used in write queue only,
700cd7d8498SEric Dumazet 		 *	  cf tcp_skb_pcount()
701cd7d8498SEric Dumazet 		 */
702cd7d8498SEric Dumazet 		__u32		tcp_tw_isn;
703cd7d8498SEric Dumazet 		__u32		tcp_gso_segs;
704cd7d8498SEric Dumazet 	};
7054de075e0SEric Dumazet 	__u8		tcp_flags;	/* TCP header flags. (tcp[13])	*/
706f4f9f6e7SNeal Cardwell 
7071da177e4SLinus Torvalds 	__u8		sacked;		/* State flags for SACK/FACK.	*/
7081da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
7091da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
7101da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
7111da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
7129d186cacSAndrey Vagin #define TCPCB_REPAIRED		0x10	/* SKB repaired (no skb_mstamp)	*/
7131da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
7149d186cacSAndrey Vagin #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS| \
7159d186cacSAndrey Vagin 				TCPCB_REPAIRED)
7161da177e4SLinus Torvalds 
717f4f9f6e7SNeal Cardwell 	__u8		ip_dsfield;	/* IPv4 tos or IPv6 dsfield	*/
718f4f9f6e7SNeal Cardwell 	/* 1 byte hole */
7191da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
720971f10ecSEric Dumazet 	union {
721971f10ecSEric Dumazet 		struct inet_skb_parm	h4;
722971f10ecSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
723971f10ecSEric Dumazet 		struct inet6_skb_parm	h6;
724971f10ecSEric Dumazet #endif
725971f10ecSEric Dumazet 	} header;	/* For incoming frames		*/
7261da177e4SLinus Torvalds };
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
7291da177e4SLinus Torvalds 
730870c3151SEric Dumazet 
731815afe17SEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
732870c3151SEric Dumazet /* This is the variant of inet6_iif() that must be used by TCP,
733870c3151SEric Dumazet  * as TCP moves IP6CB into a different location in skb->cb[]
734870c3151SEric Dumazet  */
735870c3151SEric Dumazet static inline int tcp_v6_iif(const struct sk_buff *skb)
736870c3151SEric Dumazet {
737870c3151SEric Dumazet 	return TCP_SKB_CB(skb)->header.h6.iif;
738870c3151SEric Dumazet }
739815afe17SEric Dumazet #endif
740870c3151SEric Dumazet 
7411da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
7421da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
7431da177e4SLinus Torvalds  */
7441da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
7451da177e4SLinus Torvalds {
746cd7d8498SEric Dumazet 	return TCP_SKB_CB(skb)->tcp_gso_segs;
747cd7d8498SEric Dumazet }
748cd7d8498SEric Dumazet 
749cd7d8498SEric Dumazet static inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs)
750cd7d8498SEric Dumazet {
751cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs = segs;
752cd7d8498SEric Dumazet }
753cd7d8498SEric Dumazet 
754cd7d8498SEric Dumazet static inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs)
755cd7d8498SEric Dumazet {
756cd7d8498SEric Dumazet 	TCP_SKB_CB(skb)->tcp_gso_segs += segs;
7571da177e4SLinus Torvalds }
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds /* This is valid iff tcp_skb_pcount() > 1. */
7601da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
7611da177e4SLinus Torvalds {
7627967168cSHerbert Xu 	return skb_shinfo(skb)->gso_size;
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds 
765317a76f9SStephen Hemminger /* Events passed to congestion control interface */
766317a76f9SStephen Hemminger enum tcp_ca_event {
767317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
768317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
769317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
770317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
7719890092eSFlorian Westphal 	CA_EVENT_ECN_NO_CE,	/* ECT set, but not CE marked */
7729890092eSFlorian Westphal 	CA_EVENT_ECN_IS_CE,	/* received CE marked IP packet */
7739890092eSFlorian Westphal 	CA_EVENT_DELAYED_ACK,	/* Delayed ack is sent */
7749890092eSFlorian Westphal 	CA_EVENT_NON_DELAYED_ACK,
7757354c8c3SFlorian Westphal };
7767354c8c3SFlorian Westphal 
7779890092eSFlorian Westphal /* Information about inbound ACK, passed to cong_ops->in_ack_event() */
7787354c8c3SFlorian Westphal enum tcp_ca_ack_event_flags {
7799890092eSFlorian Westphal 	CA_ACK_SLOWPATH		= (1 << 0),	/* In slow path processing */
7809890092eSFlorian Westphal 	CA_ACK_WIN_UPDATE	= (1 << 1),	/* ACK updated window */
7819890092eSFlorian Westphal 	CA_ACK_ECE		= (1 << 2),	/* ECE bit is set on ack */
782317a76f9SStephen Hemminger };
783317a76f9SStephen Hemminger 
784317a76f9SStephen Hemminger /*
785317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
786317a76f9SStephen Hemminger  */
787317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
7883ff825b2SStephen Hemminger #define TCP_CA_MAX	128
7893ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
7903ff825b2SStephen Hemminger 
79130e502a3SDaniel Borkmann /* Algorithm can be set on socket without CAP_NET_ADMIN privileges */
792164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1
79330e502a3SDaniel Borkmann /* Requires ECN/ECT set on all packets */
79430e502a3SDaniel Borkmann #define TCP_CONG_NEEDS_ECN	0x2
795164891aaSStephen Hemminger 
796317a76f9SStephen Hemminger struct tcp_congestion_ops {
797317a76f9SStephen Hemminger 	struct list_head	list;
798164891aaSStephen Hemminger 	unsigned long flags;
799317a76f9SStephen Hemminger 
800317a76f9SStephen Hemminger 	/* initialize private data (optional) */
8016687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
802317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
8036687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
804317a76f9SStephen Hemminger 
805317a76f9SStephen Hemminger 	/* return slow start threshold (required) */
8066687e988SArnaldo Carvalho de Melo 	u32 (*ssthresh)(struct sock *sk);
807317a76f9SStephen Hemminger 	/* do new cwnd calculation (required) */
80824901551SEric Dumazet 	void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked);
809317a76f9SStephen Hemminger 	/* call before changing ca_state (optional) */
8106687e988SArnaldo Carvalho de Melo 	void (*set_state)(struct sock *sk, u8 new_state);
811317a76f9SStephen Hemminger 	/* call when cwnd event occurs (optional) */
8126687e988SArnaldo Carvalho de Melo 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
8137354c8c3SFlorian Westphal 	/* call when ack arrives (optional) */
8147354c8c3SFlorian Westphal 	void (*in_ack_event)(struct sock *sk, u32 flags);
815317a76f9SStephen Hemminger 	/* new value of cwnd after loss (optional) */
8166687e988SArnaldo Carvalho de Melo 	u32  (*undo_cwnd)(struct sock *sk);
817317a76f9SStephen Hemminger 	/* hook for packet ack accounting (optional) */
81830cfd0baSStephen Hemminger 	void (*pkts_acked)(struct sock *sk, u32 num_acked, s32 rtt_us);
81973c1f4a0SArnaldo Carvalho de Melo 	/* get info for inet_diag (optional) */
8206687e988SArnaldo Carvalho de Melo 	void (*get_info)(struct sock *sk, u32 ext, struct sk_buff *skb);
821317a76f9SStephen Hemminger 
822317a76f9SStephen Hemminger 	char 		name[TCP_CA_NAME_MAX];
823317a76f9SStephen Hemminger 	struct module 	*owner;
824317a76f9SStephen Hemminger };
825317a76f9SStephen Hemminger 
8265c9f3023SJoe Perches int tcp_register_congestion_control(struct tcp_congestion_ops *type);
8275c9f3023SJoe Perches void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
828317a76f9SStephen Hemminger 
82955d8694fSFlorian Westphal void tcp_assign_congestion_control(struct sock *sk);
8305c9f3023SJoe Perches void tcp_init_congestion_control(struct sock *sk);
8315c9f3023SJoe Perches void tcp_cleanup_congestion_control(struct sock *sk);
8325c9f3023SJoe Perches int tcp_set_default_congestion_control(const char *name);
8335c9f3023SJoe Perches void tcp_get_default_congestion_control(char *name);
8345c9f3023SJoe Perches void tcp_get_available_congestion_control(char *buf, size_t len);
8355c9f3023SJoe Perches void tcp_get_allowed_congestion_control(char *buf, size_t len);
8365c9f3023SJoe Perches int tcp_set_allowed_congestion_control(char *allowed);
8375c9f3023SJoe Perches int tcp_set_congestion_control(struct sock *sk, const char *name);
838a12a601eSLi RongQing void tcp_slow_start(struct tcp_sock *tp, u32 acked);
8395c9f3023SJoe Perches void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w);
840317a76f9SStephen Hemminger 
8415c9f3023SJoe Perches u32 tcp_reno_ssthresh(struct sock *sk);
84224901551SEric Dumazet void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked);
843a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
844317a76f9SStephen Hemminger 
84530e502a3SDaniel Borkmann static inline bool tcp_ca_needs_ecn(const struct sock *sk)
84630e502a3SDaniel Borkmann {
84730e502a3SDaniel Borkmann 	const struct inet_connection_sock *icsk = inet_csk(sk);
84830e502a3SDaniel Borkmann 
84930e502a3SDaniel Borkmann 	return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN;
85030e502a3SDaniel Borkmann }
85130e502a3SDaniel Borkmann 
8526687e988SArnaldo Carvalho de Melo static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
853317a76f9SStephen Hemminger {
8546687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
8556687e988SArnaldo Carvalho de Melo 
8566687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->set_state)
8576687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->set_state(sk, ca_state);
8586687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_state = ca_state;
859317a76f9SStephen Hemminger }
860317a76f9SStephen Hemminger 
8616687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
862317a76f9SStephen Hemminger {
8636687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
8646687e988SArnaldo Carvalho de Melo 
8656687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
8666687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
867317a76f9SStephen Hemminger }
868317a76f9SStephen Hemminger 
869e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK
870e60402d0SIlpo Järvinen  * handling. SACK is negotiated with the peer, and therefore it can vary
871e60402d0SIlpo Järvinen  * between different flows.
872e60402d0SIlpo Järvinen  *
873e60402d0SIlpo Järvinen  * tcp_is_sack - SACK enabled
874e60402d0SIlpo Järvinen  * tcp_is_reno - No SACK
875e60402d0SIlpo Järvinen  * tcp_is_fack - FACK enabled, implies SACK enabled
876e60402d0SIlpo Järvinen  */
877e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp)
878e60402d0SIlpo Järvinen {
879e60402d0SIlpo Järvinen 	return tp->rx_opt.sack_ok;
880e60402d0SIlpo Järvinen }
881e60402d0SIlpo Järvinen 
882a2a385d6SEric Dumazet static inline bool tcp_is_reno(const struct tcp_sock *tp)
883e60402d0SIlpo Järvinen {
884e60402d0SIlpo Järvinen 	return !tcp_is_sack(tp);
885e60402d0SIlpo Järvinen }
886e60402d0SIlpo Järvinen 
887a2a385d6SEric Dumazet static inline bool tcp_is_fack(const struct tcp_sock *tp)
888e60402d0SIlpo Järvinen {
889ab56222aSVijay Subramanian 	return tp->rx_opt.sack_ok & TCP_FACK_ENABLED;
890e60402d0SIlpo Järvinen }
891e60402d0SIlpo Järvinen 
892e60402d0SIlpo Järvinen static inline void tcp_enable_fack(struct tcp_sock *tp)
893e60402d0SIlpo Järvinen {
894ab56222aSVijay Subramanian 	tp->rx_opt.sack_ok |= TCP_FACK_ENABLED;
895e60402d0SIlpo Järvinen }
896e60402d0SIlpo Järvinen 
897eed530b6SYuchung Cheng /* TCP early-retransmit (ER) is similar to but more conservative than
898eed530b6SYuchung Cheng  * the thin-dupack feature.  Enable ER only if thin-dupack is disabled.
899eed530b6SYuchung Cheng  */
900eed530b6SYuchung Cheng static inline void tcp_enable_early_retrans(struct tcp_sock *tp)
901eed530b6SYuchung Cheng {
902eed530b6SYuchung Cheng 	tp->do_early_retrans = sysctl_tcp_early_retrans &&
9036ba8a3b1SNandita Dukkipati 		sysctl_tcp_early_retrans < 4 && !sysctl_tcp_thin_dupack &&
9046ba8a3b1SNandita Dukkipati 		sysctl_tcp_reordering == 3;
905eed530b6SYuchung Cheng }
906eed530b6SYuchung Cheng 
907eed530b6SYuchung Cheng static inline void tcp_disable_early_retrans(struct tcp_sock *tp)
908eed530b6SYuchung Cheng {
909eed530b6SYuchung Cheng 	tp->do_early_retrans = 0;
910eed530b6SYuchung Cheng }
911eed530b6SYuchung Cheng 
91283ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
91383ae4088SIlpo Järvinen {
91483ae4088SIlpo Järvinen 	return tp->sacked_out + tp->lost_out;
91583ae4088SIlpo Järvinen }
91683ae4088SIlpo Järvinen 
9171da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
9181da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
9191da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
9201da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
9211da177e4SLinus Torvalds  *
9221da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
9231da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
9241da177e4SLinus Torvalds  *
9251da177e4SLinus Torvalds  * Read this equation as:
9261da177e4SLinus Torvalds  *
9271da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
9281da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
9291da177e4SLinus Torvalds  *	"Packets fast retransmitted"
9301da177e4SLinus Torvalds  */
93140efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
9321da177e4SLinus Torvalds {
93383ae4088SIlpo Järvinen 	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
9341da177e4SLinus Torvalds }
9351da177e4SLinus Torvalds 
9360b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH	0x7fffffff
9370b6a05c1SIlpo Järvinen 
9380b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
9390b6a05c1SIlpo Järvinen {
9400b6a05c1SIlpo Järvinen 	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
9410b6a05c1SIlpo Järvinen }
9420b6a05c1SIlpo Järvinen 
943684bad11SYuchung Cheng static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
944684bad11SYuchung Cheng {
945684bad11SYuchung Cheng 	return (TCPF_CA_CWR | TCPF_CA_Recovery) &
946684bad11SYuchung Cheng 	       (1 << inet_csk(sk)->icsk_ca_state);
947684bad11SYuchung Cheng }
948684bad11SYuchung Cheng 
9491da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
950684bad11SYuchung Cheng  * The exception is cwnd reduction phase, when cwnd is decreasing towards
9511da177e4SLinus Torvalds  * ssthresh.
9521da177e4SLinus Torvalds  */
9536687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
9541da177e4SLinus Torvalds {
9556687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
956cf533ea5SEric Dumazet 
957684bad11SYuchung Cheng 	if (tcp_in_cwnd_reduction(sk))
9581da177e4SLinus Torvalds 		return tp->snd_ssthresh;
9591da177e4SLinus Torvalds 	else
9601da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
9611da177e4SLinus Torvalds 			   ((tp->snd_cwnd >> 1) +
9621da177e4SLinus Torvalds 			    (tp->snd_cwnd >> 2)));
9631da177e4SLinus Torvalds }
9641da177e4SLinus Torvalds 
965b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */
966b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
9671da177e4SLinus Torvalds 
9685ee2c941SChristoph Paasch void tcp_enter_cwr(struct sock *sk);
9695c9f3023SJoe Perches __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
9701da177e4SLinus Torvalds 
9716b5a5c0dSNeal Cardwell /* The maximum number of MSS of available cwnd for which TSO defers
9726b5a5c0dSNeal Cardwell  * sending if not using sysctl_tcp_tso_win_divisor.
9736b5a5c0dSNeal Cardwell  */
9746b5a5c0dSNeal Cardwell static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
9756b5a5c0dSNeal Cardwell {
9766b5a5c0dSNeal Cardwell 	return 3;
9776b5a5c0dSNeal Cardwell }
9786b5a5c0dSNeal Cardwell 
9791da177e4SLinus Torvalds /* Slow start with delack produces 3 packets of burst, so that
980dd9e0ddaSJohn Heffner  * it is safe "de facto".  This will be the default - same as
981dd9e0ddaSJohn Heffner  * the default reordering threshold - but if reordering increases,
982dd9e0ddaSJohn Heffner  * we must be able to allow cwnd to burst at least this much in order
983dd9e0ddaSJohn Heffner  * to not pull it back when holes are filled.
9841da177e4SLinus Torvalds  */
9851da177e4SLinus Torvalds static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
9861da177e4SLinus Torvalds {
987dd9e0ddaSJohn Heffner 	return tp->reordering;
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
99090840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */
99190840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
99290840defSIlpo Järvinen {
99390840defSIlpo Järvinen 	return tp->snd_una + tp->snd_wnd;
99490840defSIlpo Järvinen }
995e114a710SEric Dumazet 
996e114a710SEric Dumazet /* We follow the spirit of RFC2861 to validate cwnd but implement a more
997e114a710SEric Dumazet  * flexible approach. The RFC suggests cwnd should not be raised unless
998ca8a2263SNeal Cardwell  * it was fully used previously. And that's exactly what we do in
999ca8a2263SNeal Cardwell  * congestion avoidance mode. But in slow start we allow cwnd to grow
1000ca8a2263SNeal Cardwell  * as long as the application has used half the cwnd.
1001e114a710SEric Dumazet  * Example :
1002e114a710SEric Dumazet  *    cwnd is 10 (IW10), but application sends 9 frames.
1003e114a710SEric Dumazet  *    We allow cwnd to reach 18 when all frames are ACKed.
1004e114a710SEric Dumazet  * This check is safe because it's as aggressive as slow start which already
1005e114a710SEric Dumazet  * risks 100% overshoot. The advantage is that we discourage application to
1006e114a710SEric Dumazet  * either send more filler packets or data to artificially blow up the cwnd
1007e114a710SEric Dumazet  * usage, and allow application-limited process to probe bw more aggressively.
1008e114a710SEric Dumazet  */
100924901551SEric Dumazet static inline bool tcp_is_cwnd_limited(const struct sock *sk)
1010e114a710SEric Dumazet {
1011e114a710SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1012e114a710SEric Dumazet 
1013ca8a2263SNeal Cardwell 	/* If in slow start, ensure cwnd grows to twice what was ACKed. */
1014ca8a2263SNeal Cardwell 	if (tp->snd_cwnd <= tp->snd_ssthresh)
1015ca8a2263SNeal Cardwell 		return tp->snd_cwnd < 2 * tp->max_packets_out;
1016ca8a2263SNeal Cardwell 
1017ca8a2263SNeal Cardwell 	return tp->is_cwnd_limited;
1018e114a710SEric Dumazet }
1019f4805edeSStephen Hemminger 
10209e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk)
10211da177e4SLinus Torvalds {
1022cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1023463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
10249e412ba7SIlpo Järvinen 
1025463c84b9SArnaldo Carvalho de Melo 	if (!tp->packets_out && !icsk->icsk_pending)
10263f421baaSArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
10273f421baaSArnaldo Carvalho de Melo 					  icsk->icsk_rto, TCP_RTO_MAX);
10281da177e4SLinus Torvalds }
10291da177e4SLinus Torvalds 
1030ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
10311da177e4SLinus Torvalds {
10321da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
1035ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
10381da177e4SLinus Torvalds }
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds /*
10411da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
10421da177e4SLinus Torvalds  */
1043ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr,
1044ba7808eaSFrederik Deweerdt 				   __be32 daddr, __wsum base)
10451da177e4SLinus Torvalds {
10461da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
1049b51655b9SAl Viro static inline __sum16 __tcp_checksum_complete(struct sk_buff *skb)
10501da177e4SLinus Torvalds {
1051fb286bb2SHerbert Xu 	return __skb_checksum_complete(skb);
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
1054a2a385d6SEric Dumazet static inline bool tcp_checksum_complete(struct sk_buff *skb)
10551da177e4SLinus Torvalds {
105660476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
10571da177e4SLinus Torvalds 		__tcp_checksum_complete(skb);
10581da177e4SLinus Torvalds }
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds /* Prequeue for VJ style copy to user, combined with checksumming. */
10611da177e4SLinus Torvalds 
106240efc6faSStephen Hemminger static inline void tcp_prequeue_init(struct tcp_sock *tp)
10631da177e4SLinus Torvalds {
10641da177e4SLinus Torvalds 	tp->ucopy.task = NULL;
10651da177e4SLinus Torvalds 	tp->ucopy.len = 0;
10661da177e4SLinus Torvalds 	tp->ucopy.memory = 0;
10671da177e4SLinus Torvalds 	skb_queue_head_init(&tp->ucopy.prequeue);
10681da177e4SLinus Torvalds }
10691da177e4SLinus Torvalds 
10705c9f3023SJoe Perches bool tcp_prequeue(struct sock *sk, struct sk_buff *skb);
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds #undef STATE_TRACE
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds #ifdef STATE_TRACE
10751da177e4SLinus Torvalds static const char *statename[]={
10761da177e4SLinus Torvalds 	"Unused","Established","Syn Sent","Syn Recv",
10771da177e4SLinus Torvalds 	"Fin Wait 1","Fin Wait 2","Time Wait", "Close",
10781da177e4SLinus Torvalds 	"Close Wait","Last ACK","Listen","Closing"
10791da177e4SLinus Torvalds };
10801da177e4SLinus Torvalds #endif
10815c9f3023SJoe Perches void tcp_set_state(struct sock *sk, int state);
10821da177e4SLinus Torvalds 
10835c9f3023SJoe Perches void tcp_done(struct sock *sk);
10841da177e4SLinus Torvalds 
108540efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	rx_opt->dsack = 0;
10881da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
10891da177e4SLinus Torvalds }
10901da177e4SLinus Torvalds 
10915c9f3023SJoe Perches u32 tcp_default_init_rwnd(u32 mss);
109285f16525SYuchung Cheng 
10931da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
10945c9f3023SJoe Perches void tcp_select_initial_window(int __space, __u32 mss, __u32 *rcv_wnd,
10955c9f3023SJoe Perches 			       __u32 *window_clamp, int wscale_ok,
10965c9f3023SJoe Perches 			       __u8 *rcv_wscale, __u32 init_rcv_wnd);
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds static inline int tcp_win_from_space(int space)
10991da177e4SLinus Torvalds {
11001da177e4SLinus Torvalds 	return sysctl_tcp_adv_win_scale<=0 ?
11011da177e4SLinus Torvalds 		(space>>(-sysctl_tcp_adv_win_scale)) :
11021da177e4SLinus Torvalds 		space - (space>>sysctl_tcp_adv_win_scale);
11031da177e4SLinus Torvalds }
11041da177e4SLinus Torvalds 
11051da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
11061da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
11071da177e4SLinus Torvalds {
11081da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf -
11091da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
11131da177e4SLinus Torvalds {
11141da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf);
11151da177e4SLinus Torvalds }
11161da177e4SLinus Torvalds 
111740efc6faSStephen Hemminger static inline void tcp_openreq_init(struct request_sock *req,
11181da177e4SLinus Torvalds 				    struct tcp_options_received *rx_opt,
1119e0f802fbSOctavian Purdila 				    struct sk_buff *skb, struct sock *sk)
11201da177e4SLinus Torvalds {
11212e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq = inet_rsk(req);
11222e6599cbSArnaldo Carvalho de Melo 
11231da177e4SLinus Torvalds 	req->rcv_wnd = 0;		/* So that tcp_send_synack() knows! */
11244dfc2817SFlorian Westphal 	req->cookie_ts = 0;
11252e6599cbSArnaldo Carvalho de Melo 	tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
112610467163SJerry Chu 	tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
112786c6a2c7SNeal Cardwell 	tcp_rsk(req)->snt_synack = tcp_time_stamp;
11281da177e4SLinus Torvalds 	req->mss = rx_opt->mss_clamp;
11291da177e4SLinus Torvalds 	req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
11302e6599cbSArnaldo Carvalho de Melo 	ireq->tstamp_ok = rx_opt->tstamp_ok;
11312e6599cbSArnaldo Carvalho de Melo 	ireq->sack_ok = rx_opt->sack_ok;
11322e6599cbSArnaldo Carvalho de Melo 	ireq->snd_wscale = rx_opt->snd_wscale;
11332e6599cbSArnaldo Carvalho de Melo 	ireq->wscale_ok = rx_opt->wscale_ok;
11342e6599cbSArnaldo Carvalho de Melo 	ireq->acked = 0;
11352e6599cbSArnaldo Carvalho de Melo 	ireq->ecn_ok = 0;
1136634fb979SEric Dumazet 	ireq->ir_rmt_port = tcp_hdr(skb)->source;
1137b44084c2SEric Dumazet 	ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
1138e0f802fbSOctavian Purdila 	ireq->ir_mark = inet_request_mark(sk, skb);
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
1141843f4a55SYuchung Cheng extern void tcp_openreq_init_rwin(struct request_sock *req,
1142843f4a55SYuchung Cheng 				  struct sock *sk, struct dst_entry *dst);
1143843f4a55SYuchung Cheng 
11445c9f3023SJoe Perches void tcp_enter_memory_pressure(struct sock *sk);
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
11471da177e4SLinus Torvalds {
11481da177e4SLinus Torvalds 	return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
11521da177e4SLinus Torvalds {
11531da177e4SLinus Torvalds 	return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
11541da177e4SLinus Torvalds }
11551da177e4SLinus Torvalds 
1156df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp)
1157df19a626SEric Dumazet {
1158df19a626SEric Dumazet 	return tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
1159df19a626SEric Dumazet }
1160df19a626SEric Dumazet 
11616c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
11626c37e5deSFlavio Leitner {
11636c37e5deSFlavio Leitner 	const struct inet_connection_sock *icsk = &tp->inet_conn;
11646c37e5deSFlavio Leitner 
11656c37e5deSFlavio Leitner 	return min_t(u32, tcp_time_stamp - icsk->icsk_ack.lrcvtime,
11666c37e5deSFlavio Leitner 			  tcp_time_stamp - tp->rcv_tstamp);
11676c37e5deSFlavio Leitner }
11686c37e5deSFlavio Leitner 
1169463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
11701da177e4SLinus Torvalds {
1171463c84b9SArnaldo Carvalho de Melo 	int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
1172463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
11731da177e4SLinus Torvalds 
1174463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1175463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	return fin_timeout;
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
1180a2a385d6SEric Dumazet static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
1181c887e6d2SIlpo Järvinen 				  int paws_win)
11821da177e4SLinus Torvalds {
1183c887e6d2SIlpo Järvinen 	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1184a2a385d6SEric Dumazet 		return true;
1185c887e6d2SIlpo Järvinen 	if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS))
1186a2a385d6SEric Dumazet 		return true;
1187bc2ce894SEric Dumazet 	/*
1188bc2ce894SEric Dumazet 	 * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
1189bc2ce894SEric Dumazet 	 * then following tcp messages have valid values. Ignore 0 value,
1190bc2ce894SEric Dumazet 	 * or else 'negative' tsval might forbid us to accept their packets.
1191bc2ce894SEric Dumazet 	 */
1192bc2ce894SEric Dumazet 	if (!rx_opt->ts_recent)
1193a2a385d6SEric Dumazet 		return true;
1194a2a385d6SEric Dumazet 	return false;
1195c887e6d2SIlpo Järvinen }
1196c887e6d2SIlpo Järvinen 
1197a2a385d6SEric Dumazet static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
1198c887e6d2SIlpo Järvinen 				   int rst)
1199c887e6d2SIlpo Järvinen {
1200c887e6d2SIlpo Järvinen 	if (tcp_paws_check(rx_opt, 0))
1201a2a385d6SEric Dumazet 		return false;
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
12041da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
12051da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
12061da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
12071da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
12081da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
12091da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
12101da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
12111da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
12141da177e4SLinus Torvalds 	 */
12159d729f72SJames Morris 	if (rst && get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
1216a2a385d6SEric Dumazet 		return false;
1217a2a385d6SEric Dumazet 	return true;
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
1220a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net)
12211da177e4SLinus Torvalds {
12221da177e4SLinus Torvalds 	/* See RFC 2012 */
1223cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOALGORITHM, 1);
1224cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
1225cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
1226cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_MAXCONN, -1);
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds 
12296a438bbeSStephen Hemminger /* from STCP */
1230ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
12310800f170SDavid S. Miller {
12326a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
1233ef9da47cSIlpo Järvinen }
1234ef9da47cSIlpo Järvinen 
1235ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
1236ef9da47cSIlpo Järvinen {
1237ef9da47cSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
12386a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
1239b7689205SIlpo Järvinen }
1240b7689205SIlpo Järvinen 
1241cfb6eeb4SYOSHIFUJI Hideaki /* MD5 Signature */
1242cfb6eeb4SYOSHIFUJI Hideaki struct crypto_hash;
1243cfb6eeb4SYOSHIFUJI Hideaki 
1244a915da9bSEric Dumazet union tcp_md5_addr {
1245a915da9bSEric Dumazet 	struct in_addr  a4;
1246a915da9bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1247a915da9bSEric Dumazet 	struct in6_addr	a6;
1248a915da9bSEric Dumazet #endif
1249a915da9bSEric Dumazet };
1250a915da9bSEric Dumazet 
1251cfb6eeb4SYOSHIFUJI Hideaki /* - key database */
1252cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key {
1253a915da9bSEric Dumazet 	struct hlist_node	node;
1254cfb6eeb4SYOSHIFUJI Hideaki 	u8			keylen;
1255a915da9bSEric Dumazet 	u8			family; /* AF_INET or AF_INET6 */
1256a915da9bSEric Dumazet 	union tcp_md5_addr	addr;
1257a915da9bSEric Dumazet 	u8			key[TCP_MD5SIG_MAXKEYLEN];
1258a915da9bSEric Dumazet 	struct rcu_head		rcu;
1259cfb6eeb4SYOSHIFUJI Hideaki };
1260cfb6eeb4SYOSHIFUJI Hideaki 
1261cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */
1262cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info {
1263a915da9bSEric Dumazet 	struct hlist_head	head;
1264a8afca03SEric Dumazet 	struct rcu_head		rcu;
1265cfb6eeb4SYOSHIFUJI Hideaki };
1266cfb6eeb4SYOSHIFUJI Hideaki 
1267cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */
1268cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr {
1269cfb6eeb4SYOSHIFUJI Hideaki 	__be32		saddr;
1270cfb6eeb4SYOSHIFUJI Hideaki 	__be32		daddr;
1271cfb6eeb4SYOSHIFUJI Hideaki 	__u8		pad;
1272cfb6eeb4SYOSHIFUJI Hideaki 	__u8		protocol;
1273cfb6eeb4SYOSHIFUJI Hideaki 	__be16		len;
1274cfb6eeb4SYOSHIFUJI Hideaki };
1275cfb6eeb4SYOSHIFUJI Hideaki 
1276cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr {
1277cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr	saddr;
1278cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr daddr;
1279cfb6eeb4SYOSHIFUJI Hideaki 	__be32		len;
1280cfb6eeb4SYOSHIFUJI Hideaki 	__be32		protocol;	/* including padding */
1281cfb6eeb4SYOSHIFUJI Hideaki };
1282cfb6eeb4SYOSHIFUJI Hideaki 
1283cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block {
1284cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr ip4;
1285dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1286cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_pseudohdr ip6;
1287cfb6eeb4SYOSHIFUJI Hideaki #endif
1288cfb6eeb4SYOSHIFUJI Hideaki };
1289cfb6eeb4SYOSHIFUJI Hideaki 
1290cfb6eeb4SYOSHIFUJI Hideaki /* - pool: digest algorithm, hash description and scratch buffer */
1291cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool {
1292cfb6eeb4SYOSHIFUJI Hideaki 	struct hash_desc	md5_desc;
1293cfb6eeb4SYOSHIFUJI Hideaki 	union tcp_md5sum_block	md5_blk;
1294cfb6eeb4SYOSHIFUJI Hideaki };
1295cfb6eeb4SYOSHIFUJI Hideaki 
1296cfb6eeb4SYOSHIFUJI Hideaki /* - functions */
12975c9f3023SJoe Perches int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
12985c9f3023SJoe Perches 			const struct sock *sk, const struct request_sock *req,
1299318cf7aaSEric Dumazet 			const struct sk_buff *skb);
13005c9f3023SJoe Perches int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
13015c9f3023SJoe Perches 		   int family, const u8 *newkey, u8 newkeylen, gfp_t gfp);
13025c9f3023SJoe Perches int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
1303a915da9bSEric Dumazet 		   int family);
13045c9f3023SJoe Perches struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
1305cfb6eeb4SYOSHIFUJI Hideaki 					 struct sock *addr_sk);
1306cfb6eeb4SYOSHIFUJI Hideaki 
13079501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
13085c9f3023SJoe Perches struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
13095c9f3023SJoe Perches 					 const union tcp_md5_addr *addr,
13105c9f3023SJoe Perches 					 int family);
1311a915da9bSEric Dumazet #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_key)
13129501f972SYOSHIFUJI Hideaki #else
1313a915da9bSEric Dumazet static inline struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
1314a915da9bSEric Dumazet 					 const union tcp_md5_addr *addr,
1315a915da9bSEric Dumazet 					 int family)
1316a915da9bSEric Dumazet {
1317a915da9bSEric Dumazet 	return NULL;
1318a915da9bSEric Dumazet }
13199501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	NULL
13209501f972SYOSHIFUJI Hideaki #endif
13219501f972SYOSHIFUJI Hideaki 
13225c9f3023SJoe Perches bool tcp_alloc_md5sig_pool(void);
1323cfb6eeb4SYOSHIFUJI Hideaki 
13245c9f3023SJoe Perches struct tcp_md5sig_pool *tcp_get_md5sig_pool(void);
132571cea17eSEric Dumazet static inline void tcp_put_md5sig_pool(void)
132671cea17eSEric Dumazet {
132771cea17eSEric Dumazet 	local_bh_enable();
132871cea17eSEric Dumazet }
132935790c04SEric Dumazet 
13305c9f3023SJoe Perches int tcp_md5_hash_header(struct tcp_md5sig_pool *, const struct tcphdr *);
13315c9f3023SJoe Perches int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
133295c96174SEric Dumazet 			  unsigned int header_len);
13335c9f3023SJoe Perches int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
1334cf533ea5SEric Dumazet 		     const struct tcp_md5sig_key *key);
1335cfb6eeb4SYOSHIFUJI Hideaki 
133610467163SJerry Chu /* From tcp_fastopen.c */
13375c9f3023SJoe Perches void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
13385c9f3023SJoe Perches 			    struct tcp_fastopen_cookie *cookie, int *syn_loss,
13395c9f3023SJoe Perches 			    unsigned long *last_syn_loss);
13405c9f3023SJoe Perches void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
13415c9f3023SJoe Perches 			    struct tcp_fastopen_cookie *cookie, bool syn_lost);
1342783237e8SYuchung Cheng struct tcp_fastopen_request {
1343783237e8SYuchung Cheng 	/* Fast Open cookie. Size 0 means a cookie request */
1344783237e8SYuchung Cheng 	struct tcp_fastopen_cookie	cookie;
1345783237e8SYuchung Cheng 	struct msghdr			*data;  /* data in MSG_FASTOPEN */
1346f5ddcbbbSEric Dumazet 	size_t				size;
1347f5ddcbbbSEric Dumazet 	int				copied;	/* queued in tcp_connect() */
1348783237e8SYuchung Cheng };
1349783237e8SYuchung Cheng void tcp_free_fastopen_req(struct tcp_sock *tp);
1350783237e8SYuchung Cheng 
135110467163SJerry Chu extern struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
135210467163SJerry Chu int tcp_fastopen_reset_cipher(void *key, unsigned int len);
1353843f4a55SYuchung Cheng bool tcp_try_fastopen(struct sock *sk, struct sk_buff *skb,
13545b7ed089SYuchung Cheng 		      struct request_sock *req,
1355843f4a55SYuchung Cheng 		      struct tcp_fastopen_cookie *foc,
1356843f4a55SYuchung Cheng 		      struct dst_entry *dst);
1357222e83d2SHannes Frederic Sowa void tcp_fastopen_init_key_once(bool publish);
135810467163SJerry Chu #define TCP_FASTOPEN_KEY_LENGTH 16
135910467163SJerry Chu 
136010467163SJerry Chu /* Fastopen key context */
136110467163SJerry Chu struct tcp_fastopen_context {
13627ae8639cSEric Dumazet 	struct crypto_cipher	*tfm;
136310467163SJerry Chu 	__u8			key[TCP_FASTOPEN_KEY_LENGTH];
136410467163SJerry Chu 	struct rcu_head		rcu;
136510467163SJerry Chu };
136610467163SJerry Chu 
1367fe067e8aSDavid S. Miller /* write queue abstraction */
1368fe067e8aSDavid S. Miller static inline void tcp_write_queue_purge(struct sock *sk)
1369fe067e8aSDavid S. Miller {
1370fe067e8aSDavid S. Miller 	struct sk_buff *skb;
1371fe067e8aSDavid S. Miller 
1372fe067e8aSDavid S. Miller 	while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL)
13733ab224beSHideo Aoki 		sk_wmem_free_skb(sk, skb);
13743ab224beSHideo Aoki 	sk_mem_reclaim(sk);
13758818a9d8SIlpo Järvinen 	tcp_clear_all_retrans_hints(tcp_sk(sk));
1376fe067e8aSDavid S. Miller }
1377fe067e8aSDavid S. Miller 
1378cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
1379fe067e8aSDavid S. Miller {
1380cd07a8eaSDavid S. Miller 	return skb_peek(&sk->sk_write_queue);
1381fe067e8aSDavid S. Miller }
1382fe067e8aSDavid S. Miller 
1383cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
1384fe067e8aSDavid S. Miller {
1385cd07a8eaSDavid S. Miller 	return skb_peek_tail(&sk->sk_write_queue);
1386fe067e8aSDavid S. Miller }
1387fe067e8aSDavid S. Miller 
1388cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_next(const struct sock *sk,
1389cf533ea5SEric Dumazet 						   const struct sk_buff *skb)
1390fe067e8aSDavid S. Miller {
1391cd07a8eaSDavid S. Miller 	return skb_queue_next(&sk->sk_write_queue, skb);
1392fe067e8aSDavid S. Miller }
1393fe067e8aSDavid S. Miller 
1394cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_prev(const struct sock *sk,
1395cf533ea5SEric Dumazet 						   const struct sk_buff *skb)
1396832d11c5SIlpo Järvinen {
1397832d11c5SIlpo Järvinen 	return skb_queue_prev(&sk->sk_write_queue, skb);
1398832d11c5SIlpo Järvinen }
1399832d11c5SIlpo Järvinen 
1400fe067e8aSDavid S. Miller #define tcp_for_write_queue(skb, sk)					\
1401cd07a8eaSDavid S. Miller 	skb_queue_walk(&(sk)->sk_write_queue, skb)
1402fe067e8aSDavid S. Miller 
1403fe067e8aSDavid S. Miller #define tcp_for_write_queue_from(skb, sk)				\
1404cd07a8eaSDavid S. Miller 	skb_queue_walk_from(&(sk)->sk_write_queue, skb)
1405fe067e8aSDavid S. Miller 
1406234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
1407cd07a8eaSDavid S. Miller 	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1408234b6860SIlpo Järvinen 
1409cf533ea5SEric Dumazet static inline struct sk_buff *tcp_send_head(const struct sock *sk)
1410fe067e8aSDavid S. Miller {
1411fe067e8aSDavid S. Miller 	return sk->sk_send_head;
1412fe067e8aSDavid S. Miller }
1413fe067e8aSDavid S. Miller 
1414cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk,
1415cd07a8eaSDavid S. Miller 				   const struct sk_buff *skb)
1416cd07a8eaSDavid S. Miller {
1417cd07a8eaSDavid S. Miller 	return skb_queue_is_last(&sk->sk_write_queue, skb);
1418cd07a8eaSDavid S. Miller }
1419cd07a8eaSDavid S. Miller 
1420cf533ea5SEric Dumazet static inline void tcp_advance_send_head(struct sock *sk, const struct sk_buff *skb)
1421fe067e8aSDavid S. Miller {
1422cd07a8eaSDavid S. Miller 	if (tcp_skb_is_last(sk, skb))
1423fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1424cd07a8eaSDavid S. Miller 	else
1425cd07a8eaSDavid S. Miller 		sk->sk_send_head = tcp_write_queue_next(sk, skb);
1426fe067e8aSDavid S. Miller }
1427fe067e8aSDavid S. Miller 
1428fe067e8aSDavid S. Miller static inline void tcp_check_send_head(struct sock *sk, struct sk_buff *skb_unlinked)
1429fe067e8aSDavid S. Miller {
1430fe067e8aSDavid S. Miller 	if (sk->sk_send_head == skb_unlinked)
1431fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1432fe067e8aSDavid S. Miller }
1433fe067e8aSDavid S. Miller 
1434fe067e8aSDavid S. Miller static inline void tcp_init_send_head(struct sock *sk)
1435fe067e8aSDavid S. Miller {
1436fe067e8aSDavid S. Miller 	sk->sk_send_head = NULL;
1437fe067e8aSDavid S. Miller }
1438fe067e8aSDavid S. Miller 
1439fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1440fe067e8aSDavid S. Miller {
1441fe067e8aSDavid S. Miller 	__skb_queue_tail(&sk->sk_write_queue, skb);
1442fe067e8aSDavid S. Miller }
1443fe067e8aSDavid S. Miller 
1444fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1445fe067e8aSDavid S. Miller {
1446fe067e8aSDavid S. Miller 	__tcp_add_write_queue_tail(sk, skb);
1447fe067e8aSDavid S. Miller 
1448fe067e8aSDavid S. Miller 	/* Queue it, remembering where we must start sending. */
14496859d494SIlpo Järvinen 	if (sk->sk_send_head == NULL) {
1450fe067e8aSDavid S. Miller 		sk->sk_send_head = skb;
14516859d494SIlpo Järvinen 
14526859d494SIlpo Järvinen 		if (tcp_sk(sk)->highest_sack == NULL)
14536859d494SIlpo Järvinen 			tcp_sk(sk)->highest_sack = skb;
14546859d494SIlpo Järvinen 	}
1455fe067e8aSDavid S. Miller }
1456fe067e8aSDavid S. Miller 
1457fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_head(struct sock *sk, struct sk_buff *skb)
1458fe067e8aSDavid S. Miller {
1459fe067e8aSDavid S. Miller 	__skb_queue_head(&sk->sk_write_queue, skb);
1460fe067e8aSDavid S. Miller }
1461fe067e8aSDavid S. Miller 
1462fe067e8aSDavid S. Miller /* Insert buff after skb on the write queue of sk.  */
1463fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_after(struct sk_buff *skb,
1464fe067e8aSDavid S. Miller 						struct sk_buff *buff,
1465fe067e8aSDavid S. Miller 						struct sock *sk)
1466fe067e8aSDavid S. Miller {
14677de6c033SGerrit Renker 	__skb_queue_after(&sk->sk_write_queue, skb, buff);
1468fe067e8aSDavid S. Miller }
1469fe067e8aSDavid S. Miller 
147043f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk.  */
1471fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new,
1472fe067e8aSDavid S. Miller 						  struct sk_buff *skb,
1473fe067e8aSDavid S. Miller 						  struct sock *sk)
1474fe067e8aSDavid S. Miller {
147543f59c89SDavid S. Miller 	__skb_queue_before(&sk->sk_write_queue, skb, new);
14766e421410SIlpo Järvinen 
14776e421410SIlpo Järvinen 	if (sk->sk_send_head == skb)
14786e421410SIlpo Järvinen 		sk->sk_send_head = new;
1479fe067e8aSDavid S. Miller }
1480fe067e8aSDavid S. Miller 
1481fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
1482fe067e8aSDavid S. Miller {
1483fe067e8aSDavid S. Miller 	__skb_unlink(skb, &sk->sk_write_queue);
1484fe067e8aSDavid S. Miller }
1485fe067e8aSDavid S. Miller 
1486a2a385d6SEric Dumazet static inline bool tcp_write_queue_empty(struct sock *sk)
1487fe067e8aSDavid S. Miller {
1488fe067e8aSDavid S. Miller 	return skb_queue_empty(&sk->sk_write_queue);
1489fe067e8aSDavid S. Miller }
1490fe067e8aSDavid S. Miller 
149112d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk)
149212d50c46SKrishna Kumar {
149312d50c46SKrishna Kumar 	if (tcp_send_head(sk)) {
149412d50c46SKrishna Kumar 		struct tcp_sock *tp = tcp_sk(sk);
149512d50c46SKrishna Kumar 
149612d50c46SKrishna Kumar 		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
149712d50c46SKrishna Kumar 	}
149812d50c46SKrishna Kumar }
149912d50c46SKrishna Kumar 
1500ecb97192SNeal Cardwell /* Start sequence of the skb just after the highest skb with SACKed
1501ecb97192SNeal Cardwell  * bit, valid only if sacked_out > 0 or when the caller has ensured
1502ecb97192SNeal Cardwell  * validity by itself.
1503a47e5a98SIlpo Järvinen  */
1504a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
1505a47e5a98SIlpo Järvinen {
1506a47e5a98SIlpo Järvinen 	if (!tp->sacked_out)
1507a47e5a98SIlpo Järvinen 		return tp->snd_una;
15086859d494SIlpo Järvinen 
15096859d494SIlpo Järvinen 	if (tp->highest_sack == NULL)
15106859d494SIlpo Järvinen 		return tp->snd_nxt;
15116859d494SIlpo Järvinen 
1512a47e5a98SIlpo Järvinen 	return TCP_SKB_CB(tp->highest_sack)->seq;
1513a47e5a98SIlpo Järvinen }
1514a47e5a98SIlpo Järvinen 
15156859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
15166859d494SIlpo Järvinen {
15176859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_skb_is_last(sk, skb) ? NULL :
15186859d494SIlpo Järvinen 						tcp_write_queue_next(sk, skb);
15196859d494SIlpo Järvinen }
15206859d494SIlpo Järvinen 
15216859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
15226859d494SIlpo Järvinen {
15236859d494SIlpo Järvinen 	return tcp_sk(sk)->highest_sack;
15246859d494SIlpo Järvinen }
15256859d494SIlpo Järvinen 
15266859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk)
15276859d494SIlpo Järvinen {
15286859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_write_queue_head(sk);
15296859d494SIlpo Järvinen }
15306859d494SIlpo Järvinen 
15316859d494SIlpo Järvinen /* Called when old skb is about to be deleted (to be combined with new skb) */
15326859d494SIlpo Järvinen static inline void tcp_highest_sack_combine(struct sock *sk,
15336859d494SIlpo Järvinen 					    struct sk_buff *old,
15346859d494SIlpo Järvinen 					    struct sk_buff *new)
15356859d494SIlpo Järvinen {
15366859d494SIlpo Järvinen 	if (tcp_sk(sk)->sacked_out && (old == tcp_sk(sk)->highest_sack))
15376859d494SIlpo Järvinen 		tcp_sk(sk)->highest_sack = new;
15386859d494SIlpo Järvinen }
15396859d494SIlpo Järvinen 
15405aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from
15415aa4b32fSAndreas Petlund  * increased latency). Used to trigger latency-reducing mechanisms.
15425aa4b32fSAndreas Petlund  */
1543a2a385d6SEric Dumazet static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
15445aa4b32fSAndreas Petlund {
15455aa4b32fSAndreas Petlund 	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
15465aa4b32fSAndreas Petlund }
15475aa4b32fSAndreas Petlund 
15481da177e4SLinus Torvalds /* /proc */
15491da177e4SLinus Torvalds enum tcp_seq_states {
15501da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
15511da177e4SLinus Torvalds 	TCP_SEQ_STATE_OPENREQ,
15521da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
15531da177e4SLinus Torvalds };
15541da177e4SLinus Torvalds 
155573cb88ecSArjan van de Ven int tcp_seq_open(struct inode *inode, struct file *file);
155673cb88ecSArjan van de Ven 
15571da177e4SLinus Torvalds struct tcp_seq_afinfo {
15581da177e4SLinus Torvalds 	char				*name;
15591da177e4SLinus Torvalds 	sa_family_t			family;
156073cb88ecSArjan van de Ven 	const struct file_operations	*seq_fops;
15619427c4b3SDenis V. Lunev 	struct seq_operations		seq_ops;
15621da177e4SLinus Torvalds };
15631da177e4SLinus Torvalds 
15641da177e4SLinus Torvalds struct tcp_iter_state {
1565a4146b1bSDenis V. Lunev 	struct seq_net_private	p;
15661da177e4SLinus Torvalds 	sa_family_t		family;
15671da177e4SLinus Torvalds 	enum tcp_seq_states	state;
15681da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
1569a7cb5a49SEric W. Biederman 	int			bucket, offset, sbucket, num;
1570a7cb5a49SEric W. Biederman 	kuid_t			uid;
1571a8b690f9STom Herbert 	loff_t			last_pos;
15721da177e4SLinus Torvalds };
15731da177e4SLinus Torvalds 
15745c9f3023SJoe Perches int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo);
15755c9f3023SJoe Perches void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo);
15761da177e4SLinus Torvalds 
157720380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
1578c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops;
157920380731SArnaldo Carvalho de Melo 
15805c9f3023SJoe Perches void tcp_v4_destroy_sock(struct sock *sk);
158120380731SArnaldo Carvalho de Melo 
158228be6e07SEric Dumazet struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
1583c8f44affSMichał Mirosław 				netdev_features_t features);
15845c9f3023SJoe Perches struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb);
15855c9f3023SJoe Perches int tcp_gro_complete(struct sk_buff *skb);
158628850dc7SDaniel Borkmann 
15875c9f3023SJoe Perches void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr);
1588f4c50d99SHerbert Xu 
1589c9bee3b7SEric Dumazet static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp)
1590c9bee3b7SEric Dumazet {
1591c9bee3b7SEric Dumazet 	return tp->notsent_lowat ?: sysctl_tcp_notsent_lowat;
1592c9bee3b7SEric Dumazet }
1593c9bee3b7SEric Dumazet 
1594c9bee3b7SEric Dumazet static inline bool tcp_stream_memory_free(const struct sock *sk)
1595c9bee3b7SEric Dumazet {
1596c9bee3b7SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
1597c9bee3b7SEric Dumazet 	u32 notsent_bytes = tp->write_seq - tp->snd_nxt;
1598c9bee3b7SEric Dumazet 
1599c9bee3b7SEric Dumazet 	return notsent_bytes < tcp_notsent_lowat(tp);
1600c9bee3b7SEric Dumazet }
1601c9bee3b7SEric Dumazet 
160220380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
16035c9f3023SJoe Perches int tcp4_proc_init(void);
16045c9f3023SJoe Perches void tcp4_proc_exit(void);
160520380731SArnaldo Carvalho de Melo #endif
160620380731SArnaldo Carvalho de Melo 
16075db92c99SOctavian Purdila int tcp_rtx_synack(struct sock *sk, struct request_sock *req);
16081fb6f159SOctavian Purdila int tcp_conn_request(struct request_sock_ops *rsk_ops,
16091fb6f159SOctavian Purdila 		     const struct tcp_request_sock_ops *af_ops,
16101fb6f159SOctavian Purdila 		     struct sock *sk, struct sk_buff *skb);
16115db92c99SOctavian Purdila 
1612cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */
1613cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops {
1614cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1615cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key	*(*md5_lookup) (struct sock *sk,
1616cfb6eeb4SYOSHIFUJI Hideaki 						struct sock *addr_sk);
1617cfb6eeb4SYOSHIFUJI Hideaki 	int			(*calc_md5_hash) (char *location,
1618cfb6eeb4SYOSHIFUJI Hideaki 						  struct tcp_md5sig_key *md5,
1619318cf7aaSEric Dumazet 						  const struct sock *sk,
1620318cf7aaSEric Dumazet 						  const struct request_sock *req,
1621318cf7aaSEric Dumazet 						  const struct sk_buff *skb);
1622cfb6eeb4SYOSHIFUJI Hideaki 	int			(*md5_parse) (struct sock *sk,
1623cfb6eeb4SYOSHIFUJI Hideaki 					      char __user *optval,
1624cfb6eeb4SYOSHIFUJI Hideaki 					      int optlen);
1625cfb6eeb4SYOSHIFUJI Hideaki #endif
1626cfb6eeb4SYOSHIFUJI Hideaki };
1627cfb6eeb4SYOSHIFUJI Hideaki 
1628cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops {
16292aec4a29SOctavian Purdila 	u16 mss_clamp;
1630cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1631cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key	*(*md5_lookup) (struct sock *sk,
1632cfb6eeb4SYOSHIFUJI Hideaki 						struct request_sock *req);
1633e3afe7b7SJohn Dykstra 	int			(*calc_md5_hash) (char *location,
1634e3afe7b7SJohn Dykstra 						  struct tcp_md5sig_key *md5,
1635318cf7aaSEric Dumazet 						  const struct sock *sk,
1636318cf7aaSEric Dumazet 						  const struct request_sock *req,
1637318cf7aaSEric Dumazet 						  const struct sk_buff *skb);
1638cfb6eeb4SYOSHIFUJI Hideaki #endif
163916bea70aSOctavian Purdila 	void (*init_req)(struct request_sock *req, struct sock *sk,
164016bea70aSOctavian Purdila 			 struct sk_buff *skb);
1641fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
1642fb7b37a7SOctavian Purdila 	__u32 (*cookie_init_seq)(struct sock *sk, const struct sk_buff *skb,
1643fb7b37a7SOctavian Purdila 				 __u16 *mss);
1644fb7b37a7SOctavian Purdila #endif
1645d94e0417SOctavian Purdila 	struct dst_entry *(*route_req)(struct sock *sk, struct flowi *fl,
1646d94e0417SOctavian Purdila 				       const struct request_sock *req,
1647d94e0417SOctavian Purdila 				       bool *strict);
1648936b8bdbSOctavian Purdila 	__u32 (*init_seq)(const struct sk_buff *skb);
1649d6274bd8SOctavian Purdila 	int (*send_synack)(struct sock *sk, struct dst_entry *dst,
1650d6274bd8SOctavian Purdila 			   struct flowi *fl, struct request_sock *req,
1651d6274bd8SOctavian Purdila 			   u16 queue_mapping, struct tcp_fastopen_cookie *foc);
1652695da14eSOctavian Purdila 	void (*queue_hash_add)(struct sock *sk, struct request_sock *req,
1653695da14eSOctavian Purdila 			       const unsigned long timeout);
1654cfb6eeb4SYOSHIFUJI Hideaki };
1655cfb6eeb4SYOSHIFUJI Hideaki 
1656fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES
1657fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
1658fb7b37a7SOctavian Purdila 					 struct sock *sk, struct sk_buff *skb,
1659fb7b37a7SOctavian Purdila 					 __u16 *mss)
1660fb7b37a7SOctavian Purdila {
1661fb7b37a7SOctavian Purdila 	return ops->cookie_init_seq(sk, skb, mss);
1662fb7b37a7SOctavian Purdila }
1663fb7b37a7SOctavian Purdila #else
1664fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops,
1665fb7b37a7SOctavian Purdila 					 struct sock *sk, struct sk_buff *skb,
1666fb7b37a7SOctavian Purdila 					 __u16 *mss)
1667fb7b37a7SOctavian Purdila {
1668fb7b37a7SOctavian Purdila 	return 0;
1669fb7b37a7SOctavian Purdila }
1670fb7b37a7SOctavian Purdila #endif
1671fb7b37a7SOctavian Purdila 
16725c9f3023SJoe Perches int tcpv4_offload_init(void);
167328850dc7SDaniel Borkmann 
16745c9f3023SJoe Perches void tcp_v4_init(void);
16755c9f3023SJoe Perches void tcp_init(void);
167620380731SArnaldo Carvalho de Melo 
1677e25f866fSCong Wang /*
1678e25f866fSCong Wang  * Save and compile IPv4 options, return a pointer to it
1679e25f866fSCong Wang  */
1680e25f866fSCong Wang static inline struct ip_options_rcu *tcp_v4_save_options(struct sk_buff *skb)
1681e25f866fSCong Wang {
1682e25f866fSCong Wang 	const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt;
1683e25f866fSCong Wang 	struct ip_options_rcu *dopt = NULL;
1684e25f866fSCong Wang 
1685461b74c3SCong Wang 	if (opt->optlen) {
1686e25f866fSCong Wang 		int opt_size = sizeof(*dopt) + opt->optlen;
1687e25f866fSCong Wang 
1688e25f866fSCong Wang 		dopt = kmalloc(opt_size, GFP_ATOMIC);
1689e25f866fSCong Wang 		if (dopt && __ip_options_echo(&dopt->opt, skb, opt)) {
1690e25f866fSCong Wang 			kfree(dopt);
1691e25f866fSCong Wang 			dopt = NULL;
1692e25f866fSCong Wang 		}
1693e25f866fSCong Wang 	}
1694e25f866fSCong Wang 	return dopt;
1695e25f866fSCong Wang }
1696e25f866fSCong Wang 
16971da177e4SLinus Torvalds #endif	/* _TCP_H */
1698