xref: /linux/include/net/tcp.h (revision 016818d076871c4ee34db1e8d74dc17ac1de626a)
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>
3097fc2f08SChris Leech #include <linux/dmaengine.h>
31cfb6eeb4SYOSHIFUJI Hideaki #include <linux/crypto.h>
32c6aefafbSGlenn Griffin #include <linux/cryptohash.h>
33435cf559SWilliam Allen Simpson #include <linux/kref.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;
531da177e4SLinus Torvalds extern 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 
64356f0398SNandita Dukkipati /* Offer an initial receive window of 10 mss. */
65356f0398SNandita Dukkipati #define TCP_DEFAULT_INIT_RCVWND	10
66356f0398SNandita Dukkipati 
671da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
681da177e4SLinus Torvalds #define TCP_MIN_MSS		88U
691da177e4SLinus Torvalds 
705d424d5aSJohn Heffner /* The least MTU to use for probing */
715d424d5aSJohn Heffner #define TCP_BASE_MSS		512
725d424d5aSJohn Heffner 
731da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
741da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds /* Maximal reordering. */
771da177e4SLinus Torvalds #define TCP_MAX_REORDERING	127
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
801da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds /* urg_data states */
831da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
841da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
851da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #define TCP_RETR1	3	/*
881da177e4SLinus Torvalds 				 * This is how many retries it does before it
891da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
901da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
911da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
921da177e4SLinus Torvalds 				 */
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #define TCP_RETR2	15	/*
951da177e4SLinus Torvalds 				 * This should take at least
961da177e4SLinus Torvalds 				 * 90 minutes to time out.
971da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
981da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
991da177e4SLinus Torvalds 				 */
1001da177e4SLinus Torvalds 
1016c9ff979SAlex Bergmann #define TCP_SYN_RETRIES	 6	/* This is how many retries are done
1026c9ff979SAlex Bergmann 				 * when active opening a connection.
1036c9ff979SAlex Bergmann 				 * RFC1122 says the minimum retry MUST
1046c9ff979SAlex Bergmann 				 * be at least 180secs.  Nevertheless
1056c9ff979SAlex Bergmann 				 * this value is corresponding to
1066c9ff979SAlex Bergmann 				 * 63secs of retransmission with the
1076c9ff979SAlex Bergmann 				 * current initial RTO.
1086c9ff979SAlex Bergmann 				 */
1091da177e4SLinus Torvalds 
1106c9ff979SAlex Bergmann #define TCP_SYNACK_RETRIES 5	/* This is how may retries are done
1116c9ff979SAlex Bergmann 				 * when passive opening a connection.
1126c9ff979SAlex Bergmann 				 * This is corresponding to 31secs of
1136c9ff979SAlex Bergmann 				 * retransmission with the current
1146c9ff979SAlex Bergmann 				 * initial RTO.
1156c9ff979SAlex Bergmann 				 */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1181da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1191da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1201da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1211da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1221da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1231da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1241da177e4SLinus Torvalds 				  */
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1271da177e4SLinus Torvalds #if HZ >= 100
1281da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1291da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1301da177e4SLinus Torvalds #else
1311da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1321da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1331da177e4SLinus Torvalds #endif
1341da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1351da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
136fd4f2ceaSEric Dumazet #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))	/* RFC6298 2.1 initial RTO value	*/
1379ad7c049SJerry Chu #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value, now
1389ad7c049SJerry Chu 						 * used as a fallback RTO for the
1399ad7c049SJerry Chu 						 * initial data transmission if no
1409ad7c049SJerry Chu 						 * valid RTT sample has been acquired,
1419ad7c049SJerry Chu 						 * most likely due to retrans in 3WHS.
1429ad7c049SJerry Chu 						 */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1451da177e4SLinus Torvalds 					                 * for local resources.
1461da177e4SLinus Torvalds 					                 */
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1491da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1501da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1531da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1541da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1551da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1601da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1611da177e4SLinus Torvalds 					 * after this time. It should be equal
1621da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1631da177e4SLinus Torvalds 					 * to provide reliability equal to one
1641da177e4SLinus Torvalds 					 * provided by timewait state.
1651da177e4SLinus Torvalds 					 */
1661da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1671da177e4SLinus Torvalds 					 * timestamps. It must be less than
1681da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1691da177e4SLinus Torvalds 					 */
1701da177e4SLinus Torvalds /*
1711da177e4SLinus Torvalds  *	TCP option
1721da177e4SLinus Torvalds  */
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1751da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1761da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1771da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1781da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1791da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1801da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
181cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
182435cf559SWilliam Allen Simpson #define TCPOPT_COOKIE		253	/* Cookie extension (experimental) */
1832100c8d2SYuchung Cheng #define TCPOPT_EXP		254	/* Experimental */
1842100c8d2SYuchung Cheng /* Magic number to be after the option value for sharing TCP
1852100c8d2SYuchung Cheng  * experimental options. See draft-ietf-tcpm-experimental-options-00.txt
1862100c8d2SYuchung Cheng  */
1872100c8d2SYuchung Cheng #define TCPOPT_FASTOPEN_MAGIC	0xF989
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /*
1901da177e4SLinus Torvalds  *     TCP option lengths
1911da177e4SLinus Torvalds  */
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds #define TCPOLEN_MSS            4
1941da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
1951da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
1961da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
197cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG         18
1982100c8d2SYuchung Cheng #define TCPOLEN_EXP_FASTOPEN_BASE  4
199435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_BASE    2	/* Cookie-less header extension */
200435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_PAIR    3	/* Cookie pair header extension */
201435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_MIN     (TCPOLEN_COOKIE_BASE+TCP_COOKIE_MIN)
202435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_MAX     (TCPOLEN_COOKIE_BASE+TCP_COOKIE_MAX)
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds /* But this is what stacks really send out. */
2051da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
2061da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
2071da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
2081da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
2091da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
2101da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
211cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED		20
21233ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED		4
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds /* Flags in tp->nonagle */
2151da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
2161da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
217caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
2181da177e4SLinus Torvalds 
21936e31b0aSAndreas Petlund /* TCP thin-stream limits */
22036e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
22136e31b0aSAndreas Petlund 
2227eb38527SDavid S. Miller /* TCP initial congestion window as per draft-hkchu-tcpm-initcwnd-01 */
223442b9635SDavid S. Miller #define TCP_INIT_CWND		10
224442b9635SDavid S. Miller 
225cf60af03SYuchung Cheng /* Bit Flags for sysctl_tcp_fastopen */
226cf60af03SYuchung Cheng #define	TFO_CLIENT_ENABLE	1
22710467163SJerry Chu #define	TFO_SERVER_ENABLE	2
22867da22d2SYuchung Cheng #define	TFO_CLIENT_NO_COOKIE	4	/* Data in SYN w/o cookie option */
229cf60af03SYuchung Cheng 
23010467163SJerry Chu /* Process SYN data but skip cookie validation */
23110467163SJerry Chu #define	TFO_SERVER_COOKIE_NOT_CHKED	0x100
23210467163SJerry Chu /* Accept SYN data w/o any cookie option */
23310467163SJerry Chu #define	TFO_SERVER_COOKIE_NOT_REQD	0x200
23410467163SJerry Chu 
23510467163SJerry Chu /* Force enable TFO on all listeners, i.e., not requiring the
23610467163SJerry Chu  * TCP_FASTOPEN socket option. SOCKOPT1/2 determine how to set max_qlen.
23710467163SJerry Chu  */
23810467163SJerry Chu #define	TFO_SERVER_WO_SOCKOPT1	0x400
23910467163SJerry Chu #define	TFO_SERVER_WO_SOCKOPT2	0x800
24010467163SJerry Chu /* Always create TFO child sockets on a TFO listener even when
24110467163SJerry Chu  * cookie/data not present. (For testing purpose!)
24210467163SJerry Chu  */
24310467163SJerry Chu #define	TFO_SERVER_ALWAYS	0x1000
24410467163SJerry Chu 
245295ff7edSArnaldo Carvalho de Melo extern struct inet_timewait_death_row tcp_death_row;
246295ff7edSArnaldo Carvalho de Melo 
2471da177e4SLinus Torvalds /* sysctl variables for tcp */
2481da177e4SLinus Torvalds extern int sysctl_tcp_timestamps;
2491da177e4SLinus Torvalds extern int sysctl_tcp_window_scaling;
2501da177e4SLinus Torvalds extern int sysctl_tcp_sack;
2511da177e4SLinus Torvalds extern int sysctl_tcp_fin_timeout;
2521da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_time;
2531da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_probes;
2541da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_intvl;
2551da177e4SLinus Torvalds extern int sysctl_tcp_syn_retries;
2561da177e4SLinus Torvalds extern int sysctl_tcp_synack_retries;
2571da177e4SLinus Torvalds extern int sysctl_tcp_retries1;
2581da177e4SLinus Torvalds extern int sysctl_tcp_retries2;
2591da177e4SLinus Torvalds extern int sysctl_tcp_orphan_retries;
2601da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
2612100c8d2SYuchung Cheng extern int sysctl_tcp_fastopen;
2621da177e4SLinus Torvalds extern int sysctl_tcp_retrans_collapse;
2631da177e4SLinus Torvalds extern int sysctl_tcp_stdurg;
2641da177e4SLinus Torvalds extern int sysctl_tcp_rfc1337;
2651da177e4SLinus Torvalds extern int sysctl_tcp_abort_on_overflow;
2661da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
2671da177e4SLinus Torvalds extern int sysctl_tcp_fack;
2681da177e4SLinus Torvalds extern int sysctl_tcp_reordering;
2691da177e4SLinus Torvalds extern int sysctl_tcp_ecn;
2701da177e4SLinus Torvalds extern int sysctl_tcp_dsack;
2711da177e4SLinus Torvalds extern int sysctl_tcp_wmem[3];
2721da177e4SLinus Torvalds extern int sysctl_tcp_rmem[3];
2731da177e4SLinus Torvalds extern int sysctl_tcp_app_win;
2741da177e4SLinus Torvalds extern int sysctl_tcp_adv_win_scale;
2751da177e4SLinus Torvalds extern int sysctl_tcp_tw_reuse;
2761da177e4SLinus Torvalds extern int sysctl_tcp_frto;
2773cfe3baaSIlpo Järvinen extern int sysctl_tcp_frto_response;
2781da177e4SLinus Torvalds extern int sysctl_tcp_low_latency;
27995937825SChris Leech extern int sysctl_tcp_dma_copybreak;
2801da177e4SLinus Torvalds extern int sysctl_tcp_nometrics_save;
2811da177e4SLinus Torvalds extern int sysctl_tcp_moderate_rcvbuf;
2821da177e4SLinus Torvalds extern int sysctl_tcp_tso_win_divisor;
2839772efb9SStephen Hemminger extern int sysctl_tcp_abc;
2845d424d5aSJohn Heffner extern int sysctl_tcp_mtu_probing;
2855d424d5aSJohn Heffner extern int sysctl_tcp_base_mss;
28615d99e02SRick Jones extern int sysctl_tcp_workaround_signed_windows;
28735089bb2SDavid S. Miller extern int sysctl_tcp_slow_start_after_idle;
288886236c1SJohn Heffner extern int sysctl_tcp_max_ssthresh;
289519855c5SWilliam Allen Simpson extern int sysctl_tcp_cookie_size;
29036e31b0aSAndreas Petlund extern int sysctl_tcp_thin_linear_timeouts;
2917e380175SAndreas Petlund extern int sysctl_tcp_thin_dupack;
292eed530b6SYuchung Cheng extern int sysctl_tcp_early_retrans;
29346d3ceabSEric Dumazet extern int sysctl_tcp_limit_output_bytes;
294282f23c6SEric Dumazet extern int sysctl_tcp_challenge_ack_limit;
2951da177e4SLinus Torvalds 
2968d987e5cSEric Dumazet extern atomic_long_t tcp_memory_allocated;
2971748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated;
2981da177e4SLinus Torvalds extern int tcp_memory_pressure;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds /*
3011da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
3021da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
3031da177e4SLinus Torvalds  */
3041da177e4SLinus Torvalds 
305a2a385d6SEric Dumazet static inline bool before(__u32 seq1, __u32 seq2)
3061da177e4SLinus Torvalds {
3070d630cc0SGerrit Renker         return (__s32)(seq1-seq2) < 0;
3081da177e4SLinus Torvalds }
3099a036b9cSGerrit Renker #define after(seq2, seq1) 	before(seq1, seq2)
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
312a2a385d6SEric Dumazet static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
3131da177e4SLinus Torvalds {
3141da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
317efcdbf24SArun Sharma static inline bool tcp_out_of_memory(struct sock *sk)
318efcdbf24SArun Sharma {
319efcdbf24SArun Sharma 	if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
320efcdbf24SArun Sharma 	    sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
321efcdbf24SArun Sharma 		return true;
322efcdbf24SArun Sharma 	return false;
323efcdbf24SArun Sharma }
324efcdbf24SArun Sharma 
325ad1af0feSDavid S. Miller static inline bool tcp_too_many_orphans(struct sock *sk, int shift)
326e4fd5da3SPavel Emelianov {
327ad1af0feSDavid S. Miller 	struct percpu_counter *ocp = sk->sk_prot->orphan_count;
328ad1af0feSDavid S. Miller 	int orphans = percpu_counter_read_positive(ocp);
329ad1af0feSDavid S. Miller 
330ad1af0feSDavid S. Miller 	if (orphans << shift > sysctl_tcp_max_orphans) {
331ad1af0feSDavid S. Miller 		orphans = percpu_counter_sum_positive(ocp);
332ad1af0feSDavid S. Miller 		if (orphans << shift > sysctl_tcp_max_orphans)
333ad1af0feSDavid S. Miller 			return true;
334ad1af0feSDavid S. Miller 	}
335ad1af0feSDavid S. Miller 	return false;
336e4fd5da3SPavel Emelianov }
3371da177e4SLinus Torvalds 
338efcdbf24SArun Sharma extern bool tcp_check_oom(struct sock *sk, int shift);
339efcdbf24SArun Sharma 
340a0f82f64SFlorian Westphal /* syncookies: remember time of last synqueue overflow */
341a0f82f64SFlorian Westphal static inline void tcp_synq_overflow(struct sock *sk)
342a0f82f64SFlorian Westphal {
343a0f82f64SFlorian Westphal 	tcp_sk(sk)->rx_opt.ts_recent_stamp = jiffies;
344a0f82f64SFlorian Westphal }
345a0f82f64SFlorian Westphal 
346a0f82f64SFlorian Westphal /* syncookies: no recent synqueue overflow on this listening socket? */
347a2a385d6SEric Dumazet static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
348a0f82f64SFlorian Westphal {
349a0f82f64SFlorian Westphal 	unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
3509ad7c049SJerry Chu 	return time_after(jiffies, last_overflow + TCP_TIMEOUT_FALLBACK);
351a0f82f64SFlorian Westphal }
352a0f82f64SFlorian Westphal 
3531da177e4SLinus Torvalds extern struct proto tcp_prot;
3541da177e4SLinus Torvalds 
35557ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
35657ef42d5SPavel Emelyanov #define TCP_INC_STATS_BH(net, field)	SNMP_INC_STATS_BH((net)->mib.tcp_statistics, field)
35757ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
35857ef42d5SPavel Emelyanov #define TCP_ADD_STATS_USER(net, field, val) SNMP_ADD_STATS_USER((net)->mib.tcp_statistics, field, val)
359aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
3601da177e4SLinus Torvalds 
3614acb4190SGlauber Costa extern void tcp_init_mem(struct net *net);
3624acb4190SGlauber Costa 
36346d3ceabSEric Dumazet extern void tcp_tasklet_init(void);
36446d3ceabSEric Dumazet 
3651da177e4SLinus Torvalds extern void tcp_v4_err(struct sk_buff *skb, u32);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds extern void tcp_shutdown (struct sock *sk, int how);
3681da177e4SLinus Torvalds 
369160eb5a6SDavid S. Miller extern void tcp_v4_early_demux(struct sk_buff *skb);
3701da177e4SLinus Torvalds extern int tcp_v4_rcv(struct sk_buff *skb);
3711da177e4SLinus Torvalds 
3724670fd81SDavid S. Miller extern struct inet_peer *tcp_v4_get_peer(struct sock *sk);
3738feaf0c0SArnaldo Carvalho de Melo extern int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
3747ba42910SChangli Gao extern int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
3757ba42910SChangli Gao 		       size_t size);
3767ba42910SChangli Gao extern int tcp_sendpage(struct sock *sk, struct page *page, int offset,
37753d3176bSChangli Gao 			size_t size, int flags);
37846d3ceabSEric Dumazet extern void tcp_release_cb(struct sock *sk);
3796f458dfbSEric Dumazet extern void tcp_write_timer_handler(struct sock *sk);
3806f458dfbSEric Dumazet extern void tcp_delack_timer_handler(struct sock *sk);
38153d3176bSChangli Gao extern int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
38253d3176bSChangli Gao extern int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
383cf533ea5SEric Dumazet 				 const struct tcphdr *th, unsigned int len);
38453d3176bSChangli Gao extern int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
385cf533ea5SEric Dumazet 			       const struct tcphdr *th, unsigned int len);
3861da177e4SLinus Torvalds extern void tcp_rcv_space_adjust(struct sock *sk);
3870e4b4992SChris Leech extern void tcp_cleanup_rbuf(struct sock *sk, int copied);
38853d3176bSChangli Gao extern int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
389cfb6eeb4SYOSHIFUJI Hideaki extern void tcp_twsk_destructor(struct sock *sk);
3909c55e01cSJens Axboe extern ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
39153d3176bSChangli Gao 			       struct pipe_inode_info *pipe, size_t len,
39253d3176bSChangli Gao 			       unsigned int flags);
3939c55e01cSJens Axboe 
394463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
395463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3961da177e4SLinus Torvalds {
397463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
398fc6415bcSDavid S. Miller 
399463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
400463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
401463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
4021da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
403463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
404fc6415bcSDavid S. Miller 		} else
405463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
409bdf1ee5dSIlpo Järvinen #define	TCP_ECN_OK		1
410bdf1ee5dSIlpo Järvinen #define	TCP_ECN_QUEUE_CWR	2
411bdf1ee5dSIlpo Järvinen #define	TCP_ECN_DEMAND_CWR	4
4127a269ffaSEric Dumazet #define	TCP_ECN_SEEN		8
413bdf1ee5dSIlpo Järvinen 
414fd2c3ef7SEric Dumazet enum tcp_tw_status {
4151da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
4161da177e4SLinus Torvalds 	TCP_TW_RST = 1,
4171da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
4181da177e4SLinus Torvalds 	TCP_TW_SYN = 3
4191da177e4SLinus Torvalds };
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 
4228feaf0c0SArnaldo Carvalho de Melo extern enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
4231da177e4SLinus Torvalds 						     struct sk_buff *skb,
4248feaf0c0SArnaldo Carvalho de Melo 						     const struct tcphdr *th);
4251da177e4SLinus Torvalds extern struct sock * tcp_check_req(struct sock *sk,struct sk_buff *skb,
42660236fddSArnaldo Carvalho de Melo 				   struct request_sock *req,
4278336886fSJerry Chu 				   struct request_sock **prev,
4288336886fSJerry Chu 				   bool fastopen);
42953d3176bSChangli Gao extern int tcp_child_process(struct sock *parent, struct sock *child,
4301da177e4SLinus Torvalds 			     struct sk_buff *skb);
431a2a385d6SEric Dumazet extern bool tcp_use_frto(struct sock *sk);
4321da177e4SLinus Torvalds extern void tcp_enter_frto(struct sock *sk);
4331da177e4SLinus Torvalds extern void tcp_enter_loss(struct sock *sk, int how);
4341da177e4SLinus Torvalds extern void tcp_clear_retrans(struct tcp_sock *tp);
4351da177e4SLinus Torvalds extern void tcp_update_metrics(struct sock *sk);
4364aabd8efSDavid S. Miller extern void tcp_init_metrics(struct sock *sk);
43751c5d0c4SDavid S. Miller extern void tcp_metrics_init(void);
43881166dd6SDavid S. Miller extern bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst, bool paws_check);
43981166dd6SDavid S. Miller extern bool tcp_remember_stamp(struct sock *sk);
44081166dd6SDavid S. Miller extern bool tcp_tw_remember_stamp(struct inet_timewait_sock *tw);
44181166dd6SDavid S. Miller extern void tcp_fetch_timewait_stamp(struct sock *sk, struct dst_entry *dst);
4424aabd8efSDavid S. Miller extern void tcp_disable_fack(struct tcp_sock *tp);
44353d3176bSChangli Gao extern void tcp_close(struct sock *sk, long timeout);
444900f65d3SNeal Cardwell extern void tcp_init_sock(struct sock *sk);
44553d3176bSChangli Gao extern unsigned int tcp_poll(struct file * file, struct socket *sock,
44653d3176bSChangli Gao 			     struct poll_table_struct *wait);
44753d3176bSChangli Gao extern int tcp_getsockopt(struct sock *sk, int level, int optname,
4483fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen);
44953d3176bSChangli Gao extern int tcp_setsockopt(struct sock *sk, int level, int optname,
45053d3176bSChangli Gao 			  char __user *optval, unsigned int optlen);
45153d3176bSChangli Gao extern int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
45253d3176bSChangli Gao 				 char __user *optval, int __user *optlen);
45353d3176bSChangli Gao extern int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
454b7058842SDavid S. Miller 				 char __user *optval, unsigned int optlen);
4551da177e4SLinus Torvalds extern void tcp_set_keepalive(struct sock *sk, int val);
45653d3176bSChangli Gao extern void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req);
45753d3176bSChangli Gao extern int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
45853d3176bSChangli Gao 		       size_t len, int nonblock, int flags, int *addr_len);
459cf533ea5SEric Dumazet extern void tcp_parse_options(const struct sk_buff *skb,
460cf533ea5SEric Dumazet 			      struct tcp_options_received *opt_rx, const u8 **hvpp,
4612100c8d2SYuchung Cheng 			      int estab, struct tcp_fastopen_cookie *foc);
462cf533ea5SEric Dumazet extern const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
4637d5d5525SYOSHIFUJI Hideaki 
4641da177e4SLinus Torvalds /*
4651da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4661da177e4SLinus Torvalds  */
4671da177e4SLinus Torvalds 
46853d3176bSChangli Gao extern void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
46953d3176bSChangli Gao extern int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
4701da177e4SLinus Torvalds extern struct sock * tcp_create_openreq_child(struct sock *sk,
47160236fddSArnaldo Carvalho de Melo 					      struct request_sock *req,
4721da177e4SLinus Torvalds 					      struct sk_buff *skb);
47353d3176bSChangli Gao extern struct sock * tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
47460236fddSArnaldo Carvalho de Melo 					  struct request_sock *req,
4751da177e4SLinus Torvalds 					  struct dst_entry *dst);
47653d3176bSChangli Gao extern int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
47753d3176bSChangli Gao extern int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
4781da177e4SLinus Torvalds 			  int addr_len);
4791da177e4SLinus Torvalds extern int tcp_connect(struct sock *sk);
48053d3176bSChangli Gao extern struct sk_buff * tcp_make_synack(struct sock *sk, struct dst_entry *dst,
481e6b4d113SWilliam Allen Simpson 					struct request_sock *req,
4828336886fSJerry Chu 					struct request_values *rvp,
4838336886fSJerry Chu 					struct tcp_fastopen_cookie *foc);
4841da177e4SLinus Torvalds extern int tcp_disconnect(struct sock *sk, int flags);
4851da177e4SLinus Torvalds 
486370816aeSPavel Emelyanov void tcp_connect_init(struct sock *sk);
487370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
488292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
48963d02d15SEric Dumazet void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /* From syncookies.c */
4922051f11fSFlorian Westphal extern __u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS];
4931da177e4SLinus Torvalds extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
4941da177e4SLinus Torvalds 				    struct ip_options *opt);
495e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
4961da177e4SLinus Torvalds extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb,
4971da177e4SLinus Torvalds 				     __u16 *mss);
498e05c82d3SEric Dumazet #else
499e05c82d3SEric Dumazet static inline __u32 cookie_v4_init_sequence(struct sock *sk,
500e05c82d3SEric Dumazet 					    struct sk_buff *skb,
501e05c82d3SEric Dumazet 					    __u16 *mss)
502e05c82d3SEric Dumazet {
503e05c82d3SEric Dumazet 	return 0;
504e05c82d3SEric Dumazet }
505e05c82d3SEric Dumazet #endif
5061da177e4SLinus Torvalds 
5074dfc2817SFlorian Westphal extern __u32 cookie_init_timestamp(struct request_sock *req);
508172d69e6SFlorian Westphal extern bool cookie_check_timestamp(struct tcp_options_received *opt, bool *);
5094dfc2817SFlorian Westphal 
510c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */
511c6aefafbSGlenn Griffin extern struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
512e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
513cf533ea5SEric Dumazet extern __u32 cookie_v6_init_sequence(struct sock *sk, const struct sk_buff *skb,
514c6aefafbSGlenn Griffin 				     __u16 *mss);
515e05c82d3SEric Dumazet #else
516e05c82d3SEric Dumazet static inline __u32 cookie_v6_init_sequence(struct sock *sk,
517e05c82d3SEric Dumazet 					    struct sk_buff *skb,
518e05c82d3SEric Dumazet 					    __u16 *mss)
519e05c82d3SEric Dumazet {
520e05c82d3SEric Dumazet 	return 0;
521e05c82d3SEric Dumazet }
522e05c82d3SEric Dumazet #endif
5231da177e4SLinus Torvalds /* tcp_output.c */
5241da177e4SLinus Torvalds 
5259e412ba7SIlpo Järvinen extern void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
5269e412ba7SIlpo Järvinen 				      int nonagle);
527a2a385d6SEric Dumazet extern bool tcp_may_send_now(struct sock *sk);
5281da177e4SLinus Torvalds extern int tcp_retransmit_skb(struct sock *, struct sk_buff *);
529f1ecd5d9SDamian Lukowski extern void tcp_retransmit_timer(struct sock *sk);
5301da177e4SLinus Torvalds extern void tcp_xmit_retransmit_queue(struct sock *);
5311da177e4SLinus Torvalds extern void tcp_simple_retransmit(struct sock *);
5321da177e4SLinus Torvalds extern int tcp_trim_head(struct sock *, struct sk_buff *, u32);
5336475be16SDavid S. Miller extern int tcp_fragment(struct sock *, struct sk_buff *, u32, unsigned int);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds extern void tcp_send_probe0(struct sock *);
5361da177e4SLinus Torvalds extern void tcp_send_partial(struct sock *);
5371da177e4SLinus Torvalds extern int tcp_write_wakeup(struct sock *);
5381da177e4SLinus Torvalds extern void tcp_send_fin(struct sock *sk);
539dd0fc66fSAl Viro extern void tcp_send_active_reset(struct sock *sk, gfp_t priority);
5401da177e4SLinus Torvalds extern int tcp_send_synack(struct sock *);
541a2a385d6SEric Dumazet extern bool tcp_syn_flood_action(struct sock *sk,
542946cedccSEric Dumazet 				 const struct sk_buff *skb,
543946cedccSEric Dumazet 				 const char *proto);
544c1b4a7e6SDavid S. Miller extern void tcp_push_one(struct sock *, unsigned int mss_now);
5451da177e4SLinus Torvalds extern void tcp_send_ack(struct sock *sk);
5461da177e4SLinus Torvalds extern void tcp_send_delayed_ack(struct sock *sk);
5471da177e4SLinus Torvalds 
548a762a980SDavid S. Miller /* tcp_input.c */
549a762a980SDavid S. Miller extern void tcp_cwnd_application_limited(struct sock *sk);
550750ea2baSYuchung Cheng extern void tcp_resume_early_retransmit(struct sock *sk);
551750ea2baSYuchung Cheng extern void tcp_rearm_rto(struct sock *sk);
55210467163SJerry Chu extern void tcp_reset(struct sock *sk);
553a762a980SDavid S. Miller 
5541da177e4SLinus Torvalds /* tcp_timer.c */
5551da177e4SLinus Torvalds extern void tcp_init_xmit_timers(struct sock *);
556463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
557463c84b9SArnaldo Carvalho de Melo {
558463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
559463c84b9SArnaldo Carvalho de Melo }
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds extern unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
5620c54b85fSIlpo Järvinen extern unsigned int tcp_current_mss(struct sock *sk);
5630c54b85fSIlpo Järvinen 
5640c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */
5650c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
5660c54b85fSIlpo Järvinen {
56701f83d69SAlexey Kuznetsov 	int cutoff;
56801f83d69SAlexey Kuznetsov 
56901f83d69SAlexey Kuznetsov 	/* When peer uses tiny windows, there is no use in packetizing
57001f83d69SAlexey Kuznetsov 	 * to sub-MSS pieces for the sake of SWS or making sure there
57101f83d69SAlexey Kuznetsov 	 * are enough packets in the pipe for fast recovery.
57201f83d69SAlexey Kuznetsov 	 *
57301f83d69SAlexey Kuznetsov 	 * On the other hand, for extremely large MSS devices, handling
57401f83d69SAlexey Kuznetsov 	 * smaller than MSS windows in this way does make sense.
57501f83d69SAlexey Kuznetsov 	 */
57601f83d69SAlexey Kuznetsov 	if (tp->max_window >= 512)
57701f83d69SAlexey Kuznetsov 		cutoff = (tp->max_window >> 1);
57801f83d69SAlexey Kuznetsov 	else
57901f83d69SAlexey Kuznetsov 		cutoff = tp->max_window;
58001f83d69SAlexey Kuznetsov 
58101f83d69SAlexey Kuznetsov 	if (cutoff && pktsize > cutoff)
58201f83d69SAlexey Kuznetsov 		return max_t(int, cutoff, 68U - tp->tcp_header_len);
5830c54b85fSIlpo Järvinen 	else
5840c54b85fSIlpo Järvinen 		return pktsize;
5850c54b85fSIlpo Järvinen }
5861da177e4SLinus Torvalds 
58717b085eaSArnaldo Carvalho de Melo /* tcp.c */
588cf533ea5SEric Dumazet extern void tcp_get_info(const struct sock *, struct tcp_info *);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
5911da177e4SLinus Torvalds typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
5921da177e4SLinus Torvalds 				unsigned int, size_t);
5931da177e4SLinus Torvalds extern int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
5941da177e4SLinus Torvalds 			 sk_read_actor_t recv_actor);
5951da177e4SLinus Torvalds 
59640efc6faSStephen Hemminger extern void tcp_initialize_rcv_mss(struct sock *sk);
5971da177e4SLinus Torvalds 
59867469601SEric Dumazet extern int tcp_mtu_to_mss(struct sock *sk, int pmtu);
59967469601SEric Dumazet extern int tcp_mss_to_mtu(struct sock *sk, int mss);
6005d424d5aSJohn Heffner extern void tcp_mtup_init(struct sock *sk);
6019ad7c049SJerry Chu extern void tcp_valid_rtt_meas(struct sock *sk, u32 seq_rtt);
60210467163SJerry Chu extern void tcp_init_buffer_space(struct sock *sk);
6035d424d5aSJohn Heffner 
604f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk)
605f1ecd5d9SDamian Lukowski {
606f1ecd5d9SDamian Lukowski 	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
607f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
608f1ecd5d9SDamian Lukowski }
609f1ecd5d9SDamian Lukowski 
610f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
611f1ecd5d9SDamian Lukowski {
612f1ecd5d9SDamian Lukowski 	return (tp->srtt >> 3) + tp->rttvar;
613f1ecd5d9SDamian Lukowski }
614f1ecd5d9SDamian Lukowski 
6154aabd8efSDavid S. Miller extern void tcp_set_rto(struct sock *sk);
6164aabd8efSDavid S. Miller 
61740efc6faSStephen Hemminger static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
6181da177e4SLinus Torvalds {
6191da177e4SLinus Torvalds 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
6201da177e4SLinus Torvalds 			       ntohl(TCP_FLAG_ACK) |
6211da177e4SLinus Torvalds 			       snd_wnd);
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
62440efc6faSStephen Hemminger static inline void tcp_fast_path_on(struct tcp_sock *tp)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6299e412ba7SIlpo Järvinen static inline void tcp_fast_path_check(struct sock *sk)
6301da177e4SLinus Torvalds {
6319e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
6329e412ba7SIlpo Järvinen 
633b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue) &&
6341da177e4SLinus Torvalds 	    tp->rcv_wnd &&
6351da177e4SLinus Torvalds 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
6361da177e4SLinus Torvalds 	    !tp->urg_data)
6371da177e4SLinus Torvalds 		tcp_fast_path_on(tp);
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
6400c266898SSatoru SATOH /* Compute the actual rto_min value */
6410c266898SSatoru SATOH static inline u32 tcp_rto_min(struct sock *sk)
6420c266898SSatoru SATOH {
643cf533ea5SEric Dumazet 	const struct dst_entry *dst = __sk_dst_get(sk);
6440c266898SSatoru SATOH 	u32 rto_min = TCP_RTO_MIN;
6450c266898SSatoru SATOH 
6460c266898SSatoru SATOH 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
6470c266898SSatoru SATOH 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
6480c266898SSatoru SATOH 	return rto_min;
6490c266898SSatoru SATOH }
6500c266898SSatoru SATOH 
6511da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
6521da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
6531da177e4SLinus Torvalds  * than the offered window.
6541da177e4SLinus Torvalds  */
65540efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	if (win < 0)
6601da177e4SLinus Torvalds 		win = 0;
6611da177e4SLinus Torvalds 	return (u32) win;
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
6651da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
6661da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
6671da177e4SLinus Torvalds  */
6681da177e4SLinus Torvalds extern u32 __tcp_select_window(struct sock *sk);
6691da177e4SLinus Torvalds 
670ee995283SPavel Emelyanov void tcp_send_window_probe(struct sock *sk);
671ee995283SPavel Emelyanov 
6721da177e4SLinus Torvalds /* TCP timestamps are only 32-bits, this causes a slight
6731da177e4SLinus Torvalds  * complication on 64-bit systems since we store a snapshot
67431f34269SStephen Hemminger  * of jiffies in the buffer control blocks below.  We decided
67531f34269SStephen Hemminger  * to use only the low 32-bits of jiffies and hide the ugly
6761da177e4SLinus Torvalds  * casts with the following macro.
6771da177e4SLinus Torvalds  */
6781da177e4SLinus Torvalds #define tcp_time_stamp		((__u32)(jiffies))
6791da177e4SLinus Torvalds 
680a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
681a3433f35SChangli Gao 
682a3433f35SChangli Gao #define TCPHDR_FIN 0x01
683a3433f35SChangli Gao #define TCPHDR_SYN 0x02
684a3433f35SChangli Gao #define TCPHDR_RST 0x04
685a3433f35SChangli Gao #define TCPHDR_PSH 0x08
686a3433f35SChangli Gao #define TCPHDR_ACK 0x10
687a3433f35SChangli Gao #define TCPHDR_URG 0x20
688a3433f35SChangli Gao #define TCPHDR_ECE 0x40
689a3433f35SChangli Gao #define TCPHDR_CWR 0x80
690a3433f35SChangli Gao 
691caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
692f86586faSEric Dumazet  * TCP per-packet control information to the transmission code.
693f86586faSEric Dumazet  * We also store the host-order sequence numbers in here too.
694f86586faSEric Dumazet  * This is 44 bytes if IPV6 is enabled.
695f86586faSEric Dumazet  * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
6961da177e4SLinus Torvalds  */
6971da177e4SLinus Torvalds struct tcp_skb_cb {
6981da177e4SLinus Torvalds 	union {
6991da177e4SLinus Torvalds 		struct inet_skb_parm	h4;
700dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
7011da177e4SLinus Torvalds 		struct inet6_skb_parm	h6;
7021da177e4SLinus Torvalds #endif
7031da177e4SLinus Torvalds 	} header;	/* For incoming frames		*/
7041da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
7051da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
7061da177e4SLinus Torvalds 	__u32		when;		/* used to compute rtt's	*/
7074de075e0SEric Dumazet 	__u8		tcp_flags;	/* TCP header flags. (tcp[13])	*/
708f4f9f6e7SNeal Cardwell 
7091da177e4SLinus Torvalds 	__u8		sacked;		/* State flags for SACK/FACK.	*/
7101da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
7111da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
7121da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
7131da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
7141da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
7151da177e4SLinus Torvalds #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
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	*/
7201da177e4SLinus Torvalds };
7211da177e4SLinus Torvalds 
7221da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
7231da177e4SLinus Torvalds 
724bd14b1b2SEric Dumazet /* RFC3168 : 6.1.1 SYN packets must not have ECT/ECN bits set
725bd14b1b2SEric Dumazet  *
726bd14b1b2SEric Dumazet  * If we receive a SYN packet with these bits set, it means a network is
727bd14b1b2SEric Dumazet  * playing bad games with TOS bits. In order to avoid possible false congestion
728bd14b1b2SEric Dumazet  * notifications, we disable TCP ECN negociation.
729bd14b1b2SEric Dumazet  */
730bd14b1b2SEric Dumazet static inline void
731bd14b1b2SEric Dumazet TCP_ECN_create_request(struct request_sock *req, const struct sk_buff *skb)
732bd14b1b2SEric Dumazet {
733bd14b1b2SEric Dumazet 	const struct tcphdr *th = tcp_hdr(skb);
734bd14b1b2SEric Dumazet 
735bd14b1b2SEric Dumazet 	if (sysctl_tcp_ecn && th->ece && th->cwr &&
736bd14b1b2SEric Dumazet 	    INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield))
737bd14b1b2SEric Dumazet 		inet_rsk(req)->ecn_ok = 1;
738bd14b1b2SEric Dumazet }
739bd14b1b2SEric Dumazet 
7401da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
7411da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
7421da177e4SLinus Torvalds  */
7431da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
7441da177e4SLinus Torvalds {
7457967168cSHerbert Xu 	return skb_shinfo(skb)->gso_segs;
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds /* This is valid iff tcp_skb_pcount() > 1. */
7491da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
7501da177e4SLinus Torvalds {
7517967168cSHerbert Xu 	return skb_shinfo(skb)->gso_size;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
754317a76f9SStephen Hemminger /* Events passed to congestion control interface */
755317a76f9SStephen Hemminger enum tcp_ca_event {
756317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
757317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
758317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
759317a76f9SStephen Hemminger 	CA_EVENT_FRTO,		/* fast recovery timeout */
760317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
761317a76f9SStephen Hemminger 	CA_EVENT_FAST_ACK,	/* in sequence ack */
762317a76f9SStephen Hemminger 	CA_EVENT_SLOW_ACK,	/* other ack */
763317a76f9SStephen Hemminger };
764317a76f9SStephen Hemminger 
765317a76f9SStephen Hemminger /*
766317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
767317a76f9SStephen Hemminger  */
768317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
7693ff825b2SStephen Hemminger #define TCP_CA_MAX	128
7703ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
7713ff825b2SStephen Hemminger 
772164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1
773164891aaSStephen Hemminger #define TCP_CONG_RTT_STAMP	0x2
774164891aaSStephen Hemminger 
775317a76f9SStephen Hemminger struct tcp_congestion_ops {
776317a76f9SStephen Hemminger 	struct list_head	list;
777164891aaSStephen Hemminger 	unsigned long flags;
778317a76f9SStephen Hemminger 
779317a76f9SStephen Hemminger 	/* initialize private data (optional) */
7806687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
781317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
7826687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
783317a76f9SStephen Hemminger 
784317a76f9SStephen Hemminger 	/* return slow start threshold (required) */
7856687e988SArnaldo Carvalho de Melo 	u32 (*ssthresh)(struct sock *sk);
786317a76f9SStephen Hemminger 	/* lower bound for congestion window (optional) */
78772dc5b92SStephen Hemminger 	u32 (*min_cwnd)(const struct sock *sk);
788317a76f9SStephen Hemminger 	/* do new cwnd calculation (required) */
789c3a05c60SIlpo Järvinen 	void (*cong_avoid)(struct sock *sk, u32 ack, u32 in_flight);
790317a76f9SStephen Hemminger 	/* call before changing ca_state (optional) */
7916687e988SArnaldo Carvalho de Melo 	void (*set_state)(struct sock *sk, u8 new_state);
792317a76f9SStephen Hemminger 	/* call when cwnd event occurs (optional) */
7936687e988SArnaldo Carvalho de Melo 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
794317a76f9SStephen Hemminger 	/* new value of cwnd after loss (optional) */
7956687e988SArnaldo Carvalho de Melo 	u32  (*undo_cwnd)(struct sock *sk);
796317a76f9SStephen Hemminger 	/* hook for packet ack accounting (optional) */
79730cfd0baSStephen Hemminger 	void (*pkts_acked)(struct sock *sk, u32 num_acked, s32 rtt_us);
79873c1f4a0SArnaldo Carvalho de Melo 	/* get info for inet_diag (optional) */
7996687e988SArnaldo Carvalho de Melo 	void (*get_info)(struct sock *sk, u32 ext, struct sk_buff *skb);
800317a76f9SStephen Hemminger 
801317a76f9SStephen Hemminger 	char 		name[TCP_CA_NAME_MAX];
802317a76f9SStephen Hemminger 	struct module 	*owner;
803317a76f9SStephen Hemminger };
804317a76f9SStephen Hemminger 
805317a76f9SStephen Hemminger extern int tcp_register_congestion_control(struct tcp_congestion_ops *type);
806317a76f9SStephen Hemminger extern void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
807317a76f9SStephen Hemminger 
8086687e988SArnaldo Carvalho de Melo extern void tcp_init_congestion_control(struct sock *sk);
8096687e988SArnaldo Carvalho de Melo extern void tcp_cleanup_congestion_control(struct sock *sk);
810317a76f9SStephen Hemminger extern int tcp_set_default_congestion_control(const char *name);
811317a76f9SStephen Hemminger extern void tcp_get_default_congestion_control(char *name);
8123ff825b2SStephen Hemminger extern void tcp_get_available_congestion_control(char *buf, size_t len);
813ce7bc3bfSStephen Hemminger extern void tcp_get_allowed_congestion_control(char *buf, size_t len);
814ce7bc3bfSStephen Hemminger extern int tcp_set_allowed_congestion_control(char *allowed);
8156687e988SArnaldo Carvalho de Melo extern int tcp_set_congestion_control(struct sock *sk, const char *name);
81640efc6faSStephen Hemminger extern void tcp_slow_start(struct tcp_sock *tp);
817758ce5c8SIlpo Järvinen extern void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w);
818317a76f9SStephen Hemminger 
8195f8ef48dSStephen Hemminger extern struct tcp_congestion_ops tcp_init_congestion_ops;
8206687e988SArnaldo Carvalho de Melo extern u32 tcp_reno_ssthresh(struct sock *sk);
821c3a05c60SIlpo Järvinen extern void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight);
82272dc5b92SStephen Hemminger extern u32 tcp_reno_min_cwnd(const struct sock *sk);
823a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
824317a76f9SStephen Hemminger 
8256687e988SArnaldo Carvalho de Melo static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
826317a76f9SStephen Hemminger {
8276687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
8286687e988SArnaldo Carvalho de Melo 
8296687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->set_state)
8306687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->set_state(sk, ca_state);
8316687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_state = ca_state;
832317a76f9SStephen Hemminger }
833317a76f9SStephen Hemminger 
8346687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
835317a76f9SStephen Hemminger {
8366687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
8376687e988SArnaldo Carvalho de Melo 
8386687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
8396687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
840317a76f9SStephen Hemminger }
841317a76f9SStephen Hemminger 
842e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK
843e60402d0SIlpo Järvinen  * handling. SACK is negotiated with the peer, and therefore it can vary
844e60402d0SIlpo Järvinen  * between different flows.
845e60402d0SIlpo Järvinen  *
846e60402d0SIlpo Järvinen  * tcp_is_sack - SACK enabled
847e60402d0SIlpo Järvinen  * tcp_is_reno - No SACK
848e60402d0SIlpo Järvinen  * tcp_is_fack - FACK enabled, implies SACK enabled
849e60402d0SIlpo Järvinen  */
850e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp)
851e60402d0SIlpo Järvinen {
852e60402d0SIlpo Järvinen 	return tp->rx_opt.sack_ok;
853e60402d0SIlpo Järvinen }
854e60402d0SIlpo Järvinen 
855a2a385d6SEric Dumazet static inline bool tcp_is_reno(const struct tcp_sock *tp)
856e60402d0SIlpo Järvinen {
857e60402d0SIlpo Järvinen 	return !tcp_is_sack(tp);
858e60402d0SIlpo Järvinen }
859e60402d0SIlpo Järvinen 
860a2a385d6SEric Dumazet static inline bool tcp_is_fack(const struct tcp_sock *tp)
861e60402d0SIlpo Järvinen {
862ab56222aSVijay Subramanian 	return tp->rx_opt.sack_ok & TCP_FACK_ENABLED;
863e60402d0SIlpo Järvinen }
864e60402d0SIlpo Järvinen 
865e60402d0SIlpo Järvinen static inline void tcp_enable_fack(struct tcp_sock *tp)
866e60402d0SIlpo Järvinen {
867ab56222aSVijay Subramanian 	tp->rx_opt.sack_ok |= TCP_FACK_ENABLED;
868e60402d0SIlpo Järvinen }
869e60402d0SIlpo Järvinen 
870eed530b6SYuchung Cheng /* TCP early-retransmit (ER) is similar to but more conservative than
871eed530b6SYuchung Cheng  * the thin-dupack feature.  Enable ER only if thin-dupack is disabled.
872eed530b6SYuchung Cheng  */
873eed530b6SYuchung Cheng static inline void tcp_enable_early_retrans(struct tcp_sock *tp)
874eed530b6SYuchung Cheng {
875eed530b6SYuchung Cheng 	tp->do_early_retrans = sysctl_tcp_early_retrans &&
876eed530b6SYuchung Cheng 		!sysctl_tcp_thin_dupack && sysctl_tcp_reordering == 3;
877750ea2baSYuchung Cheng 	tp->early_retrans_delayed = 0;
878eed530b6SYuchung Cheng }
879eed530b6SYuchung Cheng 
880eed530b6SYuchung Cheng static inline void tcp_disable_early_retrans(struct tcp_sock *tp)
881eed530b6SYuchung Cheng {
882eed530b6SYuchung Cheng 	tp->do_early_retrans = 0;
883eed530b6SYuchung Cheng }
884eed530b6SYuchung Cheng 
88583ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
88683ae4088SIlpo Järvinen {
88783ae4088SIlpo Järvinen 	return tp->sacked_out + tp->lost_out;
88883ae4088SIlpo Järvinen }
88983ae4088SIlpo Järvinen 
8901da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
8911da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
8921da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
8931da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
8941da177e4SLinus Torvalds  *
8951da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
8961da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
8971da177e4SLinus Torvalds  *
8981da177e4SLinus Torvalds  * Read this equation as:
8991da177e4SLinus Torvalds  *
9001da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
9011da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
9021da177e4SLinus Torvalds  *	"Packets fast retransmitted"
9031da177e4SLinus Torvalds  */
90440efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
9051da177e4SLinus Torvalds {
90683ae4088SIlpo Järvinen 	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
9090b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH	0x7fffffff
9100b6a05c1SIlpo Järvinen 
9110b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
9120b6a05c1SIlpo Järvinen {
9130b6a05c1SIlpo Järvinen 	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
9140b6a05c1SIlpo Järvinen }
9150b6a05c1SIlpo Järvinen 
916684bad11SYuchung Cheng static inline bool tcp_in_cwnd_reduction(const struct sock *sk)
917684bad11SYuchung Cheng {
918684bad11SYuchung Cheng 	return (TCPF_CA_CWR | TCPF_CA_Recovery) &
919684bad11SYuchung Cheng 	       (1 << inet_csk(sk)->icsk_ca_state);
920684bad11SYuchung Cheng }
921684bad11SYuchung Cheng 
9221da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
923684bad11SYuchung Cheng  * The exception is cwnd reduction phase, when cwnd is decreasing towards
9241da177e4SLinus Torvalds  * ssthresh.
9251da177e4SLinus Torvalds  */
9266687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
9271da177e4SLinus Torvalds {
9286687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
929cf533ea5SEric Dumazet 
930684bad11SYuchung Cheng 	if (tcp_in_cwnd_reduction(sk))
9311da177e4SLinus Torvalds 		return tp->snd_ssthresh;
9321da177e4SLinus Torvalds 	else
9331da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
9341da177e4SLinus Torvalds 			   ((tp->snd_cwnd >> 1) +
9351da177e4SLinus Torvalds 			    (tp->snd_cwnd >> 2)));
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
938b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */
939b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
9401da177e4SLinus Torvalds 
9413cfe3baaSIlpo Järvinen extern void tcp_enter_cwr(struct sock *sk, const int set_ssthresh);
942cf533ea5SEric Dumazet extern __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
9431da177e4SLinus Torvalds 
9446b5a5c0dSNeal Cardwell /* The maximum number of MSS of available cwnd for which TSO defers
9456b5a5c0dSNeal Cardwell  * sending if not using sysctl_tcp_tso_win_divisor.
9466b5a5c0dSNeal Cardwell  */
9476b5a5c0dSNeal Cardwell static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
9486b5a5c0dSNeal Cardwell {
9496b5a5c0dSNeal Cardwell 	return 3;
9506b5a5c0dSNeal Cardwell }
9516b5a5c0dSNeal Cardwell 
9521da177e4SLinus Torvalds /* Slow start with delack produces 3 packets of burst, so that
953dd9e0ddaSJohn Heffner  * it is safe "de facto".  This will be the default - same as
954dd9e0ddaSJohn Heffner  * the default reordering threshold - but if reordering increases,
955dd9e0ddaSJohn Heffner  * we must be able to allow cwnd to burst at least this much in order
956dd9e0ddaSJohn Heffner  * to not pull it back when holes are filled.
9571da177e4SLinus Torvalds  */
9581da177e4SLinus Torvalds static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
9591da177e4SLinus Torvalds {
960dd9e0ddaSJohn Heffner 	return tp->reordering;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
96390840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */
96490840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
96590840defSIlpo Järvinen {
96690840defSIlpo Järvinen 	return tp->snd_una + tp->snd_wnd;
96790840defSIlpo Järvinen }
968a2a385d6SEric Dumazet extern bool tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight);
969f4805edeSStephen Hemminger 
970c1bd24b7SChuck Lever static inline void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss,
9711da177e4SLinus Torvalds 				       const struct sk_buff *skb)
9721da177e4SLinus Torvalds {
9731da177e4SLinus Torvalds 	if (skb->len < mss)
9741da177e4SLinus Torvalds 		tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9779e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk)
9781da177e4SLinus Torvalds {
979cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
980463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
9819e412ba7SIlpo Järvinen 
982463c84b9SArnaldo Carvalho de Melo 	if (!tp->packets_out && !icsk->icsk_pending)
9833f421baaSArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
9843f421baaSArnaldo Carvalho de Melo 					  icsk->icsk_rto, TCP_RTO_MAX);
9851da177e4SLinus Torvalds }
9861da177e4SLinus Torvalds 
987ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
9881da177e4SLinus Torvalds {
9891da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
9901da177e4SLinus Torvalds }
9911da177e4SLinus Torvalds 
992ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
9951da177e4SLinus Torvalds }
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds /*
9981da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
9991da177e4SLinus Torvalds  */
1000ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr,
1001ba7808eaSFrederik Deweerdt 				   __be32 daddr, __wsum base)
10021da177e4SLinus Torvalds {
10031da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
10041da177e4SLinus Torvalds }
10051da177e4SLinus Torvalds 
1006b51655b9SAl Viro static inline __sum16 __tcp_checksum_complete(struct sk_buff *skb)
10071da177e4SLinus Torvalds {
1008fb286bb2SHerbert Xu 	return __skb_checksum_complete(skb);
10091da177e4SLinus Torvalds }
10101da177e4SLinus Torvalds 
1011a2a385d6SEric Dumazet static inline bool tcp_checksum_complete(struct sk_buff *skb)
10121da177e4SLinus Torvalds {
101360476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
10141da177e4SLinus Torvalds 		__tcp_checksum_complete(skb);
10151da177e4SLinus Torvalds }
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds /* Prequeue for VJ style copy to user, combined with checksumming. */
10181da177e4SLinus Torvalds 
101940efc6faSStephen Hemminger static inline void tcp_prequeue_init(struct tcp_sock *tp)
10201da177e4SLinus Torvalds {
10211da177e4SLinus Torvalds 	tp->ucopy.task = NULL;
10221da177e4SLinus Torvalds 	tp->ucopy.len = 0;
10231da177e4SLinus Torvalds 	tp->ucopy.memory = 0;
10241da177e4SLinus Torvalds 	skb_queue_head_init(&tp->ucopy.prequeue);
102597fc2f08SChris Leech #ifdef CONFIG_NET_DMA
102697fc2f08SChris Leech 	tp->ucopy.dma_chan = NULL;
102797fc2f08SChris Leech 	tp->ucopy.wakeup = 0;
102897fc2f08SChris Leech 	tp->ucopy.pinned_list = NULL;
102997fc2f08SChris Leech 	tp->ucopy.dma_cookie = 0;
103097fc2f08SChris Leech #endif
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds /* Packet is added to VJ-style prequeue for processing in process
10341da177e4SLinus Torvalds  * context, if a reader task is waiting. Apparently, this exciting
10351da177e4SLinus Torvalds  * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93)
10361da177e4SLinus Torvalds  * failed somewhere. Latency? Burstiness? Well, at least now we will
10371da177e4SLinus Torvalds  * see, why it failed. 8)8)				  --ANK
10381da177e4SLinus Torvalds  *
10391da177e4SLinus Torvalds  * NOTE: is this not too big to inline?
10401da177e4SLinus Torvalds  */
1041a2a385d6SEric Dumazet static inline bool tcp_prequeue(struct sock *sk, struct sk_buff *skb)
10421da177e4SLinus Torvalds {
10431da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
10441da177e4SLinus Torvalds 
1045f5f8d86bSEric Dumazet 	if (sysctl_tcp_low_latency || !tp->ucopy.task)
1046a2a385d6SEric Dumazet 		return false;
1047f5f8d86bSEric Dumazet 
10481da177e4SLinus Torvalds 	__skb_queue_tail(&tp->ucopy.prequeue, skb);
10491da177e4SLinus Torvalds 	tp->ucopy.memory += skb->truesize;
10501da177e4SLinus Torvalds 	if (tp->ucopy.memory > sk->sk_rcvbuf) {
10511da177e4SLinus Torvalds 		struct sk_buff *skb1;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 		BUG_ON(sock_owned_by_user(sk));
10541da177e4SLinus Torvalds 
10551da177e4SLinus Torvalds 		while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) {
1056c57943a1SPeter Zijlstra 			sk_backlog_rcv(sk, skb1);
1057f5f8d86bSEric Dumazet 			NET_INC_STATS_BH(sock_net(sk),
1058f5f8d86bSEric Dumazet 					 LINUX_MIB_TCPPREQUEUEDROPPED);
10591da177e4SLinus Torvalds 		}
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 		tp->ucopy.memory = 0;
10621da177e4SLinus Torvalds 	} else if (skb_queue_len(&tp->ucopy.prequeue) == 1) {
1063aa395145SEric Dumazet 		wake_up_interruptible_sync_poll(sk_sleep(sk),
10647aedec2aSEric Dumazet 					   POLLIN | POLLRDNORM | POLLRDBAND);
1065463c84b9SArnaldo Carvalho de Melo 		if (!inet_csk_ack_scheduled(sk))
1066463c84b9SArnaldo Carvalho de Melo 			inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
10670c266898SSatoru SATOH 						  (3 * tcp_rto_min(sk)) / 4,
10683f421baaSArnaldo Carvalho de Melo 						  TCP_RTO_MAX);
10691da177e4SLinus Torvalds 	}
1070a2a385d6SEric Dumazet 	return true;
10711da177e4SLinus Torvalds }
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds #undef STATE_TRACE
10751da177e4SLinus Torvalds 
10761da177e4SLinus Torvalds #ifdef STATE_TRACE
10771da177e4SLinus Torvalds static const char *statename[]={
10781da177e4SLinus Torvalds 	"Unused","Established","Syn Sent","Syn Recv",
10791da177e4SLinus Torvalds 	"Fin Wait 1","Fin Wait 2","Time Wait", "Close",
10801da177e4SLinus Torvalds 	"Close Wait","Last ACK","Listen","Closing"
10811da177e4SLinus Torvalds };
10821da177e4SLinus Torvalds #endif
1083490d5046SIlpo Järvinen extern void tcp_set_state(struct sock *sk, int state);
10841da177e4SLinus Torvalds 
10854ac02babSAndi Kleen extern void tcp_done(struct sock *sk);
10861da177e4SLinus Torvalds 
108740efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
10881da177e4SLinus Torvalds {
10891da177e4SLinus Torvalds 	rx_opt->dsack = 0;
10901da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
10911da177e4SLinus Torvalds }
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
10941da177e4SLinus Torvalds extern void tcp_select_initial_window(int __space, __u32 mss,
10951da177e4SLinus Torvalds 				      __u32 *rcv_wnd, __u32 *window_clamp,
109631d12926Slaurent chavey 				      int wscale_ok, __u8 *rcv_wscale,
109731d12926Slaurent chavey 				      __u32 init_rcv_wnd);
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds static inline int tcp_win_from_space(int space)
11001da177e4SLinus Torvalds {
11011da177e4SLinus Torvalds 	return sysctl_tcp_adv_win_scale<=0 ?
11021da177e4SLinus Torvalds 		(space>>(-sysctl_tcp_adv_win_scale)) :
11031da177e4SLinus Torvalds 		space - (space>>sysctl_tcp_adv_win_scale);
11041da177e4SLinus Torvalds }
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
11071da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
11081da177e4SLinus Torvalds {
11091da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf -
11101da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
11111da177e4SLinus Torvalds }
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
11141da177e4SLinus Torvalds {
11151da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf);
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
111840efc6faSStephen Hemminger static inline void tcp_openreq_init(struct request_sock *req,
11191da177e4SLinus Torvalds 				    struct tcp_options_received *rx_opt,
11201da177e4SLinus Torvalds 				    struct sk_buff *skb)
11211da177e4SLinus Torvalds {
11222e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq = inet_rsk(req);
11232e6599cbSArnaldo Carvalho de Melo 
11241da177e4SLinus Torvalds 	req->rcv_wnd = 0;		/* So that tcp_send_synack() knows! */
11254dfc2817SFlorian Westphal 	req->cookie_ts = 0;
11262e6599cbSArnaldo Carvalho de Melo 	tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
112710467163SJerry Chu 	tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
1128*016818d0SNeal Cardwell 	tcp_rsk(req)->snt_synack = 0;
11291da177e4SLinus Torvalds 	req->mss = rx_opt->mss_clamp;
11301da177e4SLinus Torvalds 	req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
11312e6599cbSArnaldo Carvalho de Melo 	ireq->tstamp_ok = rx_opt->tstamp_ok;
11322e6599cbSArnaldo Carvalho de Melo 	ireq->sack_ok = rx_opt->sack_ok;
11332e6599cbSArnaldo Carvalho de Melo 	ireq->snd_wscale = rx_opt->snd_wscale;
11342e6599cbSArnaldo Carvalho de Melo 	ireq->wscale_ok = rx_opt->wscale_ok;
11352e6599cbSArnaldo Carvalho de Melo 	ireq->acked = 0;
11362e6599cbSArnaldo Carvalho de Melo 	ireq->ecn_ok = 0;
1137aa8223c7SArnaldo Carvalho de Melo 	ireq->rmt_port = tcp_hdr(skb)->source;
1138a3116ac5SKOVACS Krisztian 	ireq->loc_port = tcp_hdr(skb)->dest;
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds 
1141623df484SNeal Cardwell /* Compute time elapsed between SYNACK and the ACK completing 3WHS */
1142623df484SNeal Cardwell static inline void tcp_synack_rtt_meas(struct sock *sk,
1143623df484SNeal Cardwell 				       struct request_sock *req)
1144623df484SNeal Cardwell {
1145623df484SNeal Cardwell 	if (tcp_rsk(req)->snt_synack)
1146623df484SNeal Cardwell 		tcp_valid_rtt_meas(sk,
1147623df484SNeal Cardwell 		    tcp_time_stamp - tcp_rsk(req)->snt_synack);
1148623df484SNeal Cardwell }
1149623df484SNeal Cardwell 
11505c52ba17SPavel Emelyanov extern void tcp_enter_memory_pressure(struct sock *sk);
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
11531da177e4SLinus Torvalds {
11541da177e4SLinus Torvalds 	return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
11551da177e4SLinus Torvalds }
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
11601da177e4SLinus Torvalds }
11611da177e4SLinus Torvalds 
1162df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp)
1163df19a626SEric Dumazet {
1164df19a626SEric Dumazet 	return tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
1165df19a626SEric Dumazet }
1166df19a626SEric Dumazet 
11676c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
11686c37e5deSFlavio Leitner {
11696c37e5deSFlavio Leitner 	const struct inet_connection_sock *icsk = &tp->inet_conn;
11706c37e5deSFlavio Leitner 
11716c37e5deSFlavio Leitner 	return min_t(u32, tcp_time_stamp - icsk->icsk_ack.lrcvtime,
11726c37e5deSFlavio Leitner 			  tcp_time_stamp - tp->rcv_tstamp);
11736c37e5deSFlavio Leitner }
11746c37e5deSFlavio Leitner 
1175463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
11761da177e4SLinus Torvalds {
1177463c84b9SArnaldo Carvalho de Melo 	int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
1178463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
11791da177e4SLinus Torvalds 
1180463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1181463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds 	return fin_timeout;
11841da177e4SLinus Torvalds }
11851da177e4SLinus Torvalds 
1186a2a385d6SEric Dumazet static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
1187c887e6d2SIlpo Järvinen 				  int paws_win)
11881da177e4SLinus Torvalds {
1189c887e6d2SIlpo Järvinen 	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1190a2a385d6SEric Dumazet 		return true;
1191c887e6d2SIlpo Järvinen 	if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS))
1192a2a385d6SEric Dumazet 		return true;
1193bc2ce894SEric Dumazet 	/*
1194bc2ce894SEric Dumazet 	 * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
1195bc2ce894SEric Dumazet 	 * then following tcp messages have valid values. Ignore 0 value,
1196bc2ce894SEric Dumazet 	 * or else 'negative' tsval might forbid us to accept their packets.
1197bc2ce894SEric Dumazet 	 */
1198bc2ce894SEric Dumazet 	if (!rx_opt->ts_recent)
1199a2a385d6SEric Dumazet 		return true;
1200a2a385d6SEric Dumazet 	return false;
1201c887e6d2SIlpo Järvinen }
1202c887e6d2SIlpo Järvinen 
1203a2a385d6SEric Dumazet static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
1204c887e6d2SIlpo Järvinen 				   int rst)
1205c887e6d2SIlpo Järvinen {
1206c887e6d2SIlpo Järvinen 	if (tcp_paws_check(rx_opt, 0))
1207a2a385d6SEric Dumazet 		return false;
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
12101da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
12111da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
12121da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
12131da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
12141da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
12151da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
12161da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
12171da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
12201da177e4SLinus Torvalds 	 */
12219d729f72SJames Morris 	if (rst && get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
1222a2a385d6SEric Dumazet 		return false;
1223a2a385d6SEric Dumazet 	return true;
12241da177e4SLinus Torvalds }
12251da177e4SLinus Torvalds 
1226a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net)
12271da177e4SLinus Torvalds {
12281da177e4SLinus Torvalds 	/* See RFC 2012 */
1229cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOALGORITHM, 1);
1230cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
1231cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
1232cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_MAXCONN, -1);
12331da177e4SLinus Torvalds }
12341da177e4SLinus Torvalds 
12356a438bbeSStephen Hemminger /* from STCP */
1236ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
12370800f170SDavid S. Miller {
12386a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
12396a438bbeSStephen Hemminger 	tp->scoreboard_skb_hint = NULL;
1240ef9da47cSIlpo Järvinen }
1241ef9da47cSIlpo Järvinen 
1242ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
1243ef9da47cSIlpo Järvinen {
1244ef9da47cSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
12456a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
1246b7689205SIlpo Järvinen }
1247b7689205SIlpo Järvinen 
1248cfb6eeb4SYOSHIFUJI Hideaki /* MD5 Signature */
1249cfb6eeb4SYOSHIFUJI Hideaki struct crypto_hash;
1250cfb6eeb4SYOSHIFUJI Hideaki 
1251a915da9bSEric Dumazet union tcp_md5_addr {
1252a915da9bSEric Dumazet 	struct in_addr  a4;
1253a915da9bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1254a915da9bSEric Dumazet 	struct in6_addr	a6;
1255a915da9bSEric Dumazet #endif
1256a915da9bSEric Dumazet };
1257a915da9bSEric Dumazet 
1258cfb6eeb4SYOSHIFUJI Hideaki /* - key database */
1259cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key {
1260a915da9bSEric Dumazet 	struct hlist_node	node;
1261cfb6eeb4SYOSHIFUJI Hideaki 	u8			keylen;
1262a915da9bSEric Dumazet 	u8			family; /* AF_INET or AF_INET6 */
1263a915da9bSEric Dumazet 	union tcp_md5_addr	addr;
1264a915da9bSEric Dumazet 	u8			key[TCP_MD5SIG_MAXKEYLEN];
1265a915da9bSEric Dumazet 	struct rcu_head		rcu;
1266cfb6eeb4SYOSHIFUJI Hideaki };
1267cfb6eeb4SYOSHIFUJI Hideaki 
1268cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */
1269cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info {
1270a915da9bSEric Dumazet 	struct hlist_head	head;
1271a8afca03SEric Dumazet 	struct rcu_head		rcu;
1272cfb6eeb4SYOSHIFUJI Hideaki };
1273cfb6eeb4SYOSHIFUJI Hideaki 
1274cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */
1275cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr {
1276cfb6eeb4SYOSHIFUJI Hideaki 	__be32		saddr;
1277cfb6eeb4SYOSHIFUJI Hideaki 	__be32		daddr;
1278cfb6eeb4SYOSHIFUJI Hideaki 	__u8		pad;
1279cfb6eeb4SYOSHIFUJI Hideaki 	__u8		protocol;
1280cfb6eeb4SYOSHIFUJI Hideaki 	__be16		len;
1281cfb6eeb4SYOSHIFUJI Hideaki };
1282cfb6eeb4SYOSHIFUJI Hideaki 
1283cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr {
1284cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr	saddr;
1285cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr daddr;
1286cfb6eeb4SYOSHIFUJI Hideaki 	__be32		len;
1287cfb6eeb4SYOSHIFUJI Hideaki 	__be32		protocol;	/* including padding */
1288cfb6eeb4SYOSHIFUJI Hideaki };
1289cfb6eeb4SYOSHIFUJI Hideaki 
1290cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block {
1291cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr ip4;
1292dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1293cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_pseudohdr ip6;
1294cfb6eeb4SYOSHIFUJI Hideaki #endif
1295cfb6eeb4SYOSHIFUJI Hideaki };
1296cfb6eeb4SYOSHIFUJI Hideaki 
1297cfb6eeb4SYOSHIFUJI Hideaki /* - pool: digest algorithm, hash description and scratch buffer */
1298cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool {
1299cfb6eeb4SYOSHIFUJI Hideaki 	struct hash_desc	md5_desc;
1300cfb6eeb4SYOSHIFUJI Hideaki 	union tcp_md5sum_block	md5_blk;
1301cfb6eeb4SYOSHIFUJI Hideaki };
1302cfb6eeb4SYOSHIFUJI Hideaki 
1303cfb6eeb4SYOSHIFUJI Hideaki /* - functions */
130453d3176bSChangli Gao extern int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
1305318cf7aaSEric Dumazet 			       const struct sock *sk,
1306318cf7aaSEric Dumazet 			       const struct request_sock *req,
1307318cf7aaSEric Dumazet 			       const struct sk_buff *skb);
1308a915da9bSEric Dumazet extern int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
1309a915da9bSEric Dumazet 			  int family, const u8 *newkey,
1310a915da9bSEric Dumazet 			  u8 newkeylen, gfp_t gfp);
1311a915da9bSEric Dumazet extern int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
1312a915da9bSEric Dumazet 			  int family);
1313cfb6eeb4SYOSHIFUJI Hideaki extern struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
1314cfb6eeb4SYOSHIFUJI Hideaki 					 struct sock *addr_sk);
1315cfb6eeb4SYOSHIFUJI Hideaki 
13169501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1317a915da9bSEric Dumazet extern struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
1318a915da9bSEric Dumazet 			const union tcp_md5_addr *addr, int family);
1319a915da9bSEric Dumazet #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_key)
13209501f972SYOSHIFUJI Hideaki #else
1321a915da9bSEric Dumazet static inline struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
1322a915da9bSEric Dumazet 					 const union tcp_md5_addr *addr,
1323a915da9bSEric Dumazet 					 int family)
1324a915da9bSEric Dumazet {
1325a915da9bSEric Dumazet 	return NULL;
1326a915da9bSEric Dumazet }
13279501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	NULL
13289501f972SYOSHIFUJI Hideaki #endif
13299501f972SYOSHIFUJI Hideaki 
1330765cf997SEric Dumazet extern struct tcp_md5sig_pool __percpu *tcp_alloc_md5sig_pool(struct sock *);
1331cfb6eeb4SYOSHIFUJI Hideaki extern void tcp_free_md5sig_pool(void);
1332cfb6eeb4SYOSHIFUJI Hideaki 
133335790c04SEric Dumazet extern struct tcp_md5sig_pool	*tcp_get_md5sig_pool(void);
133435790c04SEric Dumazet extern void tcp_put_md5sig_pool(void);
133535790c04SEric Dumazet 
1336ca35a0efSEric Dumazet extern int tcp_md5_hash_header(struct tcp_md5sig_pool *, const struct tcphdr *);
1337cf533ea5SEric Dumazet extern int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
133895c96174SEric Dumazet 				 unsigned int header_len);
133949a72dfbSAdam Langley extern int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
1340cf533ea5SEric Dumazet 			    const struct tcp_md5sig_key *key);
1341cfb6eeb4SYOSHIFUJI Hideaki 
134210467163SJerry Chu /* From tcp_fastopen.c */
134310467163SJerry Chu extern void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
134410467163SJerry Chu 				   struct tcp_fastopen_cookie *cookie,
134510467163SJerry Chu 				   int *syn_loss, unsigned long *last_syn_loss);
134610467163SJerry Chu extern void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
134710467163SJerry Chu 				   struct tcp_fastopen_cookie *cookie,
134810467163SJerry Chu 				   bool syn_lost);
1349783237e8SYuchung Cheng struct tcp_fastopen_request {
1350783237e8SYuchung Cheng 	/* Fast Open cookie. Size 0 means a cookie request */
1351783237e8SYuchung Cheng 	struct tcp_fastopen_cookie	cookie;
1352783237e8SYuchung Cheng 	struct msghdr			*data;  /* data in MSG_FASTOPEN */
1353783237e8SYuchung Cheng 	u16				copied;	/* queued in tcp_connect() */
1354783237e8SYuchung Cheng };
1355783237e8SYuchung Cheng void tcp_free_fastopen_req(struct tcp_sock *tp);
1356783237e8SYuchung Cheng 
135710467163SJerry Chu extern struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
135810467163SJerry Chu int tcp_fastopen_reset_cipher(void *key, unsigned int len);
135910467163SJerry Chu void tcp_fastopen_cookie_gen(__be32 addr, struct tcp_fastopen_cookie *foc);
136010467163SJerry Chu 
136110467163SJerry Chu #define TCP_FASTOPEN_KEY_LENGTH 16
136210467163SJerry Chu 
136310467163SJerry Chu /* Fastopen key context */
136410467163SJerry Chu struct tcp_fastopen_context {
136510467163SJerry Chu 	struct crypto_cipher __rcu	*tfm;
136610467163SJerry Chu 	__u8				key[TCP_FASTOPEN_KEY_LENGTH];
136710467163SJerry Chu 	struct rcu_head			rcu;
136810467163SJerry Chu };
136910467163SJerry Chu 
1370fe067e8aSDavid S. Miller /* write queue abstraction */
1371fe067e8aSDavid S. Miller static inline void tcp_write_queue_purge(struct sock *sk)
1372fe067e8aSDavid S. Miller {
1373fe067e8aSDavid S. Miller 	struct sk_buff *skb;
1374fe067e8aSDavid S. Miller 
1375fe067e8aSDavid S. Miller 	while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL)
13763ab224beSHideo Aoki 		sk_wmem_free_skb(sk, skb);
13773ab224beSHideo Aoki 	sk_mem_reclaim(sk);
13788818a9d8SIlpo Järvinen 	tcp_clear_all_retrans_hints(tcp_sk(sk));
1379fe067e8aSDavid S. Miller }
1380fe067e8aSDavid S. Miller 
1381cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
1382fe067e8aSDavid S. Miller {
1383cd07a8eaSDavid S. Miller 	return skb_peek(&sk->sk_write_queue);
1384fe067e8aSDavid S. Miller }
1385fe067e8aSDavid S. Miller 
1386cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
1387fe067e8aSDavid S. Miller {
1388cd07a8eaSDavid S. Miller 	return skb_peek_tail(&sk->sk_write_queue);
1389fe067e8aSDavid S. Miller }
1390fe067e8aSDavid S. Miller 
1391cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_next(const struct sock *sk,
1392cf533ea5SEric Dumazet 						   const struct sk_buff *skb)
1393fe067e8aSDavid S. Miller {
1394cd07a8eaSDavid S. Miller 	return skb_queue_next(&sk->sk_write_queue, skb);
1395fe067e8aSDavid S. Miller }
1396fe067e8aSDavid S. Miller 
1397cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_prev(const struct sock *sk,
1398cf533ea5SEric Dumazet 						   const struct sk_buff *skb)
1399832d11c5SIlpo Järvinen {
1400832d11c5SIlpo Järvinen 	return skb_queue_prev(&sk->sk_write_queue, skb);
1401832d11c5SIlpo Järvinen }
1402832d11c5SIlpo Järvinen 
1403fe067e8aSDavid S. Miller #define tcp_for_write_queue(skb, sk)					\
1404cd07a8eaSDavid S. Miller 	skb_queue_walk(&(sk)->sk_write_queue, skb)
1405fe067e8aSDavid S. Miller 
1406fe067e8aSDavid S. Miller #define tcp_for_write_queue_from(skb, sk)				\
1407cd07a8eaSDavid S. Miller 	skb_queue_walk_from(&(sk)->sk_write_queue, skb)
1408fe067e8aSDavid S. Miller 
1409234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
1410cd07a8eaSDavid S. Miller 	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1411234b6860SIlpo Järvinen 
1412cf533ea5SEric Dumazet static inline struct sk_buff *tcp_send_head(const struct sock *sk)
1413fe067e8aSDavid S. Miller {
1414fe067e8aSDavid S. Miller 	return sk->sk_send_head;
1415fe067e8aSDavid S. Miller }
1416fe067e8aSDavid S. Miller 
1417cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk,
1418cd07a8eaSDavid S. Miller 				   const struct sk_buff *skb)
1419cd07a8eaSDavid S. Miller {
1420cd07a8eaSDavid S. Miller 	return skb_queue_is_last(&sk->sk_write_queue, skb);
1421cd07a8eaSDavid S. Miller }
1422cd07a8eaSDavid S. Miller 
1423cf533ea5SEric Dumazet static inline void tcp_advance_send_head(struct sock *sk, const struct sk_buff *skb)
1424fe067e8aSDavid S. Miller {
1425cd07a8eaSDavid S. Miller 	if (tcp_skb_is_last(sk, skb))
1426fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1427cd07a8eaSDavid S. Miller 	else
1428cd07a8eaSDavid S. Miller 		sk->sk_send_head = tcp_write_queue_next(sk, skb);
1429fe067e8aSDavid S. Miller }
1430fe067e8aSDavid S. Miller 
1431fe067e8aSDavid S. Miller static inline void tcp_check_send_head(struct sock *sk, struct sk_buff *skb_unlinked)
1432fe067e8aSDavid S. Miller {
1433fe067e8aSDavid S. Miller 	if (sk->sk_send_head == skb_unlinked)
1434fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1435fe067e8aSDavid S. Miller }
1436fe067e8aSDavid S. Miller 
1437fe067e8aSDavid S. Miller static inline void tcp_init_send_head(struct sock *sk)
1438fe067e8aSDavid S. Miller {
1439fe067e8aSDavid S. Miller 	sk->sk_send_head = NULL;
1440fe067e8aSDavid S. Miller }
1441fe067e8aSDavid S. Miller 
1442fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1443fe067e8aSDavid S. Miller {
1444fe067e8aSDavid S. Miller 	__skb_queue_tail(&sk->sk_write_queue, skb);
1445fe067e8aSDavid S. Miller }
1446fe067e8aSDavid S. Miller 
1447fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1448fe067e8aSDavid S. Miller {
1449fe067e8aSDavid S. Miller 	__tcp_add_write_queue_tail(sk, skb);
1450fe067e8aSDavid S. Miller 
1451fe067e8aSDavid S. Miller 	/* Queue it, remembering where we must start sending. */
14526859d494SIlpo Järvinen 	if (sk->sk_send_head == NULL) {
1453fe067e8aSDavid S. Miller 		sk->sk_send_head = skb;
14546859d494SIlpo Järvinen 
14556859d494SIlpo Järvinen 		if (tcp_sk(sk)->highest_sack == NULL)
14566859d494SIlpo Järvinen 			tcp_sk(sk)->highest_sack = skb;
14576859d494SIlpo Järvinen 	}
1458fe067e8aSDavid S. Miller }
1459fe067e8aSDavid S. Miller 
1460fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_head(struct sock *sk, struct sk_buff *skb)
1461fe067e8aSDavid S. Miller {
1462fe067e8aSDavid S. Miller 	__skb_queue_head(&sk->sk_write_queue, skb);
1463fe067e8aSDavid S. Miller }
1464fe067e8aSDavid S. Miller 
1465fe067e8aSDavid S. Miller /* Insert buff after skb on the write queue of sk.  */
1466fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_after(struct sk_buff *skb,
1467fe067e8aSDavid S. Miller 						struct sk_buff *buff,
1468fe067e8aSDavid S. Miller 						struct sock *sk)
1469fe067e8aSDavid S. Miller {
14707de6c033SGerrit Renker 	__skb_queue_after(&sk->sk_write_queue, skb, buff);
1471fe067e8aSDavid S. Miller }
1472fe067e8aSDavid S. Miller 
147343f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk.  */
1474fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new,
1475fe067e8aSDavid S. Miller 						  struct sk_buff *skb,
1476fe067e8aSDavid S. Miller 						  struct sock *sk)
1477fe067e8aSDavid S. Miller {
147843f59c89SDavid S. Miller 	__skb_queue_before(&sk->sk_write_queue, skb, new);
14796e421410SIlpo Järvinen 
14806e421410SIlpo Järvinen 	if (sk->sk_send_head == skb)
14816e421410SIlpo Järvinen 		sk->sk_send_head = new;
1482fe067e8aSDavid S. Miller }
1483fe067e8aSDavid S. Miller 
1484fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
1485fe067e8aSDavid S. Miller {
1486fe067e8aSDavid S. Miller 	__skb_unlink(skb, &sk->sk_write_queue);
1487fe067e8aSDavid S. Miller }
1488fe067e8aSDavid S. Miller 
1489a2a385d6SEric Dumazet static inline bool tcp_write_queue_empty(struct sock *sk)
1490fe067e8aSDavid S. Miller {
1491fe067e8aSDavid S. Miller 	return skb_queue_empty(&sk->sk_write_queue);
1492fe067e8aSDavid S. Miller }
1493fe067e8aSDavid S. Miller 
149412d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk)
149512d50c46SKrishna Kumar {
149612d50c46SKrishna Kumar 	if (tcp_send_head(sk)) {
149712d50c46SKrishna Kumar 		struct tcp_sock *tp = tcp_sk(sk);
149812d50c46SKrishna Kumar 
149912d50c46SKrishna Kumar 		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
150012d50c46SKrishna Kumar 	}
150112d50c46SKrishna Kumar }
150212d50c46SKrishna Kumar 
1503ecb97192SNeal Cardwell /* Start sequence of the skb just after the highest skb with SACKed
1504ecb97192SNeal Cardwell  * bit, valid only if sacked_out > 0 or when the caller has ensured
1505ecb97192SNeal Cardwell  * validity by itself.
1506a47e5a98SIlpo Järvinen  */
1507a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
1508a47e5a98SIlpo Järvinen {
1509a47e5a98SIlpo Järvinen 	if (!tp->sacked_out)
1510a47e5a98SIlpo Järvinen 		return tp->snd_una;
15116859d494SIlpo Järvinen 
15126859d494SIlpo Järvinen 	if (tp->highest_sack == NULL)
15136859d494SIlpo Järvinen 		return tp->snd_nxt;
15146859d494SIlpo Järvinen 
1515a47e5a98SIlpo Järvinen 	return TCP_SKB_CB(tp->highest_sack)->seq;
1516a47e5a98SIlpo Järvinen }
1517a47e5a98SIlpo Järvinen 
15186859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
15196859d494SIlpo Järvinen {
15206859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_skb_is_last(sk, skb) ? NULL :
15216859d494SIlpo Järvinen 						tcp_write_queue_next(sk, skb);
15226859d494SIlpo Järvinen }
15236859d494SIlpo Järvinen 
15246859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
15256859d494SIlpo Järvinen {
15266859d494SIlpo Järvinen 	return tcp_sk(sk)->highest_sack;
15276859d494SIlpo Järvinen }
15286859d494SIlpo Järvinen 
15296859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk)
15306859d494SIlpo Järvinen {
15316859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_write_queue_head(sk);
15326859d494SIlpo Järvinen }
15336859d494SIlpo Järvinen 
15346859d494SIlpo Järvinen /* Called when old skb is about to be deleted (to be combined with new skb) */
15356859d494SIlpo Järvinen static inline void tcp_highest_sack_combine(struct sock *sk,
15366859d494SIlpo Järvinen 					    struct sk_buff *old,
15376859d494SIlpo Järvinen 					    struct sk_buff *new)
15386859d494SIlpo Järvinen {
15396859d494SIlpo Järvinen 	if (tcp_sk(sk)->sacked_out && (old == tcp_sk(sk)->highest_sack))
15406859d494SIlpo Järvinen 		tcp_sk(sk)->highest_sack = new;
15416859d494SIlpo Järvinen }
15426859d494SIlpo Järvinen 
15435aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from
15445aa4b32fSAndreas Petlund  * increased latency). Used to trigger latency-reducing mechanisms.
15455aa4b32fSAndreas Petlund  */
1546a2a385d6SEric Dumazet static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
15475aa4b32fSAndreas Petlund {
15485aa4b32fSAndreas Petlund 	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
15495aa4b32fSAndreas Petlund }
15505aa4b32fSAndreas Petlund 
15511da177e4SLinus Torvalds /* /proc */
15521da177e4SLinus Torvalds enum tcp_seq_states {
15531da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
15541da177e4SLinus Torvalds 	TCP_SEQ_STATE_OPENREQ,
15551da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
15561da177e4SLinus Torvalds 	TCP_SEQ_STATE_TIME_WAIT,
15571da177e4SLinus Torvalds };
15581da177e4SLinus Torvalds 
155973cb88ecSArjan van de Ven int tcp_seq_open(struct inode *inode, struct file *file);
156073cb88ecSArjan van de Ven 
15611da177e4SLinus Torvalds struct tcp_seq_afinfo {
15621da177e4SLinus Torvalds 	char				*name;
15631da177e4SLinus Torvalds 	sa_family_t			family;
156473cb88ecSArjan van de Ven 	const struct file_operations	*seq_fops;
15659427c4b3SDenis V. Lunev 	struct seq_operations		seq_ops;
15661da177e4SLinus Torvalds };
15671da177e4SLinus Torvalds 
15681da177e4SLinus Torvalds struct tcp_iter_state {
1569a4146b1bSDenis V. Lunev 	struct seq_net_private	p;
15701da177e4SLinus Torvalds 	sa_family_t		family;
15711da177e4SLinus Torvalds 	enum tcp_seq_states	state;
15721da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
1573a7cb5a49SEric W. Biederman 	int			bucket, offset, sbucket, num;
1574a7cb5a49SEric W. Biederman 	kuid_t			uid;
1575a8b690f9STom Herbert 	loff_t			last_pos;
15761da177e4SLinus Torvalds };
15771da177e4SLinus Torvalds 
15786f8b13bcSDaniel Lezcano extern int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo);
15796f8b13bcSDaniel Lezcano extern void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo);
15801da177e4SLinus Torvalds 
158120380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
1582c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops;
158320380731SArnaldo Carvalho de Melo 
15847d06b2e0SBrian Haley extern void tcp_v4_destroy_sock(struct sock *sk);
158520380731SArnaldo Carvalho de Melo 
1586a430a43dSHerbert Xu extern int tcp_v4_gso_send_check(struct sk_buff *skb);
1587c8f44affSMichał Mirosław extern struct sk_buff *tcp_tso_segment(struct sk_buff *skb,
1588c8f44affSMichał Mirosław 				       netdev_features_t features);
1589bf296b12SHerbert Xu extern struct sk_buff **tcp_gro_receive(struct sk_buff **head,
1590bf296b12SHerbert Xu 					struct sk_buff *skb);
1591bf296b12SHerbert Xu extern struct sk_buff **tcp4_gro_receive(struct sk_buff **head,
1592bf296b12SHerbert Xu 					 struct sk_buff *skb);
1593bf296b12SHerbert Xu extern int tcp_gro_complete(struct sk_buff *skb);
1594bf296b12SHerbert Xu extern int tcp4_gro_complete(struct sk_buff *skb);
1595f4c50d99SHerbert Xu 
159620380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
159720380731SArnaldo Carvalho de Melo extern int tcp4_proc_init(void);
159820380731SArnaldo Carvalho de Melo extern void tcp4_proc_exit(void);
159920380731SArnaldo Carvalho de Melo #endif
160020380731SArnaldo Carvalho de Melo 
1601cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */
1602cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops {
1603cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1604cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key	*(*md5_lookup) (struct sock *sk,
1605cfb6eeb4SYOSHIFUJI Hideaki 						struct sock *addr_sk);
1606cfb6eeb4SYOSHIFUJI Hideaki 	int			(*calc_md5_hash) (char *location,
1607cfb6eeb4SYOSHIFUJI Hideaki 						  struct tcp_md5sig_key *md5,
1608318cf7aaSEric Dumazet 						  const struct sock *sk,
1609318cf7aaSEric Dumazet 						  const struct request_sock *req,
1610318cf7aaSEric Dumazet 						  const struct sk_buff *skb);
1611cfb6eeb4SYOSHIFUJI Hideaki 	int			(*md5_parse) (struct sock *sk,
1612cfb6eeb4SYOSHIFUJI Hideaki 					      char __user *optval,
1613cfb6eeb4SYOSHIFUJI Hideaki 					      int optlen);
1614cfb6eeb4SYOSHIFUJI Hideaki #endif
1615cfb6eeb4SYOSHIFUJI Hideaki };
1616cfb6eeb4SYOSHIFUJI Hideaki 
1617cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops {
1618cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1619cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key	*(*md5_lookup) (struct sock *sk,
1620cfb6eeb4SYOSHIFUJI Hideaki 						struct request_sock *req);
1621e3afe7b7SJohn Dykstra 	int			(*calc_md5_hash) (char *location,
1622e3afe7b7SJohn Dykstra 						  struct tcp_md5sig_key *md5,
1623318cf7aaSEric Dumazet 						  const struct sock *sk,
1624318cf7aaSEric Dumazet 						  const struct request_sock *req,
1625318cf7aaSEric Dumazet 						  const struct sk_buff *skb);
1626cfb6eeb4SYOSHIFUJI Hideaki #endif
1627cfb6eeb4SYOSHIFUJI Hideaki };
1628cfb6eeb4SYOSHIFUJI Hideaki 
1629da5c78c8SWilliam Allen Simpson /* Using SHA1 for now, define some constants.
1630da5c78c8SWilliam Allen Simpson  */
1631da5c78c8SWilliam Allen Simpson #define COOKIE_DIGEST_WORDS (SHA_DIGEST_WORDS)
1632da5c78c8SWilliam Allen Simpson #define COOKIE_MESSAGE_WORDS (SHA_MESSAGE_BYTES / 4)
1633da5c78c8SWilliam Allen Simpson #define COOKIE_WORKSPACE_WORDS (COOKIE_DIGEST_WORDS + COOKIE_MESSAGE_WORDS)
1634da5c78c8SWilliam Allen Simpson 
1635da5c78c8SWilliam Allen Simpson extern int tcp_cookie_generator(u32 *bakery);
1636da5c78c8SWilliam Allen Simpson 
1637435cf559SWilliam Allen Simpson /**
1638435cf559SWilliam Allen Simpson  *	struct tcp_cookie_values - each socket needs extra space for the
1639435cf559SWilliam Allen Simpson  *	cookies, together with (optional) space for any SYN data.
1640435cf559SWilliam Allen Simpson  *
1641435cf559SWilliam Allen Simpson  *	A tcp_sock contains a pointer to the current value, and this is
1642435cf559SWilliam Allen Simpson  *	cloned to the tcp_timewait_sock.
1643435cf559SWilliam Allen Simpson  *
1644435cf559SWilliam Allen Simpson  * @cookie_pair:	variable data from the option exchange.
1645435cf559SWilliam Allen Simpson  *
1646435cf559SWilliam Allen Simpson  * @cookie_desired:	user specified tcpct_cookie_desired.  Zero
1647435cf559SWilliam Allen Simpson  *			indicates default (sysctl_tcp_cookie_size).
1648435cf559SWilliam Allen Simpson  *			After cookie sent, remembers size of cookie.
1649435cf559SWilliam Allen Simpson  *			Range 0, TCP_COOKIE_MIN to TCP_COOKIE_MAX.
1650435cf559SWilliam Allen Simpson  *
1651435cf559SWilliam Allen Simpson  * @s_data_desired:	user specified tcpct_s_data_desired.  When the
1652435cf559SWilliam Allen Simpson  *			constant payload is specified (@s_data_constant),
1653435cf559SWilliam Allen Simpson  *			holds its length instead.
1654435cf559SWilliam Allen Simpson  *			Range 0 to TCP_MSS_DESIRED.
1655435cf559SWilliam Allen Simpson  *
1656435cf559SWilliam Allen Simpson  * @s_data_payload:	constant data that is to be included in the
1657435cf559SWilliam Allen Simpson  *			payload of SYN or SYNACK segments when the
1658435cf559SWilliam Allen Simpson  *			cookie option is present.
1659435cf559SWilliam Allen Simpson  */
1660435cf559SWilliam Allen Simpson struct tcp_cookie_values {
1661435cf559SWilliam Allen Simpson 	struct kref	kref;
1662435cf559SWilliam Allen Simpson 	u8		cookie_pair[TCP_COOKIE_PAIR_SIZE];
1663435cf559SWilliam Allen Simpson 	u8		cookie_pair_size;
1664435cf559SWilliam Allen Simpson 	u8		cookie_desired;
1665435cf559SWilliam Allen Simpson 	u16		s_data_desired:11,
1666435cf559SWilliam Allen Simpson 			s_data_constant:1,
1667435cf559SWilliam Allen Simpson 			s_data_in:1,
1668435cf559SWilliam Allen Simpson 			s_data_out:1,
1669435cf559SWilliam Allen Simpson 			s_data_unused:2;
1670435cf559SWilliam Allen Simpson 	u8		s_data_payload[0];
1671435cf559SWilliam Allen Simpson };
1672435cf559SWilliam Allen Simpson 
1673435cf559SWilliam Allen Simpson static inline void tcp_cookie_values_release(struct kref *kref)
1674435cf559SWilliam Allen Simpson {
1675435cf559SWilliam Allen Simpson 	kfree(container_of(kref, struct tcp_cookie_values, kref));
1676435cf559SWilliam Allen Simpson }
1677435cf559SWilliam Allen Simpson 
1678435cf559SWilliam Allen Simpson /* The length of constant payload data.  Note that s_data_desired is
1679435cf559SWilliam Allen Simpson  * overloaded, depending on s_data_constant: either the length of constant
1680435cf559SWilliam Allen Simpson  * data (returned here) or the limit on variable data.
1681435cf559SWilliam Allen Simpson  */
1682435cf559SWilliam Allen Simpson static inline int tcp_s_data_size(const struct tcp_sock *tp)
1683435cf559SWilliam Allen Simpson {
1684435cf559SWilliam Allen Simpson 	return (tp->cookie_values != NULL && tp->cookie_values->s_data_constant)
1685435cf559SWilliam Allen Simpson 		? tp->cookie_values->s_data_desired
1686435cf559SWilliam Allen Simpson 		: 0;
1687435cf559SWilliam Allen Simpson }
1688435cf559SWilliam Allen Simpson 
1689435cf559SWilliam Allen Simpson /**
1690435cf559SWilliam Allen Simpson  *	struct tcp_extend_values - tcp_ipv?.c to tcp_output.c workspace.
1691435cf559SWilliam Allen Simpson  *
1692435cf559SWilliam Allen Simpson  *	As tcp_request_sock has already been extended in other places, the
1693435cf559SWilliam Allen Simpson  *	only remaining method is to pass stack values along as function
1694435cf559SWilliam Allen Simpson  *	parameters.  These parameters are not needed after sending SYNACK.
1695435cf559SWilliam Allen Simpson  *
1696435cf559SWilliam Allen Simpson  * @cookie_bakery:	cryptographic secret and message workspace.
1697435cf559SWilliam Allen Simpson  *
1698435cf559SWilliam Allen Simpson  * @cookie_plus:	bytes in authenticator/cookie option, copied from
1699435cf559SWilliam Allen Simpson  *			struct tcp_options_received (above).
1700435cf559SWilliam Allen Simpson  */
1701435cf559SWilliam Allen Simpson struct tcp_extend_values {
1702435cf559SWilliam Allen Simpson 	struct request_values		rv;
1703435cf559SWilliam Allen Simpson 	u32				cookie_bakery[COOKIE_WORKSPACE_WORDS];
1704435cf559SWilliam Allen Simpson 	u8				cookie_plus:6,
1705435cf559SWilliam Allen Simpson 					cookie_out_never:1,
1706435cf559SWilliam Allen Simpson 					cookie_in_always:1;
1707435cf559SWilliam Allen Simpson };
1708435cf559SWilliam Allen Simpson 
1709435cf559SWilliam Allen Simpson static inline struct tcp_extend_values *tcp_xv(struct request_values *rvp)
1710435cf559SWilliam Allen Simpson {
1711435cf559SWilliam Allen Simpson 	return (struct tcp_extend_values *)rvp;
1712435cf559SWilliam Allen Simpson }
1713435cf559SWilliam Allen Simpson 
17149b0f976fSDenis V. Lunev extern void tcp_v4_init(void);
171520380731SArnaldo Carvalho de Melo extern void tcp_init(void);
171620380731SArnaldo Carvalho de Melo 
17171da177e4SLinus Torvalds #endif	/* _TCP_H */
1718