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 FASTRETRANS_DEBUG 1 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds #include <linux/list.h> 241da177e4SLinus Torvalds #include <linux/tcp.h> 251da177e4SLinus Torvalds #include <linux/slab.h> 261da177e4SLinus Torvalds #include <linux/cache.h> 271da177e4SLinus Torvalds #include <linux/percpu.h> 28fb286bb2SHerbert Xu #include <linux/skbuff.h> 2997fc2f08SChris Leech #include <linux/dmaengine.h> 30cfb6eeb4SYOSHIFUJI Hideaki #include <linux/crypto.h> 31c6aefafbSGlenn Griffin #include <linux/cryptohash.h> 32435cf559SWilliam Allen Simpson #include <linux/kref.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> 47180d8cd9SGlauber Costa #include <linux/memcontrol.h> 481da177e4SLinus Torvalds 490f7ff927SArnaldo Carvalho de Melo extern struct inet_hashinfo tcp_hashinfo; 501da177e4SLinus Torvalds 51dd24c001SEric Dumazet extern struct percpu_counter tcp_orphan_count; 521da177e4SLinus Torvalds extern void tcp_time_wait(struct sock *sk, int state, int timeo); 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #define MAX_TCP_HEADER (128 + MAX_HEADER) 5533ad798cSAdam Langley #define MAX_TCP_OPTION_SPACE 40 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds /* 581da177e4SLinus Torvalds * Never offer a window over 32767 without using window scaling. Some 591da177e4SLinus Torvalds * poor stacks do signed 16bit maths! 601da177e4SLinus Torvalds */ 611da177e4SLinus Torvalds #define MAX_TCP_WINDOW 32767U 621da177e4SLinus Torvalds 63356f0398SNandita Dukkipati /* Offer an initial receive window of 10 mss. */ 64356f0398SNandita Dukkipati #define TCP_DEFAULT_INIT_RCVWND 10 65356f0398SNandita Dukkipati 661da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */ 671da177e4SLinus Torvalds #define TCP_MIN_MSS 88U 681da177e4SLinus Torvalds 695d424d5aSJohn Heffner /* The least MTU to use for probing */ 705d424d5aSJohn Heffner #define TCP_BASE_MSS 512 715d424d5aSJohn Heffner 721da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */ 731da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds /* Maximal reordering. */ 761da177e4SLinus Torvalds #define TCP_MAX_REORDERING 127 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */ 791da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS 16U 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* urg_data states */ 821da177e4SLinus Torvalds #define TCP_URG_VALID 0x0100 831da177e4SLinus Torvalds #define TCP_URG_NOTYET 0x0200 841da177e4SLinus Torvalds #define TCP_URG_READ 0x0400 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds #define TCP_RETR1 3 /* 871da177e4SLinus Torvalds * This is how many retries it does before it 881da177e4SLinus Torvalds * tries to figure out if the gateway is 891da177e4SLinus Torvalds * down. Minimal RFC value is 3; it corresponds 901da177e4SLinus Torvalds * to ~3sec-8min depending on RTO. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds #define TCP_RETR2 15 /* 941da177e4SLinus Torvalds * This should take at least 951da177e4SLinus Torvalds * 90 minutes to time out. 961da177e4SLinus Torvalds * RFC1122 says that the limit is 100 sec. 971da177e4SLinus Torvalds * 15 is ~13-30min depending on RTO. 981da177e4SLinus Torvalds */ 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds #define TCP_SYN_RETRIES 5 /* number of times to retry active opening a 101caa20d9aSStephen Hemminger * connection: ~180sec is RFC minimum */ 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds #define TCP_SYNACK_RETRIES 5 /* number of times to retry passive opening a 104caa20d9aSStephen Hemminger * connection: ~180sec is RFC minimum */ 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)) 1259ad7c049SJerry Chu #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ)) /* RFC2988bis initial RTO value */ 1269ad7c049SJerry Chu #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ)) /* RFC 1122 initial RTO value, now 1279ad7c049SJerry Chu * used as a fallback RTO for the 1289ad7c049SJerry Chu * initial data transmission if no 1299ad7c049SJerry Chu * valid RTT sample has been acquired, 1309ad7c049SJerry Chu * most likely due to retrans in 3WHS. 1319ad7c049SJerry Chu */ 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes 1341da177e4SLinus Torvalds * for local resources. 1351da177e4SLinus Torvalds */ 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME (120*60*HZ) /* two hours */ 1381da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES 9 /* Max of 9 keepalive probes */ 1391da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL (75*HZ) 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE 32767 1421da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL 32767 1431da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT 127 1441da177e4SLinus Torvalds #define MAX_TCP_SYNCNT 127 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds #define TCP_SYNQ_INTERVAL (HZ/5) /* Period of SYNACK timer */ 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds #define TCP_PAWS_24DAYS (60 * 60 * 24 * 24) 1491da177e4SLinus Torvalds #define TCP_PAWS_MSL 60 /* Per-host timestamps are invalidated 1501da177e4SLinus Torvalds * after this time. It should be equal 1511da177e4SLinus Torvalds * (or greater than) TCP_TIMEWAIT_LEN 1521da177e4SLinus Torvalds * to provide reliability equal to one 1531da177e4SLinus Torvalds * provided by timewait state. 1541da177e4SLinus Torvalds */ 1551da177e4SLinus Torvalds #define TCP_PAWS_WINDOW 1 /* Replay window for per-host 1561da177e4SLinus Torvalds * timestamps. It must be less than 1571da177e4SLinus Torvalds * minimal timewait lifetime. 1581da177e4SLinus Torvalds */ 1591da177e4SLinus Torvalds /* 1601da177e4SLinus Torvalds * TCP option 1611da177e4SLinus Torvalds */ 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds #define TCPOPT_NOP 1 /* Padding */ 1641da177e4SLinus Torvalds #define TCPOPT_EOL 0 /* End of options */ 1651da177e4SLinus Torvalds #define TCPOPT_MSS 2 /* Segment size negotiating */ 1661da177e4SLinus Torvalds #define TCPOPT_WINDOW 3 /* Window scaling */ 1671da177e4SLinus Torvalds #define TCPOPT_SACK_PERM 4 /* SACK Permitted */ 1681da177e4SLinus Torvalds #define TCPOPT_SACK 5 /* SACK Block */ 1691da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */ 170cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG 19 /* MD5 Signature (RFC2385) */ 171435cf559SWilliam Allen Simpson #define TCPOPT_COOKIE 253 /* Cookie extension (experimental) */ 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* 1741da177e4SLinus Torvalds * TCP option lengths 1751da177e4SLinus Torvalds */ 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds #define TCPOLEN_MSS 4 1781da177e4SLinus Torvalds #define TCPOLEN_WINDOW 3 1791da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM 2 1801da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP 10 181cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG 18 182435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_BASE 2 /* Cookie-less header extension */ 183435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_PAIR 3 /* Cookie pair header extension */ 184435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_MIN (TCPOLEN_COOKIE_BASE+TCP_COOKIE_MIN) 185435cf559SWilliam Allen Simpson #define TCPOLEN_COOKIE_MAX (TCPOLEN_COOKIE_BASE+TCP_COOKIE_MAX) 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* But this is what stacks really send out. */ 1881da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED 12 1891da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED 4 1901da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED 4 1911da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE 2 1921da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED 4 1931da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK 8 194cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED 20 19533ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED 4 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /* Flags in tp->nonagle */ 1981da177e4SLinus Torvalds #define TCP_NAGLE_OFF 1 /* Nagle's algo is disabled */ 1991da177e4SLinus Torvalds #define TCP_NAGLE_CORK 2 /* Socket is corked */ 200caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH 4 /* Cork is overridden for already queued data */ 2011da177e4SLinus Torvalds 20236e31b0aSAndreas Petlund /* TCP thin-stream limits */ 20336e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6 /* After 6 linear retries, do exp. backoff */ 20436e31b0aSAndreas Petlund 2057eb38527SDavid S. Miller /* TCP initial congestion window as per draft-hkchu-tcpm-initcwnd-01 */ 206442b9635SDavid S. Miller #define TCP_INIT_CWND 10 207442b9635SDavid S. Miller 208295ff7edSArnaldo Carvalho de Melo extern struct inet_timewait_death_row tcp_death_row; 209295ff7edSArnaldo Carvalho de Melo 2101da177e4SLinus Torvalds /* sysctl variables for tcp */ 2111da177e4SLinus Torvalds extern int sysctl_tcp_timestamps; 2121da177e4SLinus Torvalds extern int sysctl_tcp_window_scaling; 2131da177e4SLinus Torvalds extern int sysctl_tcp_sack; 2141da177e4SLinus Torvalds extern int sysctl_tcp_fin_timeout; 2151da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_time; 2161da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_probes; 2171da177e4SLinus Torvalds extern int sysctl_tcp_keepalive_intvl; 2181da177e4SLinus Torvalds extern int sysctl_tcp_syn_retries; 2191da177e4SLinus Torvalds extern int sysctl_tcp_synack_retries; 2201da177e4SLinus Torvalds extern int sysctl_tcp_retries1; 2211da177e4SLinus Torvalds extern int sysctl_tcp_retries2; 2221da177e4SLinus Torvalds extern int sysctl_tcp_orphan_retries; 2231da177e4SLinus Torvalds extern int sysctl_tcp_syncookies; 2241da177e4SLinus Torvalds extern int sysctl_tcp_retrans_collapse; 2251da177e4SLinus Torvalds extern int sysctl_tcp_stdurg; 2261da177e4SLinus Torvalds extern int sysctl_tcp_rfc1337; 2271da177e4SLinus Torvalds extern int sysctl_tcp_abort_on_overflow; 2281da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans; 2291da177e4SLinus Torvalds extern int sysctl_tcp_fack; 2301da177e4SLinus Torvalds extern int sysctl_tcp_reordering; 2311da177e4SLinus Torvalds extern int sysctl_tcp_ecn; 2321da177e4SLinus Torvalds extern int sysctl_tcp_dsack; 2331da177e4SLinus Torvalds extern int sysctl_tcp_wmem[3]; 2341da177e4SLinus Torvalds extern int sysctl_tcp_rmem[3]; 2351da177e4SLinus Torvalds extern int sysctl_tcp_app_win; 2361da177e4SLinus Torvalds extern int sysctl_tcp_adv_win_scale; 2371da177e4SLinus Torvalds extern int sysctl_tcp_tw_reuse; 2381da177e4SLinus Torvalds extern int sysctl_tcp_frto; 2393cfe3baaSIlpo Järvinen extern int sysctl_tcp_frto_response; 2401da177e4SLinus Torvalds extern int sysctl_tcp_low_latency; 24195937825SChris Leech extern int sysctl_tcp_dma_copybreak; 2421da177e4SLinus Torvalds extern int sysctl_tcp_nometrics_save; 2431da177e4SLinus Torvalds extern int sysctl_tcp_moderate_rcvbuf; 2441da177e4SLinus Torvalds extern int sysctl_tcp_tso_win_divisor; 2459772efb9SStephen Hemminger extern int sysctl_tcp_abc; 2465d424d5aSJohn Heffner extern int sysctl_tcp_mtu_probing; 2475d424d5aSJohn Heffner extern int sysctl_tcp_base_mss; 24815d99e02SRick Jones extern int sysctl_tcp_workaround_signed_windows; 24935089bb2SDavid S. Miller extern int sysctl_tcp_slow_start_after_idle; 250886236c1SJohn Heffner extern int sysctl_tcp_max_ssthresh; 251519855c5SWilliam Allen Simpson extern int sysctl_tcp_cookie_size; 25236e31b0aSAndreas Petlund extern int sysctl_tcp_thin_linear_timeouts; 2537e380175SAndreas Petlund extern int sysctl_tcp_thin_dupack; 2541da177e4SLinus Torvalds 2558d987e5cSEric Dumazet extern atomic_long_t tcp_memory_allocated; 2561748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated; 2571da177e4SLinus Torvalds extern int tcp_memory_pressure; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds /* 2601da177e4SLinus Torvalds * The next routines deal with comparing 32 bit unsigned ints 2611da177e4SLinus Torvalds * and worry about wraparound (automatic with unsigned arithmetic). 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds static inline int before(__u32 seq1, __u32 seq2) 2651da177e4SLinus Torvalds { 2660d630cc0SGerrit Renker return (__s32)(seq1-seq2) < 0; 2671da177e4SLinus Torvalds } 2689a036b9cSGerrit Renker #define after(seq2, seq1) before(seq1, seq2) 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */ 2711da177e4SLinus Torvalds static inline int between(__u32 seq1, __u32 seq2, __u32 seq3) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds return seq3 - seq2 >= seq1 - seq2; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 276efcdbf24SArun Sharma static inline bool tcp_out_of_memory(struct sock *sk) 277efcdbf24SArun Sharma { 278efcdbf24SArun Sharma if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF && 279efcdbf24SArun Sharma sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2)) 280efcdbf24SArun Sharma return true; 281efcdbf24SArun Sharma return false; 282efcdbf24SArun Sharma } 283efcdbf24SArun Sharma 284ad1af0feSDavid S. Miller static inline bool tcp_too_many_orphans(struct sock *sk, int shift) 285e4fd5da3SPavel Emelianov { 286ad1af0feSDavid S. Miller struct percpu_counter *ocp = sk->sk_prot->orphan_count; 287ad1af0feSDavid S. Miller int orphans = percpu_counter_read_positive(ocp); 288ad1af0feSDavid S. Miller 289ad1af0feSDavid S. Miller if (orphans << shift > sysctl_tcp_max_orphans) { 290ad1af0feSDavid S. Miller orphans = percpu_counter_sum_positive(ocp); 291ad1af0feSDavid S. Miller if (orphans << shift > sysctl_tcp_max_orphans) 292ad1af0feSDavid S. Miller return true; 293ad1af0feSDavid S. Miller } 294ad1af0feSDavid S. Miller return false; 295e4fd5da3SPavel Emelianov } 2961da177e4SLinus Torvalds 297efcdbf24SArun Sharma extern bool tcp_check_oom(struct sock *sk, int shift); 298efcdbf24SArun Sharma 299a0f82f64SFlorian Westphal /* syncookies: remember time of last synqueue overflow */ 300a0f82f64SFlorian Westphal static inline void tcp_synq_overflow(struct sock *sk) 301a0f82f64SFlorian Westphal { 302a0f82f64SFlorian Westphal tcp_sk(sk)->rx_opt.ts_recent_stamp = jiffies; 303a0f82f64SFlorian Westphal } 304a0f82f64SFlorian Westphal 305a0f82f64SFlorian Westphal /* syncookies: no recent synqueue overflow on this listening socket? */ 306a0f82f64SFlorian Westphal static inline int tcp_synq_no_recent_overflow(const struct sock *sk) 307a0f82f64SFlorian Westphal { 308a0f82f64SFlorian Westphal unsigned long last_overflow = tcp_sk(sk)->rx_opt.ts_recent_stamp; 3099ad7c049SJerry Chu return time_after(jiffies, last_overflow + TCP_TIMEOUT_FALLBACK); 310a0f82f64SFlorian Westphal } 311a0f82f64SFlorian Westphal 3121da177e4SLinus Torvalds extern struct proto tcp_prot; 3131da177e4SLinus Torvalds 31457ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.tcp_statistics, field) 31557ef42d5SPavel Emelyanov #define TCP_INC_STATS_BH(net, field) SNMP_INC_STATS_BH((net)->mib.tcp_statistics, field) 31657ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field) SNMP_DEC_STATS((net)->mib.tcp_statistics, field) 31757ef42d5SPavel Emelyanov #define TCP_ADD_STATS_USER(net, field, val) SNMP_ADD_STATS_USER((net)->mib.tcp_statistics, field, val) 318aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val) SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val) 3191da177e4SLinus Torvalds 3204acb4190SGlauber Costa extern void tcp_init_mem(struct net *net); 3214acb4190SGlauber Costa 3221da177e4SLinus Torvalds extern void tcp_v4_err(struct sk_buff *skb, u32); 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds extern void tcp_shutdown (struct sock *sk, int how); 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds extern int tcp_v4_rcv(struct sk_buff *skb); 3271da177e4SLinus Torvalds 3283f419d2dSDavid S. Miller extern struct inet_peer *tcp_v4_get_peer(struct sock *sk, bool *release_it); 329ccb7c410SDavid S. Miller extern void *tcp_v4_tw_get_peer(struct sock *sk); 3308feaf0c0SArnaldo Carvalho de Melo extern int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw); 3317ba42910SChangli Gao extern int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 3327ba42910SChangli Gao size_t size); 3337ba42910SChangli Gao extern int tcp_sendpage(struct sock *sk, struct page *page, int offset, 33453d3176bSChangli Gao size_t size, int flags); 33553d3176bSChangli Gao extern int tcp_ioctl(struct sock *sk, int cmd, unsigned long arg); 33653d3176bSChangli Gao extern int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb, 337cf533ea5SEric Dumazet const struct tcphdr *th, unsigned int len); 33853d3176bSChangli Gao extern int tcp_rcv_established(struct sock *sk, struct sk_buff *skb, 339cf533ea5SEric Dumazet const struct tcphdr *th, unsigned int len); 3401da177e4SLinus Torvalds extern void tcp_rcv_space_adjust(struct sock *sk); 3410e4b4992SChris Leech extern void tcp_cleanup_rbuf(struct sock *sk, int copied); 34253d3176bSChangli Gao extern int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp); 343cfb6eeb4SYOSHIFUJI Hideaki extern void tcp_twsk_destructor(struct sock *sk); 3449c55e01cSJens Axboe extern ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos, 34553d3176bSChangli Gao struct pipe_inode_info *pipe, size_t len, 34653d3176bSChangli Gao unsigned int flags); 3479c55e01cSJens Axboe 348463c84b9SArnaldo Carvalho de Melo static inline void tcp_dec_quickack_mode(struct sock *sk, 349463c84b9SArnaldo Carvalho de Melo const unsigned int pkts) 3501da177e4SLinus Torvalds { 351463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 352fc6415bcSDavid S. Miller 353463c84b9SArnaldo Carvalho de Melo if (icsk->icsk_ack.quick) { 354463c84b9SArnaldo Carvalho de Melo if (pkts >= icsk->icsk_ack.quick) { 355463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.quick = 0; 3561da177e4SLinus Torvalds /* Leaving quickack mode we deflate ATO. */ 357463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN; 358fc6415bcSDavid S. Miller } else 359463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.quick -= pkts; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds 363bdf1ee5dSIlpo Järvinen #define TCP_ECN_OK 1 364bdf1ee5dSIlpo Järvinen #define TCP_ECN_QUEUE_CWR 2 365bdf1ee5dSIlpo Järvinen #define TCP_ECN_DEMAND_CWR 4 3667a269ffaSEric Dumazet #define TCP_ECN_SEEN 8 367bdf1ee5dSIlpo Järvinen 368bdf1ee5dSIlpo Järvinen static __inline__ void 369bdf1ee5dSIlpo Järvinen TCP_ECN_create_request(struct request_sock *req, struct tcphdr *th) 370bdf1ee5dSIlpo Järvinen { 371bdf1ee5dSIlpo Järvinen if (sysctl_tcp_ecn && th->ece && th->cwr) 372bdf1ee5dSIlpo Järvinen inet_rsk(req)->ecn_ok = 1; 373bdf1ee5dSIlpo Järvinen } 374bdf1ee5dSIlpo Järvinen 375fd2c3ef7SEric Dumazet enum tcp_tw_status { 3761da177e4SLinus Torvalds TCP_TW_SUCCESS = 0, 3771da177e4SLinus Torvalds TCP_TW_RST = 1, 3781da177e4SLinus Torvalds TCP_TW_ACK = 2, 3791da177e4SLinus Torvalds TCP_TW_SYN = 3 3801da177e4SLinus Torvalds }; 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds 3838feaf0c0SArnaldo Carvalho de Melo extern enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw, 3841da177e4SLinus Torvalds struct sk_buff *skb, 3858feaf0c0SArnaldo Carvalho de Melo const struct tcphdr *th); 3861da177e4SLinus Torvalds extern struct sock * tcp_check_req(struct sock *sk,struct sk_buff *skb, 38760236fddSArnaldo Carvalho de Melo struct request_sock *req, 38860236fddSArnaldo Carvalho de Melo struct request_sock **prev); 38953d3176bSChangli Gao extern int tcp_child_process(struct sock *parent, struct sock *child, 3901da177e4SLinus Torvalds struct sk_buff *skb); 39146d0de4eSIlpo Järvinen extern int tcp_use_frto(struct sock *sk); 3921da177e4SLinus Torvalds extern void tcp_enter_frto(struct sock *sk); 3931da177e4SLinus Torvalds extern void tcp_enter_loss(struct sock *sk, int how); 3941da177e4SLinus Torvalds extern void tcp_clear_retrans(struct tcp_sock *tp); 3951da177e4SLinus Torvalds extern void tcp_update_metrics(struct sock *sk); 39653d3176bSChangli Gao extern void tcp_close(struct sock *sk, long timeout); 39753d3176bSChangli Gao extern unsigned int tcp_poll(struct file * file, struct socket *sock, 39853d3176bSChangli Gao struct poll_table_struct *wait); 39953d3176bSChangli Gao extern int tcp_getsockopt(struct sock *sk, int level, int optname, 4003fdadf7dSDmitry Mishin char __user *optval, int __user *optlen); 40153d3176bSChangli Gao extern int tcp_setsockopt(struct sock *sk, int level, int optname, 40253d3176bSChangli Gao char __user *optval, unsigned int optlen); 40353d3176bSChangli Gao extern int compat_tcp_getsockopt(struct sock *sk, int level, int optname, 40453d3176bSChangli Gao char __user *optval, int __user *optlen); 40553d3176bSChangli Gao extern int compat_tcp_setsockopt(struct sock *sk, int level, int optname, 406b7058842SDavid S. Miller char __user *optval, unsigned int optlen); 4071da177e4SLinus Torvalds extern void tcp_set_keepalive(struct sock *sk, int val); 40853d3176bSChangli Gao extern void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req); 40953d3176bSChangli Gao extern int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 41053d3176bSChangli Gao size_t len, int nonblock, int flags, int *addr_len); 411cf533ea5SEric Dumazet extern void tcp_parse_options(const struct sk_buff *skb, 412cf533ea5SEric Dumazet struct tcp_options_received *opt_rx, const u8 **hvpp, 413bb5b7c11SDavid S. Miller int estab); 414cf533ea5SEric Dumazet extern const u8 *tcp_parse_md5sig_option(const struct tcphdr *th); 4157d5d5525SYOSHIFUJI Hideaki 4161da177e4SLinus Torvalds /* 4171da177e4SLinus Torvalds * TCP v4 functions exported for the inet6 API 4181da177e4SLinus Torvalds */ 4191da177e4SLinus Torvalds 42053d3176bSChangli Gao extern void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb); 42153d3176bSChangli Gao extern int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb); 4221da177e4SLinus Torvalds extern struct sock * tcp_create_openreq_child(struct sock *sk, 42360236fddSArnaldo Carvalho de Melo struct request_sock *req, 4241da177e4SLinus Torvalds struct sk_buff *skb); 42553d3176bSChangli Gao extern struct sock * tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb, 42660236fddSArnaldo Carvalho de Melo struct request_sock *req, 4271da177e4SLinus Torvalds struct dst_entry *dst); 42853d3176bSChangli Gao extern int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb); 42953d3176bSChangli Gao extern int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, 4301da177e4SLinus Torvalds int addr_len); 4311da177e4SLinus Torvalds extern int tcp_connect(struct sock *sk); 43253d3176bSChangli Gao extern struct sk_buff * tcp_make_synack(struct sock *sk, struct dst_entry *dst, 433e6b4d113SWilliam Allen Simpson struct request_sock *req, 434e6b4d113SWilliam Allen Simpson struct request_values *rvp); 4351da177e4SLinus Torvalds extern int tcp_disconnect(struct sock *sk, int flags); 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds /* From syncookies.c */ 4392051f11fSFlorian Westphal extern __u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS]; 4401da177e4SLinus Torvalds extern struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb, 4411da177e4SLinus Torvalds struct ip_options *opt); 442e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES 4431da177e4SLinus Torvalds extern __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb, 4441da177e4SLinus Torvalds __u16 *mss); 445e05c82d3SEric Dumazet #else 446e05c82d3SEric Dumazet static inline __u32 cookie_v4_init_sequence(struct sock *sk, 447e05c82d3SEric Dumazet struct sk_buff *skb, 448e05c82d3SEric Dumazet __u16 *mss) 449e05c82d3SEric Dumazet { 450e05c82d3SEric Dumazet return 0; 451e05c82d3SEric Dumazet } 452e05c82d3SEric Dumazet #endif 4531da177e4SLinus Torvalds 4544dfc2817SFlorian Westphal extern __u32 cookie_init_timestamp(struct request_sock *req); 455172d69e6SFlorian Westphal extern bool cookie_check_timestamp(struct tcp_options_received *opt, bool *); 4564dfc2817SFlorian Westphal 457c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */ 458c6aefafbSGlenn Griffin extern struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb); 459e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES 460cf533ea5SEric Dumazet extern __u32 cookie_v6_init_sequence(struct sock *sk, const struct sk_buff *skb, 461c6aefafbSGlenn Griffin __u16 *mss); 462e05c82d3SEric Dumazet #else 463e05c82d3SEric Dumazet static inline __u32 cookie_v6_init_sequence(struct sock *sk, 464e05c82d3SEric Dumazet struct sk_buff *skb, 465e05c82d3SEric Dumazet __u16 *mss) 466e05c82d3SEric Dumazet { 467e05c82d3SEric Dumazet return 0; 468e05c82d3SEric Dumazet } 469e05c82d3SEric Dumazet #endif 4701da177e4SLinus Torvalds /* tcp_output.c */ 4711da177e4SLinus Torvalds 4729e412ba7SIlpo Järvinen extern void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss, 4739e412ba7SIlpo Järvinen int nonagle); 4749e412ba7SIlpo Järvinen extern int tcp_may_send_now(struct sock *sk); 4751da177e4SLinus Torvalds extern int tcp_retransmit_skb(struct sock *, struct sk_buff *); 476f1ecd5d9SDamian Lukowski extern void tcp_retransmit_timer(struct sock *sk); 4771da177e4SLinus Torvalds extern void tcp_xmit_retransmit_queue(struct sock *); 4781da177e4SLinus Torvalds extern void tcp_simple_retransmit(struct sock *); 4791da177e4SLinus Torvalds extern int tcp_trim_head(struct sock *, struct sk_buff *, u32); 4806475be16SDavid S. Miller extern int tcp_fragment(struct sock *, struct sk_buff *, u32, unsigned int); 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds extern void tcp_send_probe0(struct sock *); 4831da177e4SLinus Torvalds extern void tcp_send_partial(struct sock *); 4841da177e4SLinus Torvalds extern int tcp_write_wakeup(struct sock *); 4851da177e4SLinus Torvalds extern void tcp_send_fin(struct sock *sk); 486dd0fc66fSAl Viro extern void tcp_send_active_reset(struct sock *sk, gfp_t priority); 4871da177e4SLinus Torvalds extern int tcp_send_synack(struct sock *); 488946cedccSEric Dumazet extern int tcp_syn_flood_action(struct sock *sk, 489946cedccSEric Dumazet const struct sk_buff *skb, 490946cedccSEric Dumazet const char *proto); 491c1b4a7e6SDavid S. Miller extern void tcp_push_one(struct sock *, unsigned int mss_now); 4921da177e4SLinus Torvalds extern void tcp_send_ack(struct sock *sk); 4931da177e4SLinus Torvalds extern void tcp_send_delayed_ack(struct sock *sk); 4941da177e4SLinus Torvalds 495a762a980SDavid S. Miller /* tcp_input.c */ 496a762a980SDavid S. Miller extern void tcp_cwnd_application_limited(struct sock *sk); 497a762a980SDavid S. Miller 4981da177e4SLinus Torvalds /* tcp_timer.c */ 4991da177e4SLinus Torvalds extern void tcp_init_xmit_timers(struct sock *); 500463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk) 501463c84b9SArnaldo Carvalho de Melo { 502463c84b9SArnaldo Carvalho de Melo inet_csk_clear_xmit_timers(sk); 503463c84b9SArnaldo Carvalho de Melo } 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds extern unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu); 5060c54b85fSIlpo Järvinen extern unsigned int tcp_current_mss(struct sock *sk); 5070c54b85fSIlpo Järvinen 5080c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */ 5090c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize) 5100c54b85fSIlpo Järvinen { 51101f83d69SAlexey Kuznetsov int cutoff; 51201f83d69SAlexey Kuznetsov 51301f83d69SAlexey Kuznetsov /* When peer uses tiny windows, there is no use in packetizing 51401f83d69SAlexey Kuznetsov * to sub-MSS pieces for the sake of SWS or making sure there 51501f83d69SAlexey Kuznetsov * are enough packets in the pipe for fast recovery. 51601f83d69SAlexey Kuznetsov * 51701f83d69SAlexey Kuznetsov * On the other hand, for extremely large MSS devices, handling 51801f83d69SAlexey Kuznetsov * smaller than MSS windows in this way does make sense. 51901f83d69SAlexey Kuznetsov */ 52001f83d69SAlexey Kuznetsov if (tp->max_window >= 512) 52101f83d69SAlexey Kuznetsov cutoff = (tp->max_window >> 1); 52201f83d69SAlexey Kuznetsov else 52301f83d69SAlexey Kuznetsov cutoff = tp->max_window; 52401f83d69SAlexey Kuznetsov 52501f83d69SAlexey Kuznetsov if (cutoff && pktsize > cutoff) 52601f83d69SAlexey Kuznetsov return max_t(int, cutoff, 68U - tp->tcp_header_len); 5270c54b85fSIlpo Järvinen else 5280c54b85fSIlpo Järvinen return pktsize; 5290c54b85fSIlpo Järvinen } 5301da177e4SLinus Torvalds 53117b085eaSArnaldo Carvalho de Melo /* tcp.c */ 532cf533ea5SEric Dumazet extern void tcp_get_info(const struct sock *, struct tcp_info *); 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */ 5351da177e4SLinus Torvalds typedef int (*sk_read_actor_t)(read_descriptor_t *, struct sk_buff *, 5361da177e4SLinus Torvalds unsigned int, size_t); 5371da177e4SLinus Torvalds extern int tcp_read_sock(struct sock *sk, read_descriptor_t *desc, 5381da177e4SLinus Torvalds sk_read_actor_t recv_actor); 5391da177e4SLinus Torvalds 54040efc6faSStephen Hemminger extern void tcp_initialize_rcv_mss(struct sock *sk); 5411da177e4SLinus Torvalds 542cf533ea5SEric Dumazet extern int tcp_mtu_to_mss(const struct sock *sk, int pmtu); 543cf533ea5SEric Dumazet extern int tcp_mss_to_mtu(const struct sock *sk, int mss); 5445d424d5aSJohn Heffner extern void tcp_mtup_init(struct sock *sk); 5459ad7c049SJerry Chu extern void tcp_valid_rtt_meas(struct sock *sk, u32 seq_rtt); 5465d424d5aSJohn Heffner 547f1ecd5d9SDamian Lukowski static inline void tcp_bound_rto(const struct sock *sk) 548f1ecd5d9SDamian Lukowski { 549f1ecd5d9SDamian Lukowski if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX) 550f1ecd5d9SDamian Lukowski inet_csk(sk)->icsk_rto = TCP_RTO_MAX; 551f1ecd5d9SDamian Lukowski } 552f1ecd5d9SDamian Lukowski 553f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp) 554f1ecd5d9SDamian Lukowski { 555f1ecd5d9SDamian Lukowski return (tp->srtt >> 3) + tp->rttvar; 556f1ecd5d9SDamian Lukowski } 557f1ecd5d9SDamian Lukowski 55840efc6faSStephen Hemminger static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd) 5591da177e4SLinus Torvalds { 5601da177e4SLinus Torvalds tp->pred_flags = htonl((tp->tcp_header_len << 26) | 5611da177e4SLinus Torvalds ntohl(TCP_FLAG_ACK) | 5621da177e4SLinus Torvalds snd_wnd); 5631da177e4SLinus Torvalds } 5641da177e4SLinus Torvalds 56540efc6faSStephen Hemminger static inline void tcp_fast_path_on(struct tcp_sock *tp) 5661da177e4SLinus Torvalds { 5671da177e4SLinus Torvalds __tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5709e412ba7SIlpo Järvinen static inline void tcp_fast_path_check(struct sock *sk) 5711da177e4SLinus Torvalds { 5729e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 5739e412ba7SIlpo Järvinen 574b03efcfbSDavid S. Miller if (skb_queue_empty(&tp->out_of_order_queue) && 5751da177e4SLinus Torvalds tp->rcv_wnd && 5761da177e4SLinus Torvalds atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf && 5771da177e4SLinus Torvalds !tp->urg_data) 5781da177e4SLinus Torvalds tcp_fast_path_on(tp); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5810c266898SSatoru SATOH /* Compute the actual rto_min value */ 5820c266898SSatoru SATOH static inline u32 tcp_rto_min(struct sock *sk) 5830c266898SSatoru SATOH { 584cf533ea5SEric Dumazet const struct dst_entry *dst = __sk_dst_get(sk); 5850c266898SSatoru SATOH u32 rto_min = TCP_RTO_MIN; 5860c266898SSatoru SATOH 5870c266898SSatoru SATOH if (dst && dst_metric_locked(dst, RTAX_RTO_MIN)) 5880c266898SSatoru SATOH rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN); 5890c266898SSatoru SATOH return rto_min; 5900c266898SSatoru SATOH } 5910c266898SSatoru SATOH 5921da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising. 5931da177e4SLinus Torvalds * Rcv_nxt can be after the window if our peer push more data 5941da177e4SLinus Torvalds * than the offered window. 5951da177e4SLinus Torvalds */ 59640efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp) 5971da177e4SLinus Torvalds { 5981da177e4SLinus Torvalds s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds if (win < 0) 6011da177e4SLinus Torvalds win = 0; 6021da177e4SLinus Torvalds return (u32) win; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without 6061da177e4SLinus Torvalds * scaling applied to the result. The caller does these things 6071da177e4SLinus Torvalds * if necessary. This is a "raw" window selection. 6081da177e4SLinus Torvalds */ 6091da177e4SLinus Torvalds extern u32 __tcp_select_window(struct sock *sk); 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds /* TCP timestamps are only 32-bits, this causes a slight 6121da177e4SLinus Torvalds * complication on 64-bit systems since we store a snapshot 61331f34269SStephen Hemminger * of jiffies in the buffer control blocks below. We decided 61431f34269SStephen Hemminger * to use only the low 32-bits of jiffies and hide the ugly 6151da177e4SLinus Torvalds * casts with the following macro. 6161da177e4SLinus Torvalds */ 6171da177e4SLinus Torvalds #define tcp_time_stamp ((__u32)(jiffies)) 6181da177e4SLinus Torvalds 619a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13]) 620a3433f35SChangli Gao 621a3433f35SChangli Gao #define TCPHDR_FIN 0x01 622a3433f35SChangli Gao #define TCPHDR_SYN 0x02 623a3433f35SChangli Gao #define TCPHDR_RST 0x04 624a3433f35SChangli Gao #define TCPHDR_PSH 0x08 625a3433f35SChangli Gao #define TCPHDR_ACK 0x10 626a3433f35SChangli Gao #define TCPHDR_URG 0x20 627a3433f35SChangli Gao #define TCPHDR_ECE 0x40 628a3433f35SChangli Gao #define TCPHDR_CWR 0x80 629a3433f35SChangli Gao 630caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass 631f86586faSEric Dumazet * TCP per-packet control information to the transmission code. 632f86586faSEric Dumazet * We also store the host-order sequence numbers in here too. 633f86586faSEric Dumazet * This is 44 bytes if IPV6 is enabled. 634f86586faSEric Dumazet * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately. 6351da177e4SLinus Torvalds */ 6361da177e4SLinus Torvalds struct tcp_skb_cb { 6371da177e4SLinus Torvalds union { 6381da177e4SLinus Torvalds struct inet_skb_parm h4; 639dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 6401da177e4SLinus Torvalds struct inet6_skb_parm h6; 6411da177e4SLinus Torvalds #endif 6421da177e4SLinus Torvalds } header; /* For incoming frames */ 6431da177e4SLinus Torvalds __u32 seq; /* Starting sequence number */ 6441da177e4SLinus Torvalds __u32 end_seq; /* SEQ + FIN + SYN + datalen */ 6451da177e4SLinus Torvalds __u32 when; /* used to compute rtt's */ 6464de075e0SEric Dumazet __u8 tcp_flags; /* TCP header flags. (tcp[13]) */ 6471da177e4SLinus Torvalds __u8 sacked; /* State flags for SACK/FACK. */ 6481da177e4SLinus Torvalds #define TCPCB_SACKED_ACKED 0x01 /* SKB ACK'd by a SACK block */ 6491da177e4SLinus Torvalds #define TCPCB_SACKED_RETRANS 0x02 /* SKB retransmitted */ 6501da177e4SLinus Torvalds #define TCPCB_LOST 0x04 /* SKB is lost */ 6511da177e4SLinus Torvalds #define TCPCB_TAGBITS 0x07 /* All tag bits */ 652b82d1bb4SEric Dumazet __u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */ 653b82d1bb4SEric Dumazet /* 1 byte hole */ 6541da177e4SLinus Torvalds #define TCPCB_EVER_RETRANS 0x80 /* Ever retransmitted frame */ 6551da177e4SLinus Torvalds #define TCPCB_RETRANS (TCPCB_SACKED_RETRANS|TCPCB_EVER_RETRANS) 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds __u32 ack_seq; /* Sequence number ACK'd */ 6581da177e4SLinus Torvalds }; 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds #define TCP_SKB_CB(__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0])) 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual 6631da177e4SLinus Torvalds * packets. To keep these tracked properly, we use this. 6641da177e4SLinus Torvalds */ 6651da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb) 6661da177e4SLinus Torvalds { 6677967168cSHerbert Xu return skb_shinfo(skb)->gso_segs; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds /* This is valid iff tcp_skb_pcount() > 1. */ 6711da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb) 6721da177e4SLinus Torvalds { 6737967168cSHerbert Xu return skb_shinfo(skb)->gso_size; 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 676317a76f9SStephen Hemminger /* Events passed to congestion control interface */ 677317a76f9SStephen Hemminger enum tcp_ca_event { 678317a76f9SStephen Hemminger CA_EVENT_TX_START, /* first transmit when no packets in flight */ 679317a76f9SStephen Hemminger CA_EVENT_CWND_RESTART, /* congestion window restart */ 680317a76f9SStephen Hemminger CA_EVENT_COMPLETE_CWR, /* end of congestion recovery */ 681317a76f9SStephen Hemminger CA_EVENT_FRTO, /* fast recovery timeout */ 682317a76f9SStephen Hemminger CA_EVENT_LOSS, /* loss timeout */ 683317a76f9SStephen Hemminger CA_EVENT_FAST_ACK, /* in sequence ack */ 684317a76f9SStephen Hemminger CA_EVENT_SLOW_ACK, /* other ack */ 685317a76f9SStephen Hemminger }; 686317a76f9SStephen Hemminger 687317a76f9SStephen Hemminger /* 688317a76f9SStephen Hemminger * Interface for adding new TCP congestion control handlers 689317a76f9SStephen Hemminger */ 690317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX 16 6913ff825b2SStephen Hemminger #define TCP_CA_MAX 128 6923ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX (TCP_CA_NAME_MAX*TCP_CA_MAX) 6933ff825b2SStephen Hemminger 694164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1 695164891aaSStephen Hemminger #define TCP_CONG_RTT_STAMP 0x2 696164891aaSStephen Hemminger 697317a76f9SStephen Hemminger struct tcp_congestion_ops { 698317a76f9SStephen Hemminger struct list_head list; 699164891aaSStephen Hemminger unsigned long flags; 700317a76f9SStephen Hemminger 701317a76f9SStephen Hemminger /* initialize private data (optional) */ 7026687e988SArnaldo Carvalho de Melo void (*init)(struct sock *sk); 703317a76f9SStephen Hemminger /* cleanup private data (optional) */ 7046687e988SArnaldo Carvalho de Melo void (*release)(struct sock *sk); 705317a76f9SStephen Hemminger 706317a76f9SStephen Hemminger /* return slow start threshold (required) */ 7076687e988SArnaldo Carvalho de Melo u32 (*ssthresh)(struct sock *sk); 708317a76f9SStephen Hemminger /* lower bound for congestion window (optional) */ 70972dc5b92SStephen Hemminger u32 (*min_cwnd)(const struct sock *sk); 710317a76f9SStephen Hemminger /* do new cwnd calculation (required) */ 711c3a05c60SIlpo Järvinen void (*cong_avoid)(struct sock *sk, u32 ack, u32 in_flight); 712317a76f9SStephen Hemminger /* call before changing ca_state (optional) */ 7136687e988SArnaldo Carvalho de Melo void (*set_state)(struct sock *sk, u8 new_state); 714317a76f9SStephen Hemminger /* call when cwnd event occurs (optional) */ 7156687e988SArnaldo Carvalho de Melo void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev); 716317a76f9SStephen Hemminger /* new value of cwnd after loss (optional) */ 7176687e988SArnaldo Carvalho de Melo u32 (*undo_cwnd)(struct sock *sk); 718317a76f9SStephen Hemminger /* hook for packet ack accounting (optional) */ 71930cfd0baSStephen Hemminger void (*pkts_acked)(struct sock *sk, u32 num_acked, s32 rtt_us); 72073c1f4a0SArnaldo Carvalho de Melo /* get info for inet_diag (optional) */ 7216687e988SArnaldo Carvalho de Melo void (*get_info)(struct sock *sk, u32 ext, struct sk_buff *skb); 722317a76f9SStephen Hemminger 723317a76f9SStephen Hemminger char name[TCP_CA_NAME_MAX]; 724317a76f9SStephen Hemminger struct module *owner; 725317a76f9SStephen Hemminger }; 726317a76f9SStephen Hemminger 727317a76f9SStephen Hemminger extern int tcp_register_congestion_control(struct tcp_congestion_ops *type); 728317a76f9SStephen Hemminger extern void tcp_unregister_congestion_control(struct tcp_congestion_ops *type); 729317a76f9SStephen Hemminger 7306687e988SArnaldo Carvalho de Melo extern void tcp_init_congestion_control(struct sock *sk); 7316687e988SArnaldo Carvalho de Melo extern void tcp_cleanup_congestion_control(struct sock *sk); 732317a76f9SStephen Hemminger extern int tcp_set_default_congestion_control(const char *name); 733317a76f9SStephen Hemminger extern void tcp_get_default_congestion_control(char *name); 7343ff825b2SStephen Hemminger extern void tcp_get_available_congestion_control(char *buf, size_t len); 735ce7bc3bfSStephen Hemminger extern void tcp_get_allowed_congestion_control(char *buf, size_t len); 736ce7bc3bfSStephen Hemminger extern int tcp_set_allowed_congestion_control(char *allowed); 7376687e988SArnaldo Carvalho de Melo extern int tcp_set_congestion_control(struct sock *sk, const char *name); 73840efc6faSStephen Hemminger extern void tcp_slow_start(struct tcp_sock *tp); 739758ce5c8SIlpo Järvinen extern void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w); 740317a76f9SStephen Hemminger 7415f8ef48dSStephen Hemminger extern struct tcp_congestion_ops tcp_init_congestion_ops; 7426687e988SArnaldo Carvalho de Melo extern u32 tcp_reno_ssthresh(struct sock *sk); 743c3a05c60SIlpo Järvinen extern void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight); 74472dc5b92SStephen Hemminger extern u32 tcp_reno_min_cwnd(const struct sock *sk); 745a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno; 746317a76f9SStephen Hemminger 7476687e988SArnaldo Carvalho de Melo static inline void tcp_set_ca_state(struct sock *sk, const u8 ca_state) 748317a76f9SStephen Hemminger { 7496687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 7506687e988SArnaldo Carvalho de Melo 7516687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_ops->set_state) 7526687e988SArnaldo Carvalho de Melo icsk->icsk_ca_ops->set_state(sk, ca_state); 7536687e988SArnaldo Carvalho de Melo icsk->icsk_ca_state = ca_state; 754317a76f9SStephen Hemminger } 755317a76f9SStephen Hemminger 7566687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event) 757317a76f9SStephen Hemminger { 7586687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 7596687e988SArnaldo Carvalho de Melo 7606687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_ops->cwnd_event) 7616687e988SArnaldo Carvalho de Melo icsk->icsk_ca_ops->cwnd_event(sk, event); 762317a76f9SStephen Hemminger } 763317a76f9SStephen Hemminger 764e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK 765e60402d0SIlpo Järvinen * handling. SACK is negotiated with the peer, and therefore it can vary 766e60402d0SIlpo Järvinen * between different flows. 767e60402d0SIlpo Järvinen * 768e60402d0SIlpo Järvinen * tcp_is_sack - SACK enabled 769e60402d0SIlpo Järvinen * tcp_is_reno - No SACK 770e60402d0SIlpo Järvinen * tcp_is_fack - FACK enabled, implies SACK enabled 771e60402d0SIlpo Järvinen */ 772e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp) 773e60402d0SIlpo Järvinen { 774e60402d0SIlpo Järvinen return tp->rx_opt.sack_ok; 775e60402d0SIlpo Järvinen } 776e60402d0SIlpo Järvinen 777e60402d0SIlpo Järvinen static inline int tcp_is_reno(const struct tcp_sock *tp) 778e60402d0SIlpo Järvinen { 779e60402d0SIlpo Järvinen return !tcp_is_sack(tp); 780e60402d0SIlpo Järvinen } 781e60402d0SIlpo Järvinen 782e60402d0SIlpo Järvinen static inline int tcp_is_fack(const struct tcp_sock *tp) 783e60402d0SIlpo Järvinen { 784ab56222aSVijay Subramanian return tp->rx_opt.sack_ok & TCP_FACK_ENABLED; 785e60402d0SIlpo Järvinen } 786e60402d0SIlpo Järvinen 787e60402d0SIlpo Järvinen static inline void tcp_enable_fack(struct tcp_sock *tp) 788e60402d0SIlpo Järvinen { 789ab56222aSVijay Subramanian tp->rx_opt.sack_ok |= TCP_FACK_ENABLED; 790e60402d0SIlpo Järvinen } 791e60402d0SIlpo Järvinen 79283ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp) 79383ae4088SIlpo Järvinen { 79483ae4088SIlpo Järvinen return tp->sacked_out + tp->lost_out; 79583ae4088SIlpo Järvinen } 79683ae4088SIlpo Järvinen 7971da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best 7981da177e4SLinus Torvalds * of our knowledge. In many cases it is conservative, but where 7991da177e4SLinus Torvalds * detailed information is available from the receiver (via SACK 8001da177e4SLinus Torvalds * blocks etc.) we can make more aggressive calculations. 8011da177e4SLinus Torvalds * 8021da177e4SLinus Torvalds * Use this for decisions involving congestion control, use just 8031da177e4SLinus Torvalds * tp->packets_out to determine if the send queue is empty or not. 8041da177e4SLinus Torvalds * 8051da177e4SLinus Torvalds * Read this equation as: 8061da177e4SLinus Torvalds * 8071da177e4SLinus Torvalds * "Packets sent once on transmission queue" MINUS 8081da177e4SLinus Torvalds * "Packets left network, but not honestly ACKed yet" PLUS 8091da177e4SLinus Torvalds * "Packets fast retransmitted" 8101da177e4SLinus Torvalds */ 81140efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp) 8121da177e4SLinus Torvalds { 81383ae4088SIlpo Järvinen return tp->packets_out - tcp_left_out(tp) + tp->retrans_out; 8141da177e4SLinus Torvalds } 8151da177e4SLinus Torvalds 8160b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH 0x7fffffff 8170b6a05c1SIlpo Järvinen 8180b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp) 8190b6a05c1SIlpo Järvinen { 8200b6a05c1SIlpo Järvinen return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH; 8210b6a05c1SIlpo Järvinen } 8220b6a05c1SIlpo Järvinen 8231da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd. 8241da177e4SLinus Torvalds * The exception is rate halving phase, when cwnd is decreasing towards 8251da177e4SLinus Torvalds * ssthresh. 8261da177e4SLinus Torvalds */ 8276687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk) 8281da177e4SLinus Torvalds { 8296687e988SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 830cf533ea5SEric Dumazet 8316687e988SArnaldo Carvalho de Melo if ((1 << inet_csk(sk)->icsk_ca_state) & (TCPF_CA_CWR | TCPF_CA_Recovery)) 8321da177e4SLinus Torvalds return tp->snd_ssthresh; 8331da177e4SLinus Torvalds else 8341da177e4SLinus Torvalds return max(tp->snd_ssthresh, 8351da177e4SLinus Torvalds ((tp->snd_cwnd >> 1) + 8361da177e4SLinus Torvalds (tp->snd_cwnd >> 2))); 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 839b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */ 840b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp) WARN_ON(tcp_left_out(tp) > tp->packets_out) 8411da177e4SLinus Torvalds 8423cfe3baaSIlpo Järvinen extern void tcp_enter_cwr(struct sock *sk, const int set_ssthresh); 843cf533ea5SEric Dumazet extern __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst); 8441da177e4SLinus Torvalds 8456b5a5c0dSNeal Cardwell /* The maximum number of MSS of available cwnd for which TSO defers 8466b5a5c0dSNeal Cardwell * sending if not using sysctl_tcp_tso_win_divisor. 8476b5a5c0dSNeal Cardwell */ 8486b5a5c0dSNeal Cardwell static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp) 8496b5a5c0dSNeal Cardwell { 8506b5a5c0dSNeal Cardwell return 3; 8516b5a5c0dSNeal Cardwell } 8526b5a5c0dSNeal Cardwell 8531da177e4SLinus Torvalds /* Slow start with delack produces 3 packets of burst, so that 854dd9e0ddaSJohn Heffner * it is safe "de facto". This will be the default - same as 855dd9e0ddaSJohn Heffner * the default reordering threshold - but if reordering increases, 856dd9e0ddaSJohn Heffner * we must be able to allow cwnd to burst at least this much in order 857dd9e0ddaSJohn Heffner * to not pull it back when holes are filled. 8581da177e4SLinus Torvalds */ 8591da177e4SLinus Torvalds static __inline__ __u32 tcp_max_burst(const struct tcp_sock *tp) 8601da177e4SLinus Torvalds { 861dd9e0ddaSJohn Heffner return tp->reordering; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 86490840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */ 86590840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp) 86690840defSIlpo Järvinen { 86790840defSIlpo Järvinen return tp->snd_una + tp->snd_wnd; 86890840defSIlpo Järvinen } 869cea14e0eSIlpo Järvinen extern int tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight); 870f4805edeSStephen Hemminger 871c1bd24b7SChuck Lever static inline void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss, 8721da177e4SLinus Torvalds const struct sk_buff *skb) 8731da177e4SLinus Torvalds { 8741da177e4SLinus Torvalds if (skb->len < mss) 8751da177e4SLinus Torvalds tp->snd_sml = TCP_SKB_CB(skb)->end_seq; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 8789e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk) 8791da177e4SLinus Torvalds { 880cf533ea5SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 881463c84b9SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 8829e412ba7SIlpo Järvinen 883463c84b9SArnaldo Carvalho de Melo if (!tp->packets_out && !icsk->icsk_pending) 8843f421baaSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0, 8853f421baaSArnaldo Carvalho de Melo icsk->icsk_rto, TCP_RTO_MAX); 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 888ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq) 8891da177e4SLinus Torvalds { 8901da177e4SLinus Torvalds tp->snd_wl1 = seq; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 893ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq) 8941da177e4SLinus Torvalds { 8951da177e4SLinus Torvalds tp->snd_wl1 = seq; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds /* 8991da177e4SLinus Torvalds * Calculate(/check) TCP checksum 9001da177e4SLinus Torvalds */ 901ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr, 902ba7808eaSFrederik Deweerdt __be32 daddr, __wsum base) 9031da177e4SLinus Torvalds { 9041da177e4SLinus Torvalds return csum_tcpudp_magic(saddr,daddr,len,IPPROTO_TCP,base); 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 907b51655b9SAl Viro static inline __sum16 __tcp_checksum_complete(struct sk_buff *skb) 9081da177e4SLinus Torvalds { 909fb286bb2SHerbert Xu return __skb_checksum_complete(skb); 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 91240efc6faSStephen Hemminger static inline int tcp_checksum_complete(struct sk_buff *skb) 9131da177e4SLinus Torvalds { 91460476372SHerbert Xu return !skb_csum_unnecessary(skb) && 9151da177e4SLinus Torvalds __tcp_checksum_complete(skb); 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds /* Prequeue for VJ style copy to user, combined with checksumming. */ 9191da177e4SLinus Torvalds 92040efc6faSStephen Hemminger static inline void tcp_prequeue_init(struct tcp_sock *tp) 9211da177e4SLinus Torvalds { 9221da177e4SLinus Torvalds tp->ucopy.task = NULL; 9231da177e4SLinus Torvalds tp->ucopy.len = 0; 9241da177e4SLinus Torvalds tp->ucopy.memory = 0; 9251da177e4SLinus Torvalds skb_queue_head_init(&tp->ucopy.prequeue); 92697fc2f08SChris Leech #ifdef CONFIG_NET_DMA 92797fc2f08SChris Leech tp->ucopy.dma_chan = NULL; 92897fc2f08SChris Leech tp->ucopy.wakeup = 0; 92997fc2f08SChris Leech tp->ucopy.pinned_list = NULL; 93097fc2f08SChris Leech tp->ucopy.dma_cookie = 0; 93197fc2f08SChris Leech #endif 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* Packet is added to VJ-style prequeue for processing in process 9351da177e4SLinus Torvalds * context, if a reader task is waiting. Apparently, this exciting 9361da177e4SLinus Torvalds * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93) 9371da177e4SLinus Torvalds * failed somewhere. Latency? Burstiness? Well, at least now we will 9381da177e4SLinus Torvalds * see, why it failed. 8)8) --ANK 9391da177e4SLinus Torvalds * 9401da177e4SLinus Torvalds * NOTE: is this not too big to inline? 9411da177e4SLinus Torvalds */ 94240efc6faSStephen Hemminger static inline int tcp_prequeue(struct sock *sk, struct sk_buff *skb) 9431da177e4SLinus Torvalds { 9441da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 9451da177e4SLinus Torvalds 946f5f8d86bSEric Dumazet if (sysctl_tcp_low_latency || !tp->ucopy.task) 947f5f8d86bSEric Dumazet return 0; 948f5f8d86bSEric Dumazet 9491da177e4SLinus Torvalds __skb_queue_tail(&tp->ucopy.prequeue, skb); 9501da177e4SLinus Torvalds tp->ucopy.memory += skb->truesize; 9511da177e4SLinus Torvalds if (tp->ucopy.memory > sk->sk_rcvbuf) { 9521da177e4SLinus Torvalds struct sk_buff *skb1; 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds BUG_ON(sock_owned_by_user(sk)); 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) { 957c57943a1SPeter Zijlstra sk_backlog_rcv(sk, skb1); 958f5f8d86bSEric Dumazet NET_INC_STATS_BH(sock_net(sk), 959f5f8d86bSEric Dumazet LINUX_MIB_TCPPREQUEUEDROPPED); 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds tp->ucopy.memory = 0; 9631da177e4SLinus Torvalds } else if (skb_queue_len(&tp->ucopy.prequeue) == 1) { 964aa395145SEric Dumazet wake_up_interruptible_sync_poll(sk_sleep(sk), 9657aedec2aSEric Dumazet POLLIN | POLLRDNORM | POLLRDBAND); 966463c84b9SArnaldo Carvalho de Melo if (!inet_csk_ack_scheduled(sk)) 967463c84b9SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 9680c266898SSatoru SATOH (3 * tcp_rto_min(sk)) / 4, 9693f421baaSArnaldo Carvalho de Melo TCP_RTO_MAX); 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds return 1; 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds #undef STATE_TRACE 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds #ifdef STATE_TRACE 9781da177e4SLinus Torvalds static const char *statename[]={ 9791da177e4SLinus Torvalds "Unused","Established","Syn Sent","Syn Recv", 9801da177e4SLinus Torvalds "Fin Wait 1","Fin Wait 2","Time Wait", "Close", 9811da177e4SLinus Torvalds "Close Wait","Last ACK","Listen","Closing" 9821da177e4SLinus Torvalds }; 9831da177e4SLinus Torvalds #endif 984490d5046SIlpo Järvinen extern void tcp_set_state(struct sock *sk, int state); 9851da177e4SLinus Torvalds 9864ac02babSAndi Kleen extern void tcp_done(struct sock *sk); 9871da177e4SLinus Torvalds 98840efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt) 9891da177e4SLinus Torvalds { 9901da177e4SLinus Torvalds rx_opt->dsack = 0; 9911da177e4SLinus Torvalds rx_opt->num_sacks = 0; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */ 9951da177e4SLinus Torvalds extern void tcp_select_initial_window(int __space, __u32 mss, 9961da177e4SLinus Torvalds __u32 *rcv_wnd, __u32 *window_clamp, 99731d12926Slaurent chavey int wscale_ok, __u8 *rcv_wscale, 99831d12926Slaurent chavey __u32 init_rcv_wnd); 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds static inline int tcp_win_from_space(int space) 10011da177e4SLinus Torvalds { 10021da177e4SLinus Torvalds return sysctl_tcp_adv_win_scale<=0 ? 10031da177e4SLinus Torvalds (space>>(-sysctl_tcp_adv_win_scale)) : 10041da177e4SLinus Torvalds space - (space>>sysctl_tcp_adv_win_scale); 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */ 10081da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk) 10091da177e4SLinus Torvalds { 10101da177e4SLinus Torvalds return tcp_win_from_space(sk->sk_rcvbuf - 10111da177e4SLinus Torvalds atomic_read(&sk->sk_rmem_alloc)); 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk) 10151da177e4SLinus Torvalds { 10161da177e4SLinus Torvalds return tcp_win_from_space(sk->sk_rcvbuf); 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 101940efc6faSStephen Hemminger static inline void tcp_openreq_init(struct request_sock *req, 10201da177e4SLinus Torvalds struct tcp_options_received *rx_opt, 10211da177e4SLinus Torvalds struct sk_buff *skb) 10221da177e4SLinus Torvalds { 10232e6599cbSArnaldo Carvalho de Melo struct inet_request_sock *ireq = inet_rsk(req); 10242e6599cbSArnaldo Carvalho de Melo 10251da177e4SLinus Torvalds req->rcv_wnd = 0; /* So that tcp_send_synack() knows! */ 10264dfc2817SFlorian Westphal req->cookie_ts = 0; 10272e6599cbSArnaldo Carvalho de Melo tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq; 10281da177e4SLinus Torvalds req->mss = rx_opt->mss_clamp; 10291da177e4SLinus Torvalds req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0; 10302e6599cbSArnaldo Carvalho de Melo ireq->tstamp_ok = rx_opt->tstamp_ok; 10312e6599cbSArnaldo Carvalho de Melo ireq->sack_ok = rx_opt->sack_ok; 10322e6599cbSArnaldo Carvalho de Melo ireq->snd_wscale = rx_opt->snd_wscale; 10332e6599cbSArnaldo Carvalho de Melo ireq->wscale_ok = rx_opt->wscale_ok; 10342e6599cbSArnaldo Carvalho de Melo ireq->acked = 0; 10352e6599cbSArnaldo Carvalho de Melo ireq->ecn_ok = 0; 1036aa8223c7SArnaldo Carvalho de Melo ireq->rmt_port = tcp_hdr(skb)->source; 1037a3116ac5SKOVACS Krisztian ireq->loc_port = tcp_hdr(skb)->dest; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10405c52ba17SPavel Emelyanov extern void tcp_enter_memory_pressure(struct sock *sk); 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp) 10431da177e4SLinus Torvalds { 10441da177e4SLinus Torvalds return tp->keepalive_intvl ? : sysctl_tcp_keepalive_intvl; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp) 10481da177e4SLinus Torvalds { 10491da177e4SLinus Torvalds return tp->keepalive_time ? : sysctl_tcp_keepalive_time; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 1052df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp) 1053df19a626SEric Dumazet { 1054df19a626SEric Dumazet return tp->keepalive_probes ? : sysctl_tcp_keepalive_probes; 1055df19a626SEric Dumazet } 1056df19a626SEric Dumazet 10576c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp) 10586c37e5deSFlavio Leitner { 10596c37e5deSFlavio Leitner const struct inet_connection_sock *icsk = &tp->inet_conn; 10606c37e5deSFlavio Leitner 10616c37e5deSFlavio Leitner return min_t(u32, tcp_time_stamp - icsk->icsk_ack.lrcvtime, 10626c37e5deSFlavio Leitner tcp_time_stamp - tp->rcv_tstamp); 10636c37e5deSFlavio Leitner } 10646c37e5deSFlavio Leitner 1065463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk) 10661da177e4SLinus Torvalds { 1067463c84b9SArnaldo Carvalho de Melo int fin_timeout = tcp_sk(sk)->linger2 ? : sysctl_tcp_fin_timeout; 1068463c84b9SArnaldo Carvalho de Melo const int rto = inet_csk(sk)->icsk_rto; 10691da177e4SLinus Torvalds 1070463c84b9SArnaldo Carvalho de Melo if (fin_timeout < (rto << 2) - (rto >> 1)) 1071463c84b9SArnaldo Carvalho de Melo fin_timeout = (rto << 2) - (rto >> 1); 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds return fin_timeout; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 1076c887e6d2SIlpo Järvinen static inline int tcp_paws_check(const struct tcp_options_received *rx_opt, 1077c887e6d2SIlpo Järvinen int paws_win) 10781da177e4SLinus Torvalds { 1079c887e6d2SIlpo Järvinen if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win) 1080c887e6d2SIlpo Järvinen return 1; 1081c887e6d2SIlpo Järvinen if (unlikely(get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_24DAYS)) 1082c887e6d2SIlpo Järvinen return 1; 1083bc2ce894SEric Dumazet /* 1084bc2ce894SEric Dumazet * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0, 1085bc2ce894SEric Dumazet * then following tcp messages have valid values. Ignore 0 value, 1086bc2ce894SEric Dumazet * or else 'negative' tsval might forbid us to accept their packets. 1087bc2ce894SEric Dumazet */ 1088bc2ce894SEric Dumazet if (!rx_opt->ts_recent) 1089bc2ce894SEric Dumazet return 1; 10901da177e4SLinus Torvalds return 0; 1091c887e6d2SIlpo Järvinen } 1092c887e6d2SIlpo Järvinen 1093c887e6d2SIlpo Järvinen static inline int tcp_paws_reject(const struct tcp_options_received *rx_opt, 1094c887e6d2SIlpo Järvinen int rst) 1095c887e6d2SIlpo Järvinen { 1096c887e6d2SIlpo Järvinen if (tcp_paws_check(rx_opt, 0)) 10971da177e4SLinus Torvalds return 0; 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds /* RST segments are not recommended to carry timestamp, 11001da177e4SLinus Torvalds and, if they do, it is recommended to ignore PAWS because 11011da177e4SLinus Torvalds "their cleanup function should take precedence over timestamps." 11021da177e4SLinus Torvalds Certainly, it is mistake. It is necessary to understand the reasons 11031da177e4SLinus Torvalds of this constraint to relax it: if peer reboots, clock may go 11041da177e4SLinus Torvalds out-of-sync and half-open connections will not be reset. 11051da177e4SLinus Torvalds Actually, the problem would be not existing if all 11061da177e4SLinus Torvalds the implementations followed draft about maintaining clock 11071da177e4SLinus Torvalds via reboots. Linux-2.2 DOES NOT! 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds However, we can relax time bounds for RST segments to MSL. 11101da177e4SLinus Torvalds */ 11119d729f72SJames Morris if (rst && get_seconds() >= rx_opt->ts_recent_stamp + TCP_PAWS_MSL) 11121da177e4SLinus Torvalds return 0; 11131da177e4SLinus Torvalds return 1; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 1116a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net) 11171da177e4SLinus Torvalds { 11181da177e4SLinus Torvalds /* See RFC 2012 */ 1119cf1100a7SPavel Emelyanov TCP_ADD_STATS_USER(net, TCP_MIB_RTOALGORITHM, 1); 1120cf1100a7SPavel Emelyanov TCP_ADD_STATS_USER(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ); 1121cf1100a7SPavel Emelyanov TCP_ADD_STATS_USER(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ); 1122cf1100a7SPavel Emelyanov TCP_ADD_STATS_USER(net, TCP_MIB_MAXCONN, -1); 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 11256a438bbeSStephen Hemminger /* from STCP */ 1126ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp) 11270800f170SDavid S. Miller { 11286a438bbeSStephen Hemminger tp->lost_skb_hint = NULL; 11296a438bbeSStephen Hemminger tp->scoreboard_skb_hint = NULL; 1130ef9da47cSIlpo Järvinen } 1131ef9da47cSIlpo Järvinen 1132ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp) 1133ef9da47cSIlpo Järvinen { 1134ef9da47cSIlpo Järvinen tcp_clear_retrans_hints_partial(tp); 11356a438bbeSStephen Hemminger tp->retransmit_skb_hint = NULL; 1136b7689205SIlpo Järvinen } 1137b7689205SIlpo Järvinen 1138cfb6eeb4SYOSHIFUJI Hideaki /* MD5 Signature */ 1139cfb6eeb4SYOSHIFUJI Hideaki struct crypto_hash; 1140cfb6eeb4SYOSHIFUJI Hideaki 1141cfb6eeb4SYOSHIFUJI Hideaki /* - key database */ 1142cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key { 1143cfb6eeb4SYOSHIFUJI Hideaki u8 *key; 1144cfb6eeb4SYOSHIFUJI Hideaki u8 keylen; 1145cfb6eeb4SYOSHIFUJI Hideaki }; 1146cfb6eeb4SYOSHIFUJI Hideaki 1147cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_md5sig_key { 1148f8ab18d2SDavid S. Miller struct tcp_md5sig_key base; 1149cfb6eeb4SYOSHIFUJI Hideaki __be32 addr; 1150cfb6eeb4SYOSHIFUJI Hideaki }; 1151cfb6eeb4SYOSHIFUJI Hideaki 1152cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_md5sig_key { 1153f8ab18d2SDavid S. Miller struct tcp_md5sig_key base; 1154cfb6eeb4SYOSHIFUJI Hideaki #if 0 1155cfb6eeb4SYOSHIFUJI Hideaki u32 scope_id; /* XXX */ 1156cfb6eeb4SYOSHIFUJI Hideaki #endif 1157cfb6eeb4SYOSHIFUJI Hideaki struct in6_addr addr; 1158cfb6eeb4SYOSHIFUJI Hideaki }; 1159cfb6eeb4SYOSHIFUJI Hideaki 1160cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */ 1161cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info { 1162cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_md5sig_key *keys4; 1163dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1164cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_md5sig_key *keys6; 1165cfb6eeb4SYOSHIFUJI Hideaki u32 entries6; 1166cfb6eeb4SYOSHIFUJI Hideaki u32 alloced6; 1167cfb6eeb4SYOSHIFUJI Hideaki #endif 1168cfb6eeb4SYOSHIFUJI Hideaki u32 entries4; 1169cfb6eeb4SYOSHIFUJI Hideaki u32 alloced4; 1170cfb6eeb4SYOSHIFUJI Hideaki }; 1171cfb6eeb4SYOSHIFUJI Hideaki 1172cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */ 1173cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr { 1174cfb6eeb4SYOSHIFUJI Hideaki __be32 saddr; 1175cfb6eeb4SYOSHIFUJI Hideaki __be32 daddr; 1176cfb6eeb4SYOSHIFUJI Hideaki __u8 pad; 1177cfb6eeb4SYOSHIFUJI Hideaki __u8 protocol; 1178cfb6eeb4SYOSHIFUJI Hideaki __be16 len; 1179cfb6eeb4SYOSHIFUJI Hideaki }; 1180cfb6eeb4SYOSHIFUJI Hideaki 1181cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr { 1182cfb6eeb4SYOSHIFUJI Hideaki struct in6_addr saddr; 1183cfb6eeb4SYOSHIFUJI Hideaki struct in6_addr daddr; 1184cfb6eeb4SYOSHIFUJI Hideaki __be32 len; 1185cfb6eeb4SYOSHIFUJI Hideaki __be32 protocol; /* including padding */ 1186cfb6eeb4SYOSHIFUJI Hideaki }; 1187cfb6eeb4SYOSHIFUJI Hideaki 1188cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block { 1189cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr ip4; 1190dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1191cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr ip6; 1192cfb6eeb4SYOSHIFUJI Hideaki #endif 1193cfb6eeb4SYOSHIFUJI Hideaki }; 1194cfb6eeb4SYOSHIFUJI Hideaki 1195cfb6eeb4SYOSHIFUJI Hideaki /* - pool: digest algorithm, hash description and scratch buffer */ 1196cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_pool { 1197cfb6eeb4SYOSHIFUJI Hideaki struct hash_desc md5_desc; 1198cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block md5_blk; 1199cfb6eeb4SYOSHIFUJI Hideaki }; 1200cfb6eeb4SYOSHIFUJI Hideaki 1201cfb6eeb4SYOSHIFUJI Hideaki /* - functions */ 120253d3176bSChangli Gao extern int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key, 1203318cf7aaSEric Dumazet const struct sock *sk, 1204318cf7aaSEric Dumazet const struct request_sock *req, 1205318cf7aaSEric Dumazet const struct sk_buff *skb); 1206cfb6eeb4SYOSHIFUJI Hideaki extern struct tcp_md5sig_key * tcp_v4_md5_lookup(struct sock *sk, 1207cfb6eeb4SYOSHIFUJI Hideaki struct sock *addr_sk); 120853d3176bSChangli Gao extern int tcp_v4_md5_do_add(struct sock *sk, __be32 addr, u8 *newkey, 1209cfb6eeb4SYOSHIFUJI Hideaki u8 newkeylen); 121053d3176bSChangli Gao extern int tcp_v4_md5_do_del(struct sock *sk, __be32 addr); 1211cfb6eeb4SYOSHIFUJI Hideaki 12129501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 12139501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk) ((twsk)->tw_md5_keylen ? \ 12149501f972SYOSHIFUJI Hideaki &(struct tcp_md5sig_key) { \ 12159501f972SYOSHIFUJI Hideaki .key = (twsk)->tw_md5_key, \ 12169501f972SYOSHIFUJI Hideaki .keylen = (twsk)->tw_md5_keylen, \ 12179501f972SYOSHIFUJI Hideaki } : NULL) 12189501f972SYOSHIFUJI Hideaki #else 12199501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk) NULL 12209501f972SYOSHIFUJI Hideaki #endif 12219501f972SYOSHIFUJI Hideaki 1222765cf997SEric Dumazet extern struct tcp_md5sig_pool __percpu *tcp_alloc_md5sig_pool(struct sock *); 1223cfb6eeb4SYOSHIFUJI Hideaki extern void tcp_free_md5sig_pool(void); 1224cfb6eeb4SYOSHIFUJI Hideaki 122535790c04SEric Dumazet extern struct tcp_md5sig_pool *tcp_get_md5sig_pool(void); 122635790c04SEric Dumazet extern void tcp_put_md5sig_pool(void); 122735790c04SEric Dumazet 1228ca35a0efSEric Dumazet extern int tcp_md5_hash_header(struct tcp_md5sig_pool *, const struct tcphdr *); 1229cf533ea5SEric Dumazet extern int tcp_md5_hash_skb_data(struct tcp_md5sig_pool *, const struct sk_buff *, 123049a72dfbSAdam Langley unsigned header_len); 123149a72dfbSAdam Langley extern int tcp_md5_hash_key(struct tcp_md5sig_pool *hp, 1232cf533ea5SEric Dumazet const struct tcp_md5sig_key *key); 1233cfb6eeb4SYOSHIFUJI Hideaki 1234fe067e8aSDavid S. Miller /* write queue abstraction */ 1235fe067e8aSDavid S. Miller static inline void tcp_write_queue_purge(struct sock *sk) 1236fe067e8aSDavid S. Miller { 1237fe067e8aSDavid S. Miller struct sk_buff *skb; 1238fe067e8aSDavid S. Miller 1239fe067e8aSDavid S. Miller while ((skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) 12403ab224beSHideo Aoki sk_wmem_free_skb(sk, skb); 12413ab224beSHideo Aoki sk_mem_reclaim(sk); 12428818a9d8SIlpo Järvinen tcp_clear_all_retrans_hints(tcp_sk(sk)); 1243fe067e8aSDavid S. Miller } 1244fe067e8aSDavid S. Miller 1245cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_head(const struct sock *sk) 1246fe067e8aSDavid S. Miller { 1247cd07a8eaSDavid S. Miller return skb_peek(&sk->sk_write_queue); 1248fe067e8aSDavid S. Miller } 1249fe067e8aSDavid S. Miller 1250cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk) 1251fe067e8aSDavid S. Miller { 1252cd07a8eaSDavid S. Miller return skb_peek_tail(&sk->sk_write_queue); 1253fe067e8aSDavid S. Miller } 1254fe067e8aSDavid S. Miller 1255cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_next(const struct sock *sk, 1256cf533ea5SEric Dumazet const struct sk_buff *skb) 1257fe067e8aSDavid S. Miller { 1258cd07a8eaSDavid S. Miller return skb_queue_next(&sk->sk_write_queue, skb); 1259fe067e8aSDavid S. Miller } 1260fe067e8aSDavid S. Miller 1261cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_prev(const struct sock *sk, 1262cf533ea5SEric Dumazet const struct sk_buff *skb) 1263832d11c5SIlpo Järvinen { 1264832d11c5SIlpo Järvinen return skb_queue_prev(&sk->sk_write_queue, skb); 1265832d11c5SIlpo Järvinen } 1266832d11c5SIlpo Järvinen 1267fe067e8aSDavid S. Miller #define tcp_for_write_queue(skb, sk) \ 1268cd07a8eaSDavid S. Miller skb_queue_walk(&(sk)->sk_write_queue, skb) 1269fe067e8aSDavid S. Miller 1270fe067e8aSDavid S. Miller #define tcp_for_write_queue_from(skb, sk) \ 1271cd07a8eaSDavid S. Miller skb_queue_walk_from(&(sk)->sk_write_queue, skb) 1272fe067e8aSDavid S. Miller 1273234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk) \ 1274cd07a8eaSDavid S. Miller skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp) 1275234b6860SIlpo Järvinen 1276cf533ea5SEric Dumazet static inline struct sk_buff *tcp_send_head(const struct sock *sk) 1277fe067e8aSDavid S. Miller { 1278fe067e8aSDavid S. Miller return sk->sk_send_head; 1279fe067e8aSDavid S. Miller } 1280fe067e8aSDavid S. Miller 1281cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk, 1282cd07a8eaSDavid S. Miller const struct sk_buff *skb) 1283cd07a8eaSDavid S. Miller { 1284cd07a8eaSDavid S. Miller return skb_queue_is_last(&sk->sk_write_queue, skb); 1285cd07a8eaSDavid S. Miller } 1286cd07a8eaSDavid S. Miller 1287cf533ea5SEric Dumazet static inline void tcp_advance_send_head(struct sock *sk, const struct sk_buff *skb) 1288fe067e8aSDavid S. Miller { 1289cd07a8eaSDavid S. Miller if (tcp_skb_is_last(sk, skb)) 1290fe067e8aSDavid S. Miller sk->sk_send_head = NULL; 1291cd07a8eaSDavid S. Miller else 1292cd07a8eaSDavid S. Miller sk->sk_send_head = tcp_write_queue_next(sk, skb); 1293fe067e8aSDavid S. Miller } 1294fe067e8aSDavid S. Miller 1295fe067e8aSDavid S. Miller static inline void tcp_check_send_head(struct sock *sk, struct sk_buff *skb_unlinked) 1296fe067e8aSDavid S. Miller { 1297fe067e8aSDavid S. Miller if (sk->sk_send_head == skb_unlinked) 1298fe067e8aSDavid S. Miller sk->sk_send_head = NULL; 1299fe067e8aSDavid S. Miller } 1300fe067e8aSDavid S. Miller 1301fe067e8aSDavid S. Miller static inline void tcp_init_send_head(struct sock *sk) 1302fe067e8aSDavid S. Miller { 1303fe067e8aSDavid S. Miller sk->sk_send_head = NULL; 1304fe067e8aSDavid S. Miller } 1305fe067e8aSDavid S. Miller 1306fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb) 1307fe067e8aSDavid S. Miller { 1308fe067e8aSDavid S. Miller __skb_queue_tail(&sk->sk_write_queue, skb); 1309fe067e8aSDavid S. Miller } 1310fe067e8aSDavid S. Miller 1311fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb) 1312fe067e8aSDavid S. Miller { 1313fe067e8aSDavid S. Miller __tcp_add_write_queue_tail(sk, skb); 1314fe067e8aSDavid S. Miller 1315fe067e8aSDavid S. Miller /* Queue it, remembering where we must start sending. */ 13166859d494SIlpo Järvinen if (sk->sk_send_head == NULL) { 1317fe067e8aSDavid S. Miller sk->sk_send_head = skb; 13186859d494SIlpo Järvinen 13196859d494SIlpo Järvinen if (tcp_sk(sk)->highest_sack == NULL) 13206859d494SIlpo Järvinen tcp_sk(sk)->highest_sack = skb; 13216859d494SIlpo Järvinen } 1322fe067e8aSDavid S. Miller } 1323fe067e8aSDavid S. Miller 1324fe067e8aSDavid S. Miller static inline void __tcp_add_write_queue_head(struct sock *sk, struct sk_buff *skb) 1325fe067e8aSDavid S. Miller { 1326fe067e8aSDavid S. Miller __skb_queue_head(&sk->sk_write_queue, skb); 1327fe067e8aSDavid S. Miller } 1328fe067e8aSDavid S. Miller 1329fe067e8aSDavid S. Miller /* Insert buff after skb on the write queue of sk. */ 1330fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_after(struct sk_buff *skb, 1331fe067e8aSDavid S. Miller struct sk_buff *buff, 1332fe067e8aSDavid S. Miller struct sock *sk) 1333fe067e8aSDavid S. Miller { 13347de6c033SGerrit Renker __skb_queue_after(&sk->sk_write_queue, skb, buff); 1335fe067e8aSDavid S. Miller } 1336fe067e8aSDavid S. Miller 133743f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk. */ 1338fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new, 1339fe067e8aSDavid S. Miller struct sk_buff *skb, 1340fe067e8aSDavid S. Miller struct sock *sk) 1341fe067e8aSDavid S. Miller { 134243f59c89SDavid S. Miller __skb_queue_before(&sk->sk_write_queue, skb, new); 13436e421410SIlpo Järvinen 13446e421410SIlpo Järvinen if (sk->sk_send_head == skb) 13456e421410SIlpo Järvinen sk->sk_send_head = new; 1346fe067e8aSDavid S. Miller } 1347fe067e8aSDavid S. Miller 1348fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk) 1349fe067e8aSDavid S. Miller { 1350fe067e8aSDavid S. Miller __skb_unlink(skb, &sk->sk_write_queue); 1351fe067e8aSDavid S. Miller } 1352fe067e8aSDavid S. Miller 1353fe067e8aSDavid S. Miller static inline int tcp_write_queue_empty(struct sock *sk) 1354fe067e8aSDavid S. Miller { 1355fe067e8aSDavid S. Miller return skb_queue_empty(&sk->sk_write_queue); 1356fe067e8aSDavid S. Miller } 1357fe067e8aSDavid S. Miller 135812d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk) 135912d50c46SKrishna Kumar { 136012d50c46SKrishna Kumar if (tcp_send_head(sk)) { 136112d50c46SKrishna Kumar struct tcp_sock *tp = tcp_sk(sk); 136212d50c46SKrishna Kumar 136312d50c46SKrishna Kumar __tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle); 136412d50c46SKrishna Kumar } 136512d50c46SKrishna Kumar } 136612d50c46SKrishna Kumar 1367*ecb97192SNeal Cardwell /* Start sequence of the skb just after the highest skb with SACKed 1368*ecb97192SNeal Cardwell * bit, valid only if sacked_out > 0 or when the caller has ensured 1369*ecb97192SNeal Cardwell * validity by itself. 1370a47e5a98SIlpo Järvinen */ 1371a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp) 1372a47e5a98SIlpo Järvinen { 1373a47e5a98SIlpo Järvinen if (!tp->sacked_out) 1374a47e5a98SIlpo Järvinen return tp->snd_una; 13756859d494SIlpo Järvinen 13766859d494SIlpo Järvinen if (tp->highest_sack == NULL) 13776859d494SIlpo Järvinen return tp->snd_nxt; 13786859d494SIlpo Järvinen 1379a47e5a98SIlpo Järvinen return TCP_SKB_CB(tp->highest_sack)->seq; 1380a47e5a98SIlpo Järvinen } 1381a47e5a98SIlpo Järvinen 13826859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb) 13836859d494SIlpo Järvinen { 13846859d494SIlpo Järvinen tcp_sk(sk)->highest_sack = tcp_skb_is_last(sk, skb) ? NULL : 13856859d494SIlpo Järvinen tcp_write_queue_next(sk, skb); 13866859d494SIlpo Järvinen } 13876859d494SIlpo Järvinen 13886859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk) 13896859d494SIlpo Järvinen { 13906859d494SIlpo Järvinen return tcp_sk(sk)->highest_sack; 13916859d494SIlpo Järvinen } 13926859d494SIlpo Järvinen 13936859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk) 13946859d494SIlpo Järvinen { 13956859d494SIlpo Järvinen tcp_sk(sk)->highest_sack = tcp_write_queue_head(sk); 13966859d494SIlpo Järvinen } 13976859d494SIlpo Järvinen 13986859d494SIlpo Järvinen /* Called when old skb is about to be deleted (to be combined with new skb) */ 13996859d494SIlpo Järvinen static inline void tcp_highest_sack_combine(struct sock *sk, 14006859d494SIlpo Järvinen struct sk_buff *old, 14016859d494SIlpo Järvinen struct sk_buff *new) 14026859d494SIlpo Järvinen { 14036859d494SIlpo Järvinen if (tcp_sk(sk)->sacked_out && (old == tcp_sk(sk)->highest_sack)) 14046859d494SIlpo Järvinen tcp_sk(sk)->highest_sack = new; 14056859d494SIlpo Järvinen } 14066859d494SIlpo Järvinen 14075aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from 14085aa4b32fSAndreas Petlund * increased latency). Used to trigger latency-reducing mechanisms. 14095aa4b32fSAndreas Petlund */ 14105aa4b32fSAndreas Petlund static inline unsigned int tcp_stream_is_thin(struct tcp_sock *tp) 14115aa4b32fSAndreas Petlund { 14125aa4b32fSAndreas Petlund return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp); 14135aa4b32fSAndreas Petlund } 14145aa4b32fSAndreas Petlund 14151da177e4SLinus Torvalds /* /proc */ 14161da177e4SLinus Torvalds enum tcp_seq_states { 14171da177e4SLinus Torvalds TCP_SEQ_STATE_LISTENING, 14181da177e4SLinus Torvalds TCP_SEQ_STATE_OPENREQ, 14191da177e4SLinus Torvalds TCP_SEQ_STATE_ESTABLISHED, 14201da177e4SLinus Torvalds TCP_SEQ_STATE_TIME_WAIT, 14211da177e4SLinus Torvalds }; 14221da177e4SLinus Torvalds 142373cb88ecSArjan van de Ven int tcp_seq_open(struct inode *inode, struct file *file); 142473cb88ecSArjan van de Ven 14251da177e4SLinus Torvalds struct tcp_seq_afinfo { 14261da177e4SLinus Torvalds char *name; 14271da177e4SLinus Torvalds sa_family_t family; 142873cb88ecSArjan van de Ven const struct file_operations *seq_fops; 14299427c4b3SDenis V. Lunev struct seq_operations seq_ops; 14301da177e4SLinus Torvalds }; 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds struct tcp_iter_state { 1433a4146b1bSDenis V. Lunev struct seq_net_private p; 14341da177e4SLinus Torvalds sa_family_t family; 14351da177e4SLinus Torvalds enum tcp_seq_states state; 14361da177e4SLinus Torvalds struct sock *syn_wait_sk; 1437a8b690f9STom Herbert int bucket, offset, sbucket, num, uid; 1438a8b690f9STom Herbert loff_t last_pos; 14391da177e4SLinus Torvalds }; 14401da177e4SLinus Torvalds 14416f8b13bcSDaniel Lezcano extern int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo); 14426f8b13bcSDaniel Lezcano extern void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo); 14431da177e4SLinus Torvalds 144420380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops; 1445c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops; 144620380731SArnaldo Carvalho de Melo 14477d06b2e0SBrian Haley extern void tcp_v4_destroy_sock(struct sock *sk); 144820380731SArnaldo Carvalho de Melo 1449a430a43dSHerbert Xu extern int tcp_v4_gso_send_check(struct sk_buff *skb); 1450c8f44affSMichał Mirosław extern struct sk_buff *tcp_tso_segment(struct sk_buff *skb, 1451c8f44affSMichał Mirosław netdev_features_t features); 1452bf296b12SHerbert Xu extern struct sk_buff **tcp_gro_receive(struct sk_buff **head, 1453bf296b12SHerbert Xu struct sk_buff *skb); 1454bf296b12SHerbert Xu extern struct sk_buff **tcp4_gro_receive(struct sk_buff **head, 1455bf296b12SHerbert Xu struct sk_buff *skb); 1456bf296b12SHerbert Xu extern int tcp_gro_complete(struct sk_buff *skb); 1457bf296b12SHerbert Xu extern int tcp4_gro_complete(struct sk_buff *skb); 1458f4c50d99SHerbert Xu 145920380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS 146020380731SArnaldo Carvalho de Melo extern int tcp4_proc_init(void); 146120380731SArnaldo Carvalho de Melo extern void tcp4_proc_exit(void); 146220380731SArnaldo Carvalho de Melo #endif 146320380731SArnaldo Carvalho de Melo 1464cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */ 1465cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops { 1466cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 1467cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key *(*md5_lookup) (struct sock *sk, 1468cfb6eeb4SYOSHIFUJI Hideaki struct sock *addr_sk); 1469cfb6eeb4SYOSHIFUJI Hideaki int (*calc_md5_hash) (char *location, 1470cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key *md5, 1471318cf7aaSEric Dumazet const struct sock *sk, 1472318cf7aaSEric Dumazet const struct request_sock *req, 1473318cf7aaSEric Dumazet const struct sk_buff *skb); 1474cfb6eeb4SYOSHIFUJI Hideaki int (*md5_add) (struct sock *sk, 1475cfb6eeb4SYOSHIFUJI Hideaki struct sock *addr_sk, 1476cfb6eeb4SYOSHIFUJI Hideaki u8 *newkey, 1477cfb6eeb4SYOSHIFUJI Hideaki u8 len); 1478cfb6eeb4SYOSHIFUJI Hideaki int (*md5_parse) (struct sock *sk, 1479cfb6eeb4SYOSHIFUJI Hideaki char __user *optval, 1480cfb6eeb4SYOSHIFUJI Hideaki int optlen); 1481cfb6eeb4SYOSHIFUJI Hideaki #endif 1482cfb6eeb4SYOSHIFUJI Hideaki }; 1483cfb6eeb4SYOSHIFUJI Hideaki 1484cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops { 1485cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 1486cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key *(*md5_lookup) (struct sock *sk, 1487cfb6eeb4SYOSHIFUJI Hideaki struct request_sock *req); 1488e3afe7b7SJohn Dykstra int (*calc_md5_hash) (char *location, 1489e3afe7b7SJohn Dykstra struct tcp_md5sig_key *md5, 1490318cf7aaSEric Dumazet const struct sock *sk, 1491318cf7aaSEric Dumazet const struct request_sock *req, 1492318cf7aaSEric Dumazet const struct sk_buff *skb); 1493cfb6eeb4SYOSHIFUJI Hideaki #endif 1494cfb6eeb4SYOSHIFUJI Hideaki }; 1495cfb6eeb4SYOSHIFUJI Hideaki 1496da5c78c8SWilliam Allen Simpson /* Using SHA1 for now, define some constants. 1497da5c78c8SWilliam Allen Simpson */ 1498da5c78c8SWilliam Allen Simpson #define COOKIE_DIGEST_WORDS (SHA_DIGEST_WORDS) 1499da5c78c8SWilliam Allen Simpson #define COOKIE_MESSAGE_WORDS (SHA_MESSAGE_BYTES / 4) 1500da5c78c8SWilliam Allen Simpson #define COOKIE_WORKSPACE_WORDS (COOKIE_DIGEST_WORDS + COOKIE_MESSAGE_WORDS) 1501da5c78c8SWilliam Allen Simpson 1502da5c78c8SWilliam Allen Simpson extern int tcp_cookie_generator(u32 *bakery); 1503da5c78c8SWilliam Allen Simpson 1504435cf559SWilliam Allen Simpson /** 1505435cf559SWilliam Allen Simpson * struct tcp_cookie_values - each socket needs extra space for the 1506435cf559SWilliam Allen Simpson * cookies, together with (optional) space for any SYN data. 1507435cf559SWilliam Allen Simpson * 1508435cf559SWilliam Allen Simpson * A tcp_sock contains a pointer to the current value, and this is 1509435cf559SWilliam Allen Simpson * cloned to the tcp_timewait_sock. 1510435cf559SWilliam Allen Simpson * 1511435cf559SWilliam Allen Simpson * @cookie_pair: variable data from the option exchange. 1512435cf559SWilliam Allen Simpson * 1513435cf559SWilliam Allen Simpson * @cookie_desired: user specified tcpct_cookie_desired. Zero 1514435cf559SWilliam Allen Simpson * indicates default (sysctl_tcp_cookie_size). 1515435cf559SWilliam Allen Simpson * After cookie sent, remembers size of cookie. 1516435cf559SWilliam Allen Simpson * Range 0, TCP_COOKIE_MIN to TCP_COOKIE_MAX. 1517435cf559SWilliam Allen Simpson * 1518435cf559SWilliam Allen Simpson * @s_data_desired: user specified tcpct_s_data_desired. When the 1519435cf559SWilliam Allen Simpson * constant payload is specified (@s_data_constant), 1520435cf559SWilliam Allen Simpson * holds its length instead. 1521435cf559SWilliam Allen Simpson * Range 0 to TCP_MSS_DESIRED. 1522435cf559SWilliam Allen Simpson * 1523435cf559SWilliam Allen Simpson * @s_data_payload: constant data that is to be included in the 1524435cf559SWilliam Allen Simpson * payload of SYN or SYNACK segments when the 1525435cf559SWilliam Allen Simpson * cookie option is present. 1526435cf559SWilliam Allen Simpson */ 1527435cf559SWilliam Allen Simpson struct tcp_cookie_values { 1528435cf559SWilliam Allen Simpson struct kref kref; 1529435cf559SWilliam Allen Simpson u8 cookie_pair[TCP_COOKIE_PAIR_SIZE]; 1530435cf559SWilliam Allen Simpson u8 cookie_pair_size; 1531435cf559SWilliam Allen Simpson u8 cookie_desired; 1532435cf559SWilliam Allen Simpson u16 s_data_desired:11, 1533435cf559SWilliam Allen Simpson s_data_constant:1, 1534435cf559SWilliam Allen Simpson s_data_in:1, 1535435cf559SWilliam Allen Simpson s_data_out:1, 1536435cf559SWilliam Allen Simpson s_data_unused:2; 1537435cf559SWilliam Allen Simpson u8 s_data_payload[0]; 1538435cf559SWilliam Allen Simpson }; 1539435cf559SWilliam Allen Simpson 1540435cf559SWilliam Allen Simpson static inline void tcp_cookie_values_release(struct kref *kref) 1541435cf559SWilliam Allen Simpson { 1542435cf559SWilliam Allen Simpson kfree(container_of(kref, struct tcp_cookie_values, kref)); 1543435cf559SWilliam Allen Simpson } 1544435cf559SWilliam Allen Simpson 1545435cf559SWilliam Allen Simpson /* The length of constant payload data. Note that s_data_desired is 1546435cf559SWilliam Allen Simpson * overloaded, depending on s_data_constant: either the length of constant 1547435cf559SWilliam Allen Simpson * data (returned here) or the limit on variable data. 1548435cf559SWilliam Allen Simpson */ 1549435cf559SWilliam Allen Simpson static inline int tcp_s_data_size(const struct tcp_sock *tp) 1550435cf559SWilliam Allen Simpson { 1551435cf559SWilliam Allen Simpson return (tp->cookie_values != NULL && tp->cookie_values->s_data_constant) 1552435cf559SWilliam Allen Simpson ? tp->cookie_values->s_data_desired 1553435cf559SWilliam Allen Simpson : 0; 1554435cf559SWilliam Allen Simpson } 1555435cf559SWilliam Allen Simpson 1556435cf559SWilliam Allen Simpson /** 1557435cf559SWilliam Allen Simpson * struct tcp_extend_values - tcp_ipv?.c to tcp_output.c workspace. 1558435cf559SWilliam Allen Simpson * 1559435cf559SWilliam Allen Simpson * As tcp_request_sock has already been extended in other places, the 1560435cf559SWilliam Allen Simpson * only remaining method is to pass stack values along as function 1561435cf559SWilliam Allen Simpson * parameters. These parameters are not needed after sending SYNACK. 1562435cf559SWilliam Allen Simpson * 1563435cf559SWilliam Allen Simpson * @cookie_bakery: cryptographic secret and message workspace. 1564435cf559SWilliam Allen Simpson * 1565435cf559SWilliam Allen Simpson * @cookie_plus: bytes in authenticator/cookie option, copied from 1566435cf559SWilliam Allen Simpson * struct tcp_options_received (above). 1567435cf559SWilliam Allen Simpson */ 1568435cf559SWilliam Allen Simpson struct tcp_extend_values { 1569435cf559SWilliam Allen Simpson struct request_values rv; 1570435cf559SWilliam Allen Simpson u32 cookie_bakery[COOKIE_WORKSPACE_WORDS]; 1571435cf559SWilliam Allen Simpson u8 cookie_plus:6, 1572435cf559SWilliam Allen Simpson cookie_out_never:1, 1573435cf559SWilliam Allen Simpson cookie_in_always:1; 1574435cf559SWilliam Allen Simpson }; 1575435cf559SWilliam Allen Simpson 1576435cf559SWilliam Allen Simpson static inline struct tcp_extend_values *tcp_xv(struct request_values *rvp) 1577435cf559SWilliam Allen Simpson { 1578435cf559SWilliam Allen Simpson return (struct tcp_extend_values *)rvp; 1579435cf559SWilliam Allen Simpson } 1580435cf559SWilliam Allen Simpson 15819b0f976fSDenis V. Lunev extern void tcp_v4_init(void); 158220380731SArnaldo Carvalho de Melo extern void tcp_init(void); 158320380731SArnaldo Carvalho de Melo 15841da177e4SLinus Torvalds #endif /* _TCP_H */ 1585