xref: /linux/include/net/tcp.h (revision 463c84b97f24010a67cd871746d6a7e4c925a5f9)
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
22*463c84b9SArnaldo Carvalho de Melo #define INET_CSK_DEBUG 1
231da177e4SLinus Torvalds #define FASTRETRANS_DEBUG 1
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /* Cancel timers, when they are not required. */
26*463c84b9SArnaldo Carvalho de Melo #undef INET_CSK_CLEAR_TIMERS
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <linux/config.h>
291da177e4SLinus Torvalds #include <linux/list.h>
301da177e4SLinus Torvalds #include <linux/tcp.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/cache.h>
331da177e4SLinus Torvalds #include <linux/percpu.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 int tcp_tw_count;
481da177e4SLinus Torvalds extern void tcp_time_wait(struct sock *sk, int state, int timeo);
498feaf0c0SArnaldo Carvalho de Melo extern void tcp_tw_deschedule(struct inet_timewait_sock *tw);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #define MAX_TCP_HEADER	(128 + MAX_HEADER)
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * Never offer a window over 32767 without using window scaling. Some
551da177e4SLinus Torvalds  * poor stacks do signed 16bit maths!
561da177e4SLinus Torvalds  */
571da177e4SLinus Torvalds #define MAX_TCP_WINDOW		32767U
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
601da177e4SLinus Torvalds #define TCP_MIN_MSS		88U
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /* Minimal RCV_MSS. */
631da177e4SLinus Torvalds #define TCP_MIN_RCVMSS		536U
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
661da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /* Maximal reordering. */
691da177e4SLinus Torvalds #define TCP_MAX_REORDERING	127
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
721da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* urg_data states */
751da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
761da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
771da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds #define TCP_RETR1	3	/*
801da177e4SLinus Torvalds 				 * This is how many retries it does before it
811da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
821da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
831da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
841da177e4SLinus Torvalds 				 */
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds #define TCP_RETR2	15	/*
871da177e4SLinus Torvalds 				 * This should take at least
881da177e4SLinus Torvalds 				 * 90 minutes to time out.
891da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
901da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
911da177e4SLinus Torvalds 				 */
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds #define TCP_SYN_RETRIES	 5	/* number of times to retry active opening a
941da177e4SLinus Torvalds 				 * connection: ~180sec is RFC minumum	*/
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds #define TCP_SYNACK_RETRIES 5	/* number of times to retry passive opening a
971da177e4SLinus Torvalds 				 * connection: ~180sec is RFC minumum	*/
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #define TCP_ORPHAN_RETRIES 7	/* number of times to retry on an orphaned
1011da177e4SLinus Torvalds 				 * socket. 7 is ~50sec-16min.
1021da177e4SLinus Torvalds 				 */
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1061da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1071da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1081da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1091da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1101da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1111da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1121da177e4SLinus Torvalds 				  */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1151da177e4SLinus Torvalds #if HZ >= 100
1161da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1171da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1181da177e4SLinus Torvalds #else
1191da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1201da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1211da177e4SLinus Torvalds #endif
1221da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1231da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
1241da177e4SLinus Torvalds #define TCP_TIMEOUT_INIT ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value	*/
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1271da177e4SLinus Torvalds 					                 * for local resources.
1281da177e4SLinus Torvalds 					                 */
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1311da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1321da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1351da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1361da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1371da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1401da177e4SLinus Torvalds #define TCP_SYNQ_HSIZE		512	/* Size of SYNACK hash table */
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1431da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1441da177e4SLinus Torvalds 					 * after this time. It should be equal
1451da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1461da177e4SLinus Torvalds 					 * to provide reliability equal to one
1471da177e4SLinus Torvalds 					 * provided by timewait state.
1481da177e4SLinus Torvalds 					 */
1491da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1501da177e4SLinus Torvalds 					 * timestamps. It must be less than
1511da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1521da177e4SLinus Torvalds 					 */
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds #define TCP_TW_RECYCLE_SLOTS_LOG	5
1551da177e4SLinus Torvalds #define TCP_TW_RECYCLE_SLOTS		(1<<TCP_TW_RECYCLE_SLOTS_LOG)
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /* If time > 4sec, it is "slow" path, no recycling is required,
1581da177e4SLinus Torvalds    so that we select tick to get range about 4 seconds.
1591da177e4SLinus Torvalds  */
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds #if HZ <= 16 || HZ > 4096
1621da177e4SLinus Torvalds # error Unsupported: HZ <= 16 or HZ > 4096
1631da177e4SLinus Torvalds #elif HZ <= 32
1641da177e4SLinus Torvalds # define TCP_TW_RECYCLE_TICK (5+2-TCP_TW_RECYCLE_SLOTS_LOG)
1651da177e4SLinus Torvalds #elif HZ <= 64
1661da177e4SLinus Torvalds # define TCP_TW_RECYCLE_TICK (6+2-TCP_TW_RECYCLE_SLOTS_LOG)
1671da177e4SLinus Torvalds #elif HZ <= 128
1681da177e4SLinus Torvalds # define TCP_TW_RECYCLE_TICK (7+2-TCP_TW_RECYCLE_SLOTS_LOG)
1691da177e4SLinus Torvalds #elif HZ <= 256
1701da177e4SLinus Torvalds # define TCP_TW_RECYCLE_TICK (8+2-TCP_TW_RECYCLE_SLOTS_LOG)
1711da177e4SLinus Torvalds #elif HZ <= 512
1721da177e4SLinus Torvalds # define TCP_TW_RECYCLE_TICK (9+2-TCP_TW_RECYCLE_SLOTS_LOG)
1731da177e4SLinus Torvalds #elif HZ <= 1024
1741da177e4SLinus Torvalds # define TCP_TW_RECYCLE_TICK (10+2-TCP_TW_RECYCLE_SLOTS_LOG)
1751da177e4SLinus Torvalds #elif HZ <= 2048
1761da177e4SLinus Torvalds # define TCP_TW_RECYCLE_TICK (11+2-TCP_TW_RECYCLE_SLOTS_LOG)
1771da177e4SLinus Torvalds #else
1781da177e4SLinus Torvalds # define TCP_TW_RECYCLE_TICK (12+2-TCP_TW_RECYCLE_SLOTS_LOG)
1791da177e4SLinus Torvalds #endif
1801da177e4SLinus Torvalds /*
1811da177e4SLinus Torvalds  *	TCP option
1821da177e4SLinus Torvalds  */
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1851da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1861da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1871da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1881da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1891da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1901da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds  *     TCP option lengths
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds #define TCPOLEN_MSS            4
1971da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
1981da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
1991da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds /* But this is what stacks really send out. */
2021da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
2031da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
2041da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
2051da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
2061da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
2071da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
2081da177e4SLinus Torvalds 
209*463c84b9SArnaldo Carvalho de Melo #define ICSK_TIME_RETRANS	1	/* Retransmit timer */
210*463c84b9SArnaldo Carvalho de Melo #define ICSK_TIME_DACK		2	/* Delayed ack timer */
211*463c84b9SArnaldo Carvalho de Melo #define ICSK_TIME_PROBE0	3	/* Zero window probe timer */
212*463c84b9SArnaldo Carvalho de Melo #define ICSK_TIME_KEEPOPEN	4	/* Keepalive timer */
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds /* Flags in tp->nonagle */
2151da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
2161da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
2171da177e4SLinus Torvalds #define TCP_NAGLE_PUSH		4	/* Cork is overriden for already queued data */
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds /* sysctl variables for tcp */
2201da177e4SLinus Torvalds extern int sysctl_tcp_timestamps;
2211da177e4SLinus Torvalds extern int sysctl_tcp_window_scaling;
2221da177e4SLinus Torvalds extern int sysctl_tcp_sack;
2231da177e4SLinus Torvalds extern int sysctl_tcp_fin_timeout;
2241da177e4SLinus Torvalds extern int sysctl_tcp_tw_recycle;
2251da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_time;
2261da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_probes;
2271da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_intvl;
2281da177e4SLinus Torvalds extern int sysctl_tcp_syn_retries;
2291da177e4SLinus Torvalds extern int sysctl_tcp_synack_retries;
2301da177e4SLinus Torvalds extern int sysctl_tcp_retries1;
2311da177e4SLinus Torvalds extern int sysctl_tcp_retries2;
2321da177e4SLinus Torvalds extern int sysctl_tcp_orphan_retries;
2331da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
2341da177e4SLinus Torvalds extern int sysctl_tcp_retrans_collapse;
2351da177e4SLinus Torvalds extern int sysctl_tcp_stdurg;
2361da177e4SLinus Torvalds extern int sysctl_tcp_rfc1337;
2371da177e4SLinus Torvalds extern int sysctl_tcp_abort_on_overflow;
2381da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
2391da177e4SLinus Torvalds extern int sysctl_tcp_max_tw_buckets;
2401da177e4SLinus Torvalds extern int sysctl_tcp_fack;
2411da177e4SLinus Torvalds extern int sysctl_tcp_reordering;
2421da177e4SLinus Torvalds extern int sysctl_tcp_ecn;
2431da177e4SLinus Torvalds extern int sysctl_tcp_dsack;
2441da177e4SLinus Torvalds extern int sysctl_tcp_mem[3];
2451da177e4SLinus Torvalds extern int sysctl_tcp_wmem[3];
2461da177e4SLinus Torvalds extern int sysctl_tcp_rmem[3];
2471da177e4SLinus Torvalds extern int sysctl_tcp_app_win;
2481da177e4SLinus Torvalds extern int sysctl_tcp_adv_win_scale;
2491da177e4SLinus Torvalds extern int sysctl_tcp_tw_reuse;
2501da177e4SLinus Torvalds extern int sysctl_tcp_frto;
2511da177e4SLinus Torvalds extern int sysctl_tcp_low_latency;
2521da177e4SLinus Torvalds extern int sysctl_tcp_nometrics_save;
2531da177e4SLinus Torvalds extern int sysctl_tcp_moderate_rcvbuf;
2541da177e4SLinus Torvalds extern int sysctl_tcp_tso_win_divisor;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds extern atomic_t tcp_memory_allocated;
2571da177e4SLinus Torvalds extern atomic_t tcp_sockets_allocated;
2581da177e4SLinus Torvalds extern int tcp_memory_pressure;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
261*463c84b9SArnaldo Carvalho de Melo #define AF_INET_FAMILY(fam) ((fam) == AF_INET)
2621da177e4SLinus Torvalds #else
263*463c84b9SArnaldo Carvalho de Melo #define AF_INET_FAMILY(fam) 1
2641da177e4SLinus Torvalds #endif
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds /*
2671da177e4SLinus Torvalds  *	Pointers to address related TCP functions
2681da177e4SLinus Torvalds  *	(i.e. things that depend on the address family)
2691da177e4SLinus Torvalds  */
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds struct tcp_func {
2721da177e4SLinus Torvalds 	int			(*queue_xmit)		(struct sk_buff *skb,
2731da177e4SLinus Torvalds 							 int ipfragok);
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	void			(*send_check)		(struct sock *sk,
2761da177e4SLinus Torvalds 							 struct tcphdr *th,
2771da177e4SLinus Torvalds 							 int len,
2781da177e4SLinus Torvalds 							 struct sk_buff *skb);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	int			(*rebuild_header)	(struct sock *sk);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	int			(*conn_request)		(struct sock *sk,
2831da177e4SLinus Torvalds 							 struct sk_buff *skb);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	struct sock *		(*syn_recv_sock)	(struct sock *sk,
2861da177e4SLinus Torvalds 							 struct sk_buff *skb,
28760236fddSArnaldo Carvalho de Melo 							 struct request_sock *req,
2881da177e4SLinus Torvalds 							 struct dst_entry *dst);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	int			(*remember_stamp)	(struct sock *sk);
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	__u16			net_header_len;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	int			(*setsockopt)		(struct sock *sk,
2951da177e4SLinus Torvalds 							 int level,
2961da177e4SLinus Torvalds 							 int optname,
2971da177e4SLinus Torvalds 							 char __user *optval,
2981da177e4SLinus Torvalds 							 int optlen);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	int			(*getsockopt)		(struct sock *sk,
3011da177e4SLinus Torvalds 							 int level,
3021da177e4SLinus Torvalds 							 int optname,
3031da177e4SLinus Torvalds 							 char __user *optval,
3041da177e4SLinus Torvalds 							 int __user *optlen);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	void			(*addr2sockaddr)	(struct sock *sk,
3081da177e4SLinus Torvalds 							 struct sockaddr *);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	int sockaddr_len;
3111da177e4SLinus Torvalds };
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds /*
3141da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
3151da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
3161da177e4SLinus Torvalds  */
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds static inline int before(__u32 seq1, __u32 seq2)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds         return (__s32)(seq1-seq2) < 0;
3211da177e4SLinus Torvalds }
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds static inline int after(__u32 seq1, __u32 seq2)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	return (__s32)(seq2-seq1) < 0;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
3301da177e4SLinus Torvalds static inline int between(__u32 seq1, __u32 seq2, __u32 seq3)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds extern struct proto tcp_prot;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds DECLARE_SNMP_STAT(struct tcp_mib, tcp_statistics);
3391da177e4SLinus Torvalds #define TCP_INC_STATS(field)		SNMP_INC_STATS(tcp_statistics, field)
3401da177e4SLinus Torvalds #define TCP_INC_STATS_BH(field)		SNMP_INC_STATS_BH(tcp_statistics, field)
3411da177e4SLinus Torvalds #define TCP_INC_STATS_USER(field) 	SNMP_INC_STATS_USER(tcp_statistics, field)
3421da177e4SLinus Torvalds #define TCP_DEC_STATS(field)		SNMP_DEC_STATS(tcp_statistics, field)
3431da177e4SLinus Torvalds #define TCP_ADD_STATS_BH(field, val)	SNMP_ADD_STATS_BH(tcp_statistics, field, val)
3441da177e4SLinus Torvalds #define TCP_ADD_STATS_USER(field, val)	SNMP_ADD_STATS_USER(tcp_statistics, field, val)
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds extern void			tcp_v4_err(struct sk_buff *skb, u32);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds extern void			tcp_shutdown (struct sock *sk, int how);
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds extern int			tcp_v4_rcv(struct sk_buff *skb);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds extern int			tcp_v4_remember_stamp(struct sock *sk);
3531da177e4SLinus Torvalds 
3548feaf0c0SArnaldo Carvalho de Melo extern int		    	tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds extern int			tcp_sendmsg(struct kiocb *iocb, struct sock *sk,
3571da177e4SLinus Torvalds 					    struct msghdr *msg, size_t size);
3581da177e4SLinus Torvalds extern ssize_t			tcp_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags);
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds extern int			tcp_ioctl(struct sock *sk,
3611da177e4SLinus Torvalds 					  int cmd,
3621da177e4SLinus Torvalds 					  unsigned long arg);
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds extern int			tcp_rcv_state_process(struct sock *sk,
3651da177e4SLinus Torvalds 						      struct sk_buff *skb,
3661da177e4SLinus Torvalds 						      struct tcphdr *th,
3671da177e4SLinus Torvalds 						      unsigned len);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds extern int			tcp_rcv_established(struct sock *sk,
3701da177e4SLinus Torvalds 						    struct sk_buff *skb,
3711da177e4SLinus Torvalds 						    struct tcphdr *th,
3721da177e4SLinus Torvalds 						    unsigned len);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds extern void			tcp_rcv_space_adjust(struct sock *sk);
3751da177e4SLinus Torvalds 
376*463c84b9SArnaldo Carvalho de Melo enum inet_csk_ack_state_t {
377*463c84b9SArnaldo Carvalho de Melo 	ICSK_ACK_SCHED	= 1,
378*463c84b9SArnaldo Carvalho de Melo 	ICSK_ACK_TIMER  = 2,
379*463c84b9SArnaldo Carvalho de Melo 	ICSK_ACK_PUSHED = 4
3801da177e4SLinus Torvalds };
3811da177e4SLinus Torvalds 
382*463c84b9SArnaldo Carvalho de Melo static inline void inet_csk_schedule_ack(struct sock *sk)
3831da177e4SLinus Torvalds {
384*463c84b9SArnaldo Carvalho de Melo 	inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_SCHED;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
387*463c84b9SArnaldo Carvalho de Melo static inline int inet_csk_ack_scheduled(const struct sock *sk)
3881da177e4SLinus Torvalds {
389*463c84b9SArnaldo Carvalho de Melo 	return inet_csk(sk)->icsk_ack.pending & ICSK_ACK_SCHED;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
392*463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
393*463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3941da177e4SLinus Torvalds {
395*463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
396fc6415bcSDavid S. Miller 
397*463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
398*463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
399*463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
4001da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
401*463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
402fc6415bcSDavid S. Miller 		} else
403*463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
4041da177e4SLinus Torvalds 	}
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
407*463c84b9SArnaldo Carvalho de Melo extern void tcp_enter_quickack_mode(struct sock *sk);
4081da177e4SLinus Torvalds 
409*463c84b9SArnaldo Carvalho de Melo static inline void inet_csk_delack_init(struct sock *sk)
4101da177e4SLinus Torvalds {
411*463c84b9SArnaldo Carvalho de Melo 	memset(&inet_csk(sk)->icsk_ack, 0, sizeof(inet_csk(sk)->icsk_ack));
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds static inline void tcp_clear_options(struct tcp_options_received *rx_opt)
4151da177e4SLinus Torvalds {
4161da177e4SLinus Torvalds  	rx_opt->tstamp_ok = rx_opt->sack_ok = rx_opt->wscale_ok = rx_opt->snd_wscale = 0;
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds enum tcp_tw_status
4201da177e4SLinus Torvalds {
4211da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
4221da177e4SLinus Torvalds 	TCP_TW_RST = 1,
4231da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
4241da177e4SLinus Torvalds 	TCP_TW_SYN = 3
4251da177e4SLinus Torvalds };
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 
4288feaf0c0SArnaldo Carvalho de Melo extern enum tcp_tw_status	tcp_timewait_state_process(struct inet_timewait_sock *tw,
4291da177e4SLinus Torvalds 							   struct sk_buff *skb,
4308feaf0c0SArnaldo Carvalho de Melo 							   const struct tcphdr *th);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds extern struct sock *		tcp_check_req(struct sock *sk,struct sk_buff *skb,
43360236fddSArnaldo Carvalho de Melo 					      struct request_sock *req,
43460236fddSArnaldo Carvalho de Melo 					      struct request_sock **prev);
4351da177e4SLinus Torvalds extern int			tcp_child_process(struct sock *parent,
4361da177e4SLinus Torvalds 						  struct sock *child,
4371da177e4SLinus Torvalds 						  struct sk_buff *skb);
4381da177e4SLinus Torvalds extern void			tcp_enter_frto(struct sock *sk);
4391da177e4SLinus Torvalds extern void			tcp_enter_loss(struct sock *sk, int how);
4401da177e4SLinus Torvalds extern void			tcp_clear_retrans(struct tcp_sock *tp);
4411da177e4SLinus Torvalds extern void			tcp_update_metrics(struct sock *sk);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds extern void			tcp_close(struct sock *sk,
4441da177e4SLinus Torvalds 					  long timeout);
445*463c84b9SArnaldo Carvalho de Melo extern struct sock *		inet_csk_accept(struct sock *sk, int flags, int *err);
4461da177e4SLinus Torvalds extern unsigned int		tcp_poll(struct file * file, struct socket *sock, struct poll_table_struct *wait);
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds extern int			tcp_getsockopt(struct sock *sk, int level,
4491da177e4SLinus Torvalds 					       int optname,
4501da177e4SLinus Torvalds 					       char __user *optval,
4511da177e4SLinus Torvalds 					       int __user *optlen);
4521da177e4SLinus Torvalds extern int			tcp_setsockopt(struct sock *sk, int level,
4531da177e4SLinus Torvalds 					       int optname, char __user *optval,
4541da177e4SLinus Torvalds 					       int optlen);
4551da177e4SLinus Torvalds extern void			tcp_set_keepalive(struct sock *sk, int val);
4561da177e4SLinus Torvalds extern int			tcp_recvmsg(struct kiocb *iocb, struct sock *sk,
4571da177e4SLinus Torvalds 					    struct msghdr *msg,
4581da177e4SLinus Torvalds 					    size_t len, int nonblock,
4591da177e4SLinus Torvalds 					    int flags, int *addr_len);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds extern int			tcp_listen_start(struct sock *sk);
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds extern void			tcp_parse_options(struct sk_buff *skb,
4641da177e4SLinus Torvalds 						  struct tcp_options_received *opt_rx,
4651da177e4SLinus Torvalds 						  int estab);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds /*
4681da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4691da177e4SLinus Torvalds  */
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds extern void		       	tcp_v4_send_check(struct sock *sk,
4721da177e4SLinus Torvalds 						  struct tcphdr *th, int len,
4731da177e4SLinus Torvalds 						  struct sk_buff *skb);
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds extern int			tcp_v4_conn_request(struct sock *sk,
4761da177e4SLinus Torvalds 						    struct sk_buff *skb);
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds extern struct sock *		tcp_create_openreq_child(struct sock *sk,
47960236fddSArnaldo Carvalho de Melo 							 struct request_sock *req,
4801da177e4SLinus Torvalds 							 struct sk_buff *skb);
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds extern struct sock *		tcp_v4_syn_recv_sock(struct sock *sk,
4831da177e4SLinus Torvalds 						     struct sk_buff *skb,
48460236fddSArnaldo Carvalho de Melo 						     struct request_sock *req,
4851da177e4SLinus Torvalds 							struct dst_entry *dst);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds extern int			tcp_v4_do_rcv(struct sock *sk,
4881da177e4SLinus Torvalds 					      struct sk_buff *skb);
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds extern int			tcp_v4_connect(struct sock *sk,
4911da177e4SLinus Torvalds 					       struct sockaddr *uaddr,
4921da177e4SLinus Torvalds 					       int addr_len);
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds extern int			tcp_connect(struct sock *sk);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds extern struct sk_buff *		tcp_make_synack(struct sock *sk,
4971da177e4SLinus Torvalds 						struct dst_entry *dst,
49860236fddSArnaldo Carvalho de Melo 						struct request_sock *req);
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds extern int			tcp_disconnect(struct sock *sk, int flags);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds extern void			tcp_unhash(struct sock *sk);
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds extern int			tcp_v4_hash_connecting(struct sock *sk);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds /* From syncookies.c */
5081da177e4SLinus Torvalds extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
5091da177e4SLinus Torvalds 				    struct ip_options *opt);
5101da177e4SLinus Torvalds extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb,
5111da177e4SLinus Torvalds 				     __u16 *mss);
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds /* tcp_output.c */
5141da177e4SLinus Torvalds 
515f6302d1dSDavid S. Miller extern void __tcp_push_pending_frames(struct sock *sk, struct tcp_sock *tp,
516a2e2a59cSDavid S. Miller 				      unsigned int cur_mss, int nonagle);
517f6302d1dSDavid S. Miller extern int tcp_may_send_now(struct sock *sk, struct tcp_sock *tp);
5181da177e4SLinus Torvalds extern int tcp_retransmit_skb(struct sock *, struct sk_buff *);
5191da177e4SLinus Torvalds extern void tcp_xmit_retransmit_queue(struct sock *);
5201da177e4SLinus Torvalds extern void tcp_simple_retransmit(struct sock *);
5211da177e4SLinus Torvalds extern int tcp_trim_head(struct sock *, struct sk_buff *, u32);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds extern void tcp_send_probe0(struct sock *);
5241da177e4SLinus Torvalds extern void tcp_send_partial(struct sock *);
5251da177e4SLinus Torvalds extern int  tcp_write_wakeup(struct sock *);
5261da177e4SLinus Torvalds extern void tcp_send_fin(struct sock *sk);
52786a76cafSVictor Fusco extern void tcp_send_active_reset(struct sock *sk,
52886a76cafSVictor Fusco                                   unsigned int __nocast priority);
5291da177e4SLinus Torvalds extern int  tcp_send_synack(struct sock *);
530c1b4a7e6SDavid S. Miller extern void tcp_push_one(struct sock *, unsigned int mss_now);
5311da177e4SLinus Torvalds extern void tcp_send_ack(struct sock *sk);
5321da177e4SLinus Torvalds extern void tcp_send_delayed_ack(struct sock *sk);
5331da177e4SLinus Torvalds 
534a762a980SDavid S. Miller /* tcp_input.c */
535a762a980SDavid S. Miller extern void tcp_cwnd_application_limited(struct sock *sk);
536a762a980SDavid S. Miller 
5371da177e4SLinus Torvalds /* tcp_timer.c */
5381da177e4SLinus Torvalds extern void tcp_init_xmit_timers(struct sock *);
539*463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
540*463c84b9SArnaldo Carvalho de Melo {
541*463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
542*463c84b9SArnaldo Carvalho de Melo }
5431da177e4SLinus Torvalds 
544*463c84b9SArnaldo Carvalho de Melo extern void inet_csk_delete_keepalive_timer(struct sock *sk);
545*463c84b9SArnaldo Carvalho de Melo extern void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long timeout);
5461da177e4SLinus Torvalds extern unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
5471da177e4SLinus Torvalds extern unsigned int tcp_current_mss(struct sock *sk, int large);
5481da177e4SLinus Torvalds 
549*463c84b9SArnaldo Carvalho de Melo #ifdef INET_CSK_DEBUG
550*463c84b9SArnaldo Carvalho de Melo extern const char inet_csk_timer_bug_msg[];
5511da177e4SLinus Torvalds #endif
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds /* tcp_diag.c */
5541da177e4SLinus Torvalds extern void tcp_get_info(struct sock *, struct tcp_info *);
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
5571da177e4SLinus Torvalds typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
5581da177e4SLinus Torvalds 				unsigned int, size_t);
5591da177e4SLinus Torvalds extern int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
5601da177e4SLinus Torvalds 			 sk_read_actor_t recv_actor);
5611da177e4SLinus Torvalds 
562*463c84b9SArnaldo Carvalho de Melo static inline void inet_csk_clear_xmit_timer(struct sock *sk, const int what)
5631da177e4SLinus Torvalds {
564*463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
5651da177e4SLinus Torvalds 
566*463c84b9SArnaldo Carvalho de Melo 	if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0) {
567*463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_pending = 0;
568*463c84b9SArnaldo Carvalho de Melo #ifdef INET_CSK_CLEAR_TIMERS
569*463c84b9SArnaldo Carvalho de Melo 		sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
5701da177e4SLinus Torvalds #endif
571*463c84b9SArnaldo Carvalho de Melo 	} else if (what == ICSK_TIME_DACK) {
572*463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.blocked = icsk->icsk_ack.pending = 0;
573*463c84b9SArnaldo Carvalho de Melo #ifdef INET_CSK_CLEAR_TIMERS
574*463c84b9SArnaldo Carvalho de Melo 		sk_stop_timer(sk, &icsk->icsk_delack_timer);
5751da177e4SLinus Torvalds #endif
576*463c84b9SArnaldo Carvalho de Melo 	}
577*463c84b9SArnaldo Carvalho de Melo #ifdef INET_CSK_DEBUG
578*463c84b9SArnaldo Carvalho de Melo 	else {
579*463c84b9SArnaldo Carvalho de Melo 		pr_debug(inet_csk_timer_bug_msg);
580*463c84b9SArnaldo Carvalho de Melo 	}
5811da177e4SLinus Torvalds #endif
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds /*
5851da177e4SLinus Torvalds  *	Reset the retransmission timer
5861da177e4SLinus Torvalds  */
587*463c84b9SArnaldo Carvalho de Melo static inline void inet_csk_reset_xmit_timer(struct sock *sk, const int what,
588*463c84b9SArnaldo Carvalho de Melo 					     unsigned long when)
5891da177e4SLinus Torvalds {
590*463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	if (when > TCP_RTO_MAX) {
593*463c84b9SArnaldo Carvalho de Melo #ifdef INET_CSK_DEBUG
594*463c84b9SArnaldo Carvalho de Melo 		pr_debug("reset_xmit_timer: sk=%p %d when=0x%lx, caller=%p\n",
595*463c84b9SArnaldo Carvalho de Melo 			 sk, what, when, current_text_addr());
5961da177e4SLinus Torvalds #endif
5971da177e4SLinus Torvalds 		when = TCP_RTO_MAX;
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 
600*463c84b9SArnaldo Carvalho de Melo 	if (what == ICSK_TIME_RETRANS || what == ICSK_TIME_PROBE0) {
601*463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_pending = what;
602*463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_timeout = jiffies + when;
603*463c84b9SArnaldo Carvalho de Melo 		sk_reset_timer(sk, &icsk->icsk_retransmit_timer, icsk->icsk_timeout);
604*463c84b9SArnaldo Carvalho de Melo 	} else if (what == ICSK_TIME_DACK) {
605*463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.pending |= ICSK_ACK_TIMER;
606*463c84b9SArnaldo Carvalho de Melo 		icsk->icsk_ack.timeout = jiffies + when;
607*463c84b9SArnaldo Carvalho de Melo 		sk_reset_timer(sk, &icsk->icsk_delack_timer, icsk->icsk_ack.timeout);
608*463c84b9SArnaldo Carvalho de Melo 	}
609*463c84b9SArnaldo Carvalho de Melo #ifdef INET_CSK_DEBUG
610*463c84b9SArnaldo Carvalho de Melo 	else {
611*463c84b9SArnaldo Carvalho de Melo 		pr_debug(inet_csk_timer_bug_msg);
612*463c84b9SArnaldo Carvalho de Melo 	}
6131da177e4SLinus Torvalds #endif
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds /* Initialize RCV_MSS value.
6171da177e4SLinus Torvalds  * RCV_MSS is an our guess about MSS used by the peer.
6181da177e4SLinus Torvalds  * We haven't any direct information about the MSS.
6191da177e4SLinus Torvalds  * It's better to underestimate the RCV_MSS rather than overestimate.
6201da177e4SLinus Torvalds  * Overestimations make us ACKing less frequently than needed.
6211da177e4SLinus Torvalds  * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
6221da177e4SLinus Torvalds  */
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds static inline void tcp_initialize_rcv_mss(struct sock *sk)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
627c1b4a7e6SDavid S. Miller 	unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	hint = min(hint, tp->rcv_wnd/2);
6301da177e4SLinus Torvalds 	hint = min(hint, TCP_MIN_RCVMSS);
6311da177e4SLinus Torvalds 	hint = max(hint, TCP_MIN_MSS);
6321da177e4SLinus Torvalds 
633*463c84b9SArnaldo Carvalho de Melo 	inet_csk(sk)->icsk_ack.rcv_mss = hint;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds static __inline__ void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
6391da177e4SLinus Torvalds 			       ntohl(TCP_FLAG_ACK) |
6401da177e4SLinus Torvalds 			       snd_wnd);
6411da177e4SLinus Torvalds }
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds static __inline__ void tcp_fast_path_on(struct tcp_sock *tp)
6441da177e4SLinus Torvalds {
6451da177e4SLinus Torvalds 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds static inline void tcp_fast_path_check(struct sock *sk, struct tcp_sock *tp)
6491da177e4SLinus Torvalds {
650b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue) &&
6511da177e4SLinus Torvalds 	    tp->rcv_wnd &&
6521da177e4SLinus Torvalds 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
6531da177e4SLinus Torvalds 	    !tp->urg_data)
6541da177e4SLinus Torvalds 		tcp_fast_path_on(tp);
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
6581da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
6591da177e4SLinus Torvalds  * than the offered window.
6601da177e4SLinus Torvalds  */
6611da177e4SLinus Torvalds static __inline__ u32 tcp_receive_window(const struct tcp_sock *tp)
6621da177e4SLinus Torvalds {
6631da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	if (win < 0)
6661da177e4SLinus Torvalds 		win = 0;
6671da177e4SLinus Torvalds 	return (u32) win;
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
6711da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
6721da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
6731da177e4SLinus Torvalds  */
6741da177e4SLinus Torvalds extern u32	__tcp_select_window(struct sock *sk);
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds /* TCP timestamps are only 32-bits, this causes a slight
6771da177e4SLinus Torvalds  * complication on 64-bit systems since we store a snapshot
6781da177e4SLinus Torvalds  * of jiffies in the buffer control blocks below.  We decidely
6791da177e4SLinus Torvalds  * only use of the low 32-bits of jiffies and hide the ugly
6801da177e4SLinus Torvalds  * casts with the following macro.
6811da177e4SLinus Torvalds  */
6821da177e4SLinus Torvalds #define tcp_time_stamp		((__u32)(jiffies))
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds /* This is what the send packet queueing engine uses to pass
6851da177e4SLinus Torvalds  * TCP per-packet control information to the transmission
6861da177e4SLinus Torvalds  * code.  We also store the host-order sequence numbers in
6871da177e4SLinus Torvalds  * here too.  This is 36 bytes on 32-bit architectures,
6881da177e4SLinus Torvalds  * 40 bytes on 64-bit machines, if this grows please adjust
6891da177e4SLinus Torvalds  * skbuff.h:skbuff->cb[xxx] size appropriately.
6901da177e4SLinus Torvalds  */
6911da177e4SLinus Torvalds struct tcp_skb_cb {
6921da177e4SLinus Torvalds 	union {
6931da177e4SLinus Torvalds 		struct inet_skb_parm	h4;
6941da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
6951da177e4SLinus Torvalds 		struct inet6_skb_parm	h6;
6961da177e4SLinus Torvalds #endif
6971da177e4SLinus Torvalds 	} header;	/* For incoming frames		*/
6981da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
6991da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
7001da177e4SLinus Torvalds 	__u32		when;		/* used to compute rtt's	*/
7011da177e4SLinus Torvalds 	__u8		flags;		/* TCP header flags.		*/
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	/* NOTE: These must match up to the flags byte in a
7041da177e4SLinus Torvalds 	 *       real TCP header.
7051da177e4SLinus Torvalds 	 */
7061da177e4SLinus Torvalds #define TCPCB_FLAG_FIN		0x01
7071da177e4SLinus Torvalds #define TCPCB_FLAG_SYN		0x02
7081da177e4SLinus Torvalds #define TCPCB_FLAG_RST		0x04
7091da177e4SLinus Torvalds #define TCPCB_FLAG_PSH		0x08
7101da177e4SLinus Torvalds #define TCPCB_FLAG_ACK		0x10
7111da177e4SLinus Torvalds #define TCPCB_FLAG_URG		0x20
7121da177e4SLinus Torvalds #define TCPCB_FLAG_ECE		0x40
7131da177e4SLinus Torvalds #define TCPCB_FLAG_CWR		0x80
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	__u8		sacked;		/* State flags for SACK/FACK.	*/
7161da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
7171da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
7181da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
7191da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
7221da177e4SLinus Torvalds #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds #define TCPCB_URG		0x20	/* Urgent pointer advenced here	*/
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds #define TCPCB_AT_TAIL		(TCPCB_URG)
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	__u16		urg_ptr;	/* Valid w/URG flags is set.	*/
7291da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
7301da177e4SLinus Torvalds };
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds #include <net/tcp_ecn.h>
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
7371da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
7381da177e4SLinus Torvalds  */
7391da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
7401da177e4SLinus Torvalds {
7411da177e4SLinus Torvalds 	return skb_shinfo(skb)->tso_segs;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds /* This is valid iff tcp_skb_pcount() > 1. */
7451da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	return skb_shinfo(skb)->tso_size;
7481da177e4SLinus Torvalds }
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds static inline void tcp_dec_pcount_approx(__u32 *count,
7511da177e4SLinus Torvalds 					 const struct sk_buff *skb)
7521da177e4SLinus Torvalds {
7531da177e4SLinus Torvalds 	if (*count) {
7541da177e4SLinus Torvalds 		*count -= tcp_skb_pcount(skb);
7551da177e4SLinus Torvalds 		if ((int)*count < 0)
7561da177e4SLinus Torvalds 			*count = 0;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds }
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds static inline void tcp_packets_out_inc(struct sock *sk,
7611da177e4SLinus Torvalds 				       struct tcp_sock *tp,
7621da177e4SLinus Torvalds 				       const struct sk_buff *skb)
7631da177e4SLinus Torvalds {
7641da177e4SLinus Torvalds 	int orig = tp->packets_out;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	tp->packets_out += tcp_skb_pcount(skb);
7671da177e4SLinus Torvalds 	if (!orig)
768*463c84b9SArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, inet_csk(sk)->icsk_rto);
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds static inline void tcp_packets_out_dec(struct tcp_sock *tp,
7721da177e4SLinus Torvalds 				       const struct sk_buff *skb)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	tp->packets_out -= tcp_skb_pcount(skb);
7751da177e4SLinus Torvalds }
7761da177e4SLinus Torvalds 
777317a76f9SStephen Hemminger /* Events passed to congestion control interface */
778317a76f9SStephen Hemminger enum tcp_ca_event {
779317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
780317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
781317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
782317a76f9SStephen Hemminger 	CA_EVENT_FRTO,		/* fast recovery timeout */
783317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
784317a76f9SStephen Hemminger 	CA_EVENT_FAST_ACK,	/* in sequence ack */
785317a76f9SStephen Hemminger 	CA_EVENT_SLOW_ACK,	/* other ack */
786317a76f9SStephen Hemminger };
787317a76f9SStephen Hemminger 
788317a76f9SStephen Hemminger /*
789317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
790317a76f9SStephen Hemminger  */
791317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
792317a76f9SStephen Hemminger struct tcp_congestion_ops {
793317a76f9SStephen Hemminger 	struct list_head	list;
794317a76f9SStephen Hemminger 
795317a76f9SStephen Hemminger 	/* initialize private data (optional) */
796317a76f9SStephen Hemminger 	void (*init)(struct tcp_sock *tp);
797317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
798317a76f9SStephen Hemminger 	void (*release)(struct tcp_sock *tp);
799317a76f9SStephen Hemminger 
800317a76f9SStephen Hemminger 	/* return slow start threshold (required) */
801317a76f9SStephen Hemminger 	u32 (*ssthresh)(struct tcp_sock *tp);
802317a76f9SStephen Hemminger 	/* lower bound for congestion window (optional) */
803317a76f9SStephen Hemminger 	u32 (*min_cwnd)(struct tcp_sock *tp);
804317a76f9SStephen Hemminger 	/* do new cwnd calculation (required) */
805317a76f9SStephen Hemminger 	void (*cong_avoid)(struct tcp_sock *tp, u32 ack,
806317a76f9SStephen Hemminger 			   u32 rtt, u32 in_flight, int good_ack);
807317a76f9SStephen Hemminger 	/* round trip time sample per acked packet (optional) */
808317a76f9SStephen Hemminger 	void (*rtt_sample)(struct tcp_sock *tp, u32 usrtt);
809317a76f9SStephen Hemminger 	/* call before changing ca_state (optional) */
810317a76f9SStephen Hemminger 	void (*set_state)(struct tcp_sock *tp, u8 new_state);
811317a76f9SStephen Hemminger 	/* call when cwnd event occurs (optional) */
812317a76f9SStephen Hemminger 	void (*cwnd_event)(struct tcp_sock *tp, enum tcp_ca_event ev);
813317a76f9SStephen Hemminger 	/* new value of cwnd after loss (optional) */
814317a76f9SStephen Hemminger 	u32  (*undo_cwnd)(struct tcp_sock *tp);
815317a76f9SStephen Hemminger 	/* hook for packet ack accounting (optional) */
816317a76f9SStephen Hemminger 	void (*pkts_acked)(struct tcp_sock *tp, u32 num_acked);
817317a76f9SStephen Hemminger 	/* get info for tcp_diag (optional) */
818317a76f9SStephen Hemminger 	void (*get_info)(struct tcp_sock *tp, u32 ext, struct sk_buff *skb);
819317a76f9SStephen Hemminger 
820317a76f9SStephen Hemminger 	char 		name[TCP_CA_NAME_MAX];
821317a76f9SStephen Hemminger 	struct module 	*owner;
822317a76f9SStephen Hemminger };
823317a76f9SStephen Hemminger 
824317a76f9SStephen Hemminger extern int tcp_register_congestion_control(struct tcp_congestion_ops *type);
825317a76f9SStephen Hemminger extern void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
826317a76f9SStephen Hemminger 
827317a76f9SStephen Hemminger extern void tcp_init_congestion_control(struct tcp_sock *tp);
828317a76f9SStephen Hemminger extern void tcp_cleanup_congestion_control(struct tcp_sock *tp);
829317a76f9SStephen Hemminger extern int tcp_set_default_congestion_control(const char *name);
830317a76f9SStephen Hemminger extern void tcp_get_default_congestion_control(char *name);
8315f8ef48dSStephen Hemminger extern int tcp_set_congestion_control(struct tcp_sock *tp, const char *name);
832317a76f9SStephen Hemminger 
8335f8ef48dSStephen Hemminger extern struct tcp_congestion_ops tcp_init_congestion_ops;
834317a76f9SStephen Hemminger extern u32 tcp_reno_ssthresh(struct tcp_sock *tp);
835317a76f9SStephen Hemminger extern void tcp_reno_cong_avoid(struct tcp_sock *tp, u32 ack,
836317a76f9SStephen Hemminger 				u32 rtt, u32 in_flight, int flag);
837317a76f9SStephen Hemminger extern u32 tcp_reno_min_cwnd(struct tcp_sock *tp);
838a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
839317a76f9SStephen Hemminger 
840317a76f9SStephen Hemminger static inline void tcp_set_ca_state(struct tcp_sock *tp, u8 ca_state)
841317a76f9SStephen Hemminger {
842317a76f9SStephen Hemminger 	if (tp->ca_ops->set_state)
843317a76f9SStephen Hemminger 		tp->ca_ops->set_state(tp, ca_state);
844317a76f9SStephen Hemminger 	tp->ca_state = ca_state;
845317a76f9SStephen Hemminger }
846317a76f9SStephen Hemminger 
847317a76f9SStephen Hemminger static inline void tcp_ca_event(struct tcp_sock *tp, enum tcp_ca_event event)
848317a76f9SStephen Hemminger {
849317a76f9SStephen Hemminger 	if (tp->ca_ops->cwnd_event)
850317a76f9SStephen Hemminger 		tp->ca_ops->cwnd_event(tp, event);
851317a76f9SStephen Hemminger }
852317a76f9SStephen Hemminger 
8531da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
8541da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
8551da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
8561da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
8571da177e4SLinus Torvalds  *
8581da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
8591da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
8601da177e4SLinus Torvalds  *
8611da177e4SLinus Torvalds  * Read this equation as:
8621da177e4SLinus Torvalds  *
8631da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
8641da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
8651da177e4SLinus Torvalds  *	"Packets fast retransmitted"
8661da177e4SLinus Torvalds  */
8671da177e4SLinus Torvalds static __inline__ unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
8681da177e4SLinus Torvalds {
8691da177e4SLinus Torvalds 	return (tp->packets_out - tp->left_out + tp->retrans_out);
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
8731da177e4SLinus Torvalds  * The exception is rate halving phase, when cwnd is decreasing towards
8741da177e4SLinus Torvalds  * ssthresh.
8751da177e4SLinus Torvalds  */
8761da177e4SLinus Torvalds static inline __u32 tcp_current_ssthresh(struct tcp_sock *tp)
8771da177e4SLinus Torvalds {
8781da177e4SLinus Torvalds 	if ((1<<tp->ca_state)&(TCPF_CA_CWR|TCPF_CA_Recovery))
8791da177e4SLinus Torvalds 		return tp->snd_ssthresh;
8801da177e4SLinus Torvalds 	else
8811da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
8821da177e4SLinus Torvalds 			   ((tp->snd_cwnd >> 1) +
8831da177e4SLinus Torvalds 			    (tp->snd_cwnd >> 2)));
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds static inline void tcp_sync_left_out(struct tcp_sock *tp)
8871da177e4SLinus Torvalds {
8881da177e4SLinus Torvalds 	if (tp->rx_opt.sack_ok &&
8891da177e4SLinus Torvalds 	    (tp->sacked_out >= tp->packets_out - tp->lost_out))
8901da177e4SLinus Torvalds 		tp->sacked_out = tp->packets_out - tp->lost_out;
8911da177e4SLinus Torvalds 	tp->left_out = tp->sacked_out + tp->lost_out;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
894d1b04c08SBaruch Even /* Set slow start threshold and cwnd not falling to slow start */
8951da177e4SLinus Torvalds static inline void __tcp_enter_cwr(struct tcp_sock *tp)
8961da177e4SLinus Torvalds {
8971da177e4SLinus Torvalds 	tp->undo_marker = 0;
898317a76f9SStephen Hemminger 	tp->snd_ssthresh = tp->ca_ops->ssthresh(tp);
8991da177e4SLinus Torvalds 	tp->snd_cwnd = min(tp->snd_cwnd,
9001da177e4SLinus Torvalds 			   tcp_packets_in_flight(tp) + 1U);
9011da177e4SLinus Torvalds 	tp->snd_cwnd_cnt = 0;
9021da177e4SLinus Torvalds 	tp->high_seq = tp->snd_nxt;
9031da177e4SLinus Torvalds 	tp->snd_cwnd_stamp = tcp_time_stamp;
9041da177e4SLinus Torvalds 	TCP_ECN_queue_cwr(tp);
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds static inline void tcp_enter_cwr(struct tcp_sock *tp)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	tp->prior_ssthresh = 0;
9101da177e4SLinus Torvalds 	if (tp->ca_state < TCP_CA_CWR) {
9111da177e4SLinus Torvalds 		__tcp_enter_cwr(tp);
9121da177e4SLinus Torvalds 		tcp_set_ca_state(tp, TCP_CA_CWR);
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds 
9161da177e4SLinus Torvalds extern __u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst);
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds /* Slow start with delack produces 3 packets of burst, so that
9191da177e4SLinus Torvalds  * it is safe "de facto".
9201da177e4SLinus Torvalds  */
9211da177e4SLinus Torvalds static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds 	return 3;
9241da177e4SLinus Torvalds }
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds static __inline__ void tcp_minshall_update(struct tcp_sock *tp, int mss,
9271da177e4SLinus Torvalds 					   const struct sk_buff *skb)
9281da177e4SLinus Torvalds {
9291da177e4SLinus Torvalds 	if (skb->len < mss)
9301da177e4SLinus Torvalds 		tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
9311da177e4SLinus Torvalds }
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds static __inline__ void tcp_check_probe_timer(struct sock *sk, struct tcp_sock *tp)
9341da177e4SLinus Torvalds {
935*463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
936*463c84b9SArnaldo Carvalho de Melo 	if (!tp->packets_out && !icsk->icsk_pending)
937*463c84b9SArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0, icsk->icsk_rto);
9381da177e4SLinus Torvalds }
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds static __inline__ void tcp_push_pending_frames(struct sock *sk,
9411da177e4SLinus Torvalds 					       struct tcp_sock *tp)
9421da177e4SLinus Torvalds {
9431da177e4SLinus Torvalds 	__tcp_push_pending_frames(sk, tp, tcp_current_mss(sk, 1), tp->nonagle);
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds static __inline__ void tcp_init_wl(struct tcp_sock *tp, u32 ack, u32 seq)
9471da177e4SLinus Torvalds {
9481da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds static __inline__ void tcp_update_wl(struct tcp_sock *tp, u32 ack, u32 seq)
9521da177e4SLinus Torvalds {
9531da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds extern void tcp_destroy_sock(struct sock *sk);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds /*
9601da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
9611da177e4SLinus Torvalds  */
9621da177e4SLinus Torvalds static __inline__ u16 tcp_v4_check(struct tcphdr *th, int len,
9631da177e4SLinus Torvalds 				   unsigned long saddr, unsigned long daddr,
9641da177e4SLinus Torvalds 				   unsigned long base)
9651da177e4SLinus Torvalds {
9661da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds static __inline__ int __tcp_checksum_complete(struct sk_buff *skb)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	return (unsigned short)csum_fold(skb_checksum(skb, 0, skb->len, skb->csum));
9721da177e4SLinus Torvalds }
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds static __inline__ int tcp_checksum_complete(struct sk_buff *skb)
9751da177e4SLinus Torvalds {
9761da177e4SLinus Torvalds 	return skb->ip_summed != CHECKSUM_UNNECESSARY &&
9771da177e4SLinus Torvalds 		__tcp_checksum_complete(skb);
9781da177e4SLinus Torvalds }
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds /* Prequeue for VJ style copy to user, combined with checksumming. */
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds static __inline__ void tcp_prequeue_init(struct tcp_sock *tp)
9831da177e4SLinus Torvalds {
9841da177e4SLinus Torvalds 	tp->ucopy.task = NULL;
9851da177e4SLinus Torvalds 	tp->ucopy.len = 0;
9861da177e4SLinus Torvalds 	tp->ucopy.memory = 0;
9871da177e4SLinus Torvalds 	skb_queue_head_init(&tp->ucopy.prequeue);
9881da177e4SLinus Torvalds }
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds /* Packet is added to VJ-style prequeue for processing in process
9911da177e4SLinus Torvalds  * context, if a reader task is waiting. Apparently, this exciting
9921da177e4SLinus Torvalds  * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93)
9931da177e4SLinus Torvalds  * failed somewhere. Latency? Burstiness? Well, at least now we will
9941da177e4SLinus Torvalds  * see, why it failed. 8)8)				  --ANK
9951da177e4SLinus Torvalds  *
9961da177e4SLinus Torvalds  * NOTE: is this not too big to inline?
9971da177e4SLinus Torvalds  */
9981da177e4SLinus Torvalds static __inline__ int tcp_prequeue(struct sock *sk, struct sk_buff *skb)
9991da177e4SLinus Torvalds {
10001da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	if (!sysctl_tcp_low_latency && tp->ucopy.task) {
10031da177e4SLinus Torvalds 		__skb_queue_tail(&tp->ucopy.prequeue, skb);
10041da177e4SLinus Torvalds 		tp->ucopy.memory += skb->truesize;
10051da177e4SLinus Torvalds 		if (tp->ucopy.memory > sk->sk_rcvbuf) {
10061da177e4SLinus Torvalds 			struct sk_buff *skb1;
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 			BUG_ON(sock_owned_by_user(sk));
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 			while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) {
10111da177e4SLinus Torvalds 				sk->sk_backlog_rcv(sk, skb1);
10121da177e4SLinus Torvalds 				NET_INC_STATS_BH(LINUX_MIB_TCPPREQUEUEDROPPED);
10131da177e4SLinus Torvalds 			}
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 			tp->ucopy.memory = 0;
10161da177e4SLinus Torvalds 		} else if (skb_queue_len(&tp->ucopy.prequeue) == 1) {
10171da177e4SLinus Torvalds 			wake_up_interruptible(sk->sk_sleep);
1018*463c84b9SArnaldo Carvalho de Melo 			if (!inet_csk_ack_scheduled(sk))
1019*463c84b9SArnaldo Carvalho de Melo 				inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
1020*463c84b9SArnaldo Carvalho de Melo 						          (3 * TCP_RTO_MIN) / 4);
10211da177e4SLinus Torvalds 		}
10221da177e4SLinus Torvalds 		return 1;
10231da177e4SLinus Torvalds 	}
10241da177e4SLinus Torvalds 	return 0;
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds #undef STATE_TRACE
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds #ifdef STATE_TRACE
10311da177e4SLinus Torvalds static const char *statename[]={
10321da177e4SLinus Torvalds 	"Unused","Established","Syn Sent","Syn Recv",
10331da177e4SLinus Torvalds 	"Fin Wait 1","Fin Wait 2","Time Wait", "Close",
10341da177e4SLinus Torvalds 	"Close Wait","Last ACK","Listen","Closing"
10351da177e4SLinus Torvalds };
10361da177e4SLinus Torvalds #endif
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds static __inline__ void tcp_set_state(struct sock *sk, int state)
10391da177e4SLinus Torvalds {
10401da177e4SLinus Torvalds 	int oldstate = sk->sk_state;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	switch (state) {
10431da177e4SLinus Torvalds 	case TCP_ESTABLISHED:
10441da177e4SLinus Torvalds 		if (oldstate != TCP_ESTABLISHED)
10451da177e4SLinus Torvalds 			TCP_INC_STATS(TCP_MIB_CURRESTAB);
10461da177e4SLinus Torvalds 		break;
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	case TCP_CLOSE:
10491da177e4SLinus Torvalds 		if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED)
10501da177e4SLinus Torvalds 			TCP_INC_STATS(TCP_MIB_ESTABRESETS);
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
1053*463c84b9SArnaldo Carvalho de Melo 		if (inet_csk(sk)->icsk_bind_hash &&
10541da177e4SLinus Torvalds 		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
10552d8c4ce5SArnaldo Carvalho de Melo 			inet_put_port(&tcp_hashinfo, sk);
10561da177e4SLinus Torvalds 		/* fall through */
10571da177e4SLinus Torvalds 	default:
10581da177e4SLinus Torvalds 		if (oldstate==TCP_ESTABLISHED)
10591da177e4SLinus Torvalds 			TCP_DEC_STATS(TCP_MIB_CURRESTAB);
10601da177e4SLinus Torvalds 	}
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	/* Change state AFTER socket is unhashed to avoid closed
10631da177e4SLinus Torvalds 	 * socket sitting in hash tables.
10641da177e4SLinus Torvalds 	 */
10651da177e4SLinus Torvalds 	sk->sk_state = state;
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds #ifdef STATE_TRACE
10681da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n",sk, statename[oldstate],statename[state]);
10691da177e4SLinus Torvalds #endif
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds static __inline__ void tcp_done(struct sock *sk)
10731da177e4SLinus Torvalds {
10741da177e4SLinus Torvalds 	tcp_set_state(sk, TCP_CLOSE);
10751da177e4SLinus Torvalds 	tcp_clear_xmit_timers(sk);
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	sk->sk_shutdown = SHUTDOWN_MASK;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD))
10801da177e4SLinus Torvalds 		sk->sk_state_change(sk);
10811da177e4SLinus Torvalds 	else
10821da177e4SLinus Torvalds 		tcp_destroy_sock(sk);
10831da177e4SLinus Torvalds }
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds static __inline__ void tcp_sack_reset(struct tcp_options_received *rx_opt)
10861da177e4SLinus Torvalds {
10871da177e4SLinus Torvalds 	rx_opt->dsack = 0;
10881da177e4SLinus Torvalds 	rx_opt->eff_sacks = 0;
10891da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
10901da177e4SLinus Torvalds }
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds static __inline__ void tcp_build_and_update_options(__u32 *ptr, struct tcp_sock *tp, __u32 tstamp)
10931da177e4SLinus Torvalds {
10941da177e4SLinus Torvalds 	if (tp->rx_opt.tstamp_ok) {
10951da177e4SLinus Torvalds 		*ptr++ = __constant_htonl((TCPOPT_NOP << 24) |
10961da177e4SLinus Torvalds 					  (TCPOPT_NOP << 16) |
10971da177e4SLinus Torvalds 					  (TCPOPT_TIMESTAMP << 8) |
10981da177e4SLinus Torvalds 					  TCPOLEN_TIMESTAMP);
10991da177e4SLinus Torvalds 		*ptr++ = htonl(tstamp);
11001da177e4SLinus Torvalds 		*ptr++ = htonl(tp->rx_opt.ts_recent);
11011da177e4SLinus Torvalds 	}
11021da177e4SLinus Torvalds 	if (tp->rx_opt.eff_sacks) {
11031da177e4SLinus Torvalds 		struct tcp_sack_block *sp = tp->rx_opt.dsack ? tp->duplicate_sack : tp->selective_acks;
11041da177e4SLinus Torvalds 		int this_sack;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 		*ptr++ = __constant_htonl((TCPOPT_NOP << 24) |
11071da177e4SLinus Torvalds 					  (TCPOPT_NOP << 16) |
11081da177e4SLinus Torvalds 					  (TCPOPT_SACK << 8) |
11091da177e4SLinus Torvalds 					  (TCPOLEN_SACK_BASE +
11101da177e4SLinus Torvalds 					   (tp->rx_opt.eff_sacks * TCPOLEN_SACK_PERBLOCK)));
11111da177e4SLinus Torvalds 		for(this_sack = 0; this_sack < tp->rx_opt.eff_sacks; this_sack++) {
11121da177e4SLinus Torvalds 			*ptr++ = htonl(sp[this_sack].start_seq);
11131da177e4SLinus Torvalds 			*ptr++ = htonl(sp[this_sack].end_seq);
11141da177e4SLinus Torvalds 		}
11151da177e4SLinus Torvalds 		if (tp->rx_opt.dsack) {
11161da177e4SLinus Torvalds 			tp->rx_opt.dsack = 0;
11171da177e4SLinus Torvalds 			tp->rx_opt.eff_sacks--;
11181da177e4SLinus Torvalds 		}
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds }
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds /* Construct a tcp options header for a SYN or SYN_ACK packet.
11231da177e4SLinus Torvalds  * If this is every changed make sure to change the definition of
11241da177e4SLinus Torvalds  * MAX_SYN_SIZE to match the new maximum number of options that you
11251da177e4SLinus Torvalds  * can generate.
11261da177e4SLinus Torvalds  */
11271da177e4SLinus Torvalds static inline void tcp_syn_build_options(__u32 *ptr, int mss, int ts, int sack,
11281da177e4SLinus Torvalds 					     int offer_wscale, int wscale, __u32 tstamp, __u32 ts_recent)
11291da177e4SLinus Torvalds {
11301da177e4SLinus Torvalds 	/* We always get an MSS option.
11311da177e4SLinus Torvalds 	 * The option bytes which will be seen in normal data
11321da177e4SLinus Torvalds 	 * packets should timestamps be used, must be in the MSS
11331da177e4SLinus Torvalds 	 * advertised.  But we subtract them from tp->mss_cache so
11341da177e4SLinus Torvalds 	 * that calculations in tcp_sendmsg are simpler etc.
11351da177e4SLinus Torvalds 	 * So account for this fact here if necessary.  If we
11361da177e4SLinus Torvalds 	 * don't do this correctly, as a receiver we won't
11371da177e4SLinus Torvalds 	 * recognize data packets as being full sized when we
11381da177e4SLinus Torvalds 	 * should, and thus we won't abide by the delayed ACK
11391da177e4SLinus Torvalds 	 * rules correctly.
11401da177e4SLinus Torvalds 	 * SACKs don't matter, we never delay an ACK when we
11411da177e4SLinus Torvalds 	 * have any of those going out.
11421da177e4SLinus Torvalds 	 */
11431da177e4SLinus Torvalds 	*ptr++ = htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) | mss);
11441da177e4SLinus Torvalds 	if (ts) {
11451da177e4SLinus Torvalds 		if(sack)
11461da177e4SLinus Torvalds 			*ptr++ = __constant_htonl((TCPOPT_SACK_PERM << 24) | (TCPOLEN_SACK_PERM << 16) |
11471da177e4SLinus Torvalds 						  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
11481da177e4SLinus Torvalds 		else
11491da177e4SLinus Torvalds 			*ptr++ = __constant_htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
11501da177e4SLinus Torvalds 						  (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
11511da177e4SLinus Torvalds 		*ptr++ = htonl(tstamp);		/* TSVAL */
11521da177e4SLinus Torvalds 		*ptr++ = htonl(ts_recent);	/* TSECR */
11531da177e4SLinus Torvalds 	} else if(sack)
11541da177e4SLinus Torvalds 		*ptr++ = __constant_htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
11551da177e4SLinus Torvalds 					  (TCPOPT_SACK_PERM << 8) | TCPOLEN_SACK_PERM);
11561da177e4SLinus Torvalds 	if (offer_wscale)
11571da177e4SLinus Torvalds 		*ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_WINDOW << 16) | (TCPOLEN_WINDOW << 8) | (wscale));
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
11611da177e4SLinus Torvalds extern void tcp_select_initial_window(int __space, __u32 mss,
11621da177e4SLinus Torvalds 				      __u32 *rcv_wnd, __u32 *window_clamp,
11631da177e4SLinus Torvalds 				      int wscale_ok, __u8 *rcv_wscale);
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds static inline int tcp_win_from_space(int space)
11661da177e4SLinus Torvalds {
11671da177e4SLinus Torvalds 	return sysctl_tcp_adv_win_scale<=0 ?
11681da177e4SLinus Torvalds 		(space>>(-sysctl_tcp_adv_win_scale)) :
11691da177e4SLinus Torvalds 		space - (space>>sysctl_tcp_adv_win_scale);
11701da177e4SLinus Torvalds }
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
11731da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
11741da177e4SLinus Torvalds {
11751da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf -
11761da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
11771da177e4SLinus Torvalds }
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
11801da177e4SLinus Torvalds {
11811da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf);
11821da177e4SLinus Torvalds }
11831da177e4SLinus Torvalds 
1184*463c84b9SArnaldo Carvalho de Melo static inline void inet_csk_reqsk_queue_add(struct sock *sk,
1185*463c84b9SArnaldo Carvalho de Melo 					    struct request_sock *req,
11861da177e4SLinus Torvalds 					    struct sock *child)
11871da177e4SLinus Torvalds {
1188*463c84b9SArnaldo Carvalho de Melo 	reqsk_queue_add(&inet_csk(sk)->icsk_accept_queue, req, sk, child);
11891da177e4SLinus Torvalds }
11901da177e4SLinus Torvalds 
1191*463c84b9SArnaldo Carvalho de Melo static inline void inet_csk_reqsk_queue_removed(struct sock *sk,
1192*463c84b9SArnaldo Carvalho de Melo 						struct request_sock *req)
11931da177e4SLinus Torvalds {
1194*463c84b9SArnaldo Carvalho de Melo 	if (reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req) == 0)
1195*463c84b9SArnaldo Carvalho de Melo 		inet_csk_delete_keepalive_timer(sk);
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
1198*463c84b9SArnaldo Carvalho de Melo static inline void inet_csk_reqsk_queue_added(struct sock *sk,
1199*463c84b9SArnaldo Carvalho de Melo 					      const unsigned long timeout)
12001da177e4SLinus Torvalds {
1201*463c84b9SArnaldo Carvalho de Melo 	if (reqsk_queue_added(&inet_csk(sk)->icsk_accept_queue) == 0)
1202*463c84b9SArnaldo Carvalho de Melo 		inet_csk_reset_keepalive_timer(sk, timeout);
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds 
1205*463c84b9SArnaldo Carvalho de Melo static inline int inet_csk_reqsk_queue_len(const struct sock *sk)
12061da177e4SLinus Torvalds {
1207*463c84b9SArnaldo Carvalho de Melo 	return reqsk_queue_len(&inet_csk(sk)->icsk_accept_queue);
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
1210*463c84b9SArnaldo Carvalho de Melo static inline int inet_csk_reqsk_queue_young(const struct sock *sk)
12111da177e4SLinus Torvalds {
1212*463c84b9SArnaldo Carvalho de Melo 	return reqsk_queue_len_young(&inet_csk(sk)->icsk_accept_queue);
12131da177e4SLinus Torvalds }
12141da177e4SLinus Torvalds 
1215*463c84b9SArnaldo Carvalho de Melo static inline int inet_csk_reqsk_queue_is_full(const struct sock *sk)
12161da177e4SLinus Torvalds {
1217*463c84b9SArnaldo Carvalho de Melo 	return reqsk_queue_is_full(&inet_csk(sk)->icsk_accept_queue);
12181da177e4SLinus Torvalds }
12191da177e4SLinus Torvalds 
1220*463c84b9SArnaldo Carvalho de Melo static inline void inet_csk_reqsk_queue_unlink(struct sock *sk,
1221*463c84b9SArnaldo Carvalho de Melo 					       struct request_sock *req,
122260236fddSArnaldo Carvalho de Melo 					       struct request_sock **prev)
12231da177e4SLinus Torvalds {
1224*463c84b9SArnaldo Carvalho de Melo 	reqsk_queue_unlink(&inet_csk(sk)->icsk_accept_queue, req, prev);
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds 
1227*463c84b9SArnaldo Carvalho de Melo static inline void inet_csk_reqsk_queue_drop(struct sock *sk,
1228*463c84b9SArnaldo Carvalho de Melo 					     struct request_sock *req,
122960236fddSArnaldo Carvalho de Melo 					     struct request_sock **prev)
12301da177e4SLinus Torvalds {
1231*463c84b9SArnaldo Carvalho de Melo 	inet_csk_reqsk_queue_unlink(sk, req, prev);
1232*463c84b9SArnaldo Carvalho de Melo 	inet_csk_reqsk_queue_removed(sk, req);
123360236fddSArnaldo Carvalho de Melo 	reqsk_free(req);
12341da177e4SLinus Torvalds }
12351da177e4SLinus Torvalds 
123660236fddSArnaldo Carvalho de Melo static __inline__ void tcp_openreq_init(struct request_sock *req,
12371da177e4SLinus Torvalds 					struct tcp_options_received *rx_opt,
12381da177e4SLinus Torvalds 					struct sk_buff *skb)
12391da177e4SLinus Torvalds {
12402e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq = inet_rsk(req);
12412e6599cbSArnaldo Carvalho de Melo 
12421da177e4SLinus Torvalds 	req->rcv_wnd = 0;		/* So that tcp_send_synack() knows! */
12432e6599cbSArnaldo Carvalho de Melo 	tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
12441da177e4SLinus Torvalds 	req->mss = rx_opt->mss_clamp;
12451da177e4SLinus Torvalds 	req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
12462e6599cbSArnaldo Carvalho de Melo 	ireq->tstamp_ok = rx_opt->tstamp_ok;
12472e6599cbSArnaldo Carvalho de Melo 	ireq->sack_ok = rx_opt->sack_ok;
12482e6599cbSArnaldo Carvalho de Melo 	ireq->snd_wscale = rx_opt->snd_wscale;
12492e6599cbSArnaldo Carvalho de Melo 	ireq->wscale_ok = rx_opt->wscale_ok;
12502e6599cbSArnaldo Carvalho de Melo 	ireq->acked = 0;
12512e6599cbSArnaldo Carvalho de Melo 	ireq->ecn_ok = 0;
12522e6599cbSArnaldo Carvalho de Melo 	ireq->rmt_port = skb->h.th->source;
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds extern void tcp_enter_memory_pressure(void);
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
12581da177e4SLinus Torvalds {
12591da177e4SLinus Torvalds 	return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
12601da177e4SLinus Torvalds }
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
12631da177e4SLinus Torvalds {
12641da177e4SLinus Torvalds 	return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
12651da177e4SLinus Torvalds }
12661da177e4SLinus Torvalds 
1267*463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
12681da177e4SLinus Torvalds {
1269*463c84b9SArnaldo Carvalho de Melo 	int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
1270*463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
12711da177e4SLinus Torvalds 
1272*463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1273*463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	return fin_timeout;
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds static inline int tcp_paws_check(const struct tcp_options_received *rx_opt, int rst)
12791da177e4SLinus Torvalds {
12801da177e4SLinus Torvalds 	if ((s32)(rx_opt->rcv_tsval - rx_opt->ts_recent) >= 0)
12811da177e4SLinus Torvalds 		return 0;
12821da177e4SLinus Torvalds 	if (xtime.tv_sec >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)
12831da177e4SLinus Torvalds 		return 0;
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
12861da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
12871da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
12881da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
12891da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
12901da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
12911da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
12921da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
12931da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
12961da177e4SLinus Torvalds 	 */
12971da177e4SLinus Torvalds 	if (rst && xtime.tv_sec >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
12981da177e4SLinus Torvalds 		return 0;
12991da177e4SLinus Torvalds 	return 1;
13001da177e4SLinus Torvalds }
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds #define TCP_CHECK_TIMER(sk) do { } while (0)
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds static inline int tcp_use_frto(const struct sock *sk)
13051da177e4SLinus Torvalds {
13061da177e4SLinus Torvalds 	const struct tcp_sock *tp = tcp_sk(sk);
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds 	/* F-RTO must be activated in sysctl and there must be some
13091da177e4SLinus Torvalds 	 * unsent new data, and the advertised window should allow
13101da177e4SLinus Torvalds 	 * sending it.
13111da177e4SLinus Torvalds 	 */
13121da177e4SLinus Torvalds 	return (sysctl_tcp_frto && sk->sk_send_head &&
13131da177e4SLinus Torvalds 		!after(TCP_SKB_CB(sk->sk_send_head)->end_seq,
13141da177e4SLinus Torvalds 		       tp->snd_una + tp->snd_wnd));
13151da177e4SLinus Torvalds }
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds static inline void tcp_mib_init(void)
13181da177e4SLinus Torvalds {
13191da177e4SLinus Torvalds 	/* See RFC 2012 */
13201da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_RTOALGORITHM, 1);
13211da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
13221da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
13231da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_MAXCONN, -1);
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds /* /proc */
13271da177e4SLinus Torvalds enum tcp_seq_states {
13281da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
13291da177e4SLinus Torvalds 	TCP_SEQ_STATE_OPENREQ,
13301da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
13311da177e4SLinus Torvalds 	TCP_SEQ_STATE_TIME_WAIT,
13321da177e4SLinus Torvalds };
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds struct tcp_seq_afinfo {
13351da177e4SLinus Torvalds 	struct module		*owner;
13361da177e4SLinus Torvalds 	char			*name;
13371da177e4SLinus Torvalds 	sa_family_t		family;
13381da177e4SLinus Torvalds 	int			(*seq_show) (struct seq_file *m, void *v);
13391da177e4SLinus Torvalds 	struct file_operations	*seq_fops;
13401da177e4SLinus Torvalds };
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds struct tcp_iter_state {
13431da177e4SLinus Torvalds 	sa_family_t		family;
13441da177e4SLinus Torvalds 	enum tcp_seq_states	state;
13451da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
13461da177e4SLinus Torvalds 	int			bucket, sbucket, num, uid;
13471da177e4SLinus Torvalds 	struct seq_operations	seq_ops;
13481da177e4SLinus Torvalds };
13491da177e4SLinus Torvalds 
13501da177e4SLinus Torvalds extern int tcp_proc_register(struct tcp_seq_afinfo *afinfo);
13511da177e4SLinus Torvalds extern void tcp_proc_unregister(struct tcp_seq_afinfo *afinfo);
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds #endif	/* _TCP_H */
1354