11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Implementation of the Transmission Control Protocol(TCP). 71da177e4SLinus Torvalds * 802c30a84SJesper Juhl * Authors: Ross Biro 91da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 101da177e4SLinus Torvalds * Mark Evans, <evansmp@uhura.aston.ac.uk> 111da177e4SLinus Torvalds * Corey Minyard <wf-rch!minyard@relay.EU.net> 121da177e4SLinus Torvalds * Florian La Roche, <flla@stud.uni-sb.de> 131da177e4SLinus Torvalds * Charles Hedrick, <hedrick@klinzhai.rutgers.edu> 141da177e4SLinus Torvalds * Linus Torvalds, <torvalds@cs.helsinki.fi> 151da177e4SLinus Torvalds * Alan Cox, <gw4pts@gw4pts.ampr.org> 161da177e4SLinus Torvalds * Matthew Dillon, <dillon@apollo.west.oic.com> 171da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 181da177e4SLinus Torvalds * Jorge Cwik, <jorge@laser.satlink.net> 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds /* 221da177e4SLinus Torvalds * Changes: 231da177e4SLinus Torvalds * Pedro Roque : Fast Retransmit/Recovery. 241da177e4SLinus Torvalds * Two receive queues. 251da177e4SLinus Torvalds * Retransmit queue handled by TCP. 261da177e4SLinus Torvalds * Better retransmit timer handling. 271da177e4SLinus Torvalds * New congestion avoidance. 281da177e4SLinus Torvalds * Header prediction. 291da177e4SLinus Torvalds * Variable renaming. 301da177e4SLinus Torvalds * 311da177e4SLinus Torvalds * Eric : Fast Retransmit. 321da177e4SLinus Torvalds * Randy Scott : MSS option defines. 331da177e4SLinus Torvalds * Eric Schenk : Fixes to slow start algorithm. 341da177e4SLinus Torvalds * Eric Schenk : Yet another double ACK bug. 351da177e4SLinus Torvalds * Eric Schenk : Delayed ACK bug fixes. 361da177e4SLinus Torvalds * Eric Schenk : Floyd style fast retrans war avoidance. 371da177e4SLinus Torvalds * David S. Miller : Don't allow zero congestion window. 381da177e4SLinus Torvalds * Eric Schenk : Fix retransmitter so that it sends 391da177e4SLinus Torvalds * next packet on ack of previous packet. 401da177e4SLinus Torvalds * Andi Kleen : Moved open_request checking here 411da177e4SLinus Torvalds * and process RSTs for open_requests. 421da177e4SLinus Torvalds * Andi Kleen : Better prune_queue, and other fixes. 43caa20d9aSStephen Hemminger * Andrey Savochkin: Fix RTT measurements in the presence of 441da177e4SLinus Torvalds * timestamps. 451da177e4SLinus Torvalds * Andrey Savochkin: Check sequence numbers correctly when 461da177e4SLinus Torvalds * removing SACKs due to in sequence incoming 471da177e4SLinus Torvalds * data segments. 481da177e4SLinus Torvalds * Andi Kleen: Make sure we never ack data there is not 491da177e4SLinus Torvalds * enough room for. Also make this condition 501da177e4SLinus Torvalds * a fatal error if it might still happen. 511da177e4SLinus Torvalds * Andi Kleen: Add tcp_measure_rcv_mss to make 521da177e4SLinus Torvalds * connections with MSS<min(MTU,ann. MSS) 531da177e4SLinus Torvalds * work without delayed acks. 541da177e4SLinus Torvalds * Andi Kleen: Process packets with PSH set in the 551da177e4SLinus Torvalds * fast path. 561da177e4SLinus Torvalds * J Hadi Salim: ECN support 571da177e4SLinus Torvalds * Andrei Gurtov, 581da177e4SLinus Torvalds * Pasi Sarolahti, 591da177e4SLinus Torvalds * Panu Kuhlberg: Experimental audit of TCP (re)transmission 601da177e4SLinus Torvalds * engine. Lots of bugs are found. 611da177e4SLinus Torvalds * Pasi Sarolahti: F-RTO for dealing with spurious RTOs 621da177e4SLinus Torvalds */ 631da177e4SLinus Torvalds 64afd46503SJoe Perches #define pr_fmt(fmt) "TCP: " fmt 65afd46503SJoe Perches 661da177e4SLinus Torvalds #include <linux/mm.h> 675a0e3ad6STejun Heo #include <linux/slab.h> 681da177e4SLinus Torvalds #include <linux/module.h> 691da177e4SLinus Torvalds #include <linux/sysctl.h> 70a0bffffcSIlpo Järvinen #include <linux/kernel.h> 71ad971f61SEric Dumazet #include <linux/prefetch.h> 725ffc02a1SSatoru SATOH #include <net/dst.h> 731da177e4SLinus Torvalds #include <net/tcp.h> 741da177e4SLinus Torvalds #include <net/inet_common.h> 751da177e4SLinus Torvalds #include <linux/ipsec.h> 761da177e4SLinus Torvalds #include <asm/unaligned.h> 77e1c8a607SWillem de Bruijn #include <linux/errqueue.h> 781da177e4SLinus Torvalds 79ab32ea5dSBrian Haley int sysctl_tcp_timestamps __read_mostly = 1; 80ab32ea5dSBrian Haley int sysctl_tcp_window_scaling __read_mostly = 1; 81ab32ea5dSBrian Haley int sysctl_tcp_sack __read_mostly = 1; 82ab32ea5dSBrian Haley int sysctl_tcp_fack __read_mostly = 1; 83ab32ea5dSBrian Haley int sysctl_tcp_reordering __read_mostly = TCP_FASTRETRANS_THRESH; 84dca145ffSEric Dumazet int sysctl_tcp_max_reordering __read_mostly = 300; 854bc2f18bSEric Dumazet EXPORT_SYMBOL(sysctl_tcp_reordering); 86ab32ea5dSBrian Haley int sysctl_tcp_dsack __read_mostly = 1; 87ab32ea5dSBrian Haley int sysctl_tcp_app_win __read_mostly = 31; 88b49960a0SEric Dumazet int sysctl_tcp_adv_win_scale __read_mostly = 1; 894bc2f18bSEric Dumazet EXPORT_SYMBOL(sysctl_tcp_adv_win_scale); 901da177e4SLinus Torvalds 91282f23c6SEric Dumazet /* rfc5961 challenge ack rate limiting */ 92282f23c6SEric Dumazet int sysctl_tcp_challenge_ack_limit = 100; 93282f23c6SEric Dumazet 94ab32ea5dSBrian Haley int sysctl_tcp_stdurg __read_mostly; 95ab32ea5dSBrian Haley int sysctl_tcp_rfc1337 __read_mostly; 96ab32ea5dSBrian Haley int sysctl_tcp_max_orphans __read_mostly = NR_FILE; 97c96fd3d4SIlpo Järvinen int sysctl_tcp_frto __read_mostly = 2; 981da177e4SLinus Torvalds 997e380175SAndreas Petlund int sysctl_tcp_thin_dupack __read_mostly; 1007e380175SAndreas Petlund 101ab32ea5dSBrian Haley int sysctl_tcp_moderate_rcvbuf __read_mostly = 1; 1026ba8a3b1SNandita Dukkipati int sysctl_tcp_early_retrans __read_mostly = 3; 103*032ee423SNeal Cardwell int sysctl_tcp_invalid_ratelimit __read_mostly = HZ/2; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds #define FLAG_DATA 0x01 /* Incoming frame contained data. */ 1061da177e4SLinus Torvalds #define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */ 1071da177e4SLinus Torvalds #define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */ 1081da177e4SLinus Torvalds #define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */ 1091da177e4SLinus Torvalds #define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */ 1101da177e4SLinus Torvalds #define FLAG_DATA_SACKED 0x20 /* New SACK. */ 1111da177e4SLinus Torvalds #define FLAG_ECE 0x40 /* ECE in this ACK */ 1121da177e4SLinus Torvalds #define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/ 113e33099f9SYuchung Cheng #define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */ 1142e605294SIlpo Järvinen #define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */ 115564262c1SRyousei Takano #define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */ 116cadbd031SIlpo Järvinen #define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */ 11712fb3dd9SEric Dumazet #define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */ 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds #define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED) 1201da177e4SLinus Torvalds #define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED) 1211da177e4SLinus Torvalds #define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE) 1221da177e4SLinus Torvalds #define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED) 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH) 125bdf1ee5dSIlpo Järvinen #define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH)) 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /* Adapt the MSS value used to make delayed ack decision to the 1281da177e4SLinus Torvalds * real world. 1291da177e4SLinus Torvalds */ 130056834d9SIlpo Järvinen static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb) 1311da177e4SLinus Torvalds { 132463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 133463c84b9SArnaldo Carvalho de Melo const unsigned int lss = icsk->icsk_ack.last_seg_size; 134463c84b9SArnaldo Carvalho de Melo unsigned int len; 1351da177e4SLinus Torvalds 136463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.last_seg_size = 0; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* skb->len may jitter because of SACKs, even if peer 1391da177e4SLinus Torvalds * sends good full-sized frames. 1401da177e4SLinus Torvalds */ 141ff9b5e0fSHerbert Xu len = skb_shinfo(skb)->gso_size ? : skb->len; 142463c84b9SArnaldo Carvalho de Melo if (len >= icsk->icsk_ack.rcv_mss) { 143463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.rcv_mss = len; 1441da177e4SLinus Torvalds } else { 1451da177e4SLinus Torvalds /* Otherwise, we make more careful check taking into account, 1461da177e4SLinus Torvalds * that SACKs block is variable. 1471da177e4SLinus Torvalds * 1481da177e4SLinus Torvalds * "len" is invariant segment length, including TCP header. 1491da177e4SLinus Torvalds */ 1509c70220bSArnaldo Carvalho de Melo len += skb->data - skb_transport_header(skb); 151bee7ca9eSWilliam Allen Simpson if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) || 1521da177e4SLinus Torvalds /* If PSH is not set, packet should be 1531da177e4SLinus Torvalds * full sized, provided peer TCP is not badly broken. 1541da177e4SLinus Torvalds * This observation (if it is correct 8)) allows 1551da177e4SLinus Torvalds * to handle super-low mtu links fairly. 1561da177e4SLinus Torvalds */ 1571da177e4SLinus Torvalds (len >= TCP_MIN_MSS + sizeof(struct tcphdr) && 158aa8223c7SArnaldo Carvalho de Melo !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) { 1591da177e4SLinus Torvalds /* Subtract also invariant (if peer is RFC compliant), 1601da177e4SLinus Torvalds * tcp header plus fixed timestamp option length. 1611da177e4SLinus Torvalds * Resulting "len" is MSS free of SACK jitter. 1621da177e4SLinus Torvalds */ 163463c84b9SArnaldo Carvalho de Melo len -= tcp_sk(sk)->tcp_header_len; 164463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.last_seg_size = len; 1651da177e4SLinus Torvalds if (len == lss) { 166463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.rcv_mss = len; 1671da177e4SLinus Torvalds return; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds } 1701ef9696cSAlexey Kuznetsov if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED) 1711ef9696cSAlexey Kuznetsov icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2; 172463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.pending |= ICSK_ACK_PUSHED; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 176463c84b9SArnaldo Carvalho de Melo static void tcp_incr_quickack(struct sock *sk) 1771da177e4SLinus Torvalds { 178463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 17995c96174SEric Dumazet unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss); 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds if (quickacks == 0) 1821da177e4SLinus Torvalds quickacks = 2; 183463c84b9SArnaldo Carvalho de Melo if (quickacks > icsk->icsk_ack.quick) 184463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871b9f4092Sstephen hemminger static void tcp_enter_quickack_mode(struct sock *sk) 1881da177e4SLinus Torvalds { 189463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 190463c84b9SArnaldo Carvalho de Melo tcp_incr_quickack(sk); 191463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.pingpong = 0; 192463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* Send ACKs quickly, if "quick" count is not exhausted 1961da177e4SLinus Torvalds * and the session is not interactive. 1971da177e4SLinus Torvalds */ 1981da177e4SLinus Torvalds 199a2a385d6SEric Dumazet static inline bool tcp_in_quickack_mode(const struct sock *sk) 2001da177e4SLinus Torvalds { 201463c84b9SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 202a2a385d6SEric Dumazet 203463c84b9SArnaldo Carvalho de Melo return icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong; 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 206735d3831SFlorian Westphal static void tcp_ecn_queue_cwr(struct tcp_sock *tp) 207bdf1ee5dSIlpo Järvinen { 208bdf1ee5dSIlpo Järvinen if (tp->ecn_flags & TCP_ECN_OK) 209bdf1ee5dSIlpo Järvinen tp->ecn_flags |= TCP_ECN_QUEUE_CWR; 210bdf1ee5dSIlpo Järvinen } 211bdf1ee5dSIlpo Järvinen 212735d3831SFlorian Westphal static void tcp_ecn_accept_cwr(struct tcp_sock *tp, const struct sk_buff *skb) 213bdf1ee5dSIlpo Järvinen { 214bdf1ee5dSIlpo Järvinen if (tcp_hdr(skb)->cwr) 215bdf1ee5dSIlpo Järvinen tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR; 216bdf1ee5dSIlpo Järvinen } 217bdf1ee5dSIlpo Järvinen 218735d3831SFlorian Westphal static void tcp_ecn_withdraw_cwr(struct tcp_sock *tp) 219bdf1ee5dSIlpo Järvinen { 220bdf1ee5dSIlpo Järvinen tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR; 221bdf1ee5dSIlpo Järvinen } 222bdf1ee5dSIlpo Järvinen 223735d3831SFlorian Westphal static void __tcp_ecn_check_ce(struct tcp_sock *tp, const struct sk_buff *skb) 224bdf1ee5dSIlpo Järvinen { 225b82d1bb4SEric Dumazet switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) { 2267a269ffaSEric Dumazet case INET_ECN_NOT_ECT: 227bdf1ee5dSIlpo Järvinen /* Funny extension: if ECT is not set on a segment, 2287a269ffaSEric Dumazet * and we already seen ECT on a previous segment, 2297a269ffaSEric Dumazet * it is probably a retransmit. 2307a269ffaSEric Dumazet */ 2317a269ffaSEric Dumazet if (tp->ecn_flags & TCP_ECN_SEEN) 232bdf1ee5dSIlpo Järvinen tcp_enter_quickack_mode((struct sock *)tp); 2337a269ffaSEric Dumazet break; 2347a269ffaSEric Dumazet case INET_ECN_CE: 2359890092eSFlorian Westphal if (tcp_ca_needs_ecn((struct sock *)tp)) 2369890092eSFlorian Westphal tcp_ca_event((struct sock *)tp, CA_EVENT_ECN_IS_CE); 2379890092eSFlorian Westphal 238aae06bf5SEric Dumazet if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) { 239aae06bf5SEric Dumazet /* Better not delay acks, sender can have a very low cwnd */ 240aae06bf5SEric Dumazet tcp_enter_quickack_mode((struct sock *)tp); 2417a269ffaSEric Dumazet tp->ecn_flags |= TCP_ECN_DEMAND_CWR; 242aae06bf5SEric Dumazet } 2437a269ffaSEric Dumazet tp->ecn_flags |= TCP_ECN_SEEN; 2449890092eSFlorian Westphal break; 2459890092eSFlorian Westphal default: 2469890092eSFlorian Westphal if (tcp_ca_needs_ecn((struct sock *)tp)) 2479890092eSFlorian Westphal tcp_ca_event((struct sock *)tp, CA_EVENT_ECN_NO_CE); 2489890092eSFlorian Westphal tp->ecn_flags |= TCP_ECN_SEEN; 2499890092eSFlorian Westphal break; 250bdf1ee5dSIlpo Järvinen } 251bdf1ee5dSIlpo Järvinen } 252bdf1ee5dSIlpo Järvinen 253735d3831SFlorian Westphal static void tcp_ecn_check_ce(struct tcp_sock *tp, const struct sk_buff *skb) 254735d3831SFlorian Westphal { 255735d3831SFlorian Westphal if (tp->ecn_flags & TCP_ECN_OK) 256735d3831SFlorian Westphal __tcp_ecn_check_ce(tp, skb); 257735d3831SFlorian Westphal } 258735d3831SFlorian Westphal 259735d3831SFlorian Westphal static void tcp_ecn_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th) 260bdf1ee5dSIlpo Järvinen { 261bdf1ee5dSIlpo Järvinen if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr)) 262bdf1ee5dSIlpo Järvinen tp->ecn_flags &= ~TCP_ECN_OK; 263bdf1ee5dSIlpo Järvinen } 264bdf1ee5dSIlpo Järvinen 265735d3831SFlorian Westphal static void tcp_ecn_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th) 266bdf1ee5dSIlpo Järvinen { 267bdf1ee5dSIlpo Järvinen if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr)) 268bdf1ee5dSIlpo Järvinen tp->ecn_flags &= ~TCP_ECN_OK; 269bdf1ee5dSIlpo Järvinen } 270bdf1ee5dSIlpo Järvinen 271735d3831SFlorian Westphal static bool tcp_ecn_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th) 272bdf1ee5dSIlpo Järvinen { 273bdf1ee5dSIlpo Järvinen if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK)) 274a2a385d6SEric Dumazet return true; 275a2a385d6SEric Dumazet return false; 276bdf1ee5dSIlpo Järvinen } 277bdf1ee5dSIlpo Järvinen 2781da177e4SLinus Torvalds /* Buffer size and advertised window tuning. 2791da177e4SLinus Torvalds * 2801da177e4SLinus Torvalds * 1. Tuning sk->sk_sndbuf, when connection enters established state. 2811da177e4SLinus Torvalds */ 2821da177e4SLinus Torvalds 2836ae70532SEric Dumazet static void tcp_sndbuf_expand(struct sock *sk) 2841da177e4SLinus Torvalds { 2856ae70532SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 2866ae70532SEric Dumazet int sndmem, per_mss; 2876ae70532SEric Dumazet u32 nr_segs; 2881da177e4SLinus Torvalds 2896ae70532SEric Dumazet /* Worst case is non GSO/TSO : each frame consumes one skb 2906ae70532SEric Dumazet * and skb->head is kmalloced using power of two area of memory 2916ae70532SEric Dumazet */ 2926ae70532SEric Dumazet per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) + 2936ae70532SEric Dumazet MAX_TCP_HEADER + 2946ae70532SEric Dumazet SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 2956ae70532SEric Dumazet 2966ae70532SEric Dumazet per_mss = roundup_pow_of_two(per_mss) + 2976ae70532SEric Dumazet SKB_DATA_ALIGN(sizeof(struct sk_buff)); 2986ae70532SEric Dumazet 2996ae70532SEric Dumazet nr_segs = max_t(u32, TCP_INIT_CWND, tp->snd_cwnd); 3006ae70532SEric Dumazet nr_segs = max_t(u32, nr_segs, tp->reordering + 1); 3016ae70532SEric Dumazet 3026ae70532SEric Dumazet /* Fast Recovery (RFC 5681 3.2) : 3036ae70532SEric Dumazet * Cubic needs 1.7 factor, rounded to 2 to include 3046ae70532SEric Dumazet * extra cushion (application might react slowly to POLLOUT) 3056ae70532SEric Dumazet */ 3066ae70532SEric Dumazet sndmem = 2 * nr_segs * per_mss; 3076ae70532SEric Dumazet 30806a59ecbSEric Dumazet if (sk->sk_sndbuf < sndmem) 30906a59ecbSEric Dumazet sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds /* 2. Tuning advertised window (window_clamp, rcv_ssthresh) 3131da177e4SLinus Torvalds * 3141da177e4SLinus Torvalds * All tcp_full_space() is split to two parts: "network" buffer, allocated 3151da177e4SLinus Torvalds * forward and advertised in receiver window (tp->rcv_wnd) and 3161da177e4SLinus Torvalds * "application buffer", required to isolate scheduling/application 3171da177e4SLinus Torvalds * latencies from network. 3181da177e4SLinus Torvalds * window_clamp is maximal advertised window. It can be less than 3191da177e4SLinus Torvalds * tcp_full_space(), in this case tcp_full_space() - window_clamp 3201da177e4SLinus Torvalds * is reserved for "application" buffer. The less window_clamp is 3211da177e4SLinus Torvalds * the smoother our behaviour from viewpoint of network, but the lower 3221da177e4SLinus Torvalds * throughput and the higher sensitivity of the connection to losses. 8) 3231da177e4SLinus Torvalds * 3241da177e4SLinus Torvalds * rcv_ssthresh is more strict window_clamp used at "slow start" 3251da177e4SLinus Torvalds * phase to predict further behaviour of this connection. 3261da177e4SLinus Torvalds * It is used for two goals: 3271da177e4SLinus Torvalds * - to enforce header prediction at sender, even when application 3281da177e4SLinus Torvalds * requires some significant "application buffer". It is check #1. 3291da177e4SLinus Torvalds * - to prevent pruning of receive queue because of misprediction 3301da177e4SLinus Torvalds * of receiver window. Check #2. 3311da177e4SLinus Torvalds * 3321da177e4SLinus Torvalds * The scheme does not work when sender sends good segments opening 333caa20d9aSStephen Hemminger * window and then starts to feed us spaghetti. But it should work 3341da177e4SLinus Torvalds * in common situations. Otherwise, we have to rely on queue collapsing. 3351da177e4SLinus Torvalds */ 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds /* Slow part of check#2. */ 3389e412ba7SIlpo Järvinen static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb) 3391da177e4SLinus Torvalds { 3409e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 3411da177e4SLinus Torvalds /* Optimize this! */ 342dfd4f0aeSEric Dumazet int truesize = tcp_win_from_space(skb->truesize) >> 1; 343dfd4f0aeSEric Dumazet int window = tcp_win_from_space(sysctl_tcp_rmem[2]) >> 1; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds while (tp->rcv_ssthresh <= window) { 3461da177e4SLinus Torvalds if (truesize <= skb->len) 347463c84b9SArnaldo Carvalho de Melo return 2 * inet_csk(sk)->icsk_ack.rcv_mss; 3481da177e4SLinus Torvalds 3491da177e4SLinus Torvalds truesize >>= 1; 3501da177e4SLinus Torvalds window >>= 1; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds return 0; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 355cf533ea5SEric Dumazet static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb) 3561da177e4SLinus Torvalds { 3579e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 3589e412ba7SIlpo Järvinen 3591da177e4SLinus Torvalds /* Check #1 */ 3601da177e4SLinus Torvalds if (tp->rcv_ssthresh < tp->window_clamp && 3611da177e4SLinus Torvalds (int)tp->rcv_ssthresh < tcp_space(sk) && 362180d8cd9SGlauber Costa !sk_under_memory_pressure(sk)) { 3631da177e4SLinus Torvalds int incr; 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds /* Check #2. Increase window, if skb with such overhead 3661da177e4SLinus Torvalds * will fit to rcvbuf in future. 3671da177e4SLinus Torvalds */ 3681da177e4SLinus Torvalds if (tcp_win_from_space(skb->truesize) <= skb->len) 3691da177e4SLinus Torvalds incr = 2 * tp->advmss; 3701da177e4SLinus Torvalds else 3719e412ba7SIlpo Järvinen incr = __tcp_grow_window(sk, skb); 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds if (incr) { 3744d846f02SEric Dumazet incr = max_t(int, incr, 2 * skb->len); 375056834d9SIlpo Järvinen tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr, 376056834d9SIlpo Järvinen tp->window_clamp); 377463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_ack.quick |= 1; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds /* 3. Tuning rcvbuf, when connection enters established state. */ 3831da177e4SLinus Torvalds static void tcp_fixup_rcvbuf(struct sock *sk) 3841da177e4SLinus Torvalds { 385e9266a02SEric Dumazet u32 mss = tcp_sk(sk)->advmss; 386e9266a02SEric Dumazet int rcvmem; 3871da177e4SLinus Torvalds 38885f16525SYuchung Cheng rcvmem = 2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) * 38985f16525SYuchung Cheng tcp_default_init_rwnd(mss); 390e9266a02SEric Dumazet 391b0983d3cSEric Dumazet /* Dynamic Right Sizing (DRS) has 2 to 3 RTT latency 392b0983d3cSEric Dumazet * Allow enough cushion so that sender is not limited by our window 393b0983d3cSEric Dumazet */ 394b0983d3cSEric Dumazet if (sysctl_tcp_moderate_rcvbuf) 395b0983d3cSEric Dumazet rcvmem <<= 2; 396b0983d3cSEric Dumazet 397e9266a02SEric Dumazet if (sk->sk_rcvbuf < rcvmem) 398e9266a02SEric Dumazet sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]); 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 401caa20d9aSStephen Hemminger /* 4. Try to fixup all. It is made immediately after connection enters 4021da177e4SLinus Torvalds * established state. 4031da177e4SLinus Torvalds */ 40410467163SJerry Chu void tcp_init_buffer_space(struct sock *sk) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 4071da177e4SLinus Torvalds int maxwin; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) 4101da177e4SLinus Torvalds tcp_fixup_rcvbuf(sk); 4111da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) 4126ae70532SEric Dumazet tcp_sndbuf_expand(sk); 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds tp->rcvq_space.space = tp->rcv_wnd; 415b0983d3cSEric Dumazet tp->rcvq_space.time = tcp_time_stamp; 416b0983d3cSEric Dumazet tp->rcvq_space.seq = tp->copied_seq; 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds maxwin = tcp_full_space(sk); 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds if (tp->window_clamp >= maxwin) { 4211da177e4SLinus Torvalds tp->window_clamp = maxwin; 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss) 4241da177e4SLinus Torvalds tp->window_clamp = max(maxwin - 4251da177e4SLinus Torvalds (maxwin >> sysctl_tcp_app_win), 4261da177e4SLinus Torvalds 4 * tp->advmss); 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds /* Force reservation of one segment. */ 4301da177e4SLinus Torvalds if (sysctl_tcp_app_win && 4311da177e4SLinus Torvalds tp->window_clamp > 2 * tp->advmss && 4321da177e4SLinus Torvalds tp->window_clamp + tp->advmss > maxwin) 4331da177e4SLinus Torvalds tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss); 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp); 4361da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds /* 5. Recalculate window clamp after socket hit its memory bounds. */ 4409e412ba7SIlpo Järvinen static void tcp_clamp_window(struct sock *sk) 4411da177e4SLinus Torvalds { 4429e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 4436687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 4441da177e4SLinus Torvalds 4456687e988SArnaldo Carvalho de Melo icsk->icsk_ack.quick = 0; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] && 4481da177e4SLinus Torvalds !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) && 449180d8cd9SGlauber Costa !sk_under_memory_pressure(sk) && 450180d8cd9SGlauber Costa sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) { 4511da177e4SLinus Torvalds sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc), 4521da177e4SLinus Torvalds sysctl_tcp_rmem[2]); 4531da177e4SLinus Torvalds } 454326f36e9SJohn Heffner if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) 4551da177e4SLinus Torvalds tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss); 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 45840efc6faSStephen Hemminger /* Initialize RCV_MSS value. 45940efc6faSStephen Hemminger * RCV_MSS is an our guess about MSS used by the peer. 46040efc6faSStephen Hemminger * We haven't any direct information about the MSS. 46140efc6faSStephen Hemminger * It's better to underestimate the RCV_MSS rather than overestimate. 46240efc6faSStephen Hemminger * Overestimations make us ACKing less frequently than needed. 46340efc6faSStephen Hemminger * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss(). 46440efc6faSStephen Hemminger */ 46540efc6faSStephen Hemminger void tcp_initialize_rcv_mss(struct sock *sk) 46640efc6faSStephen Hemminger { 467cf533ea5SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 46840efc6faSStephen Hemminger unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache); 46940efc6faSStephen Hemminger 47040efc6faSStephen Hemminger hint = min(hint, tp->rcv_wnd / 2); 471bee7ca9eSWilliam Allen Simpson hint = min(hint, TCP_MSS_DEFAULT); 47240efc6faSStephen Hemminger hint = max(hint, TCP_MIN_MSS); 47340efc6faSStephen Hemminger 47440efc6faSStephen Hemminger inet_csk(sk)->icsk_ack.rcv_mss = hint; 47540efc6faSStephen Hemminger } 4764bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_initialize_rcv_mss); 47740efc6faSStephen Hemminger 4781da177e4SLinus Torvalds /* Receiver "autotuning" code. 4791da177e4SLinus Torvalds * 4801da177e4SLinus Torvalds * The algorithm for RTT estimation w/o timestamps is based on 4811da177e4SLinus Torvalds * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL. 482631dd1a8SJustin P. Mattock * <http://public.lanl.gov/radiant/pubs.html#DRS> 4831da177e4SLinus Torvalds * 4841da177e4SLinus Torvalds * More detail on this code can be found at 485631dd1a8SJustin P. Mattock * <http://staff.psc.edu/jheffner/>, 4861da177e4SLinus Torvalds * though this reference is out of date. A new paper 4871da177e4SLinus Torvalds * is pending. 4881da177e4SLinus Torvalds */ 4891da177e4SLinus Torvalds static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep) 4901da177e4SLinus Torvalds { 4911da177e4SLinus Torvalds u32 new_sample = tp->rcv_rtt_est.rtt; 4921da177e4SLinus Torvalds long m = sample; 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds if (m == 0) 4951da177e4SLinus Torvalds m = 1; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds if (new_sample != 0) { 4981da177e4SLinus Torvalds /* If we sample in larger samples in the non-timestamp 4991da177e4SLinus Torvalds * case, we could grossly overestimate the RTT especially 5001da177e4SLinus Torvalds * with chatty applications or bulk transfer apps which 5011da177e4SLinus Torvalds * are stalled on filesystem I/O. 5021da177e4SLinus Torvalds * 5031da177e4SLinus Torvalds * Also, since we are only going for a minimum in the 50431f34269SStephen Hemminger * non-timestamp case, we do not smooth things out 505caa20d9aSStephen Hemminger * else with timestamps disabled convergence takes too 5061da177e4SLinus Torvalds * long. 5071da177e4SLinus Torvalds */ 5081da177e4SLinus Torvalds if (!win_dep) { 5091da177e4SLinus Torvalds m -= (new_sample >> 3); 5101da177e4SLinus Torvalds new_sample += m; 51118a223e0SNeal Cardwell } else { 51218a223e0SNeal Cardwell m <<= 3; 51318a223e0SNeal Cardwell if (m < new_sample) 51418a223e0SNeal Cardwell new_sample = m; 51518a223e0SNeal Cardwell } 5161da177e4SLinus Torvalds } else { 517caa20d9aSStephen Hemminger /* No previous measure. */ 5181da177e4SLinus Torvalds new_sample = m << 3; 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds if (tp->rcv_rtt_est.rtt != new_sample) 5221da177e4SLinus Torvalds tp->rcv_rtt_est.rtt = new_sample; 5231da177e4SLinus Torvalds } 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp) 5261da177e4SLinus Torvalds { 5271da177e4SLinus Torvalds if (tp->rcv_rtt_est.time == 0) 5281da177e4SLinus Torvalds goto new_measure; 5291da177e4SLinus Torvalds if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq)) 5301da177e4SLinus Torvalds return; 531651913ceSNeal Cardwell tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rcv_rtt_est.time, 1); 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds new_measure: 5341da177e4SLinus Torvalds tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd; 5351da177e4SLinus Torvalds tp->rcv_rtt_est.time = tcp_time_stamp; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 538056834d9SIlpo Järvinen static inline void tcp_rcv_rtt_measure_ts(struct sock *sk, 539056834d9SIlpo Järvinen const struct sk_buff *skb) 5401da177e4SLinus Torvalds { 541463c84b9SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 5421da177e4SLinus Torvalds if (tp->rx_opt.rcv_tsecr && 5431da177e4SLinus Torvalds (TCP_SKB_CB(skb)->end_seq - 544463c84b9SArnaldo Carvalho de Melo TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss)) 5451da177e4SLinus Torvalds tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0); 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /* 5491da177e4SLinus Torvalds * This function should be called every time data is copied to user space. 5501da177e4SLinus Torvalds * It calculates the appropriate TCP receive buffer space. 5511da177e4SLinus Torvalds */ 5521da177e4SLinus Torvalds void tcp_rcv_space_adjust(struct sock *sk) 5531da177e4SLinus Torvalds { 5541da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 5551da177e4SLinus Torvalds int time; 556b0983d3cSEric Dumazet int copied; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds time = tcp_time_stamp - tp->rcvq_space.time; 559056834d9SIlpo Järvinen if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0) 5601da177e4SLinus Torvalds return; 5611da177e4SLinus Torvalds 562b0983d3cSEric Dumazet /* Number of bytes copied to user in last RTT */ 563b0983d3cSEric Dumazet copied = tp->copied_seq - tp->rcvq_space.seq; 564b0983d3cSEric Dumazet if (copied <= tp->rcvq_space.space) 565b0983d3cSEric Dumazet goto new_measure; 5661da177e4SLinus Torvalds 567b0983d3cSEric Dumazet /* A bit of theory : 568b0983d3cSEric Dumazet * copied = bytes received in previous RTT, our base window 569b0983d3cSEric Dumazet * To cope with packet losses, we need a 2x factor 570b0983d3cSEric Dumazet * To cope with slow start, and sender growing its cwin by 100 % 571b0983d3cSEric Dumazet * every RTT, we need a 4x factor, because the ACK we are sending 572b0983d3cSEric Dumazet * now is for the next RTT, not the current one : 573b0983d3cSEric Dumazet * <prev RTT . ><current RTT .. ><next RTT .... > 574b0983d3cSEric Dumazet */ 5751da177e4SLinus Torvalds 5766fcf9412SJohn Heffner if (sysctl_tcp_moderate_rcvbuf && 5776fcf9412SJohn Heffner !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 578b0983d3cSEric Dumazet int rcvwin, rcvmem, rcvbuf; 5791da177e4SLinus Torvalds 580b0983d3cSEric Dumazet /* minimal window to cope with packet losses, assuming 581b0983d3cSEric Dumazet * steady state. Add some cushion because of small variations. 5821da177e4SLinus Torvalds */ 583b0983d3cSEric Dumazet rcvwin = (copied << 1) + 16 * tp->advmss; 584b0983d3cSEric Dumazet 585b0983d3cSEric Dumazet /* If rate increased by 25%, 586b0983d3cSEric Dumazet * assume slow start, rcvwin = 3 * copied 587b0983d3cSEric Dumazet * If rate increased by 50%, 588b0983d3cSEric Dumazet * assume sender can use 2x growth, rcvwin = 4 * copied 589b0983d3cSEric Dumazet */ 590b0983d3cSEric Dumazet if (copied >= 591b0983d3cSEric Dumazet tp->rcvq_space.space + (tp->rcvq_space.space >> 2)) { 592b0983d3cSEric Dumazet if (copied >= 593b0983d3cSEric Dumazet tp->rcvq_space.space + (tp->rcvq_space.space >> 1)) 594b0983d3cSEric Dumazet rcvwin <<= 1; 595b0983d3cSEric Dumazet else 596b0983d3cSEric Dumazet rcvwin += (rcvwin >> 1); 597b0983d3cSEric Dumazet } 598b0983d3cSEric Dumazet 59987fb4b7bSEric Dumazet rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER); 6001da177e4SLinus Torvalds while (tcp_win_from_space(rcvmem) < tp->advmss) 6011da177e4SLinus Torvalds rcvmem += 128; 602b0983d3cSEric Dumazet 603b0983d3cSEric Dumazet rcvbuf = min(rcvwin / tp->advmss * rcvmem, sysctl_tcp_rmem[2]); 604b0983d3cSEric Dumazet if (rcvbuf > sk->sk_rcvbuf) { 605b0983d3cSEric Dumazet sk->sk_rcvbuf = rcvbuf; 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds /* Make the window clamp follow along. */ 608b0983d3cSEric Dumazet tp->window_clamp = rcvwin; 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds } 611b0983d3cSEric Dumazet tp->rcvq_space.space = copied; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds new_measure: 6141da177e4SLinus Torvalds tp->rcvq_space.seq = tp->copied_seq; 6151da177e4SLinus Torvalds tp->rcvq_space.time = tcp_time_stamp; 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /* There is something which you must keep in mind when you analyze the 6191da177e4SLinus Torvalds * behavior of the tp->ato delayed ack timeout interval. When a 6201da177e4SLinus Torvalds * connection starts up, we want to ack as quickly as possible. The 6211da177e4SLinus Torvalds * problem is that "good" TCP's do slow start at the beginning of data 6221da177e4SLinus Torvalds * transmission. The means that until we send the first few ACK's the 6231da177e4SLinus Torvalds * sender will sit on his end and only queue most of his data, because 6241da177e4SLinus Torvalds * he can only send snd_cwnd unacked packets at any given time. For 6251da177e4SLinus Torvalds * each ACK we send, he increments snd_cwnd and transmits more of his 6261da177e4SLinus Torvalds * queue. -DaveM 6271da177e4SLinus Torvalds */ 6289e412ba7SIlpo Järvinen static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb) 6291da177e4SLinus Torvalds { 6309e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 631463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 6321da177e4SLinus Torvalds u32 now; 6331da177e4SLinus Torvalds 634463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 6351da177e4SLinus Torvalds 636463c84b9SArnaldo Carvalho de Melo tcp_measure_rcv_mss(sk, skb); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds tcp_rcv_rtt_measure(tp); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds now = tcp_time_stamp; 6411da177e4SLinus Torvalds 642463c84b9SArnaldo Carvalho de Melo if (!icsk->icsk_ack.ato) { 6431da177e4SLinus Torvalds /* The _first_ data packet received, initialize 6441da177e4SLinus Torvalds * delayed ACK engine. 6451da177e4SLinus Torvalds */ 646463c84b9SArnaldo Carvalho de Melo tcp_incr_quickack(sk); 647463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN; 6481da177e4SLinus Torvalds } else { 649463c84b9SArnaldo Carvalho de Melo int m = now - icsk->icsk_ack.lrcvtime; 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds if (m <= TCP_ATO_MIN / 2) { 6521da177e4SLinus Torvalds /* The fastest case is the first. */ 653463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2; 654463c84b9SArnaldo Carvalho de Melo } else if (m < icsk->icsk_ack.ato) { 655463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m; 656463c84b9SArnaldo Carvalho de Melo if (icsk->icsk_ack.ato > icsk->icsk_rto) 657463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = icsk->icsk_rto; 658463c84b9SArnaldo Carvalho de Melo } else if (m > icsk->icsk_rto) { 659caa20d9aSStephen Hemminger /* Too long gap. Apparently sender failed to 6601da177e4SLinus Torvalds * restart window, so that we send ACKs quickly. 6611da177e4SLinus Torvalds */ 662463c84b9SArnaldo Carvalho de Melo tcp_incr_quickack(sk); 6633ab224beSHideo Aoki sk_mem_reclaim(sk); 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds } 666463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.lrcvtime = now; 6671da177e4SLinus Torvalds 668735d3831SFlorian Westphal tcp_ecn_check_ce(tp, skb); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds if (skb->len >= 128) 6719e412ba7SIlpo Järvinen tcp_grow_window(sk, skb); 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds /* Called to compute a smoothed rtt estimate. The data fed to this 6751da177e4SLinus Torvalds * routine either comes from timestamps, or from segments that were 6761da177e4SLinus Torvalds * known _not_ to have been retransmitted [see Karn/Partridge 6771da177e4SLinus Torvalds * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88 6781da177e4SLinus Torvalds * piece by Van Jacobson. 6791da177e4SLinus Torvalds * NOTE: the next three routines used to be one big routine. 6801da177e4SLinus Torvalds * To save cycles in the RFC 1323 implementation it was better to break 6811da177e4SLinus Torvalds * it up into three procedures. -- erics 6821da177e4SLinus Torvalds */ 683740b0f18SEric Dumazet static void tcp_rtt_estimator(struct sock *sk, long mrtt_us) 6841da177e4SLinus Torvalds { 6856687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 686740b0f18SEric Dumazet long m = mrtt_us; /* RTT */ 687740b0f18SEric Dumazet u32 srtt = tp->srtt_us; 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds /* The following amusing code comes from Jacobson's 6901da177e4SLinus Torvalds * article in SIGCOMM '88. Note that rtt and mdev 6911da177e4SLinus Torvalds * are scaled versions of rtt and mean deviation. 6921da177e4SLinus Torvalds * This is designed to be as fast as possible 6931da177e4SLinus Torvalds * m stands for "measurement". 6941da177e4SLinus Torvalds * 6951da177e4SLinus Torvalds * On a 1990 paper the rto value is changed to: 6961da177e4SLinus Torvalds * RTO = rtt + 4 * mdev 6971da177e4SLinus Torvalds * 6981da177e4SLinus Torvalds * Funny. This algorithm seems to be very broken. 6991da177e4SLinus Torvalds * These formulae increase RTO, when it should be decreased, increase 70031f34269SStephen Hemminger * too slowly, when it should be increased quickly, decrease too quickly 7011da177e4SLinus Torvalds * etc. I guess in BSD RTO takes ONE value, so that it is absolutely 7021da177e4SLinus Torvalds * does not matter how to _calculate_ it. Seems, it was trap 7031da177e4SLinus Torvalds * that VJ failed to avoid. 8) 7041da177e4SLinus Torvalds */ 7054a5ab4e2SEric Dumazet if (srtt != 0) { 7064a5ab4e2SEric Dumazet m -= (srtt >> 3); /* m is now error in rtt est */ 7074a5ab4e2SEric Dumazet srtt += m; /* rtt = 7/8 rtt + 1/8 new */ 7081da177e4SLinus Torvalds if (m < 0) { 7091da177e4SLinus Torvalds m = -m; /* m is now abs(error) */ 710740b0f18SEric Dumazet m -= (tp->mdev_us >> 2); /* similar update on mdev */ 7111da177e4SLinus Torvalds /* This is similar to one of Eifel findings. 7121da177e4SLinus Torvalds * Eifel blocks mdev updates when rtt decreases. 7131da177e4SLinus Torvalds * This solution is a bit different: we use finer gain 7141da177e4SLinus Torvalds * for mdev in this case (alpha*beta). 7151da177e4SLinus Torvalds * Like Eifel it also prevents growth of rto, 7161da177e4SLinus Torvalds * but also it limits too fast rto decreases, 7171da177e4SLinus Torvalds * happening in pure Eifel. 7181da177e4SLinus Torvalds */ 7191da177e4SLinus Torvalds if (m > 0) 7201da177e4SLinus Torvalds m >>= 3; 7211da177e4SLinus Torvalds } else { 722740b0f18SEric Dumazet m -= (tp->mdev_us >> 2); /* similar update on mdev */ 7231da177e4SLinus Torvalds } 724740b0f18SEric Dumazet tp->mdev_us += m; /* mdev = 3/4 mdev + 1/4 new */ 725740b0f18SEric Dumazet if (tp->mdev_us > tp->mdev_max_us) { 726740b0f18SEric Dumazet tp->mdev_max_us = tp->mdev_us; 727740b0f18SEric Dumazet if (tp->mdev_max_us > tp->rttvar_us) 728740b0f18SEric Dumazet tp->rttvar_us = tp->mdev_max_us; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds if (after(tp->snd_una, tp->rtt_seq)) { 731740b0f18SEric Dumazet if (tp->mdev_max_us < tp->rttvar_us) 732740b0f18SEric Dumazet tp->rttvar_us -= (tp->rttvar_us - tp->mdev_max_us) >> 2; 7331da177e4SLinus Torvalds tp->rtt_seq = tp->snd_nxt; 734740b0f18SEric Dumazet tp->mdev_max_us = tcp_rto_min_us(sk); 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds } else { 7371da177e4SLinus Torvalds /* no previous measure. */ 7384a5ab4e2SEric Dumazet srtt = m << 3; /* take the measured time to be rtt */ 739740b0f18SEric Dumazet tp->mdev_us = m << 1; /* make sure rto = 3*rtt */ 740740b0f18SEric Dumazet tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk)); 741740b0f18SEric Dumazet tp->mdev_max_us = tp->rttvar_us; 7421da177e4SLinus Torvalds tp->rtt_seq = tp->snd_nxt; 7431da177e4SLinus Torvalds } 744740b0f18SEric Dumazet tp->srtt_us = max(1U, srtt); 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 74795bd09ebSEric Dumazet /* Set the sk_pacing_rate to allow proper sizing of TSO packets. 74895bd09ebSEric Dumazet * Note: TCP stack does not yet implement pacing. 74995bd09ebSEric Dumazet * FQ packet scheduler can be used to implement cheap but effective 75095bd09ebSEric Dumazet * TCP pacing, to smooth the burst on large writes when packets 75195bd09ebSEric Dumazet * in flight is significantly lower than cwnd (or rwin) 75295bd09ebSEric Dumazet */ 75395bd09ebSEric Dumazet static void tcp_update_pacing_rate(struct sock *sk) 75495bd09ebSEric Dumazet { 75595bd09ebSEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 75695bd09ebSEric Dumazet u64 rate; 75795bd09ebSEric Dumazet 75895bd09ebSEric Dumazet /* set sk_pacing_rate to 200 % of current rate (mss * cwnd / srtt) */ 759740b0f18SEric Dumazet rate = (u64)tp->mss_cache * 2 * (USEC_PER_SEC << 3); 76095bd09ebSEric Dumazet 76195bd09ebSEric Dumazet rate *= max(tp->snd_cwnd, tp->packets_out); 76295bd09ebSEric Dumazet 763740b0f18SEric Dumazet if (likely(tp->srtt_us)) 764740b0f18SEric Dumazet do_div(rate, tp->srtt_us); 76595bd09ebSEric Dumazet 766ba537427SEric Dumazet /* ACCESS_ONCE() is needed because sch_fq fetches sk_pacing_rate 767ba537427SEric Dumazet * without any lock. We want to make sure compiler wont store 768ba537427SEric Dumazet * intermediate values in this location. 769ba537427SEric Dumazet */ 770ba537427SEric Dumazet ACCESS_ONCE(sk->sk_pacing_rate) = min_t(u64, rate, 771ba537427SEric Dumazet sk->sk_max_pacing_rate); 77295bd09ebSEric Dumazet } 77395bd09ebSEric Dumazet 7741da177e4SLinus Torvalds /* Calculate rto without backoff. This is the second half of Van Jacobson's 7751da177e4SLinus Torvalds * routine referred to above. 7761da177e4SLinus Torvalds */ 777f7e56a76Sstephen hemminger static void tcp_set_rto(struct sock *sk) 7781da177e4SLinus Torvalds { 779463c84b9SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 7801da177e4SLinus Torvalds /* Old crap is replaced with new one. 8) 7811da177e4SLinus Torvalds * 7821da177e4SLinus Torvalds * More seriously: 7831da177e4SLinus Torvalds * 1. If rtt variance happened to be less 50msec, it is hallucination. 7841da177e4SLinus Torvalds * It cannot be less due to utterly erratic ACK generation made 7851da177e4SLinus Torvalds * at least by solaris and freebsd. "Erratic ACKs" has _nothing_ 7861da177e4SLinus Torvalds * to do with delayed acks, because at cwnd>2 true delack timeout 7871da177e4SLinus Torvalds * is invisible. Actually, Linux-2.4 also generates erratic 788caa20d9aSStephen Hemminger * ACKs in some circumstances. 7891da177e4SLinus Torvalds */ 790f1ecd5d9SDamian Lukowski inet_csk(sk)->icsk_rto = __tcp_set_rto(tp); 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /* 2. Fixups made earlier cannot be right. 7931da177e4SLinus Torvalds * If we do not estimate RTO correctly without them, 7941da177e4SLinus Torvalds * all the algo is pure shit and should be replaced 795caa20d9aSStephen Hemminger * with correct one. It is exactly, which we pretend to do. 7961da177e4SLinus Torvalds */ 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds /* NOTE: clamping at TCP_RTO_MIN is not required, current algo 7991da177e4SLinus Torvalds * guarantees that rto is higher. 8001da177e4SLinus Torvalds */ 801f1ecd5d9SDamian Lukowski tcp_bound_rto(sk); 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 804cf533ea5SEric Dumazet __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst) 8051da177e4SLinus Torvalds { 8061da177e4SLinus Torvalds __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0); 8071da177e4SLinus Torvalds 80822b71c8fSGerrit Renker if (!cwnd) 809442b9635SDavid S. Miller cwnd = TCP_INIT_CWND; 8101da177e4SLinus Torvalds return min_t(__u32, cwnd, tp->snd_cwnd_clamp); 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 813e60402d0SIlpo Järvinen /* 814e60402d0SIlpo Järvinen * Packet counting of FACK is based on in-order assumptions, therefore TCP 815e60402d0SIlpo Järvinen * disables it when reordering is detected 816e60402d0SIlpo Järvinen */ 8174aabd8efSDavid S. Miller void tcp_disable_fack(struct tcp_sock *tp) 818e60402d0SIlpo Järvinen { 81985cc391cSIlpo Järvinen /* RFC3517 uses different metric in lost marker => reset on change */ 82085cc391cSIlpo Järvinen if (tcp_is_fack(tp)) 82185cc391cSIlpo Järvinen tp->lost_skb_hint = NULL; 822ab56222aSVijay Subramanian tp->rx_opt.sack_ok &= ~TCP_FACK_ENABLED; 823e60402d0SIlpo Järvinen } 824e60402d0SIlpo Järvinen 825564262c1SRyousei Takano /* Take a notice that peer is sending D-SACKs */ 826e60402d0SIlpo Järvinen static void tcp_dsack_seen(struct tcp_sock *tp) 827e60402d0SIlpo Järvinen { 828ab56222aSVijay Subramanian tp->rx_opt.sack_ok |= TCP_DSACK_SEEN; 829e60402d0SIlpo Järvinen } 830e60402d0SIlpo Järvinen 8316687e988SArnaldo Carvalho de Melo static void tcp_update_reordering(struct sock *sk, const int metric, 8326687e988SArnaldo Carvalho de Melo const int ts) 8331da177e4SLinus Torvalds { 8346687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 8351da177e4SLinus Torvalds if (metric > tp->reordering) { 83640b215e5SPavel Emelyanov int mib_idx; 83740b215e5SPavel Emelyanov 838dca145ffSEric Dumazet tp->reordering = min(sysctl_tcp_max_reordering, metric); 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds /* This exciting event is worth to be remembered. 8) */ 8411da177e4SLinus Torvalds if (ts) 84240b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPTSREORDER; 843e60402d0SIlpo Järvinen else if (tcp_is_reno(tp)) 84440b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPRENOREORDER; 845e60402d0SIlpo Järvinen else if (tcp_is_fack(tp)) 84640b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPFACKREORDER; 8471da177e4SLinus Torvalds else 84840b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPSACKREORDER; 84940b215e5SPavel Emelyanov 850de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), mib_idx); 8511da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 1 85291df42beSJoe Perches pr_debug("Disorder%d %d %u f%u s%u rr%d\n", 8536687e988SArnaldo Carvalho de Melo tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state, 8541da177e4SLinus Torvalds tp->reordering, 8551da177e4SLinus Torvalds tp->fackets_out, 8561da177e4SLinus Torvalds tp->sacked_out, 8571da177e4SLinus Torvalds tp->undo_marker ? tp->undo_retrans : 0); 8581da177e4SLinus Torvalds #endif 859e60402d0SIlpo Järvinen tcp_disable_fack(tp); 8601da177e4SLinus Torvalds } 861eed530b6SYuchung Cheng 862eed530b6SYuchung Cheng if (metric > 0) 863eed530b6SYuchung Cheng tcp_disable_early_retrans(tp); 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds 866006f582cSIlpo Järvinen /* This must be called before lost_out is incremented */ 867c8c213f2SIlpo Järvinen static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb) 868c8c213f2SIlpo Järvinen { 869006f582cSIlpo Järvinen if ((tp->retransmit_skb_hint == NULL) || 870c8c213f2SIlpo Järvinen before(TCP_SKB_CB(skb)->seq, 871c8c213f2SIlpo Järvinen TCP_SKB_CB(tp->retransmit_skb_hint)->seq)) 872006f582cSIlpo Järvinen tp->retransmit_skb_hint = skb; 873006f582cSIlpo Järvinen 874006f582cSIlpo Järvinen if (!tp->lost_out || 875006f582cSIlpo Järvinen after(TCP_SKB_CB(skb)->end_seq, tp->retransmit_high)) 876006f582cSIlpo Järvinen tp->retransmit_high = TCP_SKB_CB(skb)->end_seq; 877c8c213f2SIlpo Järvinen } 878c8c213f2SIlpo Järvinen 87941ea36e3SIlpo Järvinen static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb) 88041ea36e3SIlpo Järvinen { 88141ea36e3SIlpo Järvinen if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) { 88241ea36e3SIlpo Järvinen tcp_verify_retransmit_hint(tp, skb); 88341ea36e3SIlpo Järvinen 88441ea36e3SIlpo Järvinen tp->lost_out += tcp_skb_pcount(skb); 88541ea36e3SIlpo Järvinen TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; 88641ea36e3SIlpo Järvinen } 88741ea36e3SIlpo Järvinen } 88841ea36e3SIlpo Järvinen 889e1aa680fSIlpo Järvinen static void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp, 890e1aa680fSIlpo Järvinen struct sk_buff *skb) 891006f582cSIlpo Järvinen { 892006f582cSIlpo Järvinen tcp_verify_retransmit_hint(tp, skb); 893006f582cSIlpo Järvinen 894006f582cSIlpo Järvinen if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) { 895006f582cSIlpo Järvinen tp->lost_out += tcp_skb_pcount(skb); 896006f582cSIlpo Järvinen TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; 897006f582cSIlpo Järvinen } 898006f582cSIlpo Järvinen } 899006f582cSIlpo Järvinen 9001da177e4SLinus Torvalds /* This procedure tags the retransmission queue when SACKs arrive. 9011da177e4SLinus Torvalds * 9021da177e4SLinus Torvalds * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L). 9031da177e4SLinus Torvalds * Packets in queue with these bits set are counted in variables 9041da177e4SLinus Torvalds * sacked_out, retrans_out and lost_out, correspondingly. 9051da177e4SLinus Torvalds * 9061da177e4SLinus Torvalds * Valid combinations are: 9071da177e4SLinus Torvalds * Tag InFlight Description 9081da177e4SLinus Torvalds * 0 1 - orig segment is in flight. 9091da177e4SLinus Torvalds * S 0 - nothing flies, orig reached receiver. 9101da177e4SLinus Torvalds * L 0 - nothing flies, orig lost by net. 9111da177e4SLinus Torvalds * R 2 - both orig and retransmit are in flight. 9121da177e4SLinus Torvalds * L|R 1 - orig is lost, retransmit is in flight. 9131da177e4SLinus Torvalds * S|R 1 - orig reached receiver, retrans is still in flight. 9141da177e4SLinus Torvalds * (L|S|R is logically valid, it could occur when L|R is sacked, 9151da177e4SLinus Torvalds * but it is equivalent to plain S and code short-curcuits it to S. 9161da177e4SLinus Torvalds * L|S is logically invalid, it would mean -1 packet in flight 8)) 9171da177e4SLinus Torvalds * 9181da177e4SLinus Torvalds * These 6 states form finite state machine, controlled by the following events: 9191da177e4SLinus Torvalds * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue()) 9201da177e4SLinus Torvalds * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue()) 921974c1236SYuchung Cheng * 3. Loss detection event of two flavors: 9221da177e4SLinus Torvalds * A. Scoreboard estimator decided the packet is lost. 9231da177e4SLinus Torvalds * A'. Reno "three dupacks" marks head of queue lost. 924974c1236SYuchung Cheng * A''. Its FACK modification, head until snd.fack is lost. 925974c1236SYuchung Cheng * B. SACK arrives sacking SND.NXT at the moment, when the 9261da177e4SLinus Torvalds * segment was retransmitted. 9271da177e4SLinus Torvalds * 4. D-SACK added new rule: D-SACK changes any tag to S. 9281da177e4SLinus Torvalds * 9291da177e4SLinus Torvalds * It is pleasant to note, that state diagram turns out to be commutative, 9301da177e4SLinus Torvalds * so that we are allowed not to be bothered by order of our actions, 9311da177e4SLinus Torvalds * when multiple events arrive simultaneously. (see the function below). 9321da177e4SLinus Torvalds * 9331da177e4SLinus Torvalds * Reordering detection. 9341da177e4SLinus Torvalds * -------------------- 9351da177e4SLinus Torvalds * Reordering metric is maximal distance, which a packet can be displaced 9361da177e4SLinus Torvalds * in packet stream. With SACKs we can estimate it: 9371da177e4SLinus Torvalds * 9381da177e4SLinus Torvalds * 1. SACK fills old hole and the corresponding segment was not 9391da177e4SLinus Torvalds * ever retransmitted -> reordering. Alas, we cannot use it 9401da177e4SLinus Torvalds * when segment was retransmitted. 9411da177e4SLinus Torvalds * 2. The last flaw is solved with D-SACK. D-SACK arrives 9421da177e4SLinus Torvalds * for retransmitted and already SACKed segment -> reordering.. 9431da177e4SLinus Torvalds * Both of these heuristics are not used in Loss state, when we cannot 9441da177e4SLinus Torvalds * account for retransmits accurately. 9455b3c9882SIlpo Järvinen * 9465b3c9882SIlpo Järvinen * SACK block validation. 9475b3c9882SIlpo Järvinen * ---------------------- 9485b3c9882SIlpo Järvinen * 9495b3c9882SIlpo Järvinen * SACK block range validation checks that the received SACK block fits to 9505b3c9882SIlpo Järvinen * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT. 9515b3c9882SIlpo Järvinen * Note that SND.UNA is not included to the range though being valid because 9520e835331SIlpo Järvinen * it means that the receiver is rather inconsistent with itself reporting 9530e835331SIlpo Järvinen * SACK reneging when it should advance SND.UNA. Such SACK block this is 9540e835331SIlpo Järvinen * perfectly valid, however, in light of RFC2018 which explicitly states 9550e835331SIlpo Järvinen * that "SACK block MUST reflect the newest segment. Even if the newest 9560e835331SIlpo Järvinen * segment is going to be discarded ...", not that it looks very clever 9570e835331SIlpo Järvinen * in case of head skb. Due to potentional receiver driven attacks, we 9580e835331SIlpo Järvinen * choose to avoid immediate execution of a walk in write queue due to 9590e835331SIlpo Järvinen * reneging and defer head skb's loss recovery to standard loss recovery 9600e835331SIlpo Järvinen * procedure that will eventually trigger (nothing forbids us doing this). 9615b3c9882SIlpo Järvinen * 9625b3c9882SIlpo Järvinen * Implements also blockage to start_seq wrap-around. Problem lies in the 9635b3c9882SIlpo Järvinen * fact that though start_seq (s) is before end_seq (i.e., not reversed), 9645b3c9882SIlpo Järvinen * there's no guarantee that it will be before snd_nxt (n). The problem 9655b3c9882SIlpo Järvinen * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt 9665b3c9882SIlpo Järvinen * wrap (s_w): 9675b3c9882SIlpo Järvinen * 9685b3c9882SIlpo Järvinen * <- outs wnd -> <- wrapzone -> 9695b3c9882SIlpo Järvinen * u e n u_w e_w s n_w 9705b3c9882SIlpo Järvinen * | | | | | | | 9715b3c9882SIlpo Järvinen * |<------------+------+----- TCP seqno space --------------+---------->| 9725b3c9882SIlpo Järvinen * ...-- <2^31 ->| |<--------... 9735b3c9882SIlpo Järvinen * ...---- >2^31 ------>| |<--------... 9745b3c9882SIlpo Järvinen * 9755b3c9882SIlpo Järvinen * Current code wouldn't be vulnerable but it's better still to discard such 9765b3c9882SIlpo Järvinen * crazy SACK blocks. Doing this check for start_seq alone closes somewhat 9775b3c9882SIlpo Järvinen * similar case (end_seq after snd_nxt wrap) as earlier reversed check in 9785b3c9882SIlpo Järvinen * snd_nxt wrap -> snd_una region will then become "well defined", i.e., 9795b3c9882SIlpo Järvinen * equal to the ideal case (infinite seqno space without wrap caused issues). 9805b3c9882SIlpo Järvinen * 9815b3c9882SIlpo Järvinen * With D-SACK the lower bound is extended to cover sequence space below 9825b3c9882SIlpo Järvinen * SND.UNA down to undo_marker, which is the last point of interest. Yet 983564262c1SRyousei Takano * again, D-SACK block must not to go across snd_una (for the same reason as 9845b3c9882SIlpo Järvinen * for the normal SACK blocks, explained above). But there all simplicity 9855b3c9882SIlpo Järvinen * ends, TCP might receive valid D-SACKs below that. As long as they reside 9865b3c9882SIlpo Järvinen * fully below undo_marker they do not affect behavior in anyway and can 9875b3c9882SIlpo Järvinen * therefore be safely ignored. In rare cases (which are more or less 9885b3c9882SIlpo Järvinen * theoretical ones), the D-SACK will nicely cross that boundary due to skb 9895b3c9882SIlpo Järvinen * fragmentation and packet reordering past skb's retransmission. To consider 9905b3c9882SIlpo Järvinen * them correctly, the acceptable range must be extended even more though 9915b3c9882SIlpo Järvinen * the exact amount is rather hard to quantify. However, tp->max_window can 9925b3c9882SIlpo Järvinen * be used as an exaggerated estimate. 9931da177e4SLinus Torvalds */ 994a2a385d6SEric Dumazet static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack, 9955b3c9882SIlpo Järvinen u32 start_seq, u32 end_seq) 9965b3c9882SIlpo Järvinen { 9975b3c9882SIlpo Järvinen /* Too far in future, or reversed (interpretation is ambiguous) */ 9985b3c9882SIlpo Järvinen if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq)) 999a2a385d6SEric Dumazet return false; 10005b3c9882SIlpo Järvinen 10015b3c9882SIlpo Järvinen /* Nasty start_seq wrap-around check (see comments above) */ 10025b3c9882SIlpo Järvinen if (!before(start_seq, tp->snd_nxt)) 1003a2a385d6SEric Dumazet return false; 10045b3c9882SIlpo Järvinen 1005564262c1SRyousei Takano /* In outstanding window? ...This is valid exit for D-SACKs too. 10065b3c9882SIlpo Järvinen * start_seq == snd_una is non-sensical (see comments above) 10075b3c9882SIlpo Järvinen */ 10085b3c9882SIlpo Järvinen if (after(start_seq, tp->snd_una)) 1009a2a385d6SEric Dumazet return true; 10105b3c9882SIlpo Järvinen 10115b3c9882SIlpo Järvinen if (!is_dsack || !tp->undo_marker) 1012a2a385d6SEric Dumazet return false; 10135b3c9882SIlpo Järvinen 10145b3c9882SIlpo Järvinen /* ...Then it's D-SACK, and must reside below snd_una completely */ 1015f779b2d6SZheng Yan if (after(end_seq, tp->snd_una)) 1016a2a385d6SEric Dumazet return false; 10175b3c9882SIlpo Järvinen 10185b3c9882SIlpo Järvinen if (!before(start_seq, tp->undo_marker)) 1019a2a385d6SEric Dumazet return true; 10205b3c9882SIlpo Järvinen 10215b3c9882SIlpo Järvinen /* Too old */ 10225b3c9882SIlpo Järvinen if (!after(end_seq, tp->undo_marker)) 1023a2a385d6SEric Dumazet return false; 10245b3c9882SIlpo Järvinen 10255b3c9882SIlpo Järvinen /* Undo_marker boundary crossing (overestimates a lot). Known already: 10265b3c9882SIlpo Järvinen * start_seq < undo_marker and end_seq >= undo_marker. 10275b3c9882SIlpo Järvinen */ 10285b3c9882SIlpo Järvinen return !before(start_seq, end_seq - tp->max_window); 10295b3c9882SIlpo Järvinen } 10305b3c9882SIlpo Järvinen 10311c1e87edSIlpo Järvinen /* Check for lost retransmit. This superb idea is borrowed from "ratehalving". 1032974c1236SYuchung Cheng * Event "B". Later note: FACK people cheated me again 8), we have to account 10331c1e87edSIlpo Järvinen * for reordering! Ugly, but should help. 1034f785a8e2SIlpo Järvinen * 1035f785a8e2SIlpo Järvinen * Search retransmitted skbs from write_queue that were sent when snd_nxt was 1036f785a8e2SIlpo Järvinen * less than what is now known to be received by the other end (derived from 10379f58f3b7SIlpo Järvinen * highest SACK block). Also calculate the lowest snd_nxt among the remaining 10389f58f3b7SIlpo Järvinen * retransmitted skbs to avoid some costly processing per ACKs. 10391c1e87edSIlpo Järvinen */ 1040407ef1deSIlpo Järvinen static void tcp_mark_lost_retrans(struct sock *sk) 10411c1e87edSIlpo Järvinen { 10429f58f3b7SIlpo Järvinen const struct inet_connection_sock *icsk = inet_csk(sk); 10431c1e87edSIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 10441c1e87edSIlpo Järvinen struct sk_buff *skb; 1045f785a8e2SIlpo Järvinen int cnt = 0; 1046df2e014bSIlpo Järvinen u32 new_low_seq = tp->snd_nxt; 10476859d494SIlpo Järvinen u32 received_upto = tcp_highest_sack_seq(tp); 10489f58f3b7SIlpo Järvinen 10499f58f3b7SIlpo Järvinen if (!tcp_is_fack(tp) || !tp->retrans_out || 10509f58f3b7SIlpo Järvinen !after(received_upto, tp->lost_retrans_low) || 10519f58f3b7SIlpo Järvinen icsk->icsk_ca_state != TCP_CA_Recovery) 1052407ef1deSIlpo Järvinen return; 10531c1e87edSIlpo Järvinen 10541c1e87edSIlpo Järvinen tcp_for_write_queue(skb, sk) { 10551c1e87edSIlpo Järvinen u32 ack_seq = TCP_SKB_CB(skb)->ack_seq; 10561c1e87edSIlpo Järvinen 10571c1e87edSIlpo Järvinen if (skb == tcp_send_head(sk)) 10581c1e87edSIlpo Järvinen break; 1059f785a8e2SIlpo Järvinen if (cnt == tp->retrans_out) 10601c1e87edSIlpo Järvinen break; 10611c1e87edSIlpo Järvinen if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una)) 10621c1e87edSIlpo Järvinen continue; 10631c1e87edSIlpo Järvinen 1064f785a8e2SIlpo Järvinen if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)) 1065f785a8e2SIlpo Järvinen continue; 1066f785a8e2SIlpo Järvinen 1067d0af4160SIlpo Järvinen /* TODO: We would like to get rid of tcp_is_fack(tp) only 1068d0af4160SIlpo Järvinen * constraint here (see above) but figuring out that at 1069d0af4160SIlpo Järvinen * least tp->reordering SACK blocks reside between ack_seq 1070d0af4160SIlpo Järvinen * and received_upto is not easy task to do cheaply with 1071d0af4160SIlpo Järvinen * the available datastructures. 1072d0af4160SIlpo Järvinen * 1073d0af4160SIlpo Järvinen * Whether FACK should check here for tp->reordering segs 1074d0af4160SIlpo Järvinen * in-between one could argue for either way (it would be 1075d0af4160SIlpo Järvinen * rather simple to implement as we could count fack_count 1076d0af4160SIlpo Järvinen * during the walk and do tp->fackets_out - fack_count). 1077d0af4160SIlpo Järvinen */ 1078d0af4160SIlpo Järvinen if (after(received_upto, ack_seq)) { 10791c1e87edSIlpo Järvinen TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS; 10801c1e87edSIlpo Järvinen tp->retrans_out -= tcp_skb_pcount(skb); 10811c1e87edSIlpo Järvinen 1082006f582cSIlpo Järvinen tcp_skb_mark_lost_uncond_verify(tp, skb); 1083de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT); 1084f785a8e2SIlpo Järvinen } else { 1085df2e014bSIlpo Järvinen if (before(ack_seq, new_low_seq)) 1086b08d6cb2SIlpo Järvinen new_low_seq = ack_seq; 1087f785a8e2SIlpo Järvinen cnt += tcp_skb_pcount(skb); 10881c1e87edSIlpo Järvinen } 10891c1e87edSIlpo Järvinen } 1090b08d6cb2SIlpo Järvinen 1091b08d6cb2SIlpo Järvinen if (tp->retrans_out) 1092b08d6cb2SIlpo Järvinen tp->lost_retrans_low = new_low_seq; 10931c1e87edSIlpo Järvinen } 10945b3c9882SIlpo Järvinen 1095a2a385d6SEric Dumazet static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb, 1096d06e021dSDavid S. Miller struct tcp_sack_block_wire *sp, int num_sacks, 1097d06e021dSDavid S. Miller u32 prior_snd_una) 1098d06e021dSDavid S. Miller { 10991ed83465SPavel Emelyanov struct tcp_sock *tp = tcp_sk(sk); 1100d3e2ce3bSHarvey Harrison u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq); 1101d3e2ce3bSHarvey Harrison u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq); 1102a2a385d6SEric Dumazet bool dup_sack = false; 1103d06e021dSDavid S. Miller 1104d06e021dSDavid S. Miller if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) { 1105a2a385d6SEric Dumazet dup_sack = true; 1106e60402d0SIlpo Järvinen tcp_dsack_seen(tp); 1107de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKRECV); 1108d06e021dSDavid S. Miller } else if (num_sacks > 1) { 1109d3e2ce3bSHarvey Harrison u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq); 1110d3e2ce3bSHarvey Harrison u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq); 1111d06e021dSDavid S. Miller 1112d06e021dSDavid S. Miller if (!after(end_seq_0, end_seq_1) && 1113d06e021dSDavid S. Miller !before(start_seq_0, start_seq_1)) { 1114a2a385d6SEric Dumazet dup_sack = true; 1115e60402d0SIlpo Järvinen tcp_dsack_seen(tp); 1116de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), 1117de0744afSPavel Emelyanov LINUX_MIB_TCPDSACKOFORECV); 1118d06e021dSDavid S. Miller } 1119d06e021dSDavid S. Miller } 1120d06e021dSDavid S. Miller 1121d06e021dSDavid S. Miller /* D-SACK for already forgotten data... Do dumb counting. */ 11226e08d5e3SYuchung Cheng if (dup_sack && tp->undo_marker && tp->undo_retrans > 0 && 1123d06e021dSDavid S. Miller !after(end_seq_0, prior_snd_una) && 1124d06e021dSDavid S. Miller after(end_seq_0, tp->undo_marker)) 1125d06e021dSDavid S. Miller tp->undo_retrans--; 1126d06e021dSDavid S. Miller 1127d06e021dSDavid S. Miller return dup_sack; 1128d06e021dSDavid S. Miller } 1129d06e021dSDavid S. Miller 1130a1197f5aSIlpo Järvinen struct tcp_sacktag_state { 1131a1197f5aSIlpo Järvinen int reord; 1132a1197f5aSIlpo Järvinen int fack_count; 1133740b0f18SEric Dumazet long rtt_us; /* RTT measured by SACKing never-retransmitted data */ 1134a1197f5aSIlpo Järvinen int flag; 1135a1197f5aSIlpo Järvinen }; 1136a1197f5aSIlpo Järvinen 1137d1935942SIlpo Järvinen /* Check if skb is fully within the SACK block. In presence of GSO skbs, 1138d1935942SIlpo Järvinen * the incoming SACK may not exactly match but we can find smaller MSS 1139d1935942SIlpo Järvinen * aligned portion of it that matches. Therefore we might need to fragment 1140d1935942SIlpo Järvinen * which may fail and creates some hassle (caller must handle error case 1141d1935942SIlpo Järvinen * returns). 1142832d11c5SIlpo Järvinen * 1143832d11c5SIlpo Järvinen * FIXME: this could be merged to shift decision code 1144d1935942SIlpo Järvinen */ 11450f79efdcSAdrian Bunk static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb, 1146d1935942SIlpo Järvinen u32 start_seq, u32 end_seq) 1147d1935942SIlpo Järvinen { 1148a2a385d6SEric Dumazet int err; 1149a2a385d6SEric Dumazet bool in_sack; 1150d1935942SIlpo Järvinen unsigned int pkt_len; 1151adb92db8SIlpo Järvinen unsigned int mss; 1152d1935942SIlpo Järvinen 1153d1935942SIlpo Järvinen in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) && 1154d1935942SIlpo Järvinen !before(end_seq, TCP_SKB_CB(skb)->end_seq); 1155d1935942SIlpo Järvinen 1156d1935942SIlpo Järvinen if (tcp_skb_pcount(skb) > 1 && !in_sack && 1157d1935942SIlpo Järvinen after(TCP_SKB_CB(skb)->end_seq, start_seq)) { 1158adb92db8SIlpo Järvinen mss = tcp_skb_mss(skb); 1159d1935942SIlpo Järvinen in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq); 1160d1935942SIlpo Järvinen 1161adb92db8SIlpo Järvinen if (!in_sack) { 1162d1935942SIlpo Järvinen pkt_len = start_seq - TCP_SKB_CB(skb)->seq; 1163adb92db8SIlpo Järvinen if (pkt_len < mss) 1164adb92db8SIlpo Järvinen pkt_len = mss; 1165adb92db8SIlpo Järvinen } else { 1166d1935942SIlpo Järvinen pkt_len = end_seq - TCP_SKB_CB(skb)->seq; 1167adb92db8SIlpo Järvinen if (pkt_len < mss) 1168adb92db8SIlpo Järvinen return -EINVAL; 1169adb92db8SIlpo Järvinen } 1170adb92db8SIlpo Järvinen 1171adb92db8SIlpo Järvinen /* Round if necessary so that SACKs cover only full MSSes 1172adb92db8SIlpo Järvinen * and/or the remaining small portion (if present) 1173adb92db8SIlpo Järvinen */ 1174adb92db8SIlpo Järvinen if (pkt_len > mss) { 1175adb92db8SIlpo Järvinen unsigned int new_len = (pkt_len / mss) * mss; 1176adb92db8SIlpo Järvinen if (!in_sack && new_len < pkt_len) { 1177adb92db8SIlpo Järvinen new_len += mss; 11782cd0d743SNeal Cardwell if (new_len >= skb->len) 1179adb92db8SIlpo Järvinen return 0; 1180adb92db8SIlpo Järvinen } 1181adb92db8SIlpo Järvinen pkt_len = new_len; 1182adb92db8SIlpo Järvinen } 11836cc55e09SOctavian Purdila err = tcp_fragment(sk, skb, pkt_len, mss, GFP_ATOMIC); 1184d1935942SIlpo Järvinen if (err < 0) 1185d1935942SIlpo Järvinen return err; 1186d1935942SIlpo Järvinen } 1187d1935942SIlpo Järvinen 1188d1935942SIlpo Järvinen return in_sack; 1189d1935942SIlpo Järvinen } 1190d1935942SIlpo Järvinen 1191cc9a672eSNeal Cardwell /* Mark the given newly-SACKed range as such, adjusting counters and hints. */ 1192cc9a672eSNeal Cardwell static u8 tcp_sacktag_one(struct sock *sk, 1193cc9a672eSNeal Cardwell struct tcp_sacktag_state *state, u8 sacked, 1194cc9a672eSNeal Cardwell u32 start_seq, u32 end_seq, 1195740b0f18SEric Dumazet int dup_sack, int pcount, 1196740b0f18SEric Dumazet const struct skb_mstamp *xmit_time) 11979e10c47cSIlpo Järvinen { 11986859d494SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 1199a1197f5aSIlpo Järvinen int fack_count = state->fack_count; 12009e10c47cSIlpo Järvinen 12019e10c47cSIlpo Järvinen /* Account D-SACK for retransmitted packet. */ 12029e10c47cSIlpo Järvinen if (dup_sack && (sacked & TCPCB_RETRANS)) { 12036e08d5e3SYuchung Cheng if (tp->undo_marker && tp->undo_retrans > 0 && 1204cc9a672eSNeal Cardwell after(end_seq, tp->undo_marker)) 12059e10c47cSIlpo Järvinen tp->undo_retrans--; 1206ede9f3b1SIlpo Järvinen if (sacked & TCPCB_SACKED_ACKED) 1207a1197f5aSIlpo Järvinen state->reord = min(fack_count, state->reord); 12089e10c47cSIlpo Järvinen } 12099e10c47cSIlpo Järvinen 12109e10c47cSIlpo Järvinen /* Nothing to do; acked frame is about to be dropped (was ACKed). */ 1211cc9a672eSNeal Cardwell if (!after(end_seq, tp->snd_una)) 1212a1197f5aSIlpo Järvinen return sacked; 12139e10c47cSIlpo Järvinen 12149e10c47cSIlpo Järvinen if (!(sacked & TCPCB_SACKED_ACKED)) { 12159e10c47cSIlpo Järvinen if (sacked & TCPCB_SACKED_RETRANS) { 12169e10c47cSIlpo Järvinen /* If the segment is not tagged as lost, 12179e10c47cSIlpo Järvinen * we do not clear RETRANS, believing 12189e10c47cSIlpo Järvinen * that retransmission is still in flight. 12199e10c47cSIlpo Järvinen */ 12209e10c47cSIlpo Järvinen if (sacked & TCPCB_LOST) { 1221a1197f5aSIlpo Järvinen sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS); 1222f58b22fdSIlpo Järvinen tp->lost_out -= pcount; 1223f58b22fdSIlpo Järvinen tp->retrans_out -= pcount; 12249e10c47cSIlpo Järvinen } 12259e10c47cSIlpo Järvinen } else { 12269e10c47cSIlpo Järvinen if (!(sacked & TCPCB_RETRANS)) { 12279e10c47cSIlpo Järvinen /* New sack for not retransmitted frame, 12289e10c47cSIlpo Järvinen * which was in hole. It is reordering. 12299e10c47cSIlpo Järvinen */ 1230cc9a672eSNeal Cardwell if (before(start_seq, 12319e10c47cSIlpo Järvinen tcp_highest_sack_seq(tp))) 1232a1197f5aSIlpo Järvinen state->reord = min(fack_count, 1233a1197f5aSIlpo Järvinen state->reord); 1234e33099f9SYuchung Cheng if (!after(end_seq, tp->high_seq)) 1235e33099f9SYuchung Cheng state->flag |= FLAG_ORIG_SACK_ACKED; 123659c9af42SYuchung Cheng /* Pick the earliest sequence sacked for RTT */ 1237740b0f18SEric Dumazet if (state->rtt_us < 0) { 1238740b0f18SEric Dumazet struct skb_mstamp now; 1239740b0f18SEric Dumazet 1240740b0f18SEric Dumazet skb_mstamp_get(&now); 1241740b0f18SEric Dumazet state->rtt_us = skb_mstamp_us_delta(&now, 1242740b0f18SEric Dumazet xmit_time); 1243740b0f18SEric Dumazet } 12449e10c47cSIlpo Järvinen } 12459e10c47cSIlpo Järvinen 12469e10c47cSIlpo Järvinen if (sacked & TCPCB_LOST) { 1247a1197f5aSIlpo Järvinen sacked &= ~TCPCB_LOST; 1248f58b22fdSIlpo Järvinen tp->lost_out -= pcount; 12499e10c47cSIlpo Järvinen } 12509e10c47cSIlpo Järvinen } 12519e10c47cSIlpo Järvinen 1252a1197f5aSIlpo Järvinen sacked |= TCPCB_SACKED_ACKED; 1253a1197f5aSIlpo Järvinen state->flag |= FLAG_DATA_SACKED; 1254f58b22fdSIlpo Järvinen tp->sacked_out += pcount; 12559e10c47cSIlpo Järvinen 1256f58b22fdSIlpo Järvinen fack_count += pcount; 12579e10c47cSIlpo Järvinen 12589e10c47cSIlpo Järvinen /* Lost marker hint past SACKed? Tweak RFC3517 cnt */ 12599e10c47cSIlpo Järvinen if (!tcp_is_fack(tp) && (tp->lost_skb_hint != NULL) && 1260cc9a672eSNeal Cardwell before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq)) 1261f58b22fdSIlpo Järvinen tp->lost_cnt_hint += pcount; 12629e10c47cSIlpo Järvinen 12639e10c47cSIlpo Järvinen if (fack_count > tp->fackets_out) 12649e10c47cSIlpo Järvinen tp->fackets_out = fack_count; 12659e10c47cSIlpo Järvinen } 12669e10c47cSIlpo Järvinen 12679e10c47cSIlpo Järvinen /* D-SACK. We can detect redundant retransmission in S|R and plain R 12689e10c47cSIlpo Järvinen * frames and clear it. undo_retrans is decreased above, L|R frames 12699e10c47cSIlpo Järvinen * are accounted above as well. 12709e10c47cSIlpo Järvinen */ 1271a1197f5aSIlpo Järvinen if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) { 1272a1197f5aSIlpo Järvinen sacked &= ~TCPCB_SACKED_RETRANS; 1273f58b22fdSIlpo Järvinen tp->retrans_out -= pcount; 12749e10c47cSIlpo Järvinen } 12759e10c47cSIlpo Järvinen 1276a1197f5aSIlpo Järvinen return sacked; 12779e10c47cSIlpo Järvinen } 12789e10c47cSIlpo Järvinen 1279daef52baSNeal Cardwell /* Shift newly-SACKed bytes from this skb to the immediately previous 1280daef52baSNeal Cardwell * already-SACKed sk_buff. Mark the newly-SACKed bytes as such. 1281daef52baSNeal Cardwell */ 1282a2a385d6SEric Dumazet static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *skb, 1283a1197f5aSIlpo Järvinen struct tcp_sacktag_state *state, 12849ec06ff5SIlpo Järvinen unsigned int pcount, int shifted, int mss, 1285a2a385d6SEric Dumazet bool dup_sack) 1286832d11c5SIlpo Järvinen { 1287832d11c5SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 128850133161SIlpo Järvinen struct sk_buff *prev = tcp_write_queue_prev(sk, skb); 1289daef52baSNeal Cardwell u32 start_seq = TCP_SKB_CB(skb)->seq; /* start of newly-SACKed */ 1290daef52baSNeal Cardwell u32 end_seq = start_seq + shifted; /* end of newly-SACKed */ 1291832d11c5SIlpo Järvinen 1292832d11c5SIlpo Järvinen BUG_ON(!pcount); 1293832d11c5SIlpo Järvinen 12944c90d3b3SNeal Cardwell /* Adjust counters and hints for the newly sacked sequence 12954c90d3b3SNeal Cardwell * range but discard the return value since prev is already 12964c90d3b3SNeal Cardwell * marked. We must tag the range first because the seq 12974c90d3b3SNeal Cardwell * advancement below implicitly advances 12984c90d3b3SNeal Cardwell * tcp_highest_sack_seq() when skb is highest_sack. 12994c90d3b3SNeal Cardwell */ 13004c90d3b3SNeal Cardwell tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked, 130159c9af42SYuchung Cheng start_seq, end_seq, dup_sack, pcount, 1302740b0f18SEric Dumazet &skb->skb_mstamp); 13034c90d3b3SNeal Cardwell 13044c90d3b3SNeal Cardwell if (skb == tp->lost_skb_hint) 13050af2a0d0SNeal Cardwell tp->lost_cnt_hint += pcount; 13060af2a0d0SNeal Cardwell 1307832d11c5SIlpo Järvinen TCP_SKB_CB(prev)->end_seq += shifted; 1308832d11c5SIlpo Järvinen TCP_SKB_CB(skb)->seq += shifted; 1309832d11c5SIlpo Järvinen 1310cd7d8498SEric Dumazet tcp_skb_pcount_add(prev, pcount); 1311cd7d8498SEric Dumazet BUG_ON(tcp_skb_pcount(skb) < pcount); 1312cd7d8498SEric Dumazet tcp_skb_pcount_add(skb, -pcount); 1313832d11c5SIlpo Järvinen 1314832d11c5SIlpo Järvinen /* When we're adding to gso_segs == 1, gso_size will be zero, 1315832d11c5SIlpo Järvinen * in theory this shouldn't be necessary but as long as DSACK 1316832d11c5SIlpo Järvinen * code can come after this skb later on it's better to keep 1317832d11c5SIlpo Järvinen * setting gso_size to something. 1318832d11c5SIlpo Järvinen */ 1319832d11c5SIlpo Järvinen if (!skb_shinfo(prev)->gso_size) { 1320832d11c5SIlpo Järvinen skb_shinfo(prev)->gso_size = mss; 1321c9af6db4SPravin B Shelar skb_shinfo(prev)->gso_type = sk->sk_gso_type; 1322832d11c5SIlpo Järvinen } 1323832d11c5SIlpo Järvinen 1324832d11c5SIlpo Järvinen /* CHECKME: To clear or not to clear? Mimics normal skb currently */ 1325cd7d8498SEric Dumazet if (tcp_skb_pcount(skb) <= 1) { 1326832d11c5SIlpo Järvinen skb_shinfo(skb)->gso_size = 0; 1327c9af6db4SPravin B Shelar skb_shinfo(skb)->gso_type = 0; 1328832d11c5SIlpo Järvinen } 1329832d11c5SIlpo Järvinen 1330832d11c5SIlpo Järvinen /* Difference in this won't matter, both ACKed by the same cumul. ACK */ 1331832d11c5SIlpo Järvinen TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS); 1332832d11c5SIlpo Järvinen 1333832d11c5SIlpo Järvinen if (skb->len > 0) { 1334832d11c5SIlpo Järvinen BUG_ON(!tcp_skb_pcount(skb)); 1335111cc8b9SIlpo Järvinen NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTED); 1336a2a385d6SEric Dumazet return false; 1337832d11c5SIlpo Järvinen } 1338832d11c5SIlpo Järvinen 1339832d11c5SIlpo Järvinen /* Whole SKB was eaten :-) */ 1340832d11c5SIlpo Järvinen 134192ee76b6SIlpo Järvinen if (skb == tp->retransmit_skb_hint) 134292ee76b6SIlpo Järvinen tp->retransmit_skb_hint = prev; 134392ee76b6SIlpo Järvinen if (skb == tp->lost_skb_hint) { 134492ee76b6SIlpo Järvinen tp->lost_skb_hint = prev; 134592ee76b6SIlpo Järvinen tp->lost_cnt_hint -= tcp_skb_pcount(prev); 134692ee76b6SIlpo Järvinen } 134792ee76b6SIlpo Järvinen 13485e8a402fSEric Dumazet TCP_SKB_CB(prev)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags; 13495e8a402fSEric Dumazet if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) 13505e8a402fSEric Dumazet TCP_SKB_CB(prev)->end_seq++; 13515e8a402fSEric Dumazet 1352832d11c5SIlpo Järvinen if (skb == tcp_highest_sack(sk)) 1353832d11c5SIlpo Järvinen tcp_advance_highest_sack(sk, skb); 1354832d11c5SIlpo Järvinen 1355832d11c5SIlpo Järvinen tcp_unlink_write_queue(skb, sk); 1356832d11c5SIlpo Järvinen sk_wmem_free_skb(sk, skb); 1357832d11c5SIlpo Järvinen 1358111cc8b9SIlpo Järvinen NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKMERGED); 1359111cc8b9SIlpo Järvinen 1360a2a385d6SEric Dumazet return true; 1361832d11c5SIlpo Järvinen } 1362832d11c5SIlpo Järvinen 1363832d11c5SIlpo Järvinen /* I wish gso_size would have a bit more sane initialization than 1364832d11c5SIlpo Järvinen * something-or-zero which complicates things 1365832d11c5SIlpo Järvinen */ 1366cf533ea5SEric Dumazet static int tcp_skb_seglen(const struct sk_buff *skb) 1367832d11c5SIlpo Järvinen { 1368775ffabfSIlpo Järvinen return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb); 1369832d11c5SIlpo Järvinen } 1370832d11c5SIlpo Järvinen 1371832d11c5SIlpo Järvinen /* Shifting pages past head area doesn't work */ 1372cf533ea5SEric Dumazet static int skb_can_shift(const struct sk_buff *skb) 1373832d11c5SIlpo Järvinen { 1374832d11c5SIlpo Järvinen return !skb_headlen(skb) && skb_is_nonlinear(skb); 1375832d11c5SIlpo Järvinen } 1376832d11c5SIlpo Järvinen 1377832d11c5SIlpo Järvinen /* Try collapsing SACK blocks spanning across multiple skbs to a single 1378832d11c5SIlpo Järvinen * skb. 1379832d11c5SIlpo Järvinen */ 1380832d11c5SIlpo Järvinen static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb, 1381a1197f5aSIlpo Järvinen struct tcp_sacktag_state *state, 1382832d11c5SIlpo Järvinen u32 start_seq, u32 end_seq, 1383a2a385d6SEric Dumazet bool dup_sack) 1384832d11c5SIlpo Järvinen { 1385832d11c5SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 1386832d11c5SIlpo Järvinen struct sk_buff *prev; 1387832d11c5SIlpo Järvinen int mss; 1388832d11c5SIlpo Järvinen int pcount = 0; 1389832d11c5SIlpo Järvinen int len; 1390832d11c5SIlpo Järvinen int in_sack; 1391832d11c5SIlpo Järvinen 1392832d11c5SIlpo Järvinen if (!sk_can_gso(sk)) 1393832d11c5SIlpo Järvinen goto fallback; 1394832d11c5SIlpo Järvinen 1395832d11c5SIlpo Järvinen /* Normally R but no L won't result in plain S */ 1396832d11c5SIlpo Järvinen if (!dup_sack && 13979969ca5fSIlpo Järvinen (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS) 1398832d11c5SIlpo Järvinen goto fallback; 1399832d11c5SIlpo Järvinen if (!skb_can_shift(skb)) 1400832d11c5SIlpo Järvinen goto fallback; 1401832d11c5SIlpo Järvinen /* This frame is about to be dropped (was ACKed). */ 1402832d11c5SIlpo Järvinen if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una)) 1403832d11c5SIlpo Järvinen goto fallback; 1404832d11c5SIlpo Järvinen 1405832d11c5SIlpo Järvinen /* Can only happen with delayed DSACK + discard craziness */ 1406832d11c5SIlpo Järvinen if (unlikely(skb == tcp_write_queue_head(sk))) 1407832d11c5SIlpo Järvinen goto fallback; 1408832d11c5SIlpo Järvinen prev = tcp_write_queue_prev(sk, skb); 1409832d11c5SIlpo Järvinen 1410832d11c5SIlpo Järvinen if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) 1411832d11c5SIlpo Järvinen goto fallback; 1412832d11c5SIlpo Järvinen 1413832d11c5SIlpo Järvinen in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) && 1414832d11c5SIlpo Järvinen !before(end_seq, TCP_SKB_CB(skb)->end_seq); 1415832d11c5SIlpo Järvinen 1416832d11c5SIlpo Järvinen if (in_sack) { 1417832d11c5SIlpo Järvinen len = skb->len; 1418832d11c5SIlpo Järvinen pcount = tcp_skb_pcount(skb); 1419775ffabfSIlpo Järvinen mss = tcp_skb_seglen(skb); 1420832d11c5SIlpo Järvinen 1421832d11c5SIlpo Järvinen /* TODO: Fix DSACKs to not fragment already SACKed and we can 1422832d11c5SIlpo Järvinen * drop this restriction as unnecessary 1423832d11c5SIlpo Järvinen */ 1424775ffabfSIlpo Järvinen if (mss != tcp_skb_seglen(prev)) 1425832d11c5SIlpo Järvinen goto fallback; 1426832d11c5SIlpo Järvinen } else { 1427832d11c5SIlpo Järvinen if (!after(TCP_SKB_CB(skb)->end_seq, start_seq)) 1428832d11c5SIlpo Järvinen goto noop; 1429832d11c5SIlpo Järvinen /* CHECKME: This is non-MSS split case only?, this will 1430832d11c5SIlpo Järvinen * cause skipped skbs due to advancing loop btw, original 1431832d11c5SIlpo Järvinen * has that feature too 1432832d11c5SIlpo Järvinen */ 1433832d11c5SIlpo Järvinen if (tcp_skb_pcount(skb) <= 1) 1434832d11c5SIlpo Järvinen goto noop; 1435832d11c5SIlpo Järvinen 1436832d11c5SIlpo Järvinen in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq); 1437832d11c5SIlpo Järvinen if (!in_sack) { 1438832d11c5SIlpo Järvinen /* TODO: head merge to next could be attempted here 1439832d11c5SIlpo Järvinen * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)), 1440832d11c5SIlpo Järvinen * though it might not be worth of the additional hassle 1441832d11c5SIlpo Järvinen * 1442832d11c5SIlpo Järvinen * ...we can probably just fallback to what was done 1443832d11c5SIlpo Järvinen * previously. We could try merging non-SACKed ones 1444832d11c5SIlpo Järvinen * as well but it probably isn't going to buy off 1445832d11c5SIlpo Järvinen * because later SACKs might again split them, and 1446832d11c5SIlpo Järvinen * it would make skb timestamp tracking considerably 1447832d11c5SIlpo Järvinen * harder problem. 1448832d11c5SIlpo Järvinen */ 1449832d11c5SIlpo Järvinen goto fallback; 1450832d11c5SIlpo Järvinen } 1451832d11c5SIlpo Järvinen 1452832d11c5SIlpo Järvinen len = end_seq - TCP_SKB_CB(skb)->seq; 1453832d11c5SIlpo Järvinen BUG_ON(len < 0); 1454832d11c5SIlpo Järvinen BUG_ON(len > skb->len); 1455832d11c5SIlpo Järvinen 1456832d11c5SIlpo Järvinen /* MSS boundaries should be honoured or else pcount will 1457832d11c5SIlpo Järvinen * severely break even though it makes things bit trickier. 1458832d11c5SIlpo Järvinen * Optimize common case to avoid most of the divides 1459832d11c5SIlpo Järvinen */ 1460832d11c5SIlpo Järvinen mss = tcp_skb_mss(skb); 1461832d11c5SIlpo Järvinen 1462832d11c5SIlpo Järvinen /* TODO: Fix DSACKs to not fragment already SACKed and we can 1463832d11c5SIlpo Järvinen * drop this restriction as unnecessary 1464832d11c5SIlpo Järvinen */ 1465775ffabfSIlpo Järvinen if (mss != tcp_skb_seglen(prev)) 1466832d11c5SIlpo Järvinen goto fallback; 1467832d11c5SIlpo Järvinen 1468832d11c5SIlpo Järvinen if (len == mss) { 1469832d11c5SIlpo Järvinen pcount = 1; 1470832d11c5SIlpo Järvinen } else if (len < mss) { 1471832d11c5SIlpo Järvinen goto noop; 1472832d11c5SIlpo Järvinen } else { 1473832d11c5SIlpo Järvinen pcount = len / mss; 1474832d11c5SIlpo Järvinen len = pcount * mss; 1475832d11c5SIlpo Järvinen } 1476832d11c5SIlpo Järvinen } 1477832d11c5SIlpo Järvinen 14784648dc97SNeal Cardwell /* tcp_sacktag_one() won't SACK-tag ranges below snd_una */ 14794648dc97SNeal Cardwell if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una)) 14804648dc97SNeal Cardwell goto fallback; 14814648dc97SNeal Cardwell 1482832d11c5SIlpo Järvinen if (!skb_shift(prev, skb, len)) 1483832d11c5SIlpo Järvinen goto fallback; 14849ec06ff5SIlpo Järvinen if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack)) 1485832d11c5SIlpo Järvinen goto out; 1486832d11c5SIlpo Järvinen 1487832d11c5SIlpo Järvinen /* Hole filled allows collapsing with the next as well, this is very 1488832d11c5SIlpo Järvinen * useful when hole on every nth skb pattern happens 1489832d11c5SIlpo Järvinen */ 1490832d11c5SIlpo Järvinen if (prev == tcp_write_queue_tail(sk)) 1491832d11c5SIlpo Järvinen goto out; 1492832d11c5SIlpo Järvinen skb = tcp_write_queue_next(sk, prev); 1493832d11c5SIlpo Järvinen 1494f0bc52f3SIlpo Järvinen if (!skb_can_shift(skb) || 1495f0bc52f3SIlpo Järvinen (skb == tcp_send_head(sk)) || 1496f0bc52f3SIlpo Järvinen ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) || 1497775ffabfSIlpo Järvinen (mss != tcp_skb_seglen(skb))) 1498832d11c5SIlpo Järvinen goto out; 1499832d11c5SIlpo Järvinen 1500832d11c5SIlpo Järvinen len = skb->len; 1501832d11c5SIlpo Järvinen if (skb_shift(prev, skb, len)) { 1502832d11c5SIlpo Järvinen pcount += tcp_skb_pcount(skb); 15039ec06ff5SIlpo Järvinen tcp_shifted_skb(sk, skb, state, tcp_skb_pcount(skb), len, mss, 0); 1504832d11c5SIlpo Järvinen } 1505832d11c5SIlpo Järvinen 1506832d11c5SIlpo Järvinen out: 1507a1197f5aSIlpo Järvinen state->fack_count += pcount; 1508832d11c5SIlpo Järvinen return prev; 1509832d11c5SIlpo Järvinen 1510832d11c5SIlpo Järvinen noop: 1511832d11c5SIlpo Järvinen return skb; 1512832d11c5SIlpo Järvinen 1513832d11c5SIlpo Järvinen fallback: 1514111cc8b9SIlpo Järvinen NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK); 1515832d11c5SIlpo Järvinen return NULL; 1516832d11c5SIlpo Järvinen } 1517832d11c5SIlpo Järvinen 151868f8353bSIlpo Järvinen static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk, 151968f8353bSIlpo Järvinen struct tcp_sack_block *next_dup, 1520a1197f5aSIlpo Järvinen struct tcp_sacktag_state *state, 152168f8353bSIlpo Järvinen u32 start_seq, u32 end_seq, 1522a2a385d6SEric Dumazet bool dup_sack_in) 152368f8353bSIlpo Järvinen { 1524832d11c5SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 1525832d11c5SIlpo Järvinen struct sk_buff *tmp; 1526832d11c5SIlpo Järvinen 152768f8353bSIlpo Järvinen tcp_for_write_queue_from(skb, sk) { 152868f8353bSIlpo Järvinen int in_sack = 0; 1529a2a385d6SEric Dumazet bool dup_sack = dup_sack_in; 153068f8353bSIlpo Järvinen 153168f8353bSIlpo Järvinen if (skb == tcp_send_head(sk)) 153268f8353bSIlpo Järvinen break; 153368f8353bSIlpo Järvinen 153468f8353bSIlpo Järvinen /* queue is in-order => we can short-circuit the walk early */ 153568f8353bSIlpo Järvinen if (!before(TCP_SKB_CB(skb)->seq, end_seq)) 153668f8353bSIlpo Järvinen break; 153768f8353bSIlpo Järvinen 153868f8353bSIlpo Järvinen if ((next_dup != NULL) && 153968f8353bSIlpo Järvinen before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) { 154068f8353bSIlpo Järvinen in_sack = tcp_match_skb_to_sack(sk, skb, 154168f8353bSIlpo Järvinen next_dup->start_seq, 154268f8353bSIlpo Järvinen next_dup->end_seq); 154368f8353bSIlpo Järvinen if (in_sack > 0) 1544a2a385d6SEric Dumazet dup_sack = true; 154568f8353bSIlpo Järvinen } 154668f8353bSIlpo Järvinen 1547832d11c5SIlpo Järvinen /* skb reference here is a bit tricky to get right, since 1548832d11c5SIlpo Järvinen * shifting can eat and free both this skb and the next, 1549832d11c5SIlpo Järvinen * so not even _safe variant of the loop is enough. 1550832d11c5SIlpo Järvinen */ 1551832d11c5SIlpo Järvinen if (in_sack <= 0) { 1552a1197f5aSIlpo Järvinen tmp = tcp_shift_skb_data(sk, skb, state, 1553a1197f5aSIlpo Järvinen start_seq, end_seq, dup_sack); 1554832d11c5SIlpo Järvinen if (tmp != NULL) { 1555832d11c5SIlpo Järvinen if (tmp != skb) { 1556832d11c5SIlpo Järvinen skb = tmp; 1557832d11c5SIlpo Järvinen continue; 1558832d11c5SIlpo Järvinen } 1559832d11c5SIlpo Järvinen 1560832d11c5SIlpo Järvinen in_sack = 0; 1561832d11c5SIlpo Järvinen } else { 1562832d11c5SIlpo Järvinen in_sack = tcp_match_skb_to_sack(sk, skb, 1563832d11c5SIlpo Järvinen start_seq, 1564056834d9SIlpo Järvinen end_seq); 1565832d11c5SIlpo Järvinen } 1566832d11c5SIlpo Järvinen } 1567832d11c5SIlpo Järvinen 156868f8353bSIlpo Järvinen if (unlikely(in_sack < 0)) 156968f8353bSIlpo Järvinen break; 157068f8353bSIlpo Järvinen 1571832d11c5SIlpo Järvinen if (in_sack) { 1572cc9a672eSNeal Cardwell TCP_SKB_CB(skb)->sacked = 1573cc9a672eSNeal Cardwell tcp_sacktag_one(sk, 1574a1197f5aSIlpo Järvinen state, 1575cc9a672eSNeal Cardwell TCP_SKB_CB(skb)->sacked, 1576cc9a672eSNeal Cardwell TCP_SKB_CB(skb)->seq, 1577cc9a672eSNeal Cardwell TCP_SKB_CB(skb)->end_seq, 1578a1197f5aSIlpo Järvinen dup_sack, 157959c9af42SYuchung Cheng tcp_skb_pcount(skb), 1580740b0f18SEric Dumazet &skb->skb_mstamp); 158168f8353bSIlpo Järvinen 1582832d11c5SIlpo Järvinen if (!before(TCP_SKB_CB(skb)->seq, 1583832d11c5SIlpo Järvinen tcp_highest_sack_seq(tp))) 1584832d11c5SIlpo Järvinen tcp_advance_highest_sack(sk, skb); 1585832d11c5SIlpo Järvinen } 1586832d11c5SIlpo Järvinen 1587a1197f5aSIlpo Järvinen state->fack_count += tcp_skb_pcount(skb); 158868f8353bSIlpo Järvinen } 158968f8353bSIlpo Järvinen return skb; 159068f8353bSIlpo Järvinen } 159168f8353bSIlpo Järvinen 159268f8353bSIlpo Järvinen /* Avoid all extra work that is being done by sacktag while walking in 159368f8353bSIlpo Järvinen * a normal way 159468f8353bSIlpo Järvinen */ 159568f8353bSIlpo Järvinen static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk, 1596a1197f5aSIlpo Järvinen struct tcp_sacktag_state *state, 1597a1197f5aSIlpo Järvinen u32 skip_to_seq) 159868f8353bSIlpo Järvinen { 159968f8353bSIlpo Järvinen tcp_for_write_queue_from(skb, sk) { 160068f8353bSIlpo Järvinen if (skb == tcp_send_head(sk)) 160168f8353bSIlpo Järvinen break; 160268f8353bSIlpo Järvinen 1603e8bae275SIlpo Järvinen if (after(TCP_SKB_CB(skb)->end_seq, skip_to_seq)) 160468f8353bSIlpo Järvinen break; 1605d152a7d8SIlpo Järvinen 1606a1197f5aSIlpo Järvinen state->fack_count += tcp_skb_pcount(skb); 160768f8353bSIlpo Järvinen } 160868f8353bSIlpo Järvinen return skb; 160968f8353bSIlpo Järvinen } 161068f8353bSIlpo Järvinen 161168f8353bSIlpo Järvinen static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb, 161268f8353bSIlpo Järvinen struct sock *sk, 161368f8353bSIlpo Järvinen struct tcp_sack_block *next_dup, 1614a1197f5aSIlpo Järvinen struct tcp_sacktag_state *state, 1615a1197f5aSIlpo Järvinen u32 skip_to_seq) 161668f8353bSIlpo Järvinen { 161768f8353bSIlpo Järvinen if (next_dup == NULL) 161868f8353bSIlpo Järvinen return skb; 161968f8353bSIlpo Järvinen 162068f8353bSIlpo Järvinen if (before(next_dup->start_seq, skip_to_seq)) { 1621a1197f5aSIlpo Järvinen skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq); 1622a1197f5aSIlpo Järvinen skb = tcp_sacktag_walk(skb, sk, NULL, state, 162368f8353bSIlpo Järvinen next_dup->start_seq, next_dup->end_seq, 1624a1197f5aSIlpo Järvinen 1); 162568f8353bSIlpo Järvinen } 162668f8353bSIlpo Järvinen 162768f8353bSIlpo Järvinen return skb; 162868f8353bSIlpo Järvinen } 162968f8353bSIlpo Järvinen 1630cf533ea5SEric Dumazet static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache) 163168f8353bSIlpo Järvinen { 163268f8353bSIlpo Järvinen return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache); 163368f8353bSIlpo Järvinen } 163468f8353bSIlpo Järvinen 16351da177e4SLinus Torvalds static int 1636cf533ea5SEric Dumazet tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb, 1637740b0f18SEric Dumazet u32 prior_snd_una, long *sack_rtt_us) 16381da177e4SLinus Torvalds { 16391da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 1640cf533ea5SEric Dumazet const unsigned char *ptr = (skb_transport_header(ack_skb) + 16419c70220bSArnaldo Carvalho de Melo TCP_SKB_CB(ack_skb)->sacked); 1642fd6dad61SIlpo Järvinen struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2); 16434389ddedSAdam Langley struct tcp_sack_block sp[TCP_NUM_SACKS]; 164468f8353bSIlpo Järvinen struct tcp_sack_block *cache; 1645a1197f5aSIlpo Järvinen struct tcp_sacktag_state state; 164668f8353bSIlpo Järvinen struct sk_buff *skb; 16474389ddedSAdam Langley int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3); 1648fd6dad61SIlpo Järvinen int used_sacks; 1649a2a385d6SEric Dumazet bool found_dup_sack = false; 165068f8353bSIlpo Järvinen int i, j; 1651fda03fbbSBaruch Even int first_sack_index; 16521da177e4SLinus Torvalds 1653a1197f5aSIlpo Järvinen state.flag = 0; 1654a1197f5aSIlpo Järvinen state.reord = tp->packets_out; 1655740b0f18SEric Dumazet state.rtt_us = -1L; 1656a1197f5aSIlpo Järvinen 1657d738cd8fSIlpo Järvinen if (!tp->sacked_out) { 1658de83c058SIlpo Järvinen if (WARN_ON(tp->fackets_out)) 16591da177e4SLinus Torvalds tp->fackets_out = 0; 16606859d494SIlpo Järvinen tcp_highest_sack_reset(sk); 1661d738cd8fSIlpo Järvinen } 16621da177e4SLinus Torvalds 16631ed83465SPavel Emelyanov found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire, 1664d06e021dSDavid S. Miller num_sacks, prior_snd_una); 1665d06e021dSDavid S. Miller if (found_dup_sack) 1666a1197f5aSIlpo Järvinen state.flag |= FLAG_DSACKING_ACK; 16676f74651aSBaruch Even 16686f74651aSBaruch Even /* Eliminate too old ACKs, but take into 16696f74651aSBaruch Even * account more or less fresh ones, they can 16706f74651aSBaruch Even * contain valid SACK info. 16716f74651aSBaruch Even */ 16726f74651aSBaruch Even if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window)) 16736f74651aSBaruch Even return 0; 16746f74651aSBaruch Even 167596a2d41aSIlpo Järvinen if (!tp->packets_out) 167696a2d41aSIlpo Järvinen goto out; 167796a2d41aSIlpo Järvinen 1678fd6dad61SIlpo Järvinen used_sacks = 0; 1679fd6dad61SIlpo Järvinen first_sack_index = 0; 1680fd6dad61SIlpo Järvinen for (i = 0; i < num_sacks; i++) { 1681a2a385d6SEric Dumazet bool dup_sack = !i && found_dup_sack; 1682fd6dad61SIlpo Järvinen 1683d3e2ce3bSHarvey Harrison sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq); 1684d3e2ce3bSHarvey Harrison sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq); 1685fd6dad61SIlpo Järvinen 1686fd6dad61SIlpo Järvinen if (!tcp_is_sackblock_valid(tp, dup_sack, 1687fd6dad61SIlpo Järvinen sp[used_sacks].start_seq, 1688fd6dad61SIlpo Järvinen sp[used_sacks].end_seq)) { 168940b215e5SPavel Emelyanov int mib_idx; 169040b215e5SPavel Emelyanov 1691fd6dad61SIlpo Järvinen if (dup_sack) { 1692fd6dad61SIlpo Järvinen if (!tp->undo_marker) 169340b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO; 1694fd6dad61SIlpo Järvinen else 169540b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD; 1696fd6dad61SIlpo Järvinen } else { 1697fd6dad61SIlpo Järvinen /* Don't count olds caused by ACK reordering */ 1698fd6dad61SIlpo Järvinen if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) && 1699fd6dad61SIlpo Järvinen !after(sp[used_sacks].end_seq, tp->snd_una)) 1700fd6dad61SIlpo Järvinen continue; 170140b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPSACKDISCARD; 1702fd6dad61SIlpo Järvinen } 170340b215e5SPavel Emelyanov 1704de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), mib_idx); 1705fd6dad61SIlpo Järvinen if (i == 0) 1706fd6dad61SIlpo Järvinen first_sack_index = -1; 1707fd6dad61SIlpo Järvinen continue; 1708fd6dad61SIlpo Järvinen } 1709fd6dad61SIlpo Järvinen 1710fd6dad61SIlpo Järvinen /* Ignore very old stuff early */ 1711fd6dad61SIlpo Järvinen if (!after(sp[used_sacks].end_seq, prior_snd_una)) 1712fd6dad61SIlpo Järvinen continue; 1713fd6dad61SIlpo Järvinen 1714fd6dad61SIlpo Järvinen used_sacks++; 1715fd6dad61SIlpo Järvinen } 1716fd6dad61SIlpo Järvinen 17176a438bbeSStephen Hemminger /* order SACK blocks to allow in order walk of the retrans queue */ 1718fd6dad61SIlpo Järvinen for (i = used_sacks - 1; i > 0; i--) { 17196a438bbeSStephen Hemminger for (j = 0; j < i; j++) { 1720fd6dad61SIlpo Järvinen if (after(sp[j].start_seq, sp[j + 1].start_seq)) { 1721a0bffffcSIlpo Järvinen swap(sp[j], sp[j + 1]); 1722fda03fbbSBaruch Even 1723fda03fbbSBaruch Even /* Track where the first SACK block goes to */ 1724fda03fbbSBaruch Even if (j == first_sack_index) 1725fda03fbbSBaruch Even first_sack_index = j + 1; 17266a438bbeSStephen Hemminger } 17276a438bbeSStephen Hemminger } 17286a438bbeSStephen Hemminger } 17296a438bbeSStephen Hemminger 173068f8353bSIlpo Järvinen skb = tcp_write_queue_head(sk); 1731a1197f5aSIlpo Järvinen state.fack_count = 0; 173268f8353bSIlpo Järvinen i = 0; 173368f8353bSIlpo Järvinen 173468f8353bSIlpo Järvinen if (!tp->sacked_out) { 173568f8353bSIlpo Järvinen /* It's already past, so skip checking against it */ 173668f8353bSIlpo Järvinen cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache); 173768f8353bSIlpo Järvinen } else { 173868f8353bSIlpo Järvinen cache = tp->recv_sack_cache; 173968f8353bSIlpo Järvinen /* Skip empty blocks in at head of the cache */ 174068f8353bSIlpo Järvinen while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq && 174168f8353bSIlpo Järvinen !cache->end_seq) 174268f8353bSIlpo Järvinen cache++; 1743fda03fbbSBaruch Even } 1744fda03fbbSBaruch Even 174568f8353bSIlpo Järvinen while (i < used_sacks) { 1746fd6dad61SIlpo Järvinen u32 start_seq = sp[i].start_seq; 1747fd6dad61SIlpo Järvinen u32 end_seq = sp[i].end_seq; 1748a2a385d6SEric Dumazet bool dup_sack = (found_dup_sack && (i == first_sack_index)); 174968f8353bSIlpo Järvinen struct tcp_sack_block *next_dup = NULL; 1750e56d6cd6SIlpo Järvinen 175168f8353bSIlpo Järvinen if (found_dup_sack && ((i + 1) == first_sack_index)) 175268f8353bSIlpo Järvinen next_dup = &sp[i + 1]; 17531da177e4SLinus Torvalds 175468f8353bSIlpo Järvinen /* Skip too early cached blocks */ 175568f8353bSIlpo Järvinen while (tcp_sack_cache_ok(tp, cache) && 175668f8353bSIlpo Järvinen !before(start_seq, cache->end_seq)) 175768f8353bSIlpo Järvinen cache++; 17581da177e4SLinus Torvalds 175968f8353bSIlpo Järvinen /* Can skip some work by looking recv_sack_cache? */ 176068f8353bSIlpo Järvinen if (tcp_sack_cache_ok(tp, cache) && !dup_sack && 176168f8353bSIlpo Järvinen after(end_seq, cache->start_seq)) { 1762fe067e8aSDavid S. Miller 176368f8353bSIlpo Järvinen /* Head todo? */ 176468f8353bSIlpo Järvinen if (before(start_seq, cache->start_seq)) { 1765a1197f5aSIlpo Järvinen skb = tcp_sacktag_skip(skb, sk, &state, 1766a1197f5aSIlpo Järvinen start_seq); 1767056834d9SIlpo Järvinen skb = tcp_sacktag_walk(skb, sk, next_dup, 1768a1197f5aSIlpo Järvinen &state, 1769056834d9SIlpo Järvinen start_seq, 1770056834d9SIlpo Järvinen cache->start_seq, 1771a1197f5aSIlpo Järvinen dup_sack); 1772fda03fbbSBaruch Even } 17736a438bbeSStephen Hemminger 177468f8353bSIlpo Järvinen /* Rest of the block already fully processed? */ 177520de20beSIlpo Järvinen if (!after(end_seq, cache->end_seq)) 177620de20beSIlpo Järvinen goto advance_sp; 177720de20beSIlpo Järvinen 1778056834d9SIlpo Järvinen skb = tcp_maybe_skipping_dsack(skb, sk, next_dup, 1779a1197f5aSIlpo Järvinen &state, 1780a1197f5aSIlpo Järvinen cache->end_seq); 178168f8353bSIlpo Järvinen 178268f8353bSIlpo Järvinen /* ...tail remains todo... */ 17836859d494SIlpo Järvinen if (tcp_highest_sack_seq(tp) == cache->end_seq) { 178420de20beSIlpo Järvinen /* ...but better entrypoint exists! */ 17856859d494SIlpo Järvinen skb = tcp_highest_sack(sk); 17866859d494SIlpo Järvinen if (skb == NULL) 17876859d494SIlpo Järvinen break; 1788a1197f5aSIlpo Järvinen state.fack_count = tp->fackets_out; 178968f8353bSIlpo Järvinen cache++; 179068f8353bSIlpo Järvinen goto walk; 1791e56d6cd6SIlpo Järvinen } 1792e56d6cd6SIlpo Järvinen 1793a1197f5aSIlpo Järvinen skb = tcp_sacktag_skip(skb, sk, &state, cache->end_seq); 179468f8353bSIlpo Järvinen /* Check overlap against next cached too (past this one already) */ 179568f8353bSIlpo Järvinen cache++; 179668f8353bSIlpo Järvinen continue; 17971da177e4SLinus Torvalds } 1798fbd52eb2SIlpo Järvinen 17996859d494SIlpo Järvinen if (!before(start_seq, tcp_highest_sack_seq(tp))) { 18006859d494SIlpo Järvinen skb = tcp_highest_sack(sk); 18016859d494SIlpo Järvinen if (skb == NULL) 18026859d494SIlpo Järvinen break; 1803a1197f5aSIlpo Järvinen state.fack_count = tp->fackets_out; 180468f8353bSIlpo Järvinen } 1805a1197f5aSIlpo Järvinen skb = tcp_sacktag_skip(skb, sk, &state, start_seq); 180668f8353bSIlpo Järvinen 180768f8353bSIlpo Järvinen walk: 1808a1197f5aSIlpo Järvinen skb = tcp_sacktag_walk(skb, sk, next_dup, &state, 1809a1197f5aSIlpo Järvinen start_seq, end_seq, dup_sack); 181068f8353bSIlpo Järvinen 181168f8353bSIlpo Järvinen advance_sp: 181268f8353bSIlpo Järvinen i++; 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds 181568f8353bSIlpo Järvinen /* Clear the head of the cache sack blocks so we can skip it next time */ 181668f8353bSIlpo Järvinen for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) { 181768f8353bSIlpo Järvinen tp->recv_sack_cache[i].start_seq = 0; 181868f8353bSIlpo Järvinen tp->recv_sack_cache[i].end_seq = 0; 181968f8353bSIlpo Järvinen } 182068f8353bSIlpo Järvinen for (j = 0; j < used_sacks; j++) 182168f8353bSIlpo Järvinen tp->recv_sack_cache[i++] = sp[j]; 182268f8353bSIlpo Järvinen 1823407ef1deSIlpo Järvinen tcp_mark_lost_retrans(sk); 18241da177e4SLinus Torvalds 182586426c22SIlpo Järvinen tcp_verify_left_out(tp); 182686426c22SIlpo Järvinen 1827a1197f5aSIlpo Järvinen if ((state.reord < tp->fackets_out) && 18289b44190dSYuchung Cheng ((inet_csk(sk)->icsk_ca_state != TCP_CA_Loss) || tp->undo_marker)) 1829a1197f5aSIlpo Järvinen tcp_update_reordering(sk, tp->fackets_out - state.reord, 0); 18301da177e4SLinus Torvalds 183196a2d41aSIlpo Järvinen out: 183296a2d41aSIlpo Järvinen 18331da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0 1834547b792cSIlpo Järvinen WARN_ON((int)tp->sacked_out < 0); 1835547b792cSIlpo Järvinen WARN_ON((int)tp->lost_out < 0); 1836547b792cSIlpo Järvinen WARN_ON((int)tp->retrans_out < 0); 1837547b792cSIlpo Järvinen WARN_ON((int)tcp_packets_in_flight(tp) < 0); 18381da177e4SLinus Torvalds #endif 1839740b0f18SEric Dumazet *sack_rtt_us = state.rtt_us; 1840a1197f5aSIlpo Järvinen return state.flag; 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds 1843882bebaaSIlpo Järvinen /* Limits sacked_out so that sum with lost_out isn't ever larger than 1844a2a385d6SEric Dumazet * packets_out. Returns false if sacked_out adjustement wasn't necessary. 184530935cf4SIlpo Järvinen */ 1846a2a385d6SEric Dumazet static bool tcp_limit_reno_sacked(struct tcp_sock *tp) 18474ddf6676SIlpo Järvinen { 18484ddf6676SIlpo Järvinen u32 holes; 18494ddf6676SIlpo Järvinen 18504ddf6676SIlpo Järvinen holes = max(tp->lost_out, 1U); 18514ddf6676SIlpo Järvinen holes = min(holes, tp->packets_out); 18524ddf6676SIlpo Järvinen 18534ddf6676SIlpo Järvinen if ((tp->sacked_out + holes) > tp->packets_out) { 18544ddf6676SIlpo Järvinen tp->sacked_out = tp->packets_out - holes; 1855a2a385d6SEric Dumazet return true; 18564ddf6676SIlpo Järvinen } 1857a2a385d6SEric Dumazet return false; 1858882bebaaSIlpo Järvinen } 1859882bebaaSIlpo Järvinen 1860882bebaaSIlpo Järvinen /* If we receive more dupacks than we expected counting segments 1861882bebaaSIlpo Järvinen * in assumption of absent reordering, interpret this as reordering. 1862882bebaaSIlpo Järvinen * The only another reason could be bug in receiver TCP. 1863882bebaaSIlpo Järvinen */ 1864882bebaaSIlpo Järvinen static void tcp_check_reno_reordering(struct sock *sk, const int addend) 1865882bebaaSIlpo Järvinen { 1866882bebaaSIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 1867882bebaaSIlpo Järvinen if (tcp_limit_reno_sacked(tp)) 1868882bebaaSIlpo Järvinen tcp_update_reordering(sk, tp->packets_out + addend, 0); 18694ddf6676SIlpo Järvinen } 18704ddf6676SIlpo Järvinen 18714ddf6676SIlpo Järvinen /* Emulate SACKs for SACKless connection: account for a new dupack. */ 18724ddf6676SIlpo Järvinen 18734ddf6676SIlpo Järvinen static void tcp_add_reno_sack(struct sock *sk) 18744ddf6676SIlpo Järvinen { 18754ddf6676SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 18764ddf6676SIlpo Järvinen tp->sacked_out++; 18774ddf6676SIlpo Järvinen tcp_check_reno_reordering(sk, 0); 1878005903bcSIlpo Järvinen tcp_verify_left_out(tp); 18794ddf6676SIlpo Järvinen } 18804ddf6676SIlpo Järvinen 18814ddf6676SIlpo Järvinen /* Account for ACK, ACKing some data in Reno Recovery phase. */ 18824ddf6676SIlpo Järvinen 18834ddf6676SIlpo Järvinen static void tcp_remove_reno_sacks(struct sock *sk, int acked) 18844ddf6676SIlpo Järvinen { 18854ddf6676SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 18864ddf6676SIlpo Järvinen 18874ddf6676SIlpo Järvinen if (acked > 0) { 18884ddf6676SIlpo Järvinen /* One ACK acked hole. The rest eat duplicate ACKs. */ 18894ddf6676SIlpo Järvinen if (acked - 1 >= tp->sacked_out) 18904ddf6676SIlpo Järvinen tp->sacked_out = 0; 18914ddf6676SIlpo Järvinen else 18924ddf6676SIlpo Järvinen tp->sacked_out -= acked - 1; 18934ddf6676SIlpo Järvinen } 18944ddf6676SIlpo Järvinen tcp_check_reno_reordering(sk, acked); 1895005903bcSIlpo Järvinen tcp_verify_left_out(tp); 18964ddf6676SIlpo Järvinen } 18974ddf6676SIlpo Järvinen 18984ddf6676SIlpo Järvinen static inline void tcp_reset_reno_sack(struct tcp_sock *tp) 18994ddf6676SIlpo Järvinen { 19004ddf6676SIlpo Järvinen tp->sacked_out = 0; 19014ddf6676SIlpo Järvinen } 19024ddf6676SIlpo Järvinen 1903989e04c5SYuchung Cheng void tcp_clear_retrans(struct tcp_sock *tp) 19041da177e4SLinus Torvalds { 19051da177e4SLinus Torvalds tp->retrans_out = 0; 19061da177e4SLinus Torvalds tp->lost_out = 0; 19071da177e4SLinus Torvalds tp->undo_marker = 0; 19086e08d5e3SYuchung Cheng tp->undo_retrans = -1; 19094cd82999SIlpo Järvinen tp->fackets_out = 0; 19104cd82999SIlpo Järvinen tp->sacked_out = 0; 19114cd82999SIlpo Järvinen } 19124cd82999SIlpo Järvinen 1913989e04c5SYuchung Cheng static inline void tcp_init_undo(struct tcp_sock *tp) 1914989e04c5SYuchung Cheng { 1915989e04c5SYuchung Cheng tp->undo_marker = tp->snd_una; 1916989e04c5SYuchung Cheng /* Retransmission still in flight may cause DSACKs later. */ 1917989e04c5SYuchung Cheng tp->undo_retrans = tp->retrans_out ? : -1; 1918989e04c5SYuchung Cheng } 1919989e04c5SYuchung Cheng 19205ae344c9SNeal Cardwell /* Enter Loss state. If we detect SACK reneging, forget all SACK information 19211da177e4SLinus Torvalds * and reset tags completely, otherwise preserve SACKs. If receiver 19221da177e4SLinus Torvalds * dropped its ofo queue, we will know this due to reneging detection. 19231da177e4SLinus Torvalds */ 19245ae344c9SNeal Cardwell void tcp_enter_loss(struct sock *sk) 19251da177e4SLinus Torvalds { 19266687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 19271da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 19281da177e4SLinus Torvalds struct sk_buff *skb; 1929e33099f9SYuchung Cheng bool new_recovery = false; 19305ae344c9SNeal Cardwell bool is_reneg; /* is receiver reneging on SACKs? */ 19311da177e4SLinus Torvalds 19321da177e4SLinus Torvalds /* Reduce ssthresh if it has not yet been made inside this window. */ 1933e33099f9SYuchung Cheng if (icsk->icsk_ca_state <= TCP_CA_Disorder || 1934e33099f9SYuchung Cheng !after(tp->high_seq, tp->snd_una) || 19356687e988SArnaldo Carvalho de Melo (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) { 1936e33099f9SYuchung Cheng new_recovery = true; 19376687e988SArnaldo Carvalho de Melo tp->prior_ssthresh = tcp_current_ssthresh(sk); 19386687e988SArnaldo Carvalho de Melo tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk); 19396687e988SArnaldo Carvalho de Melo tcp_ca_event(sk, CA_EVENT_LOSS); 1940989e04c5SYuchung Cheng tcp_init_undo(tp); 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds tp->snd_cwnd = 1; 19431da177e4SLinus Torvalds tp->snd_cwnd_cnt = 0; 19441da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 19451da177e4SLinus Torvalds 1946989e04c5SYuchung Cheng tp->retrans_out = 0; 1947989e04c5SYuchung Cheng tp->lost_out = 0; 19484cd82999SIlpo Järvinen 19494cd82999SIlpo Järvinen if (tcp_is_reno(tp)) 19504cd82999SIlpo Järvinen tcp_reset_reno_sack(tp); 19511da177e4SLinus Torvalds 19525ae344c9SNeal Cardwell skb = tcp_write_queue_head(sk); 19535ae344c9SNeal Cardwell is_reneg = skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED); 19545ae344c9SNeal Cardwell if (is_reneg) { 19555ae344c9SNeal Cardwell NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSACKRENEGING); 19564cd82999SIlpo Järvinen tp->sacked_out = 0; 19574cd82999SIlpo Järvinen tp->fackets_out = 0; 1958b7689205SIlpo Järvinen } 195964edc273SIlpo Järvinen tcp_clear_all_retrans_hints(tp); 19601da177e4SLinus Torvalds 1961fe067e8aSDavid S. Miller tcp_for_write_queue(skb, sk) { 1962fe067e8aSDavid S. Miller if (skb == tcp_send_head(sk)) 1963fe067e8aSDavid S. Miller break; 19644cd82999SIlpo Järvinen 19651da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED; 19665ae344c9SNeal Cardwell if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED) || is_reneg) { 19671da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED; 19681da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; 19691da177e4SLinus Torvalds tp->lost_out += tcp_skb_pcount(skb); 1970006f582cSIlpo Järvinen tp->retransmit_high = TCP_SKB_CB(skb)->end_seq; 19711da177e4SLinus Torvalds } 19721da177e4SLinus Torvalds } 1973005903bcSIlpo Järvinen tcp_verify_left_out(tp); 19741da177e4SLinus Torvalds 197574c181d5SYuchung Cheng /* Timeout in disordered state after receiving substantial DUPACKs 197674c181d5SYuchung Cheng * suggests that the degree of reordering is over-estimated. 197774c181d5SYuchung Cheng */ 197874c181d5SYuchung Cheng if (icsk->icsk_ca_state <= TCP_CA_Disorder && 197974c181d5SYuchung Cheng tp->sacked_out >= sysctl_tcp_reordering) 19801da177e4SLinus Torvalds tp->reordering = min_t(unsigned int, tp->reordering, 19811da177e4SLinus Torvalds sysctl_tcp_reordering); 19826687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Loss); 19831da177e4SLinus Torvalds tp->high_seq = tp->snd_nxt; 1984735d3831SFlorian Westphal tcp_ecn_queue_cwr(tp); 1985e33099f9SYuchung Cheng 1986e33099f9SYuchung Cheng /* F-RTO RFC5682 sec 3.1 step 1: retransmit SND.UNA if no previous 1987e33099f9SYuchung Cheng * loss recovery is underway except recurring timeout(s) on 1988e33099f9SYuchung Cheng * the same SND.UNA (sec 3.2). Disable F-RTO on path MTU probing 1989e33099f9SYuchung Cheng */ 1990e33099f9SYuchung Cheng tp->frto = sysctl_tcp_frto && 1991e33099f9SYuchung Cheng (new_recovery || icsk->icsk_retransmits) && 1992e33099f9SYuchung Cheng !inet_csk(sk)->icsk_mtup.probe_size; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 1995cadbd031SIlpo Järvinen /* If ACK arrived pointing to a remembered SACK, it means that our 1996cadbd031SIlpo Järvinen * remembered SACKs do not reflect real state of receiver i.e. 19971da177e4SLinus Torvalds * receiver _host_ is heavily congested (or buggy). 1998cadbd031SIlpo Järvinen * 19995ae344c9SNeal Cardwell * To avoid big spurious retransmission bursts due to transient SACK 20005ae344c9SNeal Cardwell * scoreboard oddities that look like reneging, we give the receiver a 20015ae344c9SNeal Cardwell * little time (max(RTT/2, 10ms)) to send us some more ACKs that will 20025ae344c9SNeal Cardwell * restore sanity to the SACK scoreboard. If the apparent reneging 20035ae344c9SNeal Cardwell * persists until this RTO then we'll clear the SACK scoreboard. 20041da177e4SLinus Torvalds */ 2005a2a385d6SEric Dumazet static bool tcp_check_sack_reneging(struct sock *sk, int flag) 2006cadbd031SIlpo Järvinen { 2007cadbd031SIlpo Järvinen if (flag & FLAG_SACK_RENEGING) { 20085ae344c9SNeal Cardwell struct tcp_sock *tp = tcp_sk(sk); 20095ae344c9SNeal Cardwell unsigned long delay = max(usecs_to_jiffies(tp->srtt_us >> 4), 20105ae344c9SNeal Cardwell msecs_to_jiffies(10)); 20111da177e4SLinus Torvalds 2012463c84b9SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 20135ae344c9SNeal Cardwell delay, TCP_RTO_MAX); 2014a2a385d6SEric Dumazet return true; 20151da177e4SLinus Torvalds } 2016a2a385d6SEric Dumazet return false; 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds 2019cf533ea5SEric Dumazet static inline int tcp_fackets_out(const struct tcp_sock *tp) 20201da177e4SLinus Torvalds { 2021e60402d0SIlpo Järvinen return tcp_is_reno(tp) ? tp->sacked_out + 1 : tp->fackets_out; 20221da177e4SLinus Torvalds } 20231da177e4SLinus Torvalds 202485cc391cSIlpo Järvinen /* Heurestics to calculate number of duplicate ACKs. There's no dupACKs 202585cc391cSIlpo Järvinen * counter when SACK is enabled (without SACK, sacked_out is used for 202685cc391cSIlpo Järvinen * that purpose). 202785cc391cSIlpo Järvinen * 202885cc391cSIlpo Järvinen * Instead, with FACK TCP uses fackets_out that includes both SACKed 202985cc391cSIlpo Järvinen * segments up to the highest received SACK block so far and holes in 203085cc391cSIlpo Järvinen * between them. 203185cc391cSIlpo Järvinen * 203285cc391cSIlpo Järvinen * With reordering, holes may still be in flight, so RFC3517 recovery 203385cc391cSIlpo Järvinen * uses pure sacked_out (total number of SACKed segments) even though 203485cc391cSIlpo Järvinen * it violates the RFC that uses duplicate ACKs, often these are equal 203585cc391cSIlpo Järvinen * but when e.g. out-of-window ACKs or packet duplication occurs, 203685cc391cSIlpo Järvinen * they differ. Since neither occurs due to loss, TCP should really 203785cc391cSIlpo Järvinen * ignore them. 203885cc391cSIlpo Järvinen */ 2039cf533ea5SEric Dumazet static inline int tcp_dupack_heuristics(const struct tcp_sock *tp) 204085cc391cSIlpo Järvinen { 204185cc391cSIlpo Järvinen return tcp_is_fack(tp) ? tp->fackets_out : tp->sacked_out + 1; 204285cc391cSIlpo Järvinen } 204385cc391cSIlpo Järvinen 2044750ea2baSYuchung Cheng static bool tcp_pause_early_retransmit(struct sock *sk, int flag) 2045750ea2baSYuchung Cheng { 2046750ea2baSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 2047750ea2baSYuchung Cheng unsigned long delay; 2048750ea2baSYuchung Cheng 2049750ea2baSYuchung Cheng /* Delay early retransmit and entering fast recovery for 2050750ea2baSYuchung Cheng * max(RTT/4, 2msec) unless ack has ECE mark, no RTT samples 2051750ea2baSYuchung Cheng * available, or RTO is scheduled to fire first. 2052750ea2baSYuchung Cheng */ 20536ba8a3b1SNandita Dukkipati if (sysctl_tcp_early_retrans < 2 || sysctl_tcp_early_retrans > 3 || 2054740b0f18SEric Dumazet (flag & FLAG_ECE) || !tp->srtt_us) 2055750ea2baSYuchung Cheng return false; 2056750ea2baSYuchung Cheng 2057740b0f18SEric Dumazet delay = max(usecs_to_jiffies(tp->srtt_us >> 5), 2058740b0f18SEric Dumazet msecs_to_jiffies(2)); 2059740b0f18SEric Dumazet 2060750ea2baSYuchung Cheng if (!time_after(inet_csk(sk)->icsk_timeout, (jiffies + delay))) 2061750ea2baSYuchung Cheng return false; 2062750ea2baSYuchung Cheng 20636ba8a3b1SNandita Dukkipati inet_csk_reset_xmit_timer(sk, ICSK_TIME_EARLY_RETRANS, delay, 20646ba8a3b1SNandita Dukkipati TCP_RTO_MAX); 2065750ea2baSYuchung Cheng return true; 2066750ea2baSYuchung Cheng } 2067750ea2baSYuchung Cheng 20681da177e4SLinus Torvalds /* Linux NewReno/SACK/FACK/ECN state machine. 20691da177e4SLinus Torvalds * -------------------------------------- 20701da177e4SLinus Torvalds * 20711da177e4SLinus Torvalds * "Open" Normal state, no dubious events, fast path. 20721da177e4SLinus Torvalds * "Disorder" In all the respects it is "Open", 20731da177e4SLinus Torvalds * but requires a bit more attention. It is entered when 20741da177e4SLinus Torvalds * we see some SACKs or dupacks. It is split of "Open" 20751da177e4SLinus Torvalds * mainly to move some processing from fast path to slow one. 20761da177e4SLinus Torvalds * "CWR" CWND was reduced due to some Congestion Notification event. 20771da177e4SLinus Torvalds * It can be ECN, ICMP source quench, local device congestion. 20781da177e4SLinus Torvalds * "Recovery" CWND was reduced, we are fast-retransmitting. 20791da177e4SLinus Torvalds * "Loss" CWND was reduced due to RTO timeout or SACK reneging. 20801da177e4SLinus Torvalds * 20811da177e4SLinus Torvalds * tcp_fastretrans_alert() is entered: 20821da177e4SLinus Torvalds * - each incoming ACK, if state is not "Open" 20831da177e4SLinus Torvalds * - when arrived ACK is unusual, namely: 20841da177e4SLinus Torvalds * * SACK 20851da177e4SLinus Torvalds * * Duplicate ACK. 20861da177e4SLinus Torvalds * * ECN ECE. 20871da177e4SLinus Torvalds * 20881da177e4SLinus Torvalds * Counting packets in flight is pretty simple. 20891da177e4SLinus Torvalds * 20901da177e4SLinus Torvalds * in_flight = packets_out - left_out + retrans_out 20911da177e4SLinus Torvalds * 20921da177e4SLinus Torvalds * packets_out is SND.NXT-SND.UNA counted in packets. 20931da177e4SLinus Torvalds * 20941da177e4SLinus Torvalds * retrans_out is number of retransmitted segments. 20951da177e4SLinus Torvalds * 20961da177e4SLinus Torvalds * left_out is number of segments left network, but not ACKed yet. 20971da177e4SLinus Torvalds * 20981da177e4SLinus Torvalds * left_out = sacked_out + lost_out 20991da177e4SLinus Torvalds * 21001da177e4SLinus Torvalds * sacked_out: Packets, which arrived to receiver out of order 21011da177e4SLinus Torvalds * and hence not ACKed. With SACKs this number is simply 21021da177e4SLinus Torvalds * amount of SACKed data. Even without SACKs 21031da177e4SLinus Torvalds * it is easy to give pretty reliable estimate of this number, 21041da177e4SLinus Torvalds * counting duplicate ACKs. 21051da177e4SLinus Torvalds * 21061da177e4SLinus Torvalds * lost_out: Packets lost by network. TCP has no explicit 21071da177e4SLinus Torvalds * "loss notification" feedback from network (for now). 21081da177e4SLinus Torvalds * It means that this number can be only _guessed_. 21091da177e4SLinus Torvalds * Actually, it is the heuristics to predict lossage that 21101da177e4SLinus Torvalds * distinguishes different algorithms. 21111da177e4SLinus Torvalds * 21121da177e4SLinus Torvalds * F.e. after RTO, when all the queue is considered as lost, 21131da177e4SLinus Torvalds * lost_out = packets_out and in_flight = retrans_out. 21141da177e4SLinus Torvalds * 21151da177e4SLinus Torvalds * Essentially, we have now two algorithms counting 21161da177e4SLinus Torvalds * lost packets. 21171da177e4SLinus Torvalds * 21181da177e4SLinus Torvalds * FACK: It is the simplest heuristics. As soon as we decided 21191da177e4SLinus Torvalds * that something is lost, we decide that _all_ not SACKed 21201da177e4SLinus Torvalds * packets until the most forward SACK are lost. I.e. 21211da177e4SLinus Torvalds * lost_out = fackets_out - sacked_out and left_out = fackets_out. 21221da177e4SLinus Torvalds * It is absolutely correct estimate, if network does not reorder 21231da177e4SLinus Torvalds * packets. And it loses any connection to reality when reordering 21241da177e4SLinus Torvalds * takes place. We use FACK by default until reordering 21251da177e4SLinus Torvalds * is suspected on the path to this destination. 21261da177e4SLinus Torvalds * 21271da177e4SLinus Torvalds * NewReno: when Recovery is entered, we assume that one segment 21281da177e4SLinus Torvalds * is lost (classic Reno). While we are in Recovery and 21291da177e4SLinus Torvalds * a partial ACK arrives, we assume that one more packet 21301da177e4SLinus Torvalds * is lost (NewReno). This heuristics are the same in NewReno 21311da177e4SLinus Torvalds * and SACK. 21321da177e4SLinus Torvalds * 21331da177e4SLinus Torvalds * Imagine, that's all! Forget about all this shamanism about CWND inflation 21341da177e4SLinus Torvalds * deflation etc. CWND is real congestion window, never inflated, changes 21351da177e4SLinus Torvalds * only according to classic VJ rules. 21361da177e4SLinus Torvalds * 21371da177e4SLinus Torvalds * Really tricky (and requiring careful tuning) part of algorithm 21381da177e4SLinus Torvalds * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue(). 21391da177e4SLinus Torvalds * The first determines the moment _when_ we should reduce CWND and, 21401da177e4SLinus Torvalds * hence, slow down forward transmission. In fact, it determines the moment 21411da177e4SLinus Torvalds * when we decide that hole is caused by loss, rather than by a reorder. 21421da177e4SLinus Torvalds * 21431da177e4SLinus Torvalds * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill 21441da177e4SLinus Torvalds * holes, caused by lost packets. 21451da177e4SLinus Torvalds * 21461da177e4SLinus Torvalds * And the most logically complicated part of algorithm is undo 21471da177e4SLinus Torvalds * heuristics. We detect false retransmits due to both too early 21481da177e4SLinus Torvalds * fast retransmit (reordering) and underestimated RTO, analyzing 21491da177e4SLinus Torvalds * timestamps and D-SACKs. When we detect that some segments were 21501da177e4SLinus Torvalds * retransmitted by mistake and CWND reduction was wrong, we undo 21511da177e4SLinus Torvalds * window reduction and abort recovery phase. This logic is hidden 21521da177e4SLinus Torvalds * inside several functions named tcp_try_undo_<something>. 21531da177e4SLinus Torvalds */ 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds /* This function decides, when we should leave Disordered state 21561da177e4SLinus Torvalds * and enter Recovery phase, reducing congestion window. 21571da177e4SLinus Torvalds * 21581da177e4SLinus Torvalds * Main question: may we further continue forward transmission 21591da177e4SLinus Torvalds * with the same cwnd? 21601da177e4SLinus Torvalds */ 2161a2a385d6SEric Dumazet static bool tcp_time_to_recover(struct sock *sk, int flag) 21621da177e4SLinus Torvalds { 21639e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 21641da177e4SLinus Torvalds __u32 packets_out; 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds /* Trick#1: The loss is proven. */ 21671da177e4SLinus Torvalds if (tp->lost_out) 2168a2a385d6SEric Dumazet return true; 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds /* Not-A-Trick#2 : Classic rule... */ 2171ea84e555SAndreas Petlund if (tcp_dupack_heuristics(tp) > tp->reordering) 2172a2a385d6SEric Dumazet return true; 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds /* Trick#4: It is still not OK... But will it be useful to delay 21751da177e4SLinus Torvalds * recovery more? 21761da177e4SLinus Torvalds */ 21771da177e4SLinus Torvalds packets_out = tp->packets_out; 21781da177e4SLinus Torvalds if (packets_out <= tp->reordering && 21791da177e4SLinus Torvalds tp->sacked_out >= max_t(__u32, packets_out/2, sysctl_tcp_reordering) && 21809e412ba7SIlpo Järvinen !tcp_may_send_now(sk)) { 21811da177e4SLinus Torvalds /* We have nothing to send. This connection is limited 21821da177e4SLinus Torvalds * either by receiver window or by application. 21831da177e4SLinus Torvalds */ 2184a2a385d6SEric Dumazet return true; 21851da177e4SLinus Torvalds } 21861da177e4SLinus Torvalds 21877e380175SAndreas Petlund /* If a thin stream is detected, retransmit after first 21887e380175SAndreas Petlund * received dupack. Employ only if SACK is supported in order 21897e380175SAndreas Petlund * to avoid possible corner-case series of spurious retransmissions 21907e380175SAndreas Petlund * Use only if there are no unsent data. 21917e380175SAndreas Petlund */ 21927e380175SAndreas Petlund if ((tp->thin_dupack || sysctl_tcp_thin_dupack) && 21937e380175SAndreas Petlund tcp_stream_is_thin(tp) && tcp_dupack_heuristics(tp) > 1 && 21947e380175SAndreas Petlund tcp_is_sack(tp) && !tcp_send_head(sk)) 2195a2a385d6SEric Dumazet return true; 21967e380175SAndreas Petlund 2197eed530b6SYuchung Cheng /* Trick#6: TCP early retransmit, per RFC5827. To avoid spurious 2198eed530b6SYuchung Cheng * retransmissions due to small network reorderings, we implement 2199eed530b6SYuchung Cheng * Mitigation A.3 in the RFC and delay the retransmission for a short 2200eed530b6SYuchung Cheng * interval if appropriate. 2201eed530b6SYuchung Cheng */ 2202eed530b6SYuchung Cheng if (tp->do_early_retrans && !tp->retrans_out && tp->sacked_out && 22036ba8a3b1SNandita Dukkipati (tp->packets_out >= (tp->sacked_out + 1) && tp->packets_out < 4) && 2204eed530b6SYuchung Cheng !tcp_may_send_now(sk)) 2205750ea2baSYuchung Cheng return !tcp_pause_early_retransmit(sk, flag); 2206eed530b6SYuchung Cheng 2207a2a385d6SEric Dumazet return false; 22081da177e4SLinus Torvalds } 22091da177e4SLinus Torvalds 2210974c1236SYuchung Cheng /* Detect loss in event "A" above by marking head of queue up as lost. 2211974c1236SYuchung Cheng * For FACK or non-SACK(Reno) senders, the first "packets" number of segments 2212974c1236SYuchung Cheng * are considered lost. For RFC3517 SACK, a segment is considered lost if it 2213974c1236SYuchung Cheng * has at least tp->reordering SACKed seqments above it; "packets" refers to 2214974c1236SYuchung Cheng * the maximum SACKed segments to pass before reaching this limit. 221585cc391cSIlpo Järvinen */ 22161fdb9361SIlpo Järvinen static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head) 22171da177e4SLinus Torvalds { 22189e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 22191da177e4SLinus Torvalds struct sk_buff *skb; 2220c137f3ddSIlpo Järvinen int cnt, oldcnt; 2221c137f3ddSIlpo Järvinen int err; 2222c137f3ddSIlpo Järvinen unsigned int mss; 2223974c1236SYuchung Cheng /* Use SACK to deduce losses of new sequences sent during recovery */ 2224974c1236SYuchung Cheng const u32 loss_high = tcp_is_sack(tp) ? tp->snd_nxt : tp->high_seq; 22251da177e4SLinus Torvalds 2226547b792cSIlpo Järvinen WARN_ON(packets > tp->packets_out); 22276a438bbeSStephen Hemminger if (tp->lost_skb_hint) { 22286a438bbeSStephen Hemminger skb = tp->lost_skb_hint; 22296a438bbeSStephen Hemminger cnt = tp->lost_cnt_hint; 22301fdb9361SIlpo Järvinen /* Head already handled? */ 22311fdb9361SIlpo Järvinen if (mark_head && skb != tcp_write_queue_head(sk)) 22321fdb9361SIlpo Järvinen return; 22336a438bbeSStephen Hemminger } else { 2234fe067e8aSDavid S. Miller skb = tcp_write_queue_head(sk); 22356a438bbeSStephen Hemminger cnt = 0; 22366a438bbeSStephen Hemminger } 22371da177e4SLinus Torvalds 2238fe067e8aSDavid S. Miller tcp_for_write_queue_from(skb, sk) { 2239fe067e8aSDavid S. Miller if (skb == tcp_send_head(sk)) 2240fe067e8aSDavid S. Miller break; 22416a438bbeSStephen Hemminger /* TODO: do this better */ 22426a438bbeSStephen Hemminger /* this is not the most efficient way to do this... */ 22436a438bbeSStephen Hemminger tp->lost_skb_hint = skb; 22446a438bbeSStephen Hemminger tp->lost_cnt_hint = cnt; 224585cc391cSIlpo Järvinen 2246974c1236SYuchung Cheng if (after(TCP_SKB_CB(skb)->end_seq, loss_high)) 2247c137f3ddSIlpo Järvinen break; 2248c137f3ddSIlpo Järvinen 2249c137f3ddSIlpo Järvinen oldcnt = cnt; 2250ad1984e8SIlpo Järvinen if (tcp_is_fack(tp) || tcp_is_reno(tp) || 225185cc391cSIlpo Järvinen (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) 22526a438bbeSStephen Hemminger cnt += tcp_skb_pcount(skb); 225385cc391cSIlpo Järvinen 2254c137f3ddSIlpo Järvinen if (cnt > packets) { 2255b3de7559SYuchung Cheng if ((tcp_is_sack(tp) && !tcp_is_fack(tp)) || 2256c0638c24SNeal Cardwell (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) || 2257b3de7559SYuchung Cheng (oldcnt >= packets)) 22581da177e4SLinus Torvalds break; 2259c137f3ddSIlpo Järvinen 2260c137f3ddSIlpo Järvinen mss = skb_shinfo(skb)->gso_size; 22616cc55e09SOctavian Purdila err = tcp_fragment(sk, skb, (packets - oldcnt) * mss, 22626cc55e09SOctavian Purdila mss, GFP_ATOMIC); 2263c137f3ddSIlpo Järvinen if (err < 0) 2264c137f3ddSIlpo Järvinen break; 2265c137f3ddSIlpo Järvinen cnt = packets; 2266c137f3ddSIlpo Järvinen } 2267c137f3ddSIlpo Järvinen 226841ea36e3SIlpo Järvinen tcp_skb_mark_lost(tp, skb); 22691fdb9361SIlpo Järvinen 22701fdb9361SIlpo Järvinen if (mark_head) 22711fdb9361SIlpo Järvinen break; 22721da177e4SLinus Torvalds } 2273005903bcSIlpo Järvinen tcp_verify_left_out(tp); 22741da177e4SLinus Torvalds } 22751da177e4SLinus Torvalds 22761da177e4SLinus Torvalds /* Account newly detected lost packet(s) */ 22771da177e4SLinus Torvalds 227885cc391cSIlpo Järvinen static void tcp_update_scoreboard(struct sock *sk, int fast_rexmit) 22791da177e4SLinus Torvalds { 22809e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 22819e412ba7SIlpo Järvinen 228285cc391cSIlpo Järvinen if (tcp_is_reno(tp)) { 22831fdb9361SIlpo Järvinen tcp_mark_head_lost(sk, 1, 1); 228485cc391cSIlpo Järvinen } else if (tcp_is_fack(tp)) { 22851da177e4SLinus Torvalds int lost = tp->fackets_out - tp->reordering; 22861da177e4SLinus Torvalds if (lost <= 0) 22871da177e4SLinus Torvalds lost = 1; 22881fdb9361SIlpo Järvinen tcp_mark_head_lost(sk, lost, 0); 22891da177e4SLinus Torvalds } else { 229085cc391cSIlpo Järvinen int sacked_upto = tp->sacked_out - tp->reordering; 22911fdb9361SIlpo Järvinen if (sacked_upto >= 0) 22921fdb9361SIlpo Järvinen tcp_mark_head_lost(sk, sacked_upto, 0); 22931fdb9361SIlpo Järvinen else if (fast_rexmit) 22941fdb9361SIlpo Järvinen tcp_mark_head_lost(sk, 1, 1); 22951da177e4SLinus Torvalds } 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds /* CWND moderation, preventing bursts due to too big ACKs 22991da177e4SLinus Torvalds * in dubious situations. 23001da177e4SLinus Torvalds */ 23011da177e4SLinus Torvalds static inline void tcp_moderate_cwnd(struct tcp_sock *tp) 23021da177e4SLinus Torvalds { 23031da177e4SLinus Torvalds tp->snd_cwnd = min(tp->snd_cwnd, 23041da177e4SLinus Torvalds tcp_packets_in_flight(tp) + tcp_max_burst(tp)); 23051da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 23061da177e4SLinus Torvalds } 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds /* Nothing was retransmitted or returned timestamp is less 23091da177e4SLinus Torvalds * than timestamp of the first retransmission. 23101da177e4SLinus Torvalds */ 231167b95bd7SVijay Subramanian static inline bool tcp_packet_delayed(const struct tcp_sock *tp) 23121da177e4SLinus Torvalds { 23131da177e4SLinus Torvalds return !tp->retrans_stamp || 23141da177e4SLinus Torvalds (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && 2315d7ee147dSArnd Hannemann before(tp->rx_opt.rcv_tsecr, tp->retrans_stamp)); 23161da177e4SLinus Torvalds } 23171da177e4SLinus Torvalds 23181da177e4SLinus Torvalds /* Undo procedures. */ 23191da177e4SLinus Torvalds 23201f37bf87SMarcelo Leitner /* We can clear retrans_stamp when there are no retransmissions in the 23211f37bf87SMarcelo Leitner * window. It would seem that it is trivially available for us in 23221f37bf87SMarcelo Leitner * tp->retrans_out, however, that kind of assumptions doesn't consider 23231f37bf87SMarcelo Leitner * what will happen if errors occur when sending retransmission for the 23241f37bf87SMarcelo Leitner * second time. ...It could the that such segment has only 23251f37bf87SMarcelo Leitner * TCPCB_EVER_RETRANS set at the present time. It seems that checking 23261f37bf87SMarcelo Leitner * the head skb is enough except for some reneging corner cases that 23271f37bf87SMarcelo Leitner * are not worth the effort. 23281f37bf87SMarcelo Leitner * 23291f37bf87SMarcelo Leitner * Main reason for all this complexity is the fact that connection dying 23301f37bf87SMarcelo Leitner * time now depends on the validity of the retrans_stamp, in particular, 23311f37bf87SMarcelo Leitner * that successive retransmissions of a segment must not advance 23321f37bf87SMarcelo Leitner * retrans_stamp under any conditions. 23331f37bf87SMarcelo Leitner */ 23341f37bf87SMarcelo Leitner static bool tcp_any_retrans_done(const struct sock *sk) 23351f37bf87SMarcelo Leitner { 23361f37bf87SMarcelo Leitner const struct tcp_sock *tp = tcp_sk(sk); 23371f37bf87SMarcelo Leitner struct sk_buff *skb; 23381f37bf87SMarcelo Leitner 23391f37bf87SMarcelo Leitner if (tp->retrans_out) 23401f37bf87SMarcelo Leitner return true; 23411f37bf87SMarcelo Leitner 23421f37bf87SMarcelo Leitner skb = tcp_write_queue_head(sk); 23431f37bf87SMarcelo Leitner if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS)) 23441f37bf87SMarcelo Leitner return true; 23451f37bf87SMarcelo Leitner 23461f37bf87SMarcelo Leitner return false; 23471f37bf87SMarcelo Leitner } 23481f37bf87SMarcelo Leitner 23491da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 1 23509e412ba7SIlpo Järvinen static void DBGUNDO(struct sock *sk, const char *msg) 23511da177e4SLinus Torvalds { 23529e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 23531da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 23549e412ba7SIlpo Järvinen 2355569508c9SYOSHIFUJI Hideaki if (sk->sk_family == AF_INET) { 235691df42beSJoe Perches pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n", 23571da177e4SLinus Torvalds msg, 2358288fcee8SJoe Perches &inet->inet_daddr, ntohs(inet->inet_dport), 235983ae4088SIlpo Järvinen tp->snd_cwnd, tcp_left_out(tp), 23601da177e4SLinus Torvalds tp->snd_ssthresh, tp->prior_ssthresh, 23611da177e4SLinus Torvalds tp->packets_out); 23621da177e4SLinus Torvalds } 2363dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6) 2364569508c9SYOSHIFUJI Hideaki else if (sk->sk_family == AF_INET6) { 2365569508c9SYOSHIFUJI Hideaki struct ipv6_pinfo *np = inet6_sk(sk); 236691df42beSJoe Perches pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n", 2367569508c9SYOSHIFUJI Hideaki msg, 2368288fcee8SJoe Perches &np->daddr, ntohs(inet->inet_dport), 2369569508c9SYOSHIFUJI Hideaki tp->snd_cwnd, tcp_left_out(tp), 2370569508c9SYOSHIFUJI Hideaki tp->snd_ssthresh, tp->prior_ssthresh, 2371569508c9SYOSHIFUJI Hideaki tp->packets_out); 2372569508c9SYOSHIFUJI Hideaki } 2373569508c9SYOSHIFUJI Hideaki #endif 2374569508c9SYOSHIFUJI Hideaki } 23751da177e4SLinus Torvalds #else 23761da177e4SLinus Torvalds #define DBGUNDO(x...) do { } while (0) 23771da177e4SLinus Torvalds #endif 23781da177e4SLinus Torvalds 23797026b912SYuchung Cheng static void tcp_undo_cwnd_reduction(struct sock *sk, bool unmark_loss) 23801da177e4SLinus Torvalds { 23816687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 23826687e988SArnaldo Carvalho de Melo 23836a63df46SYuchung Cheng if (unmark_loss) { 23846a63df46SYuchung Cheng struct sk_buff *skb; 23856a63df46SYuchung Cheng 23866a63df46SYuchung Cheng tcp_for_write_queue(skb, sk) { 23876a63df46SYuchung Cheng if (skb == tcp_send_head(sk)) 23886a63df46SYuchung Cheng break; 23896a63df46SYuchung Cheng TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST; 23906a63df46SYuchung Cheng } 23916a63df46SYuchung Cheng tp->lost_out = 0; 23926a63df46SYuchung Cheng tcp_clear_all_retrans_hints(tp); 23936a63df46SYuchung Cheng } 23946a63df46SYuchung Cheng 23951da177e4SLinus Torvalds if (tp->prior_ssthresh) { 23966687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 23976687e988SArnaldo Carvalho de Melo 23986687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_ops->undo_cwnd) 23996687e988SArnaldo Carvalho de Melo tp->snd_cwnd = icsk->icsk_ca_ops->undo_cwnd(sk); 24001da177e4SLinus Torvalds else 24011da177e4SLinus Torvalds tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh << 1); 24021da177e4SLinus Torvalds 24037026b912SYuchung Cheng if (tp->prior_ssthresh > tp->snd_ssthresh) { 24041da177e4SLinus Torvalds tp->snd_ssthresh = tp->prior_ssthresh; 2405735d3831SFlorian Westphal tcp_ecn_withdraw_cwr(tp); 24061da177e4SLinus Torvalds } 24071da177e4SLinus Torvalds } else { 24081da177e4SLinus Torvalds tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh); 24091da177e4SLinus Torvalds } 24101da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 24116a63df46SYuchung Cheng tp->undo_marker = 0; 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds 241467b95bd7SVijay Subramanian static inline bool tcp_may_undo(const struct tcp_sock *tp) 24151da177e4SLinus Torvalds { 2416056834d9SIlpo Järvinen return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp)); 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds 24191da177e4SLinus Torvalds /* People celebrate: "We love our President!" */ 2420a2a385d6SEric Dumazet static bool tcp_try_undo_recovery(struct sock *sk) 24211da177e4SLinus Torvalds { 24229e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 24239e412ba7SIlpo Järvinen 24241da177e4SLinus Torvalds if (tcp_may_undo(tp)) { 242540b215e5SPavel Emelyanov int mib_idx; 242640b215e5SPavel Emelyanov 24271da177e4SLinus Torvalds /* Happy end! We did not retransmit anything 24281da177e4SLinus Torvalds * or our original transmission succeeded. 24291da177e4SLinus Torvalds */ 24309e412ba7SIlpo Järvinen DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans"); 24317026b912SYuchung Cheng tcp_undo_cwnd_reduction(sk, false); 24326687e988SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss) 243340b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPLOSSUNDO; 24341da177e4SLinus Torvalds else 243540b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPFULLUNDO; 243640b215e5SPavel Emelyanov 2437de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), mib_idx); 24381da177e4SLinus Torvalds } 2439e60402d0SIlpo Järvinen if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) { 24401da177e4SLinus Torvalds /* Hold old state until something *above* high_seq 24411da177e4SLinus Torvalds * is ACKed. For Reno it is MUST to prevent false 24421da177e4SLinus Torvalds * fast retransmits (RFC2582). SACK TCP is safe. */ 24431da177e4SLinus Torvalds tcp_moderate_cwnd(tp); 24441f37bf87SMarcelo Leitner if (!tcp_any_retrans_done(sk)) 24451f37bf87SMarcelo Leitner tp->retrans_stamp = 0; 2446a2a385d6SEric Dumazet return true; 24471da177e4SLinus Torvalds } 24486687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open); 2449a2a385d6SEric Dumazet return false; 24501da177e4SLinus Torvalds } 24511da177e4SLinus Torvalds 24521da177e4SLinus Torvalds /* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */ 2453c7d9d6a1SYuchung Cheng static bool tcp_try_undo_dsack(struct sock *sk) 24541da177e4SLinus Torvalds { 24559e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 24569e412ba7SIlpo Järvinen 24571da177e4SLinus Torvalds if (tp->undo_marker && !tp->undo_retrans) { 24589e412ba7SIlpo Järvinen DBGUNDO(sk, "D-SACK"); 24597026b912SYuchung Cheng tcp_undo_cwnd_reduction(sk, false); 2460de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKUNDO); 2461c7d9d6a1SYuchung Cheng return true; 24621da177e4SLinus Torvalds } 2463c7d9d6a1SYuchung Cheng return false; 24641da177e4SLinus Torvalds } 24651da177e4SLinus Torvalds 2466e33099f9SYuchung Cheng /* Undo during loss recovery after partial ACK or using F-RTO. */ 2467e33099f9SYuchung Cheng static bool tcp_try_undo_loss(struct sock *sk, bool frto_undo) 24681da177e4SLinus Torvalds { 24699e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 24709e412ba7SIlpo Järvinen 2471e33099f9SYuchung Cheng if (frto_undo || tcp_may_undo(tp)) { 24727026b912SYuchung Cheng tcp_undo_cwnd_reduction(sk, true); 24736a438bbeSStephen Hemminger 24749e412ba7SIlpo Järvinen DBGUNDO(sk, "partial loss"); 2475de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSSUNDO); 2476e33099f9SYuchung Cheng if (frto_undo) 2477e33099f9SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), 2478e33099f9SYuchung Cheng LINUX_MIB_TCPSPURIOUSRTOS); 2479463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_retransmits = 0; 2480e33099f9SYuchung Cheng if (frto_undo || tcp_is_sack(tp)) 24816687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open); 2482a2a385d6SEric Dumazet return true; 24831da177e4SLinus Torvalds } 2484a2a385d6SEric Dumazet return false; 24851da177e4SLinus Torvalds } 24861da177e4SLinus Torvalds 2487684bad11SYuchung Cheng /* The cwnd reduction in CWR and Recovery use the PRR algorithm 2488684bad11SYuchung Cheng * https://datatracker.ietf.org/doc/draft-ietf-tcpm-proportional-rate-reduction/ 2489fb4d3d1dSYuchung Cheng * It computes the number of packets to send (sndcnt) based on packets newly 2490fb4d3d1dSYuchung Cheng * delivered: 2491fb4d3d1dSYuchung Cheng * 1) If the packets in flight is larger than ssthresh, PRR spreads the 2492fb4d3d1dSYuchung Cheng * cwnd reductions across a full RTT. 2493fb4d3d1dSYuchung Cheng * 2) If packets in flight is lower than ssthresh (such as due to excess 2494fb4d3d1dSYuchung Cheng * losses and/or application stalls), do not perform any further cwnd 2495fb4d3d1dSYuchung Cheng * reductions, but instead slow start up to ssthresh. 2496fb4d3d1dSYuchung Cheng */ 24975ee2c941SChristoph Paasch static void tcp_init_cwnd_reduction(struct sock *sk) 2498684bad11SYuchung Cheng { 2499684bad11SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 2500684bad11SYuchung Cheng 2501684bad11SYuchung Cheng tp->high_seq = tp->snd_nxt; 25029b717a8dSNandita Dukkipati tp->tlp_high_seq = 0; 2503684bad11SYuchung Cheng tp->snd_cwnd_cnt = 0; 2504684bad11SYuchung Cheng tp->prior_cwnd = tp->snd_cwnd; 2505684bad11SYuchung Cheng tp->prr_delivered = 0; 2506684bad11SYuchung Cheng tp->prr_out = 0; 2507684bad11SYuchung Cheng tp->snd_ssthresh = inet_csk(sk)->icsk_ca_ops->ssthresh(sk); 2508735d3831SFlorian Westphal tcp_ecn_queue_cwr(tp); 2509684bad11SYuchung Cheng } 2510684bad11SYuchung Cheng 25116804973fSYuchung Cheng static void tcp_cwnd_reduction(struct sock *sk, const int prior_unsacked, 2512684bad11SYuchung Cheng int fast_rexmit) 2513fb4d3d1dSYuchung Cheng { 2514fb4d3d1dSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 2515fb4d3d1dSYuchung Cheng int sndcnt = 0; 2516fb4d3d1dSYuchung Cheng int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp); 25176804973fSYuchung Cheng int newly_acked_sacked = prior_unsacked - 25186804973fSYuchung Cheng (tp->packets_out - tp->sacked_out); 2519fb4d3d1dSYuchung Cheng 2520684bad11SYuchung Cheng tp->prr_delivered += newly_acked_sacked; 2521fb4d3d1dSYuchung Cheng if (tcp_packets_in_flight(tp) > tp->snd_ssthresh) { 2522fb4d3d1dSYuchung Cheng u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered + 2523fb4d3d1dSYuchung Cheng tp->prior_cwnd - 1; 2524fb4d3d1dSYuchung Cheng sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out; 2525fb4d3d1dSYuchung Cheng } else { 2526fb4d3d1dSYuchung Cheng sndcnt = min_t(int, delta, 2527fb4d3d1dSYuchung Cheng max_t(int, tp->prr_delivered - tp->prr_out, 2528fb4d3d1dSYuchung Cheng newly_acked_sacked) + 1); 2529fb4d3d1dSYuchung Cheng } 2530fb4d3d1dSYuchung Cheng 2531fb4d3d1dSYuchung Cheng sndcnt = max(sndcnt, (fast_rexmit ? 1 : 0)); 2532fb4d3d1dSYuchung Cheng tp->snd_cwnd = tcp_packets_in_flight(tp) + sndcnt; 2533fb4d3d1dSYuchung Cheng } 2534fb4d3d1dSYuchung Cheng 2535684bad11SYuchung Cheng static inline void tcp_end_cwnd_reduction(struct sock *sk) 25361da177e4SLinus Torvalds { 25376687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 2538a262f0cdSNandita Dukkipati 2539684bad11SYuchung Cheng /* Reset cwnd to ssthresh in CWR or Recovery (unless it's undone) */ 2540684bad11SYuchung Cheng if (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR || 2541684bad11SYuchung Cheng (tp->undo_marker && tp->snd_ssthresh < TCP_INFINITE_SSTHRESH)) { 254267d4120aSYuchung Cheng tp->snd_cwnd = tp->snd_ssthresh; 25431da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 254467d4120aSYuchung Cheng } 25456687e988SArnaldo Carvalho de Melo tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR); 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 2548684bad11SYuchung Cheng /* Enter CWR state. Disable cwnd undo since congestion is proven with ECN */ 25495ee2c941SChristoph Paasch void tcp_enter_cwr(struct sock *sk) 255009484d1fSYuchung Cheng { 255109484d1fSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 255209484d1fSYuchung Cheng 255309484d1fSYuchung Cheng tp->prior_ssthresh = 0; 2554684bad11SYuchung Cheng if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) { 255509484d1fSYuchung Cheng tp->undo_marker = 0; 25565ee2c941SChristoph Paasch tcp_init_cwnd_reduction(sk); 255709484d1fSYuchung Cheng tcp_set_ca_state(sk, TCP_CA_CWR); 255809484d1fSYuchung Cheng } 255909484d1fSYuchung Cheng } 256009484d1fSYuchung Cheng 25618aca6cb1SIlpo Järvinen static void tcp_try_keep_open(struct sock *sk) 25628aca6cb1SIlpo Järvinen { 25638aca6cb1SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 25648aca6cb1SIlpo Järvinen int state = TCP_CA_Open; 25658aca6cb1SIlpo Järvinen 2566f698204bSNeal Cardwell if (tcp_left_out(tp) || tcp_any_retrans_done(sk)) 25678aca6cb1SIlpo Järvinen state = TCP_CA_Disorder; 25688aca6cb1SIlpo Järvinen 25698aca6cb1SIlpo Järvinen if (inet_csk(sk)->icsk_ca_state != state) { 25708aca6cb1SIlpo Järvinen tcp_set_ca_state(sk, state); 25718aca6cb1SIlpo Järvinen tp->high_seq = tp->snd_nxt; 25728aca6cb1SIlpo Järvinen } 25738aca6cb1SIlpo Järvinen } 25748aca6cb1SIlpo Järvinen 25756804973fSYuchung Cheng static void tcp_try_to_open(struct sock *sk, int flag, const int prior_unsacked) 25761da177e4SLinus Torvalds { 25779e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 25789e412ba7SIlpo Järvinen 257986426c22SIlpo Järvinen tcp_verify_left_out(tp); 258086426c22SIlpo Järvinen 25819b44190dSYuchung Cheng if (!tcp_any_retrans_done(sk)) 25821da177e4SLinus Torvalds tp->retrans_stamp = 0; 25831da177e4SLinus Torvalds 25841da177e4SLinus Torvalds if (flag & FLAG_ECE) 25855ee2c941SChristoph Paasch tcp_enter_cwr(sk); 25861da177e4SLinus Torvalds 25876687e988SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) { 25888aca6cb1SIlpo Järvinen tcp_try_keep_open(sk); 25891da177e4SLinus Torvalds } else { 25906804973fSYuchung Cheng tcp_cwnd_reduction(sk, prior_unsacked, 0); 25911da177e4SLinus Torvalds } 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds 25945d424d5aSJohn Heffner static void tcp_mtup_probe_failed(struct sock *sk) 25955d424d5aSJohn Heffner { 25965d424d5aSJohn Heffner struct inet_connection_sock *icsk = inet_csk(sk); 25975d424d5aSJohn Heffner 25985d424d5aSJohn Heffner icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1; 25995d424d5aSJohn Heffner icsk->icsk_mtup.probe_size = 0; 26005d424d5aSJohn Heffner } 26015d424d5aSJohn Heffner 260272211e90SIlpo Järvinen static void tcp_mtup_probe_success(struct sock *sk) 26035d424d5aSJohn Heffner { 26045d424d5aSJohn Heffner struct tcp_sock *tp = tcp_sk(sk); 26055d424d5aSJohn Heffner struct inet_connection_sock *icsk = inet_csk(sk); 26065d424d5aSJohn Heffner 26075d424d5aSJohn Heffner /* FIXME: breaks with very large cwnd */ 26085d424d5aSJohn Heffner tp->prior_ssthresh = tcp_current_ssthresh(sk); 26095d424d5aSJohn Heffner tp->snd_cwnd = tp->snd_cwnd * 26105d424d5aSJohn Heffner tcp_mss_to_mtu(sk, tp->mss_cache) / 26115d424d5aSJohn Heffner icsk->icsk_mtup.probe_size; 26125d424d5aSJohn Heffner tp->snd_cwnd_cnt = 0; 26135d424d5aSJohn Heffner tp->snd_cwnd_stamp = tcp_time_stamp; 26149c6d5e55SJohn Heffner tp->snd_ssthresh = tcp_current_ssthresh(sk); 26155d424d5aSJohn Heffner 26165d424d5aSJohn Heffner icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size; 26175d424d5aSJohn Heffner icsk->icsk_mtup.probe_size = 0; 26185d424d5aSJohn Heffner tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 26195d424d5aSJohn Heffner } 26205d424d5aSJohn Heffner 2621e1aa680fSIlpo Järvinen /* Do a simple retransmit without using the backoff mechanisms in 2622e1aa680fSIlpo Järvinen * tcp_timer. This is used for path mtu discovery. 2623e1aa680fSIlpo Järvinen * The socket is already locked here. 2624e1aa680fSIlpo Järvinen */ 2625e1aa680fSIlpo Järvinen void tcp_simple_retransmit(struct sock *sk) 2626e1aa680fSIlpo Järvinen { 2627e1aa680fSIlpo Järvinen const struct inet_connection_sock *icsk = inet_csk(sk); 2628e1aa680fSIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 2629e1aa680fSIlpo Järvinen struct sk_buff *skb; 26300c54b85fSIlpo Järvinen unsigned int mss = tcp_current_mss(sk); 2631e1aa680fSIlpo Järvinen u32 prior_lost = tp->lost_out; 2632e1aa680fSIlpo Järvinen 2633e1aa680fSIlpo Järvinen tcp_for_write_queue(skb, sk) { 2634e1aa680fSIlpo Järvinen if (skb == tcp_send_head(sk)) 2635e1aa680fSIlpo Järvinen break; 2636775ffabfSIlpo Järvinen if (tcp_skb_seglen(skb) > mss && 2637e1aa680fSIlpo Järvinen !(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) { 2638e1aa680fSIlpo Järvinen if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) { 2639e1aa680fSIlpo Järvinen TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS; 2640e1aa680fSIlpo Järvinen tp->retrans_out -= tcp_skb_pcount(skb); 2641e1aa680fSIlpo Järvinen } 2642e1aa680fSIlpo Järvinen tcp_skb_mark_lost_uncond_verify(tp, skb); 2643e1aa680fSIlpo Järvinen } 2644e1aa680fSIlpo Järvinen } 2645e1aa680fSIlpo Järvinen 2646e1aa680fSIlpo Järvinen tcp_clear_retrans_hints_partial(tp); 2647e1aa680fSIlpo Järvinen 2648e1aa680fSIlpo Järvinen if (prior_lost == tp->lost_out) 2649e1aa680fSIlpo Järvinen return; 2650e1aa680fSIlpo Järvinen 2651e1aa680fSIlpo Järvinen if (tcp_is_reno(tp)) 2652e1aa680fSIlpo Järvinen tcp_limit_reno_sacked(tp); 2653e1aa680fSIlpo Järvinen 2654e1aa680fSIlpo Järvinen tcp_verify_left_out(tp); 2655e1aa680fSIlpo Järvinen 2656e1aa680fSIlpo Järvinen /* Don't muck with the congestion window here. 2657e1aa680fSIlpo Järvinen * Reason is that we do not increase amount of _data_ 2658e1aa680fSIlpo Järvinen * in network, but units changed and effective 2659e1aa680fSIlpo Järvinen * cwnd/ssthresh really reduced now. 2660e1aa680fSIlpo Järvinen */ 2661e1aa680fSIlpo Järvinen if (icsk->icsk_ca_state != TCP_CA_Loss) { 2662e1aa680fSIlpo Järvinen tp->high_seq = tp->snd_nxt; 2663e1aa680fSIlpo Järvinen tp->snd_ssthresh = tcp_current_ssthresh(sk); 2664e1aa680fSIlpo Järvinen tp->prior_ssthresh = 0; 2665e1aa680fSIlpo Järvinen tp->undo_marker = 0; 2666e1aa680fSIlpo Järvinen tcp_set_ca_state(sk, TCP_CA_Loss); 2667e1aa680fSIlpo Järvinen } 2668e1aa680fSIlpo Järvinen tcp_xmit_retransmit_queue(sk); 2669e1aa680fSIlpo Järvinen } 26704bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_simple_retransmit); 2671e1aa680fSIlpo Järvinen 26721fbc3405SYuchung Cheng static void tcp_enter_recovery(struct sock *sk, bool ece_ack) 26731fbc3405SYuchung Cheng { 26741fbc3405SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 26751fbc3405SYuchung Cheng int mib_idx; 26761fbc3405SYuchung Cheng 26771fbc3405SYuchung Cheng if (tcp_is_reno(tp)) 26781fbc3405SYuchung Cheng mib_idx = LINUX_MIB_TCPRENORECOVERY; 26791fbc3405SYuchung Cheng else 26801fbc3405SYuchung Cheng mib_idx = LINUX_MIB_TCPSACKRECOVERY; 26811fbc3405SYuchung Cheng 26821fbc3405SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), mib_idx); 26831fbc3405SYuchung Cheng 26841fbc3405SYuchung Cheng tp->prior_ssthresh = 0; 2685989e04c5SYuchung Cheng tcp_init_undo(tp); 26861fbc3405SYuchung Cheng 26871fbc3405SYuchung Cheng if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) { 26881fbc3405SYuchung Cheng if (!ece_ack) 26891fbc3405SYuchung Cheng tp->prior_ssthresh = tcp_current_ssthresh(sk); 26905ee2c941SChristoph Paasch tcp_init_cwnd_reduction(sk); 26911fbc3405SYuchung Cheng } 26921fbc3405SYuchung Cheng tcp_set_ca_state(sk, TCP_CA_Recovery); 26931fbc3405SYuchung Cheng } 26941fbc3405SYuchung Cheng 2695ab42d9eeSYuchung Cheng /* Process an ACK in CA_Loss state. Move to CA_Open if lost data are 2696ab42d9eeSYuchung Cheng * recovered or spurious. Otherwise retransmits more on partial ACKs. 2697ab42d9eeSYuchung Cheng */ 2698e33099f9SYuchung Cheng static void tcp_process_loss(struct sock *sk, int flag, bool is_dupack) 2699ab42d9eeSYuchung Cheng { 2700ab42d9eeSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 2701e33099f9SYuchung Cheng bool recovered = !before(tp->snd_una, tp->high_seq); 2702ab42d9eeSYuchung Cheng 2703e33099f9SYuchung Cheng if (tp->frto) { /* F-RTO RFC5682 sec 3.1 (sack enhanced version). */ 2704e33099f9SYuchung Cheng /* Step 3.b. A timeout is spurious if not all data are 2705e33099f9SYuchung Cheng * lost, i.e., never-retransmitted data are (s)acked. 2706e33099f9SYuchung Cheng */ 27070cfa5c07SYuchung Cheng if (tcp_try_undo_loss(sk, flag & FLAG_ORIG_SACK_ACKED)) 2708e33099f9SYuchung Cheng return; 27090cfa5c07SYuchung Cheng 2710e33099f9SYuchung Cheng if (after(tp->snd_nxt, tp->high_seq) && 2711e33099f9SYuchung Cheng (flag & FLAG_DATA_SACKED || is_dupack)) { 2712e33099f9SYuchung Cheng tp->frto = 0; /* Loss was real: 2nd part of step 3.a */ 2713e33099f9SYuchung Cheng } else if (flag & FLAG_SND_UNA_ADVANCED && !recovered) { 2714e33099f9SYuchung Cheng tp->high_seq = tp->snd_nxt; 2715e33099f9SYuchung Cheng __tcp_push_pending_frames(sk, tcp_current_mss(sk), 2716e33099f9SYuchung Cheng TCP_NAGLE_OFF); 2717e33099f9SYuchung Cheng if (after(tp->snd_nxt, tp->high_seq)) 2718e33099f9SYuchung Cheng return; /* Step 2.b */ 2719e33099f9SYuchung Cheng tp->frto = 0; 2720e33099f9SYuchung Cheng } 2721e33099f9SYuchung Cheng } 2722e33099f9SYuchung Cheng 2723e33099f9SYuchung Cheng if (recovered) { 2724e33099f9SYuchung Cheng /* F-RTO RFC5682 sec 3.1 step 2.a and 1st part of step 3.a */ 2725ab42d9eeSYuchung Cheng tcp_try_undo_recovery(sk); 2726ab42d9eeSYuchung Cheng return; 2727ab42d9eeSYuchung Cheng } 2728e33099f9SYuchung Cheng if (tcp_is_reno(tp)) { 2729e33099f9SYuchung Cheng /* A Reno DUPACK means new data in F-RTO step 2.b above are 2730e33099f9SYuchung Cheng * delivered. Lower inflight to clock out (re)tranmissions. 2731e33099f9SYuchung Cheng */ 2732e33099f9SYuchung Cheng if (after(tp->snd_nxt, tp->high_seq) && is_dupack) 2733e33099f9SYuchung Cheng tcp_add_reno_sack(sk); 2734e33099f9SYuchung Cheng else if (flag & FLAG_SND_UNA_ADVANCED) 2735ab42d9eeSYuchung Cheng tcp_reset_reno_sack(tp); 2736e33099f9SYuchung Cheng } 2737e33099f9SYuchung Cheng if (tcp_try_undo_loss(sk, false)) 2738ab42d9eeSYuchung Cheng return; 2739ab42d9eeSYuchung Cheng tcp_xmit_retransmit_queue(sk); 2740ab42d9eeSYuchung Cheng } 2741ab42d9eeSYuchung Cheng 27426a63df46SYuchung Cheng /* Undo during fast recovery after partial ACK. */ 27437026b912SYuchung Cheng static bool tcp_try_undo_partial(struct sock *sk, const int acked, 27447026b912SYuchung Cheng const int prior_unsacked) 27456a63df46SYuchung Cheng { 27466a63df46SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 27476a63df46SYuchung Cheng 27487026b912SYuchung Cheng if (tp->undo_marker && tcp_packet_delayed(tp)) { 27496a63df46SYuchung Cheng /* Plain luck! Hole if filled with delayed 27506a63df46SYuchung Cheng * packet, rather than with a retransmit. 27516a63df46SYuchung Cheng */ 27527026b912SYuchung Cheng tcp_update_reordering(sk, tcp_fackets_out(tp) + acked, 1); 27537026b912SYuchung Cheng 27547026b912SYuchung Cheng /* We are getting evidence that the reordering degree is higher 27557026b912SYuchung Cheng * than we realized. If there are no retransmits out then we 27567026b912SYuchung Cheng * can undo. Otherwise we clock out new packets but do not 27577026b912SYuchung Cheng * mark more packets lost or retransmit more. 27587026b912SYuchung Cheng */ 27597026b912SYuchung Cheng if (tp->retrans_out) { 27607026b912SYuchung Cheng tcp_cwnd_reduction(sk, prior_unsacked, 0); 27617026b912SYuchung Cheng return true; 27627026b912SYuchung Cheng } 27637026b912SYuchung Cheng 27646a63df46SYuchung Cheng if (!tcp_any_retrans_done(sk)) 27656a63df46SYuchung Cheng tp->retrans_stamp = 0; 27666a63df46SYuchung Cheng 27677026b912SYuchung Cheng DBGUNDO(sk, "partial recovery"); 27687026b912SYuchung Cheng tcp_undo_cwnd_reduction(sk, true); 27696a63df46SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO); 27707026b912SYuchung Cheng tcp_try_keep_open(sk); 27717026b912SYuchung Cheng return true; 27726a63df46SYuchung Cheng } 27737026b912SYuchung Cheng return false; 27746a63df46SYuchung Cheng } 27756a63df46SYuchung Cheng 27761da177e4SLinus Torvalds /* Process an event, which can update packets-in-flight not trivially. 27771da177e4SLinus Torvalds * Main goal of this function is to calculate new estimate for left_out, 27781da177e4SLinus Torvalds * taking into account both packets sitting in receiver's buffer and 27791da177e4SLinus Torvalds * packets lost by network. 27801da177e4SLinus Torvalds * 27811da177e4SLinus Torvalds * Besides that it does CWND reduction, when packet loss is detected 27821da177e4SLinus Torvalds * and changes state of machine. 27831da177e4SLinus Torvalds * 27841da177e4SLinus Torvalds * It does _not_ decide what to send, it is made in function 27851da177e4SLinus Torvalds * tcp_xmit_retransmit_queue(). 27861da177e4SLinus Torvalds */ 27876804973fSYuchung Cheng static void tcp_fastretrans_alert(struct sock *sk, const int acked, 27886804973fSYuchung Cheng const int prior_unsacked, 278935f079ebSNandita Dukkipati bool is_dupack, int flag) 27901da177e4SLinus Torvalds { 27916687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 27921da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 27936a63df46SYuchung Cheng bool do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) && 279485cc391cSIlpo Järvinen (tcp_fackets_out(tp) > tp->reordering)); 27951fbc3405SYuchung Cheng int fast_rexmit = 0; 27961da177e4SLinus Torvalds 27973ccd3130SIlpo Järvinen if (WARN_ON(!tp->packets_out && tp->sacked_out)) 27981da177e4SLinus Torvalds tp->sacked_out = 0; 279991fed7a1SIlpo Järvinen if (WARN_ON(!tp->sacked_out && tp->fackets_out)) 28001da177e4SLinus Torvalds tp->fackets_out = 0; 28011da177e4SLinus Torvalds 28021da177e4SLinus Torvalds /* Now state machine starts. 28031da177e4SLinus Torvalds * A. ECE, hence prohibit cwnd undoing, the reduction is required. */ 28041da177e4SLinus Torvalds if (flag & FLAG_ECE) 28051da177e4SLinus Torvalds tp->prior_ssthresh = 0; 28061da177e4SLinus Torvalds 28071da177e4SLinus Torvalds /* B. In all the states check for reneging SACKs. */ 2808cadbd031SIlpo Järvinen if (tcp_check_sack_reneging(sk, flag)) 28091da177e4SLinus Torvalds return; 28101da177e4SLinus Torvalds 2811974c1236SYuchung Cheng /* C. Check consistency of the current state. */ 2812005903bcSIlpo Järvinen tcp_verify_left_out(tp); 28131da177e4SLinus Torvalds 2814974c1236SYuchung Cheng /* D. Check state exit conditions. State can be terminated 28151da177e4SLinus Torvalds * when high_seq is ACKed. */ 28166687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_state == TCP_CA_Open) { 2817547b792cSIlpo Järvinen WARN_ON(tp->retrans_out != 0); 28181da177e4SLinus Torvalds tp->retrans_stamp = 0; 28191da177e4SLinus Torvalds } else if (!before(tp->snd_una, tp->high_seq)) { 28206687e988SArnaldo Carvalho de Melo switch (icsk->icsk_ca_state) { 28211da177e4SLinus Torvalds case TCP_CA_CWR: 28221da177e4SLinus Torvalds /* CWR is to be held something *above* high_seq 28231da177e4SLinus Torvalds * is ACKed for CWR bit to reach receiver. */ 28241da177e4SLinus Torvalds if (tp->snd_una != tp->high_seq) { 2825684bad11SYuchung Cheng tcp_end_cwnd_reduction(sk); 28266687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open); 28271da177e4SLinus Torvalds } 28281da177e4SLinus Torvalds break; 28291da177e4SLinus Torvalds 28301da177e4SLinus Torvalds case TCP_CA_Recovery: 2831e60402d0SIlpo Järvinen if (tcp_is_reno(tp)) 28321da177e4SLinus Torvalds tcp_reset_reno_sack(tp); 28339e412ba7SIlpo Järvinen if (tcp_try_undo_recovery(sk)) 28341da177e4SLinus Torvalds return; 2835684bad11SYuchung Cheng tcp_end_cwnd_reduction(sk); 28361da177e4SLinus Torvalds break; 28371da177e4SLinus Torvalds } 28381da177e4SLinus Torvalds } 28391da177e4SLinus Torvalds 2840974c1236SYuchung Cheng /* E. Process state. */ 28416687e988SArnaldo Carvalho de Melo switch (icsk->icsk_ca_state) { 28421da177e4SLinus Torvalds case TCP_CA_Recovery: 28432e605294SIlpo Järvinen if (!(flag & FLAG_SND_UNA_ADVANCED)) { 2844e60402d0SIlpo Järvinen if (tcp_is_reno(tp) && is_dupack) 28456687e988SArnaldo Carvalho de Melo tcp_add_reno_sack(sk); 28467026b912SYuchung Cheng } else { 28477026b912SYuchung Cheng if (tcp_try_undo_partial(sk, acked, prior_unsacked)) 28487026b912SYuchung Cheng return; 28497026b912SYuchung Cheng /* Partial ACK arrived. Force fast retransmit. */ 28507026b912SYuchung Cheng do_lost = tcp_is_reno(tp) || 28517026b912SYuchung Cheng tcp_fackets_out(tp) > tp->reordering; 28527026b912SYuchung Cheng } 2853c7d9d6a1SYuchung Cheng if (tcp_try_undo_dsack(sk)) { 2854c7d9d6a1SYuchung Cheng tcp_try_keep_open(sk); 2855c7d9d6a1SYuchung Cheng return; 2856c7d9d6a1SYuchung Cheng } 28571da177e4SLinus Torvalds break; 28581da177e4SLinus Torvalds case TCP_CA_Loss: 2859e33099f9SYuchung Cheng tcp_process_loss(sk, flag, is_dupack); 28606687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_state != TCP_CA_Open) 28611da177e4SLinus Torvalds return; 2862ab42d9eeSYuchung Cheng /* Fall through to processing in Open state. */ 28631da177e4SLinus Torvalds default: 2864e60402d0SIlpo Järvinen if (tcp_is_reno(tp)) { 28652e605294SIlpo Järvinen if (flag & FLAG_SND_UNA_ADVANCED) 28661da177e4SLinus Torvalds tcp_reset_reno_sack(tp); 28671da177e4SLinus Torvalds if (is_dupack) 28686687e988SArnaldo Carvalho de Melo tcp_add_reno_sack(sk); 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds 2871f698204bSNeal Cardwell if (icsk->icsk_ca_state <= TCP_CA_Disorder) 28729e412ba7SIlpo Järvinen tcp_try_undo_dsack(sk); 28731da177e4SLinus Torvalds 2874750ea2baSYuchung Cheng if (!tcp_time_to_recover(sk, flag)) { 28756804973fSYuchung Cheng tcp_try_to_open(sk, flag, prior_unsacked); 28761da177e4SLinus Torvalds return; 28771da177e4SLinus Torvalds } 28781da177e4SLinus Torvalds 28795d424d5aSJohn Heffner /* MTU probe failure: don't reduce cwnd */ 28805d424d5aSJohn Heffner if (icsk->icsk_ca_state < TCP_CA_CWR && 28815d424d5aSJohn Heffner icsk->icsk_mtup.probe_size && 28820e7b1368SJohn Heffner tp->snd_una == tp->mtu_probe.probe_seq_start) { 28835d424d5aSJohn Heffner tcp_mtup_probe_failed(sk); 28845d424d5aSJohn Heffner /* Restores the reduction we did in tcp_mtup_probe() */ 28855d424d5aSJohn Heffner tp->snd_cwnd++; 28865d424d5aSJohn Heffner tcp_simple_retransmit(sk); 28875d424d5aSJohn Heffner return; 28885d424d5aSJohn Heffner } 28895d424d5aSJohn Heffner 28901da177e4SLinus Torvalds /* Otherwise enter Recovery state */ 28911fbc3405SYuchung Cheng tcp_enter_recovery(sk, (flag & FLAG_ECE)); 289285cc391cSIlpo Järvinen fast_rexmit = 1; 28931da177e4SLinus Torvalds } 28941da177e4SLinus Torvalds 28953e59cb0dSYuchung Cheng if (do_lost) 289685cc391cSIlpo Järvinen tcp_update_scoreboard(sk, fast_rexmit); 28976804973fSYuchung Cheng tcp_cwnd_reduction(sk, prior_unsacked, fast_rexmit); 28981da177e4SLinus Torvalds tcp_xmit_retransmit_queue(sk); 28991da177e4SLinus Torvalds } 29001da177e4SLinus Torvalds 2901ed08495cSYuchung Cheng static inline bool tcp_ack_update_rtt(struct sock *sk, const int flag, 2902740b0f18SEric Dumazet long seq_rtt_us, long sack_rtt_us) 290341834b73SIlpo Järvinen { 29045b08e47cSYuchung Cheng const struct tcp_sock *tp = tcp_sk(sk); 290541834b73SIlpo Järvinen 29065b08e47cSYuchung Cheng /* Prefer RTT measured from ACK's timing to TS-ECR. This is because 29075b08e47cSYuchung Cheng * broken middle-boxes or peers may corrupt TS-ECR fields. But 29085b08e47cSYuchung Cheng * Karn's algorithm forbids taking RTT if some retransmitted data 29095b08e47cSYuchung Cheng * is acked (RFC6298). 29101da177e4SLinus Torvalds */ 29115b08e47cSYuchung Cheng if (flag & FLAG_RETRANS_DATA_ACKED) 2912740b0f18SEric Dumazet seq_rtt_us = -1L; 29135b08e47cSYuchung Cheng 2914740b0f18SEric Dumazet if (seq_rtt_us < 0) 2915740b0f18SEric Dumazet seq_rtt_us = sack_rtt_us; 2916ed08495cSYuchung Cheng 29171da177e4SLinus Torvalds /* RTTM Rule: A TSecr value received in a segment is used to 29181da177e4SLinus Torvalds * update the averaged RTT measurement only if the segment 29191da177e4SLinus Torvalds * acknowledges some new data, i.e., only if it advances the 29201da177e4SLinus Torvalds * left edge of the send window. 29211da177e4SLinus Torvalds * See draft-ietf-tcplw-high-performance-00, section 3.3. 29221da177e4SLinus Torvalds */ 2923740b0f18SEric Dumazet if (seq_rtt_us < 0 && tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && 29242909d874SYuchung Cheng flag & FLAG_ACKED) 2925740b0f18SEric Dumazet seq_rtt_us = jiffies_to_usecs(tcp_time_stamp - tp->rx_opt.rcv_tsecr); 292641834b73SIlpo Järvinen 2927740b0f18SEric Dumazet if (seq_rtt_us < 0) 2928ed08495cSYuchung Cheng return false; 29291da177e4SLinus Torvalds 2930740b0f18SEric Dumazet tcp_rtt_estimator(sk, seq_rtt_us); 29315b08e47cSYuchung Cheng tcp_set_rto(sk); 29321da177e4SLinus Torvalds 29335b08e47cSYuchung Cheng /* RFC6298: only reset backoff on valid RTT measurement. */ 29345b08e47cSYuchung Cheng inet_csk(sk)->icsk_backoff = 0; 2935ed08495cSYuchung Cheng return true; 29361da177e4SLinus Torvalds } 29371da177e4SLinus Torvalds 2938375fe02cSYuchung Cheng /* Compute time elapsed between (last) SYNACK and the ACK completing 3WHS. */ 2939bc15afa3SYuchung Cheng static void tcp_synack_rtt_meas(struct sock *sk, const u32 synack_stamp) 2940375fe02cSYuchung Cheng { 2941375fe02cSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 2942740b0f18SEric Dumazet long seq_rtt_us = -1L; 2943375fe02cSYuchung Cheng 2944bc15afa3SYuchung Cheng if (synack_stamp && !tp->total_retrans) 2945740b0f18SEric Dumazet seq_rtt_us = jiffies_to_usecs(tcp_time_stamp - synack_stamp); 2946bc15afa3SYuchung Cheng 2947bc15afa3SYuchung Cheng /* If the ACK acks both the SYNACK and the (Fast Open'd) data packets 2948bc15afa3SYuchung Cheng * sent in SYN_RECV, SYNACK RTT is the smooth RTT computed in tcp_ack() 2949bc15afa3SYuchung Cheng */ 2950740b0f18SEric Dumazet if (!tp->srtt_us) 2951740b0f18SEric Dumazet tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, seq_rtt_us, -1L); 2952375fe02cSYuchung Cheng } 2953375fe02cSYuchung Cheng 295424901551SEric Dumazet static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 acked) 29551da177e4SLinus Torvalds { 29566687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 295724901551SEric Dumazet 295824901551SEric Dumazet icsk->icsk_ca_ops->cong_avoid(sk, ack, acked); 29596687e988SArnaldo Carvalho de Melo tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp; 29601da177e4SLinus Torvalds } 29611da177e4SLinus Torvalds 29621da177e4SLinus Torvalds /* Restart timer after forward progress on connection. 29631da177e4SLinus Torvalds * RFC2988 recommends to restart timer to now+rto. 29641da177e4SLinus Torvalds */ 2965750ea2baSYuchung Cheng void tcp_rearm_rto(struct sock *sk) 29661da177e4SLinus Torvalds { 29676ba8a3b1SNandita Dukkipati const struct inet_connection_sock *icsk = inet_csk(sk); 2968750ea2baSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 29699e412ba7SIlpo Järvinen 2970168a8f58SJerry Chu /* If the retrans timer is currently being used by Fast Open 2971168a8f58SJerry Chu * for SYN-ACK retrans purpose, stay put. 2972168a8f58SJerry Chu */ 2973168a8f58SJerry Chu if (tp->fastopen_rsk) 2974168a8f58SJerry Chu return; 2975168a8f58SJerry Chu 29761da177e4SLinus Torvalds if (!tp->packets_out) { 2977463c84b9SArnaldo Carvalho de Melo inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS); 29781da177e4SLinus Torvalds } else { 2979750ea2baSYuchung Cheng u32 rto = inet_csk(sk)->icsk_rto; 2980750ea2baSYuchung Cheng /* Offset the time elapsed after installing regular RTO */ 29816ba8a3b1SNandita Dukkipati if (icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS || 29826ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { 2983750ea2baSYuchung Cheng struct sk_buff *skb = tcp_write_queue_head(sk); 29847faee5c0SEric Dumazet const u32 rto_time_stamp = 29857faee5c0SEric Dumazet tcp_skb_timestamp(skb) + rto; 2986750ea2baSYuchung Cheng s32 delta = (s32)(rto_time_stamp - tcp_time_stamp); 2987750ea2baSYuchung Cheng /* delta may not be positive if the socket is locked 29886ba8a3b1SNandita Dukkipati * when the retrans timer fires and is rescheduled. 2989750ea2baSYuchung Cheng */ 2990750ea2baSYuchung Cheng if (delta > 0) 2991750ea2baSYuchung Cheng rto = delta; 29921da177e4SLinus Torvalds } 2993750ea2baSYuchung Cheng inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto, 2994750ea2baSYuchung Cheng TCP_RTO_MAX); 2995750ea2baSYuchung Cheng } 2996750ea2baSYuchung Cheng } 2997750ea2baSYuchung Cheng 2998750ea2baSYuchung Cheng /* This function is called when the delayed ER timer fires. TCP enters 2999750ea2baSYuchung Cheng * fast recovery and performs fast-retransmit. 3000750ea2baSYuchung Cheng */ 3001750ea2baSYuchung Cheng void tcp_resume_early_retransmit(struct sock *sk) 3002750ea2baSYuchung Cheng { 3003750ea2baSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 3004750ea2baSYuchung Cheng 3005750ea2baSYuchung Cheng tcp_rearm_rto(sk); 3006750ea2baSYuchung Cheng 3007750ea2baSYuchung Cheng /* Stop if ER is disabled after the delayed ER timer is scheduled */ 3008750ea2baSYuchung Cheng if (!tp->do_early_retrans) 3009750ea2baSYuchung Cheng return; 3010750ea2baSYuchung Cheng 3011750ea2baSYuchung Cheng tcp_enter_recovery(sk, false); 3012750ea2baSYuchung Cheng tcp_update_scoreboard(sk, 1); 3013750ea2baSYuchung Cheng tcp_xmit_retransmit_queue(sk); 30141da177e4SLinus Torvalds } 30151da177e4SLinus Torvalds 30167c46a03eSIlpo Järvinen /* If we get here, the whole TSO packet has not been acked. */ 301713fcf850SIlpo Järvinen static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb) 30181da177e4SLinus Torvalds { 30191da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 30207c46a03eSIlpo Järvinen u32 packets_acked; 30211da177e4SLinus Torvalds 30227c46a03eSIlpo Järvinen BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una)); 30231da177e4SLinus Torvalds 30241da177e4SLinus Torvalds packets_acked = tcp_skb_pcount(skb); 30257c46a03eSIlpo Järvinen if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq)) 30261da177e4SLinus Torvalds return 0; 30271da177e4SLinus Torvalds packets_acked -= tcp_skb_pcount(skb); 30281da177e4SLinus Torvalds 30291da177e4SLinus Torvalds if (packets_acked) { 30301da177e4SLinus Torvalds BUG_ON(tcp_skb_pcount(skb) == 0); 30317c46a03eSIlpo Järvinen BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)); 30321da177e4SLinus Torvalds } 30331da177e4SLinus Torvalds 303413fcf850SIlpo Järvinen return packets_acked; 30351da177e4SLinus Torvalds } 30361da177e4SLinus Torvalds 3037ad971f61SEric Dumazet static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb, 3038ad971f61SEric Dumazet u32 prior_snd_una) 3039ad971f61SEric Dumazet { 3040ad971f61SEric Dumazet const struct skb_shared_info *shinfo; 3041ad971f61SEric Dumazet 3042ad971f61SEric Dumazet /* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */ 3043ad971f61SEric Dumazet if (likely(!(sk->sk_tsflags & SOF_TIMESTAMPING_TX_ACK))) 3044ad971f61SEric Dumazet return; 3045ad971f61SEric Dumazet 3046ad971f61SEric Dumazet shinfo = skb_shinfo(skb); 3047ad971f61SEric Dumazet if ((shinfo->tx_flags & SKBTX_ACK_TSTAMP) && 3048ad971f61SEric Dumazet between(shinfo->tskey, prior_snd_una, tcp_sk(sk)->snd_una - 1)) 3049ad971f61SEric Dumazet __skb_tstamp_tx(skb, NULL, sk, SCM_TSTAMP_ACK); 3050ad971f61SEric Dumazet } 3051ad971f61SEric Dumazet 30527c46a03eSIlpo Järvinen /* Remove acknowledged frames from the retransmission queue. If our packet 30537c46a03eSIlpo Järvinen * is before the ack sequence we can discard it as it's confirmed to have 30547c46a03eSIlpo Järvinen * arrived at the other end. 30557c46a03eSIlpo Järvinen */ 305633f5f57eSIlpo Järvinen static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets, 3057740b0f18SEric Dumazet u32 prior_snd_una, long sack_rtt_us) 30581da177e4SLinus Torvalds { 30592d2abbabSStephen Hemminger const struct inet_connection_sock *icsk = inet_csk(sk); 3060740b0f18SEric Dumazet struct skb_mstamp first_ackt, last_ackt, now; 3061740b0f18SEric Dumazet struct tcp_sock *tp = tcp_sk(sk); 306290638a04SIlpo Järvinen u32 prior_sacked = tp->sacked_out; 3063740b0f18SEric Dumazet u32 reord = tp->packets_out; 3064740b0f18SEric Dumazet bool fully_acked = true; 3065740b0f18SEric Dumazet long ca_seq_rtt_us = -1L; 3066740b0f18SEric Dumazet long seq_rtt_us = -1L; 3067740b0f18SEric Dumazet struct sk_buff *skb; 3068740b0f18SEric Dumazet u32 pkts_acked = 0; 30692f715c1dSYuchung Cheng bool rtt_update; 3070740b0f18SEric Dumazet int flag = 0; 3071740b0f18SEric Dumazet 3072740b0f18SEric Dumazet first_ackt.v64 = 0; 30731da177e4SLinus Torvalds 30747c46a03eSIlpo Järvinen while ((skb = tcp_write_queue_head(sk)) && skb != tcp_send_head(sk)) { 30751da177e4SLinus Torvalds struct tcp_skb_cb *scb = TCP_SKB_CB(skb); 30767c46a03eSIlpo Järvinen u8 sacked = scb->sacked; 3077740b0f18SEric Dumazet u32 acked_pcount; 30781da177e4SLinus Torvalds 3079ad971f61SEric Dumazet tcp_ack_tstamp(sk, skb, prior_snd_una); 3080712a7221SWillem de Bruijn 30812072c228SGavin McCullagh /* Determine how many packets and what bytes were acked, tso and else */ 30821da177e4SLinus Torvalds if (after(scb->end_seq, tp->snd_una)) { 308313fcf850SIlpo Järvinen if (tcp_skb_pcount(skb) == 1 || 308413fcf850SIlpo Järvinen !after(tp->snd_una, scb->seq)) 30851da177e4SLinus Torvalds break; 308613fcf850SIlpo Järvinen 308772018835SIlpo Järvinen acked_pcount = tcp_tso_acked(sk, skb); 308872018835SIlpo Järvinen if (!acked_pcount) 308913fcf850SIlpo Järvinen break; 309013fcf850SIlpo Järvinen 3091a2a385d6SEric Dumazet fully_acked = false; 309213fcf850SIlpo Järvinen } else { 3093ad971f61SEric Dumazet /* Speedup tcp_unlink_write_queue() and next loop */ 3094ad971f61SEric Dumazet prefetchw(skb->next); 309572018835SIlpo Järvinen acked_pcount = tcp_skb_pcount(skb); 30961da177e4SLinus Torvalds } 30971da177e4SLinus Torvalds 3098ad971f61SEric Dumazet if (unlikely(sacked & TCPCB_RETRANS)) { 30991da177e4SLinus Torvalds if (sacked & TCPCB_SACKED_RETRANS) 310072018835SIlpo Järvinen tp->retrans_out -= acked_pcount; 31017c46a03eSIlpo Järvinen flag |= FLAG_RETRANS_DATA_ACKED; 3102c7caf8d3SIlpo Järvinen } else { 3103740b0f18SEric Dumazet last_ackt = skb->skb_mstamp; 3104431a9124SEric Dumazet WARN_ON_ONCE(last_ackt.v64 == 0); 3105740b0f18SEric Dumazet if (!first_ackt.v64) 3106740b0f18SEric Dumazet first_ackt = last_ackt; 3107740b0f18SEric Dumazet 3108c7caf8d3SIlpo Järvinen if (!(sacked & TCPCB_SACKED_ACKED)) 310972018835SIlpo Järvinen reord = min(pkts_acked, reord); 3110e33099f9SYuchung Cheng if (!after(scb->end_seq, tp->high_seq)) 3111e33099f9SYuchung Cheng flag |= FLAG_ORIG_SACK_ACKED; 3112c7caf8d3SIlpo Järvinen } 31137c46a03eSIlpo Järvinen 31141da177e4SLinus Torvalds if (sacked & TCPCB_SACKED_ACKED) 311572018835SIlpo Järvinen tp->sacked_out -= acked_pcount; 31161da177e4SLinus Torvalds if (sacked & TCPCB_LOST) 311772018835SIlpo Järvinen tp->lost_out -= acked_pcount; 31187c46a03eSIlpo Järvinen 311972018835SIlpo Järvinen tp->packets_out -= acked_pcount; 312072018835SIlpo Järvinen pkts_acked += acked_pcount; 312113fcf850SIlpo Järvinen 3122009a2e3eSIlpo Järvinen /* Initial outgoing SYN's get put onto the write_queue 3123009a2e3eSIlpo Järvinen * just like anything else we transmit. It is not 3124009a2e3eSIlpo Järvinen * true data, and if we misinform our callers that 3125009a2e3eSIlpo Järvinen * this ACK acks real data, we will erroneously exit 3126009a2e3eSIlpo Järvinen * connection startup slow start one packet too 3127009a2e3eSIlpo Järvinen * quickly. This is severely frowned upon behavior. 3128009a2e3eSIlpo Järvinen */ 3129ad971f61SEric Dumazet if (likely(!(scb->tcp_flags & TCPHDR_SYN))) { 3130009a2e3eSIlpo Järvinen flag |= FLAG_DATA_ACKED; 3131009a2e3eSIlpo Järvinen } else { 3132009a2e3eSIlpo Järvinen flag |= FLAG_SYN_ACKED; 3133009a2e3eSIlpo Järvinen tp->retrans_stamp = 0; 3134009a2e3eSIlpo Järvinen } 3135009a2e3eSIlpo Järvinen 313613fcf850SIlpo Järvinen if (!fully_acked) 313713fcf850SIlpo Järvinen break; 313813fcf850SIlpo Järvinen 3139fe067e8aSDavid S. Miller tcp_unlink_write_queue(skb, sk); 31403ab224beSHideo Aoki sk_wmem_free_skb(sk, skb); 3141ad971f61SEric Dumazet if (unlikely(skb == tp->retransmit_skb_hint)) 3142ef9da47cSIlpo Järvinen tp->retransmit_skb_hint = NULL; 3143ad971f61SEric Dumazet if (unlikely(skb == tp->lost_skb_hint)) 314490638a04SIlpo Järvinen tp->lost_skb_hint = NULL; 31451da177e4SLinus Torvalds } 31461da177e4SLinus Torvalds 314733f5f57eSIlpo Järvinen if (likely(between(tp->snd_up, prior_snd_una, tp->snd_una))) 314833f5f57eSIlpo Järvinen tp->snd_up = tp->snd_una; 314933f5f57eSIlpo Järvinen 3150cadbd031SIlpo Järvinen if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) 3151cadbd031SIlpo Järvinen flag |= FLAG_SACK_RENEGING; 3152cadbd031SIlpo Järvinen 3153740b0f18SEric Dumazet skb_mstamp_get(&now); 3154ad971f61SEric Dumazet if (likely(first_ackt.v64)) { 3155740b0f18SEric Dumazet seq_rtt_us = skb_mstamp_us_delta(&now, &first_ackt); 3156740b0f18SEric Dumazet ca_seq_rtt_us = skb_mstamp_us_delta(&now, &last_ackt); 3157740b0f18SEric Dumazet } 3158740b0f18SEric Dumazet 3159740b0f18SEric Dumazet rtt_update = tcp_ack_update_rtt(sk, flag, seq_rtt_us, sack_rtt_us); 3160ed08495cSYuchung Cheng 31617c46a03eSIlpo Järvinen if (flag & FLAG_ACKED) { 3162164891aaSStephen Hemminger const struct tcp_congestion_ops *ca_ops 3163164891aaSStephen Hemminger = inet_csk(sk)->icsk_ca_ops; 3164164891aaSStephen Hemminger 31652f715c1dSYuchung Cheng tcp_rearm_rto(sk); 316672211e90SIlpo Järvinen if (unlikely(icsk->icsk_mtup.probe_size && 316772211e90SIlpo Järvinen !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) { 316872211e90SIlpo Järvinen tcp_mtup_probe_success(sk); 316972211e90SIlpo Järvinen } 317072211e90SIlpo Järvinen 3171c7caf8d3SIlpo Järvinen if (tcp_is_reno(tp)) { 3172c7caf8d3SIlpo Järvinen tcp_remove_reno_sacks(sk, pkts_acked); 3173c7caf8d3SIlpo Järvinen } else { 317459a08cbaSIlpo Järvinen int delta; 317559a08cbaSIlpo Järvinen 3176c7caf8d3SIlpo Järvinen /* Non-retransmitted hole got filled? That's reordering */ 3177c7caf8d3SIlpo Järvinen if (reord < prior_fackets) 3178c7caf8d3SIlpo Järvinen tcp_update_reordering(sk, tp->fackets_out - reord, 0); 317990638a04SIlpo Järvinen 318059a08cbaSIlpo Järvinen delta = tcp_is_fack(tp) ? pkts_acked : 318159a08cbaSIlpo Järvinen prior_sacked - tp->sacked_out; 318259a08cbaSIlpo Järvinen tp->lost_cnt_hint -= min(tp->lost_cnt_hint, delta); 3183c7caf8d3SIlpo Järvinen } 3184c7caf8d3SIlpo Järvinen 318591fed7a1SIlpo Järvinen tp->fackets_out -= min(pkts_acked, tp->fackets_out); 318668f8353bSIlpo Järvinen 3187932eb763SKenneth Klette Jonassen if (ca_ops->pkts_acked) { 3188932eb763SKenneth Klette Jonassen long rtt_us = min_t(ulong, ca_seq_rtt_us, sack_rtt_us); 3189932eb763SKenneth Klette Jonassen ca_ops->pkts_acked(sk, pkts_acked, rtt_us); 3190932eb763SKenneth Klette Jonassen } 3191b9ce204fSIlpo Järvinen 3192740b0f18SEric Dumazet } else if (skb && rtt_update && sack_rtt_us >= 0 && 3193740b0f18SEric Dumazet sack_rtt_us > skb_mstamp_us_delta(&now, &skb->skb_mstamp)) { 31942f715c1dSYuchung Cheng /* Do not re-arm RTO if the sack RTT is measured from data sent 31952f715c1dSYuchung Cheng * after when the head was last (re)transmitted. Otherwise the 31962f715c1dSYuchung Cheng * timeout may continue to extend in loss recovery. 31972f715c1dSYuchung Cheng */ 31982f715c1dSYuchung Cheng tcp_rearm_rto(sk); 31991da177e4SLinus Torvalds } 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0 3202547b792cSIlpo Järvinen WARN_ON((int)tp->sacked_out < 0); 3203547b792cSIlpo Järvinen WARN_ON((int)tp->lost_out < 0); 3204547b792cSIlpo Järvinen WARN_ON((int)tp->retrans_out < 0); 3205e60402d0SIlpo Järvinen if (!tp->packets_out && tcp_is_sack(tp)) { 3206cfcabdccSStephen Hemminger icsk = inet_csk(sk); 32071da177e4SLinus Torvalds if (tp->lost_out) { 320891df42beSJoe Perches pr_debug("Leak l=%u %d\n", 32096687e988SArnaldo Carvalho de Melo tp->lost_out, icsk->icsk_ca_state); 32101da177e4SLinus Torvalds tp->lost_out = 0; 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds if (tp->sacked_out) { 321391df42beSJoe Perches pr_debug("Leak s=%u %d\n", 32146687e988SArnaldo Carvalho de Melo tp->sacked_out, icsk->icsk_ca_state); 32151da177e4SLinus Torvalds tp->sacked_out = 0; 32161da177e4SLinus Torvalds } 32171da177e4SLinus Torvalds if (tp->retrans_out) { 321891df42beSJoe Perches pr_debug("Leak r=%u %d\n", 32196687e988SArnaldo Carvalho de Melo tp->retrans_out, icsk->icsk_ca_state); 32201da177e4SLinus Torvalds tp->retrans_out = 0; 32211da177e4SLinus Torvalds } 32221da177e4SLinus Torvalds } 32231da177e4SLinus Torvalds #endif 32247c46a03eSIlpo Järvinen return flag; 32251da177e4SLinus Torvalds } 32261da177e4SLinus Torvalds 32271da177e4SLinus Torvalds static void tcp_ack_probe(struct sock *sk) 32281da177e4SLinus Torvalds { 3229463c84b9SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 3230463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 32311da177e4SLinus Torvalds 32321da177e4SLinus Torvalds /* Was it a usable window open? */ 32331da177e4SLinus Torvalds 323490840defSIlpo Järvinen if (!after(TCP_SKB_CB(tcp_send_head(sk))->end_seq, tcp_wnd_end(tp))) { 3235463c84b9SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 3236463c84b9SArnaldo Carvalho de Melo inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0); 32371da177e4SLinus Torvalds /* Socket must be waked up by subsequent tcp_data_snd_check(). 32381da177e4SLinus Torvalds * This function is not for random using! 32391da177e4SLinus Torvalds */ 32401da177e4SLinus Torvalds } else { 3241fcdd1cf4SEric Dumazet unsigned long when = inet_csk_rto_backoff(icsk, TCP_RTO_MAX); 3242fcdd1cf4SEric Dumazet 3243463c84b9SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0, 3244fcdd1cf4SEric Dumazet when, TCP_RTO_MAX); 32451da177e4SLinus Torvalds } 32461da177e4SLinus Torvalds } 32471da177e4SLinus Torvalds 324867b95bd7SVijay Subramanian static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag) 32491da177e4SLinus Torvalds { 3250a02cec21SEric Dumazet return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) || 3251a02cec21SEric Dumazet inet_csk(sk)->icsk_ca_state != TCP_CA_Open; 32521da177e4SLinus Torvalds } 32531da177e4SLinus Torvalds 32540f7cc9a3SYuchung Cheng /* Decide wheather to run the increase function of congestion control. */ 325567b95bd7SVijay Subramanian static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag) 32561da177e4SLinus Torvalds { 32570f7cc9a3SYuchung Cheng if (tcp_in_cwnd_reduction(sk)) 32580f7cc9a3SYuchung Cheng return false; 32590f7cc9a3SYuchung Cheng 32600f7cc9a3SYuchung Cheng /* If reordering is high then always grow cwnd whenever data is 32610f7cc9a3SYuchung Cheng * delivered regardless of its ordering. Otherwise stay conservative 326216edfe7eSYuchung Cheng * and only grow cwnd on in-order delivery (RFC5681). A stretched ACK w/ 32630f7cc9a3SYuchung Cheng * new SACK or ECE mark may first advance cwnd here and later reduce 32640f7cc9a3SYuchung Cheng * cwnd in tcp_fastretrans_alert() based on more states. 32650f7cc9a3SYuchung Cheng */ 32660f7cc9a3SYuchung Cheng if (tcp_sk(sk)->reordering > sysctl_tcp_reordering) 32670f7cc9a3SYuchung Cheng return flag & FLAG_FORWARD_PROGRESS; 32680f7cc9a3SYuchung Cheng 326916edfe7eSYuchung Cheng return flag & FLAG_DATA_ACKED; 32701da177e4SLinus Torvalds } 32711da177e4SLinus Torvalds 32721da177e4SLinus Torvalds /* Check that window update is acceptable. 32731da177e4SLinus Torvalds * The function assumes that snd_una<=ack<=snd_next. 32741da177e4SLinus Torvalds */ 327567b95bd7SVijay Subramanian static inline bool tcp_may_update_window(const struct tcp_sock *tp, 3276056834d9SIlpo Järvinen const u32 ack, const u32 ack_seq, 3277056834d9SIlpo Järvinen const u32 nwin) 32781da177e4SLinus Torvalds { 3279a02cec21SEric Dumazet return after(ack, tp->snd_una) || 32801da177e4SLinus Torvalds after(ack_seq, tp->snd_wl1) || 3281a02cec21SEric Dumazet (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd); 32821da177e4SLinus Torvalds } 32831da177e4SLinus Torvalds 32841da177e4SLinus Torvalds /* Update our send window. 32851da177e4SLinus Torvalds * 32861da177e4SLinus Torvalds * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2 32871da177e4SLinus Torvalds * and in FreeBSD. NetBSD's one is even worse.) is wrong. 32881da177e4SLinus Torvalds */ 3289cf533ea5SEric Dumazet static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack, 32909e412ba7SIlpo Järvinen u32 ack_seq) 32911da177e4SLinus Torvalds { 32929e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 32931da177e4SLinus Torvalds int flag = 0; 3294aa8223c7SArnaldo Carvalho de Melo u32 nwin = ntohs(tcp_hdr(skb)->window); 32951da177e4SLinus Torvalds 3296aa8223c7SArnaldo Carvalho de Melo if (likely(!tcp_hdr(skb)->syn)) 32971da177e4SLinus Torvalds nwin <<= tp->rx_opt.snd_wscale; 32981da177e4SLinus Torvalds 32991da177e4SLinus Torvalds if (tcp_may_update_window(tp, ack, ack_seq, nwin)) { 33001da177e4SLinus Torvalds flag |= FLAG_WIN_UPDATE; 3301ee7537b6SHantzis Fotis tcp_update_wl(tp, ack_seq); 33021da177e4SLinus Torvalds 33031da177e4SLinus Torvalds if (tp->snd_wnd != nwin) { 33041da177e4SLinus Torvalds tp->snd_wnd = nwin; 33051da177e4SLinus Torvalds 33061da177e4SLinus Torvalds /* Note, it is the only place, where 33071da177e4SLinus Torvalds * fast path is recovered for sending TCP. 33081da177e4SLinus Torvalds */ 33092ad41065SHerbert Xu tp->pred_flags = 0; 33109e412ba7SIlpo Järvinen tcp_fast_path_check(sk); 33111da177e4SLinus Torvalds 33121da177e4SLinus Torvalds if (nwin > tp->max_window) { 33131da177e4SLinus Torvalds tp->max_window = nwin; 3314d83d8461SArnaldo Carvalho de Melo tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie); 33151da177e4SLinus Torvalds } 33161da177e4SLinus Torvalds } 33171da177e4SLinus Torvalds } 33181da177e4SLinus Torvalds 33191da177e4SLinus Torvalds tp->snd_una = ack; 33201da177e4SLinus Torvalds 33211da177e4SLinus Torvalds return flag; 33221da177e4SLinus Torvalds } 33231da177e4SLinus Torvalds 3324354e4aa3SEric Dumazet /* RFC 5961 7 [ACK Throttling] */ 3325354e4aa3SEric Dumazet static void tcp_send_challenge_ack(struct sock *sk) 3326354e4aa3SEric Dumazet { 3327354e4aa3SEric Dumazet /* unprotected vars, we dont care of overwrites */ 3328354e4aa3SEric Dumazet static u32 challenge_timestamp; 3329354e4aa3SEric Dumazet static unsigned int challenge_count; 3330354e4aa3SEric Dumazet u32 now = jiffies / HZ; 3331354e4aa3SEric Dumazet 3332354e4aa3SEric Dumazet if (now != challenge_timestamp) { 3333354e4aa3SEric Dumazet challenge_timestamp = now; 3334354e4aa3SEric Dumazet challenge_count = 0; 3335354e4aa3SEric Dumazet } 3336354e4aa3SEric Dumazet if (++challenge_count <= sysctl_tcp_challenge_ack_limit) { 3337354e4aa3SEric Dumazet NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPCHALLENGEACK); 3338354e4aa3SEric Dumazet tcp_send_ack(sk); 3339354e4aa3SEric Dumazet } 3340354e4aa3SEric Dumazet } 3341354e4aa3SEric Dumazet 334212fb3dd9SEric Dumazet static void tcp_store_ts_recent(struct tcp_sock *tp) 334312fb3dd9SEric Dumazet { 334412fb3dd9SEric Dumazet tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval; 334512fb3dd9SEric Dumazet tp->rx_opt.ts_recent_stamp = get_seconds(); 334612fb3dd9SEric Dumazet } 334712fb3dd9SEric Dumazet 334812fb3dd9SEric Dumazet static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq) 334912fb3dd9SEric Dumazet { 335012fb3dd9SEric Dumazet if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) { 335112fb3dd9SEric Dumazet /* PAWS bug workaround wrt. ACK frames, the PAWS discard 335212fb3dd9SEric Dumazet * extra check below makes sure this can only happen 335312fb3dd9SEric Dumazet * for pure ACK frames. -DaveM 335412fb3dd9SEric Dumazet * 335512fb3dd9SEric Dumazet * Not only, also it occurs for expired timestamps. 335612fb3dd9SEric Dumazet */ 335712fb3dd9SEric Dumazet 335812fb3dd9SEric Dumazet if (tcp_paws_check(&tp->rx_opt, 0)) 335912fb3dd9SEric Dumazet tcp_store_ts_recent(tp); 336012fb3dd9SEric Dumazet } 336112fb3dd9SEric Dumazet } 336212fb3dd9SEric Dumazet 33639b717a8dSNandita Dukkipati /* This routine deals with acks during a TLP episode. 336408abdffaSSébastien Barré * We mark the end of a TLP episode on receiving TLP dupack or when 336508abdffaSSébastien Barré * ack is after tlp_high_seq. 33669b717a8dSNandita Dukkipati * Ref: loss detection algorithm in draft-dukkipati-tcpm-tcp-loss-probe. 33679b717a8dSNandita Dukkipati */ 33689b717a8dSNandita Dukkipati static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag) 33699b717a8dSNandita Dukkipati { 33709b717a8dSNandita Dukkipati struct tcp_sock *tp = tcp_sk(sk); 33719b717a8dSNandita Dukkipati 337208abdffaSSébastien Barré if (before(ack, tp->tlp_high_seq)) 33739b717a8dSNandita Dukkipati return; 33749b717a8dSNandita Dukkipati 337508abdffaSSébastien Barré if (flag & FLAG_DSACKING_ACK) { 337608abdffaSSébastien Barré /* This DSACK means original and TLP probe arrived; no loss */ 33779b717a8dSNandita Dukkipati tp->tlp_high_seq = 0; 337808abdffaSSébastien Barré } else if (after(ack, tp->tlp_high_seq)) { 337908abdffaSSébastien Barré /* ACK advances: there was a loss, so reduce cwnd. Reset 338008abdffaSSébastien Barré * tlp_high_seq in tcp_init_cwnd_reduction() 338108abdffaSSébastien Barré */ 33825ee2c941SChristoph Paasch tcp_init_cwnd_reduction(sk); 33839b717a8dSNandita Dukkipati tcp_set_ca_state(sk, TCP_CA_CWR); 33849b717a8dSNandita Dukkipati tcp_end_cwnd_reduction(sk); 3385031afe49SYuchung Cheng tcp_try_keep_open(sk); 33869b717a8dSNandita Dukkipati NET_INC_STATS_BH(sock_net(sk), 33879b717a8dSNandita Dukkipati LINUX_MIB_TCPLOSSPROBERECOVERY); 338808abdffaSSébastien Barré } else if (!(flag & (FLAG_SND_UNA_ADVANCED | 338908abdffaSSébastien Barré FLAG_NOT_DUP | FLAG_DATA_SACKED))) { 339008abdffaSSébastien Barré /* Pure dupack: original and TLP probe arrived; no loss */ 339108abdffaSSébastien Barré tp->tlp_high_seq = 0; 33929b717a8dSNandita Dukkipati } 33939b717a8dSNandita Dukkipati } 33949b717a8dSNandita Dukkipati 33957354c8c3SFlorian Westphal static inline void tcp_in_ack_event(struct sock *sk, u32 flags) 33967354c8c3SFlorian Westphal { 33977354c8c3SFlorian Westphal const struct inet_connection_sock *icsk = inet_csk(sk); 33987354c8c3SFlorian Westphal 33997354c8c3SFlorian Westphal if (icsk->icsk_ca_ops->in_ack_event) 34007354c8c3SFlorian Westphal icsk->icsk_ca_ops->in_ack_event(sk, flags); 34017354c8c3SFlorian Westphal } 34027354c8c3SFlorian Westphal 34031da177e4SLinus Torvalds /* This routine deals with incoming acks, but not outgoing ones. */ 3404cf533ea5SEric Dumazet static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag) 34051da177e4SLinus Torvalds { 34066687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 34071da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 34081da177e4SLinus Torvalds u32 prior_snd_una = tp->snd_una; 34091da177e4SLinus Torvalds u32 ack_seq = TCP_SKB_CB(skb)->seq; 34101da177e4SLinus Torvalds u32 ack = TCP_SKB_CB(skb)->ack_seq; 34117d2b55f8SNeal Cardwell bool is_dupack = false; 3412c7caf8d3SIlpo Järvinen u32 prior_fackets; 341335f079ebSNandita Dukkipati int prior_packets = tp->packets_out; 34146804973fSYuchung Cheng const int prior_unsacked = tp->packets_out - tp->sacked_out; 34156804973fSYuchung Cheng int acked = 0; /* Number of packets newly acked */ 3416740b0f18SEric Dumazet long sack_rtt_us = -1L; 34171da177e4SLinus Torvalds 3418ad971f61SEric Dumazet /* We very likely will need to access write queue head. */ 3419ad971f61SEric Dumazet prefetchw(sk->sk_write_queue.next); 3420ad971f61SEric Dumazet 342196e0bf4bSJohn Dykstra /* If the ack is older than previous acks 34221da177e4SLinus Torvalds * then we can probably ignore it. 34231da177e4SLinus Torvalds */ 3424354e4aa3SEric Dumazet if (before(ack, prior_snd_una)) { 3425354e4aa3SEric Dumazet /* RFC 5961 5.2 [Blind Data Injection Attack].[Mitigation] */ 3426354e4aa3SEric Dumazet if (before(ack, prior_snd_una - tp->max_window)) { 3427354e4aa3SEric Dumazet tcp_send_challenge_ack(sk); 3428354e4aa3SEric Dumazet return -1; 3429354e4aa3SEric Dumazet } 34301da177e4SLinus Torvalds goto old_ack; 3431354e4aa3SEric Dumazet } 34321da177e4SLinus Torvalds 343396e0bf4bSJohn Dykstra /* If the ack includes data we haven't sent yet, discard 343496e0bf4bSJohn Dykstra * this segment (RFC793 Section 3.9). 343596e0bf4bSJohn Dykstra */ 343696e0bf4bSJohn Dykstra if (after(ack, tp->snd_nxt)) 343796e0bf4bSJohn Dykstra goto invalid_ack; 343896e0bf4bSJohn Dykstra 34396ba8a3b1SNandita Dukkipati if (icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS || 34406ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) 3441750ea2baSYuchung Cheng tcp_rearm_rto(sk); 3442750ea2baSYuchung Cheng 34430c9ab092SNeal Cardwell if (after(ack, prior_snd_una)) { 34442e605294SIlpo Järvinen flag |= FLAG_SND_UNA_ADVANCED; 34450c9ab092SNeal Cardwell icsk->icsk_retransmits = 0; 34460c9ab092SNeal Cardwell } 34472e605294SIlpo Järvinen 3448c7caf8d3SIlpo Järvinen prior_fackets = tp->fackets_out; 3449c7caf8d3SIlpo Järvinen 345012fb3dd9SEric Dumazet /* ts_recent update must be made after we are sure that the packet 345112fb3dd9SEric Dumazet * is in window. 345212fb3dd9SEric Dumazet */ 345312fb3dd9SEric Dumazet if (flag & FLAG_UPDATE_TS_RECENT) 345412fb3dd9SEric Dumazet tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq); 345512fb3dd9SEric Dumazet 34561da177e4SLinus Torvalds if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) { 34571da177e4SLinus Torvalds /* Window is constant, pure forward advance. 34581da177e4SLinus Torvalds * No more checks are required. 34591da177e4SLinus Torvalds * Note, we use the fact that SND.UNA>=SND.WL2. 34601da177e4SLinus Torvalds */ 3461ee7537b6SHantzis Fotis tcp_update_wl(tp, ack_seq); 34621da177e4SLinus Torvalds tp->snd_una = ack; 34631da177e4SLinus Torvalds flag |= FLAG_WIN_UPDATE; 34641da177e4SLinus Torvalds 34659890092eSFlorian Westphal tcp_in_ack_event(sk, CA_ACK_WIN_UPDATE); 3466317a76f9SStephen Hemminger 3467de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPACKS); 34681da177e4SLinus Torvalds } else { 34699890092eSFlorian Westphal u32 ack_ev_flags = CA_ACK_SLOWPATH; 34709890092eSFlorian Westphal 34711da177e4SLinus Torvalds if (ack_seq != TCP_SKB_CB(skb)->end_seq) 34721da177e4SLinus Torvalds flag |= FLAG_DATA; 34731da177e4SLinus Torvalds else 3474de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPPUREACKS); 34751da177e4SLinus Torvalds 34769e412ba7SIlpo Järvinen flag |= tcp_ack_update_window(sk, skb, ack, ack_seq); 34771da177e4SLinus Torvalds 34781da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->sacked) 347959c9af42SYuchung Cheng flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una, 3480740b0f18SEric Dumazet &sack_rtt_us); 34811da177e4SLinus Torvalds 3482735d3831SFlorian Westphal if (tcp_ecn_rcv_ecn_echo(tp, tcp_hdr(skb))) { 34831da177e4SLinus Torvalds flag |= FLAG_ECE; 34849890092eSFlorian Westphal ack_ev_flags |= CA_ACK_ECE; 34859890092eSFlorian Westphal } 34861da177e4SLinus Torvalds 34879890092eSFlorian Westphal if (flag & FLAG_WIN_UPDATE) 34889890092eSFlorian Westphal ack_ev_flags |= CA_ACK_WIN_UPDATE; 34899890092eSFlorian Westphal 34909890092eSFlorian Westphal tcp_in_ack_event(sk, ack_ev_flags); 34911da177e4SLinus Torvalds } 34921da177e4SLinus Torvalds 34931da177e4SLinus Torvalds /* We passed data and got it acked, remove any soft error 34941da177e4SLinus Torvalds * log. Something worked... 34951da177e4SLinus Torvalds */ 34961da177e4SLinus Torvalds sk->sk_err_soft = 0; 34974b53fb67SDavid S. Miller icsk->icsk_probes_out = 0; 34981da177e4SLinus Torvalds tp->rcv_tstamp = tcp_time_stamp; 34991da177e4SLinus Torvalds if (!prior_packets) 35001da177e4SLinus Torvalds goto no_queue; 35011da177e4SLinus Torvalds 35021da177e4SLinus Torvalds /* See if we can take anything off of the retransmit queue. */ 35036804973fSYuchung Cheng acked = tp->packets_out; 3504740b0f18SEric Dumazet flag |= tcp_clean_rtx_queue(sk, prior_fackets, prior_snd_una, 3505740b0f18SEric Dumazet sack_rtt_us); 35066804973fSYuchung Cheng acked -= tp->packets_out; 3507a262f0cdSNandita Dukkipati 35080f7cc9a3SYuchung Cheng /* Advance cwnd if state allows */ 35090f7cc9a3SYuchung Cheng if (tcp_may_raise_cwnd(sk, flag)) 351024901551SEric Dumazet tcp_cong_avoid(sk, ack, acked); 35110f7cc9a3SYuchung Cheng 35120f7cc9a3SYuchung Cheng if (tcp_ack_is_dubious(sk, flag)) { 35137d2b55f8SNeal Cardwell is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP)); 35146804973fSYuchung Cheng tcp_fastretrans_alert(sk, acked, prior_unsacked, 35156804973fSYuchung Cheng is_dupack, flag); 35161da177e4SLinus Torvalds } 35179b717a8dSNandita Dukkipati if (tp->tlp_high_seq) 35189b717a8dSNandita Dukkipati tcp_process_tlp_ack(sk, ack, flag); 35199b717a8dSNandita Dukkipati 35205110effeSDavid S. Miller if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP)) { 35215110effeSDavid S. Miller struct dst_entry *dst = __sk_dst_get(sk); 35225110effeSDavid S. Miller if (dst) 35235110effeSDavid S. Miller dst_confirm(dst); 35245110effeSDavid S. Miller } 35256ba8a3b1SNandita Dukkipati 35266ba8a3b1SNandita Dukkipati if (icsk->icsk_pending == ICSK_TIME_RETRANS) 35276ba8a3b1SNandita Dukkipati tcp_schedule_loss_probe(sk); 352895bd09ebSEric Dumazet tcp_update_pacing_rate(sk); 35291da177e4SLinus Torvalds return 1; 35301da177e4SLinus Torvalds 35311da177e4SLinus Torvalds no_queue: 35325628adf1SNeal Cardwell /* If data was DSACKed, see if we can undo a cwnd reduction. */ 35335628adf1SNeal Cardwell if (flag & FLAG_DSACKING_ACK) 35346804973fSYuchung Cheng tcp_fastretrans_alert(sk, acked, prior_unsacked, 35356804973fSYuchung Cheng is_dupack, flag); 35361da177e4SLinus Torvalds /* If this ack opens up a zero window, clear backoff. It was 35371da177e4SLinus Torvalds * being used to time the probes, and is probably far higher than 35381da177e4SLinus Torvalds * it needs to be for normal retransmission. 35391da177e4SLinus Torvalds */ 3540fe067e8aSDavid S. Miller if (tcp_send_head(sk)) 35411da177e4SLinus Torvalds tcp_ack_probe(sk); 35429b717a8dSNandita Dukkipati 35439b717a8dSNandita Dukkipati if (tp->tlp_high_seq) 35449b717a8dSNandita Dukkipati tcp_process_tlp_ack(sk, ack, flag); 35451da177e4SLinus Torvalds return 1; 35461da177e4SLinus Torvalds 354796e0bf4bSJohn Dykstra invalid_ack: 354896e0bf4bSJohn Dykstra SOCK_DEBUG(sk, "Ack %u after %u:%u\n", ack, tp->snd_una, tp->snd_nxt); 354996e0bf4bSJohn Dykstra return -1; 355096e0bf4bSJohn Dykstra 35511da177e4SLinus Torvalds old_ack: 3552e95ae2f2SNeal Cardwell /* If data was SACKed, tag it and see if we should send more data. 3553e95ae2f2SNeal Cardwell * If data was DSACKed, see if we can undo a cwnd reduction. 3554e95ae2f2SNeal Cardwell */ 35558aca6cb1SIlpo Järvinen if (TCP_SKB_CB(skb)->sacked) { 355659c9af42SYuchung Cheng flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una, 3557740b0f18SEric Dumazet &sack_rtt_us); 35586804973fSYuchung Cheng tcp_fastretrans_alert(sk, acked, prior_unsacked, 35596804973fSYuchung Cheng is_dupack, flag); 35608aca6cb1SIlpo Järvinen } 35611da177e4SLinus Torvalds 356296e0bf4bSJohn Dykstra SOCK_DEBUG(sk, "Ack %u before %u:%u\n", ack, tp->snd_una, tp->snd_nxt); 35631da177e4SLinus Torvalds return 0; 35641da177e4SLinus Torvalds } 35651da177e4SLinus Torvalds 35661da177e4SLinus Torvalds /* Look for tcp options. Normally only called on SYN and SYNACK packets. 35671da177e4SLinus Torvalds * But, this can also be called on packets in the established flow when 35681da177e4SLinus Torvalds * the fast version below fails. 35691da177e4SLinus Torvalds */ 35701a2c6181SChristoph Paasch void tcp_parse_options(const struct sk_buff *skb, 35711a2c6181SChristoph Paasch struct tcp_options_received *opt_rx, int estab, 35722100c8d2SYuchung Cheng struct tcp_fastopen_cookie *foc) 35731da177e4SLinus Torvalds { 3574cf533ea5SEric Dumazet const unsigned char *ptr; 3575cf533ea5SEric Dumazet const struct tcphdr *th = tcp_hdr(skb); 35761da177e4SLinus Torvalds int length = (th->doff * 4) - sizeof(struct tcphdr); 35771da177e4SLinus Torvalds 3578cf533ea5SEric Dumazet ptr = (const unsigned char *)(th + 1); 35791da177e4SLinus Torvalds opt_rx->saw_tstamp = 0; 35801da177e4SLinus Torvalds 35811da177e4SLinus Torvalds while (length > 0) { 35821da177e4SLinus Torvalds int opcode = *ptr++; 35831da177e4SLinus Torvalds int opsize; 35841da177e4SLinus Torvalds 35851da177e4SLinus Torvalds switch (opcode) { 35861da177e4SLinus Torvalds case TCPOPT_EOL: 35871da177e4SLinus Torvalds return; 35881da177e4SLinus Torvalds case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ 35891da177e4SLinus Torvalds length--; 35901da177e4SLinus Torvalds continue; 35911da177e4SLinus Torvalds default: 35921da177e4SLinus Torvalds opsize = *ptr++; 35931da177e4SLinus Torvalds if (opsize < 2) /* "silly options" */ 35941da177e4SLinus Torvalds return; 35951da177e4SLinus Torvalds if (opsize > length) 35961da177e4SLinus Torvalds return; /* don't parse partial options */ 35971da177e4SLinus Torvalds switch (opcode) { 35981da177e4SLinus Torvalds case TCPOPT_MSS: 35991da177e4SLinus Torvalds if (opsize == TCPOLEN_MSS && th->syn && !estab) { 3600d3e2ce3bSHarvey Harrison u16 in_mss = get_unaligned_be16(ptr); 36011da177e4SLinus Torvalds if (in_mss) { 3602f038ac8fSIlpo Järvinen if (opt_rx->user_mss && 3603f038ac8fSIlpo Järvinen opt_rx->user_mss < in_mss) 36041da177e4SLinus Torvalds in_mss = opt_rx->user_mss; 36051da177e4SLinus Torvalds opt_rx->mss_clamp = in_mss; 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds } 36081da177e4SLinus Torvalds break; 36091da177e4SLinus Torvalds case TCPOPT_WINDOW: 3610f038ac8fSIlpo Järvinen if (opsize == TCPOLEN_WINDOW && th->syn && 3611bb5b7c11SDavid S. Miller !estab && sysctl_tcp_window_scaling) { 36121da177e4SLinus Torvalds __u8 snd_wscale = *(__u8 *)ptr; 36131da177e4SLinus Torvalds opt_rx->wscale_ok = 1; 36141da177e4SLinus Torvalds if (snd_wscale > 14) { 3615e87cc472SJoe Perches net_info_ratelimited("%s: Illegal window scaling value %d >14 received\n", 3616058bd4d2SJoe Perches __func__, 36171da177e4SLinus Torvalds snd_wscale); 36181da177e4SLinus Torvalds snd_wscale = 14; 36191da177e4SLinus Torvalds } 36201da177e4SLinus Torvalds opt_rx->snd_wscale = snd_wscale; 36211da177e4SLinus Torvalds } 36221da177e4SLinus Torvalds break; 36231da177e4SLinus Torvalds case TCPOPT_TIMESTAMP: 3624f038ac8fSIlpo Järvinen if ((opsize == TCPOLEN_TIMESTAMP) && 3625f038ac8fSIlpo Järvinen ((estab && opt_rx->tstamp_ok) || 3626bb5b7c11SDavid S. Miller (!estab && sysctl_tcp_timestamps))) { 36271da177e4SLinus Torvalds opt_rx->saw_tstamp = 1; 3628d3e2ce3bSHarvey Harrison opt_rx->rcv_tsval = get_unaligned_be32(ptr); 3629d3e2ce3bSHarvey Harrison opt_rx->rcv_tsecr = get_unaligned_be32(ptr + 4); 36301da177e4SLinus Torvalds } 36311da177e4SLinus Torvalds break; 36321da177e4SLinus Torvalds case TCPOPT_SACK_PERM: 3633f038ac8fSIlpo Järvinen if (opsize == TCPOLEN_SACK_PERM && th->syn && 3634bb5b7c11SDavid S. Miller !estab && sysctl_tcp_sack) { 3635ab56222aSVijay Subramanian opt_rx->sack_ok = TCP_SACK_SEEN; 36361da177e4SLinus Torvalds tcp_sack_reset(opt_rx); 36371da177e4SLinus Torvalds } 36381da177e4SLinus Torvalds break; 36391da177e4SLinus Torvalds 36401da177e4SLinus Torvalds case TCPOPT_SACK: 36411da177e4SLinus Torvalds if ((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) && 36421da177e4SLinus Torvalds !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) && 36431da177e4SLinus Torvalds opt_rx->sack_ok) { 36441da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th; 36451da177e4SLinus Torvalds } 3646d7ea5b91SIlpo Järvinen break; 3647cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 3648cfb6eeb4SYOSHIFUJI Hideaki case TCPOPT_MD5SIG: 3649cfb6eeb4SYOSHIFUJI Hideaki /* 3650cfb6eeb4SYOSHIFUJI Hideaki * The MD5 Hash has already been 3651cfb6eeb4SYOSHIFUJI Hideaki * checked (see tcp_v{4,6}_do_rcv()). 3652cfb6eeb4SYOSHIFUJI Hideaki */ 3653cfb6eeb4SYOSHIFUJI Hideaki break; 3654cfb6eeb4SYOSHIFUJI Hideaki #endif 36552100c8d2SYuchung Cheng case TCPOPT_EXP: 36562100c8d2SYuchung Cheng /* Fast Open option shares code 254 using a 36572100c8d2SYuchung Cheng * 16 bits magic number. It's valid only in 36582100c8d2SYuchung Cheng * SYN or SYN-ACK with an even size. 36592100c8d2SYuchung Cheng */ 36602100c8d2SYuchung Cheng if (opsize < TCPOLEN_EXP_FASTOPEN_BASE || 36612100c8d2SYuchung Cheng get_unaligned_be16(ptr) != TCPOPT_FASTOPEN_MAGIC || 36622100c8d2SYuchung Cheng foc == NULL || !th->syn || (opsize & 1)) 36632100c8d2SYuchung Cheng break; 36642100c8d2SYuchung Cheng foc->len = opsize - TCPOLEN_EXP_FASTOPEN_BASE; 36652100c8d2SYuchung Cheng if (foc->len >= TCP_FASTOPEN_COOKIE_MIN && 36662100c8d2SYuchung Cheng foc->len <= TCP_FASTOPEN_COOKIE_MAX) 36672100c8d2SYuchung Cheng memcpy(foc->val, ptr + 2, foc->len); 36682100c8d2SYuchung Cheng else if (foc->len != 0) 36692100c8d2SYuchung Cheng foc->len = -1; 36702100c8d2SYuchung Cheng break; 36712100c8d2SYuchung Cheng 36722100c8d2SYuchung Cheng } 36731da177e4SLinus Torvalds ptr += opsize-2; 36741da177e4SLinus Torvalds length -= opsize; 36753ff50b79SStephen Hemminger } 36761da177e4SLinus Torvalds } 36771da177e4SLinus Torvalds } 36784bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_parse_options); 36791da177e4SLinus Torvalds 3680a2a385d6SEric Dumazet static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr *th) 3681a4356b29SIlpo Järvinen { 3682cf533ea5SEric Dumazet const __be32 *ptr = (const __be32 *)(th + 1); 3683a4356b29SIlpo Järvinen 3684a4356b29SIlpo Järvinen if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) 3685a4356b29SIlpo Järvinen | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) { 3686a4356b29SIlpo Järvinen tp->rx_opt.saw_tstamp = 1; 3687a4356b29SIlpo Järvinen ++ptr; 3688a4356b29SIlpo Järvinen tp->rx_opt.rcv_tsval = ntohl(*ptr); 3689a4356b29SIlpo Järvinen ++ptr; 3690e3e12028SAndrew Vagin if (*ptr) 3691ee684b6fSAndrey Vagin tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset; 3692e3e12028SAndrew Vagin else 3693e3e12028SAndrew Vagin tp->rx_opt.rcv_tsecr = 0; 3694a2a385d6SEric Dumazet return true; 3695a4356b29SIlpo Järvinen } 3696a2a385d6SEric Dumazet return false; 3697a4356b29SIlpo Järvinen } 3698a4356b29SIlpo Järvinen 36991da177e4SLinus Torvalds /* Fast parse options. This hopes to only see timestamps. 37001da177e4SLinus Torvalds * If it is wrong it falls back on tcp_parse_options(). 37011da177e4SLinus Torvalds */ 3702a2a385d6SEric Dumazet static bool tcp_fast_parse_options(const struct sk_buff *skb, 37031a2c6181SChristoph Paasch const struct tcphdr *th, struct tcp_sock *tp) 37041da177e4SLinus Torvalds { 37054957faadSWilliam Allen Simpson /* In the spirit of fast parsing, compare doff directly to constant 37064957faadSWilliam Allen Simpson * values. Because equality is used, short doff can be ignored here. 37074957faadSWilliam Allen Simpson */ 37084957faadSWilliam Allen Simpson if (th->doff == (sizeof(*th) / 4)) { 37091da177e4SLinus Torvalds tp->rx_opt.saw_tstamp = 0; 3710a2a385d6SEric Dumazet return false; 37111da177e4SLinus Torvalds } else if (tp->rx_opt.tstamp_ok && 37124957faadSWilliam Allen Simpson th->doff == ((sizeof(*th) + TCPOLEN_TSTAMP_ALIGNED) / 4)) { 3713a4356b29SIlpo Järvinen if (tcp_parse_aligned_timestamp(tp, th)) 3714a2a385d6SEric Dumazet return true; 37151da177e4SLinus Torvalds } 3716ee684b6fSAndrey Vagin 37171a2c6181SChristoph Paasch tcp_parse_options(skb, &tp->rx_opt, 1, NULL); 3718e3e12028SAndrew Vagin if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) 3719ee684b6fSAndrey Vagin tp->rx_opt.rcv_tsecr -= tp->tsoffset; 3720ee684b6fSAndrey Vagin 3721a2a385d6SEric Dumazet return true; 37221da177e4SLinus Torvalds } 37231da177e4SLinus Torvalds 37247d5d5525SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 37257d5d5525SYOSHIFUJI Hideaki /* 37267d5d5525SYOSHIFUJI Hideaki * Parse MD5 Signature option 37277d5d5525SYOSHIFUJI Hideaki */ 3728cf533ea5SEric Dumazet const u8 *tcp_parse_md5sig_option(const struct tcphdr *th) 37297d5d5525SYOSHIFUJI Hideaki { 37307d5d5525SYOSHIFUJI Hideaki int length = (th->doff << 2) - sizeof(*th); 3731cf533ea5SEric Dumazet const u8 *ptr = (const u8 *)(th + 1); 37327d5d5525SYOSHIFUJI Hideaki 37337d5d5525SYOSHIFUJI Hideaki /* If the TCP option is too short, we can short cut */ 37347d5d5525SYOSHIFUJI Hideaki if (length < TCPOLEN_MD5SIG) 37357d5d5525SYOSHIFUJI Hideaki return NULL; 37367d5d5525SYOSHIFUJI Hideaki 37377d5d5525SYOSHIFUJI Hideaki while (length > 0) { 37387d5d5525SYOSHIFUJI Hideaki int opcode = *ptr++; 37397d5d5525SYOSHIFUJI Hideaki int opsize; 37407d5d5525SYOSHIFUJI Hideaki 37417d5d5525SYOSHIFUJI Hideaki switch (opcode) { 37427d5d5525SYOSHIFUJI Hideaki case TCPOPT_EOL: 37437d5d5525SYOSHIFUJI Hideaki return NULL; 37447d5d5525SYOSHIFUJI Hideaki case TCPOPT_NOP: 37457d5d5525SYOSHIFUJI Hideaki length--; 37467d5d5525SYOSHIFUJI Hideaki continue; 37477d5d5525SYOSHIFUJI Hideaki default: 37487d5d5525SYOSHIFUJI Hideaki opsize = *ptr++; 37497d5d5525SYOSHIFUJI Hideaki if (opsize < 2 || opsize > length) 37507d5d5525SYOSHIFUJI Hideaki return NULL; 37517d5d5525SYOSHIFUJI Hideaki if (opcode == TCPOPT_MD5SIG) 3752ba78e2ddSDmitry Popov return opsize == TCPOLEN_MD5SIG ? ptr : NULL; 37537d5d5525SYOSHIFUJI Hideaki } 37547d5d5525SYOSHIFUJI Hideaki ptr += opsize - 2; 37557d5d5525SYOSHIFUJI Hideaki length -= opsize; 37567d5d5525SYOSHIFUJI Hideaki } 37577d5d5525SYOSHIFUJI Hideaki return NULL; 37587d5d5525SYOSHIFUJI Hideaki } 37594bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_parse_md5sig_option); 37607d5d5525SYOSHIFUJI Hideaki #endif 37617d5d5525SYOSHIFUJI Hideaki 37621da177e4SLinus Torvalds /* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM 37631da177e4SLinus Torvalds * 37641da177e4SLinus Torvalds * It is not fatal. If this ACK does _not_ change critical state (seqs, window) 37651da177e4SLinus Torvalds * it can pass through stack. So, the following predicate verifies that 37661da177e4SLinus Torvalds * this segment is not used for anything but congestion avoidance or 37671da177e4SLinus Torvalds * fast retransmit. Moreover, we even are able to eliminate most of such 37681da177e4SLinus Torvalds * second order effects, if we apply some small "replay" window (~RTO) 37691da177e4SLinus Torvalds * to timestamp space. 37701da177e4SLinus Torvalds * 37711da177e4SLinus Torvalds * All these measures still do not guarantee that we reject wrapped ACKs 37721da177e4SLinus Torvalds * on networks with high bandwidth, when sequence space is recycled fastly, 37731da177e4SLinus Torvalds * but it guarantees that such events will be very rare and do not affect 37741da177e4SLinus Torvalds * connection seriously. This doesn't look nice, but alas, PAWS is really 37751da177e4SLinus Torvalds * buggy extension. 37761da177e4SLinus Torvalds * 37771da177e4SLinus Torvalds * [ Later note. Even worse! It is buggy for segments _with_ data. RFC 37781da177e4SLinus Torvalds * states that events when retransmit arrives after original data are rare. 37791da177e4SLinus Torvalds * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is 37801da177e4SLinus Torvalds * the biggest problem on large power networks even with minor reordering. 37811da177e4SLinus Torvalds * OK, let's give it small replay window. If peer clock is even 1hz, it is safe 37821da177e4SLinus Torvalds * up to bandwidth of 18Gigabit/sec. 8) ] 37831da177e4SLinus Torvalds */ 37841da177e4SLinus Torvalds 3785463c84b9SArnaldo Carvalho de Melo static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb) 37861da177e4SLinus Torvalds { 3787cf533ea5SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 3788cf533ea5SEric Dumazet const struct tcphdr *th = tcp_hdr(skb); 37891da177e4SLinus Torvalds u32 seq = TCP_SKB_CB(skb)->seq; 37901da177e4SLinus Torvalds u32 ack = TCP_SKB_CB(skb)->ack_seq; 37911da177e4SLinus Torvalds 37921da177e4SLinus Torvalds return (/* 1. Pure ACK with correct sequence number. */ 37931da177e4SLinus Torvalds (th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) && 37941da177e4SLinus Torvalds 37951da177e4SLinus Torvalds /* 2. ... and duplicate ACK. */ 37961da177e4SLinus Torvalds ack == tp->snd_una && 37971da177e4SLinus Torvalds 37981da177e4SLinus Torvalds /* 3. ... and does not update window. */ 37991da177e4SLinus Torvalds !tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) && 38001da177e4SLinus Torvalds 38011da177e4SLinus Torvalds /* 4. ... and sits in replay window. */ 3802463c84b9SArnaldo Carvalho de Melo (s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) <= (inet_csk(sk)->icsk_rto * 1024) / HZ); 38031da177e4SLinus Torvalds } 38041da177e4SLinus Torvalds 380567b95bd7SVijay Subramanian static inline bool tcp_paws_discard(const struct sock *sk, 3806056834d9SIlpo Järvinen const struct sk_buff *skb) 38071da177e4SLinus Torvalds { 3808463c84b9SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 3809c887e6d2SIlpo Järvinen 3810c887e6d2SIlpo Järvinen return !tcp_paws_check(&tp->rx_opt, TCP_PAWS_WINDOW) && 3811c887e6d2SIlpo Järvinen !tcp_disordered_ack(sk, skb); 38121da177e4SLinus Torvalds } 38131da177e4SLinus Torvalds 38141da177e4SLinus Torvalds /* Check segment sequence number for validity. 38151da177e4SLinus Torvalds * 38161da177e4SLinus Torvalds * Segment controls are considered valid, if the segment 38171da177e4SLinus Torvalds * fits to the window after truncation to the window. Acceptability 38181da177e4SLinus Torvalds * of data (and SYN, FIN, of course) is checked separately. 38191da177e4SLinus Torvalds * See tcp_data_queue(), for example. 38201da177e4SLinus Torvalds * 38211da177e4SLinus Torvalds * Also, controls (RST is main one) are accepted using RCV.WUP instead 38221da177e4SLinus Torvalds * of RCV.NXT. Peer still did not advance his SND.UNA when we 38231da177e4SLinus Torvalds * delayed ACK, so that hisSND.UNA<=ourRCV.WUP. 38241da177e4SLinus Torvalds * (borrowed from freebsd) 38251da177e4SLinus Torvalds */ 38261da177e4SLinus Torvalds 382767b95bd7SVijay Subramanian static inline bool tcp_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq) 38281da177e4SLinus Torvalds { 38291da177e4SLinus Torvalds return !before(end_seq, tp->rcv_wup) && 38301da177e4SLinus Torvalds !after(seq, tp->rcv_nxt + tcp_receive_window(tp)); 38311da177e4SLinus Torvalds } 38321da177e4SLinus Torvalds 38331da177e4SLinus Torvalds /* When we get a reset we do this. */ 383410467163SJerry Chu void tcp_reset(struct sock *sk) 38351da177e4SLinus Torvalds { 38361da177e4SLinus Torvalds /* We want the right error as BSD sees it (and indeed as we do). */ 38371da177e4SLinus Torvalds switch (sk->sk_state) { 38381da177e4SLinus Torvalds case TCP_SYN_SENT: 38391da177e4SLinus Torvalds sk->sk_err = ECONNREFUSED; 38401da177e4SLinus Torvalds break; 38411da177e4SLinus Torvalds case TCP_CLOSE_WAIT: 38421da177e4SLinus Torvalds sk->sk_err = EPIPE; 38431da177e4SLinus Torvalds break; 38441da177e4SLinus Torvalds case TCP_CLOSE: 38451da177e4SLinus Torvalds return; 38461da177e4SLinus Torvalds default: 38471da177e4SLinus Torvalds sk->sk_err = ECONNRESET; 38481da177e4SLinus Torvalds } 3849a4d25803STom Marshall /* This barrier is coupled with smp_rmb() in tcp_poll() */ 3850a4d25803STom Marshall smp_wmb(); 38511da177e4SLinus Torvalds 38521da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) 38531da177e4SLinus Torvalds sk->sk_error_report(sk); 38541da177e4SLinus Torvalds 38551da177e4SLinus Torvalds tcp_done(sk); 38561da177e4SLinus Torvalds } 38571da177e4SLinus Torvalds 38581da177e4SLinus Torvalds /* 38591da177e4SLinus Torvalds * Process the FIN bit. This now behaves as it is supposed to work 38601da177e4SLinus Torvalds * and the FIN takes effect when it is validly part of sequence 38611da177e4SLinus Torvalds * space. Not before when we get holes. 38621da177e4SLinus Torvalds * 38631da177e4SLinus Torvalds * If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT 38641da177e4SLinus Torvalds * (and thence onto LAST-ACK and finally, CLOSE, we never enter 38651da177e4SLinus Torvalds * TIME-WAIT) 38661da177e4SLinus Torvalds * 38671da177e4SLinus Torvalds * If we are in FINWAIT-1, a received FIN indicates simultaneous 38681da177e4SLinus Torvalds * close and we go into CLOSING (and later onto TIME-WAIT) 38691da177e4SLinus Torvalds * 38701da177e4SLinus Torvalds * If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT. 38711da177e4SLinus Torvalds */ 387220c4cb79SEric Dumazet static void tcp_fin(struct sock *sk) 38731da177e4SLinus Torvalds { 38741da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 3875bcefe17cSCong Wang const struct dst_entry *dst; 38761da177e4SLinus Torvalds 3877463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 38781da177e4SLinus Torvalds 38791da177e4SLinus Torvalds sk->sk_shutdown |= RCV_SHUTDOWN; 38801da177e4SLinus Torvalds sock_set_flag(sk, SOCK_DONE); 38811da177e4SLinus Torvalds 38821da177e4SLinus Torvalds switch (sk->sk_state) { 38831da177e4SLinus Torvalds case TCP_SYN_RECV: 38841da177e4SLinus Torvalds case TCP_ESTABLISHED: 38851da177e4SLinus Torvalds /* Move to CLOSE_WAIT */ 38861da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSE_WAIT); 3887bcefe17cSCong Wang dst = __sk_dst_get(sk); 3888bcefe17cSCong Wang if (!dst || !dst_metric(dst, RTAX_QUICKACK)) 3889463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_ack.pingpong = 1; 38901da177e4SLinus Torvalds break; 38911da177e4SLinus Torvalds 38921da177e4SLinus Torvalds case TCP_CLOSE_WAIT: 38931da177e4SLinus Torvalds case TCP_CLOSING: 38941da177e4SLinus Torvalds /* Received a retransmission of the FIN, do 38951da177e4SLinus Torvalds * nothing. 38961da177e4SLinus Torvalds */ 38971da177e4SLinus Torvalds break; 38981da177e4SLinus Torvalds case TCP_LAST_ACK: 38991da177e4SLinus Torvalds /* RFC793: Remain in the LAST-ACK state. */ 39001da177e4SLinus Torvalds break; 39011da177e4SLinus Torvalds 39021da177e4SLinus Torvalds case TCP_FIN_WAIT1: 39031da177e4SLinus Torvalds /* This case occurs when a simultaneous close 39041da177e4SLinus Torvalds * happens, we must ack the received FIN and 39051da177e4SLinus Torvalds * enter the CLOSING state. 39061da177e4SLinus Torvalds */ 39071da177e4SLinus Torvalds tcp_send_ack(sk); 39081da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSING); 39091da177e4SLinus Torvalds break; 39101da177e4SLinus Torvalds case TCP_FIN_WAIT2: 39111da177e4SLinus Torvalds /* Received a FIN -- send ACK and enter TIME_WAIT. */ 39121da177e4SLinus Torvalds tcp_send_ack(sk); 39131da177e4SLinus Torvalds tcp_time_wait(sk, TCP_TIME_WAIT, 0); 39141da177e4SLinus Torvalds break; 39151da177e4SLinus Torvalds default: 39161da177e4SLinus Torvalds /* Only TCP_LISTEN and TCP_CLOSE are left, in these 39171da177e4SLinus Torvalds * cases we should never reach this piece of code. 39181da177e4SLinus Torvalds */ 3919058bd4d2SJoe Perches pr_err("%s: Impossible, sk->sk_state=%d\n", 39200dc47877SHarvey Harrison __func__, sk->sk_state); 39211da177e4SLinus Torvalds break; 39223ff50b79SStephen Hemminger } 39231da177e4SLinus Torvalds 39241da177e4SLinus Torvalds /* It _is_ possible, that we have something out-of-order _after_ FIN. 39251da177e4SLinus Torvalds * Probably, we should reset in this case. For now drop them. 39261da177e4SLinus Torvalds */ 39271da177e4SLinus Torvalds __skb_queue_purge(&tp->out_of_order_queue); 3928e60402d0SIlpo Järvinen if (tcp_is_sack(tp)) 39291da177e4SLinus Torvalds tcp_sack_reset(&tp->rx_opt); 39303ab224beSHideo Aoki sk_mem_reclaim(sk); 39311da177e4SLinus Torvalds 39321da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) { 39331da177e4SLinus Torvalds sk->sk_state_change(sk); 39341da177e4SLinus Torvalds 39351da177e4SLinus Torvalds /* Do not send POLL_HUP for half duplex close. */ 39361da177e4SLinus Torvalds if (sk->sk_shutdown == SHUTDOWN_MASK || 39371da177e4SLinus Torvalds sk->sk_state == TCP_CLOSE) 39388d8ad9d7SPavel Emelyanov sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP); 39391da177e4SLinus Torvalds else 39408d8ad9d7SPavel Emelyanov sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN); 39411da177e4SLinus Torvalds } 39421da177e4SLinus Torvalds } 39431da177e4SLinus Torvalds 3944a2a385d6SEric Dumazet static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq, 3945056834d9SIlpo Järvinen u32 end_seq) 39461da177e4SLinus Torvalds { 39471da177e4SLinus Torvalds if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) { 39481da177e4SLinus Torvalds if (before(seq, sp->start_seq)) 39491da177e4SLinus Torvalds sp->start_seq = seq; 39501da177e4SLinus Torvalds if (after(end_seq, sp->end_seq)) 39511da177e4SLinus Torvalds sp->end_seq = end_seq; 3952a2a385d6SEric Dumazet return true; 39531da177e4SLinus Torvalds } 3954a2a385d6SEric Dumazet return false; 39551da177e4SLinus Torvalds } 39561da177e4SLinus Torvalds 39571ed83465SPavel Emelyanov static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq) 39581da177e4SLinus Torvalds { 39591ed83465SPavel Emelyanov struct tcp_sock *tp = tcp_sk(sk); 39601ed83465SPavel Emelyanov 3961bb5b7c11SDavid S. Miller if (tcp_is_sack(tp) && sysctl_tcp_dsack) { 396240b215e5SPavel Emelyanov int mib_idx; 396340b215e5SPavel Emelyanov 39641da177e4SLinus Torvalds if (before(seq, tp->rcv_nxt)) 396540b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPDSACKOLDSENT; 39661da177e4SLinus Torvalds else 396740b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPDSACKOFOSENT; 396840b215e5SPavel Emelyanov 3969de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), mib_idx); 39701da177e4SLinus Torvalds 39711da177e4SLinus Torvalds tp->rx_opt.dsack = 1; 39721da177e4SLinus Torvalds tp->duplicate_sack[0].start_seq = seq; 39731da177e4SLinus Torvalds tp->duplicate_sack[0].end_seq = end_seq; 39741da177e4SLinus Torvalds } 39751da177e4SLinus Torvalds } 39761da177e4SLinus Torvalds 39771ed83465SPavel Emelyanov static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq) 39781da177e4SLinus Torvalds { 39791ed83465SPavel Emelyanov struct tcp_sock *tp = tcp_sk(sk); 39801ed83465SPavel Emelyanov 39811da177e4SLinus Torvalds if (!tp->rx_opt.dsack) 39821ed83465SPavel Emelyanov tcp_dsack_set(sk, seq, end_seq); 39831da177e4SLinus Torvalds else 39841da177e4SLinus Torvalds tcp_sack_extend(tp->duplicate_sack, seq, end_seq); 39851da177e4SLinus Torvalds } 39861da177e4SLinus Torvalds 3987cf533ea5SEric Dumazet static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb) 39881da177e4SLinus Torvalds { 39891da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 39901da177e4SLinus Torvalds 39911da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && 39921da177e4SLinus Torvalds before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { 3993de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOST); 3994463c84b9SArnaldo Carvalho de Melo tcp_enter_quickack_mode(sk); 39951da177e4SLinus Torvalds 3996bb5b7c11SDavid S. Miller if (tcp_is_sack(tp) && sysctl_tcp_dsack) { 39971da177e4SLinus Torvalds u32 end_seq = TCP_SKB_CB(skb)->end_seq; 39981da177e4SLinus Torvalds 39991da177e4SLinus Torvalds if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) 40001da177e4SLinus Torvalds end_seq = tp->rcv_nxt; 40011ed83465SPavel Emelyanov tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, end_seq); 40021da177e4SLinus Torvalds } 40031da177e4SLinus Torvalds } 40041da177e4SLinus Torvalds 40051da177e4SLinus Torvalds tcp_send_ack(sk); 40061da177e4SLinus Torvalds } 40071da177e4SLinus Torvalds 40081da177e4SLinus Torvalds /* These routines update the SACK block as out-of-order packets arrive or 40091da177e4SLinus Torvalds * in-order packets close up the sequence space. 40101da177e4SLinus Torvalds */ 40111da177e4SLinus Torvalds static void tcp_sack_maybe_coalesce(struct tcp_sock *tp) 40121da177e4SLinus Torvalds { 40131da177e4SLinus Torvalds int this_sack; 40141da177e4SLinus Torvalds struct tcp_sack_block *sp = &tp->selective_acks[0]; 40151da177e4SLinus Torvalds struct tcp_sack_block *swalk = sp + 1; 40161da177e4SLinus Torvalds 40171da177e4SLinus Torvalds /* See if the recent change to the first SACK eats into 40181da177e4SLinus Torvalds * or hits the sequence space of other SACK blocks, if so coalesce. 40191da177e4SLinus Torvalds */ 40201da177e4SLinus Torvalds for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;) { 40211da177e4SLinus Torvalds if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) { 40221da177e4SLinus Torvalds int i; 40231da177e4SLinus Torvalds 40241da177e4SLinus Torvalds /* Zap SWALK, by moving every further SACK up by one slot. 40251da177e4SLinus Torvalds * Decrease num_sacks. 40261da177e4SLinus Torvalds */ 40271da177e4SLinus Torvalds tp->rx_opt.num_sacks--; 40281da177e4SLinus Torvalds for (i = this_sack; i < tp->rx_opt.num_sacks; i++) 40291da177e4SLinus Torvalds sp[i] = sp[i + 1]; 40301da177e4SLinus Torvalds continue; 40311da177e4SLinus Torvalds } 40321da177e4SLinus Torvalds this_sack++, swalk++; 40331da177e4SLinus Torvalds } 40341da177e4SLinus Torvalds } 40351da177e4SLinus Torvalds 40361da177e4SLinus Torvalds static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq) 40371da177e4SLinus Torvalds { 40381da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 40391da177e4SLinus Torvalds struct tcp_sack_block *sp = &tp->selective_acks[0]; 40401da177e4SLinus Torvalds int cur_sacks = tp->rx_opt.num_sacks; 40411da177e4SLinus Torvalds int this_sack; 40421da177e4SLinus Torvalds 40431da177e4SLinus Torvalds if (!cur_sacks) 40441da177e4SLinus Torvalds goto new_sack; 40451da177e4SLinus Torvalds 40461da177e4SLinus Torvalds for (this_sack = 0; this_sack < cur_sacks; this_sack++, sp++) { 40471da177e4SLinus Torvalds if (tcp_sack_extend(sp, seq, end_seq)) { 40481da177e4SLinus Torvalds /* Rotate this_sack to the first one. */ 40491da177e4SLinus Torvalds for (; this_sack > 0; this_sack--, sp--) 4050a0bffffcSIlpo Järvinen swap(*sp, *(sp - 1)); 40511da177e4SLinus Torvalds if (cur_sacks > 1) 40521da177e4SLinus Torvalds tcp_sack_maybe_coalesce(tp); 40531da177e4SLinus Torvalds return; 40541da177e4SLinus Torvalds } 40551da177e4SLinus Torvalds } 40561da177e4SLinus Torvalds 40571da177e4SLinus Torvalds /* Could not find an adjacent existing SACK, build a new one, 40581da177e4SLinus Torvalds * put it at the front, and shift everyone else down. We 40591da177e4SLinus Torvalds * always know there is at least one SACK present already here. 40601da177e4SLinus Torvalds * 40611da177e4SLinus Torvalds * If the sack array is full, forget about the last one. 40621da177e4SLinus Torvalds */ 40634389ddedSAdam Langley if (this_sack >= TCP_NUM_SACKS) { 40641da177e4SLinus Torvalds this_sack--; 40651da177e4SLinus Torvalds tp->rx_opt.num_sacks--; 40661da177e4SLinus Torvalds sp--; 40671da177e4SLinus Torvalds } 40681da177e4SLinus Torvalds for (; this_sack > 0; this_sack--, sp--) 40691da177e4SLinus Torvalds *sp = *(sp - 1); 40701da177e4SLinus Torvalds 40711da177e4SLinus Torvalds new_sack: 40721da177e4SLinus Torvalds /* Build the new head SACK, and we're done. */ 40731da177e4SLinus Torvalds sp->start_seq = seq; 40741da177e4SLinus Torvalds sp->end_seq = end_seq; 40751da177e4SLinus Torvalds tp->rx_opt.num_sacks++; 40761da177e4SLinus Torvalds } 40771da177e4SLinus Torvalds 40781da177e4SLinus Torvalds /* RCV.NXT advances, some SACKs should be eaten. */ 40791da177e4SLinus Torvalds 40801da177e4SLinus Torvalds static void tcp_sack_remove(struct tcp_sock *tp) 40811da177e4SLinus Torvalds { 40821da177e4SLinus Torvalds struct tcp_sack_block *sp = &tp->selective_acks[0]; 40831da177e4SLinus Torvalds int num_sacks = tp->rx_opt.num_sacks; 40841da177e4SLinus Torvalds int this_sack; 40851da177e4SLinus Torvalds 40861da177e4SLinus Torvalds /* Empty ofo queue, hence, all the SACKs are eaten. Clear. */ 4087b03efcfbSDavid S. Miller if (skb_queue_empty(&tp->out_of_order_queue)) { 40881da177e4SLinus Torvalds tp->rx_opt.num_sacks = 0; 40891da177e4SLinus Torvalds return; 40901da177e4SLinus Torvalds } 40911da177e4SLinus Torvalds 40921da177e4SLinus Torvalds for (this_sack = 0; this_sack < num_sacks;) { 40931da177e4SLinus Torvalds /* Check if the start of the sack is covered by RCV.NXT. */ 40941da177e4SLinus Torvalds if (!before(tp->rcv_nxt, sp->start_seq)) { 40951da177e4SLinus Torvalds int i; 40961da177e4SLinus Torvalds 40971da177e4SLinus Torvalds /* RCV.NXT must cover all the block! */ 4098547b792cSIlpo Järvinen WARN_ON(before(tp->rcv_nxt, sp->end_seq)); 40991da177e4SLinus Torvalds 41001da177e4SLinus Torvalds /* Zap this SACK, by moving forward any other SACKS. */ 41011da177e4SLinus Torvalds for (i = this_sack+1; i < num_sacks; i++) 41021da177e4SLinus Torvalds tp->selective_acks[i-1] = tp->selective_acks[i]; 41031da177e4SLinus Torvalds num_sacks--; 41041da177e4SLinus Torvalds continue; 41051da177e4SLinus Torvalds } 41061da177e4SLinus Torvalds this_sack++; 41071da177e4SLinus Torvalds sp++; 41081da177e4SLinus Torvalds } 41091da177e4SLinus Torvalds tp->rx_opt.num_sacks = num_sacks; 41101da177e4SLinus Torvalds } 41111da177e4SLinus Torvalds 41121402d366SEric Dumazet /** 41131402d366SEric Dumazet * tcp_try_coalesce - try to merge skb to prior one 41141402d366SEric Dumazet * @sk: socket 41151402d366SEric Dumazet * @to: prior buffer 41161402d366SEric Dumazet * @from: buffer to add in queue 4117923dd347SEric Dumazet * @fragstolen: pointer to boolean 41181402d366SEric Dumazet * 41191402d366SEric Dumazet * Before queueing skb @from after @to, try to merge them 41201402d366SEric Dumazet * to reduce overall memory use and queue lengths, if cost is small. 41211402d366SEric Dumazet * Packets in ofo or receive queues can stay a long time. 41221402d366SEric Dumazet * Better try to coalesce them right now to avoid future collapses. 4123783c175fSEric Dumazet * Returns true if caller should free @from instead of queueing it 41241402d366SEric Dumazet */ 4125783c175fSEric Dumazet static bool tcp_try_coalesce(struct sock *sk, 41261402d366SEric Dumazet struct sk_buff *to, 4127329033f6SEric Dumazet struct sk_buff *from, 4128329033f6SEric Dumazet bool *fragstolen) 41291402d366SEric Dumazet { 4130bad43ca8SEric Dumazet int delta; 41311402d366SEric Dumazet 4132329033f6SEric Dumazet *fragstolen = false; 413334a802a5SAlexander Duyck 41341ca7ee30SEric Dumazet /* Its possible this segment overlaps with prior segment in queue */ 41351ca7ee30SEric Dumazet if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq) 41361ca7ee30SEric Dumazet return false; 41371ca7ee30SEric Dumazet 4138bad43ca8SEric Dumazet if (!skb_try_coalesce(to, from, fragstolen, &delta)) 4139783c175fSEric Dumazet return false; 414034a802a5SAlexander Duyck 41411402d366SEric Dumazet atomic_add(delta, &sk->sk_rmem_alloc); 41421402d366SEric Dumazet sk_mem_charge(sk, delta); 414334a802a5SAlexander Duyck NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE); 414434a802a5SAlexander Duyck TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq; 414534a802a5SAlexander Duyck TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq; 4146e93a0435SEric Dumazet TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags; 414734a802a5SAlexander Duyck return true; 41481402d366SEric Dumazet } 41491402d366SEric Dumazet 41501da177e4SLinus Torvalds /* This one checks to see if we can put data from the 41511da177e4SLinus Torvalds * out_of_order queue into the receive_queue. 41521da177e4SLinus Torvalds */ 41531da177e4SLinus Torvalds static void tcp_ofo_queue(struct sock *sk) 41541da177e4SLinus Torvalds { 41551da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 41561da177e4SLinus Torvalds __u32 dsack_high = tp->rcv_nxt; 4157bd1e75abSEric Dumazet struct sk_buff *skb, *tail; 4158bd1e75abSEric Dumazet bool fragstolen, eaten; 41591da177e4SLinus Torvalds 41601da177e4SLinus Torvalds while ((skb = skb_peek(&tp->out_of_order_queue)) != NULL) { 41611da177e4SLinus Torvalds if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) 41621da177e4SLinus Torvalds break; 41631da177e4SLinus Torvalds 41641da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->seq, dsack_high)) { 41651da177e4SLinus Torvalds __u32 dsack = dsack_high; 41661da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->end_seq, dsack_high)) 41671da177e4SLinus Torvalds dsack_high = TCP_SKB_CB(skb)->end_seq; 41681da177e4SLinus Torvalds tcp_dsack_extend(sk, TCP_SKB_CB(skb)->seq, dsack); 41691da177e4SLinus Torvalds } 41701da177e4SLinus Torvalds 4171bd1e75abSEric Dumazet __skb_unlink(skb, &tp->out_of_order_queue); 41721da177e4SLinus Torvalds if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) { 41731da177e4SLinus Torvalds SOCK_DEBUG(sk, "ofo packet was already received\n"); 41741da177e4SLinus Torvalds __kfree_skb(skb); 41751da177e4SLinus Torvalds continue; 41761da177e4SLinus Torvalds } 41771da177e4SLinus Torvalds SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n", 41781da177e4SLinus Torvalds tp->rcv_nxt, TCP_SKB_CB(skb)->seq, 41791da177e4SLinus Torvalds TCP_SKB_CB(skb)->end_seq); 41801da177e4SLinus Torvalds 4181bd1e75abSEric Dumazet tail = skb_peek_tail(&sk->sk_receive_queue); 4182bd1e75abSEric Dumazet eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen); 41831da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 4184bd1e75abSEric Dumazet if (!eaten) 4185bd1e75abSEric Dumazet __skb_queue_tail(&sk->sk_receive_queue, skb); 4186bd1e75abSEric Dumazet if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) 41871da177e4SLinus Torvalds tcp_fin(sk); 4188bd1e75abSEric Dumazet if (eaten) 4189bd1e75abSEric Dumazet kfree_skb_partial(skb, fragstolen); 41901da177e4SLinus Torvalds } 41911da177e4SLinus Torvalds } 41921da177e4SLinus Torvalds 41931da177e4SLinus Torvalds static bool tcp_prune_ofo_queue(struct sock *sk); 41941da177e4SLinus Torvalds static int tcp_prune_queue(struct sock *sk); 41951da177e4SLinus Torvalds 41961da177e4SLinus Torvalds static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb, 41971da177e4SLinus Torvalds unsigned int size) 41981da177e4SLinus Torvalds { 41991da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || 42001da177e4SLinus Torvalds !sk_rmem_schedule(sk, skb, size)) { 42011da177e4SLinus Torvalds 42021da177e4SLinus Torvalds if (tcp_prune_queue(sk) < 0) 42031da177e4SLinus Torvalds return -1; 42041ed83465SPavel Emelyanov 42051da177e4SLinus Torvalds if (!sk_rmem_schedule(sk, skb, size)) { 42061da177e4SLinus Torvalds if (!tcp_prune_ofo_queue(sk)) 42071da177e4SLinus Torvalds return -1; 42081da177e4SLinus Torvalds 42098728b834SDavid S. Miller if (!sk_rmem_schedule(sk, skb, size)) 42101da177e4SLinus Torvalds return -1; 42111da177e4SLinus Torvalds } 42121da177e4SLinus Torvalds } 42131da177e4SLinus Torvalds return 0; 42141da177e4SLinus Torvalds } 42151da177e4SLinus Torvalds 4216e86b2919SEric Dumazet static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb) 4217e86b2919SEric Dumazet { 4218e86b2919SEric Dumazet struct tcp_sock *tp = tcp_sk(sk); 4219e86b2919SEric Dumazet struct sk_buff *skb1; 4220e86b2919SEric Dumazet u32 seq, end_seq; 4221e86b2919SEric Dumazet 4222735d3831SFlorian Westphal tcp_ecn_check_ce(tp, skb); 4223e86b2919SEric Dumazet 4224c76562b6SMel Gorman if (unlikely(tcp_try_rmem_schedule(sk, skb, skb->truesize))) { 4225a6df1ae9SEric Dumazet NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFODROP); 4226e86b2919SEric Dumazet __kfree_skb(skb); 4227e86b2919SEric Dumazet return; 4228e86b2919SEric Dumazet } 4229e86b2919SEric Dumazet 4230e86b2919SEric Dumazet /* Disable header prediction. */ 4231e86b2919SEric Dumazet tp->pred_flags = 0; 4232e86b2919SEric Dumazet inet_csk_schedule_ack(sk); 4233e86b2919SEric Dumazet 4234a6df1ae9SEric Dumazet NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOQUEUE); 4235e86b2919SEric Dumazet SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n", 4236e86b2919SEric Dumazet tp->rcv_nxt, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq); 4237e86b2919SEric Dumazet 4238e86b2919SEric Dumazet skb1 = skb_peek_tail(&tp->out_of_order_queue); 4239e86b2919SEric Dumazet if (!skb1) { 4240e86b2919SEric Dumazet /* Initial out of order segment, build 1 SACK. */ 4241e86b2919SEric Dumazet if (tcp_is_sack(tp)) { 4242e86b2919SEric Dumazet tp->rx_opt.num_sacks = 1; 4243e86b2919SEric Dumazet tp->selective_acks[0].start_seq = TCP_SKB_CB(skb)->seq; 4244e86b2919SEric Dumazet tp->selective_acks[0].end_seq = 4245e86b2919SEric Dumazet TCP_SKB_CB(skb)->end_seq; 4246e86b2919SEric Dumazet } 4247e86b2919SEric Dumazet __skb_queue_head(&tp->out_of_order_queue, skb); 4248e86b2919SEric Dumazet goto end; 4249e86b2919SEric Dumazet } 4250e86b2919SEric Dumazet 4251e86b2919SEric Dumazet seq = TCP_SKB_CB(skb)->seq; 4252e86b2919SEric Dumazet end_seq = TCP_SKB_CB(skb)->end_seq; 4253e86b2919SEric Dumazet 4254e86b2919SEric Dumazet if (seq == TCP_SKB_CB(skb1)->end_seq) { 4255329033f6SEric Dumazet bool fragstolen; 4256329033f6SEric Dumazet 4257329033f6SEric Dumazet if (!tcp_try_coalesce(sk, skb1, skb, &fragstolen)) { 42581402d366SEric Dumazet __skb_queue_after(&tp->out_of_order_queue, skb1, skb); 42591402d366SEric Dumazet } else { 42604e4f1fc2SEric Dumazet tcp_grow_window(sk, skb); 4261923dd347SEric Dumazet kfree_skb_partial(skb, fragstolen); 4262c8628155SEric Dumazet skb = NULL; 4263c8628155SEric Dumazet } 4264e86b2919SEric Dumazet 4265e86b2919SEric Dumazet if (!tp->rx_opt.num_sacks || 4266e86b2919SEric Dumazet tp->selective_acks[0].end_seq != seq) 4267e86b2919SEric Dumazet goto add_sack; 4268e86b2919SEric Dumazet 4269e86b2919SEric Dumazet /* Common case: data arrive in order after hole. */ 4270e86b2919SEric Dumazet tp->selective_acks[0].end_seq = end_seq; 4271e86b2919SEric Dumazet goto end; 4272e86b2919SEric Dumazet } 4273e86b2919SEric Dumazet 4274e86b2919SEric Dumazet /* Find place to insert this segment. */ 4275e86b2919SEric Dumazet while (1) { 4276e86b2919SEric Dumazet if (!after(TCP_SKB_CB(skb1)->seq, seq)) 4277e86b2919SEric Dumazet break; 4278e86b2919SEric Dumazet if (skb_queue_is_first(&tp->out_of_order_queue, skb1)) { 4279e86b2919SEric Dumazet skb1 = NULL; 4280e86b2919SEric Dumazet break; 4281e86b2919SEric Dumazet } 4282e86b2919SEric Dumazet skb1 = skb_queue_prev(&tp->out_of_order_queue, skb1); 4283e86b2919SEric Dumazet } 4284e86b2919SEric Dumazet 4285e86b2919SEric Dumazet /* Do skb overlap to previous one? */ 4286e86b2919SEric Dumazet if (skb1 && before(seq, TCP_SKB_CB(skb1)->end_seq)) { 4287e86b2919SEric Dumazet if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) { 4288e86b2919SEric Dumazet /* All the bits are present. Drop. */ 4289a6df1ae9SEric Dumazet NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE); 4290e86b2919SEric Dumazet __kfree_skb(skb); 4291e86b2919SEric Dumazet skb = NULL; 4292e86b2919SEric Dumazet tcp_dsack_set(sk, seq, end_seq); 4293e86b2919SEric Dumazet goto add_sack; 4294e86b2919SEric Dumazet } 4295e86b2919SEric Dumazet if (after(seq, TCP_SKB_CB(skb1)->seq)) { 4296e86b2919SEric Dumazet /* Partial overlap. */ 4297e86b2919SEric Dumazet tcp_dsack_set(sk, seq, 4298e86b2919SEric Dumazet TCP_SKB_CB(skb1)->end_seq); 4299e86b2919SEric Dumazet } else { 4300e86b2919SEric Dumazet if (skb_queue_is_first(&tp->out_of_order_queue, 4301e86b2919SEric Dumazet skb1)) 4302e86b2919SEric Dumazet skb1 = NULL; 4303e86b2919SEric Dumazet else 4304e86b2919SEric Dumazet skb1 = skb_queue_prev( 4305e86b2919SEric Dumazet &tp->out_of_order_queue, 4306e86b2919SEric Dumazet skb1); 4307e86b2919SEric Dumazet } 4308e86b2919SEric Dumazet } 4309e86b2919SEric Dumazet if (!skb1) 4310e86b2919SEric Dumazet __skb_queue_head(&tp->out_of_order_queue, skb); 4311e86b2919SEric Dumazet else 4312e86b2919SEric Dumazet __skb_queue_after(&tp->out_of_order_queue, skb1, skb); 4313e86b2919SEric Dumazet 4314e86b2919SEric Dumazet /* And clean segments covered by new one as whole. */ 4315e86b2919SEric Dumazet while (!skb_queue_is_last(&tp->out_of_order_queue, skb)) { 4316e86b2919SEric Dumazet skb1 = skb_queue_next(&tp->out_of_order_queue, skb); 4317e86b2919SEric Dumazet 4318e86b2919SEric Dumazet if (!after(end_seq, TCP_SKB_CB(skb1)->seq)) 4319e86b2919SEric Dumazet break; 4320e86b2919SEric Dumazet if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) { 4321e86b2919SEric Dumazet tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq, 4322e86b2919SEric Dumazet end_seq); 4323e86b2919SEric Dumazet break; 4324e86b2919SEric Dumazet } 4325e86b2919SEric Dumazet __skb_unlink(skb1, &tp->out_of_order_queue); 4326e86b2919SEric Dumazet tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq, 4327e86b2919SEric Dumazet TCP_SKB_CB(skb1)->end_seq); 4328a6df1ae9SEric Dumazet NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE); 4329e86b2919SEric Dumazet __kfree_skb(skb1); 4330e86b2919SEric Dumazet } 4331e86b2919SEric Dumazet 4332e86b2919SEric Dumazet add_sack: 4333e86b2919SEric Dumazet if (tcp_is_sack(tp)) 4334e86b2919SEric Dumazet tcp_sack_new_ofo_skb(sk, seq, end_seq); 4335e86b2919SEric Dumazet end: 43364e4f1fc2SEric Dumazet if (skb) { 43374e4f1fc2SEric Dumazet tcp_grow_window(sk, skb); 4338e86b2919SEric Dumazet skb_set_owner_r(skb, sk); 4339e86b2919SEric Dumazet } 43404e4f1fc2SEric Dumazet } 4341e86b2919SEric Dumazet 4342292e8d8cSPavel Emelyanov static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int hdrlen, 4343b081f85cSEric Dumazet bool *fragstolen) 4344b081f85cSEric Dumazet { 4345b081f85cSEric Dumazet int eaten; 4346b081f85cSEric Dumazet struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue); 4347b081f85cSEric Dumazet 4348b081f85cSEric Dumazet __skb_pull(skb, hdrlen); 4349b081f85cSEric Dumazet eaten = (tail && 4350b081f85cSEric Dumazet tcp_try_coalesce(sk, tail, skb, fragstolen)) ? 1 : 0; 4351b081f85cSEric Dumazet tcp_sk(sk)->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 4352b081f85cSEric Dumazet if (!eaten) { 4353b081f85cSEric Dumazet __skb_queue_tail(&sk->sk_receive_queue, skb); 4354b081f85cSEric Dumazet skb_set_owner_r(skb, sk); 4355b081f85cSEric Dumazet } 4356b081f85cSEric Dumazet return eaten; 4357b081f85cSEric Dumazet } 4358e86b2919SEric Dumazet 4359292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size) 4360292e8d8cSPavel Emelyanov { 4361cb93471aSEric Dumazet struct sk_buff *skb; 4362292e8d8cSPavel Emelyanov bool fragstolen; 4363292e8d8cSPavel Emelyanov 4364c454e611SPavel Emelyanov if (size == 0) 4365c454e611SPavel Emelyanov return 0; 4366c454e611SPavel Emelyanov 4367cb93471aSEric Dumazet skb = alloc_skb(size, sk->sk_allocation); 4368292e8d8cSPavel Emelyanov if (!skb) 4369292e8d8cSPavel Emelyanov goto err; 4370292e8d8cSPavel Emelyanov 4371cb93471aSEric Dumazet if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) 4372c76562b6SMel Gorman goto err_free; 4373c76562b6SMel Gorman 43746ce8e9ceSAl Viro if (memcpy_from_msg(skb_put(skb, size), msg, size)) 4375292e8d8cSPavel Emelyanov goto err_free; 4376292e8d8cSPavel Emelyanov 4377292e8d8cSPavel Emelyanov TCP_SKB_CB(skb)->seq = tcp_sk(sk)->rcv_nxt; 4378292e8d8cSPavel Emelyanov TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size; 4379292e8d8cSPavel Emelyanov TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1; 4380292e8d8cSPavel Emelyanov 4381cb93471aSEric Dumazet if (tcp_queue_rcv(sk, skb, 0, &fragstolen)) { 4382292e8d8cSPavel Emelyanov WARN_ON_ONCE(fragstolen); /* should not happen */ 4383292e8d8cSPavel Emelyanov __kfree_skb(skb); 4384292e8d8cSPavel Emelyanov } 4385292e8d8cSPavel Emelyanov return size; 4386292e8d8cSPavel Emelyanov 4387292e8d8cSPavel Emelyanov err_free: 4388292e8d8cSPavel Emelyanov kfree_skb(skb); 4389292e8d8cSPavel Emelyanov err: 4390292e8d8cSPavel Emelyanov return -ENOMEM; 4391292e8d8cSPavel Emelyanov } 4392292e8d8cSPavel Emelyanov 43931da177e4SLinus Torvalds static void tcp_data_queue(struct sock *sk, struct sk_buff *skb) 43941da177e4SLinus Torvalds { 43951da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 43961da177e4SLinus Torvalds int eaten = -1; 4397329033f6SEric Dumazet bool fragstolen = false; 43981da177e4SLinus Torvalds 43991da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) 44001da177e4SLinus Torvalds goto drop; 44011da177e4SLinus Torvalds 4402f84af32cSEric Dumazet skb_dst_drop(skb); 4403155c6e1aSPeter Pan(潘卫平) __skb_pull(skb, tcp_hdr(skb)->doff * 4); 44041da177e4SLinus Torvalds 4405735d3831SFlorian Westphal tcp_ecn_accept_cwr(tp, skb); 44061da177e4SLinus Torvalds 44071da177e4SLinus Torvalds tp->rx_opt.dsack = 0; 44081da177e4SLinus Torvalds 44091da177e4SLinus Torvalds /* Queue data for delivery to the user. 44101da177e4SLinus Torvalds * Packets in sequence go to the receive queue. 44111da177e4SLinus Torvalds * Out of sequence packets to the out_of_order_queue. 44121da177e4SLinus Torvalds */ 44131da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) { 44141da177e4SLinus Torvalds if (tcp_receive_window(tp) == 0) 44151da177e4SLinus Torvalds goto out_of_window; 44161da177e4SLinus Torvalds 44171da177e4SLinus Torvalds /* Ok. In sequence. In window. */ 44181da177e4SLinus Torvalds if (tp->ucopy.task == current && 44191da177e4SLinus Torvalds tp->copied_seq == tp->rcv_nxt && tp->ucopy.len && 44201da177e4SLinus Torvalds sock_owned_by_user(sk) && !tp->urg_data) { 44211da177e4SLinus Torvalds int chunk = min_t(unsigned int, skb->len, 44221da177e4SLinus Torvalds tp->ucopy.len); 44231da177e4SLinus Torvalds 44241da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 44251da177e4SLinus Torvalds 44261da177e4SLinus Torvalds local_bh_enable(); 4427f4362a2cSAl Viro if (!skb_copy_datagram_msg(skb, 0, tp->ucopy.msg, chunk)) { 44281da177e4SLinus Torvalds tp->ucopy.len -= chunk; 44291da177e4SLinus Torvalds tp->copied_seq += chunk; 443044f5324bSJerry Chu eaten = (chunk == skb->len); 44311da177e4SLinus Torvalds tcp_rcv_space_adjust(sk); 44321da177e4SLinus Torvalds } 44331da177e4SLinus Torvalds local_bh_disable(); 44341da177e4SLinus Torvalds } 44351da177e4SLinus Torvalds 44361da177e4SLinus Torvalds if (eaten <= 0) { 44371da177e4SLinus Torvalds queue_and_out: 44381da177e4SLinus Torvalds if (eaten < 0 && 4439c76562b6SMel Gorman tcp_try_rmem_schedule(sk, skb, skb->truesize)) 44401da177e4SLinus Torvalds goto drop; 4441b000cd37SVitaliy Gusev 4442b081f85cSEric Dumazet eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen); 44431402d366SEric Dumazet } 44441da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 44451da177e4SLinus Torvalds if (skb->len) 44469e412ba7SIlpo Järvinen tcp_event_data_recv(sk, skb); 4447155c6e1aSPeter Pan(潘卫平) if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) 444820c4cb79SEric Dumazet tcp_fin(sk); 44491da177e4SLinus Torvalds 4450b03efcfbSDavid S. Miller if (!skb_queue_empty(&tp->out_of_order_queue)) { 44511da177e4SLinus Torvalds tcp_ofo_queue(sk); 44521da177e4SLinus Torvalds 44531da177e4SLinus Torvalds /* RFC2581. 4.2. SHOULD send immediate ACK, when 44541da177e4SLinus Torvalds * gap in queue is filled. 44551da177e4SLinus Torvalds */ 4456b03efcfbSDavid S. Miller if (skb_queue_empty(&tp->out_of_order_queue)) 4457463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_ack.pingpong = 0; 44581da177e4SLinus Torvalds } 44591da177e4SLinus Torvalds 44601da177e4SLinus Torvalds if (tp->rx_opt.num_sacks) 44611da177e4SLinus Torvalds tcp_sack_remove(tp); 44621da177e4SLinus Torvalds 44639e412ba7SIlpo Järvinen tcp_fast_path_check(sk); 44641da177e4SLinus Torvalds 4465923dd347SEric Dumazet if (eaten > 0) 4466923dd347SEric Dumazet kfree_skb_partial(skb, fragstolen); 44671d57f195SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) 4468676d2369SDavid S. Miller sk->sk_data_ready(sk); 44691da177e4SLinus Torvalds return; 44701da177e4SLinus Torvalds } 44711da177e4SLinus Torvalds 44721da177e4SLinus Torvalds if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) { 44731da177e4SLinus Torvalds /* A retransmit, 2nd most common case. Force an immediate ack. */ 4474de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOST); 44751ed83465SPavel Emelyanov tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq); 44761da177e4SLinus Torvalds 44771da177e4SLinus Torvalds out_of_window: 4478463c84b9SArnaldo Carvalho de Melo tcp_enter_quickack_mode(sk); 4479463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 44801da177e4SLinus Torvalds drop: 44811da177e4SLinus Torvalds __kfree_skb(skb); 44821da177e4SLinus Torvalds return; 44831da177e4SLinus Torvalds } 44841da177e4SLinus Torvalds 44851da177e4SLinus Torvalds /* Out of window. F.e. zero window probe. */ 44861da177e4SLinus Torvalds if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp))) 44871da177e4SLinus Torvalds goto out_of_window; 44881da177e4SLinus Torvalds 4489463c84b9SArnaldo Carvalho de Melo tcp_enter_quickack_mode(sk); 44901da177e4SLinus Torvalds 44911da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { 44921da177e4SLinus Torvalds /* Partial packet, seq < rcv_next < end_seq */ 44931da177e4SLinus Torvalds SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n", 44941da177e4SLinus Torvalds tp->rcv_nxt, TCP_SKB_CB(skb)->seq, 44951da177e4SLinus Torvalds TCP_SKB_CB(skb)->end_seq); 44961da177e4SLinus Torvalds 44971ed83465SPavel Emelyanov tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt); 44981da177e4SLinus Torvalds 44991da177e4SLinus Torvalds /* If window is closed, drop tail of packet. But after 45001da177e4SLinus Torvalds * remembering D-SACK for its head made in previous line. 45011da177e4SLinus Torvalds */ 45021da177e4SLinus Torvalds if (!tcp_receive_window(tp)) 45031da177e4SLinus Torvalds goto out_of_window; 45041da177e4SLinus Torvalds goto queue_and_out; 45051da177e4SLinus Torvalds } 45061da177e4SLinus Torvalds 4507e86b2919SEric Dumazet tcp_data_queue_ofo(sk, skb); 45081da177e4SLinus Torvalds } 45091da177e4SLinus Torvalds 45102cf46637SIlpo Järvinen static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb, 45112cf46637SIlpo Järvinen struct sk_buff_head *list) 45122cf46637SIlpo Järvinen { 451391521944SDavid S. Miller struct sk_buff *next = NULL; 451491521944SDavid S. Miller 451591521944SDavid S. Miller if (!skb_queue_is_last(list, skb)) 451691521944SDavid S. Miller next = skb_queue_next(list, skb); 45172cf46637SIlpo Järvinen 45182cf46637SIlpo Järvinen __skb_unlink(skb, list); 45192cf46637SIlpo Järvinen __kfree_skb(skb); 45202cf46637SIlpo Järvinen NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRCVCOLLAPSED); 45212cf46637SIlpo Järvinen 45222cf46637SIlpo Järvinen return next; 45232cf46637SIlpo Järvinen } 45242cf46637SIlpo Järvinen 45251da177e4SLinus Torvalds /* Collapse contiguous sequence of skbs head..tail with 45261da177e4SLinus Torvalds * sequence numbers start..end. 452791521944SDavid S. Miller * 452891521944SDavid S. Miller * If tail is NULL, this means until the end of the list. 452991521944SDavid S. Miller * 45301da177e4SLinus Torvalds * Segments with FIN/SYN are not collapsed (only because this 45311da177e4SLinus Torvalds * simplifies code) 45321da177e4SLinus Torvalds */ 45331da177e4SLinus Torvalds static void 45348728b834SDavid S. Miller tcp_collapse(struct sock *sk, struct sk_buff_head *list, 45358728b834SDavid S. Miller struct sk_buff *head, struct sk_buff *tail, 45368728b834SDavid S. Miller u32 start, u32 end) 45371da177e4SLinus Torvalds { 453891521944SDavid S. Miller struct sk_buff *skb, *n; 453991521944SDavid S. Miller bool end_of_skbs; 45401da177e4SLinus Torvalds 4541caa20d9aSStephen Hemminger /* First, check that queue is collapsible and find 45421da177e4SLinus Torvalds * the point where collapsing can be useful. */ 454391521944SDavid S. Miller skb = head; 454491521944SDavid S. Miller restart: 454591521944SDavid S. Miller end_of_skbs = true; 454691521944SDavid S. Miller skb_queue_walk_from_safe(list, skb, n) { 454791521944SDavid S. Miller if (skb == tail) 454891521944SDavid S. Miller break; 45491da177e4SLinus Torvalds /* No new bits? It is possible on ofo queue. */ 45501da177e4SLinus Torvalds if (!before(start, TCP_SKB_CB(skb)->end_seq)) { 45512cf46637SIlpo Järvinen skb = tcp_collapse_one(sk, skb, list); 455291521944SDavid S. Miller if (!skb) 455391521944SDavid S. Miller break; 455491521944SDavid S. Miller goto restart; 45551da177e4SLinus Torvalds } 45561da177e4SLinus Torvalds 45571da177e4SLinus Torvalds /* The first skb to collapse is: 45581da177e4SLinus Torvalds * - not SYN/FIN and 45591da177e4SLinus Torvalds * - bloated or contains data before "start" or 45601da177e4SLinus Torvalds * overlaps to the next one. 45611da177e4SLinus Torvalds */ 4562e11ecddfSEric Dumazet if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) && 45631da177e4SLinus Torvalds (tcp_win_from_space(skb->truesize) > skb->len || 456491521944SDavid S. Miller before(TCP_SKB_CB(skb)->seq, start))) { 456591521944SDavid S. Miller end_of_skbs = false; 45661da177e4SLinus Torvalds break; 456791521944SDavid S. Miller } 456891521944SDavid S. Miller 456991521944SDavid S. Miller if (!skb_queue_is_last(list, skb)) { 457091521944SDavid S. Miller struct sk_buff *next = skb_queue_next(list, skb); 457191521944SDavid S. Miller if (next != tail && 457291521944SDavid S. Miller TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(next)->seq) { 457391521944SDavid S. Miller end_of_skbs = false; 457491521944SDavid S. Miller break; 457591521944SDavid S. Miller } 457691521944SDavid S. Miller } 45771da177e4SLinus Torvalds 45781da177e4SLinus Torvalds /* Decided to skip this, advance start seq. */ 45791da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->end_seq; 45801da177e4SLinus Torvalds } 4581e11ecddfSEric Dumazet if (end_of_skbs || 4582e11ecddfSEric Dumazet (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN))) 45831da177e4SLinus Torvalds return; 45841da177e4SLinus Torvalds 45851da177e4SLinus Torvalds while (before(start, end)) { 4586b3d6cb92SEric Dumazet int copy = min_t(int, SKB_MAX_ORDER(0, 0), end - start); 45871da177e4SLinus Torvalds struct sk_buff *nskb; 45881da177e4SLinus Torvalds 4589b3d6cb92SEric Dumazet nskb = alloc_skb(copy, GFP_ATOMIC); 45901da177e4SLinus Torvalds if (!nskb) 45911da177e4SLinus Torvalds return; 4592c51957daSArnaldo Carvalho de Melo 45931da177e4SLinus Torvalds memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); 45941da177e4SLinus Torvalds TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start; 459543f59c89SDavid S. Miller __skb_queue_before(list, skb, nskb); 45963ab224beSHideo Aoki skb_set_owner_r(nskb, sk); 45971da177e4SLinus Torvalds 45981da177e4SLinus Torvalds /* Copy data, releasing collapsed skbs. */ 45991da177e4SLinus Torvalds while (copy > 0) { 46001da177e4SLinus Torvalds int offset = start - TCP_SKB_CB(skb)->seq; 46011da177e4SLinus Torvalds int size = TCP_SKB_CB(skb)->end_seq - start; 46021da177e4SLinus Torvalds 460309a62660SKris Katterjohn BUG_ON(offset < 0); 46041da177e4SLinus Torvalds if (size > 0) { 46051da177e4SLinus Torvalds size = min(copy, size); 46061da177e4SLinus Torvalds if (skb_copy_bits(skb, offset, skb_put(nskb, size), size)) 46071da177e4SLinus Torvalds BUG(); 46081da177e4SLinus Torvalds TCP_SKB_CB(nskb)->end_seq += size; 46091da177e4SLinus Torvalds copy -= size; 46101da177e4SLinus Torvalds start += size; 46111da177e4SLinus Torvalds } 46121da177e4SLinus Torvalds if (!before(start, TCP_SKB_CB(skb)->end_seq)) { 46132cf46637SIlpo Järvinen skb = tcp_collapse_one(sk, skb, list); 461491521944SDavid S. Miller if (!skb || 461591521944SDavid S. Miller skb == tail || 4616e11ecddfSEric Dumazet (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN))) 46171da177e4SLinus Torvalds return; 46181da177e4SLinus Torvalds } 46191da177e4SLinus Torvalds } 46201da177e4SLinus Torvalds } 46211da177e4SLinus Torvalds } 46221da177e4SLinus Torvalds 46231da177e4SLinus Torvalds /* Collapse ofo queue. Algorithm: select contiguous sequence of skbs 46241da177e4SLinus Torvalds * and tcp_collapse() them until all the queue is collapsed. 46251da177e4SLinus Torvalds */ 46261da177e4SLinus Torvalds static void tcp_collapse_ofo_queue(struct sock *sk) 46271da177e4SLinus Torvalds { 46281da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 46291da177e4SLinus Torvalds struct sk_buff *skb = skb_peek(&tp->out_of_order_queue); 46301da177e4SLinus Torvalds struct sk_buff *head; 46311da177e4SLinus Torvalds u32 start, end; 46321da177e4SLinus Torvalds 46331da177e4SLinus Torvalds if (skb == NULL) 46341da177e4SLinus Torvalds return; 46351da177e4SLinus Torvalds 46361da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->seq; 46371da177e4SLinus Torvalds end = TCP_SKB_CB(skb)->end_seq; 46381da177e4SLinus Torvalds head = skb; 46391da177e4SLinus Torvalds 46401da177e4SLinus Torvalds for (;;) { 464191521944SDavid S. Miller struct sk_buff *next = NULL; 464291521944SDavid S. Miller 464391521944SDavid S. Miller if (!skb_queue_is_last(&tp->out_of_order_queue, skb)) 464491521944SDavid S. Miller next = skb_queue_next(&tp->out_of_order_queue, skb); 464591521944SDavid S. Miller skb = next; 46461da177e4SLinus Torvalds 46471da177e4SLinus Torvalds /* Segment is terminated when we see gap or when 46481da177e4SLinus Torvalds * we are at the end of all the queue. */ 464991521944SDavid S. Miller if (!skb || 46501da177e4SLinus Torvalds after(TCP_SKB_CB(skb)->seq, end) || 46511da177e4SLinus Torvalds before(TCP_SKB_CB(skb)->end_seq, start)) { 46528728b834SDavid S. Miller tcp_collapse(sk, &tp->out_of_order_queue, 46538728b834SDavid S. Miller head, skb, start, end); 46541da177e4SLinus Torvalds head = skb; 465591521944SDavid S. Miller if (!skb) 46561da177e4SLinus Torvalds break; 46571da177e4SLinus Torvalds /* Start new segment */ 46581da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->seq; 46591da177e4SLinus Torvalds end = TCP_SKB_CB(skb)->end_seq; 46601da177e4SLinus Torvalds } else { 46611da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->seq, start)) 46621da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->seq; 46631da177e4SLinus Torvalds if (after(TCP_SKB_CB(skb)->end_seq, end)) 46641da177e4SLinus Torvalds end = TCP_SKB_CB(skb)->end_seq; 46651da177e4SLinus Torvalds } 46661da177e4SLinus Torvalds } 46671da177e4SLinus Torvalds } 46681da177e4SLinus Torvalds 4669b000cd37SVitaliy Gusev /* 4670b000cd37SVitaliy Gusev * Purge the out-of-order queue. 467156f367bbSVitaliy Gusev * Return true if queue was pruned. 4672b000cd37SVitaliy Gusev */ 4673a2a385d6SEric Dumazet static bool tcp_prune_ofo_queue(struct sock *sk) 4674b000cd37SVitaliy Gusev { 4675b000cd37SVitaliy Gusev struct tcp_sock *tp = tcp_sk(sk); 4676a2a385d6SEric Dumazet bool res = false; 4677b000cd37SVitaliy Gusev 4678b000cd37SVitaliy Gusev if (!skb_queue_empty(&tp->out_of_order_queue)) { 4679de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_OFOPRUNED); 4680b000cd37SVitaliy Gusev __skb_queue_purge(&tp->out_of_order_queue); 4681b000cd37SVitaliy Gusev 4682b000cd37SVitaliy Gusev /* Reset SACK state. A conforming SACK implementation will 4683b000cd37SVitaliy Gusev * do the same at a timeout based retransmit. When a connection 4684b000cd37SVitaliy Gusev * is in a sad state like this, we care only about integrity 4685b000cd37SVitaliy Gusev * of the connection not performance. 4686b000cd37SVitaliy Gusev */ 4687b000cd37SVitaliy Gusev if (tp->rx_opt.sack_ok) 4688b000cd37SVitaliy Gusev tcp_sack_reset(&tp->rx_opt); 4689b000cd37SVitaliy Gusev sk_mem_reclaim(sk); 4690a2a385d6SEric Dumazet res = true; 4691b000cd37SVitaliy Gusev } 469256f367bbSVitaliy Gusev return res; 4693b000cd37SVitaliy Gusev } 4694b000cd37SVitaliy Gusev 46951da177e4SLinus Torvalds /* Reduce allocated memory if we can, trying to get 46961da177e4SLinus Torvalds * the socket within its memory limits again. 46971da177e4SLinus Torvalds * 46981da177e4SLinus Torvalds * Return less than zero if we should start dropping frames 46991da177e4SLinus Torvalds * until the socket owning process reads some of the data 47001da177e4SLinus Torvalds * to stabilize the situation. 47011da177e4SLinus Torvalds */ 47021da177e4SLinus Torvalds static int tcp_prune_queue(struct sock *sk) 47031da177e4SLinus Torvalds { 47041da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 47051da177e4SLinus Torvalds 47061da177e4SLinus Torvalds SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq); 47071da177e4SLinus Torvalds 4708de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PRUNECALLED); 47091da177e4SLinus Torvalds 47101da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) 47119e412ba7SIlpo Järvinen tcp_clamp_window(sk); 4712180d8cd9SGlauber Costa else if (sk_under_memory_pressure(sk)) 47131da177e4SLinus Torvalds tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss); 47141da177e4SLinus Torvalds 47151da177e4SLinus Torvalds tcp_collapse_ofo_queue(sk); 471691521944SDavid S. Miller if (!skb_queue_empty(&sk->sk_receive_queue)) 47178728b834SDavid S. Miller tcp_collapse(sk, &sk->sk_receive_queue, 471891521944SDavid S. Miller skb_peek(&sk->sk_receive_queue), 471991521944SDavid S. Miller NULL, 47201da177e4SLinus Torvalds tp->copied_seq, tp->rcv_nxt); 47213ab224beSHideo Aoki sk_mem_reclaim(sk); 47221da177e4SLinus Torvalds 47231da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) 47241da177e4SLinus Torvalds return 0; 47251da177e4SLinus Torvalds 47261da177e4SLinus Torvalds /* Collapsing did not help, destructive actions follow. 47271da177e4SLinus Torvalds * This must not ever occur. */ 47281da177e4SLinus Torvalds 4729b000cd37SVitaliy Gusev tcp_prune_ofo_queue(sk); 47301da177e4SLinus Torvalds 47311da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) 47321da177e4SLinus Torvalds return 0; 47331da177e4SLinus Torvalds 47341da177e4SLinus Torvalds /* If we are really being abused, tell the caller to silently 47351da177e4SLinus Torvalds * drop receive data on the floor. It will get retransmitted 47361da177e4SLinus Torvalds * and hopefully then we'll have sufficient space. 47371da177e4SLinus Torvalds */ 4738de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_RCVPRUNED); 47391da177e4SLinus Torvalds 47401da177e4SLinus Torvalds /* Massive buffer overcommit. */ 47411da177e4SLinus Torvalds tp->pred_flags = 0; 47421da177e4SLinus Torvalds return -1; 47431da177e4SLinus Torvalds } 47441da177e4SLinus Torvalds 4745a2a385d6SEric Dumazet static bool tcp_should_expand_sndbuf(const struct sock *sk) 47460d9901dfSDavid S. Miller { 4747cf533ea5SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk); 47489e412ba7SIlpo Järvinen 47490d9901dfSDavid S. Miller /* If the user specified a specific send buffer setting, do 47500d9901dfSDavid S. Miller * not modify it. 47510d9901dfSDavid S. Miller */ 47520d9901dfSDavid S. Miller if (sk->sk_userlocks & SOCK_SNDBUF_LOCK) 4753a2a385d6SEric Dumazet return false; 47540d9901dfSDavid S. Miller 47550d9901dfSDavid S. Miller /* If we are under global TCP memory pressure, do not expand. */ 4756180d8cd9SGlauber Costa if (sk_under_memory_pressure(sk)) 4757a2a385d6SEric Dumazet return false; 47580d9901dfSDavid S. Miller 47590d9901dfSDavid S. Miller /* If we are under soft global TCP memory pressure, do not expand. */ 4760180d8cd9SGlauber Costa if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0)) 4761a2a385d6SEric Dumazet return false; 47620d9901dfSDavid S. Miller 47630d9901dfSDavid S. Miller /* If we filled the congestion window, do not expand. */ 47640d9901dfSDavid S. Miller if (tp->packets_out >= tp->snd_cwnd) 4765a2a385d6SEric Dumazet return false; 47660d9901dfSDavid S. Miller 4767a2a385d6SEric Dumazet return true; 47680d9901dfSDavid S. Miller } 47691da177e4SLinus Torvalds 47701da177e4SLinus Torvalds /* When incoming ACK allowed to free some skb from write_queue, 47711da177e4SLinus Torvalds * we remember this event in flag SOCK_QUEUE_SHRUNK and wake up socket 47721da177e4SLinus Torvalds * on the exit from tcp input handler. 47731da177e4SLinus Torvalds * 47741da177e4SLinus Torvalds * PROBLEM: sndbuf expansion does not work well with largesend. 47751da177e4SLinus Torvalds */ 47761da177e4SLinus Torvalds static void tcp_new_space(struct sock *sk) 47771da177e4SLinus Torvalds { 47781da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 47791da177e4SLinus Torvalds 47809e412ba7SIlpo Järvinen if (tcp_should_expand_sndbuf(sk)) { 47816ae70532SEric Dumazet tcp_sndbuf_expand(sk); 47821da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 47831da177e4SLinus Torvalds } 47841da177e4SLinus Torvalds 47851da177e4SLinus Torvalds sk->sk_write_space(sk); 47861da177e4SLinus Torvalds } 47871da177e4SLinus Torvalds 478840efc6faSStephen Hemminger static void tcp_check_space(struct sock *sk) 47891da177e4SLinus Torvalds { 47901da177e4SLinus Torvalds if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) { 47911da177e4SLinus Torvalds sock_reset_flag(sk, SOCK_QUEUE_SHRUNK); 47921da177e4SLinus Torvalds if (sk->sk_socket && 47931da177e4SLinus Torvalds test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) 47941da177e4SLinus Torvalds tcp_new_space(sk); 47951da177e4SLinus Torvalds } 47961da177e4SLinus Torvalds } 47971da177e4SLinus Torvalds 47989e412ba7SIlpo Järvinen static inline void tcp_data_snd_check(struct sock *sk) 47991da177e4SLinus Torvalds { 48009e412ba7SIlpo Järvinen tcp_push_pending_frames(sk); 48011da177e4SLinus Torvalds tcp_check_space(sk); 48021da177e4SLinus Torvalds } 48031da177e4SLinus Torvalds 48041da177e4SLinus Torvalds /* 48051da177e4SLinus Torvalds * Check if sending an ack is needed. 48061da177e4SLinus Torvalds */ 48071da177e4SLinus Torvalds static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible) 48081da177e4SLinus Torvalds { 48091da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 48101da177e4SLinus Torvalds 48111da177e4SLinus Torvalds /* More than one full frame received... */ 48129d4fb27dSJoe Perches if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss && 48131da177e4SLinus Torvalds /* ... and right edge of window advances far enough. 48141da177e4SLinus Torvalds * (tcp_recvmsg() will send ACK otherwise). Or... 48151da177e4SLinus Torvalds */ 48169d4fb27dSJoe Perches __tcp_select_window(sk) >= tp->rcv_wnd) || 48171da177e4SLinus Torvalds /* We ACK each frame or... */ 4818463c84b9SArnaldo Carvalho de Melo tcp_in_quickack_mode(sk) || 48191da177e4SLinus Torvalds /* We have out of order data. */ 4820056834d9SIlpo Järvinen (ofo_possible && skb_peek(&tp->out_of_order_queue))) { 48211da177e4SLinus Torvalds /* Then ack it now */ 48221da177e4SLinus Torvalds tcp_send_ack(sk); 48231da177e4SLinus Torvalds } else { 48241da177e4SLinus Torvalds /* Else, send delayed ack. */ 48251da177e4SLinus Torvalds tcp_send_delayed_ack(sk); 48261da177e4SLinus Torvalds } 48271da177e4SLinus Torvalds } 48281da177e4SLinus Torvalds 482940efc6faSStephen Hemminger static inline void tcp_ack_snd_check(struct sock *sk) 48301da177e4SLinus Torvalds { 4831463c84b9SArnaldo Carvalho de Melo if (!inet_csk_ack_scheduled(sk)) { 48321da177e4SLinus Torvalds /* We sent a data segment already. */ 48331da177e4SLinus Torvalds return; 48341da177e4SLinus Torvalds } 48351da177e4SLinus Torvalds __tcp_ack_snd_check(sk, 1); 48361da177e4SLinus Torvalds } 48371da177e4SLinus Torvalds 48381da177e4SLinus Torvalds /* 48391da177e4SLinus Torvalds * This routine is only called when we have urgent data 4840caa20d9aSStephen Hemminger * signaled. Its the 'slow' part of tcp_urg. It could be 48411da177e4SLinus Torvalds * moved inline now as tcp_urg is only called from one 48421da177e4SLinus Torvalds * place. We handle URGent data wrong. We have to - as 48431da177e4SLinus Torvalds * BSD still doesn't use the correction from RFC961. 48441da177e4SLinus Torvalds * For 1003.1g we should support a new option TCP_STDURG to permit 48451da177e4SLinus Torvalds * either form (or just set the sysctl tcp_stdurg). 48461da177e4SLinus Torvalds */ 48471da177e4SLinus Torvalds 4848cf533ea5SEric Dumazet static void tcp_check_urg(struct sock *sk, const struct tcphdr *th) 48491da177e4SLinus Torvalds { 48501da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 48511da177e4SLinus Torvalds u32 ptr = ntohs(th->urg_ptr); 48521da177e4SLinus Torvalds 48531da177e4SLinus Torvalds if (ptr && !sysctl_tcp_stdurg) 48541da177e4SLinus Torvalds ptr--; 48551da177e4SLinus Torvalds ptr += ntohl(th->seq); 48561da177e4SLinus Torvalds 48571da177e4SLinus Torvalds /* Ignore urgent data that we've already seen and read. */ 48581da177e4SLinus Torvalds if (after(tp->copied_seq, ptr)) 48591da177e4SLinus Torvalds return; 48601da177e4SLinus Torvalds 48611da177e4SLinus Torvalds /* Do not replay urg ptr. 48621da177e4SLinus Torvalds * 48631da177e4SLinus Torvalds * NOTE: interesting situation not covered by specs. 48641da177e4SLinus Torvalds * Misbehaving sender may send urg ptr, pointing to segment, 48651da177e4SLinus Torvalds * which we already have in ofo queue. We are not able to fetch 48661da177e4SLinus Torvalds * such data and will stay in TCP_URG_NOTYET until will be eaten 48671da177e4SLinus Torvalds * by recvmsg(). Seems, we are not obliged to handle such wicked 48681da177e4SLinus Torvalds * situations. But it is worth to think about possibility of some 48691da177e4SLinus Torvalds * DoSes using some hypothetical application level deadlock. 48701da177e4SLinus Torvalds */ 48711da177e4SLinus Torvalds if (before(ptr, tp->rcv_nxt)) 48721da177e4SLinus Torvalds return; 48731da177e4SLinus Torvalds 48741da177e4SLinus Torvalds /* Do we already have a newer (or duplicate) urgent pointer? */ 48751da177e4SLinus Torvalds if (tp->urg_data && !after(ptr, tp->urg_seq)) 48761da177e4SLinus Torvalds return; 48771da177e4SLinus Torvalds 48781da177e4SLinus Torvalds /* Tell the world about our new urgent pointer. */ 48791da177e4SLinus Torvalds sk_send_sigurg(sk); 48801da177e4SLinus Torvalds 48811da177e4SLinus Torvalds /* We may be adding urgent data when the last byte read was 48821da177e4SLinus Torvalds * urgent. To do this requires some care. We cannot just ignore 48831da177e4SLinus Torvalds * tp->copied_seq since we would read the last urgent byte again 48841da177e4SLinus Torvalds * as data, nor can we alter copied_seq until this data arrives 4885caa20d9aSStephen Hemminger * or we break the semantics of SIOCATMARK (and thus sockatmark()) 48861da177e4SLinus Torvalds * 48871da177e4SLinus Torvalds * NOTE. Double Dutch. Rendering to plain English: author of comment 48881da177e4SLinus Torvalds * above did something sort of send("A", MSG_OOB); send("B", MSG_OOB); 48891da177e4SLinus Torvalds * and expect that both A and B disappear from stream. This is _wrong_. 48901da177e4SLinus Torvalds * Though this happens in BSD with high probability, this is occasional. 48911da177e4SLinus Torvalds * Any application relying on this is buggy. Note also, that fix "works" 48921da177e4SLinus Torvalds * only in this artificial test. Insert some normal data between A and B and we will 48931da177e4SLinus Torvalds * decline of BSD again. Verdict: it is better to remove to trap 48941da177e4SLinus Torvalds * buggy users. 48951da177e4SLinus Torvalds */ 48961da177e4SLinus Torvalds if (tp->urg_seq == tp->copied_seq && tp->urg_data && 4897056834d9SIlpo Järvinen !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) { 48981da177e4SLinus Torvalds struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 48991da177e4SLinus Torvalds tp->copied_seq++; 49001da177e4SLinus Torvalds if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) { 49018728b834SDavid S. Miller __skb_unlink(skb, &sk->sk_receive_queue); 49021da177e4SLinus Torvalds __kfree_skb(skb); 49031da177e4SLinus Torvalds } 49041da177e4SLinus Torvalds } 49051da177e4SLinus Torvalds 49061da177e4SLinus Torvalds tp->urg_data = TCP_URG_NOTYET; 49071da177e4SLinus Torvalds tp->urg_seq = ptr; 49081da177e4SLinus Torvalds 49091da177e4SLinus Torvalds /* Disable header prediction. */ 49101da177e4SLinus Torvalds tp->pred_flags = 0; 49111da177e4SLinus Torvalds } 49121da177e4SLinus Torvalds 49131da177e4SLinus Torvalds /* This is the 'fast' part of urgent handling. */ 4914cf533ea5SEric Dumazet static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th) 49151da177e4SLinus Torvalds { 49161da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 49171da177e4SLinus Torvalds 49181da177e4SLinus Torvalds /* Check if we get a new urgent pointer - normally not. */ 49191da177e4SLinus Torvalds if (th->urg) 49201da177e4SLinus Torvalds tcp_check_urg(sk, th); 49211da177e4SLinus Torvalds 49221da177e4SLinus Torvalds /* Do we wait for any urgent data? - normally not... */ 49231da177e4SLinus Torvalds if (tp->urg_data == TCP_URG_NOTYET) { 49241da177e4SLinus Torvalds u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) - 49251da177e4SLinus Torvalds th->syn; 49261da177e4SLinus Torvalds 49271da177e4SLinus Torvalds /* Is the urgent pointer pointing into this packet? */ 49281da177e4SLinus Torvalds if (ptr < skb->len) { 49291da177e4SLinus Torvalds u8 tmp; 49301da177e4SLinus Torvalds if (skb_copy_bits(skb, ptr, &tmp, 1)) 49311da177e4SLinus Torvalds BUG(); 49321da177e4SLinus Torvalds tp->urg_data = TCP_URG_VALID | tmp; 49331da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) 4934676d2369SDavid S. Miller sk->sk_data_ready(sk); 49351da177e4SLinus Torvalds } 49361da177e4SLinus Torvalds } 49371da177e4SLinus Torvalds } 49381da177e4SLinus Torvalds 49391da177e4SLinus Torvalds static int tcp_copy_to_iovec(struct sock *sk, struct sk_buff *skb, int hlen) 49401da177e4SLinus Torvalds { 49411da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 49421da177e4SLinus Torvalds int chunk = skb->len - hlen; 49431da177e4SLinus Torvalds int err; 49441da177e4SLinus Torvalds 49451da177e4SLinus Torvalds local_bh_enable(); 494660476372SHerbert Xu if (skb_csum_unnecessary(skb)) 4947f4362a2cSAl Viro err = skb_copy_datagram_msg(skb, hlen, tp->ucopy.msg, chunk); 49481da177e4SLinus Torvalds else 4949f4362a2cSAl Viro err = skb_copy_and_csum_datagram_msg(skb, hlen, tp->ucopy.msg); 49501da177e4SLinus Torvalds 49511da177e4SLinus Torvalds if (!err) { 49521da177e4SLinus Torvalds tp->ucopy.len -= chunk; 49531da177e4SLinus Torvalds tp->copied_seq += chunk; 49541da177e4SLinus Torvalds tcp_rcv_space_adjust(sk); 49551da177e4SLinus Torvalds } 49561da177e4SLinus Torvalds 49571da177e4SLinus Torvalds local_bh_disable(); 49581da177e4SLinus Torvalds return err; 49591da177e4SLinus Torvalds } 49601da177e4SLinus Torvalds 4961056834d9SIlpo Järvinen static __sum16 __tcp_checksum_complete_user(struct sock *sk, 4962056834d9SIlpo Järvinen struct sk_buff *skb) 49631da177e4SLinus Torvalds { 4964b51655b9SAl Viro __sum16 result; 49651da177e4SLinus Torvalds 49661da177e4SLinus Torvalds if (sock_owned_by_user(sk)) { 49671da177e4SLinus Torvalds local_bh_enable(); 49681da177e4SLinus Torvalds result = __tcp_checksum_complete(skb); 49691da177e4SLinus Torvalds local_bh_disable(); 49701da177e4SLinus Torvalds } else { 49711da177e4SLinus Torvalds result = __tcp_checksum_complete(skb); 49721da177e4SLinus Torvalds } 49731da177e4SLinus Torvalds return result; 49741da177e4SLinus Torvalds } 49751da177e4SLinus Torvalds 497667b95bd7SVijay Subramanian static inline bool tcp_checksum_complete_user(struct sock *sk, 4977056834d9SIlpo Järvinen struct sk_buff *skb) 49781da177e4SLinus Torvalds { 497960476372SHerbert Xu return !skb_csum_unnecessary(skb) && 49801da177e4SLinus Torvalds __tcp_checksum_complete_user(sk, skb); 49811da177e4SLinus Torvalds } 49821da177e4SLinus Torvalds 4983cbe2d128SIlpo Järvinen /* Does PAWS and seqno based validation of an incoming segment, flags will 4984cbe2d128SIlpo Järvinen * play significant role here. 4985cbe2d128SIlpo Järvinen */ 49860c24604bSEric Dumazet static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb, 4987cf533ea5SEric Dumazet const struct tcphdr *th, int syn_inerr) 4988cbe2d128SIlpo Järvinen { 4989cbe2d128SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 4990cbe2d128SIlpo Järvinen 4991cbe2d128SIlpo Järvinen /* RFC1323: H1. Apply PAWS check first. */ 49921a2c6181SChristoph Paasch if (tcp_fast_parse_options(skb, th, tp) && tp->rx_opt.saw_tstamp && 4993cbe2d128SIlpo Järvinen tcp_paws_discard(sk, skb)) { 4994cbe2d128SIlpo Järvinen if (!th->rst) { 4995cbe2d128SIlpo Järvinen NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED); 4996cbe2d128SIlpo Järvinen tcp_send_dupack(sk, skb); 4997cbe2d128SIlpo Järvinen goto discard; 4998cbe2d128SIlpo Järvinen } 4999cbe2d128SIlpo Järvinen /* Reset is accepted even if it did not pass PAWS. */ 5000cbe2d128SIlpo Järvinen } 5001cbe2d128SIlpo Järvinen 5002cbe2d128SIlpo Järvinen /* Step 1: check sequence number */ 5003cbe2d128SIlpo Järvinen if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) { 5004cbe2d128SIlpo Järvinen /* RFC793, page 37: "In all states except SYN-SENT, all reset 5005cbe2d128SIlpo Järvinen * (RST) segments are validated by checking their SEQ-fields." 5006cbe2d128SIlpo Järvinen * And page 69: "If an incoming segment is not acceptable, 5007cbe2d128SIlpo Järvinen * an acknowledgment should be sent in reply (unless the RST 5008cbe2d128SIlpo Järvinen * bit is set, if so drop the segment and return)". 5009cbe2d128SIlpo Järvinen */ 5010e3715899SEric Dumazet if (!th->rst) { 5011e3715899SEric Dumazet if (th->syn) 5012e3715899SEric Dumazet goto syn_challenge; 5013cbe2d128SIlpo Järvinen tcp_send_dupack(sk, skb); 5014e3715899SEric Dumazet } 5015cbe2d128SIlpo Järvinen goto discard; 5016cbe2d128SIlpo Järvinen } 5017cbe2d128SIlpo Järvinen 5018cbe2d128SIlpo Järvinen /* Step 2: check RST bit */ 5019cbe2d128SIlpo Järvinen if (th->rst) { 5020282f23c6SEric Dumazet /* RFC 5961 3.2 : 5021282f23c6SEric Dumazet * If sequence number exactly matches RCV.NXT, then 5022282f23c6SEric Dumazet * RESET the connection 5023282f23c6SEric Dumazet * else 5024282f23c6SEric Dumazet * Send a challenge ACK 5025282f23c6SEric Dumazet */ 5026282f23c6SEric Dumazet if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) 5027cbe2d128SIlpo Järvinen tcp_reset(sk); 5028282f23c6SEric Dumazet else 5029282f23c6SEric Dumazet tcp_send_challenge_ack(sk); 5030cbe2d128SIlpo Järvinen goto discard; 5031cbe2d128SIlpo Järvinen } 5032cbe2d128SIlpo Järvinen 5033cbe2d128SIlpo Järvinen /* step 3: check security and precedence [ignored] */ 5034cbe2d128SIlpo Järvinen 50350c24604bSEric Dumazet /* step 4: Check for a SYN 5036cd214535SSowmini Varadhan * RFC 5961 4.2 : Send a challenge ack 50370c24604bSEric Dumazet */ 50380c24604bSEric Dumazet if (th->syn) { 5039e3715899SEric Dumazet syn_challenge: 5040cbe2d128SIlpo Järvinen if (syn_inerr) 5041cbe2d128SIlpo Järvinen TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS); 50420c24604bSEric Dumazet NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE); 50430c24604bSEric Dumazet tcp_send_challenge_ack(sk); 50440c24604bSEric Dumazet goto discard; 5045cbe2d128SIlpo Järvinen } 5046cbe2d128SIlpo Järvinen 50470c24604bSEric Dumazet return true; 5048cbe2d128SIlpo Järvinen 5049cbe2d128SIlpo Järvinen discard: 5050cbe2d128SIlpo Järvinen __kfree_skb(skb); 50510c24604bSEric Dumazet return false; 5052cbe2d128SIlpo Järvinen } 5053cbe2d128SIlpo Järvinen 50541da177e4SLinus Torvalds /* 50551da177e4SLinus Torvalds * TCP receive function for the ESTABLISHED state. 50561da177e4SLinus Torvalds * 50571da177e4SLinus Torvalds * It is split into a fast path and a slow path. The fast path is 50581da177e4SLinus Torvalds * disabled when: 50591da177e4SLinus Torvalds * - A zero window was announced from us - zero window probing 50601da177e4SLinus Torvalds * is only handled properly in the slow path. 50611da177e4SLinus Torvalds * - Out of order segments arrived. 50621da177e4SLinus Torvalds * - Urgent data is expected. 50631da177e4SLinus Torvalds * - There is no buffer space left 50641da177e4SLinus Torvalds * - Unexpected TCP flags/window values/header lengths are received 50651da177e4SLinus Torvalds * (detected by checking the TCP header against pred_flags) 50661da177e4SLinus Torvalds * - Data is sent in both directions. Fast path only supports pure senders 50671da177e4SLinus Torvalds * or pure receivers (this means either the sequence number or the ack 50681da177e4SLinus Torvalds * value must stay constant) 50691da177e4SLinus Torvalds * - Unexpected TCP option. 50701da177e4SLinus Torvalds * 50711da177e4SLinus Torvalds * When these conditions are not satisfied it drops into a standard 50721da177e4SLinus Torvalds * receive procedure patterned after RFC793 to handle all cases. 50731da177e4SLinus Torvalds * The first three cases are guaranteed by proper pred_flags setting, 50741da177e4SLinus Torvalds * the rest is checked inline. Fast processing is turned on in 50751da177e4SLinus Torvalds * tcp_data_queue when everything is OK. 50761da177e4SLinus Torvalds */ 5077c995ae22SVijay Subramanian void tcp_rcv_established(struct sock *sk, struct sk_buff *skb, 5078cf533ea5SEric Dumazet const struct tcphdr *th, unsigned int len) 50791da177e4SLinus Torvalds { 50801da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 50811da177e4SLinus Torvalds 50825d299f3dSEric Dumazet if (unlikely(sk->sk_rx_dst == NULL)) 50835d299f3dSEric Dumazet inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb); 50841da177e4SLinus Torvalds /* 50851da177e4SLinus Torvalds * Header prediction. 50861da177e4SLinus Torvalds * The code loosely follows the one in the famous 50871da177e4SLinus Torvalds * "30 instruction TCP receive" Van Jacobson mail. 50881da177e4SLinus Torvalds * 50891da177e4SLinus Torvalds * Van's trick is to deposit buffers into socket queue 50901da177e4SLinus Torvalds * on a device interrupt, to call tcp_recv function 50911da177e4SLinus Torvalds * on the receive process context and checksum and copy 50921da177e4SLinus Torvalds * the buffer to user space. smart... 50931da177e4SLinus Torvalds * 50941da177e4SLinus Torvalds * Our current scheme is not silly either but we take the 50951da177e4SLinus Torvalds * extra cost of the net_bh soft interrupt processing... 50961da177e4SLinus Torvalds * We do checksum and copy also but from device to kernel. 50971da177e4SLinus Torvalds */ 50981da177e4SLinus Torvalds 50991da177e4SLinus Torvalds tp->rx_opt.saw_tstamp = 0; 51001da177e4SLinus Torvalds 51011da177e4SLinus Torvalds /* pred_flags is 0xS?10 << 16 + snd_wnd 5102caa20d9aSStephen Hemminger * if header_prediction is to be made 51031da177e4SLinus Torvalds * 'S' will always be tp->tcp_header_len >> 2 51041da177e4SLinus Torvalds * '?' will be 0 for the fast path, otherwise pred_flags is 0 to 51051da177e4SLinus Torvalds * turn it off (when there are holes in the receive 51061da177e4SLinus Torvalds * space for instance) 51071da177e4SLinus Torvalds * PSH flag is ignored. 51081da177e4SLinus Torvalds */ 51091da177e4SLinus Torvalds 51101da177e4SLinus Torvalds if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags && 511196e0bf4bSJohn Dykstra TCP_SKB_CB(skb)->seq == tp->rcv_nxt && 511296e0bf4bSJohn Dykstra !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) { 51131da177e4SLinus Torvalds int tcp_header_len = tp->tcp_header_len; 51141da177e4SLinus Torvalds 51151da177e4SLinus Torvalds /* Timestamp header prediction: tcp_header_len 51161da177e4SLinus Torvalds * is automatically equal to th->doff*4 due to pred_flags 51171da177e4SLinus Torvalds * match. 51181da177e4SLinus Torvalds */ 51191da177e4SLinus Torvalds 51201da177e4SLinus Torvalds /* Check timestamp */ 51211da177e4SLinus Torvalds if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) { 51221da177e4SLinus Torvalds /* No? Slow path! */ 5123a4356b29SIlpo Järvinen if (!tcp_parse_aligned_timestamp(tp, th)) 51241da177e4SLinus Torvalds goto slow_path; 51251da177e4SLinus Torvalds 51261da177e4SLinus Torvalds /* If PAWS failed, check it more carefully in slow path */ 51271da177e4SLinus Torvalds if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0) 51281da177e4SLinus Torvalds goto slow_path; 51291da177e4SLinus Torvalds 51301da177e4SLinus Torvalds /* DO NOT update ts_recent here, if checksum fails 51311da177e4SLinus Torvalds * and timestamp was corrupted part, it will result 51321da177e4SLinus Torvalds * in a hung connection since we will drop all 51331da177e4SLinus Torvalds * future packets due to the PAWS test. 51341da177e4SLinus Torvalds */ 51351da177e4SLinus Torvalds } 51361da177e4SLinus Torvalds 51371da177e4SLinus Torvalds if (len <= tcp_header_len) { 51381da177e4SLinus Torvalds /* Bulk data transfer: sender */ 51391da177e4SLinus Torvalds if (len == tcp_header_len) { 51401da177e4SLinus Torvalds /* Predicted packet is in window by definition. 51411da177e4SLinus Torvalds * seq == rcv_nxt and rcv_wup <= rcv_nxt. 51421da177e4SLinus Torvalds * Hence, check seq<=rcv_wup reduces to: 51431da177e4SLinus Torvalds */ 51441da177e4SLinus Torvalds if (tcp_header_len == 51451da177e4SLinus Torvalds (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) && 51461da177e4SLinus Torvalds tp->rcv_nxt == tp->rcv_wup) 51471da177e4SLinus Torvalds tcp_store_ts_recent(tp); 51481da177e4SLinus Torvalds 51491da177e4SLinus Torvalds /* We know that such packets are checksummed 51501da177e4SLinus Torvalds * on entry. 51511da177e4SLinus Torvalds */ 51521da177e4SLinus Torvalds tcp_ack(sk, skb, 0); 51531da177e4SLinus Torvalds __kfree_skb(skb); 51549e412ba7SIlpo Järvinen tcp_data_snd_check(sk); 5155c995ae22SVijay Subramanian return; 51561da177e4SLinus Torvalds } else { /* Header too small */ 515763231bddSPavel Emelyanov TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS); 51581da177e4SLinus Torvalds goto discard; 51591da177e4SLinus Torvalds } 51601da177e4SLinus Torvalds } else { 51611da177e4SLinus Torvalds int eaten = 0; 5162b081f85cSEric Dumazet bool fragstolen = false; 51631da177e4SLinus Torvalds 516459ea33a6SJiri Kosina if (tp->ucopy.task == current && 5165d27f9bc1SDan Williams tp->copied_seq == tp->rcv_nxt && 5166d27f9bc1SDan Williams len - tcp_header_len <= tp->ucopy.len && 5167d27f9bc1SDan Williams sock_owned_by_user(sk)) { 51681da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 51691da177e4SLinus Torvalds 5170d27f9bc1SDan Williams if (!tcp_copy_to_iovec(sk, skb, tcp_header_len)) { 51711da177e4SLinus Torvalds /* Predicted packet is in window by definition. 51721da177e4SLinus Torvalds * seq == rcv_nxt and rcv_wup <= rcv_nxt. 51731da177e4SLinus Torvalds * Hence, check seq<=rcv_wup reduces to: 51741da177e4SLinus Torvalds */ 51751da177e4SLinus Torvalds if (tcp_header_len == 51761da177e4SLinus Torvalds (sizeof(struct tcphdr) + 51771da177e4SLinus Torvalds TCPOLEN_TSTAMP_ALIGNED) && 51781da177e4SLinus Torvalds tp->rcv_nxt == tp->rcv_wup) 51791da177e4SLinus Torvalds tcp_store_ts_recent(tp); 51801da177e4SLinus Torvalds 5181463c84b9SArnaldo Carvalho de Melo tcp_rcv_rtt_measure_ts(sk, skb); 51821da177e4SLinus Torvalds 51831da177e4SLinus Torvalds __skb_pull(skb, tcp_header_len); 51841da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 5185de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITSTOUSER); 5186d27f9bc1SDan Williams eaten = 1; 51871da177e4SLinus Torvalds } 51881da177e4SLinus Torvalds } 51891da177e4SLinus Torvalds if (!eaten) { 51901da177e4SLinus Torvalds if (tcp_checksum_complete_user(sk, skb)) 51911da177e4SLinus Torvalds goto csum_error; 51921da177e4SLinus Torvalds 5193aab2b4bfSNeal Cardwell if ((int)skb->truesize > sk->sk_forward_alloc) 5194aab2b4bfSNeal Cardwell goto step5; 5195aab2b4bfSNeal Cardwell 51961da177e4SLinus Torvalds /* Predicted packet is in window by definition. 51971da177e4SLinus Torvalds * seq == rcv_nxt and rcv_wup <= rcv_nxt. 51981da177e4SLinus Torvalds * Hence, check seq<=rcv_wup reduces to: 51991da177e4SLinus Torvalds */ 52001da177e4SLinus Torvalds if (tcp_header_len == 52011da177e4SLinus Torvalds (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) && 52021da177e4SLinus Torvalds tp->rcv_nxt == tp->rcv_wup) 52031da177e4SLinus Torvalds tcp_store_ts_recent(tp); 52041da177e4SLinus Torvalds 5205463c84b9SArnaldo Carvalho de Melo tcp_rcv_rtt_measure_ts(sk, skb); 52061da177e4SLinus Torvalds 5207de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITS); 52081da177e4SLinus Torvalds 52091da177e4SLinus Torvalds /* Bulk data transfer: receiver */ 5210b081f85cSEric Dumazet eaten = tcp_queue_rcv(sk, skb, tcp_header_len, 5211b081f85cSEric Dumazet &fragstolen); 52121da177e4SLinus Torvalds } 52131da177e4SLinus Torvalds 52149e412ba7SIlpo Järvinen tcp_event_data_recv(sk, skb); 52151da177e4SLinus Torvalds 52161da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) { 52171da177e4SLinus Torvalds /* Well, only one small jumplet in fast path... */ 52181da177e4SLinus Torvalds tcp_ack(sk, skb, FLAG_DATA); 52199e412ba7SIlpo Järvinen tcp_data_snd_check(sk); 5220463c84b9SArnaldo Carvalho de Melo if (!inet_csk_ack_scheduled(sk)) 52211da177e4SLinus Torvalds goto no_ack; 52221da177e4SLinus Torvalds } 52231da177e4SLinus Torvalds 52241da177e4SLinus Torvalds __tcp_ack_snd_check(sk, 0); 52251da177e4SLinus Torvalds no_ack: 52261da177e4SLinus Torvalds if (eaten) 5227b081f85cSEric Dumazet kfree_skb_partial(skb, fragstolen); 5228676d2369SDavid S. Miller sk->sk_data_ready(sk); 5229c995ae22SVijay Subramanian return; 52301da177e4SLinus Torvalds } 52311da177e4SLinus Torvalds } 52321da177e4SLinus Torvalds 52331da177e4SLinus Torvalds slow_path: 52341da177e4SLinus Torvalds if (len < (th->doff << 2) || tcp_checksum_complete_user(sk, skb)) 52351da177e4SLinus Torvalds goto csum_error; 52361da177e4SLinus Torvalds 52370c228e83SCalvin Owens if (!th->ack && !th->rst && !th->syn) 5238c3ae62afSEric Dumazet goto discard; 5239c3ae62afSEric Dumazet 52401da177e4SLinus Torvalds /* 52411da177e4SLinus Torvalds * Standard slow path. 52421da177e4SLinus Torvalds */ 52431da177e4SLinus Torvalds 52440c24604bSEric Dumazet if (!tcp_validate_incoming(sk, skb, th, 1)) 5245c995ae22SVijay Subramanian return; 52461da177e4SLinus Torvalds 52471da177e4SLinus Torvalds step5: 524812fb3dd9SEric Dumazet if (tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT) < 0) 524996e0bf4bSJohn Dykstra goto discard; 52501da177e4SLinus Torvalds 5251463c84b9SArnaldo Carvalho de Melo tcp_rcv_rtt_measure_ts(sk, skb); 52521da177e4SLinus Torvalds 52531da177e4SLinus Torvalds /* Process urgent data. */ 52541da177e4SLinus Torvalds tcp_urg(sk, skb, th); 52551da177e4SLinus Torvalds 52561da177e4SLinus Torvalds /* step 7: process the segment text */ 52571da177e4SLinus Torvalds tcp_data_queue(sk, skb); 52581da177e4SLinus Torvalds 52599e412ba7SIlpo Järvinen tcp_data_snd_check(sk); 52601da177e4SLinus Torvalds tcp_ack_snd_check(sk); 5261c995ae22SVijay Subramanian return; 52621da177e4SLinus Torvalds 52631da177e4SLinus Torvalds csum_error: 52646a5dc9e5SEric Dumazet TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_CSUMERRORS); 526563231bddSPavel Emelyanov TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS); 52661da177e4SLinus Torvalds 52671da177e4SLinus Torvalds discard: 52681da177e4SLinus Torvalds __kfree_skb(skb); 52691da177e4SLinus Torvalds } 52704bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_rcv_established); 52711da177e4SLinus Torvalds 5272370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb) 5273370816aeSPavel Emelyanov { 5274370816aeSPavel Emelyanov struct tcp_sock *tp = tcp_sk(sk); 5275370816aeSPavel Emelyanov struct inet_connection_sock *icsk = inet_csk(sk); 5276370816aeSPavel Emelyanov 5277370816aeSPavel Emelyanov tcp_set_state(sk, TCP_ESTABLISHED); 5278370816aeSPavel Emelyanov 527941063e9dSDavid S. Miller if (skb != NULL) { 52805d299f3dSEric Dumazet icsk->icsk_af_ops->sk_rx_dst_set(sk, skb); 5281370816aeSPavel Emelyanov security_inet_conn_established(sk, skb); 528241063e9dSDavid S. Miller } 5283370816aeSPavel Emelyanov 5284370816aeSPavel Emelyanov /* Make sure socket is routed, for correct metrics. */ 5285370816aeSPavel Emelyanov icsk->icsk_af_ops->rebuild_header(sk); 5286370816aeSPavel Emelyanov 5287370816aeSPavel Emelyanov tcp_init_metrics(sk); 5288370816aeSPavel Emelyanov 5289370816aeSPavel Emelyanov tcp_init_congestion_control(sk); 5290370816aeSPavel Emelyanov 5291370816aeSPavel Emelyanov /* Prevent spurious tcp_cwnd_restart() on first data 5292370816aeSPavel Emelyanov * packet. 5293370816aeSPavel Emelyanov */ 5294370816aeSPavel Emelyanov tp->lsndtime = tcp_time_stamp; 5295370816aeSPavel Emelyanov 5296370816aeSPavel Emelyanov tcp_init_buffer_space(sk); 5297370816aeSPavel Emelyanov 5298370816aeSPavel Emelyanov if (sock_flag(sk, SOCK_KEEPOPEN)) 5299370816aeSPavel Emelyanov inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp)); 5300370816aeSPavel Emelyanov 5301370816aeSPavel Emelyanov if (!tp->rx_opt.snd_wscale) 5302370816aeSPavel Emelyanov __tcp_fast_path_on(tp, tp->snd_wnd); 5303370816aeSPavel Emelyanov else 5304370816aeSPavel Emelyanov tp->pred_flags = 0; 5305370816aeSPavel Emelyanov 5306370816aeSPavel Emelyanov if (!sock_flag(sk, SOCK_DEAD)) { 5307370816aeSPavel Emelyanov sk->sk_state_change(sk); 5308370816aeSPavel Emelyanov sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT); 5309370816aeSPavel Emelyanov } 5310370816aeSPavel Emelyanov } 5311370816aeSPavel Emelyanov 53128e4178c1SYuchung Cheng static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack, 53138e4178c1SYuchung Cheng struct tcp_fastopen_cookie *cookie) 53148e4178c1SYuchung Cheng { 53158e4178c1SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk); 531667da22d2SYuchung Cheng struct sk_buff *data = tp->syn_data ? tcp_write_queue_head(sk) : NULL; 53178e4178c1SYuchung Cheng u16 mss = tp->rx_opt.mss_clamp; 5318aab48743SYuchung Cheng bool syn_drop; 53198e4178c1SYuchung Cheng 53208e4178c1SYuchung Cheng if (mss == tp->rx_opt.user_mss) { 53218e4178c1SYuchung Cheng struct tcp_options_received opt; 53228e4178c1SYuchung Cheng 53238e4178c1SYuchung Cheng /* Get original SYNACK MSS value if user MSS sets mss_clamp */ 53248e4178c1SYuchung Cheng tcp_clear_options(&opt); 53258e4178c1SYuchung Cheng opt.user_mss = opt.mss_clamp = 0; 53261a2c6181SChristoph Paasch tcp_parse_options(synack, &opt, 0, NULL); 53278e4178c1SYuchung Cheng mss = opt.mss_clamp; 53288e4178c1SYuchung Cheng } 53298e4178c1SYuchung Cheng 533067da22d2SYuchung Cheng if (!tp->syn_fastopen) /* Ignore an unsolicited cookie */ 533167da22d2SYuchung Cheng cookie->len = -1; 533267da22d2SYuchung Cheng 5333aab48743SYuchung Cheng /* The SYN-ACK neither has cookie nor acknowledges the data. Presumably 5334aab48743SYuchung Cheng * the remote receives only the retransmitted (regular) SYNs: either 5335aab48743SYuchung Cheng * the original SYN-data or the corresponding SYN-ACK is lost. 5336aab48743SYuchung Cheng */ 533766555e92SYuchung Cheng syn_drop = (cookie->len <= 0 && data && tp->total_retrans); 5338aab48743SYuchung Cheng 5339aab48743SYuchung Cheng tcp_fastopen_cache_set(sk, mss, cookie, syn_drop); 53408e4178c1SYuchung Cheng 53418e4178c1SYuchung Cheng if (data) { /* Retransmit unacked data in SYN */ 534293b174adSYuchung Cheng tcp_for_write_queue_from(data, sk) { 534393b174adSYuchung Cheng if (data == tcp_send_head(sk) || 534493b174adSYuchung Cheng __tcp_retransmit_skb(sk, data)) 534593b174adSYuchung Cheng break; 534693b174adSYuchung Cheng } 53478e4178c1SYuchung Cheng tcp_rearm_rto(sk); 5348f19c29e3SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVEFAIL); 53498e4178c1SYuchung Cheng return true; 53508e4178c1SYuchung Cheng } 53516f73601eSYuchung Cheng tp->syn_data_acked = tp->syn_data; 5352f19c29e3SYuchung Cheng if (tp->syn_data_acked) 5353f19c29e3SYuchung Cheng NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVE); 53548e4178c1SYuchung Cheng return false; 53558e4178c1SYuchung Cheng } 53568e4178c1SYuchung Cheng 53571da177e4SLinus Torvalds static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb, 5358cf533ea5SEric Dumazet const struct tcphdr *th, unsigned int len) 53591da177e4SLinus Torvalds { 5360d83d8461SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 53614957faadSWilliam Allen Simpson struct tcp_sock *tp = tcp_sk(sk); 53628e4178c1SYuchung Cheng struct tcp_fastopen_cookie foc = { .len = -1 }; 53634957faadSWilliam Allen Simpson int saved_clamp = tp->rx_opt.mss_clamp; 53641da177e4SLinus Torvalds 53651a2c6181SChristoph Paasch tcp_parse_options(skb, &tp->rx_opt, 0, &foc); 5366e3e12028SAndrew Vagin if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) 5367ee684b6fSAndrey Vagin tp->rx_opt.rcv_tsecr -= tp->tsoffset; 53681da177e4SLinus Torvalds 53691da177e4SLinus Torvalds if (th->ack) { 53701da177e4SLinus Torvalds /* rfc793: 53711da177e4SLinus Torvalds * "If the state is SYN-SENT then 53721da177e4SLinus Torvalds * first check the ACK bit 53731da177e4SLinus Torvalds * If the ACK bit is set 53741da177e4SLinus Torvalds * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send 53751da177e4SLinus Torvalds * a reset (unless the RST bit is set, if so drop 53761da177e4SLinus Torvalds * the segment and return)" 53771da177e4SLinus Torvalds */ 53788e4178c1SYuchung Cheng if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) || 53798e4178c1SYuchung Cheng after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) 53801da177e4SLinus Torvalds goto reset_and_undo; 53811da177e4SLinus Torvalds 53821da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && 53831da177e4SLinus Torvalds !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp, 53841da177e4SLinus Torvalds tcp_time_stamp)) { 5385de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSACTIVEREJECTED); 53861da177e4SLinus Torvalds goto reset_and_undo; 53871da177e4SLinus Torvalds } 53881da177e4SLinus Torvalds 53891da177e4SLinus Torvalds /* Now ACK is acceptable. 53901da177e4SLinus Torvalds * 53911da177e4SLinus Torvalds * "If the RST bit is set 53921da177e4SLinus Torvalds * If the ACK was acceptable then signal the user "error: 53931da177e4SLinus Torvalds * connection reset", drop the segment, enter CLOSED state, 53941da177e4SLinus Torvalds * delete TCB, and return." 53951da177e4SLinus Torvalds */ 53961da177e4SLinus Torvalds 53971da177e4SLinus Torvalds if (th->rst) { 53981da177e4SLinus Torvalds tcp_reset(sk); 53991da177e4SLinus Torvalds goto discard; 54001da177e4SLinus Torvalds } 54011da177e4SLinus Torvalds 54021da177e4SLinus Torvalds /* rfc793: 54031da177e4SLinus Torvalds * "fifth, if neither of the SYN or RST bits is set then 54041da177e4SLinus Torvalds * drop the segment and return." 54051da177e4SLinus Torvalds * 54061da177e4SLinus Torvalds * See note below! 54071da177e4SLinus Torvalds * --ANK(990513) 54081da177e4SLinus Torvalds */ 54091da177e4SLinus Torvalds if (!th->syn) 54101da177e4SLinus Torvalds goto discard_and_undo; 54111da177e4SLinus Torvalds 54121da177e4SLinus Torvalds /* rfc793: 54131da177e4SLinus Torvalds * "If the SYN bit is on ... 54141da177e4SLinus Torvalds * are acceptable then ... 54151da177e4SLinus Torvalds * (our SYN has been ACKed), change the connection 54161da177e4SLinus Torvalds * state to ESTABLISHED..." 54171da177e4SLinus Torvalds */ 54181da177e4SLinus Torvalds 5419735d3831SFlorian Westphal tcp_ecn_rcv_synack(tp, th); 54201da177e4SLinus Torvalds 54211b3a6926SRazvan Ghitulete tcp_init_wl(tp, TCP_SKB_CB(skb)->seq); 54221da177e4SLinus Torvalds tcp_ack(sk, skb, FLAG_SLOWPATH); 54231da177e4SLinus Torvalds 54241da177e4SLinus Torvalds /* Ok.. it's good. Set up sequence numbers and 54251da177e4SLinus Torvalds * move to established. 54261da177e4SLinus Torvalds */ 54271da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1; 54281da177e4SLinus Torvalds tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1; 54291da177e4SLinus Torvalds 54301da177e4SLinus Torvalds /* RFC1323: The window in SYN & SYN/ACK segments is 54311da177e4SLinus Torvalds * never scaled. 54321da177e4SLinus Torvalds */ 54331da177e4SLinus Torvalds tp->snd_wnd = ntohs(th->window); 54341da177e4SLinus Torvalds 54351da177e4SLinus Torvalds if (!tp->rx_opt.wscale_ok) { 54361da177e4SLinus Torvalds tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0; 54371da177e4SLinus Torvalds tp->window_clamp = min(tp->window_clamp, 65535U); 54381da177e4SLinus Torvalds } 54391da177e4SLinus Torvalds 54401da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp) { 54411da177e4SLinus Torvalds tp->rx_opt.tstamp_ok = 1; 54421da177e4SLinus Torvalds tp->tcp_header_len = 54431da177e4SLinus Torvalds sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED; 54441da177e4SLinus Torvalds tp->advmss -= TCPOLEN_TSTAMP_ALIGNED; 54451da177e4SLinus Torvalds tcp_store_ts_recent(tp); 54461da177e4SLinus Torvalds } else { 54471da177e4SLinus Torvalds tp->tcp_header_len = sizeof(struct tcphdr); 54481da177e4SLinus Torvalds } 54491da177e4SLinus Torvalds 5450e60402d0SIlpo Järvinen if (tcp_is_sack(tp) && sysctl_tcp_fack) 5451e60402d0SIlpo Järvinen tcp_enable_fack(tp); 54521da177e4SLinus Torvalds 54535d424d5aSJohn Heffner tcp_mtup_init(sk); 5454d83d8461SArnaldo Carvalho de Melo tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 54551da177e4SLinus Torvalds tcp_initialize_rcv_mss(sk); 54561da177e4SLinus Torvalds 54571da177e4SLinus Torvalds /* Remember, tcp_poll() does not lock socket! 54581da177e4SLinus Torvalds * Change state from SYN-SENT only after copied_seq 54591da177e4SLinus Torvalds * is initialized. */ 54601da177e4SLinus Torvalds tp->copied_seq = tp->rcv_nxt; 54614957faadSWilliam Allen Simpson 5462e16aa207SRalf Baechle smp_mb(); 54631da177e4SLinus Torvalds 5464370816aeSPavel Emelyanov tcp_finish_connect(sk, skb); 54651da177e4SLinus Torvalds 546667da22d2SYuchung Cheng if ((tp->syn_fastopen || tp->syn_data) && 546767da22d2SYuchung Cheng tcp_rcv_fastopen_synack(sk, skb, &foc)) 54688e4178c1SYuchung Cheng return -1; 54698e4178c1SYuchung Cheng 5470295f7324SArnaldo Carvalho de Melo if (sk->sk_write_pending || 5471295f7324SArnaldo Carvalho de Melo icsk->icsk_accept_queue.rskq_defer_accept || 5472295f7324SArnaldo Carvalho de Melo icsk->icsk_ack.pingpong) { 54731da177e4SLinus Torvalds /* Save one ACK. Data will be ready after 54741da177e4SLinus Torvalds * several ticks, if write_pending is set. 54751da177e4SLinus Torvalds * 54761da177e4SLinus Torvalds * It may be deleted, but with this feature tcpdumps 54771da177e4SLinus Torvalds * look so _wonderfully_ clever, that I was not able 54781da177e4SLinus Torvalds * to stand against the temptation 8) --ANK 54791da177e4SLinus Torvalds */ 5480463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 5481295f7324SArnaldo Carvalho de Melo icsk->icsk_ack.lrcvtime = tcp_time_stamp; 5482463c84b9SArnaldo Carvalho de Melo tcp_enter_quickack_mode(sk); 54833f421baaSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 54843f421baaSArnaldo Carvalho de Melo TCP_DELACK_MAX, TCP_RTO_MAX); 54851da177e4SLinus Torvalds 54861da177e4SLinus Torvalds discard: 54871da177e4SLinus Torvalds __kfree_skb(skb); 54881da177e4SLinus Torvalds return 0; 54891da177e4SLinus Torvalds } else { 54901da177e4SLinus Torvalds tcp_send_ack(sk); 54911da177e4SLinus Torvalds } 54921da177e4SLinus Torvalds return -1; 54931da177e4SLinus Torvalds } 54941da177e4SLinus Torvalds 54951da177e4SLinus Torvalds /* No ACK in the segment */ 54961da177e4SLinus Torvalds 54971da177e4SLinus Torvalds if (th->rst) { 54981da177e4SLinus Torvalds /* rfc793: 54991da177e4SLinus Torvalds * "If the RST bit is set 55001da177e4SLinus Torvalds * 55011da177e4SLinus Torvalds * Otherwise (no ACK) drop the segment and return." 55021da177e4SLinus Torvalds */ 55031da177e4SLinus Torvalds 55041da177e4SLinus Torvalds goto discard_and_undo; 55051da177e4SLinus Torvalds } 55061da177e4SLinus Torvalds 55071da177e4SLinus Torvalds /* PAWS check. */ 5508056834d9SIlpo Järvinen if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp && 5509c887e6d2SIlpo Järvinen tcp_paws_reject(&tp->rx_opt, 0)) 55101da177e4SLinus Torvalds goto discard_and_undo; 55111da177e4SLinus Torvalds 55121da177e4SLinus Torvalds if (th->syn) { 55131da177e4SLinus Torvalds /* We see SYN without ACK. It is attempt of 55141da177e4SLinus Torvalds * simultaneous connect with crossed SYNs. 55151da177e4SLinus Torvalds * Particularly, it can be connect to self. 55161da177e4SLinus Torvalds */ 55171da177e4SLinus Torvalds tcp_set_state(sk, TCP_SYN_RECV); 55181da177e4SLinus Torvalds 55191da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp) { 55201da177e4SLinus Torvalds tp->rx_opt.tstamp_ok = 1; 55211da177e4SLinus Torvalds tcp_store_ts_recent(tp); 55221da177e4SLinus Torvalds tp->tcp_header_len = 55231da177e4SLinus Torvalds sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED; 55241da177e4SLinus Torvalds } else { 55251da177e4SLinus Torvalds tp->tcp_header_len = sizeof(struct tcphdr); 55261da177e4SLinus Torvalds } 55271da177e4SLinus Torvalds 55281da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1; 55291da177e4SLinus Torvalds tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1; 55301da177e4SLinus Torvalds 55311da177e4SLinus Torvalds /* RFC1323: The window in SYN & SYN/ACK segments is 55321da177e4SLinus Torvalds * never scaled. 55331da177e4SLinus Torvalds */ 55341da177e4SLinus Torvalds tp->snd_wnd = ntohs(th->window); 55351da177e4SLinus Torvalds tp->snd_wl1 = TCP_SKB_CB(skb)->seq; 55361da177e4SLinus Torvalds tp->max_window = tp->snd_wnd; 55371da177e4SLinus Torvalds 5538735d3831SFlorian Westphal tcp_ecn_rcv_syn(tp, th); 55391da177e4SLinus Torvalds 55405d424d5aSJohn Heffner tcp_mtup_init(sk); 5541d83d8461SArnaldo Carvalho de Melo tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 55421da177e4SLinus Torvalds tcp_initialize_rcv_mss(sk); 55431da177e4SLinus Torvalds 55441da177e4SLinus Torvalds tcp_send_synack(sk); 55451da177e4SLinus Torvalds #if 0 55461da177e4SLinus Torvalds /* Note, we could accept data and URG from this segment. 5547168a8f58SJerry Chu * There are no obstacles to make this (except that we must 5548168a8f58SJerry Chu * either change tcp_recvmsg() to prevent it from returning data 5549168a8f58SJerry Chu * before 3WHS completes per RFC793, or employ TCP Fast Open). 55501da177e4SLinus Torvalds * 55511da177e4SLinus Torvalds * However, if we ignore data in ACKless segments sometimes, 55521da177e4SLinus Torvalds * we have no reasons to accept it sometimes. 55531da177e4SLinus Torvalds * Also, seems the code doing it in step6 of tcp_rcv_state_process 55541da177e4SLinus Torvalds * is not flawless. So, discard packet for sanity. 55551da177e4SLinus Torvalds * Uncomment this return to process the data. 55561da177e4SLinus Torvalds */ 55571da177e4SLinus Torvalds return -1; 55581da177e4SLinus Torvalds #else 55591da177e4SLinus Torvalds goto discard; 55601da177e4SLinus Torvalds #endif 55611da177e4SLinus Torvalds } 55621da177e4SLinus Torvalds /* "fifth, if neither of the SYN or RST bits is set then 55631da177e4SLinus Torvalds * drop the segment and return." 55641da177e4SLinus Torvalds */ 55651da177e4SLinus Torvalds 55661da177e4SLinus Torvalds discard_and_undo: 55671da177e4SLinus Torvalds tcp_clear_options(&tp->rx_opt); 55681da177e4SLinus Torvalds tp->rx_opt.mss_clamp = saved_clamp; 55691da177e4SLinus Torvalds goto discard; 55701da177e4SLinus Torvalds 55711da177e4SLinus Torvalds reset_and_undo: 55721da177e4SLinus Torvalds tcp_clear_options(&tp->rx_opt); 55731da177e4SLinus Torvalds tp->rx_opt.mss_clamp = saved_clamp; 55741da177e4SLinus Torvalds return 1; 55751da177e4SLinus Torvalds } 55761da177e4SLinus Torvalds 55771da177e4SLinus Torvalds /* 55781da177e4SLinus Torvalds * This function implements the receiving procedure of RFC 793 for 55791da177e4SLinus Torvalds * all states except ESTABLISHED and TIME_WAIT. 55801da177e4SLinus Torvalds * It's called from both tcp_v4_rcv and tcp_v6_rcv and should be 55811da177e4SLinus Torvalds * address independent. 55821da177e4SLinus Torvalds */ 55831da177e4SLinus Torvalds 55841da177e4SLinus Torvalds int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb, 5585cf533ea5SEric Dumazet const struct tcphdr *th, unsigned int len) 55861da177e4SLinus Torvalds { 55871da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 55888292a17aSArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 5589168a8f58SJerry Chu struct request_sock *req; 55901da177e4SLinus Torvalds int queued = 0; 55911f6afc81SEric Dumazet bool acceptable; 5592bc15afa3SYuchung Cheng u32 synack_stamp; 55931da177e4SLinus Torvalds 55941da177e4SLinus Torvalds tp->rx_opt.saw_tstamp = 0; 55951da177e4SLinus Torvalds 55961da177e4SLinus Torvalds switch (sk->sk_state) { 55971da177e4SLinus Torvalds case TCP_CLOSE: 55981da177e4SLinus Torvalds goto discard; 55991da177e4SLinus Torvalds 56001da177e4SLinus Torvalds case TCP_LISTEN: 56011da177e4SLinus Torvalds if (th->ack) 56021da177e4SLinus Torvalds return 1; 56031da177e4SLinus Torvalds 56041da177e4SLinus Torvalds if (th->rst) 56051da177e4SLinus Torvalds goto discard; 56061da177e4SLinus Torvalds 56071da177e4SLinus Torvalds if (th->syn) { 5608fdf5af0dSEric Dumazet if (th->fin) 5609fdf5af0dSEric Dumazet goto discard; 56108292a17aSArnaldo Carvalho de Melo if (icsk->icsk_af_ops->conn_request(sk, skb) < 0) 56111da177e4SLinus Torvalds return 1; 56121da177e4SLinus Torvalds 56131da177e4SLinus Torvalds /* Now we have several options: In theory there is 56141da177e4SLinus Torvalds * nothing else in the frame. KA9Q has an option to 56151da177e4SLinus Torvalds * send data with the syn, BSD accepts data with the 56161da177e4SLinus Torvalds * syn up to the [to be] advertised window and 56171da177e4SLinus Torvalds * Solaris 2.1 gives you a protocol error. For now 56181da177e4SLinus Torvalds * we just ignore it, that fits the spec precisely 56191da177e4SLinus Torvalds * and avoids incompatibilities. It would be nice in 56201da177e4SLinus Torvalds * future to drop through and process the data. 56211da177e4SLinus Torvalds * 56221da177e4SLinus Torvalds * Now that TTCP is starting to be used we ought to 56231da177e4SLinus Torvalds * queue this data. 56241da177e4SLinus Torvalds * But, this leaves one open to an easy denial of 56251da177e4SLinus Torvalds * service attack, and SYN cookies can't defend 56261da177e4SLinus Torvalds * against this problem. So, we drop the data 5627fb7e2399SMasayuki Nakagawa * in the interest of security over speed unless 5628fb7e2399SMasayuki Nakagawa * it's still in use. 56291da177e4SLinus Torvalds */ 5630fb7e2399SMasayuki Nakagawa kfree_skb(skb); 5631fb7e2399SMasayuki Nakagawa return 0; 56321da177e4SLinus Torvalds } 56331da177e4SLinus Torvalds goto discard; 56341da177e4SLinus Torvalds 56351da177e4SLinus Torvalds case TCP_SYN_SENT: 56361da177e4SLinus Torvalds queued = tcp_rcv_synsent_state_process(sk, skb, th, len); 56371da177e4SLinus Torvalds if (queued >= 0) 56381da177e4SLinus Torvalds return queued; 56391da177e4SLinus Torvalds 56401da177e4SLinus Torvalds /* Do step6 onward by hand. */ 56411da177e4SLinus Torvalds tcp_urg(sk, skb, th); 56421da177e4SLinus Torvalds __kfree_skb(skb); 56439e412ba7SIlpo Järvinen tcp_data_snd_check(sk); 56441da177e4SLinus Torvalds return 0; 56451da177e4SLinus Torvalds } 56461da177e4SLinus Torvalds 5647168a8f58SJerry Chu req = tp->fastopen_rsk; 5648168a8f58SJerry Chu if (req != NULL) { 564937561f68SJerry Chu WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV && 5650168a8f58SJerry Chu sk->sk_state != TCP_FIN_WAIT1); 5651168a8f58SJerry Chu 5652168a8f58SJerry Chu if (tcp_check_req(sk, skb, req, NULL, true) == NULL) 5653168a8f58SJerry Chu goto discard; 5654e69bebdeSNeal Cardwell } 5655c3ae62afSEric Dumazet 56560c228e83SCalvin Owens if (!th->ack && !th->rst && !th->syn) 5657c3ae62afSEric Dumazet goto discard; 5658c3ae62afSEric Dumazet 5659e69bebdeSNeal Cardwell if (!tcp_validate_incoming(sk, skb, th, 0)) 56600c24604bSEric Dumazet return 0; 56611da177e4SLinus Torvalds 56621da177e4SLinus Torvalds /* step 5: check the ACK field */ 56631f6afc81SEric Dumazet acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH | 566412fb3dd9SEric Dumazet FLAG_UPDATE_TS_RECENT) > 0; 56651da177e4SLinus Torvalds 56661da177e4SLinus Torvalds switch (sk->sk_state) { 56671da177e4SLinus Torvalds case TCP_SYN_RECV: 566861eb9003SJoe Perches if (!acceptable) 566961eb9003SJoe Perches return 1; 567061eb9003SJoe Perches 567161eb9003SJoe Perches /* Once we leave TCP_SYN_RECV, we no longer need req 567261eb9003SJoe Perches * so release it. 5673168a8f58SJerry Chu */ 5674168a8f58SJerry Chu if (req) { 5675bc15afa3SYuchung Cheng synack_stamp = tcp_rsk(req)->snt_synack; 5676e6c022a4SEric Dumazet tp->total_retrans = req->num_retrans; 5677168a8f58SJerry Chu reqsk_fastopen_remove(sk, req, false); 5678168a8f58SJerry Chu } else { 5679bc15afa3SYuchung Cheng synack_stamp = tp->lsndtime; 568061eb9003SJoe Perches /* Make sure socket is routed, for correct metrics. */ 5681168a8f58SJerry Chu icsk->icsk_af_ops->rebuild_header(sk); 5682168a8f58SJerry Chu tcp_init_congestion_control(sk); 5683168a8f58SJerry Chu 5684168a8f58SJerry Chu tcp_mtup_init(sk); 56851da177e4SLinus Torvalds tp->copied_seq = tp->rcv_nxt; 5686b0983d3cSEric Dumazet tcp_init_buffer_space(sk); 5687168a8f58SJerry Chu } 5688e16aa207SRalf Baechle smp_mb(); 56891da177e4SLinus Torvalds tcp_set_state(sk, TCP_ESTABLISHED); 56901da177e4SLinus Torvalds sk->sk_state_change(sk); 56911da177e4SLinus Torvalds 569261eb9003SJoe Perches /* Note, that this wakeup is only for marginal crossed SYN case. 569361eb9003SJoe Perches * Passively open sockets are not waked up, because 569461eb9003SJoe Perches * sk->sk_sleep == NULL and sk->sk_socket == NULL. 56951da177e4SLinus Torvalds */ 56968d8ad9d7SPavel Emelyanov if (sk->sk_socket) 56971f6afc81SEric Dumazet sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT); 56981da177e4SLinus Torvalds 56991da177e4SLinus Torvalds tp->snd_una = TCP_SKB_CB(skb)->ack_seq; 570061eb9003SJoe Perches tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale; 5701ee7537b6SHantzis Fotis tcp_init_wl(tp, TCP_SKB_CB(skb)->seq); 5702bc15afa3SYuchung Cheng tcp_synack_rtt_meas(sk, synack_stamp); 57031da177e4SLinus Torvalds 57041da177e4SLinus Torvalds if (tp->rx_opt.tstamp_ok) 57051da177e4SLinus Torvalds tp->advmss -= TCPOLEN_TSTAMP_ALIGNED; 57061da177e4SLinus Torvalds 5707168a8f58SJerry Chu if (req) { 570861eb9003SJoe Perches /* Re-arm the timer because data may have been sent out. 570961eb9003SJoe Perches * This is similar to the regular data transmission case 5710168a8f58SJerry Chu * when new data has just been ack'ed. 5711168a8f58SJerry Chu * 571261eb9003SJoe Perches * (TFO) - we could try to be more aggressive and 571361eb9003SJoe Perches * retransmitting any data sooner based on when they 571461eb9003SJoe Perches * are sent out. 57151da177e4SLinus Torvalds */ 5716168a8f58SJerry Chu tcp_rearm_rto(sk); 5717168a8f58SJerry Chu } else 57181da177e4SLinus Torvalds tcp_init_metrics(sk); 57191da177e4SLinus Torvalds 572002cf4ebdSNeal Cardwell tcp_update_pacing_rate(sk); 572102cf4ebdSNeal Cardwell 572261eb9003SJoe Perches /* Prevent spurious tcp_cwnd_restart() on first data packet */ 57231da177e4SLinus Torvalds tp->lsndtime = tcp_time_stamp; 57241da177e4SLinus Torvalds 57251da177e4SLinus Torvalds tcp_initialize_rcv_mss(sk); 57261da177e4SLinus Torvalds tcp_fast_path_on(tp); 57271da177e4SLinus Torvalds break; 57281da177e4SLinus Torvalds 5729c48b22daSJoe Perches case TCP_FIN_WAIT1: { 5730c48b22daSJoe Perches struct dst_entry *dst; 5731c48b22daSJoe Perches int tmo; 5732c48b22daSJoe Perches 5733168a8f58SJerry Chu /* If we enter the TCP_FIN_WAIT1 state and we are a 5734168a8f58SJerry Chu * Fast Open socket and this is the first acceptable 5735168a8f58SJerry Chu * ACK we have received, this would have acknowledged 5736168a8f58SJerry Chu * our SYNACK so stop the SYNACK timer. 5737168a8f58SJerry Chu */ 573837561f68SJerry Chu if (req != NULL) { 573937561f68SJerry Chu /* Return RST if ack_seq is invalid. 574037561f68SJerry Chu * Note that RFC793 only says to generate a 574137561f68SJerry Chu * DUPACK for it but for TCP Fast Open it seems 574237561f68SJerry Chu * better to treat this case like TCP_SYN_RECV 574337561f68SJerry Chu * above. 574437561f68SJerry Chu */ 574537561f68SJerry Chu if (!acceptable) 574637561f68SJerry Chu return 1; 5747168a8f58SJerry Chu /* We no longer need the request sock. */ 5748168a8f58SJerry Chu reqsk_fastopen_remove(sk, req, false); 5749168a8f58SJerry Chu tcp_rearm_rto(sk); 5750168a8f58SJerry Chu } 5751c48b22daSJoe Perches if (tp->snd_una != tp->write_seq) 5752c48b22daSJoe Perches break; 57535110effeSDavid S. Miller 57541da177e4SLinus Torvalds tcp_set_state(sk, TCP_FIN_WAIT2); 57551da177e4SLinus Torvalds sk->sk_shutdown |= SEND_SHUTDOWN; 57565110effeSDavid S. Miller 57575110effeSDavid S. Miller dst = __sk_dst_get(sk); 57585110effeSDavid S. Miller if (dst) 57595110effeSDavid S. Miller dst_confirm(dst); 57601da177e4SLinus Torvalds 57611f6afc81SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) { 57621da177e4SLinus Torvalds /* Wake up lingering close() */ 57631da177e4SLinus Torvalds sk->sk_state_change(sk); 5764c48b22daSJoe Perches break; 5765c48b22daSJoe Perches } 57661da177e4SLinus Torvalds 57671da177e4SLinus Torvalds if (tp->linger2 < 0 || 57681da177e4SLinus Torvalds (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && 57691da177e4SLinus Torvalds after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) { 57701da177e4SLinus Torvalds tcp_done(sk); 5771de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA); 57721da177e4SLinus Torvalds return 1; 57731da177e4SLinus Torvalds } 57741da177e4SLinus Torvalds 5775463c84b9SArnaldo Carvalho de Melo tmo = tcp_fin_time(sk); 57761da177e4SLinus Torvalds if (tmo > TCP_TIMEWAIT_LEN) { 5777463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN); 57781da177e4SLinus Torvalds } else if (th->fin || sock_owned_by_user(sk)) { 57791da177e4SLinus Torvalds /* Bad case. We could lose such FIN otherwise. 57801da177e4SLinus Torvalds * It is not a big problem, but it looks confusing 57811da177e4SLinus Torvalds * and not so rare event. We still can lose it now, 57821da177e4SLinus Torvalds * if it spins in bh_lock_sock(), but it is really 57831da177e4SLinus Torvalds * marginal case. 57841da177e4SLinus Torvalds */ 5785463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(sk, tmo); 57861da177e4SLinus Torvalds } else { 57871da177e4SLinus Torvalds tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); 57881da177e4SLinus Torvalds goto discard; 57891da177e4SLinus Torvalds } 57901da177e4SLinus Torvalds break; 5791c48b22daSJoe Perches } 57921da177e4SLinus Torvalds 57931da177e4SLinus Torvalds case TCP_CLOSING: 57941da177e4SLinus Torvalds if (tp->snd_una == tp->write_seq) { 57951da177e4SLinus Torvalds tcp_time_wait(sk, TCP_TIME_WAIT, 0); 57961da177e4SLinus Torvalds goto discard; 57971da177e4SLinus Torvalds } 57981da177e4SLinus Torvalds break; 57991da177e4SLinus Torvalds 58001da177e4SLinus Torvalds case TCP_LAST_ACK: 58011da177e4SLinus Torvalds if (tp->snd_una == tp->write_seq) { 58021da177e4SLinus Torvalds tcp_update_metrics(sk); 58031da177e4SLinus Torvalds tcp_done(sk); 58041da177e4SLinus Torvalds goto discard; 58051da177e4SLinus Torvalds } 58061da177e4SLinus Torvalds break; 58071da177e4SLinus Torvalds } 58081da177e4SLinus Torvalds 58091da177e4SLinus Torvalds /* step 6: check the URG bit */ 58101da177e4SLinus Torvalds tcp_urg(sk, skb, th); 58111da177e4SLinus Torvalds 58121da177e4SLinus Torvalds /* step 7: process the segment text */ 58131da177e4SLinus Torvalds switch (sk->sk_state) { 58141da177e4SLinus Torvalds case TCP_CLOSE_WAIT: 58151da177e4SLinus Torvalds case TCP_CLOSING: 58161da177e4SLinus Torvalds case TCP_LAST_ACK: 58171da177e4SLinus Torvalds if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) 58181da177e4SLinus Torvalds break; 58191da177e4SLinus Torvalds case TCP_FIN_WAIT1: 58201da177e4SLinus Torvalds case TCP_FIN_WAIT2: 58211da177e4SLinus Torvalds /* RFC 793 says to queue data in these states, 58221da177e4SLinus Torvalds * RFC 1122 says we MUST send a reset. 58231da177e4SLinus Torvalds * BSD 4.4 also does reset. 58241da177e4SLinus Torvalds */ 58251da177e4SLinus Torvalds if (sk->sk_shutdown & RCV_SHUTDOWN) { 58261da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && 58271da177e4SLinus Torvalds after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) { 5828de0744afSPavel Emelyanov NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA); 58291da177e4SLinus Torvalds tcp_reset(sk); 58301da177e4SLinus Torvalds return 1; 58311da177e4SLinus Torvalds } 58321da177e4SLinus Torvalds } 58331da177e4SLinus Torvalds /* Fall through */ 58341da177e4SLinus Torvalds case TCP_ESTABLISHED: 58351da177e4SLinus Torvalds tcp_data_queue(sk, skb); 58361da177e4SLinus Torvalds queued = 1; 58371da177e4SLinus Torvalds break; 58381da177e4SLinus Torvalds } 58391da177e4SLinus Torvalds 58401da177e4SLinus Torvalds /* tcp_data could move socket to TIME-WAIT */ 58411da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE) { 58429e412ba7SIlpo Järvinen tcp_data_snd_check(sk); 58431da177e4SLinus Torvalds tcp_ack_snd_check(sk); 58441da177e4SLinus Torvalds } 58451da177e4SLinus Torvalds 58461da177e4SLinus Torvalds if (!queued) { 58471da177e4SLinus Torvalds discard: 58481da177e4SLinus Torvalds __kfree_skb(skb); 58491da177e4SLinus Torvalds } 58501da177e4SLinus Torvalds return 0; 58511da177e4SLinus Torvalds } 58521da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_rcv_state_process); 58531fb6f159SOctavian Purdila 58541fb6f159SOctavian Purdila static inline void pr_drop_req(struct request_sock *req, __u16 port, int family) 58551fb6f159SOctavian Purdila { 58561fb6f159SOctavian Purdila struct inet_request_sock *ireq = inet_rsk(req); 58571fb6f159SOctavian Purdila 58581fb6f159SOctavian Purdila if (family == AF_INET) 5859ba7a46f1SJoe Perches net_dbg_ratelimited("drop open request from %pI4/%u\n", 58601fb6f159SOctavian Purdila &ireq->ir_rmt_addr, port); 58614135ab82SOctavian Purdila #if IS_ENABLED(CONFIG_IPV6) 58624135ab82SOctavian Purdila else if (family == AF_INET6) 5863ba7a46f1SJoe Perches net_dbg_ratelimited("drop open request from %pI6/%u\n", 58641fb6f159SOctavian Purdila &ireq->ir_v6_rmt_addr, port); 58654135ab82SOctavian Purdila #endif 58661fb6f159SOctavian Purdila } 58671fb6f159SOctavian Purdila 5868d82bd122SFlorian Westphal /* RFC3168 : 6.1.1 SYN packets must not have ECT/ECN bits set 5869d82bd122SFlorian Westphal * 5870d82bd122SFlorian Westphal * If we receive a SYN packet with these bits set, it means a 5871d82bd122SFlorian Westphal * network is playing bad games with TOS bits. In order to 5872d82bd122SFlorian Westphal * avoid possible false congestion notifications, we disable 5873f4e715c3Sstephen hemminger * TCP ECN negotiation. 5874d82bd122SFlorian Westphal * 5875d82bd122SFlorian Westphal * Exception: tcp_ca wants ECN. This is required for DCTCP 5876843c2fdfSFlorian Westphal * congestion control: Linux DCTCP asserts ECT on all packets, 5877843c2fdfSFlorian Westphal * including SYN, which is most optimal solution; however, 5878843c2fdfSFlorian Westphal * others, such as FreeBSD do not. 5879d82bd122SFlorian Westphal */ 5880d82bd122SFlorian Westphal static void tcp_ecn_create_request(struct request_sock *req, 5881d82bd122SFlorian Westphal const struct sk_buff *skb, 5882f7b3bec6SFlorian Westphal const struct sock *listen_sk, 5883f7b3bec6SFlorian Westphal const struct dst_entry *dst) 5884d82bd122SFlorian Westphal { 5885d82bd122SFlorian Westphal const struct tcphdr *th = tcp_hdr(skb); 5886d82bd122SFlorian Westphal const struct net *net = sock_net(listen_sk); 5887d82bd122SFlorian Westphal bool th_ecn = th->ece && th->cwr; 5888843c2fdfSFlorian Westphal bool ect, ecn_ok; 5889d82bd122SFlorian Westphal 5890d82bd122SFlorian Westphal if (!th_ecn) 5891d82bd122SFlorian Westphal return; 5892d82bd122SFlorian Westphal 5893d82bd122SFlorian Westphal ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield); 5894f7b3bec6SFlorian Westphal ecn_ok = net->ipv4.sysctl_tcp_ecn || dst_feature(dst, RTAX_FEATURE_ECN); 5895d82bd122SFlorian Westphal 5896843c2fdfSFlorian Westphal if ((!ect && ecn_ok) || tcp_ca_needs_ecn(listen_sk)) 5897d82bd122SFlorian Westphal inet_rsk(req)->ecn_ok = 1; 5898d82bd122SFlorian Westphal } 5899d82bd122SFlorian Westphal 59001fb6f159SOctavian Purdila int tcp_conn_request(struct request_sock_ops *rsk_ops, 59011fb6f159SOctavian Purdila const struct tcp_request_sock_ops *af_ops, 59021fb6f159SOctavian Purdila struct sock *sk, struct sk_buff *skb) 59031fb6f159SOctavian Purdila { 59041fb6f159SOctavian Purdila struct tcp_options_received tmp_opt; 59051fb6f159SOctavian Purdila struct request_sock *req; 59061fb6f159SOctavian Purdila struct tcp_sock *tp = tcp_sk(sk); 59071fb6f159SOctavian Purdila struct dst_entry *dst = NULL; 590804317dafSEric Dumazet __u32 isn = TCP_SKB_CB(skb)->tcp_tw_isn; 59091fb6f159SOctavian Purdila bool want_cookie = false, fastopen; 59101fb6f159SOctavian Purdila struct flowi fl; 59111fb6f159SOctavian Purdila struct tcp_fastopen_cookie foc = { .len = -1 }; 59121fb6f159SOctavian Purdila int err; 59131fb6f159SOctavian Purdila 59141fb6f159SOctavian Purdila 59151fb6f159SOctavian Purdila /* TW buckets are converted to open requests without 59161fb6f159SOctavian Purdila * limitations, they conserve resources and peer is 59171fb6f159SOctavian Purdila * evidently real one. 59181fb6f159SOctavian Purdila */ 59191fb6f159SOctavian Purdila if ((sysctl_tcp_syncookies == 2 || 59201fb6f159SOctavian Purdila inet_csk_reqsk_queue_is_full(sk)) && !isn) { 59211fb6f159SOctavian Purdila want_cookie = tcp_syn_flood_action(sk, skb, rsk_ops->slab_name); 59221fb6f159SOctavian Purdila if (!want_cookie) 59231fb6f159SOctavian Purdila goto drop; 59241fb6f159SOctavian Purdila } 59251fb6f159SOctavian Purdila 59261fb6f159SOctavian Purdila 59271fb6f159SOctavian Purdila /* Accept backlog is full. If we have already queued enough 59281fb6f159SOctavian Purdila * of warm entries in syn queue, drop request. It is better than 59291fb6f159SOctavian Purdila * clogging syn queue with openreqs with exponentially increasing 59301fb6f159SOctavian Purdila * timeout. 59311fb6f159SOctavian Purdila */ 59321fb6f159SOctavian Purdila if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) { 59331fb6f159SOctavian Purdila NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS); 59341fb6f159SOctavian Purdila goto drop; 59351fb6f159SOctavian Purdila } 59361fb6f159SOctavian Purdila 59371fb6f159SOctavian Purdila req = inet_reqsk_alloc(rsk_ops); 59381fb6f159SOctavian Purdila if (!req) 59391fb6f159SOctavian Purdila goto drop; 59401fb6f159SOctavian Purdila 59411fb6f159SOctavian Purdila tcp_rsk(req)->af_specific = af_ops; 59421fb6f159SOctavian Purdila 59431fb6f159SOctavian Purdila tcp_clear_options(&tmp_opt); 59441fb6f159SOctavian Purdila tmp_opt.mss_clamp = af_ops->mss_clamp; 59451fb6f159SOctavian Purdila tmp_opt.user_mss = tp->rx_opt.user_mss; 59461fb6f159SOctavian Purdila tcp_parse_options(skb, &tmp_opt, 0, want_cookie ? NULL : &foc); 59471fb6f159SOctavian Purdila 59481fb6f159SOctavian Purdila if (want_cookie && !tmp_opt.saw_tstamp) 59491fb6f159SOctavian Purdila tcp_clear_options(&tmp_opt); 59501fb6f159SOctavian Purdila 59511fb6f159SOctavian Purdila tmp_opt.tstamp_ok = tmp_opt.saw_tstamp; 59521fb6f159SOctavian Purdila tcp_openreq_init(req, &tmp_opt, skb, sk); 59531fb6f159SOctavian Purdila 59541fb6f159SOctavian Purdila af_ops->init_req(req, sk, skb); 59551fb6f159SOctavian Purdila 59561fb6f159SOctavian Purdila if (security_inet_conn_request(sk, skb, req)) 59571fb6f159SOctavian Purdila goto drop_and_free; 59581fb6f159SOctavian Purdila 5959f7b3bec6SFlorian Westphal if (!want_cookie && !isn) { 59601fb6f159SOctavian Purdila /* VJ's idea. We save last timestamp seen 59611fb6f159SOctavian Purdila * from the destination in peer table, when entering 59621fb6f159SOctavian Purdila * state TIME-WAIT, and check against it before 59631fb6f159SOctavian Purdila * accepting new connection request. 59641fb6f159SOctavian Purdila * 59651fb6f159SOctavian Purdila * If "isn" is not zero, this request hit alive 59661fb6f159SOctavian Purdila * timewait bucket, so that all the necessary checks 59671fb6f159SOctavian Purdila * are made in the function processing timewait state. 59681fb6f159SOctavian Purdila */ 5969a26552afSHannes Frederic Sowa if (tcp_death_row.sysctl_tw_recycle) { 59701fb6f159SOctavian Purdila bool strict; 59711fb6f159SOctavian Purdila 59721fb6f159SOctavian Purdila dst = af_ops->route_req(sk, &fl, req, &strict); 5973a26552afSHannes Frederic Sowa 59741fb6f159SOctavian Purdila if (dst && strict && 5975a26552afSHannes Frederic Sowa !tcp_peer_is_proven(req, dst, true, 5976a26552afSHannes Frederic Sowa tmp_opt.saw_tstamp)) { 59771fb6f159SOctavian Purdila NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED); 59781fb6f159SOctavian Purdila goto drop_and_release; 59791fb6f159SOctavian Purdila } 59801fb6f159SOctavian Purdila } 59811fb6f159SOctavian Purdila /* Kill the following clause, if you dislike this way. */ 59821fb6f159SOctavian Purdila else if (!sysctl_tcp_syncookies && 59831fb6f159SOctavian Purdila (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) < 59841fb6f159SOctavian Purdila (sysctl_max_syn_backlog >> 2)) && 5985a26552afSHannes Frederic Sowa !tcp_peer_is_proven(req, dst, false, 5986a26552afSHannes Frederic Sowa tmp_opt.saw_tstamp)) { 59871fb6f159SOctavian Purdila /* Without syncookies last quarter of 59881fb6f159SOctavian Purdila * backlog is filled with destinations, 59891fb6f159SOctavian Purdila * proven to be alive. 59901fb6f159SOctavian Purdila * It means that we continue to communicate 59911fb6f159SOctavian Purdila * to destinations, already remembered 59921fb6f159SOctavian Purdila * to the moment of synflood. 59931fb6f159SOctavian Purdila */ 59941fb6f159SOctavian Purdila pr_drop_req(req, ntohs(tcp_hdr(skb)->source), 59951fb6f159SOctavian Purdila rsk_ops->family); 59961fb6f159SOctavian Purdila goto drop_and_release; 59971fb6f159SOctavian Purdila } 59981fb6f159SOctavian Purdila 59991fb6f159SOctavian Purdila isn = af_ops->init_seq(skb); 60001fb6f159SOctavian Purdila } 60011fb6f159SOctavian Purdila if (!dst) { 60021fb6f159SOctavian Purdila dst = af_ops->route_req(sk, &fl, req, NULL); 60031fb6f159SOctavian Purdila if (!dst) 60041fb6f159SOctavian Purdila goto drop_and_free; 60051fb6f159SOctavian Purdila } 60061fb6f159SOctavian Purdila 6007f7b3bec6SFlorian Westphal tcp_ecn_create_request(req, skb, sk, dst); 6008f7b3bec6SFlorian Westphal 6009f7b3bec6SFlorian Westphal if (want_cookie) { 6010f7b3bec6SFlorian Westphal isn = cookie_init_sequence(af_ops, sk, skb, &req->mss); 6011f7b3bec6SFlorian Westphal req->cookie_ts = tmp_opt.tstamp_ok; 6012f7b3bec6SFlorian Westphal if (!tmp_opt.tstamp_ok) 6013f7b3bec6SFlorian Westphal inet_rsk(req)->ecn_ok = 0; 6014f7b3bec6SFlorian Westphal } 6015f7b3bec6SFlorian Westphal 60161fb6f159SOctavian Purdila tcp_rsk(req)->snt_isn = isn; 60171fb6f159SOctavian Purdila tcp_openreq_init_rwin(req, sk, dst); 60181fb6f159SOctavian Purdila fastopen = !want_cookie && 60191fb6f159SOctavian Purdila tcp_try_fastopen(sk, skb, req, &foc, dst); 60201fb6f159SOctavian Purdila err = af_ops->send_synack(sk, dst, &fl, req, 60211fb6f159SOctavian Purdila skb_get_queue_mapping(skb), &foc); 60221fb6f159SOctavian Purdila if (!fastopen) { 60231fb6f159SOctavian Purdila if (err || want_cookie) 60241fb6f159SOctavian Purdila goto drop_and_free; 60251fb6f159SOctavian Purdila 60261fb6f159SOctavian Purdila tcp_rsk(req)->listener = NULL; 60271fb6f159SOctavian Purdila af_ops->queue_hash_add(sk, req, TCP_TIMEOUT_INIT); 60281fb6f159SOctavian Purdila } 60291fb6f159SOctavian Purdila 60301fb6f159SOctavian Purdila return 0; 60311fb6f159SOctavian Purdila 60321fb6f159SOctavian Purdila drop_and_release: 60331fb6f159SOctavian Purdila dst_release(dst); 60341fb6f159SOctavian Purdila drop_and_free: 60351fb6f159SOctavian Purdila reqsk_free(req); 60361fb6f159SOctavian Purdila drop: 60371fb6f159SOctavian Purdila NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS); 60381fb6f159SOctavian Purdila return 0; 60391fb6f159SOctavian Purdila } 60401fb6f159SOctavian Purdila EXPORT_SYMBOL(tcp_conn_request); 6041