xref: /linux/include/net/tcp.h (revision 17b085eacef81a6286bd478f2ec75e04abb091cb)
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/config.h>
251da177e4SLinus Torvalds #include <linux/list.h>
261da177e4SLinus Torvalds #include <linux/tcp.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/cache.h>
291da177e4SLinus Torvalds #include <linux/percpu.h>
303f421baaSArnaldo Carvalho de Melo 
313f421baaSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
32295ff7edSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h>
3377d8bf9cSArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
341da177e4SLinus Torvalds #include <net/checksum.h>
352e6599cbSArnaldo Carvalho de Melo #include <net/request_sock.h>
361da177e4SLinus Torvalds #include <net/sock.h>
371da177e4SLinus Torvalds #include <net/snmp.h>
381da177e4SLinus Torvalds #include <net/ip.h>
39c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
40c752f073SArnaldo Carvalho de Melo 
411da177e4SLinus Torvalds #include <linux/seq_file.h>
421da177e4SLinus Torvalds 
430f7ff927SArnaldo Carvalho de Melo extern struct inet_hashinfo tcp_hashinfo;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds extern atomic_t tcp_orphan_count;
461da177e4SLinus Torvalds extern void tcp_time_wait(struct sock *sk, int state, int timeo);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds #define MAX_TCP_HEADER	(128 + MAX_HEADER)
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /*
511da177e4SLinus Torvalds  * Never offer a window over 32767 without using window scaling. Some
521da177e4SLinus Torvalds  * poor stacks do signed 16bit maths!
531da177e4SLinus Torvalds  */
541da177e4SLinus Torvalds #define MAX_TCP_WINDOW		32767U
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
571da177e4SLinus Torvalds #define TCP_MIN_MSS		88U
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* Minimal RCV_MSS. */
601da177e4SLinus Torvalds #define TCP_MIN_RCVMSS		536U
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
631da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* Maximal reordering. */
661da177e4SLinus Torvalds #define TCP_MAX_REORDERING	127
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
691da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* urg_data states */
721da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
731da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
741da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds #define TCP_RETR1	3	/*
771da177e4SLinus Torvalds 				 * This is how many retries it does before it
781da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
791da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
801da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
811da177e4SLinus Torvalds 				 */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds #define TCP_RETR2	15	/*
841da177e4SLinus Torvalds 				 * This should take at least
851da177e4SLinus Torvalds 				 * 90 minutes to time out.
861da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
871da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
881da177e4SLinus Torvalds 				 */
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds #define TCP_SYN_RETRIES	 5	/* number of times to retry active opening a
911da177e4SLinus Torvalds 				 * connection: ~180sec is RFC minumum	*/
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #define TCP_SYNACK_RETRIES 5	/* number of times to retry passive opening a
941da177e4SLinus Torvalds 				 * connection: ~180sec is RFC minumum	*/
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #define TCP_ORPHAN_RETRIES 7	/* number of times to retry on an orphaned
981da177e4SLinus Torvalds 				 * socket. 7 is ~50sec-16min.
991da177e4SLinus Torvalds 				 */
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1031da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1041da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1051da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1061da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1071da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1081da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1091da177e4SLinus Torvalds 				  */
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1121da177e4SLinus Torvalds #if HZ >= 100
1131da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1141da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1151da177e4SLinus Torvalds #else
1161da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1171da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1181da177e4SLinus Torvalds #endif
1191da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1201da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
1211da177e4SLinus Torvalds #define TCP_TIMEOUT_INIT ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value	*/
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1241da177e4SLinus Torvalds 					                 * for local resources.
1251da177e4SLinus Torvalds 					                 */
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1281da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1291da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1321da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1331da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1341da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1371da177e4SLinus Torvalds #define TCP_SYNQ_HSIZE		512	/* Size of SYNACK hash table */
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1401da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1411da177e4SLinus Torvalds 					 * after this time. It should be equal
1421da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1431da177e4SLinus Torvalds 					 * to provide reliability equal to one
1441da177e4SLinus Torvalds 					 * provided by timewait state.
1451da177e4SLinus Torvalds 					 */
1461da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1471da177e4SLinus Torvalds 					 * timestamps. It must be less than
1481da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1491da177e4SLinus Torvalds 					 */
1501da177e4SLinus Torvalds /*
1511da177e4SLinus Torvalds  *	TCP option
1521da177e4SLinus Torvalds  */
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1551da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1561da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1571da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1581da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1591da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1601da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /*
1631da177e4SLinus Torvalds  *     TCP option lengths
1641da177e4SLinus Torvalds  */
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds #define TCPOLEN_MSS            4
1671da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
1681da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
1691da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds /* But this is what stacks really send out. */
1721da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
1731da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
1741da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
1751da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
1761da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
1771da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds /* Flags in tp->nonagle */
1801da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
1811da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
1821da177e4SLinus Torvalds #define TCP_NAGLE_PUSH		4	/* Cork is overriden for already queued data */
1831da177e4SLinus Torvalds 
184295ff7edSArnaldo Carvalho de Melo extern struct inet_timewait_death_row tcp_death_row;
185295ff7edSArnaldo Carvalho de Melo 
1861da177e4SLinus Torvalds /* sysctl variables for tcp */
1871da177e4SLinus Torvalds extern int sysctl_tcp_timestamps;
1881da177e4SLinus Torvalds extern int sysctl_tcp_window_scaling;
1891da177e4SLinus Torvalds extern int sysctl_tcp_sack;
1901da177e4SLinus Torvalds extern int sysctl_tcp_fin_timeout;
1911da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_time;
1921da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_probes;
1931da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_intvl;
1941da177e4SLinus Torvalds extern int sysctl_tcp_syn_retries;
1951da177e4SLinus Torvalds extern int sysctl_tcp_synack_retries;
1961da177e4SLinus Torvalds extern int sysctl_tcp_retries1;
1971da177e4SLinus Torvalds extern int sysctl_tcp_retries2;
1981da177e4SLinus Torvalds extern int sysctl_tcp_orphan_retries;
1991da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
2001da177e4SLinus Torvalds extern int sysctl_tcp_retrans_collapse;
2011da177e4SLinus Torvalds extern int sysctl_tcp_stdurg;
2021da177e4SLinus Torvalds extern int sysctl_tcp_rfc1337;
2031da177e4SLinus Torvalds extern int sysctl_tcp_abort_on_overflow;
2041da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
2051da177e4SLinus Torvalds extern int sysctl_tcp_fack;
2061da177e4SLinus Torvalds extern int sysctl_tcp_reordering;
2071da177e4SLinus Torvalds extern int sysctl_tcp_ecn;
2081da177e4SLinus Torvalds extern int sysctl_tcp_dsack;
2091da177e4SLinus Torvalds extern int sysctl_tcp_mem[3];
2101da177e4SLinus Torvalds extern int sysctl_tcp_wmem[3];
2111da177e4SLinus Torvalds extern int sysctl_tcp_rmem[3];
2121da177e4SLinus Torvalds extern int sysctl_tcp_app_win;
2131da177e4SLinus Torvalds extern int sysctl_tcp_adv_win_scale;
2141da177e4SLinus Torvalds extern int sysctl_tcp_tw_reuse;
2151da177e4SLinus Torvalds extern int sysctl_tcp_frto;
2161da177e4SLinus Torvalds extern int sysctl_tcp_low_latency;
2171da177e4SLinus Torvalds extern int sysctl_tcp_nometrics_save;
2181da177e4SLinus Torvalds extern int sysctl_tcp_moderate_rcvbuf;
2191da177e4SLinus Torvalds extern int sysctl_tcp_tso_win_divisor;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds extern atomic_t tcp_memory_allocated;
2221da177e4SLinus Torvalds extern atomic_t tcp_sockets_allocated;
2231da177e4SLinus Torvalds extern int tcp_memory_pressure;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds /*
2261da177e4SLinus Torvalds  *	Pointers to address related TCP functions
2271da177e4SLinus Torvalds  *	(i.e. things that depend on the address family)
2281da177e4SLinus Torvalds  */
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds struct tcp_func {
2311da177e4SLinus Torvalds 	int			(*queue_xmit)		(struct sk_buff *skb,
2321da177e4SLinus Torvalds 							 int ipfragok);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	void			(*send_check)		(struct sock *sk,
2351da177e4SLinus Torvalds 							 struct tcphdr *th,
2361da177e4SLinus Torvalds 							 int len,
2371da177e4SLinus Torvalds 							 struct sk_buff *skb);
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	int			(*rebuild_header)	(struct sock *sk);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	int			(*conn_request)		(struct sock *sk,
2421da177e4SLinus Torvalds 							 struct sk_buff *skb);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	struct sock *		(*syn_recv_sock)	(struct sock *sk,
2451da177e4SLinus Torvalds 							 struct sk_buff *skb,
24660236fddSArnaldo Carvalho de Melo 							 struct request_sock *req,
2471da177e4SLinus Torvalds 							 struct dst_entry *dst);
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	int			(*remember_stamp)	(struct sock *sk);
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	__u16			net_header_len;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	int			(*setsockopt)		(struct sock *sk,
2541da177e4SLinus Torvalds 							 int level,
2551da177e4SLinus Torvalds 							 int optname,
2561da177e4SLinus Torvalds 							 char __user *optval,
2571da177e4SLinus Torvalds 							 int optlen);
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	int			(*getsockopt)		(struct sock *sk,
2601da177e4SLinus Torvalds 							 int level,
2611da177e4SLinus Torvalds 							 int optname,
2621da177e4SLinus Torvalds 							 char __user *optval,
2631da177e4SLinus Torvalds 							 int __user *optlen);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	void			(*addr2sockaddr)	(struct sock *sk,
2671da177e4SLinus Torvalds 							 struct sockaddr *);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	int sockaddr_len;
2701da177e4SLinus Torvalds };
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds /*
2731da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
2741da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
2751da177e4SLinus Torvalds  */
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds static inline int before(__u32 seq1, __u32 seq2)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds         return (__s32)(seq1-seq2) < 0;
2801da177e4SLinus Torvalds }
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds static inline int after(__u32 seq1, __u32 seq2)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	return (__s32)(seq2-seq1) < 0;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
2891da177e4SLinus Torvalds static inline int between(__u32 seq1, __u32 seq2, __u32 seq3)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds extern struct proto tcp_prot;
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds DECLARE_SNMP_STAT(struct tcp_mib, tcp_statistics);
2981da177e4SLinus Torvalds #define TCP_INC_STATS(field)		SNMP_INC_STATS(tcp_statistics, field)
2991da177e4SLinus Torvalds #define TCP_INC_STATS_BH(field)		SNMP_INC_STATS_BH(tcp_statistics, field)
3001da177e4SLinus Torvalds #define TCP_INC_STATS_USER(field) 	SNMP_INC_STATS_USER(tcp_statistics, field)
3011da177e4SLinus Torvalds #define TCP_DEC_STATS(field)		SNMP_DEC_STATS(tcp_statistics, field)
3021da177e4SLinus Torvalds #define TCP_ADD_STATS_BH(field, val)	SNMP_ADD_STATS_BH(tcp_statistics, field, val)
3031da177e4SLinus Torvalds #define TCP_ADD_STATS_USER(field, val)	SNMP_ADD_STATS_USER(tcp_statistics, field, val)
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds extern void			tcp_v4_err(struct sk_buff *skb, u32);
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds extern void			tcp_shutdown (struct sock *sk, int how);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds extern int			tcp_v4_rcv(struct sk_buff *skb);
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds extern int			tcp_v4_remember_stamp(struct sock *sk);
3121da177e4SLinus Torvalds 
3138feaf0c0SArnaldo Carvalho de Melo extern int		    	tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds extern int			tcp_sendmsg(struct kiocb *iocb, struct sock *sk,
3161da177e4SLinus Torvalds 					    struct msghdr *msg, size_t size);
3171da177e4SLinus Torvalds extern ssize_t			tcp_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds extern int			tcp_ioctl(struct sock *sk,
3201da177e4SLinus Torvalds 					  int cmd,
3211da177e4SLinus Torvalds 					  unsigned long arg);
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds extern int			tcp_rcv_state_process(struct sock *sk,
3241da177e4SLinus Torvalds 						      struct sk_buff *skb,
3251da177e4SLinus Torvalds 						      struct tcphdr *th,
3261da177e4SLinus Torvalds 						      unsigned len);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds extern int			tcp_rcv_established(struct sock *sk,
3291da177e4SLinus Torvalds 						    struct sk_buff *skb,
3301da177e4SLinus Torvalds 						    struct tcphdr *th,
3311da177e4SLinus Torvalds 						    unsigned len);
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds extern void			tcp_rcv_space_adjust(struct sock *sk);
3341da177e4SLinus Torvalds 
335463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
336463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3371da177e4SLinus Torvalds {
338463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
339fc6415bcSDavid S. Miller 
340463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
341463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
342463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3431da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
344463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
345fc6415bcSDavid S. Miller 		} else
346463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3471da177e4SLinus Torvalds 	}
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
350463c84b9SArnaldo Carvalho de Melo extern void tcp_enter_quickack_mode(struct sock *sk);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static inline void tcp_clear_options(struct tcp_options_received *rx_opt)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds  	rx_opt->tstamp_ok = rx_opt->sack_ok = rx_opt->wscale_ok = rx_opt->snd_wscale = 0;
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds enum tcp_tw_status
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
3601da177e4SLinus Torvalds 	TCP_TW_RST = 1,
3611da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
3621da177e4SLinus Torvalds 	TCP_TW_SYN = 3
3631da177e4SLinus Torvalds };
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 
3668feaf0c0SArnaldo Carvalho de Melo extern enum tcp_tw_status	tcp_timewait_state_process(struct inet_timewait_sock *tw,
3671da177e4SLinus Torvalds 							   struct sk_buff *skb,
3688feaf0c0SArnaldo Carvalho de Melo 							   const struct tcphdr *th);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds extern struct sock *		tcp_check_req(struct sock *sk,struct sk_buff *skb,
37160236fddSArnaldo Carvalho de Melo 					      struct request_sock *req,
37260236fddSArnaldo Carvalho de Melo 					      struct request_sock **prev);
3731da177e4SLinus Torvalds extern int			tcp_child_process(struct sock *parent,
3741da177e4SLinus Torvalds 						  struct sock *child,
3751da177e4SLinus Torvalds 						  struct sk_buff *skb);
3761da177e4SLinus Torvalds extern void			tcp_enter_frto(struct sock *sk);
3771da177e4SLinus Torvalds extern void			tcp_enter_loss(struct sock *sk, int how);
3781da177e4SLinus Torvalds extern void			tcp_clear_retrans(struct tcp_sock *tp);
3791da177e4SLinus Torvalds extern void			tcp_update_metrics(struct sock *sk);
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds extern void			tcp_close(struct sock *sk,
3821da177e4SLinus Torvalds 					  long timeout);
3831da177e4SLinus Torvalds extern unsigned int		tcp_poll(struct file * file, struct socket *sock, struct poll_table_struct *wait);
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds extern int			tcp_getsockopt(struct sock *sk, int level,
3861da177e4SLinus Torvalds 					       int optname,
3871da177e4SLinus Torvalds 					       char __user *optval,
3881da177e4SLinus Torvalds 					       int __user *optlen);
3891da177e4SLinus Torvalds extern int			tcp_setsockopt(struct sock *sk, int level,
3901da177e4SLinus Torvalds 					       int optname, char __user *optval,
3911da177e4SLinus Torvalds 					       int optlen);
3921da177e4SLinus Torvalds extern void			tcp_set_keepalive(struct sock *sk, int val);
3931da177e4SLinus Torvalds extern int			tcp_recvmsg(struct kiocb *iocb, struct sock *sk,
3941da177e4SLinus Torvalds 					    struct msghdr *msg,
3951da177e4SLinus Torvalds 					    size_t len, int nonblock,
3961da177e4SLinus Torvalds 					    int flags, int *addr_len);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds extern void			tcp_parse_options(struct sk_buff *skb,
3991da177e4SLinus Torvalds 						  struct tcp_options_received *opt_rx,
4001da177e4SLinus Torvalds 						  int estab);
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /*
4031da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4041da177e4SLinus Torvalds  */
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds extern void		       	tcp_v4_send_check(struct sock *sk,
4071da177e4SLinus Torvalds 						  struct tcphdr *th, int len,
4081da177e4SLinus Torvalds 						  struct sk_buff *skb);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds extern int			tcp_v4_conn_request(struct sock *sk,
4111da177e4SLinus Torvalds 						    struct sk_buff *skb);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds extern struct sock *		tcp_create_openreq_child(struct sock *sk,
41460236fddSArnaldo Carvalho de Melo 							 struct request_sock *req,
4151da177e4SLinus Torvalds 							 struct sk_buff *skb);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds extern struct sock *		tcp_v4_syn_recv_sock(struct sock *sk,
4181da177e4SLinus Torvalds 						     struct sk_buff *skb,
41960236fddSArnaldo Carvalho de Melo 						     struct request_sock *req,
4201da177e4SLinus Torvalds 							struct dst_entry *dst);
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds extern int			tcp_v4_do_rcv(struct sock *sk,
4231da177e4SLinus Torvalds 					      struct sk_buff *skb);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds extern int			tcp_v4_connect(struct sock *sk,
4261da177e4SLinus Torvalds 					       struct sockaddr *uaddr,
4271da177e4SLinus Torvalds 					       int addr_len);
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds extern int			tcp_connect(struct sock *sk);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds extern struct sk_buff *		tcp_make_synack(struct sock *sk,
4321da177e4SLinus Torvalds 						struct dst_entry *dst,
43360236fddSArnaldo Carvalho de Melo 						struct request_sock *req);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds extern int			tcp_disconnect(struct sock *sk, int flags);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds extern void			tcp_unhash(struct sock *sk);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds extern int			tcp_v4_hash_connecting(struct sock *sk);
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds /* From syncookies.c */
4431da177e4SLinus Torvalds extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
4441da177e4SLinus Torvalds 				    struct ip_options *opt);
4451da177e4SLinus Torvalds extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb,
4461da177e4SLinus Torvalds 				     __u16 *mss);
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds /* tcp_output.c */
4491da177e4SLinus Torvalds 
450f6302d1dSDavid S. Miller extern void __tcp_push_pending_frames(struct sock *sk, struct tcp_sock *tp,
451a2e2a59cSDavid S. Miller 				      unsigned int cur_mss, int nonagle);
452f6302d1dSDavid S. Miller extern int tcp_may_send_now(struct sock *sk, struct tcp_sock *tp);
4531da177e4SLinus Torvalds extern int tcp_retransmit_skb(struct sock *, struct sk_buff *);
4541da177e4SLinus Torvalds extern void tcp_xmit_retransmit_queue(struct sock *);
4551da177e4SLinus Torvalds extern void tcp_simple_retransmit(struct sock *);
4561da177e4SLinus Torvalds extern int tcp_trim_head(struct sock *, struct sk_buff *, u32);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds extern void tcp_send_probe0(struct sock *);
4591da177e4SLinus Torvalds extern void tcp_send_partial(struct sock *);
4601da177e4SLinus Torvalds extern int  tcp_write_wakeup(struct sock *);
4611da177e4SLinus Torvalds extern void tcp_send_fin(struct sock *sk);
46286a76cafSVictor Fusco extern void tcp_send_active_reset(struct sock *sk,
46386a76cafSVictor Fusco                                   unsigned int __nocast priority);
4641da177e4SLinus Torvalds extern int  tcp_send_synack(struct sock *);
465c1b4a7e6SDavid S. Miller extern void tcp_push_one(struct sock *, unsigned int mss_now);
4661da177e4SLinus Torvalds extern void tcp_send_ack(struct sock *sk);
4671da177e4SLinus Torvalds extern void tcp_send_delayed_ack(struct sock *sk);
4681da177e4SLinus Torvalds 
469a762a980SDavid S. Miller /* tcp_input.c */
470a762a980SDavid S. Miller extern void tcp_cwnd_application_limited(struct sock *sk);
471a762a980SDavid S. Miller 
4721da177e4SLinus Torvalds /* tcp_timer.c */
4731da177e4SLinus Torvalds extern void tcp_init_xmit_timers(struct sock *);
474463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
475463c84b9SArnaldo Carvalho de Melo {
476463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
477463c84b9SArnaldo Carvalho de Melo }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds extern unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
4801da177e4SLinus Torvalds extern unsigned int tcp_current_mss(struct sock *sk, int large);
4811da177e4SLinus Torvalds 
482*17b085eaSArnaldo Carvalho de Melo /* tcp.c */
4831da177e4SLinus Torvalds extern void tcp_get_info(struct sock *, struct tcp_info *);
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
4861da177e4SLinus Torvalds typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
4871da177e4SLinus Torvalds 				unsigned int, size_t);
4881da177e4SLinus Torvalds extern int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
4891da177e4SLinus Torvalds 			 sk_read_actor_t recv_actor);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /* Initialize RCV_MSS value.
4921da177e4SLinus Torvalds  * RCV_MSS is an our guess about MSS used by the peer.
4931da177e4SLinus Torvalds  * We haven't any direct information about the MSS.
4941da177e4SLinus Torvalds  * It's better to underestimate the RCV_MSS rather than overestimate.
4951da177e4SLinus Torvalds  * Overestimations make us ACKing less frequently than needed.
4961da177e4SLinus Torvalds  * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
4971da177e4SLinus Torvalds  */
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds static inline void tcp_initialize_rcv_mss(struct sock *sk)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
502c1b4a7e6SDavid S. Miller 	unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	hint = min(hint, tp->rcv_wnd/2);
5051da177e4SLinus Torvalds 	hint = min(hint, TCP_MIN_RCVMSS);
5061da177e4SLinus Torvalds 	hint = max(hint, TCP_MIN_MSS);
5071da177e4SLinus Torvalds 
508463c84b9SArnaldo Carvalho de Melo 	inet_csk(sk)->icsk_ack.rcv_mss = hint;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds static __inline__ void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
5141da177e4SLinus Torvalds 			       ntohl(TCP_FLAG_ACK) |
5151da177e4SLinus Torvalds 			       snd_wnd);
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds static __inline__ void tcp_fast_path_on(struct tcp_sock *tp)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds static inline void tcp_fast_path_check(struct sock *sk, struct tcp_sock *tp)
5241da177e4SLinus Torvalds {
525b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue) &&
5261da177e4SLinus Torvalds 	    tp->rcv_wnd &&
5271da177e4SLinus Torvalds 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
5281da177e4SLinus Torvalds 	    !tp->urg_data)
5291da177e4SLinus Torvalds 		tcp_fast_path_on(tp);
5301da177e4SLinus Torvalds }
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
5331da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
5341da177e4SLinus Torvalds  * than the offered window.
5351da177e4SLinus Torvalds  */
5361da177e4SLinus Torvalds static __inline__ u32 tcp_receive_window(const struct tcp_sock *tp)
5371da177e4SLinus Torvalds {
5381da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	if (win < 0)
5411da177e4SLinus Torvalds 		win = 0;
5421da177e4SLinus Torvalds 	return (u32) win;
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
5461da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
5471da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
5481da177e4SLinus Torvalds  */
5491da177e4SLinus Torvalds extern u32	__tcp_select_window(struct sock *sk);
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /* TCP timestamps are only 32-bits, this causes a slight
5521da177e4SLinus Torvalds  * complication on 64-bit systems since we store a snapshot
5531da177e4SLinus Torvalds  * of jiffies in the buffer control blocks below.  We decidely
5541da177e4SLinus Torvalds  * only use of the low 32-bits of jiffies and hide the ugly
5551da177e4SLinus Torvalds  * casts with the following macro.
5561da177e4SLinus Torvalds  */
5571da177e4SLinus Torvalds #define tcp_time_stamp		((__u32)(jiffies))
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds /* This is what the send packet queueing engine uses to pass
5601da177e4SLinus Torvalds  * TCP per-packet control information to the transmission
5611da177e4SLinus Torvalds  * code.  We also store the host-order sequence numbers in
5621da177e4SLinus Torvalds  * here too.  This is 36 bytes on 32-bit architectures,
5631da177e4SLinus Torvalds  * 40 bytes on 64-bit machines, if this grows please adjust
5641da177e4SLinus Torvalds  * skbuff.h:skbuff->cb[xxx] size appropriately.
5651da177e4SLinus Torvalds  */
5661da177e4SLinus Torvalds struct tcp_skb_cb {
5671da177e4SLinus Torvalds 	union {
5681da177e4SLinus Torvalds 		struct inet_skb_parm	h4;
5691da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
5701da177e4SLinus Torvalds 		struct inet6_skb_parm	h6;
5711da177e4SLinus Torvalds #endif
5721da177e4SLinus Torvalds 	} header;	/* For incoming frames		*/
5731da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
5741da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
5751da177e4SLinus Torvalds 	__u32		when;		/* used to compute rtt's	*/
5761da177e4SLinus Torvalds 	__u8		flags;		/* TCP header flags.		*/
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	/* NOTE: These must match up to the flags byte in a
5791da177e4SLinus Torvalds 	 *       real TCP header.
5801da177e4SLinus Torvalds 	 */
5811da177e4SLinus Torvalds #define TCPCB_FLAG_FIN		0x01
5821da177e4SLinus Torvalds #define TCPCB_FLAG_SYN		0x02
5831da177e4SLinus Torvalds #define TCPCB_FLAG_RST		0x04
5841da177e4SLinus Torvalds #define TCPCB_FLAG_PSH		0x08
5851da177e4SLinus Torvalds #define TCPCB_FLAG_ACK		0x10
5861da177e4SLinus Torvalds #define TCPCB_FLAG_URG		0x20
5871da177e4SLinus Torvalds #define TCPCB_FLAG_ECE		0x40
5881da177e4SLinus Torvalds #define TCPCB_FLAG_CWR		0x80
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	__u8		sacked;		/* State flags for SACK/FACK.	*/
5911da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
5921da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
5931da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
5941da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
5971da177e4SLinus Torvalds #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds #define TCPCB_URG		0x20	/* Urgent pointer advenced here	*/
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds #define TCPCB_AT_TAIL		(TCPCB_URG)
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	__u16		urg_ptr;	/* Valid w/URG flags is set.	*/
6041da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
6051da177e4SLinus Torvalds };
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds #include <net/tcp_ecn.h>
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
6121da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
6131da177e4SLinus Torvalds  */
6141da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	return skb_shinfo(skb)->tso_segs;
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds /* This is valid iff tcp_skb_pcount() > 1. */
6201da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	return skb_shinfo(skb)->tso_size;
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds static inline void tcp_dec_pcount_approx(__u32 *count,
6261da177e4SLinus Torvalds 					 const struct sk_buff *skb)
6271da177e4SLinus Torvalds {
6281da177e4SLinus Torvalds 	if (*count) {
6291da177e4SLinus Torvalds 		*count -= tcp_skb_pcount(skb);
6301da177e4SLinus Torvalds 		if ((int)*count < 0)
6311da177e4SLinus Torvalds 			*count = 0;
6321da177e4SLinus Torvalds 	}
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds static inline void tcp_packets_out_inc(struct sock *sk,
6361da177e4SLinus Torvalds 				       struct tcp_sock *tp,
6371da177e4SLinus Torvalds 				       const struct sk_buff *skb)
6381da177e4SLinus Torvalds {
6391da177e4SLinus Torvalds 	int orig = tp->packets_out;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	tp->packets_out += tcp_skb_pcount(skb);
6421da177e4SLinus Torvalds 	if (!orig)
6433f421baaSArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
6443f421baaSArnaldo Carvalho de Melo 					  inet_csk(sk)->icsk_rto, TCP_RTO_MAX);
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds static inline void tcp_packets_out_dec(struct tcp_sock *tp,
6481da177e4SLinus Torvalds 				       const struct sk_buff *skb)
6491da177e4SLinus Torvalds {
6501da177e4SLinus Torvalds 	tp->packets_out -= tcp_skb_pcount(skb);
6511da177e4SLinus Torvalds }
6521da177e4SLinus Torvalds 
653317a76f9SStephen Hemminger /* Events passed to congestion control interface */
654317a76f9SStephen Hemminger enum tcp_ca_event {
655317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
656317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
657317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
658317a76f9SStephen Hemminger 	CA_EVENT_FRTO,		/* fast recovery timeout */
659317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
660317a76f9SStephen Hemminger 	CA_EVENT_FAST_ACK,	/* in sequence ack */
661317a76f9SStephen Hemminger 	CA_EVENT_SLOW_ACK,	/* other ack */
662317a76f9SStephen Hemminger };
663317a76f9SStephen Hemminger 
664317a76f9SStephen Hemminger /*
665317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
666317a76f9SStephen Hemminger  */
667317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
668317a76f9SStephen Hemminger struct tcp_congestion_ops {
669317a76f9SStephen Hemminger 	struct list_head	list;
670317a76f9SStephen Hemminger 
671317a76f9SStephen Hemminger 	/* initialize private data (optional) */
6726687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
673317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
6746687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
675317a76f9SStephen Hemminger 
676317a76f9SStephen Hemminger 	/* return slow start threshold (required) */
6776687e988SArnaldo Carvalho de Melo 	u32 (*ssthresh)(struct sock *sk);
678317a76f9SStephen Hemminger 	/* lower bound for congestion window (optional) */
6796687e988SArnaldo Carvalho de Melo 	u32 (*min_cwnd)(struct sock *sk);
680317a76f9SStephen Hemminger 	/* do new cwnd calculation (required) */
6816687e988SArnaldo Carvalho de Melo 	void (*cong_avoid)(struct sock *sk, u32 ack,
682317a76f9SStephen Hemminger 			   u32 rtt, u32 in_flight, int good_ack);
683317a76f9SStephen Hemminger 	/* round trip time sample per acked packet (optional) */
6846687e988SArnaldo Carvalho de Melo 	void (*rtt_sample)(struct sock *sk, u32 usrtt);
685317a76f9SStephen Hemminger 	/* call before changing ca_state (optional) */
6866687e988SArnaldo Carvalho de Melo 	void (*set_state)(struct sock *sk, u8 new_state);
687317a76f9SStephen Hemminger 	/* call when cwnd event occurs (optional) */
6886687e988SArnaldo Carvalho de Melo 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
689317a76f9SStephen Hemminger 	/* new value of cwnd after loss (optional) */
6906687e988SArnaldo Carvalho de Melo 	u32  (*undo_cwnd)(struct sock *sk);
691317a76f9SStephen Hemminger 	/* hook for packet ack accounting (optional) */
6926687e988SArnaldo Carvalho de Melo 	void (*pkts_acked)(struct sock *sk, u32 num_acked);
69373c1f4a0SArnaldo Carvalho de Melo 	/* get info for inet_diag (optional) */
6946687e988SArnaldo Carvalho de Melo 	void (*get_info)(struct sock *sk, u32 ext, struct sk_buff *skb);
695317a76f9SStephen Hemminger 
696317a76f9SStephen Hemminger 	char 		name[TCP_CA_NAME_MAX];
697317a76f9SStephen Hemminger 	struct module 	*owner;
698317a76f9SStephen Hemminger };
699317a76f9SStephen Hemminger 
700317a76f9SStephen Hemminger extern int tcp_register_congestion_control(struct tcp_congestion_ops *type);
701317a76f9SStephen Hemminger extern void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
702317a76f9SStephen Hemminger 
7036687e988SArnaldo Carvalho de Melo extern void tcp_init_congestion_control(struct sock *sk);
7046687e988SArnaldo Carvalho de Melo extern void tcp_cleanup_congestion_control(struct sock *sk);
705317a76f9SStephen Hemminger extern int tcp_set_default_congestion_control(const char *name);
706317a76f9SStephen Hemminger extern void tcp_get_default_congestion_control(char *name);
7076687e988SArnaldo Carvalho de Melo extern int tcp_set_congestion_control(struct sock *sk, const char *name);
708317a76f9SStephen Hemminger 
7095f8ef48dSStephen Hemminger extern struct tcp_congestion_ops tcp_init_congestion_ops;
7106687e988SArnaldo Carvalho de Melo extern u32 tcp_reno_ssthresh(struct sock *sk);
7116687e988SArnaldo Carvalho de Melo extern void tcp_reno_cong_avoid(struct sock *sk, u32 ack,
712317a76f9SStephen Hemminger 				u32 rtt, u32 in_flight, int flag);
7136687e988SArnaldo Carvalho de Melo extern u32 tcp_reno_min_cwnd(struct sock *sk);
714a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
715317a76f9SStephen Hemminger 
7166687e988SArnaldo Carvalho de Melo static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
717317a76f9SStephen Hemminger {
7186687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
7196687e988SArnaldo Carvalho de Melo 
7206687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->set_state)
7216687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->set_state(sk, ca_state);
7226687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_state = ca_state;
723317a76f9SStephen Hemminger }
724317a76f9SStephen Hemminger 
7256687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
726317a76f9SStephen Hemminger {
7276687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
7286687e988SArnaldo Carvalho de Melo 
7296687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
7306687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
731317a76f9SStephen Hemminger }
732317a76f9SStephen Hemminger 
7331da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
7341da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
7351da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
7361da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
7371da177e4SLinus Torvalds  *
7381da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
7391da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
7401da177e4SLinus Torvalds  *
7411da177e4SLinus Torvalds  * Read this equation as:
7421da177e4SLinus Torvalds  *
7431da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
7441da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
7451da177e4SLinus Torvalds  *	"Packets fast retransmitted"
7461da177e4SLinus Torvalds  */
7471da177e4SLinus Torvalds static __inline__ unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	return (tp->packets_out - tp->left_out + tp->retrans_out);
7501da177e4SLinus Torvalds }
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
7531da177e4SLinus Torvalds  * The exception is rate halving phase, when cwnd is decreasing towards
7541da177e4SLinus Torvalds  * ssthresh.
7551da177e4SLinus Torvalds  */
7566687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
7571da177e4SLinus Torvalds {
7586687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
7596687e988SArnaldo Carvalho de Melo 	if ((1 << inet_csk(sk)->icsk_ca_state) & (TCPF_CA_CWR | TCPF_CA_Recovery))
7601da177e4SLinus Torvalds 		return tp->snd_ssthresh;
7611da177e4SLinus Torvalds 	else
7621da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
7631da177e4SLinus Torvalds 			   ((tp->snd_cwnd >> 1) +
7641da177e4SLinus Torvalds 			    (tp->snd_cwnd >> 2)));
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds static inline void tcp_sync_left_out(struct tcp_sock *tp)
7681da177e4SLinus Torvalds {
7691da177e4SLinus Torvalds 	if (tp->rx_opt.sack_ok &&
7701da177e4SLinus Torvalds 	    (tp->sacked_out >= tp->packets_out - tp->lost_out))
7711da177e4SLinus Torvalds 		tp->sacked_out = tp->packets_out - tp->lost_out;
7721da177e4SLinus Torvalds 	tp->left_out = tp->sacked_out + tp->lost_out;
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds 
775d1b04c08SBaruch Even /* Set slow start threshold and cwnd not falling to slow start */
7766687e988SArnaldo Carvalho de Melo static inline void __tcp_enter_cwr(struct sock *sk)
7771da177e4SLinus Torvalds {
7786687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
7796687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
7806687e988SArnaldo Carvalho de Melo 
7811da177e4SLinus Torvalds 	tp->undo_marker = 0;
7826687e988SArnaldo Carvalho de Melo 	tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
7831da177e4SLinus Torvalds 	tp->snd_cwnd = min(tp->snd_cwnd,
7841da177e4SLinus Torvalds 			   tcp_packets_in_flight(tp) + 1U);
7851da177e4SLinus Torvalds 	tp->snd_cwnd_cnt = 0;
7861da177e4SLinus Torvalds 	tp->high_seq = tp->snd_nxt;
7871da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
7881da177e4SLinus Torvalds 	TCP_ECN_queue_cwr(tp);
7891da177e4SLinus Torvalds }
7901da177e4SLinus Torvalds 
7916687e988SArnaldo Carvalho de Melo static inline void tcp_enter_cwr(struct sock *sk)
7921da177e4SLinus Torvalds {
7936687e988SArnaldo Carvalho de Melo 	struct tcp_sock *tp = tcp_sk(sk);
7946687e988SArnaldo Carvalho de Melo 
7951da177e4SLinus Torvalds 	tp->prior_ssthresh = 0;
7966687e988SArnaldo Carvalho de Melo 	if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
7976687e988SArnaldo Carvalho de Melo 		__tcp_enter_cwr(sk);
7986687e988SArnaldo Carvalho de Melo 		tcp_set_ca_state(sk, TCP_CA_CWR);
7991da177e4SLinus Torvalds 	}
8001da177e4SLinus Torvalds }
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds extern __u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst);
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds /* Slow start with delack produces 3 packets of burst, so that
8051da177e4SLinus Torvalds  * it is safe "de facto".
8061da177e4SLinus Torvalds  */
8071da177e4SLinus Torvalds static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
8081da177e4SLinus Torvalds {
8091da177e4SLinus Torvalds 	return 3;
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds static __inline__ void tcp_minshall_update(struct tcp_sock *tp, int mss,
8131da177e4SLinus Torvalds 					   const struct sk_buff *skb)
8141da177e4SLinus Torvalds {
8151da177e4SLinus Torvalds 	if (skb->len < mss)
8161da177e4SLinus Torvalds 		tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds static __inline__ void tcp_check_probe_timer(struct sock *sk, struct tcp_sock *tp)
8201da177e4SLinus Torvalds {
821463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
822463c84b9SArnaldo Carvalho de Melo 	if (!tp->packets_out && !icsk->icsk_pending)
8233f421baaSArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
8243f421baaSArnaldo Carvalho de Melo 					  icsk->icsk_rto, TCP_RTO_MAX);
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds static __inline__ void tcp_push_pending_frames(struct sock *sk,
8281da177e4SLinus Torvalds 					       struct tcp_sock *tp)
8291da177e4SLinus Torvalds {
8301da177e4SLinus Torvalds 	__tcp_push_pending_frames(sk, tp, tcp_current_mss(sk, 1), tp->nonagle);
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds static __inline__ void tcp_init_wl(struct tcp_sock *tp, u32 ack, u32 seq)
8341da177e4SLinus Torvalds {
8351da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds static __inline__ void tcp_update_wl(struct tcp_sock *tp, u32 ack, u32 seq)
8391da177e4SLinus Torvalds {
8401da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
8411da177e4SLinus Torvalds }
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds /*
8441da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
8451da177e4SLinus Torvalds  */
8461da177e4SLinus Torvalds static __inline__ u16 tcp_v4_check(struct tcphdr *th, int len,
8471da177e4SLinus Torvalds 				   unsigned long saddr, unsigned long daddr,
8481da177e4SLinus Torvalds 				   unsigned long base)
8491da177e4SLinus Torvalds {
8501da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
8511da177e4SLinus Torvalds }
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds static __inline__ int __tcp_checksum_complete(struct sk_buff *skb)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	return (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds static __inline__ int tcp_checksum_complete(struct sk_buff *skb)
8591da177e4SLinus Torvalds {
8601da177e4SLinus Torvalds 	return skb->ip_summed != CHECKSUM_UNNECESSARY &&
8611da177e4SLinus Torvalds 		__tcp_checksum_complete(skb);
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds /* Prequeue for VJ style copy to user, combined with checksumming. */
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds static __inline__ void tcp_prequeue_init(struct tcp_sock *tp)
8671da177e4SLinus Torvalds {
8681da177e4SLinus Torvalds 	tp->ucopy.task = NULL;
8691da177e4SLinus Torvalds 	tp->ucopy.len = 0;
8701da177e4SLinus Torvalds 	tp->ucopy.memory = 0;
8711da177e4SLinus Torvalds 	skb_queue_head_init(&tp->ucopy.prequeue);
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds /* Packet is added to VJ-style prequeue for processing in process
8751da177e4SLinus Torvalds  * context, if a reader task is waiting. Apparently, this exciting
8761da177e4SLinus Torvalds  * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93)
8771da177e4SLinus Torvalds  * failed somewhere. Latency? Burstiness? Well, at least now we will
8781da177e4SLinus Torvalds  * see, why it failed. 8)8)				  --ANK
8791da177e4SLinus Torvalds  *
8801da177e4SLinus Torvalds  * NOTE: is this not too big to inline?
8811da177e4SLinus Torvalds  */
8821da177e4SLinus Torvalds static __inline__ int tcp_prequeue(struct sock *sk, struct sk_buff *skb)
8831da177e4SLinus Torvalds {
8841da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	if (!sysctl_tcp_low_latency && tp->ucopy.task) {
8871da177e4SLinus Torvalds 		__skb_queue_tail(&tp->ucopy.prequeue, skb);
8881da177e4SLinus Torvalds 		tp->ucopy.memory += skb->truesize;
8891da177e4SLinus Torvalds 		if (tp->ucopy.memory > sk->sk_rcvbuf) {
8901da177e4SLinus Torvalds 			struct sk_buff *skb1;
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 			BUG_ON(sock_owned_by_user(sk));
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 			while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) {
8951da177e4SLinus Torvalds 				sk->sk_backlog_rcv(sk, skb1);
8961da177e4SLinus Torvalds 				NET_INC_STATS_BH(LINUX_MIB_TCPPREQUEUEDROPPED);
8971da177e4SLinus Torvalds 			}
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 			tp->ucopy.memory = 0;
9001da177e4SLinus Torvalds 		} else if (skb_queue_len(&tp->ucopy.prequeue) == 1) {
9011da177e4SLinus Torvalds 			wake_up_interruptible(sk->sk_sleep);
902463c84b9SArnaldo Carvalho de Melo 			if (!inet_csk_ack_scheduled(sk))
903463c84b9SArnaldo Carvalho de Melo 				inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
9043f421baaSArnaldo Carvalho de Melo 						          (3 * TCP_RTO_MIN) / 4,
9053f421baaSArnaldo Carvalho de Melo 							  TCP_RTO_MAX);
9061da177e4SLinus Torvalds 		}
9071da177e4SLinus Torvalds 		return 1;
9081da177e4SLinus Torvalds 	}
9091da177e4SLinus Torvalds 	return 0;
9101da177e4SLinus Torvalds }
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds #undef STATE_TRACE
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds #ifdef STATE_TRACE
9161da177e4SLinus Torvalds static const char *statename[]={
9171da177e4SLinus Torvalds 	"Unused","Established","Syn Sent","Syn Recv",
9181da177e4SLinus Torvalds 	"Fin Wait 1","Fin Wait 2","Time Wait", "Close",
9191da177e4SLinus Torvalds 	"Close Wait","Last ACK","Listen","Closing"
9201da177e4SLinus Torvalds };
9211da177e4SLinus Torvalds #endif
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds static __inline__ void tcp_set_state(struct sock *sk, int state)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	int oldstate = sk->sk_state;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	switch (state) {
9281da177e4SLinus Torvalds 	case TCP_ESTABLISHED:
9291da177e4SLinus Torvalds 		if (oldstate != TCP_ESTABLISHED)
9301da177e4SLinus Torvalds 			TCP_INC_STATS(TCP_MIB_CURRESTAB);
9311da177e4SLinus Torvalds 		break;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	case TCP_CLOSE:
9341da177e4SLinus Torvalds 		if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED)
9351da177e4SLinus Torvalds 			TCP_INC_STATS(TCP_MIB_ESTABRESETS);
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
938463c84b9SArnaldo Carvalho de Melo 		if (inet_csk(sk)->icsk_bind_hash &&
9391da177e4SLinus Torvalds 		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
9402d8c4ce5SArnaldo Carvalho de Melo 			inet_put_port(&tcp_hashinfo, sk);
9411da177e4SLinus Torvalds 		/* fall through */
9421da177e4SLinus Torvalds 	default:
9431da177e4SLinus Torvalds 		if (oldstate==TCP_ESTABLISHED)
9441da177e4SLinus Torvalds 			TCP_DEC_STATS(TCP_MIB_CURRESTAB);
9451da177e4SLinus Torvalds 	}
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds 	/* Change state AFTER socket is unhashed to avoid closed
9481da177e4SLinus Torvalds 	 * socket sitting in hash tables.
9491da177e4SLinus Torvalds 	 */
9501da177e4SLinus Torvalds 	sk->sk_state = state;
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds #ifdef STATE_TRACE
9531da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n",sk, statename[oldstate],statename[state]);
9541da177e4SLinus Torvalds #endif
9551da177e4SLinus Torvalds }
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds static __inline__ void tcp_done(struct sock *sk)
9581da177e4SLinus Torvalds {
9591da177e4SLinus Torvalds 	tcp_set_state(sk, TCP_CLOSE);
9601da177e4SLinus Torvalds 	tcp_clear_xmit_timers(sk);
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	sk->sk_shutdown = SHUTDOWN_MASK;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD))
9651da177e4SLinus Torvalds 		sk->sk_state_change(sk);
9661da177e4SLinus Torvalds 	else
9670a5578cfSArnaldo Carvalho de Melo 		inet_csk_destroy_sock(sk);
9681da177e4SLinus Torvalds }
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds static __inline__ void tcp_sack_reset(struct tcp_options_received *rx_opt)
9711da177e4SLinus Torvalds {
9721da177e4SLinus Torvalds 	rx_opt->dsack = 0;
9731da177e4SLinus Torvalds 	rx_opt->eff_sacks = 0;
9741da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds static __inline__ void tcp_build_and_update_options(__u32 *ptr, struct tcp_sock *tp, __u32 tstamp)
9781da177e4SLinus Torvalds {
9791da177e4SLinus Torvalds 	if (tp->rx_opt.tstamp_ok) {
9801da177e4SLinus Torvalds 		*ptr++ = __constant_htonl((TCPOPT_NOP << 24) |
9811da177e4SLinus Torvalds 					  (TCPOPT_NOP << 16) |
9821da177e4SLinus Torvalds 					  (TCPOPT_TIMESTAMP << 8) |
9831da177e4SLinus Torvalds 					  TCPOLEN_TIMESTAMP);
9841da177e4SLinus Torvalds 		*ptr++ = htonl(tstamp);
9851da177e4SLinus Torvalds 		*ptr++ = htonl(tp->rx_opt.ts_recent);
9861da177e4SLinus Torvalds 	}
9871da177e4SLinus Torvalds 	if (tp->rx_opt.eff_sacks) {
9881da177e4SLinus Torvalds 		struct tcp_sack_block *sp = tp->rx_opt.dsack ? tp->duplicate_sack : tp->selective_acks;
9891da177e4SLinus Torvalds 		int this_sack;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 		*ptr++ = __constant_htonl((TCPOPT_NOP << 24) |
9921da177e4SLinus Torvalds 					  (TCPOPT_NOP << 16) |
9931da177e4SLinus Torvalds 					  (TCPOPT_SACK << 8) |
9941da177e4SLinus Torvalds 					  (TCPOLEN_SACK_BASE +
9951da177e4SLinus Torvalds 					   (tp->rx_opt.eff_sacks * TCPOLEN_SACK_PERBLOCK)));
9961da177e4SLinus Torvalds 		for(this_sack = 0; this_sack < tp->rx_opt.eff_sacks; this_sack++) {
9971da177e4SLinus Torvalds 			*ptr++ = htonl(sp[this_sack].start_seq);
9981da177e4SLinus Torvalds 			*ptr++ = htonl(sp[this_sack].end_seq);
9991da177e4SLinus Torvalds 		}
10001da177e4SLinus Torvalds 		if (tp->rx_opt.dsack) {
10011da177e4SLinus Torvalds 			tp->rx_opt.dsack = 0;
10021da177e4SLinus Torvalds 			tp->rx_opt.eff_sacks--;
10031da177e4SLinus Torvalds 		}
10041da177e4SLinus Torvalds 	}
10051da177e4SLinus Torvalds }
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds /* Construct a tcp options header for a SYN or SYN_ACK packet.
10081da177e4SLinus Torvalds  * If this is every changed make sure to change the definition of
10091da177e4SLinus Torvalds  * MAX_SYN_SIZE to match the new maximum number of options that you
10101da177e4SLinus Torvalds  * can generate.
10111da177e4SLinus Torvalds  */
10121da177e4SLinus Torvalds static inline void tcp_syn_build_options(__u32 *ptr, int mss, int ts, int sack,
10131da177e4SLinus Torvalds 					     int offer_wscale, int wscale, __u32 tstamp, __u32 ts_recent)
10141da177e4SLinus Torvalds {
10151da177e4SLinus Torvalds 	/* We always get an MSS option.
10161da177e4SLinus Torvalds 	 * The option bytes which will be seen in normal data
10171da177e4SLinus Torvalds 	 * packets should timestamps be used, must be in the MSS
10181da177e4SLinus Torvalds 	 * advertised.  But we subtract them from tp->mss_cache so
10191da177e4SLinus Torvalds 	 * that calculations in tcp_sendmsg are simpler etc.
10201da177e4SLinus Torvalds 	 * So account for this fact here if necessary.  If we
10211da177e4SLinus Torvalds 	 * don't do this correctly, as a receiver we won't
10221da177e4SLinus Torvalds 	 * recognize data packets as being full sized when we
10231da177e4SLinus Torvalds 	 * should, and thus we won't abide by the delayed ACK
10241da177e4SLinus Torvalds 	 * rules correctly.
10251da177e4SLinus Torvalds 	 * SACKs don't matter, we never delay an ACK when we
10261da177e4SLinus Torvalds 	 * have any of those going out.
10271da177e4SLinus Torvalds 	 */
10281da177e4SLinus Torvalds 	*ptr++ = htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | mss);
10291da177e4SLinus Torvalds 	if (ts) {
10301da177e4SLinus Torvalds 		if(sack)
10311da177e4SLinus Torvalds 			*ptr++ = __constant_htonl((TCPOPT_SACK_PERM << 24) | (TCPOLEN_SACK_PERM << 16) |
10321da177e4SLinus Torvalds 						  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
10331da177e4SLinus Torvalds 		else
10341da177e4SLinus Torvalds 			*ptr++ = __constant_htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
10351da177e4SLinus Torvalds 						  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
10361da177e4SLinus Torvalds 		*ptr++ = htonl(tstamp);		/* TSVAL */
10371da177e4SLinus Torvalds 		*ptr++ = htonl(ts_recent);	/* TSECR */
10381da177e4SLinus Torvalds 	} else if(sack)
10391da177e4SLinus Torvalds 		*ptr++ = __constant_htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
10401da177e4SLinus Torvalds 					  (TCPOPT_SACK_PERM << 8) | TCPOLEN_SACK_PERM);
10411da177e4SLinus Torvalds 	if (offer_wscale)
10421da177e4SLinus Torvalds 		*ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_WINDOW << 16) | (TCPOLEN_WINDOW << 8) | (wscale));
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
10461da177e4SLinus Torvalds extern void tcp_select_initial_window(int __space, __u32 mss,
10471da177e4SLinus Torvalds 				      __u32 *rcv_wnd, __u32 *window_clamp,
10481da177e4SLinus Torvalds 				      int wscale_ok, __u8 *rcv_wscale);
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds static inline int tcp_win_from_space(int space)
10511da177e4SLinus Torvalds {
10521da177e4SLinus Torvalds 	return sysctl_tcp_adv_win_scale<=0 ?
10531da177e4SLinus Torvalds 		(space>>(-sysctl_tcp_adv_win_scale)) :
10541da177e4SLinus Torvalds 		space - (space>>sysctl_tcp_adv_win_scale);
10551da177e4SLinus Torvalds }
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
10581da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
10591da177e4SLinus Torvalds {
10601da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf -
10611da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
10621da177e4SLinus Torvalds }
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
10651da177e4SLinus Torvalds {
10661da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf);
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds 
106960236fddSArnaldo Carvalho de Melo static __inline__ void tcp_openreq_init(struct request_sock *req,
10701da177e4SLinus Torvalds 					struct tcp_options_received *rx_opt,
10711da177e4SLinus Torvalds 					struct sk_buff *skb)
10721da177e4SLinus Torvalds {
10732e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq = inet_rsk(req);
10742e6599cbSArnaldo Carvalho de Melo 
10751da177e4SLinus Torvalds 	req->rcv_wnd = 0;		/* So that tcp_send_synack() knows! */
10762e6599cbSArnaldo Carvalho de Melo 	tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
10771da177e4SLinus Torvalds 	req->mss = rx_opt->mss_clamp;
10781da177e4SLinus Torvalds 	req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
10792e6599cbSArnaldo Carvalho de Melo 	ireq->tstamp_ok = rx_opt->tstamp_ok;
10802e6599cbSArnaldo Carvalho de Melo 	ireq->sack_ok = rx_opt->sack_ok;
10812e6599cbSArnaldo Carvalho de Melo 	ireq->snd_wscale = rx_opt->snd_wscale;
10822e6599cbSArnaldo Carvalho de Melo 	ireq->wscale_ok = rx_opt->wscale_ok;
10832e6599cbSArnaldo Carvalho de Melo 	ireq->acked = 0;
10842e6599cbSArnaldo Carvalho de Melo 	ireq->ecn_ok = 0;
10852e6599cbSArnaldo Carvalho de Melo 	ireq->rmt_port = skb->h.th->source;
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds extern void tcp_enter_memory_pressure(void);
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
10911da177e4SLinus Torvalds {
10921da177e4SLinus Torvalds 	return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
10961da177e4SLinus Torvalds {
10971da177e4SLinus Torvalds 	return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
10981da177e4SLinus Torvalds }
10991da177e4SLinus Torvalds 
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 
11111da177e4SLinus Torvalds static inline int tcp_paws_check(const struct tcp_options_received *rx_opt, int rst)
11121da177e4SLinus Torvalds {
11131da177e4SLinus Torvalds 	if ((s32)(rx_opt->rcv_tsval - rx_opt->ts_recent) >= 0)
11141da177e4SLinus Torvalds 		return 0;
11151da177e4SLinus Torvalds 	if (xtime.tv_sec >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)
11161da177e4SLinus Torvalds 		return 0;
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
11191da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
11201da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
11211da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
11221da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
11231da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
11241da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
11251da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
11261da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
11291da177e4SLinus Torvalds 	 */
11301da177e4SLinus Torvalds 	if (rst && xtime.tv_sec >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
11311da177e4SLinus Torvalds 		return 0;
11321da177e4SLinus Torvalds 	return 1;
11331da177e4SLinus Torvalds }
11341da177e4SLinus Torvalds 
11351da177e4SLinus Torvalds #define TCP_CHECK_TIMER(sk) do { } while (0)
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds static inline int tcp_use_frto(const struct sock *sk)
11381da177e4SLinus Torvalds {
11391da177e4SLinus Torvalds 	const struct tcp_sock *tp = tcp_sk(sk);
11401da177e4SLinus Torvalds 
11411da177e4SLinus Torvalds 	/* F-RTO must be activated in sysctl and there must be some
11421da177e4SLinus Torvalds 	 * unsent new data, and the advertised window should allow
11431da177e4SLinus Torvalds 	 * sending it.
11441da177e4SLinus Torvalds 	 */
11451da177e4SLinus Torvalds 	return (sysctl_tcp_frto && sk->sk_send_head &&
11461da177e4SLinus Torvalds 		!after(TCP_SKB_CB(sk->sk_send_head)->end_seq,
11471da177e4SLinus Torvalds 		       tp->snd_una + tp->snd_wnd));
11481da177e4SLinus Torvalds }
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds static inline void tcp_mib_init(void)
11511da177e4SLinus Torvalds {
11521da177e4SLinus Torvalds 	/* See RFC 2012 */
11531da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_RTOALGORITHM, 1);
11541da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
11551da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
11561da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_MAXCONN, -1);
11571da177e4SLinus Torvalds }
11581da177e4SLinus Torvalds 
11591da177e4SLinus Torvalds /* /proc */
11601da177e4SLinus Torvalds enum tcp_seq_states {
11611da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
11621da177e4SLinus Torvalds 	TCP_SEQ_STATE_OPENREQ,
11631da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
11641da177e4SLinus Torvalds 	TCP_SEQ_STATE_TIME_WAIT,
11651da177e4SLinus Torvalds };
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds struct tcp_seq_afinfo {
11681da177e4SLinus Torvalds 	struct module		*owner;
11691da177e4SLinus Torvalds 	char			*name;
11701da177e4SLinus Torvalds 	sa_family_t		family;
11711da177e4SLinus Torvalds 	int			(*seq_show) (struct seq_file *m, void *v);
11721da177e4SLinus Torvalds 	struct file_operations	*seq_fops;
11731da177e4SLinus Torvalds };
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds struct tcp_iter_state {
11761da177e4SLinus Torvalds 	sa_family_t		family;
11771da177e4SLinus Torvalds 	enum tcp_seq_states	state;
11781da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
11791da177e4SLinus Torvalds 	int			bucket, sbucket, num, uid;
11801da177e4SLinus Torvalds 	struct seq_operations	seq_ops;
11811da177e4SLinus Torvalds };
11821da177e4SLinus Torvalds 
11831da177e4SLinus Torvalds extern int tcp_proc_register(struct tcp_seq_afinfo *afinfo);
11841da177e4SLinus Torvalds extern void tcp_proc_unregister(struct tcp_seq_afinfo *afinfo);
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds #endif	/* _TCP_H */
1187