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