xref: /linux/include/net/tcp.h (revision f86586fa48a94c7a9c7f36650a2e0e4a947881c9)
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 TCP_DEBUG 1
221da177e4SLinus Torvalds #define FASTRETRANS_DEBUG 1
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <linux/list.h>
251da177e4SLinus Torvalds #include <linux/tcp.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>
481da177e4SLinus Torvalds 
490f7ff927SArnaldo Carvalho de Melo extern struct inet_hashinfo tcp_hashinfo;
501da177e4SLinus Torvalds 
51dd24c001SEric Dumazet extern struct percpu_counter tcp_orphan_count;
521da177e4SLinus Torvalds extern void tcp_time_wait(struct sock *sk, int state, int timeo);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #define MAX_TCP_HEADER	(128 + MAX_HEADER)
5533ad798cSAdam Langley #define MAX_TCP_OPTION_SPACE 40
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds  * Never offer a window over 32767 without using window scaling. Some
591da177e4SLinus Torvalds  * poor stacks do signed 16bit maths!
601da177e4SLinus Torvalds  */
611da177e4SLinus Torvalds #define MAX_TCP_WINDOW		32767U
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
641da177e4SLinus Torvalds #define TCP_MIN_MSS		88U
651da177e4SLinus Torvalds 
665d424d5aSJohn Heffner /* The least MTU to use for probing */
675d424d5aSJohn Heffner #define TCP_BASE_MSS		512
685d424d5aSJohn Heffner 
691da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
701da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /* Maximal reordering. */
731da177e4SLinus Torvalds #define TCP_MAX_REORDERING	127
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
761da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /* urg_data states */
791da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
801da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
811da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #define TCP_RETR1	3	/*
841da177e4SLinus Torvalds 				 * This is how many retries it does before it
851da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
861da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
871da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
881da177e4SLinus Torvalds 				 */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #define TCP_RETR2	15	/*
911da177e4SLinus Torvalds 				 * This should take at least
921da177e4SLinus Torvalds 				 * 90 minutes to time out.
931da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
941da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
951da177e4SLinus Torvalds 				 */
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #define TCP_SYN_RETRIES	 5	/* number of times to retry active opening a
98caa20d9aSStephen Hemminger 				 * connection: ~180sec is RFC minimum	*/
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #define TCP_SYNACK_RETRIES 5	/* number of times to retry passive opening a
101caa20d9aSStephen Hemminger 				 * connection: ~180sec is RFC minimum	*/
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds #define TCP_ORPHAN_RETRIES 7	/* number of times to retry on an orphaned
1051da177e4SLinus Torvalds 				 * socket. 7 is ~50sec-16min.
1061da177e4SLinus Torvalds 				 */
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1101da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1111da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1121da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1131da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1141da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1151da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1161da177e4SLinus Torvalds 				  */
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1191da177e4SLinus Torvalds #if HZ >= 100
1201da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1211da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1221da177e4SLinus Torvalds #else
1231da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1241da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1251da177e4SLinus Torvalds #endif
1261da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1271da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
1281da177e4SLinus Torvalds #define TCP_TIMEOUT_INIT ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value	*/
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1311da177e4SLinus Torvalds 					                 * for local resources.
1321da177e4SLinus Torvalds 					                 */
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1351da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1361da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1391da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1401da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1411da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1461da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1471da177e4SLinus Torvalds 					 * after this time. It should be equal
1481da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1491da177e4SLinus Torvalds 					 * to provide reliability equal to one
1501da177e4SLinus Torvalds 					 * provided by timewait state.
1511da177e4SLinus Torvalds 					 */
1521da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1531da177e4SLinus Torvalds 					 * timestamps. It must be less than
1541da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1551da177e4SLinus Torvalds 					 */
1561da177e4SLinus Torvalds /*
1571da177e4SLinus Torvalds  *	TCP option
1581da177e4SLinus Torvalds  */
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1611da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1621da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1631da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1641da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1651da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1661da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
167cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
168435cf559SWilliam Allen Simpson #define TCPOPT_COOKIE		253	/* Cookie extension (experimental) */
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds /*
1711da177e4SLinus Torvalds  *     TCP option lengths
1721da177e4SLinus Torvalds  */
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds #define TCPOLEN_MSS            4
1751da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
1761da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
1771da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
178cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG         18
179435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_BASE    2	/* Cookie-less header extension */
180435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_PAIR    3	/* Cookie pair header extension */
181435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_MIN     (TCPOLEN_COOKIE_BASE+TCP_COOKIE_MIN)
182435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_MAX     (TCPOLEN_COOKIE_BASE+TCP_COOKIE_MAX)
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds /* But this is what stacks really send out. */
1851da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
1861da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
1871da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
1881da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
1891da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
1901da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
191cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED		20
19233ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED		4
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /* Flags in tp->nonagle */
1951da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
1961da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
197caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
1981da177e4SLinus Torvalds 
19936e31b0aSAndreas Petlund /* TCP thin-stream limits */
20036e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6       /* After 6 linear retries, do exp. backoff */
20136e31b0aSAndreas Petlund 
202295ff7edSArnaldo Carvalho de Melo extern struct inet_timewait_death_row tcp_death_row;
203295ff7edSArnaldo Carvalho de Melo 
2041da177e4SLinus Torvalds /* sysctl variables for tcp */
2051da177e4SLinus Torvalds extern int sysctl_tcp_timestamps;
2061da177e4SLinus Torvalds extern int sysctl_tcp_window_scaling;
2071da177e4SLinus Torvalds extern int sysctl_tcp_sack;
2081da177e4SLinus Torvalds extern int sysctl_tcp_fin_timeout;
2091da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_time;
2101da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_probes;
2111da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_intvl;
2121da177e4SLinus Torvalds extern int sysctl_tcp_syn_retries;
2131da177e4SLinus Torvalds extern int sysctl_tcp_synack_retries;
2141da177e4SLinus Torvalds extern int sysctl_tcp_retries1;
2151da177e4SLinus Torvalds extern int sysctl_tcp_retries2;
2161da177e4SLinus Torvalds extern int sysctl_tcp_orphan_retries;
2171da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
2181da177e4SLinus Torvalds extern int sysctl_tcp_retrans_collapse;
2191da177e4SLinus Torvalds extern int sysctl_tcp_stdurg;
2201da177e4SLinus Torvalds extern int sysctl_tcp_rfc1337;
2211da177e4SLinus Torvalds extern int sysctl_tcp_abort_on_overflow;
2221da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
2231da177e4SLinus Torvalds extern int sysctl_tcp_fack;
2241da177e4SLinus Torvalds extern int sysctl_tcp_reordering;
2251da177e4SLinus Torvalds extern int sysctl_tcp_ecn;
2261da177e4SLinus Torvalds extern int sysctl_tcp_dsack;
2271da177e4SLinus Torvalds extern int sysctl_tcp_mem[3];
2281da177e4SLinus Torvalds extern int sysctl_tcp_wmem[3];
2291da177e4SLinus Torvalds extern int sysctl_tcp_rmem[3];
2301da177e4SLinus Torvalds extern int sysctl_tcp_app_win;
2311da177e4SLinus Torvalds extern int sysctl_tcp_adv_win_scale;
2321da177e4SLinus Torvalds extern int sysctl_tcp_tw_reuse;
2331da177e4SLinus Torvalds extern int sysctl_tcp_frto;
2343cfe3baaSIlpo Järvinen extern int sysctl_tcp_frto_response;
2351da177e4SLinus Torvalds extern int sysctl_tcp_low_latency;
23695937825SChris Leech extern int sysctl_tcp_dma_copybreak;
2371da177e4SLinus Torvalds extern int sysctl_tcp_nometrics_save;
2381da177e4SLinus Torvalds extern int sysctl_tcp_moderate_rcvbuf;
2391da177e4SLinus Torvalds extern int sysctl_tcp_tso_win_divisor;
2409772efb9SStephen Hemminger extern int sysctl_tcp_abc;
2415d424d5aSJohn Heffner extern int sysctl_tcp_mtu_probing;
2425d424d5aSJohn Heffner extern int sysctl_tcp_base_mss;
24315d99e02SRick Jones extern int sysctl_tcp_workaround_signed_windows;
24435089bb2SDavid S. Miller extern int sysctl_tcp_slow_start_after_idle;
245886236c1SJohn Heffner extern int sysctl_tcp_max_ssthresh;
246519855c5SWilliam Allen Simpson extern int sysctl_tcp_cookie_size;
24736e31b0aSAndreas Petlund extern int sysctl_tcp_thin_linear_timeouts;
2487e380175SAndreas Petlund extern int sysctl_tcp_thin_dupack;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds extern atomic_t tcp_memory_allocated;
2511748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated;
2521da177e4SLinus Torvalds extern int tcp_memory_pressure;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
2561da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
2571da177e4SLinus Torvalds  */
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds static inline int before(__u32 seq1, __u32 seq2)
2601da177e4SLinus Torvalds {
2610d630cc0SGerrit Renker         return (__s32)(seq1-seq2) < 0;
2621da177e4SLinus Torvalds }
2639a036b9cSGerrit Renker #define after(seq2, seq1) 	before(seq1, seq2)
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
2661da177e4SLinus Torvalds static inline int between(__u32 seq1, __u32 seq2, __u32 seq3)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
271e4fd5da3SPavel Emelianov static inline int tcp_too_many_orphans(struct sock *sk, int num)
272e4fd5da3SPavel Emelianov {
273e4fd5da3SPavel Emelianov 	return (num > sysctl_tcp_max_orphans) ||
274e4fd5da3SPavel Emelianov 		(sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
275e4fd5da3SPavel Emelianov 		 atomic_read(&tcp_memory_allocated) > sysctl_tcp_mem[2]);
276e4fd5da3SPavel Emelianov }
2771da177e4SLinus Torvalds 
278a0f82f64SFlorian Westphal /* syncookies: remember time of last synqueue overflow */
279a0f82f64SFlorian Westphal static inline void tcp_synq_overflow(struct sock *sk)
280a0f82f64SFlorian Westphal {
281a0f82f64SFlorian Westphal 	tcp_sk(sk)->rx_opt.ts_recent_stamp = jiffies;
282a0f82f64SFlorian Westphal }
283a0f82f64SFlorian Westphal 
284a0f82f64SFlorian Westphal /* syncookies: no recent synqueue overflow on this listening socket? */
285a0f82f64SFlorian Westphal static inline int tcp_synq_no_recent_overflow(const struct sock *sk)
286a0f82f64SFlorian Westphal {
287a0f82f64SFlorian Westphal 	unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
288a0f82f64SFlorian Westphal 	return time_after(jiffies, last_overflow + TCP_TIMEOUT_INIT);
289a0f82f64SFlorian Westphal }
290a0f82f64SFlorian Westphal 
2911da177e4SLinus Torvalds extern struct proto tcp_prot;
2921da177e4SLinus Torvalds 
29357ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
29457ef42d5SPavel Emelyanov #define TCP_INC_STATS_BH(net, field)	SNMP_INC_STATS_BH((net)->mib.tcp_statistics, field)
29557ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
29657ef42d5SPavel Emelyanov #define TCP_ADD_STATS_USER(net, field, val) SNMP_ADD_STATS_USER((net)->mib.tcp_statistics, field, val)
297aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val)	SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val)
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds extern void tcp_v4_err(struct sk_buff *skb, u32);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds extern void tcp_shutdown (struct sock *sk, int how);
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds extern int tcp_v4_rcv(struct sk_buff *skb);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds extern int tcp_v4_remember_stamp(struct sock *sk);
3068feaf0c0SArnaldo Carvalho de Melo extern int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
3077ba42910SChangli Gao extern int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
3087ba42910SChangli Gao 		       size_t size);
3097ba42910SChangli Gao extern int tcp_sendpage(struct sock *sk, struct page *page, int offset,
31053d3176bSChangli Gao 			size_t size, int flags);
31153d3176bSChangli Gao extern int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg);
31253d3176bSChangli Gao extern int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
31353d3176bSChangli Gao 				 struct tcphdr *th, unsigned len);
31453d3176bSChangli Gao extern int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
31553d3176bSChangli Gao 			       struct tcphdr *th, unsigned len);
3161da177e4SLinus Torvalds extern void tcp_rcv_space_adjust(struct sock *sk);
3170e4b4992SChris Leech extern void tcp_cleanup_rbuf(struct sock *sk, int copied);
31853d3176bSChangli Gao extern int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp);
319cfb6eeb4SYOSHIFUJI Hideaki extern void tcp_twsk_destructor(struct sock *sk);
3209c55e01cSJens Axboe extern ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos,
32153d3176bSChangli Gao 			       struct pipe_inode_info *pipe, size_t len,
32253d3176bSChangli Gao 			       unsigned int flags);
3239c55e01cSJens Axboe 
324463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
325463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3261da177e4SLinus Torvalds {
327463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
328fc6415bcSDavid S. Miller 
329463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
330463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
331463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3321da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
333463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
334fc6415bcSDavid S. Miller 		} else
335463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds }
3381da177e4SLinus Torvalds 
339463c84b9SArnaldo Carvalho de Melo extern void tcp_enter_quickack_mode(struct sock *sk);
3401da177e4SLinus Torvalds 
341bdf1ee5dSIlpo Järvinen #define	TCP_ECN_OK		1
342bdf1ee5dSIlpo Järvinen #define	TCP_ECN_QUEUE_CWR	2
343bdf1ee5dSIlpo Järvinen #define	TCP_ECN_DEMAND_CWR	4
344bdf1ee5dSIlpo Järvinen 
345bdf1ee5dSIlpo Järvinen static __inline__ void
346bdf1ee5dSIlpo Järvinen TCP_ECN_create_request(struct request_sock *req, struct tcphdr *th)
347bdf1ee5dSIlpo Järvinen {
348bdf1ee5dSIlpo Järvinen 	if (sysctl_tcp_ecn && th->ece && th->cwr)
349bdf1ee5dSIlpo Järvinen 		inet_rsk(req)->ecn_ok = 1;
350bdf1ee5dSIlpo Järvinen }
351bdf1ee5dSIlpo Järvinen 
352fd2c3ef7SEric Dumazet enum tcp_tw_status {
3531da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
3541da177e4SLinus Torvalds 	TCP_TW_RST = 1,
3551da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
3561da177e4SLinus Torvalds 	TCP_TW_SYN = 3
3571da177e4SLinus Torvalds };
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 
3608feaf0c0SArnaldo Carvalho de Melo extern enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw,
3611da177e4SLinus Torvalds 						     struct sk_buff *skb,
3628feaf0c0SArnaldo Carvalho de Melo 						     const struct tcphdr *th);
3631da177e4SLinus Torvalds extern struct sock * tcp_check_req(struct sock *sk,struct sk_buff *skb,
36460236fddSArnaldo Carvalho de Melo 				   struct request_sock *req,
36560236fddSArnaldo Carvalho de Melo 				   struct request_sock **prev);
36653d3176bSChangli Gao extern int tcp_child_process(struct sock *parent, struct sock *child,
3671da177e4SLinus Torvalds 			     struct sk_buff *skb);
36846d0de4eSIlpo Järvinen extern int tcp_use_frto(struct sock *sk);
3691da177e4SLinus Torvalds extern void tcp_enter_frto(struct sock *sk);
3701da177e4SLinus Torvalds extern void tcp_enter_loss(struct sock *sk, int how);
3711da177e4SLinus Torvalds extern void tcp_clear_retrans(struct tcp_sock *tp);
3721da177e4SLinus Torvalds extern void tcp_update_metrics(struct sock *sk);
37353d3176bSChangli Gao extern void tcp_close(struct sock *sk, long timeout);
37453d3176bSChangli Gao extern unsigned int tcp_poll(struct file * file, struct socket *sock,
37553d3176bSChangli Gao 			     struct poll_table_struct *wait);
37653d3176bSChangli Gao extern int tcp_getsockopt(struct sock *sk, int level, int optname,
3773fdadf7dSDmitry Mishin 			  char __user *optval, int __user *optlen);
37853d3176bSChangli Gao extern int tcp_setsockopt(struct sock *sk, int level, int optname,
37953d3176bSChangli Gao 			  char __user *optval, unsigned int optlen);
38053d3176bSChangli Gao extern int compat_tcp_getsockopt(struct sock *sk, int level, int optname,
38153d3176bSChangli Gao 				 char __user *optval, int __user *optlen);
38253d3176bSChangli Gao extern int compat_tcp_setsockopt(struct sock *sk, int level, int optname,
383b7058842SDavid S. Miller 				 char __user *optval, unsigned int optlen);
3841da177e4SLinus Torvalds extern void tcp_set_keepalive(struct sock *sk, int val);
38553d3176bSChangli Gao extern void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req);
38653d3176bSChangli Gao extern int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
38753d3176bSChangli Gao 		       size_t len, int nonblock, int flags, int *addr_len);
3881da177e4SLinus Torvalds extern void tcp_parse_options(struct sk_buff *skb,
38953d3176bSChangli Gao 			      struct tcp_options_received *opt_rx, u8 **hvpp,
390bb5b7c11SDavid S. Miller 			      int estab);
3917d5d5525SYOSHIFUJI Hideaki extern u8 *tcp_parse_md5sig_option(struct tcphdr *th);
3927d5d5525SYOSHIFUJI Hideaki 
3931da177e4SLinus Torvalds /*
3941da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
3951da177e4SLinus Torvalds  */
3961da177e4SLinus Torvalds 
39753d3176bSChangli Gao extern void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb);
39853d3176bSChangli Gao extern int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
3991da177e4SLinus Torvalds extern struct sock * tcp_create_openreq_child(struct sock *sk,
40060236fddSArnaldo Carvalho de Melo 					      struct request_sock *req,
4011da177e4SLinus Torvalds 					      struct sk_buff *skb);
40253d3176bSChangli Gao extern struct sock * tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
40360236fddSArnaldo Carvalho de Melo 					  struct request_sock *req,
4041da177e4SLinus Torvalds 					  struct dst_entry *dst);
40553d3176bSChangli Gao extern int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
40653d3176bSChangli Gao extern int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr,
4071da177e4SLinus Torvalds 			  int addr_len);
4081da177e4SLinus Torvalds extern int tcp_connect(struct sock *sk);
40953d3176bSChangli Gao extern struct sk_buff * tcp_make_synack(struct sock *sk, struct dst_entry *dst,
410e6b4d113SWilliam Allen Simpson 					struct request_sock *req,
411e6b4d113SWilliam Allen Simpson 					struct request_values *rvp);
4121da177e4SLinus Torvalds extern int tcp_disconnect(struct sock *sk, int flags);
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds /* From syncookies.c */
4162051f11fSFlorian Westphal extern __u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS];
4171da177e4SLinus Torvalds extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
4181da177e4SLinus Torvalds 				    struct ip_options *opt);
4191da177e4SLinus Torvalds extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb,
4201da177e4SLinus Torvalds 				     __u16 *mss);
4211da177e4SLinus Torvalds 
4224dfc2817SFlorian Westphal extern __u32 cookie_init_timestamp(struct request_sock *req);
423172d69e6SFlorian Westphal extern bool cookie_check_timestamp(struct tcp_options_received *opt, bool *);
4244dfc2817SFlorian Westphal 
425c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */
426c6aefafbSGlenn Griffin extern struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
427c6aefafbSGlenn Griffin extern __u32 cookie_v6_init_sequence(struct sock *sk, struct sk_buff *skb,
428c6aefafbSGlenn Griffin 				     __u16 *mss);
429c6aefafbSGlenn Griffin 
4301da177e4SLinus Torvalds /* tcp_output.c */
4311da177e4SLinus Torvalds 
4329e412ba7SIlpo Järvinen extern void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
4339e412ba7SIlpo Järvinen 				      int nonagle);
4349e412ba7SIlpo Järvinen extern int tcp_may_send_now(struct sock *sk);
4351da177e4SLinus Torvalds extern int tcp_retransmit_skb(struct sock *, struct sk_buff *);
436f1ecd5d9SDamian Lukowski extern void tcp_retransmit_timer(struct sock *sk);
4371da177e4SLinus Torvalds extern void tcp_xmit_retransmit_queue(struct sock *);
4381da177e4SLinus Torvalds extern void tcp_simple_retransmit(struct sock *);
4391da177e4SLinus Torvalds extern int tcp_trim_head(struct sock *, struct sk_buff *, u32);
4406475be16SDavid S. Miller extern int tcp_fragment(struct sock *, struct sk_buff *, u32, unsigned int);
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds extern void tcp_send_probe0(struct sock *);
4431da177e4SLinus Torvalds extern void tcp_send_partial(struct sock *);
4441da177e4SLinus Torvalds extern int tcp_write_wakeup(struct sock *);
4451da177e4SLinus Torvalds extern void tcp_send_fin(struct sock *sk);
446dd0fc66fSAl Viro extern void tcp_send_active_reset(struct sock *sk, gfp_t priority);
4471da177e4SLinus Torvalds extern int tcp_send_synack(struct sock *);
448c1b4a7e6SDavid S. Miller extern void tcp_push_one(struct sock *, unsigned int mss_now);
4491da177e4SLinus Torvalds extern void tcp_send_ack(struct sock *sk);
4501da177e4SLinus Torvalds extern void tcp_send_delayed_ack(struct sock *sk);
4511da177e4SLinus Torvalds 
452a762a980SDavid S. Miller /* tcp_input.c */
453a762a980SDavid S. Miller extern void tcp_cwnd_application_limited(struct sock *sk);
454a762a980SDavid S. Miller 
4551da177e4SLinus Torvalds /* tcp_timer.c */
4561da177e4SLinus Torvalds extern void tcp_init_xmit_timers(struct sock *);
457463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
458463c84b9SArnaldo Carvalho de Melo {
459463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
460463c84b9SArnaldo Carvalho de Melo }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds extern unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
4630c54b85fSIlpo Järvinen extern unsigned int tcp_current_mss(struct sock *sk);
4640c54b85fSIlpo Järvinen 
4650c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */
4660c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
4670c54b85fSIlpo Järvinen {
4680c54b85fSIlpo Järvinen 	if (tp->max_window && pktsize > (tp->max_window >> 1))
4690c54b85fSIlpo Järvinen 		return max(tp->max_window >> 1, 68U - tp->tcp_header_len);
4700c54b85fSIlpo Järvinen 	else
4710c54b85fSIlpo Järvinen 		return pktsize;
4720c54b85fSIlpo Järvinen }
4731da177e4SLinus Torvalds 
47417b085eaSArnaldo Carvalho de Melo /* tcp.c */
4751da177e4SLinus Torvalds extern void tcp_get_info(struct sock *, struct tcp_info *);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
4781da177e4SLinus Torvalds typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
4791da177e4SLinus Torvalds 				unsigned int, size_t);
4801da177e4SLinus Torvalds extern int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
4811da177e4SLinus Torvalds 			 sk_read_actor_t recv_actor);
4821da177e4SLinus Torvalds 
48340efc6faSStephen Hemminger extern void tcp_initialize_rcv_mss(struct sock *sk);
4841da177e4SLinus Torvalds 
4855d424d5aSJohn Heffner extern int tcp_mtu_to_mss(struct sock *sk, int pmtu);
4865d424d5aSJohn Heffner extern int tcp_mss_to_mtu(struct sock *sk, int mss);
4875d424d5aSJohn Heffner extern void tcp_mtup_init(struct sock *sk);
4885d424d5aSJohn Heffner 
489f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk)
490f1ecd5d9SDamian Lukowski {
491f1ecd5d9SDamian Lukowski 	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
492f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
493f1ecd5d9SDamian Lukowski }
494f1ecd5d9SDamian Lukowski 
495f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
496f1ecd5d9SDamian Lukowski {
497f1ecd5d9SDamian Lukowski 	return (tp->srtt >> 3) + tp->rttvar;
498f1ecd5d9SDamian Lukowski }
499f1ecd5d9SDamian Lukowski 
50040efc6faSStephen Hemminger static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
5031da177e4SLinus Torvalds 			       ntohl(TCP_FLAG_ACK) |
5041da177e4SLinus Torvalds 			       snd_wnd);
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
50740efc6faSStephen Hemminger static inline void tcp_fast_path_on(struct tcp_sock *tp)
5081da177e4SLinus Torvalds {
5091da177e4SLinus Torvalds 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5129e412ba7SIlpo Järvinen static inline void tcp_fast_path_check(struct sock *sk)
5131da177e4SLinus Torvalds {
5149e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
5159e412ba7SIlpo Järvinen 
516b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue) &&
5171da177e4SLinus Torvalds 	    tp->rcv_wnd &&
5181da177e4SLinus Torvalds 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
5191da177e4SLinus Torvalds 	    !tp->urg_data)
5201da177e4SLinus Torvalds 		tcp_fast_path_on(tp);
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5230c266898SSatoru SATOH /* Compute the actual rto_min value */
5240c266898SSatoru SATOH static inline u32 tcp_rto_min(struct sock *sk)
5250c266898SSatoru SATOH {
5260c266898SSatoru SATOH 	struct dst_entry *dst = __sk_dst_get(sk);
5270c266898SSatoru SATOH 	u32 rto_min = TCP_RTO_MIN;
5280c266898SSatoru SATOH 
5290c266898SSatoru SATOH 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
5300c266898SSatoru SATOH 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
5310c266898SSatoru SATOH 	return rto_min;
5320c266898SSatoru SATOH }
5330c266898SSatoru SATOH 
5341da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
5351da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
5361da177e4SLinus Torvalds  * than the offered window.
5371da177e4SLinus Torvalds  */
53840efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
5391da177e4SLinus Torvalds {
5401da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	if (win < 0)
5431da177e4SLinus Torvalds 		win = 0;
5441da177e4SLinus Torvalds 	return (u32) win;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
5481da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
5491da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
5501da177e4SLinus Torvalds  */
5511da177e4SLinus Torvalds extern u32 __tcp_select_window(struct sock *sk);
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds /* TCP timestamps are only 32-bits, this causes a slight
5541da177e4SLinus Torvalds  * complication on 64-bit systems since we store a snapshot
55531f34269SStephen Hemminger  * of jiffies in the buffer control blocks below.  We decided
55631f34269SStephen Hemminger  * to use only the low 32-bits of jiffies and hide the ugly
5571da177e4SLinus Torvalds  * casts with the following macro.
5581da177e4SLinus Torvalds  */
5591da177e4SLinus Torvalds #define tcp_time_stamp		((__u32)(jiffies))
5601da177e4SLinus Torvalds 
561a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13])
562a3433f35SChangli Gao 
563a3433f35SChangli Gao #define TCPHDR_FIN 0x01
564a3433f35SChangli Gao #define TCPHDR_SYN 0x02
565a3433f35SChangli Gao #define TCPHDR_RST 0x04
566a3433f35SChangli Gao #define TCPHDR_PSH 0x08
567a3433f35SChangli Gao #define TCPHDR_ACK 0x10
568a3433f35SChangli Gao #define TCPHDR_URG 0x20
569a3433f35SChangli Gao #define TCPHDR_ECE 0x40
570a3433f35SChangli Gao #define TCPHDR_CWR 0x80
571a3433f35SChangli Gao 
572caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
573*f86586faSEric Dumazet  * TCP per-packet control information to the transmission code.
574*f86586faSEric Dumazet  * We also store the host-order sequence numbers in here too.
575*f86586faSEric Dumazet  * This is 44 bytes if IPV6 is enabled.
576*f86586faSEric Dumazet  * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately.
5771da177e4SLinus Torvalds  */
5781da177e4SLinus Torvalds struct tcp_skb_cb {
5791da177e4SLinus Torvalds 	union {
5801da177e4SLinus Torvalds 		struct inet_skb_parm	h4;
5811da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
5821da177e4SLinus Torvalds 		struct inet6_skb_parm	h6;
5831da177e4SLinus Torvalds #endif
5841da177e4SLinus Torvalds 	} header;	/* For incoming frames		*/
5851da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
5861da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
5871da177e4SLinus Torvalds 	__u32		when;		/* used to compute rtt's	*/
5881da177e4SLinus Torvalds 	__u8		flags;		/* TCP header flags.		*/
5891da177e4SLinus Torvalds 	__u8		sacked;		/* State flags for SACK/FACK.	*/
5901da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
5911da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
5921da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
5931da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
5961da177e4SLinus Torvalds #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
5991da177e4SLinus Torvalds };
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
6041da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
6051da177e4SLinus Torvalds  */
6061da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
6071da177e4SLinus Torvalds {
6087967168cSHerbert Xu 	return skb_shinfo(skb)->gso_segs;
6091da177e4SLinus Torvalds }
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds /* This is valid iff tcp_skb_pcount() > 1. */
6121da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
6131da177e4SLinus Torvalds {
6147967168cSHerbert Xu 	return skb_shinfo(skb)->gso_size;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
617317a76f9SStephen Hemminger /* Events passed to congestion control interface */
618317a76f9SStephen Hemminger enum tcp_ca_event {
619317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
620317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
621317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
622317a76f9SStephen Hemminger 	CA_EVENT_FRTO,		/* fast recovery timeout */
623317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
624317a76f9SStephen Hemminger 	CA_EVENT_FAST_ACK,	/* in sequence ack */
625317a76f9SStephen Hemminger 	CA_EVENT_SLOW_ACK,	/* other ack */
626317a76f9SStephen Hemminger };
627317a76f9SStephen Hemminger 
628317a76f9SStephen Hemminger /*
629317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
630317a76f9SStephen Hemminger  */
631317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
6323ff825b2SStephen Hemminger #define TCP_CA_MAX	128
6333ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
6343ff825b2SStephen Hemminger 
635164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1
636164891aaSStephen Hemminger #define TCP_CONG_RTT_STAMP	0x2
637164891aaSStephen Hemminger 
638317a76f9SStephen Hemminger struct tcp_congestion_ops {
639317a76f9SStephen Hemminger 	struct list_head	list;
640164891aaSStephen Hemminger 	unsigned long flags;
641317a76f9SStephen Hemminger 
642317a76f9SStephen Hemminger 	/* initialize private data (optional) */
6436687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
644317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
6456687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
646317a76f9SStephen Hemminger 
647317a76f9SStephen Hemminger 	/* return slow start threshold (required) */
6486687e988SArnaldo Carvalho de Melo 	u32 (*ssthresh)(struct sock *sk);
649317a76f9SStephen Hemminger 	/* lower bound for congestion window (optional) */
65072dc5b92SStephen Hemminger 	u32 (*min_cwnd)(const struct sock *sk);
651317a76f9SStephen Hemminger 	/* do new cwnd calculation (required) */
652c3a05c60SIlpo Järvinen 	void (*cong_avoid)(struct sock *sk, u32 ack, u32 in_flight);
653317a76f9SStephen Hemminger 	/* call before changing ca_state (optional) */
6546687e988SArnaldo Carvalho de Melo 	void (*set_state)(struct sock *sk, u8 new_state);
655317a76f9SStephen Hemminger 	/* call when cwnd event occurs (optional) */
6566687e988SArnaldo Carvalho de Melo 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
657317a76f9SStephen Hemminger 	/* new value of cwnd after loss (optional) */
6586687e988SArnaldo Carvalho de Melo 	u32  (*undo_cwnd)(struct sock *sk);
659317a76f9SStephen Hemminger 	/* hook for packet ack accounting (optional) */
66030cfd0baSStephen Hemminger 	void (*pkts_acked)(struct sock *sk, u32 num_acked, s32 rtt_us);
66173c1f4a0SArnaldo Carvalho de Melo 	/* get info for inet_diag (optional) */
6626687e988SArnaldo Carvalho de Melo 	void (*get_info)(struct sock *sk, u32 ext, struct sk_buff *skb);
663317a76f9SStephen Hemminger 
664317a76f9SStephen Hemminger 	char 		name[TCP_CA_NAME_MAX];
665317a76f9SStephen Hemminger 	struct module 	*owner;
666317a76f9SStephen Hemminger };
667317a76f9SStephen Hemminger 
668317a76f9SStephen Hemminger extern int tcp_register_congestion_control(struct tcp_congestion_ops *type);
669317a76f9SStephen Hemminger extern void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
670317a76f9SStephen Hemminger 
6716687e988SArnaldo Carvalho de Melo extern void tcp_init_congestion_control(struct sock *sk);
6726687e988SArnaldo Carvalho de Melo extern void tcp_cleanup_congestion_control(struct sock *sk);
673317a76f9SStephen Hemminger extern int tcp_set_default_congestion_control(const char *name);
674317a76f9SStephen Hemminger extern void tcp_get_default_congestion_control(char *name);
6753ff825b2SStephen Hemminger extern void tcp_get_available_congestion_control(char *buf, size_t len);
676ce7bc3bfSStephen Hemminger extern void tcp_get_allowed_congestion_control(char *buf, size_t len);
677ce7bc3bfSStephen Hemminger extern int tcp_set_allowed_congestion_control(char *allowed);
6786687e988SArnaldo Carvalho de Melo extern int tcp_set_congestion_control(struct sock *sk, const char *name);
67940efc6faSStephen Hemminger extern void tcp_slow_start(struct tcp_sock *tp);
680758ce5c8SIlpo Järvinen extern void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w);
681317a76f9SStephen Hemminger 
6825f8ef48dSStephen Hemminger extern struct tcp_congestion_ops tcp_init_congestion_ops;
6836687e988SArnaldo Carvalho de Melo extern u32 tcp_reno_ssthresh(struct sock *sk);
684c3a05c60SIlpo Järvinen extern void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight);
68572dc5b92SStephen Hemminger extern u32 tcp_reno_min_cwnd(const struct sock *sk);
686a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
687317a76f9SStephen Hemminger 
6886687e988SArnaldo Carvalho de Melo static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
689317a76f9SStephen Hemminger {
6906687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
6916687e988SArnaldo Carvalho de Melo 
6926687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->set_state)
6936687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->set_state(sk, ca_state);
6946687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_state = ca_state;
695317a76f9SStephen Hemminger }
696317a76f9SStephen Hemminger 
6976687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
698317a76f9SStephen Hemminger {
6996687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
7006687e988SArnaldo Carvalho de Melo 
7016687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
7026687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
703317a76f9SStephen Hemminger }
704317a76f9SStephen Hemminger 
705e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK
706e60402d0SIlpo Järvinen  * handling. SACK is negotiated with the peer, and therefore it can vary
707e60402d0SIlpo Järvinen  * between different flows.
708e60402d0SIlpo Järvinen  *
709e60402d0SIlpo Järvinen  * tcp_is_sack - SACK enabled
710e60402d0SIlpo Järvinen  * tcp_is_reno - No SACK
711e60402d0SIlpo Järvinen  * tcp_is_fack - FACK enabled, implies SACK enabled
712e60402d0SIlpo Järvinen  */
713e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp)
714e60402d0SIlpo Järvinen {
715e60402d0SIlpo Järvinen 	return tp->rx_opt.sack_ok;
716e60402d0SIlpo Järvinen }
717e60402d0SIlpo Järvinen 
718e60402d0SIlpo Järvinen static inline int tcp_is_reno(const struct tcp_sock *tp)
719e60402d0SIlpo Järvinen {
720e60402d0SIlpo Järvinen 	return !tcp_is_sack(tp);
721e60402d0SIlpo Järvinen }
722e60402d0SIlpo Järvinen 
723e60402d0SIlpo Järvinen static inline int tcp_is_fack(const struct tcp_sock *tp)
724e60402d0SIlpo Järvinen {
725e60402d0SIlpo Järvinen 	return tp->rx_opt.sack_ok & 2;
726e60402d0SIlpo Järvinen }
727e60402d0SIlpo Järvinen 
728e60402d0SIlpo Järvinen static inline void tcp_enable_fack(struct tcp_sock *tp)
729e60402d0SIlpo Järvinen {
730e60402d0SIlpo Järvinen 	tp->rx_opt.sack_ok |= 2;
731e60402d0SIlpo Järvinen }
732e60402d0SIlpo Järvinen 
73383ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
73483ae4088SIlpo Järvinen {
73583ae4088SIlpo Järvinen 	return tp->sacked_out + tp->lost_out;
73683ae4088SIlpo Järvinen }
73783ae4088SIlpo Järvinen 
7381da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
7391da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
7401da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
7411da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
7421da177e4SLinus Torvalds  *
7431da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
7441da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
7451da177e4SLinus Torvalds  *
7461da177e4SLinus Torvalds  * Read this equation as:
7471da177e4SLinus Torvalds  *
7481da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
7491da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
7501da177e4SLinus Torvalds  *	"Packets fast retransmitted"
7511da177e4SLinus Torvalds  */
75240efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
7531da177e4SLinus Torvalds {
75483ae4088SIlpo Järvinen 	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
7551da177e4SLinus Torvalds }
7561da177e4SLinus Torvalds 
7570b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH	0x7fffffff
7580b6a05c1SIlpo Järvinen 
7590b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
7600b6a05c1SIlpo Järvinen {
7610b6a05c1SIlpo Järvinen 	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
7620b6a05c1SIlpo Järvinen }
7630b6a05c1SIlpo Järvinen 
7641da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
7651da177e4SLinus Torvalds  * The exception is rate halving phase, when cwnd is decreasing towards
7661da177e4SLinus Torvalds  * ssthresh.
7671da177e4SLinus Torvalds  */
7686687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
7691da177e4SLinus Torvalds {
7706687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
7716687e988SArnaldo Carvalho de Melo 	if ((1 << inet_csk(sk)->icsk_ca_state) & (TCPF_CA_CWR | TCPF_CA_Recovery))
7721da177e4SLinus Torvalds 		return tp->snd_ssthresh;
7731da177e4SLinus Torvalds 	else
7741da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
7751da177e4SLinus Torvalds 			   ((tp->snd_cwnd >> 1) +
7761da177e4SLinus Torvalds 			    (tp->snd_cwnd >> 2)));
7771da177e4SLinus Torvalds }
7781da177e4SLinus Torvalds 
779b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */
780b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
7811da177e4SLinus Torvalds 
7823cfe3baaSIlpo Järvinen extern void tcp_enter_cwr(struct sock *sk, const int set_ssthresh);
7831da177e4SLinus Torvalds extern __u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst);
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds /* Slow start with delack produces 3 packets of burst, so that
786dd9e0ddaSJohn Heffner  * it is safe "de facto".  This will be the default - same as
787dd9e0ddaSJohn Heffner  * the default reordering threshold - but if reordering increases,
788dd9e0ddaSJohn Heffner  * we must be able to allow cwnd to burst at least this much in order
789dd9e0ddaSJohn Heffner  * to not pull it back when holes are filled.
7901da177e4SLinus Torvalds  */
7911da177e4SLinus Torvalds static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
7921da177e4SLinus Torvalds {
793dd9e0ddaSJohn Heffner 	return tp->reordering;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
79690840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */
79790840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
79890840defSIlpo Järvinen {
79990840defSIlpo Järvinen 	return tp->snd_una + tp->snd_wnd;
80090840defSIlpo Järvinen }
801cea14e0eSIlpo Järvinen extern int tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight);
802f4805edeSStephen Hemminger 
803c1bd24b7SChuck Lever static inline void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss,
8041da177e4SLinus Torvalds 				       const struct sk_buff *skb)
8051da177e4SLinus Torvalds {
8061da177e4SLinus Torvalds 	if (skb->len < mss)
8071da177e4SLinus Torvalds 		tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
8109e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk)
8111da177e4SLinus Torvalds {
8129e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
813463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
8149e412ba7SIlpo Järvinen 
815463c84b9SArnaldo Carvalho de Melo 	if (!tp->packets_out && !icsk->icsk_pending)
8163f421baaSArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
8173f421baaSArnaldo Carvalho de Melo 					  icsk->icsk_rto, TCP_RTO_MAX);
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
820ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
825ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
8261da177e4SLinus Torvalds {
8271da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds /*
8311da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
8321da177e4SLinus Torvalds  */
833ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr,
834ba7808eaSFrederik Deweerdt 				   __be32 daddr, __wsum base)
8351da177e4SLinus Torvalds {
8361da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
839b51655b9SAl Viro static inline __sum16 __tcp_checksum_complete(struct sk_buff *skb)
8401da177e4SLinus Torvalds {
841fb286bb2SHerbert Xu 	return __skb_checksum_complete(skb);
8421da177e4SLinus Torvalds }
8431da177e4SLinus Torvalds 
84440efc6faSStephen Hemminger static inline int tcp_checksum_complete(struct sk_buff *skb)
8451da177e4SLinus Torvalds {
84660476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
8471da177e4SLinus Torvalds 		__tcp_checksum_complete(skb);
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds /* Prequeue for VJ style copy to user, combined with checksumming. */
8511da177e4SLinus Torvalds 
85240efc6faSStephen Hemminger static inline void tcp_prequeue_init(struct tcp_sock *tp)
8531da177e4SLinus Torvalds {
8541da177e4SLinus Torvalds 	tp->ucopy.task = NULL;
8551da177e4SLinus Torvalds 	tp->ucopy.len = 0;
8561da177e4SLinus Torvalds 	tp->ucopy.memory = 0;
8571da177e4SLinus Torvalds 	skb_queue_head_init(&tp->ucopy.prequeue);
85897fc2f08SChris Leech #ifdef CONFIG_NET_DMA
85997fc2f08SChris Leech 	tp->ucopy.dma_chan = NULL;
86097fc2f08SChris Leech 	tp->ucopy.wakeup = 0;
86197fc2f08SChris Leech 	tp->ucopy.pinned_list = NULL;
86297fc2f08SChris Leech 	tp->ucopy.dma_cookie = 0;
86397fc2f08SChris Leech #endif
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds /* Packet is added to VJ-style prequeue for processing in process
8671da177e4SLinus Torvalds  * context, if a reader task is waiting. Apparently, this exciting
8681da177e4SLinus Torvalds  * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93)
8691da177e4SLinus Torvalds  * failed somewhere. Latency? Burstiness? Well, at least now we will
8701da177e4SLinus Torvalds  * see, why it failed. 8)8)				  --ANK
8711da177e4SLinus Torvalds  *
8721da177e4SLinus Torvalds  * NOTE: is this not too big to inline?
8731da177e4SLinus Torvalds  */
87440efc6faSStephen Hemminger static inline int tcp_prequeue(struct sock *sk, struct sk_buff *skb)
8751da177e4SLinus Torvalds {
8761da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
8771da177e4SLinus Torvalds 
878f5f8d86bSEric Dumazet 	if (sysctl_tcp_low_latency || !tp->ucopy.task)
879f5f8d86bSEric Dumazet 		return 0;
880f5f8d86bSEric Dumazet 
8811da177e4SLinus Torvalds 	__skb_queue_tail(&tp->ucopy.prequeue, skb);
8821da177e4SLinus Torvalds 	tp->ucopy.memory += skb->truesize;
8831da177e4SLinus Torvalds 	if (tp->ucopy.memory > sk->sk_rcvbuf) {
8841da177e4SLinus Torvalds 		struct sk_buff *skb1;
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 		BUG_ON(sock_owned_by_user(sk));
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 		while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) {
889c57943a1SPeter Zijlstra 			sk_backlog_rcv(sk, skb1);
890f5f8d86bSEric Dumazet 			NET_INC_STATS_BH(sock_net(sk),
891f5f8d86bSEric Dumazet 					 LINUX_MIB_TCPPREQUEUEDROPPED);
8921da177e4SLinus Torvalds 		}
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 		tp->ucopy.memory = 0;
8951da177e4SLinus Torvalds 	} else if (skb_queue_len(&tp->ucopy.prequeue) == 1) {
896aa395145SEric Dumazet 		wake_up_interruptible_sync_poll(sk_sleep(sk),
8977aedec2aSEric Dumazet 					   POLLIN | POLLRDNORM | POLLRDBAND);
898463c84b9SArnaldo Carvalho de Melo 		if (!inet_csk_ack_scheduled(sk))
899463c84b9SArnaldo Carvalho de Melo 			inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
9000c266898SSatoru SATOH 						  (3 * tcp_rto_min(sk)) / 4,
9013f421baaSArnaldo Carvalho de Melo 						  TCP_RTO_MAX);
9021da177e4SLinus Torvalds 	}
9031da177e4SLinus Torvalds 	return 1;
9041da177e4SLinus Torvalds }
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds #undef STATE_TRACE
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds #ifdef STATE_TRACE
9101da177e4SLinus Torvalds static const char *statename[]={
9111da177e4SLinus Torvalds 	"Unused","Established","Syn Sent","Syn Recv",
9121da177e4SLinus Torvalds 	"Fin Wait 1","Fin Wait 2","Time Wait", "Close",
9131da177e4SLinus Torvalds 	"Close Wait","Last ACK","Listen","Closing"
9141da177e4SLinus Torvalds };
9151da177e4SLinus Torvalds #endif
916490d5046SIlpo Järvinen extern void tcp_set_state(struct sock *sk, int state);
9171da177e4SLinus Torvalds 
9184ac02babSAndi Kleen extern void tcp_done(struct sock *sk);
9191da177e4SLinus Torvalds 
92040efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
9211da177e4SLinus Torvalds {
9221da177e4SLinus Torvalds 	rx_opt->dsack = 0;
9231da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
9271da177e4SLinus Torvalds extern void tcp_select_initial_window(int __space, __u32 mss,
9281da177e4SLinus Torvalds 				      __u32 *rcv_wnd, __u32 *window_clamp,
92931d12926Slaurent chavey 				      int wscale_ok, __u8 *rcv_wscale,
93031d12926Slaurent chavey 				      __u32 init_rcv_wnd);
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds static inline int tcp_win_from_space(int space)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	return sysctl_tcp_adv_win_scale<=0 ?
9351da177e4SLinus Torvalds 		(space>>(-sysctl_tcp_adv_win_scale)) :
9361da177e4SLinus Torvalds 		space - (space>>sysctl_tcp_adv_win_scale);
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
9401da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf -
9431da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
9471da177e4SLinus Torvalds {
9481da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf);
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
95140efc6faSStephen Hemminger static inline void tcp_openreq_init(struct request_sock *req,
9521da177e4SLinus Torvalds 				    struct tcp_options_received *rx_opt,
9531da177e4SLinus Torvalds 				    struct sk_buff *skb)
9541da177e4SLinus Torvalds {
9552e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq = inet_rsk(req);
9562e6599cbSArnaldo Carvalho de Melo 
9571da177e4SLinus Torvalds 	req->rcv_wnd = 0;		/* So that tcp_send_synack() knows! */
9584dfc2817SFlorian Westphal 	req->cookie_ts = 0;
9592e6599cbSArnaldo Carvalho de Melo 	tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
9601da177e4SLinus Torvalds 	req->mss = rx_opt->mss_clamp;
9611da177e4SLinus Torvalds 	req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
9622e6599cbSArnaldo Carvalho de Melo 	ireq->tstamp_ok = rx_opt->tstamp_ok;
9632e6599cbSArnaldo Carvalho de Melo 	ireq->sack_ok = rx_opt->sack_ok;
9642e6599cbSArnaldo Carvalho de Melo 	ireq->snd_wscale = rx_opt->snd_wscale;
9652e6599cbSArnaldo Carvalho de Melo 	ireq->wscale_ok = rx_opt->wscale_ok;
9662e6599cbSArnaldo Carvalho de Melo 	ireq->acked = 0;
9672e6599cbSArnaldo Carvalho de Melo 	ireq->ecn_ok = 0;
968aa8223c7SArnaldo Carvalho de Melo 	ireq->rmt_port = tcp_hdr(skb)->source;
969a3116ac5SKOVACS Krisztian 	ireq->loc_port = tcp_hdr(skb)->dest;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds 
9725c52ba17SPavel Emelyanov extern void tcp_enter_memory_pressure(struct sock *sk);
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
9751da177e4SLinus Torvalds {
9761da177e4SLinus Torvalds 	return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
9821da177e4SLinus Torvalds }
9831da177e4SLinus Torvalds 
984df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp)
985df19a626SEric Dumazet {
986df19a626SEric Dumazet 	return tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
987df19a626SEric Dumazet }
988df19a626SEric Dumazet 
9896c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp)
9906c37e5deSFlavio Leitner {
9916c37e5deSFlavio Leitner 	const struct inet_connection_sock *icsk = &tp->inet_conn;
9926c37e5deSFlavio Leitner 
9936c37e5deSFlavio Leitner 	return min_t(u32, tcp_time_stamp - icsk->icsk_ack.lrcvtime,
9946c37e5deSFlavio Leitner 			  tcp_time_stamp - tp->rcv_tstamp);
9956c37e5deSFlavio Leitner }
9966c37e5deSFlavio Leitner 
997463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
9981da177e4SLinus Torvalds {
999463c84b9SArnaldo Carvalho de Melo 	int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
1000463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
10011da177e4SLinus Torvalds 
1002463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1003463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	return fin_timeout;
10061da177e4SLinus Torvalds }
10071da177e4SLinus Torvalds 
1008c887e6d2SIlpo Järvinen static inline int tcp_paws_check(const struct tcp_options_received *rx_opt,
1009c887e6d2SIlpo Järvinen 				 int paws_win)
10101da177e4SLinus Torvalds {
1011c887e6d2SIlpo Järvinen 	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1012c887e6d2SIlpo Järvinen 		return 1;
1013c887e6d2SIlpo Järvinen 	if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS))
1014c887e6d2SIlpo Järvinen 		return 1;
1015c887e6d2SIlpo Järvinen 
10161da177e4SLinus Torvalds 	return 0;
1017c887e6d2SIlpo Järvinen }
1018c887e6d2SIlpo Järvinen 
1019c887e6d2SIlpo Järvinen static inline int tcp_paws_reject(const struct tcp_options_received *rx_opt,
1020c887e6d2SIlpo Järvinen 				  int rst)
1021c887e6d2SIlpo Järvinen {
1022c887e6d2SIlpo Järvinen 	if (tcp_paws_check(rx_opt, 0))
10231da177e4SLinus Torvalds 		return 0;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
10261da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
10271da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
10281da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
10291da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
10301da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
10311da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
10321da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
10331da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
10361da177e4SLinus Torvalds 	 */
10379d729f72SJames Morris 	if (rst && get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
10381da177e4SLinus Torvalds 		return 0;
10391da177e4SLinus Torvalds 	return 1;
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds #define TCP_CHECK_TIMER(sk) do { } while (0)
10431da177e4SLinus Torvalds 
1044a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net)
10451da177e4SLinus Torvalds {
10461da177e4SLinus Torvalds 	/* See RFC 2012 */
1047cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOALGORITHM, 1);
1048cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
1049cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
1050cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_MAXCONN, -1);
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
10536a438bbeSStephen Hemminger /* from STCP */
1054ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
10550800f170SDavid S. Miller {
10566a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
10576a438bbeSStephen Hemminger 	tp->scoreboard_skb_hint = NULL;
1058ef9da47cSIlpo Järvinen }
1059ef9da47cSIlpo Järvinen 
1060ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
1061ef9da47cSIlpo Järvinen {
1062ef9da47cSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
10636a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
1064b7689205SIlpo Järvinen }
1065b7689205SIlpo Järvinen 
1066cfb6eeb4SYOSHIFUJI Hideaki /* MD5 Signature */
1067cfb6eeb4SYOSHIFUJI Hideaki struct crypto_hash;
1068cfb6eeb4SYOSHIFUJI Hideaki 
1069cfb6eeb4SYOSHIFUJI Hideaki /* - key database */
1070cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key {
1071cfb6eeb4SYOSHIFUJI Hideaki 	u8			*key;
1072cfb6eeb4SYOSHIFUJI Hideaki 	u8			keylen;
1073cfb6eeb4SYOSHIFUJI Hideaki };
1074cfb6eeb4SYOSHIFUJI Hideaki 
1075cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_md5sig_key {
1076f8ab18d2SDavid S. Miller 	struct tcp_md5sig_key	base;
1077cfb6eeb4SYOSHIFUJI Hideaki 	__be32			addr;
1078cfb6eeb4SYOSHIFUJI Hideaki };
1079cfb6eeb4SYOSHIFUJI Hideaki 
1080cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_md5sig_key {
1081f8ab18d2SDavid S. Miller 	struct tcp_md5sig_key	base;
1082cfb6eeb4SYOSHIFUJI Hideaki #if 0
1083cfb6eeb4SYOSHIFUJI Hideaki 	u32			scope_id;	/* XXX */
1084cfb6eeb4SYOSHIFUJI Hideaki #endif
1085cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr		addr;
1086cfb6eeb4SYOSHIFUJI Hideaki };
1087cfb6eeb4SYOSHIFUJI Hideaki 
1088cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */
1089cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info {
1090cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_md5sig_key	*keys4;
1091cfb6eeb4SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1092cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_md5sig_key	*keys6;
1093cfb6eeb4SYOSHIFUJI Hideaki 	u32			entries6;
1094cfb6eeb4SYOSHIFUJI Hideaki 	u32			alloced6;
1095cfb6eeb4SYOSHIFUJI Hideaki #endif
1096cfb6eeb4SYOSHIFUJI Hideaki 	u32			entries4;
1097cfb6eeb4SYOSHIFUJI Hideaki 	u32			alloced4;
1098cfb6eeb4SYOSHIFUJI Hideaki };
1099cfb6eeb4SYOSHIFUJI Hideaki 
1100cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */
1101cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr {
1102cfb6eeb4SYOSHIFUJI Hideaki 	__be32		saddr;
1103cfb6eeb4SYOSHIFUJI Hideaki 	__be32		daddr;
1104cfb6eeb4SYOSHIFUJI Hideaki 	__u8		pad;
1105cfb6eeb4SYOSHIFUJI Hideaki 	__u8		protocol;
1106cfb6eeb4SYOSHIFUJI Hideaki 	__be16		len;
1107cfb6eeb4SYOSHIFUJI Hideaki };
1108cfb6eeb4SYOSHIFUJI Hideaki 
1109cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr {
1110cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr	saddr;
1111cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr daddr;
1112cfb6eeb4SYOSHIFUJI Hideaki 	__be32		len;
1113cfb6eeb4SYOSHIFUJI Hideaki 	__be32		protocol;	/* including padding */
1114cfb6eeb4SYOSHIFUJI Hideaki };
1115cfb6eeb4SYOSHIFUJI Hideaki 
1116cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block {
1117cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr ip4;
1118cfb6eeb4SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1119cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_pseudohdr ip6;
1120cfb6eeb4SYOSHIFUJI Hideaki #endif
1121cfb6eeb4SYOSHIFUJI Hideaki };
1122cfb6eeb4SYOSHIFUJI Hideaki 
1123cfb6eeb4SYOSHIFUJI Hideaki /* - pool: digest algorithm, hash description and scratch buffer */
1124cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool {
1125cfb6eeb4SYOSHIFUJI Hideaki 	struct hash_desc	md5_desc;
1126cfb6eeb4SYOSHIFUJI Hideaki 	union tcp_md5sum_block	md5_blk;
1127cfb6eeb4SYOSHIFUJI Hideaki };
1128cfb6eeb4SYOSHIFUJI Hideaki 
1129cfb6eeb4SYOSHIFUJI Hideaki #define TCP_MD5SIG_MAXKEYS	(~(u32)0)	/* really?! */
1130cfb6eeb4SYOSHIFUJI Hideaki 
1131cfb6eeb4SYOSHIFUJI Hideaki /* - functions */
113253d3176bSChangli Gao extern int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
113353d3176bSChangli Gao 			       struct sock *sk, struct request_sock *req,
113449a72dfbSAdam Langley 			       struct sk_buff *skb);
1135cfb6eeb4SYOSHIFUJI Hideaki extern struct tcp_md5sig_key * tcp_v4_md5_lookup(struct sock *sk,
1136cfb6eeb4SYOSHIFUJI Hideaki 						 struct sock *addr_sk);
113753d3176bSChangli Gao extern int tcp_v4_md5_do_add(struct sock *sk, __be32 addr, u8 *newkey,
1138cfb6eeb4SYOSHIFUJI Hideaki 			     u8 newkeylen);
113953d3176bSChangli Gao extern int tcp_v4_md5_do_del(struct sock *sk, __be32 addr);
1140cfb6eeb4SYOSHIFUJI Hideaki 
11419501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
11429501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_keylen ? 		 \
11439501f972SYOSHIFUJI Hideaki 				 &(struct tcp_md5sig_key) {		 \
11449501f972SYOSHIFUJI Hideaki 					.key = (twsk)->tw_md5_key,	 \
11459501f972SYOSHIFUJI Hideaki 					.keylen = (twsk)->tw_md5_keylen, \
11469501f972SYOSHIFUJI Hideaki 				} : NULL)
11479501f972SYOSHIFUJI Hideaki #else
11489501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	NULL
11499501f972SYOSHIFUJI Hideaki #endif
11509501f972SYOSHIFUJI Hideaki 
11517d720c3eSTejun Heo extern struct tcp_md5sig_pool * __percpu *tcp_alloc_md5sig_pool(struct sock *);
1152cfb6eeb4SYOSHIFUJI Hideaki extern void tcp_free_md5sig_pool(void);
1153cfb6eeb4SYOSHIFUJI Hideaki 
115435790c04SEric Dumazet extern struct tcp_md5sig_pool	*tcp_get_md5sig_pool(void);
115535790c04SEric Dumazet extern void tcp_put_md5sig_pool(void);
115635790c04SEric Dumazet 
115749a72dfbSAdam Langley extern int tcp_md5_hash_header(struct tcp_md5sig_pool *, struct tcphdr *);
115849a72dfbSAdam Langley extern int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, struct sk_buff *,
115949a72dfbSAdam Langley 				 unsigned header_len);
116049a72dfbSAdam Langley extern int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
116149a72dfbSAdam Langley 			    struct tcp_md5sig_key *key);
1162cfb6eeb4SYOSHIFUJI Hideaki 
1163fe067e8aSDavid S. Miller /* write queue abstraction */
1164fe067e8aSDavid S. Miller static inline void tcp_write_queue_purge(struct sock *sk)
1165fe067e8aSDavid S. Miller {
1166fe067e8aSDavid S. Miller 	struct sk_buff *skb;
1167fe067e8aSDavid S. Miller 
1168fe067e8aSDavid S. Miller 	while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL)
11693ab224beSHideo Aoki 		sk_wmem_free_skb(sk, skb);
11703ab224beSHideo Aoki 	sk_mem_reclaim(sk);
11718818a9d8SIlpo Järvinen 	tcp_clear_all_retrans_hints(tcp_sk(sk));
1172fe067e8aSDavid S. Miller }
1173fe067e8aSDavid S. Miller 
1174fe067e8aSDavid S. Miller static inline struct sk_buff *tcp_write_queue_head(struct sock *sk)
1175fe067e8aSDavid S. Miller {
1176cd07a8eaSDavid S. Miller 	return skb_peek(&sk->sk_write_queue);
1177fe067e8aSDavid S. Miller }
1178fe067e8aSDavid S. Miller 
1179fe067e8aSDavid S. Miller static inline struct sk_buff *tcp_write_queue_tail(struct sock *sk)
1180fe067e8aSDavid S. Miller {
1181cd07a8eaSDavid S. Miller 	return skb_peek_tail(&sk->sk_write_queue);
1182fe067e8aSDavid S. Miller }
1183fe067e8aSDavid S. Miller 
1184fe067e8aSDavid S. Miller static inline struct sk_buff *tcp_write_queue_next(struct sock *sk, struct sk_buff *skb)
1185fe067e8aSDavid S. Miller {
1186cd07a8eaSDavid S. Miller 	return skb_queue_next(&sk->sk_write_queue, skb);
1187fe067e8aSDavid S. Miller }
1188fe067e8aSDavid S. Miller 
1189832d11c5SIlpo Järvinen static inline struct sk_buff *tcp_write_queue_prev(struct sock *sk, struct sk_buff *skb)
1190832d11c5SIlpo Järvinen {
1191832d11c5SIlpo Järvinen 	return skb_queue_prev(&sk->sk_write_queue, skb);
1192832d11c5SIlpo Järvinen }
1193832d11c5SIlpo Järvinen 
1194fe067e8aSDavid S. Miller #define tcp_for_write_queue(skb, sk)					\
1195cd07a8eaSDavid S. Miller 	skb_queue_walk(&(sk)->sk_write_queue, skb)
1196fe067e8aSDavid S. Miller 
1197fe067e8aSDavid S. Miller #define tcp_for_write_queue_from(skb, sk)				\
1198cd07a8eaSDavid S. Miller 	skb_queue_walk_from(&(sk)->sk_write_queue, skb)
1199fe067e8aSDavid S. Miller 
1200234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
1201cd07a8eaSDavid S. Miller 	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1202234b6860SIlpo Järvinen 
1203fe067e8aSDavid S. Miller static inline struct sk_buff *tcp_send_head(struct sock *sk)
1204fe067e8aSDavid S. Miller {
1205fe067e8aSDavid S. Miller 	return sk->sk_send_head;
1206fe067e8aSDavid S. Miller }
1207fe067e8aSDavid S. Miller 
1208cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk,
1209cd07a8eaSDavid S. Miller 				   const struct sk_buff *skb)
1210cd07a8eaSDavid S. Miller {
1211cd07a8eaSDavid S. Miller 	return skb_queue_is_last(&sk->sk_write_queue, skb);
1212cd07a8eaSDavid S. Miller }
1213cd07a8eaSDavid S. Miller 
1214fe067e8aSDavid S. Miller static inline void tcp_advance_send_head(struct sock *sk, struct sk_buff *skb)
1215fe067e8aSDavid S. Miller {
1216cd07a8eaSDavid S. Miller 	if (tcp_skb_is_last(sk, skb))
1217fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1218cd07a8eaSDavid S. Miller 	else
1219cd07a8eaSDavid S. Miller 		sk->sk_send_head = tcp_write_queue_next(sk, skb);
1220fe067e8aSDavid S. Miller }
1221fe067e8aSDavid S. Miller 
1222fe067e8aSDavid S. Miller static inline void tcp_check_send_head(struct sock *sk, struct sk_buff *skb_unlinked)
1223fe067e8aSDavid S. Miller {
1224fe067e8aSDavid S. Miller 	if (sk->sk_send_head == skb_unlinked)
1225fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1226fe067e8aSDavid S. Miller }
1227fe067e8aSDavid S. Miller 
1228fe067e8aSDavid S. Miller static inline void tcp_init_send_head(struct sock *sk)
1229fe067e8aSDavid S. Miller {
1230fe067e8aSDavid S. Miller 	sk->sk_send_head = NULL;
1231fe067e8aSDavid S. Miller }
1232fe067e8aSDavid S. Miller 
1233fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1234fe067e8aSDavid S. Miller {
1235fe067e8aSDavid S. Miller 	__skb_queue_tail(&sk->sk_write_queue, skb);
1236fe067e8aSDavid S. Miller }
1237fe067e8aSDavid S. Miller 
1238fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1239fe067e8aSDavid S. Miller {
1240fe067e8aSDavid S. Miller 	__tcp_add_write_queue_tail(sk, skb);
1241fe067e8aSDavid S. Miller 
1242fe067e8aSDavid S. Miller 	/* Queue it, remembering where we must start sending. */
12436859d494SIlpo Järvinen 	if (sk->sk_send_head == NULL) {
1244fe067e8aSDavid S. Miller 		sk->sk_send_head = skb;
12456859d494SIlpo Järvinen 
12466859d494SIlpo Järvinen 		if (tcp_sk(sk)->highest_sack == NULL)
12476859d494SIlpo Järvinen 			tcp_sk(sk)->highest_sack = skb;
12486859d494SIlpo Järvinen 	}
1249fe067e8aSDavid S. Miller }
1250fe067e8aSDavid S. Miller 
1251fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_head(struct sock *sk, struct sk_buff *skb)
1252fe067e8aSDavid S. Miller {
1253fe067e8aSDavid S. Miller 	__skb_queue_head(&sk->sk_write_queue, skb);
1254fe067e8aSDavid S. Miller }
1255fe067e8aSDavid S. Miller 
1256fe067e8aSDavid S. Miller /* Insert buff after skb on the write queue of sk.  */
1257fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_after(struct sk_buff *skb,
1258fe067e8aSDavid S. Miller 						struct sk_buff *buff,
1259fe067e8aSDavid S. Miller 						struct sock *sk)
1260fe067e8aSDavid S. Miller {
12617de6c033SGerrit Renker 	__skb_queue_after(&sk->sk_write_queue, skb, buff);
1262fe067e8aSDavid S. Miller }
1263fe067e8aSDavid S. Miller 
126443f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk.  */
1265fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new,
1266fe067e8aSDavid S. Miller 						  struct sk_buff *skb,
1267fe067e8aSDavid S. Miller 						  struct sock *sk)
1268fe067e8aSDavid S. Miller {
126943f59c89SDavid S. Miller 	__skb_queue_before(&sk->sk_write_queue, skb, new);
12706e421410SIlpo Järvinen 
12716e421410SIlpo Järvinen 	if (sk->sk_send_head == skb)
12726e421410SIlpo Järvinen 		sk->sk_send_head = new;
1273fe067e8aSDavid S. Miller }
1274fe067e8aSDavid S. Miller 
1275fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
1276fe067e8aSDavid S. Miller {
1277fe067e8aSDavid S. Miller 	__skb_unlink(skb, &sk->sk_write_queue);
1278fe067e8aSDavid S. Miller }
1279fe067e8aSDavid S. Miller 
1280fe067e8aSDavid S. Miller static inline int tcp_write_queue_empty(struct sock *sk)
1281fe067e8aSDavid S. Miller {
1282fe067e8aSDavid S. Miller 	return skb_queue_empty(&sk->sk_write_queue);
1283fe067e8aSDavid S. Miller }
1284fe067e8aSDavid S. Miller 
128512d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk)
128612d50c46SKrishna Kumar {
128712d50c46SKrishna Kumar 	if (tcp_send_head(sk)) {
128812d50c46SKrishna Kumar 		struct tcp_sock *tp = tcp_sk(sk);
128912d50c46SKrishna Kumar 
129012d50c46SKrishna Kumar 		__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
129112d50c46SKrishna Kumar 	}
129212d50c46SKrishna Kumar }
129312d50c46SKrishna Kumar 
1294a47e5a98SIlpo Järvinen /* Start sequence of the highest skb with SACKed bit, valid only if
1295a47e5a98SIlpo Järvinen  * sacked > 0 or when the caller has ensured validity by itself.
1296a47e5a98SIlpo Järvinen  */
1297a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
1298a47e5a98SIlpo Järvinen {
1299a47e5a98SIlpo Järvinen 	if (!tp->sacked_out)
1300a47e5a98SIlpo Järvinen 		return tp->snd_una;
13016859d494SIlpo Järvinen 
13026859d494SIlpo Järvinen 	if (tp->highest_sack == NULL)
13036859d494SIlpo Järvinen 		return tp->snd_nxt;
13046859d494SIlpo Järvinen 
1305a47e5a98SIlpo Järvinen 	return TCP_SKB_CB(tp->highest_sack)->seq;
1306a47e5a98SIlpo Järvinen }
1307a47e5a98SIlpo Järvinen 
13086859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
13096859d494SIlpo Järvinen {
13106859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_skb_is_last(sk, skb) ? NULL :
13116859d494SIlpo Järvinen 						tcp_write_queue_next(sk, skb);
13126859d494SIlpo Järvinen }
13136859d494SIlpo Järvinen 
13146859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
13156859d494SIlpo Järvinen {
13166859d494SIlpo Järvinen 	return tcp_sk(sk)->highest_sack;
13176859d494SIlpo Järvinen }
13186859d494SIlpo Järvinen 
13196859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk)
13206859d494SIlpo Järvinen {
13216859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_write_queue_head(sk);
13226859d494SIlpo Järvinen }
13236859d494SIlpo Järvinen 
13246859d494SIlpo Järvinen /* Called when old skb is about to be deleted (to be combined with new skb) */
13256859d494SIlpo Järvinen static inline void tcp_highest_sack_combine(struct sock *sk,
13266859d494SIlpo Järvinen 					    struct sk_buff *old,
13276859d494SIlpo Järvinen 					    struct sk_buff *new)
13286859d494SIlpo Järvinen {
13296859d494SIlpo Järvinen 	if (tcp_sk(sk)->sacked_out && (old == tcp_sk(sk)->highest_sack))
13306859d494SIlpo Järvinen 		tcp_sk(sk)->highest_sack = new;
13316859d494SIlpo Järvinen }
13326859d494SIlpo Järvinen 
13335aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from
13345aa4b32fSAndreas Petlund  * increased latency). Used to trigger latency-reducing mechanisms.
13355aa4b32fSAndreas Petlund  */
13365aa4b32fSAndreas Petlund static inline unsigned int tcp_stream_is_thin(struct tcp_sock *tp)
13375aa4b32fSAndreas Petlund {
13385aa4b32fSAndreas Petlund 	return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp);
13395aa4b32fSAndreas Petlund }
13405aa4b32fSAndreas Petlund 
13411da177e4SLinus Torvalds /* /proc */
13421da177e4SLinus Torvalds enum tcp_seq_states {
13431da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
13441da177e4SLinus Torvalds 	TCP_SEQ_STATE_OPENREQ,
13451da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
13461da177e4SLinus Torvalds 	TCP_SEQ_STATE_TIME_WAIT,
13471da177e4SLinus Torvalds };
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds struct tcp_seq_afinfo {
13501da177e4SLinus Torvalds 	char			*name;
13511da177e4SLinus Torvalds 	sa_family_t		family;
135268fcadd1SDenis V. Lunev 	struct file_operations	seq_fops;
13539427c4b3SDenis V. Lunev 	struct seq_operations	seq_ops;
13541da177e4SLinus Torvalds };
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds struct tcp_iter_state {
1357a4146b1bSDenis V. Lunev 	struct seq_net_private	p;
13581da177e4SLinus Torvalds 	sa_family_t		family;
13591da177e4SLinus Torvalds 	enum tcp_seq_states	state;
13601da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
1361a8b690f9STom Herbert 	int			bucket, offset, sbucket, num, uid;
1362a8b690f9STom Herbert 	loff_t			last_pos;
13631da177e4SLinus Torvalds };
13641da177e4SLinus Torvalds 
13656f8b13bcSDaniel Lezcano extern int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo);
13666f8b13bcSDaniel Lezcano extern void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo);
13671da177e4SLinus Torvalds 
136820380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
1369c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops;
137020380731SArnaldo Carvalho de Melo 
13717d06b2e0SBrian Haley extern void tcp_v4_destroy_sock(struct sock *sk);
137220380731SArnaldo Carvalho de Melo 
1373a430a43dSHerbert Xu extern int tcp_v4_gso_send_check(struct sk_buff *skb);
1374576a30ebSHerbert Xu extern struct sk_buff *tcp_tso_segment(struct sk_buff *skb, int features);
1375bf296b12SHerbert Xu extern struct sk_buff **tcp_gro_receive(struct sk_buff **head,
1376bf296b12SHerbert Xu 					struct sk_buff *skb);
1377bf296b12SHerbert Xu extern struct sk_buff **tcp4_gro_receive(struct sk_buff **head,
1378bf296b12SHerbert Xu 					 struct sk_buff *skb);
1379bf296b12SHerbert Xu extern int tcp_gro_complete(struct sk_buff *skb);
1380bf296b12SHerbert Xu extern int tcp4_gro_complete(struct sk_buff *skb);
1381f4c50d99SHerbert Xu 
138220380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
138320380731SArnaldo Carvalho de Melo extern int tcp4_proc_init(void);
138420380731SArnaldo Carvalho de Melo extern void tcp4_proc_exit(void);
138520380731SArnaldo Carvalho de Melo #endif
138620380731SArnaldo Carvalho de Melo 
1387cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */
1388cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops {
1389cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1390cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key	*(*md5_lookup) (struct sock *sk,
1391cfb6eeb4SYOSHIFUJI Hideaki 						struct sock *addr_sk);
1392cfb6eeb4SYOSHIFUJI Hideaki 	int			(*calc_md5_hash) (char *location,
1393cfb6eeb4SYOSHIFUJI Hideaki 						  struct tcp_md5sig_key *md5,
1394cfb6eeb4SYOSHIFUJI Hideaki 						  struct sock *sk,
1395cfb6eeb4SYOSHIFUJI Hideaki 						  struct request_sock *req,
139649a72dfbSAdam Langley 						  struct sk_buff *skb);
1397cfb6eeb4SYOSHIFUJI Hideaki 	int			(*md5_add) (struct sock *sk,
1398cfb6eeb4SYOSHIFUJI Hideaki 					    struct sock *addr_sk,
1399cfb6eeb4SYOSHIFUJI Hideaki 					    u8 *newkey,
1400cfb6eeb4SYOSHIFUJI Hideaki 					    u8 len);
1401cfb6eeb4SYOSHIFUJI Hideaki 	int			(*md5_parse) (struct sock *sk,
1402cfb6eeb4SYOSHIFUJI Hideaki 					      char __user *optval,
1403cfb6eeb4SYOSHIFUJI Hideaki 					      int optlen);
1404cfb6eeb4SYOSHIFUJI Hideaki #endif
1405cfb6eeb4SYOSHIFUJI Hideaki };
1406cfb6eeb4SYOSHIFUJI Hideaki 
1407cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops {
1408cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1409cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key	*(*md5_lookup) (struct sock *sk,
1410cfb6eeb4SYOSHIFUJI Hideaki 						struct request_sock *req);
1411e3afe7b7SJohn Dykstra 	int			(*calc_md5_hash) (char *location,
1412e3afe7b7SJohn Dykstra 						  struct tcp_md5sig_key *md5,
1413e3afe7b7SJohn Dykstra 						  struct sock *sk,
1414e3afe7b7SJohn Dykstra 						  struct request_sock *req,
1415e3afe7b7SJohn Dykstra 						  struct sk_buff *skb);
1416cfb6eeb4SYOSHIFUJI Hideaki #endif
1417cfb6eeb4SYOSHIFUJI Hideaki };
1418cfb6eeb4SYOSHIFUJI Hideaki 
1419da5c78c8SWilliam Allen Simpson /* Using SHA1 for now, define some constants.
1420da5c78c8SWilliam Allen Simpson  */
1421da5c78c8SWilliam Allen Simpson #define COOKIE_DIGEST_WORDS (SHA_DIGEST_WORDS)
1422da5c78c8SWilliam Allen Simpson #define COOKIE_MESSAGE_WORDS (SHA_MESSAGE_BYTES / 4)
1423da5c78c8SWilliam Allen Simpson #define COOKIE_WORKSPACE_WORDS (COOKIE_DIGEST_WORDS + COOKIE_MESSAGE_WORDS)
1424da5c78c8SWilliam Allen Simpson 
1425da5c78c8SWilliam Allen Simpson extern int tcp_cookie_generator(u32 *bakery);
1426da5c78c8SWilliam Allen Simpson 
1427435cf559SWilliam Allen Simpson /**
1428435cf559SWilliam Allen Simpson  *	struct tcp_cookie_values - each socket needs extra space for the
1429435cf559SWilliam Allen Simpson  *	cookies, together with (optional) space for any SYN data.
1430435cf559SWilliam Allen Simpson  *
1431435cf559SWilliam Allen Simpson  *	A tcp_sock contains a pointer to the current value, and this is
1432435cf559SWilliam Allen Simpson  *	cloned to the tcp_timewait_sock.
1433435cf559SWilliam Allen Simpson  *
1434435cf559SWilliam Allen Simpson  * @cookie_pair:	variable data from the option exchange.
1435435cf559SWilliam Allen Simpson  *
1436435cf559SWilliam Allen Simpson  * @cookie_desired:	user specified tcpct_cookie_desired.  Zero
1437435cf559SWilliam Allen Simpson  *			indicates default (sysctl_tcp_cookie_size).
1438435cf559SWilliam Allen Simpson  *			After cookie sent, remembers size of cookie.
1439435cf559SWilliam Allen Simpson  *			Range 0, TCP_COOKIE_MIN to TCP_COOKIE_MAX.
1440435cf559SWilliam Allen Simpson  *
1441435cf559SWilliam Allen Simpson  * @s_data_desired:	user specified tcpct_s_data_desired.  When the
1442435cf559SWilliam Allen Simpson  *			constant payload is specified (@s_data_constant),
1443435cf559SWilliam Allen Simpson  *			holds its length instead.
1444435cf559SWilliam Allen Simpson  *			Range 0 to TCP_MSS_DESIRED.
1445435cf559SWilliam Allen Simpson  *
1446435cf559SWilliam Allen Simpson  * @s_data_payload:	constant data that is to be included in the
1447435cf559SWilliam Allen Simpson  *			payload of SYN or SYNACK segments when the
1448435cf559SWilliam Allen Simpson  *			cookie option is present.
1449435cf559SWilliam Allen Simpson  */
1450435cf559SWilliam Allen Simpson struct tcp_cookie_values {
1451435cf559SWilliam Allen Simpson 	struct kref	kref;
1452435cf559SWilliam Allen Simpson 	u8		cookie_pair[TCP_COOKIE_PAIR_SIZE];
1453435cf559SWilliam Allen Simpson 	u8		cookie_pair_size;
1454435cf559SWilliam Allen Simpson 	u8		cookie_desired;
1455435cf559SWilliam Allen Simpson 	u16		s_data_desired:11,
1456435cf559SWilliam Allen Simpson 			s_data_constant:1,
1457435cf559SWilliam Allen Simpson 			s_data_in:1,
1458435cf559SWilliam Allen Simpson 			s_data_out:1,
1459435cf559SWilliam Allen Simpson 			s_data_unused:2;
1460435cf559SWilliam Allen Simpson 	u8		s_data_payload[0];
1461435cf559SWilliam Allen Simpson };
1462435cf559SWilliam Allen Simpson 
1463435cf559SWilliam Allen Simpson static inline void tcp_cookie_values_release(struct kref *kref)
1464435cf559SWilliam Allen Simpson {
1465435cf559SWilliam Allen Simpson 	kfree(container_of(kref, struct tcp_cookie_values, kref));
1466435cf559SWilliam Allen Simpson }
1467435cf559SWilliam Allen Simpson 
1468435cf559SWilliam Allen Simpson /* The length of constant payload data.  Note that s_data_desired is
1469435cf559SWilliam Allen Simpson  * overloaded, depending on s_data_constant: either the length of constant
1470435cf559SWilliam Allen Simpson  * data (returned here) or the limit on variable data.
1471435cf559SWilliam Allen Simpson  */
1472435cf559SWilliam Allen Simpson static inline int tcp_s_data_size(const struct tcp_sock *tp)
1473435cf559SWilliam Allen Simpson {
1474435cf559SWilliam Allen Simpson 	return (tp->cookie_values != NULL && tp->cookie_values->s_data_constant)
1475435cf559SWilliam Allen Simpson 		? tp->cookie_values->s_data_desired
1476435cf559SWilliam Allen Simpson 		: 0;
1477435cf559SWilliam Allen Simpson }
1478435cf559SWilliam Allen Simpson 
1479435cf559SWilliam Allen Simpson /**
1480435cf559SWilliam Allen Simpson  *	struct tcp_extend_values - tcp_ipv?.c to tcp_output.c workspace.
1481435cf559SWilliam Allen Simpson  *
1482435cf559SWilliam Allen Simpson  *	As tcp_request_sock has already been extended in other places, the
1483435cf559SWilliam Allen Simpson  *	only remaining method is to pass stack values along as function
1484435cf559SWilliam Allen Simpson  *	parameters.  These parameters are not needed after sending SYNACK.
1485435cf559SWilliam Allen Simpson  *
1486435cf559SWilliam Allen Simpson  * @cookie_bakery:	cryptographic secret and message workspace.
1487435cf559SWilliam Allen Simpson  *
1488435cf559SWilliam Allen Simpson  * @cookie_plus:	bytes in authenticator/cookie option, copied from
1489435cf559SWilliam Allen Simpson  *			struct tcp_options_received (above).
1490435cf559SWilliam Allen Simpson  */
1491435cf559SWilliam Allen Simpson struct tcp_extend_values {
1492435cf559SWilliam Allen Simpson 	struct request_values		rv;
1493435cf559SWilliam Allen Simpson 	u32				cookie_bakery[COOKIE_WORKSPACE_WORDS];
1494435cf559SWilliam Allen Simpson 	u8				cookie_plus:6,
1495435cf559SWilliam Allen Simpson 					cookie_out_never:1,
1496435cf559SWilliam Allen Simpson 					cookie_in_always:1;
1497435cf559SWilliam Allen Simpson };
1498435cf559SWilliam Allen Simpson 
1499435cf559SWilliam Allen Simpson static inline struct tcp_extend_values *tcp_xv(struct request_values *rvp)
1500435cf559SWilliam Allen Simpson {
1501435cf559SWilliam Allen Simpson 	return (struct tcp_extend_values *)rvp;
1502435cf559SWilliam Allen Simpson }
1503435cf559SWilliam Allen Simpson 
15049b0f976fSDenis V. Lunev extern void tcp_v4_init(void);
150520380731SArnaldo Carvalho de Melo extern void tcp_init(void);
150620380731SArnaldo Carvalho de Melo 
15071da177e4SLinus Torvalds #endif	/* _TCP_H */
1508