xref: /linux/include/net/tcp.h (revision 3fdadf7d27e3fbcf72930941884387d1f4936f04)
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 
635d424d5aSJohn Heffner /* The least MTU to use for probing */
645d424d5aSJohn Heffner #define TCP_BASE_MSS		512
655d424d5aSJohn Heffner 
661da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
671da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /* Maximal reordering. */
701da177e4SLinus Torvalds #define TCP_MAX_REORDERING	127
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
731da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds /* urg_data states */
761da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
771da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
781da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds #define TCP_RETR1	3	/*
811da177e4SLinus Torvalds 				 * This is how many retries it does before it
821da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
831da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
841da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
851da177e4SLinus Torvalds 				 */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #define TCP_RETR2	15	/*
881da177e4SLinus Torvalds 				 * This should take at least
891da177e4SLinus Torvalds 				 * 90 minutes to time out.
901da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
911da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
921da177e4SLinus Torvalds 				 */
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #define TCP_SYN_RETRIES	 5	/* number of times to retry active opening a
95caa20d9aSStephen Hemminger 				 * connection: ~180sec is RFC minimum	*/
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds #define TCP_SYNACK_RETRIES 5	/* number of times to retry passive opening a
98caa20d9aSStephen Hemminger 				 * connection: ~180sec is RFC minimum	*/
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds #define TCP_ORPHAN_RETRIES 7	/* number of times to retry on an orphaned
1021da177e4SLinus Torvalds 				 * socket. 7 is ~50sec-16min.
1031da177e4SLinus Torvalds 				 */
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1071da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1081da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1091da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1101da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1111da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1121da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1131da177e4SLinus Torvalds 				  */
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1161da177e4SLinus Torvalds #if HZ >= 100
1171da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1181da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1191da177e4SLinus Torvalds #else
1201da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1211da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1221da177e4SLinus Torvalds #endif
1231da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1241da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
1251da177e4SLinus Torvalds #define TCP_TIMEOUT_INIT ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value	*/
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1281da177e4SLinus Torvalds 					                 * for local resources.
1291da177e4SLinus Torvalds 					                 */
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1321da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1331da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1361da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1371da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1381da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1411da177e4SLinus Torvalds #define TCP_SYNQ_HSIZE		512	/* Size of SYNACK hash table */
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1441da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1451da177e4SLinus Torvalds 					 * after this time. It should be equal
1461da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1471da177e4SLinus Torvalds 					 * to provide reliability equal to one
1481da177e4SLinus Torvalds 					 * provided by timewait state.
1491da177e4SLinus Torvalds 					 */
1501da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1511da177e4SLinus Torvalds 					 * timestamps. It must be less than
1521da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1531da177e4SLinus Torvalds 					 */
1541da177e4SLinus Torvalds /*
1551da177e4SLinus Torvalds  *	TCP option
1561da177e4SLinus Torvalds  */
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1591da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1601da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1611da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1621da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1631da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1641da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /*
1671da177e4SLinus Torvalds  *     TCP option lengths
1681da177e4SLinus Torvalds  */
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds #define TCPOLEN_MSS            4
1711da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
1721da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
1731da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /* But this is what stacks really send out. */
1761da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
1771da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
1781da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
1791da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
1801da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
1811da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /* Flags in tp->nonagle */
1841da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
1851da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
186caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
1871da177e4SLinus Torvalds 
188295ff7edSArnaldo Carvalho de Melo extern struct inet_timewait_death_row tcp_death_row;
189295ff7edSArnaldo Carvalho de Melo 
1901da177e4SLinus Torvalds /* sysctl variables for tcp */
1911da177e4SLinus Torvalds extern int sysctl_tcp_timestamps;
1921da177e4SLinus Torvalds extern int sysctl_tcp_window_scaling;
1931da177e4SLinus Torvalds extern int sysctl_tcp_sack;
1941da177e4SLinus Torvalds extern int sysctl_tcp_fin_timeout;
1951da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_time;
1961da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_probes;
1971da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_intvl;
1981da177e4SLinus Torvalds extern int sysctl_tcp_syn_retries;
1991da177e4SLinus Torvalds extern int sysctl_tcp_synack_retries;
2001da177e4SLinus Torvalds extern int sysctl_tcp_retries1;
2011da177e4SLinus Torvalds extern int sysctl_tcp_retries2;
2021da177e4SLinus Torvalds extern int sysctl_tcp_orphan_retries;
2031da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
2041da177e4SLinus Torvalds extern int sysctl_tcp_retrans_collapse;
2051da177e4SLinus Torvalds extern int sysctl_tcp_stdurg;
2061da177e4SLinus Torvalds extern int sysctl_tcp_rfc1337;
2071da177e4SLinus Torvalds extern int sysctl_tcp_abort_on_overflow;
2081da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
2091da177e4SLinus Torvalds extern int sysctl_tcp_fack;
2101da177e4SLinus Torvalds extern int sysctl_tcp_reordering;
2111da177e4SLinus Torvalds extern int sysctl_tcp_ecn;
2121da177e4SLinus Torvalds extern int sysctl_tcp_dsack;
2131da177e4SLinus Torvalds extern int sysctl_tcp_mem[3];
2141da177e4SLinus Torvalds extern int sysctl_tcp_wmem[3];
2151da177e4SLinus Torvalds extern int sysctl_tcp_rmem[3];
2161da177e4SLinus Torvalds extern int sysctl_tcp_app_win;
2171da177e4SLinus Torvalds extern int sysctl_tcp_adv_win_scale;
2181da177e4SLinus Torvalds extern int sysctl_tcp_tw_reuse;
2191da177e4SLinus Torvalds extern int sysctl_tcp_frto;
2201da177e4SLinus Torvalds extern int sysctl_tcp_low_latency;
2211da177e4SLinus Torvalds extern int sysctl_tcp_nometrics_save;
2221da177e4SLinus Torvalds extern int sysctl_tcp_moderate_rcvbuf;
2231da177e4SLinus Torvalds extern int sysctl_tcp_tso_win_divisor;
2249772efb9SStephen Hemminger extern int sysctl_tcp_abc;
2255d424d5aSJohn Heffner extern int sysctl_tcp_mtu_probing;
2265d424d5aSJohn Heffner extern int sysctl_tcp_base_mss;
22715d99e02SRick Jones extern int sysctl_tcp_workaround_signed_windows;
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds extern atomic_t tcp_memory_allocated;
2301da177e4SLinus Torvalds extern atomic_t tcp_sockets_allocated;
2311da177e4SLinus Torvalds extern int tcp_memory_pressure;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds /*
2341da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
2351da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
2361da177e4SLinus Torvalds  */
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds static inline int before(__u32 seq1, __u32 seq2)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds         return (__s32)(seq1-seq2) < 0;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds static inline int after(__u32 seq1, __u32 seq2)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	return (__s32)(seq2-seq1) < 0;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
2501da177e4SLinus Torvalds static inline int between(__u32 seq1, __u32 seq2, __u32 seq3)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds extern struct proto tcp_prot;
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds DECLARE_SNMP_STAT(struct tcp_mib, tcp_statistics);
2591da177e4SLinus Torvalds #define TCP_INC_STATS(field)		SNMP_INC_STATS(tcp_statistics, field)
2601da177e4SLinus Torvalds #define TCP_INC_STATS_BH(field)		SNMP_INC_STATS_BH(tcp_statistics, field)
2611da177e4SLinus Torvalds #define TCP_INC_STATS_USER(field) 	SNMP_INC_STATS_USER(tcp_statistics, field)
2621da177e4SLinus Torvalds #define TCP_DEC_STATS(field)		SNMP_DEC_STATS(tcp_statistics, field)
2631da177e4SLinus Torvalds #define TCP_ADD_STATS_BH(field, val)	SNMP_ADD_STATS_BH(tcp_statistics, field, val)
2641da177e4SLinus Torvalds #define TCP_ADD_STATS_USER(field, val)	SNMP_ADD_STATS_USER(tcp_statistics, field, val)
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds extern void			tcp_v4_err(struct sk_buff *skb, u32);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds extern void			tcp_shutdown (struct sock *sk, int how);
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds extern int			tcp_v4_rcv(struct sk_buff *skb);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds extern int			tcp_v4_remember_stamp(struct sock *sk);
2731da177e4SLinus Torvalds 
2748feaf0c0SArnaldo Carvalho de Melo extern int		    	tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds extern int			tcp_sendmsg(struct kiocb *iocb, struct sock *sk,
2771da177e4SLinus Torvalds 					    struct msghdr *msg, size_t size);
2781da177e4SLinus Torvalds extern ssize_t			tcp_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds extern int			tcp_ioctl(struct sock *sk,
2811da177e4SLinus Torvalds 					  int cmd,
2821da177e4SLinus Torvalds 					  unsigned long arg);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds extern int			tcp_rcv_state_process(struct sock *sk,
2851da177e4SLinus Torvalds 						      struct sk_buff *skb,
2861da177e4SLinus Torvalds 						      struct tcphdr *th,
2871da177e4SLinus Torvalds 						      unsigned len);
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds extern int			tcp_rcv_established(struct sock *sk,
2901da177e4SLinus Torvalds 						    struct sk_buff *skb,
2911da177e4SLinus Torvalds 						    struct tcphdr *th,
2921da177e4SLinus Torvalds 						    unsigned len);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds extern void			tcp_rcv_space_adjust(struct sock *sk);
2951da177e4SLinus Torvalds 
2966d6ee43eSArnaldo Carvalho de Melo extern int			tcp_twsk_unique(struct sock *sk,
2976d6ee43eSArnaldo Carvalho de Melo 						struct sock *sktw, void *twp);
2986d6ee43eSArnaldo Carvalho de Melo 
299463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
300463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3011da177e4SLinus Torvalds {
302463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
303fc6415bcSDavid S. Miller 
304463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
305463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
306463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3071da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
308463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
309fc6415bcSDavid S. Miller 		} else
310463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3111da177e4SLinus Torvalds 	}
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds 
314463c84b9SArnaldo Carvalho de Melo extern void tcp_enter_quickack_mode(struct sock *sk);
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds static inline void tcp_clear_options(struct tcp_options_received *rx_opt)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds  	rx_opt->tstamp_ok = rx_opt->sack_ok = rx_opt->wscale_ok = rx_opt->snd_wscale = 0;
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds enum tcp_tw_status
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
3241da177e4SLinus Torvalds 	TCP_TW_RST = 1,
3251da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
3261da177e4SLinus Torvalds 	TCP_TW_SYN = 3
3271da177e4SLinus Torvalds };
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 
3308feaf0c0SArnaldo Carvalho de Melo extern enum tcp_tw_status	tcp_timewait_state_process(struct inet_timewait_sock *tw,
3311da177e4SLinus Torvalds 							   struct sk_buff *skb,
3328feaf0c0SArnaldo Carvalho de Melo 							   const struct tcphdr *th);
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds extern struct sock *		tcp_check_req(struct sock *sk,struct sk_buff *skb,
33560236fddSArnaldo Carvalho de Melo 					      struct request_sock *req,
33660236fddSArnaldo Carvalho de Melo 					      struct request_sock **prev);
3371da177e4SLinus Torvalds extern int			tcp_child_process(struct sock *parent,
3381da177e4SLinus Torvalds 						  struct sock *child,
3391da177e4SLinus Torvalds 						  struct sk_buff *skb);
3401da177e4SLinus Torvalds extern void			tcp_enter_frto(struct sock *sk);
3411da177e4SLinus Torvalds extern void			tcp_enter_loss(struct sock *sk, int how);
3421da177e4SLinus Torvalds extern void			tcp_clear_retrans(struct tcp_sock *tp);
3431da177e4SLinus Torvalds extern void			tcp_update_metrics(struct sock *sk);
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds extern void			tcp_close(struct sock *sk,
3461da177e4SLinus Torvalds 					  long timeout);
3471da177e4SLinus Torvalds extern unsigned int		tcp_poll(struct file * file, struct socket *sock, struct poll_table_struct *wait);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds extern int			tcp_getsockopt(struct sock *sk, int level,
3501da177e4SLinus Torvalds 					       int optname,
3511da177e4SLinus Torvalds 					       char __user *optval,
3521da177e4SLinus Torvalds 					       int __user *optlen);
3531da177e4SLinus Torvalds extern int			tcp_setsockopt(struct sock *sk, int level,
3541da177e4SLinus Torvalds 					       int optname, char __user *optval,
3551da177e4SLinus Torvalds 					       int optlen);
356*3fdadf7dSDmitry Mishin extern int			compat_tcp_getsockopt(struct sock *sk,
357*3fdadf7dSDmitry Mishin 					int level, int optname,
358*3fdadf7dSDmitry Mishin 					char __user *optval, int __user *optlen);
359*3fdadf7dSDmitry Mishin extern int			compat_tcp_setsockopt(struct sock *sk,
360*3fdadf7dSDmitry Mishin 					int level, int optname,
361*3fdadf7dSDmitry Mishin 					char __user *optval, int optlen);
3621da177e4SLinus Torvalds extern void			tcp_set_keepalive(struct sock *sk, int val);
3631da177e4SLinus Torvalds extern int			tcp_recvmsg(struct kiocb *iocb, struct sock *sk,
3641da177e4SLinus Torvalds 					    struct msghdr *msg,
3651da177e4SLinus Torvalds 					    size_t len, int nonblock,
3661da177e4SLinus Torvalds 					    int flags, int *addr_len);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds extern void			tcp_parse_options(struct sk_buff *skb,
3691da177e4SLinus Torvalds 						  struct tcp_options_received *opt_rx,
3701da177e4SLinus Torvalds 						  int estab);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /*
3731da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
3741da177e4SLinus Torvalds  */
3751da177e4SLinus Torvalds 
3768292a17aSArnaldo Carvalho de Melo extern void		       	tcp_v4_send_check(struct sock *sk, int len,
3771da177e4SLinus Torvalds 						  struct sk_buff *skb);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds extern int			tcp_v4_conn_request(struct sock *sk,
3801da177e4SLinus Torvalds 						    struct sk_buff *skb);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds extern struct sock *		tcp_create_openreq_child(struct sock *sk,
38360236fddSArnaldo Carvalho de Melo 							 struct request_sock *req,
3841da177e4SLinus Torvalds 							 struct sk_buff *skb);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds extern struct sock *		tcp_v4_syn_recv_sock(struct sock *sk,
3871da177e4SLinus Torvalds 						     struct sk_buff *skb,
38860236fddSArnaldo Carvalho de Melo 						     struct request_sock *req,
3891da177e4SLinus Torvalds 							struct dst_entry *dst);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds extern int			tcp_v4_do_rcv(struct sock *sk,
3921da177e4SLinus Torvalds 					      struct sk_buff *skb);
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds extern int			tcp_v4_connect(struct sock *sk,
3951da177e4SLinus Torvalds 					       struct sockaddr *uaddr,
3961da177e4SLinus Torvalds 					       int addr_len);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds extern int			tcp_connect(struct sock *sk);
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds extern struct sk_buff *		tcp_make_synack(struct sock *sk,
4011da177e4SLinus Torvalds 						struct dst_entry *dst,
40260236fddSArnaldo Carvalho de Melo 						struct request_sock *req);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds extern int			tcp_disconnect(struct sock *sk, int flags);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds extern void			tcp_unhash(struct sock *sk);
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds extern int			tcp_v4_hash_connecting(struct sock *sk);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds /* From syncookies.c */
4121da177e4SLinus Torvalds extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
4131da177e4SLinus Torvalds 				    struct ip_options *opt);
4141da177e4SLinus Torvalds extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb,
4151da177e4SLinus Torvalds 				     __u16 *mss);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds /* tcp_output.c */
4181da177e4SLinus Torvalds 
419f6302d1dSDavid S. Miller extern void __tcp_push_pending_frames(struct sock *sk, struct tcp_sock *tp,
420a2e2a59cSDavid S. Miller 				      unsigned int cur_mss, int nonagle);
421f6302d1dSDavid S. Miller extern int tcp_may_send_now(struct sock *sk, struct tcp_sock *tp);
4221da177e4SLinus Torvalds extern int tcp_retransmit_skb(struct sock *, struct sk_buff *);
4231da177e4SLinus Torvalds extern void tcp_xmit_retransmit_queue(struct sock *);
4241da177e4SLinus Torvalds extern void tcp_simple_retransmit(struct sock *);
4251da177e4SLinus Torvalds extern int tcp_trim_head(struct sock *, struct sk_buff *, u32);
4266475be16SDavid S. Miller extern int tcp_fragment(struct sock *, struct sk_buff *, u32, unsigned int);
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds extern void tcp_send_probe0(struct sock *);
4291da177e4SLinus Torvalds extern void tcp_send_partial(struct sock *);
4301da177e4SLinus Torvalds extern int  tcp_write_wakeup(struct sock *);
4311da177e4SLinus Torvalds extern void tcp_send_fin(struct sock *sk);
432dd0fc66fSAl Viro extern void tcp_send_active_reset(struct sock *sk, gfp_t priority);
4331da177e4SLinus Torvalds extern int  tcp_send_synack(struct sock *);
434c1b4a7e6SDavid S. Miller extern void tcp_push_one(struct sock *, unsigned int mss_now);
4351da177e4SLinus Torvalds extern void tcp_send_ack(struct sock *sk);
4361da177e4SLinus Torvalds extern void tcp_send_delayed_ack(struct sock *sk);
4371da177e4SLinus Torvalds 
438a762a980SDavid S. Miller /* tcp_input.c */
439a762a980SDavid S. Miller extern void tcp_cwnd_application_limited(struct sock *sk);
440a762a980SDavid S. Miller 
4411da177e4SLinus Torvalds /* tcp_timer.c */
4421da177e4SLinus Torvalds extern void tcp_init_xmit_timers(struct sock *);
443463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
444463c84b9SArnaldo Carvalho de Melo {
445463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
446463c84b9SArnaldo Carvalho de Melo }
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds extern unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
4491da177e4SLinus Torvalds extern unsigned int tcp_current_mss(struct sock *sk, int large);
4501da177e4SLinus Torvalds 
45117b085eaSArnaldo Carvalho de Melo /* tcp.c */
4521da177e4SLinus Torvalds extern void tcp_get_info(struct sock *, struct tcp_info *);
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
4551da177e4SLinus Torvalds typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
4561da177e4SLinus Torvalds 				unsigned int, size_t);
4571da177e4SLinus Torvalds extern int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
4581da177e4SLinus Torvalds 			 sk_read_actor_t recv_actor);
4591da177e4SLinus Torvalds 
46040efc6faSStephen Hemminger extern void tcp_initialize_rcv_mss(struct sock *sk);
4611da177e4SLinus Torvalds 
4625d424d5aSJohn Heffner extern int tcp_mtu_to_mss(struct sock *sk, int pmtu);
4635d424d5aSJohn Heffner extern int tcp_mss_to_mtu(struct sock *sk, int mss);
4645d424d5aSJohn Heffner extern void tcp_mtup_init(struct sock *sk);
4655d424d5aSJohn Heffner 
46640efc6faSStephen Hemminger static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
4671da177e4SLinus Torvalds {
4681da177e4SLinus Torvalds 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
4691da177e4SLinus Torvalds 			       ntohl(TCP_FLAG_ACK) |
4701da177e4SLinus Torvalds 			       snd_wnd);
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
47340efc6faSStephen Hemminger static inline void tcp_fast_path_on(struct tcp_sock *tp)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds static inline void tcp_fast_path_check(struct sock *sk, struct tcp_sock *tp)
4791da177e4SLinus Torvalds {
480b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue) &&
4811da177e4SLinus Torvalds 	    tp->rcv_wnd &&
4821da177e4SLinus Torvalds 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
4831da177e4SLinus Torvalds 	    !tp->urg_data)
4841da177e4SLinus Torvalds 		tcp_fast_path_on(tp);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
4881da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
4891da177e4SLinus Torvalds  * than the offered window.
4901da177e4SLinus Torvalds  */
49140efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	if (win < 0)
4961da177e4SLinus Torvalds 		win = 0;
4971da177e4SLinus Torvalds 	return (u32) win;
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
5011da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
5021da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
5031da177e4SLinus Torvalds  */
5041da177e4SLinus Torvalds extern u32	__tcp_select_window(struct sock *sk);
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds /* TCP timestamps are only 32-bits, this causes a slight
5071da177e4SLinus Torvalds  * complication on 64-bit systems since we store a snapshot
50831f34269SStephen Hemminger  * of jiffies in the buffer control blocks below.  We decided
50931f34269SStephen Hemminger  * to use only the low 32-bits of jiffies and hide the ugly
5101da177e4SLinus Torvalds  * casts with the following macro.
5111da177e4SLinus Torvalds  */
5121da177e4SLinus Torvalds #define tcp_time_stamp		((__u32)(jiffies))
5131da177e4SLinus Torvalds 
514caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
5151da177e4SLinus Torvalds  * TCP per-packet control information to the transmission
5161da177e4SLinus Torvalds  * code.  We also store the host-order sequence numbers in
5171da177e4SLinus Torvalds  * here too.  This is 36 bytes on 32-bit architectures,
5181da177e4SLinus Torvalds  * 40 bytes on 64-bit machines, if this grows please adjust
5191da177e4SLinus Torvalds  * skbuff.h:skbuff->cb[xxx] size appropriately.
5201da177e4SLinus Torvalds  */
5211da177e4SLinus Torvalds struct tcp_skb_cb {
5221da177e4SLinus Torvalds 	union {
5231da177e4SLinus Torvalds 		struct inet_skb_parm	h4;
5241da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
5251da177e4SLinus Torvalds 		struct inet6_skb_parm	h6;
5261da177e4SLinus Torvalds #endif
5271da177e4SLinus Torvalds 	} header;	/* For incoming frames		*/
5281da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
5291da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
5301da177e4SLinus Torvalds 	__u32		when;		/* used to compute rtt's	*/
5311da177e4SLinus Torvalds 	__u8		flags;		/* TCP header flags.		*/
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	/* NOTE: These must match up to the flags byte in a
5341da177e4SLinus Torvalds 	 *       real TCP header.
5351da177e4SLinus Torvalds 	 */
5361da177e4SLinus Torvalds #define TCPCB_FLAG_FIN		0x01
5371da177e4SLinus Torvalds #define TCPCB_FLAG_SYN		0x02
5381da177e4SLinus Torvalds #define TCPCB_FLAG_RST		0x04
5391da177e4SLinus Torvalds #define TCPCB_FLAG_PSH		0x08
5401da177e4SLinus Torvalds #define TCPCB_FLAG_ACK		0x10
5411da177e4SLinus Torvalds #define TCPCB_FLAG_URG		0x20
5421da177e4SLinus Torvalds #define TCPCB_FLAG_ECE		0x40
5431da177e4SLinus Torvalds #define TCPCB_FLAG_CWR		0x80
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	__u8		sacked;		/* State flags for SACK/FACK.	*/
5461da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
5471da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
5481da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
5491da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
5521da177e4SLinus Torvalds #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
5531da177e4SLinus Torvalds 
554caa20d9aSStephen Hemminger #define TCPCB_URG		0x20	/* Urgent pointer advanced here	*/
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds #define TCPCB_AT_TAIL		(TCPCB_URG)
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	__u16		urg_ptr;	/* Valid w/URG flags is set.	*/
5591da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
5601da177e4SLinus Torvalds };
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds #include <net/tcp_ecn.h>
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
5671da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
5681da177e4SLinus Torvalds  */
5691da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	return skb_shinfo(skb)->tso_segs;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds /* This is valid iff tcp_skb_pcount() > 1. */
5751da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds 	return skb_shinfo(skb)->tso_size;
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds static inline void tcp_dec_pcount_approx(__u32 *count,
5811da177e4SLinus Torvalds 					 const struct sk_buff *skb)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	if (*count) {
5841da177e4SLinus Torvalds 		*count -= tcp_skb_pcount(skb);
5851da177e4SLinus Torvalds 		if ((int)*count < 0)
5861da177e4SLinus Torvalds 			*count = 0;
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds }
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds static inline void tcp_packets_out_inc(struct sock *sk,
5911da177e4SLinus Torvalds 				       struct tcp_sock *tp,
5921da177e4SLinus Torvalds 				       const struct sk_buff *skb)
5931da177e4SLinus Torvalds {
5941da177e4SLinus Torvalds 	int orig = tp->packets_out;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	tp->packets_out += tcp_skb_pcount(skb);
5971da177e4SLinus Torvalds 	if (!orig)
5983f421baaSArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
5993f421baaSArnaldo Carvalho de Melo 					  inet_csk(sk)->icsk_rto, TCP_RTO_MAX);
6001da177e4SLinus Torvalds }
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds static inline void tcp_packets_out_dec(struct tcp_sock *tp,
6031da177e4SLinus Torvalds 				       const struct sk_buff *skb)
6041da177e4SLinus Torvalds {
6051da177e4SLinus Torvalds 	tp->packets_out -= tcp_skb_pcount(skb);
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
608317a76f9SStephen Hemminger /* Events passed to congestion control interface */
609317a76f9SStephen Hemminger enum tcp_ca_event {
610317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
611317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
612317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
613317a76f9SStephen Hemminger 	CA_EVENT_FRTO,		/* fast recovery timeout */
614317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
615317a76f9SStephen Hemminger 	CA_EVENT_FAST_ACK,	/* in sequence ack */
616317a76f9SStephen Hemminger 	CA_EVENT_SLOW_ACK,	/* other ack */
617317a76f9SStephen Hemminger };
618317a76f9SStephen Hemminger 
619317a76f9SStephen Hemminger /*
620317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
621317a76f9SStephen Hemminger  */
622317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
623317a76f9SStephen Hemminger struct tcp_congestion_ops {
624317a76f9SStephen Hemminger 	struct list_head	list;
625317a76f9SStephen Hemminger 
626317a76f9SStephen Hemminger 	/* initialize private data (optional) */
6276687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
628317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
6296687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
630317a76f9SStephen Hemminger 
631317a76f9SStephen Hemminger 	/* return slow start threshold (required) */
6326687e988SArnaldo Carvalho de Melo 	u32 (*ssthresh)(struct sock *sk);
633317a76f9SStephen Hemminger 	/* lower bound for congestion window (optional) */
6346687e988SArnaldo Carvalho de Melo 	u32 (*min_cwnd)(struct sock *sk);
635317a76f9SStephen Hemminger 	/* do new cwnd calculation (required) */
6366687e988SArnaldo Carvalho de Melo 	void (*cong_avoid)(struct sock *sk, u32 ack,
637317a76f9SStephen Hemminger 			   u32 rtt, u32 in_flight, int good_ack);
638317a76f9SStephen Hemminger 	/* round trip time sample per acked packet (optional) */
6396687e988SArnaldo Carvalho de Melo 	void (*rtt_sample)(struct sock *sk, u32 usrtt);
640317a76f9SStephen Hemminger 	/* call before changing ca_state (optional) */
6416687e988SArnaldo Carvalho de Melo 	void (*set_state)(struct sock *sk, u8 new_state);
642317a76f9SStephen Hemminger 	/* call when cwnd event occurs (optional) */
6436687e988SArnaldo Carvalho de Melo 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
644317a76f9SStephen Hemminger 	/* new value of cwnd after loss (optional) */
6456687e988SArnaldo Carvalho de Melo 	u32  (*undo_cwnd)(struct sock *sk);
646317a76f9SStephen Hemminger 	/* hook for packet ack accounting (optional) */
6476687e988SArnaldo Carvalho de Melo 	void (*pkts_acked)(struct sock *sk, u32 num_acked);
64873c1f4a0SArnaldo Carvalho de Melo 	/* get info for inet_diag (optional) */
6496687e988SArnaldo Carvalho de Melo 	void (*get_info)(struct sock *sk, u32 ext, struct sk_buff *skb);
650317a76f9SStephen Hemminger 
651317a76f9SStephen Hemminger 	char 		name[TCP_CA_NAME_MAX];
652317a76f9SStephen Hemminger 	struct module 	*owner;
653317a76f9SStephen Hemminger };
654317a76f9SStephen Hemminger 
655317a76f9SStephen Hemminger extern int tcp_register_congestion_control(struct tcp_congestion_ops *type);
656317a76f9SStephen Hemminger extern void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
657317a76f9SStephen Hemminger 
6586687e988SArnaldo Carvalho de Melo extern void tcp_init_congestion_control(struct sock *sk);
6596687e988SArnaldo Carvalho de Melo extern void tcp_cleanup_congestion_control(struct sock *sk);
660317a76f9SStephen Hemminger extern int tcp_set_default_congestion_control(const char *name);
661317a76f9SStephen Hemminger extern void tcp_get_default_congestion_control(char *name);
6626687e988SArnaldo Carvalho de Melo extern int tcp_set_congestion_control(struct sock *sk, const char *name);
66340efc6faSStephen Hemminger extern void tcp_slow_start(struct tcp_sock *tp);
664317a76f9SStephen Hemminger 
6655f8ef48dSStephen Hemminger extern struct tcp_congestion_ops tcp_init_congestion_ops;
6666687e988SArnaldo Carvalho de Melo extern u32 tcp_reno_ssthresh(struct sock *sk);
6676687e988SArnaldo Carvalho de Melo extern void tcp_reno_cong_avoid(struct sock *sk, u32 ack,
668317a76f9SStephen Hemminger 				u32 rtt, u32 in_flight, int flag);
6696687e988SArnaldo Carvalho de Melo extern u32 tcp_reno_min_cwnd(struct sock *sk);
670a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
671317a76f9SStephen Hemminger 
6726687e988SArnaldo Carvalho de Melo static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
673317a76f9SStephen Hemminger {
6746687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
6756687e988SArnaldo Carvalho de Melo 
6766687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->set_state)
6776687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->set_state(sk, ca_state);
6786687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_state = ca_state;
679317a76f9SStephen Hemminger }
680317a76f9SStephen Hemminger 
6816687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
682317a76f9SStephen Hemminger {
6836687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
6846687e988SArnaldo Carvalho de Melo 
6856687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
6866687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
687317a76f9SStephen Hemminger }
688317a76f9SStephen Hemminger 
6891da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
6901da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
6911da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
6921da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
6931da177e4SLinus Torvalds  *
6941da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
6951da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
6961da177e4SLinus Torvalds  *
6971da177e4SLinus Torvalds  * Read this equation as:
6981da177e4SLinus Torvalds  *
6991da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
7001da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
7011da177e4SLinus Torvalds  *	"Packets fast retransmitted"
7021da177e4SLinus Torvalds  */
70340efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
7041da177e4SLinus Torvalds {
7051da177e4SLinus Torvalds 	return (tp->packets_out - tp->left_out + tp->retrans_out);
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
7091da177e4SLinus Torvalds  * The exception is rate halving phase, when cwnd is decreasing towards
7101da177e4SLinus Torvalds  * ssthresh.
7111da177e4SLinus Torvalds  */
7126687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
7131da177e4SLinus Torvalds {
7146687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
7156687e988SArnaldo Carvalho de Melo 	if ((1 << inet_csk(sk)->icsk_ca_state) & (TCPF_CA_CWR | TCPF_CA_Recovery))
7161da177e4SLinus Torvalds 		return tp->snd_ssthresh;
7171da177e4SLinus Torvalds 	else
7181da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
7191da177e4SLinus Torvalds 			   ((tp->snd_cwnd >> 1) +
7201da177e4SLinus Torvalds 			    (tp->snd_cwnd >> 2)));
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds static inline void tcp_sync_left_out(struct tcp_sock *tp)
7241da177e4SLinus Torvalds {
7251da177e4SLinus Torvalds 	if (tp->rx_opt.sack_ok &&
7261da177e4SLinus Torvalds 	    (tp->sacked_out >= tp->packets_out - tp->lost_out))
7271da177e4SLinus Torvalds 		tp->sacked_out = tp->packets_out - tp->lost_out;
7281da177e4SLinus Torvalds 	tp->left_out = tp->sacked_out + tp->lost_out;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
73140efc6faSStephen Hemminger extern void tcp_enter_cwr(struct sock *sk);
7321da177e4SLinus Torvalds extern __u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst);
7331da177e4SLinus Torvalds 
7341da177e4SLinus Torvalds /* Slow start with delack produces 3 packets of burst, so that
7351da177e4SLinus Torvalds  * it is safe "de facto".
7361da177e4SLinus Torvalds  */
7371da177e4SLinus Torvalds static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
7381da177e4SLinus Torvalds {
7391da177e4SLinus Torvalds 	return 3;
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds 
742f4805edeSStephen Hemminger /* RFC2861 Check whether we are limited by application or congestion window
743f4805edeSStephen Hemminger  * This is the inverse of cwnd check in tcp_tso_should_defer
744f4805edeSStephen Hemminger  */
745f4805edeSStephen Hemminger static inline int tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight)
746f4805edeSStephen Hemminger {
747f4805edeSStephen Hemminger 	const struct tcp_sock *tp = tcp_sk(sk);
748f4805edeSStephen Hemminger 	u32 left;
749f4805edeSStephen Hemminger 
750f4805edeSStephen Hemminger 	if (in_flight >= tp->snd_cwnd)
751f4805edeSStephen Hemminger 		return 1;
752f4805edeSStephen Hemminger 
753f4805edeSStephen Hemminger 	if (!(sk->sk_route_caps & NETIF_F_TSO))
754f4805edeSStephen Hemminger 		return 0;
755f4805edeSStephen Hemminger 
756f4805edeSStephen Hemminger 	left = tp->snd_cwnd - in_flight;
757f4805edeSStephen Hemminger 	if (sysctl_tcp_tso_win_divisor)
758f4805edeSStephen Hemminger 		return left * sysctl_tcp_tso_win_divisor < tp->snd_cwnd;
759f4805edeSStephen Hemminger 	else
760f4805edeSStephen Hemminger 		return left <= tcp_max_burst(tp);
761f4805edeSStephen Hemminger }
762f4805edeSStephen Hemminger 
76340efc6faSStephen Hemminger static inline void tcp_minshall_update(struct tcp_sock *tp, int mss,
7641da177e4SLinus Torvalds 				       const struct sk_buff *skb)
7651da177e4SLinus Torvalds {
7661da177e4SLinus Torvalds 	if (skb->len < mss)
7671da177e4SLinus Torvalds 		tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
77040efc6faSStephen Hemminger static inline void tcp_check_probe_timer(struct sock *sk, struct tcp_sock *tp)
7711da177e4SLinus Torvalds {
772463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
773463c84b9SArnaldo Carvalho de Melo 	if (!tp->packets_out && !icsk->icsk_pending)
7743f421baaSArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
7753f421baaSArnaldo Carvalho de Melo 					  icsk->icsk_rto, TCP_RTO_MAX);
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
77840efc6faSStephen Hemminger static inline void tcp_push_pending_frames(struct sock *sk,
7791da177e4SLinus Torvalds 					   struct tcp_sock *tp)
7801da177e4SLinus Torvalds {
7811da177e4SLinus Torvalds 	__tcp_push_pending_frames(sk, tp, tcp_current_mss(sk, 1), tp->nonagle);
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
78440efc6faSStephen Hemminger static inline void tcp_init_wl(struct tcp_sock *tp, u32 ack, u32 seq)
7851da177e4SLinus Torvalds {
7861da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
78940efc6faSStephen Hemminger static inline void tcp_update_wl(struct tcp_sock *tp, u32 ack, u32 seq)
7901da177e4SLinus Torvalds {
7911da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds /*
7951da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
7961da177e4SLinus Torvalds  */
79740efc6faSStephen Hemminger static inline u16 tcp_v4_check(struct tcphdr *th, int len,
7981da177e4SLinus Torvalds 			       unsigned long saddr, unsigned long daddr,
7991da177e4SLinus Torvalds 			       unsigned long base)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
80440efc6faSStephen Hemminger static inline int __tcp_checksum_complete(struct sk_buff *skb)
8051da177e4SLinus Torvalds {
806fb286bb2SHerbert Xu 	return __skb_checksum_complete(skb);
8071da177e4SLinus Torvalds }
8081da177e4SLinus Torvalds 
80940efc6faSStephen Hemminger static inline int tcp_checksum_complete(struct sk_buff *skb)
8101da177e4SLinus Torvalds {
8111da177e4SLinus Torvalds 	return skb->ip_summed != CHECKSUM_UNNECESSARY &&
8121da177e4SLinus Torvalds 		__tcp_checksum_complete(skb);
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds /* Prequeue for VJ style copy to user, combined with checksumming. */
8161da177e4SLinus Torvalds 
81740efc6faSStephen Hemminger static inline void tcp_prequeue_init(struct tcp_sock *tp)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	tp->ucopy.task = NULL;
8201da177e4SLinus Torvalds 	tp->ucopy.len = 0;
8211da177e4SLinus Torvalds 	tp->ucopy.memory = 0;
8221da177e4SLinus Torvalds 	skb_queue_head_init(&tp->ucopy.prequeue);
8231da177e4SLinus Torvalds }
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds /* Packet is added to VJ-style prequeue for processing in process
8261da177e4SLinus Torvalds  * context, if a reader task is waiting. Apparently, this exciting
8271da177e4SLinus Torvalds  * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93)
8281da177e4SLinus Torvalds  * failed somewhere. Latency? Burstiness? Well, at least now we will
8291da177e4SLinus Torvalds  * see, why it failed. 8)8)				  --ANK
8301da177e4SLinus Torvalds  *
8311da177e4SLinus Torvalds  * NOTE: is this not too big to inline?
8321da177e4SLinus Torvalds  */
83340efc6faSStephen Hemminger static inline int tcp_prequeue(struct sock *sk, struct sk_buff *skb)
8341da177e4SLinus Torvalds {
8351da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	if (!sysctl_tcp_low_latency && tp->ucopy.task) {
8381da177e4SLinus Torvalds 		__skb_queue_tail(&tp->ucopy.prequeue, skb);
8391da177e4SLinus Torvalds 		tp->ucopy.memory += skb->truesize;
8401da177e4SLinus Torvalds 		if (tp->ucopy.memory > sk->sk_rcvbuf) {
8411da177e4SLinus Torvalds 			struct sk_buff *skb1;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 			BUG_ON(sock_owned_by_user(sk));
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 			while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) {
8461da177e4SLinus Torvalds 				sk->sk_backlog_rcv(sk, skb1);
8471da177e4SLinus Torvalds 				NET_INC_STATS_BH(LINUX_MIB_TCPPREQUEUEDROPPED);
8481da177e4SLinus Torvalds 			}
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds 			tp->ucopy.memory = 0;
8511da177e4SLinus Torvalds 		} else if (skb_queue_len(&tp->ucopy.prequeue) == 1) {
8521da177e4SLinus Torvalds 			wake_up_interruptible(sk->sk_sleep);
853463c84b9SArnaldo Carvalho de Melo 			if (!inet_csk_ack_scheduled(sk))
854463c84b9SArnaldo Carvalho de Melo 				inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
8553f421baaSArnaldo Carvalho de Melo 						          (3 * TCP_RTO_MIN) / 4,
8563f421baaSArnaldo Carvalho de Melo 							  TCP_RTO_MAX);
8571da177e4SLinus Torvalds 		}
8581da177e4SLinus Torvalds 		return 1;
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 	return 0;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds #undef STATE_TRACE
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds #ifdef STATE_TRACE
8671da177e4SLinus Torvalds static const char *statename[]={
8681da177e4SLinus Torvalds 	"Unused","Established","Syn Sent","Syn Recv",
8691da177e4SLinus Torvalds 	"Fin Wait 1","Fin Wait 2","Time Wait", "Close",
8701da177e4SLinus Torvalds 	"Close Wait","Last ACK","Listen","Closing"
8711da177e4SLinus Torvalds };
8721da177e4SLinus Torvalds #endif
8731da177e4SLinus Torvalds 
87440efc6faSStephen Hemminger static inline void tcp_set_state(struct sock *sk, int state)
8751da177e4SLinus Torvalds {
8761da177e4SLinus Torvalds 	int oldstate = sk->sk_state;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	switch (state) {
8791da177e4SLinus Torvalds 	case TCP_ESTABLISHED:
8801da177e4SLinus Torvalds 		if (oldstate != TCP_ESTABLISHED)
8811da177e4SLinus Torvalds 			TCP_INC_STATS(TCP_MIB_CURRESTAB);
8821da177e4SLinus Torvalds 		break;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	case TCP_CLOSE:
8851da177e4SLinus Torvalds 		if (oldstate == TCP_CLOSE_WAIT || oldstate == TCP_ESTABLISHED)
8861da177e4SLinus Torvalds 			TCP_INC_STATS(TCP_MIB_ESTABRESETS);
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 		sk->sk_prot->unhash(sk);
889463c84b9SArnaldo Carvalho de Melo 		if (inet_csk(sk)->icsk_bind_hash &&
8901da177e4SLinus Torvalds 		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
8912d8c4ce5SArnaldo Carvalho de Melo 			inet_put_port(&tcp_hashinfo, sk);
8921da177e4SLinus Torvalds 		/* fall through */
8931da177e4SLinus Torvalds 	default:
8941da177e4SLinus Torvalds 		if (oldstate==TCP_ESTABLISHED)
8951da177e4SLinus Torvalds 			TCP_DEC_STATS(TCP_MIB_CURRESTAB);
8961da177e4SLinus Torvalds 	}
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	/* Change state AFTER socket is unhashed to avoid closed
8991da177e4SLinus Torvalds 	 * socket sitting in hash tables.
9001da177e4SLinus Torvalds 	 */
9011da177e4SLinus Torvalds 	sk->sk_state = state;
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds #ifdef STATE_TRACE
9041da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "TCP sk=%p, State %s -> %s\n",sk, statename[oldstate],statename[state]);
9051da177e4SLinus Torvalds #endif
9061da177e4SLinus Torvalds }
9071da177e4SLinus Torvalds 
90840efc6faSStephen Hemminger static inline void tcp_done(struct sock *sk)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	tcp_set_state(sk, TCP_CLOSE);
9111da177e4SLinus Torvalds 	tcp_clear_xmit_timers(sk);
9121da177e4SLinus Torvalds 
9131da177e4SLinus Torvalds 	sk->sk_shutdown = SHUTDOWN_MASK;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD))
9161da177e4SLinus Torvalds 		sk->sk_state_change(sk);
9171da177e4SLinus Torvalds 	else
9180a5578cfSArnaldo Carvalho de Melo 		inet_csk_destroy_sock(sk);
9191da177e4SLinus Torvalds }
9201da177e4SLinus Torvalds 
92140efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds 	rx_opt->dsack = 0;
9241da177e4SLinus Torvalds 	rx_opt->eff_sacks = 0;
9251da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
9261da177e4SLinus Torvalds }
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
9291da177e4SLinus Torvalds extern void tcp_select_initial_window(int __space, __u32 mss,
9301da177e4SLinus Torvalds 				      __u32 *rcv_wnd, __u32 *window_clamp,
9311da177e4SLinus Torvalds 				      int wscale_ok, __u8 *rcv_wscale);
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds static inline int tcp_win_from_space(int space)
9341da177e4SLinus Torvalds {
9351da177e4SLinus Torvalds 	return sysctl_tcp_adv_win_scale<=0 ?
9361da177e4SLinus Torvalds 		(space>>(-sysctl_tcp_adv_win_scale)) :
9371da177e4SLinus Torvalds 		space - (space>>sysctl_tcp_adv_win_scale);
9381da177e4SLinus Torvalds }
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
9411da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
9421da177e4SLinus Torvalds {
9431da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf -
9441da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
9451da177e4SLinus Torvalds }
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
9481da177e4SLinus Torvalds {
9491da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf);
9501da177e4SLinus Torvalds }
9511da177e4SLinus Torvalds 
95240efc6faSStephen Hemminger static inline void tcp_openreq_init(struct request_sock *req,
9531da177e4SLinus Torvalds 				    struct tcp_options_received *rx_opt,
9541da177e4SLinus Torvalds 				    struct sk_buff *skb)
9551da177e4SLinus Torvalds {
9562e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq = inet_rsk(req);
9572e6599cbSArnaldo Carvalho de Melo 
9581da177e4SLinus Torvalds 	req->rcv_wnd = 0;		/* So that tcp_send_synack() knows! */
9592e6599cbSArnaldo Carvalho de Melo 	tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
9601da177e4SLinus Torvalds 	req->mss = rx_opt->mss_clamp;
9611da177e4SLinus Torvalds 	req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
9622e6599cbSArnaldo Carvalho de Melo 	ireq->tstamp_ok = rx_opt->tstamp_ok;
9632e6599cbSArnaldo Carvalho de Melo 	ireq->sack_ok = rx_opt->sack_ok;
9642e6599cbSArnaldo Carvalho de Melo 	ireq->snd_wscale = rx_opt->snd_wscale;
9652e6599cbSArnaldo Carvalho de Melo 	ireq->wscale_ok = rx_opt->wscale_ok;
9662e6599cbSArnaldo Carvalho de Melo 	ireq->acked = 0;
9672e6599cbSArnaldo Carvalho de Melo 	ireq->ecn_ok = 0;
9682e6599cbSArnaldo Carvalho de Melo 	ireq->rmt_port = skb->h.th->source;
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds extern void tcp_enter_memory_pressure(void);
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
9741da177e4SLinus Torvalds {
9751da177e4SLinus Torvalds 	return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
9791da177e4SLinus Torvalds {
9801da177e4SLinus Torvalds 	return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
9811da177e4SLinus Torvalds }
9821da177e4SLinus Torvalds 
983463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
9841da177e4SLinus Torvalds {
985463c84b9SArnaldo Carvalho de Melo 	int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
986463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
9871da177e4SLinus Torvalds 
988463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
989463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	return fin_timeout;
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds static inline int tcp_paws_check(const struct tcp_options_received *rx_opt, int rst)
9951da177e4SLinus Torvalds {
9961da177e4SLinus Torvalds 	if ((s32)(rx_opt->rcv_tsval - rx_opt->ts_recent) >= 0)
9971da177e4SLinus Torvalds 		return 0;
9981da177e4SLinus Torvalds 	if (xtime.tv_sec >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)
9991da177e4SLinus Torvalds 		return 0;
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
10021da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
10031da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
10041da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
10051da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
10061da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
10071da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
10081da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
10091da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
10121da177e4SLinus Torvalds 	 */
10131da177e4SLinus Torvalds 	if (rst && xtime.tv_sec >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
10141da177e4SLinus Torvalds 		return 0;
10151da177e4SLinus Torvalds 	return 1;
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds #define TCP_CHECK_TIMER(sk) do { } while (0)
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds static inline int tcp_use_frto(const struct sock *sk)
10211da177e4SLinus Torvalds {
10221da177e4SLinus Torvalds 	const struct tcp_sock *tp = tcp_sk(sk);
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	/* F-RTO must be activated in sysctl and there must be some
10251da177e4SLinus Torvalds 	 * unsent new data, and the advertised window should allow
10261da177e4SLinus Torvalds 	 * sending it.
10271da177e4SLinus Torvalds 	 */
10281da177e4SLinus Torvalds 	return (sysctl_tcp_frto && sk->sk_send_head &&
10291da177e4SLinus Torvalds 		!after(TCP_SKB_CB(sk->sk_send_head)->end_seq,
10301da177e4SLinus Torvalds 		       tp->snd_una + tp->snd_wnd));
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds static inline void tcp_mib_init(void)
10341da177e4SLinus Torvalds {
10351da177e4SLinus Torvalds 	/* See RFC 2012 */
10361da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_RTOALGORITHM, 1);
10371da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
10381da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
10391da177e4SLinus Torvalds 	TCP_ADD_STATS_USER(TCP_MIB_MAXCONN, -1);
10401da177e4SLinus Torvalds }
10411da177e4SLinus Torvalds 
10426a438bbeSStephen Hemminger /*from STCP */
10436a438bbeSStephen Hemminger static inline void clear_all_retrans_hints(struct tcp_sock *tp){
10446a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
10456a438bbeSStephen Hemminger 	tp->scoreboard_skb_hint = NULL;
10466a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
10476a438bbeSStephen Hemminger 	tp->forward_skb_hint = NULL;
10486a438bbeSStephen Hemminger 	tp->fastpath_skb_hint = NULL;
10496a438bbeSStephen Hemminger }
10506a438bbeSStephen Hemminger 
10511da177e4SLinus Torvalds /* /proc */
10521da177e4SLinus Torvalds enum tcp_seq_states {
10531da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
10541da177e4SLinus Torvalds 	TCP_SEQ_STATE_OPENREQ,
10551da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
10561da177e4SLinus Torvalds 	TCP_SEQ_STATE_TIME_WAIT,
10571da177e4SLinus Torvalds };
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds struct tcp_seq_afinfo {
10601da177e4SLinus Torvalds 	struct module		*owner;
10611da177e4SLinus Torvalds 	char			*name;
10621da177e4SLinus Torvalds 	sa_family_t		family;
10631da177e4SLinus Torvalds 	int			(*seq_show) (struct seq_file *m, void *v);
10641da177e4SLinus Torvalds 	struct file_operations	*seq_fops;
10651da177e4SLinus Torvalds };
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds struct tcp_iter_state {
10681da177e4SLinus Torvalds 	sa_family_t		family;
10691da177e4SLinus Torvalds 	enum tcp_seq_states	state;
10701da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
10711da177e4SLinus Torvalds 	int			bucket, sbucket, num, uid;
10721da177e4SLinus Torvalds 	struct seq_operations	seq_ops;
10731da177e4SLinus Torvalds };
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds extern int tcp_proc_register(struct tcp_seq_afinfo *afinfo);
10761da177e4SLinus Torvalds extern void tcp_proc_unregister(struct tcp_seq_afinfo *afinfo);
10771da177e4SLinus Torvalds 
107820380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
107920380731SArnaldo Carvalho de Melo 
108020380731SArnaldo Carvalho de Melo extern int tcp_v4_destroy_sock(struct sock *sk);
108120380731SArnaldo Carvalho de Melo 
108220380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
108320380731SArnaldo Carvalho de Melo extern int  tcp4_proc_init(void);
108420380731SArnaldo Carvalho de Melo extern void tcp4_proc_exit(void);
108520380731SArnaldo Carvalho de Melo #endif
108620380731SArnaldo Carvalho de Melo 
108720380731SArnaldo Carvalho de Melo extern void tcp_v4_init(struct net_proto_family *ops);
108820380731SArnaldo Carvalho de Melo extern void tcp_init(void);
108920380731SArnaldo Carvalho de Melo 
10901da177e4SLinus Torvalds #endif	/* _TCP_H */
1091