xref: /linux/include/net/tcp.h (revision e6b4d11367519bc71729c09d05a126b133c755be)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Definitions for the TCP module.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Version:	@(#)tcp.h	1.0.5	05/23/93
91da177e4SLinus Torvalds  *
1002c30a84SJesper Juhl  * Authors:	Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
141da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
151da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
161da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
171da177e4SLinus Torvalds  */
181da177e4SLinus Torvalds #ifndef _TCP_H
191da177e4SLinus Torvalds #define _TCP_H
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #define TCP_DEBUG 1
221da177e4SLinus Torvalds #define FASTRETRANS_DEBUG 1
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <linux/list.h>
251da177e4SLinus Torvalds #include <linux/tcp.h>
261da177e4SLinus Torvalds #include <linux/slab.h>
271da177e4SLinus Torvalds #include <linux/cache.h>
281da177e4SLinus Torvalds #include <linux/percpu.h>
29fb286bb2SHerbert Xu #include <linux/skbuff.h>
3097fc2f08SChris Leech #include <linux/dmaengine.h>
31cfb6eeb4SYOSHIFUJI Hideaki #include <linux/crypto.h>
32c6aefafbSGlenn Griffin #include <linux/cryptohash.h>
333f421baaSArnaldo Carvalho de Melo 
343f421baaSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h>
35295ff7edSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h>
3677d8bf9cSArnaldo Carvalho de Melo #include <net/inet_hashtables.h>
371da177e4SLinus Torvalds #include <net/checksum.h>
382e6599cbSArnaldo Carvalho de Melo #include <net/request_sock.h>
391da177e4SLinus Torvalds #include <net/sock.h>
401da177e4SLinus Torvalds #include <net/snmp.h>
411da177e4SLinus Torvalds #include <net/ip.h>
42c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
43bdf1ee5dSIlpo Järvinen #include <net/inet_ecn.h>
440c266898SSatoru SATOH #include <net/dst.h>
45c752f073SArnaldo Carvalho de Melo 
461da177e4SLinus Torvalds #include <linux/seq_file.h>
471da177e4SLinus Torvalds 
480f7ff927SArnaldo Carvalho de Melo extern struct inet_hashinfo tcp_hashinfo;
491da177e4SLinus Torvalds 
50dd24c001SEric Dumazet extern struct percpu_counter tcp_orphan_count;
511da177e4SLinus Torvalds extern void tcp_time_wait(struct sock *sk, int state, int timeo);
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #define MAX_TCP_HEADER	(128 + MAX_HEADER)
5433ad798cSAdam Langley #define MAX_TCP_OPTION_SPACE 40
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds  * Never offer a window over 32767 without using window scaling. Some
581da177e4SLinus Torvalds  * poor stacks do signed 16bit maths!
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds #define MAX_TCP_WINDOW		32767U
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */
631da177e4SLinus Torvalds #define TCP_MIN_MSS		88U
641da177e4SLinus Torvalds 
655d424d5aSJohn Heffner /* The least MTU to use for probing */
665d424d5aSJohn Heffner #define TCP_BASE_MSS		512
675d424d5aSJohn Heffner 
681da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */
691da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* Maximal reordering. */
721da177e4SLinus Torvalds #define TCP_MAX_REORDERING	127
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */
751da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS	16U
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds /* urg_data states */
781da177e4SLinus Torvalds #define TCP_URG_VALID	0x0100
791da177e4SLinus Torvalds #define TCP_URG_NOTYET	0x0200
801da177e4SLinus Torvalds #define TCP_URG_READ	0x0400
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds #define TCP_RETR1	3	/*
831da177e4SLinus Torvalds 				 * This is how many retries it does before it
841da177e4SLinus Torvalds 				 * tries to figure out if the gateway is
851da177e4SLinus Torvalds 				 * down. Minimal RFC value is 3; it corresponds
861da177e4SLinus Torvalds 				 * to ~3sec-8min depending on RTO.
871da177e4SLinus Torvalds 				 */
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #define TCP_RETR2	15	/*
901da177e4SLinus Torvalds 				 * This should take at least
911da177e4SLinus Torvalds 				 * 90 minutes to time out.
921da177e4SLinus Torvalds 				 * RFC1122 says that the limit is 100 sec.
931da177e4SLinus Torvalds 				 * 15 is ~13-30min depending on RTO.
941da177e4SLinus Torvalds 				 */
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds #define TCP_SYN_RETRIES	 5	/* number of times to retry active opening a
97caa20d9aSStephen Hemminger 				 * connection: ~180sec is RFC minimum	*/
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds #define TCP_SYNACK_RETRIES 5	/* number of times to retry passive opening a
100caa20d9aSStephen Hemminger 				 * connection: ~180sec is RFC minimum	*/
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds #define TCP_ORPHAN_RETRIES 7	/* number of times to retry on an orphaned
1041da177e4SLinus Torvalds 				 * socket. 7 is ~50sec-16min.
1051da177e4SLinus Torvalds 				 */
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
1091da177e4SLinus Torvalds 				  * state, about 60 seconds	*/
1101da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT	TCP_TIMEWAIT_LEN
1111da177e4SLinus Torvalds                                  /* BSD style FIN_WAIT2 deadlock breaker.
1121da177e4SLinus Torvalds 				  * It used to be 3min, new value is 60sec,
1131da177e4SLinus Torvalds 				  * to combine FIN-WAIT-2 timeout with
1141da177e4SLinus Torvalds 				  * TIME-WAIT timer.
1151da177e4SLinus Torvalds 				  */
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #define TCP_DELACK_MAX	((unsigned)(HZ/5))	/* maximal time to delay before sending an ACK */
1181da177e4SLinus Torvalds #if HZ >= 100
1191da177e4SLinus Torvalds #define TCP_DELACK_MIN	((unsigned)(HZ/25))	/* minimal time to delay before sending an ACK */
1201da177e4SLinus Torvalds #define TCP_ATO_MIN	((unsigned)(HZ/25))
1211da177e4SLinus Torvalds #else
1221da177e4SLinus Torvalds #define TCP_DELACK_MIN	4U
1231da177e4SLinus Torvalds #define TCP_ATO_MIN	4U
1241da177e4SLinus Torvalds #endif
1251da177e4SLinus Torvalds #define TCP_RTO_MAX	((unsigned)(120*HZ))
1261da177e4SLinus Torvalds #define TCP_RTO_MIN	((unsigned)(HZ/5))
1271da177e4SLinus Torvalds #define TCP_TIMEOUT_INIT ((unsigned)(3*HZ))	/* RFC 1122 initial RTO value	*/
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes
1301da177e4SLinus Torvalds 					                 * for local resources.
1311da177e4SLinus Torvalds 					                 */
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME	(120*60*HZ)	/* two hours */
1341da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES	9		/* Max of 9 keepalive probes	*/
1351da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL	(75*HZ)
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE	32767
1381da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL	32767
1391da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT		127
1401da177e4SLinus Torvalds #define MAX_TCP_SYNCNT		127
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL	(HZ/5)	/* Period of SYNACK timer */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds #define TCP_PAWS_24DAYS	(60 * 60 * 24 * 24)
1451da177e4SLinus Torvalds #define TCP_PAWS_MSL	60		/* Per-host timestamps are invalidated
1461da177e4SLinus Torvalds 					 * after this time. It should be equal
1471da177e4SLinus Torvalds 					 * (or greater than) TCP_TIMEWAIT_LEN
1481da177e4SLinus Torvalds 					 * to provide reliability equal to one
1491da177e4SLinus Torvalds 					 * provided by timewait state.
1501da177e4SLinus Torvalds 					 */
1511da177e4SLinus Torvalds #define TCP_PAWS_WINDOW	1		/* Replay window for per-host
1521da177e4SLinus Torvalds 					 * timestamps. It must be less than
1531da177e4SLinus Torvalds 					 * minimal timewait lifetime.
1541da177e4SLinus Torvalds 					 */
1551da177e4SLinus Torvalds /*
1561da177e4SLinus Torvalds  *	TCP option
1571da177e4SLinus Torvalds  */
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds #define TCPOPT_NOP		1	/* Padding */
1601da177e4SLinus Torvalds #define TCPOPT_EOL		0	/* End of options */
1611da177e4SLinus Torvalds #define TCPOPT_MSS		2	/* Segment size negotiating */
1621da177e4SLinus Torvalds #define TCPOPT_WINDOW		3	/* Window scaling */
1631da177e4SLinus Torvalds #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
1641da177e4SLinus Torvalds #define TCPOPT_SACK             5       /* SACK Block */
1651da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP	8	/* Better RTT estimations/PAWS */
166cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG		19	/* MD5 Signature (RFC2385) */
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds /*
1691da177e4SLinus Torvalds  *     TCP option lengths
1701da177e4SLinus Torvalds  */
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds #define TCPOLEN_MSS            4
1731da177e4SLinus Torvalds #define TCPOLEN_WINDOW         3
1741da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM      2
1751da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP      10
176cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG         18
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* But this is what stacks really send out. */
1791da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED		12
1801da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED		4
1811da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED	4
1821da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE		2
1831da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED	4
1841da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK		8
185cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED		20
18633ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED		4
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /* Flags in tp->nonagle */
1891da177e4SLinus Torvalds #define TCP_NAGLE_OFF		1	/* Nagle's algo is disabled */
1901da177e4SLinus Torvalds #define TCP_NAGLE_CORK		2	/* Socket is corked	    */
191caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH		4	/* Cork is overridden for already queued data */
1921da177e4SLinus Torvalds 
193295ff7edSArnaldo Carvalho de Melo extern struct inet_timewait_death_row tcp_death_row;
194295ff7edSArnaldo Carvalho de Melo 
1951da177e4SLinus Torvalds /* sysctl variables for tcp */
1961da177e4SLinus Torvalds extern int sysctl_tcp_timestamps;
1971da177e4SLinus Torvalds extern int sysctl_tcp_window_scaling;
1981da177e4SLinus Torvalds extern int sysctl_tcp_sack;
1991da177e4SLinus Torvalds extern int sysctl_tcp_fin_timeout;
2001da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_time;
2011da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_probes;
2021da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_intvl;
2031da177e4SLinus Torvalds extern int sysctl_tcp_syn_retries;
2041da177e4SLinus Torvalds extern int sysctl_tcp_synack_retries;
2051da177e4SLinus Torvalds extern int sysctl_tcp_retries1;
2061da177e4SLinus Torvalds extern int sysctl_tcp_retries2;
2071da177e4SLinus Torvalds extern int sysctl_tcp_orphan_retries;
2081da177e4SLinus Torvalds extern int sysctl_tcp_syncookies;
2091da177e4SLinus Torvalds extern int sysctl_tcp_retrans_collapse;
2101da177e4SLinus Torvalds extern int sysctl_tcp_stdurg;
2111da177e4SLinus Torvalds extern int sysctl_tcp_rfc1337;
2121da177e4SLinus Torvalds extern int sysctl_tcp_abort_on_overflow;
2131da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans;
2141da177e4SLinus Torvalds extern int sysctl_tcp_fack;
2151da177e4SLinus Torvalds extern int sysctl_tcp_reordering;
2161da177e4SLinus Torvalds extern int sysctl_tcp_ecn;
2171da177e4SLinus Torvalds extern int sysctl_tcp_dsack;
2181da177e4SLinus Torvalds extern int sysctl_tcp_mem[3];
2191da177e4SLinus Torvalds extern int sysctl_tcp_wmem[3];
2201da177e4SLinus Torvalds extern int sysctl_tcp_rmem[3];
2211da177e4SLinus Torvalds extern int sysctl_tcp_app_win;
2221da177e4SLinus Torvalds extern int sysctl_tcp_adv_win_scale;
2231da177e4SLinus Torvalds extern int sysctl_tcp_tw_reuse;
2241da177e4SLinus Torvalds extern int sysctl_tcp_frto;
2253cfe3baaSIlpo Järvinen extern int sysctl_tcp_frto_response;
2261da177e4SLinus Torvalds extern int sysctl_tcp_low_latency;
22795937825SChris Leech extern int sysctl_tcp_dma_copybreak;
2281da177e4SLinus Torvalds extern int sysctl_tcp_nometrics_save;
2291da177e4SLinus Torvalds extern int sysctl_tcp_moderate_rcvbuf;
2301da177e4SLinus Torvalds extern int sysctl_tcp_tso_win_divisor;
2319772efb9SStephen Hemminger extern int sysctl_tcp_abc;
2325d424d5aSJohn Heffner extern int sysctl_tcp_mtu_probing;
2335d424d5aSJohn Heffner extern int sysctl_tcp_base_mss;
23415d99e02SRick Jones extern int sysctl_tcp_workaround_signed_windows;
23535089bb2SDavid S. Miller extern int sysctl_tcp_slow_start_after_idle;
236886236c1SJohn Heffner extern int sysctl_tcp_max_ssthresh;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds extern atomic_t tcp_memory_allocated;
2391748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated;
2401da177e4SLinus Torvalds extern int tcp_memory_pressure;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds /*
2431da177e4SLinus Torvalds  * The next routines deal with comparing 32 bit unsigned ints
2441da177e4SLinus Torvalds  * and worry about wraparound (automatic with unsigned arithmetic).
2451da177e4SLinus Torvalds  */
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds static inline int before(__u32 seq1, __u32 seq2)
2481da177e4SLinus Torvalds {
2490d630cc0SGerrit Renker         return (__s32)(seq1-seq2) < 0;
2501da177e4SLinus Torvalds }
2519a036b9cSGerrit Renker #define after(seq2, seq1) 	before(seq1, seq2)
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */
2541da177e4SLinus Torvalds static inline int between(__u32 seq1, __u32 seq2, __u32 seq3)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	return seq3 - seq2 >= seq1 - seq2;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
259e4fd5da3SPavel Emelianov static inline int tcp_too_many_orphans(struct sock *sk, int num)
260e4fd5da3SPavel Emelianov {
261e4fd5da3SPavel Emelianov 	return (num > sysctl_tcp_max_orphans) ||
262e4fd5da3SPavel Emelianov 		(sk->sk_wmem_queued > SOCK_MIN_SNDBUF &&
263e4fd5da3SPavel Emelianov 		 atomic_read(&tcp_memory_allocated) > sysctl_tcp_mem[2]);
264e4fd5da3SPavel Emelianov }
2651da177e4SLinus Torvalds 
266a0f82f64SFlorian Westphal /* syncookies: remember time of last synqueue overflow */
267a0f82f64SFlorian Westphal static inline void tcp_synq_overflow(struct sock *sk)
268a0f82f64SFlorian Westphal {
269a0f82f64SFlorian Westphal 	tcp_sk(sk)->rx_opt.ts_recent_stamp = jiffies;
270a0f82f64SFlorian Westphal }
271a0f82f64SFlorian Westphal 
272a0f82f64SFlorian Westphal /* syncookies: no recent synqueue overflow on this listening socket? */
273a0f82f64SFlorian Westphal static inline int tcp_synq_no_recent_overflow(const struct sock *sk)
274a0f82f64SFlorian Westphal {
275a0f82f64SFlorian Westphal 	unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp;
276a0f82f64SFlorian Westphal 	return time_after(jiffies, last_overflow + TCP_TIMEOUT_INIT);
277a0f82f64SFlorian Westphal }
278a0f82f64SFlorian Westphal 
2791da177e4SLinus Torvalds extern struct proto tcp_prot;
2801da177e4SLinus Torvalds 
28157ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field)	SNMP_INC_STATS((net)->mib.tcp_statistics, field)
28257ef42d5SPavel Emelyanov #define TCP_INC_STATS_BH(net, field)	SNMP_INC_STATS_BH((net)->mib.tcp_statistics, field)
28357ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field)	SNMP_DEC_STATS((net)->mib.tcp_statistics, field)
28457ef42d5SPavel Emelyanov #define TCP_ADD_STATS_USER(net, field, val) SNMP_ADD_STATS_USER((net)->mib.tcp_statistics, field, val)
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds extern void			tcp_v4_err(struct sk_buff *skb, u32);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds extern void			tcp_shutdown (struct sock *sk, int how);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds extern int			tcp_v4_rcv(struct sk_buff *skb);
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds extern int			tcp_v4_remember_stamp(struct sock *sk);
2931da177e4SLinus Torvalds 
2948feaf0c0SArnaldo Carvalho de Melo extern int		    	tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw);
2951da177e4SLinus Torvalds 
2963516ffb0SDavid S. Miller extern int			tcp_sendmsg(struct kiocb *iocb, struct socket *sock,
2971da177e4SLinus Torvalds 					    struct msghdr *msg, size_t size);
2981da177e4SLinus Torvalds extern ssize_t			tcp_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags);
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds extern int			tcp_ioctl(struct sock *sk,
3011da177e4SLinus Torvalds 					  int cmd,
3021da177e4SLinus Torvalds 					  unsigned long arg);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds extern int			tcp_rcv_state_process(struct sock *sk,
3051da177e4SLinus Torvalds 						      struct sk_buff *skb,
3061da177e4SLinus Torvalds 						      struct tcphdr *th,
3071da177e4SLinus Torvalds 						      unsigned len);
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds extern int			tcp_rcv_established(struct sock *sk,
3101da177e4SLinus Torvalds 						    struct sk_buff *skb,
3111da177e4SLinus Torvalds 						    struct tcphdr *th,
3121da177e4SLinus Torvalds 						    unsigned len);
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds extern void			tcp_rcv_space_adjust(struct sock *sk);
3151da177e4SLinus Torvalds 
3160e4b4992SChris Leech extern void			tcp_cleanup_rbuf(struct sock *sk, int copied);
3170e4b4992SChris Leech 
3186d6ee43eSArnaldo Carvalho de Melo extern int			tcp_twsk_unique(struct sock *sk,
3196d6ee43eSArnaldo Carvalho de Melo 						struct sock *sktw, void *twp);
3206d6ee43eSArnaldo Carvalho de Melo 
321cfb6eeb4SYOSHIFUJI Hideaki extern void			tcp_twsk_destructor(struct sock *sk);
322cfb6eeb4SYOSHIFUJI Hideaki 
3239c55e01cSJens Axboe extern ssize_t			tcp_splice_read(struct socket *sk, loff_t *ppos,
3249c55e01cSJens Axboe 					        struct pipe_inode_info *pipe, size_t len, unsigned int flags);
3259c55e01cSJens Axboe 
326463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk,
327463c84b9SArnaldo Carvalho de Melo 					 const unsigned int pkts)
3281da177e4SLinus Torvalds {
329463c84b9SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
330fc6415bcSDavid S. Miller 
331463c84b9SArnaldo Carvalho de Melo 	if (icsk->icsk_ack.quick) {
332463c84b9SArnaldo Carvalho de Melo 		if (pkts >= icsk->icsk_ack.quick) {
333463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick = 0;
3341da177e4SLinus Torvalds 			/* Leaving quickack mode we deflate ATO. */
335463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.ato   = TCP_ATO_MIN;
336fc6415bcSDavid S. Miller 		} else
337463c84b9SArnaldo Carvalho de Melo 			icsk->icsk_ack.quick -= pkts;
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
341463c84b9SArnaldo Carvalho de Melo extern void tcp_enter_quickack_mode(struct sock *sk);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds static inline void tcp_clear_options(struct tcp_options_received *rx_opt)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds  	rx_opt->tstamp_ok = rx_opt->sack_ok = rx_opt->wscale_ok = rx_opt->snd_wscale = 0;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
348bdf1ee5dSIlpo Järvinen #define	TCP_ECN_OK		1
349bdf1ee5dSIlpo Järvinen #define	TCP_ECN_QUEUE_CWR	2
350bdf1ee5dSIlpo Järvinen #define	TCP_ECN_DEMAND_CWR	4
351bdf1ee5dSIlpo Järvinen 
352bdf1ee5dSIlpo Järvinen static __inline__ void
353bdf1ee5dSIlpo Järvinen TCP_ECN_create_request(struct request_sock *req, struct tcphdr *th)
354bdf1ee5dSIlpo Järvinen {
355bdf1ee5dSIlpo Järvinen 	if (sysctl_tcp_ecn && th->ece && th->cwr)
356bdf1ee5dSIlpo Järvinen 		inet_rsk(req)->ecn_ok = 1;
357bdf1ee5dSIlpo Järvinen }
358bdf1ee5dSIlpo Järvinen 
359fd2c3ef7SEric Dumazet enum tcp_tw_status {
3601da177e4SLinus Torvalds 	TCP_TW_SUCCESS = 0,
3611da177e4SLinus Torvalds 	TCP_TW_RST = 1,
3621da177e4SLinus Torvalds 	TCP_TW_ACK = 2,
3631da177e4SLinus Torvalds 	TCP_TW_SYN = 3
3641da177e4SLinus Torvalds };
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 
3678feaf0c0SArnaldo Carvalho de Melo extern enum tcp_tw_status	tcp_timewait_state_process(struct inet_timewait_sock *tw,
3681da177e4SLinus Torvalds 							   struct sk_buff *skb,
3698feaf0c0SArnaldo Carvalho de Melo 							   const struct tcphdr *th);
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds extern struct sock *		tcp_check_req(struct sock *sk,struct sk_buff *skb,
37260236fddSArnaldo Carvalho de Melo 					      struct request_sock *req,
37360236fddSArnaldo Carvalho de Melo 					      struct request_sock **prev);
3741da177e4SLinus Torvalds extern int			tcp_child_process(struct sock *parent,
3751da177e4SLinus Torvalds 						  struct sock *child,
3761da177e4SLinus Torvalds 						  struct sk_buff *skb);
37746d0de4eSIlpo Järvinen extern int			tcp_use_frto(struct sock *sk);
3781da177e4SLinus Torvalds extern void			tcp_enter_frto(struct sock *sk);
3791da177e4SLinus Torvalds extern void			tcp_enter_loss(struct sock *sk, int how);
3801da177e4SLinus Torvalds extern void			tcp_clear_retrans(struct tcp_sock *tp);
3811da177e4SLinus Torvalds extern void			tcp_update_metrics(struct sock *sk);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds extern void			tcp_close(struct sock *sk,
3841da177e4SLinus Torvalds 					  long timeout);
3851da177e4SLinus Torvalds extern unsigned int		tcp_poll(struct file * file, struct socket *sock, struct poll_table_struct *wait);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds extern int			tcp_getsockopt(struct sock *sk, int level,
3881da177e4SLinus Torvalds 					       int optname,
3891da177e4SLinus Torvalds 					       char __user *optval,
3901da177e4SLinus Torvalds 					       int __user *optlen);
3911da177e4SLinus Torvalds extern int			tcp_setsockopt(struct sock *sk, int level,
3921da177e4SLinus Torvalds 					       int optname, char __user *optval,
393b7058842SDavid S. Miller 					       unsigned int optlen);
3943fdadf7dSDmitry Mishin extern int			compat_tcp_getsockopt(struct sock *sk,
3953fdadf7dSDmitry Mishin 					int level, int optname,
3963fdadf7dSDmitry Mishin 					char __user *optval, int __user *optlen);
3973fdadf7dSDmitry Mishin extern int			compat_tcp_setsockopt(struct sock *sk,
3983fdadf7dSDmitry Mishin 					int level, int optname,
399b7058842SDavid S. Miller 					char __user *optval, unsigned int optlen);
4001da177e4SLinus Torvalds extern void			tcp_set_keepalive(struct sock *sk, int val);
4011da177e4SLinus Torvalds extern int			tcp_recvmsg(struct kiocb *iocb, struct sock *sk,
4021da177e4SLinus Torvalds 					    struct msghdr *msg,
4031da177e4SLinus Torvalds 					    size_t len, int nonblock,
4041da177e4SLinus Torvalds 					    int flags, int *addr_len);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds extern void			tcp_parse_options(struct sk_buff *skb,
4071da177e4SLinus Torvalds 						  struct tcp_options_received *opt_rx,
408022c3f7dSGilad Ben-Yossef 						  int estab,
409022c3f7dSGilad Ben-Yossef 						  struct dst_entry *dst);
4101da177e4SLinus Torvalds 
4117d5d5525SYOSHIFUJI Hideaki extern u8			*tcp_parse_md5sig_option(struct tcphdr *th);
4127d5d5525SYOSHIFUJI Hideaki 
4131da177e4SLinus Torvalds /*
4141da177e4SLinus Torvalds  *	TCP v4 functions exported for the inet6 API
4151da177e4SLinus Torvalds  */
4161da177e4SLinus Torvalds 
4178292a17aSArnaldo Carvalho de Melo extern void		       	tcp_v4_send_check(struct sock *sk, int len,
4181da177e4SLinus Torvalds 						  struct sk_buff *skb);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds extern int			tcp_v4_conn_request(struct sock *sk,
4211da177e4SLinus Torvalds 						    struct sk_buff *skb);
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds extern struct sock *		tcp_create_openreq_child(struct sock *sk,
42460236fddSArnaldo Carvalho de Melo 							 struct request_sock *req,
4251da177e4SLinus Torvalds 							 struct sk_buff *skb);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds extern struct sock *		tcp_v4_syn_recv_sock(struct sock *sk,
4281da177e4SLinus Torvalds 						     struct sk_buff *skb,
42960236fddSArnaldo Carvalho de Melo 						     struct request_sock *req,
4301da177e4SLinus Torvalds 							struct dst_entry *dst);
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds extern int			tcp_v4_do_rcv(struct sock *sk,
4331da177e4SLinus Torvalds 					      struct sk_buff *skb);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds extern int			tcp_v4_connect(struct sock *sk,
4361da177e4SLinus Torvalds 					       struct sockaddr *uaddr,
4371da177e4SLinus Torvalds 					       int addr_len);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds extern int			tcp_connect(struct sock *sk);
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds extern struct sk_buff *		tcp_make_synack(struct sock *sk,
4421da177e4SLinus Torvalds 						struct dst_entry *dst,
443*e6b4d113SWilliam Allen Simpson 						struct request_sock *req,
444*e6b4d113SWilliam Allen Simpson 						struct request_values *rvp);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds extern int			tcp_disconnect(struct sock *sk, int flags);
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds /* From syncookies.c */
4502051f11fSFlorian Westphal extern __u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS];
4511da177e4SLinus Torvalds extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
4521da177e4SLinus Torvalds 				    struct ip_options *opt);
4531da177e4SLinus Torvalds extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb,
4541da177e4SLinus Torvalds 				     __u16 *mss);
4551da177e4SLinus Torvalds 
4564dfc2817SFlorian Westphal extern __u32 cookie_init_timestamp(struct request_sock *req);
4574dfc2817SFlorian Westphal extern void cookie_check_timestamp(struct tcp_options_received *tcp_opt);
4584dfc2817SFlorian Westphal 
459c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */
460c6aefafbSGlenn Griffin extern struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb);
461c6aefafbSGlenn Griffin extern __u32 cookie_v6_init_sequence(struct sock *sk, struct sk_buff *skb,
462c6aefafbSGlenn Griffin 				     __u16 *mss);
463c6aefafbSGlenn Griffin 
4641da177e4SLinus Torvalds /* tcp_output.c */
4651da177e4SLinus Torvalds 
4669e412ba7SIlpo Järvinen extern void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss,
4679e412ba7SIlpo Järvinen 				      int nonagle);
4689e412ba7SIlpo Järvinen extern int tcp_may_send_now(struct sock *sk);
4691da177e4SLinus Torvalds extern int tcp_retransmit_skb(struct sock *, struct sk_buff *);
470f1ecd5d9SDamian Lukowski extern void tcp_retransmit_timer(struct sock *sk);
4711da177e4SLinus Torvalds extern void tcp_xmit_retransmit_queue(struct sock *);
4721da177e4SLinus Torvalds extern void tcp_simple_retransmit(struct sock *);
4731da177e4SLinus Torvalds extern int tcp_trim_head(struct sock *, struct sk_buff *, u32);
4746475be16SDavid S. Miller extern int tcp_fragment(struct sock *, struct sk_buff *, u32, unsigned int);
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds extern void tcp_send_probe0(struct sock *);
4771da177e4SLinus Torvalds extern void tcp_send_partial(struct sock *);
4781da177e4SLinus Torvalds extern int  tcp_write_wakeup(struct sock *);
4791da177e4SLinus Torvalds extern void tcp_send_fin(struct sock *sk);
480dd0fc66fSAl Viro extern void tcp_send_active_reset(struct sock *sk, gfp_t priority);
4811da177e4SLinus Torvalds extern int  tcp_send_synack(struct sock *);
482c1b4a7e6SDavid S. Miller extern void tcp_push_one(struct sock *, unsigned int mss_now);
4831da177e4SLinus Torvalds extern void tcp_send_ack(struct sock *sk);
4841da177e4SLinus Torvalds extern void tcp_send_delayed_ack(struct sock *sk);
4851da177e4SLinus Torvalds 
486a762a980SDavid S. Miller /* tcp_input.c */
487a762a980SDavid S. Miller extern void tcp_cwnd_application_limited(struct sock *sk);
488a762a980SDavid S. Miller 
4891da177e4SLinus Torvalds /* tcp_timer.c */
4901da177e4SLinus Torvalds extern void tcp_init_xmit_timers(struct sock *);
491463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk)
492463c84b9SArnaldo Carvalho de Melo {
493463c84b9SArnaldo Carvalho de Melo 	inet_csk_clear_xmit_timers(sk);
494463c84b9SArnaldo Carvalho de Melo }
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds extern unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu);
4970c54b85fSIlpo Järvinen extern unsigned int tcp_current_mss(struct sock *sk);
4980c54b85fSIlpo Järvinen 
4990c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */
5000c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize)
5010c54b85fSIlpo Järvinen {
5020c54b85fSIlpo Järvinen 	if (tp->max_window && pktsize > (tp->max_window >> 1))
5030c54b85fSIlpo Järvinen 		return max(tp->max_window >> 1, 68U - tp->tcp_header_len);
5040c54b85fSIlpo Järvinen 	else
5050c54b85fSIlpo Järvinen 		return pktsize;
5060c54b85fSIlpo Järvinen }
5071da177e4SLinus Torvalds 
50817b085eaSArnaldo Carvalho de Melo /* tcp.c */
5091da177e4SLinus Torvalds extern void tcp_get_info(struct sock *, struct tcp_info *);
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */
5121da177e4SLinus Torvalds typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *,
5131da177e4SLinus Torvalds 				unsigned int, size_t);
5141da177e4SLinus Torvalds extern int tcp_read_sock(struct sock *sk, read_descriptor_t *desc,
5151da177e4SLinus Torvalds 			 sk_read_actor_t recv_actor);
5161da177e4SLinus Torvalds 
51740efc6faSStephen Hemminger extern void tcp_initialize_rcv_mss(struct sock *sk);
5181da177e4SLinus Torvalds 
5195d424d5aSJohn Heffner extern int tcp_mtu_to_mss(struct sock *sk, int pmtu);
5205d424d5aSJohn Heffner extern int tcp_mss_to_mtu(struct sock *sk, int mss);
5215d424d5aSJohn Heffner extern void tcp_mtup_init(struct sock *sk);
5225d424d5aSJohn Heffner 
523f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk)
524f1ecd5d9SDamian Lukowski {
525f1ecd5d9SDamian Lukowski 	if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX)
526f1ecd5d9SDamian Lukowski 		inet_csk(sk)->icsk_rto = TCP_RTO_MAX;
527f1ecd5d9SDamian Lukowski }
528f1ecd5d9SDamian Lukowski 
529f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp)
530f1ecd5d9SDamian Lukowski {
531f1ecd5d9SDamian Lukowski 	return (tp->srtt >> 3) + tp->rttvar;
532f1ecd5d9SDamian Lukowski }
533f1ecd5d9SDamian Lukowski 
53440efc6faSStephen Hemminger static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd)
5351da177e4SLinus Torvalds {
5361da177e4SLinus Torvalds 	tp->pred_flags = htonl((tp->tcp_header_len << 26) |
5371da177e4SLinus Torvalds 			       ntohl(TCP_FLAG_ACK) |
5381da177e4SLinus Torvalds 			       snd_wnd);
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
54140efc6faSStephen Hemminger static inline void tcp_fast_path_on(struct tcp_sock *tp)
5421da177e4SLinus Torvalds {
5431da177e4SLinus Torvalds 	__tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale);
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
5469e412ba7SIlpo Järvinen static inline void tcp_fast_path_check(struct sock *sk)
5471da177e4SLinus Torvalds {
5489e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
5499e412ba7SIlpo Järvinen 
550b03efcfbSDavid S. Miller 	if (skb_queue_empty(&tp->out_of_order_queue) &&
5511da177e4SLinus Torvalds 	    tp->rcv_wnd &&
5521da177e4SLinus Torvalds 	    atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf &&
5531da177e4SLinus Torvalds 	    !tp->urg_data)
5541da177e4SLinus Torvalds 		tcp_fast_path_on(tp);
5551da177e4SLinus Torvalds }
5561da177e4SLinus Torvalds 
5570c266898SSatoru SATOH /* Compute the actual rto_min value */
5580c266898SSatoru SATOH static inline u32 tcp_rto_min(struct sock *sk)
5590c266898SSatoru SATOH {
5600c266898SSatoru SATOH 	struct dst_entry *dst = __sk_dst_get(sk);
5610c266898SSatoru SATOH 	u32 rto_min = TCP_RTO_MIN;
5620c266898SSatoru SATOH 
5630c266898SSatoru SATOH 	if (dst && dst_metric_locked(dst, RTAX_RTO_MIN))
5640c266898SSatoru SATOH 		rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN);
5650c266898SSatoru SATOH 	return rto_min;
5660c266898SSatoru SATOH }
5670c266898SSatoru SATOH 
5681da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising.
5691da177e4SLinus Torvalds  * Rcv_nxt can be after the window if our peer push more data
5701da177e4SLinus Torvalds  * than the offered window.
5711da177e4SLinus Torvalds  */
57240efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	if (win < 0)
5771da177e4SLinus Torvalds 		win = 0;
5781da177e4SLinus Torvalds 	return (u32) win;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without
5821da177e4SLinus Torvalds  * scaling applied to the result.  The caller does these things
5831da177e4SLinus Torvalds  * if necessary.  This is a "raw" window selection.
5841da177e4SLinus Torvalds  */
5851da177e4SLinus Torvalds extern u32	__tcp_select_window(struct sock *sk);
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds /* TCP timestamps are only 32-bits, this causes a slight
5881da177e4SLinus Torvalds  * complication on 64-bit systems since we store a snapshot
58931f34269SStephen Hemminger  * of jiffies in the buffer control blocks below.  We decided
59031f34269SStephen Hemminger  * to use only the low 32-bits of jiffies and hide the ugly
5911da177e4SLinus Torvalds  * casts with the following macro.
5921da177e4SLinus Torvalds  */
5931da177e4SLinus Torvalds #define tcp_time_stamp		((__u32)(jiffies))
5941da177e4SLinus Torvalds 
595caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass
5961da177e4SLinus Torvalds  * TCP per-packet control information to the transmission
5971da177e4SLinus Torvalds  * code.  We also store the host-order sequence numbers in
5981da177e4SLinus Torvalds  * here too.  This is 36 bytes on 32-bit architectures,
5991da177e4SLinus Torvalds  * 40 bytes on 64-bit machines, if this grows please adjust
6001da177e4SLinus Torvalds  * skbuff.h:skbuff->cb[xxx] size appropriately.
6011da177e4SLinus Torvalds  */
6021da177e4SLinus Torvalds struct tcp_skb_cb {
6031da177e4SLinus Torvalds 	union {
6041da177e4SLinus Torvalds 		struct inet_skb_parm	h4;
6051da177e4SLinus Torvalds #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
6061da177e4SLinus Torvalds 		struct inet6_skb_parm	h6;
6071da177e4SLinus Torvalds #endif
6081da177e4SLinus Torvalds 	} header;	/* For incoming frames		*/
6091da177e4SLinus Torvalds 	__u32		seq;		/* Starting sequence number	*/
6101da177e4SLinus Torvalds 	__u32		end_seq;	/* SEQ + FIN + SYN + datalen	*/
6111da177e4SLinus Torvalds 	__u32		when;		/* used to compute rtt's	*/
6121da177e4SLinus Torvalds 	__u8		flags;		/* TCP header flags.		*/
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	/* NOTE: These must match up to the flags byte in a
6151da177e4SLinus Torvalds 	 *       real TCP header.
6161da177e4SLinus Torvalds 	 */
6171da177e4SLinus Torvalds #define TCPCB_FLAG_FIN		0x01
6181da177e4SLinus Torvalds #define TCPCB_FLAG_SYN		0x02
6191da177e4SLinus Torvalds #define TCPCB_FLAG_RST		0x04
6201da177e4SLinus Torvalds #define TCPCB_FLAG_PSH		0x08
6211da177e4SLinus Torvalds #define TCPCB_FLAG_ACK		0x10
6221da177e4SLinus Torvalds #define TCPCB_FLAG_URG		0x20
6231da177e4SLinus Torvalds #define TCPCB_FLAG_ECE		0x40
6241da177e4SLinus Torvalds #define TCPCB_FLAG_CWR		0x80
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	__u8		sacked;		/* State flags for SACK/FACK.	*/
6271da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED	0x01	/* SKB ACK'd by a SACK block	*/
6281da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS	0x02	/* SKB retransmitted		*/
6291da177e4SLinus Torvalds #define TCPCB_LOST		0x04	/* SKB is lost			*/
6301da177e4SLinus Torvalds #define TCPCB_TAGBITS		0x07	/* All tag bits			*/
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS	0x80	/* Ever retransmitted frame	*/
6331da177e4SLinus Torvalds #define TCPCB_RETRANS		(TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS)
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	__u32		ack_seq;	/* Sequence number ACK'd	*/
6361da177e4SLinus Torvalds };
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds #define TCP_SKB_CB(__skb)	((struct tcp_skb_cb *)&((__skb)->cb[0]))
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual
6411da177e4SLinus Torvalds  * packets.  To keep these tracked properly, we use this.
6421da177e4SLinus Torvalds  */
6431da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb)
6441da177e4SLinus Torvalds {
6457967168cSHerbert Xu 	return skb_shinfo(skb)->gso_segs;
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds /* This is valid iff tcp_skb_pcount() > 1. */
6491da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb)
6501da177e4SLinus Torvalds {
6517967168cSHerbert Xu 	return skb_shinfo(skb)->gso_size;
6521da177e4SLinus Torvalds }
6531da177e4SLinus Torvalds 
654317a76f9SStephen Hemminger /* Events passed to congestion control interface */
655317a76f9SStephen Hemminger enum tcp_ca_event {
656317a76f9SStephen Hemminger 	CA_EVENT_TX_START,	/* first transmit when no packets in flight */
657317a76f9SStephen Hemminger 	CA_EVENT_CWND_RESTART,	/* congestion window restart */
658317a76f9SStephen Hemminger 	CA_EVENT_COMPLETE_CWR,	/* end of congestion recovery */
659317a76f9SStephen Hemminger 	CA_EVENT_FRTO,		/* fast recovery timeout */
660317a76f9SStephen Hemminger 	CA_EVENT_LOSS,		/* loss timeout */
661317a76f9SStephen Hemminger 	CA_EVENT_FAST_ACK,	/* in sequence ack */
662317a76f9SStephen Hemminger 	CA_EVENT_SLOW_ACK,	/* other ack */
663317a76f9SStephen Hemminger };
664317a76f9SStephen Hemminger 
665317a76f9SStephen Hemminger /*
666317a76f9SStephen Hemminger  * Interface for adding new TCP congestion control handlers
667317a76f9SStephen Hemminger  */
668317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX	16
6693ff825b2SStephen Hemminger #define TCP_CA_MAX	128
6703ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX	(TCP_CA_NAME_MAX*TCP_CA_MAX)
6713ff825b2SStephen Hemminger 
672164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1
673164891aaSStephen Hemminger #define TCP_CONG_RTT_STAMP	0x2
674164891aaSStephen Hemminger 
675317a76f9SStephen Hemminger struct tcp_congestion_ops {
676317a76f9SStephen Hemminger 	struct list_head	list;
677164891aaSStephen Hemminger 	unsigned long flags;
678317a76f9SStephen Hemminger 
679317a76f9SStephen Hemminger 	/* initialize private data (optional) */
6806687e988SArnaldo Carvalho de Melo 	void (*init)(struct sock *sk);
681317a76f9SStephen Hemminger 	/* cleanup private data  (optional) */
6826687e988SArnaldo Carvalho de Melo 	void (*release)(struct sock *sk);
683317a76f9SStephen Hemminger 
684317a76f9SStephen Hemminger 	/* return slow start threshold (required) */
6856687e988SArnaldo Carvalho de Melo 	u32 (*ssthresh)(struct sock *sk);
686317a76f9SStephen Hemminger 	/* lower bound for congestion window (optional) */
68772dc5b92SStephen Hemminger 	u32 (*min_cwnd)(const struct sock *sk);
688317a76f9SStephen Hemminger 	/* do new cwnd calculation (required) */
689c3a05c60SIlpo Järvinen 	void (*cong_avoid)(struct sock *sk, u32 ack, u32 in_flight);
690317a76f9SStephen Hemminger 	/* call before changing ca_state (optional) */
6916687e988SArnaldo Carvalho de Melo 	void (*set_state)(struct sock *sk, u8 new_state);
692317a76f9SStephen Hemminger 	/* call when cwnd event occurs (optional) */
6936687e988SArnaldo Carvalho de Melo 	void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev);
694317a76f9SStephen Hemminger 	/* new value of cwnd after loss (optional) */
6956687e988SArnaldo Carvalho de Melo 	u32  (*undo_cwnd)(struct sock *sk);
696317a76f9SStephen Hemminger 	/* hook for packet ack accounting (optional) */
69730cfd0baSStephen Hemminger 	void (*pkts_acked)(struct sock *sk, u32 num_acked, s32 rtt_us);
69873c1f4a0SArnaldo Carvalho de Melo 	/* get info for inet_diag (optional) */
6996687e988SArnaldo Carvalho de Melo 	void (*get_info)(struct sock *sk, u32 ext, struct sk_buff *skb);
700317a76f9SStephen Hemminger 
701317a76f9SStephen Hemminger 	char 		name[TCP_CA_NAME_MAX];
702317a76f9SStephen Hemminger 	struct module 	*owner;
703317a76f9SStephen Hemminger };
704317a76f9SStephen Hemminger 
705317a76f9SStephen Hemminger extern int tcp_register_congestion_control(struct tcp_congestion_ops *type);
706317a76f9SStephen Hemminger extern void tcp_unregister_congestion_control(struct tcp_congestion_ops *type);
707317a76f9SStephen Hemminger 
7086687e988SArnaldo Carvalho de Melo extern void tcp_init_congestion_control(struct sock *sk);
7096687e988SArnaldo Carvalho de Melo extern void tcp_cleanup_congestion_control(struct sock *sk);
710317a76f9SStephen Hemminger extern int tcp_set_default_congestion_control(const char *name);
711317a76f9SStephen Hemminger extern void tcp_get_default_congestion_control(char *name);
7123ff825b2SStephen Hemminger extern void tcp_get_available_congestion_control(char *buf, size_t len);
713ce7bc3bfSStephen Hemminger extern void tcp_get_allowed_congestion_control(char *buf, size_t len);
714ce7bc3bfSStephen Hemminger extern int tcp_set_allowed_congestion_control(char *allowed);
7156687e988SArnaldo Carvalho de Melo extern int tcp_set_congestion_control(struct sock *sk, const char *name);
71640efc6faSStephen Hemminger extern void tcp_slow_start(struct tcp_sock *tp);
717758ce5c8SIlpo Järvinen extern void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w);
718317a76f9SStephen Hemminger 
7195f8ef48dSStephen Hemminger extern struct tcp_congestion_ops tcp_init_congestion_ops;
7206687e988SArnaldo Carvalho de Melo extern u32 tcp_reno_ssthresh(struct sock *sk);
721c3a05c60SIlpo Järvinen extern void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight);
72272dc5b92SStephen Hemminger extern u32 tcp_reno_min_cwnd(const struct sock *sk);
723a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno;
724317a76f9SStephen Hemminger 
7256687e988SArnaldo Carvalho de Melo static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state)
726317a76f9SStephen Hemminger {
7276687e988SArnaldo Carvalho de Melo 	struct inet_connection_sock *icsk = inet_csk(sk);
7286687e988SArnaldo Carvalho de Melo 
7296687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->set_state)
7306687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->set_state(sk, ca_state);
7316687e988SArnaldo Carvalho de Melo 	icsk->icsk_ca_state = ca_state;
732317a76f9SStephen Hemminger }
733317a76f9SStephen Hemminger 
7346687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event)
735317a76f9SStephen Hemminger {
7366687e988SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
7376687e988SArnaldo Carvalho de Melo 
7386687e988SArnaldo Carvalho de Melo 	if (icsk->icsk_ca_ops->cwnd_event)
7396687e988SArnaldo Carvalho de Melo 		icsk->icsk_ca_ops->cwnd_event(sk, event);
740317a76f9SStephen Hemminger }
741317a76f9SStephen Hemminger 
742e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK
743e60402d0SIlpo Järvinen  * handling. SACK is negotiated with the peer, and therefore it can vary
744e60402d0SIlpo Järvinen  * between different flows.
745e60402d0SIlpo Järvinen  *
746e60402d0SIlpo Järvinen  * tcp_is_sack - SACK enabled
747e60402d0SIlpo Järvinen  * tcp_is_reno - No SACK
748e60402d0SIlpo Järvinen  * tcp_is_fack - FACK enabled, implies SACK enabled
749e60402d0SIlpo Järvinen  */
750e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp)
751e60402d0SIlpo Järvinen {
752e60402d0SIlpo Järvinen 	return tp->rx_opt.sack_ok;
753e60402d0SIlpo Järvinen }
754e60402d0SIlpo Järvinen 
755e60402d0SIlpo Järvinen static inline int tcp_is_reno(const struct tcp_sock *tp)
756e60402d0SIlpo Järvinen {
757e60402d0SIlpo Järvinen 	return !tcp_is_sack(tp);
758e60402d0SIlpo Järvinen }
759e60402d0SIlpo Järvinen 
760e60402d0SIlpo Järvinen static inline int tcp_is_fack(const struct tcp_sock *tp)
761e60402d0SIlpo Järvinen {
762e60402d0SIlpo Järvinen 	return tp->rx_opt.sack_ok & 2;
763e60402d0SIlpo Järvinen }
764e60402d0SIlpo Järvinen 
765e60402d0SIlpo Järvinen static inline void tcp_enable_fack(struct tcp_sock *tp)
766e60402d0SIlpo Järvinen {
767e60402d0SIlpo Järvinen 	tp->rx_opt.sack_ok |= 2;
768e60402d0SIlpo Järvinen }
769e60402d0SIlpo Järvinen 
77083ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp)
77183ae4088SIlpo Järvinen {
77283ae4088SIlpo Järvinen 	return tp->sacked_out + tp->lost_out;
77383ae4088SIlpo Järvinen }
77483ae4088SIlpo Järvinen 
7751da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best
7761da177e4SLinus Torvalds  * of our knowledge.  In many cases it is conservative, but where
7771da177e4SLinus Torvalds  * detailed information is available from the receiver (via SACK
7781da177e4SLinus Torvalds  * blocks etc.) we can make more aggressive calculations.
7791da177e4SLinus Torvalds  *
7801da177e4SLinus Torvalds  * Use this for decisions involving congestion control, use just
7811da177e4SLinus Torvalds  * tp->packets_out to determine if the send queue is empty or not.
7821da177e4SLinus Torvalds  *
7831da177e4SLinus Torvalds  * Read this equation as:
7841da177e4SLinus Torvalds  *
7851da177e4SLinus Torvalds  *	"Packets sent once on transmission queue" MINUS
7861da177e4SLinus Torvalds  *	"Packets left network, but not honestly ACKed yet" PLUS
7871da177e4SLinus Torvalds  *	"Packets fast retransmitted"
7881da177e4SLinus Torvalds  */
78940efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp)
7901da177e4SLinus Torvalds {
79183ae4088SIlpo Järvinen 	return tp->packets_out - tcp_left_out(tp) + tp->retrans_out;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds 
7940b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH	0x7fffffff
7950b6a05c1SIlpo Järvinen 
7960b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp)
7970b6a05c1SIlpo Järvinen {
7980b6a05c1SIlpo Järvinen 	return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH;
7990b6a05c1SIlpo Järvinen }
8000b6a05c1SIlpo Järvinen 
8011da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd.
8021da177e4SLinus Torvalds  * The exception is rate halving phase, when cwnd is decreasing towards
8031da177e4SLinus Torvalds  * ssthresh.
8041da177e4SLinus Torvalds  */
8056687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk)
8061da177e4SLinus Torvalds {
8076687e988SArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
8086687e988SArnaldo Carvalho de Melo 	if ((1 << inet_csk(sk)->icsk_ca_state) & (TCPF_CA_CWR | TCPF_CA_Recovery))
8091da177e4SLinus Torvalds 		return tp->snd_ssthresh;
8101da177e4SLinus Torvalds 	else
8111da177e4SLinus Torvalds 		return max(tp->snd_ssthresh,
8121da177e4SLinus Torvalds 			   ((tp->snd_cwnd >> 1) +
8131da177e4SLinus Torvalds 			    (tp->snd_cwnd >> 2)));
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
816b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */
817b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp)	WARN_ON(tcp_left_out(tp) > tp->packets_out)
8181da177e4SLinus Torvalds 
8193cfe3baaSIlpo Järvinen extern void tcp_enter_cwr(struct sock *sk, const int set_ssthresh);
8201da177e4SLinus Torvalds extern __u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst);
8211da177e4SLinus Torvalds 
8221da177e4SLinus Torvalds /* Slow start with delack produces 3 packets of burst, so that
823dd9e0ddaSJohn Heffner  * it is safe "de facto".  This will be the default - same as
824dd9e0ddaSJohn Heffner  * the default reordering threshold - but if reordering increases,
825dd9e0ddaSJohn Heffner  * we must be able to allow cwnd to burst at least this much in order
826dd9e0ddaSJohn Heffner  * to not pull it back when holes are filled.
8271da177e4SLinus Torvalds  */
8281da177e4SLinus Torvalds static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp)
8291da177e4SLinus Torvalds {
830dd9e0ddaSJohn Heffner 	return tp->reordering;
8311da177e4SLinus Torvalds }
8321da177e4SLinus Torvalds 
83390840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */
83490840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
83590840defSIlpo Järvinen {
83690840defSIlpo Järvinen 	return tp->snd_una + tp->snd_wnd;
83790840defSIlpo Järvinen }
838cea14e0eSIlpo Järvinen extern int tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight);
839f4805edeSStephen Hemminger 
840c1bd24b7SChuck Lever static inline void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss,
8411da177e4SLinus Torvalds 				       const struct sk_buff *skb)
8421da177e4SLinus Torvalds {
8431da177e4SLinus Torvalds 	if (skb->len < mss)
8441da177e4SLinus Torvalds 		tp->snd_sml = TCP_SKB_CB(skb)->end_seq;
8451da177e4SLinus Torvalds }
8461da177e4SLinus Torvalds 
8479e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk)
8481da177e4SLinus Torvalds {
8499e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
850463c84b9SArnaldo Carvalho de Melo 	const struct inet_connection_sock *icsk = inet_csk(sk);
8519e412ba7SIlpo Järvinen 
852463c84b9SArnaldo Carvalho de Melo 	if (!tp->packets_out && !icsk->icsk_pending)
8533f421baaSArnaldo Carvalho de Melo 		inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
8543f421baaSArnaldo Carvalho de Melo 					  icsk->icsk_rto, TCP_RTO_MAX);
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
8579e412ba7SIlpo Järvinen static inline void tcp_push_pending_frames(struct sock *sk)
8581da177e4SLinus Torvalds {
8599e412ba7SIlpo Järvinen 	struct tcp_sock *tp = tcp_sk(sk);
8609e412ba7SIlpo Järvinen 
8610c54b85fSIlpo Järvinen 	__tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle);
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
864ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq)
8651da177e4SLinus Torvalds {
8661da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
869ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq)
8701da177e4SLinus Torvalds {
8711da177e4SLinus Torvalds 	tp->snd_wl1 = seq;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds /*
8751da177e4SLinus Torvalds  * Calculate(/check) TCP checksum
8761da177e4SLinus Torvalds  */
877ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr,
878ba7808eaSFrederik Deweerdt 				   __be32 daddr, __wsum base)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base);
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
883b51655b9SAl Viro static inline __sum16 __tcp_checksum_complete(struct sk_buff *skb)
8841da177e4SLinus Torvalds {
885fb286bb2SHerbert Xu 	return __skb_checksum_complete(skb);
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
88840efc6faSStephen Hemminger static inline int tcp_checksum_complete(struct sk_buff *skb)
8891da177e4SLinus Torvalds {
89060476372SHerbert Xu 	return !skb_csum_unnecessary(skb) &&
8911da177e4SLinus Torvalds 		__tcp_checksum_complete(skb);
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds /* Prequeue for VJ style copy to user, combined with checksumming. */
8951da177e4SLinus Torvalds 
89640efc6faSStephen Hemminger static inline void tcp_prequeue_init(struct tcp_sock *tp)
8971da177e4SLinus Torvalds {
8981da177e4SLinus Torvalds 	tp->ucopy.task = NULL;
8991da177e4SLinus Torvalds 	tp->ucopy.len = 0;
9001da177e4SLinus Torvalds 	tp->ucopy.memory = 0;
9011da177e4SLinus Torvalds 	skb_queue_head_init(&tp->ucopy.prequeue);
90297fc2f08SChris Leech #ifdef CONFIG_NET_DMA
90397fc2f08SChris Leech 	tp->ucopy.dma_chan = NULL;
90497fc2f08SChris Leech 	tp->ucopy.wakeup = 0;
90597fc2f08SChris Leech 	tp->ucopy.pinned_list = NULL;
90697fc2f08SChris Leech 	tp->ucopy.dma_cookie = 0;
90797fc2f08SChris Leech #endif
9081da177e4SLinus Torvalds }
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds /* Packet is added to VJ-style prequeue for processing in process
9111da177e4SLinus Torvalds  * context, if a reader task is waiting. Apparently, this exciting
9121da177e4SLinus Torvalds  * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93)
9131da177e4SLinus Torvalds  * failed somewhere. Latency? Burstiness? Well, at least now we will
9141da177e4SLinus Torvalds  * see, why it failed. 8)8)				  --ANK
9151da177e4SLinus Torvalds  *
9161da177e4SLinus Torvalds  * NOTE: is this not too big to inline?
9171da177e4SLinus Torvalds  */
91840efc6faSStephen Hemminger static inline int tcp_prequeue(struct sock *sk, struct sk_buff *skb)
9191da177e4SLinus Torvalds {
9201da177e4SLinus Torvalds 	struct tcp_sock *tp = tcp_sk(sk);
9211da177e4SLinus Torvalds 
922f5f8d86bSEric Dumazet 	if (sysctl_tcp_low_latency || !tp->ucopy.task)
923f5f8d86bSEric Dumazet 		return 0;
924f5f8d86bSEric Dumazet 
9251da177e4SLinus Torvalds 	__skb_queue_tail(&tp->ucopy.prequeue, skb);
9261da177e4SLinus Torvalds 	tp->ucopy.memory += skb->truesize;
9271da177e4SLinus Torvalds 	if (tp->ucopy.memory > sk->sk_rcvbuf) {
9281da177e4SLinus Torvalds 		struct sk_buff *skb1;
9291da177e4SLinus Torvalds 
9301da177e4SLinus Torvalds 		BUG_ON(sock_owned_by_user(sk));
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 		while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) {
933c57943a1SPeter Zijlstra 			sk_backlog_rcv(sk, skb1);
934f5f8d86bSEric Dumazet 			NET_INC_STATS_BH(sock_net(sk),
935f5f8d86bSEric Dumazet 					 LINUX_MIB_TCPPREQUEUEDROPPED);
9361da177e4SLinus Torvalds 		}
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 		tp->ucopy.memory = 0;
9391da177e4SLinus Torvalds 	} else if (skb_queue_len(&tp->ucopy.prequeue) == 1) {
9407aedec2aSEric Dumazet 		wake_up_interruptible_poll(sk->sk_sleep,
9417aedec2aSEric Dumazet 					   POLLIN | POLLRDNORM | POLLRDBAND);
942463c84b9SArnaldo Carvalho de Melo 		if (!inet_csk_ack_scheduled(sk))
943463c84b9SArnaldo Carvalho de Melo 			inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
9440c266898SSatoru SATOH 						  (3 * tcp_rto_min(sk)) / 4,
9453f421baaSArnaldo Carvalho de Melo 						  TCP_RTO_MAX);
9461da177e4SLinus Torvalds 	}
9471da177e4SLinus Torvalds 	return 1;
9481da177e4SLinus Torvalds }
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds #undef STATE_TRACE
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds #ifdef STATE_TRACE
9541da177e4SLinus Torvalds static const char *statename[]={
9551da177e4SLinus Torvalds 	"Unused","Established","Syn Sent","Syn Recv",
9561da177e4SLinus Torvalds 	"Fin Wait 1","Fin Wait 2","Time Wait", "Close",
9571da177e4SLinus Torvalds 	"Close Wait","Last ACK","Listen","Closing"
9581da177e4SLinus Torvalds };
9591da177e4SLinus Torvalds #endif
960490d5046SIlpo Järvinen extern void tcp_set_state(struct sock *sk, int state);
9611da177e4SLinus Torvalds 
9624ac02babSAndi Kleen extern void tcp_done(struct sock *sk);
9631da177e4SLinus Torvalds 
96440efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt)
9651da177e4SLinus Torvalds {
9661da177e4SLinus Torvalds 	rx_opt->dsack = 0;
9671da177e4SLinus Torvalds 	rx_opt->num_sacks = 0;
9681da177e4SLinus Torvalds }
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */
9711da177e4SLinus Torvalds extern void tcp_select_initial_window(int __space, __u32 mss,
9721da177e4SLinus Torvalds 				      __u32 *rcv_wnd, __u32 *window_clamp,
9731da177e4SLinus Torvalds 				      int wscale_ok, __u8 *rcv_wscale);
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds static inline int tcp_win_from_space(int space)
9761da177e4SLinus Torvalds {
9771da177e4SLinus Torvalds 	return sysctl_tcp_adv_win_scale<=0 ?
9781da177e4SLinus Torvalds 		(space>>(-sysctl_tcp_adv_win_scale)) :
9791da177e4SLinus Torvalds 		space - (space>>sysctl_tcp_adv_win_scale);
9801da177e4SLinus Torvalds }
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */
9831da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk)
9841da177e4SLinus Torvalds {
9851da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf -
9861da177e4SLinus Torvalds 				  atomic_read(&sk->sk_rmem_alloc));
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk)
9901da177e4SLinus Torvalds {
9911da177e4SLinus Torvalds 	return tcp_win_from_space(sk->sk_rcvbuf);
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds 
99440efc6faSStephen Hemminger static inline void tcp_openreq_init(struct request_sock *req,
9951da177e4SLinus Torvalds 				    struct tcp_options_received *rx_opt,
9961da177e4SLinus Torvalds 				    struct sk_buff *skb)
9971da177e4SLinus Torvalds {
9982e6599cbSArnaldo Carvalho de Melo 	struct inet_request_sock *ireq = inet_rsk(req);
9992e6599cbSArnaldo Carvalho de Melo 
10001da177e4SLinus Torvalds 	req->rcv_wnd = 0;		/* So that tcp_send_synack() knows! */
10014dfc2817SFlorian Westphal 	req->cookie_ts = 0;
10022e6599cbSArnaldo Carvalho de Melo 	tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
10031da177e4SLinus Torvalds 	req->mss = rx_opt->mss_clamp;
10041da177e4SLinus Torvalds 	req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
10052e6599cbSArnaldo Carvalho de Melo 	ireq->tstamp_ok = rx_opt->tstamp_ok;
10062e6599cbSArnaldo Carvalho de Melo 	ireq->sack_ok = rx_opt->sack_ok;
10072e6599cbSArnaldo Carvalho de Melo 	ireq->snd_wscale = rx_opt->snd_wscale;
10082e6599cbSArnaldo Carvalho de Melo 	ireq->wscale_ok = rx_opt->wscale_ok;
10092e6599cbSArnaldo Carvalho de Melo 	ireq->acked = 0;
10102e6599cbSArnaldo Carvalho de Melo 	ireq->ecn_ok = 0;
1011aa8223c7SArnaldo Carvalho de Melo 	ireq->rmt_port = tcp_hdr(skb)->source;
1012a3116ac5SKOVACS Krisztian 	ireq->loc_port = tcp_hdr(skb)->dest;
10131da177e4SLinus Torvalds }
10141da177e4SLinus Torvalds 
10155c52ba17SPavel Emelyanov extern void tcp_enter_memory_pressure(struct sock *sk);
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp)
10181da177e4SLinus Torvalds {
10191da177e4SLinus Torvalds 	return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl;
10201da177e4SLinus Torvalds }
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp)
10231da177e4SLinus Torvalds {
10241da177e4SLinus Torvalds 	return tp->keepalive_time ? : sysctl_tcp_keepalive_time;
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
1027df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp)
1028df19a626SEric Dumazet {
1029df19a626SEric Dumazet 	return tp->keepalive_probes ? : sysctl_tcp_keepalive_probes;
1030df19a626SEric Dumazet }
1031df19a626SEric Dumazet 
1032463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk)
10331da177e4SLinus Torvalds {
1034463c84b9SArnaldo Carvalho de Melo 	int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout;
1035463c84b9SArnaldo Carvalho de Melo 	const int rto = inet_csk(sk)->icsk_rto;
10361da177e4SLinus Torvalds 
1037463c84b9SArnaldo Carvalho de Melo 	if (fin_timeout < (rto << 2) - (rto >> 1))
1038463c84b9SArnaldo Carvalho de Melo 		fin_timeout = (rto << 2) - (rto >> 1);
10391da177e4SLinus Torvalds 
10401da177e4SLinus Torvalds 	return fin_timeout;
10411da177e4SLinus Torvalds }
10421da177e4SLinus Torvalds 
1043c887e6d2SIlpo Järvinen static inline int tcp_paws_check(const struct tcp_options_received *rx_opt,
1044c887e6d2SIlpo Järvinen 				 int paws_win)
10451da177e4SLinus Torvalds {
1046c887e6d2SIlpo Järvinen 	if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win)
1047c887e6d2SIlpo Järvinen 		return 1;
1048c887e6d2SIlpo Järvinen 	if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS))
1049c887e6d2SIlpo Järvinen 		return 1;
1050c887e6d2SIlpo Järvinen 
10511da177e4SLinus Torvalds 	return 0;
1052c887e6d2SIlpo Järvinen }
1053c887e6d2SIlpo Järvinen 
1054c887e6d2SIlpo Järvinen static inline int tcp_paws_reject(const struct tcp_options_received *rx_opt,
1055c887e6d2SIlpo Järvinen 				  int rst)
1056c887e6d2SIlpo Järvinen {
1057c887e6d2SIlpo Järvinen 	if (tcp_paws_check(rx_opt, 0))
10581da177e4SLinus Torvalds 		return 0;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	/* RST segments are not recommended to carry timestamp,
10611da177e4SLinus Torvalds 	   and, if they do, it is recommended to ignore PAWS because
10621da177e4SLinus Torvalds 	   "their cleanup function should take precedence over timestamps."
10631da177e4SLinus Torvalds 	   Certainly, it is mistake. It is necessary to understand the reasons
10641da177e4SLinus Torvalds 	   of this constraint to relax it: if peer reboots, clock may go
10651da177e4SLinus Torvalds 	   out-of-sync and half-open connections will not be reset.
10661da177e4SLinus Torvalds 	   Actually, the problem would be not existing if all
10671da177e4SLinus Torvalds 	   the implementations followed draft about maintaining clock
10681da177e4SLinus Torvalds 	   via reboots. Linux-2.2 DOES NOT!
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	   However, we can relax time bounds for RST segments to MSL.
10711da177e4SLinus Torvalds 	 */
10729d729f72SJames Morris 	if (rst && get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL)
10731da177e4SLinus Torvalds 		return 0;
10741da177e4SLinus Torvalds 	return 1;
10751da177e4SLinus Torvalds }
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds #define TCP_CHECK_TIMER(sk) do { } while (0)
10781da177e4SLinus Torvalds 
1079a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net)
10801da177e4SLinus Torvalds {
10811da177e4SLinus Torvalds 	/* See RFC 2012 */
1082cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOALGORITHM, 1);
1083cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ);
1084cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ);
1085cf1100a7SPavel Emelyanov 	TCP_ADD_STATS_USER(net, TCP_MIB_MAXCONN, -1);
10861da177e4SLinus Torvalds }
10871da177e4SLinus Torvalds 
10886a438bbeSStephen Hemminger /* from STCP */
1089ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp)
10900800f170SDavid S. Miller {
10916a438bbeSStephen Hemminger 	tp->lost_skb_hint = NULL;
10926a438bbeSStephen Hemminger 	tp->scoreboard_skb_hint = NULL;
1093ef9da47cSIlpo Järvinen }
1094ef9da47cSIlpo Järvinen 
1095ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp)
1096ef9da47cSIlpo Järvinen {
1097ef9da47cSIlpo Järvinen 	tcp_clear_retrans_hints_partial(tp);
10986a438bbeSStephen Hemminger 	tp->retransmit_skb_hint = NULL;
1099b7689205SIlpo Järvinen }
1100b7689205SIlpo Järvinen 
1101cfb6eeb4SYOSHIFUJI Hideaki /* MD5 Signature */
1102cfb6eeb4SYOSHIFUJI Hideaki struct crypto_hash;
1103cfb6eeb4SYOSHIFUJI Hideaki 
1104cfb6eeb4SYOSHIFUJI Hideaki /* - key database */
1105cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key {
1106cfb6eeb4SYOSHIFUJI Hideaki 	u8			*key;
1107cfb6eeb4SYOSHIFUJI Hideaki 	u8			keylen;
1108cfb6eeb4SYOSHIFUJI Hideaki };
1109cfb6eeb4SYOSHIFUJI Hideaki 
1110cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_md5sig_key {
1111f8ab18d2SDavid S. Miller 	struct tcp_md5sig_key	base;
1112cfb6eeb4SYOSHIFUJI Hideaki 	__be32			addr;
1113cfb6eeb4SYOSHIFUJI Hideaki };
1114cfb6eeb4SYOSHIFUJI Hideaki 
1115cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_md5sig_key {
1116f8ab18d2SDavid S. Miller 	struct tcp_md5sig_key	base;
1117cfb6eeb4SYOSHIFUJI Hideaki #if 0
1118cfb6eeb4SYOSHIFUJI Hideaki 	u32			scope_id;	/* XXX */
1119cfb6eeb4SYOSHIFUJI Hideaki #endif
1120cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr		addr;
1121cfb6eeb4SYOSHIFUJI Hideaki };
1122cfb6eeb4SYOSHIFUJI Hideaki 
1123cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */
1124cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info {
1125cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_md5sig_key	*keys4;
1126cfb6eeb4SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1127cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_md5sig_key	*keys6;
1128cfb6eeb4SYOSHIFUJI Hideaki 	u32			entries6;
1129cfb6eeb4SYOSHIFUJI Hideaki 	u32			alloced6;
1130cfb6eeb4SYOSHIFUJI Hideaki #endif
1131cfb6eeb4SYOSHIFUJI Hideaki 	u32			entries4;
1132cfb6eeb4SYOSHIFUJI Hideaki 	u32			alloced4;
1133cfb6eeb4SYOSHIFUJI Hideaki };
1134cfb6eeb4SYOSHIFUJI Hideaki 
1135cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */
1136cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr {
1137cfb6eeb4SYOSHIFUJI Hideaki 	__be32		saddr;
1138cfb6eeb4SYOSHIFUJI Hideaki 	__be32		daddr;
1139cfb6eeb4SYOSHIFUJI Hideaki 	__u8		pad;
1140cfb6eeb4SYOSHIFUJI Hideaki 	__u8		protocol;
1141cfb6eeb4SYOSHIFUJI Hideaki 	__be16		len;
1142cfb6eeb4SYOSHIFUJI Hideaki };
1143cfb6eeb4SYOSHIFUJI Hideaki 
1144cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr {
1145cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr	saddr;
1146cfb6eeb4SYOSHIFUJI Hideaki 	struct in6_addr daddr;
1147cfb6eeb4SYOSHIFUJI Hideaki 	__be32		len;
1148cfb6eeb4SYOSHIFUJI Hideaki 	__be32		protocol;	/* including padding */
1149cfb6eeb4SYOSHIFUJI Hideaki };
1150cfb6eeb4SYOSHIFUJI Hideaki 
1151cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block {
1152cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp4_pseudohdr ip4;
1153cfb6eeb4SYOSHIFUJI Hideaki #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1154cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp6_pseudohdr ip6;
1155cfb6eeb4SYOSHIFUJI Hideaki #endif
1156cfb6eeb4SYOSHIFUJI Hideaki };
1157cfb6eeb4SYOSHIFUJI Hideaki 
1158cfb6eeb4SYOSHIFUJI Hideaki /* - pool: digest algorithm, hash description and scratch buffer */
1159cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool {
1160cfb6eeb4SYOSHIFUJI Hideaki 	struct hash_desc	md5_desc;
1161cfb6eeb4SYOSHIFUJI Hideaki 	union tcp_md5sum_block	md5_blk;
1162cfb6eeb4SYOSHIFUJI Hideaki };
1163cfb6eeb4SYOSHIFUJI Hideaki 
1164cfb6eeb4SYOSHIFUJI Hideaki #define TCP_MD5SIG_MAXKEYS	(~(u32)0)	/* really?! */
1165cfb6eeb4SYOSHIFUJI Hideaki 
1166cfb6eeb4SYOSHIFUJI Hideaki /* - functions */
116749a72dfbSAdam Langley extern int			tcp_v4_md5_hash_skb(char *md5_hash,
1168cfb6eeb4SYOSHIFUJI Hideaki 						    struct tcp_md5sig_key *key,
1169cfb6eeb4SYOSHIFUJI Hideaki 						    struct sock *sk,
1170cfb6eeb4SYOSHIFUJI Hideaki 						    struct request_sock *req,
117149a72dfbSAdam Langley 						    struct sk_buff *skb);
117249a72dfbSAdam Langley 
1173cfb6eeb4SYOSHIFUJI Hideaki extern struct tcp_md5sig_key	*tcp_v4_md5_lookup(struct sock *sk,
1174cfb6eeb4SYOSHIFUJI Hideaki 						   struct sock *addr_sk);
1175cfb6eeb4SYOSHIFUJI Hideaki 
1176cfb6eeb4SYOSHIFUJI Hideaki extern int			tcp_v4_md5_do_add(struct sock *sk,
1177cfb6eeb4SYOSHIFUJI Hideaki 						  __be32 addr,
1178cfb6eeb4SYOSHIFUJI Hideaki 						  u8 *newkey,
1179cfb6eeb4SYOSHIFUJI Hideaki 						  u8 newkeylen);
1180cfb6eeb4SYOSHIFUJI Hideaki 
1181cfb6eeb4SYOSHIFUJI Hideaki extern int			tcp_v4_md5_do_del(struct sock *sk,
11828e5200f5SAl Viro 						  __be32 addr);
1183cfb6eeb4SYOSHIFUJI Hideaki 
11849501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
11859501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	((twsk)->tw_md5_keylen ? 		 \
11869501f972SYOSHIFUJI Hideaki 				 &(struct tcp_md5sig_key) {		 \
11879501f972SYOSHIFUJI Hideaki 					.key = (twsk)->tw_md5_key,	 \
11889501f972SYOSHIFUJI Hideaki 					.keylen = (twsk)->tw_md5_keylen, \
11899501f972SYOSHIFUJI Hideaki 				} : NULL)
11909501f972SYOSHIFUJI Hideaki #else
11919501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk)	NULL
11929501f972SYOSHIFUJI Hideaki #endif
11939501f972SYOSHIFUJI Hideaki 
1194aa133076SWu Fengguang extern struct tcp_md5sig_pool	**tcp_alloc_md5sig_pool(struct sock *);
1195cfb6eeb4SYOSHIFUJI Hideaki extern void			tcp_free_md5sig_pool(void);
1196cfb6eeb4SYOSHIFUJI Hideaki 
1197cfb6eeb4SYOSHIFUJI Hideaki extern struct tcp_md5sig_pool	*__tcp_get_md5sig_pool(int cpu);
1198cfb6eeb4SYOSHIFUJI Hideaki extern void			__tcp_put_md5sig_pool(void);
119949a72dfbSAdam Langley extern int tcp_md5_hash_header(struct tcp_md5sig_pool *, struct tcphdr *);
120049a72dfbSAdam Langley extern int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, struct sk_buff *,
120149a72dfbSAdam Langley 				 unsigned header_len);
120249a72dfbSAdam Langley extern int tcp_md5_hash_key(struct tcp_md5sig_pool *hp,
120349a72dfbSAdam Langley 			    struct tcp_md5sig_key *key);
1204cfb6eeb4SYOSHIFUJI Hideaki 
1205cfb6eeb4SYOSHIFUJI Hideaki static inline
1206cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool		*tcp_get_md5sig_pool(void)
1207cfb6eeb4SYOSHIFUJI Hideaki {
1208cfb6eeb4SYOSHIFUJI Hideaki 	int cpu = get_cpu();
1209cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_pool *ret = __tcp_get_md5sig_pool(cpu);
1210cfb6eeb4SYOSHIFUJI Hideaki 	if (!ret)
1211cfb6eeb4SYOSHIFUJI Hideaki 		put_cpu();
1212cfb6eeb4SYOSHIFUJI Hideaki 	return ret;
1213cfb6eeb4SYOSHIFUJI Hideaki }
1214cfb6eeb4SYOSHIFUJI Hideaki 
1215cfb6eeb4SYOSHIFUJI Hideaki static inline void		tcp_put_md5sig_pool(void)
1216cfb6eeb4SYOSHIFUJI Hideaki {
1217cfb6eeb4SYOSHIFUJI Hideaki 	__tcp_put_md5sig_pool();
1218cfb6eeb4SYOSHIFUJI Hideaki 	put_cpu();
1219cfb6eeb4SYOSHIFUJI Hideaki }
1220cfb6eeb4SYOSHIFUJI Hideaki 
1221fe067e8aSDavid S. Miller /* write queue abstraction */
1222fe067e8aSDavid S. Miller static inline void tcp_write_queue_purge(struct sock *sk)
1223fe067e8aSDavid S. Miller {
1224fe067e8aSDavid S. Miller 	struct sk_buff *skb;
1225fe067e8aSDavid S. Miller 
1226fe067e8aSDavid S. Miller 	while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL)
12273ab224beSHideo Aoki 		sk_wmem_free_skb(sk, skb);
12283ab224beSHideo Aoki 	sk_mem_reclaim(sk);
1229fe067e8aSDavid S. Miller }
1230fe067e8aSDavid S. Miller 
1231fe067e8aSDavid S. Miller static inline struct sk_buff *tcp_write_queue_head(struct sock *sk)
1232fe067e8aSDavid S. Miller {
1233cd07a8eaSDavid S. Miller 	return skb_peek(&sk->sk_write_queue);
1234fe067e8aSDavid S. Miller }
1235fe067e8aSDavid S. Miller 
1236fe067e8aSDavid S. Miller static inline struct sk_buff *tcp_write_queue_tail(struct sock *sk)
1237fe067e8aSDavid S. Miller {
1238cd07a8eaSDavid S. Miller 	return skb_peek_tail(&sk->sk_write_queue);
1239fe067e8aSDavid S. Miller }
1240fe067e8aSDavid S. Miller 
1241fe067e8aSDavid S. Miller static inline struct sk_buff *tcp_write_queue_next(struct sock *sk, struct sk_buff *skb)
1242fe067e8aSDavid S. Miller {
1243cd07a8eaSDavid S. Miller 	return skb_queue_next(&sk->sk_write_queue, skb);
1244fe067e8aSDavid S. Miller }
1245fe067e8aSDavid S. Miller 
1246832d11c5SIlpo Järvinen static inline struct sk_buff *tcp_write_queue_prev(struct sock *sk, struct sk_buff *skb)
1247832d11c5SIlpo Järvinen {
1248832d11c5SIlpo Järvinen 	return skb_queue_prev(&sk->sk_write_queue, skb);
1249832d11c5SIlpo Järvinen }
1250832d11c5SIlpo Järvinen 
1251fe067e8aSDavid S. Miller #define tcp_for_write_queue(skb, sk)					\
1252cd07a8eaSDavid S. Miller 	skb_queue_walk(&(sk)->sk_write_queue, skb)
1253fe067e8aSDavid S. Miller 
1254fe067e8aSDavid S. Miller #define tcp_for_write_queue_from(skb, sk)				\
1255cd07a8eaSDavid S. Miller 	skb_queue_walk_from(&(sk)->sk_write_queue, skb)
1256fe067e8aSDavid S. Miller 
1257234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk)			\
1258cd07a8eaSDavid S. Miller 	skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp)
1259234b6860SIlpo Järvinen 
12605152fc7dSDamian Lukowski /* This function calculates a "timeout" which is equivalent to the timeout of a
12615152fc7dSDamian Lukowski  * TCP connection after "boundary" unsucessful, exponentially backed-off
12625152fc7dSDamian Lukowski  * retransmissions with an initial RTO of TCP_RTO_MIN.
12635152fc7dSDamian Lukowski  */
12646fa12c85SDamian Lukowski static inline bool retransmits_timed_out(const struct sock *sk,
12656fa12c85SDamian Lukowski 					 unsigned int boundary)
12666fa12c85SDamian Lukowski {
12675152fc7dSDamian Lukowski 	unsigned int timeout, linear_backoff_thresh;
12685152fc7dSDamian Lukowski 
12696fa12c85SDamian Lukowski 	if (!inet_csk(sk)->icsk_retransmits)
12706fa12c85SDamian Lukowski 		return false;
12716fa12c85SDamian Lukowski 
12725152fc7dSDamian Lukowski 	linear_backoff_thresh = ilog2(TCP_RTO_MAX/TCP_RTO_MIN);
12736fa12c85SDamian Lukowski 
12745152fc7dSDamian Lukowski 	if (boundary <= linear_backoff_thresh)
12755152fc7dSDamian Lukowski 		timeout = ((2 << boundary) - 1) * TCP_RTO_MIN;
12766fa12c85SDamian Lukowski 	else
12775152fc7dSDamian Lukowski 		timeout = ((2 << linear_backoff_thresh) - 1) * TCP_RTO_MIN +
12785152fc7dSDamian Lukowski 			  (boundary - linear_backoff_thresh) * TCP_RTO_MAX;
12796fa12c85SDamian Lukowski 
12805152fc7dSDamian Lukowski 	return (tcp_time_stamp - tcp_sk(sk)->retrans_stamp) >= timeout;
12816fa12c85SDamian Lukowski }
12826fa12c85SDamian Lukowski 
1283fe067e8aSDavid S. Miller static inline struct sk_buff *tcp_send_head(struct sock *sk)
1284fe067e8aSDavid S. Miller {
1285fe067e8aSDavid S. Miller 	return sk->sk_send_head;
1286fe067e8aSDavid S. Miller }
1287fe067e8aSDavid S. Miller 
1288cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk,
1289cd07a8eaSDavid S. Miller 				   const struct sk_buff *skb)
1290cd07a8eaSDavid S. Miller {
1291cd07a8eaSDavid S. Miller 	return skb_queue_is_last(&sk->sk_write_queue, skb);
1292cd07a8eaSDavid S. Miller }
1293cd07a8eaSDavid S. Miller 
1294fe067e8aSDavid S. Miller static inline void tcp_advance_send_head(struct sock *sk, struct sk_buff *skb)
1295fe067e8aSDavid S. Miller {
1296cd07a8eaSDavid S. Miller 	if (tcp_skb_is_last(sk, skb))
1297fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1298cd07a8eaSDavid S. Miller 	else
1299cd07a8eaSDavid S. Miller 		sk->sk_send_head = tcp_write_queue_next(sk, skb);
1300fe067e8aSDavid S. Miller }
1301fe067e8aSDavid S. Miller 
1302fe067e8aSDavid S. Miller static inline void tcp_check_send_head(struct sock *sk, struct sk_buff *skb_unlinked)
1303fe067e8aSDavid S. Miller {
1304fe067e8aSDavid S. Miller 	if (sk->sk_send_head == skb_unlinked)
1305fe067e8aSDavid S. Miller 		sk->sk_send_head = NULL;
1306fe067e8aSDavid S. Miller }
1307fe067e8aSDavid S. Miller 
1308fe067e8aSDavid S. Miller static inline void tcp_init_send_head(struct sock *sk)
1309fe067e8aSDavid S. Miller {
1310fe067e8aSDavid S. Miller 	sk->sk_send_head = NULL;
1311fe067e8aSDavid S. Miller }
1312fe067e8aSDavid S. Miller 
1313fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1314fe067e8aSDavid S. Miller {
1315fe067e8aSDavid S. Miller 	__skb_queue_tail(&sk->sk_write_queue, skb);
1316fe067e8aSDavid S. Miller }
1317fe067e8aSDavid S. Miller 
1318fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
1319fe067e8aSDavid S. Miller {
1320fe067e8aSDavid S. Miller 	__tcp_add_write_queue_tail(sk, skb);
1321fe067e8aSDavid S. Miller 
1322fe067e8aSDavid S. Miller 	/* Queue it, remembering where we must start sending. */
13236859d494SIlpo Järvinen 	if (sk->sk_send_head == NULL) {
1324fe067e8aSDavid S. Miller 		sk->sk_send_head = skb;
13256859d494SIlpo Järvinen 
13266859d494SIlpo Järvinen 		if (tcp_sk(sk)->highest_sack == NULL)
13276859d494SIlpo Järvinen 			tcp_sk(sk)->highest_sack = skb;
13286859d494SIlpo Järvinen 	}
1329fe067e8aSDavid S. Miller }
1330fe067e8aSDavid S. Miller 
1331fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_head(struct sock *sk, struct sk_buff *skb)
1332fe067e8aSDavid S. Miller {
1333fe067e8aSDavid S. Miller 	__skb_queue_head(&sk->sk_write_queue, skb);
1334fe067e8aSDavid S. Miller }
1335fe067e8aSDavid S. Miller 
1336fe067e8aSDavid S. Miller /* Insert buff after skb on the write queue of sk.  */
1337fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_after(struct sk_buff *skb,
1338fe067e8aSDavid S. Miller 						struct sk_buff *buff,
1339fe067e8aSDavid S. Miller 						struct sock *sk)
1340fe067e8aSDavid S. Miller {
13417de6c033SGerrit Renker 	__skb_queue_after(&sk->sk_write_queue, skb, buff);
1342fe067e8aSDavid S. Miller }
1343fe067e8aSDavid S. Miller 
134443f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk.  */
1345fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new,
1346fe067e8aSDavid S. Miller 						  struct sk_buff *skb,
1347fe067e8aSDavid S. Miller 						  struct sock *sk)
1348fe067e8aSDavid S. Miller {
134943f59c89SDavid S. Miller 	__skb_queue_before(&sk->sk_write_queue, skb, new);
13506e421410SIlpo Järvinen 
13516e421410SIlpo Järvinen 	if (sk->sk_send_head == skb)
13526e421410SIlpo Järvinen 		sk->sk_send_head = new;
1353fe067e8aSDavid S. Miller }
1354fe067e8aSDavid S. Miller 
1355fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk)
1356fe067e8aSDavid S. Miller {
1357fe067e8aSDavid S. Miller 	__skb_unlink(skb, &sk->sk_write_queue);
1358fe067e8aSDavid S. Miller }
1359fe067e8aSDavid S. Miller 
1360fe067e8aSDavid S. Miller static inline int tcp_write_queue_empty(struct sock *sk)
1361fe067e8aSDavid S. Miller {
1362fe067e8aSDavid S. Miller 	return skb_queue_empty(&sk->sk_write_queue);
1363fe067e8aSDavid S. Miller }
1364fe067e8aSDavid S. Miller 
1365a47e5a98SIlpo Järvinen /* Start sequence of the highest skb with SACKed bit, valid only if
1366a47e5a98SIlpo Järvinen  * sacked > 0 or when the caller has ensured validity by itself.
1367a47e5a98SIlpo Järvinen  */
1368a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp)
1369a47e5a98SIlpo Järvinen {
1370a47e5a98SIlpo Järvinen 	if (!tp->sacked_out)
1371a47e5a98SIlpo Järvinen 		return tp->snd_una;
13726859d494SIlpo Järvinen 
13736859d494SIlpo Järvinen 	if (tp->highest_sack == NULL)
13746859d494SIlpo Järvinen 		return tp->snd_nxt;
13756859d494SIlpo Järvinen 
1376a47e5a98SIlpo Järvinen 	return TCP_SKB_CB(tp->highest_sack)->seq;
1377a47e5a98SIlpo Järvinen }
1378a47e5a98SIlpo Järvinen 
13796859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb)
13806859d494SIlpo Järvinen {
13816859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_skb_is_last(sk, skb) ? NULL :
13826859d494SIlpo Järvinen 						tcp_write_queue_next(sk, skb);
13836859d494SIlpo Järvinen }
13846859d494SIlpo Järvinen 
13856859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk)
13866859d494SIlpo Järvinen {
13876859d494SIlpo Järvinen 	return tcp_sk(sk)->highest_sack;
13886859d494SIlpo Järvinen }
13896859d494SIlpo Järvinen 
13906859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk)
13916859d494SIlpo Järvinen {
13926859d494SIlpo Järvinen 	tcp_sk(sk)->highest_sack = tcp_write_queue_head(sk);
13936859d494SIlpo Järvinen }
13946859d494SIlpo Järvinen 
13956859d494SIlpo Järvinen /* Called when old skb is about to be deleted (to be combined with new skb) */
13966859d494SIlpo Järvinen static inline void tcp_highest_sack_combine(struct sock *sk,
13976859d494SIlpo Järvinen 					    struct sk_buff *old,
13986859d494SIlpo Järvinen 					    struct sk_buff *new)
13996859d494SIlpo Järvinen {
14006859d494SIlpo Järvinen 	if (tcp_sk(sk)->sacked_out && (old == tcp_sk(sk)->highest_sack))
14016859d494SIlpo Järvinen 		tcp_sk(sk)->highest_sack = new;
14026859d494SIlpo Järvinen }
14036859d494SIlpo Järvinen 
14041da177e4SLinus Torvalds /* /proc */
14051da177e4SLinus Torvalds enum tcp_seq_states {
14061da177e4SLinus Torvalds 	TCP_SEQ_STATE_LISTENING,
14071da177e4SLinus Torvalds 	TCP_SEQ_STATE_OPENREQ,
14081da177e4SLinus Torvalds 	TCP_SEQ_STATE_ESTABLISHED,
14091da177e4SLinus Torvalds 	TCP_SEQ_STATE_TIME_WAIT,
14101da177e4SLinus Torvalds };
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds struct tcp_seq_afinfo {
14131da177e4SLinus Torvalds 	char			*name;
14141da177e4SLinus Torvalds 	sa_family_t		family;
141568fcadd1SDenis V. Lunev 	struct file_operations	seq_fops;
14169427c4b3SDenis V. Lunev 	struct seq_operations	seq_ops;
14171da177e4SLinus Torvalds };
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds struct tcp_iter_state {
1420a4146b1bSDenis V. Lunev 	struct seq_net_private	p;
14211da177e4SLinus Torvalds 	sa_family_t		family;
14221da177e4SLinus Torvalds 	enum tcp_seq_states	state;
14231da177e4SLinus Torvalds 	struct sock		*syn_wait_sk;
14241da177e4SLinus Torvalds 	int			bucket, sbucket, num, uid;
14251da177e4SLinus Torvalds };
14261da177e4SLinus Torvalds 
14276f8b13bcSDaniel Lezcano extern int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo);
14286f8b13bcSDaniel Lezcano extern void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo);
14291da177e4SLinus Torvalds 
143020380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops;
1431c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops;
143220380731SArnaldo Carvalho de Melo 
14337d06b2e0SBrian Haley extern void tcp_v4_destroy_sock(struct sock *sk);
143420380731SArnaldo Carvalho de Melo 
1435a430a43dSHerbert Xu extern int tcp_v4_gso_send_check(struct sk_buff *skb);
1436576a30ebSHerbert Xu extern struct sk_buff *tcp_tso_segment(struct sk_buff *skb, int features);
1437bf296b12SHerbert Xu extern struct sk_buff **tcp_gro_receive(struct sk_buff **head,
1438bf296b12SHerbert Xu 					struct sk_buff *skb);
1439bf296b12SHerbert Xu extern struct sk_buff **tcp4_gro_receive(struct sk_buff **head,
1440bf296b12SHerbert Xu 					 struct sk_buff *skb);
1441bf296b12SHerbert Xu extern int tcp_gro_complete(struct sk_buff *skb);
1442bf296b12SHerbert Xu extern int tcp4_gro_complete(struct sk_buff *skb);
1443f4c50d99SHerbert Xu 
144420380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
144520380731SArnaldo Carvalho de Melo extern int  tcp4_proc_init(void);
144620380731SArnaldo Carvalho de Melo extern void tcp4_proc_exit(void);
144720380731SArnaldo Carvalho de Melo #endif
144820380731SArnaldo Carvalho de Melo 
1449cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */
1450cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops {
1451cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1452cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key	*(*md5_lookup) (struct sock *sk,
1453cfb6eeb4SYOSHIFUJI Hideaki 						struct sock *addr_sk);
1454cfb6eeb4SYOSHIFUJI Hideaki 	int			(*calc_md5_hash) (char *location,
1455cfb6eeb4SYOSHIFUJI Hideaki 						  struct tcp_md5sig_key *md5,
1456cfb6eeb4SYOSHIFUJI Hideaki 						  struct sock *sk,
1457cfb6eeb4SYOSHIFUJI Hideaki 						  struct request_sock *req,
145849a72dfbSAdam Langley 						  struct sk_buff *skb);
1459cfb6eeb4SYOSHIFUJI Hideaki 	int			(*md5_add) (struct sock *sk,
1460cfb6eeb4SYOSHIFUJI Hideaki 					    struct sock *addr_sk,
1461cfb6eeb4SYOSHIFUJI Hideaki 					    u8 *newkey,
1462cfb6eeb4SYOSHIFUJI Hideaki 					    u8 len);
1463cfb6eeb4SYOSHIFUJI Hideaki 	int			(*md5_parse) (struct sock *sk,
1464cfb6eeb4SYOSHIFUJI Hideaki 					      char __user *optval,
1465cfb6eeb4SYOSHIFUJI Hideaki 					      int optlen);
1466cfb6eeb4SYOSHIFUJI Hideaki #endif
1467cfb6eeb4SYOSHIFUJI Hideaki };
1468cfb6eeb4SYOSHIFUJI Hideaki 
1469cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops {
1470cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
1471cfb6eeb4SYOSHIFUJI Hideaki 	struct tcp_md5sig_key	*(*md5_lookup) (struct sock *sk,
1472cfb6eeb4SYOSHIFUJI Hideaki 						struct request_sock *req);
1473e3afe7b7SJohn Dykstra 	int			(*calc_md5_hash) (char *location,
1474e3afe7b7SJohn Dykstra 						  struct tcp_md5sig_key *md5,
1475e3afe7b7SJohn Dykstra 						  struct sock *sk,
1476e3afe7b7SJohn Dykstra 						  struct request_sock *req,
1477e3afe7b7SJohn Dykstra 						  struct sk_buff *skb);
1478cfb6eeb4SYOSHIFUJI Hideaki #endif
1479cfb6eeb4SYOSHIFUJI Hideaki };
1480cfb6eeb4SYOSHIFUJI Hideaki 
14819b0f976fSDenis V. Lunev extern void tcp_v4_init(void);
148220380731SArnaldo Carvalho de Melo extern void tcp_init(void);
148320380731SArnaldo Carvalho de Melo 
14841da177e4SLinus Torvalds #endif	/* _TCP_H */
1485