xref: /linux/include/net/tcp.h (revision a2a385d627e1549da4b43a8b3dfe370589766e1c)
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 
1011da177e4SLinus Torvalds #define TCP_SYN_RETRIES	 5	/* number of times to retry active opening a
102caa20d9aSStephen Hemminger 				 * connection: ~180sec is RFC minimum	*/
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds #define TCP_SYNACK_RETRIES 5	/* number of times to retry passive opening a
105caa20d9aSStephen Hemminger 				 * connection: ~180sec is RFC minimum	*/
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1081da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1091da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1101da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1111da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1121da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1131da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1141da177e4SLinus Torvalds 				  */
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1171da177e4SLinus Torvalds #if HZ >= 100
1181da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1191da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1201da177e4SLinus Torvalds #else
1211da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1221da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1231da177e4SLinus Torvalds #endif
1241da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1251da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
126fd4f2ceaSEric Dumazet #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ))	/* RFC6298 2.1 initial RTO value	*/
1279ad7c049SJerry Chu #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value, now
1289ad7c049SJerry Chu 						 * used as a fallback RTO for the
1299ad7c049SJerry Chu 						 * initial data transmission if no
1309ad7c049SJerry Chu 						 * valid RTT sample has been acquired,
1319ad7c049SJerry Chu 						 * most likely due to retrans in 3WHS.
1329ad7c049SJerry Chu 						 */
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1351da177e4SLinus Torvalds 					                 * for local resources.
1361da177e4SLinus Torvalds 					                 */
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1391da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1401da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1431da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1441da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1451da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1501da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1511da177e4SLinus Torvalds 					 * after this time. It should be equal
1521da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1531da177e4SLinus Torvalds 					 * to provide reliability equal to one
1541da177e4SLinus Torvalds 					 * provided by timewait state.
1551da177e4SLinus Torvalds 					 */
1561da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1571da177e4SLinus Torvalds 					 * timestamps. It must be less than
1581da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1591da177e4SLinus Torvalds 					 */
1601da177e4SLinus Torvalds /*
1611da177e4SLinus Torvalds  *	TCP option
1621da177e4SLinus Torvalds  */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1651da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1661da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1671da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1681da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1691da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1701da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
171cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
172435cf559SWilliam Allen Simpson #define TCPOPT_COOKIE		253	/* Cookie extension (experimental) */
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds /*
1751da177e4SLinus Torvalds  *     TCP option lengths
1761da177e4SLinus Torvalds  */
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds #define TCPOLEN_MSS            4
1791da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
1801da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
1811da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
182cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG         18
183435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_BASE    2	/* Cookie-less header extension */
184435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_PAIR    3	/* Cookie pair header extension */
185435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_MIN     (TCPOLEN_COOKIE_BASE+TCP_COOKIE_MIN)
186435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_MAX     (TCPOLEN_COOKIE_BASE+TCP_COOKIE_MAX)
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /* But this is what stacks really send out. */
1891da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
1901da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
1911da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
1921da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
1931da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
1941da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
195cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED		20
19633ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED		4
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /* Flags in tp->nonagle */
1991da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
2001da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
201caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
2021da177e4SLinus Torvalds 
20336e31b0aSAndreas Petlund /* TCP thin-stream limits */
20436e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
20536e31b0aSAndreas Petlund 
2067eb38527SDavid S. Miller /* TCP initial congestion window as per draft-hkchu-tcpm-initcwnd-01 */
207442b9635SDavid S. Miller #define TCP_INIT_CWND		10
208442b9635SDavid S. Miller 
209295ff7edSArnaldo Carvalho de Melo extern struct inet_timewait_death_row tcp_death_row;
210295ff7edSArnaldo Carvalho de Melo 
2111da177e4SLinus Torvalds /* sysctl variables for tcp */
2121da177e4SLinus Torvalds extern int sysctl_tcp_timestamps;
2131da177e4SLinus Torvalds extern int sysctl_tcp_window_scaling;
2141da177e4SLinus Torvalds extern int sysctl_tcp_sack;
2151da177e4SLinus Torvalds extern int sysctl_tcp_fin_timeout;
2161da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_time;
2171da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_probes;
2181da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_intvl;
2191da177e4SLinus Torvalds extern int sysctl_tcp_syn_retries;
2201da177e4SLinus Torvalds extern int sysctl_tcp_synack_retries;
2211da177e4SLinus Torvalds extern int sysctl_tcp_retries1;
2221da177e4SLinus Torvalds extern int sysctl_tcp_retries2;
2231da177e4SLinus Torvalds extern int sysctl_tcp_orphan_retries;
2241da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
2251da177e4SLinus Torvalds extern int sysctl_tcp_retrans_collapse;
2261da177e4SLinus Torvalds extern int sysctl_tcp_stdurg;
2271da177e4SLinus Torvalds extern int sysctl_tcp_rfc1337;
2281da177e4SLinus Torvalds extern int sysctl_tcp_abort_on_overflow;
2291da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
2301da177e4SLinus Torvalds extern int sysctl_tcp_fack;
2311da177e4SLinus Torvalds extern int sysctl_tcp_reordering;
2321da177e4SLinus Torvalds extern int sysctl_tcp_ecn;
2331da177e4SLinus Torvalds extern int sysctl_tcp_dsack;
2341da177e4SLinus Torvalds extern int sysctl_tcp_wmem[3];
2351da177e4SLinus Torvalds extern int sysctl_tcp_rmem[3];
2361da177e4SLinus Torvalds extern int sysctl_tcp_app_win;
2371da177e4SLinus Torvalds extern int sysctl_tcp_adv_win_scale;
2381da177e4SLinus Torvalds extern int sysctl_tcp_tw_reuse;
2391da177e4SLinus Torvalds extern int sysctl_tcp_frto;
2403cfe3baaSIlpo Järvinen extern int sysctl_tcp_frto_response;
2411da177e4SLinus Torvalds extern int sysctl_tcp_low_latency;
24295937825SChris Leech extern int sysctl_tcp_dma_copybreak;
2431da177e4SLinus Torvalds extern int sysctl_tcp_nometrics_save;
2441da177e4SLinus Torvalds extern int sysctl_tcp_moderate_rcvbuf;
2451da177e4SLinus Torvalds extern int sysctl_tcp_tso_win_divisor;
2469772efb9SStephen Hemminger extern int sysctl_tcp_abc;
2475d424d5aSJohn Heffner extern int sysctl_tcp_mtu_probing;
2485d424d5aSJohn Heffner extern int sysctl_tcp_base_mss;
24915d99e02SRick Jones extern int sysctl_tcp_workaround_signed_windows;
25035089bb2SDavid S. Miller extern int sysctl_tcp_slow_start_after_idle;
251886236c1SJohn Heffner extern int sysctl_tcp_max_ssthresh;
252519855c5SWilliam Allen Simpson extern int sysctl_tcp_cookie_size;
25336e31b0aSAndreas Petlund extern int sysctl_tcp_thin_linear_timeouts;
2547e380175SAndreas Petlund extern int sysctl_tcp_thin_dupack;
255eed530b6SYuchung Cheng extern int sysctl_tcp_early_retrans;
2561da177e4SLinus Torvalds 
2578d987e5cSEric Dumazet extern atomic_long_t tcp_memory_allocated;
2581748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated;
2591da177e4SLinus Torvalds extern int tcp_memory_pressure;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds /*
2621da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
2631da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
2641da177e4SLinus Torvalds  */
2651da177e4SLinus Torvalds 
266*a2a385d6SEric Dumazet static inline bool before(__u32 seq1, __u32 seq2)
2671da177e4SLinus Torvalds {
2680d630cc0SGerrit Renker         return (__s32)(seq1-seq2) < 0;
2691da177e4SLinus Torvalds }
2709a036b9cSGerrit Renker #define after(seq2, seq1) 	before(seq1, seq2)
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
273*a2a385d6SEric Dumazet static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
278efcdbf24SArun Sharma static inline bool tcp_out_of_memory(struct sock *sk)
279efcdbf24SArun Sharma {
280efcdbf24SArun Sharma 	if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
281efcdbf24SArun Sharma 	    sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2))
282efcdbf24SArun Sharma 		return true;
283efcdbf24SArun Sharma 	return false;
284efcdbf24SArun Sharma }
285efcdbf24SArun Sharma 
286ad1af0feSDavid S. Miller static inline bool tcp_too_many_orphans(struct sock *sk, int shift)
287e4fd5da3SPavel Emelianov {
288ad1af0feSDavid S. Miller 	struct percpu_counter *ocp = sk->sk_prot->orphan_count;
289ad1af0feSDavid S. Miller 	int orphans = percpu_counter_read_positive(ocp);
290ad1af0feSDavid S. Miller 
291ad1af0feSDavid S. Miller 	if (orphans << shift > sysctl_tcp_max_orphans) {
292ad1af0feSDavid S. Miller 		orphans = percpu_counter_sum_positive(ocp);
293ad1af0feSDavid S. Miller 		if (orphans << shift > sysctl_tcp_max_orphans)
294ad1af0feSDavid S. Miller 			return true;
295ad1af0feSDavid S. Miller 	}
296ad1af0feSDavid S. Miller 	return false;
297e4fd5da3SPavel Emelianov }
2981da177e4SLinus Torvalds 
299efcdbf24SArun Sharma extern bool tcp_check_oom(struct sock *sk, int shift);
300efcdbf24SArun Sharma 
301a0f82f64SFlorian Westphal /* syncookies: remember time of last synqueue overflow */
302a0f82f64SFlorian Westphal static inline void tcp_synq_overflow(struct sock *sk)
303a0f82f64SFlorian Westphal {
304a0f82f64SFlorian Westphal 	tcp_sk(sk)->rx_opt.ts_recent_stamp = jiffies;
305a0f82f64SFlorian Westphal }
306a0f82f64SFlorian Westphal 
307a0f82f64SFlorian Westphal /* syncookies: no recent synqueue overflow on this listening socket? */
308*a2a385d6SEric Dumazet static inline bool tcp_synq_no_recent_overflow(const struct sock *sk)
309a0f82f64SFlorian Westphal {
310a0f82f64SFlorian Westphal 	unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
3119ad7c049SJerry Chu 	return time_after(jiffies, last_overflow + TCP_TIMEOUT_FALLBACK);
312a0f82f64SFlorian Westphal }
313a0f82f64SFlorian Westphal 
3141da177e4SLinus Torvalds extern struct proto tcp_prot;
3151da177e4SLinus Torvalds 
31657ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
31757ef42d5SPavel Emelyanov #define TCP_INC_STATS_BH(net, field)	SNMP_INC_STATS_BH((net)->mib.tcp_statistics, field)
31857ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
31957ef42d5SPavel Emelyanov #define TCP_ADD_STATS_USER(net, field, val) SNMP_ADD_STATS_USER((net)->mib.tcp_statistics, field, val)
320aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
3211da177e4SLinus Torvalds 
3224acb4190SGlauber Costa extern void tcp_init_mem(struct net *net);
3234acb4190SGlauber Costa 
3241da177e4SLinus Torvalds extern void tcp_v4_err(struct sk_buff *skb, u32);
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds extern void tcp_shutdown (struct sock *sk, int how);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds extern int tcp_v4_rcv(struct sk_buff *skb);
3291da177e4SLinus Torvalds 
3303f419d2dSDavid S. Miller extern struct inet_peer *tcp_v4_get_peer(struct sock *sk, bool *release_it);
331ccb7c410SDavid S. Miller extern void *tcp_v4_tw_get_peer(struct sock *sk);
3328feaf0c0SArnaldo Carvalho de Melo extern int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
3337ba42910SChangli Gao extern int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
3347ba42910SChangli Gao 		       size_t size);
3357ba42910SChangli Gao extern int tcp_sendpage(struct sock *sk, struct page *page, int offset,
33653d3176bSChangli Gao 			size_t size, int flags);
33753d3176bSChangli Gao extern int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
33853d3176bSChangli Gao extern int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
339cf533ea5SEric Dumazet 				 const struct tcphdr *th, unsigned int len);
34053d3176bSChangli Gao extern int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
341cf533ea5SEric Dumazet 			       const struct tcphdr *th, unsigned int len);
3421da177e4SLinus Torvalds extern void tcp_rcv_space_adjust(struct sock *sk);
3430e4b4992SChris Leech extern void tcp_cleanup_rbuf(struct sock *sk, int copied);
34453d3176bSChangli Gao extern int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
345cfb6eeb4SYOSHIFUJI Hideaki extern void tcp_twsk_destructor(struct sock *sk);
3469c55e01cSJens Axboe extern ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
34753d3176bSChangli Gao 			       struct pipe_inode_info *pipe, size_t len,
34853d3176bSChangli Gao 			       unsigned int flags);
3499c55e01cSJens Axboe 
350463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
351463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3521da177e4SLinus Torvalds {
353463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
354fc6415bcSDavid S. Miller 
355463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
356463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
357463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3581da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
359463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
360fc6415bcSDavid S. Miller 		} else
361463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
365bdf1ee5dSIlpo Järvinen #define	TCP_ECN_OK		1
366bdf1ee5dSIlpo Järvinen #define	TCP_ECN_QUEUE_CWR	2
367bdf1ee5dSIlpo Järvinen #define	TCP_ECN_DEMAND_CWR	4
3687a269ffaSEric Dumazet #define	TCP_ECN_SEEN		8
369bdf1ee5dSIlpo Järvinen 
370fd2c3ef7SEric Dumazet enum tcp_tw_status {
3711da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
3721da177e4SLinus Torvalds 	TCP_TW_RST = 1,
3731da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
3741da177e4SLinus Torvalds 	TCP_TW_SYN = 3
3751da177e4SLinus Torvalds };
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 
3788feaf0c0SArnaldo Carvalho de Melo extern enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
3791da177e4SLinus Torvalds 						     struct sk_buff *skb,
3808feaf0c0SArnaldo Carvalho de Melo 						     const struct tcphdr *th);
3811da177e4SLinus Torvalds extern struct sock * tcp_check_req(struct sock *sk,struct sk_buff *skb,
38260236fddSArnaldo Carvalho de Melo 				   struct request_sock *req,
38360236fddSArnaldo Carvalho de Melo 				   struct request_sock **prev);
38453d3176bSChangli Gao extern int tcp_child_process(struct sock *parent, struct sock *child,
3851da177e4SLinus Torvalds 			     struct sk_buff *skb);
386*a2a385d6SEric Dumazet extern bool tcp_use_frto(struct sock *sk);
3871da177e4SLinus Torvalds extern void tcp_enter_frto(struct sock *sk);
3881da177e4SLinus Torvalds extern void tcp_enter_loss(struct sock *sk, int how);
3891da177e4SLinus Torvalds extern void tcp_clear_retrans(struct tcp_sock *tp);
3901da177e4SLinus Torvalds extern void tcp_update_metrics(struct sock *sk);
39153d3176bSChangli Gao extern void tcp_close(struct sock *sk, long timeout);
392900f65d3SNeal Cardwell extern void tcp_init_sock(struct sock *sk);
39353d3176bSChangli Gao extern unsigned int tcp_poll(struct file * file, struct socket *sock,
39453d3176bSChangli Gao 			     struct poll_table_struct *wait);
39553d3176bSChangli Gao extern int tcp_getsockopt(struct sock *sk, int level, int optname,
3963fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen);
39753d3176bSChangli Gao extern int tcp_setsockopt(struct sock *sk, int level, int optname,
39853d3176bSChangli Gao 			  char __user *optval, unsigned int optlen);
39953d3176bSChangli Gao extern int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
40053d3176bSChangli Gao 				 char __user *optval, int __user *optlen);
40153d3176bSChangli Gao extern int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
402b7058842SDavid S. Miller 				 char __user *optval, unsigned int optlen);
4031da177e4SLinus Torvalds extern void tcp_set_keepalive(struct sock *sk, int val);
40453d3176bSChangli Gao extern void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req);
40553d3176bSChangli Gao extern int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
40653d3176bSChangli Gao 		       size_t len, int nonblock, int flags, int *addr_len);
407cf533ea5SEric Dumazet extern void tcp_parse_options(const struct sk_buff *skb,
408cf533ea5SEric Dumazet 			      struct tcp_options_received *opt_rx, const u8 **hvpp,
409bb5b7c11SDavid S. Miller 			      int estab);
410cf533ea5SEric Dumazet extern const u8 *tcp_parse_md5sig_option(const struct tcphdr *th);
4117d5d5525SYOSHIFUJI Hideaki 
4121da177e4SLinus Torvalds /*
4131da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4141da177e4SLinus Torvalds  */
4151da177e4SLinus Torvalds 
41653d3176bSChangli Gao extern void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
41753d3176bSChangli Gao extern int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
4181da177e4SLinus Torvalds extern struct sock * tcp_create_openreq_child(struct sock *sk,
41960236fddSArnaldo Carvalho de Melo 					      struct request_sock *req,
4201da177e4SLinus Torvalds 					      struct sk_buff *skb);
42153d3176bSChangli Gao extern struct sock * tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
42260236fddSArnaldo Carvalho de Melo 					  struct request_sock *req,
4231da177e4SLinus Torvalds 					  struct dst_entry *dst);
42453d3176bSChangli Gao extern int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
42553d3176bSChangli Gao extern int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
4261da177e4SLinus Torvalds 			  int addr_len);
4271da177e4SLinus Torvalds extern int tcp_connect(struct sock *sk);
42853d3176bSChangli Gao extern struct sk_buff * tcp_make_synack(struct sock *sk, struct dst_entry *dst,
429e6b4d113SWilliam Allen Simpson 					struct request_sock *req,
430e6b4d113SWilliam Allen Simpson 					struct request_values *rvp);
4311da177e4SLinus Torvalds extern int tcp_disconnect(struct sock *sk, int flags);
4321da177e4SLinus Torvalds 
433370816aeSPavel Emelyanov void tcp_connect_init(struct sock *sk);
434370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb);
435292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds /* From syncookies.c */
4382051f11fSFlorian Westphal extern __u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS];
4391da177e4SLinus Torvalds extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
4401da177e4SLinus Torvalds 				    struct ip_options *opt);
441e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
4421da177e4SLinus Torvalds extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb,
4431da177e4SLinus Torvalds 				     __u16 *mss);
444e05c82d3SEric Dumazet #else
445e05c82d3SEric Dumazet static inline __u32 cookie_v4_init_sequence(struct sock *sk,
446e05c82d3SEric Dumazet 					    struct sk_buff *skb,
447e05c82d3SEric Dumazet 					    __u16 *mss)
448e05c82d3SEric Dumazet {
449e05c82d3SEric Dumazet 	return 0;
450e05c82d3SEric Dumazet }
451e05c82d3SEric Dumazet #endif
4521da177e4SLinus Torvalds 
4534dfc2817SFlorian Westphal extern __u32 cookie_init_timestamp(struct request_sock *req);
454172d69e6SFlorian Westphal extern bool cookie_check_timestamp(struct tcp_options_received *opt, bool *);
4554dfc2817SFlorian Westphal 
456c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */
457c6aefafbSGlenn Griffin extern struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
458e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES
459cf533ea5SEric Dumazet extern __u32 cookie_v6_init_sequence(struct sock *sk, const struct sk_buff *skb,
460c6aefafbSGlenn Griffin 				     __u16 *mss);
461e05c82d3SEric Dumazet #else
462e05c82d3SEric Dumazet static inline __u32 cookie_v6_init_sequence(struct sock *sk,
463e05c82d3SEric Dumazet 					    struct sk_buff *skb,
464e05c82d3SEric Dumazet 					    __u16 *mss)
465e05c82d3SEric Dumazet {
466e05c82d3SEric Dumazet 	return 0;
467e05c82d3SEric Dumazet }
468e05c82d3SEric Dumazet #endif
4691da177e4SLinus Torvalds /* tcp_output.c */
4701da177e4SLinus Torvalds 
4719e412ba7SIlpo Järvinen extern void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
4729e412ba7SIlpo Järvinen 				      int nonagle);
473*a2a385d6SEric Dumazet extern bool tcp_may_send_now(struct sock *sk);
4741da177e4SLinus Torvalds extern int tcp_retransmit_skb(struct sock *, struct sk_buff *);
475f1ecd5d9SDamian Lukowski extern void tcp_retransmit_timer(struct sock *sk);
4761da177e4SLinus Torvalds extern void tcp_xmit_retransmit_queue(struct sock *);
4771da177e4SLinus Torvalds extern void tcp_simple_retransmit(struct sock *);
4781da177e4SLinus Torvalds extern int tcp_trim_head(struct sock *, struct sk_buff *, u32);
4796475be16SDavid S. Miller extern int tcp_fragment(struct sock *, struct sk_buff *, u32, unsigned int);
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds extern void tcp_send_probe0(struct sock *);
4821da177e4SLinus Torvalds extern void tcp_send_partial(struct sock *);
4831da177e4SLinus Torvalds extern int tcp_write_wakeup(struct sock *);
4841da177e4SLinus Torvalds extern void tcp_send_fin(struct sock *sk);
485dd0fc66fSAl Viro extern void tcp_send_active_reset(struct sock *sk, gfp_t priority);
4861da177e4SLinus Torvalds extern int tcp_send_synack(struct sock *);
487*a2a385d6SEric Dumazet extern bool tcp_syn_flood_action(struct sock *sk,
488946cedccSEric Dumazet 				 const struct sk_buff *skb,
489946cedccSEric Dumazet 				 const char *proto);
490c1b4a7e6SDavid S. Miller extern void tcp_push_one(struct sock *, unsigned int mss_now);
4911da177e4SLinus Torvalds extern void tcp_send_ack(struct sock *sk);
4921da177e4SLinus Torvalds extern void tcp_send_delayed_ack(struct sock *sk);
4931da177e4SLinus Torvalds 
494a762a980SDavid S. Miller /* tcp_input.c */
495a762a980SDavid S. Miller extern void tcp_cwnd_application_limited(struct sock *sk);
496750ea2baSYuchung Cheng extern void tcp_resume_early_retransmit(struct sock *sk);
497750ea2baSYuchung Cheng extern void tcp_rearm_rto(struct sock *sk);
498a762a980SDavid S. Miller 
4991da177e4SLinus Torvalds /* tcp_timer.c */
5001da177e4SLinus Torvalds extern void tcp_init_xmit_timers(struct sock *);
501463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
502463c84b9SArnaldo Carvalho de Melo {
503463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
504463c84b9SArnaldo Carvalho de Melo }
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds extern unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
5070c54b85fSIlpo Järvinen extern unsigned int tcp_current_mss(struct sock *sk);
5080c54b85fSIlpo Järvinen 
5090c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */
5100c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
5110c54b85fSIlpo Järvinen {
51201f83d69SAlexey Kuznetsov 	int cutoff;
51301f83d69SAlexey Kuznetsov 
51401f83d69SAlexey Kuznetsov 	/* When peer uses tiny windows, there is no use in packetizing
51501f83d69SAlexey Kuznetsov 	 * to sub-MSS pieces for the sake of SWS or making sure there
51601f83d69SAlexey Kuznetsov 	 * are enough packets in the pipe for fast recovery.
51701f83d69SAlexey Kuznetsov 	 *
51801f83d69SAlexey Kuznetsov 	 * On the other hand, for extremely large MSS devices, handling
51901f83d69SAlexey Kuznetsov 	 * smaller than MSS windows in this way does make sense.
52001f83d69SAlexey Kuznetsov 	 */
52101f83d69SAlexey Kuznetsov 	if (tp->max_window >= 512)
52201f83d69SAlexey Kuznetsov 		cutoff = (tp->max_window >> 1);
52301f83d69SAlexey Kuznetsov 	else
52401f83d69SAlexey Kuznetsov 		cutoff = tp->max_window;
52501f83d69SAlexey Kuznetsov 
52601f83d69SAlexey Kuznetsov 	if (cutoff && pktsize > cutoff)
52701f83d69SAlexey Kuznetsov 		return max_t(int, cutoff, 68U - tp->tcp_header_len);
5280c54b85fSIlpo Järvinen 	else
5290c54b85fSIlpo Järvinen 		return pktsize;
5300c54b85fSIlpo Järvinen }
5311da177e4SLinus Torvalds 
53217b085eaSArnaldo Carvalho de Melo /* tcp.c */
533cf533ea5SEric Dumazet extern void tcp_get_info(const struct sock *, struct tcp_info *);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
5361da177e4SLinus Torvalds typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
5371da177e4SLinus Torvalds 				unsigned int, size_t);
5381da177e4SLinus Torvalds extern int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
5391da177e4SLinus Torvalds 			 sk_read_actor_t recv_actor);
5401da177e4SLinus Torvalds 
54140efc6faSStephen Hemminger extern void tcp_initialize_rcv_mss(struct sock *sk);
5421da177e4SLinus Torvalds 
54367469601SEric Dumazet extern int tcp_mtu_to_mss(struct sock *sk, int pmtu);
54467469601SEric Dumazet extern int tcp_mss_to_mtu(struct sock *sk, int mss);
5455d424d5aSJohn Heffner extern void tcp_mtup_init(struct sock *sk);
5469ad7c049SJerry Chu extern void tcp_valid_rtt_meas(struct sock *sk, u32 seq_rtt);
5475d424d5aSJohn Heffner 
548f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk)
549f1ecd5d9SDamian Lukowski {
550f1ecd5d9SDamian Lukowski 	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
551f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
552f1ecd5d9SDamian Lukowski }
553f1ecd5d9SDamian Lukowski 
554f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
555f1ecd5d9SDamian Lukowski {
556f1ecd5d9SDamian Lukowski 	return (tp->srtt >> 3) + tp->rttvar;
557f1ecd5d9SDamian Lukowski }
558f1ecd5d9SDamian Lukowski 
55940efc6faSStephen Hemminger static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
5601da177e4SLinus Torvalds {
5611da177e4SLinus Torvalds 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
5621da177e4SLinus Torvalds 			       ntohl(TCP_FLAG_ACK) |
5631da177e4SLinus Torvalds 			       snd_wnd);
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
56640efc6faSStephen Hemminger static inline void tcp_fast_path_on(struct tcp_sock *tp)
5671da177e4SLinus Torvalds {
5681da177e4SLinus Torvalds 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5719e412ba7SIlpo Järvinen static inline void tcp_fast_path_check(struct sock *sk)
5721da177e4SLinus Torvalds {
5739e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
5749e412ba7SIlpo Järvinen 
575b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue) &&
5761da177e4SLinus Torvalds 	    tp->rcv_wnd &&
5771da177e4SLinus Torvalds 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
5781da177e4SLinus Torvalds 	    !tp->urg_data)
5791da177e4SLinus Torvalds 		tcp_fast_path_on(tp);
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds 
5820c266898SSatoru SATOH /* Compute the actual rto_min value */
5830c266898SSatoru SATOH static inline u32 tcp_rto_min(struct sock *sk)
5840c266898SSatoru SATOH {
585cf533ea5SEric Dumazet 	const struct dst_entry *dst = __sk_dst_get(sk);
5860c266898SSatoru SATOH 	u32 rto_min = TCP_RTO_MIN;
5870c266898SSatoru SATOH 
5880c266898SSatoru SATOH 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
5890c266898SSatoru SATOH 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
5900c266898SSatoru SATOH 	return rto_min;
5910c266898SSatoru SATOH }
5920c266898SSatoru SATOH 
5931da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
5941da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
5951da177e4SLinus Torvalds  * than the offered window.
5961da177e4SLinus Torvalds  */
59740efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
5981da177e4SLinus Torvalds {
5991da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	if (win < 0)
6021da177e4SLinus Torvalds 		win = 0;
6031da177e4SLinus Torvalds 	return (u32) win;
6041da177e4SLinus Torvalds }
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
6071da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
6081da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
6091da177e4SLinus Torvalds  */
6101da177e4SLinus Torvalds extern u32 __tcp_select_window(struct sock *sk);
6111da177e4SLinus Torvalds 
612ee995283SPavel Emelyanov void tcp_send_window_probe(struct sock *sk);
613ee995283SPavel Emelyanov 
6141da177e4SLinus Torvalds /* TCP timestamps are only 32-bits, this causes a slight
6151da177e4SLinus Torvalds  * complication on 64-bit systems since we store a snapshot
61631f34269SStephen Hemminger  * of jiffies in the buffer control blocks below.  We decided
61731f34269SStephen Hemminger  * to use only the low 32-bits of jiffies and hide the ugly
6181da177e4SLinus Torvalds  * casts with the following macro.
6191da177e4SLinus Torvalds  */
6201da177e4SLinus Torvalds #define tcp_time_stamp		((__u32)(jiffies))
6211da177e4SLinus Torvalds 
622a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
623a3433f35SChangli Gao 
624a3433f35SChangli Gao #define TCPHDR_FIN 0x01
625a3433f35SChangli Gao #define TCPHDR_SYN 0x02
626a3433f35SChangli Gao #define TCPHDR_RST 0x04
627a3433f35SChangli Gao #define TCPHDR_PSH 0x08
628a3433f35SChangli Gao #define TCPHDR_ACK 0x10
629a3433f35SChangli Gao #define TCPHDR_URG 0x20
630a3433f35SChangli Gao #define TCPHDR_ECE 0x40
631a3433f35SChangli Gao #define TCPHDR_CWR 0x80
632a3433f35SChangli Gao 
633caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
634f86586faSEric Dumazet  * TCP per-packet control information to the transmission code.
635f86586faSEric Dumazet  * We also store the host-order sequence numbers in here too.
636f86586faSEric Dumazet  * This is 44 bytes if IPV6 is enabled.
637f86586faSEric Dumazet  * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
6381da177e4SLinus Torvalds  */
6391da177e4SLinus Torvalds struct tcp_skb_cb {
6401da177e4SLinus Torvalds 	union {
6411da177e4SLinus Torvalds 		struct inet_skb_parm	h4;
642dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
6431da177e4SLinus Torvalds 		struct inet6_skb_parm	h6;
6441da177e4SLinus Torvalds #endif
6451da177e4SLinus Torvalds 	} header;	/* For incoming frames		*/
6461da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
6471da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
6481da177e4SLinus Torvalds 	__u32		when;		/* used to compute rtt's	*/
6494de075e0SEric Dumazet 	__u8		tcp_flags;	/* TCP header flags. (tcp[13])	*/
650f4f9f6e7SNeal Cardwell 
6511da177e4SLinus Torvalds 	__u8		sacked;		/* State flags for SACK/FACK.	*/
6521da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
6531da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
6541da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
6551da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
6561da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
6571da177e4SLinus Torvalds #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
6581da177e4SLinus Torvalds 
659f4f9f6e7SNeal Cardwell 	__u8		ip_dsfield;	/* IPv4 tos or IPv6 dsfield	*/
660f4f9f6e7SNeal Cardwell 	/* 1 byte hole */
6611da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
6621da177e4SLinus Torvalds };
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
6651da177e4SLinus Torvalds 
666bd14b1b2SEric Dumazet /* RFC3168 : 6.1.1 SYN packets must not have ECT/ECN bits set
667bd14b1b2SEric Dumazet  *
668bd14b1b2SEric Dumazet  * If we receive a SYN packet with these bits set, it means a network is
669bd14b1b2SEric Dumazet  * playing bad games with TOS bits. In order to avoid possible false congestion
670bd14b1b2SEric Dumazet  * notifications, we disable TCP ECN negociation.
671bd14b1b2SEric Dumazet  */
672bd14b1b2SEric Dumazet static inline void
673bd14b1b2SEric Dumazet TCP_ECN_create_request(struct request_sock *req, const struct sk_buff *skb)
674bd14b1b2SEric Dumazet {
675bd14b1b2SEric Dumazet 	const struct tcphdr *th = tcp_hdr(skb);
676bd14b1b2SEric Dumazet 
677bd14b1b2SEric Dumazet 	if (sysctl_tcp_ecn && th->ece && th->cwr &&
678bd14b1b2SEric Dumazet 	    INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield))
679bd14b1b2SEric Dumazet 		inet_rsk(req)->ecn_ok = 1;
680bd14b1b2SEric Dumazet }
681bd14b1b2SEric Dumazet 
6821da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
6831da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
6841da177e4SLinus Torvalds  */
6851da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
6861da177e4SLinus Torvalds {
6877967168cSHerbert Xu 	return skb_shinfo(skb)->gso_segs;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds /* This is valid iff tcp_skb_pcount() > 1. */
6911da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
6921da177e4SLinus Torvalds {
6937967168cSHerbert Xu 	return skb_shinfo(skb)->gso_size;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds 
696317a76f9SStephen Hemminger /* Events passed to congestion control interface */
697317a76f9SStephen Hemminger enum tcp_ca_event {
698317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
699317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
700317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
701317a76f9SStephen Hemminger 	CA_EVENT_FRTO,		/* fast recovery timeout */
702317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
703317a76f9SStephen Hemminger 	CA_EVENT_FAST_ACK,	/* in sequence ack */
704317a76f9SStephen Hemminger 	CA_EVENT_SLOW_ACK,	/* other ack */
705317a76f9SStephen Hemminger };
706317a76f9SStephen Hemminger 
707317a76f9SStephen Hemminger /*
708317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
709317a76f9SStephen Hemminger  */
710317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
7113ff825b2SStephen Hemminger #define TCP_CA_MAX	128
7123ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
7133ff825b2SStephen Hemminger 
714164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1
715164891aaSStephen Hemminger #define TCP_CONG_RTT_STAMP	0x2
716164891aaSStephen Hemminger 
717317a76f9SStephen Hemminger struct tcp_congestion_ops {
718317a76f9SStephen Hemminger 	struct list_head	list;
719164891aaSStephen Hemminger 	unsigned long flags;
720317a76f9SStephen Hemminger 
721317a76f9SStephen Hemminger 	/* initialize private data (optional) */
7226687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
723317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
7246687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
725317a76f9SStephen Hemminger 
726317a76f9SStephen Hemminger 	/* return slow start threshold (required) */
7276687e988SArnaldo Carvalho de Melo 	u32 (*ssthresh)(struct sock *sk);
728317a76f9SStephen Hemminger 	/* lower bound for congestion window (optional) */
72972dc5b92SStephen Hemminger 	u32 (*min_cwnd)(const struct sock *sk);
730317a76f9SStephen Hemminger 	/* do new cwnd calculation (required) */
731c3a05c60SIlpo Järvinen 	void (*cong_avoid)(struct sock *sk, u32 ack, u32 in_flight);
732317a76f9SStephen Hemminger 	/* call before changing ca_state (optional) */
7336687e988SArnaldo Carvalho de Melo 	void (*set_state)(struct sock *sk, u8 new_state);
734317a76f9SStephen Hemminger 	/* call when cwnd event occurs (optional) */
7356687e988SArnaldo Carvalho de Melo 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
736317a76f9SStephen Hemminger 	/* new value of cwnd after loss (optional) */
7376687e988SArnaldo Carvalho de Melo 	u32  (*undo_cwnd)(struct sock *sk);
738317a76f9SStephen Hemminger 	/* hook for packet ack accounting (optional) */
73930cfd0baSStephen Hemminger 	void (*pkts_acked)(struct sock *sk, u32 num_acked, s32 rtt_us);
74073c1f4a0SArnaldo Carvalho de Melo 	/* get info for inet_diag (optional) */
7416687e988SArnaldo Carvalho de Melo 	void (*get_info)(struct sock *sk, u32 ext, struct sk_buff *skb);
742317a76f9SStephen Hemminger 
743317a76f9SStephen Hemminger 	char 		name[TCP_CA_NAME_MAX];
744317a76f9SStephen Hemminger 	struct module 	*owner;
745317a76f9SStephen Hemminger };
746317a76f9SStephen Hemminger 
747317a76f9SStephen Hemminger extern int tcp_register_congestion_control(struct tcp_congestion_ops *type);
748317a76f9SStephen Hemminger extern void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
749317a76f9SStephen Hemminger 
7506687e988SArnaldo Carvalho de Melo extern void tcp_init_congestion_control(struct sock *sk);
7516687e988SArnaldo Carvalho de Melo extern void tcp_cleanup_congestion_control(struct sock *sk);
752317a76f9SStephen Hemminger extern int tcp_set_default_congestion_control(const char *name);
753317a76f9SStephen Hemminger extern void tcp_get_default_congestion_control(char *name);
7543ff825b2SStephen Hemminger extern void tcp_get_available_congestion_control(char *buf, size_t len);
755ce7bc3bfSStephen Hemminger extern void tcp_get_allowed_congestion_control(char *buf, size_t len);
756ce7bc3bfSStephen Hemminger extern int tcp_set_allowed_congestion_control(char *allowed);
7576687e988SArnaldo Carvalho de Melo extern int tcp_set_congestion_control(struct sock *sk, const char *name);
75840efc6faSStephen Hemminger extern void tcp_slow_start(struct tcp_sock *tp);
759758ce5c8SIlpo Järvinen extern void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w);
760317a76f9SStephen Hemminger 
7615f8ef48dSStephen Hemminger extern struct tcp_congestion_ops tcp_init_congestion_ops;
7626687e988SArnaldo Carvalho de Melo extern u32 tcp_reno_ssthresh(struct sock *sk);
763c3a05c60SIlpo Järvinen extern void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight);
76472dc5b92SStephen Hemminger extern u32 tcp_reno_min_cwnd(const struct sock *sk);
765a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
766317a76f9SStephen Hemminger 
7676687e988SArnaldo Carvalho de Melo static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
768317a76f9SStephen Hemminger {
7696687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
7706687e988SArnaldo Carvalho de Melo 
7716687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->set_state)
7726687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->set_state(sk, ca_state);
7736687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_state = ca_state;
774317a76f9SStephen Hemminger }
775317a76f9SStephen Hemminger 
7766687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
777317a76f9SStephen Hemminger {
7786687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
7796687e988SArnaldo Carvalho de Melo 
7806687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
7816687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
782317a76f9SStephen Hemminger }
783317a76f9SStephen Hemminger 
784e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK
785e60402d0SIlpo Järvinen  * handling. SACK is negotiated with the peer, and therefore it can vary
786e60402d0SIlpo Järvinen  * between different flows.
787e60402d0SIlpo Järvinen  *
788e60402d0SIlpo Järvinen  * tcp_is_sack - SACK enabled
789e60402d0SIlpo Järvinen  * tcp_is_reno - No SACK
790e60402d0SIlpo Järvinen  * tcp_is_fack - FACK enabled, implies SACK enabled
791e60402d0SIlpo Järvinen  */
792e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp)
793e60402d0SIlpo Järvinen {
794e60402d0SIlpo Järvinen 	return tp->rx_opt.sack_ok;
795e60402d0SIlpo Järvinen }
796e60402d0SIlpo Järvinen 
797*a2a385d6SEric Dumazet static inline bool tcp_is_reno(const struct tcp_sock *tp)
798e60402d0SIlpo Järvinen {
799e60402d0SIlpo Järvinen 	return !tcp_is_sack(tp);
800e60402d0SIlpo Järvinen }
801e60402d0SIlpo Järvinen 
802*a2a385d6SEric Dumazet static inline bool tcp_is_fack(const struct tcp_sock *tp)
803e60402d0SIlpo Järvinen {
804ab56222aSVijay Subramanian 	return tp->rx_opt.sack_ok & TCP_FACK_ENABLED;
805e60402d0SIlpo Järvinen }
806e60402d0SIlpo Järvinen 
807e60402d0SIlpo Järvinen static inline void tcp_enable_fack(struct tcp_sock *tp)
808e60402d0SIlpo Järvinen {
809ab56222aSVijay Subramanian 	tp->rx_opt.sack_ok |= TCP_FACK_ENABLED;
810e60402d0SIlpo Järvinen }
811e60402d0SIlpo Järvinen 
812eed530b6SYuchung Cheng /* TCP early-retransmit (ER) is similar to but more conservative than
813eed530b6SYuchung Cheng  * the thin-dupack feature.  Enable ER only if thin-dupack is disabled.
814eed530b6SYuchung Cheng  */
815eed530b6SYuchung Cheng static inline void tcp_enable_early_retrans(struct tcp_sock *tp)
816eed530b6SYuchung Cheng {
817eed530b6SYuchung Cheng 	tp->do_early_retrans = sysctl_tcp_early_retrans &&
818eed530b6SYuchung Cheng 		!sysctl_tcp_thin_dupack && sysctl_tcp_reordering == 3;
819750ea2baSYuchung Cheng 	tp->early_retrans_delayed = 0;
820eed530b6SYuchung Cheng }
821eed530b6SYuchung Cheng 
822eed530b6SYuchung Cheng static inline void tcp_disable_early_retrans(struct tcp_sock *tp)
823eed530b6SYuchung Cheng {
824eed530b6SYuchung Cheng 	tp->do_early_retrans = 0;
825eed530b6SYuchung Cheng }
826eed530b6SYuchung Cheng 
82783ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
82883ae4088SIlpo Järvinen {
82983ae4088SIlpo Järvinen 	return tp->sacked_out + tp->lost_out;
83083ae4088SIlpo Järvinen }
83183ae4088SIlpo Järvinen 
8321da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
8331da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
8341da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
8351da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
8361da177e4SLinus Torvalds  *
8371da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
8381da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
8391da177e4SLinus Torvalds  *
8401da177e4SLinus Torvalds  * Read this equation as:
8411da177e4SLinus Torvalds  *
8421da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
8431da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
8441da177e4SLinus Torvalds  *	"Packets fast retransmitted"
8451da177e4SLinus Torvalds  */
84640efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
8471da177e4SLinus Torvalds {
84883ae4088SIlpo Järvinen 	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
8491da177e4SLinus Torvalds }
8501da177e4SLinus Torvalds 
8510b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH	0x7fffffff
8520b6a05c1SIlpo Järvinen 
8530b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
8540b6a05c1SIlpo Järvinen {
8550b6a05c1SIlpo Järvinen 	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
8560b6a05c1SIlpo Järvinen }
8570b6a05c1SIlpo Järvinen 
8581da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
8591da177e4SLinus Torvalds  * The exception is rate halving phase, when cwnd is decreasing towards
8601da177e4SLinus Torvalds  * ssthresh.
8611da177e4SLinus Torvalds  */
8626687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
8631da177e4SLinus Torvalds {
8646687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
865cf533ea5SEric Dumazet 
8666687e988SArnaldo Carvalho de Melo 	if ((1 << inet_csk(sk)->icsk_ca_state) & (TCPF_CA_CWR | TCPF_CA_Recovery))
8671da177e4SLinus Torvalds 		return tp->snd_ssthresh;
8681da177e4SLinus Torvalds 	else
8691da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
8701da177e4SLinus Torvalds 			   ((tp->snd_cwnd >> 1) +
8711da177e4SLinus Torvalds 			    (tp->snd_cwnd >> 2)));
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
874b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */
875b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
8761da177e4SLinus Torvalds 
8773cfe3baaSIlpo Järvinen extern void tcp_enter_cwr(struct sock *sk, const int set_ssthresh);
878cf533ea5SEric Dumazet extern __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst);
8791da177e4SLinus Torvalds 
8806b5a5c0dSNeal Cardwell /* The maximum number of MSS of available cwnd for which TSO defers
8816b5a5c0dSNeal Cardwell  * sending if not using sysctl_tcp_tso_win_divisor.
8826b5a5c0dSNeal Cardwell  */
8836b5a5c0dSNeal Cardwell static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp)
8846b5a5c0dSNeal Cardwell {
8856b5a5c0dSNeal Cardwell 	return 3;
8866b5a5c0dSNeal Cardwell }
8876b5a5c0dSNeal Cardwell 
8881da177e4SLinus Torvalds /* Slow start with delack produces 3 packets of burst, so that
889dd9e0ddaSJohn Heffner  * it is safe "de facto".  This will be the default - same as
890dd9e0ddaSJohn Heffner  * the default reordering threshold - but if reordering increases,
891dd9e0ddaSJohn Heffner  * we must be able to allow cwnd to burst at least this much in order
892dd9e0ddaSJohn Heffner  * to not pull it back when holes are filled.
8931da177e4SLinus Torvalds  */
8941da177e4SLinus Torvalds static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
8951da177e4SLinus Torvalds {
896dd9e0ddaSJohn Heffner 	return tp->reordering;
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
89990840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */
90090840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
90190840defSIlpo Järvinen {
90290840defSIlpo Järvinen 	return tp->snd_una + tp->snd_wnd;
90390840defSIlpo Järvinen }
904*a2a385d6SEric Dumazet extern bool tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight);
905f4805edeSStephen Hemminger 
906c1bd24b7SChuck Lever static inline void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss,
9071da177e4SLinus Torvalds 				       const struct sk_buff *skb)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	if (skb->len < mss)
9101da177e4SLinus Torvalds 		tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
9111da177e4SLinus Torvalds }
9121da177e4SLinus Torvalds 
9139e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk)
9141da177e4SLinus Torvalds {
915cf533ea5SEric Dumazet 	const struct tcp_sock *tp = tcp_sk(sk);
916463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
9179e412ba7SIlpo Järvinen 
918463c84b9SArnaldo Carvalho de Melo 	if (!tp->packets_out && !icsk->icsk_pending)
9193f421baaSArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
9203f421baaSArnaldo Carvalho de Melo 					  icsk->icsk_rto, TCP_RTO_MAX);
9211da177e4SLinus Torvalds }
9221da177e4SLinus Torvalds 
923ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
928ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
9291da177e4SLinus Torvalds {
9301da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds /*
9341da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
9351da177e4SLinus Torvalds  */
936ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr,
937ba7808eaSFrederik Deweerdt 				   __be32 daddr, __wsum base)
9381da177e4SLinus Torvalds {
9391da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
9401da177e4SLinus Torvalds }
9411da177e4SLinus Torvalds 
942b51655b9SAl Viro static inline __sum16 __tcp_checksum_complete(struct sk_buff *skb)
9431da177e4SLinus Torvalds {
944fb286bb2SHerbert Xu 	return __skb_checksum_complete(skb);
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
947*a2a385d6SEric Dumazet static inline bool tcp_checksum_complete(struct sk_buff *skb)
9481da177e4SLinus Torvalds {
94960476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
9501da177e4SLinus Torvalds 		__tcp_checksum_complete(skb);
9511da177e4SLinus Torvalds }
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds /* Prequeue for VJ style copy to user, combined with checksumming. */
9541da177e4SLinus Torvalds 
95540efc6faSStephen Hemminger static inline void tcp_prequeue_init(struct tcp_sock *tp)
9561da177e4SLinus Torvalds {
9571da177e4SLinus Torvalds 	tp->ucopy.task = NULL;
9581da177e4SLinus Torvalds 	tp->ucopy.len = 0;
9591da177e4SLinus Torvalds 	tp->ucopy.memory = 0;
9601da177e4SLinus Torvalds 	skb_queue_head_init(&tp->ucopy.prequeue);
96197fc2f08SChris Leech #ifdef CONFIG_NET_DMA
96297fc2f08SChris Leech 	tp->ucopy.dma_chan = NULL;
96397fc2f08SChris Leech 	tp->ucopy.wakeup = 0;
96497fc2f08SChris Leech 	tp->ucopy.pinned_list = NULL;
96597fc2f08SChris Leech 	tp->ucopy.dma_cookie = 0;
96697fc2f08SChris Leech #endif
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds /* Packet is added to VJ-style prequeue for processing in process
9701da177e4SLinus Torvalds  * context, if a reader task is waiting. Apparently, this exciting
9711da177e4SLinus Torvalds  * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93)
9721da177e4SLinus Torvalds  * failed somewhere. Latency? Burstiness? Well, at least now we will
9731da177e4SLinus Torvalds  * see, why it failed. 8)8)				  --ANK
9741da177e4SLinus Torvalds  *
9751da177e4SLinus Torvalds  * NOTE: is this not too big to inline?
9761da177e4SLinus Torvalds  */
977*a2a385d6SEric Dumazet static inline bool tcp_prequeue(struct sock *sk, struct sk_buff *skb)
9781da177e4SLinus Torvalds {
9791da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
9801da177e4SLinus Torvalds 
981f5f8d86bSEric Dumazet 	if (sysctl_tcp_low_latency || !tp->ucopy.task)
982*a2a385d6SEric Dumazet 		return false;
983f5f8d86bSEric Dumazet 
9841da177e4SLinus Torvalds 	__skb_queue_tail(&tp->ucopy.prequeue, skb);
9851da177e4SLinus Torvalds 	tp->ucopy.memory += skb->truesize;
9861da177e4SLinus Torvalds 	if (tp->ucopy.memory > sk->sk_rcvbuf) {
9871da177e4SLinus Torvalds 		struct sk_buff *skb1;
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 		BUG_ON(sock_owned_by_user(sk));
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 		while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) {
992c57943a1SPeter Zijlstra 			sk_backlog_rcv(sk, skb1);
993f5f8d86bSEric Dumazet 			NET_INC_STATS_BH(sock_net(sk),
994f5f8d86bSEric Dumazet 					 LINUX_MIB_TCPPREQUEUEDROPPED);
9951da177e4SLinus Torvalds 		}
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 		tp->ucopy.memory = 0;
9981da177e4SLinus Torvalds 	} else if (skb_queue_len(&tp->ucopy.prequeue) == 1) {
999aa395145SEric Dumazet 		wake_up_interruptible_sync_poll(sk_sleep(sk),
10007aedec2aSEric Dumazet 					   POLLIN | POLLRDNORM | POLLRDBAND);
1001463c84b9SArnaldo Carvalho de Melo 		if (!inet_csk_ack_scheduled(sk))
1002463c84b9SArnaldo Carvalho de Melo 			inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
10030c266898SSatoru SATOH 						  (3 * tcp_rto_min(sk)) / 4,
10043f421baaSArnaldo Carvalho de Melo 						  TCP_RTO_MAX);
10051da177e4SLinus Torvalds 	}
1006*a2a385d6SEric Dumazet 	return true;
10071da177e4SLinus Torvalds }
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds #undef STATE_TRACE
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds #ifdef STATE_TRACE
10131da177e4SLinus Torvalds static const char *statename[]={
10141da177e4SLinus Torvalds 	"Unused","Established","Syn Sent","Syn Recv",
10151da177e4SLinus Torvalds 	"Fin Wait 1","Fin Wait 2","Time Wait", "Close",
10161da177e4SLinus Torvalds 	"Close Wait","Last ACK","Listen","Closing"
10171da177e4SLinus Torvalds };
10181da177e4SLinus Torvalds #endif
1019490d5046SIlpo Järvinen extern void tcp_set_state(struct sock *sk, int state);
10201da177e4SLinus Torvalds 
10214ac02babSAndi Kleen extern void tcp_done(struct sock *sk);
10221da177e4SLinus Torvalds 
102340efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
10241da177e4SLinus Torvalds {
10251da177e4SLinus Torvalds 	rx_opt->dsack = 0;
10261da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
10271da177e4SLinus Torvalds }
10281da177e4SLinus Torvalds 
10291da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
10301da177e4SLinus Torvalds extern void tcp_select_initial_window(int __space, __u32 mss,
10311da177e4SLinus Torvalds 				      __u32 *rcv_wnd, __u32 *window_clamp,
103231d12926Slaurent chavey 				      int wscale_ok, __u8 *rcv_wscale,
103331d12926Slaurent chavey 				      __u32 init_rcv_wnd);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds static inline int tcp_win_from_space(int space)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	return sysctl_tcp_adv_win_scale<=0 ?
10381da177e4SLinus Torvalds 		(space>>(-sysctl_tcp_adv_win_scale)) :
10391da177e4SLinus Torvalds 		space - (space>>sysctl_tcp_adv_win_scale);
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
10431da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
10441da177e4SLinus Torvalds {
10451da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf -
10461da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
10471da177e4SLinus Torvalds }
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
10501da177e4SLinus Torvalds {
10511da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf);
10521da177e4SLinus Torvalds }
10531da177e4SLinus Torvalds 
105440efc6faSStephen Hemminger static inline void tcp_openreq_init(struct request_sock *req,
10551da177e4SLinus Torvalds 				    struct tcp_options_received *rx_opt,
10561da177e4SLinus Torvalds 				    struct sk_buff *skb)
10571da177e4SLinus Torvalds {
10582e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq = inet_rsk(req);
10592e6599cbSArnaldo Carvalho de Melo 
10601da177e4SLinus Torvalds 	req->rcv_wnd = 0;		/* So that tcp_send_synack() knows! */
10614dfc2817SFlorian Westphal 	req->cookie_ts = 0;
10622e6599cbSArnaldo Carvalho de Melo 	tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
10631da177e4SLinus Torvalds 	req->mss = rx_opt->mss_clamp;
10641da177e4SLinus Torvalds 	req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
10652e6599cbSArnaldo Carvalho de Melo 	ireq->tstamp_ok = rx_opt->tstamp_ok;
10662e6599cbSArnaldo Carvalho de Melo 	ireq->sack_ok = rx_opt->sack_ok;
10672e6599cbSArnaldo Carvalho de Melo 	ireq->snd_wscale = rx_opt->snd_wscale;
10682e6599cbSArnaldo Carvalho de Melo 	ireq->wscale_ok = rx_opt->wscale_ok;
10692e6599cbSArnaldo Carvalho de Melo 	ireq->acked = 0;
10702e6599cbSArnaldo Carvalho de Melo 	ireq->ecn_ok = 0;
1071aa8223c7SArnaldo Carvalho de Melo 	ireq->rmt_port = tcp_hdr(skb)->source;
1072a3116ac5SKOVACS Krisztian 	ireq->loc_port = tcp_hdr(skb)->dest;
10731da177e4SLinus Torvalds }
10741da177e4SLinus Torvalds 
10755c52ba17SPavel Emelyanov extern void tcp_enter_memory_pressure(struct sock *sk);
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
10781da177e4SLinus Torvalds {
10791da177e4SLinus Torvalds 	return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
10831da177e4SLinus Torvalds {
10841da177e4SLinus Torvalds 	return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
1087df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp)
1088df19a626SEric Dumazet {
1089df19a626SEric Dumazet 	return tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
1090df19a626SEric Dumazet }
1091df19a626SEric Dumazet 
10926c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
10936c37e5deSFlavio Leitner {
10946c37e5deSFlavio Leitner 	const struct inet_connection_sock *icsk = &tp->inet_conn;
10956c37e5deSFlavio Leitner 
10966c37e5deSFlavio Leitner 	return min_t(u32, tcp_time_stamp - icsk->icsk_ack.lrcvtime,
10976c37e5deSFlavio Leitner 			  tcp_time_stamp - tp->rcv_tstamp);
10986c37e5deSFlavio Leitner }
10996c37e5deSFlavio Leitner 
1100463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
11011da177e4SLinus Torvalds {
1102463c84b9SArnaldo Carvalho de Melo 	int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
1103463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
11041da177e4SLinus Torvalds 
1105463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1106463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 	return fin_timeout;
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
1111*a2a385d6SEric Dumazet static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt,
1112c887e6d2SIlpo Järvinen 				  int paws_win)
11131da177e4SLinus Torvalds {
1114c887e6d2SIlpo Järvinen 	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1115*a2a385d6SEric Dumazet 		return true;
1116c887e6d2SIlpo Järvinen 	if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS))
1117*a2a385d6SEric Dumazet 		return true;
1118bc2ce894SEric Dumazet 	/*
1119bc2ce894SEric Dumazet 	 * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0,
1120bc2ce894SEric Dumazet 	 * then following tcp messages have valid values. Ignore 0 value,
1121bc2ce894SEric Dumazet 	 * or else 'negative' tsval might forbid us to accept their packets.
1122bc2ce894SEric Dumazet 	 */
1123bc2ce894SEric Dumazet 	if (!rx_opt->ts_recent)
1124*a2a385d6SEric Dumazet 		return true;
1125*a2a385d6SEric Dumazet 	return false;
1126c887e6d2SIlpo Järvinen }
1127c887e6d2SIlpo Järvinen 
1128*a2a385d6SEric Dumazet static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt,
1129c887e6d2SIlpo Järvinen 				   int rst)
1130c887e6d2SIlpo Järvinen {
1131c887e6d2SIlpo Järvinen 	if (tcp_paws_check(rx_opt, 0))
1132*a2a385d6SEric Dumazet 		return false;
11331da177e4SLinus Torvalds 
11341da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
11351da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
11361da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
11371da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
11381da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
11391da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
11401da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
11411da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
11421da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
11451da177e4SLinus Torvalds 	 */
11469d729f72SJames Morris 	if (rst && get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
1147*a2a385d6SEric Dumazet 		return false;
1148*a2a385d6SEric Dumazet 	return true;
11491da177e4SLinus Torvalds }
11501da177e4SLinus Torvalds 
1151a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net)
11521da177e4SLinus Torvalds {
11531da177e4SLinus Torvalds 	/* See RFC 2012 */
1154cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOALGORITHM, 1);
1155cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
1156cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
1157cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_MAXCONN, -1);
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
11606a438bbeSStephen Hemminger /* from STCP */
1161ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
11620800f170SDavid S. Miller {
11636a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
11646a438bbeSStephen Hemminger 	tp->scoreboard_skb_hint = NULL;
1165ef9da47cSIlpo Järvinen }
1166ef9da47cSIlpo Järvinen 
1167ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
1168ef9da47cSIlpo Järvinen {
1169ef9da47cSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
11706a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
1171b7689205SIlpo Järvinen }
1172b7689205SIlpo Järvinen 
1173cfb6eeb4SYOSHIFUJI Hideaki /* MD5 Signature */
1174cfb6eeb4SYOSHIFUJI Hideaki struct crypto_hash;
1175cfb6eeb4SYOSHIFUJI Hideaki 
1176a915da9bSEric Dumazet union tcp_md5_addr {
1177a915da9bSEric Dumazet 	struct in_addr  a4;
1178a915da9bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1179a915da9bSEric Dumazet 	struct in6_addr	a6;
1180a915da9bSEric Dumazet #endif
1181a915da9bSEric Dumazet };
1182a915da9bSEric Dumazet 
1183cfb6eeb4SYOSHIFUJI Hideaki /* - key database */
1184cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key {
1185a915da9bSEric Dumazet 	struct hlist_node	node;
1186cfb6eeb4SYOSHIFUJI Hideaki 	u8			keylen;
1187a915da9bSEric Dumazet 	u8			family; /* AF_INET or AF_INET6 */
1188a915da9bSEric Dumazet 	union tcp_md5_addr	addr;
1189a915da9bSEric Dumazet 	u8			key[TCP_MD5SIG_MAXKEYLEN];
1190a915da9bSEric Dumazet 	struct rcu_head		rcu;
1191cfb6eeb4SYOSHIFUJI Hideaki };
1192cfb6eeb4SYOSHIFUJI Hideaki 
1193cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */
1194cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info {
1195a915da9bSEric Dumazet 	struct hlist_head	head;
1196a8afca03SEric Dumazet 	struct rcu_head		rcu;
1197cfb6eeb4SYOSHIFUJI Hideaki };
1198cfb6eeb4SYOSHIFUJI Hideaki 
1199cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */
1200cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr {
1201cfb6eeb4SYOSHIFUJI Hideaki 	__be32		saddr;
1202cfb6eeb4SYOSHIFUJI Hideaki 	__be32		daddr;
1203cfb6eeb4SYOSHIFUJI Hideaki 	__u8		pad;
1204cfb6eeb4SYOSHIFUJI Hideaki 	__u8		protocol;
1205cfb6eeb4SYOSHIFUJI Hideaki 	__be16		len;
1206cfb6eeb4SYOSHIFUJI Hideaki };
1207cfb6eeb4SYOSHIFUJI Hideaki 
1208cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr {
1209cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr	saddr;
1210cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr daddr;
1211cfb6eeb4SYOSHIFUJI Hideaki 	__be32		len;
1212cfb6eeb4SYOSHIFUJI Hideaki 	__be32		protocol;	/* including padding */
1213cfb6eeb4SYOSHIFUJI Hideaki };
1214cfb6eeb4SYOSHIFUJI Hideaki 
1215cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block {
1216cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr ip4;
1217dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
1218cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_pseudohdr ip6;
1219cfb6eeb4SYOSHIFUJI Hideaki #endif
1220cfb6eeb4SYOSHIFUJI Hideaki };
1221cfb6eeb4SYOSHIFUJI Hideaki 
1222cfb6eeb4SYOSHIFUJI Hideaki /* - pool: digest algorithm, hash description and scratch buffer */
1223cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool {
1224cfb6eeb4SYOSHIFUJI Hideaki 	struct hash_desc	md5_desc;
1225cfb6eeb4SYOSHIFUJI Hideaki 	union tcp_md5sum_block	md5_blk;
1226cfb6eeb4SYOSHIFUJI Hideaki };
1227cfb6eeb4SYOSHIFUJI Hideaki 
1228cfb6eeb4SYOSHIFUJI Hideaki /* - functions */
122953d3176bSChangli Gao extern int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
1230318cf7aaSEric Dumazet 			       const struct sock *sk,
1231318cf7aaSEric Dumazet 			       const struct request_sock *req,
1232318cf7aaSEric Dumazet 			       const struct sk_buff *skb);
1233a915da9bSEric Dumazet extern int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
1234a915da9bSEric Dumazet 			  int family, const u8 *newkey,
1235a915da9bSEric Dumazet 			  u8 newkeylen, gfp_t gfp);
1236a915da9bSEric Dumazet extern int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr,
1237a915da9bSEric Dumazet 			  int family);
1238cfb6eeb4SYOSHIFUJI Hideaki extern struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
1239cfb6eeb4SYOSHIFUJI Hideaki 					 struct sock *addr_sk);
1240cfb6eeb4SYOSHIFUJI Hideaki 
12419501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1242a915da9bSEric Dumazet extern struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
1243a915da9bSEric Dumazet 			const union tcp_md5_addr *addr, int family);
1244a915da9bSEric Dumazet #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_key)
12459501f972SYOSHIFUJI Hideaki #else
1246a915da9bSEric Dumazet static inline struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
1247a915da9bSEric Dumazet 					 const union tcp_md5_addr *addr,
1248a915da9bSEric Dumazet 					 int family)
1249a915da9bSEric Dumazet {
1250a915da9bSEric Dumazet 	return NULL;
1251a915da9bSEric Dumazet }
12529501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	NULL
12539501f972SYOSHIFUJI Hideaki #endif
12549501f972SYOSHIFUJI Hideaki 
1255765cf997SEric Dumazet extern struct tcp_md5sig_pool __percpu *tcp_alloc_md5sig_pool(struct sock *);
1256cfb6eeb4SYOSHIFUJI Hideaki extern void tcp_free_md5sig_pool(void);
1257cfb6eeb4SYOSHIFUJI Hideaki 
125835790c04SEric Dumazet extern struct tcp_md5sig_pool	*tcp_get_md5sig_pool(void);
125935790c04SEric Dumazet extern void tcp_put_md5sig_pool(void);
126035790c04SEric Dumazet 
1261ca35a0efSEric Dumazet extern int tcp_md5_hash_header(struct tcp_md5sig_pool *, const struct tcphdr *);
1262cf533ea5SEric Dumazet extern int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *,
126395c96174SEric Dumazet 				 unsigned int header_len);
126449a72dfbSAdam Langley extern int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
1265cf533ea5SEric Dumazet 			    const struct tcp_md5sig_key *key);
1266cfb6eeb4SYOSHIFUJI Hideaki 
1267fe067e8aSDavid S. Miller /* write queue abstraction */
1268fe067e8aSDavid S. Miller static inline void tcp_write_queue_purge(struct sock *sk)
1269fe067e8aSDavid S. Miller {
1270fe067e8aSDavid S. Miller 	struct sk_buff *skb;
1271fe067e8aSDavid S. Miller 
1272fe067e8aSDavid S. Miller 	while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL)
12733ab224beSHideo Aoki 		sk_wmem_free_skb(sk, skb);
12743ab224beSHideo Aoki 	sk_mem_reclaim(sk);
12758818a9d8SIlpo Järvinen 	tcp_clear_all_retrans_hints(tcp_sk(sk));
1276fe067e8aSDavid S. Miller }
1277fe067e8aSDavid S. Miller 
1278cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk)
1279fe067e8aSDavid S. Miller {
1280cd07a8eaSDavid S. Miller 	return skb_peek(&sk->sk_write_queue);
1281fe067e8aSDavid S. Miller }
1282fe067e8aSDavid S. Miller 
1283cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk)
1284fe067e8aSDavid S. Miller {
1285cd07a8eaSDavid S. Miller 	return skb_peek_tail(&sk->sk_write_queue);
1286fe067e8aSDavid S. Miller }
1287fe067e8aSDavid S. Miller 
1288cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_next(const struct sock *sk,
1289cf533ea5SEric Dumazet 						   const struct sk_buff *skb)
1290fe067e8aSDavid S. Miller {
1291cd07a8eaSDavid S. Miller 	return skb_queue_next(&sk->sk_write_queue, skb);
1292fe067e8aSDavid S. Miller }
1293fe067e8aSDavid S. Miller 
1294cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_prev(const struct sock *sk,
1295cf533ea5SEric Dumazet 						   const struct sk_buff *skb)
1296832d11c5SIlpo Järvinen {
1297832d11c5SIlpo Järvinen 	return skb_queue_prev(&sk->sk_write_queue, skb);
1298832d11c5SIlpo Järvinen }
1299832d11c5SIlpo Järvinen 
1300fe067e8aSDavid S. Miller #define tcp_for_write_queue(skb, sk)					\
1301cd07a8eaSDavid S. Miller 	skb_queue_walk(&(sk)->sk_write_queue, skb)
1302fe067e8aSDavid S. Miller 
1303fe067e8aSDavid S. Miller #define tcp_for_write_queue_from(skb, sk)				\
1304cd07a8eaSDavid S. Miller 	skb_queue_walk_from(&(sk)->sk_write_queue, skb)
1305fe067e8aSDavid S. Miller 
1306234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
1307cd07a8eaSDavid S. Miller 	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1308234b6860SIlpo Järvinen 
1309cf533ea5SEric Dumazet static inline struct sk_buff *tcp_send_head(const struct sock *sk)
1310fe067e8aSDavid S. Miller {
1311fe067e8aSDavid S. Miller 	return sk->sk_send_head;
1312fe067e8aSDavid S. Miller }
1313fe067e8aSDavid S. Miller 
1314cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk,
1315cd07a8eaSDavid S. Miller 				   const struct sk_buff *skb)
1316cd07a8eaSDavid S. Miller {
1317cd07a8eaSDavid S. Miller 	return skb_queue_is_last(&sk->sk_write_queue, skb);
1318cd07a8eaSDavid S. Miller }
1319cd07a8eaSDavid S. Miller 
1320cf533ea5SEric Dumazet static inline void tcp_advance_send_head(struct sock *sk, const struct sk_buff *skb)
1321fe067e8aSDavid S. Miller {
1322cd07a8eaSDavid S. Miller 	if (tcp_skb_is_last(sk, skb))
1323fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1324cd07a8eaSDavid S. Miller 	else
1325cd07a8eaSDavid S. Miller 		sk->sk_send_head = tcp_write_queue_next(sk, skb);
1326fe067e8aSDavid S. Miller }
1327fe067e8aSDavid S. Miller 
1328fe067e8aSDavid S. Miller static inline void tcp_check_send_head(struct sock *sk, struct sk_buff *skb_unlinked)
1329fe067e8aSDavid S. Miller {
1330fe067e8aSDavid S. Miller 	if (sk->sk_send_head == skb_unlinked)
1331fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1332fe067e8aSDavid S. Miller }
1333fe067e8aSDavid S. Miller 
1334fe067e8aSDavid S. Miller static inline void tcp_init_send_head(struct sock *sk)
1335fe067e8aSDavid S. Miller {
1336fe067e8aSDavid S. Miller 	sk->sk_send_head = NULL;
1337fe067e8aSDavid S. Miller }
1338fe067e8aSDavid S. Miller 
1339fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1340fe067e8aSDavid S. Miller {
1341fe067e8aSDavid S. Miller 	__skb_queue_tail(&sk->sk_write_queue, skb);
1342fe067e8aSDavid S. Miller }
1343fe067e8aSDavid S. Miller 
1344fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1345fe067e8aSDavid S. Miller {
1346fe067e8aSDavid S. Miller 	__tcp_add_write_queue_tail(sk, skb);
1347fe067e8aSDavid S. Miller 
1348fe067e8aSDavid S. Miller 	/* Queue it, remembering where we must start sending. */
13496859d494SIlpo Järvinen 	if (sk->sk_send_head == NULL) {
1350fe067e8aSDavid S. Miller 		sk->sk_send_head = skb;
13516859d494SIlpo Järvinen 
13526859d494SIlpo Järvinen 		if (tcp_sk(sk)->highest_sack == NULL)
13536859d494SIlpo Järvinen 			tcp_sk(sk)->highest_sack = skb;
13546859d494SIlpo Järvinen 	}
1355fe067e8aSDavid S. Miller }
1356fe067e8aSDavid S. Miller 
1357fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_head(struct sock *sk, struct sk_buff *skb)
1358fe067e8aSDavid S. Miller {
1359fe067e8aSDavid S. Miller 	__skb_queue_head(&sk->sk_write_queue, skb);
1360fe067e8aSDavid S. Miller }
1361fe067e8aSDavid S. Miller 
1362fe067e8aSDavid S. Miller /* Insert buff after skb on the write queue of sk.  */
1363fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_after(struct sk_buff *skb,
1364fe067e8aSDavid S. Miller 						struct sk_buff *buff,
1365fe067e8aSDavid S. Miller 						struct sock *sk)
1366fe067e8aSDavid S. Miller {
13677de6c033SGerrit Renker 	__skb_queue_after(&sk->sk_write_queue, skb, buff);
1368fe067e8aSDavid S. Miller }
1369fe067e8aSDavid S. Miller 
137043f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk.  */
1371fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new,
1372fe067e8aSDavid S. Miller 						  struct sk_buff *skb,
1373fe067e8aSDavid S. Miller 						  struct sock *sk)
1374fe067e8aSDavid S. Miller {
137543f59c89SDavid S. Miller 	__skb_queue_before(&sk->sk_write_queue, skb, new);
13766e421410SIlpo Järvinen 
13776e421410SIlpo Järvinen 	if (sk->sk_send_head == skb)
13786e421410SIlpo Järvinen 		sk->sk_send_head = new;
1379fe067e8aSDavid S. Miller }
1380fe067e8aSDavid S. Miller 
1381fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
1382fe067e8aSDavid S. Miller {
1383fe067e8aSDavid S. Miller 	__skb_unlink(skb, &sk->sk_write_queue);
1384fe067e8aSDavid S. Miller }
1385fe067e8aSDavid S. Miller 
1386*a2a385d6SEric Dumazet static inline bool tcp_write_queue_empty(struct sock *sk)
1387fe067e8aSDavid S. Miller {
1388fe067e8aSDavid S. Miller 	return skb_queue_empty(&sk->sk_write_queue);
1389fe067e8aSDavid S. Miller }
1390fe067e8aSDavid S. Miller 
139112d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk)
139212d50c46SKrishna Kumar {
139312d50c46SKrishna Kumar 	if (tcp_send_head(sk)) {
139412d50c46SKrishna Kumar 		struct tcp_sock *tp = tcp_sk(sk);
139512d50c46SKrishna Kumar 
139612d50c46SKrishna Kumar 		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
139712d50c46SKrishna Kumar 	}
139812d50c46SKrishna Kumar }
139912d50c46SKrishna Kumar 
1400ecb97192SNeal Cardwell /* Start sequence of the skb just after the highest skb with SACKed
1401ecb97192SNeal Cardwell  * bit, valid only if sacked_out > 0 or when the caller has ensured
1402ecb97192SNeal Cardwell  * validity by itself.
1403a47e5a98SIlpo Järvinen  */
1404a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
1405a47e5a98SIlpo Järvinen {
1406a47e5a98SIlpo Järvinen 	if (!tp->sacked_out)
1407a47e5a98SIlpo Järvinen 		return tp->snd_una;
14086859d494SIlpo Järvinen 
14096859d494SIlpo Järvinen 	if (tp->highest_sack == NULL)
14106859d494SIlpo Järvinen 		return tp->snd_nxt;
14116859d494SIlpo Järvinen 
1412a47e5a98SIlpo Järvinen 	return TCP_SKB_CB(tp->highest_sack)->seq;
1413a47e5a98SIlpo Järvinen }
1414a47e5a98SIlpo Järvinen 
14156859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
14166859d494SIlpo Järvinen {
14176859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_skb_is_last(sk, skb) ? NULL :
14186859d494SIlpo Järvinen 						tcp_write_queue_next(sk, skb);
14196859d494SIlpo Järvinen }
14206859d494SIlpo Järvinen 
14216859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
14226859d494SIlpo Järvinen {
14236859d494SIlpo Järvinen 	return tcp_sk(sk)->highest_sack;
14246859d494SIlpo Järvinen }
14256859d494SIlpo Järvinen 
14266859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk)
14276859d494SIlpo Järvinen {
14286859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_write_queue_head(sk);
14296859d494SIlpo Järvinen }
14306859d494SIlpo Järvinen 
14316859d494SIlpo Järvinen /* Called when old skb is about to be deleted (to be combined with new skb) */
14326859d494SIlpo Järvinen static inline void tcp_highest_sack_combine(struct sock *sk,
14336859d494SIlpo Järvinen 					    struct sk_buff *old,
14346859d494SIlpo Järvinen 					    struct sk_buff *new)
14356859d494SIlpo Järvinen {
14366859d494SIlpo Järvinen 	if (tcp_sk(sk)->sacked_out && (old == tcp_sk(sk)->highest_sack))
14376859d494SIlpo Järvinen 		tcp_sk(sk)->highest_sack = new;
14386859d494SIlpo Järvinen }
14396859d494SIlpo Järvinen 
14405aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from
14415aa4b32fSAndreas Petlund  * increased latency). Used to trigger latency-reducing mechanisms.
14425aa4b32fSAndreas Petlund  */
1443*a2a385d6SEric Dumazet static inline bool tcp_stream_is_thin(struct tcp_sock *tp)
14445aa4b32fSAndreas Petlund {
14455aa4b32fSAndreas Petlund 	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
14465aa4b32fSAndreas Petlund }
14475aa4b32fSAndreas Petlund 
14481da177e4SLinus Torvalds /* /proc */
14491da177e4SLinus Torvalds enum tcp_seq_states {
14501da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
14511da177e4SLinus Torvalds 	TCP_SEQ_STATE_OPENREQ,
14521da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
14531da177e4SLinus Torvalds 	TCP_SEQ_STATE_TIME_WAIT,
14541da177e4SLinus Torvalds };
14551da177e4SLinus Torvalds 
145673cb88ecSArjan van de Ven int tcp_seq_open(struct inode *inode, struct file *file);
145773cb88ecSArjan van de Ven 
14581da177e4SLinus Torvalds struct tcp_seq_afinfo {
14591da177e4SLinus Torvalds 	char				*name;
14601da177e4SLinus Torvalds 	sa_family_t			family;
146173cb88ecSArjan van de Ven 	const struct file_operations	*seq_fops;
14629427c4b3SDenis V. Lunev 	struct seq_operations		seq_ops;
14631da177e4SLinus Torvalds };
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds struct tcp_iter_state {
1466a4146b1bSDenis V. Lunev 	struct seq_net_private	p;
14671da177e4SLinus Torvalds 	sa_family_t		family;
14681da177e4SLinus Torvalds 	enum tcp_seq_states	state;
14691da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
1470a8b690f9STom Herbert 	int			bucket, offset, sbucket, num, uid;
1471a8b690f9STom Herbert 	loff_t			last_pos;
14721da177e4SLinus Torvalds };
14731da177e4SLinus Torvalds 
14746f8b13bcSDaniel Lezcano extern int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo);
14756f8b13bcSDaniel Lezcano extern void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo);
14761da177e4SLinus Torvalds 
147720380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
1478c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops;
147920380731SArnaldo Carvalho de Melo 
14807d06b2e0SBrian Haley extern void tcp_v4_destroy_sock(struct sock *sk);
148120380731SArnaldo Carvalho de Melo 
1482a430a43dSHerbert Xu extern int tcp_v4_gso_send_check(struct sk_buff *skb);
1483c8f44affSMichał Mirosław extern struct sk_buff *tcp_tso_segment(struct sk_buff *skb,
1484c8f44affSMichał Mirosław 				       netdev_features_t features);
1485bf296b12SHerbert Xu extern struct sk_buff **tcp_gro_receive(struct sk_buff **head,
1486bf296b12SHerbert Xu 					struct sk_buff *skb);
1487bf296b12SHerbert Xu extern struct sk_buff **tcp4_gro_receive(struct sk_buff **head,
1488bf296b12SHerbert Xu 					 struct sk_buff *skb);
1489bf296b12SHerbert Xu extern int tcp_gro_complete(struct sk_buff *skb);
1490bf296b12SHerbert Xu extern int tcp4_gro_complete(struct sk_buff *skb);
1491f4c50d99SHerbert Xu 
149220380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
149320380731SArnaldo Carvalho de Melo extern int tcp4_proc_init(void);
149420380731SArnaldo Carvalho de Melo extern void tcp4_proc_exit(void);
149520380731SArnaldo Carvalho de Melo #endif
149620380731SArnaldo Carvalho de Melo 
1497cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */
1498cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops {
1499cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1500cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key	*(*md5_lookup) (struct sock *sk,
1501cfb6eeb4SYOSHIFUJI Hideaki 						struct sock *addr_sk);
1502cfb6eeb4SYOSHIFUJI Hideaki 	int			(*calc_md5_hash) (char *location,
1503cfb6eeb4SYOSHIFUJI Hideaki 						  struct tcp_md5sig_key *md5,
1504318cf7aaSEric Dumazet 						  const struct sock *sk,
1505318cf7aaSEric Dumazet 						  const struct request_sock *req,
1506318cf7aaSEric Dumazet 						  const struct sk_buff *skb);
1507cfb6eeb4SYOSHIFUJI Hideaki 	int			(*md5_parse) (struct sock *sk,
1508cfb6eeb4SYOSHIFUJI Hideaki 					      char __user *optval,
1509cfb6eeb4SYOSHIFUJI Hideaki 					      int optlen);
1510cfb6eeb4SYOSHIFUJI Hideaki #endif
1511cfb6eeb4SYOSHIFUJI Hideaki };
1512cfb6eeb4SYOSHIFUJI Hideaki 
1513cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops {
1514cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1515cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key	*(*md5_lookup) (struct sock *sk,
1516cfb6eeb4SYOSHIFUJI Hideaki 						struct request_sock *req);
1517e3afe7b7SJohn Dykstra 	int			(*calc_md5_hash) (char *location,
1518e3afe7b7SJohn Dykstra 						  struct tcp_md5sig_key *md5,
1519318cf7aaSEric Dumazet 						  const struct sock *sk,
1520318cf7aaSEric Dumazet 						  const struct request_sock *req,
1521318cf7aaSEric Dumazet 						  const struct sk_buff *skb);
1522cfb6eeb4SYOSHIFUJI Hideaki #endif
1523cfb6eeb4SYOSHIFUJI Hideaki };
1524cfb6eeb4SYOSHIFUJI Hideaki 
1525da5c78c8SWilliam Allen Simpson /* Using SHA1 for now, define some constants.
1526da5c78c8SWilliam Allen Simpson  */
1527da5c78c8SWilliam Allen Simpson #define COOKIE_DIGEST_WORDS (SHA_DIGEST_WORDS)
1528da5c78c8SWilliam Allen Simpson #define COOKIE_MESSAGE_WORDS (SHA_MESSAGE_BYTES / 4)
1529da5c78c8SWilliam Allen Simpson #define COOKIE_WORKSPACE_WORDS (COOKIE_DIGEST_WORDS + COOKIE_MESSAGE_WORDS)
1530da5c78c8SWilliam Allen Simpson 
1531da5c78c8SWilliam Allen Simpson extern int tcp_cookie_generator(u32 *bakery);
1532da5c78c8SWilliam Allen Simpson 
1533435cf559SWilliam Allen Simpson /**
1534435cf559SWilliam Allen Simpson  *	struct tcp_cookie_values - each socket needs extra space for the
1535435cf559SWilliam Allen Simpson  *	cookies, together with (optional) space for any SYN data.
1536435cf559SWilliam Allen Simpson  *
1537435cf559SWilliam Allen Simpson  *	A tcp_sock contains a pointer to the current value, and this is
1538435cf559SWilliam Allen Simpson  *	cloned to the tcp_timewait_sock.
1539435cf559SWilliam Allen Simpson  *
1540435cf559SWilliam Allen Simpson  * @cookie_pair:	variable data from the option exchange.
1541435cf559SWilliam Allen Simpson  *
1542435cf559SWilliam Allen Simpson  * @cookie_desired:	user specified tcpct_cookie_desired.  Zero
1543435cf559SWilliam Allen Simpson  *			indicates default (sysctl_tcp_cookie_size).
1544435cf559SWilliam Allen Simpson  *			After cookie sent, remembers size of cookie.
1545435cf559SWilliam Allen Simpson  *			Range 0, TCP_COOKIE_MIN to TCP_COOKIE_MAX.
1546435cf559SWilliam Allen Simpson  *
1547435cf559SWilliam Allen Simpson  * @s_data_desired:	user specified tcpct_s_data_desired.  When the
1548435cf559SWilliam Allen Simpson  *			constant payload is specified (@s_data_constant),
1549435cf559SWilliam Allen Simpson  *			holds its length instead.
1550435cf559SWilliam Allen Simpson  *			Range 0 to TCP_MSS_DESIRED.
1551435cf559SWilliam Allen Simpson  *
1552435cf559SWilliam Allen Simpson  * @s_data_payload:	constant data that is to be included in the
1553435cf559SWilliam Allen Simpson  *			payload of SYN or SYNACK segments when the
1554435cf559SWilliam Allen Simpson  *			cookie option is present.
1555435cf559SWilliam Allen Simpson  */
1556435cf559SWilliam Allen Simpson struct tcp_cookie_values {
1557435cf559SWilliam Allen Simpson 	struct kref	kref;
1558435cf559SWilliam Allen Simpson 	u8		cookie_pair[TCP_COOKIE_PAIR_SIZE];
1559435cf559SWilliam Allen Simpson 	u8		cookie_pair_size;
1560435cf559SWilliam Allen Simpson 	u8		cookie_desired;
1561435cf559SWilliam Allen Simpson 	u16		s_data_desired:11,
1562435cf559SWilliam Allen Simpson 			s_data_constant:1,
1563435cf559SWilliam Allen Simpson 			s_data_in:1,
1564435cf559SWilliam Allen Simpson 			s_data_out:1,
1565435cf559SWilliam Allen Simpson 			s_data_unused:2;
1566435cf559SWilliam Allen Simpson 	u8		s_data_payload[0];
1567435cf559SWilliam Allen Simpson };
1568435cf559SWilliam Allen Simpson 
1569435cf559SWilliam Allen Simpson static inline void tcp_cookie_values_release(struct kref *kref)
1570435cf559SWilliam Allen Simpson {
1571435cf559SWilliam Allen Simpson 	kfree(container_of(kref, struct tcp_cookie_values, kref));
1572435cf559SWilliam Allen Simpson }
1573435cf559SWilliam Allen Simpson 
1574435cf559SWilliam Allen Simpson /* The length of constant payload data.  Note that s_data_desired is
1575435cf559SWilliam Allen Simpson  * overloaded, depending on s_data_constant: either the length of constant
1576435cf559SWilliam Allen Simpson  * data (returned here) or the limit on variable data.
1577435cf559SWilliam Allen Simpson  */
1578435cf559SWilliam Allen Simpson static inline int tcp_s_data_size(const struct tcp_sock *tp)
1579435cf559SWilliam Allen Simpson {
1580435cf559SWilliam Allen Simpson 	return (tp->cookie_values != NULL && tp->cookie_values->s_data_constant)
1581435cf559SWilliam Allen Simpson 		? tp->cookie_values->s_data_desired
1582435cf559SWilliam Allen Simpson 		: 0;
1583435cf559SWilliam Allen Simpson }
1584435cf559SWilliam Allen Simpson 
1585435cf559SWilliam Allen Simpson /**
1586435cf559SWilliam Allen Simpson  *	struct tcp_extend_values - tcp_ipv?.c to tcp_output.c workspace.
1587435cf559SWilliam Allen Simpson  *
1588435cf559SWilliam Allen Simpson  *	As tcp_request_sock has already been extended in other places, the
1589435cf559SWilliam Allen Simpson  *	only remaining method is to pass stack values along as function
1590435cf559SWilliam Allen Simpson  *	parameters.  These parameters are not needed after sending SYNACK.
1591435cf559SWilliam Allen Simpson  *
1592435cf559SWilliam Allen Simpson  * @cookie_bakery:	cryptographic secret and message workspace.
1593435cf559SWilliam Allen Simpson  *
1594435cf559SWilliam Allen Simpson  * @cookie_plus:	bytes in authenticator/cookie option, copied from
1595435cf559SWilliam Allen Simpson  *			struct tcp_options_received (above).
1596435cf559SWilliam Allen Simpson  */
1597435cf559SWilliam Allen Simpson struct tcp_extend_values {
1598435cf559SWilliam Allen Simpson 	struct request_values		rv;
1599435cf559SWilliam Allen Simpson 	u32				cookie_bakery[COOKIE_WORKSPACE_WORDS];
1600435cf559SWilliam Allen Simpson 	u8				cookie_plus:6,
1601435cf559SWilliam Allen Simpson 					cookie_out_never:1,
1602435cf559SWilliam Allen Simpson 					cookie_in_always:1;
1603435cf559SWilliam Allen Simpson };
1604435cf559SWilliam Allen Simpson 
1605435cf559SWilliam Allen Simpson static inline struct tcp_extend_values *tcp_xv(struct request_values *rvp)
1606435cf559SWilliam Allen Simpson {
1607435cf559SWilliam Allen Simpson 	return (struct tcp_extend_values *)rvp;
1608435cf559SWilliam Allen Simpson }
1609435cf559SWilliam Allen Simpson 
16109b0f976fSDenis V. Lunev extern void tcp_v4_init(void);
161120380731SArnaldo Carvalho de Melo extern void tcp_init(void);
161220380731SArnaldo Carvalho de Melo 
16131da177e4SLinus Torvalds #endif	/* _TCP_H */
1614