12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */ 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 51da177e4SLinus Torvalds * interface as the means of communication with the user level. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Definitions for the TCP module. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Version: @(#)tcp.h 1.0.5 05/23/93 101da177e4SLinus Torvalds * 1102c30a84SJesper Juhl * Authors: Ross Biro 121da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds #ifndef _TCP_H 151da177e4SLinus Torvalds #define _TCP_H 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #define FASTRETRANS_DEBUG 1 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <linux/list.h> 201da177e4SLinus Torvalds #include <linux/tcp.h> 21187f1882SPaul Gortmaker #include <linux/bug.h> 221da177e4SLinus Torvalds #include <linux/slab.h> 231da177e4SLinus Torvalds #include <linux/cache.h> 241da177e4SLinus Torvalds #include <linux/percpu.h> 25fb286bb2SHerbert Xu #include <linux/skbuff.h> 26435cf559SWilliam Allen Simpson #include <linux/kref.h> 27740b0f18SEric Dumazet #include <linux/ktime.h> 2805e22e83SEric Dumazet #include <linux/indirect_call_wrapper.h> 293f421baaSArnaldo Carvalho de Melo 303f421baaSArnaldo Carvalho de Melo #include <net/inet_connection_sock.h> 31295ff7edSArnaldo Carvalho de Melo #include <net/inet_timewait_sock.h> 3277d8bf9cSArnaldo Carvalho de Melo #include <net/inet_hashtables.h> 331da177e4SLinus Torvalds #include <net/checksum.h> 342e6599cbSArnaldo Carvalho de Melo #include <net/request_sock.h> 3540a1227eSMartin KaFai Lau #include <net/sock_reuseport.h> 361da177e4SLinus Torvalds #include <net/sock.h> 371da177e4SLinus Torvalds #include <net/snmp.h> 381da177e4SLinus Torvalds #include <net/ip.h> 39c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 40c845f5f3SDmitry Safonov #include <net/tcp_ao.h> 41bdf1ee5dSIlpo Järvinen #include <net/inet_ecn.h> 420c266898SSatoru SATOH #include <net/dst.h> 4385712484SMat Martineau #include <net/mptcp.h> 44c752f073SArnaldo Carvalho de Melo 451da177e4SLinus Torvalds #include <linux/seq_file.h> 46180d8cd9SGlauber Costa #include <linux/memcontrol.h> 4740304b2aSLawrence Brakmo #include <linux/bpf-cgroup.h> 48438ac880SArd Biesheuvel #include <linux/siphash.h> 4940304b2aSLawrence Brakmo 500f7ff927SArnaldo Carvalho de Melo extern struct inet_hashinfo tcp_hashinfo; 511da177e4SLinus Torvalds 5219757cebSEric Dumazet DECLARE_PER_CPU(unsigned int, tcp_orphan_count); 5319757cebSEric Dumazet int tcp_orphan_count_sum(void); 5419757cebSEric Dumazet 5541eecbd7SEric Dumazet DECLARE_PER_CPU(u32, tcp_tw_isn); 5641eecbd7SEric Dumazet 575c9f3023SJoe Perches void tcp_time_wait(struct sock *sk, int state, int timeo); 581da177e4SLinus Torvalds 599bacd256SEric Dumazet #define MAX_TCP_HEADER L1_CACHE_ALIGN(128 + MAX_HEADER) 6033ad798cSAdam Langley #define MAX_TCP_OPTION_SPACE 40 613b4929f6SEric Dumazet #define TCP_MIN_SND_MSS 48 623b4929f6SEric Dumazet #define TCP_MIN_GSO_SIZE (TCP_MIN_SND_MSS - MAX_TCP_OPTION_SPACE) 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* 651da177e4SLinus Torvalds * Never offer a window over 32767 without using window scaling. Some 661da177e4SLinus Torvalds * poor stacks do signed 16bit maths! 671da177e4SLinus Torvalds */ 681da177e4SLinus Torvalds #define MAX_TCP_WINDOW 32767U 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* Minimal accepted MSS. It is (60+60+8) - (20+20). */ 711da177e4SLinus Torvalds #define TCP_MIN_MSS 88U 721da177e4SLinus Torvalds 731555e6fdSJosh Hunt /* The initial MTU to use for probing */ 74dcd8fb85SFan Du #define TCP_BASE_MSS 1024 755d424d5aSJohn Heffner 7605cbc0dbSFan Du /* probing interval, default to 10 minutes as per RFC4821 */ 7705cbc0dbSFan Du #define TCP_PROBE_INTERVAL 600 7805cbc0dbSFan Du 796b58e0a5SFan Du /* Specify interval when tcp mtu probing will stop */ 806b58e0a5SFan Du #define TCP_PROBE_THRESHOLD 8 816b58e0a5SFan Du 821da177e4SLinus Torvalds /* After receiving this amount of duplicate ACKs fast retransmit starts. */ 831da177e4SLinus Torvalds #define TCP_FASTRETRANS_THRESH 3 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* Maximal number of ACKs sent quickly to accelerate slow-start. */ 861da177e4SLinus Torvalds #define TCP_MAX_QUICKACKS 16U 871da177e4SLinus Torvalds 88589c49cbSGao Feng /* Maximal number of window scale according to RFC1323 */ 89589c49cbSGao Feng #define TCP_MAX_WSCALE 14U 90589c49cbSGao Feng 911da177e4SLinus Torvalds /* urg_data states */ 921da177e4SLinus Torvalds #define TCP_URG_VALID 0x0100 931da177e4SLinus Torvalds #define TCP_URG_NOTYET 0x0200 941da177e4SLinus Torvalds #define TCP_URG_READ 0x0400 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds #define TCP_RETR1 3 /* 971da177e4SLinus Torvalds * This is how many retries it does before it 981da177e4SLinus Torvalds * tries to figure out if the gateway is 991da177e4SLinus Torvalds * down. Minimal RFC value is 3; it corresponds 1001da177e4SLinus Torvalds * to ~3sec-8min depending on RTO. 1011da177e4SLinus Torvalds */ 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds #define TCP_RETR2 15 /* 1041da177e4SLinus Torvalds * This should take at least 1051da177e4SLinus Torvalds * 90 minutes to time out. 1061da177e4SLinus Torvalds * RFC1122 says that the limit is 100 sec. 1071da177e4SLinus Torvalds * 15 is ~13-30min depending on RTO. 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds 1106c9ff979SAlex Bergmann #define TCP_SYN_RETRIES 6 /* This is how many retries are done 1116c9ff979SAlex Bergmann * when active opening a connection. 1126c9ff979SAlex Bergmann * RFC1122 says the minimum retry MUST 1136c9ff979SAlex Bergmann * be at least 180secs. Nevertheless 1146c9ff979SAlex Bergmann * this value is corresponding to 1156c9ff979SAlex Bergmann * 63secs of retransmission with the 1166c9ff979SAlex Bergmann * current initial RTO. 1176c9ff979SAlex Bergmann */ 1181da177e4SLinus Torvalds 1196c9ff979SAlex Bergmann #define TCP_SYNACK_RETRIES 5 /* This is how may retries are done 1206c9ff979SAlex Bergmann * when passive opening a connection. 1216c9ff979SAlex Bergmann * This is corresponding to 31secs of 1226c9ff979SAlex Bergmann * retransmission with the current 1236c9ff979SAlex Bergmann * initial RTO. 1246c9ff979SAlex Bergmann */ 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds #define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT 1271da177e4SLinus Torvalds * state, about 60 seconds */ 1281da177e4SLinus Torvalds #define TCP_FIN_TIMEOUT TCP_TIMEWAIT_LEN 1291da177e4SLinus Torvalds /* BSD style FIN_WAIT2 deadlock breaker. 1301da177e4SLinus Torvalds * It used to be 3min, new value is 60sec, 1311da177e4SLinus Torvalds * to combine FIN-WAIT-2 timeout with 1321da177e4SLinus Torvalds * TIME-WAIT timer. 1331da177e4SLinus Torvalds */ 134f0628c52SCambda Zhu #define TCP_FIN_TIMEOUT_MAX (120 * HZ) /* max TCP_LINGER2 value (two minutes) */ 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds #define TCP_DELACK_MAX ((unsigned)(HZ/5)) /* maximal time to delay before sending an ACK */ 13795b9a87cSDavid Morley static_assert((1 << ATO_BITS) > TCP_DELACK_MAX); 13895b9a87cSDavid Morley 1391da177e4SLinus Torvalds #if HZ >= 100 1401da177e4SLinus Torvalds #define TCP_DELACK_MIN ((unsigned)(HZ/25)) /* minimal time to delay before sending an ACK */ 1411da177e4SLinus Torvalds #define TCP_ATO_MIN ((unsigned)(HZ/25)) 1421da177e4SLinus Torvalds #else 1431da177e4SLinus Torvalds #define TCP_DELACK_MIN 4U 1441da177e4SLinus Torvalds #define TCP_ATO_MIN 4U 1451da177e4SLinus Torvalds #endif 1461da177e4SLinus Torvalds #define TCP_RTO_MAX ((unsigned)(120*HZ)) 1471da177e4SLinus Torvalds #define TCP_RTO_MIN ((unsigned)(HZ/5)) 148bb4d991aSYuchung Cheng #define TCP_TIMEOUT_MIN (2U) /* Min timeout for TCP timers in jiffies */ 1491c2709cfSNeal Cardwell 1501c2709cfSNeal Cardwell #define TCP_TIMEOUT_MIN_US (2*USEC_PER_MSEC) /* Min TCP timeout in microsecs */ 1511c2709cfSNeal Cardwell 152fd4f2ceaSEric Dumazet #define TCP_TIMEOUT_INIT ((unsigned)(1*HZ)) /* RFC6298 2.1 initial RTO value */ 1539ad7c049SJerry Chu #define TCP_TIMEOUT_FALLBACK ((unsigned)(3*HZ)) /* RFC 1122 initial RTO value, now 1549ad7c049SJerry Chu * used as a fallback RTO for the 1559ad7c049SJerry Chu * initial data transmission if no 1569ad7c049SJerry Chu * valid RTT sample has been acquired, 1579ad7c049SJerry Chu * most likely due to retrans in 3WHS. 1589ad7c049SJerry Chu */ 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds #define TCP_RESOURCE_PROBE_INTERVAL ((unsigned)(HZ/2U)) /* Maximal interval between probes 1611da177e4SLinus Torvalds * for local resources. 1621da177e4SLinus Torvalds */ 1631da177e4SLinus Torvalds #define TCP_KEEPALIVE_TIME (120*60*HZ) /* two hours */ 1641da177e4SLinus Torvalds #define TCP_KEEPALIVE_PROBES 9 /* Max of 9 keepalive probes */ 1651da177e4SLinus Torvalds #define TCP_KEEPALIVE_INTVL (75*HZ) 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds #define MAX_TCP_KEEPIDLE 32767 1681da177e4SLinus Torvalds #define MAX_TCP_KEEPINTVL 32767 1691da177e4SLinus Torvalds #define MAX_TCP_KEEPCNT 127 1701da177e4SLinus Torvalds #define MAX_TCP_SYNCNT 127 1711da177e4SLinus Torvalds 172af772144SEric Dumazet /* Ensure that TCP PAWS checks are relaxed after ~2147 seconds 173af772144SEric Dumazet * to avoid overflows. This assumes a clock smaller than 1 Mhz. 174af772144SEric Dumazet * Default clock is 1 Khz, tcp_usec_ts uses 1 Mhz. 175af772144SEric Dumazet */ 176af772144SEric Dumazet #define TCP_PAWS_WRAP (INT_MAX / USEC_PER_SEC) 177af772144SEric Dumazet 1781da177e4SLinus Torvalds #define TCP_PAWS_MSL 60 /* Per-host timestamps are invalidated 1791da177e4SLinus Torvalds * after this time. It should be equal 1801da177e4SLinus Torvalds * (or greater than) TCP_TIMEWAIT_LEN 1811da177e4SLinus Torvalds * to provide reliability equal to one 1821da177e4SLinus Torvalds * provided by timewait state. 1831da177e4SLinus Torvalds */ 1841da177e4SLinus Torvalds #define TCP_PAWS_WINDOW 1 /* Replay window for per-host 1851da177e4SLinus Torvalds * timestamps. It must be less than 1861da177e4SLinus Torvalds * minimal timewait lifetime. 1871da177e4SLinus Torvalds */ 1881da177e4SLinus Torvalds /* 1891da177e4SLinus Torvalds * TCP option 1901da177e4SLinus Torvalds */ 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds #define TCPOPT_NOP 1 /* Padding */ 1931da177e4SLinus Torvalds #define TCPOPT_EOL 0 /* End of options */ 1941da177e4SLinus Torvalds #define TCPOPT_MSS 2 /* Segment size negotiating */ 1951da177e4SLinus Torvalds #define TCPOPT_WINDOW 3 /* Window scaling */ 1961da177e4SLinus Torvalds #define TCPOPT_SACK_PERM 4 /* SACK Permitted */ 1971da177e4SLinus Torvalds #define TCPOPT_SACK 5 /* SACK Block */ 1981da177e4SLinus Torvalds #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */ 199cfb6eeb4SYOSHIFUJI Hideaki #define TCPOPT_MD5SIG 19 /* MD5 Signature (RFC2385) */ 2001e03d32bSDmitry Safonov #define TCPOPT_AO 29 /* Authentication Option (RFC5925) */ 201c74a39c8SMat Martineau #define TCPOPT_MPTCP 30 /* Multipath TCP (RFC6824) */ 2027f9b838bSDaniel Lee #define TCPOPT_FASTOPEN 34 /* Fast open (RFC7413) */ 2032100c8d2SYuchung Cheng #define TCPOPT_EXP 254 /* Experimental */ 2042100c8d2SYuchung Cheng /* Magic number to be after the option value for sharing TCP 2052100c8d2SYuchung Cheng * experimental options. See draft-ietf-tcpm-experimental-options-00.txt 2062100c8d2SYuchung Cheng */ 2072100c8d2SYuchung Cheng #define TCPOPT_FASTOPEN_MAGIC 0xF989 20860e2a778SUrsula Braun #define TCPOPT_SMC_MAGIC 0xE2D4C3D9 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds /* 2111da177e4SLinus Torvalds * TCP option lengths 2121da177e4SLinus Torvalds */ 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds #define TCPOLEN_MSS 4 2151da177e4SLinus Torvalds #define TCPOLEN_WINDOW 3 2161da177e4SLinus Torvalds #define TCPOLEN_SACK_PERM 2 2171da177e4SLinus Torvalds #define TCPOLEN_TIMESTAMP 10 218cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG 18 2197f9b838bSDaniel Lee #define TCPOLEN_FASTOPEN_BASE 2 2202100c8d2SYuchung Cheng #define TCPOLEN_EXP_FASTOPEN_BASE 4 22160e2a778SUrsula Braun #define TCPOLEN_EXP_SMC_BASE 6 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds /* But this is what stacks really send out. */ 2241da177e4SLinus Torvalds #define TCPOLEN_TSTAMP_ALIGNED 12 2251da177e4SLinus Torvalds #define TCPOLEN_WSCALE_ALIGNED 4 2261da177e4SLinus Torvalds #define TCPOLEN_SACKPERM_ALIGNED 4 2271da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE 2 2281da177e4SLinus Torvalds #define TCPOLEN_SACK_BASE_ALIGNED 4 2291da177e4SLinus Torvalds #define TCPOLEN_SACK_PERBLOCK 8 230cfb6eeb4SYOSHIFUJI Hideaki #define TCPOLEN_MD5SIG_ALIGNED 20 23133ad798cSAdam Langley #define TCPOLEN_MSS_ALIGNED 4 23260e2a778SUrsula Braun #define TCPOLEN_EXP_SMC_BASE_ALIGNED 8 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds /* Flags in tp->nonagle */ 2351da177e4SLinus Torvalds #define TCP_NAGLE_OFF 1 /* Nagle's algo is disabled */ 2361da177e4SLinus Torvalds #define TCP_NAGLE_CORK 2 /* Socket is corked */ 237caa20d9aSStephen Hemminger #define TCP_NAGLE_PUSH 4 /* Cork is overridden for already queued data */ 2381da177e4SLinus Torvalds 23936e31b0aSAndreas Petlund /* TCP thin-stream limits */ 24036e31b0aSAndreas Petlund #define TCP_THIN_LINEAR_RETRIES 6 /* After 6 linear retries, do exp. backoff */ 24136e31b0aSAndreas Petlund 24221603fc4SJörg Thalheim /* TCP initial congestion window as per rfc6928 */ 243442b9635SDavid S. Miller #define TCP_INIT_CWND 10 244442b9635SDavid S. Miller 245cf60af03SYuchung Cheng /* Bit Flags for sysctl_tcp_fastopen */ 246cf60af03SYuchung Cheng #define TFO_CLIENT_ENABLE 1 24710467163SJerry Chu #define TFO_SERVER_ENABLE 2 24867da22d2SYuchung Cheng #define TFO_CLIENT_NO_COOKIE 4 /* Data in SYN w/o cookie option */ 249cf60af03SYuchung Cheng 25010467163SJerry Chu /* Accept SYN data w/o any cookie option */ 25110467163SJerry Chu #define TFO_SERVER_COOKIE_NOT_REQD 0x200 25210467163SJerry Chu 25310467163SJerry Chu /* Force enable TFO on all listeners, i.e., not requiring the 254cebc5cbaSYuchung Cheng * TCP_FASTOPEN socket option. 25510467163SJerry Chu */ 25610467163SJerry Chu #define TFO_SERVER_WO_SOCKOPT1 0x400 25710467163SJerry Chu 258295ff7edSArnaldo Carvalho de Melo 2591da177e4SLinus Torvalds /* sysctl variables for tcp */ 2601da177e4SLinus Torvalds extern int sysctl_tcp_max_orphans; 261a4fe34bfSEric W. Biederman extern long sysctl_tcp_mem[3]; 262e20223f1SEric Dumazet 263a0370b3fSYuchung Cheng #define TCP_RACK_LOSS_DETECTION 0x1 /* Use RACK to detect losses */ 2641f255691SPriyaranjan Jha #define TCP_RACK_STATIC_REO_WND 0x2 /* Use static RACK reo wnd */ 26520b654dfSYuchung Cheng #define TCP_RACK_NO_DUPTHRESH 0x4 /* Do not use DUPACK threshold in RACK */ 266a0370b3fSYuchung Cheng 2678d987e5cSEric Dumazet extern atomic_long_t tcp_memory_allocated; 2680defbb0aSEric Dumazet DECLARE_PER_CPU(int, tcp_memory_per_cpu_fw_alloc); 2690defbb0aSEric Dumazet 2701748376bSEric Dumazet extern struct percpu_counter tcp_sockets_allocated; 27106044751SEric Dumazet extern unsigned long tcp_memory_pressure; 2721da177e4SLinus Torvalds 273b8da51ebSEric Dumazet /* optimized version of sk_under_memory_pressure() for TCP sockets */ 274b8da51ebSEric Dumazet static inline bool tcp_under_memory_pressure(const struct sock *sk) 275b8da51ebSEric Dumazet { 276baac50bbSJohannes Weiner if (mem_cgroup_sockets_enabled && sk->sk_memcg && 277baac50bbSJohannes Weiner mem_cgroup_under_socket_pressure(sk->sk_memcg)) 278e805605cSJohannes Weiner return true; 279b8da51ebSEric Dumazet 2801f142c17SEric Dumazet return READ_ONCE(tcp_memory_pressure); 281b8da51ebSEric Dumazet } 2821da177e4SLinus Torvalds /* 2831da177e4SLinus Torvalds * The next routines deal with comparing 32 bit unsigned ints 2841da177e4SLinus Torvalds * and worry about wraparound (automatic with unsigned arithmetic). 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds 287a2a385d6SEric Dumazet static inline bool before(__u32 seq1, __u32 seq2) 2881da177e4SLinus Torvalds { 2890d630cc0SGerrit Renker return (__s32)(seq1-seq2) < 0; 2901da177e4SLinus Torvalds } 2919a036b9cSGerrit Renker #define after(seq2, seq1) before(seq1, seq2) 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds /* is s2<=s1<=s3 ? */ 294a2a385d6SEric Dumazet static inline bool between(__u32 seq1, __u32 seq2, __u32 seq3) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds return seq3 - seq2 >= seq1 - seq2; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 299efcdbf24SArun Sharma static inline bool tcp_out_of_memory(struct sock *sk) 300efcdbf24SArun Sharma { 301efcdbf24SArun Sharma if (sk->sk_wmem_queued > SOCK_MIN_SNDBUF && 302efcdbf24SArun Sharma sk_memory_allocated(sk) > sk_prot_mem_limits(sk, 2)) 303efcdbf24SArun Sharma return true; 304efcdbf24SArun Sharma return false; 305efcdbf24SArun Sharma } 306efcdbf24SArun Sharma 30703271f3aSTalal Ahmad static inline void tcp_wmem_free_skb(struct sock *sk, struct sk_buff *skb) 30803271f3aSTalal Ahmad { 30903271f3aSTalal Ahmad sk_wmem_queued_add(sk, -skb->truesize); 3109b65b17dSTalal Ahmad if (!skb_zcopy_pure(skb)) 31103271f3aSTalal Ahmad sk_mem_uncharge(sk, skb->truesize); 3129b65b17dSTalal Ahmad else 3139b65b17dSTalal Ahmad sk_mem_uncharge(sk, SKB_TRUESIZE(skb_end_offset(skb))); 31403271f3aSTalal Ahmad __kfree_skb(skb); 31503271f3aSTalal Ahmad } 31603271f3aSTalal Ahmad 317a6c5ea4cSEric Dumazet void sk_forced_mem_schedule(struct sock *sk, int size); 318a6c5ea4cSEric Dumazet 3195c9f3023SJoe Perches bool tcp_check_oom(struct sock *sk, int shift); 320efcdbf24SArun Sharma 321a0f82f64SFlorian Westphal 3221da177e4SLinus Torvalds extern struct proto tcp_prot; 3231da177e4SLinus Torvalds 32457ef42d5SPavel Emelyanov #define TCP_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.tcp_statistics, field) 32513415e46SEric Dumazet #define __TCP_INC_STATS(net, field) __SNMP_INC_STATS((net)->mib.tcp_statistics, field) 32657ef42d5SPavel Emelyanov #define TCP_DEC_STATS(net, field) SNMP_DEC_STATS((net)->mib.tcp_statistics, field) 327aa2ea058STom Herbert #define TCP_ADD_STATS(net, field, val) SNMP_ADD_STATS((net)->mib.tcp_statistics, field, val) 3281da177e4SLinus Torvalds 3295c9f3023SJoe Perches void tcp_tasklet_init(void); 33046d3ceabSEric Dumazet 33132bbd879SStefano Brivio int tcp_v4_err(struct sk_buff *skb, u32); 3321da177e4SLinus Torvalds 3335c9f3023SJoe Perches void tcp_shutdown(struct sock *sk, int how); 3341da177e4SLinus Torvalds 3357487449cSPaolo Abeni int tcp_v4_early_demux(struct sk_buff *skb); 3365c9f3023SJoe Perches int tcp_v4_rcv(struct sk_buff *skb); 3371da177e4SLinus Torvalds 33827728ba8SEric Dumazet void tcp_remove_empty_skb(struct sock *sk); 3391b784140SYing Xue int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size); 340306b13ebSTom Herbert int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size); 3413242abebSBenjamin Hesmans int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg, int *copied, 3423242abebSBenjamin Hesmans size_t size, struct ubuf_info *uarg); 3431d7e4538SDavid Howells void tcp_splice_eof(struct socket *sock); 34435b2c321SMat Martineau int tcp_send_mss(struct sock *sk, int *size_goal, int flags); 345fbf93406SEric Dumazet int tcp_wmem_schedule(struct sock *sk, int copy); 34635b2c321SMat Martineau void tcp_push(struct sock *sk, int flags, int mss_now, int nonagle, 34735b2c321SMat Martineau int size_goal); 3485c9f3023SJoe Perches void tcp_release_cb(struct sock *sk); 3495c9f3023SJoe Perches void tcp_wfree(struct sk_buff *skb); 3505c9f3023SJoe Perches void tcp_write_timer_handler(struct sock *sk); 3515c9f3023SJoe Perches void tcp_delack_timer_handler(struct sock *sk); 352e1d001faSBreno Leitao int tcp_ioctl(struct sock *sk, int cmd, int *karg); 3537d6ed9afSJason Xing enum skb_drop_reason tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb); 3543d97d88eSYafang Shao void tcp_rcv_established(struct sock *sk, struct sk_buff *skb); 3555c9f3023SJoe Perches void tcp_rcv_space_adjust(struct sock *sk); 3565c9f3023SJoe Perches int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp); 3575c9f3023SJoe Perches void tcp_twsk_destructor(struct sock *sk); 3581eeb5043SEric Dumazet void tcp_twsk_purge(struct list_head *net_exit_list); 3595c9f3023SJoe Perches ssize_t tcp_splice_read(struct socket *sk, loff_t *ppos, 36053d3176bSChangli Gao struct pipe_inode_info *pipe, size_t len, 36153d3176bSChangli Gao unsigned int flags); 3625882efffSEric Dumazet struct sk_buff *tcp_stream_alloc_skb(struct sock *sk, gfp_t gfp, 363f8dd3b8dSEric Dumazet bool force_schedule); 3649c55e01cSJens Axboe 365059217c1SNeal Cardwell static inline void tcp_dec_quickack_mode(struct sock *sk) 3661da177e4SLinus Torvalds { 367463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 368fc6415bcSDavid S. Miller 369463c84b9SArnaldo Carvalho de Melo if (icsk->icsk_ack.quick) { 370059217c1SNeal Cardwell /* How many ACKs S/ACKing new data have we sent? */ 371059217c1SNeal Cardwell const unsigned int pkts = inet_csk_ack_scheduled(sk) ? 1 : 0; 372059217c1SNeal Cardwell 373463c84b9SArnaldo Carvalho de Melo if (pkts >= icsk->icsk_ack.quick) { 374463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.quick = 0; 3751da177e4SLinus Torvalds /* Leaving quickack mode we deflate ATO. */ 376463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN; 377fc6415bcSDavid S. Miller } else 378463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.quick -= pkts; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 382bdf1ee5dSIlpo Järvinen #define TCP_ECN_OK 1 383bdf1ee5dSIlpo Järvinen #define TCP_ECN_QUEUE_CWR 2 384bdf1ee5dSIlpo Järvinen #define TCP_ECN_DEMAND_CWR 4 3857a269ffaSEric Dumazet #define TCP_ECN_SEEN 8 386bdf1ee5dSIlpo Järvinen 387fd2c3ef7SEric Dumazet enum tcp_tw_status { 3881da177e4SLinus Torvalds TCP_TW_SUCCESS = 0, 3891da177e4SLinus Torvalds TCP_TW_RST = 1, 3901da177e4SLinus Torvalds TCP_TW_ACK = 2, 3911da177e4SLinus Torvalds TCP_TW_SYN = 3 3921da177e4SLinus Torvalds }; 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds 3955c9f3023SJoe Perches enum tcp_tw_status tcp_timewait_state_process(struct inet_timewait_sock *tw, 3961da177e4SLinus Torvalds struct sk_buff *skb, 39741eecbd7SEric Dumazet const struct tcphdr *th, 39841eecbd7SEric Dumazet u32 *tw_isn); 3995c9f3023SJoe Perches struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb, 400e0f9759fSEric Dumazet struct request_sock *req, bool fastopen, 401e0f9759fSEric Dumazet bool *lost_race); 402b9825695SJason Xing enum skb_drop_reason tcp_child_process(struct sock *parent, struct sock *child, 4031da177e4SLinus Torvalds struct sk_buff *skb); 4045ae344c9SNeal Cardwell void tcp_enter_loss(struct sock *sk); 4057e901ee7SYuchung Cheng void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int newly_lost, int flag); 4065c9f3023SJoe Perches void tcp_clear_retrans(struct tcp_sock *tp); 4075c9f3023SJoe Perches void tcp_update_metrics(struct sock *sk); 4085c9f3023SJoe Perches void tcp_init_metrics(struct sock *sk); 4095c9f3023SJoe Perches void tcp_metrics_init(void); 410d82bae12SSoheil Hassas Yeganeh bool tcp_peer_is_proven(struct request_sock *req, struct dst_entry *dst); 41177c3c956SPaolo Abeni void __tcp_close(struct sock *sk, long timeout); 4125c9f3023SJoe Perches void tcp_close(struct sock *sk, long timeout); 4135c9f3023SJoe Perches void tcp_init_sock(struct sock *sk); 41472be0fe6SMartin KaFai Lau void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb); 415a11e1d43SLinus Torvalds __poll_t tcp_poll(struct file *file, struct socket *sock, 416a11e1d43SLinus Torvalds struct poll_table_struct *wait); 417273b7f0fSMartin KaFai Lau int do_tcp_getsockopt(struct sock *sk, int level, 418273b7f0fSMartin KaFai Lau int optname, sockptr_t optval, sockptr_t optlen); 4195c9f3023SJoe Perches int tcp_getsockopt(struct sock *sk, int level, int optname, 4203fdadf7dSDmitry Mishin char __user *optval, int __user *optlen); 4219cacf81fSStanislav Fomichev bool tcp_bpf_bypass_getsockopt(int level, int optname); 4220c751f70SMartin KaFai Lau int do_tcp_setsockopt(struct sock *sk, int level, int optname, 4230c751f70SMartin KaFai Lau sockptr_t optval, unsigned int optlen); 424a7b75c5aSChristoph Hellwig int tcp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, 425a7b75c5aSChristoph Hellwig unsigned int optlen); 4265c9f3023SJoe Perches void tcp_set_keepalive(struct sock *sk, int val); 42742cb80a2SEric Dumazet void tcp_syn_ack_timeout(const struct request_sock *req); 428ec095263SOliver Hartkopp int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 4291b784140SYing Xue int flags, int *addr_len); 430d1361840SEric Dumazet int tcp_set_rcvlowat(struct sock *sk, int val); 431cb811109SPrankur gupta int tcp_set_window_clamp(struct sock *sk, int val); 432892bfd3dSFlorian Westphal void tcp_update_recv_tstamps(struct sk_buff *skb, 433892bfd3dSFlorian Westphal struct scm_timestamping_internal *tss); 434892bfd3dSFlorian Westphal void tcp_recv_timestamp(struct msghdr *msg, const struct sock *sk, 435892bfd3dSFlorian Westphal struct scm_timestamping_internal *tss); 43603f45c88SEric Dumazet void tcp_data_ready(struct sock *sk); 437340a6f3dSYafang Shao #ifdef CONFIG_MMU 43893ab6cc6SEric Dumazet int tcp_mmap(struct file *file, struct socket *sock, 43993ab6cc6SEric Dumazet struct vm_area_struct *vma); 440340a6f3dSYafang Shao #endif 441eed29f17SEric Dumazet void tcp_parse_options(const struct net *net, const struct sk_buff *skb, 4421a2c6181SChristoph Paasch struct tcp_options_received *opt_rx, 4432100c8d2SYuchung Cheng int estab, struct tcp_fastopen_cookie *foc); 4447d5d5525SYOSHIFUJI Hideaki 4451da177e4SLinus Torvalds /* 4469349d600SPetar Penkov * BPF SKB-less helpers 4479349d600SPetar Penkov */ 4489349d600SPetar Penkov u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph, 4499349d600SPetar Penkov struct tcphdr *th, u32 *cookie); 4509349d600SPetar Penkov u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph, 4519349d600SPetar Penkov struct tcphdr *th, u32 *cookie); 45233bf9885SMaxim Mikityanskiy u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss); 4539349d600SPetar Penkov u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops, 4549349d600SPetar Penkov const struct tcp_request_sock_ops *af_ops, 4559349d600SPetar Penkov struct sock *sk, struct tcphdr *th); 4569349d600SPetar Penkov /* 4571da177e4SLinus Torvalds * TCP v4 functions exported for the inet6 API 4581da177e4SLinus Torvalds */ 4591da177e4SLinus Torvalds 4605c9f3023SJoe Perches void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb); 4614fab9071SNeal Cardwell void tcp_v4_mtu_reduced(struct sock *sk); 4629cf74903SEric Dumazet void tcp_req_err(struct sock *sk, u32 seq, bool abort); 463d2924569SEric Dumazet void tcp_ld_RTO_revert(struct sock *sk, u32 seq); 4645c9f3023SJoe Perches int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb); 465c28c6f04SEric Dumazet struct sock *tcp_create_openreq_child(const struct sock *sk, 46660236fddSArnaldo Carvalho de Melo struct request_sock *req, 4671da177e4SLinus Torvalds struct sk_buff *skb); 46881164413SDaniel Borkmann void tcp_ca_openreq_child(struct sock *sk, const struct dst_entry *dst); 4690c27171eSEric Dumazet struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb, 47060236fddSArnaldo Carvalho de Melo struct request_sock *req, 4715e0724d0SEric Dumazet struct dst_entry *dst, 4725e0724d0SEric Dumazet struct request_sock *req_unhash, 4735e0724d0SEric Dumazet bool *own_req); 4745c9f3023SJoe Perches int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb); 4755c9f3023SJoe Perches int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len); 4765c9f3023SJoe Perches int tcp_connect(struct sock *sk); 477b3d05147SEric Dumazet enum tcp_synack_type { 478b3d05147SEric Dumazet TCP_SYNACK_NORMAL, 479b3d05147SEric Dumazet TCP_SYNACK_FASTOPEN, 480b3d05147SEric Dumazet TCP_SYNACK_COOKIE, 481b3d05147SEric Dumazet }; 4825d062de7SEric Dumazet struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst, 483e6b4d113SWilliam Allen Simpson struct request_sock *req, 484ca6fb065SEric Dumazet struct tcp_fastopen_cookie *foc, 485331fca43SMartin KaFai Lau enum tcp_synack_type synack_type, 486331fca43SMartin KaFai Lau struct sk_buff *syn_skb); 4875c9f3023SJoe Perches int tcp_disconnect(struct sock *sk, int flags); 4881da177e4SLinus Torvalds 489370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb); 490292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size); 49163d02d15SEric Dumazet void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb); 4921da177e4SLinus Torvalds 4931da177e4SLinus Torvalds /* From syncookies.c */ 494b80c0e78SEric Dumazet struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb, 495b80c0e78SEric Dumazet struct request_sock *req, 496efce3d1fSKuniyuki Iwashima struct dst_entry *dst); 4977577bc82SKuniyuki Iwashima int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th); 498461b74c3SCong Wang struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb); 4996fc8c827SFlorian Westphal struct request_sock *cookie_tcp_reqsk_alloc(const struct request_sock_ops *ops, 5008e7bab6bSKuniyuki Iwashima struct sock *sk, struct sk_buff *skb, 5018e7bab6bSKuniyuki Iwashima struct tcp_options_received *tcp_opt, 5028e7bab6bSKuniyuki Iwashima int mss, u32 tsoff); 5038e7bab6bSKuniyuki Iwashima 504b3f086a7SKuniyuki Iwashima #if IS_ENABLED(CONFIG_BPF) 505b3f086a7SKuniyuki Iwashima struct bpf_tcp_req_attrs { 506b3f086a7SKuniyuki Iwashima u32 rcv_tsval; 507b3f086a7SKuniyuki Iwashima u32 rcv_tsecr; 508b3f086a7SKuniyuki Iwashima u16 mss; 509b3f086a7SKuniyuki Iwashima u8 rcv_wscale; 510b3f086a7SKuniyuki Iwashima u8 snd_wscale; 511b3f086a7SKuniyuki Iwashima u8 ecn_ok; 512b3f086a7SKuniyuki Iwashima u8 wscale_ok; 513b3f086a7SKuniyuki Iwashima u8 sack_ok; 514b3f086a7SKuniyuki Iwashima u8 tstamp_ok; 515b3f086a7SKuniyuki Iwashima u8 usec_ts_ok; 516b3f086a7SKuniyuki Iwashima u8 reserved[3]; 517b3f086a7SKuniyuki Iwashima }; 518b3f086a7SKuniyuki Iwashima #endif 519b3f086a7SKuniyuki Iwashima 520e05c82d3SEric Dumazet #ifdef CONFIG_SYN_COOKIES 5218c27bd75SFlorian Westphal 52263262315SEric Dumazet /* Syncookies use a monotonic timer which increments every 60 seconds. 5238c27bd75SFlorian Westphal * This counter is used both as a hash input and partially encoded into 5248c27bd75SFlorian Westphal * the cookie value. A cookie is only validated further if the delta 5258c27bd75SFlorian Westphal * between the current counter value and the encoded one is less than this, 52663262315SEric Dumazet * i.e. a sent cookie is valid only at most for 2*60 seconds (or less if 5278c27bd75SFlorian Westphal * the counter advances immediately after a cookie is generated). 5288c27bd75SFlorian Westphal */ 5298c27bd75SFlorian Westphal #define MAX_SYNCOOKIE_AGE 2 530264ea103SEric Dumazet #define TCP_SYNCOOKIE_PERIOD (60 * HZ) 531264ea103SEric Dumazet #define TCP_SYNCOOKIE_VALID (MAX_SYNCOOKIE_AGE * TCP_SYNCOOKIE_PERIOD) 532264ea103SEric Dumazet 533264ea103SEric Dumazet /* syncookies: remember time of last synqueue overflow 534264ea103SEric Dumazet * But do not dirty this field too often (once per second is enough) 5353f684b4bSEric Dumazet * It is racy as we do not hold a lock, but race is very minor. 536264ea103SEric Dumazet */ 5373f684b4bSEric Dumazet static inline void tcp_synq_overflow(const struct sock *sk) 538264ea103SEric Dumazet { 53940a1227eSMartin KaFai Lau unsigned int last_overflow; 540cca9bab1SArnd Bergmann unsigned int now = jiffies; 541264ea103SEric Dumazet 54240a1227eSMartin KaFai Lau if (sk->sk_reuseport) { 54340a1227eSMartin KaFai Lau struct sock_reuseport *reuse; 54440a1227eSMartin KaFai Lau 54540a1227eSMartin KaFai Lau reuse = rcu_dereference(sk->sk_reuseport_cb); 54640a1227eSMartin KaFai Lau if (likely(reuse)) { 54740a1227eSMartin KaFai Lau last_overflow = READ_ONCE(reuse->synq_overflow_ts); 54804d26e7bSGuillaume Nault if (!time_between32(now, last_overflow, 54904d26e7bSGuillaume Nault last_overflow + HZ)) 55040a1227eSMartin KaFai Lau WRITE_ONCE(reuse->synq_overflow_ts, now); 55140a1227eSMartin KaFai Lau return; 55240a1227eSMartin KaFai Lau } 55340a1227eSMartin KaFai Lau } 55440a1227eSMartin KaFai Lau 555721c8dafSGuillaume Nault last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp); 55604d26e7bSGuillaume Nault if (!time_between32(now, last_overflow, last_overflow + HZ)) 557e9d9da91SEric Dumazet WRITE_ONCE(tcp_sk_rw(sk)->rx_opt.ts_recent_stamp, now); 558264ea103SEric Dumazet } 559264ea103SEric Dumazet 560264ea103SEric Dumazet /* syncookies: no recent synqueue overflow on this listening socket? */ 561264ea103SEric Dumazet static inline bool tcp_synq_no_recent_overflow(const struct sock *sk) 562264ea103SEric Dumazet { 56340a1227eSMartin KaFai Lau unsigned int last_overflow; 564cca9bab1SArnd Bergmann unsigned int now = jiffies; 565264ea103SEric Dumazet 56640a1227eSMartin KaFai Lau if (sk->sk_reuseport) { 56740a1227eSMartin KaFai Lau struct sock_reuseport *reuse; 56840a1227eSMartin KaFai Lau 56940a1227eSMartin KaFai Lau reuse = rcu_dereference(sk->sk_reuseport_cb); 57040a1227eSMartin KaFai Lau if (likely(reuse)) { 57140a1227eSMartin KaFai Lau last_overflow = READ_ONCE(reuse->synq_overflow_ts); 572cb44a08fSGuillaume Nault return !time_between32(now, last_overflow - HZ, 573cb44a08fSGuillaume Nault last_overflow + 57440a1227eSMartin KaFai Lau TCP_SYNCOOKIE_VALID); 57540a1227eSMartin KaFai Lau } 57640a1227eSMartin KaFai Lau } 57740a1227eSMartin KaFai Lau 578721c8dafSGuillaume Nault last_overflow = READ_ONCE(tcp_sk(sk)->rx_opt.ts_recent_stamp); 579cb44a08fSGuillaume Nault 580cb44a08fSGuillaume Nault /* If last_overflow <= jiffies <= last_overflow + TCP_SYNCOOKIE_VALID, 581cb44a08fSGuillaume Nault * then we're under synflood. However, we have to use 582cb44a08fSGuillaume Nault * 'last_overflow - HZ' as lower bound. That's because a concurrent 583cb44a08fSGuillaume Nault * tcp_synq_overflow() could update .ts_recent_stamp after we read 584cb44a08fSGuillaume Nault * jiffies but before we store .ts_recent_stamp into last_overflow, 585cb44a08fSGuillaume Nault * which could lead to rejecting a valid syncookie. 586cb44a08fSGuillaume Nault */ 587cb44a08fSGuillaume Nault return !time_between32(now, last_overflow - HZ, 588cb44a08fSGuillaume Nault last_overflow + TCP_SYNCOOKIE_VALID); 589264ea103SEric Dumazet } 5908c27bd75SFlorian Westphal 5918c27bd75SFlorian Westphal static inline u32 tcp_cookie_time(void) 5928c27bd75SFlorian Westphal { 59363262315SEric Dumazet u64 val = get_jiffies_64(); 59463262315SEric Dumazet 595264ea103SEric Dumazet do_div(val, TCP_SYNCOOKIE_PERIOD); 59663262315SEric Dumazet return val; 5978c27bd75SFlorian Westphal } 5988c27bd75SFlorian Westphal 599b18afb6fSKuniyuki Iwashima /* Convert one nsec 64bit timestamp to ts (ms or usec resolution) */ 600b18afb6fSKuniyuki Iwashima static inline u64 tcp_ns_to_ts(bool usec_ts, u64 val) 601b18afb6fSKuniyuki Iwashima { 602b18afb6fSKuniyuki Iwashima if (usec_ts) 603b18afb6fSKuniyuki Iwashima return div_u64(val, NSEC_PER_USEC); 604b18afb6fSKuniyuki Iwashima 605b18afb6fSKuniyuki Iwashima return div_u64(val, NSEC_PER_MSEC); 606b18afb6fSKuniyuki Iwashima } 607b18afb6fSKuniyuki Iwashima 6085c9f3023SJoe Perches u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th, 6095c9f3023SJoe Perches u16 *mssp); 6103f684b4bSEric Dumazet __u32 cookie_v4_init_sequence(const struct sk_buff *skb, __u16 *mss); 611200ecef6SEric Dumazet u64 cookie_init_timestamp(struct request_sock *req, u64 now); 612f9301034SEric Dumazet bool cookie_timestamp_decode(const struct net *net, 613f9301034SEric Dumazet struct tcp_options_received *opt); 6148e7bab6bSKuniyuki Iwashima 6158e7bab6bSKuniyuki Iwashima static inline bool cookie_ecn_ok(const struct net *net, const struct dst_entry *dst) 6168e7bab6bSKuniyuki Iwashima { 6178e7bab6bSKuniyuki Iwashima return READ_ONCE(net->ipv4.sysctl_tcp_ecn) || 6188e7bab6bSKuniyuki Iwashima dst_feature(dst, RTAX_FEATURE_ECN); 6198e7bab6bSKuniyuki Iwashima } 6204dfc2817SFlorian Westphal 621695751e3SKuniyuki Iwashima #if IS_ENABLED(CONFIG_BPF) 622695751e3SKuniyuki Iwashima static inline bool cookie_bpf_ok(struct sk_buff *skb) 623695751e3SKuniyuki Iwashima { 624695751e3SKuniyuki Iwashima return skb->sk; 625695751e3SKuniyuki Iwashima } 626695751e3SKuniyuki Iwashima 627695751e3SKuniyuki Iwashima struct request_sock *cookie_bpf_check(struct sock *sk, struct sk_buff *skb); 628695751e3SKuniyuki Iwashima #else 629695751e3SKuniyuki Iwashima static inline bool cookie_bpf_ok(struct sk_buff *skb) 630695751e3SKuniyuki Iwashima { 631695751e3SKuniyuki Iwashima return false; 632695751e3SKuniyuki Iwashima } 633695751e3SKuniyuki Iwashima 634695751e3SKuniyuki Iwashima static inline struct request_sock *cookie_bpf_check(struct net *net, struct sock *sk, 635695751e3SKuniyuki Iwashima struct sk_buff *skb) 636695751e3SKuniyuki Iwashima { 637695751e3SKuniyuki Iwashima return NULL; 638695751e3SKuniyuki Iwashima } 639695751e3SKuniyuki Iwashima #endif 640695751e3SKuniyuki Iwashima 641c6aefafbSGlenn Griffin /* From net/ipv6/syncookies.c */ 6427577bc82SKuniyuki Iwashima int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th); 6435c9f3023SJoe Perches struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb); 644f1673381SFlorian Westphal 6455c9f3023SJoe Perches u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph, 64681eb6a14SPatrick McHardy const struct tcphdr *th, u16 *mssp); 6473f684b4bSEric Dumazet __u32 cookie_v6_init_sequence(const struct sk_buff *skb, __u16 *mss); 648e05c82d3SEric Dumazet #endif 6491da177e4SLinus Torvalds /* tcp_output.c */ 6501da177e4SLinus Torvalds 65104d8825cSPaolo Abeni void tcp_skb_entail(struct sock *sk, struct sk_buff *skb); 65204d8825cSPaolo Abeni void tcp_mark_push(struct tcp_sock *tp, struct sk_buff *skb); 6535c9f3023SJoe Perches void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss, 6549e412ba7SIlpo Järvinen int nonagle); 65510d3be56SEric Dumazet int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs); 65610d3be56SEric Dumazet int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs); 6575c9f3023SJoe Perches void tcp_retransmit_timer(struct sock *sk); 6585c9f3023SJoe Perches void tcp_xmit_retransmit_queue(struct sock *); 6595c9f3023SJoe Perches void tcp_simple_retransmit(struct sock *); 66057dde7f7SYuchung Cheng void tcp_enter_recovery(struct sock *sk, bool ece_ack); 6615c9f3023SJoe Perches int tcp_trim_head(struct sock *, struct sk_buff *, u32); 66275c119afSEric Dumazet enum tcp_queue { 66375c119afSEric Dumazet TCP_FRAG_IN_WRITE_QUEUE, 66475c119afSEric Dumazet TCP_FRAG_IN_RTX_QUEUE, 66575c119afSEric Dumazet }; 66675c119afSEric Dumazet int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue, 66775c119afSEric Dumazet struct sk_buff *skb, u32 len, 66875c119afSEric Dumazet unsigned int mss_now, gfp_t gfp); 6691da177e4SLinus Torvalds 6705c9f3023SJoe Perches void tcp_send_probe0(struct sock *); 671e520af48SEric Dumazet int tcp_write_wakeup(struct sock *, int mib); 6725c9f3023SJoe Perches void tcp_send_fin(struct sock *sk); 673*5691276bSJason Xing void tcp_send_active_reset(struct sock *sk, gfp_t priority, 674*5691276bSJason Xing enum sk_rst_reason reason); 6755c9f3023SJoe Perches int tcp_send_synack(struct sock *); 6765c9f3023SJoe Perches void tcp_push_one(struct sock *, unsigned int mss_now); 67727cde44aSYuchung Cheng void __tcp_send_ack(struct sock *sk, u32 rcv_nxt); 6785c9f3023SJoe Perches void tcp_send_ack(struct sock *sk); 6795c9f3023SJoe Perches void tcp_send_delayed_ack(struct sock *sk); 6805c9f3023SJoe Perches void tcp_send_loss_probe(struct sock *sk); 681ed66dfafSNeal Cardwell bool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto); 682cfea5a68SMartin KaFai Lau void tcp_skb_collapse_tstamp(struct sk_buff *skb, 683cfea5a68SMartin KaFai Lau const struct sk_buff *next_skb); 6841da177e4SLinus Torvalds 685a762a980SDavid S. Miller /* tcp_input.c */ 6865c9f3023SJoe Perches void tcp_rearm_rto(struct sock *sk); 6870f1c28aeSYuchung Cheng void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req); 688049fe386SFlorian Westphal void tcp_reset(struct sock *sk, struct sk_buff *skb); 689e3e17b77SEric Dumazet void tcp_fin(struct sock *sk); 6904bfe744fSEric Dumazet void tcp_check_space(struct sock *sk); 69130c6f0bfSfuyuanli void tcp_sack_compress_send_ack(struct sock *sk); 692a762a980SDavid S. Miller 6931da177e4SLinus Torvalds /* tcp_timer.c */ 6945c9f3023SJoe Perches void tcp_init_xmit_timers(struct sock *); 695463c84b9SArnaldo Carvalho de Melo static inline void tcp_clear_xmit_timers(struct sock *sk) 696463c84b9SArnaldo Carvalho de Melo { 69773a6bab5SEric Dumazet if (hrtimer_try_to_cancel(&tcp_sk(sk)->pacing_timer) == 1) 698cf0dd203SEric Dumazet __sock_put(sk); 69973a6bab5SEric Dumazet 7005d9f4262SEric Dumazet if (hrtimer_try_to_cancel(&tcp_sk(sk)->compressed_ack_timer) == 1) 7015d9f4262SEric Dumazet __sock_put(sk); 7025d9f4262SEric Dumazet 703463c84b9SArnaldo Carvalho de Melo inet_csk_clear_xmit_timers(sk); 704463c84b9SArnaldo Carvalho de Melo } 7051da177e4SLinus Torvalds 7065c9f3023SJoe Perches unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu); 7075c9f3023SJoe Perches unsigned int tcp_current_mss(struct sock *sk); 708344db93aSEnke Chen u32 tcp_clamp_probe0_to_user_timeout(const struct sock *sk, u32 when); 7090c54b85fSIlpo Järvinen 7100c54b85fSIlpo Järvinen /* Bound MSS / TSO packet size with the half of the window */ 7110c54b85fSIlpo Järvinen static inline int tcp_bound_to_half_wnd(struct tcp_sock *tp, int pktsize) 7120c54b85fSIlpo Järvinen { 71301f83d69SAlexey Kuznetsov int cutoff; 71401f83d69SAlexey Kuznetsov 71501f83d69SAlexey Kuznetsov /* When peer uses tiny windows, there is no use in packetizing 71601f83d69SAlexey Kuznetsov * to sub-MSS pieces for the sake of SWS or making sure there 71701f83d69SAlexey Kuznetsov * are enough packets in the pipe for fast recovery. 71801f83d69SAlexey Kuznetsov * 71901f83d69SAlexey Kuznetsov * On the other hand, for extremely large MSS devices, handling 72001f83d69SAlexey Kuznetsov * smaller than MSS windows in this way does make sense. 72101f83d69SAlexey Kuznetsov */ 7222631b79fSSeymour, Shane M if (tp->max_window > TCP_MSS_DEFAULT) 72301f83d69SAlexey Kuznetsov cutoff = (tp->max_window >> 1); 72401f83d69SAlexey Kuznetsov else 72501f83d69SAlexey Kuznetsov cutoff = tp->max_window; 72601f83d69SAlexey Kuznetsov 72701f83d69SAlexey Kuznetsov if (cutoff && pktsize > cutoff) 72801f83d69SAlexey Kuznetsov return max_t(int, cutoff, 68U - tp->tcp_header_len); 7290c54b85fSIlpo Järvinen else 7300c54b85fSIlpo Järvinen return pktsize; 7310c54b85fSIlpo Järvinen } 7321da177e4SLinus Torvalds 73317b085eaSArnaldo Carvalho de Melo /* tcp.c */ 7340df48c26SEric Dumazet void tcp_get_info(struct sock *, struct tcp_info *); 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds /* Read 'sendfile()'-style from a TCP socket */ 7375c9f3023SJoe Perches int tcp_read_sock(struct sock *sk, read_descriptor_t *desc, 7381da177e4SLinus Torvalds sk_read_actor_t recv_actor); 739965b57b4SCong Wang int tcp_read_skb(struct sock *sk, skb_read_actor_t recv_actor); 7403f92a64eSJakub Kicinski struct sk_buff *tcp_recv_skb(struct sock *sk, u32 seq, u32 *off); 7413f92a64eSJakub Kicinski void tcp_read_done(struct sock *sk, size_t len); 7421da177e4SLinus Torvalds 7435c9f3023SJoe Perches void tcp_initialize_rcv_mss(struct sock *sk); 7441da177e4SLinus Torvalds 7455c9f3023SJoe Perches int tcp_mtu_to_mss(struct sock *sk, int pmtu); 7465c9f3023SJoe Perches int tcp_mss_to_mtu(struct sock *sk, int mss); 7475c9f3023SJoe Perches void tcp_mtup_init(struct sock *sk); 7485d424d5aSJohn Heffner 74958169ec9SEric Dumazet static inline void tcp_bound_rto(struct sock *sk) 750f1ecd5d9SDamian Lukowski { 751f1ecd5d9SDamian Lukowski if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX) 752f1ecd5d9SDamian Lukowski inet_csk(sk)->icsk_rto = TCP_RTO_MAX; 753f1ecd5d9SDamian Lukowski } 754f1ecd5d9SDamian Lukowski 755f1ecd5d9SDamian Lukowski static inline u32 __tcp_set_rto(const struct tcp_sock *tp) 756f1ecd5d9SDamian Lukowski { 757740b0f18SEric Dumazet return usecs_to_jiffies((tp->srtt_us >> 3) + tp->rttvar_us); 758f1ecd5d9SDamian Lukowski } 759f1ecd5d9SDamian Lukowski 76031770e34SFlorian Westphal static inline void __tcp_fast_path_on(struct tcp_sock *tp, u32 snd_wnd) 76131770e34SFlorian Westphal { 76271158bb1SPaolo Abeni /* mptcp hooks are only on the slow path */ 76371158bb1SPaolo Abeni if (sk_is_mptcp((struct sock *)tp)) 76471158bb1SPaolo Abeni return; 76571158bb1SPaolo Abeni 76631770e34SFlorian Westphal tp->pred_flags = htonl((tp->tcp_header_len << 26) | 76731770e34SFlorian Westphal ntohl(TCP_FLAG_ACK) | 76831770e34SFlorian Westphal snd_wnd); 76931770e34SFlorian Westphal } 77031770e34SFlorian Westphal 77131770e34SFlorian Westphal static inline void tcp_fast_path_on(struct tcp_sock *tp) 77231770e34SFlorian Westphal { 77331770e34SFlorian Westphal __tcp_fast_path_on(tp, tp->snd_wnd >> tp->rx_opt.snd_wscale); 77431770e34SFlorian Westphal } 77531770e34SFlorian Westphal 77631770e34SFlorian Westphal static inline void tcp_fast_path_check(struct sock *sk) 77731770e34SFlorian Westphal { 77831770e34SFlorian Westphal struct tcp_sock *tp = tcp_sk(sk); 77931770e34SFlorian Westphal 78031770e34SFlorian Westphal if (RB_EMPTY_ROOT(&tp->out_of_order_queue) && 78131770e34SFlorian Westphal tp->rcv_wnd && 78231770e34SFlorian Westphal atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf && 78331770e34SFlorian Westphal !tp->urg_data) 78431770e34SFlorian Westphal tcp_fast_path_on(tp); 78531770e34SFlorian Westphal } 78631770e34SFlorian Westphal 787bbf80d71SEric Dumazet u32 tcp_delack_max(const struct sock *sk); 788bbf80d71SEric Dumazet 7890c266898SSatoru SATOH /* Compute the actual rto_min value */ 790f68a181fSEric Dumazet static inline u32 tcp_rto_min(const struct sock *sk) 7910c266898SSatoru SATOH { 792cf533ea5SEric Dumazet const struct dst_entry *dst = __sk_dst_get(sk); 793ca584ba0SMartin KaFai Lau u32 rto_min = inet_csk(sk)->icsk_rto_min; 7940c266898SSatoru SATOH 7950c266898SSatoru SATOH if (dst && dst_metric_locked(dst, RTAX_RTO_MIN)) 7960c266898SSatoru SATOH rto_min = dst_metric_rtt(dst, RTAX_RTO_MIN); 7970c266898SSatoru SATOH return rto_min; 7980c266898SSatoru SATOH } 7990c266898SSatoru SATOH 800f68a181fSEric Dumazet static inline u32 tcp_rto_min_us(const struct sock *sk) 801740b0f18SEric Dumazet { 802740b0f18SEric Dumazet return jiffies_to_usecs(tcp_rto_min(sk)); 803740b0f18SEric Dumazet } 804740b0f18SEric Dumazet 80581164413SDaniel Borkmann static inline bool tcp_ca_dst_locked(const struct dst_entry *dst) 80681164413SDaniel Borkmann { 80781164413SDaniel Borkmann return dst_metric_locked(dst, RTAX_CC_ALGO); 80881164413SDaniel Borkmann } 80981164413SDaniel Borkmann 810f6722583SYuchung Cheng /* Minimum RTT in usec. ~0 means not available. */ 811f6722583SYuchung Cheng static inline u32 tcp_min_rtt(const struct tcp_sock *tp) 812f6722583SYuchung Cheng { 81364033892SNeal Cardwell return minmax_get(&tp->rtt_min); 814f6722583SYuchung Cheng } 815f6722583SYuchung Cheng 8161da177e4SLinus Torvalds /* Compute the actual receive window we are currently advertising. 8171da177e4SLinus Torvalds * Rcv_nxt can be after the window if our peer push more data 8181da177e4SLinus Torvalds * than the offered window. 8191da177e4SLinus Torvalds */ 82040efc6faSStephen Hemminger static inline u32 tcp_receive_window(const struct tcp_sock *tp) 8211da177e4SLinus Torvalds { 8221da177e4SLinus Torvalds s32 win = tp->rcv_wup + tp->rcv_wnd - tp->rcv_nxt; 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds if (win < 0) 8251da177e4SLinus Torvalds win = 0; 8261da177e4SLinus Torvalds return (u32) win; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* Choose a new window, without checks for shrinking, and without 8301da177e4SLinus Torvalds * scaling applied to the result. The caller does these things 8311da177e4SLinus Torvalds * if necessary. This is a "raw" window selection. 8321da177e4SLinus Torvalds */ 8335c9f3023SJoe Perches u32 __tcp_select_window(struct sock *sk); 8341da177e4SLinus Torvalds 835ee995283SPavel Emelyanov void tcp_send_window_probe(struct sock *sk); 836ee995283SPavel Emelyanov 837ec66eda8SEric Dumazet /* TCP uses 32bit jiffies to save some space. 838ec66eda8SEric Dumazet * Note that this is different from tcp_time_stamp, which 839ec66eda8SEric Dumazet * historically has been the same until linux-4.13. 840ec66eda8SEric Dumazet */ 841ec66eda8SEric Dumazet #define tcp_jiffies32 ((u32)jiffies) 842ec66eda8SEric Dumazet 8439a568de4SEric Dumazet /* 8449a568de4SEric Dumazet * Deliver a 32bit value for TCP timestamp option (RFC 7323) 8459a568de4SEric Dumazet * It is no longer tied to jiffies, but to 1 ms clock. 8469a568de4SEric Dumazet * Note: double check if you want to use tcp_jiffies32 instead of this. 8471da177e4SLinus Torvalds */ 8489a568de4SEric Dumazet #define TCP_TS_HZ 1000 8499a568de4SEric Dumazet 8509a568de4SEric Dumazet static inline u64 tcp_clock_ns(void) 8519a568de4SEric Dumazet { 852fb420d5dSEric Dumazet return ktime_get_ns(); 8539a568de4SEric Dumazet } 8549a568de4SEric Dumazet 8559a568de4SEric Dumazet static inline u64 tcp_clock_us(void) 8569a568de4SEric Dumazet { 8579a568de4SEric Dumazet return div_u64(tcp_clock_ns(), NSEC_PER_USEC); 8589a568de4SEric Dumazet } 8599a568de4SEric Dumazet 8602a7c8d29SEric Dumazet static inline u64 tcp_clock_ms(void) 8612a7c8d29SEric Dumazet { 8622a7c8d29SEric Dumazet return div_u64(tcp_clock_ns(), NSEC_PER_MSEC); 8632a7c8d29SEric Dumazet } 8642a7c8d29SEric Dumazet 86516cf6477SEric Dumazet /* TCP Timestamp included in TS option (RFC 1323) can either use ms 86616cf6477SEric Dumazet * or usec resolution. Each socket carries a flag to select one or other 86716cf6477SEric Dumazet * resolution, as the route attribute could change anytime. 86816cf6477SEric Dumazet * Each flow must stick to initial resolution. 86916cf6477SEric Dumazet */ 87016cf6477SEric Dumazet static inline u32 tcp_clock_ts(bool usec_ts) 87116cf6477SEric Dumazet { 87216cf6477SEric Dumazet return usec_ts ? tcp_clock_us() : tcp_clock_ms(); 87316cf6477SEric Dumazet } 87416cf6477SEric Dumazet 87599d67955SEric Dumazet static inline u32 tcp_time_stamp_ms(const struct tcp_sock *tp) 87699d67955SEric Dumazet { 87799d67955SEric Dumazet return div_u64(tp->tcp_mstamp, USEC_PER_MSEC); 87899d67955SEric Dumazet } 87999d67955SEric Dumazet 8809d0c00f5SEric Dumazet static inline u32 tcp_time_stamp_ts(const struct tcp_sock *tp) 8819d0c00f5SEric Dumazet { 882614e8316SEric Dumazet if (tp->tcp_usec_ts) 883614e8316SEric Dumazet return tp->tcp_mstamp; 8849d0c00f5SEric Dumazet return tcp_time_stamp_ms(tp); 8859d0c00f5SEric Dumazet } 8869d0c00f5SEric Dumazet 8879799ccb0SEric Dumazet void tcp_mstamp_refresh(struct tcp_sock *tp); 8889a568de4SEric Dumazet 8899a568de4SEric Dumazet static inline u32 tcp_stamp_us_delta(u64 t1, u64 t0) 8909a568de4SEric Dumazet { 8919a568de4SEric Dumazet return max_t(s64, t1 - t0, 0); 8929a568de4SEric Dumazet } 8931da177e4SLinus Torvalds 8942fd66ffbSEric Dumazet /* provide the departure time in us unit */ 8952fd66ffbSEric Dumazet static inline u64 tcp_skb_timestamp_us(const struct sk_buff *skb) 8962fd66ffbSEric Dumazet { 897d3edd06eSEric Dumazet return div_u64(skb->skb_mstamp_ns, NSEC_PER_USEC); 8982fd66ffbSEric Dumazet } 8992fd66ffbSEric Dumazet 900d1a02ed6SEric Dumazet /* Provide skb TSval in usec or ms unit */ 901d1a02ed6SEric Dumazet static inline u32 tcp_skb_timestamp_ts(bool usec_ts, const struct sk_buff *skb) 902d1a02ed6SEric Dumazet { 903d1a02ed6SEric Dumazet if (usec_ts) 904d1a02ed6SEric Dumazet return tcp_skb_timestamp_us(skb); 905d1a02ed6SEric Dumazet 906d1a02ed6SEric Dumazet return div_u64(skb->skb_mstamp_ns, NSEC_PER_MSEC); 907d1a02ed6SEric Dumazet } 908d1a02ed6SEric Dumazet 90916cf6477SEric Dumazet static inline u32 tcp_tw_tsval(const struct tcp_timewait_sock *tcptw) 91016cf6477SEric Dumazet { 911614e8316SEric Dumazet return tcp_clock_ts(tcptw->tw_sk.tw_usec_ts) + tcptw->tw_ts_offset; 91216cf6477SEric Dumazet } 91316cf6477SEric Dumazet 91416cf6477SEric Dumazet static inline u32 tcp_rsk_tsval(const struct tcp_request_sock *treq) 91516cf6477SEric Dumazet { 916614e8316SEric Dumazet return tcp_clock_ts(treq->req_usec_ts) + treq->ts_off; 91716cf6477SEric Dumazet } 9187faee5c0SEric Dumazet 919a3433f35SChangli Gao #define tcp_flag_byte(th) (((u_int8_t *)th)[13]) 920a3433f35SChangli Gao 921a3433f35SChangli Gao #define TCPHDR_FIN 0x01 922a3433f35SChangli Gao #define TCPHDR_SYN 0x02 923a3433f35SChangli Gao #define TCPHDR_RST 0x04 924a3433f35SChangli Gao #define TCPHDR_PSH 0x08 925a3433f35SChangli Gao #define TCPHDR_ACK 0x10 926a3433f35SChangli Gao #define TCPHDR_URG 0x20 927a3433f35SChangli Gao #define TCPHDR_ECE 0x40 928a3433f35SChangli Gao #define TCPHDR_CWR 0x80 929a3433f35SChangli Gao 93049213555SDaniel Borkmann #define TCPHDR_SYN_ECN (TCPHDR_SYN | TCPHDR_ECE | TCPHDR_CWR) 93149213555SDaniel Borkmann 93214b5fb21SPhilo Lu /* State flags for sacked in struct tcp_skb_cb */ 93314b5fb21SPhilo Lu enum tcp_skb_cb_sacked_flags { 93414b5fb21SPhilo Lu TCPCB_SACKED_ACKED = (1 << 0), /* SKB ACK'd by a SACK block */ 93514b5fb21SPhilo Lu TCPCB_SACKED_RETRANS = (1 << 1), /* SKB retransmitted */ 93614b5fb21SPhilo Lu TCPCB_LOST = (1 << 2), /* SKB is lost */ 93714b5fb21SPhilo Lu TCPCB_TAGBITS = (TCPCB_SACKED_ACKED | TCPCB_SACKED_RETRANS | 93814b5fb21SPhilo Lu TCPCB_LOST), /* All tag bits */ 93914b5fb21SPhilo Lu TCPCB_REPAIRED = (1 << 4), /* SKB repaired (no skb_mstamp_ns) */ 94014b5fb21SPhilo Lu TCPCB_EVER_RETRANS = (1 << 7), /* Ever retransmitted frame */ 94114b5fb21SPhilo Lu TCPCB_RETRANS = (TCPCB_SACKED_RETRANS | TCPCB_EVER_RETRANS | 94214b5fb21SPhilo Lu TCPCB_REPAIRED), 94314b5fb21SPhilo Lu }; 94414b5fb21SPhilo Lu 945caa20d9aSStephen Hemminger /* This is what the send packet queuing engine uses to pass 946f86586faSEric Dumazet * TCP per-packet control information to the transmission code. 947f86586faSEric Dumazet * We also store the host-order sequence numbers in here too. 948f86586faSEric Dumazet * This is 44 bytes if IPV6 is enabled. 949f86586faSEric Dumazet * If this grows please adjust skbuff.h:skbuff->cb[xxx] size appropriately. 9501da177e4SLinus Torvalds */ 9511da177e4SLinus Torvalds struct tcp_skb_cb { 9521da177e4SLinus Torvalds __u32 seq; /* Starting sequence number */ 9531da177e4SLinus Torvalds __u32 end_seq; /* SEQ + FIN + SYN + datalen */ 954cd7d8498SEric Dumazet union { 95541eecbd7SEric Dumazet /* Note : 956f69ad292SEric Dumazet * tcp_gso_segs/size are used in write queue only, 957f69ad292SEric Dumazet * cf tcp_skb_pcount()/tcp_skb_mss() 958cd7d8498SEric Dumazet */ 959f69ad292SEric Dumazet struct { 960f69ad292SEric Dumazet u16 tcp_gso_segs; 961f69ad292SEric Dumazet u16 tcp_gso_size; 962f69ad292SEric Dumazet }; 963cd7d8498SEric Dumazet }; 9644de075e0SEric Dumazet __u8 tcp_flags; /* TCP header flags. (tcp[13]) */ 965f4f9f6e7SNeal Cardwell 966713bafeaSYuchung Cheng __u8 sacked; /* State flags for SACK. */ 967f4f9f6e7SNeal Cardwell __u8 ip_dsfield; /* IPv4 tos or IPv6 dsfield */ 9686b084928SSoheil Hassas Yeganeh __u8 txstamp_ack:1, /* Record TX timestamp for ack? */ 969c134ecb8SMartin KaFai Lau eor:1, /* Is skb MSG_EOR marked? */ 97098aaa913SMike Maloney has_rxtstamp:1, /* SKB has a RX timestamp */ 97198aaa913SMike Maloney unused:5; 9721da177e4SLinus Torvalds __u32 ack_seq; /* Sequence number ACK'd */ 973971f10ecSEric Dumazet union { 974b75803d5SLawrence Brakmo struct { 97540bc6063SYuchung Cheng #define TCPCB_DELIVERED_CE_MASK ((1U<<20) - 1) 976b9f64820SYuchung Cheng /* There is space for up to 24 bytes */ 97740bc6063SYuchung Cheng __u32 is_app_limited:1, /* cwnd not fully used? */ 97840bc6063SYuchung Cheng delivered_ce:20, 97940bc6063SYuchung Cheng unused:11; 980b9f64820SYuchung Cheng /* pkts S/ACKed so far upon tx of skb, incl retrans: */ 981b9f64820SYuchung Cheng __u32 delivered; 982b9f64820SYuchung Cheng /* start of send pipeline phase */ 9839a568de4SEric Dumazet u64 first_tx_mstamp; 984b9f64820SYuchung Cheng /* when we reached the "delivered" count */ 9859a568de4SEric Dumazet u64 delivered_mstamp; 986b75803d5SLawrence Brakmo } tx; /* only used for outgoing skbs */ 987b75803d5SLawrence Brakmo union { 988971f10ecSEric Dumazet struct inet_skb_parm h4; 989971f10ecSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 990971f10ecSEric Dumazet struct inet6_skb_parm h6; 991971f10ecSEric Dumazet #endif 992b75803d5SLawrence Brakmo } header; /* For incoming skbs */ 993b75803d5SLawrence Brakmo }; 9941da177e4SLinus Torvalds }; 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds #define TCP_SKB_CB(__skb) ((struct tcp_skb_cb *)&((__skb)->cb[0])) 9971da177e4SLinus Torvalds 9989b9e2f25SEric Dumazet extern const struct inet_connection_sock_af_ops ipv4_specific; 9999b9e2f25SEric Dumazet 1000815afe17SEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1001870c3151SEric Dumazet /* This is the variant of inet6_iif() that must be used by TCP, 1002870c3151SEric Dumazet * as TCP moves IP6CB into a different location in skb->cb[] 1003870c3151SEric Dumazet */ 1004870c3151SEric Dumazet static inline int tcp_v6_iif(const struct sk_buff *skb) 1005870c3151SEric Dumazet { 100624b711edSDavid Ahern return TCP_SKB_CB(skb)->header.h6.iif; 100724b711edSDavid Ahern } 100824b711edSDavid Ahern 100924b711edSDavid Ahern static inline int tcp_v6_iif_l3_slave(const struct sk_buff *skb) 101024b711edSDavid Ahern { 1011a04a480dSDavid Ahern bool l3_slave = ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags); 101274b20582SDavid Ahern 101374b20582SDavid Ahern return l3_slave ? skb->skb_iif : TCP_SKB_CB(skb)->header.h6.iif; 1014870c3151SEric Dumazet } 10154297a0efSDavid Ahern 10164297a0efSDavid Ahern /* TCP_SKB_CB reference means this can not be used from early demux */ 10174297a0efSDavid Ahern static inline int tcp_v6_sdif(const struct sk_buff *skb) 10184297a0efSDavid Ahern { 10194297a0efSDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) 10204297a0efSDavid Ahern if (skb && ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags)) 10214297a0efSDavid Ahern return TCP_SKB_CB(skb)->header.h6.iif; 10224297a0efSDavid Ahern #endif 10234297a0efSDavid Ahern return 0; 10244297a0efSDavid Ahern } 1025dd2e0b86SEric Dumazet 1026b03d2142SEric Dumazet extern const struct inet_connection_sock_af_ops ipv6_specific; 1027b03d2142SEric Dumazet 1028dd2e0b86SEric Dumazet INDIRECT_CALLABLE_DECLARE(void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb)); 1029243600eeSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp_v6_rcv(struct sk_buff *skb)); 103011052589SKuniyuki Iwashima void tcp_v6_early_demux(struct sk_buff *skb); 1031dd2e0b86SEric Dumazet 1032815afe17SEric Dumazet #endif 1033870c3151SEric Dumazet 10343fa6f616SDavid Ahern /* TCP_SKB_CB reference means this can not be used from early demux */ 10353fa6f616SDavid Ahern static inline int tcp_v4_sdif(struct sk_buff *skb) 10363fa6f616SDavid Ahern { 10373fa6f616SDavid Ahern #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) 10383fa6f616SDavid Ahern if (skb && ipv4_l3mdev_skb(TCP_SKB_CB(skb)->header.h4.flags)) 10393fa6f616SDavid Ahern return TCP_SKB_CB(skb)->header.h4.iif; 10403fa6f616SDavid Ahern #endif 10413fa6f616SDavid Ahern return 0; 10423fa6f616SDavid Ahern } 10433fa6f616SDavid Ahern 10441da177e4SLinus Torvalds /* Due to TSO, an SKB can be composed of multiple actual 10451da177e4SLinus Torvalds * packets. To keep these tracked properly, we use this. 10461da177e4SLinus Torvalds */ 10471da177e4SLinus Torvalds static inline int tcp_skb_pcount(const struct sk_buff *skb) 10481da177e4SLinus Torvalds { 1049cd7d8498SEric Dumazet return TCP_SKB_CB(skb)->tcp_gso_segs; 1050cd7d8498SEric Dumazet } 1051cd7d8498SEric Dumazet 1052cd7d8498SEric Dumazet static inline void tcp_skb_pcount_set(struct sk_buff *skb, int segs) 1053cd7d8498SEric Dumazet { 1054cd7d8498SEric Dumazet TCP_SKB_CB(skb)->tcp_gso_segs = segs; 1055cd7d8498SEric Dumazet } 1056cd7d8498SEric Dumazet 1057cd7d8498SEric Dumazet static inline void tcp_skb_pcount_add(struct sk_buff *skb, int segs) 1058cd7d8498SEric Dumazet { 1059cd7d8498SEric Dumazet TCP_SKB_CB(skb)->tcp_gso_segs += segs; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 1062f69ad292SEric Dumazet /* This is valid iff skb is in write queue and tcp_skb_pcount() > 1. */ 10631da177e4SLinus Torvalds static inline int tcp_skb_mss(const struct sk_buff *skb) 10641da177e4SLinus Torvalds { 1065f69ad292SEric Dumazet return TCP_SKB_CB(skb)->tcp_gso_size; 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds 1068c134ecb8SMartin KaFai Lau static inline bool tcp_skb_can_collapse_to(const struct sk_buff *skb) 1069c134ecb8SMartin KaFai Lau { 1070c134ecb8SMartin KaFai Lau return likely(!TCP_SKB_CB(skb)->eor); 1071c134ecb8SMartin KaFai Lau } 1072c134ecb8SMartin KaFai Lau 107385712484SMat Martineau static inline bool tcp_skb_can_collapse(const struct sk_buff *to, 107485712484SMat Martineau const struct sk_buff *from) 107585712484SMat Martineau { 107685712484SMat Martineau return likely(tcp_skb_can_collapse_to(to) && 10779b65b17dSTalal Ahmad mptcp_skb_can_collapse(to, from) && 10789b65b17dSTalal Ahmad skb_pure_zcopy_same(to, from)); 107985712484SMat Martineau } 108085712484SMat Martineau 1081317a76f9SStephen Hemminger /* Events passed to congestion control interface */ 1082317a76f9SStephen Hemminger enum tcp_ca_event { 1083317a76f9SStephen Hemminger CA_EVENT_TX_START, /* first transmit when no packets in flight */ 1084317a76f9SStephen Hemminger CA_EVENT_CWND_RESTART, /* congestion window restart */ 1085317a76f9SStephen Hemminger CA_EVENT_COMPLETE_CWR, /* end of congestion recovery */ 1086317a76f9SStephen Hemminger CA_EVENT_LOSS, /* loss timeout */ 10879890092eSFlorian Westphal CA_EVENT_ECN_NO_CE, /* ECT set, but not CE marked */ 10889890092eSFlorian Westphal CA_EVENT_ECN_IS_CE, /* received CE marked IP packet */ 10897354c8c3SFlorian Westphal }; 10907354c8c3SFlorian Westphal 10919890092eSFlorian Westphal /* Information about inbound ACK, passed to cong_ops->in_ack_event() */ 10927354c8c3SFlorian Westphal enum tcp_ca_ack_event_flags { 1093c1d2b4c3SFlorian Westphal CA_ACK_SLOWPATH = (1 << 0), /* In slow path processing */ 1094c1d2b4c3SFlorian Westphal CA_ACK_WIN_UPDATE = (1 << 1), /* ACK updated window */ 1095c1d2b4c3SFlorian Westphal CA_ACK_ECE = (1 << 2), /* ECE bit is set on ack */ 1096317a76f9SStephen Hemminger }; 1097317a76f9SStephen Hemminger 1098317a76f9SStephen Hemminger /* 1099317a76f9SStephen Hemminger * Interface for adding new TCP congestion control handlers 1100317a76f9SStephen Hemminger */ 1101317a76f9SStephen Hemminger #define TCP_CA_NAME_MAX 16 11023ff825b2SStephen Hemminger #define TCP_CA_MAX 128 11033ff825b2SStephen Hemminger #define TCP_CA_BUF_MAX (TCP_CA_NAME_MAX*TCP_CA_MAX) 11043ff825b2SStephen Hemminger 1105c5c6a8abSDaniel Borkmann #define TCP_CA_UNSPEC 0 1106c5c6a8abSDaniel Borkmann 110730e502a3SDaniel Borkmann /* Algorithm can be set on socket without CAP_NET_ADMIN privileges */ 1108164891aaSStephen Hemminger #define TCP_CONG_NON_RESTRICTED 0x1 110930e502a3SDaniel Borkmann /* Requires ECN/ECT set on all packets */ 111030e502a3SDaniel Borkmann #define TCP_CONG_NEEDS_ECN 0x2 11110baf26b0SMartin KaFai Lau #define TCP_CONG_MASK (TCP_CONG_NON_RESTRICTED | TCP_CONG_NEEDS_ECN) 1112164891aaSStephen Hemminger 111364f40ff5SEric Dumazet union tcp_cc_info; 111464f40ff5SEric Dumazet 1115756ee172SLawrence Brakmo struct ack_sample { 1116756ee172SLawrence Brakmo u32 pkts_acked; 1117756ee172SLawrence Brakmo s32 rtt_us; 11186f094b9eSLawrence Brakmo u32 in_flight; 1119756ee172SLawrence Brakmo }; 1120756ee172SLawrence Brakmo 1121b9f64820SYuchung Cheng /* A rate sample measures the number of (original/retransmitted) data 1122b9f64820SYuchung Cheng * packets delivered "delivered" over an interval of time "interval_us". 1123b9f64820SYuchung Cheng * The tcp_rate.c code fills in the rate sample, and congestion 1124b9f64820SYuchung Cheng * control modules that define a cong_control function to run at the end 1125b9f64820SYuchung Cheng * of ACK processing can optionally chose to consult this sample when 1126b9f64820SYuchung Cheng * setting cwnd and pacing rate. 1127b9f64820SYuchung Cheng * A sample is invalid if "delivered" or "interval_us" is negative. 1128b9f64820SYuchung Cheng */ 1129b9f64820SYuchung Cheng struct rate_sample { 11309a568de4SEric Dumazet u64 prior_mstamp; /* starting timestamp for interval */ 1131b9f64820SYuchung Cheng u32 prior_delivered; /* tp->delivered at "prior_mstamp" */ 113240bc6063SYuchung Cheng u32 prior_delivered_ce;/* tp->delivered_ce at "prior_mstamp" */ 1133b9f64820SYuchung Cheng s32 delivered; /* number of packets delivered over interval */ 113440bc6063SYuchung Cheng s32 delivered_ce; /* number of packets delivered w/ CE marks*/ 1135b9f64820SYuchung Cheng long interval_us; /* time for tp->delivered to incr "delivered" */ 11364929c942SDeepti Raghavan u32 snd_interval_us; /* snd interval for delivered packets */ 11374929c942SDeepti Raghavan u32 rcv_interval_us; /* rcv interval for delivered packets */ 1138b9f64820SYuchung Cheng long rtt_us; /* RTT of last (S)ACKed packet (or -1) */ 1139b9f64820SYuchung Cheng int losses; /* number of packets marked lost upon ACK */ 1140b9f64820SYuchung Cheng u32 acked_sacked; /* number of packets newly (S)ACKed upon ACK */ 1141b9f64820SYuchung Cheng u32 prior_in_flight; /* in flight before this ACK */ 1142b253a068SPengcheng Yang u32 last_end_seq; /* end_seq of most recently ACKed packet */ 1143d7722e85SSoheil Hassas Yeganeh bool is_app_limited; /* is sample from packet with bubble in pipe? */ 1144b9f64820SYuchung Cheng bool is_retrans; /* is sample from retransmission? */ 1145e4286603SYuchung Cheng bool is_ack_delayed; /* is this (likely) a delayed ACK? */ 1146b9f64820SYuchung Cheng }; 1147b9f64820SYuchung Cheng 1148317a76f9SStephen Hemminger struct tcp_congestion_ops { 114982506665SEric Dumazet /* fast path fields are put first to fill one cache line */ 115082506665SEric Dumazet 115182506665SEric Dumazet /* return slow start threshold (required) */ 115282506665SEric Dumazet u32 (*ssthresh)(struct sock *sk); 115382506665SEric Dumazet 115482506665SEric Dumazet /* do new cwnd calculation (required) */ 115582506665SEric Dumazet void (*cong_avoid)(struct sock *sk, u32 ack, u32 acked); 115682506665SEric Dumazet 115782506665SEric Dumazet /* call before changing ca_state (optional) */ 115882506665SEric Dumazet void (*set_state)(struct sock *sk, u8 new_state); 115982506665SEric Dumazet 116082506665SEric Dumazet /* call when cwnd event occurs (optional) */ 116182506665SEric Dumazet void (*cwnd_event)(struct sock *sk, enum tcp_ca_event ev); 116282506665SEric Dumazet 116382506665SEric Dumazet /* call when ack arrives (optional) */ 116482506665SEric Dumazet void (*in_ack_event)(struct sock *sk, u32 flags); 116582506665SEric Dumazet 116682506665SEric Dumazet /* hook for packet ack accounting (optional) */ 116782506665SEric Dumazet void (*pkts_acked)(struct sock *sk, const struct ack_sample *sample); 116882506665SEric Dumazet 116982506665SEric Dumazet /* override sysctl_tcp_min_tso_segs */ 117082506665SEric Dumazet u32 (*min_tso_segs)(struct sock *sk); 117182506665SEric Dumazet 117282506665SEric Dumazet /* call when packets are delivered to update cwnd and pacing rate, 117382506665SEric Dumazet * after all the ca_state processing. (optional) 117482506665SEric Dumazet */ 117582506665SEric Dumazet void (*cong_control)(struct sock *sk, const struct rate_sample *rs); 117682506665SEric Dumazet 117782506665SEric Dumazet 117882506665SEric Dumazet /* new value of cwnd after loss (required) */ 117982506665SEric Dumazet u32 (*undo_cwnd)(struct sock *sk); 118082506665SEric Dumazet /* returns the multiplier used in tcp_sndbuf_expand (optional) */ 118182506665SEric Dumazet u32 (*sndbuf_expand)(struct sock *sk); 118282506665SEric Dumazet 118382506665SEric Dumazet /* control/slow paths put last */ 118482506665SEric Dumazet /* get info for inet_diag (optional) */ 118582506665SEric Dumazet size_t (*get_info)(struct sock *sk, u32 ext, int *attr, 118682506665SEric Dumazet union tcp_cc_info *info); 118782506665SEric Dumazet 118882506665SEric Dumazet char name[TCP_CA_NAME_MAX]; 118982506665SEric Dumazet struct module *owner; 1190317a76f9SStephen Hemminger struct list_head list; 1191c5c6a8abSDaniel Borkmann u32 key; 1192c5c6a8abSDaniel Borkmann u32 flags; 1193317a76f9SStephen Hemminger 1194317a76f9SStephen Hemminger /* initialize private data (optional) */ 11956687e988SArnaldo Carvalho de Melo void (*init)(struct sock *sk); 1196317a76f9SStephen Hemminger /* cleanup private data (optional) */ 11976687e988SArnaldo Carvalho de Melo void (*release)(struct sock *sk); 119882506665SEric Dumazet } ____cacheline_aligned_in_smp; 1199317a76f9SStephen Hemminger 12005c9f3023SJoe Perches int tcp_register_congestion_control(struct tcp_congestion_ops *type); 12015c9f3023SJoe Perches void tcp_unregister_congestion_control(struct tcp_congestion_ops *type); 12028fb1a76aSKui-Feng Lee int tcp_update_congestion_control(struct tcp_congestion_ops *type, 12038fb1a76aSKui-Feng Lee struct tcp_congestion_ops *old_type); 12048fb1a76aSKui-Feng Lee int tcp_validate_congestion_control(struct tcp_congestion_ops *ca); 1205317a76f9SStephen Hemminger 120655d8694fSFlorian Westphal void tcp_assign_congestion_control(struct sock *sk); 12075c9f3023SJoe Perches void tcp_init_congestion_control(struct sock *sk); 12085c9f3023SJoe Perches void tcp_cleanup_congestion_control(struct sock *sk); 12096670e152SStephen Hemminger int tcp_set_default_congestion_control(struct net *net, const char *name); 12106670e152SStephen Hemminger void tcp_get_default_congestion_control(struct net *net, char *name); 12115c9f3023SJoe Perches void tcp_get_available_congestion_control(char *buf, size_t len); 12125c9f3023SJoe Perches void tcp_get_allowed_congestion_control(char *buf, size_t len); 12135c9f3023SJoe Perches int tcp_set_allowed_congestion_control(char *allowed); 12148d650cdeSEric Dumazet int tcp_set_congestion_control(struct sock *sk, const char *name, bool load, 121529a94932SNeal Cardwell bool cap_net_admin); 1216e73ebb08SNeal Cardwell u32 tcp_slow_start(struct tcp_sock *tp, u32 acked); 1217e73ebb08SNeal Cardwell void tcp_cong_avoid_ai(struct tcp_sock *tp, u32 w, u32 acked); 1218317a76f9SStephen Hemminger 12195c9f3023SJoe Perches u32 tcp_reno_ssthresh(struct sock *sk); 1220e9799183SFlorian Westphal u32 tcp_reno_undo_cwnd(struct sock *sk); 122124901551SEric Dumazet void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked); 1222a8acfbacSDavid S. Miller extern struct tcp_congestion_ops tcp_reno; 1223317a76f9SStephen Hemminger 12240baf26b0SMartin KaFai Lau struct tcp_congestion_ops *tcp_ca_find(const char *name); 1225c5c6a8abSDaniel Borkmann struct tcp_congestion_ops *tcp_ca_find_key(u32 key); 12266670e152SStephen Hemminger u32 tcp_ca_get_key_by_name(struct net *net, const char *name, bool *ecn_ca); 1227ea697639SDaniel Borkmann #ifdef CONFIG_INET 1228c5c6a8abSDaniel Borkmann char *tcp_ca_get_name_by_key(u32 key, char *buffer); 1229ea697639SDaniel Borkmann #else 1230ea697639SDaniel Borkmann static inline char *tcp_ca_get_name_by_key(u32 key, char *buffer) 1231ea697639SDaniel Borkmann { 1232ea697639SDaniel Borkmann return NULL; 1233ea697639SDaniel Borkmann } 1234ea697639SDaniel Borkmann #endif 1235c5c6a8abSDaniel Borkmann 123630e502a3SDaniel Borkmann static inline bool tcp_ca_needs_ecn(const struct sock *sk) 123730e502a3SDaniel Borkmann { 123830e502a3SDaniel Borkmann const struct inet_connection_sock *icsk = inet_csk(sk); 123930e502a3SDaniel Borkmann 124030e502a3SDaniel Borkmann return icsk->icsk_ca_ops->flags & TCP_CONG_NEEDS_ECN; 124130e502a3SDaniel Borkmann } 124230e502a3SDaniel Borkmann 12436687e988SArnaldo Carvalho de Melo static inline void tcp_ca_event(struct sock *sk, const enum tcp_ca_event event) 1244317a76f9SStephen Hemminger { 12456687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 12466687e988SArnaldo Carvalho de Melo 12476687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_ops->cwnd_event) 12486687e988SArnaldo Carvalho de Melo icsk->icsk_ca_ops->cwnd_event(sk, event); 1249317a76f9SStephen Hemminger } 1250317a76f9SStephen Hemminger 125115fcdf6aSPing Gan /* From tcp_cong.c */ 125215fcdf6aSPing Gan void tcp_set_ca_state(struct sock *sk, const u8 ca_state); 125315fcdf6aSPing Gan 1254b9f64820SYuchung Cheng /* From tcp_rate.c */ 1255b9f64820SYuchung Cheng void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb); 1256b9f64820SYuchung Cheng void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb, 1257b9f64820SYuchung Cheng struct rate_sample *rs); 1258b9f64820SYuchung Cheng void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost, 1259d4761754SYousuk Seung bool is_sack_reneg, struct rate_sample *rs); 1260d7722e85SSoheil Hassas Yeganeh void tcp_rate_check_app_limited(struct sock *sk); 1261b9f64820SYuchung Cheng 1262b253a068SPengcheng Yang static inline bool tcp_skb_sent_after(u64 t1, u64 t2, u32 seq1, u32 seq2) 1263b253a068SPengcheng Yang { 1264b253a068SPengcheng Yang return t1 > t2 || (t1 == t2 && after(seq1, seq2)); 1265b253a068SPengcheng Yang } 1266b253a068SPengcheng Yang 1267e60402d0SIlpo Järvinen /* These functions determine how the current flow behaves in respect of SACK 1268e60402d0SIlpo Järvinen * handling. SACK is negotiated with the peer, and therefore it can vary 1269e60402d0SIlpo Järvinen * between different flows. 1270e60402d0SIlpo Järvinen * 1271e60402d0SIlpo Järvinen * tcp_is_sack - SACK enabled 1272e60402d0SIlpo Järvinen * tcp_is_reno - No SACK 1273e60402d0SIlpo Järvinen */ 1274e60402d0SIlpo Järvinen static inline int tcp_is_sack(const struct tcp_sock *tp) 1275e60402d0SIlpo Järvinen { 1276ebeef4bcSEric Dumazet return likely(tp->rx_opt.sack_ok); 1277e60402d0SIlpo Järvinen } 1278e60402d0SIlpo Järvinen 1279a2a385d6SEric Dumazet static inline bool tcp_is_reno(const struct tcp_sock *tp) 1280e60402d0SIlpo Järvinen { 1281e60402d0SIlpo Järvinen return !tcp_is_sack(tp); 1282e60402d0SIlpo Järvinen } 1283e60402d0SIlpo Järvinen 128483ae4088SIlpo Järvinen static inline unsigned int tcp_left_out(const struct tcp_sock *tp) 128583ae4088SIlpo Järvinen { 128683ae4088SIlpo Järvinen return tp->sacked_out + tp->lost_out; 128783ae4088SIlpo Järvinen } 128883ae4088SIlpo Järvinen 12891da177e4SLinus Torvalds /* This determines how many packets are "in the network" to the best 12901da177e4SLinus Torvalds * of our knowledge. In many cases it is conservative, but where 12911da177e4SLinus Torvalds * detailed information is available from the receiver (via SACK 12921da177e4SLinus Torvalds * blocks etc.) we can make more aggressive calculations. 12931da177e4SLinus Torvalds * 12941da177e4SLinus Torvalds * Use this for decisions involving congestion control, use just 12951da177e4SLinus Torvalds * tp->packets_out to determine if the send queue is empty or not. 12961da177e4SLinus Torvalds * 12971da177e4SLinus Torvalds * Read this equation as: 12981da177e4SLinus Torvalds * 12991da177e4SLinus Torvalds * "Packets sent once on transmission queue" MINUS 13001da177e4SLinus Torvalds * "Packets left network, but not honestly ACKed yet" PLUS 13011da177e4SLinus Torvalds * "Packets fast retransmitted" 13021da177e4SLinus Torvalds */ 130340efc6faSStephen Hemminger static inline unsigned int tcp_packets_in_flight(const struct tcp_sock *tp) 13041da177e4SLinus Torvalds { 130583ae4088SIlpo Järvinen return tp->packets_out - tcp_left_out(tp) + tp->retrans_out; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 13080b6a05c1SIlpo Järvinen #define TCP_INFINITE_SSTHRESH 0x7fffffff 13090b6a05c1SIlpo Järvinen 131040570375SEric Dumazet static inline u32 tcp_snd_cwnd(const struct tcp_sock *tp) 131140570375SEric Dumazet { 131240570375SEric Dumazet return tp->snd_cwnd; 131340570375SEric Dumazet } 131440570375SEric Dumazet 131540570375SEric Dumazet static inline void tcp_snd_cwnd_set(struct tcp_sock *tp, u32 val) 131640570375SEric Dumazet { 131740570375SEric Dumazet WARN_ON_ONCE((int)val <= 0); 131840570375SEric Dumazet tp->snd_cwnd = val; 131940570375SEric Dumazet } 132040570375SEric Dumazet 1321071d5080SYuchung Cheng static inline bool tcp_in_slow_start(const struct tcp_sock *tp) 1322071d5080SYuchung Cheng { 132340570375SEric Dumazet return tcp_snd_cwnd(tp) < tp->snd_ssthresh; 1324071d5080SYuchung Cheng } 1325071d5080SYuchung Cheng 13260b6a05c1SIlpo Järvinen static inline bool tcp_in_initial_slowstart(const struct tcp_sock *tp) 13270b6a05c1SIlpo Järvinen { 13280b6a05c1SIlpo Järvinen return tp->snd_ssthresh >= TCP_INFINITE_SSTHRESH; 13290b6a05c1SIlpo Järvinen } 13300b6a05c1SIlpo Järvinen 1331684bad11SYuchung Cheng static inline bool tcp_in_cwnd_reduction(const struct sock *sk) 1332684bad11SYuchung Cheng { 1333684bad11SYuchung Cheng return (TCPF_CA_CWR | TCPF_CA_Recovery) & 1334684bad11SYuchung Cheng (1 << inet_csk(sk)->icsk_ca_state); 1335684bad11SYuchung Cheng } 1336684bad11SYuchung Cheng 13371da177e4SLinus Torvalds /* If cwnd > ssthresh, we may raise ssthresh to be half-way to cwnd. 1338684bad11SYuchung Cheng * The exception is cwnd reduction phase, when cwnd is decreasing towards 13391da177e4SLinus Torvalds * ssthresh. 13401da177e4SLinus Torvalds */ 13416687e988SArnaldo Carvalho de Melo static inline __u32 tcp_current_ssthresh(const struct sock *sk) 13421da177e4SLinus Torvalds { 13436687e988SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 1344cf533ea5SEric Dumazet 1345684bad11SYuchung Cheng if (tcp_in_cwnd_reduction(sk)) 13461da177e4SLinus Torvalds return tp->snd_ssthresh; 13471da177e4SLinus Torvalds else 13481da177e4SLinus Torvalds return max(tp->snd_ssthresh, 134940570375SEric Dumazet ((tcp_snd_cwnd(tp) >> 1) + 135040570375SEric Dumazet (tcp_snd_cwnd(tp) >> 2))); 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 1353b9c4595bSIlpo Järvinen /* Use define here intentionally to get WARN_ON location shown at the caller */ 1354b9c4595bSIlpo Järvinen #define tcp_verify_left_out(tp) WARN_ON(tcp_left_out(tp) > tp->packets_out) 13551da177e4SLinus Torvalds 13565ee2c941SChristoph Paasch void tcp_enter_cwr(struct sock *sk); 13575c9f3023SJoe Perches __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst); 13581da177e4SLinus Torvalds 13596b5a5c0dSNeal Cardwell /* The maximum number of MSS of available cwnd for which TSO defers 13606b5a5c0dSNeal Cardwell * sending if not using sysctl_tcp_tso_win_divisor. 13616b5a5c0dSNeal Cardwell */ 13626b5a5c0dSNeal Cardwell static inline __u32 tcp_max_tso_deferred_mss(const struct tcp_sock *tp) 13636b5a5c0dSNeal Cardwell { 13646b5a5c0dSNeal Cardwell return 3; 13656b5a5c0dSNeal Cardwell } 13666b5a5c0dSNeal Cardwell 136790840defSIlpo Järvinen /* Returns end sequence number of the receiver's advertised window */ 136890840defSIlpo Järvinen static inline u32 tcp_wnd_end(const struct tcp_sock *tp) 136990840defSIlpo Järvinen { 137090840defSIlpo Järvinen return tp->snd_una + tp->snd_wnd; 137190840defSIlpo Järvinen } 1372e114a710SEric Dumazet 1373e114a710SEric Dumazet /* We follow the spirit of RFC2861 to validate cwnd but implement a more 1374e114a710SEric Dumazet * flexible approach. The RFC suggests cwnd should not be raised unless 1375ca8a2263SNeal Cardwell * it was fully used previously. And that's exactly what we do in 1376ca8a2263SNeal Cardwell * congestion avoidance mode. But in slow start we allow cwnd to grow 1377ca8a2263SNeal Cardwell * as long as the application has used half the cwnd. 1378e114a710SEric Dumazet * Example : 1379e114a710SEric Dumazet * cwnd is 10 (IW10), but application sends 9 frames. 1380e114a710SEric Dumazet * We allow cwnd to reach 18 when all frames are ACKed. 1381e114a710SEric Dumazet * This check is safe because it's as aggressive as slow start which already 1382e114a710SEric Dumazet * risks 100% overshoot. The advantage is that we discourage application to 1383e114a710SEric Dumazet * either send more filler packets or data to artificially blow up the cwnd 1384e114a710SEric Dumazet * usage, and allow application-limited process to probe bw more aggressively. 1385e114a710SEric Dumazet */ 138624901551SEric Dumazet static inline bool tcp_is_cwnd_limited(const struct sock *sk) 1387e114a710SEric Dumazet { 1388e114a710SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 1389e114a710SEric Dumazet 1390f4ce91ceSNeal Cardwell if (tp->is_cwnd_limited) 1391f4ce91ceSNeal Cardwell return true; 1392f4ce91ceSNeal Cardwell 1393ca8a2263SNeal Cardwell /* If in slow start, ensure cwnd grows to twice what was ACKed. */ 1394071d5080SYuchung Cheng if (tcp_in_slow_start(tp)) 139540570375SEric Dumazet return tcp_snd_cwnd(tp) < 2 * tp->max_packets_out; 1396ca8a2263SNeal Cardwell 1397f4ce91ceSNeal Cardwell return false; 1398e114a710SEric Dumazet } 1399f4805edeSStephen Hemminger 1400cadefe5fSEric Dumazet /* BBR congestion control needs pacing. 1401cadefe5fSEric Dumazet * Same remark for SO_MAX_PACING_RATE. 1402cadefe5fSEric Dumazet * sch_fq packet scheduler is efficiently handling pacing, 1403cadefe5fSEric Dumazet * but is not always installed/used. 1404cadefe5fSEric Dumazet * Return true if TCP stack should pace packets itself. 1405cadefe5fSEric Dumazet */ 1406cadefe5fSEric Dumazet static inline bool tcp_needs_internal_pacing(const struct sock *sk) 1407cadefe5fSEric Dumazet { 1408cadefe5fSEric Dumazet return smp_load_acquire(&sk->sk_pacing_status) == SK_PACING_NEEDED; 1409cadefe5fSEric Dumazet } 1410cadefe5fSEric Dumazet 14118dc242adSEric Dumazet /* Estimates in how many jiffies next packet for this flow can be sent. 14128dc242adSEric Dumazet * Scheduling a retransmit timer too early would be silly. 14133f80e08fSEric Dumazet */ 14148dc242adSEric Dumazet static inline unsigned long tcp_pacing_delay(const struct sock *sk) 14153f80e08fSEric Dumazet { 14168dc242adSEric Dumazet s64 delay = tcp_sk(sk)->tcp_wstamp_ns - tcp_sk(sk)->tcp_clock_cache; 14173f80e08fSEric Dumazet 14188dc242adSEric Dumazet return delay > 0 ? nsecs_to_jiffies(delay) : 0; 14193f80e08fSEric Dumazet } 14203f80e08fSEric Dumazet 14213f80e08fSEric Dumazet static inline void tcp_reset_xmit_timer(struct sock *sk, 14223f80e08fSEric Dumazet const int what, 14233f80e08fSEric Dumazet unsigned long when, 14248dc242adSEric Dumazet const unsigned long max_when) 14253f80e08fSEric Dumazet { 14268dc242adSEric Dumazet inet_csk_reset_xmit_timer(sk, what, when + tcp_pacing_delay(sk), 14273f80e08fSEric Dumazet max_when); 14283f80e08fSEric Dumazet } 14293f80e08fSEric Dumazet 143021c8fe99SEric Dumazet /* Something is really bad, we could not queue an additional packet, 14313f80e08fSEric Dumazet * because qdisc is full or receiver sent a 0 window, or we are paced. 143221c8fe99SEric Dumazet * We do not want to add fuel to the fire, or abort too early, 143321c8fe99SEric Dumazet * so make sure the timer we arm now is at least 200ms in the future, 143421c8fe99SEric Dumazet * regardless of current icsk_rto value (as it could be ~2ms) 143521c8fe99SEric Dumazet */ 143621c8fe99SEric Dumazet static inline unsigned long tcp_probe0_base(const struct sock *sk) 143721c8fe99SEric Dumazet { 143821c8fe99SEric Dumazet return max_t(unsigned long, inet_csk(sk)->icsk_rto, TCP_RTO_MIN); 143921c8fe99SEric Dumazet } 144021c8fe99SEric Dumazet 144121c8fe99SEric Dumazet /* Variant of inet_csk_rto_backoff() used for zero window probes */ 144221c8fe99SEric Dumazet static inline unsigned long tcp_probe0_when(const struct sock *sk, 144321c8fe99SEric Dumazet unsigned long max_when) 144421c8fe99SEric Dumazet { 14456d4634d1SCambda Zhu u8 backoff = min_t(u8, ilog2(TCP_RTO_MAX / TCP_RTO_MIN) + 1, 14466d4634d1SCambda Zhu inet_csk(sk)->icsk_backoff); 14476d4634d1SCambda Zhu u64 when = (u64)tcp_probe0_base(sk) << backoff; 144821c8fe99SEric Dumazet 144921c8fe99SEric Dumazet return (unsigned long)min_t(u64, when, max_when); 145021c8fe99SEric Dumazet } 145121c8fe99SEric Dumazet 14529e412ba7SIlpo Järvinen static inline void tcp_check_probe_timer(struct sock *sk) 14531da177e4SLinus Torvalds { 145421c8fe99SEric Dumazet if (!tcp_sk(sk)->packets_out && !inet_csk(sk)->icsk_pending) 14553f80e08fSEric Dumazet tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0, 14568dc242adSEric Dumazet tcp_probe0_base(sk), TCP_RTO_MAX); 14571da177e4SLinus Torvalds } 14581da177e4SLinus Torvalds 1459ee7537b6SHantzis Fotis static inline void tcp_init_wl(struct tcp_sock *tp, u32 seq) 14601da177e4SLinus Torvalds { 14611da177e4SLinus Torvalds tp->snd_wl1 = seq; 14621da177e4SLinus Torvalds } 14631da177e4SLinus Torvalds 1464ee7537b6SHantzis Fotis static inline void tcp_update_wl(struct tcp_sock *tp, u32 seq) 14651da177e4SLinus Torvalds { 14661da177e4SLinus Torvalds tp->snd_wl1 = seq; 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds /* 14701da177e4SLinus Torvalds * Calculate(/check) TCP checksum 14711da177e4SLinus Torvalds */ 1472ba7808eaSFrederik Deweerdt static inline __sum16 tcp_v4_check(int len, __be32 saddr, 1473ba7808eaSFrederik Deweerdt __be32 daddr, __wsum base) 14741da177e4SLinus Torvalds { 14751da177e4SLinus Torvalds return csum_tcpudp_magic(saddr, daddr, len, IPPROTO_TCP, base); 14761da177e4SLinus Torvalds } 14771da177e4SLinus Torvalds 1478a2a385d6SEric Dumazet static inline bool tcp_checksum_complete(struct sk_buff *skb) 14791da177e4SLinus Torvalds { 148060476372SHerbert Xu return !skb_csum_unnecessary(skb) && 14816ab6dfa6SCong Wang __skb_checksum_complete(skb); 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds 14847a26dc9eSMenglong Dong bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb, 14857a26dc9eSMenglong Dong enum skb_drop_reason *reason); 1486f35f8219SEric Dumazet 1487f35f8219SEric Dumazet 1488ac6e7800SEric Dumazet int tcp_filter(struct sock *sk, struct sk_buff *skb); 14895c9f3023SJoe Perches void tcp_set_state(struct sock *sk, int state); 14905c9f3023SJoe Perches void tcp_done(struct sock *sk); 1491c1e64e29SLorenzo Colitti int tcp_abort(struct sock *sk, int err); 1492c1e64e29SLorenzo Colitti 149340efc6faSStephen Hemminger static inline void tcp_sack_reset(struct tcp_options_received *rx_opt) 14941da177e4SLinus Torvalds { 14951da177e4SLinus Torvalds rx_opt->dsack = 0; 14961da177e4SLinus Torvalds rx_opt->num_sacks = 0; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 14996f021c62SEric Dumazet void tcp_cwnd_restart(struct sock *sk, s32 delta); 15006f021c62SEric Dumazet 15016f021c62SEric Dumazet static inline void tcp_slow_start_after_idle_check(struct sock *sk) 15026f021c62SEric Dumazet { 15031b1fc3fdSWei Wang const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; 15046f021c62SEric Dumazet struct tcp_sock *tp = tcp_sk(sk); 15056f021c62SEric Dumazet s32 delta; 15066f021c62SEric Dumazet 15074845b571SKuniyuki Iwashima if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle) || 15084845b571SKuniyuki Iwashima tp->packets_out || ca_ops->cong_control) 15096f021c62SEric Dumazet return; 1510d635fbe2SEric Dumazet delta = tcp_jiffies32 - tp->lsndtime; 15116f021c62SEric Dumazet if (delta > inet_csk(sk)->icsk_rto) 15126f021c62SEric Dumazet tcp_cwnd_restart(sk, delta); 15136f021c62SEric Dumazet } 151485f16525SYuchung Cheng 15151da177e4SLinus Torvalds /* Determine a window scaling and initial window to offer. */ 1516ceef9ab6SEric Dumazet void tcp_select_initial_window(const struct sock *sk, int __space, 1517ceef9ab6SEric Dumazet __u32 mss, __u32 *rcv_wnd, 15185c9f3023SJoe Perches __u32 *window_clamp, int wscale_ok, 15195c9f3023SJoe Perches __u8 *rcv_wscale, __u32 init_rcv_wnd); 15201da177e4SLinus Torvalds 1521b8dc6d6cSPaolo Abeni static inline int __tcp_win_from_space(u8 scaling_ratio, int space) 15221da177e4SLinus Torvalds { 1523b8dc6d6cSPaolo Abeni s64 scaled_space = (s64)space * scaling_ratio; 1524c4836742SGao Feng 1525dfa2f048SEric Dumazet return scaled_space >> TCP_RMEM_TO_WIN_SCALE; 1526dfa2f048SEric Dumazet } 1527dfa2f048SEric Dumazet 1528b8dc6d6cSPaolo Abeni static inline int tcp_win_from_space(const struct sock *sk, int space) 1529b8dc6d6cSPaolo Abeni { 1530b8dc6d6cSPaolo Abeni return __tcp_win_from_space(tcp_sk(sk)->scaling_ratio, space); 1531b8dc6d6cSPaolo Abeni } 1532b8dc6d6cSPaolo Abeni 1533b8dc6d6cSPaolo Abeni /* inverse of __tcp_win_from_space() */ 1534b8dc6d6cSPaolo Abeni static inline int __tcp_space_from_win(u8 scaling_ratio, int win) 1535dfa2f048SEric Dumazet { 1536dfa2f048SEric Dumazet u64 val = (u64)win << TCP_RMEM_TO_WIN_SCALE; 1537dfa2f048SEric Dumazet 1538b8dc6d6cSPaolo Abeni do_div(val, scaling_ratio); 1539dfa2f048SEric Dumazet return val; 1540dfa2f048SEric Dumazet } 1541dfa2f048SEric Dumazet 1542b8dc6d6cSPaolo Abeni static inline int tcp_space_from_win(const struct sock *sk, int win) 1543b8dc6d6cSPaolo Abeni { 1544b8dc6d6cSPaolo Abeni return __tcp_space_from_win(tcp_sk(sk)->scaling_ratio, win); 1545b8dc6d6cSPaolo Abeni } 1546b8dc6d6cSPaolo Abeni 1547697a6c8cSHechao Li /* Assume a 50% default for skb->len/skb->truesize ratio. 1548dfa2f048SEric Dumazet * This may be adjusted later in tcp_measure_rcv_mss(). 1549dfa2f048SEric Dumazet */ 1550697a6c8cSHechao Li #define TCP_DEFAULT_SCALING_RATIO (1 << (TCP_RMEM_TO_WIN_SCALE - 1)) 1551849ee75aSPaolo Abeni 1552849ee75aSPaolo Abeni static inline void tcp_scaling_ratio_init(struct sock *sk) 1553849ee75aSPaolo Abeni { 1554849ee75aSPaolo Abeni tcp_sk(sk)->scaling_ratio = TCP_DEFAULT_SCALING_RATIO; 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds /* Note: caller must be prepared to deal with negative returns */ 15581da177e4SLinus Torvalds static inline int tcp_space(const struct sock *sk) 15591da177e4SLinus Torvalds { 1560ebb3b78dSEric Dumazet return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf) - 156170c26558SEric Dumazet READ_ONCE(sk->sk_backlog.len) - 15621da177e4SLinus Torvalds atomic_read(&sk->sk_rmem_alloc)); 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds static inline int tcp_full_space(const struct sock *sk) 15661da177e4SLinus Torvalds { 1567ebb3b78dSEric Dumazet return tcp_win_from_space(sk, READ_ONCE(sk->sk_rcvbuf)); 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 157058d3aadeSPaolo Abeni static inline void __tcp_adjust_rcv_ssthresh(struct sock *sk, u32 new_ssthresh) 1571053f3684SWei Wang { 1572053f3684SWei Wang int unused_mem = sk_unused_reserved_mem(sk); 1573053f3684SWei Wang struct tcp_sock *tp = tcp_sk(sk); 1574053f3684SWei Wang 157558d3aadeSPaolo Abeni tp->rcv_ssthresh = min(tp->rcv_ssthresh, new_ssthresh); 1576053f3684SWei Wang if (unused_mem) 1577053f3684SWei Wang tp->rcv_ssthresh = max_t(u32, tp->rcv_ssthresh, 1578053f3684SWei Wang tcp_win_from_space(sk, unused_mem)); 1579053f3684SWei Wang } 1580053f3684SWei Wang 158158d3aadeSPaolo Abeni static inline void tcp_adjust_rcv_ssthresh(struct sock *sk) 158258d3aadeSPaolo Abeni { 158358d3aadeSPaolo Abeni __tcp_adjust_rcv_ssthresh(sk, 4U * tcp_sk(sk)->advmss); 158458d3aadeSPaolo Abeni } 158558d3aadeSPaolo Abeni 1586c76c6956SPaolo Abeni void tcp_cleanup_rbuf(struct sock *sk, int copied); 1587e5c6de5fSJohn Fastabend void __tcp_cleanup_rbuf(struct sock *sk, int copied); 1588e5c6de5fSJohn Fastabend 1589c76c6956SPaolo Abeni 159024adbc16SEric Dumazet /* We provision sk_rcvbuf around 200% of sk_rcvlowat. 159124adbc16SEric Dumazet * If 87.5 % (7/8) of the space has been consumed, we want to override 159224adbc16SEric Dumazet * SO_RCVLOWAT constraint, since we are receiving skbs with too small 159324adbc16SEric Dumazet * len/truesize ratio. 159424adbc16SEric Dumazet */ 159524adbc16SEric Dumazet static inline bool tcp_rmem_pressure(const struct sock *sk) 159624adbc16SEric Dumazet { 1597f969dc5aSEric Dumazet int rcvbuf, threshold; 1598f969dc5aSEric Dumazet 1599f969dc5aSEric Dumazet if (tcp_under_memory_pressure(sk)) 1600f969dc5aSEric Dumazet return true; 1601f969dc5aSEric Dumazet 1602f969dc5aSEric Dumazet rcvbuf = READ_ONCE(sk->sk_rcvbuf); 1603f969dc5aSEric Dumazet threshold = rcvbuf - (rcvbuf >> 3); 160424adbc16SEric Dumazet 160524adbc16SEric Dumazet return atomic_read(&sk->sk_rmem_alloc) > threshold; 160624adbc16SEric Dumazet } 160724adbc16SEric Dumazet 160805dc72abSEric Dumazet static inline bool tcp_epollin_ready(const struct sock *sk, int target) 160905dc72abSEric Dumazet { 161005dc72abSEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 161105dc72abSEric Dumazet int avail = READ_ONCE(tp->rcv_nxt) - READ_ONCE(tp->copied_seq); 161205dc72abSEric Dumazet 161305dc72abSEric Dumazet if (avail <= 0) 161405dc72abSEric Dumazet return false; 161505dc72abSEric Dumazet 161605dc72abSEric Dumazet return (avail >= target) || tcp_rmem_pressure(sk) || 161705dc72abSEric Dumazet (tcp_receive_window(tp) <= inet_csk(sk)->icsk_ack.rcv_mss); 161805dc72abSEric Dumazet } 161905dc72abSEric Dumazet 1620843f4a55SYuchung Cheng extern void tcp_openreq_init_rwin(struct request_sock *req, 1621b1964b5fSEric Dumazet const struct sock *sk_listener, 1622b1964b5fSEric Dumazet const struct dst_entry *dst); 1623843f4a55SYuchung Cheng 16245c9f3023SJoe Perches void tcp_enter_memory_pressure(struct sock *sk); 162506044751SEric Dumazet void tcp_leave_memory_pressure(struct sock *sk); 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds static inline int keepalive_intvl_when(const struct tcp_sock *tp) 16281da177e4SLinus Torvalds { 1629b840d15dSNikolay Borisov struct net *net = sock_net((struct sock *)tp); 16305ecf9d4fSEric Dumazet int val; 1631b840d15dSNikolay Borisov 16325ecf9d4fSEric Dumazet /* Paired with WRITE_ONCE() in tcp_sock_set_keepintvl() 16335ecf9d4fSEric Dumazet * and do_tcp_setsockopt(). 16345ecf9d4fSEric Dumazet */ 16355ecf9d4fSEric Dumazet val = READ_ONCE(tp->keepalive_intvl); 16365ecf9d4fSEric Dumazet 16375ecf9d4fSEric Dumazet return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_intvl); 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds static inline int keepalive_time_when(const struct tcp_sock *tp) 16411da177e4SLinus Torvalds { 164213b287e8SNikolay Borisov struct net *net = sock_net((struct sock *)tp); 16434164245cSEric Dumazet int val; 164413b287e8SNikolay Borisov 16454164245cSEric Dumazet /* Paired with WRITE_ONCE() in tcp_sock_set_keepidle_locked() */ 16464164245cSEric Dumazet val = READ_ONCE(tp->keepalive_time); 16474164245cSEric Dumazet 16484164245cSEric Dumazet return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_time); 16491da177e4SLinus Torvalds } 16501da177e4SLinus Torvalds 1651df19a626SEric Dumazet static inline int keepalive_probes(const struct tcp_sock *tp) 1652df19a626SEric Dumazet { 16539bd6861bSNikolay Borisov struct net *net = sock_net((struct sock *)tp); 16546e5e1de6SEric Dumazet int val; 16559bd6861bSNikolay Borisov 16566e5e1de6SEric Dumazet /* Paired with WRITE_ONCE() in tcp_sock_set_keepcnt() 16576e5e1de6SEric Dumazet * and do_tcp_setsockopt(). 16586e5e1de6SEric Dumazet */ 16596e5e1de6SEric Dumazet val = READ_ONCE(tp->keepalive_probes); 16606e5e1de6SEric Dumazet 16616e5e1de6SEric Dumazet return val ? : READ_ONCE(net->ipv4.sysctl_tcp_keepalive_probes); 1662df19a626SEric Dumazet } 1663df19a626SEric Dumazet 16646c37e5deSFlavio Leitner static inline u32 keepalive_time_elapsed(const struct tcp_sock *tp) 16656c37e5deSFlavio Leitner { 16666c37e5deSFlavio Leitner const struct inet_connection_sock *icsk = &tp->inet_conn; 16676c37e5deSFlavio Leitner 166870eabf0eSEric Dumazet return min_t(u32, tcp_jiffies32 - icsk->icsk_ack.lrcvtime, 166970eabf0eSEric Dumazet tcp_jiffies32 - tp->rcv_tstamp); 16706c37e5deSFlavio Leitner } 16716c37e5deSFlavio Leitner 1672463c84b9SArnaldo Carvalho de Melo static inline int tcp_fin_time(const struct sock *sk) 16731da177e4SLinus Torvalds { 167439e24435SKuniyuki Iwashima int fin_timeout = tcp_sk(sk)->linger2 ? : 167539e24435SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fin_timeout); 1676463c84b9SArnaldo Carvalho de Melo const int rto = inet_csk(sk)->icsk_rto; 16771da177e4SLinus Torvalds 1678463c84b9SArnaldo Carvalho de Melo if (fin_timeout < (rto << 2) - (rto >> 1)) 1679463c84b9SArnaldo Carvalho de Melo fin_timeout = (rto << 2) - (rto >> 1); 16801da177e4SLinus Torvalds 16811da177e4SLinus Torvalds return fin_timeout; 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds 1684a2a385d6SEric Dumazet static inline bool tcp_paws_check(const struct tcp_options_received *rx_opt, 1685c887e6d2SIlpo Järvinen int paws_win) 16861da177e4SLinus Torvalds { 1687c887e6d2SIlpo Järvinen if ((s32)(rx_opt->ts_recent - rx_opt->rcv_tsval) <= paws_win) 1688a2a385d6SEric Dumazet return true; 1689cca9bab1SArnd Bergmann if (unlikely(!time_before32(ktime_get_seconds(), 1690af772144SEric Dumazet rx_opt->ts_recent_stamp + TCP_PAWS_WRAP))) 1691a2a385d6SEric Dumazet return true; 1692bc2ce894SEric Dumazet /* 1693bc2ce894SEric Dumazet * Some OSes send SYN and SYNACK messages with tsval=0 tsecr=0, 1694bc2ce894SEric Dumazet * then following tcp messages have valid values. Ignore 0 value, 1695bc2ce894SEric Dumazet * or else 'negative' tsval might forbid us to accept their packets. 1696bc2ce894SEric Dumazet */ 1697bc2ce894SEric Dumazet if (!rx_opt->ts_recent) 1698a2a385d6SEric Dumazet return true; 1699a2a385d6SEric Dumazet return false; 1700c887e6d2SIlpo Järvinen } 1701c887e6d2SIlpo Järvinen 1702a2a385d6SEric Dumazet static inline bool tcp_paws_reject(const struct tcp_options_received *rx_opt, 1703c887e6d2SIlpo Järvinen int rst) 1704c887e6d2SIlpo Järvinen { 1705c887e6d2SIlpo Järvinen if (tcp_paws_check(rx_opt, 0)) 1706a2a385d6SEric Dumazet return false; 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds /* RST segments are not recommended to carry timestamp, 17091da177e4SLinus Torvalds and, if they do, it is recommended to ignore PAWS because 17101da177e4SLinus Torvalds "their cleanup function should take precedence over timestamps." 17111da177e4SLinus Torvalds Certainly, it is mistake. It is necessary to understand the reasons 17121da177e4SLinus Torvalds of this constraint to relax it: if peer reboots, clock may go 17131da177e4SLinus Torvalds out-of-sync and half-open connections will not be reset. 17141da177e4SLinus Torvalds Actually, the problem would be not existing if all 17151da177e4SLinus Torvalds the implementations followed draft about maintaining clock 17161da177e4SLinus Torvalds via reboots. Linux-2.2 DOES NOT! 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds However, we can relax time bounds for RST segments to MSL. 17191da177e4SLinus Torvalds */ 1720cca9bab1SArnd Bergmann if (rst && !time_before32(ktime_get_seconds(), 1721cca9bab1SArnd Bergmann rx_opt->ts_recent_stamp + TCP_PAWS_MSL)) 1722a2a385d6SEric Dumazet return false; 1723a2a385d6SEric Dumazet return true; 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 17267970ddc8SEric Dumazet bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb, 17277970ddc8SEric Dumazet int mib_idx, u32 *last_oow_ack_time); 1728032ee423SNeal Cardwell 1729a9c19329SPavel Emelyanov static inline void tcp_mib_init(struct net *net) 17301da177e4SLinus Torvalds { 17311da177e4SLinus Torvalds /* See RFC 2012 */ 17326aef70a8SEric Dumazet TCP_ADD_STATS(net, TCP_MIB_RTOALGORITHM, 1); 17336aef70a8SEric Dumazet TCP_ADD_STATS(net, TCP_MIB_RTOMIN, TCP_RTO_MIN*1000/HZ); 17346aef70a8SEric Dumazet TCP_ADD_STATS(net, TCP_MIB_RTOMAX, TCP_RTO_MAX*1000/HZ); 17356aef70a8SEric Dumazet TCP_ADD_STATS(net, TCP_MIB_MAXCONN, -1); 17361da177e4SLinus Torvalds } 17371da177e4SLinus Torvalds 17386a438bbeSStephen Hemminger /* from STCP */ 1739ef9da47cSIlpo Järvinen static inline void tcp_clear_retrans_hints_partial(struct tcp_sock *tp) 17400800f170SDavid S. Miller { 17416a438bbeSStephen Hemminger tp->lost_skb_hint = NULL; 1742ef9da47cSIlpo Järvinen } 1743ef9da47cSIlpo Järvinen 1744ef9da47cSIlpo Järvinen static inline void tcp_clear_all_retrans_hints(struct tcp_sock *tp) 1745ef9da47cSIlpo Järvinen { 1746ef9da47cSIlpo Järvinen tcp_clear_retrans_hints_partial(tp); 17476a438bbeSStephen Hemminger tp->retransmit_skb_hint = NULL; 1748b7689205SIlpo Järvinen } 1749b7689205SIlpo Järvinen 1750c845f5f3SDmitry Safonov #define tcp_md5_addr tcp_ao_addr 1751a915da9bSEric Dumazet 1752cfb6eeb4SYOSHIFUJI Hideaki /* - key database */ 1753cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_key { 1754a915da9bSEric Dumazet struct hlist_node node; 1755cfb6eeb4SYOSHIFUJI Hideaki u8 keylen; 1756a915da9bSEric Dumazet u8 family; /* AF_INET or AF_INET6 */ 17576797318eSIvan Delalande u8 prefixlen; 1758a76c2315SLeonard Crestez u8 flags; 1759dea53bb8SDavid Ahern union tcp_md5_addr addr; 1760dea53bb8SDavid Ahern int l3index; /* set if key added with L3 scope */ 1761a915da9bSEric Dumazet u8 key[TCP_MD5SIG_MAXKEYLEN]; 1762a915da9bSEric Dumazet struct rcu_head rcu; 1763cfb6eeb4SYOSHIFUJI Hideaki }; 1764cfb6eeb4SYOSHIFUJI Hideaki 1765cfb6eeb4SYOSHIFUJI Hideaki /* - sock block */ 1766cfb6eeb4SYOSHIFUJI Hideaki struct tcp_md5sig_info { 1767a915da9bSEric Dumazet struct hlist_head head; 1768a8afca03SEric Dumazet struct rcu_head rcu; 1769cfb6eeb4SYOSHIFUJI Hideaki }; 1770cfb6eeb4SYOSHIFUJI Hideaki 1771cfb6eeb4SYOSHIFUJI Hideaki /* - pseudo header */ 1772cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr { 1773cfb6eeb4SYOSHIFUJI Hideaki __be32 saddr; 1774cfb6eeb4SYOSHIFUJI Hideaki __be32 daddr; 1775cfb6eeb4SYOSHIFUJI Hideaki __u8 pad; 1776cfb6eeb4SYOSHIFUJI Hideaki __u8 protocol; 1777cfb6eeb4SYOSHIFUJI Hideaki __be16 len; 1778cfb6eeb4SYOSHIFUJI Hideaki }; 1779cfb6eeb4SYOSHIFUJI Hideaki 1780cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr { 1781cfb6eeb4SYOSHIFUJI Hideaki struct in6_addr saddr; 1782cfb6eeb4SYOSHIFUJI Hideaki struct in6_addr daddr; 1783cfb6eeb4SYOSHIFUJI Hideaki __be32 len; 1784cfb6eeb4SYOSHIFUJI Hideaki __be32 protocol; /* including padding */ 1785cfb6eeb4SYOSHIFUJI Hideaki }; 1786cfb6eeb4SYOSHIFUJI Hideaki 1787cfb6eeb4SYOSHIFUJI Hideaki union tcp_md5sum_block { 1788cfb6eeb4SYOSHIFUJI Hideaki struct tcp4_pseudohdr ip4; 1789dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 1790cfb6eeb4SYOSHIFUJI Hideaki struct tcp6_pseudohdr ip6; 1791cfb6eeb4SYOSHIFUJI Hideaki #endif 1792cfb6eeb4SYOSHIFUJI Hideaki }; 1793cfb6eeb4SYOSHIFUJI Hideaki 17948c73b263SDmitry Safonov /* 17958c73b263SDmitry Safonov * struct tcp_sigpool - per-CPU pool of ahash_requests 17968c73b263SDmitry Safonov * @scratch: per-CPU temporary area, that can be used between 17978c73b263SDmitry Safonov * tcp_sigpool_start() and tcp_sigpool_end() to perform 17988c73b263SDmitry Safonov * crypto request 17998c73b263SDmitry Safonov * @req: pre-allocated ahash request 18008c73b263SDmitry Safonov */ 18018c73b263SDmitry Safonov struct tcp_sigpool { 180219689e38SEric Dumazet void *scratch; 18038c73b263SDmitry Safonov struct ahash_request *req; 1804cfb6eeb4SYOSHIFUJI Hideaki }; 1805cfb6eeb4SYOSHIFUJI Hideaki 18068c73b263SDmitry Safonov int tcp_sigpool_alloc_ahash(const char *alg, size_t scratch_size); 18078c73b263SDmitry Safonov void tcp_sigpool_get(unsigned int id); 18088c73b263SDmitry Safonov void tcp_sigpool_release(unsigned int id); 18098c73b263SDmitry Safonov int tcp_sigpool_hash_skb_data(struct tcp_sigpool *hp, 18108c73b263SDmitry Safonov const struct sk_buff *skb, 18118c73b263SDmitry Safonov unsigned int header_len); 18128c73b263SDmitry Safonov 18138c73b263SDmitry Safonov /** 18148c73b263SDmitry Safonov * tcp_sigpool_start - disable bh and start using tcp_sigpool_ahash 18158c73b263SDmitry Safonov * @id: tcp_sigpool that was previously allocated by tcp_sigpool_alloc_ahash() 18168c73b263SDmitry Safonov * @c: returned tcp_sigpool for usage (uninitialized on failure) 18178c73b263SDmitry Safonov * 18188c73b263SDmitry Safonov * Returns 0 on success, error otherwise. 18198c73b263SDmitry Safonov */ 18208c73b263SDmitry Safonov int tcp_sigpool_start(unsigned int id, struct tcp_sigpool *c); 18218c73b263SDmitry Safonov /** 18228c73b263SDmitry Safonov * tcp_sigpool_end - enable bh and stop using tcp_sigpool 18238c73b263SDmitry Safonov * @c: tcp_sigpool context that was returned by tcp_sigpool_start() 18248c73b263SDmitry Safonov */ 18258c73b263SDmitry Safonov void tcp_sigpool_end(struct tcp_sigpool *c); 18268c73b263SDmitry Safonov size_t tcp_sigpool_algo(unsigned int id, char *buf, size_t buf_len); 1827cfb6eeb4SYOSHIFUJI Hideaki /* - functions */ 182839f8e58eSEric Dumazet int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key, 182939f8e58eSEric Dumazet const struct sock *sk, const struct sk_buff *skb); 18305c9f3023SJoe Perches int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr, 1831a76c2315SLeonard Crestez int family, u8 prefixlen, int l3index, u8 flags, 1832459837b5SDmitry Safonov const u8 *newkey, u8 newkeylen); 1833459837b5SDmitry Safonov int tcp_md5_key_copy(struct sock *sk, const union tcp_md5_addr *addr, 1834459837b5SDmitry Safonov int family, u8 prefixlen, int l3index, 1835459837b5SDmitry Safonov struct tcp_md5sig_key *key); 1836459837b5SDmitry Safonov 18375c9f3023SJoe Perches int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr, 1838a76c2315SLeonard Crestez int family, u8 prefixlen, int l3index, u8 flags); 18390aadc739SDmitry Safonov void tcp_clear_md5_list(struct sock *sk); 1840b83e3debSEric Dumazet struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk, 1841fd3a154aSEric Dumazet const struct sock *addr_sk); 1842cfb6eeb4SYOSHIFUJI Hideaki 18439501f972SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 1844dea53bb8SDavid Ahern struct tcp_md5sig_key *__tcp_md5_do_lookup(const struct sock *sk, int l3index, 18455c9f3023SJoe Perches const union tcp_md5_addr *addr, 18460aadc739SDmitry Safonov int family, bool any_l3index); 18476015c71eSEric Dumazet static inline struct tcp_md5sig_key * 1848dea53bb8SDavid Ahern tcp_md5_do_lookup(const struct sock *sk, int l3index, 1849dea53bb8SDavid Ahern const union tcp_md5_addr *addr, int family) 18506015c71eSEric Dumazet { 1851459837b5SDmitry Safonov if (!static_branch_unlikely(&tcp_md5_needed.key)) 18526015c71eSEric Dumazet return NULL; 18530aadc739SDmitry Safonov return __tcp_md5_do_lookup(sk, l3index, addr, family, false); 18540aadc739SDmitry Safonov } 18550aadc739SDmitry Safonov 18560aadc739SDmitry Safonov static inline struct tcp_md5sig_key * 18570aadc739SDmitry Safonov tcp_md5_do_lookup_any_l3index(const struct sock *sk, 18580aadc739SDmitry Safonov const union tcp_md5_addr *addr, int family) 18590aadc739SDmitry Safonov { 18600aadc739SDmitry Safonov if (!static_branch_unlikely(&tcp_md5_needed.key)) 18610aadc739SDmitry Safonov return NULL; 18620aadc739SDmitry Safonov return __tcp_md5_do_lookup(sk, 0, addr, family, true); 18636015c71eSEric Dumazet } 18641330b6efSJakub Kicinski 18651330b6efSJakub Kicinski enum skb_drop_reason 18661330b6efSJakub Kicinski tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb, 18677bbb765bSDmitry Safonov const void *saddr, const void *daddr, 18680a3a8090SDmitry Safonov int family, int l3index, const __u8 *hash_location); 18697bbb765bSDmitry Safonov 18706015c71eSEric Dumazet 1871a915da9bSEric Dumazet #define tcp_twsk_md5_key(twsk) ((twsk)->tw_md5_key) 18729501f972SYOSHIFUJI Hideaki #else 1873dea53bb8SDavid Ahern static inline struct tcp_md5sig_key * 1874dea53bb8SDavid Ahern tcp_md5_do_lookup(const struct sock *sk, int l3index, 1875dea53bb8SDavid Ahern const union tcp_md5_addr *addr, int family) 1876a915da9bSEric Dumazet { 1877a915da9bSEric Dumazet return NULL; 1878a915da9bSEric Dumazet } 18791330b6efSJakub Kicinski 18800aadc739SDmitry Safonov static inline struct tcp_md5sig_key * 18810aadc739SDmitry Safonov tcp_md5_do_lookup_any_l3index(const struct sock *sk, 18820aadc739SDmitry Safonov const union tcp_md5_addr *addr, int family) 18830aadc739SDmitry Safonov { 18840aadc739SDmitry Safonov return NULL; 18850aadc739SDmitry Safonov } 18860aadc739SDmitry Safonov 18871330b6efSJakub Kicinski static inline enum skb_drop_reason 18881330b6efSJakub Kicinski tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb, 18897bbb765bSDmitry Safonov const void *saddr, const void *daddr, 18900a3a8090SDmitry Safonov int family, int l3index, const __u8 *hash_location) 18917bbb765bSDmitry Safonov { 18921330b6efSJakub Kicinski return SKB_NOT_DROPPED_YET; 18937bbb765bSDmitry Safonov } 18949501f972SYOSHIFUJI Hideaki #define tcp_twsk_md5_key(twsk) NULL 18959501f972SYOSHIFUJI Hideaki #endif 18969501f972SYOSHIFUJI Hideaki 18978c73b263SDmitry Safonov int tcp_md5_alloc_sigpool(void); 18988c73b263SDmitry Safonov void tcp_md5_release_sigpool(void); 18998c73b263SDmitry Safonov void tcp_md5_add_sigpool(void); 19008c73b263SDmitry Safonov extern int tcp_md5_sigpool_id; 1901cfb6eeb4SYOSHIFUJI Hideaki 19028c73b263SDmitry Safonov int tcp_md5_hash_key(struct tcp_sigpool *hp, 1903cf533ea5SEric Dumazet const struct tcp_md5sig_key *key); 1904cfb6eeb4SYOSHIFUJI Hideaki 190510467163SJerry Chu /* From tcp_fastopen.c */ 19065c9f3023SJoe Perches void tcp_fastopen_cache_get(struct sock *sk, u16 *mss, 19077268586bSYuchung Cheng struct tcp_fastopen_cookie *cookie); 19085c9f3023SJoe Perches void tcp_fastopen_cache_set(struct sock *sk, u16 mss, 19092646c831SDaniel Lee struct tcp_fastopen_cookie *cookie, bool syn_lost, 19102646c831SDaniel Lee u16 try_exp); 1911783237e8SYuchung Cheng struct tcp_fastopen_request { 1912783237e8SYuchung Cheng /* Fast Open cookie. Size 0 means a cookie request */ 1913783237e8SYuchung Cheng struct tcp_fastopen_cookie cookie; 1914783237e8SYuchung Cheng struct msghdr *data; /* data in MSG_FASTOPEN */ 1915f5ddcbbbSEric Dumazet size_t size; 1916f5ddcbbbSEric Dumazet int copied; /* queued in tcp_connect() */ 1917f859a448SWillem de Bruijn struct ubuf_info *uarg; 1918783237e8SYuchung Cheng }; 1919783237e8SYuchung Cheng void tcp_free_fastopen_req(struct tcp_sock *tp); 19201fba70e5SYuchung Cheng void tcp_fastopen_destroy_cipher(struct sock *sk); 192143713848SHaishuang Yan void tcp_fastopen_ctx_destroy(struct net *net); 19221fba70e5SYuchung Cheng int tcp_fastopen_reset_cipher(struct net *net, struct sock *sk, 1923438ac880SArd Biesheuvel void *primary_key, void *backup_key); 1924f19008e6SJason Baron int tcp_fastopen_get_cipher(struct net *net, struct inet_connection_sock *icsk, 1925f19008e6SJason Baron u64 *key); 192661d2bcaeSEric Dumazet void tcp_fastopen_add_skb(struct sock *sk, struct sk_buff *skb); 19277c85af88SEric Dumazet struct sock *tcp_try_fastopen(struct sock *sk, struct sk_buff *skb, 19285b7ed089SYuchung Cheng struct request_sock *req, 192971c02379SChristoph Paasch struct tcp_fastopen_cookie *foc, 193071c02379SChristoph Paasch const struct dst_entry *dst); 193143713848SHaishuang Yan void tcp_fastopen_init_key_once(struct net *net); 1932065263f4SWei Wang bool tcp_fastopen_cookie_check(struct sock *sk, u16 *mss, 1933065263f4SWei Wang struct tcp_fastopen_cookie *cookie); 193419f6d3f3SWei Wang bool tcp_fastopen_defer_connect(struct sock *sk, int *err); 1935438ac880SArd Biesheuvel #define TCP_FASTOPEN_KEY_LENGTH sizeof(siphash_key_t) 19369092a76dSJason Baron #define TCP_FASTOPEN_KEY_MAX 2 19379092a76dSJason Baron #define TCP_FASTOPEN_KEY_BUF_LENGTH \ 19389092a76dSJason Baron (TCP_FASTOPEN_KEY_LENGTH * TCP_FASTOPEN_KEY_MAX) 193910467163SJerry Chu 194010467163SJerry Chu /* Fastopen key context */ 194110467163SJerry Chu struct tcp_fastopen_context { 1942438ac880SArd Biesheuvel siphash_key_t key[TCP_FASTOPEN_KEY_MAX]; 1943c681edaeSArd Biesheuvel int num; 194410467163SJerry Chu struct rcu_head rcu; 194510467163SJerry Chu }; 194610467163SJerry Chu 194746c2fa39SWei Wang void tcp_fastopen_active_disable(struct sock *sk); 1948cf1ef3f0SWei Wang bool tcp_fastopen_active_should_disable(struct sock *sk); 1949cf1ef3f0SWei Wang void tcp_fastopen_active_disable_ofo_check(struct sock *sk); 19507268586bSYuchung Cheng void tcp_fastopen_active_detect_blackhole(struct sock *sk, bool expired); 1951cf1ef3f0SWei Wang 19529092a76dSJason Baron /* Caller needs to wrap with rcu_read_(un)lock() */ 19539092a76dSJason Baron static inline 19549092a76dSJason Baron struct tcp_fastopen_context *tcp_fastopen_get_ctx(const struct sock *sk) 19559092a76dSJason Baron { 19569092a76dSJason Baron struct tcp_fastopen_context *ctx; 19579092a76dSJason Baron 19589092a76dSJason Baron ctx = rcu_dereference(inet_csk(sk)->icsk_accept_queue.fastopenq.ctx); 19599092a76dSJason Baron if (!ctx) 19609092a76dSJason Baron ctx = rcu_dereference(sock_net(sk)->ipv4.tcp_fastopen_ctx); 19619092a76dSJason Baron return ctx; 19629092a76dSJason Baron } 19639092a76dSJason Baron 19649092a76dSJason Baron static inline 19659092a76dSJason Baron bool tcp_fastopen_cookie_match(const struct tcp_fastopen_cookie *foc, 19669092a76dSJason Baron const struct tcp_fastopen_cookie *orig) 19679092a76dSJason Baron { 19689092a76dSJason Baron if (orig->len == TCP_FASTOPEN_COOKIE_SIZE && 19699092a76dSJason Baron orig->len == foc->len && 19709092a76dSJason Baron !memcmp(orig->val, foc->val, foc->len)) 19719092a76dSJason Baron return true; 19729092a76dSJason Baron return false; 19739092a76dSJason Baron } 19749092a76dSJason Baron 19759092a76dSJason Baron static inline 19769092a76dSJason Baron int tcp_fastopen_context_len(const struct tcp_fastopen_context *ctx) 19779092a76dSJason Baron { 1978c681edaeSArd Biesheuvel return ctx->num; 19799092a76dSJason Baron } 19809092a76dSJason Baron 198105b055e8SFrancis Yan /* Latencies incurred by various limits for a sender. They are 198205b055e8SFrancis Yan * chronograph-like stats that are mutually exclusive. 198305b055e8SFrancis Yan */ 198405b055e8SFrancis Yan enum tcp_chrono { 198505b055e8SFrancis Yan TCP_CHRONO_UNSPEC, 198605b055e8SFrancis Yan TCP_CHRONO_BUSY, /* Actively sending data (non-empty write queue) */ 198705b055e8SFrancis Yan TCP_CHRONO_RWND_LIMITED, /* Stalled by insufficient receive window */ 198805b055e8SFrancis Yan TCP_CHRONO_SNDBUF_LIMITED, /* Stalled by insufficient send buffer */ 198905b055e8SFrancis Yan __TCP_CHRONO_MAX, 199005b055e8SFrancis Yan }; 199105b055e8SFrancis Yan 199205b055e8SFrancis Yan void tcp_chrono_start(struct sock *sk, const enum tcp_chrono type); 199305b055e8SFrancis Yan void tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type); 199405b055e8SFrancis Yan 1995e2080072SEric Dumazet /* This helper is needed, because skb->tcp_tsorted_anchor uses 1996e2080072SEric Dumazet * the same memory storage than skb->destructor/_skb_refdst 1997e2080072SEric Dumazet */ 1998e2080072SEric Dumazet static inline void tcp_skb_tsorted_anchor_cleanup(struct sk_buff *skb) 1999e2080072SEric Dumazet { 2000e2080072SEric Dumazet skb->destructor = NULL; 2001e2080072SEric Dumazet skb->_skb_refdst = 0UL; 2002e2080072SEric Dumazet } 2003e2080072SEric Dumazet 2004e2080072SEric Dumazet #define tcp_skb_tsorted_save(skb) { \ 2005e2080072SEric Dumazet unsigned long _save = skb->_skb_refdst; \ 2006e2080072SEric Dumazet skb->_skb_refdst = 0UL; 2007e2080072SEric Dumazet 2008e2080072SEric Dumazet #define tcp_skb_tsorted_restore(skb) \ 2009e2080072SEric Dumazet skb->_skb_refdst = _save; \ 2010e2080072SEric Dumazet } 2011e2080072SEric Dumazet 2012ac3f09baSEric Dumazet void tcp_write_queue_purge(struct sock *sk); 2013fe067e8aSDavid S. Miller 201475c119afSEric Dumazet static inline struct sk_buff *tcp_rtx_queue_head(const struct sock *sk) 201575c119afSEric Dumazet { 201675c119afSEric Dumazet return skb_rb_first(&sk->tcp_rtx_queue); 201775c119afSEric Dumazet } 201875c119afSEric Dumazet 2019b617158dSEric Dumazet static inline struct sk_buff *tcp_rtx_queue_tail(const struct sock *sk) 2020b617158dSEric Dumazet { 2021b617158dSEric Dumazet return skb_rb_last(&sk->tcp_rtx_queue); 2022b617158dSEric Dumazet } 2023b617158dSEric Dumazet 2024cf533ea5SEric Dumazet static inline struct sk_buff *tcp_write_queue_tail(const struct sock *sk) 2025fe067e8aSDavid S. Miller { 2026cd07a8eaSDavid S. Miller return skb_peek_tail(&sk->sk_write_queue); 2027fe067e8aSDavid S. Miller } 2028fe067e8aSDavid S. Miller 2029234b6860SIlpo Järvinen #define tcp_for_write_queue_from_safe(skb, tmp, sk) \ 2030cd07a8eaSDavid S. Miller skb_queue_walk_from_safe(&(sk)->sk_write_queue, skb, tmp) 2031234b6860SIlpo Järvinen 2032cf533ea5SEric Dumazet static inline struct sk_buff *tcp_send_head(const struct sock *sk) 2033fe067e8aSDavid S. Miller { 203475c119afSEric Dumazet return skb_peek(&sk->sk_write_queue); 2035fe067e8aSDavid S. Miller } 2036fe067e8aSDavid S. Miller 2037cd07a8eaSDavid S. Miller static inline bool tcp_skb_is_last(const struct sock *sk, 2038cd07a8eaSDavid S. Miller const struct sk_buff *skb) 2039cd07a8eaSDavid S. Miller { 2040cd07a8eaSDavid S. Miller return skb_queue_is_last(&sk->sk_write_queue, skb); 2041cd07a8eaSDavid S. Miller } 2042cd07a8eaSDavid S. Miller 2043ee2aabd3SEric Dumazet /** 2044ee2aabd3SEric Dumazet * tcp_write_queue_empty - test if any payload (or FIN) is available in write queue 2045ee2aabd3SEric Dumazet * @sk: socket 2046ee2aabd3SEric Dumazet * 2047ee2aabd3SEric Dumazet * Since the write queue can have a temporary empty skb in it, 2048ee2aabd3SEric Dumazet * we must not use "return skb_queue_empty(&sk->sk_write_queue)" 2049ee2aabd3SEric Dumazet */ 205075c119afSEric Dumazet static inline bool tcp_write_queue_empty(const struct sock *sk) 2051fe067e8aSDavid S. Miller { 2052ee2aabd3SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 2053ee2aabd3SEric Dumazet 2054ee2aabd3SEric Dumazet return tp->write_seq == tp->snd_nxt; 205575c119afSEric Dumazet } 205675c119afSEric Dumazet 205775c119afSEric Dumazet static inline bool tcp_rtx_queue_empty(const struct sock *sk) 205875c119afSEric Dumazet { 205975c119afSEric Dumazet return RB_EMPTY_ROOT(&sk->tcp_rtx_queue); 206075c119afSEric Dumazet } 206175c119afSEric Dumazet 206275c119afSEric Dumazet static inline bool tcp_rtx_and_write_queues_empty(const struct sock *sk) 206375c119afSEric Dumazet { 206475c119afSEric Dumazet return tcp_rtx_queue_empty(sk) && tcp_write_queue_empty(sk); 2065fe067e8aSDavid S. Miller } 2066fe067e8aSDavid S. Miller 2067fe067e8aSDavid S. Miller static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb) 2068fe067e8aSDavid S. Miller { 2069a43e052bSEric Dumazet __skb_queue_tail(&sk->sk_write_queue, skb); 2070fe067e8aSDavid S. Miller 2071fe067e8aSDavid S. Miller /* Queue it, remembering where we must start sending. */ 207250895b9dSEric Dumazet if (sk->sk_write_queue.next == skb) 20730f87230dSFrancis Yan tcp_chrono_start(sk, TCP_CHRONO_BUSY); 2074fe067e8aSDavid S. Miller } 2075fe067e8aSDavid S. Miller 207643f59c89SDavid S. Miller /* Insert new before skb on the write queue of sk. */ 2077fe067e8aSDavid S. Miller static inline void tcp_insert_write_queue_before(struct sk_buff *new, 2078fe067e8aSDavid S. Miller struct sk_buff *skb, 2079fe067e8aSDavid S. Miller struct sock *sk) 2080fe067e8aSDavid S. Miller { 208143f59c89SDavid S. Miller __skb_queue_before(&sk->sk_write_queue, skb, new); 2082fe067e8aSDavid S. Miller } 2083fe067e8aSDavid S. Miller 2084fe067e8aSDavid S. Miller static inline void tcp_unlink_write_queue(struct sk_buff *skb, struct sock *sk) 2085fe067e8aSDavid S. Miller { 20864a269818SEric Dumazet tcp_skb_tsorted_anchor_cleanup(skb); 2087fe067e8aSDavid S. Miller __skb_unlink(skb, &sk->sk_write_queue); 2088fe067e8aSDavid S. Miller } 2089fe067e8aSDavid S. Miller 209075c119afSEric Dumazet void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb); 209175c119afSEric Dumazet 209275c119afSEric Dumazet static inline void tcp_rtx_queue_unlink(struct sk_buff *skb, struct sock *sk) 2093fe067e8aSDavid S. Miller { 209475c119afSEric Dumazet tcp_skb_tsorted_anchor_cleanup(skb); 209575c119afSEric Dumazet rb_erase(&skb->rbnode, &sk->tcp_rtx_queue); 209675c119afSEric Dumazet } 209775c119afSEric Dumazet 209875c119afSEric Dumazet static inline void tcp_rtx_queue_unlink_and_free(struct sk_buff *skb, struct sock *sk) 209975c119afSEric Dumazet { 210075c119afSEric Dumazet list_del(&skb->tcp_tsorted_anchor); 210175c119afSEric Dumazet tcp_rtx_queue_unlink(skb, sk); 210203271f3aSTalal Ahmad tcp_wmem_free_skb(sk, skb); 2103fe067e8aSDavid S. Miller } 2104fe067e8aSDavid S. Miller 210512d50c46SKrishna Kumar static inline void tcp_push_pending_frames(struct sock *sk) 210612d50c46SKrishna Kumar { 210712d50c46SKrishna Kumar if (tcp_send_head(sk)) { 210812d50c46SKrishna Kumar struct tcp_sock *tp = tcp_sk(sk); 210912d50c46SKrishna Kumar 211012d50c46SKrishna Kumar __tcp_push_pending_frames(sk, tcp_current_mss(sk), tp->nonagle); 211112d50c46SKrishna Kumar } 211212d50c46SKrishna Kumar } 211312d50c46SKrishna Kumar 2114ecb97192SNeal Cardwell /* Start sequence of the skb just after the highest skb with SACKed 2115ecb97192SNeal Cardwell * bit, valid only if sacked_out > 0 or when the caller has ensured 2116ecb97192SNeal Cardwell * validity by itself. 2117a47e5a98SIlpo Järvinen */ 2118a47e5a98SIlpo Järvinen static inline u32 tcp_highest_sack_seq(struct tcp_sock *tp) 2119a47e5a98SIlpo Järvinen { 2120a47e5a98SIlpo Järvinen if (!tp->sacked_out) 2121a47e5a98SIlpo Järvinen return tp->snd_una; 21226859d494SIlpo Järvinen 21236859d494SIlpo Järvinen if (tp->highest_sack == NULL) 21246859d494SIlpo Järvinen return tp->snd_nxt; 21256859d494SIlpo Järvinen 2126a47e5a98SIlpo Järvinen return TCP_SKB_CB(tp->highest_sack)->seq; 2127a47e5a98SIlpo Järvinen } 2128a47e5a98SIlpo Järvinen 21296859d494SIlpo Järvinen static inline void tcp_advance_highest_sack(struct sock *sk, struct sk_buff *skb) 21306859d494SIlpo Järvinen { 213150895b9dSEric Dumazet tcp_sk(sk)->highest_sack = skb_rb_next(skb); 21326859d494SIlpo Järvinen } 21336859d494SIlpo Järvinen 21346859d494SIlpo Järvinen static inline struct sk_buff *tcp_highest_sack(struct sock *sk) 21356859d494SIlpo Järvinen { 21366859d494SIlpo Järvinen return tcp_sk(sk)->highest_sack; 21376859d494SIlpo Järvinen } 21386859d494SIlpo Järvinen 21396859d494SIlpo Järvinen static inline void tcp_highest_sack_reset(struct sock *sk) 21406859d494SIlpo Järvinen { 214150895b9dSEric Dumazet tcp_sk(sk)->highest_sack = tcp_rtx_queue_head(sk); 21426859d494SIlpo Järvinen } 21436859d494SIlpo Järvinen 21442b7cda9cSEric Dumazet /* Called when old skb is about to be deleted and replaced by new skb */ 21452b7cda9cSEric Dumazet static inline void tcp_highest_sack_replace(struct sock *sk, 21466859d494SIlpo Järvinen struct sk_buff *old, 21476859d494SIlpo Järvinen struct sk_buff *new) 21486859d494SIlpo Järvinen { 21492b7cda9cSEric Dumazet if (old == tcp_highest_sack(sk)) 21506859d494SIlpo Järvinen tcp_sk(sk)->highest_sack = new; 21516859d494SIlpo Järvinen } 21526859d494SIlpo Järvinen 2153b1f0a0e9SFlorian Westphal /* This helper checks if socket has IP_TRANSPARENT set */ 2154b1f0a0e9SFlorian Westphal static inline bool inet_sk_transparent(const struct sock *sk) 2155b1f0a0e9SFlorian Westphal { 2156b1f0a0e9SFlorian Westphal switch (sk->sk_state) { 2157b1f0a0e9SFlorian Westphal case TCP_TIME_WAIT: 2158b1f0a0e9SFlorian Westphal return inet_twsk(sk)->tw_transparent; 2159b1f0a0e9SFlorian Westphal case TCP_NEW_SYN_RECV: 2160b1f0a0e9SFlorian Westphal return inet_rsk(inet_reqsk(sk))->no_srccheck; 2161b1f0a0e9SFlorian Westphal } 21624bd0623fSEric Dumazet return inet_test_bit(TRANSPARENT, sk); 2163b1f0a0e9SFlorian Westphal } 2164b1f0a0e9SFlorian Westphal 21655aa4b32fSAndreas Petlund /* Determines whether this is a thin stream (which may suffer from 21665aa4b32fSAndreas Petlund * increased latency). Used to trigger latency-reducing mechanisms. 21675aa4b32fSAndreas Petlund */ 2168a2a385d6SEric Dumazet static inline bool tcp_stream_is_thin(struct tcp_sock *tp) 21695aa4b32fSAndreas Petlund { 21705aa4b32fSAndreas Petlund return tp->packets_out < 4 && !tcp_in_initial_slowstart(tp); 21715aa4b32fSAndreas Petlund } 21725aa4b32fSAndreas Petlund 21731da177e4SLinus Torvalds /* /proc */ 21741da177e4SLinus Torvalds enum tcp_seq_states { 21751da177e4SLinus Torvalds TCP_SEQ_STATE_LISTENING, 21761da177e4SLinus Torvalds TCP_SEQ_STATE_ESTABLISHED, 21771da177e4SLinus Torvalds }; 21781da177e4SLinus Torvalds 217937d849bbSChristoph Hellwig void *tcp_seq_start(struct seq_file *seq, loff_t *pos); 218037d849bbSChristoph Hellwig void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos); 218137d849bbSChristoph Hellwig void tcp_seq_stop(struct seq_file *seq, void *v); 218273cb88ecSArjan van de Ven 21831da177e4SLinus Torvalds struct tcp_seq_afinfo { 21841da177e4SLinus Torvalds sa_family_t family; 21851da177e4SLinus Torvalds }; 21861da177e4SLinus Torvalds 21871da177e4SLinus Torvalds struct tcp_iter_state { 2188a4146b1bSDenis V. Lunev struct seq_net_private p; 21891da177e4SLinus Torvalds enum tcp_seq_states state; 21901da177e4SLinus Torvalds struct sock *syn_wait_sk; 2191a7cb5a49SEric W. Biederman int bucket, offset, sbucket, num; 2192a8b690f9STom Herbert loff_t last_pos; 21931da177e4SLinus Torvalds }; 21941da177e4SLinus Torvalds 219520380731SArnaldo Carvalho de Melo extern struct request_sock_ops tcp_request_sock_ops; 2196c6aefafbSGlenn Griffin extern struct request_sock_ops tcp6_request_sock_ops; 219720380731SArnaldo Carvalho de Melo 21985c9f3023SJoe Perches void tcp_v4_destroy_sock(struct sock *sk); 219920380731SArnaldo Carvalho de Melo 220028be6e07SEric Dumazet struct sk_buff *tcp_gso_segment(struct sk_buff *skb, 2201c8f44affSMichał Mirosław netdev_features_t features); 2202d4546c25SDavid Miller struct sk_buff *tcp_gro_receive(struct list_head *head, struct sk_buff *skb); 22035521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp4_gro_complete(struct sk_buff *skb, int thoff)); 22045521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp4_gro_receive(struct list_head *head, struct sk_buff *skb)); 22055521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(int tcp6_gro_complete(struct sk_buff *skb, int thoff)); 22065521d95eSEric Dumazet INDIRECT_CALLABLE_DECLARE(struct sk_buff *tcp6_gro_receive(struct list_head *head, struct sk_buff *skb)); 2207e411a8e3SJacob Keller #ifdef CONFIG_INET 2208b1f2abcfSParav Pandit void tcp_gro_complete(struct sk_buff *skb); 2209e411a8e3SJacob Keller #else 2210e411a8e3SJacob Keller static inline void tcp_gro_complete(struct sk_buff *skb) { } 2211e411a8e3SJacob Keller #endif 221228850dc7SDaniel Borkmann 22135c9f3023SJoe Perches void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr); 2214f4c50d99SHerbert Xu 2215c9bee3b7SEric Dumazet static inline u32 tcp_notsent_lowat(const struct tcp_sock *tp) 2216c9bee3b7SEric Dumazet { 22174979f2d9SNikolay Borisov struct net *net = sock_net((struct sock *)tp); 22181aeb87bcSEric Dumazet u32 val; 22191aeb87bcSEric Dumazet 22201aeb87bcSEric Dumazet val = READ_ONCE(tp->notsent_lowat); 22211aeb87bcSEric Dumazet 22221aeb87bcSEric Dumazet return val ?: READ_ONCE(net->ipv4.sysctl_tcp_notsent_lowat); 2223c9bee3b7SEric Dumazet } 2224c9bee3b7SEric Dumazet 2225d3cd4924SEric Dumazet bool tcp_stream_memory_free(const struct sock *sk, int wake); 2226c9bee3b7SEric Dumazet 222720380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS 22285c9f3023SJoe Perches int tcp4_proc_init(void); 22295c9f3023SJoe Perches void tcp4_proc_exit(void); 223020380731SArnaldo Carvalho de Melo #endif 223120380731SArnaldo Carvalho de Melo 2232ea3bea3aSEric Dumazet int tcp_rtx_synack(const struct sock *sk, struct request_sock *req); 22331fb6f159SOctavian Purdila int tcp_conn_request(struct request_sock_ops *rsk_ops, 22341fb6f159SOctavian Purdila const struct tcp_request_sock_ops *af_ops, 22351fb6f159SOctavian Purdila struct sock *sk, struct sk_buff *skb); 22365db92c99SOctavian Purdila 2237cfb6eeb4SYOSHIFUJI Hideaki /* TCP af-specific functions */ 2238cfb6eeb4SYOSHIFUJI Hideaki struct tcp_sock_af_ops { 2239cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 2240b83e3debSEric Dumazet struct tcp_md5sig_key *(*md5_lookup) (const struct sock *sk, 2241fd3a154aSEric Dumazet const struct sock *addr_sk); 2242cfb6eeb4SYOSHIFUJI Hideaki int (*calc_md5_hash)(char *location, 224339f8e58eSEric Dumazet const struct tcp_md5sig_key *md5, 2244318cf7aaSEric Dumazet const struct sock *sk, 2245318cf7aaSEric Dumazet const struct sk_buff *skb); 2246cfb6eeb4SYOSHIFUJI Hideaki int (*md5_parse)(struct sock *sk, 22478917a777SIvan Delalande int optname, 2248d4c19c49SChristoph Hellwig sockptr_t optval, 2249cfb6eeb4SYOSHIFUJI Hideaki int optlen); 2250cfb6eeb4SYOSHIFUJI Hideaki #endif 22514954f17dSDmitry Safonov #ifdef CONFIG_TCP_AO 22524954f17dSDmitry Safonov int (*ao_parse)(struct sock *sk, int optname, sockptr_t optval, int optlen); 22530aadc739SDmitry Safonov struct tcp_ao_key *(*ao_lookup)(const struct sock *sk, 22540aadc739SDmitry Safonov struct sock *addr_sk, 22550aadc739SDmitry Safonov int sndid, int rcvid); 22567c2ffaf2SDmitry Safonov int (*ao_calc_key_sk)(struct tcp_ao_key *mkt, u8 *key, 22577c2ffaf2SDmitry Safonov const struct sock *sk, 22587c2ffaf2SDmitry Safonov __be32 sisn, __be32 disn, bool send); 22591e03d32bSDmitry Safonov int (*calc_ao_hash)(char *location, struct tcp_ao_key *ao, 22601e03d32bSDmitry Safonov const struct sock *sk, const struct sk_buff *skb, 22611e03d32bSDmitry Safonov const u8 *tkey, int hash_offset, u32 sne); 22624954f17dSDmitry Safonov #endif 2263cfb6eeb4SYOSHIFUJI Hideaki }; 2264cfb6eeb4SYOSHIFUJI Hideaki 2265cfb6eeb4SYOSHIFUJI Hideaki struct tcp_request_sock_ops { 22662aec4a29SOctavian Purdila u16 mss_clamp; 2267cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 2268b83e3debSEric Dumazet struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk, 2269fd3a154aSEric Dumazet const struct sock *addr_sk); 2270e3afe7b7SJohn Dykstra int (*calc_md5_hash) (char *location, 227139f8e58eSEric Dumazet const struct tcp_md5sig_key *md5, 2272318cf7aaSEric Dumazet const struct sock *sk, 2273318cf7aaSEric Dumazet const struct sk_buff *skb); 2274cfb6eeb4SYOSHIFUJI Hideaki #endif 227506b22ef2SDmitry Safonov #ifdef CONFIG_TCP_AO 227606b22ef2SDmitry Safonov struct tcp_ao_key *(*ao_lookup)(const struct sock *sk, 227706b22ef2SDmitry Safonov struct request_sock *req, 227806b22ef2SDmitry Safonov int sndid, int rcvid); 227906b22ef2SDmitry Safonov int (*ao_calc_key)(struct tcp_ao_key *mkt, u8 *key, struct request_sock *sk); 22809427c6aaSDmitry Safonov int (*ao_synack_hash)(char *ao_hash, struct tcp_ao_key *mkt, 22819427c6aaSDmitry Safonov struct request_sock *req, const struct sk_buff *skb, 22829427c6aaSDmitry Safonov int hash_offset, u32 sne); 228306b22ef2SDmitry Safonov #endif 2284fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES 22853f684b4bSEric Dumazet __u32 (*cookie_init_seq)(const struct sk_buff *skb, 2286fb7b37a7SOctavian Purdila __u16 *mss); 2287fb7b37a7SOctavian Purdila #endif 22887ea851d1SFlorian Westphal struct dst_entry *(*route_req)(const struct sock *sk, 22897ea851d1SFlorian Westphal struct sk_buff *skb, 22907ea851d1SFlorian Westphal struct flowi *fl, 2291b9e81040SEric Dumazet struct request_sock *req, 2292b9e81040SEric Dumazet u32 tw_isn); 229384b114b9SEric Dumazet u32 (*init_seq)(const struct sk_buff *skb); 22945d2ed052SEric Dumazet u32 (*init_ts_off)(const struct net *net, const struct sk_buff *skb); 22950f935dbeSEric Dumazet int (*send_synack)(const struct sock *sk, struct dst_entry *dst, 2296d6274bd8SOctavian Purdila struct flowi *fl, struct request_sock *req, 2297dc6ef6beSEric Dumazet struct tcp_fastopen_cookie *foc, 2298331fca43SMartin KaFai Lau enum tcp_synack_type synack_type, 2299331fca43SMartin KaFai Lau struct sk_buff *syn_skb); 2300cfb6eeb4SYOSHIFUJI Hideaki }; 2301cfb6eeb4SYOSHIFUJI Hideaki 230235b2c321SMat Martineau extern const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops; 230335b2c321SMat Martineau #if IS_ENABLED(CONFIG_IPV6) 230435b2c321SMat Martineau extern const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops; 230535b2c321SMat Martineau #endif 230635b2c321SMat Martineau 2307fb7b37a7SOctavian Purdila #ifdef CONFIG_SYN_COOKIES 2308fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops, 23093f684b4bSEric Dumazet const struct sock *sk, struct sk_buff *skb, 2310fb7b37a7SOctavian Purdila __u16 *mss) 2311fb7b37a7SOctavian Purdila { 23123f684b4bSEric Dumazet tcp_synq_overflow(sk); 231302a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), LINUX_MIB_SYNCOOKIESSENT); 23143f684b4bSEric Dumazet return ops->cookie_init_seq(skb, mss); 2315fb7b37a7SOctavian Purdila } 2316fb7b37a7SOctavian Purdila #else 2317fb7b37a7SOctavian Purdila static inline __u32 cookie_init_sequence(const struct tcp_request_sock_ops *ops, 23183f684b4bSEric Dumazet const struct sock *sk, struct sk_buff *skb, 2319fb7b37a7SOctavian Purdila __u16 *mss) 2320fb7b37a7SOctavian Purdila { 2321fb7b37a7SOctavian Purdila return 0; 2322fb7b37a7SOctavian Purdila } 2323fb7b37a7SOctavian Purdila #endif 2324fb7b37a7SOctavian Purdila 23251e03d32bSDmitry Safonov struct tcp_key { 23261e03d32bSDmitry Safonov union { 2327ba7783adSDmitry Safonov struct { 23281e03d32bSDmitry Safonov struct tcp_ao_key *ao_key; 2329ba7783adSDmitry Safonov char *traffic_key; 2330ba7783adSDmitry Safonov u32 sne; 2331ba7783adSDmitry Safonov u8 rcv_next; 2332ba7783adSDmitry Safonov }; 23331e03d32bSDmitry Safonov struct tcp_md5sig_key *md5_key; 23341e03d32bSDmitry Safonov }; 23351e03d32bSDmitry Safonov enum { 23361e03d32bSDmitry Safonov TCP_KEY_NONE = 0, 23371e03d32bSDmitry Safonov TCP_KEY_MD5, 23381e03d32bSDmitry Safonov TCP_KEY_AO, 23391e03d32bSDmitry Safonov } type; 23401e03d32bSDmitry Safonov }; 23411e03d32bSDmitry Safonov 23421e03d32bSDmitry Safonov static inline void tcp_get_current_key(const struct sock *sk, 23431e03d32bSDmitry Safonov struct tcp_key *out) 23441e03d32bSDmitry Safonov { 23451e03d32bSDmitry Safonov #if defined(CONFIG_TCP_AO) || defined(CONFIG_TCP_MD5SIG) 23461e03d32bSDmitry Safonov const struct tcp_sock *tp = tcp_sk(sk); 23471e03d32bSDmitry Safonov #endif 234867fa83f7SDmitry Safonov 23491e03d32bSDmitry Safonov #ifdef CONFIG_TCP_AO 235067fa83f7SDmitry Safonov if (static_branch_unlikely(&tcp_ao_needed.key)) { 23511e03d32bSDmitry Safonov struct tcp_ao_info *ao; 23521e03d32bSDmitry Safonov 235367fa83f7SDmitry Safonov ao = rcu_dereference_protected(tp->ao_info, 235467fa83f7SDmitry Safonov lockdep_sock_is_held(sk)); 23551e03d32bSDmitry Safonov if (ao) { 23561e03d32bSDmitry Safonov out->ao_key = READ_ONCE(ao->current_key); 23571e03d32bSDmitry Safonov out->type = TCP_KEY_AO; 23581e03d32bSDmitry Safonov return; 23591e03d32bSDmitry Safonov } 236067fa83f7SDmitry Safonov } 23611e03d32bSDmitry Safonov #endif 23621e03d32bSDmitry Safonov #ifdef CONFIG_TCP_MD5SIG 23631e03d32bSDmitry Safonov if (static_branch_unlikely(&tcp_md5_needed.key) && 23641e03d32bSDmitry Safonov rcu_access_pointer(tp->md5sig_info)) { 23651e03d32bSDmitry Safonov out->md5_key = tp->af_specific->md5_lookup(sk, sk); 23661e03d32bSDmitry Safonov if (out->md5_key) { 23671e03d32bSDmitry Safonov out->type = TCP_KEY_MD5; 23681e03d32bSDmitry Safonov return; 23691e03d32bSDmitry Safonov } 23701e03d32bSDmitry Safonov } 23711e03d32bSDmitry Safonov #endif 23721e03d32bSDmitry Safonov out->type = TCP_KEY_NONE; 23731e03d32bSDmitry Safonov } 23741e03d32bSDmitry Safonov 23751e03d32bSDmitry Safonov static inline bool tcp_key_is_md5(const struct tcp_key *key) 23761e03d32bSDmitry Safonov { 23771e03d32bSDmitry Safonov #ifdef CONFIG_TCP_MD5SIG 23781e03d32bSDmitry Safonov if (static_branch_unlikely(&tcp_md5_needed.key) && 23791e03d32bSDmitry Safonov key->type == TCP_KEY_MD5) 23801e03d32bSDmitry Safonov return true; 23811e03d32bSDmitry Safonov #endif 23821e03d32bSDmitry Safonov return false; 23831e03d32bSDmitry Safonov } 23841e03d32bSDmitry Safonov 23851e03d32bSDmitry Safonov static inline bool tcp_key_is_ao(const struct tcp_key *key) 23861e03d32bSDmitry Safonov { 23871e03d32bSDmitry Safonov #ifdef CONFIG_TCP_AO 238867fa83f7SDmitry Safonov if (static_branch_unlikely(&tcp_ao_needed.key) && 238967fa83f7SDmitry Safonov key->type == TCP_KEY_AO) 23901e03d32bSDmitry Safonov return true; 23911e03d32bSDmitry Safonov #endif 23921e03d32bSDmitry Safonov return false; 23931e03d32bSDmitry Safonov } 23941e03d32bSDmitry Safonov 23955c9f3023SJoe Perches int tcpv4_offload_init(void); 239628850dc7SDaniel Borkmann 23975c9f3023SJoe Perches void tcp_v4_init(void); 23985c9f3023SJoe Perches void tcp_init(void); 239920380731SArnaldo Carvalho de Melo 2400659a8ad5SYuchung Cheng /* tcp_recovery.c */ 2401d716bfdbSYuchung Cheng void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb); 24026ac06ecdSYuchung Cheng void tcp_newreno_mark_lost(struct sock *sk, bool snd_una_advanced); 2403b8fef65aSYuchung Cheng extern s32 tcp_rack_skb_timeout(struct tcp_sock *tp, struct sk_buff *skb, 2404b8fef65aSYuchung Cheng u32 reo_wnd); 240562d9f1a6SPengcheng Yang extern bool tcp_rack_mark_lost(struct sock *sk); 24061d0833dfSYuchung Cheng extern void tcp_rack_advance(struct tcp_sock *tp, u8 sacked, u32 end_seq, 24079a568de4SEric Dumazet u64 xmit_time); 240857dde7f7SYuchung Cheng extern void tcp_rack_reo_timeout(struct sock *sk); 24091f255691SPriyaranjan Jha extern void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs); 2410659a8ad5SYuchung Cheng 24111a91bb7cSMubashir Adnan Qureshi /* tcp_plb.c */ 24121a91bb7cSMubashir Adnan Qureshi 24131a91bb7cSMubashir Adnan Qureshi /* 24141a91bb7cSMubashir Adnan Qureshi * Scaling factor for fractions in PLB. For example, tcp_plb_update_state 24151a91bb7cSMubashir Adnan Qureshi * expects cong_ratio which represents fraction of traffic that experienced 24161a91bb7cSMubashir Adnan Qureshi * congestion over a single RTT. In order to avoid floating point operations, 24171a91bb7cSMubashir Adnan Qureshi * this fraction should be mapped to (1 << TCP_PLB_SCALE) and passed in. 24181a91bb7cSMubashir Adnan Qureshi */ 24191a91bb7cSMubashir Adnan Qureshi #define TCP_PLB_SCALE 8 24201a91bb7cSMubashir Adnan Qureshi 24211a91bb7cSMubashir Adnan Qureshi /* State for PLB (Protective Load Balancing) for a single TCP connection. */ 24221a91bb7cSMubashir Adnan Qureshi struct tcp_plb_state { 24231a91bb7cSMubashir Adnan Qureshi u8 consec_cong_rounds:5, /* consecutive congested rounds */ 24241a91bb7cSMubashir Adnan Qureshi unused:3; 24251a91bb7cSMubashir Adnan Qureshi u32 pause_until; /* jiffies32 when PLB can resume rerouting */ 24261a91bb7cSMubashir Adnan Qureshi }; 24271a91bb7cSMubashir Adnan Qureshi 24281a91bb7cSMubashir Adnan Qureshi static inline void tcp_plb_init(const struct sock *sk, 24291a91bb7cSMubashir Adnan Qureshi struct tcp_plb_state *plb) 24301a91bb7cSMubashir Adnan Qureshi { 24311a91bb7cSMubashir Adnan Qureshi plb->consec_cong_rounds = 0; 24321a91bb7cSMubashir Adnan Qureshi plb->pause_until = 0; 24331a91bb7cSMubashir Adnan Qureshi } 24341a91bb7cSMubashir Adnan Qureshi void tcp_plb_update_state(const struct sock *sk, struct tcp_plb_state *plb, 24351a91bb7cSMubashir Adnan Qureshi const int cong_ratio); 24361a91bb7cSMubashir Adnan Qureshi void tcp_plb_check_rehash(struct sock *sk, struct tcp_plb_state *plb); 24371a91bb7cSMubashir Adnan Qureshi void tcp_plb_update_state_upon_rto(struct sock *sk, struct tcp_plb_state *plb); 24381a91bb7cSMubashir Adnan Qureshi 2439e1a10ef7SNeal Cardwell /* At how many usecs into the future should the RTO fire? */ 2440e1a10ef7SNeal Cardwell static inline s64 tcp_rto_delta_us(const struct sock *sk) 2441e1a10ef7SNeal Cardwell { 244275c119afSEric Dumazet const struct sk_buff *skb = tcp_rtx_queue_head(sk); 2443e1a10ef7SNeal Cardwell u32 rto = inet_csk(sk)->icsk_rto; 24442fd66ffbSEric Dumazet u64 rto_time_stamp_us = tcp_skb_timestamp_us(skb) + jiffies_to_usecs(rto); 2445e1a10ef7SNeal Cardwell 2446e1a10ef7SNeal Cardwell return rto_time_stamp_us - tcp_sk(sk)->tcp_mstamp; 2447e1a10ef7SNeal Cardwell } 2448e1a10ef7SNeal Cardwell 2449e25f866fSCong Wang /* 2450e25f866fSCong Wang * Save and compile IPv4 options, return a pointer to it 2451e25f866fSCong Wang */ 245291ed1e66SPaolo Abeni static inline struct ip_options_rcu *tcp_v4_save_options(struct net *net, 245391ed1e66SPaolo Abeni struct sk_buff *skb) 2454e25f866fSCong Wang { 2455e25f866fSCong Wang const struct ip_options *opt = &TCP_SKB_CB(skb)->header.h4.opt; 2456e25f866fSCong Wang struct ip_options_rcu *dopt = NULL; 2457e25f866fSCong Wang 2458461b74c3SCong Wang if (opt->optlen) { 2459e25f866fSCong Wang int opt_size = sizeof(*dopt) + opt->optlen; 2460e25f866fSCong Wang 2461e25f866fSCong Wang dopt = kmalloc(opt_size, GFP_ATOMIC); 246291ed1e66SPaolo Abeni if (dopt && __ip_options_echo(net, &dopt->opt, skb, opt)) { 2463e25f866fSCong Wang kfree(dopt); 2464e25f866fSCong Wang dopt = NULL; 2465e25f866fSCong Wang } 2466e25f866fSCong Wang } 2467e25f866fSCong Wang return dopt; 2468e25f866fSCong Wang } 2469e25f866fSCong Wang 247098781965SEric Dumazet /* locally generated TCP pure ACKs have skb->truesize == 2 247198781965SEric Dumazet * (check tcp_send_ack() in net/ipv4/tcp_output.c ) 247298781965SEric Dumazet * This is much faster than dissecting the packet to find out. 247398781965SEric Dumazet * (Think of GRE encapsulations, IPv4, IPv6, ...) 247498781965SEric Dumazet */ 247598781965SEric Dumazet static inline bool skb_is_tcp_pure_ack(const struct sk_buff *skb) 247698781965SEric Dumazet { 247798781965SEric Dumazet return skb->truesize == 2; 247898781965SEric Dumazet } 247998781965SEric Dumazet 248098781965SEric Dumazet static inline void skb_set_tcp_pure_ack(struct sk_buff *skb) 248198781965SEric Dumazet { 248298781965SEric Dumazet skb->truesize = 2; 248398781965SEric Dumazet } 248498781965SEric Dumazet 2485473bd239STom Herbert static inline int tcp_inq(struct sock *sk) 2486473bd239STom Herbert { 2487473bd239STom Herbert struct tcp_sock *tp = tcp_sk(sk); 2488473bd239STom Herbert int answ; 2489473bd239STom Herbert 2490473bd239STom Herbert if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { 2491473bd239STom Herbert answ = 0; 2492473bd239STom Herbert } else if (sock_flag(sk, SOCK_URGINLINE) || 2493473bd239STom Herbert !tp->urg_data || 2494473bd239STom Herbert before(tp->urg_seq, tp->copied_seq) || 2495473bd239STom Herbert !before(tp->urg_seq, tp->rcv_nxt)) { 2496473bd239STom Herbert 2497473bd239STom Herbert answ = tp->rcv_nxt - tp->copied_seq; 2498473bd239STom Herbert 2499473bd239STom Herbert /* Subtract 1, if FIN was received */ 2500473bd239STom Herbert if (answ && sock_flag(sk, SOCK_DONE)) 2501473bd239STom Herbert answ--; 2502473bd239STom Herbert } else { 2503473bd239STom Herbert answ = tp->urg_seq - tp->copied_seq; 2504473bd239STom Herbert } 2505473bd239STom Herbert 2506473bd239STom Herbert return answ; 2507473bd239STom Herbert } 2508473bd239STom Herbert 250932035585STom Herbert int tcp_peek_len(struct socket *sock); 251032035585STom Herbert 2511a44d6eacSMartin KaFai Lau static inline void tcp_segs_in(struct tcp_sock *tp, const struct sk_buff *skb) 2512a44d6eacSMartin KaFai Lau { 2513a44d6eacSMartin KaFai Lau u16 segs_in; 2514a44d6eacSMartin KaFai Lau 2515a44d6eacSMartin KaFai Lau segs_in = max_t(u16, 1, skb_shinfo(skb)->gso_segs); 25160307a0b7SEric Dumazet 25170307a0b7SEric Dumazet /* We update these fields while other threads might 25180307a0b7SEric Dumazet * read them from tcp_get_info() 25190307a0b7SEric Dumazet */ 25200307a0b7SEric Dumazet WRITE_ONCE(tp->segs_in, tp->segs_in + segs_in); 2521a44d6eacSMartin KaFai Lau if (skb->len > tcp_hdrlen(skb)) 25220307a0b7SEric Dumazet WRITE_ONCE(tp->data_segs_in, tp->data_segs_in + segs_in); 2523a44d6eacSMartin KaFai Lau } 2524a44d6eacSMartin KaFai Lau 25259caad864SEric Dumazet /* 25269caad864SEric Dumazet * TCP listen path runs lockless. 25279caad864SEric Dumazet * We forced "struct sock" to be const qualified to make sure 25289caad864SEric Dumazet * we don't modify one of its field by mistake. 25299caad864SEric Dumazet * Here, we increment sk_drops which is an atomic_t, so we can safely 25309caad864SEric Dumazet * make sock writable again. 25319caad864SEric Dumazet */ 25329caad864SEric Dumazet static inline void tcp_listendrop(const struct sock *sk) 25339caad864SEric Dumazet { 25349caad864SEric Dumazet atomic_inc(&((struct sock *)sk)->sk_drops); 253502a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS); 25369caad864SEric Dumazet } 25379caad864SEric Dumazet 2538218af599SEric Dumazet enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer); 2539218af599SEric Dumazet 2540734942ccSDave Watson /* 2541734942ccSDave Watson * Interface for adding Upper Level Protocols over TCP 2542734942ccSDave Watson */ 2543734942ccSDave Watson 2544734942ccSDave Watson #define TCP_ULP_NAME_MAX 16 2545734942ccSDave Watson #define TCP_ULP_MAX 128 2546734942ccSDave Watson #define TCP_ULP_BUF_MAX (TCP_ULP_NAME_MAX*TCP_ULP_MAX) 2547734942ccSDave Watson 2548734942ccSDave Watson struct tcp_ulp_ops { 2549734942ccSDave Watson struct list_head list; 2550734942ccSDave Watson 2551734942ccSDave Watson /* initialize ulp */ 2552734942ccSDave Watson int (*init)(struct sock *sk); 255395fa1454SJohn Fastabend /* update ulp */ 255433bfe20dSJohn Fastabend void (*update)(struct sock *sk, struct proto *p, 255533bfe20dSJohn Fastabend void (*write_space)(struct sock *sk)); 2556734942ccSDave Watson /* cleanup ulp */ 2557734942ccSDave Watson void (*release)(struct sock *sk); 255861723b39SDavide Caratti /* diagnostic */ 2559b8adb69aSPaolo Abeni int (*get_info)(struct sock *sk, struct sk_buff *skb); 256061723b39SDavide Caratti size_t (*get_info_size)(const struct sock *sk); 256113230593SMat Martineau /* clone ulp */ 256213230593SMat Martineau void (*clone)(const struct request_sock *req, struct sock *newsk, 256313230593SMat Martineau const gfp_t priority); 2564734942ccSDave Watson 2565734942ccSDave Watson char name[TCP_ULP_NAME_MAX]; 2566734942ccSDave Watson struct module *owner; 2567734942ccSDave Watson }; 2568734942ccSDave Watson int tcp_register_ulp(struct tcp_ulp_ops *type); 2569734942ccSDave Watson void tcp_unregister_ulp(struct tcp_ulp_ops *type); 2570734942ccSDave Watson int tcp_set_ulp(struct sock *sk, const char *name); 2571734942ccSDave Watson void tcp_get_available_ulp(char *buf, size_t len); 2572734942ccSDave Watson void tcp_cleanup_ulp(struct sock *sk); 257333bfe20dSJohn Fastabend void tcp_update_ulp(struct sock *sk, struct proto *p, 257433bfe20dSJohn Fastabend void (*write_space)(struct sock *sk)); 2575734942ccSDave Watson 2576037b0b86SDaniel Borkmann #define MODULE_ALIAS_TCP_ULP(name) \ 2577037b0b86SDaniel Borkmann __MODULE_INFO(alias, alias_userspace, name); \ 2578037b0b86SDaniel Borkmann __MODULE_INFO(alias, alias_tcp_ulp, "tcp-ulp-" name) 2579037b0b86SDaniel Borkmann 258088759609SCong Wang #ifdef CONFIG_NET_SOCK_MSG 2581604326b4SDaniel Borkmann struct sk_msg; 2582604326b4SDaniel Borkmann struct sk_psock; 2583604326b4SDaniel Borkmann 258488759609SCong Wang #ifdef CONFIG_BPF_SYSCALL 258551e0158aSCong Wang int tcp_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore); 2586f747632bSLorenz Bauer void tcp_bpf_clone(const struct sock *sk, struct sock *newsk); 258788759609SCong Wang #endif /* CONFIG_BPF_SYSCALL */ 2588f747632bSLorenz Bauer 2589e5c6de5fSJohn Fastabend #ifdef CONFIG_INET 2590e5c6de5fSJohn Fastabend void tcp_eat_skb(struct sock *sk, struct sk_buff *skb); 2591e5c6de5fSJohn Fastabend #else 2592e5c6de5fSJohn Fastabend static inline void tcp_eat_skb(struct sock *sk, struct sk_buff *skb) 2593e5c6de5fSJohn Fastabend { 2594e5c6de5fSJohn Fastabend } 2595e5c6de5fSJohn Fastabend #endif 2596e5c6de5fSJohn Fastabend 2597a351d608SPengcheng Yang int tcp_bpf_sendmsg_redir(struct sock *sk, bool ingress, 2598a351d608SPengcheng Yang struct sk_msg *msg, u32 bytes, int flags); 25995da00404SLorenz Bauer #endif /* CONFIG_NET_SOCK_MSG */ 2600604326b4SDaniel Borkmann 260188759609SCong Wang #if !defined(CONFIG_BPF_SYSCALL) || !defined(CONFIG_NET_SOCK_MSG) 260288759609SCong Wang static inline void tcp_bpf_clone(const struct sock *sk, struct sock *newsk) 260388759609SCong Wang { 260488759609SCong Wang } 260588759609SCong Wang #endif 260688759609SCong Wang 26070813a841SMartin KaFai Lau #ifdef CONFIG_CGROUP_BPF 26080813a841SMartin KaFai Lau static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops, 26090813a841SMartin KaFai Lau struct sk_buff *skb, 26100813a841SMartin KaFai Lau unsigned int end_offset) 26110813a841SMartin KaFai Lau { 26120813a841SMartin KaFai Lau skops->skb = skb; 26130813a841SMartin KaFai Lau skops->skb_data_end = skb->data + end_offset; 26140813a841SMartin KaFai Lau } 26150813a841SMartin KaFai Lau #else 26160813a841SMartin KaFai Lau static inline void bpf_skops_init_skb(struct bpf_sock_ops_kern *skops, 26170813a841SMartin KaFai Lau struct sk_buff *skb, 26180813a841SMartin KaFai Lau unsigned int end_offset) 26190813a841SMartin KaFai Lau { 26200813a841SMartin KaFai Lau } 26210813a841SMartin KaFai Lau #endif 26220813a841SMartin KaFai Lau 262340304b2aSLawrence Brakmo /* Call BPF_SOCK_OPS program that returns an int. If the return value 262440304b2aSLawrence Brakmo * is < 0, then the BPF op failed (for example if the loaded BPF 262540304b2aSLawrence Brakmo * program does not support the chosen operation or there is no BPF 262640304b2aSLawrence Brakmo * program loaded). 262740304b2aSLawrence Brakmo */ 262840304b2aSLawrence Brakmo #ifdef CONFIG_BPF 2629de525be2SLawrence Brakmo static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args) 263040304b2aSLawrence Brakmo { 263140304b2aSLawrence Brakmo struct bpf_sock_ops_kern sock_ops; 263240304b2aSLawrence Brakmo int ret; 263340304b2aSLawrence Brakmo 2634b73042b8SLawrence Brakmo memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); 2635f19397a5SLawrence Brakmo if (sk_fullsock(sk)) { 2636f19397a5SLawrence Brakmo sock_ops.is_fullsock = 1; 263740304b2aSLawrence Brakmo sock_owned_by_me(sk); 2638f19397a5SLawrence Brakmo } 263940304b2aSLawrence Brakmo 264040304b2aSLawrence Brakmo sock_ops.sk = sk; 264140304b2aSLawrence Brakmo sock_ops.op = op; 2642de525be2SLawrence Brakmo if (nargs > 0) 2643de525be2SLawrence Brakmo memcpy(sock_ops.args, args, nargs * sizeof(*args)); 264440304b2aSLawrence Brakmo 264540304b2aSLawrence Brakmo ret = BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops); 264640304b2aSLawrence Brakmo if (ret == 0) 264740304b2aSLawrence Brakmo ret = sock_ops.reply; 264840304b2aSLawrence Brakmo else 264940304b2aSLawrence Brakmo ret = -1; 265040304b2aSLawrence Brakmo return ret; 265140304b2aSLawrence Brakmo } 2652de525be2SLawrence Brakmo 2653de525be2SLawrence Brakmo static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2) 2654de525be2SLawrence Brakmo { 2655de525be2SLawrence Brakmo u32 args[2] = {arg1, arg2}; 2656de525be2SLawrence Brakmo 2657de525be2SLawrence Brakmo return tcp_call_bpf(sk, op, 2, args); 2658de525be2SLawrence Brakmo } 2659de525be2SLawrence Brakmo 2660de525be2SLawrence Brakmo static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2, 2661de525be2SLawrence Brakmo u32 arg3) 2662de525be2SLawrence Brakmo { 2663de525be2SLawrence Brakmo u32 args[3] = {arg1, arg2, arg3}; 2664de525be2SLawrence Brakmo 2665de525be2SLawrence Brakmo return tcp_call_bpf(sk, op, 3, args); 2666de525be2SLawrence Brakmo } 2667de525be2SLawrence Brakmo 266840304b2aSLawrence Brakmo #else 2669de525be2SLawrence Brakmo static inline int tcp_call_bpf(struct sock *sk, int op, u32 nargs, u32 *args) 267040304b2aSLawrence Brakmo { 267140304b2aSLawrence Brakmo return -EPERM; 267240304b2aSLawrence Brakmo } 2673de525be2SLawrence Brakmo 2674de525be2SLawrence Brakmo static inline int tcp_call_bpf_2arg(struct sock *sk, int op, u32 arg1, u32 arg2) 2675de525be2SLawrence Brakmo { 2676de525be2SLawrence Brakmo return -EPERM; 2677de525be2SLawrence Brakmo } 2678de525be2SLawrence Brakmo 2679de525be2SLawrence Brakmo static inline int tcp_call_bpf_3arg(struct sock *sk, int op, u32 arg1, u32 arg2, 2680de525be2SLawrence Brakmo u32 arg3) 2681de525be2SLawrence Brakmo { 2682de525be2SLawrence Brakmo return -EPERM; 2683de525be2SLawrence Brakmo } 2684de525be2SLawrence Brakmo 268540304b2aSLawrence Brakmo #endif 268640304b2aSLawrence Brakmo 26878550f328SLawrence Brakmo static inline u32 tcp_timeout_init(struct sock *sk) 26888550f328SLawrence Brakmo { 26898550f328SLawrence Brakmo int timeout; 26908550f328SLawrence Brakmo 2691de525be2SLawrence Brakmo timeout = tcp_call_bpf(sk, BPF_SOCK_OPS_TIMEOUT_INIT, 0, NULL); 26928550f328SLawrence Brakmo 26938550f328SLawrence Brakmo if (timeout <= 0) 26948550f328SLawrence Brakmo timeout = TCP_TIMEOUT_INIT; 26955903123fSAkhmat Karakotov return min_t(int, timeout, TCP_RTO_MAX); 26968550f328SLawrence Brakmo } 26978550f328SLawrence Brakmo 269813d3b1ebSLawrence Brakmo static inline u32 tcp_rwnd_init_bpf(struct sock *sk) 269913d3b1ebSLawrence Brakmo { 270013d3b1ebSLawrence Brakmo int rwnd; 270113d3b1ebSLawrence Brakmo 2702de525be2SLawrence Brakmo rwnd = tcp_call_bpf(sk, BPF_SOCK_OPS_RWND_INIT, 0, NULL); 270313d3b1ebSLawrence Brakmo 270413d3b1ebSLawrence Brakmo if (rwnd < 0) 270513d3b1ebSLawrence Brakmo rwnd = 0; 270613d3b1ebSLawrence Brakmo return rwnd; 270713d3b1ebSLawrence Brakmo } 270891b5b21cSLawrence Brakmo 270991b5b21cSLawrence Brakmo static inline bool tcp_bpf_ca_needs_ecn(struct sock *sk) 271091b5b21cSLawrence Brakmo { 2711de525be2SLawrence Brakmo return (tcp_call_bpf(sk, BPF_SOCK_OPS_NEEDS_ECN, 0, NULL) == 1); 271291b5b21cSLawrence Brakmo } 271360e2a778SUrsula Braun 271423729ff2SStanislav Fomichev static inline void tcp_bpf_rtt(struct sock *sk) 271523729ff2SStanislav Fomichev { 2716bef8e263SArnd Bergmann if (BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), BPF_SOCK_OPS_RTT_CB_FLAG)) 271723729ff2SStanislav Fomichev tcp_call_bpf(sk, BPF_SOCK_OPS_RTT_CB, 0, NULL); 271823729ff2SStanislav Fomichev } 271923729ff2SStanislav Fomichev 272060e2a778SUrsula Braun #if IS_ENABLED(CONFIG_SMC) 272160e2a778SUrsula Braun extern struct static_key_false tcp_have_smc; 272260e2a778SUrsula Braun #endif 27236dac1523SIlya Lesokhin 27246dac1523SIlya Lesokhin #if IS_ENABLED(CONFIG_TLS_DEVICE) 27256dac1523SIlya Lesokhin void clean_acked_data_enable(struct inet_connection_sock *icsk, 27266dac1523SIlya Lesokhin void (*cad)(struct sock *sk, u32 ack_seq)); 27276dac1523SIlya Lesokhin void clean_acked_data_disable(struct inet_connection_sock *icsk); 2728494bc1d2SJakub Kicinski void clean_acked_data_flush(void); 27296dac1523SIlya Lesokhin #endif 27306dac1523SIlya Lesokhin 2731a842fe14SEric Dumazet DECLARE_STATIC_KEY_FALSE(tcp_tx_delay_enabled); 2732a842fe14SEric Dumazet static inline void tcp_add_tx_delay(struct sk_buff *skb, 2733a842fe14SEric Dumazet const struct tcp_sock *tp) 2734a842fe14SEric Dumazet { 2735a842fe14SEric Dumazet if (static_branch_unlikely(&tcp_tx_delay_enabled)) 2736a842fe14SEric Dumazet skb->skb_mstamp_ns += (u64)tp->tcp_tx_delay * NSEC_PER_USEC; 2737a842fe14SEric Dumazet } 2738a842fe14SEric Dumazet 2739d6fb396cSEric Dumazet /* Compute Earliest Departure Time for some control packets 2740d6fb396cSEric Dumazet * like ACK or RST for TIME_WAIT or non ESTABLISHED sockets. 2741d6fb396cSEric Dumazet */ 2742d6fb396cSEric Dumazet static inline u64 tcp_transmit_time(const struct sock *sk) 2743a842fe14SEric Dumazet { 2744a842fe14SEric Dumazet if (static_branch_unlikely(&tcp_tx_delay_enabled)) { 2745a842fe14SEric Dumazet u32 delay = (sk->sk_state == TCP_TIME_WAIT) ? 2746a842fe14SEric Dumazet tcp_twsk(sk)->tw_tx_delay : tcp_sk(sk)->tcp_tx_delay; 2747a842fe14SEric Dumazet 2748d6fb396cSEric Dumazet return tcp_clock_ns() + (u64)delay * NSEC_PER_USEC; 2749a842fe14SEric Dumazet } 2750d6fb396cSEric Dumazet return 0; 2751a842fe14SEric Dumazet } 2752a842fe14SEric Dumazet 2753f7dca36fSDmitry Safonov static inline int tcp_parse_auth_options(const struct tcphdr *th, 2754f7dca36fSDmitry Safonov const u8 **md5_hash, const struct tcp_ao_hdr **aoh) 2755f7dca36fSDmitry Safonov { 2756f7dca36fSDmitry Safonov const u8 *md5_tmp, *ao_tmp; 2757f7dca36fSDmitry Safonov int ret; 2758f7dca36fSDmitry Safonov 2759f7dca36fSDmitry Safonov ret = tcp_do_parse_auth_options(th, &md5_tmp, &ao_tmp); 2760f7dca36fSDmitry Safonov if (ret) 2761f7dca36fSDmitry Safonov return ret; 2762f7dca36fSDmitry Safonov 2763f7dca36fSDmitry Safonov if (md5_hash) 2764f7dca36fSDmitry Safonov *md5_hash = md5_tmp; 2765f7dca36fSDmitry Safonov 2766f7dca36fSDmitry Safonov if (aoh) { 2767f7dca36fSDmitry Safonov if (!ao_tmp) 2768f7dca36fSDmitry Safonov *aoh = NULL; 2769f7dca36fSDmitry Safonov else 2770f7dca36fSDmitry Safonov *aoh = (struct tcp_ao_hdr *)(ao_tmp - 2); 2771f7dca36fSDmitry Safonov } 2772f7dca36fSDmitry Safonov 2773f7dca36fSDmitry Safonov return 0; 2774f7dca36fSDmitry Safonov } 2775f7dca36fSDmitry Safonov 27760aadc739SDmitry Safonov static inline bool tcp_ao_required(struct sock *sk, const void *saddr, 2777248411b8SDmitry Safonov int family, int l3index, bool stat_inc) 27780aadc739SDmitry Safonov { 27790aadc739SDmitry Safonov #ifdef CONFIG_TCP_AO 27800aadc739SDmitry Safonov struct tcp_ao_info *ao_info; 27810aadc739SDmitry Safonov struct tcp_ao_key *ao_key; 27820aadc739SDmitry Safonov 278367fa83f7SDmitry Safonov if (!static_branch_unlikely(&tcp_ao_needed.key)) 278467fa83f7SDmitry Safonov return false; 278567fa83f7SDmitry Safonov 27860aadc739SDmitry Safonov ao_info = rcu_dereference_check(tcp_sk(sk)->ao_info, 27870aadc739SDmitry Safonov lockdep_sock_is_held(sk)); 27880aadc739SDmitry Safonov if (!ao_info) 27890aadc739SDmitry Safonov return false; 27900aadc739SDmitry Safonov 2791248411b8SDmitry Safonov ao_key = tcp_ao_do_lookup(sk, l3index, saddr, family, -1, -1); 2792af09a341SDmitry Safonov if (ao_info->ao_required || ao_key) { 2793af09a341SDmitry Safonov if (stat_inc) { 2794af09a341SDmitry Safonov NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOREQUIRED); 2795af09a341SDmitry Safonov atomic64_inc(&ao_info->counters.ao_required); 2796af09a341SDmitry Safonov } 27970aadc739SDmitry Safonov return true; 2798af09a341SDmitry Safonov } 27990aadc739SDmitry Safonov #endif 28000aadc739SDmitry Safonov return false; 28010aadc739SDmitry Safonov } 28020aadc739SDmitry Safonov 28030a3a8090SDmitry Safonov /* Called with rcu_read_lock() */ 28040a3a8090SDmitry Safonov static inline enum skb_drop_reason 28050a3a8090SDmitry Safonov tcp_inbound_hash(struct sock *sk, const struct request_sock *req, 28060a3a8090SDmitry Safonov const struct sk_buff *skb, 28070a3a8090SDmitry Safonov const void *saddr, const void *daddr, 28080a3a8090SDmitry Safonov int family, int dif, int sdif) 28090a3a8090SDmitry Safonov { 28100a3a8090SDmitry Safonov const struct tcphdr *th = tcp_hdr(skb); 28110a3a8090SDmitry Safonov const struct tcp_ao_hdr *aoh; 28120a3a8090SDmitry Safonov const __u8 *md5_location; 28130a3a8090SDmitry Safonov int l3index; 28140a3a8090SDmitry Safonov 28150a3a8090SDmitry Safonov /* Invalid option or two times meet any of auth options */ 28162717b5adSDmitry Safonov if (tcp_parse_auth_options(th, &md5_location, &aoh)) { 28172717b5adSDmitry Safonov tcp_hash_fail("TCP segment has incorrect auth options set", 28182717b5adSDmitry Safonov family, skb, ""); 28190a3a8090SDmitry Safonov return SKB_DROP_REASON_TCP_AUTH_HDR; 28202717b5adSDmitry Safonov } 28210a3a8090SDmitry Safonov 28220a3a8090SDmitry Safonov if (req) { 2823af09a341SDmitry Safonov if (tcp_rsk_used_ao(req) != !!aoh) { 2824af09a341SDmitry Safonov NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD); 28252717b5adSDmitry Safonov tcp_hash_fail("TCP connection can't start/end using TCP-AO", 28262717b5adSDmitry Safonov family, skb, "%s", 28272717b5adSDmitry Safonov !aoh ? "missing AO" : "AO signed"); 28280a3a8090SDmitry Safonov return SKB_DROP_REASON_TCP_AOFAILURE; 28290a3a8090SDmitry Safonov } 2830af09a341SDmitry Safonov } 28310a3a8090SDmitry Safonov 28320a3a8090SDmitry Safonov /* sdif set, means packet ingressed via a device 28330a3a8090SDmitry Safonov * in an L3 domain and dif is set to the l3mdev 28340a3a8090SDmitry Safonov */ 28350a3a8090SDmitry Safonov l3index = sdif ? dif : 0; 28360a3a8090SDmitry Safonov 28370a3a8090SDmitry Safonov /* Fast path: unsigned segments */ 28380a3a8090SDmitry Safonov if (likely(!md5_location && !aoh)) { 28390a3a8090SDmitry Safonov /* Drop if there's TCP-MD5 or TCP-AO key with any rcvid/sndid 28400a3a8090SDmitry Safonov * for the remote peer. On TCP-AO established connection 28410a3a8090SDmitry Safonov * the last key is impossible to remove, so there's 28420a3a8090SDmitry Safonov * always at least one current_key. 28430a3a8090SDmitry Safonov */ 2844248411b8SDmitry Safonov if (tcp_ao_required(sk, saddr, family, l3index, true)) { 28452717b5adSDmitry Safonov tcp_hash_fail("AO hash is required, but not found", 28462717b5adSDmitry Safonov family, skb, "L3 index %d", l3index); 28470a3a8090SDmitry Safonov return SKB_DROP_REASON_TCP_AONOTFOUND; 28482717b5adSDmitry Safonov } 28490a3a8090SDmitry Safonov if (unlikely(tcp_md5_do_lookup(sk, l3index, saddr, family))) { 28500a3a8090SDmitry Safonov NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND); 2851248411b8SDmitry Safonov tcp_hash_fail("MD5 Hash not found", 2852248411b8SDmitry Safonov family, skb, "L3 index %d", l3index); 28530a3a8090SDmitry Safonov return SKB_DROP_REASON_TCP_MD5NOTFOUND; 28540a3a8090SDmitry Safonov } 28550a3a8090SDmitry Safonov return SKB_NOT_DROPPED_YET; 28560a3a8090SDmitry Safonov } 28570a3a8090SDmitry Safonov 28580a3a8090SDmitry Safonov if (aoh) 2859248411b8SDmitry Safonov return tcp_inbound_ao_hash(sk, skb, family, req, l3index, aoh); 28600a3a8090SDmitry Safonov 28610a3a8090SDmitry Safonov return tcp_inbound_md5_hash(sk, skb, saddr, daddr, family, 28620a3a8090SDmitry Safonov l3index, md5_location); 28630a3a8090SDmitry Safonov } 28640a3a8090SDmitry Safonov 28651da177e4SLinus Torvalds #endif /* _TCP_H */ 2866