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 * 81da177e4SLinus Torvalds * Version: $Id: tcp_input.c,v 1.243 2002/02/01 22:01:04 davem Exp $ 91da177e4SLinus Torvalds * 1002c30a84SJesper Juhl * Authors: Ross Biro 111da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 121da177e4SLinus Torvalds * Mark Evans, <evansmp@uhura.aston.ac.uk> 131da177e4SLinus Torvalds * Corey Minyard <wf-rch!minyard@relay.EU.net> 141da177e4SLinus Torvalds * Florian La Roche, <flla@stud.uni-sb.de> 151da177e4SLinus Torvalds * Charles Hedrick, <hedrick@klinzhai.rutgers.edu> 161da177e4SLinus Torvalds * Linus Torvalds, <torvalds@cs.helsinki.fi> 171da177e4SLinus Torvalds * Alan Cox, <gw4pts@gw4pts.ampr.org> 181da177e4SLinus Torvalds * Matthew Dillon, <dillon@apollo.west.oic.com> 191da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 201da177e4SLinus Torvalds * Jorge Cwik, <jorge@laser.satlink.net> 211da177e4SLinus Torvalds */ 221da177e4SLinus Torvalds 231da177e4SLinus Torvalds /* 241da177e4SLinus Torvalds * Changes: 251da177e4SLinus Torvalds * Pedro Roque : Fast Retransmit/Recovery. 261da177e4SLinus Torvalds * Two receive queues. 271da177e4SLinus Torvalds * Retransmit queue handled by TCP. 281da177e4SLinus Torvalds * Better retransmit timer handling. 291da177e4SLinus Torvalds * New congestion avoidance. 301da177e4SLinus Torvalds * Header prediction. 311da177e4SLinus Torvalds * Variable renaming. 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * Eric : Fast Retransmit. 341da177e4SLinus Torvalds * Randy Scott : MSS option defines. 351da177e4SLinus Torvalds * Eric Schenk : Fixes to slow start algorithm. 361da177e4SLinus Torvalds * Eric Schenk : Yet another double ACK bug. 371da177e4SLinus Torvalds * Eric Schenk : Delayed ACK bug fixes. 381da177e4SLinus Torvalds * Eric Schenk : Floyd style fast retrans war avoidance. 391da177e4SLinus Torvalds * David S. Miller : Don't allow zero congestion window. 401da177e4SLinus Torvalds * Eric Schenk : Fix retransmitter so that it sends 411da177e4SLinus Torvalds * next packet on ack of previous packet. 421da177e4SLinus Torvalds * Andi Kleen : Moved open_request checking here 431da177e4SLinus Torvalds * and process RSTs for open_requests. 441da177e4SLinus Torvalds * Andi Kleen : Better prune_queue, and other fixes. 45caa20d9aSStephen Hemminger * Andrey Savochkin: Fix RTT measurements in the presence of 461da177e4SLinus Torvalds * timestamps. 471da177e4SLinus Torvalds * Andrey Savochkin: Check sequence numbers correctly when 481da177e4SLinus Torvalds * removing SACKs due to in sequence incoming 491da177e4SLinus Torvalds * data segments. 501da177e4SLinus Torvalds * Andi Kleen: Make sure we never ack data there is not 511da177e4SLinus Torvalds * enough room for. Also make this condition 521da177e4SLinus Torvalds * a fatal error if it might still happen. 531da177e4SLinus Torvalds * Andi Kleen: Add tcp_measure_rcv_mss to make 541da177e4SLinus Torvalds * connections with MSS<min(MTU,ann. MSS) 551da177e4SLinus Torvalds * work without delayed acks. 561da177e4SLinus Torvalds * Andi Kleen: Process packets with PSH set in the 571da177e4SLinus Torvalds * fast path. 581da177e4SLinus Torvalds * J Hadi Salim: ECN support 591da177e4SLinus Torvalds * Andrei Gurtov, 601da177e4SLinus Torvalds * Pasi Sarolahti, 611da177e4SLinus Torvalds * Panu Kuhlberg: Experimental audit of TCP (re)transmission 621da177e4SLinus Torvalds * engine. Lots of bugs are found. 631da177e4SLinus Torvalds * Pasi Sarolahti: F-RTO for dealing with spurious RTOs 641da177e4SLinus Torvalds */ 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds #include <linux/mm.h> 671da177e4SLinus Torvalds #include <linux/module.h> 681da177e4SLinus Torvalds #include <linux/sysctl.h> 691da177e4SLinus Torvalds #include <net/tcp.h> 701da177e4SLinus Torvalds #include <net/inet_common.h> 711da177e4SLinus Torvalds #include <linux/ipsec.h> 721da177e4SLinus Torvalds #include <asm/unaligned.h> 731a2449a8SChris Leech #include <net/netdma.h> 741da177e4SLinus Torvalds 75ab32ea5dSBrian Haley int sysctl_tcp_timestamps __read_mostly = 1; 76ab32ea5dSBrian Haley int sysctl_tcp_window_scaling __read_mostly = 1; 77ab32ea5dSBrian Haley int sysctl_tcp_sack __read_mostly = 1; 78ab32ea5dSBrian Haley int sysctl_tcp_fack __read_mostly = 1; 79ab32ea5dSBrian Haley int sysctl_tcp_reordering __read_mostly = TCP_FASTRETRANS_THRESH; 80ab32ea5dSBrian Haley int sysctl_tcp_ecn __read_mostly; 81ab32ea5dSBrian Haley int sysctl_tcp_dsack __read_mostly = 1; 82ab32ea5dSBrian Haley int sysctl_tcp_app_win __read_mostly = 31; 83ab32ea5dSBrian Haley int sysctl_tcp_adv_win_scale __read_mostly = 2; 841da177e4SLinus Torvalds 85ab32ea5dSBrian Haley int sysctl_tcp_stdurg __read_mostly; 86ab32ea5dSBrian Haley int sysctl_tcp_rfc1337 __read_mostly; 87ab32ea5dSBrian Haley int sysctl_tcp_max_orphans __read_mostly = NR_FILE; 88c96fd3d4SIlpo Järvinen int sysctl_tcp_frto __read_mostly = 2; 893cfe3baaSIlpo Järvinen int sysctl_tcp_frto_response __read_mostly; 90ab32ea5dSBrian Haley int sysctl_tcp_nometrics_save __read_mostly; 911da177e4SLinus Torvalds 92ab32ea5dSBrian Haley int sysctl_tcp_moderate_rcvbuf __read_mostly = 1; 93ab32ea5dSBrian Haley int sysctl_tcp_abc __read_mostly; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds #define FLAG_DATA 0x01 /* Incoming frame contained data. */ 961da177e4SLinus Torvalds #define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */ 971da177e4SLinus Torvalds #define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */ 981da177e4SLinus Torvalds #define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */ 991da177e4SLinus Torvalds #define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */ 1001da177e4SLinus Torvalds #define FLAG_DATA_SACKED 0x20 /* New SACK. */ 1011da177e4SLinus Torvalds #define FLAG_ECE 0x40 /* ECE in this ACK */ 1021da177e4SLinus Torvalds #define FLAG_DATA_LOST 0x80 /* SACK detected data lossage. */ 1031da177e4SLinus Torvalds #define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/ 1044dc2665eSIlpo Järvinen #define FLAG_ONLY_ORIG_SACKED 0x200 /* SACKs only non-rexmit sent before RTO */ 1052e605294SIlpo Järvinen #define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */ 10649ff4bb4SIlpo Järvinen #define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained DSACK info */ 107009a2e3eSIlpo Järvinen #define FLAG_NONHEAD_RETRANS_ACKED 0x1000 /* Non-head rexmitted data was ACKed */ 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds #define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED) 1101da177e4SLinus Torvalds #define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED) 1111da177e4SLinus Torvalds #define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE) 1121da177e4SLinus Torvalds #define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED) 1132e605294SIlpo Järvinen #define FLAG_ANY_PROGRESS (FLAG_FORWARD_PROGRESS|FLAG_SND_UNA_ADVANCED) 1141da177e4SLinus Torvalds 1154dc2665eSIlpo Järvinen #define IsSackFrto() (sysctl_tcp_frto == 0x2) 1164dc2665eSIlpo Järvinen 1171da177e4SLinus Torvalds #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH) 118bdf1ee5dSIlpo Järvinen #define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH)) 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* Adapt the MSS value used to make delayed ack decision to the 1211da177e4SLinus Torvalds * real world. 1221da177e4SLinus Torvalds */ 12340efc6faSStephen Hemminger static void tcp_measure_rcv_mss(struct sock *sk, 124463c84b9SArnaldo Carvalho de Melo const struct sk_buff *skb) 1251da177e4SLinus Torvalds { 126463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 127463c84b9SArnaldo Carvalho de Melo const unsigned int lss = icsk->icsk_ack.last_seg_size; 128463c84b9SArnaldo Carvalho de Melo unsigned int len; 1291da177e4SLinus Torvalds 130463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.last_seg_size = 0; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* skb->len may jitter because of SACKs, even if peer 1331da177e4SLinus Torvalds * sends good full-sized frames. 1341da177e4SLinus Torvalds */ 135ff9b5e0fSHerbert Xu len = skb_shinfo(skb)->gso_size ?: skb->len; 136463c84b9SArnaldo Carvalho de Melo if (len >= icsk->icsk_ack.rcv_mss) { 137463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.rcv_mss = len; 1381da177e4SLinus Torvalds } else { 1391da177e4SLinus Torvalds /* Otherwise, we make more careful check taking into account, 1401da177e4SLinus Torvalds * that SACKs block is variable. 1411da177e4SLinus Torvalds * 1421da177e4SLinus Torvalds * "len" is invariant segment length, including TCP header. 1431da177e4SLinus Torvalds */ 1449c70220bSArnaldo Carvalho de Melo len += skb->data - skb_transport_header(skb); 1451da177e4SLinus Torvalds if (len >= TCP_MIN_RCVMSS + sizeof(struct tcphdr) || 1461da177e4SLinus Torvalds /* If PSH is not set, packet should be 1471da177e4SLinus Torvalds * full sized, provided peer TCP is not badly broken. 1481da177e4SLinus Torvalds * This observation (if it is correct 8)) allows 1491da177e4SLinus Torvalds * to handle super-low mtu links fairly. 1501da177e4SLinus Torvalds */ 1511da177e4SLinus Torvalds (len >= TCP_MIN_MSS + sizeof(struct tcphdr) && 152aa8223c7SArnaldo Carvalho de Melo !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) { 1531da177e4SLinus Torvalds /* Subtract also invariant (if peer is RFC compliant), 1541da177e4SLinus Torvalds * tcp header plus fixed timestamp option length. 1551da177e4SLinus Torvalds * Resulting "len" is MSS free of SACK jitter. 1561da177e4SLinus Torvalds */ 157463c84b9SArnaldo Carvalho de Melo len -= tcp_sk(sk)->tcp_header_len; 158463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.last_seg_size = len; 1591da177e4SLinus Torvalds if (len == lss) { 160463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.rcv_mss = len; 1611da177e4SLinus Torvalds return; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds } 1641ef9696cSAlexey Kuznetsov if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED) 1651ef9696cSAlexey Kuznetsov icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2; 166463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.pending |= ICSK_ACK_PUSHED; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 170463c84b9SArnaldo Carvalho de Melo static void tcp_incr_quickack(struct sock *sk) 1711da177e4SLinus Torvalds { 172463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 173463c84b9SArnaldo Carvalho de Melo unsigned quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss); 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds if (quickacks==0) 1761da177e4SLinus Torvalds quickacks=2; 177463c84b9SArnaldo Carvalho de Melo if (quickacks > icsk->icsk_ack.quick) 178463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS); 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 181463c84b9SArnaldo Carvalho de Melo void tcp_enter_quickack_mode(struct sock *sk) 1821da177e4SLinus Torvalds { 183463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 184463c84b9SArnaldo Carvalho de Melo tcp_incr_quickack(sk); 185463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.pingpong = 0; 186463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* Send ACKs quickly, if "quick" count is not exhausted 1901da177e4SLinus Torvalds * and the session is not interactive. 1911da177e4SLinus Torvalds */ 1921da177e4SLinus Torvalds 193463c84b9SArnaldo Carvalho de Melo static inline int tcp_in_quickack_mode(const struct sock *sk) 1941da177e4SLinus Torvalds { 195463c84b9SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 196463c84b9SArnaldo Carvalho de Melo return icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 199bdf1ee5dSIlpo Järvinen static inline void TCP_ECN_queue_cwr(struct tcp_sock *tp) 200bdf1ee5dSIlpo Järvinen { 201bdf1ee5dSIlpo Järvinen if (tp->ecn_flags&TCP_ECN_OK) 202bdf1ee5dSIlpo Järvinen tp->ecn_flags |= TCP_ECN_QUEUE_CWR; 203bdf1ee5dSIlpo Järvinen } 204bdf1ee5dSIlpo Järvinen 205bdf1ee5dSIlpo Järvinen static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, struct sk_buff *skb) 206bdf1ee5dSIlpo Järvinen { 207bdf1ee5dSIlpo Järvinen if (tcp_hdr(skb)->cwr) 208bdf1ee5dSIlpo Järvinen tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR; 209bdf1ee5dSIlpo Järvinen } 210bdf1ee5dSIlpo Järvinen 211bdf1ee5dSIlpo Järvinen static inline void TCP_ECN_withdraw_cwr(struct tcp_sock *tp) 212bdf1ee5dSIlpo Järvinen { 213bdf1ee5dSIlpo Järvinen tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR; 214bdf1ee5dSIlpo Järvinen } 215bdf1ee5dSIlpo Järvinen 216bdf1ee5dSIlpo Järvinen static inline void TCP_ECN_check_ce(struct tcp_sock *tp, struct sk_buff *skb) 217bdf1ee5dSIlpo Järvinen { 218bdf1ee5dSIlpo Järvinen if (tp->ecn_flags&TCP_ECN_OK) { 219bdf1ee5dSIlpo Järvinen if (INET_ECN_is_ce(TCP_SKB_CB(skb)->flags)) 220bdf1ee5dSIlpo Järvinen tp->ecn_flags |= TCP_ECN_DEMAND_CWR; 221bdf1ee5dSIlpo Järvinen /* Funny extension: if ECT is not set on a segment, 222bdf1ee5dSIlpo Järvinen * it is surely retransmit. It is not in ECN RFC, 223bdf1ee5dSIlpo Järvinen * but Linux follows this rule. */ 224bdf1ee5dSIlpo Järvinen else if (INET_ECN_is_not_ect((TCP_SKB_CB(skb)->flags))) 225bdf1ee5dSIlpo Järvinen tcp_enter_quickack_mode((struct sock *)tp); 226bdf1ee5dSIlpo Järvinen } 227bdf1ee5dSIlpo Järvinen } 228bdf1ee5dSIlpo Järvinen 229bdf1ee5dSIlpo Järvinen static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, struct tcphdr *th) 230bdf1ee5dSIlpo Järvinen { 231bdf1ee5dSIlpo Järvinen if ((tp->ecn_flags&TCP_ECN_OK) && (!th->ece || th->cwr)) 232bdf1ee5dSIlpo Järvinen tp->ecn_flags &= ~TCP_ECN_OK; 233bdf1ee5dSIlpo Järvinen } 234bdf1ee5dSIlpo Järvinen 235bdf1ee5dSIlpo Järvinen static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, struct tcphdr *th) 236bdf1ee5dSIlpo Järvinen { 237bdf1ee5dSIlpo Järvinen if ((tp->ecn_flags&TCP_ECN_OK) && (!th->ece || !th->cwr)) 238bdf1ee5dSIlpo Järvinen tp->ecn_flags &= ~TCP_ECN_OK; 239bdf1ee5dSIlpo Järvinen } 240bdf1ee5dSIlpo Järvinen 241bdf1ee5dSIlpo Järvinen static inline int TCP_ECN_rcv_ecn_echo(struct tcp_sock *tp, struct tcphdr *th) 242bdf1ee5dSIlpo Järvinen { 243bdf1ee5dSIlpo Järvinen if (th->ece && !th->syn && (tp->ecn_flags&TCP_ECN_OK)) 244bdf1ee5dSIlpo Järvinen return 1; 245bdf1ee5dSIlpo Järvinen return 0; 246bdf1ee5dSIlpo Järvinen } 247bdf1ee5dSIlpo Järvinen 2481da177e4SLinus Torvalds /* Buffer size and advertised window tuning. 2491da177e4SLinus Torvalds * 2501da177e4SLinus Torvalds * 1. Tuning sk->sk_sndbuf, when connection enters established state. 2511da177e4SLinus Torvalds */ 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds static void tcp_fixup_sndbuf(struct sock *sk) 2541da177e4SLinus Torvalds { 2551da177e4SLinus Torvalds int sndmem = tcp_sk(sk)->rx_opt.mss_clamp + MAX_TCP_HEADER + 16 + 2561da177e4SLinus Torvalds sizeof(struct sk_buff); 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds if (sk->sk_sndbuf < 3 * sndmem) 2591da177e4SLinus Torvalds sk->sk_sndbuf = min(3 * sndmem, sysctl_tcp_wmem[2]); 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds /* 2. Tuning advertised window (window_clamp, rcv_ssthresh) 2631da177e4SLinus Torvalds * 2641da177e4SLinus Torvalds * All tcp_full_space() is split to two parts: "network" buffer, allocated 2651da177e4SLinus Torvalds * forward and advertised in receiver window (tp->rcv_wnd) and 2661da177e4SLinus Torvalds * "application buffer", required to isolate scheduling/application 2671da177e4SLinus Torvalds * latencies from network. 2681da177e4SLinus Torvalds * window_clamp is maximal advertised window. It can be less than 2691da177e4SLinus Torvalds * tcp_full_space(), in this case tcp_full_space() - window_clamp 2701da177e4SLinus Torvalds * is reserved for "application" buffer. The less window_clamp is 2711da177e4SLinus Torvalds * the smoother our behaviour from viewpoint of network, but the lower 2721da177e4SLinus Torvalds * throughput and the higher sensitivity of the connection to losses. 8) 2731da177e4SLinus Torvalds * 2741da177e4SLinus Torvalds * rcv_ssthresh is more strict window_clamp used at "slow start" 2751da177e4SLinus Torvalds * phase to predict further behaviour of this connection. 2761da177e4SLinus Torvalds * It is used for two goals: 2771da177e4SLinus Torvalds * - to enforce header prediction at sender, even when application 2781da177e4SLinus Torvalds * requires some significant "application buffer". It is check #1. 2791da177e4SLinus Torvalds * - to prevent pruning of receive queue because of misprediction 2801da177e4SLinus Torvalds * of receiver window. Check #2. 2811da177e4SLinus Torvalds * 2821da177e4SLinus Torvalds * The scheme does not work when sender sends good segments opening 283caa20d9aSStephen Hemminger * window and then starts to feed us spaghetti. But it should work 2841da177e4SLinus Torvalds * in common situations. Otherwise, we have to rely on queue collapsing. 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds /* Slow part of check#2. */ 2889e412ba7SIlpo Järvinen static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb) 2891da177e4SLinus Torvalds { 2909e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 2911da177e4SLinus Torvalds /* Optimize this! */ 2921da177e4SLinus Torvalds int truesize = tcp_win_from_space(skb->truesize)/2; 293326f36e9SJohn Heffner int window = tcp_win_from_space(sysctl_tcp_rmem[2])/2; 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds while (tp->rcv_ssthresh <= window) { 2961da177e4SLinus Torvalds if (truesize <= skb->len) 297463c84b9SArnaldo Carvalho de Melo return 2 * inet_csk(sk)->icsk_ack.rcv_mss; 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds truesize >>= 1; 3001da177e4SLinus Torvalds window >>= 1; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds return 0; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3059e412ba7SIlpo Järvinen static void tcp_grow_window(struct sock *sk, 3061da177e4SLinus Torvalds struct sk_buff *skb) 3071da177e4SLinus Torvalds { 3089e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 3099e412ba7SIlpo Järvinen 3101da177e4SLinus Torvalds /* Check #1 */ 3111da177e4SLinus Torvalds if (tp->rcv_ssthresh < tp->window_clamp && 3121da177e4SLinus Torvalds (int)tp->rcv_ssthresh < tcp_space(sk) && 3131da177e4SLinus Torvalds !tcp_memory_pressure) { 3141da177e4SLinus Torvalds int incr; 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds /* Check #2. Increase window, if skb with such overhead 3171da177e4SLinus Torvalds * will fit to rcvbuf in future. 3181da177e4SLinus Torvalds */ 3191da177e4SLinus Torvalds if (tcp_win_from_space(skb->truesize) <= skb->len) 3201da177e4SLinus Torvalds incr = 2*tp->advmss; 3211da177e4SLinus Torvalds else 3229e412ba7SIlpo Järvinen incr = __tcp_grow_window(sk, skb); 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds if (incr) { 3251da177e4SLinus Torvalds tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr, tp->window_clamp); 326463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_ack.quick |= 1; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* 3. Tuning rcvbuf, when connection enters established state. */ 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds static void tcp_fixup_rcvbuf(struct sock *sk) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 3361da177e4SLinus Torvalds int rcvmem = tp->advmss + MAX_TCP_HEADER + 16 + sizeof(struct sk_buff); 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds /* Try to select rcvbuf so that 4 mss-sized segments 339caa20d9aSStephen Hemminger * will fit to window and corresponding skbs will fit to our rcvbuf. 3401da177e4SLinus Torvalds * (was 3; 4 is minimum to allow fast retransmit to work.) 3411da177e4SLinus Torvalds */ 3421da177e4SLinus Torvalds while (tcp_win_from_space(rcvmem) < tp->advmss) 3431da177e4SLinus Torvalds rcvmem += 128; 3441da177e4SLinus Torvalds if (sk->sk_rcvbuf < 4 * rcvmem) 3451da177e4SLinus Torvalds sk->sk_rcvbuf = min(4 * rcvmem, sysctl_tcp_rmem[2]); 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 348caa20d9aSStephen Hemminger /* 4. Try to fixup all. It is made immediately after connection enters 3491da177e4SLinus Torvalds * established state. 3501da177e4SLinus Torvalds */ 3511da177e4SLinus Torvalds static void tcp_init_buffer_space(struct sock *sk) 3521da177e4SLinus Torvalds { 3531da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 3541da177e4SLinus Torvalds int maxwin; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) 3571da177e4SLinus Torvalds tcp_fixup_rcvbuf(sk); 3581da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) 3591da177e4SLinus Torvalds tcp_fixup_sndbuf(sk); 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds tp->rcvq_space.space = tp->rcv_wnd; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds maxwin = tcp_full_space(sk); 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds if (tp->window_clamp >= maxwin) { 3661da177e4SLinus Torvalds tp->window_clamp = maxwin; 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss) 3691da177e4SLinus Torvalds tp->window_clamp = max(maxwin - 3701da177e4SLinus Torvalds (maxwin >> sysctl_tcp_app_win), 3711da177e4SLinus Torvalds 4 * tp->advmss); 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds /* Force reservation of one segment. */ 3751da177e4SLinus Torvalds if (sysctl_tcp_app_win && 3761da177e4SLinus Torvalds tp->window_clamp > 2 * tp->advmss && 3771da177e4SLinus Torvalds tp->window_clamp + tp->advmss > maxwin) 3781da177e4SLinus Torvalds tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss); 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp); 3811da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds /* 5. Recalculate window clamp after socket hit its memory bounds. */ 3859e412ba7SIlpo Järvinen static void tcp_clamp_window(struct sock *sk) 3861da177e4SLinus Torvalds { 3879e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 3886687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 3891da177e4SLinus Torvalds 3906687e988SArnaldo Carvalho de Melo icsk->icsk_ack.quick = 0; 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] && 3931da177e4SLinus Torvalds !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) && 3941da177e4SLinus Torvalds !tcp_memory_pressure && 395326f36e9SJohn Heffner atomic_read(&tcp_memory_allocated) < sysctl_tcp_mem[0]) { 3961da177e4SLinus Torvalds sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc), 3971da177e4SLinus Torvalds sysctl_tcp_rmem[2]); 3981da177e4SLinus Torvalds } 399326f36e9SJohn Heffner if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf) 4001da177e4SLinus Torvalds tp->rcv_ssthresh = min(tp->window_clamp, 2U*tp->advmss); 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 40340efc6faSStephen Hemminger 40440efc6faSStephen Hemminger /* Initialize RCV_MSS value. 40540efc6faSStephen Hemminger * RCV_MSS is an our guess about MSS used by the peer. 40640efc6faSStephen Hemminger * We haven't any direct information about the MSS. 40740efc6faSStephen Hemminger * It's better to underestimate the RCV_MSS rather than overestimate. 40840efc6faSStephen Hemminger * Overestimations make us ACKing less frequently than needed. 40940efc6faSStephen Hemminger * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss(). 41040efc6faSStephen Hemminger */ 41140efc6faSStephen Hemminger void tcp_initialize_rcv_mss(struct sock *sk) 41240efc6faSStephen Hemminger { 41340efc6faSStephen Hemminger struct tcp_sock *tp = tcp_sk(sk); 41440efc6faSStephen Hemminger unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache); 41540efc6faSStephen Hemminger 41640efc6faSStephen Hemminger hint = min(hint, tp->rcv_wnd/2); 41740efc6faSStephen Hemminger hint = min(hint, TCP_MIN_RCVMSS); 41840efc6faSStephen Hemminger hint = max(hint, TCP_MIN_MSS); 41940efc6faSStephen Hemminger 42040efc6faSStephen Hemminger inet_csk(sk)->icsk_ack.rcv_mss = hint; 42140efc6faSStephen Hemminger } 42240efc6faSStephen Hemminger 4231da177e4SLinus Torvalds /* Receiver "autotuning" code. 4241da177e4SLinus Torvalds * 4251da177e4SLinus Torvalds * The algorithm for RTT estimation w/o timestamps is based on 4261da177e4SLinus Torvalds * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL. 4271da177e4SLinus Torvalds * <http://www.lanl.gov/radiant/website/pubs/drs/lacsi2001.ps> 4281da177e4SLinus Torvalds * 4291da177e4SLinus Torvalds * More detail on this code can be found at 4301da177e4SLinus Torvalds * <http://www.psc.edu/~jheffner/senior_thesis.ps>, 4311da177e4SLinus Torvalds * though this reference is out of date. A new paper 4321da177e4SLinus Torvalds * is pending. 4331da177e4SLinus Torvalds */ 4341da177e4SLinus Torvalds static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep) 4351da177e4SLinus Torvalds { 4361da177e4SLinus Torvalds u32 new_sample = tp->rcv_rtt_est.rtt; 4371da177e4SLinus Torvalds long m = sample; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (m == 0) 4401da177e4SLinus Torvalds m = 1; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds if (new_sample != 0) { 4431da177e4SLinus Torvalds /* If we sample in larger samples in the non-timestamp 4441da177e4SLinus Torvalds * case, we could grossly overestimate the RTT especially 4451da177e4SLinus Torvalds * with chatty applications or bulk transfer apps which 4461da177e4SLinus Torvalds * are stalled on filesystem I/O. 4471da177e4SLinus Torvalds * 4481da177e4SLinus Torvalds * Also, since we are only going for a minimum in the 44931f34269SStephen Hemminger * non-timestamp case, we do not smooth things out 450caa20d9aSStephen Hemminger * else with timestamps disabled convergence takes too 4511da177e4SLinus Torvalds * long. 4521da177e4SLinus Torvalds */ 4531da177e4SLinus Torvalds if (!win_dep) { 4541da177e4SLinus Torvalds m -= (new_sample >> 3); 4551da177e4SLinus Torvalds new_sample += m; 4561da177e4SLinus Torvalds } else if (m < new_sample) 4571da177e4SLinus Torvalds new_sample = m << 3; 4581da177e4SLinus Torvalds } else { 459caa20d9aSStephen Hemminger /* No previous measure. */ 4601da177e4SLinus Torvalds new_sample = m << 3; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds if (tp->rcv_rtt_est.rtt != new_sample) 4641da177e4SLinus Torvalds tp->rcv_rtt_est.rtt = new_sample; 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp) 4681da177e4SLinus Torvalds { 4691da177e4SLinus Torvalds if (tp->rcv_rtt_est.time == 0) 4701da177e4SLinus Torvalds goto new_measure; 4711da177e4SLinus Torvalds if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq)) 4721da177e4SLinus Torvalds return; 4731da177e4SLinus Torvalds tcp_rcv_rtt_update(tp, 4741da177e4SLinus Torvalds jiffies - tp->rcv_rtt_est.time, 4751da177e4SLinus Torvalds 1); 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds new_measure: 4781da177e4SLinus Torvalds tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd; 4791da177e4SLinus Torvalds tp->rcv_rtt_est.time = tcp_time_stamp; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 482463c84b9SArnaldo Carvalho de Melo static inline void tcp_rcv_rtt_measure_ts(struct sock *sk, const struct sk_buff *skb) 4831da177e4SLinus Torvalds { 484463c84b9SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 4851da177e4SLinus Torvalds if (tp->rx_opt.rcv_tsecr && 4861da177e4SLinus Torvalds (TCP_SKB_CB(skb)->end_seq - 487463c84b9SArnaldo Carvalho de Melo TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss)) 4881da177e4SLinus Torvalds tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0); 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /* 4921da177e4SLinus Torvalds * This function should be called every time data is copied to user space. 4931da177e4SLinus Torvalds * It calculates the appropriate TCP receive buffer space. 4941da177e4SLinus Torvalds */ 4951da177e4SLinus Torvalds void tcp_rcv_space_adjust(struct sock *sk) 4961da177e4SLinus Torvalds { 4971da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 4981da177e4SLinus Torvalds int time; 4991da177e4SLinus Torvalds int space; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds if (tp->rcvq_space.time == 0) 5021da177e4SLinus Torvalds goto new_measure; 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds time = tcp_time_stamp - tp->rcvq_space.time; 5051da177e4SLinus Torvalds if (time < (tp->rcv_rtt_est.rtt >> 3) || 5061da177e4SLinus Torvalds tp->rcv_rtt_est.rtt == 0) 5071da177e4SLinus Torvalds return; 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds space = 2 * (tp->copied_seq - tp->rcvq_space.seq); 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds space = max(tp->rcvq_space.space, space); 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds if (tp->rcvq_space.space != space) { 5141da177e4SLinus Torvalds int rcvmem; 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds tp->rcvq_space.space = space; 5171da177e4SLinus Torvalds 5186fcf9412SJohn Heffner if (sysctl_tcp_moderate_rcvbuf && 5196fcf9412SJohn Heffner !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 5201da177e4SLinus Torvalds int new_clamp = space; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /* Receive space grows, normalize in order to 5231da177e4SLinus Torvalds * take into account packet headers and sk_buff 5241da177e4SLinus Torvalds * structure overhead. 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds space /= tp->advmss; 5271da177e4SLinus Torvalds if (!space) 5281da177e4SLinus Torvalds space = 1; 5291da177e4SLinus Torvalds rcvmem = (tp->advmss + MAX_TCP_HEADER + 5301da177e4SLinus Torvalds 16 + sizeof(struct sk_buff)); 5311da177e4SLinus Torvalds while (tcp_win_from_space(rcvmem) < tp->advmss) 5321da177e4SLinus Torvalds rcvmem += 128; 5331da177e4SLinus Torvalds space *= rcvmem; 5341da177e4SLinus Torvalds space = min(space, sysctl_tcp_rmem[2]); 5351da177e4SLinus Torvalds if (space > sk->sk_rcvbuf) { 5361da177e4SLinus Torvalds sk->sk_rcvbuf = space; 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds /* Make the window clamp follow along. */ 5391da177e4SLinus Torvalds tp->window_clamp = new_clamp; 5401da177e4SLinus Torvalds } 5411da177e4SLinus Torvalds } 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds new_measure: 5451da177e4SLinus Torvalds tp->rcvq_space.seq = tp->copied_seq; 5461da177e4SLinus Torvalds tp->rcvq_space.time = tcp_time_stamp; 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds /* There is something which you must keep in mind when you analyze the 5501da177e4SLinus Torvalds * behavior of the tp->ato delayed ack timeout interval. When a 5511da177e4SLinus Torvalds * connection starts up, we want to ack as quickly as possible. The 5521da177e4SLinus Torvalds * problem is that "good" TCP's do slow start at the beginning of data 5531da177e4SLinus Torvalds * transmission. The means that until we send the first few ACK's the 5541da177e4SLinus Torvalds * sender will sit on his end and only queue most of his data, because 5551da177e4SLinus Torvalds * he can only send snd_cwnd unacked packets at any given time. For 5561da177e4SLinus Torvalds * each ACK we send, he increments snd_cwnd and transmits more of his 5571da177e4SLinus Torvalds * queue. -DaveM 5581da177e4SLinus Torvalds */ 5599e412ba7SIlpo Järvinen static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb) 5601da177e4SLinus Torvalds { 5619e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 562463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 5631da177e4SLinus Torvalds u32 now; 5641da177e4SLinus Torvalds 565463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 5661da177e4SLinus Torvalds 567463c84b9SArnaldo Carvalho de Melo tcp_measure_rcv_mss(sk, skb); 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds tcp_rcv_rtt_measure(tp); 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds now = tcp_time_stamp; 5721da177e4SLinus Torvalds 573463c84b9SArnaldo Carvalho de Melo if (!icsk->icsk_ack.ato) { 5741da177e4SLinus Torvalds /* The _first_ data packet received, initialize 5751da177e4SLinus Torvalds * delayed ACK engine. 5761da177e4SLinus Torvalds */ 577463c84b9SArnaldo Carvalho de Melo tcp_incr_quickack(sk); 578463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN; 5791da177e4SLinus Torvalds } else { 580463c84b9SArnaldo Carvalho de Melo int m = now - icsk->icsk_ack.lrcvtime; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds if (m <= TCP_ATO_MIN/2) { 5831da177e4SLinus Torvalds /* The fastest case is the first. */ 584463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2; 585463c84b9SArnaldo Carvalho de Melo } else if (m < icsk->icsk_ack.ato) { 586463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m; 587463c84b9SArnaldo Carvalho de Melo if (icsk->icsk_ack.ato > icsk->icsk_rto) 588463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = icsk->icsk_rto; 589463c84b9SArnaldo Carvalho de Melo } else if (m > icsk->icsk_rto) { 590caa20d9aSStephen Hemminger /* Too long gap. Apparently sender failed to 5911da177e4SLinus Torvalds * restart window, so that we send ACKs quickly. 5921da177e4SLinus Torvalds */ 593463c84b9SArnaldo Carvalho de Melo tcp_incr_quickack(sk); 5941da177e4SLinus Torvalds sk_stream_mem_reclaim(sk); 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds } 597463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.lrcvtime = now; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds TCP_ECN_check_ce(tp, skb); 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds if (skb->len >= 128) 6029e412ba7SIlpo Järvinen tcp_grow_window(sk, skb); 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 60505bb1fadSDavid S. Miller static u32 tcp_rto_min(struct sock *sk) 60605bb1fadSDavid S. Miller { 60705bb1fadSDavid S. Miller struct dst_entry *dst = __sk_dst_get(sk); 60805bb1fadSDavid S. Miller u32 rto_min = TCP_RTO_MIN; 60905bb1fadSDavid S. Miller 6105c127c58SDavid S. Miller if (dst && dst_metric_locked(dst, RTAX_RTO_MIN)) 61105bb1fadSDavid S. Miller rto_min = dst->metrics[RTAX_RTO_MIN-1]; 61205bb1fadSDavid S. Miller return rto_min; 61305bb1fadSDavid S. Miller } 61405bb1fadSDavid S. Miller 6151da177e4SLinus Torvalds /* Called to compute a smoothed rtt estimate. The data fed to this 6161da177e4SLinus Torvalds * routine either comes from timestamps, or from segments that were 6171da177e4SLinus Torvalds * known _not_ to have been retransmitted [see Karn/Partridge 6181da177e4SLinus Torvalds * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88 6191da177e4SLinus Torvalds * piece by Van Jacobson. 6201da177e4SLinus Torvalds * NOTE: the next three routines used to be one big routine. 6211da177e4SLinus Torvalds * To save cycles in the RFC 1323 implementation it was better to break 6221da177e4SLinus Torvalds * it up into three procedures. -- erics 6231da177e4SLinus Torvalds */ 6242d2abbabSStephen Hemminger static void tcp_rtt_estimator(struct sock *sk, const __u32 mrtt) 6251da177e4SLinus Torvalds { 6266687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 6271da177e4SLinus Torvalds long m = mrtt; /* RTT */ 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds /* The following amusing code comes from Jacobson's 6301da177e4SLinus Torvalds * article in SIGCOMM '88. Note that rtt and mdev 6311da177e4SLinus Torvalds * are scaled versions of rtt and mean deviation. 6321da177e4SLinus Torvalds * This is designed to be as fast as possible 6331da177e4SLinus Torvalds * m stands for "measurement". 6341da177e4SLinus Torvalds * 6351da177e4SLinus Torvalds * On a 1990 paper the rto value is changed to: 6361da177e4SLinus Torvalds * RTO = rtt + 4 * mdev 6371da177e4SLinus Torvalds * 6381da177e4SLinus Torvalds * Funny. This algorithm seems to be very broken. 6391da177e4SLinus Torvalds * These formulae increase RTO, when it should be decreased, increase 64031f34269SStephen Hemminger * too slowly, when it should be increased quickly, decrease too quickly 6411da177e4SLinus Torvalds * etc. I guess in BSD RTO takes ONE value, so that it is absolutely 6421da177e4SLinus Torvalds * does not matter how to _calculate_ it. Seems, it was trap 6431da177e4SLinus Torvalds * that VJ failed to avoid. 8) 6441da177e4SLinus Torvalds */ 6451da177e4SLinus Torvalds if (m == 0) 6461da177e4SLinus Torvalds m = 1; 6471da177e4SLinus Torvalds if (tp->srtt != 0) { 6481da177e4SLinus Torvalds m -= (tp->srtt >> 3); /* m is now error in rtt est */ 6491da177e4SLinus Torvalds tp->srtt += m; /* rtt = 7/8 rtt + 1/8 new */ 6501da177e4SLinus Torvalds if (m < 0) { 6511da177e4SLinus Torvalds m = -m; /* m is now abs(error) */ 6521da177e4SLinus Torvalds m -= (tp->mdev >> 2); /* similar update on mdev */ 6531da177e4SLinus Torvalds /* This is similar to one of Eifel findings. 6541da177e4SLinus Torvalds * Eifel blocks mdev updates when rtt decreases. 6551da177e4SLinus Torvalds * This solution is a bit different: we use finer gain 6561da177e4SLinus Torvalds * for mdev in this case (alpha*beta). 6571da177e4SLinus Torvalds * Like Eifel it also prevents growth of rto, 6581da177e4SLinus Torvalds * but also it limits too fast rto decreases, 6591da177e4SLinus Torvalds * happening in pure Eifel. 6601da177e4SLinus Torvalds */ 6611da177e4SLinus Torvalds if (m > 0) 6621da177e4SLinus Torvalds m >>= 3; 6631da177e4SLinus Torvalds } else { 6641da177e4SLinus Torvalds m -= (tp->mdev >> 2); /* similar update on mdev */ 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds tp->mdev += m; /* mdev = 3/4 mdev + 1/4 new */ 6671da177e4SLinus Torvalds if (tp->mdev > tp->mdev_max) { 6681da177e4SLinus Torvalds tp->mdev_max = tp->mdev; 6691da177e4SLinus Torvalds if (tp->mdev_max > tp->rttvar) 6701da177e4SLinus Torvalds tp->rttvar = tp->mdev_max; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds if (after(tp->snd_una, tp->rtt_seq)) { 6731da177e4SLinus Torvalds if (tp->mdev_max < tp->rttvar) 6741da177e4SLinus Torvalds tp->rttvar -= (tp->rttvar-tp->mdev_max)>>2; 6751da177e4SLinus Torvalds tp->rtt_seq = tp->snd_nxt; 67605bb1fadSDavid S. Miller tp->mdev_max = tcp_rto_min(sk); 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds } else { 6791da177e4SLinus Torvalds /* no previous measure. */ 6801da177e4SLinus Torvalds tp->srtt = m<<3; /* take the measured time to be rtt */ 6811da177e4SLinus Torvalds tp->mdev = m<<1; /* make sure rto = 3*rtt */ 68205bb1fadSDavid S. Miller tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk)); 6831da177e4SLinus Torvalds tp->rtt_seq = tp->snd_nxt; 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds /* Calculate rto without backoff. This is the second half of Van Jacobson's 6881da177e4SLinus Torvalds * routine referred to above. 6891da177e4SLinus Torvalds */ 690463c84b9SArnaldo Carvalho de Melo static inline void tcp_set_rto(struct sock *sk) 6911da177e4SLinus Torvalds { 692463c84b9SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 6931da177e4SLinus Torvalds /* Old crap is replaced with new one. 8) 6941da177e4SLinus Torvalds * 6951da177e4SLinus Torvalds * More seriously: 6961da177e4SLinus Torvalds * 1. If rtt variance happened to be less 50msec, it is hallucination. 6971da177e4SLinus Torvalds * It cannot be less due to utterly erratic ACK generation made 6981da177e4SLinus Torvalds * at least by solaris and freebsd. "Erratic ACKs" has _nothing_ 6991da177e4SLinus Torvalds * to do with delayed acks, because at cwnd>2 true delack timeout 7001da177e4SLinus Torvalds * is invisible. Actually, Linux-2.4 also generates erratic 701caa20d9aSStephen Hemminger * ACKs in some circumstances. 7021da177e4SLinus Torvalds */ 703463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto = (tp->srtt >> 3) + tp->rttvar; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds /* 2. Fixups made earlier cannot be right. 7061da177e4SLinus Torvalds * If we do not estimate RTO correctly without them, 7071da177e4SLinus Torvalds * all the algo is pure shit and should be replaced 708caa20d9aSStephen Hemminger * with correct one. It is exactly, which we pretend to do. 7091da177e4SLinus Torvalds */ 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds /* NOTE: clamping at TCP_RTO_MIN is not required, current algo 7131da177e4SLinus Torvalds * guarantees that rto is higher. 7141da177e4SLinus Torvalds */ 715463c84b9SArnaldo Carvalho de Melo static inline void tcp_bound_rto(struct sock *sk) 7161da177e4SLinus Torvalds { 717463c84b9SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_rto > TCP_RTO_MAX) 718463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto = TCP_RTO_MAX; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds /* Save metrics learned by this TCP session. 7221da177e4SLinus Torvalds This function is called only, when TCP finishes successfully 7231da177e4SLinus Torvalds i.e. when it enters TIME-WAIT or goes from LAST-ACK to CLOSE. 7241da177e4SLinus Torvalds */ 7251da177e4SLinus Torvalds void tcp_update_metrics(struct sock *sk) 7261da177e4SLinus Torvalds { 7271da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 7281da177e4SLinus Torvalds struct dst_entry *dst = __sk_dst_get(sk); 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds if (sysctl_tcp_nometrics_save) 7311da177e4SLinus Torvalds return; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds dst_confirm(dst); 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds if (dst && (dst->flags&DST_HOST)) { 7366687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 7371da177e4SLinus Torvalds int m; 7381da177e4SLinus Torvalds 7396687e988SArnaldo Carvalho de Melo if (icsk->icsk_backoff || !tp->srtt) { 7401da177e4SLinus Torvalds /* This session failed to estimate rtt. Why? 7411da177e4SLinus Torvalds * Probably, no packets returned in time. 7421da177e4SLinus Torvalds * Reset our results. 7431da177e4SLinus Torvalds */ 7441da177e4SLinus Torvalds if (!(dst_metric_locked(dst, RTAX_RTT))) 7451da177e4SLinus Torvalds dst->metrics[RTAX_RTT-1] = 0; 7461da177e4SLinus Torvalds return; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds m = dst_metric(dst, RTAX_RTT) - tp->srtt; 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds /* If newly calculated rtt larger than stored one, 7521da177e4SLinus Torvalds * store new one. Otherwise, use EWMA. Remember, 7531da177e4SLinus Torvalds * rtt overestimation is always better than underestimation. 7541da177e4SLinus Torvalds */ 7551da177e4SLinus Torvalds if (!(dst_metric_locked(dst, RTAX_RTT))) { 7561da177e4SLinus Torvalds if (m <= 0) 7571da177e4SLinus Torvalds dst->metrics[RTAX_RTT-1] = tp->srtt; 7581da177e4SLinus Torvalds else 7591da177e4SLinus Torvalds dst->metrics[RTAX_RTT-1] -= (m>>3); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds if (!(dst_metric_locked(dst, RTAX_RTTVAR))) { 7631da177e4SLinus Torvalds if (m < 0) 7641da177e4SLinus Torvalds m = -m; 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds /* Scale deviation to rttvar fixed point */ 7671da177e4SLinus Torvalds m >>= 1; 7681da177e4SLinus Torvalds if (m < tp->mdev) 7691da177e4SLinus Torvalds m = tp->mdev; 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds if (m >= dst_metric(dst, RTAX_RTTVAR)) 7721da177e4SLinus Torvalds dst->metrics[RTAX_RTTVAR-1] = m; 7731da177e4SLinus Torvalds else 7741da177e4SLinus Torvalds dst->metrics[RTAX_RTTVAR-1] -= 7751da177e4SLinus Torvalds (dst->metrics[RTAX_RTTVAR-1] - m)>>2; 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds if (tp->snd_ssthresh >= 0xFFFF) { 7791da177e4SLinus Torvalds /* Slow start still did not finish. */ 7801da177e4SLinus Torvalds if (dst_metric(dst, RTAX_SSTHRESH) && 7811da177e4SLinus Torvalds !dst_metric_locked(dst, RTAX_SSTHRESH) && 7821da177e4SLinus Torvalds (tp->snd_cwnd >> 1) > dst_metric(dst, RTAX_SSTHRESH)) 7831da177e4SLinus Torvalds dst->metrics[RTAX_SSTHRESH-1] = tp->snd_cwnd >> 1; 7841da177e4SLinus Torvalds if (!dst_metric_locked(dst, RTAX_CWND) && 7851da177e4SLinus Torvalds tp->snd_cwnd > dst_metric(dst, RTAX_CWND)) 7861da177e4SLinus Torvalds dst->metrics[RTAX_CWND-1] = tp->snd_cwnd; 7871da177e4SLinus Torvalds } else if (tp->snd_cwnd > tp->snd_ssthresh && 7886687e988SArnaldo Carvalho de Melo icsk->icsk_ca_state == TCP_CA_Open) { 7891da177e4SLinus Torvalds /* Cong. avoidance phase, cwnd is reliable. */ 7901da177e4SLinus Torvalds if (!dst_metric_locked(dst, RTAX_SSTHRESH)) 7911da177e4SLinus Torvalds dst->metrics[RTAX_SSTHRESH-1] = 7921da177e4SLinus Torvalds max(tp->snd_cwnd >> 1, tp->snd_ssthresh); 7931da177e4SLinus Torvalds if (!dst_metric_locked(dst, RTAX_CWND)) 7941da177e4SLinus Torvalds dst->metrics[RTAX_CWND-1] = (dst->metrics[RTAX_CWND-1] + tp->snd_cwnd) >> 1; 7951da177e4SLinus Torvalds } else { 7961da177e4SLinus Torvalds /* Else slow start did not finish, cwnd is non-sense, 7971da177e4SLinus Torvalds ssthresh may be also invalid. 7981da177e4SLinus Torvalds */ 7991da177e4SLinus Torvalds if (!dst_metric_locked(dst, RTAX_CWND)) 8001da177e4SLinus Torvalds dst->metrics[RTAX_CWND-1] = (dst->metrics[RTAX_CWND-1] + tp->snd_ssthresh) >> 1; 8011da177e4SLinus Torvalds if (dst->metrics[RTAX_SSTHRESH-1] && 8021da177e4SLinus Torvalds !dst_metric_locked(dst, RTAX_SSTHRESH) && 8031da177e4SLinus Torvalds tp->snd_ssthresh > dst->metrics[RTAX_SSTHRESH-1]) 8041da177e4SLinus Torvalds dst->metrics[RTAX_SSTHRESH-1] = tp->snd_ssthresh; 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds if (!dst_metric_locked(dst, RTAX_REORDERING)) { 8081da177e4SLinus Torvalds if (dst->metrics[RTAX_REORDERING-1] < tp->reordering && 8091da177e4SLinus Torvalds tp->reordering != sysctl_tcp_reordering) 8101da177e4SLinus Torvalds dst->metrics[RTAX_REORDERING-1] = tp->reordering; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds 81526722873SDavid S. Miller /* Numbers are taken from RFC3390. 81626722873SDavid S. Miller * 81726722873SDavid S. Miller * John Heffner states: 81826722873SDavid S. Miller * 81926722873SDavid S. Miller * The RFC specifies a window of no more than 4380 bytes 82026722873SDavid S. Miller * unless 2*MSS > 4380. Reading the pseudocode in the RFC 82126722873SDavid S. Miller * is a bit misleading because they use a clamp at 4380 bytes 82226722873SDavid S. Miller * rather than use a multiplier in the relevant range. 82326722873SDavid S. Miller */ 8241da177e4SLinus Torvalds __u32 tcp_init_cwnd(struct tcp_sock *tp, struct dst_entry *dst) 8251da177e4SLinus Torvalds { 8261da177e4SLinus Torvalds __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0); 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds if (!cwnd) { 829c1b4a7e6SDavid S. Miller if (tp->mss_cache > 1460) 8301da177e4SLinus Torvalds cwnd = 2; 8311da177e4SLinus Torvalds else 832c1b4a7e6SDavid S. Miller cwnd = (tp->mss_cache > 1095) ? 3 : 4; 8331da177e4SLinus Torvalds } 8341da177e4SLinus Torvalds return min_t(__u32, cwnd, tp->snd_cwnd_clamp); 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 83740efc6faSStephen Hemminger /* Set slow start threshold and cwnd not falling to slow start */ 8383cfe3baaSIlpo Järvinen void tcp_enter_cwr(struct sock *sk, const int set_ssthresh) 83940efc6faSStephen Hemminger { 84040efc6faSStephen Hemminger struct tcp_sock *tp = tcp_sk(sk); 8413cfe3baaSIlpo Järvinen const struct inet_connection_sock *icsk = inet_csk(sk); 84240efc6faSStephen Hemminger 84340efc6faSStephen Hemminger tp->prior_ssthresh = 0; 84440efc6faSStephen Hemminger tp->bytes_acked = 0; 845e01f9d77SIlpo Järvinen if (icsk->icsk_ca_state < TCP_CA_CWR) { 84640efc6faSStephen Hemminger tp->undo_marker = 0; 8473cfe3baaSIlpo Järvinen if (set_ssthresh) 8483cfe3baaSIlpo Järvinen tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk); 84940efc6faSStephen Hemminger tp->snd_cwnd = min(tp->snd_cwnd, 85040efc6faSStephen Hemminger tcp_packets_in_flight(tp) + 1U); 85140efc6faSStephen Hemminger tp->snd_cwnd_cnt = 0; 85240efc6faSStephen Hemminger tp->high_seq = tp->snd_nxt; 85340efc6faSStephen Hemminger tp->snd_cwnd_stamp = tcp_time_stamp; 85440efc6faSStephen Hemminger TCP_ECN_queue_cwr(tp); 85540efc6faSStephen Hemminger 85640efc6faSStephen Hemminger tcp_set_ca_state(sk, TCP_CA_CWR); 85740efc6faSStephen Hemminger } 85840efc6faSStephen Hemminger } 85940efc6faSStephen Hemminger 860e60402d0SIlpo Järvinen /* 861e60402d0SIlpo Järvinen * Packet counting of FACK is based on in-order assumptions, therefore TCP 862e60402d0SIlpo Järvinen * disables it when reordering is detected 863e60402d0SIlpo Järvinen */ 864e60402d0SIlpo Järvinen static void tcp_disable_fack(struct tcp_sock *tp) 865e60402d0SIlpo Järvinen { 866e60402d0SIlpo Järvinen tp->rx_opt.sack_ok &= ~2; 867e60402d0SIlpo Järvinen } 868e60402d0SIlpo Järvinen 869e60402d0SIlpo Järvinen /* Take a notice that peer is sending DSACKs */ 870e60402d0SIlpo Järvinen static void tcp_dsack_seen(struct tcp_sock *tp) 871e60402d0SIlpo Järvinen { 872e60402d0SIlpo Järvinen tp->rx_opt.sack_ok |= 4; 873e60402d0SIlpo Järvinen } 874e60402d0SIlpo Järvinen 8751da177e4SLinus Torvalds /* Initialize metrics on socket. */ 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds static void tcp_init_metrics(struct sock *sk) 8781da177e4SLinus Torvalds { 8791da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 8801da177e4SLinus Torvalds struct dst_entry *dst = __sk_dst_get(sk); 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds if (dst == NULL) 8831da177e4SLinus Torvalds goto reset; 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds dst_confirm(dst); 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds if (dst_metric_locked(dst, RTAX_CWND)) 8881da177e4SLinus Torvalds tp->snd_cwnd_clamp = dst_metric(dst, RTAX_CWND); 8891da177e4SLinus Torvalds if (dst_metric(dst, RTAX_SSTHRESH)) { 8901da177e4SLinus Torvalds tp->snd_ssthresh = dst_metric(dst, RTAX_SSTHRESH); 8911da177e4SLinus Torvalds if (tp->snd_ssthresh > tp->snd_cwnd_clamp) 8921da177e4SLinus Torvalds tp->snd_ssthresh = tp->snd_cwnd_clamp; 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds if (dst_metric(dst, RTAX_REORDERING) && 8951da177e4SLinus Torvalds tp->reordering != dst_metric(dst, RTAX_REORDERING)) { 896e60402d0SIlpo Järvinen tcp_disable_fack(tp); 8971da177e4SLinus Torvalds tp->reordering = dst_metric(dst, RTAX_REORDERING); 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds if (dst_metric(dst, RTAX_RTT) == 0) 9011da177e4SLinus Torvalds goto reset; 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds if (!tp->srtt && dst_metric(dst, RTAX_RTT) < (TCP_TIMEOUT_INIT << 3)) 9041da177e4SLinus Torvalds goto reset; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds /* Initial rtt is determined from SYN,SYN-ACK. 9071da177e4SLinus Torvalds * The segment is small and rtt may appear much 9081da177e4SLinus Torvalds * less than real one. Use per-dst memory 9091da177e4SLinus Torvalds * to make it more realistic. 9101da177e4SLinus Torvalds * 9111da177e4SLinus Torvalds * A bit of theory. RTT is time passed after "normal" sized packet 912caa20d9aSStephen Hemminger * is sent until it is ACKed. In normal circumstances sending small 9131da177e4SLinus Torvalds * packets force peer to delay ACKs and calculation is correct too. 9141da177e4SLinus Torvalds * The algorithm is adaptive and, provided we follow specs, it 9151da177e4SLinus Torvalds * NEVER underestimate RTT. BUT! If peer tries to make some clever 9161da177e4SLinus Torvalds * tricks sort of "quick acks" for time long enough to decrease RTT 9171da177e4SLinus Torvalds * to low value, and then abruptly stops to do it and starts to delay 9181da177e4SLinus Torvalds * ACKs, wait for troubles. 9191da177e4SLinus Torvalds */ 9201da177e4SLinus Torvalds if (dst_metric(dst, RTAX_RTT) > tp->srtt) { 9211da177e4SLinus Torvalds tp->srtt = dst_metric(dst, RTAX_RTT); 9221da177e4SLinus Torvalds tp->rtt_seq = tp->snd_nxt; 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds if (dst_metric(dst, RTAX_RTTVAR) > tp->mdev) { 9251da177e4SLinus Torvalds tp->mdev = dst_metric(dst, RTAX_RTTVAR); 9261da177e4SLinus Torvalds tp->mdev_max = tp->rttvar = max(tp->mdev, TCP_RTO_MIN); 9271da177e4SLinus Torvalds } 928463c84b9SArnaldo Carvalho de Melo tcp_set_rto(sk); 929463c84b9SArnaldo Carvalho de Melo tcp_bound_rto(sk); 930463c84b9SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_rto < TCP_TIMEOUT_INIT && !tp->rx_opt.saw_tstamp) 9311da177e4SLinus Torvalds goto reset; 9321da177e4SLinus Torvalds tp->snd_cwnd = tcp_init_cwnd(tp, dst); 9331da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 9341da177e4SLinus Torvalds return; 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds reset: 9371da177e4SLinus Torvalds /* Play conservative. If timestamps are not 9381da177e4SLinus Torvalds * supported, TCP will fail to recalculate correct 9391da177e4SLinus Torvalds * rtt, if initial rto is too small. FORGET ALL AND RESET! 9401da177e4SLinus Torvalds */ 9411da177e4SLinus Torvalds if (!tp->rx_opt.saw_tstamp && tp->srtt) { 9421da177e4SLinus Torvalds tp->srtt = 0; 9431da177e4SLinus Torvalds tp->mdev = tp->mdev_max = tp->rttvar = TCP_TIMEOUT_INIT; 944463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_rto = TCP_TIMEOUT_INIT; 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds 9486687e988SArnaldo Carvalho de Melo static void tcp_update_reordering(struct sock *sk, const int metric, 9496687e988SArnaldo Carvalho de Melo const int ts) 9501da177e4SLinus Torvalds { 9516687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 9521da177e4SLinus Torvalds if (metric > tp->reordering) { 9531da177e4SLinus Torvalds tp->reordering = min(TCP_MAX_REORDERING, metric); 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds /* This exciting event is worth to be remembered. 8) */ 9561da177e4SLinus Torvalds if (ts) 9571da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPTSREORDER); 958e60402d0SIlpo Järvinen else if (tcp_is_reno(tp)) 9591da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPRENOREORDER); 960e60402d0SIlpo Järvinen else if (tcp_is_fack(tp)) 9611da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPFACKREORDER); 9621da177e4SLinus Torvalds else 9631da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPSACKREORDER); 9641da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 1 9651da177e4SLinus Torvalds printk(KERN_DEBUG "Disorder%d %d %u f%u s%u rr%d\n", 9666687e988SArnaldo Carvalho de Melo tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state, 9671da177e4SLinus Torvalds tp->reordering, 9681da177e4SLinus Torvalds tp->fackets_out, 9691da177e4SLinus Torvalds tp->sacked_out, 9701da177e4SLinus Torvalds tp->undo_marker ? tp->undo_retrans : 0); 9711da177e4SLinus Torvalds #endif 972e60402d0SIlpo Järvinen tcp_disable_fack(tp); 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds /* This procedure tags the retransmission queue when SACKs arrive. 9771da177e4SLinus Torvalds * 9781da177e4SLinus Torvalds * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L). 9791da177e4SLinus Torvalds * Packets in queue with these bits set are counted in variables 9801da177e4SLinus Torvalds * sacked_out, retrans_out and lost_out, correspondingly. 9811da177e4SLinus Torvalds * 9821da177e4SLinus Torvalds * Valid combinations are: 9831da177e4SLinus Torvalds * Tag InFlight Description 9841da177e4SLinus Torvalds * 0 1 - orig segment is in flight. 9851da177e4SLinus Torvalds * S 0 - nothing flies, orig reached receiver. 9861da177e4SLinus Torvalds * L 0 - nothing flies, orig lost by net. 9871da177e4SLinus Torvalds * R 2 - both orig and retransmit are in flight. 9881da177e4SLinus Torvalds * L|R 1 - orig is lost, retransmit is in flight. 9891da177e4SLinus Torvalds * S|R 1 - orig reached receiver, retrans is still in flight. 9901da177e4SLinus Torvalds * (L|S|R is logically valid, it could occur when L|R is sacked, 9911da177e4SLinus Torvalds * but it is equivalent to plain S and code short-curcuits it to S. 9921da177e4SLinus Torvalds * L|S is logically invalid, it would mean -1 packet in flight 8)) 9931da177e4SLinus Torvalds * 9941da177e4SLinus Torvalds * These 6 states form finite state machine, controlled by the following events: 9951da177e4SLinus Torvalds * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue()) 9961da177e4SLinus Torvalds * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue()) 9971da177e4SLinus Torvalds * 3. Loss detection event of one of three flavors: 9981da177e4SLinus Torvalds * A. Scoreboard estimator decided the packet is lost. 9991da177e4SLinus Torvalds * A'. Reno "three dupacks" marks head of queue lost. 10001da177e4SLinus Torvalds * A''. Its FACK modfication, head until snd.fack is lost. 10011da177e4SLinus Torvalds * B. SACK arrives sacking data transmitted after never retransmitted 10021da177e4SLinus Torvalds * hole was sent out. 10031da177e4SLinus Torvalds * C. SACK arrives sacking SND.NXT at the moment, when the 10041da177e4SLinus Torvalds * segment was retransmitted. 10051da177e4SLinus Torvalds * 4. D-SACK added new rule: D-SACK changes any tag to S. 10061da177e4SLinus Torvalds * 10071da177e4SLinus Torvalds * It is pleasant to note, that state diagram turns out to be commutative, 10081da177e4SLinus Torvalds * so that we are allowed not to be bothered by order of our actions, 10091da177e4SLinus Torvalds * when multiple events arrive simultaneously. (see the function below). 10101da177e4SLinus Torvalds * 10111da177e4SLinus Torvalds * Reordering detection. 10121da177e4SLinus Torvalds * -------------------- 10131da177e4SLinus Torvalds * Reordering metric is maximal distance, which a packet can be displaced 10141da177e4SLinus Torvalds * in packet stream. With SACKs we can estimate it: 10151da177e4SLinus Torvalds * 10161da177e4SLinus Torvalds * 1. SACK fills old hole and the corresponding segment was not 10171da177e4SLinus Torvalds * ever retransmitted -> reordering. Alas, we cannot use it 10181da177e4SLinus Torvalds * when segment was retransmitted. 10191da177e4SLinus Torvalds * 2. The last flaw is solved with D-SACK. D-SACK arrives 10201da177e4SLinus Torvalds * for retransmitted and already SACKed segment -> reordering.. 10211da177e4SLinus Torvalds * Both of these heuristics are not used in Loss state, when we cannot 10221da177e4SLinus Torvalds * account for retransmits accurately. 10235b3c9882SIlpo Järvinen * 10245b3c9882SIlpo Järvinen * SACK block validation. 10255b3c9882SIlpo Järvinen * ---------------------- 10265b3c9882SIlpo Järvinen * 10275b3c9882SIlpo Järvinen * SACK block range validation checks that the received SACK block fits to 10285b3c9882SIlpo Järvinen * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT. 10295b3c9882SIlpo Järvinen * Note that SND.UNA is not included to the range though being valid because 10300e835331SIlpo Järvinen * it means that the receiver is rather inconsistent with itself reporting 10310e835331SIlpo Järvinen * SACK reneging when it should advance SND.UNA. Such SACK block this is 10320e835331SIlpo Järvinen * perfectly valid, however, in light of RFC2018 which explicitly states 10330e835331SIlpo Järvinen * that "SACK block MUST reflect the newest segment. Even if the newest 10340e835331SIlpo Järvinen * segment is going to be discarded ...", not that it looks very clever 10350e835331SIlpo Järvinen * in case of head skb. Due to potentional receiver driven attacks, we 10360e835331SIlpo Järvinen * choose to avoid immediate execution of a walk in write queue due to 10370e835331SIlpo Järvinen * reneging and defer head skb's loss recovery to standard loss recovery 10380e835331SIlpo Järvinen * procedure that will eventually trigger (nothing forbids us doing this). 10395b3c9882SIlpo Järvinen * 10405b3c9882SIlpo Järvinen * Implements also blockage to start_seq wrap-around. Problem lies in the 10415b3c9882SIlpo Järvinen * fact that though start_seq (s) is before end_seq (i.e., not reversed), 10425b3c9882SIlpo Järvinen * there's no guarantee that it will be before snd_nxt (n). The problem 10435b3c9882SIlpo Järvinen * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt 10445b3c9882SIlpo Järvinen * wrap (s_w): 10455b3c9882SIlpo Järvinen * 10465b3c9882SIlpo Järvinen * <- outs wnd -> <- wrapzone -> 10475b3c9882SIlpo Järvinen * u e n u_w e_w s n_w 10485b3c9882SIlpo Järvinen * | | | | | | | 10495b3c9882SIlpo Järvinen * |<------------+------+----- TCP seqno space --------------+---------->| 10505b3c9882SIlpo Järvinen * ...-- <2^31 ->| |<--------... 10515b3c9882SIlpo Järvinen * ...---- >2^31 ------>| |<--------... 10525b3c9882SIlpo Järvinen * 10535b3c9882SIlpo Järvinen * Current code wouldn't be vulnerable but it's better still to discard such 10545b3c9882SIlpo Järvinen * crazy SACK blocks. Doing this check for start_seq alone closes somewhat 10555b3c9882SIlpo Järvinen * similar case (end_seq after snd_nxt wrap) as earlier reversed check in 10565b3c9882SIlpo Järvinen * snd_nxt wrap -> snd_una region will then become "well defined", i.e., 10575b3c9882SIlpo Järvinen * equal to the ideal case (infinite seqno space without wrap caused issues). 10585b3c9882SIlpo Järvinen * 10595b3c9882SIlpo Järvinen * With D-SACK the lower bound is extended to cover sequence space below 10605b3c9882SIlpo Järvinen * SND.UNA down to undo_marker, which is the last point of interest. Yet 10615b3c9882SIlpo Järvinen * again, DSACK block must not to go across snd_una (for the same reason as 10625b3c9882SIlpo Järvinen * for the normal SACK blocks, explained above). But there all simplicity 10635b3c9882SIlpo Järvinen * ends, TCP might receive valid D-SACKs below that. As long as they reside 10645b3c9882SIlpo Järvinen * fully below undo_marker they do not affect behavior in anyway and can 10655b3c9882SIlpo Järvinen * therefore be safely ignored. In rare cases (which are more or less 10665b3c9882SIlpo Järvinen * theoretical ones), the D-SACK will nicely cross that boundary due to skb 10675b3c9882SIlpo Järvinen * fragmentation and packet reordering past skb's retransmission. To consider 10685b3c9882SIlpo Järvinen * them correctly, the acceptable range must be extended even more though 10695b3c9882SIlpo Järvinen * the exact amount is rather hard to quantify. However, tp->max_window can 10705b3c9882SIlpo Järvinen * be used as an exaggerated estimate. 10711da177e4SLinus Torvalds */ 10725b3c9882SIlpo Järvinen static int tcp_is_sackblock_valid(struct tcp_sock *tp, int is_dsack, 10735b3c9882SIlpo Järvinen u32 start_seq, u32 end_seq) 10745b3c9882SIlpo Järvinen { 10755b3c9882SIlpo Järvinen /* Too far in future, or reversed (interpretation is ambiguous) */ 10765b3c9882SIlpo Järvinen if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq)) 10775b3c9882SIlpo Järvinen return 0; 10785b3c9882SIlpo Järvinen 10795b3c9882SIlpo Järvinen /* Nasty start_seq wrap-around check (see comments above) */ 10805b3c9882SIlpo Järvinen if (!before(start_seq, tp->snd_nxt)) 10815b3c9882SIlpo Järvinen return 0; 10825b3c9882SIlpo Järvinen 10835b3c9882SIlpo Järvinen /* In outstanding window? ...This is valid exit for DSACKs too. 10845b3c9882SIlpo Järvinen * start_seq == snd_una is non-sensical (see comments above) 10855b3c9882SIlpo Järvinen */ 10865b3c9882SIlpo Järvinen if (after(start_seq, tp->snd_una)) 10875b3c9882SIlpo Järvinen return 1; 10885b3c9882SIlpo Järvinen 10895b3c9882SIlpo Järvinen if (!is_dsack || !tp->undo_marker) 10905b3c9882SIlpo Järvinen return 0; 10915b3c9882SIlpo Järvinen 10925b3c9882SIlpo Järvinen /* ...Then it's D-SACK, and must reside below snd_una completely */ 10935b3c9882SIlpo Järvinen if (!after(end_seq, tp->snd_una)) 10945b3c9882SIlpo Järvinen return 0; 10955b3c9882SIlpo Järvinen 10965b3c9882SIlpo Järvinen if (!before(start_seq, tp->undo_marker)) 10975b3c9882SIlpo Järvinen return 1; 10985b3c9882SIlpo Järvinen 10995b3c9882SIlpo Järvinen /* Too old */ 11005b3c9882SIlpo Järvinen if (!after(end_seq, tp->undo_marker)) 11015b3c9882SIlpo Järvinen return 0; 11025b3c9882SIlpo Järvinen 11035b3c9882SIlpo Järvinen /* Undo_marker boundary crossing (overestimates a lot). Known already: 11045b3c9882SIlpo Järvinen * start_seq < undo_marker and end_seq >= undo_marker. 11055b3c9882SIlpo Järvinen */ 11065b3c9882SIlpo Järvinen return !before(start_seq, end_seq - tp->max_window); 11075b3c9882SIlpo Järvinen } 11085b3c9882SIlpo Järvinen 11091c1e87edSIlpo Järvinen /* Check for lost retransmit. This superb idea is borrowed from "ratehalving". 11101c1e87edSIlpo Järvinen * Event "C". Later note: FACK people cheated me again 8), we have to account 11111c1e87edSIlpo Järvinen * for reordering! Ugly, but should help. 1112*f785a8e2SIlpo Järvinen * 1113*f785a8e2SIlpo Järvinen * Search retransmitted skbs from write_queue that were sent when snd_nxt was 1114*f785a8e2SIlpo Järvinen * less than what is now known to be received by the other end (derived from 1115*f785a8e2SIlpo Järvinen * SACK blocks by the caller). 11161c1e87edSIlpo Järvinen */ 1117*f785a8e2SIlpo Järvinen static int tcp_mark_lost_retrans(struct sock *sk, u32 received_upto) 11181c1e87edSIlpo Järvinen { 11191c1e87edSIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 11201c1e87edSIlpo Järvinen struct sk_buff *skb; 11211c1e87edSIlpo Järvinen int flag = 0; 1122*f785a8e2SIlpo Järvinen int cnt = 0; 11231c1e87edSIlpo Järvinen 11241c1e87edSIlpo Järvinen tcp_for_write_queue(skb, sk) { 11251c1e87edSIlpo Järvinen u32 ack_seq = TCP_SKB_CB(skb)->ack_seq; 11261c1e87edSIlpo Järvinen 11271c1e87edSIlpo Järvinen if (skb == tcp_send_head(sk)) 11281c1e87edSIlpo Järvinen break; 1129*f785a8e2SIlpo Järvinen if (cnt == tp->retrans_out) 11301c1e87edSIlpo Järvinen break; 11311c1e87edSIlpo Järvinen if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una)) 11321c1e87edSIlpo Järvinen continue; 11331c1e87edSIlpo Järvinen 1134*f785a8e2SIlpo Järvinen if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)) 1135*f785a8e2SIlpo Järvinen continue; 1136*f785a8e2SIlpo Järvinen 1137*f785a8e2SIlpo Järvinen if (after(received_upto, ack_seq) && 11381c1e87edSIlpo Järvinen (tcp_is_fack(tp) || 1139*f785a8e2SIlpo Järvinen !before(received_upto, 11401c1e87edSIlpo Järvinen ack_seq + tp->reordering * tp->mss_cache))) { 11411c1e87edSIlpo Järvinen TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS; 11421c1e87edSIlpo Järvinen tp->retrans_out -= tcp_skb_pcount(skb); 11431c1e87edSIlpo Järvinen 11441c1e87edSIlpo Järvinen /* clear lost hint */ 11451c1e87edSIlpo Järvinen tp->retransmit_skb_hint = NULL; 11461c1e87edSIlpo Järvinen 11471c1e87edSIlpo Järvinen if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) { 11481c1e87edSIlpo Järvinen tp->lost_out += tcp_skb_pcount(skb); 11491c1e87edSIlpo Järvinen TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; 11501c1e87edSIlpo Järvinen flag |= FLAG_DATA_SACKED; 11511c1e87edSIlpo Järvinen NET_INC_STATS_BH(LINUX_MIB_TCPLOSTRETRANSMIT); 11521c1e87edSIlpo Järvinen } 1153*f785a8e2SIlpo Järvinen } else { 1154*f785a8e2SIlpo Järvinen cnt += tcp_skb_pcount(skb); 11551c1e87edSIlpo Järvinen } 11561c1e87edSIlpo Järvinen } 11571c1e87edSIlpo Järvinen return flag; 11581c1e87edSIlpo Järvinen } 11595b3c9882SIlpo Järvinen 1160d06e021dSDavid S. Miller static int tcp_check_dsack(struct tcp_sock *tp, struct sk_buff *ack_skb, 1161d06e021dSDavid S. Miller struct tcp_sack_block_wire *sp, int num_sacks, 1162d06e021dSDavid S. Miller u32 prior_snd_una) 1163d06e021dSDavid S. Miller { 1164d06e021dSDavid S. Miller u32 start_seq_0 = ntohl(get_unaligned(&sp[0].start_seq)); 1165d06e021dSDavid S. Miller u32 end_seq_0 = ntohl(get_unaligned(&sp[0].end_seq)); 1166d06e021dSDavid S. Miller int dup_sack = 0; 1167d06e021dSDavid S. Miller 1168d06e021dSDavid S. Miller if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) { 1169d06e021dSDavid S. Miller dup_sack = 1; 1170e60402d0SIlpo Järvinen tcp_dsack_seen(tp); 1171d06e021dSDavid S. Miller NET_INC_STATS_BH(LINUX_MIB_TCPDSACKRECV); 1172d06e021dSDavid S. Miller } else if (num_sacks > 1) { 1173d06e021dSDavid S. Miller u32 end_seq_1 = ntohl(get_unaligned(&sp[1].end_seq)); 1174d06e021dSDavid S. Miller u32 start_seq_1 = ntohl(get_unaligned(&sp[1].start_seq)); 1175d06e021dSDavid S. Miller 1176d06e021dSDavid S. Miller if (!after(end_seq_0, end_seq_1) && 1177d06e021dSDavid S. Miller !before(start_seq_0, start_seq_1)) { 1178d06e021dSDavid S. Miller dup_sack = 1; 1179e60402d0SIlpo Järvinen tcp_dsack_seen(tp); 1180d06e021dSDavid S. Miller NET_INC_STATS_BH(LINUX_MIB_TCPDSACKOFORECV); 1181d06e021dSDavid S. Miller } 1182d06e021dSDavid S. Miller } 1183d06e021dSDavid S. Miller 1184d06e021dSDavid S. Miller /* D-SACK for already forgotten data... Do dumb counting. */ 1185d06e021dSDavid S. Miller if (dup_sack && 1186d06e021dSDavid S. Miller !after(end_seq_0, prior_snd_una) && 1187d06e021dSDavid S. Miller after(end_seq_0, tp->undo_marker)) 1188d06e021dSDavid S. Miller tp->undo_retrans--; 1189d06e021dSDavid S. Miller 1190d06e021dSDavid S. Miller return dup_sack; 1191d06e021dSDavid S. Miller } 1192d06e021dSDavid S. Miller 1193d1935942SIlpo Järvinen /* Check if skb is fully within the SACK block. In presence of GSO skbs, 1194d1935942SIlpo Järvinen * the incoming SACK may not exactly match but we can find smaller MSS 1195d1935942SIlpo Järvinen * aligned portion of it that matches. Therefore we might need to fragment 1196d1935942SIlpo Järvinen * which may fail and creates some hassle (caller must handle error case 1197d1935942SIlpo Järvinen * returns). 1198d1935942SIlpo Järvinen */ 1199d1935942SIlpo Järvinen int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb, 1200d1935942SIlpo Järvinen u32 start_seq, u32 end_seq) 1201d1935942SIlpo Järvinen { 1202d1935942SIlpo Järvinen int in_sack, err; 1203d1935942SIlpo Järvinen unsigned int pkt_len; 1204d1935942SIlpo Järvinen 1205d1935942SIlpo Järvinen in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) && 1206d1935942SIlpo Järvinen !before(end_seq, TCP_SKB_CB(skb)->end_seq); 1207d1935942SIlpo Järvinen 1208d1935942SIlpo Järvinen if (tcp_skb_pcount(skb) > 1 && !in_sack && 1209d1935942SIlpo Järvinen after(TCP_SKB_CB(skb)->end_seq, start_seq)) { 1210d1935942SIlpo Järvinen 1211d1935942SIlpo Järvinen in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq); 1212d1935942SIlpo Järvinen 1213d1935942SIlpo Järvinen if (!in_sack) 1214d1935942SIlpo Järvinen pkt_len = start_seq - TCP_SKB_CB(skb)->seq; 1215d1935942SIlpo Järvinen else 1216d1935942SIlpo Järvinen pkt_len = end_seq - TCP_SKB_CB(skb)->seq; 1217d1935942SIlpo Järvinen err = tcp_fragment(sk, skb, pkt_len, skb_shinfo(skb)->gso_size); 1218d1935942SIlpo Järvinen if (err < 0) 1219d1935942SIlpo Järvinen return err; 1220d1935942SIlpo Järvinen } 1221d1935942SIlpo Järvinen 1222d1935942SIlpo Järvinen return in_sack; 1223d1935942SIlpo Järvinen } 1224d1935942SIlpo Järvinen 12251da177e4SLinus Torvalds static int 12261da177e4SLinus Torvalds tcp_sacktag_write_queue(struct sock *sk, struct sk_buff *ack_skb, u32 prior_snd_una) 12271da177e4SLinus Torvalds { 12286687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 12291da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 12309c70220bSArnaldo Carvalho de Melo unsigned char *ptr = (skb_transport_header(ack_skb) + 12319c70220bSArnaldo Carvalho de Melo TCP_SKB_CB(ack_skb)->sacked); 1232269bd27eSAl Viro struct tcp_sack_block_wire *sp = (struct tcp_sack_block_wire *)(ptr+2); 1233fda03fbbSBaruch Even struct sk_buff *cached_skb; 12341da177e4SLinus Torvalds int num_sacks = (ptr[1] - TCPOLEN_SACK_BASE)>>3; 12351da177e4SLinus Torvalds int reord = tp->packets_out; 12361da177e4SLinus Torvalds int prior_fackets; 1237*f785a8e2SIlpo Järvinen u32 highest_sack_end_seq = 0; 12381da177e4SLinus Torvalds int flag = 0; 12397769f406SIlpo Järvinen int found_dup_sack = 0; 1240fda03fbbSBaruch Even int cached_fack_count; 12411da177e4SLinus Torvalds int i; 1242fda03fbbSBaruch Even int first_sack_index; 12431da177e4SLinus Torvalds 1244d738cd8fSIlpo Järvinen if (!tp->sacked_out) { 1245de83c058SIlpo Järvinen if (WARN_ON(tp->fackets_out)) 12461da177e4SLinus Torvalds tp->fackets_out = 0; 1247d738cd8fSIlpo Järvinen tp->highest_sack = tp->snd_una; 1248d738cd8fSIlpo Järvinen } 12491da177e4SLinus Torvalds prior_fackets = tp->fackets_out; 12501da177e4SLinus Torvalds 1251d06e021dSDavid S. Miller found_dup_sack = tcp_check_dsack(tp, ack_skb, sp, 1252d06e021dSDavid S. Miller num_sacks, prior_snd_una); 1253d06e021dSDavid S. Miller if (found_dup_sack) 125449ff4bb4SIlpo Järvinen flag |= FLAG_DSACKING_ACK; 12556f74651aSBaruch Even 12566f74651aSBaruch Even /* Eliminate too old ACKs, but take into 12576f74651aSBaruch Even * account more or less fresh ones, they can 12586f74651aSBaruch Even * contain valid SACK info. 12596f74651aSBaruch Even */ 12606f74651aSBaruch Even if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window)) 12616f74651aSBaruch Even return 0; 12626f74651aSBaruch Even 12636a438bbeSStephen Hemminger /* SACK fastpath: 12646a438bbeSStephen Hemminger * if the only SACK change is the increase of the end_seq of 12656a438bbeSStephen Hemminger * the first block then only apply that SACK block 12666a438bbeSStephen Hemminger * and use retrans queue hinting otherwise slowpath */ 12676a438bbeSStephen Hemminger flag = 1; 12686a438bbeSStephen Hemminger for (i = 0; i < num_sacks; i++) { 12696f74651aSBaruch Even __be32 start_seq = sp[i].start_seq; 12706f74651aSBaruch Even __be32 end_seq = sp[i].end_seq; 12716a438bbeSStephen Hemminger 12726a438bbeSStephen Hemminger if (i == 0) { 12736a438bbeSStephen Hemminger if (tp->recv_sack_cache[i].start_seq != start_seq) 12746a438bbeSStephen Hemminger flag = 0; 12756a438bbeSStephen Hemminger } else { 12766a438bbeSStephen Hemminger if ((tp->recv_sack_cache[i].start_seq != start_seq) || 12776a438bbeSStephen Hemminger (tp->recv_sack_cache[i].end_seq != end_seq)) 12786a438bbeSStephen Hemminger flag = 0; 12796a438bbeSStephen Hemminger } 12806a438bbeSStephen Hemminger tp->recv_sack_cache[i].start_seq = start_seq; 12816a438bbeSStephen Hemminger tp->recv_sack_cache[i].end_seq = end_seq; 12826a438bbeSStephen Hemminger } 12838a3c3a97SBaruch Even /* Clear the rest of the cache sack blocks so they won't match mistakenly. */ 12848a3c3a97SBaruch Even for (; i < ARRAY_SIZE(tp->recv_sack_cache); i++) { 12858a3c3a97SBaruch Even tp->recv_sack_cache[i].start_seq = 0; 12868a3c3a97SBaruch Even tp->recv_sack_cache[i].end_seq = 0; 12878a3c3a97SBaruch Even } 12886a438bbeSStephen Hemminger 1289fda03fbbSBaruch Even first_sack_index = 0; 12906a438bbeSStephen Hemminger if (flag) 12916a438bbeSStephen Hemminger num_sacks = 1; 12926a438bbeSStephen Hemminger else { 12936a438bbeSStephen Hemminger int j; 12946a438bbeSStephen Hemminger tp->fastpath_skb_hint = NULL; 12956a438bbeSStephen Hemminger 12966a438bbeSStephen Hemminger /* order SACK blocks to allow in order walk of the retrans queue */ 12976a438bbeSStephen Hemminger for (i = num_sacks-1; i > 0; i--) { 12986a438bbeSStephen Hemminger for (j = 0; j < i; j++){ 12996a438bbeSStephen Hemminger if (after(ntohl(sp[j].start_seq), 13006a438bbeSStephen Hemminger ntohl(sp[j+1].start_seq))){ 1301db3ccdacSBaruch Even struct tcp_sack_block_wire tmp; 1302db3ccdacSBaruch Even 1303db3ccdacSBaruch Even tmp = sp[j]; 1304db3ccdacSBaruch Even sp[j] = sp[j+1]; 1305db3ccdacSBaruch Even sp[j+1] = tmp; 1306fda03fbbSBaruch Even 1307fda03fbbSBaruch Even /* Track where the first SACK block goes to */ 1308fda03fbbSBaruch Even if (j == first_sack_index) 1309fda03fbbSBaruch Even first_sack_index = j+1; 13106a438bbeSStephen Hemminger } 13116a438bbeSStephen Hemminger 13126a438bbeSStephen Hemminger } 13136a438bbeSStephen Hemminger } 13146a438bbeSStephen Hemminger } 13156a438bbeSStephen Hemminger 13166a438bbeSStephen Hemminger /* clear flag as used for different purpose in following code */ 13176a438bbeSStephen Hemminger flag = 0; 13186a438bbeSStephen Hemminger 1319fda03fbbSBaruch Even /* Use SACK fastpath hint if valid */ 1320fda03fbbSBaruch Even cached_skb = tp->fastpath_skb_hint; 1321fda03fbbSBaruch Even cached_fack_count = tp->fastpath_cnt_hint; 1322fda03fbbSBaruch Even if (!cached_skb) { 1323fe067e8aSDavid S. Miller cached_skb = tcp_write_queue_head(sk); 1324fda03fbbSBaruch Even cached_fack_count = 0; 1325fda03fbbSBaruch Even } 1326fda03fbbSBaruch Even 13276a438bbeSStephen Hemminger for (i=0; i<num_sacks; i++, sp++) { 13286a438bbeSStephen Hemminger struct sk_buff *skb; 13296a438bbeSStephen Hemminger __u32 start_seq = ntohl(sp->start_seq); 13306a438bbeSStephen Hemminger __u32 end_seq = ntohl(sp->end_seq); 13316a438bbeSStephen Hemminger int fack_count; 13327769f406SIlpo Järvinen int dup_sack = (found_dup_sack && (i == first_sack_index)); 13336a438bbeSStephen Hemminger 133418f02545SIlpo Järvinen if (!tcp_is_sackblock_valid(tp, dup_sack, start_seq, end_seq)) { 133518f02545SIlpo Järvinen if (dup_sack) { 133618f02545SIlpo Järvinen if (!tp->undo_marker) 133718f02545SIlpo Järvinen NET_INC_STATS_BH(LINUX_MIB_TCPDSACKIGNOREDNOUNDO); 133818f02545SIlpo Järvinen else 133918f02545SIlpo Järvinen NET_INC_STATS_BH(LINUX_MIB_TCPDSACKIGNOREDOLD); 134093e68020SIlpo Järvinen } else { 134193e68020SIlpo Järvinen /* Don't count olds caused by ACK reordering */ 134293e68020SIlpo Järvinen if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) && 134393e68020SIlpo Järvinen !after(end_seq, tp->snd_una)) 134493e68020SIlpo Järvinen continue; 134518f02545SIlpo Järvinen NET_INC_STATS_BH(LINUX_MIB_TCPSACKDISCARD); 134693e68020SIlpo Järvinen } 13475b3c9882SIlpo Järvinen continue; 134818f02545SIlpo Järvinen } 13495b3c9882SIlpo Järvinen 1350fda03fbbSBaruch Even skb = cached_skb; 1351fda03fbbSBaruch Even fack_count = cached_fack_count; 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds /* Event "B" in the comment above. */ 13541da177e4SLinus Torvalds if (after(end_seq, tp->high_seq)) 13551da177e4SLinus Torvalds flag |= FLAG_DATA_LOST; 13561da177e4SLinus Torvalds 1357fe067e8aSDavid S. Miller tcp_for_write_queue_from(skb, sk) { 1358f6fb128dSIlpo Järvinen int in_sack; 13596475be16SDavid S. Miller u8 sacked; 13601da177e4SLinus Torvalds 1361fe067e8aSDavid S. Miller if (skb == tcp_send_head(sk)) 1362fe067e8aSDavid S. Miller break; 1363fe067e8aSDavid S. Miller 1364fda03fbbSBaruch Even cached_skb = skb; 1365fda03fbbSBaruch Even cached_fack_count = fack_count; 1366fda03fbbSBaruch Even if (i == first_sack_index) { 13676a438bbeSStephen Hemminger tp->fastpath_skb_hint = skb; 13686a438bbeSStephen Hemminger tp->fastpath_cnt_hint = fack_count; 1369fda03fbbSBaruch Even } 13706a438bbeSStephen Hemminger 13711da177e4SLinus Torvalds /* The retransmission queue is always in order, so 13721da177e4SLinus Torvalds * we can short-circuit the walk early. 13731da177e4SLinus Torvalds */ 13741da177e4SLinus Torvalds if (!before(TCP_SKB_CB(skb)->seq, end_seq)) 13751da177e4SLinus Torvalds break; 13761da177e4SLinus Torvalds 1377d1935942SIlpo Järvinen in_sack = tcp_match_skb_to_sack(sk, skb, start_seq, end_seq); 1378d1935942SIlpo Järvinen if (in_sack < 0) 13796475be16SDavid S. Miller break; 13806475be16SDavid S. Miller 1381f6fb128dSIlpo Järvinen fack_count += tcp_skb_pcount(skb); 13821da177e4SLinus Torvalds 13836475be16SDavid S. Miller sacked = TCP_SKB_CB(skb)->sacked; 13846475be16SDavid S. Miller 13851da177e4SLinus Torvalds /* Account D-SACK for retransmitted packet. */ 13861da177e4SLinus Torvalds if ((dup_sack && in_sack) && 13871da177e4SLinus Torvalds (sacked & TCPCB_RETRANS) && 13881da177e4SLinus Torvalds after(TCP_SKB_CB(skb)->end_seq, tp->undo_marker)) 13891da177e4SLinus Torvalds tp->undo_retrans--; 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds /* The frame is ACKed. */ 13921da177e4SLinus Torvalds if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una)) { 13931da177e4SLinus Torvalds if (sacked&TCPCB_RETRANS) { 13941da177e4SLinus Torvalds if ((dup_sack && in_sack) && 13951da177e4SLinus Torvalds (sacked&TCPCB_SACKED_ACKED)) 13961da177e4SLinus Torvalds reord = min(fack_count, reord); 13971da177e4SLinus Torvalds } else { 13981da177e4SLinus Torvalds /* If it was in a hole, we detected reordering. */ 13991da177e4SLinus Torvalds if (fack_count < prior_fackets && 14001da177e4SLinus Torvalds !(sacked&TCPCB_SACKED_ACKED)) 14011da177e4SLinus Torvalds reord = min(fack_count, reord); 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds /* Nothing to do; acked frame is about to be dropped. */ 14051da177e4SLinus Torvalds continue; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds if (!in_sack) 14091da177e4SLinus Torvalds continue; 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds if (!(sacked&TCPCB_SACKED_ACKED)) { 14121da177e4SLinus Torvalds if (sacked & TCPCB_SACKED_RETRANS) { 14131da177e4SLinus Torvalds /* If the segment is not tagged as lost, 14141da177e4SLinus Torvalds * we do not clear RETRANS, believing 14151da177e4SLinus Torvalds * that retransmission is still in flight. 14161da177e4SLinus Torvalds */ 14171da177e4SLinus Torvalds if (sacked & TCPCB_LOST) { 14181da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS); 14191da177e4SLinus Torvalds tp->lost_out -= tcp_skb_pcount(skb); 14201da177e4SLinus Torvalds tp->retrans_out -= tcp_skb_pcount(skb); 14216a438bbeSStephen Hemminger 14226a438bbeSStephen Hemminger /* clear lost hint */ 14236a438bbeSStephen Hemminger tp->retransmit_skb_hint = NULL; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds } else { 14261da177e4SLinus Torvalds /* New sack for not retransmitted frame, 14271da177e4SLinus Torvalds * which was in hole. It is reordering. 14281da177e4SLinus Torvalds */ 14291da177e4SLinus Torvalds if (!(sacked & TCPCB_RETRANS) && 14301da177e4SLinus Torvalds fack_count < prior_fackets) 14311da177e4SLinus Torvalds reord = min(fack_count, reord); 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds if (sacked & TCPCB_LOST) { 14341da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST; 14351da177e4SLinus Torvalds tp->lost_out -= tcp_skb_pcount(skb); 14366a438bbeSStephen Hemminger 14376a438bbeSStephen Hemminger /* clear lost hint */ 14386a438bbeSStephen Hemminger tp->retransmit_skb_hint = NULL; 14391da177e4SLinus Torvalds } 14404dc2665eSIlpo Järvinen /* SACK enhanced F-RTO detection. 14414dc2665eSIlpo Järvinen * Set flag if and only if non-rexmitted 14424dc2665eSIlpo Järvinen * segments below frto_highmark are 14434dc2665eSIlpo Järvinen * SACKed (RFC4138; Appendix B). 14444dc2665eSIlpo Järvinen * Clearing correct due to in-order walk 14454dc2665eSIlpo Järvinen */ 14464dc2665eSIlpo Järvinen if (after(end_seq, tp->frto_highmark)) { 14474dc2665eSIlpo Järvinen flag &= ~FLAG_ONLY_ORIG_SACKED; 14484dc2665eSIlpo Järvinen } else { 14494dc2665eSIlpo Järvinen if (!(sacked & TCPCB_RETRANS)) 14504dc2665eSIlpo Järvinen flag |= FLAG_ONLY_ORIG_SACKED; 14514dc2665eSIlpo Järvinen } 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked |= TCPCB_SACKED_ACKED; 14551da177e4SLinus Torvalds flag |= FLAG_DATA_SACKED; 14561da177e4SLinus Torvalds tp->sacked_out += tcp_skb_pcount(skb); 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds if (fack_count > tp->fackets_out) 14591da177e4SLinus Torvalds tp->fackets_out = fack_count; 1460d738cd8fSIlpo Järvinen 1461*f785a8e2SIlpo Järvinen if (after(TCP_SKB_CB(skb)->seq, tp->highest_sack)) { 1462d738cd8fSIlpo Järvinen tp->highest_sack = TCP_SKB_CB(skb)->seq; 1463*f785a8e2SIlpo Järvinen highest_sack_end_seq = TCP_SKB_CB(skb)->end_seq; 1464*f785a8e2SIlpo Järvinen } 14651da177e4SLinus Torvalds } else { 14661da177e4SLinus Torvalds if (dup_sack && (sacked&TCPCB_RETRANS)) 14671da177e4SLinus Torvalds reord = min(fack_count, reord); 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds /* D-SACK. We can detect redundant retransmission 14711da177e4SLinus Torvalds * in S|R and plain R frames and clear it. 14721da177e4SLinus Torvalds * undo_retrans is decreased above, L|R frames 14731da177e4SLinus Torvalds * are accounted above as well. 14741da177e4SLinus Torvalds */ 14751da177e4SLinus Torvalds if (dup_sack && 14761da177e4SLinus Torvalds (TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_RETRANS)) { 14771da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS; 14781da177e4SLinus Torvalds tp->retrans_out -= tcp_skb_pcount(skb); 14796a438bbeSStephen Hemminger tp->retransmit_skb_hint = NULL; 14801da177e4SLinus Torvalds } 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds 1484*f785a8e2SIlpo Järvinen if (tp->retrans_out && highest_sack_end_seq && 1485*f785a8e2SIlpo Järvinen after(highest_sack_end_seq, tp->high_seq) && 1486*f785a8e2SIlpo Järvinen icsk->icsk_ca_state == TCP_CA_Recovery) 1487*f785a8e2SIlpo Järvinen flag |= tcp_mark_lost_retrans(sk, highest_sack_end_seq); 14881da177e4SLinus Torvalds 148986426c22SIlpo Järvinen tcp_verify_left_out(tp); 149086426c22SIlpo Järvinen 1491288035f9SIlpo Järvinen if ((reord < tp->fackets_out) && icsk->icsk_ca_state != TCP_CA_Loss && 1492c5e7af0dSIlpo Järvinen (!tp->frto_highmark || after(tp->snd_una, tp->frto_highmark))) 14936687e988SArnaldo Carvalho de Melo tcp_update_reordering(sk, ((tp->fackets_out + 1) - reord), 0); 14941da177e4SLinus Torvalds 14951da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0 14961da177e4SLinus Torvalds BUG_TRAP((int)tp->sacked_out >= 0); 14971da177e4SLinus Torvalds BUG_TRAP((int)tp->lost_out >= 0); 14981da177e4SLinus Torvalds BUG_TRAP((int)tp->retrans_out >= 0); 14991da177e4SLinus Torvalds BUG_TRAP((int)tcp_packets_in_flight(tp) >= 0); 15001da177e4SLinus Torvalds #endif 15011da177e4SLinus Torvalds return flag; 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds 150495eacd27SIlpo Järvinen /* If we receive more dupacks than we expected counting segments 150595eacd27SIlpo Järvinen * in assumption of absent reordering, interpret this as reordering. 150695eacd27SIlpo Järvinen * The only another reason could be bug in receiver TCP. 150730935cf4SIlpo Järvinen */ 15084ddf6676SIlpo Järvinen static void tcp_check_reno_reordering(struct sock *sk, const int addend) 15094ddf6676SIlpo Järvinen { 15104ddf6676SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 15114ddf6676SIlpo Järvinen u32 holes; 15124ddf6676SIlpo Järvinen 15134ddf6676SIlpo Järvinen holes = max(tp->lost_out, 1U); 15144ddf6676SIlpo Järvinen holes = min(holes, tp->packets_out); 15154ddf6676SIlpo Järvinen 15164ddf6676SIlpo Järvinen if ((tp->sacked_out + holes) > tp->packets_out) { 15174ddf6676SIlpo Järvinen tp->sacked_out = tp->packets_out - holes; 15184ddf6676SIlpo Järvinen tcp_update_reordering(sk, tp->packets_out + addend, 0); 15194ddf6676SIlpo Järvinen } 15204ddf6676SIlpo Järvinen } 15214ddf6676SIlpo Järvinen 15224ddf6676SIlpo Järvinen /* Emulate SACKs for SACKless connection: account for a new dupack. */ 15234ddf6676SIlpo Järvinen 15244ddf6676SIlpo Järvinen static void tcp_add_reno_sack(struct sock *sk) 15254ddf6676SIlpo Järvinen { 15264ddf6676SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 15274ddf6676SIlpo Järvinen tp->sacked_out++; 15284ddf6676SIlpo Järvinen tcp_check_reno_reordering(sk, 0); 1529005903bcSIlpo Järvinen tcp_verify_left_out(tp); 15304ddf6676SIlpo Järvinen } 15314ddf6676SIlpo Järvinen 15324ddf6676SIlpo Järvinen /* Account for ACK, ACKing some data in Reno Recovery phase. */ 15334ddf6676SIlpo Järvinen 15344ddf6676SIlpo Järvinen static void tcp_remove_reno_sacks(struct sock *sk, int acked) 15354ddf6676SIlpo Järvinen { 15364ddf6676SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 15374ddf6676SIlpo Järvinen 15384ddf6676SIlpo Järvinen if (acked > 0) { 15394ddf6676SIlpo Järvinen /* One ACK acked hole. The rest eat duplicate ACKs. */ 15404ddf6676SIlpo Järvinen if (acked-1 >= tp->sacked_out) 15414ddf6676SIlpo Järvinen tp->sacked_out = 0; 15424ddf6676SIlpo Järvinen else 15434ddf6676SIlpo Järvinen tp->sacked_out -= acked-1; 15444ddf6676SIlpo Järvinen } 15454ddf6676SIlpo Järvinen tcp_check_reno_reordering(sk, acked); 1546005903bcSIlpo Järvinen tcp_verify_left_out(tp); 15474ddf6676SIlpo Järvinen } 15484ddf6676SIlpo Järvinen 15494ddf6676SIlpo Järvinen static inline void tcp_reset_reno_sack(struct tcp_sock *tp) 15504ddf6676SIlpo Järvinen { 15514ddf6676SIlpo Järvinen tp->sacked_out = 0; 15524ddf6676SIlpo Järvinen } 15534ddf6676SIlpo Järvinen 155495eacd27SIlpo Järvinen /* F-RTO can only be used if TCP has never retransmitted anything other than 155595eacd27SIlpo Järvinen * head (SACK enhanced variant from Appendix B of RFC4138 is more robust here) 155695eacd27SIlpo Järvinen */ 155746d0de4eSIlpo Järvinen int tcp_use_frto(struct sock *sk) 1558bdaae17dSIlpo Järvinen { 1559bdaae17dSIlpo Järvinen const struct tcp_sock *tp = tcp_sk(sk); 156046d0de4eSIlpo Järvinen struct sk_buff *skb; 1561bdaae17dSIlpo Järvinen 1562575ee714SIlpo Järvinen if (!sysctl_tcp_frto) 156346d0de4eSIlpo Järvinen return 0; 156446d0de4eSIlpo Järvinen 15654dc2665eSIlpo Järvinen if (IsSackFrto()) 15664dc2665eSIlpo Järvinen return 1; 15674dc2665eSIlpo Järvinen 156846d0de4eSIlpo Järvinen /* Avoid expensive walking of rexmit queue if possible */ 156946d0de4eSIlpo Järvinen if (tp->retrans_out > 1) 157046d0de4eSIlpo Järvinen return 0; 157146d0de4eSIlpo Järvinen 1572fe067e8aSDavid S. Miller skb = tcp_write_queue_head(sk); 1573fe067e8aSDavid S. Miller skb = tcp_write_queue_next(sk, skb); /* Skips head */ 1574fe067e8aSDavid S. Miller tcp_for_write_queue_from(skb, sk) { 1575fe067e8aSDavid S. Miller if (skb == tcp_send_head(sk)) 1576fe067e8aSDavid S. Miller break; 157746d0de4eSIlpo Järvinen if (TCP_SKB_CB(skb)->sacked&TCPCB_RETRANS) 157846d0de4eSIlpo Järvinen return 0; 157946d0de4eSIlpo Järvinen /* Short-circuit when first non-SACKed skb has been checked */ 158046d0de4eSIlpo Järvinen if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED)) 158146d0de4eSIlpo Järvinen break; 158246d0de4eSIlpo Järvinen } 158346d0de4eSIlpo Järvinen return 1; 1584bdaae17dSIlpo Järvinen } 1585bdaae17dSIlpo Järvinen 158630935cf4SIlpo Järvinen /* RTO occurred, but do not yet enter Loss state. Instead, defer RTO 158730935cf4SIlpo Järvinen * recovery a bit and use heuristics in tcp_process_frto() to detect if 1588d1a54c6aSIlpo Järvinen * the RTO was spurious. Only clear SACKED_RETRANS of the head here to 1589d1a54c6aSIlpo Järvinen * keep retrans_out counting accurate (with SACK F-RTO, other than head 1590d1a54c6aSIlpo Järvinen * may still have that bit set); TCPCB_LOST and remaining SACKED_RETRANS 1591d1a54c6aSIlpo Järvinen * bits are handled if the Loss state is really to be entered (in 1592d1a54c6aSIlpo Järvinen * tcp_enter_frto_loss). 15937487c48cSIlpo Järvinen * 15947487c48cSIlpo Järvinen * Do like tcp_enter_loss() would; when RTO expires the second time it 15957487c48cSIlpo Järvinen * does: 15967487c48cSIlpo Järvinen * "Reduce ssthresh if it has not yet been made inside this window." 15971da177e4SLinus Torvalds */ 15981da177e4SLinus Torvalds void tcp_enter_frto(struct sock *sk) 15991da177e4SLinus Torvalds { 16006687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 16011da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 16021da177e4SLinus Torvalds struct sk_buff *skb; 16031da177e4SLinus Torvalds 16047487c48cSIlpo Järvinen if ((!tp->frto_counter && icsk->icsk_ca_state <= TCP_CA_Disorder) || 16051da177e4SLinus Torvalds tp->snd_una == tp->high_seq || 16067487c48cSIlpo Järvinen ((icsk->icsk_ca_state == TCP_CA_Loss || tp->frto_counter) && 16077487c48cSIlpo Järvinen !icsk->icsk_retransmits)) { 16086687e988SArnaldo Carvalho de Melo tp->prior_ssthresh = tcp_current_ssthresh(sk); 160966e93e45SIlpo Järvinen /* Our state is too optimistic in ssthresh() call because cwnd 161066e93e45SIlpo Järvinen * is not reduced until tcp_enter_frto_loss() when previous FRTO 161166e93e45SIlpo Järvinen * recovery has not yet completed. Pattern would be this: RTO, 161266e93e45SIlpo Järvinen * Cumulative ACK, RTO (2xRTO for the same segment does not end 161366e93e45SIlpo Järvinen * up here twice). 161466e93e45SIlpo Järvinen * RFC4138 should be more specific on what to do, even though 161566e93e45SIlpo Järvinen * RTO is quite unlikely to occur after the first Cumulative ACK 161666e93e45SIlpo Järvinen * due to back-off and complexity of triggering events ... 161766e93e45SIlpo Järvinen */ 161866e93e45SIlpo Järvinen if (tp->frto_counter) { 161966e93e45SIlpo Järvinen u32 stored_cwnd; 162066e93e45SIlpo Järvinen stored_cwnd = tp->snd_cwnd; 162166e93e45SIlpo Järvinen tp->snd_cwnd = 2; 16226687e988SArnaldo Carvalho de Melo tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk); 162366e93e45SIlpo Järvinen tp->snd_cwnd = stored_cwnd; 162466e93e45SIlpo Järvinen } else { 162566e93e45SIlpo Järvinen tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk); 162666e93e45SIlpo Järvinen } 162766e93e45SIlpo Järvinen /* ... in theory, cong.control module could do "any tricks" in 162866e93e45SIlpo Järvinen * ssthresh(), which means that ca_state, lost bits and lost_out 162966e93e45SIlpo Järvinen * counter would have to be faked before the call occurs. We 163066e93e45SIlpo Järvinen * consider that too expensive, unlikely and hacky, so modules 163166e93e45SIlpo Järvinen * using these in ssthresh() must deal these incompatibility 163266e93e45SIlpo Järvinen * issues if they receives CA_EVENT_FRTO and frto_counter != 0 163366e93e45SIlpo Järvinen */ 16346687e988SArnaldo Carvalho de Melo tcp_ca_event(sk, CA_EVENT_FRTO); 16351da177e4SLinus Torvalds } 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds tp->undo_marker = tp->snd_una; 16381da177e4SLinus Torvalds tp->undo_retrans = 0; 16391da177e4SLinus Torvalds 1640fe067e8aSDavid S. Miller skb = tcp_write_queue_head(sk); 1641009a2e3eSIlpo Järvinen if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS) 1642009a2e3eSIlpo Järvinen tp->undo_marker = 0; 1643d1a54c6aSIlpo Järvinen if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) { 1644522e7548SIlpo Järvinen TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS; 1645d1a54c6aSIlpo Järvinen tp->retrans_out -= tcp_skb_pcount(skb); 16461da177e4SLinus Torvalds } 1647005903bcSIlpo Järvinen tcp_verify_left_out(tp); 16481da177e4SLinus Torvalds 16494dc2665eSIlpo Järvinen /* Earlier loss recovery underway (see RFC4138; Appendix B). 16504dc2665eSIlpo Järvinen * The last condition is necessary at least in tp->frto_counter case. 16514dc2665eSIlpo Järvinen */ 16524dc2665eSIlpo Järvinen if (IsSackFrto() && (tp->frto_counter || 16534dc2665eSIlpo Järvinen ((1 << icsk->icsk_ca_state) & (TCPF_CA_Recovery|TCPF_CA_Loss))) && 16544dc2665eSIlpo Järvinen after(tp->high_seq, tp->snd_una)) { 16554dc2665eSIlpo Järvinen tp->frto_highmark = tp->high_seq; 16564dc2665eSIlpo Järvinen } else { 16574dc2665eSIlpo Järvinen tp->frto_highmark = tp->snd_nxt; 16584dc2665eSIlpo Järvinen } 16597b0eb22bSIlpo Järvinen tcp_set_ca_state(sk, TCP_CA_Disorder); 16607b0eb22bSIlpo Järvinen tp->high_seq = tp->snd_nxt; 16617487c48cSIlpo Järvinen tp->frto_counter = 1; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds /* Enter Loss state after F-RTO was applied. Dupack arrived after RTO, 16651da177e4SLinus Torvalds * which indicates that we should follow the traditional RTO recovery, 16661da177e4SLinus Torvalds * i.e. mark everything lost and do go-back-N retransmission. 16671da177e4SLinus Torvalds */ 1668d1a54c6aSIlpo Järvinen static void tcp_enter_frto_loss(struct sock *sk, int allowed_segments, int flag) 16691da177e4SLinus Torvalds { 16701da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 16711da177e4SLinus Torvalds struct sk_buff *skb; 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds tp->lost_out = 0; 1674d1a54c6aSIlpo Järvinen tp->retrans_out = 0; 1675e60402d0SIlpo Järvinen if (tcp_is_reno(tp)) 16769bff40fdSIlpo Järvinen tcp_reset_reno_sack(tp); 16771da177e4SLinus Torvalds 1678fe067e8aSDavid S. Miller tcp_for_write_queue(skb, sk) { 1679fe067e8aSDavid S. Miller if (skb == tcp_send_head(sk)) 1680fe067e8aSDavid S. Miller break; 1681d1a54c6aSIlpo Järvinen /* 1682d1a54c6aSIlpo Järvinen * Count the retransmission made on RTO correctly (only when 1683d1a54c6aSIlpo Järvinen * waiting for the first ACK and did not get it)... 1684d1a54c6aSIlpo Järvinen */ 1685d1a54c6aSIlpo Järvinen if ((tp->frto_counter == 1) && !(flag&FLAG_DATA_ACKED)) { 16860a9f2a46SIlpo Järvinen /* For some reason this R-bit might get cleared? */ 16870a9f2a46SIlpo Järvinen if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) 1688d1a54c6aSIlpo Järvinen tp->retrans_out += tcp_skb_pcount(skb); 1689d1a54c6aSIlpo Järvinen /* ...enter this if branch just for the first segment */ 1690d1a54c6aSIlpo Järvinen flag |= FLAG_DATA_ACKED; 1691d1a54c6aSIlpo Järvinen } else { 1692009a2e3eSIlpo Järvinen if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS) 1693009a2e3eSIlpo Järvinen tp->undo_marker = 0; 1694d1a54c6aSIlpo Järvinen TCP_SKB_CB(skb)->sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS); 1695d1a54c6aSIlpo Järvinen } 16961da177e4SLinus Torvalds 16979bff40fdSIlpo Järvinen /* Don't lost mark skbs that were fwd transmitted after RTO */ 16989bff40fdSIlpo Järvinen if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED) && 16999bff40fdSIlpo Järvinen !after(TCP_SKB_CB(skb)->end_seq, tp->frto_highmark)) { 17001da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; 17011da177e4SLinus Torvalds tp->lost_out += tcp_skb_pcount(skb); 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds } 1704005903bcSIlpo Järvinen tcp_verify_left_out(tp); 17051da177e4SLinus Torvalds 170695c4922bSIlpo Järvinen tp->snd_cwnd = tcp_packets_in_flight(tp) + allowed_segments; 17071da177e4SLinus Torvalds tp->snd_cwnd_cnt = 0; 17081da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 17091da177e4SLinus Torvalds tp->frto_counter = 0; 171016e90681SIlpo Järvinen tp->bytes_acked = 0; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds tp->reordering = min_t(unsigned int, tp->reordering, 17131da177e4SLinus Torvalds sysctl_tcp_reordering); 17146687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Loss); 17151da177e4SLinus Torvalds tp->high_seq = tp->frto_highmark; 17161da177e4SLinus Torvalds TCP_ECN_queue_cwr(tp); 17176a438bbeSStephen Hemminger 1718b7689205SIlpo Järvinen tcp_clear_retrans_hints_partial(tp); 17191da177e4SLinus Torvalds } 17201da177e4SLinus Torvalds 17214cd82999SIlpo Järvinen static void tcp_clear_retrans_partial(struct tcp_sock *tp) 17221da177e4SLinus Torvalds { 17231da177e4SLinus Torvalds tp->retrans_out = 0; 17241da177e4SLinus Torvalds tp->lost_out = 0; 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds tp->undo_marker = 0; 17271da177e4SLinus Torvalds tp->undo_retrans = 0; 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds 17304cd82999SIlpo Järvinen void tcp_clear_retrans(struct tcp_sock *tp) 17314cd82999SIlpo Järvinen { 17324cd82999SIlpo Järvinen tcp_clear_retrans_partial(tp); 17334cd82999SIlpo Järvinen 17344cd82999SIlpo Järvinen tp->fackets_out = 0; 17354cd82999SIlpo Järvinen tp->sacked_out = 0; 17364cd82999SIlpo Järvinen } 17374cd82999SIlpo Järvinen 17381da177e4SLinus Torvalds /* Enter Loss state. If "how" is not zero, forget all SACK information 17391da177e4SLinus Torvalds * and reset tags completely, otherwise preserve SACKs. If receiver 17401da177e4SLinus Torvalds * dropped its ofo queue, we will know this due to reneging detection. 17411da177e4SLinus Torvalds */ 17421da177e4SLinus Torvalds void tcp_enter_loss(struct sock *sk, int how) 17431da177e4SLinus Torvalds { 17446687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 17451da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 17461da177e4SLinus Torvalds struct sk_buff *skb; 17471da177e4SLinus Torvalds 17481da177e4SLinus Torvalds /* Reduce ssthresh if it has not yet been made inside this window. */ 17496687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_state <= TCP_CA_Disorder || tp->snd_una == tp->high_seq || 17506687e988SArnaldo Carvalho de Melo (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) { 17516687e988SArnaldo Carvalho de Melo tp->prior_ssthresh = tcp_current_ssthresh(sk); 17526687e988SArnaldo Carvalho de Melo tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk); 17536687e988SArnaldo Carvalho de Melo tcp_ca_event(sk, CA_EVENT_LOSS); 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds tp->snd_cwnd = 1; 17561da177e4SLinus Torvalds tp->snd_cwnd_cnt = 0; 17571da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 17581da177e4SLinus Torvalds 17599772efb9SStephen Hemminger tp->bytes_acked = 0; 17604cd82999SIlpo Järvinen tcp_clear_retrans_partial(tp); 17614cd82999SIlpo Järvinen 17624cd82999SIlpo Järvinen if (tcp_is_reno(tp)) 17634cd82999SIlpo Järvinen tcp_reset_reno_sack(tp); 17641da177e4SLinus Torvalds 1765b7689205SIlpo Järvinen if (!how) { 17661da177e4SLinus Torvalds /* Push undo marker, if it was plain RTO and nothing 17671da177e4SLinus Torvalds * was retransmitted. */ 17681da177e4SLinus Torvalds tp->undo_marker = tp->snd_una; 1769b7689205SIlpo Järvinen tcp_clear_retrans_hints_partial(tp); 1770b7689205SIlpo Järvinen } else { 17714cd82999SIlpo Järvinen tp->sacked_out = 0; 17724cd82999SIlpo Järvinen tp->fackets_out = 0; 1773b7689205SIlpo Järvinen tcp_clear_all_retrans_hints(tp); 1774b7689205SIlpo Järvinen } 17751da177e4SLinus Torvalds 1776fe067e8aSDavid S. Miller tcp_for_write_queue(skb, sk) { 1777fe067e8aSDavid S. Miller if (skb == tcp_send_head(sk)) 1778fe067e8aSDavid S. Miller break; 17794cd82999SIlpo Järvinen 17801da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->sacked&TCPCB_RETRANS) 17811da177e4SLinus Torvalds tp->undo_marker = 0; 17821da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED; 17831da177e4SLinus Torvalds if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED) || how) { 17841da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED; 17851da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; 17861da177e4SLinus Torvalds tp->lost_out += tcp_skb_pcount(skb); 17871da177e4SLinus Torvalds } 17881da177e4SLinus Torvalds } 1789005903bcSIlpo Järvinen tcp_verify_left_out(tp); 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds tp->reordering = min_t(unsigned int, tp->reordering, 17921da177e4SLinus Torvalds sysctl_tcp_reordering); 17936687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Loss); 17941da177e4SLinus Torvalds tp->high_seq = tp->snd_nxt; 17951da177e4SLinus Torvalds TCP_ECN_queue_cwr(tp); 1796580e572aSIlpo Järvinen /* Abort FRTO algorithm if one is in progress */ 1797580e572aSIlpo Järvinen tp->frto_counter = 0; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds 1800463c84b9SArnaldo Carvalho de Melo static int tcp_check_sack_reneging(struct sock *sk) 18011da177e4SLinus Torvalds { 18021da177e4SLinus Torvalds struct sk_buff *skb; 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds /* If ACK arrived pointing to a remembered SACK, 18051da177e4SLinus Torvalds * it means that our remembered SACKs do not reflect 18061da177e4SLinus Torvalds * real state of receiver i.e. 18071da177e4SLinus Torvalds * receiver _host_ is heavily congested (or buggy). 18081da177e4SLinus Torvalds * Do processing similar to RTO timeout. 18091da177e4SLinus Torvalds */ 1810fe067e8aSDavid S. Miller if ((skb = tcp_write_queue_head(sk)) != NULL && 18111da177e4SLinus Torvalds (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) { 18126687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 18131da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPSACKRENEGING); 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds tcp_enter_loss(sk, 1); 18166687e988SArnaldo Carvalho de Melo icsk->icsk_retransmits++; 1817fe067e8aSDavid S. Miller tcp_retransmit_skb(sk, tcp_write_queue_head(sk)); 1818463c84b9SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 18196687e988SArnaldo Carvalho de Melo icsk->icsk_rto, TCP_RTO_MAX); 18201da177e4SLinus Torvalds return 1; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds return 0; 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds static inline int tcp_fackets_out(struct tcp_sock *tp) 18261da177e4SLinus Torvalds { 1827e60402d0SIlpo Järvinen return tcp_is_reno(tp) ? tp->sacked_out+1 : tp->fackets_out; 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds 1830463c84b9SArnaldo Carvalho de Melo static inline int tcp_skb_timedout(struct sock *sk, struct sk_buff *skb) 18311da177e4SLinus Torvalds { 1832463c84b9SArnaldo Carvalho de Melo return (tcp_time_stamp - TCP_SKB_CB(skb)->when > inet_csk(sk)->icsk_rto); 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 18359e412ba7SIlpo Järvinen static inline int tcp_head_timedout(struct sock *sk) 18361da177e4SLinus Torvalds { 18379e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 18389e412ba7SIlpo Järvinen 18391da177e4SLinus Torvalds return tp->packets_out && 1840fe067e8aSDavid S. Miller tcp_skb_timedout(sk, tcp_write_queue_head(sk)); 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds /* Linux NewReno/SACK/FACK/ECN state machine. 18441da177e4SLinus Torvalds * -------------------------------------- 18451da177e4SLinus Torvalds * 18461da177e4SLinus Torvalds * "Open" Normal state, no dubious events, fast path. 18471da177e4SLinus Torvalds * "Disorder" In all the respects it is "Open", 18481da177e4SLinus Torvalds * but requires a bit more attention. It is entered when 18491da177e4SLinus Torvalds * we see some SACKs or dupacks. It is split of "Open" 18501da177e4SLinus Torvalds * mainly to move some processing from fast path to slow one. 18511da177e4SLinus Torvalds * "CWR" CWND was reduced due to some Congestion Notification event. 18521da177e4SLinus Torvalds * It can be ECN, ICMP source quench, local device congestion. 18531da177e4SLinus Torvalds * "Recovery" CWND was reduced, we are fast-retransmitting. 18541da177e4SLinus Torvalds * "Loss" CWND was reduced due to RTO timeout or SACK reneging. 18551da177e4SLinus Torvalds * 18561da177e4SLinus Torvalds * tcp_fastretrans_alert() is entered: 18571da177e4SLinus Torvalds * - each incoming ACK, if state is not "Open" 18581da177e4SLinus Torvalds * - when arrived ACK is unusual, namely: 18591da177e4SLinus Torvalds * * SACK 18601da177e4SLinus Torvalds * * Duplicate ACK. 18611da177e4SLinus Torvalds * * ECN ECE. 18621da177e4SLinus Torvalds * 18631da177e4SLinus Torvalds * Counting packets in flight is pretty simple. 18641da177e4SLinus Torvalds * 18651da177e4SLinus Torvalds * in_flight = packets_out - left_out + retrans_out 18661da177e4SLinus Torvalds * 18671da177e4SLinus Torvalds * packets_out is SND.NXT-SND.UNA counted in packets. 18681da177e4SLinus Torvalds * 18691da177e4SLinus Torvalds * retrans_out is number of retransmitted segments. 18701da177e4SLinus Torvalds * 18711da177e4SLinus Torvalds * left_out is number of segments left network, but not ACKed yet. 18721da177e4SLinus Torvalds * 18731da177e4SLinus Torvalds * left_out = sacked_out + lost_out 18741da177e4SLinus Torvalds * 18751da177e4SLinus Torvalds * sacked_out: Packets, which arrived to receiver out of order 18761da177e4SLinus Torvalds * and hence not ACKed. With SACKs this number is simply 18771da177e4SLinus Torvalds * amount of SACKed data. Even without SACKs 18781da177e4SLinus Torvalds * it is easy to give pretty reliable estimate of this number, 18791da177e4SLinus Torvalds * counting duplicate ACKs. 18801da177e4SLinus Torvalds * 18811da177e4SLinus Torvalds * lost_out: Packets lost by network. TCP has no explicit 18821da177e4SLinus Torvalds * "loss notification" feedback from network (for now). 18831da177e4SLinus Torvalds * It means that this number can be only _guessed_. 18841da177e4SLinus Torvalds * Actually, it is the heuristics to predict lossage that 18851da177e4SLinus Torvalds * distinguishes different algorithms. 18861da177e4SLinus Torvalds * 18871da177e4SLinus Torvalds * F.e. after RTO, when all the queue is considered as lost, 18881da177e4SLinus Torvalds * lost_out = packets_out and in_flight = retrans_out. 18891da177e4SLinus Torvalds * 18901da177e4SLinus Torvalds * Essentially, we have now two algorithms counting 18911da177e4SLinus Torvalds * lost packets. 18921da177e4SLinus Torvalds * 18931da177e4SLinus Torvalds * FACK: It is the simplest heuristics. As soon as we decided 18941da177e4SLinus Torvalds * that something is lost, we decide that _all_ not SACKed 18951da177e4SLinus Torvalds * packets until the most forward SACK are lost. I.e. 18961da177e4SLinus Torvalds * lost_out = fackets_out - sacked_out and left_out = fackets_out. 18971da177e4SLinus Torvalds * It is absolutely correct estimate, if network does not reorder 18981da177e4SLinus Torvalds * packets. And it loses any connection to reality when reordering 18991da177e4SLinus Torvalds * takes place. We use FACK by default until reordering 19001da177e4SLinus Torvalds * is suspected on the path to this destination. 19011da177e4SLinus Torvalds * 19021da177e4SLinus Torvalds * NewReno: when Recovery is entered, we assume that one segment 19031da177e4SLinus Torvalds * is lost (classic Reno). While we are in Recovery and 19041da177e4SLinus Torvalds * a partial ACK arrives, we assume that one more packet 19051da177e4SLinus Torvalds * is lost (NewReno). This heuristics are the same in NewReno 19061da177e4SLinus Torvalds * and SACK. 19071da177e4SLinus Torvalds * 19081da177e4SLinus Torvalds * Imagine, that's all! Forget about all this shamanism about CWND inflation 19091da177e4SLinus Torvalds * deflation etc. CWND is real congestion window, never inflated, changes 19101da177e4SLinus Torvalds * only according to classic VJ rules. 19111da177e4SLinus Torvalds * 19121da177e4SLinus Torvalds * Really tricky (and requiring careful tuning) part of algorithm 19131da177e4SLinus Torvalds * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue(). 19141da177e4SLinus Torvalds * The first determines the moment _when_ we should reduce CWND and, 19151da177e4SLinus Torvalds * hence, slow down forward transmission. In fact, it determines the moment 19161da177e4SLinus Torvalds * when we decide that hole is caused by loss, rather than by a reorder. 19171da177e4SLinus Torvalds * 19181da177e4SLinus Torvalds * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill 19191da177e4SLinus Torvalds * holes, caused by lost packets. 19201da177e4SLinus Torvalds * 19211da177e4SLinus Torvalds * And the most logically complicated part of algorithm is undo 19221da177e4SLinus Torvalds * heuristics. We detect false retransmits due to both too early 19231da177e4SLinus Torvalds * fast retransmit (reordering) and underestimated RTO, analyzing 19241da177e4SLinus Torvalds * timestamps and D-SACKs. When we detect that some segments were 19251da177e4SLinus Torvalds * retransmitted by mistake and CWND reduction was wrong, we undo 19261da177e4SLinus Torvalds * window reduction and abort recovery phase. This logic is hidden 19271da177e4SLinus Torvalds * inside several functions named tcp_try_undo_<something>. 19281da177e4SLinus Torvalds */ 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds /* This function decides, when we should leave Disordered state 19311da177e4SLinus Torvalds * and enter Recovery phase, reducing congestion window. 19321da177e4SLinus Torvalds * 19331da177e4SLinus Torvalds * Main question: may we further continue forward transmission 19341da177e4SLinus Torvalds * with the same cwnd? 19351da177e4SLinus Torvalds */ 19369e412ba7SIlpo Järvinen static int tcp_time_to_recover(struct sock *sk) 19371da177e4SLinus Torvalds { 19389e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 19391da177e4SLinus Torvalds __u32 packets_out; 19401da177e4SLinus Torvalds 194152c63f1eSIlpo Järvinen /* Do not perform any recovery during FRTO algorithm */ 194252c63f1eSIlpo Järvinen if (tp->frto_counter) 194352c63f1eSIlpo Järvinen return 0; 194452c63f1eSIlpo Järvinen 19451da177e4SLinus Torvalds /* Trick#1: The loss is proven. */ 19461da177e4SLinus Torvalds if (tp->lost_out) 19471da177e4SLinus Torvalds return 1; 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds /* Not-A-Trick#2 : Classic rule... */ 19501da177e4SLinus Torvalds if (tcp_fackets_out(tp) > tp->reordering) 19511da177e4SLinus Torvalds return 1; 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds /* Trick#3 : when we use RFC2988 timer restart, fast 19541da177e4SLinus Torvalds * retransmit can be triggered by timeout of queue head. 19551da177e4SLinus Torvalds */ 19569e412ba7SIlpo Järvinen if (tcp_head_timedout(sk)) 19571da177e4SLinus Torvalds return 1; 19581da177e4SLinus Torvalds 19591da177e4SLinus Torvalds /* Trick#4: It is still not OK... But will it be useful to delay 19601da177e4SLinus Torvalds * recovery more? 19611da177e4SLinus Torvalds */ 19621da177e4SLinus Torvalds packets_out = tp->packets_out; 19631da177e4SLinus Torvalds if (packets_out <= tp->reordering && 19641da177e4SLinus Torvalds tp->sacked_out >= max_t(__u32, packets_out/2, sysctl_tcp_reordering) && 19659e412ba7SIlpo Järvinen !tcp_may_send_now(sk)) { 19661da177e4SLinus Torvalds /* We have nothing to send. This connection is limited 19671da177e4SLinus Torvalds * either by receiver window or by application. 19681da177e4SLinus Torvalds */ 19691da177e4SLinus Torvalds return 1; 19701da177e4SLinus Torvalds } 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds return 0; 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds 1975d8f4f223SIlpo Järvinen /* RFC: This is from the original, I doubt that this is necessary at all: 1976d8f4f223SIlpo Järvinen * clear xmit_retrans hint if seq of this skb is beyond hint. How could we 1977d8f4f223SIlpo Järvinen * retransmitted past LOST markings in the first place? I'm not fully sure 1978d8f4f223SIlpo Järvinen * about undo and end of connection cases, which can cause R without L? 1979d8f4f223SIlpo Järvinen */ 1980d8f4f223SIlpo Järvinen static void tcp_verify_retransmit_hint(struct tcp_sock *tp, 1981d8f4f223SIlpo Järvinen struct sk_buff *skb) 1982d8f4f223SIlpo Järvinen { 1983d8f4f223SIlpo Järvinen if ((tp->retransmit_skb_hint != NULL) && 1984d8f4f223SIlpo Järvinen before(TCP_SKB_CB(skb)->seq, 1985d8f4f223SIlpo Järvinen TCP_SKB_CB(tp->retransmit_skb_hint)->seq)) 198619b2b486SIlpo Järvinen tp->retransmit_skb_hint = NULL; 1987d8f4f223SIlpo Järvinen } 1988d8f4f223SIlpo Järvinen 19891da177e4SLinus Torvalds /* Mark head of queue up as lost. */ 19909e412ba7SIlpo Järvinen static void tcp_mark_head_lost(struct sock *sk, 19911da177e4SLinus Torvalds int packets, u32 high_seq) 19921da177e4SLinus Torvalds { 19939e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 19941da177e4SLinus Torvalds struct sk_buff *skb; 19956a438bbeSStephen Hemminger int cnt; 19961da177e4SLinus Torvalds 19976a438bbeSStephen Hemminger BUG_TRAP(packets <= tp->packets_out); 19986a438bbeSStephen Hemminger if (tp->lost_skb_hint) { 19996a438bbeSStephen Hemminger skb = tp->lost_skb_hint; 20006a438bbeSStephen Hemminger cnt = tp->lost_cnt_hint; 20016a438bbeSStephen Hemminger } else { 2002fe067e8aSDavid S. Miller skb = tcp_write_queue_head(sk); 20036a438bbeSStephen Hemminger cnt = 0; 20046a438bbeSStephen Hemminger } 20051da177e4SLinus Torvalds 2006fe067e8aSDavid S. Miller tcp_for_write_queue_from(skb, sk) { 2007fe067e8aSDavid S. Miller if (skb == tcp_send_head(sk)) 2008fe067e8aSDavid S. Miller break; 20096a438bbeSStephen Hemminger /* TODO: do this better */ 20106a438bbeSStephen Hemminger /* this is not the most efficient way to do this... */ 20116a438bbeSStephen Hemminger tp->lost_skb_hint = skb; 20126a438bbeSStephen Hemminger tp->lost_cnt_hint = cnt; 20136a438bbeSStephen Hemminger cnt += tcp_skb_pcount(skb); 20146a438bbeSStephen Hemminger if (cnt > packets || after(TCP_SKB_CB(skb)->end_seq, high_seq)) 20151da177e4SLinus Torvalds break; 20163eec0047SIlpo Järvinen if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_SACKED_ACKED|TCPCB_LOST))) { 20171da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; 20181da177e4SLinus Torvalds tp->lost_out += tcp_skb_pcount(skb); 2019d8f4f223SIlpo Järvinen tcp_verify_retransmit_hint(tp, skb); 20201da177e4SLinus Torvalds } 20211da177e4SLinus Torvalds } 2022005903bcSIlpo Järvinen tcp_verify_left_out(tp); 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 20251da177e4SLinus Torvalds /* Account newly detected lost packet(s) */ 20261da177e4SLinus Torvalds 20279e412ba7SIlpo Järvinen static void tcp_update_scoreboard(struct sock *sk) 20281da177e4SLinus Torvalds { 20299e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 20309e412ba7SIlpo Järvinen 2031e60402d0SIlpo Järvinen if (tcp_is_fack(tp)) { 20321da177e4SLinus Torvalds int lost = tp->fackets_out - tp->reordering; 20331da177e4SLinus Torvalds if (lost <= 0) 20341da177e4SLinus Torvalds lost = 1; 20359e412ba7SIlpo Järvinen tcp_mark_head_lost(sk, lost, tp->high_seq); 20361da177e4SLinus Torvalds } else { 20379e412ba7SIlpo Järvinen tcp_mark_head_lost(sk, 1, tp->high_seq); 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds /* New heuristics: it is possible only after we switched 20411da177e4SLinus Torvalds * to restart timer each time when something is ACKed. 20421da177e4SLinus Torvalds * Hence, we can detect timed out packets during fast 20431da177e4SLinus Torvalds * retransmit without falling to slow start. 20441da177e4SLinus Torvalds */ 2045e60402d0SIlpo Järvinen if (!tcp_is_reno(tp) && tcp_head_timedout(sk)) { 20461da177e4SLinus Torvalds struct sk_buff *skb; 20471da177e4SLinus Torvalds 20486a438bbeSStephen Hemminger skb = tp->scoreboard_skb_hint ? tp->scoreboard_skb_hint 2049fe067e8aSDavid S. Miller : tcp_write_queue_head(sk); 20506a438bbeSStephen Hemminger 2051fe067e8aSDavid S. Miller tcp_for_write_queue_from(skb, sk) { 2052fe067e8aSDavid S. Miller if (skb == tcp_send_head(sk)) 2053fe067e8aSDavid S. Miller break; 20546a438bbeSStephen Hemminger if (!tcp_skb_timedout(sk, skb)) 20556a438bbeSStephen Hemminger break; 20566a438bbeSStephen Hemminger 20576a438bbeSStephen Hemminger if (!(TCP_SKB_CB(skb)->sacked&TCPCB_TAGBITS)) { 20581da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked |= TCPCB_LOST; 20591da177e4SLinus Torvalds tp->lost_out += tcp_skb_pcount(skb); 2060d8f4f223SIlpo Järvinen tcp_verify_retransmit_hint(tp, skb); 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds } 20636a438bbeSStephen Hemminger 20646a438bbeSStephen Hemminger tp->scoreboard_skb_hint = skb; 20656a438bbeSStephen Hemminger 2066005903bcSIlpo Järvinen tcp_verify_left_out(tp); 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds /* CWND moderation, preventing bursts due to too big ACKs 20711da177e4SLinus Torvalds * in dubious situations. 20721da177e4SLinus Torvalds */ 20731da177e4SLinus Torvalds static inline void tcp_moderate_cwnd(struct tcp_sock *tp) 20741da177e4SLinus Torvalds { 20751da177e4SLinus Torvalds tp->snd_cwnd = min(tp->snd_cwnd, 20761da177e4SLinus Torvalds tcp_packets_in_flight(tp)+tcp_max_burst(tp)); 20771da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 208072dc5b92SStephen Hemminger /* Lower bound on congestion window is slow start threshold 208172dc5b92SStephen Hemminger * unless congestion avoidance choice decides to overide it. 208272dc5b92SStephen Hemminger */ 208372dc5b92SStephen Hemminger static inline u32 tcp_cwnd_min(const struct sock *sk) 208472dc5b92SStephen Hemminger { 208572dc5b92SStephen Hemminger const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; 208672dc5b92SStephen Hemminger 208772dc5b92SStephen Hemminger return ca_ops->min_cwnd ? ca_ops->min_cwnd(sk) : tcp_sk(sk)->snd_ssthresh; 208872dc5b92SStephen Hemminger } 208972dc5b92SStephen Hemminger 20901da177e4SLinus Torvalds /* Decrease cwnd each second ack. */ 20911e757f99SIlpo Järvinen static void tcp_cwnd_down(struct sock *sk, int flag) 20921da177e4SLinus Torvalds { 20936687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 20941da177e4SLinus Torvalds int decr = tp->snd_cwnd_cnt + 1; 20951da177e4SLinus Torvalds 209649ff4bb4SIlpo Järvinen if ((flag&(FLAG_ANY_PROGRESS|FLAG_DSACKING_ACK)) || 2097e60402d0SIlpo Järvinen (tcp_is_reno(tp) && !(flag&FLAG_NOT_DUP))) { 20981da177e4SLinus Torvalds tp->snd_cwnd_cnt = decr&1; 20991da177e4SLinus Torvalds decr >>= 1; 21001da177e4SLinus Torvalds 210172dc5b92SStephen Hemminger if (decr && tp->snd_cwnd > tcp_cwnd_min(sk)) 21021da177e4SLinus Torvalds tp->snd_cwnd -= decr; 21031da177e4SLinus Torvalds 21041da177e4SLinus Torvalds tp->snd_cwnd = min(tp->snd_cwnd, tcp_packets_in_flight(tp)+1); 21051da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 21061da177e4SLinus Torvalds } 21071e757f99SIlpo Järvinen } 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds /* Nothing was retransmitted or returned timestamp is less 21101da177e4SLinus Torvalds * than timestamp of the first retransmission. 21111da177e4SLinus Torvalds */ 21121da177e4SLinus Torvalds static inline int tcp_packet_delayed(struct tcp_sock *tp) 21131da177e4SLinus Torvalds { 21141da177e4SLinus Torvalds return !tp->retrans_stamp || 21151da177e4SLinus Torvalds (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && 21161da177e4SLinus Torvalds (__s32)(tp->rx_opt.rcv_tsecr - tp->retrans_stamp) < 0); 21171da177e4SLinus Torvalds } 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds /* Undo procedures. */ 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 1 21229e412ba7SIlpo Järvinen static void DBGUNDO(struct sock *sk, const char *msg) 21231da177e4SLinus Torvalds { 21249e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 21251da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk); 21269e412ba7SIlpo Järvinen 21271da177e4SLinus Torvalds printk(KERN_DEBUG "Undo %s %u.%u.%u.%u/%u c%u l%u ss%u/%u p%u\n", 21281da177e4SLinus Torvalds msg, 21291da177e4SLinus Torvalds NIPQUAD(inet->daddr), ntohs(inet->dport), 213083ae4088SIlpo Järvinen tp->snd_cwnd, tcp_left_out(tp), 21311da177e4SLinus Torvalds tp->snd_ssthresh, tp->prior_ssthresh, 21321da177e4SLinus Torvalds tp->packets_out); 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds #else 21351da177e4SLinus Torvalds #define DBGUNDO(x...) do { } while (0) 21361da177e4SLinus Torvalds #endif 21371da177e4SLinus Torvalds 21386687e988SArnaldo Carvalho de Melo static void tcp_undo_cwr(struct sock *sk, const int undo) 21391da177e4SLinus Torvalds { 21406687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 21416687e988SArnaldo Carvalho de Melo 21421da177e4SLinus Torvalds if (tp->prior_ssthresh) { 21436687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 21446687e988SArnaldo Carvalho de Melo 21456687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_ops->undo_cwnd) 21466687e988SArnaldo Carvalho de Melo tp->snd_cwnd = icsk->icsk_ca_ops->undo_cwnd(sk); 21471da177e4SLinus Torvalds else 21481da177e4SLinus Torvalds tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh<<1); 21491da177e4SLinus Torvalds 21501da177e4SLinus Torvalds if (undo && tp->prior_ssthresh > tp->snd_ssthresh) { 21511da177e4SLinus Torvalds tp->snd_ssthresh = tp->prior_ssthresh; 21521da177e4SLinus Torvalds TCP_ECN_withdraw_cwr(tp); 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds } else { 21551da177e4SLinus Torvalds tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh); 21561da177e4SLinus Torvalds } 21571da177e4SLinus Torvalds tcp_moderate_cwnd(tp); 21581da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 21596a438bbeSStephen Hemminger 21606a438bbeSStephen Hemminger /* There is something screwy going on with the retrans hints after 21616a438bbeSStephen Hemminger an undo */ 21625af4ec23SIlpo Järvinen tcp_clear_all_retrans_hints(tp); 21631da177e4SLinus Torvalds } 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds static inline int tcp_may_undo(struct tcp_sock *tp) 21661da177e4SLinus Torvalds { 21671da177e4SLinus Torvalds return tp->undo_marker && 21681da177e4SLinus Torvalds (!tp->undo_retrans || tcp_packet_delayed(tp)); 21691da177e4SLinus Torvalds } 21701da177e4SLinus Torvalds 21711da177e4SLinus Torvalds /* People celebrate: "We love our President!" */ 21729e412ba7SIlpo Järvinen static int tcp_try_undo_recovery(struct sock *sk) 21731da177e4SLinus Torvalds { 21749e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 21759e412ba7SIlpo Järvinen 21761da177e4SLinus Torvalds if (tcp_may_undo(tp)) { 21771da177e4SLinus Torvalds /* Happy end! We did not retransmit anything 21781da177e4SLinus Torvalds * or our original transmission succeeded. 21791da177e4SLinus Torvalds */ 21809e412ba7SIlpo Järvinen DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans"); 21816687e988SArnaldo Carvalho de Melo tcp_undo_cwr(sk, 1); 21826687e988SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss) 21831da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPLOSSUNDO); 21841da177e4SLinus Torvalds else 21851da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPFULLUNDO); 21861da177e4SLinus Torvalds tp->undo_marker = 0; 21871da177e4SLinus Torvalds } 2188e60402d0SIlpo Järvinen if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) { 21891da177e4SLinus Torvalds /* Hold old state until something *above* high_seq 21901da177e4SLinus Torvalds * is ACKed. For Reno it is MUST to prevent false 21911da177e4SLinus Torvalds * fast retransmits (RFC2582). SACK TCP is safe. */ 21921da177e4SLinus Torvalds tcp_moderate_cwnd(tp); 21931da177e4SLinus Torvalds return 1; 21941da177e4SLinus Torvalds } 21956687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open); 21961da177e4SLinus Torvalds return 0; 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds /* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */ 22009e412ba7SIlpo Järvinen static void tcp_try_undo_dsack(struct sock *sk) 22011da177e4SLinus Torvalds { 22029e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 22039e412ba7SIlpo Järvinen 22041da177e4SLinus Torvalds if (tp->undo_marker && !tp->undo_retrans) { 22059e412ba7SIlpo Järvinen DBGUNDO(sk, "D-SACK"); 22066687e988SArnaldo Carvalho de Melo tcp_undo_cwr(sk, 1); 22071da177e4SLinus Torvalds tp->undo_marker = 0; 22081da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPDSACKUNDO); 22091da177e4SLinus Torvalds } 22101da177e4SLinus Torvalds } 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds /* Undo during fast recovery after partial ACK. */ 22131da177e4SLinus Torvalds 22149e412ba7SIlpo Järvinen static int tcp_try_undo_partial(struct sock *sk, int acked) 22151da177e4SLinus Torvalds { 22169e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 22171da177e4SLinus Torvalds /* Partial ACK arrived. Force Hoe's retransmit. */ 2218e60402d0SIlpo Järvinen int failed = tcp_is_reno(tp) || tp->fackets_out>tp->reordering; 22191da177e4SLinus Torvalds 22201da177e4SLinus Torvalds if (tcp_may_undo(tp)) { 22211da177e4SLinus Torvalds /* Plain luck! Hole if filled with delayed 22221da177e4SLinus Torvalds * packet, rather than with a retransmit. 22231da177e4SLinus Torvalds */ 22241da177e4SLinus Torvalds if (tp->retrans_out == 0) 22251da177e4SLinus Torvalds tp->retrans_stamp = 0; 22261da177e4SLinus Torvalds 22276687e988SArnaldo Carvalho de Melo tcp_update_reordering(sk, tcp_fackets_out(tp) + acked, 1); 22281da177e4SLinus Torvalds 22299e412ba7SIlpo Järvinen DBGUNDO(sk, "Hoe"); 22306687e988SArnaldo Carvalho de Melo tcp_undo_cwr(sk, 0); 22311da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPPARTIALUNDO); 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds /* So... Do not make Hoe's retransmit yet. 22341da177e4SLinus Torvalds * If the first packet was delayed, the rest 22351da177e4SLinus Torvalds * ones are most probably delayed as well. 22361da177e4SLinus Torvalds */ 22371da177e4SLinus Torvalds failed = 0; 22381da177e4SLinus Torvalds } 22391da177e4SLinus Torvalds return failed; 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds /* Undo during loss recovery after partial ACK. */ 22439e412ba7SIlpo Järvinen static int tcp_try_undo_loss(struct sock *sk) 22441da177e4SLinus Torvalds { 22459e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 22469e412ba7SIlpo Järvinen 22471da177e4SLinus Torvalds if (tcp_may_undo(tp)) { 22481da177e4SLinus Torvalds struct sk_buff *skb; 2249fe067e8aSDavid S. Miller tcp_for_write_queue(skb, sk) { 2250fe067e8aSDavid S. Miller if (skb == tcp_send_head(sk)) 2251fe067e8aSDavid S. Miller break; 22521da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST; 22531da177e4SLinus Torvalds } 22546a438bbeSStephen Hemminger 22555af4ec23SIlpo Järvinen tcp_clear_all_retrans_hints(tp); 22566a438bbeSStephen Hemminger 22579e412ba7SIlpo Järvinen DBGUNDO(sk, "partial loss"); 22581da177e4SLinus Torvalds tp->lost_out = 0; 22596687e988SArnaldo Carvalho de Melo tcp_undo_cwr(sk, 1); 22601da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPLOSSUNDO); 2261463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_retransmits = 0; 22621da177e4SLinus Torvalds tp->undo_marker = 0; 2263e60402d0SIlpo Järvinen if (tcp_is_sack(tp)) 22646687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open); 22651da177e4SLinus Torvalds return 1; 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds return 0; 22681da177e4SLinus Torvalds } 22691da177e4SLinus Torvalds 22706687e988SArnaldo Carvalho de Melo static inline void tcp_complete_cwr(struct sock *sk) 22711da177e4SLinus Torvalds { 22726687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 22731da177e4SLinus Torvalds tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh); 22741da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 22756687e988SArnaldo Carvalho de Melo tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR); 22761da177e4SLinus Torvalds } 22771da177e4SLinus Torvalds 22789e412ba7SIlpo Järvinen static void tcp_try_to_open(struct sock *sk, int flag) 22791da177e4SLinus Torvalds { 22809e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 22819e412ba7SIlpo Järvinen 228286426c22SIlpo Järvinen tcp_verify_left_out(tp); 228386426c22SIlpo Järvinen 22841da177e4SLinus Torvalds if (tp->retrans_out == 0) 22851da177e4SLinus Torvalds tp->retrans_stamp = 0; 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds if (flag&FLAG_ECE) 22883cfe3baaSIlpo Järvinen tcp_enter_cwr(sk, 1); 22891da177e4SLinus Torvalds 22906687e988SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) { 22911da177e4SLinus Torvalds int state = TCP_CA_Open; 22921da177e4SLinus Torvalds 2293d02596e3SIlpo Järvinen if (tcp_left_out(tp) || tp->retrans_out || tp->undo_marker) 22941da177e4SLinus Torvalds state = TCP_CA_Disorder; 22951da177e4SLinus Torvalds 22966687e988SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_ca_state != state) { 22976687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, state); 22981da177e4SLinus Torvalds tp->high_seq = tp->snd_nxt; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds tcp_moderate_cwnd(tp); 23011da177e4SLinus Torvalds } else { 23021e757f99SIlpo Järvinen tcp_cwnd_down(sk, flag); 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds } 23051da177e4SLinus Torvalds 23065d424d5aSJohn Heffner static void tcp_mtup_probe_failed(struct sock *sk) 23075d424d5aSJohn Heffner { 23085d424d5aSJohn Heffner struct inet_connection_sock *icsk = inet_csk(sk); 23095d424d5aSJohn Heffner 23105d424d5aSJohn Heffner icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1; 23115d424d5aSJohn Heffner icsk->icsk_mtup.probe_size = 0; 23125d424d5aSJohn Heffner } 23135d424d5aSJohn Heffner 23145d424d5aSJohn Heffner static void tcp_mtup_probe_success(struct sock *sk, struct sk_buff *skb) 23155d424d5aSJohn Heffner { 23165d424d5aSJohn Heffner struct tcp_sock *tp = tcp_sk(sk); 23175d424d5aSJohn Heffner struct inet_connection_sock *icsk = inet_csk(sk); 23185d424d5aSJohn Heffner 23195d424d5aSJohn Heffner /* FIXME: breaks with very large cwnd */ 23205d424d5aSJohn Heffner tp->prior_ssthresh = tcp_current_ssthresh(sk); 23215d424d5aSJohn Heffner tp->snd_cwnd = tp->snd_cwnd * 23225d424d5aSJohn Heffner tcp_mss_to_mtu(sk, tp->mss_cache) / 23235d424d5aSJohn Heffner icsk->icsk_mtup.probe_size; 23245d424d5aSJohn Heffner tp->snd_cwnd_cnt = 0; 23255d424d5aSJohn Heffner tp->snd_cwnd_stamp = tcp_time_stamp; 23265d424d5aSJohn Heffner tp->rcv_ssthresh = tcp_current_ssthresh(sk); 23275d424d5aSJohn Heffner 23285d424d5aSJohn Heffner icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size; 23295d424d5aSJohn Heffner icsk->icsk_mtup.probe_size = 0; 23305d424d5aSJohn Heffner tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 23315d424d5aSJohn Heffner } 23325d424d5aSJohn Heffner 23335d424d5aSJohn Heffner 23341da177e4SLinus Torvalds /* Process an event, which can update packets-in-flight not trivially. 23351da177e4SLinus Torvalds * Main goal of this function is to calculate new estimate for left_out, 23361da177e4SLinus Torvalds * taking into account both packets sitting in receiver's buffer and 23371da177e4SLinus Torvalds * packets lost by network. 23381da177e4SLinus Torvalds * 23391da177e4SLinus Torvalds * Besides that it does CWND reduction, when packet loss is detected 23401da177e4SLinus Torvalds * and changes state of machine. 23411da177e4SLinus Torvalds * 23421da177e4SLinus Torvalds * It does _not_ decide what to send, it is made in function 23431da177e4SLinus Torvalds * tcp_xmit_retransmit_queue(). 23441da177e4SLinus Torvalds */ 23451da177e4SLinus Torvalds static void 23461b6d427bSIlpo Järvinen tcp_fastretrans_alert(struct sock *sk, int pkts_acked, int flag) 23471da177e4SLinus Torvalds { 23486687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 23491da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 23502e605294SIlpo Järvinen int is_dupack = !(flag&(FLAG_SND_UNA_ADVANCED|FLAG_NOT_DUP)); 23512e605294SIlpo Järvinen int do_lost = is_dupack || ((flag&FLAG_DATA_SACKED) && 23522e605294SIlpo Järvinen (tp->fackets_out > tp->reordering)); 23531da177e4SLinus Torvalds 23541da177e4SLinus Torvalds /* Some technical things: 23551da177e4SLinus Torvalds * 1. Reno does not count dupacks (sacked_out) automatically. */ 23561da177e4SLinus Torvalds if (!tp->packets_out) 23571da177e4SLinus Torvalds tp->sacked_out = 0; 235891fed7a1SIlpo Järvinen 235991fed7a1SIlpo Järvinen if (WARN_ON(!tp->sacked_out && tp->fackets_out)) 23601da177e4SLinus Torvalds tp->fackets_out = 0; 23611da177e4SLinus Torvalds 23621da177e4SLinus Torvalds /* Now state machine starts. 23631da177e4SLinus Torvalds * A. ECE, hence prohibit cwnd undoing, the reduction is required. */ 23641da177e4SLinus Torvalds if (flag&FLAG_ECE) 23651da177e4SLinus Torvalds tp->prior_ssthresh = 0; 23661da177e4SLinus Torvalds 23671da177e4SLinus Torvalds /* B. In all the states check for reneging SACKs. */ 2368463c84b9SArnaldo Carvalho de Melo if (tp->sacked_out && tcp_check_sack_reneging(sk)) 23691da177e4SLinus Torvalds return; 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds /* C. Process data loss notification, provided it is valid. */ 23721da177e4SLinus Torvalds if ((flag&FLAG_DATA_LOST) && 23731da177e4SLinus Torvalds before(tp->snd_una, tp->high_seq) && 23746687e988SArnaldo Carvalho de Melo icsk->icsk_ca_state != TCP_CA_Open && 23751da177e4SLinus Torvalds tp->fackets_out > tp->reordering) { 23769e412ba7SIlpo Järvinen tcp_mark_head_lost(sk, tp->fackets_out-tp->reordering, tp->high_seq); 23771da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPLOSS); 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds 2380005903bcSIlpo Järvinen /* D. Check consistency of the current state. */ 2381005903bcSIlpo Järvinen tcp_verify_left_out(tp); 23821da177e4SLinus Torvalds 23831da177e4SLinus Torvalds /* E. Check state exit conditions. State can be terminated 23841da177e4SLinus Torvalds * when high_seq is ACKed. */ 23856687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_state == TCP_CA_Open) { 23861da177e4SLinus Torvalds BUG_TRAP(tp->retrans_out == 0); 23871da177e4SLinus Torvalds tp->retrans_stamp = 0; 23881da177e4SLinus Torvalds } else if (!before(tp->snd_una, tp->high_seq)) { 23896687e988SArnaldo Carvalho de Melo switch (icsk->icsk_ca_state) { 23901da177e4SLinus Torvalds case TCP_CA_Loss: 23916687e988SArnaldo Carvalho de Melo icsk->icsk_retransmits = 0; 23929e412ba7SIlpo Järvinen if (tcp_try_undo_recovery(sk)) 23931da177e4SLinus Torvalds return; 23941da177e4SLinus Torvalds break; 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds case TCP_CA_CWR: 23971da177e4SLinus Torvalds /* CWR is to be held something *above* high_seq 23981da177e4SLinus Torvalds * is ACKed for CWR bit to reach receiver. */ 23991da177e4SLinus Torvalds if (tp->snd_una != tp->high_seq) { 24006687e988SArnaldo Carvalho de Melo tcp_complete_cwr(sk); 24016687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open); 24021da177e4SLinus Torvalds } 24031da177e4SLinus Torvalds break; 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds case TCP_CA_Disorder: 24069e412ba7SIlpo Järvinen tcp_try_undo_dsack(sk); 24071da177e4SLinus Torvalds if (!tp->undo_marker || 24081da177e4SLinus Torvalds /* For SACK case do not Open to allow to undo 24091da177e4SLinus Torvalds * catching for all duplicate ACKs. */ 2410e60402d0SIlpo Järvinen tcp_is_reno(tp) || tp->snd_una != tp->high_seq) { 24111da177e4SLinus Torvalds tp->undo_marker = 0; 24126687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open); 24131da177e4SLinus Torvalds } 24141da177e4SLinus Torvalds break; 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds case TCP_CA_Recovery: 2417e60402d0SIlpo Järvinen if (tcp_is_reno(tp)) 24181da177e4SLinus Torvalds tcp_reset_reno_sack(tp); 24199e412ba7SIlpo Järvinen if (tcp_try_undo_recovery(sk)) 24201da177e4SLinus Torvalds return; 24216687e988SArnaldo Carvalho de Melo tcp_complete_cwr(sk); 24221da177e4SLinus Torvalds break; 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds } 24251da177e4SLinus Torvalds 24261da177e4SLinus Torvalds /* F. Process state. */ 24276687e988SArnaldo Carvalho de Melo switch (icsk->icsk_ca_state) { 24281da177e4SLinus Torvalds case TCP_CA_Recovery: 24292e605294SIlpo Järvinen if (!(flag & FLAG_SND_UNA_ADVANCED)) { 2430e60402d0SIlpo Järvinen if (tcp_is_reno(tp) && is_dupack) 24316687e988SArnaldo Carvalho de Melo tcp_add_reno_sack(sk); 24321b6d427bSIlpo Järvinen } else 24331b6d427bSIlpo Järvinen do_lost = tcp_try_undo_partial(sk, pkts_acked); 24341da177e4SLinus Torvalds break; 24351da177e4SLinus Torvalds case TCP_CA_Loss: 24361da177e4SLinus Torvalds if (flag&FLAG_DATA_ACKED) 24376687e988SArnaldo Carvalho de Melo icsk->icsk_retransmits = 0; 24389e412ba7SIlpo Järvinen if (!tcp_try_undo_loss(sk)) { 24391da177e4SLinus Torvalds tcp_moderate_cwnd(tp); 24401da177e4SLinus Torvalds tcp_xmit_retransmit_queue(sk); 24411da177e4SLinus Torvalds return; 24421da177e4SLinus Torvalds } 24436687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_state != TCP_CA_Open) 24441da177e4SLinus Torvalds return; 24451da177e4SLinus Torvalds /* Loss is undone; fall through to processing in Open state. */ 24461da177e4SLinus Torvalds default: 2447e60402d0SIlpo Järvinen if (tcp_is_reno(tp)) { 24482e605294SIlpo Järvinen if (flag & FLAG_SND_UNA_ADVANCED) 24491da177e4SLinus Torvalds tcp_reset_reno_sack(tp); 24501da177e4SLinus Torvalds if (is_dupack) 24516687e988SArnaldo Carvalho de Melo tcp_add_reno_sack(sk); 24521da177e4SLinus Torvalds } 24531da177e4SLinus Torvalds 24546687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_state == TCP_CA_Disorder) 24559e412ba7SIlpo Järvinen tcp_try_undo_dsack(sk); 24561da177e4SLinus Torvalds 24579e412ba7SIlpo Järvinen if (!tcp_time_to_recover(sk)) { 24589e412ba7SIlpo Järvinen tcp_try_to_open(sk, flag); 24591da177e4SLinus Torvalds return; 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds 24625d424d5aSJohn Heffner /* MTU probe failure: don't reduce cwnd */ 24635d424d5aSJohn Heffner if (icsk->icsk_ca_state < TCP_CA_CWR && 24645d424d5aSJohn Heffner icsk->icsk_mtup.probe_size && 24650e7b1368SJohn Heffner tp->snd_una == tp->mtu_probe.probe_seq_start) { 24665d424d5aSJohn Heffner tcp_mtup_probe_failed(sk); 24675d424d5aSJohn Heffner /* Restores the reduction we did in tcp_mtup_probe() */ 24685d424d5aSJohn Heffner tp->snd_cwnd++; 24695d424d5aSJohn Heffner tcp_simple_retransmit(sk); 24705d424d5aSJohn Heffner return; 24715d424d5aSJohn Heffner } 24725d424d5aSJohn Heffner 24731da177e4SLinus Torvalds /* Otherwise enter Recovery state */ 24741da177e4SLinus Torvalds 2475e60402d0SIlpo Järvinen if (tcp_is_reno(tp)) 24761da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPRENORECOVERY); 24771da177e4SLinus Torvalds else 24781da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPSACKRECOVERY); 24791da177e4SLinus Torvalds 24801da177e4SLinus Torvalds tp->high_seq = tp->snd_nxt; 24811da177e4SLinus Torvalds tp->prior_ssthresh = 0; 24821da177e4SLinus Torvalds tp->undo_marker = tp->snd_una; 24831da177e4SLinus Torvalds tp->undo_retrans = tp->retrans_out; 24841da177e4SLinus Torvalds 24856687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_state < TCP_CA_CWR) { 24861da177e4SLinus Torvalds if (!(flag&FLAG_ECE)) 24876687e988SArnaldo Carvalho de Melo tp->prior_ssthresh = tcp_current_ssthresh(sk); 24886687e988SArnaldo Carvalho de Melo tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk); 24891da177e4SLinus Torvalds TCP_ECN_queue_cwr(tp); 24901da177e4SLinus Torvalds } 24911da177e4SLinus Torvalds 24929772efb9SStephen Hemminger tp->bytes_acked = 0; 24931da177e4SLinus Torvalds tp->snd_cwnd_cnt = 0; 24946687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Recovery); 24951da177e4SLinus Torvalds } 24961da177e4SLinus Torvalds 24972e605294SIlpo Järvinen if (do_lost || tcp_head_timedout(sk)) 24989e412ba7SIlpo Järvinen tcp_update_scoreboard(sk); 24991e757f99SIlpo Järvinen tcp_cwnd_down(sk, flag); 25001da177e4SLinus Torvalds tcp_xmit_retransmit_queue(sk); 25011da177e4SLinus Torvalds } 25021da177e4SLinus Torvalds 25031da177e4SLinus Torvalds /* Read draft-ietf-tcplw-high-performance before mucking 2504caa20d9aSStephen Hemminger * with this code. (Supersedes RFC1323) 25051da177e4SLinus Torvalds */ 25062d2abbabSStephen Hemminger static void tcp_ack_saw_tstamp(struct sock *sk, int flag) 25071da177e4SLinus Torvalds { 25081da177e4SLinus Torvalds /* RTTM Rule: A TSecr value received in a segment is used to 25091da177e4SLinus Torvalds * update the averaged RTT measurement only if the segment 25101da177e4SLinus Torvalds * acknowledges some new data, i.e., only if it advances the 25111da177e4SLinus Torvalds * left edge of the send window. 25121da177e4SLinus Torvalds * 25131da177e4SLinus Torvalds * See draft-ietf-tcplw-high-performance-00, section 3.3. 25141da177e4SLinus Torvalds * 1998/04/10 Andrey V. Savochkin <saw@msu.ru> 25151da177e4SLinus Torvalds * 25161da177e4SLinus Torvalds * Changed: reset backoff as soon as we see the first valid sample. 2517caa20d9aSStephen Hemminger * If we do not, we get strongly overestimated rto. With timestamps 25181da177e4SLinus Torvalds * samples are accepted even from very old segments: f.e., when rtt=1 25191da177e4SLinus Torvalds * increases to 8, we retransmit 5 times and after 8 seconds delayed 25201da177e4SLinus Torvalds * answer arrives rto becomes 120 seconds! If at least one of segments 25211da177e4SLinus Torvalds * in window is lost... Voila. --ANK (010210) 25221da177e4SLinus Torvalds */ 2523463c84b9SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 2524463c84b9SArnaldo Carvalho de Melo const __u32 seq_rtt = tcp_time_stamp - tp->rx_opt.rcv_tsecr; 25252d2abbabSStephen Hemminger tcp_rtt_estimator(sk, seq_rtt); 2526463c84b9SArnaldo Carvalho de Melo tcp_set_rto(sk); 2527463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_backoff = 0; 2528463c84b9SArnaldo Carvalho de Melo tcp_bound_rto(sk); 25291da177e4SLinus Torvalds } 25301da177e4SLinus Torvalds 25312d2abbabSStephen Hemminger static void tcp_ack_no_tstamp(struct sock *sk, u32 seq_rtt, int flag) 25321da177e4SLinus Torvalds { 25331da177e4SLinus Torvalds /* We don't have a timestamp. Can only use 25341da177e4SLinus Torvalds * packets that are not retransmitted to determine 25351da177e4SLinus Torvalds * rtt estimates. Also, we must not reset the 25361da177e4SLinus Torvalds * backoff for rto until we get a non-retransmitted 25371da177e4SLinus Torvalds * packet. This allows us to deal with a situation 25381da177e4SLinus Torvalds * where the network delay has increased suddenly. 25391da177e4SLinus Torvalds * I.e. Karn's algorithm. (SIGCOMM '87, p5.) 25401da177e4SLinus Torvalds */ 25411da177e4SLinus Torvalds 25421da177e4SLinus Torvalds if (flag & FLAG_RETRANS_DATA_ACKED) 25431da177e4SLinus Torvalds return; 25441da177e4SLinus Torvalds 25452d2abbabSStephen Hemminger tcp_rtt_estimator(sk, seq_rtt); 2546463c84b9SArnaldo Carvalho de Melo tcp_set_rto(sk); 2547463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_backoff = 0; 2548463c84b9SArnaldo Carvalho de Melo tcp_bound_rto(sk); 25491da177e4SLinus Torvalds } 25501da177e4SLinus Torvalds 2551463c84b9SArnaldo Carvalho de Melo static inline void tcp_ack_update_rtt(struct sock *sk, const int flag, 25522d2abbabSStephen Hemminger const s32 seq_rtt) 25531da177e4SLinus Torvalds { 2554463c84b9SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 25551da177e4SLinus Torvalds /* Note that peer MAY send zero echo. In this case it is ignored. (rfc1323) */ 25561da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) 25572d2abbabSStephen Hemminger tcp_ack_saw_tstamp(sk, flag); 25581da177e4SLinus Torvalds else if (seq_rtt >= 0) 25592d2abbabSStephen Hemminger tcp_ack_no_tstamp(sk, seq_rtt, flag); 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds 256216751347SStephen Hemminger static void tcp_cong_avoid(struct sock *sk, u32 ack, 2563317a76f9SStephen Hemminger u32 in_flight, int good) 25641da177e4SLinus Torvalds { 25656687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk); 256616751347SStephen Hemminger icsk->icsk_ca_ops->cong_avoid(sk, ack, in_flight, good); 25676687e988SArnaldo Carvalho de Melo tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp; 25681da177e4SLinus Torvalds } 25691da177e4SLinus Torvalds 25701da177e4SLinus Torvalds /* Restart timer after forward progress on connection. 25711da177e4SLinus Torvalds * RFC2988 recommends to restart timer to now+rto. 25721da177e4SLinus Torvalds */ 25736728e7dcSIlpo Järvinen static void tcp_rearm_rto(struct sock *sk) 25741da177e4SLinus Torvalds { 25759e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 25769e412ba7SIlpo Järvinen 25771da177e4SLinus Torvalds if (!tp->packets_out) { 2578463c84b9SArnaldo Carvalho de Melo inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS); 25791da177e4SLinus Torvalds } else { 25803f421baaSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, inet_csk(sk)->icsk_rto, TCP_RTO_MAX); 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds } 25831da177e4SLinus Torvalds 25847c46a03eSIlpo Järvinen /* If we get here, the whole TSO packet has not been acked. */ 258513fcf850SIlpo Järvinen static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb) 25861da177e4SLinus Torvalds { 25871da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 25887c46a03eSIlpo Järvinen u32 packets_acked; 25891da177e4SLinus Torvalds 25907c46a03eSIlpo Järvinen BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una)); 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds packets_acked = tcp_skb_pcount(skb); 25937c46a03eSIlpo Järvinen if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq)) 25941da177e4SLinus Torvalds return 0; 25951da177e4SLinus Torvalds packets_acked -= tcp_skb_pcount(skb); 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds if (packets_acked) { 25981da177e4SLinus Torvalds BUG_ON(tcp_skb_pcount(skb) == 0); 25997c46a03eSIlpo Järvinen BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)); 26001da177e4SLinus Torvalds } 26011da177e4SLinus Torvalds 260213fcf850SIlpo Järvinen return packets_acked; 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds 26057c46a03eSIlpo Järvinen /* Remove acknowledged frames from the retransmission queue. If our packet 26067c46a03eSIlpo Järvinen * is before the ack sequence we can discard it as it's confirmed to have 26077c46a03eSIlpo Järvinen * arrived at the other end. 26087c46a03eSIlpo Järvinen */ 26097c46a03eSIlpo Järvinen static int tcp_clean_rtx_queue(struct sock *sk, s32 *seq_rtt_p) 26101da177e4SLinus Torvalds { 26111da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 26122d2abbabSStephen Hemminger const struct inet_connection_sock *icsk = inet_csk(sk); 26131da177e4SLinus Torvalds struct sk_buff *skb; 26147c46a03eSIlpo Järvinen u32 now = tcp_time_stamp; 261513fcf850SIlpo Järvinen int fully_acked = 1; 26167c46a03eSIlpo Järvinen int flag = 0; 26176418204fSIlpo Järvinen int prior_packets = tp->packets_out; 26187c46a03eSIlpo Järvinen s32 seq_rtt = -1; 2619b9ce204fSIlpo Järvinen ktime_t last_ackt = net_invalid_timestamp(); 26201da177e4SLinus Torvalds 26217c46a03eSIlpo Järvinen while ((skb = tcp_write_queue_head(sk)) && skb != tcp_send_head(sk)) { 26221da177e4SLinus Torvalds struct tcp_skb_cb *scb = TCP_SKB_CB(skb); 262313fcf850SIlpo Järvinen u32 end_seq; 262413fcf850SIlpo Järvinen u32 packets_acked; 26257c46a03eSIlpo Järvinen u8 sacked = scb->sacked; 26261da177e4SLinus Torvalds 26271da177e4SLinus Torvalds if (after(scb->end_seq, tp->snd_una)) { 262813fcf850SIlpo Järvinen if (tcp_skb_pcount(skb) == 1 || 262913fcf850SIlpo Järvinen !after(tp->snd_una, scb->seq)) 26301da177e4SLinus Torvalds break; 263113fcf850SIlpo Järvinen 263213fcf850SIlpo Järvinen packets_acked = tcp_tso_acked(sk, skb); 263313fcf850SIlpo Järvinen if (!packets_acked) 263413fcf850SIlpo Järvinen break; 263513fcf850SIlpo Järvinen 263613fcf850SIlpo Järvinen fully_acked = 0; 263713fcf850SIlpo Järvinen end_seq = tp->snd_una; 263813fcf850SIlpo Järvinen } else { 263913fcf850SIlpo Järvinen packets_acked = tcp_skb_pcount(skb); 264013fcf850SIlpo Järvinen end_seq = scb->end_seq; 26411da177e4SLinus Torvalds } 26421da177e4SLinus Torvalds 26435d424d5aSJohn Heffner /* MTU probing checks */ 26447c46a03eSIlpo Järvinen if (fully_acked && icsk->icsk_mtup.probe_size && 26457c46a03eSIlpo Järvinen !after(tp->mtu_probe.probe_seq_end, scb->end_seq)) { 26465d424d5aSJohn Heffner tcp_mtup_probe_success(sk, skb); 26475d424d5aSJohn Heffner } 26485d424d5aSJohn Heffner 26491da177e4SLinus Torvalds if (sacked) { 26501da177e4SLinus Torvalds if (sacked & TCPCB_RETRANS) { 26511da177e4SLinus Torvalds if (sacked & TCPCB_SACKED_RETRANS) 265213fcf850SIlpo Järvinen tp->retrans_out -= packets_acked; 26537c46a03eSIlpo Järvinen flag |= FLAG_RETRANS_DATA_ACKED; 26541da177e4SLinus Torvalds seq_rtt = -1; 2655009a2e3eSIlpo Järvinen if ((flag & FLAG_DATA_ACKED) || 2656009a2e3eSIlpo Järvinen (packets_acked > 1)) 2657009a2e3eSIlpo Järvinen flag |= FLAG_NONHEAD_RETRANS_ACKED; 26582d2abbabSStephen Hemminger } else if (seq_rtt < 0) { 26591da177e4SLinus Torvalds seq_rtt = now - scb->when; 266013fcf850SIlpo Järvinen if (fully_acked) 2661164891aaSStephen Hemminger last_ackt = skb->tstamp; 2662a61bbcf2SPatrick McHardy } 26637c46a03eSIlpo Järvinen 26641da177e4SLinus Torvalds if (sacked & TCPCB_SACKED_ACKED) 266513fcf850SIlpo Järvinen tp->sacked_out -= packets_acked; 26661da177e4SLinus Torvalds if (sacked & TCPCB_LOST) 266713fcf850SIlpo Järvinen tp->lost_out -= packets_acked; 26687c46a03eSIlpo Järvinen 26697c46a03eSIlpo Järvinen if ((sacked & TCPCB_URG) && tp->urg_mode && 26707c46a03eSIlpo Järvinen !before(end_seq, tp->snd_up)) 26711da177e4SLinus Torvalds tp->urg_mode = 0; 26722d2abbabSStephen Hemminger } else if (seq_rtt < 0) { 26731da177e4SLinus Torvalds seq_rtt = now - scb->when; 267413fcf850SIlpo Järvinen if (fully_acked) 2675164891aaSStephen Hemminger last_ackt = skb->tstamp; 26762d2abbabSStephen Hemminger } 267713fcf850SIlpo Järvinen tp->packets_out -= packets_acked; 267813fcf850SIlpo Järvinen 2679009a2e3eSIlpo Järvinen /* Initial outgoing SYN's get put onto the write_queue 2680009a2e3eSIlpo Järvinen * just like anything else we transmit. It is not 2681009a2e3eSIlpo Järvinen * true data, and if we misinform our callers that 2682009a2e3eSIlpo Järvinen * this ACK acks real data, we will erroneously exit 2683009a2e3eSIlpo Järvinen * connection startup slow start one packet too 2684009a2e3eSIlpo Järvinen * quickly. This is severely frowned upon behavior. 2685009a2e3eSIlpo Järvinen */ 2686009a2e3eSIlpo Järvinen if (!(scb->flags & TCPCB_FLAG_SYN)) { 2687009a2e3eSIlpo Järvinen flag |= FLAG_DATA_ACKED; 2688009a2e3eSIlpo Järvinen } else { 2689009a2e3eSIlpo Järvinen flag |= FLAG_SYN_ACKED; 2690009a2e3eSIlpo Järvinen tp->retrans_stamp = 0; 2691009a2e3eSIlpo Järvinen } 2692009a2e3eSIlpo Järvinen 269313fcf850SIlpo Järvinen if (!fully_acked) 269413fcf850SIlpo Järvinen break; 269513fcf850SIlpo Järvinen 2696fe067e8aSDavid S. Miller tcp_unlink_write_queue(skb, sk); 26971da177e4SLinus Torvalds sk_stream_free_skb(sk, skb); 26985af4ec23SIlpo Järvinen tcp_clear_all_retrans_hints(tp); 26991da177e4SLinus Torvalds } 27001da177e4SLinus Torvalds 27017c46a03eSIlpo Järvinen if (flag & FLAG_ACKED) { 27026418204fSIlpo Järvinen u32 pkts_acked = prior_packets - tp->packets_out; 2703164891aaSStephen Hemminger const struct tcp_congestion_ops *ca_ops 2704164891aaSStephen Hemminger = inet_csk(sk)->icsk_ca_ops; 2705164891aaSStephen Hemminger 27067c46a03eSIlpo Järvinen tcp_ack_update_rtt(sk, flag, seq_rtt); 27076728e7dcSIlpo Järvinen tcp_rearm_rto(sk); 2708317a76f9SStephen Hemminger 270991fed7a1SIlpo Järvinen tp->fackets_out -= min(pkts_acked, tp->fackets_out); 271013fcf850SIlpo Järvinen /* hint's skb might be NULL but we don't need to care */ 271113fcf850SIlpo Järvinen tp->fastpath_cnt_hint -= min_t(u32, pkts_acked, 271213fcf850SIlpo Järvinen tp->fastpath_cnt_hint); 2713e60402d0SIlpo Järvinen if (tcp_is_reno(tp)) 27141b6d427bSIlpo Järvinen tcp_remove_reno_sacks(sk, pkts_acked); 27151b6d427bSIlpo Järvinen 271630cfd0baSStephen Hemminger if (ca_ops->pkts_acked) { 271730cfd0baSStephen Hemminger s32 rtt_us = -1; 2718b9ce204fSIlpo Järvinen 271930cfd0baSStephen Hemminger /* Is the ACK triggering packet unambiguous? */ 27207c46a03eSIlpo Järvinen if (!(flag & FLAG_RETRANS_DATA_ACKED)) { 272130cfd0baSStephen Hemminger /* High resolution needed and available? */ 272230cfd0baSStephen Hemminger if (ca_ops->flags & TCP_CONG_RTT_STAMP && 272330cfd0baSStephen Hemminger !ktime_equal(last_ackt, 272430cfd0baSStephen Hemminger net_invalid_timestamp())) 272530cfd0baSStephen Hemminger rtt_us = ktime_us_delta(ktime_get_real(), 272630cfd0baSStephen Hemminger last_ackt); 272730cfd0baSStephen Hemminger else if (seq_rtt > 0) 272830cfd0baSStephen Hemminger rtt_us = jiffies_to_usecs(seq_rtt); 272930cfd0baSStephen Hemminger } 273030cfd0baSStephen Hemminger 273130cfd0baSStephen Hemminger ca_ops->pkts_acked(sk, pkts_acked, rtt_us); 273230cfd0baSStephen Hemminger } 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 27351da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0 27361da177e4SLinus Torvalds BUG_TRAP((int)tp->sacked_out >= 0); 27371da177e4SLinus Torvalds BUG_TRAP((int)tp->lost_out >= 0); 27381da177e4SLinus Torvalds BUG_TRAP((int)tp->retrans_out >= 0); 2739e60402d0SIlpo Järvinen if (!tp->packets_out && tcp_is_sack(tp)) { 2740cfcabdccSStephen Hemminger icsk = inet_csk(sk); 27411da177e4SLinus Torvalds if (tp->lost_out) { 27421da177e4SLinus Torvalds printk(KERN_DEBUG "Leak l=%u %d\n", 27436687e988SArnaldo Carvalho de Melo tp->lost_out, icsk->icsk_ca_state); 27441da177e4SLinus Torvalds tp->lost_out = 0; 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds if (tp->sacked_out) { 27471da177e4SLinus Torvalds printk(KERN_DEBUG "Leak s=%u %d\n", 27486687e988SArnaldo Carvalho de Melo tp->sacked_out, icsk->icsk_ca_state); 27491da177e4SLinus Torvalds tp->sacked_out = 0; 27501da177e4SLinus Torvalds } 27511da177e4SLinus Torvalds if (tp->retrans_out) { 27521da177e4SLinus Torvalds printk(KERN_DEBUG "Leak r=%u %d\n", 27536687e988SArnaldo Carvalho de Melo tp->retrans_out, icsk->icsk_ca_state); 27541da177e4SLinus Torvalds tp->retrans_out = 0; 27551da177e4SLinus Torvalds } 27561da177e4SLinus Torvalds } 27571da177e4SLinus Torvalds #endif 27581da177e4SLinus Torvalds *seq_rtt_p = seq_rtt; 27597c46a03eSIlpo Järvinen return flag; 27601da177e4SLinus Torvalds } 27611da177e4SLinus Torvalds 27621da177e4SLinus Torvalds static void tcp_ack_probe(struct sock *sk) 27631da177e4SLinus Torvalds { 2764463c84b9SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 2765463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 27661da177e4SLinus Torvalds 27671da177e4SLinus Torvalds /* Was it a usable window open? */ 27681da177e4SLinus Torvalds 2769fe067e8aSDavid S. Miller if (!after(TCP_SKB_CB(tcp_send_head(sk))->end_seq, 27701da177e4SLinus Torvalds tp->snd_una + tp->snd_wnd)) { 2771463c84b9SArnaldo Carvalho de Melo icsk->icsk_backoff = 0; 2772463c84b9SArnaldo Carvalho de Melo inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0); 27731da177e4SLinus Torvalds /* Socket must be waked up by subsequent tcp_data_snd_check(). 27741da177e4SLinus Torvalds * This function is not for random using! 27751da177e4SLinus Torvalds */ 27761da177e4SLinus Torvalds } else { 2777463c84b9SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0, 27783f421baaSArnaldo Carvalho de Melo min(icsk->icsk_rto << icsk->icsk_backoff, TCP_RTO_MAX), 27793f421baaSArnaldo Carvalho de Melo TCP_RTO_MAX); 27801da177e4SLinus Torvalds } 27811da177e4SLinus Torvalds } 27821da177e4SLinus Torvalds 27836687e988SArnaldo Carvalho de Melo static inline int tcp_ack_is_dubious(const struct sock *sk, const int flag) 27841da177e4SLinus Torvalds { 27851da177e4SLinus Torvalds return (!(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) || 27866687e988SArnaldo Carvalho de Melo inet_csk(sk)->icsk_ca_state != TCP_CA_Open); 27871da177e4SLinus Torvalds } 27881da177e4SLinus Torvalds 27896687e988SArnaldo Carvalho de Melo static inline int tcp_may_raise_cwnd(const struct sock *sk, const int flag) 27901da177e4SLinus Torvalds { 27916687e988SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 27921da177e4SLinus Torvalds return (!(flag & FLAG_ECE) || tp->snd_cwnd < tp->snd_ssthresh) && 27936687e988SArnaldo Carvalho de Melo !((1 << inet_csk(sk)->icsk_ca_state) & (TCPF_CA_Recovery | TCPF_CA_CWR)); 27941da177e4SLinus Torvalds } 27951da177e4SLinus Torvalds 27961da177e4SLinus Torvalds /* Check that window update is acceptable. 27971da177e4SLinus Torvalds * The function assumes that snd_una<=ack<=snd_next. 27981da177e4SLinus Torvalds */ 2799463c84b9SArnaldo Carvalho de Melo static inline int tcp_may_update_window(const struct tcp_sock *tp, const u32 ack, 2800463c84b9SArnaldo Carvalho de Melo const u32 ack_seq, const u32 nwin) 28011da177e4SLinus Torvalds { 28021da177e4SLinus Torvalds return (after(ack, tp->snd_una) || 28031da177e4SLinus Torvalds after(ack_seq, tp->snd_wl1) || 28041da177e4SLinus Torvalds (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd)); 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds 28071da177e4SLinus Torvalds /* Update our send window. 28081da177e4SLinus Torvalds * 28091da177e4SLinus Torvalds * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2 28101da177e4SLinus Torvalds * and in FreeBSD. NetBSD's one is even worse.) is wrong. 28111da177e4SLinus Torvalds */ 28129e412ba7SIlpo Järvinen static int tcp_ack_update_window(struct sock *sk, struct sk_buff *skb, u32 ack, 28139e412ba7SIlpo Järvinen u32 ack_seq) 28141da177e4SLinus Torvalds { 28159e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 28161da177e4SLinus Torvalds int flag = 0; 2817aa8223c7SArnaldo Carvalho de Melo u32 nwin = ntohs(tcp_hdr(skb)->window); 28181da177e4SLinus Torvalds 2819aa8223c7SArnaldo Carvalho de Melo if (likely(!tcp_hdr(skb)->syn)) 28201da177e4SLinus Torvalds nwin <<= tp->rx_opt.snd_wscale; 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds if (tcp_may_update_window(tp, ack, ack_seq, nwin)) { 28231da177e4SLinus Torvalds flag |= FLAG_WIN_UPDATE; 28241da177e4SLinus Torvalds tcp_update_wl(tp, ack, ack_seq); 28251da177e4SLinus Torvalds 28261da177e4SLinus Torvalds if (tp->snd_wnd != nwin) { 28271da177e4SLinus Torvalds tp->snd_wnd = nwin; 28281da177e4SLinus Torvalds 28291da177e4SLinus Torvalds /* Note, it is the only place, where 28301da177e4SLinus Torvalds * fast path is recovered for sending TCP. 28311da177e4SLinus Torvalds */ 28322ad41065SHerbert Xu tp->pred_flags = 0; 28339e412ba7SIlpo Järvinen tcp_fast_path_check(sk); 28341da177e4SLinus Torvalds 28351da177e4SLinus Torvalds if (nwin > tp->max_window) { 28361da177e4SLinus Torvalds tp->max_window = nwin; 2837d83d8461SArnaldo Carvalho de Melo tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie); 28381da177e4SLinus Torvalds } 28391da177e4SLinus Torvalds } 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds 28421da177e4SLinus Torvalds tp->snd_una = ack; 28431da177e4SLinus Torvalds 28441da177e4SLinus Torvalds return flag; 28451da177e4SLinus Torvalds } 28461da177e4SLinus Torvalds 28479ead9a1dSIlpo Järvinen /* A very conservative spurious RTO response algorithm: reduce cwnd and 28489ead9a1dSIlpo Järvinen * continue in congestion avoidance. 28499ead9a1dSIlpo Järvinen */ 28509ead9a1dSIlpo Järvinen static void tcp_conservative_spur_to_response(struct tcp_sock *tp) 28519ead9a1dSIlpo Järvinen { 28529ead9a1dSIlpo Järvinen tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh); 2853aa8b6a7aSIlpo Järvinen tp->snd_cwnd_cnt = 0; 285416e90681SIlpo Järvinen tp->bytes_acked = 0; 285546323655SIlpo Järvinen TCP_ECN_queue_cwr(tp); 28569ead9a1dSIlpo Järvinen tcp_moderate_cwnd(tp); 28579ead9a1dSIlpo Järvinen } 28589ead9a1dSIlpo Järvinen 28593cfe3baaSIlpo Järvinen /* A conservative spurious RTO response algorithm: reduce cwnd using 28603cfe3baaSIlpo Järvinen * rate halving and continue in congestion avoidance. 28613cfe3baaSIlpo Järvinen */ 28623cfe3baaSIlpo Järvinen static void tcp_ratehalving_spur_to_response(struct sock *sk) 28633cfe3baaSIlpo Järvinen { 28643cfe3baaSIlpo Järvinen tcp_enter_cwr(sk, 0); 28653cfe3baaSIlpo Järvinen } 28663cfe3baaSIlpo Järvinen 2867e317f6f6SIlpo Järvinen static void tcp_undo_spur_to_response(struct sock *sk, int flag) 28683cfe3baaSIlpo Järvinen { 2869e317f6f6SIlpo Järvinen if (flag&FLAG_ECE) 2870e317f6f6SIlpo Järvinen tcp_ratehalving_spur_to_response(sk); 2871e317f6f6SIlpo Järvinen else 28723cfe3baaSIlpo Järvinen tcp_undo_cwr(sk, 1); 28733cfe3baaSIlpo Järvinen } 28743cfe3baaSIlpo Järvinen 287530935cf4SIlpo Järvinen /* F-RTO spurious RTO detection algorithm (RFC4138) 287630935cf4SIlpo Järvinen * 28776408d206SIlpo Järvinen * F-RTO affects during two new ACKs following RTO (well, almost, see inline 28786408d206SIlpo Järvinen * comments). State (ACK number) is kept in frto_counter. When ACK advances 28796408d206SIlpo Järvinen * window (but not to or beyond highest sequence sent before RTO): 288030935cf4SIlpo Järvinen * On First ACK, send two new segments out. 288130935cf4SIlpo Järvinen * On Second ACK, RTO was likely spurious. Do spurious response (response 288230935cf4SIlpo Järvinen * algorithm is not part of the F-RTO detection algorithm 288330935cf4SIlpo Järvinen * given in RFC4138 but can be selected separately). 288430935cf4SIlpo Järvinen * Otherwise (basically on duplicate ACK), RTO was (likely) caused by a loss 2885d551e454SIlpo Järvinen * and TCP falls back to conventional RTO recovery. F-RTO allows overriding 2886d551e454SIlpo Järvinen * of Nagle, this is done using frto_counter states 2 and 3, when a new data 2887d551e454SIlpo Järvinen * segment of any size sent during F-RTO, state 2 is upgraded to 3. 288830935cf4SIlpo Järvinen * 288930935cf4SIlpo Järvinen * Rationale: if the RTO was spurious, new ACKs should arrive from the 289030935cf4SIlpo Järvinen * original window even after we transmit two new data segments. 289130935cf4SIlpo Järvinen * 28924dc2665eSIlpo Järvinen * SACK version: 28934dc2665eSIlpo Järvinen * on first step, wait until first cumulative ACK arrives, then move to 28944dc2665eSIlpo Järvinen * the second step. In second step, the next ACK decides. 28954dc2665eSIlpo Järvinen * 289630935cf4SIlpo Järvinen * F-RTO is implemented (mainly) in four functions: 289730935cf4SIlpo Järvinen * - tcp_use_frto() is used to determine if TCP is can use F-RTO 289830935cf4SIlpo Järvinen * - tcp_enter_frto() prepares TCP state on RTO if F-RTO is used, it is 289930935cf4SIlpo Järvinen * called when tcp_use_frto() showed green light 290030935cf4SIlpo Järvinen * - tcp_process_frto() handles incoming ACKs during F-RTO algorithm 290130935cf4SIlpo Järvinen * - tcp_enter_frto_loss() is called if there is not enough evidence 290230935cf4SIlpo Järvinen * to prove that the RTO is indeed spurious. It transfers the control 290330935cf4SIlpo Järvinen * from F-RTO to the conventional RTO recovery 290430935cf4SIlpo Järvinen */ 29052e605294SIlpo Järvinen static int tcp_process_frto(struct sock *sk, int flag) 29061da177e4SLinus Torvalds { 29071da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 29081da177e4SLinus Torvalds 2909005903bcSIlpo Järvinen tcp_verify_left_out(tp); 29101da177e4SLinus Torvalds 29117487c48cSIlpo Järvinen /* Duplicate the behavior from Loss state (fastretrans_alert) */ 29127487c48cSIlpo Järvinen if (flag&FLAG_DATA_ACKED) 29137487c48cSIlpo Järvinen inet_csk(sk)->icsk_retransmits = 0; 29147487c48cSIlpo Järvinen 2915009a2e3eSIlpo Järvinen if ((flag & FLAG_NONHEAD_RETRANS_ACKED) || 2916009a2e3eSIlpo Järvinen ((tp->frto_counter >= 2) && (flag & FLAG_RETRANS_DATA_ACKED))) 2917009a2e3eSIlpo Järvinen tp->undo_marker = 0; 2918009a2e3eSIlpo Järvinen 291995c4922bSIlpo Järvinen if (!before(tp->snd_una, tp->frto_highmark)) { 2920d551e454SIlpo Järvinen tcp_enter_frto_loss(sk, (tp->frto_counter == 1 ? 2 : 3), flag); 29217c9a4a5bSIlpo Järvinen return 1; 292295c4922bSIlpo Järvinen } 292395c4922bSIlpo Järvinen 2924e60402d0SIlpo Järvinen if (!IsSackFrto() || tcp_is_reno(tp)) { 29254dc2665eSIlpo Järvinen /* RFC4138 shortcoming in step 2; should also have case c): 29264dc2665eSIlpo Järvinen * ACK isn't duplicate nor advances window, e.g., opposite dir 29274dc2665eSIlpo Järvinen * data, winupdate 29286408d206SIlpo Järvinen */ 29292e605294SIlpo Järvinen if (!(flag&FLAG_ANY_PROGRESS) && (flag&FLAG_NOT_DUP)) 29307c9a4a5bSIlpo Järvinen return 1; 29316408d206SIlpo Järvinen 293295c4922bSIlpo Järvinen if (!(flag&FLAG_DATA_ACKED)) { 29334dc2665eSIlpo Järvinen tcp_enter_frto_loss(sk, (tp->frto_counter == 1 ? 0 : 3), 29344dc2665eSIlpo Järvinen flag); 29357c9a4a5bSIlpo Järvinen return 1; 29361da177e4SLinus Torvalds } 29374dc2665eSIlpo Järvinen } else { 29384dc2665eSIlpo Järvinen if (!(flag&FLAG_DATA_ACKED) && (tp->frto_counter == 1)) { 29394dc2665eSIlpo Järvinen /* Prevent sending of new data. */ 29404dc2665eSIlpo Järvinen tp->snd_cwnd = min(tp->snd_cwnd, 29414dc2665eSIlpo Järvinen tcp_packets_in_flight(tp)); 29424dc2665eSIlpo Järvinen return 1; 29434dc2665eSIlpo Järvinen } 29444dc2665eSIlpo Järvinen 2945d551e454SIlpo Järvinen if ((tp->frto_counter >= 2) && 29464dc2665eSIlpo Järvinen (!(flag&FLAG_FORWARD_PROGRESS) || 29474dc2665eSIlpo Järvinen ((flag&FLAG_DATA_SACKED) && !(flag&FLAG_ONLY_ORIG_SACKED)))) { 29484dc2665eSIlpo Järvinen /* RFC4138 shortcoming (see comment above) */ 29494dc2665eSIlpo Järvinen if (!(flag&FLAG_FORWARD_PROGRESS) && (flag&FLAG_NOT_DUP)) 29504dc2665eSIlpo Järvinen return 1; 29514dc2665eSIlpo Järvinen 29524dc2665eSIlpo Järvinen tcp_enter_frto_loss(sk, 3, flag); 29534dc2665eSIlpo Järvinen return 1; 29544dc2665eSIlpo Järvinen } 29554dc2665eSIlpo Järvinen } 29561da177e4SLinus Torvalds 29571da177e4SLinus Torvalds if (tp->frto_counter == 1) { 2958575ee714SIlpo Järvinen /* Sending of the next skb must be allowed or no FRTO */ 2959575ee714SIlpo Järvinen if (!tcp_send_head(sk) || 2960575ee714SIlpo Järvinen after(TCP_SKB_CB(tcp_send_head(sk))->end_seq, 2961575ee714SIlpo Järvinen tp->snd_una + tp->snd_wnd)) { 2962d551e454SIlpo Järvinen tcp_enter_frto_loss(sk, (tp->frto_counter == 1 ? 2 : 3), 2963d551e454SIlpo Järvinen flag); 2964575ee714SIlpo Järvinen return 1; 2965575ee714SIlpo Järvinen } 2966575ee714SIlpo Järvinen 29671da177e4SLinus Torvalds tp->snd_cwnd = tcp_packets_in_flight(tp) + 2; 296894d0ea77SIlpo Järvinen tp->frto_counter = 2; 29697c9a4a5bSIlpo Järvinen return 1; 2970d551e454SIlpo Järvinen } else { 29713cfe3baaSIlpo Järvinen switch (sysctl_tcp_frto_response) { 29723cfe3baaSIlpo Järvinen case 2: 2973e317f6f6SIlpo Järvinen tcp_undo_spur_to_response(sk, flag); 29743cfe3baaSIlpo Järvinen break; 29753cfe3baaSIlpo Järvinen case 1: 29769ead9a1dSIlpo Järvinen tcp_conservative_spur_to_response(tp); 29773cfe3baaSIlpo Järvinen break; 29783cfe3baaSIlpo Järvinen default: 29793cfe3baaSIlpo Järvinen tcp_ratehalving_spur_to_response(sk); 29803cfe3baaSIlpo Järvinen break; 29813ff50b79SStephen Hemminger } 298294d0ea77SIlpo Järvinen tp->frto_counter = 0; 2983009a2e3eSIlpo Järvinen tp->undo_marker = 0; 2984912d8f0bSIlpo Järvinen NET_INC_STATS_BH(LINUX_MIB_TCPSPURIOUSRTOS); 29851da177e4SLinus Torvalds } 29867c9a4a5bSIlpo Järvinen return 0; 29871da177e4SLinus Torvalds } 29881da177e4SLinus Torvalds 29891da177e4SLinus Torvalds /* This routine deals with incoming acks, but not outgoing ones. */ 29901da177e4SLinus Torvalds static int tcp_ack(struct sock *sk, struct sk_buff *skb, int flag) 29911da177e4SLinus Torvalds { 29926687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 29931da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 29941da177e4SLinus Torvalds u32 prior_snd_una = tp->snd_una; 29951da177e4SLinus Torvalds u32 ack_seq = TCP_SKB_CB(skb)->seq; 29961da177e4SLinus Torvalds u32 ack = TCP_SKB_CB(skb)->ack_seq; 29971da177e4SLinus Torvalds u32 prior_in_flight; 29981da177e4SLinus Torvalds s32 seq_rtt; 29991da177e4SLinus Torvalds int prior_packets; 30007c9a4a5bSIlpo Järvinen int frto_cwnd = 0; 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds /* If the ack is newer than sent or older than previous acks 30031da177e4SLinus Torvalds * then we can probably ignore it. 30041da177e4SLinus Torvalds */ 30051da177e4SLinus Torvalds if (after(ack, tp->snd_nxt)) 30061da177e4SLinus Torvalds goto uninteresting_ack; 30071da177e4SLinus Torvalds 30081da177e4SLinus Torvalds if (before(ack, prior_snd_una)) 30091da177e4SLinus Torvalds goto old_ack; 30101da177e4SLinus Torvalds 30112e605294SIlpo Järvinen if (after(ack, prior_snd_una)) 30122e605294SIlpo Järvinen flag |= FLAG_SND_UNA_ADVANCED; 30132e605294SIlpo Järvinen 30143fdf3f0cSDaikichi Osuga if (sysctl_tcp_abc) { 30153fdf3f0cSDaikichi Osuga if (icsk->icsk_ca_state < TCP_CA_CWR) 30169772efb9SStephen Hemminger tp->bytes_acked += ack - prior_snd_una; 30173fdf3f0cSDaikichi Osuga else if (icsk->icsk_ca_state == TCP_CA_Loss) 30183fdf3f0cSDaikichi Osuga /* we assume just one segment left network */ 30193fdf3f0cSDaikichi Osuga tp->bytes_acked += min(ack - prior_snd_una, tp->mss_cache); 30203fdf3f0cSDaikichi Osuga } 30219772efb9SStephen Hemminger 30221da177e4SLinus Torvalds if (!(flag&FLAG_SLOWPATH) && after(ack, prior_snd_una)) { 30231da177e4SLinus Torvalds /* Window is constant, pure forward advance. 30241da177e4SLinus Torvalds * No more checks are required. 30251da177e4SLinus Torvalds * Note, we use the fact that SND.UNA>=SND.WL2. 30261da177e4SLinus Torvalds */ 30271da177e4SLinus Torvalds tcp_update_wl(tp, ack, ack_seq); 30281da177e4SLinus Torvalds tp->snd_una = ack; 30291da177e4SLinus Torvalds flag |= FLAG_WIN_UPDATE; 30301da177e4SLinus Torvalds 30316687e988SArnaldo Carvalho de Melo tcp_ca_event(sk, CA_EVENT_FAST_ACK); 3032317a76f9SStephen Hemminger 30331da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPHPACKS); 30341da177e4SLinus Torvalds } else { 30351da177e4SLinus Torvalds if (ack_seq != TCP_SKB_CB(skb)->end_seq) 30361da177e4SLinus Torvalds flag |= FLAG_DATA; 30371da177e4SLinus Torvalds else 30381da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPPUREACKS); 30391da177e4SLinus Torvalds 30409e412ba7SIlpo Järvinen flag |= tcp_ack_update_window(sk, skb, ack, ack_seq); 30411da177e4SLinus Torvalds 30421da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->sacked) 30431da177e4SLinus Torvalds flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una); 30441da177e4SLinus Torvalds 3045aa8223c7SArnaldo Carvalho de Melo if (TCP_ECN_rcv_ecn_echo(tp, tcp_hdr(skb))) 30461da177e4SLinus Torvalds flag |= FLAG_ECE; 30471da177e4SLinus Torvalds 30486687e988SArnaldo Carvalho de Melo tcp_ca_event(sk, CA_EVENT_SLOW_ACK); 30491da177e4SLinus Torvalds } 30501da177e4SLinus Torvalds 30511da177e4SLinus Torvalds /* We passed data and got it acked, remove any soft error 30521da177e4SLinus Torvalds * log. Something worked... 30531da177e4SLinus Torvalds */ 30541da177e4SLinus Torvalds sk->sk_err_soft = 0; 30551da177e4SLinus Torvalds tp->rcv_tstamp = tcp_time_stamp; 30561da177e4SLinus Torvalds prior_packets = tp->packets_out; 30571da177e4SLinus Torvalds if (!prior_packets) 30581da177e4SLinus Torvalds goto no_queue; 30591da177e4SLinus Torvalds 30601da177e4SLinus Torvalds prior_in_flight = tcp_packets_in_flight(tp); 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds /* See if we can take anything off of the retransmit queue. */ 30632d2abbabSStephen Hemminger flag |= tcp_clean_rtx_queue(sk, &seq_rtt); 30641da177e4SLinus Torvalds 30653de96471SIlpo Järvinen /* Guarantee sacktag reordering detection against wrap-arounds */ 30663de96471SIlpo Järvinen if (before(tp->frto_highmark, tp->snd_una)) 30673de96471SIlpo Järvinen tp->frto_highmark = 0; 30681da177e4SLinus Torvalds if (tp->frto_counter) 30692e605294SIlpo Järvinen frto_cwnd = tcp_process_frto(sk, flag); 30701da177e4SLinus Torvalds 30716687e988SArnaldo Carvalho de Melo if (tcp_ack_is_dubious(sk, flag)) { 3072caa20d9aSStephen Hemminger /* Advance CWND, if state allows this. */ 30737c9a4a5bSIlpo Järvinen if ((flag & FLAG_DATA_ACKED) && !frto_cwnd && 30747c9a4a5bSIlpo Järvinen tcp_may_raise_cwnd(sk, flag)) 307516751347SStephen Hemminger tcp_cong_avoid(sk, ack, prior_in_flight, 0); 30761b6d427bSIlpo Järvinen tcp_fastretrans_alert(sk, prior_packets - tp->packets_out, flag); 30771da177e4SLinus Torvalds } else { 30787c9a4a5bSIlpo Järvinen if ((flag & FLAG_DATA_ACKED) && !frto_cwnd) 307916751347SStephen Hemminger tcp_cong_avoid(sk, ack, prior_in_flight, 1); 30801da177e4SLinus Torvalds } 30811da177e4SLinus Torvalds 30821da177e4SLinus Torvalds if ((flag & FLAG_FORWARD_PROGRESS) || !(flag&FLAG_NOT_DUP)) 30831da177e4SLinus Torvalds dst_confirm(sk->sk_dst_cache); 30841da177e4SLinus Torvalds 30851da177e4SLinus Torvalds return 1; 30861da177e4SLinus Torvalds 30871da177e4SLinus Torvalds no_queue: 30886687e988SArnaldo Carvalho de Melo icsk->icsk_probes_out = 0; 30891da177e4SLinus Torvalds 30901da177e4SLinus Torvalds /* If this ack opens up a zero window, clear backoff. It was 30911da177e4SLinus Torvalds * being used to time the probes, and is probably far higher than 30921da177e4SLinus Torvalds * it needs to be for normal retransmission. 30931da177e4SLinus Torvalds */ 3094fe067e8aSDavid S. Miller if (tcp_send_head(sk)) 30951da177e4SLinus Torvalds tcp_ack_probe(sk); 30961da177e4SLinus Torvalds return 1; 30971da177e4SLinus Torvalds 30981da177e4SLinus Torvalds old_ack: 30991da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->sacked) 31001da177e4SLinus Torvalds tcp_sacktag_write_queue(sk, skb, prior_snd_una); 31011da177e4SLinus Torvalds 31021da177e4SLinus Torvalds uninteresting_ack: 31031da177e4SLinus Torvalds SOCK_DEBUG(sk, "Ack %u out of %u:%u\n", ack, tp->snd_una, tp->snd_nxt); 31041da177e4SLinus Torvalds return 0; 31051da177e4SLinus Torvalds } 31061da177e4SLinus Torvalds 31071da177e4SLinus Torvalds 31081da177e4SLinus Torvalds /* Look for tcp options. Normally only called on SYN and SYNACK packets. 31091da177e4SLinus Torvalds * But, this can also be called on packets in the established flow when 31101da177e4SLinus Torvalds * the fast version below fails. 31111da177e4SLinus Torvalds */ 31121da177e4SLinus Torvalds void tcp_parse_options(struct sk_buff *skb, struct tcp_options_received *opt_rx, int estab) 31131da177e4SLinus Torvalds { 31141da177e4SLinus Torvalds unsigned char *ptr; 3115aa8223c7SArnaldo Carvalho de Melo struct tcphdr *th = tcp_hdr(skb); 31161da177e4SLinus Torvalds int length=(th->doff*4)-sizeof(struct tcphdr); 31171da177e4SLinus Torvalds 31181da177e4SLinus Torvalds ptr = (unsigned char *)(th + 1); 31191da177e4SLinus Torvalds opt_rx->saw_tstamp = 0; 31201da177e4SLinus Torvalds 31211da177e4SLinus Torvalds while (length > 0) { 31221da177e4SLinus Torvalds int opcode=*ptr++; 31231da177e4SLinus Torvalds int opsize; 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds switch (opcode) { 31261da177e4SLinus Torvalds case TCPOPT_EOL: 31271da177e4SLinus Torvalds return; 31281da177e4SLinus Torvalds case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ 31291da177e4SLinus Torvalds length--; 31301da177e4SLinus Torvalds continue; 31311da177e4SLinus Torvalds default: 31321da177e4SLinus Torvalds opsize=*ptr++; 31331da177e4SLinus Torvalds if (opsize < 2) /* "silly options" */ 31341da177e4SLinus Torvalds return; 31351da177e4SLinus Torvalds if (opsize > length) 31361da177e4SLinus Torvalds return; /* don't parse partial options */ 31371da177e4SLinus Torvalds switch (opcode) { 31381da177e4SLinus Torvalds case TCPOPT_MSS: 31391da177e4SLinus Torvalds if (opsize==TCPOLEN_MSS && th->syn && !estab) { 31404f3608b7SAl Viro u16 in_mss = ntohs(get_unaligned((__be16 *)ptr)); 31411da177e4SLinus Torvalds if (in_mss) { 31421da177e4SLinus Torvalds if (opt_rx->user_mss && opt_rx->user_mss < in_mss) 31431da177e4SLinus Torvalds in_mss = opt_rx->user_mss; 31441da177e4SLinus Torvalds opt_rx->mss_clamp = in_mss; 31451da177e4SLinus Torvalds } 31461da177e4SLinus Torvalds } 31471da177e4SLinus Torvalds break; 31481da177e4SLinus Torvalds case TCPOPT_WINDOW: 31491da177e4SLinus Torvalds if (opsize==TCPOLEN_WINDOW && th->syn && !estab) 31501da177e4SLinus Torvalds if (sysctl_tcp_window_scaling) { 31511da177e4SLinus Torvalds __u8 snd_wscale = *(__u8 *) ptr; 31521da177e4SLinus Torvalds opt_rx->wscale_ok = 1; 31531da177e4SLinus Torvalds if (snd_wscale > 14) { 31541da177e4SLinus Torvalds if (net_ratelimit()) 31551da177e4SLinus Torvalds printk(KERN_INFO "tcp_parse_options: Illegal window " 31561da177e4SLinus Torvalds "scaling value %d >14 received.\n", 31571da177e4SLinus Torvalds snd_wscale); 31581da177e4SLinus Torvalds snd_wscale = 14; 31591da177e4SLinus Torvalds } 31601da177e4SLinus Torvalds opt_rx->snd_wscale = snd_wscale; 31611da177e4SLinus Torvalds } 31621da177e4SLinus Torvalds break; 31631da177e4SLinus Torvalds case TCPOPT_TIMESTAMP: 31641da177e4SLinus Torvalds if (opsize==TCPOLEN_TIMESTAMP) { 31651da177e4SLinus Torvalds if ((estab && opt_rx->tstamp_ok) || 31661da177e4SLinus Torvalds (!estab && sysctl_tcp_timestamps)) { 31671da177e4SLinus Torvalds opt_rx->saw_tstamp = 1; 31684f3608b7SAl Viro opt_rx->rcv_tsval = ntohl(get_unaligned((__be32 *)ptr)); 31694f3608b7SAl Viro opt_rx->rcv_tsecr = ntohl(get_unaligned((__be32 *)(ptr+4))); 31701da177e4SLinus Torvalds } 31711da177e4SLinus Torvalds } 31721da177e4SLinus Torvalds break; 31731da177e4SLinus Torvalds case TCPOPT_SACK_PERM: 31741da177e4SLinus Torvalds if (opsize==TCPOLEN_SACK_PERM && th->syn && !estab) { 31751da177e4SLinus Torvalds if (sysctl_tcp_sack) { 31761da177e4SLinus Torvalds opt_rx->sack_ok = 1; 31771da177e4SLinus Torvalds tcp_sack_reset(opt_rx); 31781da177e4SLinus Torvalds } 31791da177e4SLinus Torvalds } 31801da177e4SLinus Torvalds break; 31811da177e4SLinus Torvalds 31821da177e4SLinus Torvalds case TCPOPT_SACK: 31831da177e4SLinus Torvalds if ((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) && 31841da177e4SLinus Torvalds !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) && 31851da177e4SLinus Torvalds opt_rx->sack_ok) { 31861da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th; 31871da177e4SLinus Torvalds } 3188d7ea5b91SIlpo Järvinen break; 3189cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG 3190cfb6eeb4SYOSHIFUJI Hideaki case TCPOPT_MD5SIG: 3191cfb6eeb4SYOSHIFUJI Hideaki /* 3192cfb6eeb4SYOSHIFUJI Hideaki * The MD5 Hash has already been 3193cfb6eeb4SYOSHIFUJI Hideaki * checked (see tcp_v{4,6}_do_rcv()). 3194cfb6eeb4SYOSHIFUJI Hideaki */ 3195cfb6eeb4SYOSHIFUJI Hideaki break; 3196cfb6eeb4SYOSHIFUJI Hideaki #endif 31973ff50b79SStephen Hemminger } 31983ff50b79SStephen Hemminger 31991da177e4SLinus Torvalds ptr+=opsize-2; 32001da177e4SLinus Torvalds length-=opsize; 32013ff50b79SStephen Hemminger } 32021da177e4SLinus Torvalds } 32031da177e4SLinus Torvalds } 32041da177e4SLinus Torvalds 32051da177e4SLinus Torvalds /* Fast parse options. This hopes to only see timestamps. 32061da177e4SLinus Torvalds * If it is wrong it falls back on tcp_parse_options(). 32071da177e4SLinus Torvalds */ 320840efc6faSStephen Hemminger static int tcp_fast_parse_options(struct sk_buff *skb, struct tcphdr *th, 32091da177e4SLinus Torvalds struct tcp_sock *tp) 32101da177e4SLinus Torvalds { 32111da177e4SLinus Torvalds if (th->doff == sizeof(struct tcphdr)>>2) { 32121da177e4SLinus Torvalds tp->rx_opt.saw_tstamp = 0; 32131da177e4SLinus Torvalds return 0; 32141da177e4SLinus Torvalds } else if (tp->rx_opt.tstamp_ok && 32151da177e4SLinus Torvalds th->doff == (sizeof(struct tcphdr)>>2)+(TCPOLEN_TSTAMP_ALIGNED>>2)) { 32164f3608b7SAl Viro __be32 *ptr = (__be32 *)(th + 1); 32174f3608b7SAl Viro if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) 32181da177e4SLinus Torvalds | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) { 32191da177e4SLinus Torvalds tp->rx_opt.saw_tstamp = 1; 32201da177e4SLinus Torvalds ++ptr; 32211da177e4SLinus Torvalds tp->rx_opt.rcv_tsval = ntohl(*ptr); 32221da177e4SLinus Torvalds ++ptr; 32231da177e4SLinus Torvalds tp->rx_opt.rcv_tsecr = ntohl(*ptr); 32241da177e4SLinus Torvalds return 1; 32251da177e4SLinus Torvalds } 32261da177e4SLinus Torvalds } 32271da177e4SLinus Torvalds tcp_parse_options(skb, &tp->rx_opt, 1); 32281da177e4SLinus Torvalds return 1; 32291da177e4SLinus Torvalds } 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds static inline void tcp_store_ts_recent(struct tcp_sock *tp) 32321da177e4SLinus Torvalds { 32331da177e4SLinus Torvalds tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval; 32349d729f72SJames Morris tp->rx_opt.ts_recent_stamp = get_seconds(); 32351da177e4SLinus Torvalds } 32361da177e4SLinus Torvalds 32371da177e4SLinus Torvalds static inline void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq) 32381da177e4SLinus Torvalds { 32391da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) { 32401da177e4SLinus Torvalds /* PAWS bug workaround wrt. ACK frames, the PAWS discard 32411da177e4SLinus Torvalds * extra check below makes sure this can only happen 32421da177e4SLinus Torvalds * for pure ACK frames. -DaveM 32431da177e4SLinus Torvalds * 32441da177e4SLinus Torvalds * Not only, also it occurs for expired timestamps. 32451da177e4SLinus Torvalds */ 32461da177e4SLinus Torvalds 32471da177e4SLinus Torvalds if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) >= 0 || 32489d729f72SJames Morris get_seconds() >= tp->rx_opt.ts_recent_stamp + TCP_PAWS_24DAYS) 32491da177e4SLinus Torvalds tcp_store_ts_recent(tp); 32501da177e4SLinus Torvalds } 32511da177e4SLinus Torvalds } 32521da177e4SLinus Torvalds 32531da177e4SLinus Torvalds /* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM 32541da177e4SLinus Torvalds * 32551da177e4SLinus Torvalds * It is not fatal. If this ACK does _not_ change critical state (seqs, window) 32561da177e4SLinus Torvalds * it can pass through stack. So, the following predicate verifies that 32571da177e4SLinus Torvalds * this segment is not used for anything but congestion avoidance or 32581da177e4SLinus Torvalds * fast retransmit. Moreover, we even are able to eliminate most of such 32591da177e4SLinus Torvalds * second order effects, if we apply some small "replay" window (~RTO) 32601da177e4SLinus Torvalds * to timestamp space. 32611da177e4SLinus Torvalds * 32621da177e4SLinus Torvalds * All these measures still do not guarantee that we reject wrapped ACKs 32631da177e4SLinus Torvalds * on networks with high bandwidth, when sequence space is recycled fastly, 32641da177e4SLinus Torvalds * but it guarantees that such events will be very rare and do not affect 32651da177e4SLinus Torvalds * connection seriously. This doesn't look nice, but alas, PAWS is really 32661da177e4SLinus Torvalds * buggy extension. 32671da177e4SLinus Torvalds * 32681da177e4SLinus Torvalds * [ Later note. Even worse! It is buggy for segments _with_ data. RFC 32691da177e4SLinus Torvalds * states that events when retransmit arrives after original data are rare. 32701da177e4SLinus Torvalds * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is 32711da177e4SLinus Torvalds * the biggest problem on large power networks even with minor reordering. 32721da177e4SLinus Torvalds * OK, let's give it small replay window. If peer clock is even 1hz, it is safe 32731da177e4SLinus Torvalds * up to bandwidth of 18Gigabit/sec. 8) ] 32741da177e4SLinus Torvalds */ 32751da177e4SLinus Torvalds 3276463c84b9SArnaldo Carvalho de Melo static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb) 32771da177e4SLinus Torvalds { 3278463c84b9SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk); 3279aa8223c7SArnaldo Carvalho de Melo struct tcphdr *th = tcp_hdr(skb); 32801da177e4SLinus Torvalds u32 seq = TCP_SKB_CB(skb)->seq; 32811da177e4SLinus Torvalds u32 ack = TCP_SKB_CB(skb)->ack_seq; 32821da177e4SLinus Torvalds 32831da177e4SLinus Torvalds return (/* 1. Pure ACK with correct sequence number. */ 32841da177e4SLinus Torvalds (th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) && 32851da177e4SLinus Torvalds 32861da177e4SLinus Torvalds /* 2. ... and duplicate ACK. */ 32871da177e4SLinus Torvalds ack == tp->snd_una && 32881da177e4SLinus Torvalds 32891da177e4SLinus Torvalds /* 3. ... and does not update window. */ 32901da177e4SLinus Torvalds !tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) && 32911da177e4SLinus Torvalds 32921da177e4SLinus Torvalds /* 4. ... and sits in replay window. */ 3293463c84b9SArnaldo Carvalho de Melo (s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) <= (inet_csk(sk)->icsk_rto * 1024) / HZ); 32941da177e4SLinus Torvalds } 32951da177e4SLinus Torvalds 3296463c84b9SArnaldo Carvalho de Melo static inline int tcp_paws_discard(const struct sock *sk, const struct sk_buff *skb) 32971da177e4SLinus Torvalds { 3298463c84b9SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk); 32991da177e4SLinus Torvalds return ((s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) > TCP_PAWS_WINDOW && 33009d729f72SJames Morris get_seconds() < tp->rx_opt.ts_recent_stamp + TCP_PAWS_24DAYS && 3301463c84b9SArnaldo Carvalho de Melo !tcp_disordered_ack(sk, skb)); 33021da177e4SLinus Torvalds } 33031da177e4SLinus Torvalds 33041da177e4SLinus Torvalds /* Check segment sequence number for validity. 33051da177e4SLinus Torvalds * 33061da177e4SLinus Torvalds * Segment controls are considered valid, if the segment 33071da177e4SLinus Torvalds * fits to the window after truncation to the window. Acceptability 33081da177e4SLinus Torvalds * of data (and SYN, FIN, of course) is checked separately. 33091da177e4SLinus Torvalds * See tcp_data_queue(), for example. 33101da177e4SLinus Torvalds * 33111da177e4SLinus Torvalds * Also, controls (RST is main one) are accepted using RCV.WUP instead 33121da177e4SLinus Torvalds * of RCV.NXT. Peer still did not advance his SND.UNA when we 33131da177e4SLinus Torvalds * delayed ACK, so that hisSND.UNA<=ourRCV.WUP. 33141da177e4SLinus Torvalds * (borrowed from freebsd) 33151da177e4SLinus Torvalds */ 33161da177e4SLinus Torvalds 33171da177e4SLinus Torvalds static inline int tcp_sequence(struct tcp_sock *tp, u32 seq, u32 end_seq) 33181da177e4SLinus Torvalds { 33191da177e4SLinus Torvalds return !before(end_seq, tp->rcv_wup) && 33201da177e4SLinus Torvalds !after(seq, tp->rcv_nxt + tcp_receive_window(tp)); 33211da177e4SLinus Torvalds } 33221da177e4SLinus Torvalds 33231da177e4SLinus Torvalds /* When we get a reset we do this. */ 33241da177e4SLinus Torvalds static void tcp_reset(struct sock *sk) 33251da177e4SLinus Torvalds { 33261da177e4SLinus Torvalds /* We want the right error as BSD sees it (and indeed as we do). */ 33271da177e4SLinus Torvalds switch (sk->sk_state) { 33281da177e4SLinus Torvalds case TCP_SYN_SENT: 33291da177e4SLinus Torvalds sk->sk_err = ECONNREFUSED; 33301da177e4SLinus Torvalds break; 33311da177e4SLinus Torvalds case TCP_CLOSE_WAIT: 33321da177e4SLinus Torvalds sk->sk_err = EPIPE; 33331da177e4SLinus Torvalds break; 33341da177e4SLinus Torvalds case TCP_CLOSE: 33351da177e4SLinus Torvalds return; 33361da177e4SLinus Torvalds default: 33371da177e4SLinus Torvalds sk->sk_err = ECONNRESET; 33381da177e4SLinus Torvalds } 33391da177e4SLinus Torvalds 33401da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) 33411da177e4SLinus Torvalds sk->sk_error_report(sk); 33421da177e4SLinus Torvalds 33431da177e4SLinus Torvalds tcp_done(sk); 33441da177e4SLinus Torvalds } 33451da177e4SLinus Torvalds 33461da177e4SLinus Torvalds /* 33471da177e4SLinus Torvalds * Process the FIN bit. This now behaves as it is supposed to work 33481da177e4SLinus Torvalds * and the FIN takes effect when it is validly part of sequence 33491da177e4SLinus Torvalds * space. Not before when we get holes. 33501da177e4SLinus Torvalds * 33511da177e4SLinus Torvalds * If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT 33521da177e4SLinus Torvalds * (and thence onto LAST-ACK and finally, CLOSE, we never enter 33531da177e4SLinus Torvalds * TIME-WAIT) 33541da177e4SLinus Torvalds * 33551da177e4SLinus Torvalds * If we are in FINWAIT-1, a received FIN indicates simultaneous 33561da177e4SLinus Torvalds * close and we go into CLOSING (and later onto TIME-WAIT) 33571da177e4SLinus Torvalds * 33581da177e4SLinus Torvalds * If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT. 33591da177e4SLinus Torvalds */ 33601da177e4SLinus Torvalds static void tcp_fin(struct sk_buff *skb, struct sock *sk, struct tcphdr *th) 33611da177e4SLinus Torvalds { 33621da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 33631da177e4SLinus Torvalds 3364463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 33651da177e4SLinus Torvalds 33661da177e4SLinus Torvalds sk->sk_shutdown |= RCV_SHUTDOWN; 33671da177e4SLinus Torvalds sock_set_flag(sk, SOCK_DONE); 33681da177e4SLinus Torvalds 33691da177e4SLinus Torvalds switch (sk->sk_state) { 33701da177e4SLinus Torvalds case TCP_SYN_RECV: 33711da177e4SLinus Torvalds case TCP_ESTABLISHED: 33721da177e4SLinus Torvalds /* Move to CLOSE_WAIT */ 33731da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSE_WAIT); 3374463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_ack.pingpong = 1; 33751da177e4SLinus Torvalds break; 33761da177e4SLinus Torvalds 33771da177e4SLinus Torvalds case TCP_CLOSE_WAIT: 33781da177e4SLinus Torvalds case TCP_CLOSING: 33791da177e4SLinus Torvalds /* Received a retransmission of the FIN, do 33801da177e4SLinus Torvalds * nothing. 33811da177e4SLinus Torvalds */ 33821da177e4SLinus Torvalds break; 33831da177e4SLinus Torvalds case TCP_LAST_ACK: 33841da177e4SLinus Torvalds /* RFC793: Remain in the LAST-ACK state. */ 33851da177e4SLinus Torvalds break; 33861da177e4SLinus Torvalds 33871da177e4SLinus Torvalds case TCP_FIN_WAIT1: 33881da177e4SLinus Torvalds /* This case occurs when a simultaneous close 33891da177e4SLinus Torvalds * happens, we must ack the received FIN and 33901da177e4SLinus Torvalds * enter the CLOSING state. 33911da177e4SLinus Torvalds */ 33921da177e4SLinus Torvalds tcp_send_ack(sk); 33931da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSING); 33941da177e4SLinus Torvalds break; 33951da177e4SLinus Torvalds case TCP_FIN_WAIT2: 33961da177e4SLinus Torvalds /* Received a FIN -- send ACK and enter TIME_WAIT. */ 33971da177e4SLinus Torvalds tcp_send_ack(sk); 33981da177e4SLinus Torvalds tcp_time_wait(sk, TCP_TIME_WAIT, 0); 33991da177e4SLinus Torvalds break; 34001da177e4SLinus Torvalds default: 34011da177e4SLinus Torvalds /* Only TCP_LISTEN and TCP_CLOSE are left, in these 34021da177e4SLinus Torvalds * cases we should never reach this piece of code. 34031da177e4SLinus Torvalds */ 34041da177e4SLinus Torvalds printk(KERN_ERR "%s: Impossible, sk->sk_state=%d\n", 34051da177e4SLinus Torvalds __FUNCTION__, sk->sk_state); 34061da177e4SLinus Torvalds break; 34073ff50b79SStephen Hemminger } 34081da177e4SLinus Torvalds 34091da177e4SLinus Torvalds /* It _is_ possible, that we have something out-of-order _after_ FIN. 34101da177e4SLinus Torvalds * Probably, we should reset in this case. For now drop them. 34111da177e4SLinus Torvalds */ 34121da177e4SLinus Torvalds __skb_queue_purge(&tp->out_of_order_queue); 3413e60402d0SIlpo Järvinen if (tcp_is_sack(tp)) 34141da177e4SLinus Torvalds tcp_sack_reset(&tp->rx_opt); 34151da177e4SLinus Torvalds sk_stream_mem_reclaim(sk); 34161da177e4SLinus Torvalds 34171da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) { 34181da177e4SLinus Torvalds sk->sk_state_change(sk); 34191da177e4SLinus Torvalds 34201da177e4SLinus Torvalds /* Do not send POLL_HUP for half duplex close. */ 34211da177e4SLinus Torvalds if (sk->sk_shutdown == SHUTDOWN_MASK || 34221da177e4SLinus Torvalds sk->sk_state == TCP_CLOSE) 34231da177e4SLinus Torvalds sk_wake_async(sk, 1, POLL_HUP); 34241da177e4SLinus Torvalds else 34251da177e4SLinus Torvalds sk_wake_async(sk, 1, POLL_IN); 34261da177e4SLinus Torvalds } 34271da177e4SLinus Torvalds } 34281da177e4SLinus Torvalds 342940efc6faSStephen Hemminger static inline int tcp_sack_extend(struct tcp_sack_block *sp, u32 seq, u32 end_seq) 34301da177e4SLinus Torvalds { 34311da177e4SLinus Torvalds if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) { 34321da177e4SLinus Torvalds if (before(seq, sp->start_seq)) 34331da177e4SLinus Torvalds sp->start_seq = seq; 34341da177e4SLinus Torvalds if (after(end_seq, sp->end_seq)) 34351da177e4SLinus Torvalds sp->end_seq = end_seq; 34361da177e4SLinus Torvalds return 1; 34371da177e4SLinus Torvalds } 34381da177e4SLinus Torvalds return 0; 34391da177e4SLinus Torvalds } 34401da177e4SLinus Torvalds 344140efc6faSStephen Hemminger static void tcp_dsack_set(struct tcp_sock *tp, u32 seq, u32 end_seq) 34421da177e4SLinus Torvalds { 3443e60402d0SIlpo Järvinen if (tcp_is_sack(tp) && sysctl_tcp_dsack) { 34441da177e4SLinus Torvalds if (before(seq, tp->rcv_nxt)) 34451da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPDSACKOLDSENT); 34461da177e4SLinus Torvalds else 34471da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPDSACKOFOSENT); 34481da177e4SLinus Torvalds 34491da177e4SLinus Torvalds tp->rx_opt.dsack = 1; 34501da177e4SLinus Torvalds tp->duplicate_sack[0].start_seq = seq; 34511da177e4SLinus Torvalds tp->duplicate_sack[0].end_seq = end_seq; 34521da177e4SLinus Torvalds tp->rx_opt.eff_sacks = min(tp->rx_opt.num_sacks + 1, 4 - tp->rx_opt.tstamp_ok); 34531da177e4SLinus Torvalds } 34541da177e4SLinus Torvalds } 34551da177e4SLinus Torvalds 345640efc6faSStephen Hemminger static void tcp_dsack_extend(struct tcp_sock *tp, u32 seq, u32 end_seq) 34571da177e4SLinus Torvalds { 34581da177e4SLinus Torvalds if (!tp->rx_opt.dsack) 34591da177e4SLinus Torvalds tcp_dsack_set(tp, seq, end_seq); 34601da177e4SLinus Torvalds else 34611da177e4SLinus Torvalds tcp_sack_extend(tp->duplicate_sack, seq, end_seq); 34621da177e4SLinus Torvalds } 34631da177e4SLinus Torvalds 34641da177e4SLinus Torvalds static void tcp_send_dupack(struct sock *sk, struct sk_buff *skb) 34651da177e4SLinus Torvalds { 34661da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 34671da177e4SLinus Torvalds 34681da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && 34691da177e4SLinus Torvalds before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { 34701da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKLOST); 3471463c84b9SArnaldo Carvalho de Melo tcp_enter_quickack_mode(sk); 34721da177e4SLinus Torvalds 3473e60402d0SIlpo Järvinen if (tcp_is_sack(tp) && sysctl_tcp_dsack) { 34741da177e4SLinus Torvalds u32 end_seq = TCP_SKB_CB(skb)->end_seq; 34751da177e4SLinus Torvalds 34761da177e4SLinus Torvalds if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) 34771da177e4SLinus Torvalds end_seq = tp->rcv_nxt; 34781da177e4SLinus Torvalds tcp_dsack_set(tp, TCP_SKB_CB(skb)->seq, end_seq); 34791da177e4SLinus Torvalds } 34801da177e4SLinus Torvalds } 34811da177e4SLinus Torvalds 34821da177e4SLinus Torvalds tcp_send_ack(sk); 34831da177e4SLinus Torvalds } 34841da177e4SLinus Torvalds 34851da177e4SLinus Torvalds /* These routines update the SACK block as out-of-order packets arrive or 34861da177e4SLinus Torvalds * in-order packets close up the sequence space. 34871da177e4SLinus Torvalds */ 34881da177e4SLinus Torvalds static void tcp_sack_maybe_coalesce(struct tcp_sock *tp) 34891da177e4SLinus Torvalds { 34901da177e4SLinus Torvalds int this_sack; 34911da177e4SLinus Torvalds struct tcp_sack_block *sp = &tp->selective_acks[0]; 34921da177e4SLinus Torvalds struct tcp_sack_block *swalk = sp+1; 34931da177e4SLinus Torvalds 34941da177e4SLinus Torvalds /* See if the recent change to the first SACK eats into 34951da177e4SLinus Torvalds * or hits the sequence space of other SACK blocks, if so coalesce. 34961da177e4SLinus Torvalds */ 34971da177e4SLinus Torvalds for (this_sack = 1; this_sack < tp->rx_opt.num_sacks; ) { 34981da177e4SLinus Torvalds if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) { 34991da177e4SLinus Torvalds int i; 35001da177e4SLinus Torvalds 35011da177e4SLinus Torvalds /* Zap SWALK, by moving every further SACK up by one slot. 35021da177e4SLinus Torvalds * Decrease num_sacks. 35031da177e4SLinus Torvalds */ 35041da177e4SLinus Torvalds tp->rx_opt.num_sacks--; 35051da177e4SLinus Torvalds tp->rx_opt.eff_sacks = min(tp->rx_opt.num_sacks + tp->rx_opt.dsack, 4 - tp->rx_opt.tstamp_ok); 35061da177e4SLinus Torvalds for (i=this_sack; i < tp->rx_opt.num_sacks; i++) 35071da177e4SLinus Torvalds sp[i] = sp[i+1]; 35081da177e4SLinus Torvalds continue; 35091da177e4SLinus Torvalds } 35101da177e4SLinus Torvalds this_sack++, swalk++; 35111da177e4SLinus Torvalds } 35121da177e4SLinus Torvalds } 35131da177e4SLinus Torvalds 351440efc6faSStephen Hemminger static inline void tcp_sack_swap(struct tcp_sack_block *sack1, struct tcp_sack_block *sack2) 35151da177e4SLinus Torvalds { 35161da177e4SLinus Torvalds __u32 tmp; 35171da177e4SLinus Torvalds 35181da177e4SLinus Torvalds tmp = sack1->start_seq; 35191da177e4SLinus Torvalds sack1->start_seq = sack2->start_seq; 35201da177e4SLinus Torvalds sack2->start_seq = tmp; 35211da177e4SLinus Torvalds 35221da177e4SLinus Torvalds tmp = sack1->end_seq; 35231da177e4SLinus Torvalds sack1->end_seq = sack2->end_seq; 35241da177e4SLinus Torvalds sack2->end_seq = tmp; 35251da177e4SLinus Torvalds } 35261da177e4SLinus Torvalds 35271da177e4SLinus Torvalds static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq) 35281da177e4SLinus Torvalds { 35291da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 35301da177e4SLinus Torvalds struct tcp_sack_block *sp = &tp->selective_acks[0]; 35311da177e4SLinus Torvalds int cur_sacks = tp->rx_opt.num_sacks; 35321da177e4SLinus Torvalds int this_sack; 35331da177e4SLinus Torvalds 35341da177e4SLinus Torvalds if (!cur_sacks) 35351da177e4SLinus Torvalds goto new_sack; 35361da177e4SLinus Torvalds 35371da177e4SLinus Torvalds for (this_sack=0; this_sack<cur_sacks; this_sack++, sp++) { 35381da177e4SLinus Torvalds if (tcp_sack_extend(sp, seq, end_seq)) { 35391da177e4SLinus Torvalds /* Rotate this_sack to the first one. */ 35401da177e4SLinus Torvalds for (; this_sack>0; this_sack--, sp--) 35411da177e4SLinus Torvalds tcp_sack_swap(sp, sp-1); 35421da177e4SLinus Torvalds if (cur_sacks > 1) 35431da177e4SLinus Torvalds tcp_sack_maybe_coalesce(tp); 35441da177e4SLinus Torvalds return; 35451da177e4SLinus Torvalds } 35461da177e4SLinus Torvalds } 35471da177e4SLinus Torvalds 35481da177e4SLinus Torvalds /* Could not find an adjacent existing SACK, build a new one, 35491da177e4SLinus Torvalds * put it at the front, and shift everyone else down. We 35501da177e4SLinus Torvalds * always know there is at least one SACK present already here. 35511da177e4SLinus Torvalds * 35521da177e4SLinus Torvalds * If the sack array is full, forget about the last one. 35531da177e4SLinus Torvalds */ 35541da177e4SLinus Torvalds if (this_sack >= 4) { 35551da177e4SLinus Torvalds this_sack--; 35561da177e4SLinus Torvalds tp->rx_opt.num_sacks--; 35571da177e4SLinus Torvalds sp--; 35581da177e4SLinus Torvalds } 35591da177e4SLinus Torvalds for (; this_sack > 0; this_sack--, sp--) 35601da177e4SLinus Torvalds *sp = *(sp-1); 35611da177e4SLinus Torvalds 35621da177e4SLinus Torvalds new_sack: 35631da177e4SLinus Torvalds /* Build the new head SACK, and we're done. */ 35641da177e4SLinus Torvalds sp->start_seq = seq; 35651da177e4SLinus Torvalds sp->end_seq = end_seq; 35661da177e4SLinus Torvalds tp->rx_opt.num_sacks++; 35671da177e4SLinus Torvalds tp->rx_opt.eff_sacks = min(tp->rx_opt.num_sacks + tp->rx_opt.dsack, 4 - tp->rx_opt.tstamp_ok); 35681da177e4SLinus Torvalds } 35691da177e4SLinus Torvalds 35701da177e4SLinus Torvalds /* RCV.NXT advances, some SACKs should be eaten. */ 35711da177e4SLinus Torvalds 35721da177e4SLinus Torvalds static void tcp_sack_remove(struct tcp_sock *tp) 35731da177e4SLinus Torvalds { 35741da177e4SLinus Torvalds struct tcp_sack_block *sp = &tp->selective_acks[0]; 35751da177e4SLinus Torvalds int num_sacks = tp->rx_opt.num_sacks; 35761da177e4SLinus Torvalds int this_sack; 35771da177e4SLinus Torvalds 35781da177e4SLinus Torvalds /* Empty ofo queue, hence, all the SACKs are eaten. Clear. */ 3579b03efcfbSDavid S. Miller if (skb_queue_empty(&tp->out_of_order_queue)) { 35801da177e4SLinus Torvalds tp->rx_opt.num_sacks = 0; 35811da177e4SLinus Torvalds tp->rx_opt.eff_sacks = tp->rx_opt.dsack; 35821da177e4SLinus Torvalds return; 35831da177e4SLinus Torvalds } 35841da177e4SLinus Torvalds 35851da177e4SLinus Torvalds for (this_sack = 0; this_sack < num_sacks; ) { 35861da177e4SLinus Torvalds /* Check if the start of the sack is covered by RCV.NXT. */ 35871da177e4SLinus Torvalds if (!before(tp->rcv_nxt, sp->start_seq)) { 35881da177e4SLinus Torvalds int i; 35891da177e4SLinus Torvalds 35901da177e4SLinus Torvalds /* RCV.NXT must cover all the block! */ 35911da177e4SLinus Torvalds BUG_TRAP(!before(tp->rcv_nxt, sp->end_seq)); 35921da177e4SLinus Torvalds 35931da177e4SLinus Torvalds /* Zap this SACK, by moving forward any other SACKS. */ 35941da177e4SLinus Torvalds for (i=this_sack+1; i < num_sacks; i++) 35951da177e4SLinus Torvalds tp->selective_acks[i-1] = tp->selective_acks[i]; 35961da177e4SLinus Torvalds num_sacks--; 35971da177e4SLinus Torvalds continue; 35981da177e4SLinus Torvalds } 35991da177e4SLinus Torvalds this_sack++; 36001da177e4SLinus Torvalds sp++; 36011da177e4SLinus Torvalds } 36021da177e4SLinus Torvalds if (num_sacks != tp->rx_opt.num_sacks) { 36031da177e4SLinus Torvalds tp->rx_opt.num_sacks = num_sacks; 36041da177e4SLinus Torvalds tp->rx_opt.eff_sacks = min(tp->rx_opt.num_sacks + tp->rx_opt.dsack, 4 - tp->rx_opt.tstamp_ok); 36051da177e4SLinus Torvalds } 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds 36081da177e4SLinus Torvalds /* This one checks to see if we can put data from the 36091da177e4SLinus Torvalds * out_of_order queue into the receive_queue. 36101da177e4SLinus Torvalds */ 36111da177e4SLinus Torvalds static void tcp_ofo_queue(struct sock *sk) 36121da177e4SLinus Torvalds { 36131da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 36141da177e4SLinus Torvalds __u32 dsack_high = tp->rcv_nxt; 36151da177e4SLinus Torvalds struct sk_buff *skb; 36161da177e4SLinus Torvalds 36171da177e4SLinus Torvalds while ((skb = skb_peek(&tp->out_of_order_queue)) != NULL) { 36181da177e4SLinus Torvalds if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) 36191da177e4SLinus Torvalds break; 36201da177e4SLinus Torvalds 36211da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->seq, dsack_high)) { 36221da177e4SLinus Torvalds __u32 dsack = dsack_high; 36231da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->end_seq, dsack_high)) 36241da177e4SLinus Torvalds dsack_high = TCP_SKB_CB(skb)->end_seq; 36251da177e4SLinus Torvalds tcp_dsack_extend(tp, TCP_SKB_CB(skb)->seq, dsack); 36261da177e4SLinus Torvalds } 36271da177e4SLinus Torvalds 36281da177e4SLinus Torvalds if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) { 36291da177e4SLinus Torvalds SOCK_DEBUG(sk, "ofo packet was already received \n"); 36308728b834SDavid S. Miller __skb_unlink(skb, &tp->out_of_order_queue); 36311da177e4SLinus Torvalds __kfree_skb(skb); 36321da177e4SLinus Torvalds continue; 36331da177e4SLinus Torvalds } 36341da177e4SLinus Torvalds SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n", 36351da177e4SLinus Torvalds tp->rcv_nxt, TCP_SKB_CB(skb)->seq, 36361da177e4SLinus Torvalds TCP_SKB_CB(skb)->end_seq); 36371da177e4SLinus Torvalds 36388728b834SDavid S. Miller __skb_unlink(skb, &tp->out_of_order_queue); 36391da177e4SLinus Torvalds __skb_queue_tail(&sk->sk_receive_queue, skb); 36401da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 3641aa8223c7SArnaldo Carvalho de Melo if (tcp_hdr(skb)->fin) 3642aa8223c7SArnaldo Carvalho de Melo tcp_fin(skb, sk, tcp_hdr(skb)); 36431da177e4SLinus Torvalds } 36441da177e4SLinus Torvalds } 36451da177e4SLinus Torvalds 36461da177e4SLinus Torvalds static int tcp_prune_queue(struct sock *sk); 36471da177e4SLinus Torvalds 36481da177e4SLinus Torvalds static void tcp_data_queue(struct sock *sk, struct sk_buff *skb) 36491da177e4SLinus Torvalds { 3650aa8223c7SArnaldo Carvalho de Melo struct tcphdr *th = tcp_hdr(skb); 36511da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 36521da177e4SLinus Torvalds int eaten = -1; 36531da177e4SLinus Torvalds 36541da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) 36551da177e4SLinus Torvalds goto drop; 36561da177e4SLinus Torvalds 36571da177e4SLinus Torvalds __skb_pull(skb, th->doff*4); 36581da177e4SLinus Torvalds 36591da177e4SLinus Torvalds TCP_ECN_accept_cwr(tp, skb); 36601da177e4SLinus Torvalds 36611da177e4SLinus Torvalds if (tp->rx_opt.dsack) { 36621da177e4SLinus Torvalds tp->rx_opt.dsack = 0; 36631da177e4SLinus Torvalds tp->rx_opt.eff_sacks = min_t(unsigned int, tp->rx_opt.num_sacks, 36641da177e4SLinus Torvalds 4 - tp->rx_opt.tstamp_ok); 36651da177e4SLinus Torvalds } 36661da177e4SLinus Torvalds 36671da177e4SLinus Torvalds /* Queue data for delivery to the user. 36681da177e4SLinus Torvalds * Packets in sequence go to the receive queue. 36691da177e4SLinus Torvalds * Out of sequence packets to the out_of_order_queue. 36701da177e4SLinus Torvalds */ 36711da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) { 36721da177e4SLinus Torvalds if (tcp_receive_window(tp) == 0) 36731da177e4SLinus Torvalds goto out_of_window; 36741da177e4SLinus Torvalds 36751da177e4SLinus Torvalds /* Ok. In sequence. In window. */ 36761da177e4SLinus Torvalds if (tp->ucopy.task == current && 36771da177e4SLinus Torvalds tp->copied_seq == tp->rcv_nxt && tp->ucopy.len && 36781da177e4SLinus Torvalds sock_owned_by_user(sk) && !tp->urg_data) { 36791da177e4SLinus Torvalds int chunk = min_t(unsigned int, skb->len, 36801da177e4SLinus Torvalds tp->ucopy.len); 36811da177e4SLinus Torvalds 36821da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 36831da177e4SLinus Torvalds 36841da177e4SLinus Torvalds local_bh_enable(); 36851da177e4SLinus Torvalds if (!skb_copy_datagram_iovec(skb, 0, tp->ucopy.iov, chunk)) { 36861da177e4SLinus Torvalds tp->ucopy.len -= chunk; 36871da177e4SLinus Torvalds tp->copied_seq += chunk; 36881da177e4SLinus Torvalds eaten = (chunk == skb->len && !th->fin); 36891da177e4SLinus Torvalds tcp_rcv_space_adjust(sk); 36901da177e4SLinus Torvalds } 36911da177e4SLinus Torvalds local_bh_disable(); 36921da177e4SLinus Torvalds } 36931da177e4SLinus Torvalds 36941da177e4SLinus Torvalds if (eaten <= 0) { 36951da177e4SLinus Torvalds queue_and_out: 36961da177e4SLinus Torvalds if (eaten < 0 && 36971da177e4SLinus Torvalds (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || 36981da177e4SLinus Torvalds !sk_stream_rmem_schedule(sk, skb))) { 36991da177e4SLinus Torvalds if (tcp_prune_queue(sk) < 0 || 37001da177e4SLinus Torvalds !sk_stream_rmem_schedule(sk, skb)) 37011da177e4SLinus Torvalds goto drop; 37021da177e4SLinus Torvalds } 37031da177e4SLinus Torvalds sk_stream_set_owner_r(skb, sk); 37041da177e4SLinus Torvalds __skb_queue_tail(&sk->sk_receive_queue, skb); 37051da177e4SLinus Torvalds } 37061da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 37071da177e4SLinus Torvalds if (skb->len) 37089e412ba7SIlpo Järvinen tcp_event_data_recv(sk, skb); 37091da177e4SLinus Torvalds if (th->fin) 37101da177e4SLinus Torvalds tcp_fin(skb, sk, th); 37111da177e4SLinus Torvalds 3712b03efcfbSDavid S. Miller if (!skb_queue_empty(&tp->out_of_order_queue)) { 37131da177e4SLinus Torvalds tcp_ofo_queue(sk); 37141da177e4SLinus Torvalds 37151da177e4SLinus Torvalds /* RFC2581. 4.2. SHOULD send immediate ACK, when 37161da177e4SLinus Torvalds * gap in queue is filled. 37171da177e4SLinus Torvalds */ 3718b03efcfbSDavid S. Miller if (skb_queue_empty(&tp->out_of_order_queue)) 3719463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_ack.pingpong = 0; 37201da177e4SLinus Torvalds } 37211da177e4SLinus Torvalds 37221da177e4SLinus Torvalds if (tp->rx_opt.num_sacks) 37231da177e4SLinus Torvalds tcp_sack_remove(tp); 37241da177e4SLinus Torvalds 37259e412ba7SIlpo Järvinen tcp_fast_path_check(sk); 37261da177e4SLinus Torvalds 37271da177e4SLinus Torvalds if (eaten > 0) 37281da177e4SLinus Torvalds __kfree_skb(skb); 37291da177e4SLinus Torvalds else if (!sock_flag(sk, SOCK_DEAD)) 37301da177e4SLinus Torvalds sk->sk_data_ready(sk, 0); 37311da177e4SLinus Torvalds return; 37321da177e4SLinus Torvalds } 37331da177e4SLinus Torvalds 37341da177e4SLinus Torvalds if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) { 37351da177e4SLinus Torvalds /* A retransmit, 2nd most common case. Force an immediate ack. */ 37361da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_DELAYEDACKLOST); 37371da177e4SLinus Torvalds tcp_dsack_set(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq); 37381da177e4SLinus Torvalds 37391da177e4SLinus Torvalds out_of_window: 3740463c84b9SArnaldo Carvalho de Melo tcp_enter_quickack_mode(sk); 3741463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 37421da177e4SLinus Torvalds drop: 37431da177e4SLinus Torvalds __kfree_skb(skb); 37441da177e4SLinus Torvalds return; 37451da177e4SLinus Torvalds } 37461da177e4SLinus Torvalds 37471da177e4SLinus Torvalds /* Out of window. F.e. zero window probe. */ 37481da177e4SLinus Torvalds if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp))) 37491da177e4SLinus Torvalds goto out_of_window; 37501da177e4SLinus Torvalds 3751463c84b9SArnaldo Carvalho de Melo tcp_enter_quickack_mode(sk); 37521da177e4SLinus Torvalds 37531da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { 37541da177e4SLinus Torvalds /* Partial packet, seq < rcv_next < end_seq */ 37551da177e4SLinus Torvalds SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n", 37561da177e4SLinus Torvalds tp->rcv_nxt, TCP_SKB_CB(skb)->seq, 37571da177e4SLinus Torvalds TCP_SKB_CB(skb)->end_seq); 37581da177e4SLinus Torvalds 37591da177e4SLinus Torvalds tcp_dsack_set(tp, TCP_SKB_CB(skb)->seq, tp->rcv_nxt); 37601da177e4SLinus Torvalds 37611da177e4SLinus Torvalds /* If window is closed, drop tail of packet. But after 37621da177e4SLinus Torvalds * remembering D-SACK for its head made in previous line. 37631da177e4SLinus Torvalds */ 37641da177e4SLinus Torvalds if (!tcp_receive_window(tp)) 37651da177e4SLinus Torvalds goto out_of_window; 37661da177e4SLinus Torvalds goto queue_and_out; 37671da177e4SLinus Torvalds } 37681da177e4SLinus Torvalds 37691da177e4SLinus Torvalds TCP_ECN_check_ce(tp, skb); 37701da177e4SLinus Torvalds 37711da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || 37721da177e4SLinus Torvalds !sk_stream_rmem_schedule(sk, skb)) { 37731da177e4SLinus Torvalds if (tcp_prune_queue(sk) < 0 || 37741da177e4SLinus Torvalds !sk_stream_rmem_schedule(sk, skb)) 37751da177e4SLinus Torvalds goto drop; 37761da177e4SLinus Torvalds } 37771da177e4SLinus Torvalds 37781da177e4SLinus Torvalds /* Disable header prediction. */ 37791da177e4SLinus Torvalds tp->pred_flags = 0; 3780463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 37811da177e4SLinus Torvalds 37821da177e4SLinus Torvalds SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n", 37831da177e4SLinus Torvalds tp->rcv_nxt, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq); 37841da177e4SLinus Torvalds 37851da177e4SLinus Torvalds sk_stream_set_owner_r(skb, sk); 37861da177e4SLinus Torvalds 37871da177e4SLinus Torvalds if (!skb_peek(&tp->out_of_order_queue)) { 37881da177e4SLinus Torvalds /* Initial out of order segment, build 1 SACK. */ 3789e60402d0SIlpo Järvinen if (tcp_is_sack(tp)) { 37901da177e4SLinus Torvalds tp->rx_opt.num_sacks = 1; 37911da177e4SLinus Torvalds tp->rx_opt.dsack = 0; 37921da177e4SLinus Torvalds tp->rx_opt.eff_sacks = 1; 37931da177e4SLinus Torvalds tp->selective_acks[0].start_seq = TCP_SKB_CB(skb)->seq; 37941da177e4SLinus Torvalds tp->selective_acks[0].end_seq = 37951da177e4SLinus Torvalds TCP_SKB_CB(skb)->end_seq; 37961da177e4SLinus Torvalds } 37971da177e4SLinus Torvalds __skb_queue_head(&tp->out_of_order_queue,skb); 37981da177e4SLinus Torvalds } else { 37991da177e4SLinus Torvalds struct sk_buff *skb1 = tp->out_of_order_queue.prev; 38001da177e4SLinus Torvalds u32 seq = TCP_SKB_CB(skb)->seq; 38011da177e4SLinus Torvalds u32 end_seq = TCP_SKB_CB(skb)->end_seq; 38021da177e4SLinus Torvalds 38031da177e4SLinus Torvalds if (seq == TCP_SKB_CB(skb1)->end_seq) { 38048728b834SDavid S. Miller __skb_append(skb1, skb, &tp->out_of_order_queue); 38051da177e4SLinus Torvalds 38061da177e4SLinus Torvalds if (!tp->rx_opt.num_sacks || 38071da177e4SLinus Torvalds tp->selective_acks[0].end_seq != seq) 38081da177e4SLinus Torvalds goto add_sack; 38091da177e4SLinus Torvalds 38101da177e4SLinus Torvalds /* Common case: data arrive in order after hole. */ 38111da177e4SLinus Torvalds tp->selective_acks[0].end_seq = end_seq; 38121da177e4SLinus Torvalds return; 38131da177e4SLinus Torvalds } 38141da177e4SLinus Torvalds 38151da177e4SLinus Torvalds /* Find place to insert this segment. */ 38161da177e4SLinus Torvalds do { 38171da177e4SLinus Torvalds if (!after(TCP_SKB_CB(skb1)->seq, seq)) 38181da177e4SLinus Torvalds break; 38191da177e4SLinus Torvalds } while ((skb1 = skb1->prev) != 38201da177e4SLinus Torvalds (struct sk_buff*)&tp->out_of_order_queue); 38211da177e4SLinus Torvalds 38221da177e4SLinus Torvalds /* Do skb overlap to previous one? */ 38231da177e4SLinus Torvalds if (skb1 != (struct sk_buff*)&tp->out_of_order_queue && 38241da177e4SLinus Torvalds before(seq, TCP_SKB_CB(skb1)->end_seq)) { 38251da177e4SLinus Torvalds if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) { 38261da177e4SLinus Torvalds /* All the bits are present. Drop. */ 38271da177e4SLinus Torvalds __kfree_skb(skb); 38281da177e4SLinus Torvalds tcp_dsack_set(tp, seq, end_seq); 38291da177e4SLinus Torvalds goto add_sack; 38301da177e4SLinus Torvalds } 38311da177e4SLinus Torvalds if (after(seq, TCP_SKB_CB(skb1)->seq)) { 38321da177e4SLinus Torvalds /* Partial overlap. */ 38331da177e4SLinus Torvalds tcp_dsack_set(tp, seq, TCP_SKB_CB(skb1)->end_seq); 38341da177e4SLinus Torvalds } else { 38351da177e4SLinus Torvalds skb1 = skb1->prev; 38361da177e4SLinus Torvalds } 38371da177e4SLinus Torvalds } 38381da177e4SLinus Torvalds __skb_insert(skb, skb1, skb1->next, &tp->out_of_order_queue); 38391da177e4SLinus Torvalds 38401da177e4SLinus Torvalds /* And clean segments covered by new one as whole. */ 38411da177e4SLinus Torvalds while ((skb1 = skb->next) != 38421da177e4SLinus Torvalds (struct sk_buff*)&tp->out_of_order_queue && 38431da177e4SLinus Torvalds after(end_seq, TCP_SKB_CB(skb1)->seq)) { 38441da177e4SLinus Torvalds if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) { 38451da177e4SLinus Torvalds tcp_dsack_extend(tp, TCP_SKB_CB(skb1)->seq, end_seq); 38461da177e4SLinus Torvalds break; 38471da177e4SLinus Torvalds } 38488728b834SDavid S. Miller __skb_unlink(skb1, &tp->out_of_order_queue); 38491da177e4SLinus Torvalds tcp_dsack_extend(tp, TCP_SKB_CB(skb1)->seq, TCP_SKB_CB(skb1)->end_seq); 38501da177e4SLinus Torvalds __kfree_skb(skb1); 38511da177e4SLinus Torvalds } 38521da177e4SLinus Torvalds 38531da177e4SLinus Torvalds add_sack: 3854e60402d0SIlpo Järvinen if (tcp_is_sack(tp)) 38551da177e4SLinus Torvalds tcp_sack_new_ofo_skb(sk, seq, end_seq); 38561da177e4SLinus Torvalds } 38571da177e4SLinus Torvalds } 38581da177e4SLinus Torvalds 38591da177e4SLinus Torvalds /* Collapse contiguous sequence of skbs head..tail with 38601da177e4SLinus Torvalds * sequence numbers start..end. 38611da177e4SLinus Torvalds * Segments with FIN/SYN are not collapsed (only because this 38621da177e4SLinus Torvalds * simplifies code) 38631da177e4SLinus Torvalds */ 38641da177e4SLinus Torvalds static void 38658728b834SDavid S. Miller tcp_collapse(struct sock *sk, struct sk_buff_head *list, 38668728b834SDavid S. Miller struct sk_buff *head, struct sk_buff *tail, 38678728b834SDavid S. Miller u32 start, u32 end) 38681da177e4SLinus Torvalds { 38691da177e4SLinus Torvalds struct sk_buff *skb; 38701da177e4SLinus Torvalds 3871caa20d9aSStephen Hemminger /* First, check that queue is collapsible and find 38721da177e4SLinus Torvalds * the point where collapsing can be useful. */ 38731da177e4SLinus Torvalds for (skb = head; skb != tail; ) { 38741da177e4SLinus Torvalds /* No new bits? It is possible on ofo queue. */ 38751da177e4SLinus Torvalds if (!before(start, TCP_SKB_CB(skb)->end_seq)) { 38761da177e4SLinus Torvalds struct sk_buff *next = skb->next; 38778728b834SDavid S. Miller __skb_unlink(skb, list); 38781da177e4SLinus Torvalds __kfree_skb(skb); 38791da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPRCVCOLLAPSED); 38801da177e4SLinus Torvalds skb = next; 38811da177e4SLinus Torvalds continue; 38821da177e4SLinus Torvalds } 38831da177e4SLinus Torvalds 38841da177e4SLinus Torvalds /* The first skb to collapse is: 38851da177e4SLinus Torvalds * - not SYN/FIN and 38861da177e4SLinus Torvalds * - bloated or contains data before "start" or 38871da177e4SLinus Torvalds * overlaps to the next one. 38881da177e4SLinus Torvalds */ 3889aa8223c7SArnaldo Carvalho de Melo if (!tcp_hdr(skb)->syn && !tcp_hdr(skb)->fin && 38901da177e4SLinus Torvalds (tcp_win_from_space(skb->truesize) > skb->len || 38911da177e4SLinus Torvalds before(TCP_SKB_CB(skb)->seq, start) || 38921da177e4SLinus Torvalds (skb->next != tail && 38931da177e4SLinus Torvalds TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb->next)->seq))) 38941da177e4SLinus Torvalds break; 38951da177e4SLinus Torvalds 38961da177e4SLinus Torvalds /* Decided to skip this, advance start seq. */ 38971da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->end_seq; 38981da177e4SLinus Torvalds skb = skb->next; 38991da177e4SLinus Torvalds } 3900aa8223c7SArnaldo Carvalho de Melo if (skb == tail || tcp_hdr(skb)->syn || tcp_hdr(skb)->fin) 39011da177e4SLinus Torvalds return; 39021da177e4SLinus Torvalds 39031da177e4SLinus Torvalds while (before(start, end)) { 39041da177e4SLinus Torvalds struct sk_buff *nskb; 39051da177e4SLinus Torvalds int header = skb_headroom(skb); 39061da177e4SLinus Torvalds int copy = SKB_MAX_ORDER(header, 0); 39071da177e4SLinus Torvalds 39081da177e4SLinus Torvalds /* Too big header? This can happen with IPv6. */ 39091da177e4SLinus Torvalds if (copy < 0) 39101da177e4SLinus Torvalds return; 39111da177e4SLinus Torvalds if (end-start < copy) 39121da177e4SLinus Torvalds copy = end-start; 39131da177e4SLinus Torvalds nskb = alloc_skb(copy+header, GFP_ATOMIC); 39141da177e4SLinus Torvalds if (!nskb) 39151da177e4SLinus Torvalds return; 3916c51957daSArnaldo Carvalho de Melo 391798e399f8SArnaldo Carvalho de Melo skb_set_mac_header(nskb, skb_mac_header(skb) - skb->head); 39189c70220bSArnaldo Carvalho de Melo skb_set_network_header(nskb, (skb_network_header(skb) - 39199c70220bSArnaldo Carvalho de Melo skb->head)); 39209c70220bSArnaldo Carvalho de Melo skb_set_transport_header(nskb, (skb_transport_header(skb) - 39219c70220bSArnaldo Carvalho de Melo skb->head)); 39221da177e4SLinus Torvalds skb_reserve(nskb, header); 39231da177e4SLinus Torvalds memcpy(nskb->head, skb->head, header); 39241da177e4SLinus Torvalds memcpy(nskb->cb, skb->cb, sizeof(skb->cb)); 39251da177e4SLinus Torvalds TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start; 39268728b834SDavid S. Miller __skb_insert(nskb, skb->prev, skb, list); 39271da177e4SLinus Torvalds sk_stream_set_owner_r(nskb, sk); 39281da177e4SLinus Torvalds 39291da177e4SLinus Torvalds /* Copy data, releasing collapsed skbs. */ 39301da177e4SLinus Torvalds while (copy > 0) { 39311da177e4SLinus Torvalds int offset = start - TCP_SKB_CB(skb)->seq; 39321da177e4SLinus Torvalds int size = TCP_SKB_CB(skb)->end_seq - start; 39331da177e4SLinus Torvalds 393409a62660SKris Katterjohn BUG_ON(offset < 0); 39351da177e4SLinus Torvalds if (size > 0) { 39361da177e4SLinus Torvalds size = min(copy, size); 39371da177e4SLinus Torvalds if (skb_copy_bits(skb, offset, skb_put(nskb, size), size)) 39381da177e4SLinus Torvalds BUG(); 39391da177e4SLinus Torvalds TCP_SKB_CB(nskb)->end_seq += size; 39401da177e4SLinus Torvalds copy -= size; 39411da177e4SLinus Torvalds start += size; 39421da177e4SLinus Torvalds } 39431da177e4SLinus Torvalds if (!before(start, TCP_SKB_CB(skb)->end_seq)) { 39441da177e4SLinus Torvalds struct sk_buff *next = skb->next; 39458728b834SDavid S. Miller __skb_unlink(skb, list); 39461da177e4SLinus Torvalds __kfree_skb(skb); 39471da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPRCVCOLLAPSED); 39481da177e4SLinus Torvalds skb = next; 3949aa8223c7SArnaldo Carvalho de Melo if (skb == tail || 3950aa8223c7SArnaldo Carvalho de Melo tcp_hdr(skb)->syn || 3951aa8223c7SArnaldo Carvalho de Melo tcp_hdr(skb)->fin) 39521da177e4SLinus Torvalds return; 39531da177e4SLinus Torvalds } 39541da177e4SLinus Torvalds } 39551da177e4SLinus Torvalds } 39561da177e4SLinus Torvalds } 39571da177e4SLinus Torvalds 39581da177e4SLinus Torvalds /* Collapse ofo queue. Algorithm: select contiguous sequence of skbs 39591da177e4SLinus Torvalds * and tcp_collapse() them until all the queue is collapsed. 39601da177e4SLinus Torvalds */ 39611da177e4SLinus Torvalds static void tcp_collapse_ofo_queue(struct sock *sk) 39621da177e4SLinus Torvalds { 39631da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 39641da177e4SLinus Torvalds struct sk_buff *skb = skb_peek(&tp->out_of_order_queue); 39651da177e4SLinus Torvalds struct sk_buff *head; 39661da177e4SLinus Torvalds u32 start, end; 39671da177e4SLinus Torvalds 39681da177e4SLinus Torvalds if (skb == NULL) 39691da177e4SLinus Torvalds return; 39701da177e4SLinus Torvalds 39711da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->seq; 39721da177e4SLinus Torvalds end = TCP_SKB_CB(skb)->end_seq; 39731da177e4SLinus Torvalds head = skb; 39741da177e4SLinus Torvalds 39751da177e4SLinus Torvalds for (;;) { 39761da177e4SLinus Torvalds skb = skb->next; 39771da177e4SLinus Torvalds 39781da177e4SLinus Torvalds /* Segment is terminated when we see gap or when 39791da177e4SLinus Torvalds * we are at the end of all the queue. */ 39801da177e4SLinus Torvalds if (skb == (struct sk_buff *)&tp->out_of_order_queue || 39811da177e4SLinus Torvalds after(TCP_SKB_CB(skb)->seq, end) || 39821da177e4SLinus Torvalds before(TCP_SKB_CB(skb)->end_seq, start)) { 39838728b834SDavid S. Miller tcp_collapse(sk, &tp->out_of_order_queue, 39848728b834SDavid S. Miller head, skb, start, end); 39851da177e4SLinus Torvalds head = skb; 39861da177e4SLinus Torvalds if (skb == (struct sk_buff *)&tp->out_of_order_queue) 39871da177e4SLinus Torvalds break; 39881da177e4SLinus Torvalds /* Start new segment */ 39891da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->seq; 39901da177e4SLinus Torvalds end = TCP_SKB_CB(skb)->end_seq; 39911da177e4SLinus Torvalds } else { 39921da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->seq, start)) 39931da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->seq; 39941da177e4SLinus Torvalds if (after(TCP_SKB_CB(skb)->end_seq, end)) 39951da177e4SLinus Torvalds end = TCP_SKB_CB(skb)->end_seq; 39961da177e4SLinus Torvalds } 39971da177e4SLinus Torvalds } 39981da177e4SLinus Torvalds } 39991da177e4SLinus Torvalds 40001da177e4SLinus Torvalds /* Reduce allocated memory if we can, trying to get 40011da177e4SLinus Torvalds * the socket within its memory limits again. 40021da177e4SLinus Torvalds * 40031da177e4SLinus Torvalds * Return less than zero if we should start dropping frames 40041da177e4SLinus Torvalds * until the socket owning process reads some of the data 40051da177e4SLinus Torvalds * to stabilize the situation. 40061da177e4SLinus Torvalds */ 40071da177e4SLinus Torvalds static int tcp_prune_queue(struct sock *sk) 40081da177e4SLinus Torvalds { 40091da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 40101da177e4SLinus Torvalds 40111da177e4SLinus Torvalds SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq); 40121da177e4SLinus Torvalds 40131da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_PRUNECALLED); 40141da177e4SLinus Torvalds 40151da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) 40169e412ba7SIlpo Järvinen tcp_clamp_window(sk); 40171da177e4SLinus Torvalds else if (tcp_memory_pressure) 40181da177e4SLinus Torvalds tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss); 40191da177e4SLinus Torvalds 40201da177e4SLinus Torvalds tcp_collapse_ofo_queue(sk); 40218728b834SDavid S. Miller tcp_collapse(sk, &sk->sk_receive_queue, 40228728b834SDavid S. Miller sk->sk_receive_queue.next, 40231da177e4SLinus Torvalds (struct sk_buff*)&sk->sk_receive_queue, 40241da177e4SLinus Torvalds tp->copied_seq, tp->rcv_nxt); 40251da177e4SLinus Torvalds sk_stream_mem_reclaim(sk); 40261da177e4SLinus Torvalds 40271da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) 40281da177e4SLinus Torvalds return 0; 40291da177e4SLinus Torvalds 40301da177e4SLinus Torvalds /* Collapsing did not help, destructive actions follow. 40311da177e4SLinus Torvalds * This must not ever occur. */ 40321da177e4SLinus Torvalds 40331da177e4SLinus Torvalds /* First, purge the out_of_order queue. */ 4034b03efcfbSDavid S. Miller if (!skb_queue_empty(&tp->out_of_order_queue)) { 4035b03efcfbSDavid S. Miller NET_INC_STATS_BH(LINUX_MIB_OFOPRUNED); 40361da177e4SLinus Torvalds __skb_queue_purge(&tp->out_of_order_queue); 40371da177e4SLinus Torvalds 40381da177e4SLinus Torvalds /* Reset SACK state. A conforming SACK implementation will 40391da177e4SLinus Torvalds * do the same at a timeout based retransmit. When a connection 40401da177e4SLinus Torvalds * is in a sad state like this, we care only about integrity 40411da177e4SLinus Torvalds * of the connection not performance. 40421da177e4SLinus Torvalds */ 4043e60402d0SIlpo Järvinen if (tcp_is_sack(tp)) 40441da177e4SLinus Torvalds tcp_sack_reset(&tp->rx_opt); 40451da177e4SLinus Torvalds sk_stream_mem_reclaim(sk); 40461da177e4SLinus Torvalds } 40471da177e4SLinus Torvalds 40481da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) 40491da177e4SLinus Torvalds return 0; 40501da177e4SLinus Torvalds 40511da177e4SLinus Torvalds /* If we are really being abused, tell the caller to silently 40521da177e4SLinus Torvalds * drop receive data on the floor. It will get retransmitted 40531da177e4SLinus Torvalds * and hopefully then we'll have sufficient space. 40541da177e4SLinus Torvalds */ 40551da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_RCVPRUNED); 40561da177e4SLinus Torvalds 40571da177e4SLinus Torvalds /* Massive buffer overcommit. */ 40581da177e4SLinus Torvalds tp->pred_flags = 0; 40591da177e4SLinus Torvalds return -1; 40601da177e4SLinus Torvalds } 40611da177e4SLinus Torvalds 40621da177e4SLinus Torvalds 40631da177e4SLinus Torvalds /* RFC2861, slow part. Adjust cwnd, after it was not full during one rto. 40641da177e4SLinus Torvalds * As additional protections, we do not touch cwnd in retransmission phases, 40651da177e4SLinus Torvalds * and if application hit its sndbuf limit recently. 40661da177e4SLinus Torvalds */ 40671da177e4SLinus Torvalds void tcp_cwnd_application_limited(struct sock *sk) 40681da177e4SLinus Torvalds { 40691da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 40701da177e4SLinus Torvalds 40716687e988SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_ca_state == TCP_CA_Open && 40721da177e4SLinus Torvalds sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 40731da177e4SLinus Torvalds /* Limited by application or receiver window. */ 4074d254bcdbSIlpo Järvinen u32 init_win = tcp_init_cwnd(tp, __sk_dst_get(sk)); 4075d254bcdbSIlpo Järvinen u32 win_used = max(tp->snd_cwnd_used, init_win); 40761da177e4SLinus Torvalds if (win_used < tp->snd_cwnd) { 40776687e988SArnaldo Carvalho de Melo tp->snd_ssthresh = tcp_current_ssthresh(sk); 40781da177e4SLinus Torvalds tp->snd_cwnd = (tp->snd_cwnd + win_used) >> 1; 40791da177e4SLinus Torvalds } 40801da177e4SLinus Torvalds tp->snd_cwnd_used = 0; 40811da177e4SLinus Torvalds } 40821da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 40831da177e4SLinus Torvalds } 40841da177e4SLinus Torvalds 40859e412ba7SIlpo Järvinen static int tcp_should_expand_sndbuf(struct sock *sk) 40860d9901dfSDavid S. Miller { 40879e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk); 40889e412ba7SIlpo Järvinen 40890d9901dfSDavid S. Miller /* If the user specified a specific send buffer setting, do 40900d9901dfSDavid S. Miller * not modify it. 40910d9901dfSDavid S. Miller */ 40920d9901dfSDavid S. Miller if (sk->sk_userlocks & SOCK_SNDBUF_LOCK) 40930d9901dfSDavid S. Miller return 0; 40940d9901dfSDavid S. Miller 40950d9901dfSDavid S. Miller /* If we are under global TCP memory pressure, do not expand. */ 40960d9901dfSDavid S. Miller if (tcp_memory_pressure) 40970d9901dfSDavid S. Miller return 0; 40980d9901dfSDavid S. Miller 40990d9901dfSDavid S. Miller /* If we are under soft global TCP memory pressure, do not expand. */ 41000d9901dfSDavid S. Miller if (atomic_read(&tcp_memory_allocated) >= sysctl_tcp_mem[0]) 41010d9901dfSDavid S. Miller return 0; 41020d9901dfSDavid S. Miller 41030d9901dfSDavid S. Miller /* If we filled the congestion window, do not expand. */ 41040d9901dfSDavid S. Miller if (tp->packets_out >= tp->snd_cwnd) 41050d9901dfSDavid S. Miller return 0; 41060d9901dfSDavid S. Miller 41070d9901dfSDavid S. Miller return 1; 41080d9901dfSDavid S. Miller } 41091da177e4SLinus Torvalds 41101da177e4SLinus Torvalds /* When incoming ACK allowed to free some skb from write_queue, 41111da177e4SLinus Torvalds * we remember this event in flag SOCK_QUEUE_SHRUNK and wake up socket 41121da177e4SLinus Torvalds * on the exit from tcp input handler. 41131da177e4SLinus Torvalds * 41141da177e4SLinus Torvalds * PROBLEM: sndbuf expansion does not work well with largesend. 41151da177e4SLinus Torvalds */ 41161da177e4SLinus Torvalds static void tcp_new_space(struct sock *sk) 41171da177e4SLinus Torvalds { 41181da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 41191da177e4SLinus Torvalds 41209e412ba7SIlpo Järvinen if (tcp_should_expand_sndbuf(sk)) { 4121c1b4a7e6SDavid S. Miller int sndmem = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) + 41221da177e4SLinus Torvalds MAX_TCP_HEADER + 16 + sizeof(struct sk_buff), 41231da177e4SLinus Torvalds demanded = max_t(unsigned int, tp->snd_cwnd, 41241da177e4SLinus Torvalds tp->reordering + 1); 41251da177e4SLinus Torvalds sndmem *= 2*demanded; 41261da177e4SLinus Torvalds if (sndmem > sk->sk_sndbuf) 41271da177e4SLinus Torvalds sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]); 41281da177e4SLinus Torvalds tp->snd_cwnd_stamp = tcp_time_stamp; 41291da177e4SLinus Torvalds } 41301da177e4SLinus Torvalds 41311da177e4SLinus Torvalds sk->sk_write_space(sk); 41321da177e4SLinus Torvalds } 41331da177e4SLinus Torvalds 413440efc6faSStephen Hemminger static void tcp_check_space(struct sock *sk) 41351da177e4SLinus Torvalds { 41361da177e4SLinus Torvalds if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) { 41371da177e4SLinus Torvalds sock_reset_flag(sk, SOCK_QUEUE_SHRUNK); 41381da177e4SLinus Torvalds if (sk->sk_socket && 41391da177e4SLinus Torvalds test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) 41401da177e4SLinus Torvalds tcp_new_space(sk); 41411da177e4SLinus Torvalds } 41421da177e4SLinus Torvalds } 41431da177e4SLinus Torvalds 41449e412ba7SIlpo Järvinen static inline void tcp_data_snd_check(struct sock *sk) 41451da177e4SLinus Torvalds { 41469e412ba7SIlpo Järvinen tcp_push_pending_frames(sk); 41471da177e4SLinus Torvalds tcp_check_space(sk); 41481da177e4SLinus Torvalds } 41491da177e4SLinus Torvalds 41501da177e4SLinus Torvalds /* 41511da177e4SLinus Torvalds * Check if sending an ack is needed. 41521da177e4SLinus Torvalds */ 41531da177e4SLinus Torvalds static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible) 41541da177e4SLinus Torvalds { 41551da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 41561da177e4SLinus Torvalds 41571da177e4SLinus Torvalds /* More than one full frame received... */ 4158463c84b9SArnaldo Carvalho de Melo if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss 41591da177e4SLinus Torvalds /* ... and right edge of window advances far enough. 41601da177e4SLinus Torvalds * (tcp_recvmsg() will send ACK otherwise). Or... 41611da177e4SLinus Torvalds */ 41621da177e4SLinus Torvalds && __tcp_select_window(sk) >= tp->rcv_wnd) || 41631da177e4SLinus Torvalds /* We ACK each frame or... */ 4164463c84b9SArnaldo Carvalho de Melo tcp_in_quickack_mode(sk) || 41651da177e4SLinus Torvalds /* We have out of order data. */ 41661da177e4SLinus Torvalds (ofo_possible && 41671da177e4SLinus Torvalds skb_peek(&tp->out_of_order_queue))) { 41681da177e4SLinus Torvalds /* Then ack it now */ 41691da177e4SLinus Torvalds tcp_send_ack(sk); 41701da177e4SLinus Torvalds } else { 41711da177e4SLinus Torvalds /* Else, send delayed ack. */ 41721da177e4SLinus Torvalds tcp_send_delayed_ack(sk); 41731da177e4SLinus Torvalds } 41741da177e4SLinus Torvalds } 41751da177e4SLinus Torvalds 417640efc6faSStephen Hemminger static inline void tcp_ack_snd_check(struct sock *sk) 41771da177e4SLinus Torvalds { 4178463c84b9SArnaldo Carvalho de Melo if (!inet_csk_ack_scheduled(sk)) { 41791da177e4SLinus Torvalds /* We sent a data segment already. */ 41801da177e4SLinus Torvalds return; 41811da177e4SLinus Torvalds } 41821da177e4SLinus Torvalds __tcp_ack_snd_check(sk, 1); 41831da177e4SLinus Torvalds } 41841da177e4SLinus Torvalds 41851da177e4SLinus Torvalds /* 41861da177e4SLinus Torvalds * This routine is only called when we have urgent data 4187caa20d9aSStephen Hemminger * signaled. Its the 'slow' part of tcp_urg. It could be 41881da177e4SLinus Torvalds * moved inline now as tcp_urg is only called from one 41891da177e4SLinus Torvalds * place. We handle URGent data wrong. We have to - as 41901da177e4SLinus Torvalds * BSD still doesn't use the correction from RFC961. 41911da177e4SLinus Torvalds * For 1003.1g we should support a new option TCP_STDURG to permit 41921da177e4SLinus Torvalds * either form (or just set the sysctl tcp_stdurg). 41931da177e4SLinus Torvalds */ 41941da177e4SLinus Torvalds 41951da177e4SLinus Torvalds static void tcp_check_urg(struct sock * sk, struct tcphdr * th) 41961da177e4SLinus Torvalds { 41971da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 41981da177e4SLinus Torvalds u32 ptr = ntohs(th->urg_ptr); 41991da177e4SLinus Torvalds 42001da177e4SLinus Torvalds if (ptr && !sysctl_tcp_stdurg) 42011da177e4SLinus Torvalds ptr--; 42021da177e4SLinus Torvalds ptr += ntohl(th->seq); 42031da177e4SLinus Torvalds 42041da177e4SLinus Torvalds /* Ignore urgent data that we've already seen and read. */ 42051da177e4SLinus Torvalds if (after(tp->copied_seq, ptr)) 42061da177e4SLinus Torvalds return; 42071da177e4SLinus Torvalds 42081da177e4SLinus Torvalds /* Do not replay urg ptr. 42091da177e4SLinus Torvalds * 42101da177e4SLinus Torvalds * NOTE: interesting situation not covered by specs. 42111da177e4SLinus Torvalds * Misbehaving sender may send urg ptr, pointing to segment, 42121da177e4SLinus Torvalds * which we already have in ofo queue. We are not able to fetch 42131da177e4SLinus Torvalds * such data and will stay in TCP_URG_NOTYET until will be eaten 42141da177e4SLinus Torvalds * by recvmsg(). Seems, we are not obliged to handle such wicked 42151da177e4SLinus Torvalds * situations. But it is worth to think about possibility of some 42161da177e4SLinus Torvalds * DoSes using some hypothetical application level deadlock. 42171da177e4SLinus Torvalds */ 42181da177e4SLinus Torvalds if (before(ptr, tp->rcv_nxt)) 42191da177e4SLinus Torvalds return; 42201da177e4SLinus Torvalds 42211da177e4SLinus Torvalds /* Do we already have a newer (or duplicate) urgent pointer? */ 42221da177e4SLinus Torvalds if (tp->urg_data && !after(ptr, tp->urg_seq)) 42231da177e4SLinus Torvalds return; 42241da177e4SLinus Torvalds 42251da177e4SLinus Torvalds /* Tell the world about our new urgent pointer. */ 42261da177e4SLinus Torvalds sk_send_sigurg(sk); 42271da177e4SLinus Torvalds 42281da177e4SLinus Torvalds /* We may be adding urgent data when the last byte read was 42291da177e4SLinus Torvalds * urgent. To do this requires some care. We cannot just ignore 42301da177e4SLinus Torvalds * tp->copied_seq since we would read the last urgent byte again 42311da177e4SLinus Torvalds * as data, nor can we alter copied_seq until this data arrives 4232caa20d9aSStephen Hemminger * or we break the semantics of SIOCATMARK (and thus sockatmark()) 42331da177e4SLinus Torvalds * 42341da177e4SLinus Torvalds * NOTE. Double Dutch. Rendering to plain English: author of comment 42351da177e4SLinus Torvalds * above did something sort of send("A", MSG_OOB); send("B", MSG_OOB); 42361da177e4SLinus Torvalds * and expect that both A and B disappear from stream. This is _wrong_. 42371da177e4SLinus Torvalds * Though this happens in BSD with high probability, this is occasional. 42381da177e4SLinus Torvalds * Any application relying on this is buggy. Note also, that fix "works" 42391da177e4SLinus Torvalds * only in this artificial test. Insert some normal data between A and B and we will 42401da177e4SLinus Torvalds * decline of BSD again. Verdict: it is better to remove to trap 42411da177e4SLinus Torvalds * buggy users. 42421da177e4SLinus Torvalds */ 42431da177e4SLinus Torvalds if (tp->urg_seq == tp->copied_seq && tp->urg_data && 42441da177e4SLinus Torvalds !sock_flag(sk, SOCK_URGINLINE) && 42451da177e4SLinus Torvalds tp->copied_seq != tp->rcv_nxt) { 42461da177e4SLinus Torvalds struct sk_buff *skb = skb_peek(&sk->sk_receive_queue); 42471da177e4SLinus Torvalds tp->copied_seq++; 42481da177e4SLinus Torvalds if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) { 42498728b834SDavid S. Miller __skb_unlink(skb, &sk->sk_receive_queue); 42501da177e4SLinus Torvalds __kfree_skb(skb); 42511da177e4SLinus Torvalds } 42521da177e4SLinus Torvalds } 42531da177e4SLinus Torvalds 42541da177e4SLinus Torvalds tp->urg_data = TCP_URG_NOTYET; 42551da177e4SLinus Torvalds tp->urg_seq = ptr; 42561da177e4SLinus Torvalds 42571da177e4SLinus Torvalds /* Disable header prediction. */ 42581da177e4SLinus Torvalds tp->pred_flags = 0; 42591da177e4SLinus Torvalds } 42601da177e4SLinus Torvalds 42611da177e4SLinus Torvalds /* This is the 'fast' part of urgent handling. */ 42621da177e4SLinus Torvalds static void tcp_urg(struct sock *sk, struct sk_buff *skb, struct tcphdr *th) 42631da177e4SLinus Torvalds { 42641da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 42651da177e4SLinus Torvalds 42661da177e4SLinus Torvalds /* Check if we get a new urgent pointer - normally not. */ 42671da177e4SLinus Torvalds if (th->urg) 42681da177e4SLinus Torvalds tcp_check_urg(sk,th); 42691da177e4SLinus Torvalds 42701da177e4SLinus Torvalds /* Do we wait for any urgent data? - normally not... */ 42711da177e4SLinus Torvalds if (tp->urg_data == TCP_URG_NOTYET) { 42721da177e4SLinus Torvalds u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) - 42731da177e4SLinus Torvalds th->syn; 42741da177e4SLinus Torvalds 42751da177e4SLinus Torvalds /* Is the urgent pointer pointing into this packet? */ 42761da177e4SLinus Torvalds if (ptr < skb->len) { 42771da177e4SLinus Torvalds u8 tmp; 42781da177e4SLinus Torvalds if (skb_copy_bits(skb, ptr, &tmp, 1)) 42791da177e4SLinus Torvalds BUG(); 42801da177e4SLinus Torvalds tp->urg_data = TCP_URG_VALID | tmp; 42811da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) 42821da177e4SLinus Torvalds sk->sk_data_ready(sk, 0); 42831da177e4SLinus Torvalds } 42841da177e4SLinus Torvalds } 42851da177e4SLinus Torvalds } 42861da177e4SLinus Torvalds 42871da177e4SLinus Torvalds static int tcp_copy_to_iovec(struct sock *sk, struct sk_buff *skb, int hlen) 42881da177e4SLinus Torvalds { 42891da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 42901da177e4SLinus Torvalds int chunk = skb->len - hlen; 42911da177e4SLinus Torvalds int err; 42921da177e4SLinus Torvalds 42931da177e4SLinus Torvalds local_bh_enable(); 429460476372SHerbert Xu if (skb_csum_unnecessary(skb)) 42951da177e4SLinus Torvalds err = skb_copy_datagram_iovec(skb, hlen, tp->ucopy.iov, chunk); 42961da177e4SLinus Torvalds else 42971da177e4SLinus Torvalds err = skb_copy_and_csum_datagram_iovec(skb, hlen, 42981da177e4SLinus Torvalds tp->ucopy.iov); 42991da177e4SLinus Torvalds 43001da177e4SLinus Torvalds if (!err) { 43011da177e4SLinus Torvalds tp->ucopy.len -= chunk; 43021da177e4SLinus Torvalds tp->copied_seq += chunk; 43031da177e4SLinus Torvalds tcp_rcv_space_adjust(sk); 43041da177e4SLinus Torvalds } 43051da177e4SLinus Torvalds 43061da177e4SLinus Torvalds local_bh_disable(); 43071da177e4SLinus Torvalds return err; 43081da177e4SLinus Torvalds } 43091da177e4SLinus Torvalds 4310b51655b9SAl Viro static __sum16 __tcp_checksum_complete_user(struct sock *sk, struct sk_buff *skb) 43111da177e4SLinus Torvalds { 4312b51655b9SAl Viro __sum16 result; 43131da177e4SLinus Torvalds 43141da177e4SLinus Torvalds if (sock_owned_by_user(sk)) { 43151da177e4SLinus Torvalds local_bh_enable(); 43161da177e4SLinus Torvalds result = __tcp_checksum_complete(skb); 43171da177e4SLinus Torvalds local_bh_disable(); 43181da177e4SLinus Torvalds } else { 43191da177e4SLinus Torvalds result = __tcp_checksum_complete(skb); 43201da177e4SLinus Torvalds } 43211da177e4SLinus Torvalds return result; 43221da177e4SLinus Torvalds } 43231da177e4SLinus Torvalds 432440efc6faSStephen Hemminger static inline int tcp_checksum_complete_user(struct sock *sk, struct sk_buff *skb) 43251da177e4SLinus Torvalds { 432660476372SHerbert Xu return !skb_csum_unnecessary(skb) && 43271da177e4SLinus Torvalds __tcp_checksum_complete_user(sk, skb); 43281da177e4SLinus Torvalds } 43291da177e4SLinus Torvalds 43301a2449a8SChris Leech #ifdef CONFIG_NET_DMA 43311a2449a8SChris Leech static int tcp_dma_try_early_copy(struct sock *sk, struct sk_buff *skb, int hlen) 43321a2449a8SChris Leech { 43331a2449a8SChris Leech struct tcp_sock *tp = tcp_sk(sk); 43341a2449a8SChris Leech int chunk = skb->len - hlen; 43351a2449a8SChris Leech int dma_cookie; 43361a2449a8SChris Leech int copied_early = 0; 43371a2449a8SChris Leech 43381a2449a8SChris Leech if (tp->ucopy.wakeup) 43391a2449a8SChris Leech return 0; 43401a2449a8SChris Leech 43411a2449a8SChris Leech if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list) 43421a2449a8SChris Leech tp->ucopy.dma_chan = get_softnet_dma(); 43431a2449a8SChris Leech 434460476372SHerbert Xu if (tp->ucopy.dma_chan && skb_csum_unnecessary(skb)) { 43451a2449a8SChris Leech 43461a2449a8SChris Leech dma_cookie = dma_skb_copy_datagram_iovec(tp->ucopy.dma_chan, 43471a2449a8SChris Leech skb, hlen, tp->ucopy.iov, chunk, tp->ucopy.pinned_list); 43481a2449a8SChris Leech 43491a2449a8SChris Leech if (dma_cookie < 0) 43501a2449a8SChris Leech goto out; 43511a2449a8SChris Leech 43521a2449a8SChris Leech tp->ucopy.dma_cookie = dma_cookie; 43531a2449a8SChris Leech copied_early = 1; 43541a2449a8SChris Leech 43551a2449a8SChris Leech tp->ucopy.len -= chunk; 43561a2449a8SChris Leech tp->copied_seq += chunk; 43571a2449a8SChris Leech tcp_rcv_space_adjust(sk); 43581a2449a8SChris Leech 43591a2449a8SChris Leech if ((tp->ucopy.len == 0) || 4360aa8223c7SArnaldo Carvalho de Melo (tcp_flag_word(tcp_hdr(skb)) & TCP_FLAG_PSH) || 43611a2449a8SChris Leech (atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1))) { 43621a2449a8SChris Leech tp->ucopy.wakeup = 1; 43631a2449a8SChris Leech sk->sk_data_ready(sk, 0); 43641a2449a8SChris Leech } 43651a2449a8SChris Leech } else if (chunk > 0) { 43661a2449a8SChris Leech tp->ucopy.wakeup = 1; 43671a2449a8SChris Leech sk->sk_data_ready(sk, 0); 43681a2449a8SChris Leech } 43691a2449a8SChris Leech out: 43701a2449a8SChris Leech return copied_early; 43711a2449a8SChris Leech } 43721a2449a8SChris Leech #endif /* CONFIG_NET_DMA */ 43731a2449a8SChris Leech 43741da177e4SLinus Torvalds /* 43751da177e4SLinus Torvalds * TCP receive function for the ESTABLISHED state. 43761da177e4SLinus Torvalds * 43771da177e4SLinus Torvalds * It is split into a fast path and a slow path. The fast path is 43781da177e4SLinus Torvalds * disabled when: 43791da177e4SLinus Torvalds * - A zero window was announced from us - zero window probing 43801da177e4SLinus Torvalds * is only handled properly in the slow path. 43811da177e4SLinus Torvalds * - Out of order segments arrived. 43821da177e4SLinus Torvalds * - Urgent data is expected. 43831da177e4SLinus Torvalds * - There is no buffer space left 43841da177e4SLinus Torvalds * - Unexpected TCP flags/window values/header lengths are received 43851da177e4SLinus Torvalds * (detected by checking the TCP header against pred_flags) 43861da177e4SLinus Torvalds * - Data is sent in both directions. Fast path only supports pure senders 43871da177e4SLinus Torvalds * or pure receivers (this means either the sequence number or the ack 43881da177e4SLinus Torvalds * value must stay constant) 43891da177e4SLinus Torvalds * - Unexpected TCP option. 43901da177e4SLinus Torvalds * 43911da177e4SLinus Torvalds * When these conditions are not satisfied it drops into a standard 43921da177e4SLinus Torvalds * receive procedure patterned after RFC793 to handle all cases. 43931da177e4SLinus Torvalds * The first three cases are guaranteed by proper pred_flags setting, 43941da177e4SLinus Torvalds * the rest is checked inline. Fast processing is turned on in 43951da177e4SLinus Torvalds * tcp_data_queue when everything is OK. 43961da177e4SLinus Torvalds */ 43971da177e4SLinus Torvalds int tcp_rcv_established(struct sock *sk, struct sk_buff *skb, 43981da177e4SLinus Torvalds struct tcphdr *th, unsigned len) 43991da177e4SLinus Torvalds { 44001da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 44011da177e4SLinus Torvalds 44021da177e4SLinus Torvalds /* 44031da177e4SLinus Torvalds * Header prediction. 44041da177e4SLinus Torvalds * The code loosely follows the one in the famous 44051da177e4SLinus Torvalds * "30 instruction TCP receive" Van Jacobson mail. 44061da177e4SLinus Torvalds * 44071da177e4SLinus Torvalds * Van's trick is to deposit buffers into socket queue 44081da177e4SLinus Torvalds * on a device interrupt, to call tcp_recv function 44091da177e4SLinus Torvalds * on the receive process context and checksum and copy 44101da177e4SLinus Torvalds * the buffer to user space. smart... 44111da177e4SLinus Torvalds * 44121da177e4SLinus Torvalds * Our current scheme is not silly either but we take the 44131da177e4SLinus Torvalds * extra cost of the net_bh soft interrupt processing... 44141da177e4SLinus Torvalds * We do checksum and copy also but from device to kernel. 44151da177e4SLinus Torvalds */ 44161da177e4SLinus Torvalds 44171da177e4SLinus Torvalds tp->rx_opt.saw_tstamp = 0; 44181da177e4SLinus Torvalds 44191da177e4SLinus Torvalds /* pred_flags is 0xS?10 << 16 + snd_wnd 4420caa20d9aSStephen Hemminger * if header_prediction is to be made 44211da177e4SLinus Torvalds * 'S' will always be tp->tcp_header_len >> 2 44221da177e4SLinus Torvalds * '?' will be 0 for the fast path, otherwise pred_flags is 0 to 44231da177e4SLinus Torvalds * turn it off (when there are holes in the receive 44241da177e4SLinus Torvalds * space for instance) 44251da177e4SLinus Torvalds * PSH flag is ignored. 44261da177e4SLinus Torvalds */ 44271da177e4SLinus Torvalds 44281da177e4SLinus Torvalds if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags && 44291da177e4SLinus Torvalds TCP_SKB_CB(skb)->seq == tp->rcv_nxt) { 44301da177e4SLinus Torvalds int tcp_header_len = tp->tcp_header_len; 44311da177e4SLinus Torvalds 44321da177e4SLinus Torvalds /* Timestamp header prediction: tcp_header_len 44331da177e4SLinus Torvalds * is automatically equal to th->doff*4 due to pred_flags 44341da177e4SLinus Torvalds * match. 44351da177e4SLinus Torvalds */ 44361da177e4SLinus Torvalds 44371da177e4SLinus Torvalds /* Check timestamp */ 44381da177e4SLinus Torvalds if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) { 44394f3608b7SAl Viro __be32 *ptr = (__be32 *)(th + 1); 44401da177e4SLinus Torvalds 44411da177e4SLinus Torvalds /* No? Slow path! */ 44424f3608b7SAl Viro if (*ptr != htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) 44431da177e4SLinus Torvalds | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) 44441da177e4SLinus Torvalds goto slow_path; 44451da177e4SLinus Torvalds 44461da177e4SLinus Torvalds tp->rx_opt.saw_tstamp = 1; 44471da177e4SLinus Torvalds ++ptr; 44481da177e4SLinus Torvalds tp->rx_opt.rcv_tsval = ntohl(*ptr); 44491da177e4SLinus Torvalds ++ptr; 44501da177e4SLinus Torvalds tp->rx_opt.rcv_tsecr = ntohl(*ptr); 44511da177e4SLinus Torvalds 44521da177e4SLinus Torvalds /* If PAWS failed, check it more carefully in slow path */ 44531da177e4SLinus Torvalds if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0) 44541da177e4SLinus Torvalds goto slow_path; 44551da177e4SLinus Torvalds 44561da177e4SLinus Torvalds /* DO NOT update ts_recent here, if checksum fails 44571da177e4SLinus Torvalds * and timestamp was corrupted part, it will result 44581da177e4SLinus Torvalds * in a hung connection since we will drop all 44591da177e4SLinus Torvalds * future packets due to the PAWS test. 44601da177e4SLinus Torvalds */ 44611da177e4SLinus Torvalds } 44621da177e4SLinus Torvalds 44631da177e4SLinus Torvalds if (len <= tcp_header_len) { 44641da177e4SLinus Torvalds /* Bulk data transfer: sender */ 44651da177e4SLinus Torvalds if (len == tcp_header_len) { 44661da177e4SLinus Torvalds /* Predicted packet is in window by definition. 44671da177e4SLinus Torvalds * seq == rcv_nxt and rcv_wup <= rcv_nxt. 44681da177e4SLinus Torvalds * Hence, check seq<=rcv_wup reduces to: 44691da177e4SLinus Torvalds */ 44701da177e4SLinus Torvalds if (tcp_header_len == 44711da177e4SLinus Torvalds (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) && 44721da177e4SLinus Torvalds tp->rcv_nxt == tp->rcv_wup) 44731da177e4SLinus Torvalds tcp_store_ts_recent(tp); 44741da177e4SLinus Torvalds 44751da177e4SLinus Torvalds /* We know that such packets are checksummed 44761da177e4SLinus Torvalds * on entry. 44771da177e4SLinus Torvalds */ 44781da177e4SLinus Torvalds tcp_ack(sk, skb, 0); 44791da177e4SLinus Torvalds __kfree_skb(skb); 44809e412ba7SIlpo Järvinen tcp_data_snd_check(sk); 44811da177e4SLinus Torvalds return 0; 44821da177e4SLinus Torvalds } else { /* Header too small */ 44831da177e4SLinus Torvalds TCP_INC_STATS_BH(TCP_MIB_INERRS); 44841da177e4SLinus Torvalds goto discard; 44851da177e4SLinus Torvalds } 44861da177e4SLinus Torvalds } else { 44871da177e4SLinus Torvalds int eaten = 0; 44881a2449a8SChris Leech int copied_early = 0; 44891da177e4SLinus Torvalds 44901a2449a8SChris Leech if (tp->copied_seq == tp->rcv_nxt && 44911a2449a8SChris Leech len - tcp_header_len <= tp->ucopy.len) { 44921a2449a8SChris Leech #ifdef CONFIG_NET_DMA 44931a2449a8SChris Leech if (tcp_dma_try_early_copy(sk, skb, tcp_header_len)) { 44941a2449a8SChris Leech copied_early = 1; 44951a2449a8SChris Leech eaten = 1; 44961a2449a8SChris Leech } 44971a2449a8SChris Leech #endif 44981a2449a8SChris Leech if (tp->ucopy.task == current && sock_owned_by_user(sk) && !copied_early) { 44991da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 45001da177e4SLinus Torvalds 45011a2449a8SChris Leech if (!tcp_copy_to_iovec(sk, skb, tcp_header_len)) 45021a2449a8SChris Leech eaten = 1; 45031a2449a8SChris Leech } 45041a2449a8SChris Leech if (eaten) { 45051da177e4SLinus Torvalds /* Predicted packet is in window by definition. 45061da177e4SLinus Torvalds * seq == rcv_nxt and rcv_wup <= rcv_nxt. 45071da177e4SLinus Torvalds * Hence, check seq<=rcv_wup reduces to: 45081da177e4SLinus Torvalds */ 45091da177e4SLinus Torvalds if (tcp_header_len == 45101da177e4SLinus Torvalds (sizeof(struct tcphdr) + 45111da177e4SLinus Torvalds TCPOLEN_TSTAMP_ALIGNED) && 45121da177e4SLinus Torvalds tp->rcv_nxt == tp->rcv_wup) 45131da177e4SLinus Torvalds tcp_store_ts_recent(tp); 45141da177e4SLinus Torvalds 4515463c84b9SArnaldo Carvalho de Melo tcp_rcv_rtt_measure_ts(sk, skb); 45161da177e4SLinus Torvalds 45171da177e4SLinus Torvalds __skb_pull(skb, tcp_header_len); 45181da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 45191da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPHPHITSTOUSER); 45201da177e4SLinus Torvalds } 45211a2449a8SChris Leech if (copied_early) 45221a2449a8SChris Leech tcp_cleanup_rbuf(sk, skb->len); 45231da177e4SLinus Torvalds } 45241da177e4SLinus Torvalds if (!eaten) { 45251da177e4SLinus Torvalds if (tcp_checksum_complete_user(sk, skb)) 45261da177e4SLinus Torvalds goto csum_error; 45271da177e4SLinus Torvalds 45281da177e4SLinus Torvalds /* Predicted packet is in window by definition. 45291da177e4SLinus Torvalds * seq == rcv_nxt and rcv_wup <= rcv_nxt. 45301da177e4SLinus Torvalds * Hence, check seq<=rcv_wup reduces to: 45311da177e4SLinus Torvalds */ 45321da177e4SLinus Torvalds if (tcp_header_len == 45331da177e4SLinus Torvalds (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) && 45341da177e4SLinus Torvalds tp->rcv_nxt == tp->rcv_wup) 45351da177e4SLinus Torvalds tcp_store_ts_recent(tp); 45361da177e4SLinus Torvalds 4537463c84b9SArnaldo Carvalho de Melo tcp_rcv_rtt_measure_ts(sk, skb); 45381da177e4SLinus Torvalds 45391da177e4SLinus Torvalds if ((int)skb->truesize > sk->sk_forward_alloc) 45401da177e4SLinus Torvalds goto step5; 45411da177e4SLinus Torvalds 45421da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPHPHITS); 45431da177e4SLinus Torvalds 45441da177e4SLinus Torvalds /* Bulk data transfer: receiver */ 45451da177e4SLinus Torvalds __skb_pull(skb,tcp_header_len); 45461da177e4SLinus Torvalds __skb_queue_tail(&sk->sk_receive_queue, skb); 45471da177e4SLinus Torvalds sk_stream_set_owner_r(skb, sk); 45481da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; 45491da177e4SLinus Torvalds } 45501da177e4SLinus Torvalds 45519e412ba7SIlpo Järvinen tcp_event_data_recv(sk, skb); 45521da177e4SLinus Torvalds 45531da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) { 45541da177e4SLinus Torvalds /* Well, only one small jumplet in fast path... */ 45551da177e4SLinus Torvalds tcp_ack(sk, skb, FLAG_DATA); 45569e412ba7SIlpo Järvinen tcp_data_snd_check(sk); 4557463c84b9SArnaldo Carvalho de Melo if (!inet_csk_ack_scheduled(sk)) 45581da177e4SLinus Torvalds goto no_ack; 45591da177e4SLinus Torvalds } 45601da177e4SLinus Torvalds 45611da177e4SLinus Torvalds __tcp_ack_snd_check(sk, 0); 45621da177e4SLinus Torvalds no_ack: 45631a2449a8SChris Leech #ifdef CONFIG_NET_DMA 45641a2449a8SChris Leech if (copied_early) 45651a2449a8SChris Leech __skb_queue_tail(&sk->sk_async_wait_queue, skb); 45661a2449a8SChris Leech else 45671a2449a8SChris Leech #endif 45681da177e4SLinus Torvalds if (eaten) 45691da177e4SLinus Torvalds __kfree_skb(skb); 45701da177e4SLinus Torvalds else 45711da177e4SLinus Torvalds sk->sk_data_ready(sk, 0); 45721da177e4SLinus Torvalds return 0; 45731da177e4SLinus Torvalds } 45741da177e4SLinus Torvalds } 45751da177e4SLinus Torvalds 45761da177e4SLinus Torvalds slow_path: 45771da177e4SLinus Torvalds if (len < (th->doff<<2) || tcp_checksum_complete_user(sk, skb)) 45781da177e4SLinus Torvalds goto csum_error; 45791da177e4SLinus Torvalds 45801da177e4SLinus Torvalds /* 45811da177e4SLinus Torvalds * RFC1323: H1. Apply PAWS check first. 45821da177e4SLinus Torvalds */ 45831da177e4SLinus Torvalds if (tcp_fast_parse_options(skb, th, tp) && tp->rx_opt.saw_tstamp && 4584463c84b9SArnaldo Carvalho de Melo tcp_paws_discard(sk, skb)) { 45851da177e4SLinus Torvalds if (!th->rst) { 45861da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED); 45871da177e4SLinus Torvalds tcp_send_dupack(sk, skb); 45881da177e4SLinus Torvalds goto discard; 45891da177e4SLinus Torvalds } 45901da177e4SLinus Torvalds /* Resets are accepted even if PAWS failed. 45911da177e4SLinus Torvalds 45921da177e4SLinus Torvalds ts_recent update must be made after we are sure 45931da177e4SLinus Torvalds that the packet is in window. 45941da177e4SLinus Torvalds */ 45951da177e4SLinus Torvalds } 45961da177e4SLinus Torvalds 45971da177e4SLinus Torvalds /* 45981da177e4SLinus Torvalds * Standard slow path. 45991da177e4SLinus Torvalds */ 46001da177e4SLinus Torvalds 46011da177e4SLinus Torvalds if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) { 46021da177e4SLinus Torvalds /* RFC793, page 37: "In all states except SYN-SENT, all reset 46031da177e4SLinus Torvalds * (RST) segments are validated by checking their SEQ-fields." 46041da177e4SLinus Torvalds * And page 69: "If an incoming segment is not acceptable, 46051da177e4SLinus Torvalds * an acknowledgment should be sent in reply (unless the RST bit 46061da177e4SLinus Torvalds * is set, if so drop the segment and return)". 46071da177e4SLinus Torvalds */ 46081da177e4SLinus Torvalds if (!th->rst) 46091da177e4SLinus Torvalds tcp_send_dupack(sk, skb); 46101da177e4SLinus Torvalds goto discard; 46111da177e4SLinus Torvalds } 46121da177e4SLinus Torvalds 46131da177e4SLinus Torvalds if (th->rst) { 46141da177e4SLinus Torvalds tcp_reset(sk); 46151da177e4SLinus Torvalds goto discard; 46161da177e4SLinus Torvalds } 46171da177e4SLinus Torvalds 46181da177e4SLinus Torvalds tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq); 46191da177e4SLinus Torvalds 46201da177e4SLinus Torvalds if (th->syn && !before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { 46211da177e4SLinus Torvalds TCP_INC_STATS_BH(TCP_MIB_INERRS); 46221da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPABORTONSYN); 46231da177e4SLinus Torvalds tcp_reset(sk); 46241da177e4SLinus Torvalds return 1; 46251da177e4SLinus Torvalds } 46261da177e4SLinus Torvalds 46271da177e4SLinus Torvalds step5: 46281da177e4SLinus Torvalds if (th->ack) 46291da177e4SLinus Torvalds tcp_ack(sk, skb, FLAG_SLOWPATH); 46301da177e4SLinus Torvalds 4631463c84b9SArnaldo Carvalho de Melo tcp_rcv_rtt_measure_ts(sk, skb); 46321da177e4SLinus Torvalds 46331da177e4SLinus Torvalds /* Process urgent data. */ 46341da177e4SLinus Torvalds tcp_urg(sk, skb, th); 46351da177e4SLinus Torvalds 46361da177e4SLinus Torvalds /* step 7: process the segment text */ 46371da177e4SLinus Torvalds tcp_data_queue(sk, skb); 46381da177e4SLinus Torvalds 46399e412ba7SIlpo Järvinen tcp_data_snd_check(sk); 46401da177e4SLinus Torvalds tcp_ack_snd_check(sk); 46411da177e4SLinus Torvalds return 0; 46421da177e4SLinus Torvalds 46431da177e4SLinus Torvalds csum_error: 46441da177e4SLinus Torvalds TCP_INC_STATS_BH(TCP_MIB_INERRS); 46451da177e4SLinus Torvalds 46461da177e4SLinus Torvalds discard: 46471da177e4SLinus Torvalds __kfree_skb(skb); 46481da177e4SLinus Torvalds return 0; 46491da177e4SLinus Torvalds } 46501da177e4SLinus Torvalds 46511da177e4SLinus Torvalds static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb, 46521da177e4SLinus Torvalds struct tcphdr *th, unsigned len) 46531da177e4SLinus Torvalds { 46541da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 4655d83d8461SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 46561da177e4SLinus Torvalds int saved_clamp = tp->rx_opt.mss_clamp; 46571da177e4SLinus Torvalds 46581da177e4SLinus Torvalds tcp_parse_options(skb, &tp->rx_opt, 0); 46591da177e4SLinus Torvalds 46601da177e4SLinus Torvalds if (th->ack) { 46611da177e4SLinus Torvalds /* rfc793: 46621da177e4SLinus Torvalds * "If the state is SYN-SENT then 46631da177e4SLinus Torvalds * first check the ACK bit 46641da177e4SLinus Torvalds * If the ACK bit is set 46651da177e4SLinus Torvalds * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send 46661da177e4SLinus Torvalds * a reset (unless the RST bit is set, if so drop 46671da177e4SLinus Torvalds * the segment and return)" 46681da177e4SLinus Torvalds * 46691da177e4SLinus Torvalds * We do not send data with SYN, so that RFC-correct 46701da177e4SLinus Torvalds * test reduces to: 46711da177e4SLinus Torvalds */ 46721da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->ack_seq != tp->snd_nxt) 46731da177e4SLinus Torvalds goto reset_and_undo; 46741da177e4SLinus Torvalds 46751da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && 46761da177e4SLinus Torvalds !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp, 46771da177e4SLinus Torvalds tcp_time_stamp)) { 46781da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_PAWSACTIVEREJECTED); 46791da177e4SLinus Torvalds goto reset_and_undo; 46801da177e4SLinus Torvalds } 46811da177e4SLinus Torvalds 46821da177e4SLinus Torvalds /* Now ACK is acceptable. 46831da177e4SLinus Torvalds * 46841da177e4SLinus Torvalds * "If the RST bit is set 46851da177e4SLinus Torvalds * If the ACK was acceptable then signal the user "error: 46861da177e4SLinus Torvalds * connection reset", drop the segment, enter CLOSED state, 46871da177e4SLinus Torvalds * delete TCB, and return." 46881da177e4SLinus Torvalds */ 46891da177e4SLinus Torvalds 46901da177e4SLinus Torvalds if (th->rst) { 46911da177e4SLinus Torvalds tcp_reset(sk); 46921da177e4SLinus Torvalds goto discard; 46931da177e4SLinus Torvalds } 46941da177e4SLinus Torvalds 46951da177e4SLinus Torvalds /* rfc793: 46961da177e4SLinus Torvalds * "fifth, if neither of the SYN or RST bits is set then 46971da177e4SLinus Torvalds * drop the segment and return." 46981da177e4SLinus Torvalds * 46991da177e4SLinus Torvalds * See note below! 47001da177e4SLinus Torvalds * --ANK(990513) 47011da177e4SLinus Torvalds */ 47021da177e4SLinus Torvalds if (!th->syn) 47031da177e4SLinus Torvalds goto discard_and_undo; 47041da177e4SLinus Torvalds 47051da177e4SLinus Torvalds /* rfc793: 47061da177e4SLinus Torvalds * "If the SYN bit is on ... 47071da177e4SLinus Torvalds * are acceptable then ... 47081da177e4SLinus Torvalds * (our SYN has been ACKed), change the connection 47091da177e4SLinus Torvalds * state to ESTABLISHED..." 47101da177e4SLinus Torvalds */ 47111da177e4SLinus Torvalds 47121da177e4SLinus Torvalds TCP_ECN_rcv_synack(tp, th); 47131da177e4SLinus Torvalds 47141da177e4SLinus Torvalds tp->snd_wl1 = TCP_SKB_CB(skb)->seq; 47151da177e4SLinus Torvalds tcp_ack(sk, skb, FLAG_SLOWPATH); 47161da177e4SLinus Torvalds 47171da177e4SLinus Torvalds /* Ok.. it's good. Set up sequence numbers and 47181da177e4SLinus Torvalds * move to established. 47191da177e4SLinus Torvalds */ 47201da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1; 47211da177e4SLinus Torvalds tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1; 47221da177e4SLinus Torvalds 47231da177e4SLinus Torvalds /* RFC1323: The window in SYN & SYN/ACK segments is 47241da177e4SLinus Torvalds * never scaled. 47251da177e4SLinus Torvalds */ 47261da177e4SLinus Torvalds tp->snd_wnd = ntohs(th->window); 47271da177e4SLinus Torvalds tcp_init_wl(tp, TCP_SKB_CB(skb)->ack_seq, TCP_SKB_CB(skb)->seq); 47281da177e4SLinus Torvalds 47291da177e4SLinus Torvalds if (!tp->rx_opt.wscale_ok) { 47301da177e4SLinus Torvalds tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0; 47311da177e4SLinus Torvalds tp->window_clamp = min(tp->window_clamp, 65535U); 47321da177e4SLinus Torvalds } 47331da177e4SLinus Torvalds 47341da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp) { 47351da177e4SLinus Torvalds tp->rx_opt.tstamp_ok = 1; 47361da177e4SLinus Torvalds tp->tcp_header_len = 47371da177e4SLinus Torvalds sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED; 47381da177e4SLinus Torvalds tp->advmss -= TCPOLEN_TSTAMP_ALIGNED; 47391da177e4SLinus Torvalds tcp_store_ts_recent(tp); 47401da177e4SLinus Torvalds } else { 47411da177e4SLinus Torvalds tp->tcp_header_len = sizeof(struct tcphdr); 47421da177e4SLinus Torvalds } 47431da177e4SLinus Torvalds 4744e60402d0SIlpo Järvinen if (tcp_is_sack(tp) && sysctl_tcp_fack) 4745e60402d0SIlpo Järvinen tcp_enable_fack(tp); 47461da177e4SLinus Torvalds 47475d424d5aSJohn Heffner tcp_mtup_init(sk); 4748d83d8461SArnaldo Carvalho de Melo tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 47491da177e4SLinus Torvalds tcp_initialize_rcv_mss(sk); 47501da177e4SLinus Torvalds 47511da177e4SLinus Torvalds /* Remember, tcp_poll() does not lock socket! 47521da177e4SLinus Torvalds * Change state from SYN-SENT only after copied_seq 47531da177e4SLinus Torvalds * is initialized. */ 47541da177e4SLinus Torvalds tp->copied_seq = tp->rcv_nxt; 4755e16aa207SRalf Baechle smp_mb(); 47561da177e4SLinus Torvalds tcp_set_state(sk, TCP_ESTABLISHED); 47571da177e4SLinus Torvalds 47586b877699SVenkat Yekkirala security_inet_conn_established(sk, skb); 47596b877699SVenkat Yekkirala 47601da177e4SLinus Torvalds /* Make sure socket is routed, for correct metrics. */ 47618292a17aSArnaldo Carvalho de Melo icsk->icsk_af_ops->rebuild_header(sk); 47621da177e4SLinus Torvalds 47631da177e4SLinus Torvalds tcp_init_metrics(sk); 47641da177e4SLinus Torvalds 47656687e988SArnaldo Carvalho de Melo tcp_init_congestion_control(sk); 4766317a76f9SStephen Hemminger 47671da177e4SLinus Torvalds /* Prevent spurious tcp_cwnd_restart() on first data 47681da177e4SLinus Torvalds * packet. 47691da177e4SLinus Torvalds */ 47701da177e4SLinus Torvalds tp->lsndtime = tcp_time_stamp; 47711da177e4SLinus Torvalds 47721da177e4SLinus Torvalds tcp_init_buffer_space(sk); 47731da177e4SLinus Torvalds 47741da177e4SLinus Torvalds if (sock_flag(sk, SOCK_KEEPOPEN)) 4775463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp)); 47761da177e4SLinus Torvalds 47771da177e4SLinus Torvalds if (!tp->rx_opt.snd_wscale) 47781da177e4SLinus Torvalds __tcp_fast_path_on(tp, tp->snd_wnd); 47791da177e4SLinus Torvalds else 47801da177e4SLinus Torvalds tp->pred_flags = 0; 47811da177e4SLinus Torvalds 47821da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) { 47831da177e4SLinus Torvalds sk->sk_state_change(sk); 47841da177e4SLinus Torvalds sk_wake_async(sk, 0, POLL_OUT); 47851da177e4SLinus Torvalds } 47861da177e4SLinus Torvalds 4787295f7324SArnaldo Carvalho de Melo if (sk->sk_write_pending || 4788295f7324SArnaldo Carvalho de Melo icsk->icsk_accept_queue.rskq_defer_accept || 4789295f7324SArnaldo Carvalho de Melo icsk->icsk_ack.pingpong) { 47901da177e4SLinus Torvalds /* Save one ACK. Data will be ready after 47911da177e4SLinus Torvalds * several ticks, if write_pending is set. 47921da177e4SLinus Torvalds * 47931da177e4SLinus Torvalds * It may be deleted, but with this feature tcpdumps 47941da177e4SLinus Torvalds * look so _wonderfully_ clever, that I was not able 47951da177e4SLinus Torvalds * to stand against the temptation 8) --ANK 47961da177e4SLinus Torvalds */ 4797463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk); 4798295f7324SArnaldo Carvalho de Melo icsk->icsk_ack.lrcvtime = tcp_time_stamp; 4799295f7324SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN; 4800463c84b9SArnaldo Carvalho de Melo tcp_incr_quickack(sk); 4801463c84b9SArnaldo Carvalho de Melo tcp_enter_quickack_mode(sk); 48023f421baaSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 48033f421baaSArnaldo Carvalho de Melo TCP_DELACK_MAX, TCP_RTO_MAX); 48041da177e4SLinus Torvalds 48051da177e4SLinus Torvalds discard: 48061da177e4SLinus Torvalds __kfree_skb(skb); 48071da177e4SLinus Torvalds return 0; 48081da177e4SLinus Torvalds } else { 48091da177e4SLinus Torvalds tcp_send_ack(sk); 48101da177e4SLinus Torvalds } 48111da177e4SLinus Torvalds return -1; 48121da177e4SLinus Torvalds } 48131da177e4SLinus Torvalds 48141da177e4SLinus Torvalds /* No ACK in the segment */ 48151da177e4SLinus Torvalds 48161da177e4SLinus Torvalds if (th->rst) { 48171da177e4SLinus Torvalds /* rfc793: 48181da177e4SLinus Torvalds * "If the RST bit is set 48191da177e4SLinus Torvalds * 48201da177e4SLinus Torvalds * Otherwise (no ACK) drop the segment and return." 48211da177e4SLinus Torvalds */ 48221da177e4SLinus Torvalds 48231da177e4SLinus Torvalds goto discard_and_undo; 48241da177e4SLinus Torvalds } 48251da177e4SLinus Torvalds 48261da177e4SLinus Torvalds /* PAWS check. */ 48271da177e4SLinus Torvalds if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp && tcp_paws_check(&tp->rx_opt, 0)) 48281da177e4SLinus Torvalds goto discard_and_undo; 48291da177e4SLinus Torvalds 48301da177e4SLinus Torvalds if (th->syn) { 48311da177e4SLinus Torvalds /* We see SYN without ACK. It is attempt of 48321da177e4SLinus Torvalds * simultaneous connect with crossed SYNs. 48331da177e4SLinus Torvalds * Particularly, it can be connect to self. 48341da177e4SLinus Torvalds */ 48351da177e4SLinus Torvalds tcp_set_state(sk, TCP_SYN_RECV); 48361da177e4SLinus Torvalds 48371da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp) { 48381da177e4SLinus Torvalds tp->rx_opt.tstamp_ok = 1; 48391da177e4SLinus Torvalds tcp_store_ts_recent(tp); 48401da177e4SLinus Torvalds tp->tcp_header_len = 48411da177e4SLinus Torvalds sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED; 48421da177e4SLinus Torvalds } else { 48431da177e4SLinus Torvalds tp->tcp_header_len = sizeof(struct tcphdr); 48441da177e4SLinus Torvalds } 48451da177e4SLinus Torvalds 48461da177e4SLinus Torvalds tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1; 48471da177e4SLinus Torvalds tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1; 48481da177e4SLinus Torvalds 48491da177e4SLinus Torvalds /* RFC1323: The window in SYN & SYN/ACK segments is 48501da177e4SLinus Torvalds * never scaled. 48511da177e4SLinus Torvalds */ 48521da177e4SLinus Torvalds tp->snd_wnd = ntohs(th->window); 48531da177e4SLinus Torvalds tp->snd_wl1 = TCP_SKB_CB(skb)->seq; 48541da177e4SLinus Torvalds tp->max_window = tp->snd_wnd; 48551da177e4SLinus Torvalds 48561da177e4SLinus Torvalds TCP_ECN_rcv_syn(tp, th); 48571da177e4SLinus Torvalds 48585d424d5aSJohn Heffner tcp_mtup_init(sk); 4859d83d8461SArnaldo Carvalho de Melo tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 48601da177e4SLinus Torvalds tcp_initialize_rcv_mss(sk); 48611da177e4SLinus Torvalds 48621da177e4SLinus Torvalds 48631da177e4SLinus Torvalds tcp_send_synack(sk); 48641da177e4SLinus Torvalds #if 0 48651da177e4SLinus Torvalds /* Note, we could accept data and URG from this segment. 48661da177e4SLinus Torvalds * There are no obstacles to make this. 48671da177e4SLinus Torvalds * 48681da177e4SLinus Torvalds * However, if we ignore data in ACKless segments sometimes, 48691da177e4SLinus Torvalds * we have no reasons to accept it sometimes. 48701da177e4SLinus Torvalds * Also, seems the code doing it in step6 of tcp_rcv_state_process 48711da177e4SLinus Torvalds * is not flawless. So, discard packet for sanity. 48721da177e4SLinus Torvalds * Uncomment this return to process the data. 48731da177e4SLinus Torvalds */ 48741da177e4SLinus Torvalds return -1; 48751da177e4SLinus Torvalds #else 48761da177e4SLinus Torvalds goto discard; 48771da177e4SLinus Torvalds #endif 48781da177e4SLinus Torvalds } 48791da177e4SLinus Torvalds /* "fifth, if neither of the SYN or RST bits is set then 48801da177e4SLinus Torvalds * drop the segment and return." 48811da177e4SLinus Torvalds */ 48821da177e4SLinus Torvalds 48831da177e4SLinus Torvalds discard_and_undo: 48841da177e4SLinus Torvalds tcp_clear_options(&tp->rx_opt); 48851da177e4SLinus Torvalds tp->rx_opt.mss_clamp = saved_clamp; 48861da177e4SLinus Torvalds goto discard; 48871da177e4SLinus Torvalds 48881da177e4SLinus Torvalds reset_and_undo: 48891da177e4SLinus Torvalds tcp_clear_options(&tp->rx_opt); 48901da177e4SLinus Torvalds tp->rx_opt.mss_clamp = saved_clamp; 48911da177e4SLinus Torvalds return 1; 48921da177e4SLinus Torvalds } 48931da177e4SLinus Torvalds 48941da177e4SLinus Torvalds 48951da177e4SLinus Torvalds /* 48961da177e4SLinus Torvalds * This function implements the receiving procedure of RFC 793 for 48971da177e4SLinus Torvalds * all states except ESTABLISHED and TIME_WAIT. 48981da177e4SLinus Torvalds * It's called from both tcp_v4_rcv and tcp_v6_rcv and should be 48991da177e4SLinus Torvalds * address independent. 49001da177e4SLinus Torvalds */ 49011da177e4SLinus Torvalds 49021da177e4SLinus Torvalds int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb, 49031da177e4SLinus Torvalds struct tcphdr *th, unsigned len) 49041da177e4SLinus Torvalds { 49051da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk); 49068292a17aSArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk); 49071da177e4SLinus Torvalds int queued = 0; 49081da177e4SLinus Torvalds 49091da177e4SLinus Torvalds tp->rx_opt.saw_tstamp = 0; 49101da177e4SLinus Torvalds 49111da177e4SLinus Torvalds switch (sk->sk_state) { 49121da177e4SLinus Torvalds case TCP_CLOSE: 49131da177e4SLinus Torvalds goto discard; 49141da177e4SLinus Torvalds 49151da177e4SLinus Torvalds case TCP_LISTEN: 49161da177e4SLinus Torvalds if (th->ack) 49171da177e4SLinus Torvalds return 1; 49181da177e4SLinus Torvalds 49191da177e4SLinus Torvalds if (th->rst) 49201da177e4SLinus Torvalds goto discard; 49211da177e4SLinus Torvalds 49221da177e4SLinus Torvalds if (th->syn) { 49238292a17aSArnaldo Carvalho de Melo if (icsk->icsk_af_ops->conn_request(sk, skb) < 0) 49241da177e4SLinus Torvalds return 1; 49251da177e4SLinus Torvalds 49261da177e4SLinus Torvalds /* Now we have several options: In theory there is 49271da177e4SLinus Torvalds * nothing else in the frame. KA9Q has an option to 49281da177e4SLinus Torvalds * send data with the syn, BSD accepts data with the 49291da177e4SLinus Torvalds * syn up to the [to be] advertised window and 49301da177e4SLinus Torvalds * Solaris 2.1 gives you a protocol error. For now 49311da177e4SLinus Torvalds * we just ignore it, that fits the spec precisely 49321da177e4SLinus Torvalds * and avoids incompatibilities. It would be nice in 49331da177e4SLinus Torvalds * future to drop through and process the data. 49341da177e4SLinus Torvalds * 49351da177e4SLinus Torvalds * Now that TTCP is starting to be used we ought to 49361da177e4SLinus Torvalds * queue this data. 49371da177e4SLinus Torvalds * But, this leaves one open to an easy denial of 49381da177e4SLinus Torvalds * service attack, and SYN cookies can't defend 49391da177e4SLinus Torvalds * against this problem. So, we drop the data 4940fb7e2399SMasayuki Nakagawa * in the interest of security over speed unless 4941fb7e2399SMasayuki Nakagawa * it's still in use. 49421da177e4SLinus Torvalds */ 4943fb7e2399SMasayuki Nakagawa kfree_skb(skb); 4944fb7e2399SMasayuki Nakagawa return 0; 49451da177e4SLinus Torvalds } 49461da177e4SLinus Torvalds goto discard; 49471da177e4SLinus Torvalds 49481da177e4SLinus Torvalds case TCP_SYN_SENT: 49491da177e4SLinus Torvalds queued = tcp_rcv_synsent_state_process(sk, skb, th, len); 49501da177e4SLinus Torvalds if (queued >= 0) 49511da177e4SLinus Torvalds return queued; 49521da177e4SLinus Torvalds 49531da177e4SLinus Torvalds /* Do step6 onward by hand. */ 49541da177e4SLinus Torvalds tcp_urg(sk, skb, th); 49551da177e4SLinus Torvalds __kfree_skb(skb); 49569e412ba7SIlpo Järvinen tcp_data_snd_check(sk); 49571da177e4SLinus Torvalds return 0; 49581da177e4SLinus Torvalds } 49591da177e4SLinus Torvalds 49601da177e4SLinus Torvalds if (tcp_fast_parse_options(skb, th, tp) && tp->rx_opt.saw_tstamp && 4961463c84b9SArnaldo Carvalho de Melo tcp_paws_discard(sk, skb)) { 49621da177e4SLinus Torvalds if (!th->rst) { 49631da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_PAWSESTABREJECTED); 49641da177e4SLinus Torvalds tcp_send_dupack(sk, skb); 49651da177e4SLinus Torvalds goto discard; 49661da177e4SLinus Torvalds } 49671da177e4SLinus Torvalds /* Reset is accepted even if it did not pass PAWS. */ 49681da177e4SLinus Torvalds } 49691da177e4SLinus Torvalds 49701da177e4SLinus Torvalds /* step 1: check sequence number */ 49711da177e4SLinus Torvalds if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) { 49721da177e4SLinus Torvalds if (!th->rst) 49731da177e4SLinus Torvalds tcp_send_dupack(sk, skb); 49741da177e4SLinus Torvalds goto discard; 49751da177e4SLinus Torvalds } 49761da177e4SLinus Torvalds 49771da177e4SLinus Torvalds /* step 2: check RST bit */ 49781da177e4SLinus Torvalds if (th->rst) { 49791da177e4SLinus Torvalds tcp_reset(sk); 49801da177e4SLinus Torvalds goto discard; 49811da177e4SLinus Torvalds } 49821da177e4SLinus Torvalds 49831da177e4SLinus Torvalds tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq); 49841da177e4SLinus Torvalds 49851da177e4SLinus Torvalds /* step 3: check security and precedence [ignored] */ 49861da177e4SLinus Torvalds 49871da177e4SLinus Torvalds /* step 4: 49881da177e4SLinus Torvalds * 49891da177e4SLinus Torvalds * Check for a SYN in window. 49901da177e4SLinus Torvalds */ 49911da177e4SLinus Torvalds if (th->syn && !before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) { 49921da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPABORTONSYN); 49931da177e4SLinus Torvalds tcp_reset(sk); 49941da177e4SLinus Torvalds return 1; 49951da177e4SLinus Torvalds } 49961da177e4SLinus Torvalds 49971da177e4SLinus Torvalds /* step 5: check the ACK field */ 49981da177e4SLinus Torvalds if (th->ack) { 49991da177e4SLinus Torvalds int acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH); 50001da177e4SLinus Torvalds 50011da177e4SLinus Torvalds switch (sk->sk_state) { 50021da177e4SLinus Torvalds case TCP_SYN_RECV: 50031da177e4SLinus Torvalds if (acceptable) { 50041da177e4SLinus Torvalds tp->copied_seq = tp->rcv_nxt; 5005e16aa207SRalf Baechle smp_mb(); 50061da177e4SLinus Torvalds tcp_set_state(sk, TCP_ESTABLISHED); 50071da177e4SLinus Torvalds sk->sk_state_change(sk); 50081da177e4SLinus Torvalds 50091da177e4SLinus Torvalds /* Note, that this wakeup is only for marginal 50101da177e4SLinus Torvalds * crossed SYN case. Passively open sockets 50111da177e4SLinus Torvalds * are not waked up, because sk->sk_sleep == 50121da177e4SLinus Torvalds * NULL and sk->sk_socket == NULL. 50131da177e4SLinus Torvalds */ 50141da177e4SLinus Torvalds if (sk->sk_socket) { 50151da177e4SLinus Torvalds sk_wake_async(sk,0,POLL_OUT); 50161da177e4SLinus Torvalds } 50171da177e4SLinus Torvalds 50181da177e4SLinus Torvalds tp->snd_una = TCP_SKB_CB(skb)->ack_seq; 50191da177e4SLinus Torvalds tp->snd_wnd = ntohs(th->window) << 50201da177e4SLinus Torvalds tp->rx_opt.snd_wscale; 50211da177e4SLinus Torvalds tcp_init_wl(tp, TCP_SKB_CB(skb)->ack_seq, 50221da177e4SLinus Torvalds TCP_SKB_CB(skb)->seq); 50231da177e4SLinus Torvalds 50241da177e4SLinus Torvalds /* tcp_ack considers this ACK as duplicate 50251da177e4SLinus Torvalds * and does not calculate rtt. 50261da177e4SLinus Torvalds * Fix it at least with timestamps. 50271da177e4SLinus Torvalds */ 50281da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr && 50291da177e4SLinus Torvalds !tp->srtt) 50302d2abbabSStephen Hemminger tcp_ack_saw_tstamp(sk, 0); 50311da177e4SLinus Torvalds 50321da177e4SLinus Torvalds if (tp->rx_opt.tstamp_ok) 50331da177e4SLinus Torvalds tp->advmss -= TCPOLEN_TSTAMP_ALIGNED; 50341da177e4SLinus Torvalds 50351da177e4SLinus Torvalds /* Make sure socket is routed, for 50361da177e4SLinus Torvalds * correct metrics. 50371da177e4SLinus Torvalds */ 50388292a17aSArnaldo Carvalho de Melo icsk->icsk_af_ops->rebuild_header(sk); 50391da177e4SLinus Torvalds 50401da177e4SLinus Torvalds tcp_init_metrics(sk); 50411da177e4SLinus Torvalds 50426687e988SArnaldo Carvalho de Melo tcp_init_congestion_control(sk); 5043317a76f9SStephen Hemminger 50441da177e4SLinus Torvalds /* Prevent spurious tcp_cwnd_restart() on 50451da177e4SLinus Torvalds * first data packet. 50461da177e4SLinus Torvalds */ 50471da177e4SLinus Torvalds tp->lsndtime = tcp_time_stamp; 50481da177e4SLinus Torvalds 50495d424d5aSJohn Heffner tcp_mtup_init(sk); 50501da177e4SLinus Torvalds tcp_initialize_rcv_mss(sk); 50511da177e4SLinus Torvalds tcp_init_buffer_space(sk); 50521da177e4SLinus Torvalds tcp_fast_path_on(tp); 50531da177e4SLinus Torvalds } else { 50541da177e4SLinus Torvalds return 1; 50551da177e4SLinus Torvalds } 50561da177e4SLinus Torvalds break; 50571da177e4SLinus Torvalds 50581da177e4SLinus Torvalds case TCP_FIN_WAIT1: 50591da177e4SLinus Torvalds if (tp->snd_una == tp->write_seq) { 50601da177e4SLinus Torvalds tcp_set_state(sk, TCP_FIN_WAIT2); 50611da177e4SLinus Torvalds sk->sk_shutdown |= SEND_SHUTDOWN; 50621da177e4SLinus Torvalds dst_confirm(sk->sk_dst_cache); 50631da177e4SLinus Torvalds 50641da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) 50651da177e4SLinus Torvalds /* Wake up lingering close() */ 50661da177e4SLinus Torvalds sk->sk_state_change(sk); 50671da177e4SLinus Torvalds else { 50681da177e4SLinus Torvalds int tmo; 50691da177e4SLinus Torvalds 50701da177e4SLinus Torvalds if (tp->linger2 < 0 || 50711da177e4SLinus Torvalds (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && 50721da177e4SLinus Torvalds after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) { 50731da177e4SLinus Torvalds tcp_done(sk); 50741da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPABORTONDATA); 50751da177e4SLinus Torvalds return 1; 50761da177e4SLinus Torvalds } 50771da177e4SLinus Torvalds 5078463c84b9SArnaldo Carvalho de Melo tmo = tcp_fin_time(sk); 50791da177e4SLinus Torvalds if (tmo > TCP_TIMEWAIT_LEN) { 5080463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN); 50811da177e4SLinus Torvalds } else if (th->fin || sock_owned_by_user(sk)) { 50821da177e4SLinus Torvalds /* Bad case. We could lose such FIN otherwise. 50831da177e4SLinus Torvalds * It is not a big problem, but it looks confusing 50841da177e4SLinus Torvalds * and not so rare event. We still can lose it now, 50851da177e4SLinus Torvalds * if it spins in bh_lock_sock(), but it is really 50861da177e4SLinus Torvalds * marginal case. 50871da177e4SLinus Torvalds */ 5088463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(sk, tmo); 50891da177e4SLinus Torvalds } else { 50901da177e4SLinus Torvalds tcp_time_wait(sk, TCP_FIN_WAIT2, tmo); 50911da177e4SLinus Torvalds goto discard; 50921da177e4SLinus Torvalds } 50931da177e4SLinus Torvalds } 50941da177e4SLinus Torvalds } 50951da177e4SLinus Torvalds break; 50961da177e4SLinus Torvalds 50971da177e4SLinus Torvalds case TCP_CLOSING: 50981da177e4SLinus Torvalds if (tp->snd_una == tp->write_seq) { 50991da177e4SLinus Torvalds tcp_time_wait(sk, TCP_TIME_WAIT, 0); 51001da177e4SLinus Torvalds goto discard; 51011da177e4SLinus Torvalds } 51021da177e4SLinus Torvalds break; 51031da177e4SLinus Torvalds 51041da177e4SLinus Torvalds case TCP_LAST_ACK: 51051da177e4SLinus Torvalds if (tp->snd_una == tp->write_seq) { 51061da177e4SLinus Torvalds tcp_update_metrics(sk); 51071da177e4SLinus Torvalds tcp_done(sk); 51081da177e4SLinus Torvalds goto discard; 51091da177e4SLinus Torvalds } 51101da177e4SLinus Torvalds break; 51111da177e4SLinus Torvalds } 51121da177e4SLinus Torvalds } else 51131da177e4SLinus Torvalds goto discard; 51141da177e4SLinus Torvalds 51151da177e4SLinus Torvalds /* step 6: check the URG bit */ 51161da177e4SLinus Torvalds tcp_urg(sk, skb, th); 51171da177e4SLinus Torvalds 51181da177e4SLinus Torvalds /* step 7: process the segment text */ 51191da177e4SLinus Torvalds switch (sk->sk_state) { 51201da177e4SLinus Torvalds case TCP_CLOSE_WAIT: 51211da177e4SLinus Torvalds case TCP_CLOSING: 51221da177e4SLinus Torvalds case TCP_LAST_ACK: 51231da177e4SLinus Torvalds if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) 51241da177e4SLinus Torvalds break; 51251da177e4SLinus Torvalds case TCP_FIN_WAIT1: 51261da177e4SLinus Torvalds case TCP_FIN_WAIT2: 51271da177e4SLinus Torvalds /* RFC 793 says to queue data in these states, 51281da177e4SLinus Torvalds * RFC 1122 says we MUST send a reset. 51291da177e4SLinus Torvalds * BSD 4.4 also does reset. 51301da177e4SLinus Torvalds */ 51311da177e4SLinus Torvalds if (sk->sk_shutdown & RCV_SHUTDOWN) { 51321da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq && 51331da177e4SLinus Torvalds after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) { 51341da177e4SLinus Torvalds NET_INC_STATS_BH(LINUX_MIB_TCPABORTONDATA); 51351da177e4SLinus Torvalds tcp_reset(sk); 51361da177e4SLinus Torvalds return 1; 51371da177e4SLinus Torvalds } 51381da177e4SLinus Torvalds } 51391da177e4SLinus Torvalds /* Fall through */ 51401da177e4SLinus Torvalds case TCP_ESTABLISHED: 51411da177e4SLinus Torvalds tcp_data_queue(sk, skb); 51421da177e4SLinus Torvalds queued = 1; 51431da177e4SLinus Torvalds break; 51441da177e4SLinus Torvalds } 51451da177e4SLinus Torvalds 51461da177e4SLinus Torvalds /* tcp_data could move socket to TIME-WAIT */ 51471da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE) { 51489e412ba7SIlpo Järvinen tcp_data_snd_check(sk); 51491da177e4SLinus Torvalds tcp_ack_snd_check(sk); 51501da177e4SLinus Torvalds } 51511da177e4SLinus Torvalds 51521da177e4SLinus Torvalds if (!queued) { 51531da177e4SLinus Torvalds discard: 51541da177e4SLinus Torvalds __kfree_skb(skb); 51551da177e4SLinus Torvalds } 51561da177e4SLinus Torvalds return 0; 51571da177e4SLinus Torvalds } 51581da177e4SLinus Torvalds 51591da177e4SLinus Torvalds EXPORT_SYMBOL(sysctl_tcp_ecn); 51601da177e4SLinus Torvalds EXPORT_SYMBOL(sysctl_tcp_reordering); 51611da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_parse_options); 51621da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_rcv_established); 51631da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_rcv_state_process); 516440efc6faSStephen Hemminger EXPORT_SYMBOL(tcp_initialize_rcv_mss); 5165