1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX
41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket
51da177e4SLinus Torvalds * interface as the means of communication with the user level.
61da177e4SLinus Torvalds *
71da177e4SLinus Torvalds * Implementation of the Transmission Control Protocol(TCP).
81da177e4SLinus Torvalds *
902c30a84SJesper Juhl * Authors: Ross Biro
101da177e4SLinus Torvalds * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
111da177e4SLinus Torvalds * Mark Evans, <evansmp@uhura.aston.ac.uk>
121da177e4SLinus Torvalds * Corey Minyard <wf-rch!minyard@relay.EU.net>
131da177e4SLinus Torvalds * Florian La Roche, <flla@stud.uni-sb.de>
141da177e4SLinus Torvalds * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
151da177e4SLinus Torvalds * Linus Torvalds, <torvalds@cs.helsinki.fi>
161da177e4SLinus Torvalds * Alan Cox, <gw4pts@gw4pts.ampr.org>
171da177e4SLinus Torvalds * Matthew Dillon, <dillon@apollo.west.oic.com>
181da177e4SLinus Torvalds * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
191da177e4SLinus Torvalds * Jorge Cwik, <jorge@laser.satlink.net>
201da177e4SLinus Torvalds */
211da177e4SLinus Torvalds
221da177e4SLinus Torvalds /*
231da177e4SLinus Torvalds * Changes:
241da177e4SLinus Torvalds * Pedro Roque : Fast Retransmit/Recovery.
251da177e4SLinus Torvalds * Two receive queues.
261da177e4SLinus Torvalds * Retransmit queue handled by TCP.
271da177e4SLinus Torvalds * Better retransmit timer handling.
281da177e4SLinus Torvalds * New congestion avoidance.
291da177e4SLinus Torvalds * Header prediction.
301da177e4SLinus Torvalds * Variable renaming.
311da177e4SLinus Torvalds *
321da177e4SLinus Torvalds * Eric : Fast Retransmit.
331da177e4SLinus Torvalds * Randy Scott : MSS option defines.
341da177e4SLinus Torvalds * Eric Schenk : Fixes to slow start algorithm.
351da177e4SLinus Torvalds * Eric Schenk : Yet another double ACK bug.
361da177e4SLinus Torvalds * Eric Schenk : Delayed ACK bug fixes.
371da177e4SLinus Torvalds * Eric Schenk : Floyd style fast retrans war avoidance.
381da177e4SLinus Torvalds * David S. Miller : Don't allow zero congestion window.
391da177e4SLinus Torvalds * Eric Schenk : Fix retransmitter so that it sends
401da177e4SLinus Torvalds * next packet on ack of previous packet.
411da177e4SLinus Torvalds * Andi Kleen : Moved open_request checking here
421da177e4SLinus Torvalds * and process RSTs for open_requests.
431da177e4SLinus Torvalds * Andi Kleen : Better prune_queue, and other fixes.
44caa20d9aSStephen Hemminger * Andrey Savochkin: Fix RTT measurements in the presence of
451da177e4SLinus Torvalds * timestamps.
461da177e4SLinus Torvalds * Andrey Savochkin: Check sequence numbers correctly when
471da177e4SLinus Torvalds * removing SACKs due to in sequence incoming
481da177e4SLinus Torvalds * data segments.
491da177e4SLinus Torvalds * Andi Kleen: Make sure we never ack data there is not
501da177e4SLinus Torvalds * enough room for. Also make this condition
511da177e4SLinus Torvalds * a fatal error if it might still happen.
521da177e4SLinus Torvalds * Andi Kleen: Add tcp_measure_rcv_mss to make
531da177e4SLinus Torvalds * connections with MSS<min(MTU,ann. MSS)
541da177e4SLinus Torvalds * work without delayed acks.
551da177e4SLinus Torvalds * Andi Kleen: Process packets with PSH set in the
561da177e4SLinus Torvalds * fast path.
571da177e4SLinus Torvalds * J Hadi Salim: ECN support
581da177e4SLinus Torvalds * Andrei Gurtov,
591da177e4SLinus Torvalds * Pasi Sarolahti,
601da177e4SLinus Torvalds * Panu Kuhlberg: Experimental audit of TCP (re)transmission
611da177e4SLinus Torvalds * engine. Lots of bugs are found.
621da177e4SLinus Torvalds * Pasi Sarolahti: F-RTO for dealing with spurious RTOs
631da177e4SLinus Torvalds */
641da177e4SLinus Torvalds
65afd46503SJoe Perches #define pr_fmt(fmt) "TCP: " fmt
66afd46503SJoe Perches
671da177e4SLinus Torvalds #include <linux/mm.h>
685a0e3ad6STejun Heo #include <linux/slab.h>
691da177e4SLinus Torvalds #include <linux/module.h>
701da177e4SLinus Torvalds #include <linux/sysctl.h>
71a0bffffcSIlpo Järvinen #include <linux/kernel.h>
72ad971f61SEric Dumazet #include <linux/prefetch.h>
735ffc02a1SSatoru SATOH #include <net/dst.h>
741da177e4SLinus Torvalds #include <net/tcp.h>
75f3d93817SEric Dumazet #include <net/proto_memory.h>
761da177e4SLinus Torvalds #include <net/inet_common.h>
771da177e4SLinus Torvalds #include <linux/ipsec.h>
785f60d5f6SAl Viro #include <linux/unaligned.h>
79e1c8a607SWillem de Bruijn #include <linux/errqueue.h>
805941521cSSong Liu #include <trace/events/tcp.h>
81494bc1d2SJakub Kicinski #include <linux/jump_label_ratelimit.h>
82c6345ce7SAmritha Nambiar #include <net/busy_poll.h>
83eda7acddSPeter Krystad #include <net/mptcp.h>
841da177e4SLinus Torvalds
85ab32ea5dSBrian Haley int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
861da177e4SLinus Torvalds
871da177e4SLinus Torvalds #define FLAG_DATA 0x01 /* Incoming frame contained data. */
881da177e4SLinus Torvalds #define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */
891da177e4SLinus Torvalds #define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */
901da177e4SLinus Torvalds #define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */
911da177e4SLinus Torvalds #define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */
921da177e4SLinus Torvalds #define FLAG_DATA_SACKED 0x20 /* New SACK. */
931da177e4SLinus Torvalds #define FLAG_ECE 0x40 /* ECE in this ACK */
94291a00d1SYuchung Cheng #define FLAG_LOST_RETRANS 0x80 /* This ACK marks some retransmission lost */
9531770e34SFlorian Westphal #define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/
96e33099f9SYuchung Cheng #define FLAG_ORIG_SACK_ACKED 0x200 /* Never retransmitted data are (s)acked */
972e605294SIlpo Järvinen #define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
98564262c1SRyousei Takano #define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
99df92c839SNeal Cardwell #define FLAG_SET_XMIT_TIMER 0x1000 /* Set TLP or RTO timer */
100cadbd031SIlpo Järvinen #define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
10112fb3dd9SEric Dumazet #define FLAG_UPDATE_TS_RECENT 0x4000 /* tcp_replace_ts_recent() */
102d0e1a1b5SEric Dumazet #define FLAG_NO_CHALLENGE_ACK 0x8000 /* do not call tcp_send_challenge_ack() */
103eb36be0fSYuchung Cheng #define FLAG_ACK_MAYBE_DELAYED 0x10000 /* Likely a delayed ACK */
10463f367d9SYuchung Cheng #define FLAG_DSACK_TLP 0x20000 /* DSACK for tail loss probe */
1051da177e4SLinus Torvalds
1061da177e4SLinus Torvalds #define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
1071da177e4SLinus Torvalds #define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
108d09b9e60SPriyaranjan Jha #define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE|FLAG_DSACKING_ACK)
1091da177e4SLinus Torvalds #define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
1101da177e4SLinus Torvalds
1111da177e4SLinus Torvalds #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
112bdf1ee5dSIlpo Järvinen #define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
1131da177e4SLinus Torvalds
114e662ca40SYuchung Cheng #define REXMIT_NONE 0 /* no loss recovery to do */
115e662ca40SYuchung Cheng #define REXMIT_LOST 1 /* retransmit packets marked lost */
116e662ca40SYuchung Cheng #define REXMIT_NEW 2 /* FRTO-style transmit of unsent/new packets */
117e662ca40SYuchung Cheng
1186dac1523SIlya Lesokhin #if IS_ENABLED(CONFIG_TLS_DEVICE)
119494bc1d2SJakub Kicinski static DEFINE_STATIC_KEY_DEFERRED_FALSE(clean_acked_data_enabled, HZ);
1206dac1523SIlya Lesokhin
clean_acked_data_enable(struct inet_connection_sock * icsk,void (* cad)(struct sock * sk,u32 ack_seq))1216dac1523SIlya Lesokhin void clean_acked_data_enable(struct inet_connection_sock *icsk,
1226dac1523SIlya Lesokhin void (*cad)(struct sock *sk, u32 ack_seq))
1236dac1523SIlya Lesokhin {
1246dac1523SIlya Lesokhin icsk->icsk_clean_acked = cad;
1257b58139fSWillem de Bruijn static_branch_deferred_inc(&clean_acked_data_enabled);
1266dac1523SIlya Lesokhin }
1276dac1523SIlya Lesokhin EXPORT_SYMBOL_GPL(clean_acked_data_enable);
1286dac1523SIlya Lesokhin
clean_acked_data_disable(struct inet_connection_sock * icsk)1296dac1523SIlya Lesokhin void clean_acked_data_disable(struct inet_connection_sock *icsk)
1306dac1523SIlya Lesokhin {
131494bc1d2SJakub Kicinski static_branch_slow_dec_deferred(&clean_acked_data_enabled);
1326dac1523SIlya Lesokhin icsk->icsk_clean_acked = NULL;
1336dac1523SIlya Lesokhin }
1346dac1523SIlya Lesokhin EXPORT_SYMBOL_GPL(clean_acked_data_disable);
135494bc1d2SJakub Kicinski
clean_acked_data_flush(void)136494bc1d2SJakub Kicinski void clean_acked_data_flush(void)
137494bc1d2SJakub Kicinski {
138494bc1d2SJakub Kicinski static_key_deferred_flush(&clean_acked_data_enabled);
139494bc1d2SJakub Kicinski }
140494bc1d2SJakub Kicinski EXPORT_SYMBOL_GPL(clean_acked_data_flush);
1416dac1523SIlya Lesokhin #endif
1426dac1523SIlya Lesokhin
14372be0fe6SMartin KaFai Lau #ifdef CONFIG_CGROUP_BPF
bpf_skops_parse_hdr(struct sock * sk,struct sk_buff * skb)14400d211a4SMartin KaFai Lau static void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb)
14500d211a4SMartin KaFai Lau {
14600d211a4SMartin KaFai Lau bool unknown_opt = tcp_sk(sk)->rx_opt.saw_unknown &&
14700d211a4SMartin KaFai Lau BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk),
14800d211a4SMartin KaFai Lau BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG);
14900d211a4SMartin KaFai Lau bool parse_all_opt = BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk),
15000d211a4SMartin KaFai Lau BPF_SOCK_OPS_PARSE_ALL_HDR_OPT_CB_FLAG);
1510813a841SMartin KaFai Lau struct bpf_sock_ops_kern sock_ops;
15200d211a4SMartin KaFai Lau
15300d211a4SMartin KaFai Lau if (likely(!unknown_opt && !parse_all_opt))
15400d211a4SMartin KaFai Lau return;
15500d211a4SMartin KaFai Lau
15600d211a4SMartin KaFai Lau /* The skb will be handled in the
15700d211a4SMartin KaFai Lau * bpf_skops_established() or
15800d211a4SMartin KaFai Lau * bpf_skops_write_hdr_opt().
15900d211a4SMartin KaFai Lau */
16000d211a4SMartin KaFai Lau switch (sk->sk_state) {
16100d211a4SMartin KaFai Lau case TCP_SYN_RECV:
16200d211a4SMartin KaFai Lau case TCP_SYN_SENT:
16300d211a4SMartin KaFai Lau case TCP_LISTEN:
16400d211a4SMartin KaFai Lau return;
16500d211a4SMartin KaFai Lau }
16600d211a4SMartin KaFai Lau
1670813a841SMartin KaFai Lau sock_owned_by_me(sk);
1680813a841SMartin KaFai Lau
1690813a841SMartin KaFai Lau memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
1700813a841SMartin KaFai Lau sock_ops.op = BPF_SOCK_OPS_PARSE_HDR_OPT_CB;
1710813a841SMartin KaFai Lau sock_ops.is_fullsock = 1;
1720813a841SMartin KaFai Lau sock_ops.sk = sk;
1730813a841SMartin KaFai Lau bpf_skops_init_skb(&sock_ops, skb, tcp_hdrlen(skb));
1740813a841SMartin KaFai Lau
1750813a841SMartin KaFai Lau BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
17600d211a4SMartin KaFai Lau }
17700d211a4SMartin KaFai Lau
bpf_skops_established(struct sock * sk,int bpf_op,struct sk_buff * skb)17872be0fe6SMartin KaFai Lau static void bpf_skops_established(struct sock *sk, int bpf_op,
17972be0fe6SMartin KaFai Lau struct sk_buff *skb)
18072be0fe6SMartin KaFai Lau {
18172be0fe6SMartin KaFai Lau struct bpf_sock_ops_kern sock_ops;
18272be0fe6SMartin KaFai Lau
18372be0fe6SMartin KaFai Lau sock_owned_by_me(sk);
18472be0fe6SMartin KaFai Lau
18572be0fe6SMartin KaFai Lau memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
18672be0fe6SMartin KaFai Lau sock_ops.op = bpf_op;
18772be0fe6SMartin KaFai Lau sock_ops.is_fullsock = 1;
18872be0fe6SMartin KaFai Lau sock_ops.sk = sk;
1890813a841SMartin KaFai Lau /* sk with TCP_REPAIR_ON does not have skb in tcp_finish_connect */
1900813a841SMartin KaFai Lau if (skb)
1910813a841SMartin KaFai Lau bpf_skops_init_skb(&sock_ops, skb, tcp_hdrlen(skb));
19272be0fe6SMartin KaFai Lau
19372be0fe6SMartin KaFai Lau BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
19472be0fe6SMartin KaFai Lau }
19572be0fe6SMartin KaFai Lau #else
bpf_skops_parse_hdr(struct sock * sk,struct sk_buff * skb)19600d211a4SMartin KaFai Lau static void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb)
19700d211a4SMartin KaFai Lau {
19800d211a4SMartin KaFai Lau }
19900d211a4SMartin KaFai Lau
bpf_skops_established(struct sock * sk,int bpf_op,struct sk_buff * skb)20072be0fe6SMartin KaFai Lau static void bpf_skops_established(struct sock *sk, int bpf_op,
20172be0fe6SMartin KaFai Lau struct sk_buff *skb)
20272be0fe6SMartin KaFai Lau {
20372be0fe6SMartin KaFai Lau }
20472be0fe6SMartin KaFai Lau #endif
20572be0fe6SMartin KaFai Lau
tcp_gro_dev_warn(const struct sock * sk,const struct sk_buff * skb,unsigned int len)206b32e8fbeSEric Dumazet static __cold void tcp_gro_dev_warn(const struct sock *sk, const struct sk_buff *skb,
2070b9aefeaSMarcelo Ricardo Leitner unsigned int len)
208dcb17d22SMarcelo Ricardo Leitner {
209dcb17d22SMarcelo Ricardo Leitner struct net_device *dev;
210dcb17d22SMarcelo Ricardo Leitner
211dcb17d22SMarcelo Ricardo Leitner rcu_read_lock();
212dcb17d22SMarcelo Ricardo Leitner dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif);
213b32e8fbeSEric Dumazet if (!dev || len >= READ_ONCE(dev->mtu))
214dcb17d22SMarcelo Ricardo Leitner pr_warn("%s: Driver has suspect GRO implementation, TCP performance may be compromised.\n",
215dcb17d22SMarcelo Ricardo Leitner dev ? dev->name : "Unknown driver");
216dcb17d22SMarcelo Ricardo Leitner rcu_read_unlock();
217dcb17d22SMarcelo Ricardo Leitner }
218dcb17d22SMarcelo Ricardo Leitner
2191da177e4SLinus Torvalds /* Adapt the MSS value used to make delayed ack decision to the
2201da177e4SLinus Torvalds * real world.
2211da177e4SLinus Torvalds */
tcp_measure_rcv_mss(struct sock * sk,const struct sk_buff * skb)222056834d9SIlpo Järvinen static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
2231da177e4SLinus Torvalds {
224463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
225463c84b9SArnaldo Carvalho de Melo const unsigned int lss = icsk->icsk_ack.last_seg_size;
226463c84b9SArnaldo Carvalho de Melo unsigned int len;
2271da177e4SLinus Torvalds
228463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.last_seg_size = 0;
2291da177e4SLinus Torvalds
2301da177e4SLinus Torvalds /* skb->len may jitter because of SACKs, even if peer
2311da177e4SLinus Torvalds * sends good full-sized frames.
2321da177e4SLinus Torvalds */
233ff9b5e0fSHerbert Xu len = skb_shinfo(skb)->gso_size ? : skb->len;
234463c84b9SArnaldo Carvalho de Melo if (len >= icsk->icsk_ack.rcv_mss) {
235dfa2f048SEric Dumazet /* Note: divides are still a bit expensive.
236dfa2f048SEric Dumazet * For the moment, only adjust scaling_ratio
237dfa2f048SEric Dumazet * when we update icsk_ack.rcv_mss.
238dfa2f048SEric Dumazet */
239dfa2f048SEric Dumazet if (unlikely(len != icsk->icsk_ack.rcv_mss)) {
240dfa2f048SEric Dumazet u64 val = (u64)skb->len << TCP_RMEM_TO_WIN_SCALE;
241a2cbb160SSubash Abhinov Kasiviswanathan u8 old_ratio = tcp_sk(sk)->scaling_ratio;
242dfa2f048SEric Dumazet
243dfa2f048SEric Dumazet do_div(val, skb->truesize);
244dfa2f048SEric Dumazet tcp_sk(sk)->scaling_ratio = val ? val : 1;
245a2cbb160SSubash Abhinov Kasiviswanathan
246a2cbb160SSubash Abhinov Kasiviswanathan if (old_ratio != tcp_sk(sk)->scaling_ratio)
247a2cbb160SSubash Abhinov Kasiviswanathan WRITE_ONCE(tcp_sk(sk)->window_clamp,
248a2cbb160SSubash Abhinov Kasiviswanathan tcp_win_from_space(sk, sk->sk_rcvbuf));
249dfa2f048SEric Dumazet }
250dcb17d22SMarcelo Ricardo Leitner icsk->icsk_ack.rcv_mss = min_t(unsigned int, len,
251dcb17d22SMarcelo Ricardo Leitner tcp_sk(sk)->advmss);
2520b9aefeaSMarcelo Ricardo Leitner /* Account for possibly-removed options */
253b32e8fbeSEric Dumazet DO_ONCE_LITE_IF(len > icsk->icsk_ack.rcv_mss + MAX_TCP_OPTION_SPACE,
254b32e8fbeSEric Dumazet tcp_gro_dev_warn, sk, skb, len);
2554720852eSNeal Cardwell /* If the skb has a len of exactly 1*MSS and has the PSH bit
2564720852eSNeal Cardwell * set then it is likely the end of an application write. So
2574720852eSNeal Cardwell * more data may not be arriving soon, and yet the data sender
2584720852eSNeal Cardwell * may be waiting for an ACK if cwnd-bound or using TX zero
2594720852eSNeal Cardwell * copy. So we set ICSK_ACK_PUSHED here so that
2604720852eSNeal Cardwell * tcp_cleanup_rbuf() will send an ACK immediately if the app
2614720852eSNeal Cardwell * reads all of the data and is not ping-pong. If len > MSS
2624720852eSNeal Cardwell * then this logic does not matter (and does not hurt) because
2634720852eSNeal Cardwell * tcp_cleanup_rbuf() will always ACK immediately if the app
2644720852eSNeal Cardwell * reads data and there is more than an MSS of unACKed data.
2654720852eSNeal Cardwell */
2664720852eSNeal Cardwell if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_PSH)
2674720852eSNeal Cardwell icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
2681da177e4SLinus Torvalds } else {
2691da177e4SLinus Torvalds /* Otherwise, we make more careful check taking into account,
2701da177e4SLinus Torvalds * that SACKs block is variable.
2711da177e4SLinus Torvalds *
2721da177e4SLinus Torvalds * "len" is invariant segment length, including TCP header.
2731da177e4SLinus Torvalds */
2749c70220bSArnaldo Carvalho de Melo len += skb->data - skb_transport_header(skb);
275bee7ca9eSWilliam Allen Simpson if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
2761da177e4SLinus Torvalds /* If PSH is not set, packet should be
2771da177e4SLinus Torvalds * full sized, provided peer TCP is not badly broken.
2781da177e4SLinus Torvalds * This observation (if it is correct 8)) allows
2791da177e4SLinus Torvalds * to handle super-low mtu links fairly.
2801da177e4SLinus Torvalds */
2811da177e4SLinus Torvalds (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
282aa8223c7SArnaldo Carvalho de Melo !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
2831da177e4SLinus Torvalds /* Subtract also invariant (if peer is RFC compliant),
2841da177e4SLinus Torvalds * tcp header plus fixed timestamp option length.
2851da177e4SLinus Torvalds * Resulting "len" is MSS free of SACK jitter.
2861da177e4SLinus Torvalds */
287463c84b9SArnaldo Carvalho de Melo len -= tcp_sk(sk)->tcp_header_len;
288463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.last_seg_size = len;
2891da177e4SLinus Torvalds if (len == lss) {
290463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.rcv_mss = len;
2911da177e4SLinus Torvalds return;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds }
2941ef9696cSAlexey Kuznetsov if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
2951ef9696cSAlexey Kuznetsov icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
296463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds
tcp_incr_quickack(struct sock * sk,unsigned int max_quickacks)3009a9c9b51SEric Dumazet static void tcp_incr_quickack(struct sock *sk, unsigned int max_quickacks)
3011da177e4SLinus Torvalds {
302463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
30395c96174SEric Dumazet unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
3041da177e4SLinus Torvalds
3051da177e4SLinus Torvalds if (quickacks == 0)
3061da177e4SLinus Torvalds quickacks = 2;
3079a9c9b51SEric Dumazet quickacks = min(quickacks, max_quickacks);
308463c84b9SArnaldo Carvalho de Melo if (quickacks > icsk->icsk_ack.quick)
3099a9c9b51SEric Dumazet icsk->icsk_ack.quick = quickacks;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds
tcp_enter_quickack_mode(struct sock * sk,unsigned int max_quickacks)31203b123deSEric Dumazet static void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks)
3131da177e4SLinus Torvalds {
314463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
3159a9c9b51SEric Dumazet
3169a9c9b51SEric Dumazet tcp_incr_quickack(sk, max_quickacks);
31731954cd8SWei Wang inet_csk_exit_pingpong_mode(sk);
318463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN;
3191da177e4SLinus Torvalds }
3201da177e4SLinus Torvalds
3211da177e4SLinus Torvalds /* Send ACKs quickly, if "quick" count is not exhausted
3221da177e4SLinus Torvalds * and the session is not interactive.
3231da177e4SLinus Torvalds */
3241da177e4SLinus Torvalds
tcp_in_quickack_mode(struct sock * sk)3252251ae46SJon Maxwell static bool tcp_in_quickack_mode(struct sock *sk)
3261da177e4SLinus Torvalds {
327463c84b9SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk);
3282251ae46SJon Maxwell const struct dst_entry *dst = __sk_dst_get(sk);
329a2a385d6SEric Dumazet
3302251ae46SJon Maxwell return (dst && dst_metric(dst, RTAX_QUICKACK)) ||
33131954cd8SWei Wang (icsk->icsk_ack.quick && !inet_csk_in_pingpong_mode(sk));
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds
tcp_ecn_queue_cwr(struct tcp_sock * tp)334735d3831SFlorian Westphal static void tcp_ecn_queue_cwr(struct tcp_sock *tp)
335bdf1ee5dSIlpo Järvinen {
336bdf1ee5dSIlpo Järvinen if (tp->ecn_flags & TCP_ECN_OK)
337bdf1ee5dSIlpo Järvinen tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
338bdf1ee5dSIlpo Järvinen }
339bdf1ee5dSIlpo Järvinen
tcp_ecn_accept_cwr(struct sock * sk,const struct sk_buff * skb)340fd2123a3SYuchung Cheng static void tcp_ecn_accept_cwr(struct sock *sk, const struct sk_buff *skb)
341bdf1ee5dSIlpo Järvinen {
3429aee4000SLawrence Brakmo if (tcp_hdr(skb)->cwr) {
343fd2123a3SYuchung Cheng tcp_sk(sk)->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
3449aee4000SLawrence Brakmo
3459aee4000SLawrence Brakmo /* If the sender is telling us it has entered CWR, then its
3469aee4000SLawrence Brakmo * cwnd may be very low (even just 1 packet), so we should ACK
3479aee4000SLawrence Brakmo * immediately.
3489aee4000SLawrence Brakmo */
34925702840SDenis Kirjanov if (TCP_SKB_CB(skb)->seq != TCP_SKB_CB(skb)->end_seq)
350fd2123a3SYuchung Cheng inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW;
3519aee4000SLawrence Brakmo }
352bdf1ee5dSIlpo Järvinen }
353bdf1ee5dSIlpo Järvinen
tcp_ecn_withdraw_cwr(struct tcp_sock * tp)354735d3831SFlorian Westphal static void tcp_ecn_withdraw_cwr(struct tcp_sock *tp)
355bdf1ee5dSIlpo Järvinen {
356af38d07eSNeal Cardwell tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR;
357bdf1ee5dSIlpo Järvinen }
358bdf1ee5dSIlpo Järvinen
__tcp_ecn_check_ce(struct sock * sk,const struct sk_buff * skb)359f4c9f85fSYousuk Seung static void __tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb)
360bdf1ee5dSIlpo Järvinen {
361f4c9f85fSYousuk Seung struct tcp_sock *tp = tcp_sk(sk);
362f4c9f85fSYousuk Seung
363b82d1bb4SEric Dumazet switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
3647a269ffaSEric Dumazet case INET_ECN_NOT_ECT:
365bdf1ee5dSIlpo Järvinen /* Funny extension: if ECT is not set on a segment,
3667a269ffaSEric Dumazet * and we already seen ECT on a previous segment,
3677a269ffaSEric Dumazet * it is probably a retransmit.
3687a269ffaSEric Dumazet */
3697a269ffaSEric Dumazet if (tp->ecn_flags & TCP_ECN_SEEN)
37015ecbe94SEric Dumazet tcp_enter_quickack_mode(sk, 2);
3717a269ffaSEric Dumazet break;
3727a269ffaSEric Dumazet case INET_ECN_CE:
373f4c9f85fSYousuk Seung if (tcp_ca_needs_ecn(sk))
374f4c9f85fSYousuk Seung tcp_ca_event(sk, CA_EVENT_ECN_IS_CE);
3759890092eSFlorian Westphal
376aae06bf5SEric Dumazet if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR)) {
377aae06bf5SEric Dumazet /* Better not delay acks, sender can have a very low cwnd */
37815ecbe94SEric Dumazet tcp_enter_quickack_mode(sk, 2);
3797a269ffaSEric Dumazet tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
380aae06bf5SEric Dumazet }
3817a269ffaSEric Dumazet tp->ecn_flags |= TCP_ECN_SEEN;
3829890092eSFlorian Westphal break;
3839890092eSFlorian Westphal default:
384f4c9f85fSYousuk Seung if (tcp_ca_needs_ecn(sk))
385f4c9f85fSYousuk Seung tcp_ca_event(sk, CA_EVENT_ECN_NO_CE);
3869890092eSFlorian Westphal tp->ecn_flags |= TCP_ECN_SEEN;
3879890092eSFlorian Westphal break;
388bdf1ee5dSIlpo Järvinen }
389bdf1ee5dSIlpo Järvinen }
390bdf1ee5dSIlpo Järvinen
tcp_ecn_check_ce(struct sock * sk,const struct sk_buff * skb)391f4c9f85fSYousuk Seung static void tcp_ecn_check_ce(struct sock *sk, const struct sk_buff *skb)
392735d3831SFlorian Westphal {
393f4c9f85fSYousuk Seung if (tcp_sk(sk)->ecn_flags & TCP_ECN_OK)
394f4c9f85fSYousuk Seung __tcp_ecn_check_ce(sk, skb);
395735d3831SFlorian Westphal }
396735d3831SFlorian Westphal
tcp_ecn_rcv_synack(struct tcp_sock * tp,const struct tcphdr * th)397735d3831SFlorian Westphal static void tcp_ecn_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)
398bdf1ee5dSIlpo Järvinen {
399bdf1ee5dSIlpo Järvinen if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
400bdf1ee5dSIlpo Järvinen tp->ecn_flags &= ~TCP_ECN_OK;
401bdf1ee5dSIlpo Järvinen }
402bdf1ee5dSIlpo Järvinen
tcp_ecn_rcv_syn(struct tcp_sock * tp,const struct tcphdr * th)403735d3831SFlorian Westphal static void tcp_ecn_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)
404bdf1ee5dSIlpo Järvinen {
405bdf1ee5dSIlpo Järvinen if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
406bdf1ee5dSIlpo Järvinen tp->ecn_flags &= ~TCP_ECN_OK;
407bdf1ee5dSIlpo Järvinen }
408bdf1ee5dSIlpo Järvinen
tcp_ecn_rcv_ecn_echo(const struct tcp_sock * tp,const struct tcphdr * th)409735d3831SFlorian Westphal static bool tcp_ecn_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)
410bdf1ee5dSIlpo Järvinen {
411bdf1ee5dSIlpo Järvinen if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
412a2a385d6SEric Dumazet return true;
413a2a385d6SEric Dumazet return false;
414bdf1ee5dSIlpo Järvinen }
415bdf1ee5dSIlpo Järvinen
4161da177e4SLinus Torvalds /* Buffer size and advertised window tuning.
4171da177e4SLinus Torvalds *
4181da177e4SLinus Torvalds * 1. Tuning sk->sk_sndbuf, when connection enters established state.
4191da177e4SLinus Torvalds */
4201da177e4SLinus Torvalds
tcp_sndbuf_expand(struct sock * sk)4216ae70532SEric Dumazet static void tcp_sndbuf_expand(struct sock *sk)
4221da177e4SLinus Torvalds {
4236ae70532SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk);
42477bfc174SYuchung Cheng const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
4256ae70532SEric Dumazet int sndmem, per_mss;
4266ae70532SEric Dumazet u32 nr_segs;
4271da177e4SLinus Torvalds
4286ae70532SEric Dumazet /* Worst case is non GSO/TSO : each frame consumes one skb
4296ae70532SEric Dumazet * and skb->head is kmalloced using power of two area of memory
4306ae70532SEric Dumazet */
4316ae70532SEric Dumazet per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) +
4326ae70532SEric Dumazet MAX_TCP_HEADER +
4336ae70532SEric Dumazet SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4346ae70532SEric Dumazet
4356ae70532SEric Dumazet per_mss = roundup_pow_of_two(per_mss) +
4366ae70532SEric Dumazet SKB_DATA_ALIGN(sizeof(struct sk_buff));
4376ae70532SEric Dumazet
43840570375SEric Dumazet nr_segs = max_t(u32, TCP_INIT_CWND, tcp_snd_cwnd(tp));
4396ae70532SEric Dumazet nr_segs = max_t(u32, nr_segs, tp->reordering + 1);
4406ae70532SEric Dumazet
4416ae70532SEric Dumazet /* Fast Recovery (RFC 5681 3.2) :
4426ae70532SEric Dumazet * Cubic needs 1.7 factor, rounded to 2 to include
443a9a08845SLinus Torvalds * extra cushion (application might react slowly to EPOLLOUT)
4446ae70532SEric Dumazet */
44577bfc174SYuchung Cheng sndmem = ca_ops->sndbuf_expand ? ca_ops->sndbuf_expand(sk) : 2;
44677bfc174SYuchung Cheng sndmem *= nr_segs * per_mss;
4476ae70532SEric Dumazet
44806a59ecbSEric Dumazet if (sk->sk_sndbuf < sndmem)
449e292f05eSEric Dumazet WRITE_ONCE(sk->sk_sndbuf,
45002739545SKuniyuki Iwashima min(sndmem, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_wmem[2])));
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds
4531da177e4SLinus Torvalds /* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
4541da177e4SLinus Torvalds *
4551da177e4SLinus Torvalds * All tcp_full_space() is split to two parts: "network" buffer, allocated
4561da177e4SLinus Torvalds * forward and advertised in receiver window (tp->rcv_wnd) and
4571da177e4SLinus Torvalds * "application buffer", required to isolate scheduling/application
4581da177e4SLinus Torvalds * latencies from network.
4591da177e4SLinus Torvalds * window_clamp is maximal advertised window. It can be less than
4601da177e4SLinus Torvalds * tcp_full_space(), in this case tcp_full_space() - window_clamp
4611da177e4SLinus Torvalds * is reserved for "application" buffer. The less window_clamp is
4621da177e4SLinus Torvalds * the smoother our behaviour from viewpoint of network, but the lower
4631da177e4SLinus Torvalds * throughput and the higher sensitivity of the connection to losses. 8)
4641da177e4SLinus Torvalds *
4651da177e4SLinus Torvalds * rcv_ssthresh is more strict window_clamp used at "slow start"
4661da177e4SLinus Torvalds * phase to predict further behaviour of this connection.
4671da177e4SLinus Torvalds * It is used for two goals:
4681da177e4SLinus Torvalds * - to enforce header prediction at sender, even when application
4691da177e4SLinus Torvalds * requires some significant "application buffer". It is check #1.
4701da177e4SLinus Torvalds * - to prevent pruning of receive queue because of misprediction
4711da177e4SLinus Torvalds * of receiver window. Check #2.
4721da177e4SLinus Torvalds *
4731da177e4SLinus Torvalds * The scheme does not work when sender sends good segments opening
474caa20d9aSStephen Hemminger * window and then starts to feed us spaghetti. But it should work
4751da177e4SLinus Torvalds * in common situations. Otherwise, we have to rely on queue collapsing.
4761da177e4SLinus Torvalds */
4771da177e4SLinus Torvalds
4781da177e4SLinus Torvalds /* Slow part of check#2. */
__tcp_grow_window(const struct sock * sk,const struct sk_buff * skb,unsigned int skbtruesize)479240bfd13SEric Dumazet static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb,
480240bfd13SEric Dumazet unsigned int skbtruesize)
4811da177e4SLinus Torvalds {
482e9d9da91SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk);
4831da177e4SLinus Torvalds /* Optimize this! */
484240bfd13SEric Dumazet int truesize = tcp_win_from_space(sk, skbtruesize) >> 1;
48502739545SKuniyuki Iwashima int window = tcp_win_from_space(sk, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])) >> 1;
4861da177e4SLinus Torvalds
4871da177e4SLinus Torvalds while (tp->rcv_ssthresh <= window) {
4881da177e4SLinus Torvalds if (truesize <= skb->len)
489463c84b9SArnaldo Carvalho de Melo return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
4901da177e4SLinus Torvalds
4911da177e4SLinus Torvalds truesize >>= 1;
4921da177e4SLinus Torvalds window >>= 1;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds return 0;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds
497240bfd13SEric Dumazet /* Even if skb appears to have a bad len/truesize ratio, TCP coalescing
498240bfd13SEric Dumazet * can play nice with us, as sk_buff and skb->head might be either
499240bfd13SEric Dumazet * freed or shared with up to MAX_SKB_FRAGS segments.
500240bfd13SEric Dumazet * Only give a boost to drivers using page frag(s) to hold the frame(s),
501240bfd13SEric Dumazet * and if no payload was pulled in skb->head before reaching us.
502240bfd13SEric Dumazet */
truesize_adjust(bool adjust,const struct sk_buff * skb)503240bfd13SEric Dumazet static u32 truesize_adjust(bool adjust, const struct sk_buff *skb)
504240bfd13SEric Dumazet {
505240bfd13SEric Dumazet u32 truesize = skb->truesize;
506240bfd13SEric Dumazet
507240bfd13SEric Dumazet if (adjust && !skb_headlen(skb)) {
508240bfd13SEric Dumazet truesize -= SKB_TRUESIZE(skb_end_offset(skb));
509240bfd13SEric Dumazet /* paranoid check, some drivers might be buggy */
510240bfd13SEric Dumazet if (unlikely((int)truesize < (int)skb->len))
511240bfd13SEric Dumazet truesize = skb->truesize;
512240bfd13SEric Dumazet }
513240bfd13SEric Dumazet return truesize;
514240bfd13SEric Dumazet }
515240bfd13SEric Dumazet
tcp_grow_window(struct sock * sk,const struct sk_buff * skb,bool adjust)516240bfd13SEric Dumazet static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb,
517240bfd13SEric Dumazet bool adjust)
5181da177e4SLinus Torvalds {
5199e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
52050ce163aSEric Dumazet int room;
52150ce163aSEric Dumazet
52250ce163aSEric Dumazet room = min_t(int, tp->window_clamp, tcp_space(sk)) - tp->rcv_ssthresh;
5239e412ba7SIlpo Järvinen
524053f3684SWei Wang if (room <= 0)
525053f3684SWei Wang return;
526053f3684SWei Wang
5271da177e4SLinus Torvalds /* Check #1 */
528053f3684SWei Wang if (!tcp_under_memory_pressure(sk)) {
529240bfd13SEric Dumazet unsigned int truesize = truesize_adjust(adjust, skb);
5301da177e4SLinus Torvalds int incr;
5311da177e4SLinus Torvalds
5321da177e4SLinus Torvalds /* Check #2. Increase window, if skb with such overhead
5331da177e4SLinus Torvalds * will fit to rcvbuf in future.
5341da177e4SLinus Torvalds */
535240bfd13SEric Dumazet if (tcp_win_from_space(sk, truesize) <= skb->len)
5361da177e4SLinus Torvalds incr = 2 * tp->advmss;
5371da177e4SLinus Torvalds else
538240bfd13SEric Dumazet incr = __tcp_grow_window(sk, skb, truesize);
5391da177e4SLinus Torvalds
5401da177e4SLinus Torvalds if (incr) {
5414d846f02SEric Dumazet incr = max_t(int, incr, 2 * skb->len);
54250ce163aSEric Dumazet tp->rcv_ssthresh += min(room, incr);
543463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_ack.quick |= 1;
5441da177e4SLinus Torvalds }
545053f3684SWei Wang } else {
546053f3684SWei Wang /* Under pressure:
547053f3684SWei Wang * Adjust rcv_ssthresh according to reserved mem
548053f3684SWei Wang */
549053f3684SWei Wang tcp_adjust_rcv_ssthresh(sk);
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds
553a337531bSYuchung Cheng /* 3. Try to fixup all. It is made immediately after connection enters
5541da177e4SLinus Torvalds * established state.
5551da177e4SLinus Torvalds */
tcp_init_buffer_space(struct sock * sk)556bc183decSFlorian Westphal static void tcp_init_buffer_space(struct sock *sk)
5571da177e4SLinus Torvalds {
55802ca527aSKuniyuki Iwashima int tcp_app_win = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_app_win);
5591da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
5601da177e4SLinus Torvalds int maxwin;
5611da177e4SLinus Torvalds
5621da177e4SLinus Torvalds if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
5636ae70532SEric Dumazet tcp_sndbuf_expand(sk);
5641da177e4SLinus Torvalds
5659a568de4SEric Dumazet tcp_mstamp_refresh(tp);
566645f4c6fSEric Dumazet tp->rcvq_space.time = tp->tcp_mstamp;
567b0983d3cSEric Dumazet tp->rcvq_space.seq = tp->copied_seq;
5681da177e4SLinus Torvalds
5691da177e4SLinus Torvalds maxwin = tcp_full_space(sk);
5701da177e4SLinus Torvalds
5711da177e4SLinus Torvalds if (tp->window_clamp >= maxwin) {
572f410cbeaSEric Dumazet WRITE_ONCE(tp->window_clamp, maxwin);
5731da177e4SLinus Torvalds
5740c12654aSEric Dumazet if (tcp_app_win && maxwin > 4 * tp->advmss)
575f410cbeaSEric Dumazet WRITE_ONCE(tp->window_clamp,
576f410cbeaSEric Dumazet max(maxwin - (maxwin >> tcp_app_win),
577f410cbeaSEric Dumazet 4 * tp->advmss));
5781da177e4SLinus Torvalds }
5791da177e4SLinus Torvalds
5801da177e4SLinus Torvalds /* Force reservation of one segment. */
5810c12654aSEric Dumazet if (tcp_app_win &&
5821da177e4SLinus Torvalds tp->window_clamp > 2 * tp->advmss &&
5831da177e4SLinus Torvalds tp->window_clamp + tp->advmss > maxwin)
584f410cbeaSEric Dumazet WRITE_ONCE(tp->window_clamp,
585f410cbeaSEric Dumazet max(2 * tp->advmss, maxwin - tp->advmss));
5861da177e4SLinus Torvalds
5871da177e4SLinus Torvalds tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
588c2203cf7SEric Dumazet tp->snd_cwnd_stamp = tcp_jiffies32;
58972d05c00SEric Dumazet tp->rcvq_space.space = min3(tp->rcv_ssthresh, tp->rcv_wnd,
59072d05c00SEric Dumazet (u32)TCP_INIT_CWND * tp->advmss);
5911da177e4SLinus Torvalds }
5921da177e4SLinus Torvalds
593a337531bSYuchung Cheng /* 4. Recalculate window clamp after socket hit its memory bounds. */
tcp_clamp_window(struct sock * sk)5949e412ba7SIlpo Järvinen static void tcp_clamp_window(struct sock *sk)
5951da177e4SLinus Torvalds {
5969e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
5976687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
598356d1833SEric Dumazet struct net *net = sock_net(sk);
59902739545SKuniyuki Iwashima int rmem2;
6001da177e4SLinus Torvalds
6016687e988SArnaldo Carvalho de Melo icsk->icsk_ack.quick = 0;
60202739545SKuniyuki Iwashima rmem2 = READ_ONCE(net->ipv4.sysctl_tcp_rmem[2]);
6031da177e4SLinus Torvalds
60402739545SKuniyuki Iwashima if (sk->sk_rcvbuf < rmem2 &&
6051da177e4SLinus Torvalds !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
606b8da51ebSEric Dumazet !tcp_under_memory_pressure(sk) &&
607180d8cd9SGlauber Costa sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {
608ebb3b78dSEric Dumazet WRITE_ONCE(sk->sk_rcvbuf,
60902739545SKuniyuki Iwashima min(atomic_read(&sk->sk_rmem_alloc), rmem2));
6101da177e4SLinus Torvalds }
611326f36e9SJohn Heffner if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
6121da177e4SLinus Torvalds tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds
61540efc6faSStephen Hemminger /* Initialize RCV_MSS value.
61640efc6faSStephen Hemminger * RCV_MSS is an our guess about MSS used by the peer.
61740efc6faSStephen Hemminger * We haven't any direct information about the MSS.
61840efc6faSStephen Hemminger * It's better to underestimate the RCV_MSS rather than overestimate.
61940efc6faSStephen Hemminger * Overestimations make us ACKing less frequently than needed.
62040efc6faSStephen Hemminger * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
62140efc6faSStephen Hemminger */
tcp_initialize_rcv_mss(struct sock * sk)62240efc6faSStephen Hemminger void tcp_initialize_rcv_mss(struct sock *sk)
62340efc6faSStephen Hemminger {
624cf533ea5SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk);
62540efc6faSStephen Hemminger unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
62640efc6faSStephen Hemminger
62740efc6faSStephen Hemminger hint = min(hint, tp->rcv_wnd / 2);
628bee7ca9eSWilliam Allen Simpson hint = min(hint, TCP_MSS_DEFAULT);
62940efc6faSStephen Hemminger hint = max(hint, TCP_MIN_MSS);
63040efc6faSStephen Hemminger
63140efc6faSStephen Hemminger inet_csk(sk)->icsk_ack.rcv_mss = hint;
63240efc6faSStephen Hemminger }
6334bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_initialize_rcv_mss);
63440efc6faSStephen Hemminger
6351da177e4SLinus Torvalds /* Receiver "autotuning" code.
6361da177e4SLinus Torvalds *
6371da177e4SLinus Torvalds * The algorithm for RTT estimation w/o timestamps is based on
6381da177e4SLinus Torvalds * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
6397a6498ebSAlexander A. Klimov * <https://public.lanl.gov/radiant/pubs.html#DRS>
6401da177e4SLinus Torvalds *
6411da177e4SLinus Torvalds * More detail on this code can be found at
642631dd1a8SJustin P. Mattock * <http://staff.psc.edu/jheffner/>,
6431da177e4SLinus Torvalds * though this reference is out of date. A new paper
6441da177e4SLinus Torvalds * is pending.
6451da177e4SLinus Torvalds */
tcp_rcv_rtt_update(struct tcp_sock * tp,u32 sample,int win_dep)6461da177e4SLinus Torvalds static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
6471da177e4SLinus Torvalds {
648645f4c6fSEric Dumazet u32 new_sample = tp->rcv_rtt_est.rtt_us;
6491da177e4SLinus Torvalds long m = sample;
6501da177e4SLinus Torvalds
6511da177e4SLinus Torvalds if (new_sample != 0) {
6521da177e4SLinus Torvalds /* If we sample in larger samples in the non-timestamp
6531da177e4SLinus Torvalds * case, we could grossly overestimate the RTT especially
6541da177e4SLinus Torvalds * with chatty applications or bulk transfer apps which
6551da177e4SLinus Torvalds * are stalled on filesystem I/O.
6561da177e4SLinus Torvalds *
6571da177e4SLinus Torvalds * Also, since we are only going for a minimum in the
65831f34269SStephen Hemminger * non-timestamp case, we do not smooth things out
659caa20d9aSStephen Hemminger * else with timestamps disabled convergence takes too
6601da177e4SLinus Torvalds * long.
6611da177e4SLinus Torvalds */
6621da177e4SLinus Torvalds if (!win_dep) {
6631da177e4SLinus Torvalds m -= (new_sample >> 3);
6641da177e4SLinus Torvalds new_sample += m;
66518a223e0SNeal Cardwell } else {
66618a223e0SNeal Cardwell m <<= 3;
66718a223e0SNeal Cardwell if (m < new_sample)
66818a223e0SNeal Cardwell new_sample = m;
66918a223e0SNeal Cardwell }
6701da177e4SLinus Torvalds } else {
671caa20d9aSStephen Hemminger /* No previous measure. */
6721da177e4SLinus Torvalds new_sample = m << 3;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds
675645f4c6fSEric Dumazet tp->rcv_rtt_est.rtt_us = new_sample;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds
tcp_rcv_rtt_measure(struct tcp_sock * tp)6781da177e4SLinus Torvalds static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
6791da177e4SLinus Torvalds {
680645f4c6fSEric Dumazet u32 delta_us;
681645f4c6fSEric Dumazet
6829a568de4SEric Dumazet if (tp->rcv_rtt_est.time == 0)
6831da177e4SLinus Torvalds goto new_measure;
6841da177e4SLinus Torvalds if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
6851da177e4SLinus Torvalds return;
6869a568de4SEric Dumazet delta_us = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcv_rtt_est.time);
6879ee11bd0SWei Wang if (!delta_us)
6889ee11bd0SWei Wang delta_us = 1;
689645f4c6fSEric Dumazet tcp_rcv_rtt_update(tp, delta_us, 1);
6901da177e4SLinus Torvalds
6911da177e4SLinus Torvalds new_measure:
6921da177e4SLinus Torvalds tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
693645f4c6fSEric Dumazet tp->rcv_rtt_est.time = tp->tcp_mstamp;
6941da177e4SLinus Torvalds }
6951da177e4SLinus Torvalds
tcp_rtt_tsopt_us(const struct tcp_sock * tp)696b04c3320SEric Dumazet static s32 tcp_rtt_tsopt_us(const struct tcp_sock *tp)
697b04c3320SEric Dumazet {
698b04c3320SEric Dumazet u32 delta, delta_us;
699b04c3320SEric Dumazet
700b04c3320SEric Dumazet delta = tcp_time_stamp_ts(tp) - tp->rx_opt.rcv_tsecr;
701614e8316SEric Dumazet if (tp->tcp_usec_ts)
702614e8316SEric Dumazet return delta;
703b04c3320SEric Dumazet
704b04c3320SEric Dumazet if (likely(delta < INT_MAX / (USEC_PER_SEC / TCP_TS_HZ))) {
705b04c3320SEric Dumazet if (!delta)
706b04c3320SEric Dumazet delta = 1;
707b04c3320SEric Dumazet delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ);
708b04c3320SEric Dumazet return delta_us;
709b04c3320SEric Dumazet }
710b04c3320SEric Dumazet return -1;
711b04c3320SEric Dumazet }
712b04c3320SEric Dumazet
tcp_rcv_rtt_measure_ts(struct sock * sk,const struct sk_buff * skb)713056834d9SIlpo Järvinen static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
714056834d9SIlpo Järvinen const struct sk_buff *skb)
7151da177e4SLinus Torvalds {
716463c84b9SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk);
7179a568de4SEric Dumazet
7183f6c65d6SWei Wang if (tp->rx_opt.rcv_tsecr == tp->rcv_rtt_last_tsecr)
7193f6c65d6SWei Wang return;
7203f6c65d6SWei Wang tp->rcv_rtt_last_tsecr = tp->rx_opt.rcv_tsecr;
7213f6c65d6SWei Wang
7223f6c65d6SWei Wang if (TCP_SKB_CB(skb)->end_seq -
7233f6c65d6SWei Wang TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss) {
724b04c3320SEric Dumazet s32 delta = tcp_rtt_tsopt_us(tp);
7259a568de4SEric Dumazet
726b04c3320SEric Dumazet if (delta >= 0)
727b04c3320SEric Dumazet tcp_rcv_rtt_update(tp, delta, 0);
7281da177e4SLinus Torvalds }
7299efdda4eSEric Dumazet }
7301da177e4SLinus Torvalds
7311da177e4SLinus Torvalds /*
7321da177e4SLinus Torvalds * This function should be called every time data is copied to user space.
7331da177e4SLinus Torvalds * It calculates the appropriate TCP receive buffer space.
7341da177e4SLinus Torvalds */
tcp_rcv_space_adjust(struct sock * sk)7351da177e4SLinus Torvalds void tcp_rcv_space_adjust(struct sock *sk)
7361da177e4SLinus Torvalds {
7371da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
738607065baSEric Dumazet u32 copied;
7391da177e4SLinus Torvalds int time;
7401da177e4SLinus Torvalds
7416163849dSYafang Shao trace_tcp_rcv_space_adjust(sk);
7426163849dSYafang Shao
74386323850SEric Dumazet tcp_mstamp_refresh(tp);
7449a568de4SEric Dumazet time = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcvq_space.time);
745645f4c6fSEric Dumazet if (time < (tp->rcv_rtt_est.rtt_us >> 3) || tp->rcv_rtt_est.rtt_us == 0)
7461da177e4SLinus Torvalds return;
7471da177e4SLinus Torvalds
748b0983d3cSEric Dumazet /* Number of bytes copied to user in last RTT */
749b0983d3cSEric Dumazet copied = tp->copied_seq - tp->rcvq_space.seq;
750b0983d3cSEric Dumazet if (copied <= tp->rcvq_space.space)
751b0983d3cSEric Dumazet goto new_measure;
7521da177e4SLinus Torvalds
753b0983d3cSEric Dumazet /* A bit of theory :
754b0983d3cSEric Dumazet * copied = bytes received in previous RTT, our base window
755b0983d3cSEric Dumazet * To cope with packet losses, we need a 2x factor
756b0983d3cSEric Dumazet * To cope with slow start, and sender growing its cwin by 100 %
757b0983d3cSEric Dumazet * every RTT, we need a 4x factor, because the ACK we are sending
758b0983d3cSEric Dumazet * now is for the next RTT, not the current one :
759b0983d3cSEric Dumazet * <prev RTT . ><current RTT .. ><next RTT .... >
760b0983d3cSEric Dumazet */
7611da177e4SLinus Torvalds
762a2cbb160SSubash Abhinov Kasiviswanathan if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf) &&
763a2cbb160SSubash Abhinov Kasiviswanathan !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
764c3916ad9SEric Dumazet u64 rcvwin, grow;
765dfa2f048SEric Dumazet int rcvbuf;
7661da177e4SLinus Torvalds
767b0983d3cSEric Dumazet /* minimal window to cope with packet losses, assuming
768b0983d3cSEric Dumazet * steady state. Add some cushion because of small variations.
7691da177e4SLinus Torvalds */
770607065baSEric Dumazet rcvwin = ((u64)copied << 1) + 16 * tp->advmss;
771b0983d3cSEric Dumazet
772c3916ad9SEric Dumazet /* Accommodate for sender rate increase (eg. slow start) */
773c3916ad9SEric Dumazet grow = rcvwin * (copied - tp->rcvq_space.space);
774c3916ad9SEric Dumazet do_div(grow, tp->rcvq_space.space);
775c3916ad9SEric Dumazet rcvwin += (grow << 1);
776b0983d3cSEric Dumazet
777dfa2f048SEric Dumazet rcvbuf = min_t(u64, tcp_space_from_win(sk, rcvwin),
77802739545SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2]));
779b0983d3cSEric Dumazet if (rcvbuf > sk->sk_rcvbuf) {
780ebb3b78dSEric Dumazet WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
7811da177e4SLinus Torvalds
7821da177e4SLinus Torvalds /* Make the window clamp follow along. */
783f410cbeaSEric Dumazet WRITE_ONCE(tp->window_clamp,
784f410cbeaSEric Dumazet tcp_win_from_space(sk, rcvbuf));
7851da177e4SLinus Torvalds }
7861da177e4SLinus Torvalds }
787b0983d3cSEric Dumazet tp->rcvq_space.space = copied;
7881da177e4SLinus Torvalds
7891da177e4SLinus Torvalds new_measure:
7901da177e4SLinus Torvalds tp->rcvq_space.seq = tp->copied_seq;
791645f4c6fSEric Dumazet tp->rcvq_space.time = tp->tcp_mstamp;
7921da177e4SLinus Torvalds }
7931da177e4SLinus Torvalds
tcp_save_lrcv_flowlabel(struct sock * sk,const struct sk_buff * skb)79495b9a87cSDavid Morley static void tcp_save_lrcv_flowlabel(struct sock *sk, const struct sk_buff *skb)
79595b9a87cSDavid Morley {
79695b9a87cSDavid Morley #if IS_ENABLED(CONFIG_IPV6)
79795b9a87cSDavid Morley struct inet_connection_sock *icsk = inet_csk(sk);
79895b9a87cSDavid Morley
79995b9a87cSDavid Morley if (skb->protocol == htons(ETH_P_IPV6))
80095b9a87cSDavid Morley icsk->icsk_ack.lrcv_flowlabel = ntohl(ip6_flowlabel(ipv6_hdr(skb)));
80195b9a87cSDavid Morley #endif
80295b9a87cSDavid Morley }
80395b9a87cSDavid Morley
8041da177e4SLinus Torvalds /* There is something which you must keep in mind when you analyze the
8051da177e4SLinus Torvalds * behavior of the tp->ato delayed ack timeout interval. When a
8061da177e4SLinus Torvalds * connection starts up, we want to ack as quickly as possible. The
8071da177e4SLinus Torvalds * problem is that "good" TCP's do slow start at the beginning of data
8081da177e4SLinus Torvalds * transmission. The means that until we send the first few ACK's the
8091da177e4SLinus Torvalds * sender will sit on his end and only queue most of his data, because
8101da177e4SLinus Torvalds * he can only send snd_cwnd unacked packets at any given time. For
8111da177e4SLinus Torvalds * each ACK we send, he increments snd_cwnd and transmits more of his
8121da177e4SLinus Torvalds * queue. -DaveM
8131da177e4SLinus Torvalds */
tcp_event_data_recv(struct sock * sk,struct sk_buff * skb)8149e412ba7SIlpo Järvinen static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
8151da177e4SLinus Torvalds {
8169e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
817463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
8181da177e4SLinus Torvalds u32 now;
8191da177e4SLinus Torvalds
820463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk);
8211da177e4SLinus Torvalds
822463c84b9SArnaldo Carvalho de Melo tcp_measure_rcv_mss(sk, skb);
8231da177e4SLinus Torvalds
8241da177e4SLinus Torvalds tcp_rcv_rtt_measure(tp);
8251da177e4SLinus Torvalds
82670eabf0eSEric Dumazet now = tcp_jiffies32;
8271da177e4SLinus Torvalds
828463c84b9SArnaldo Carvalho de Melo if (!icsk->icsk_ack.ato) {
8291da177e4SLinus Torvalds /* The _first_ data packet received, initialize
8301da177e4SLinus Torvalds * delayed ACK engine.
8311da177e4SLinus Torvalds */
8329a9c9b51SEric Dumazet tcp_incr_quickack(sk, TCP_MAX_QUICKACKS);
833463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = TCP_ATO_MIN;
8341da177e4SLinus Torvalds } else {
835463c84b9SArnaldo Carvalho de Melo int m = now - icsk->icsk_ack.lrcvtime;
8361da177e4SLinus Torvalds
8371da177e4SLinus Torvalds if (m <= TCP_ATO_MIN / 2) {
8381da177e4SLinus Torvalds /* The fastest case is the first. */
839463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
840463c84b9SArnaldo Carvalho de Melo } else if (m < icsk->icsk_ack.ato) {
841463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
842463c84b9SArnaldo Carvalho de Melo if (icsk->icsk_ack.ato > icsk->icsk_rto)
843463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.ato = icsk->icsk_rto;
844463c84b9SArnaldo Carvalho de Melo } else if (m > icsk->icsk_rto) {
845caa20d9aSStephen Hemminger /* Too long gap. Apparently sender failed to
8461da177e4SLinus Torvalds * restart window, so that we send ACKs quickly.
8471da177e4SLinus Torvalds */
8489a9c9b51SEric Dumazet tcp_incr_quickack(sk, TCP_MAX_QUICKACKS);
8491da177e4SLinus Torvalds }
8501da177e4SLinus Torvalds }
851463c84b9SArnaldo Carvalho de Melo icsk->icsk_ack.lrcvtime = now;
85295b9a87cSDavid Morley tcp_save_lrcv_flowlabel(sk, skb);
8531da177e4SLinus Torvalds
854f4c9f85fSYousuk Seung tcp_ecn_check_ce(sk, skb);
8551da177e4SLinus Torvalds
8561da177e4SLinus Torvalds if (skb->len >= 128)
857240bfd13SEric Dumazet tcp_grow_window(sk, skb, true);
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds
8601da177e4SLinus Torvalds /* Called to compute a smoothed rtt estimate. The data fed to this
8611da177e4SLinus Torvalds * routine either comes from timestamps, or from segments that were
8621da177e4SLinus Torvalds * known _not_ to have been retransmitted [see Karn/Partridge
8631da177e4SLinus Torvalds * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
8641da177e4SLinus Torvalds * piece by Van Jacobson.
8651da177e4SLinus Torvalds * NOTE: the next three routines used to be one big routine.
8661da177e4SLinus Torvalds * To save cycles in the RFC 1323 implementation it was better to break
8671da177e4SLinus Torvalds * it up into three procedures. -- erics
8681da177e4SLinus Torvalds */
tcp_rtt_estimator(struct sock * sk,long mrtt_us)869740b0f18SEric Dumazet static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
8701da177e4SLinus Torvalds {
8716687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk);
872740b0f18SEric Dumazet long m = mrtt_us; /* RTT */
873740b0f18SEric Dumazet u32 srtt = tp->srtt_us;
8741da177e4SLinus Torvalds
8751da177e4SLinus Torvalds /* The following amusing code comes from Jacobson's
8761da177e4SLinus Torvalds * article in SIGCOMM '88. Note that rtt and mdev
8771da177e4SLinus Torvalds * are scaled versions of rtt and mean deviation.
8781da177e4SLinus Torvalds * This is designed to be as fast as possible
8791da177e4SLinus Torvalds * m stands for "measurement".
8801da177e4SLinus Torvalds *
8811da177e4SLinus Torvalds * On a 1990 paper the rto value is changed to:
8821da177e4SLinus Torvalds * RTO = rtt + 4 * mdev
8831da177e4SLinus Torvalds *
8841da177e4SLinus Torvalds * Funny. This algorithm seems to be very broken.
8851da177e4SLinus Torvalds * These formulae increase RTO, when it should be decreased, increase
88631f34269SStephen Hemminger * too slowly, when it should be increased quickly, decrease too quickly
8871da177e4SLinus Torvalds * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
8881da177e4SLinus Torvalds * does not matter how to _calculate_ it. Seems, it was trap
8891da177e4SLinus Torvalds * that VJ failed to avoid. 8)
8901da177e4SLinus Torvalds */
8914a5ab4e2SEric Dumazet if (srtt != 0) {
8924a5ab4e2SEric Dumazet m -= (srtt >> 3); /* m is now error in rtt est */
8934a5ab4e2SEric Dumazet srtt += m; /* rtt = 7/8 rtt + 1/8 new */
8941da177e4SLinus Torvalds if (m < 0) {
8951da177e4SLinus Torvalds m = -m; /* m is now abs(error) */
896740b0f18SEric Dumazet m -= (tp->mdev_us >> 2); /* similar update on mdev */
8971da177e4SLinus Torvalds /* This is similar to one of Eifel findings.
8981da177e4SLinus Torvalds * Eifel blocks mdev updates when rtt decreases.
8991da177e4SLinus Torvalds * This solution is a bit different: we use finer gain
9001da177e4SLinus Torvalds * for mdev in this case (alpha*beta).
9011da177e4SLinus Torvalds * Like Eifel it also prevents growth of rto,
9021da177e4SLinus Torvalds * but also it limits too fast rto decreases,
9031da177e4SLinus Torvalds * happening in pure Eifel.
9041da177e4SLinus Torvalds */
9051da177e4SLinus Torvalds if (m > 0)
9061da177e4SLinus Torvalds m >>= 3;
9071da177e4SLinus Torvalds } else {
908740b0f18SEric Dumazet m -= (tp->mdev_us >> 2); /* similar update on mdev */
9091da177e4SLinus Torvalds }
910740b0f18SEric Dumazet tp->mdev_us += m; /* mdev = 3/4 mdev + 1/4 new */
911740b0f18SEric Dumazet if (tp->mdev_us > tp->mdev_max_us) {
912740b0f18SEric Dumazet tp->mdev_max_us = tp->mdev_us;
913740b0f18SEric Dumazet if (tp->mdev_max_us > tp->rttvar_us)
914740b0f18SEric Dumazet tp->rttvar_us = tp->mdev_max_us;
9151da177e4SLinus Torvalds }
9161da177e4SLinus Torvalds if (after(tp->snd_una, tp->rtt_seq)) {
917740b0f18SEric Dumazet if (tp->mdev_max_us < tp->rttvar_us)
918740b0f18SEric Dumazet tp->rttvar_us -= (tp->rttvar_us - tp->mdev_max_us) >> 2;
9191da177e4SLinus Torvalds tp->rtt_seq = tp->snd_nxt;
920740b0f18SEric Dumazet tp->mdev_max_us = tcp_rto_min_us(sk);
92123729ff2SStanislav Fomichev
92248e2cd3eSPhilo Lu tcp_bpf_rtt(sk, mrtt_us, srtt);
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds } else {
9251da177e4SLinus Torvalds /* no previous measure. */
9264a5ab4e2SEric Dumazet srtt = m << 3; /* take the measured time to be rtt */
927740b0f18SEric Dumazet tp->mdev_us = m << 1; /* make sure rto = 3*rtt */
928740b0f18SEric Dumazet tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk));
929740b0f18SEric Dumazet tp->mdev_max_us = tp->rttvar_us;
9301da177e4SLinus Torvalds tp->rtt_seq = tp->snd_nxt;
93123729ff2SStanislav Fomichev
93248e2cd3eSPhilo Lu tcp_bpf_rtt(sk, mrtt_us, srtt);
9331da177e4SLinus Torvalds }
934740b0f18SEric Dumazet tp->srtt_us = max(1U, srtt);
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds
tcp_update_pacing_rate(struct sock * sk)93795bd09ebSEric Dumazet static void tcp_update_pacing_rate(struct sock *sk)
93895bd09ebSEric Dumazet {
93995bd09ebSEric Dumazet const struct tcp_sock *tp = tcp_sk(sk);
94095bd09ebSEric Dumazet u64 rate;
94195bd09ebSEric Dumazet
94295bd09ebSEric Dumazet /* set sk_pacing_rate to 200 % of current rate (mss * cwnd / srtt) */
94343e122b0SEric Dumazet rate = (u64)tp->mss_cache * ((USEC_PER_SEC / 100) << 3);
94443e122b0SEric Dumazet
94543e122b0SEric Dumazet /* current rate is (cwnd * mss) / srtt
94643e122b0SEric Dumazet * In Slow Start [1], set sk_pacing_rate to 200 % the current rate.
94743e122b0SEric Dumazet * In Congestion Avoidance phase, set it to 120 % the current rate.
94843e122b0SEric Dumazet *
94943e122b0SEric Dumazet * [1] : Normal Slow Start condition is (tp->snd_cwnd < tp->snd_ssthresh)
95043e122b0SEric Dumazet * If snd_cwnd >= (tp->snd_ssthresh / 2), we are approaching
95143e122b0SEric Dumazet * end of slow start and should slow down.
95243e122b0SEric Dumazet */
95340570375SEric Dumazet if (tcp_snd_cwnd(tp) < tp->snd_ssthresh / 2)
95459bf6c65SKuniyuki Iwashima rate *= READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_pacing_ss_ratio);
95543e122b0SEric Dumazet else
95659bf6c65SKuniyuki Iwashima rate *= READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_pacing_ca_ratio);
95795bd09ebSEric Dumazet
95840570375SEric Dumazet rate *= max(tcp_snd_cwnd(tp), tp->packets_out);
95995bd09ebSEric Dumazet
960740b0f18SEric Dumazet if (likely(tp->srtt_us))
961740b0f18SEric Dumazet do_div(rate, tp->srtt_us);
96295bd09ebSEric Dumazet
963a9da6f29SMark Rutland /* WRITE_ONCE() is needed because sch_fq fetches sk_pacing_rate
964ba537427SEric Dumazet * without any lock. We want to make sure compiler wont store
965ba537427SEric Dumazet * intermediate values in this location.
966ba537427SEric Dumazet */
96728b24f90SEric Dumazet WRITE_ONCE(sk->sk_pacing_rate,
96828b24f90SEric Dumazet min_t(u64, rate, READ_ONCE(sk->sk_max_pacing_rate)));
96995bd09ebSEric Dumazet }
97095bd09ebSEric Dumazet
9711da177e4SLinus Torvalds /* Calculate rto without backoff. This is the second half of Van Jacobson's
9721da177e4SLinus Torvalds * routine referred to above.
9731da177e4SLinus Torvalds */
tcp_set_rto(struct sock * sk)974f7e56a76Sstephen hemminger static void tcp_set_rto(struct sock *sk)
9751da177e4SLinus Torvalds {
976463c84b9SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk);
9771da177e4SLinus Torvalds /* Old crap is replaced with new one. 8)
9781da177e4SLinus Torvalds *
9791da177e4SLinus Torvalds * More seriously:
9801da177e4SLinus Torvalds * 1. If rtt variance happened to be less 50msec, it is hallucination.
9811da177e4SLinus Torvalds * It cannot be less due to utterly erratic ACK generation made
9821da177e4SLinus Torvalds * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
9831da177e4SLinus Torvalds * to do with delayed acks, because at cwnd>2 true delack timeout
9841da177e4SLinus Torvalds * is invisible. Actually, Linux-2.4 also generates erratic
985caa20d9aSStephen Hemminger * ACKs in some circumstances.
9861da177e4SLinus Torvalds */
987f1ecd5d9SDamian Lukowski inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
9881da177e4SLinus Torvalds
9891da177e4SLinus Torvalds /* 2. Fixups made earlier cannot be right.
9901da177e4SLinus Torvalds * If we do not estimate RTO correctly without them,
9911da177e4SLinus Torvalds * all the algo is pure shit and should be replaced
992caa20d9aSStephen Hemminger * with correct one. It is exactly, which we pretend to do.
9931da177e4SLinus Torvalds */
9941da177e4SLinus Torvalds
9951da177e4SLinus Torvalds /* NOTE: clamping at TCP_RTO_MIN is not required, current algo
9961da177e4SLinus Torvalds * guarantees that rto is higher.
9971da177e4SLinus Torvalds */
998f1ecd5d9SDamian Lukowski tcp_bound_rto(sk);
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds
tcp_init_cwnd(const struct tcp_sock * tp,const struct dst_entry * dst)1001cf533ea5SEric Dumazet __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)
10021da177e4SLinus Torvalds {
10031da177e4SLinus Torvalds __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
10041da177e4SLinus Torvalds
100522b71c8fSGerrit Renker if (!cwnd)
1006442b9635SDavid S. Miller cwnd = TCP_INIT_CWND;
10071da177e4SLinus Torvalds return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds
1010a71d77e6SPriyaranjan Jha struct tcp_sacktag_state {
1011a71d77e6SPriyaranjan Jha /* Timestamps for earliest and latest never-retransmitted segment
1012a71d77e6SPriyaranjan Jha * that was SACKed. RTO needs the earliest RTT to stay conservative,
1013a71d77e6SPriyaranjan Jha * but congestion control should still get an accurate delay signal.
1014a71d77e6SPriyaranjan Jha */
1015a71d77e6SPriyaranjan Jha u64 first_sackt;
1016a71d77e6SPriyaranjan Jha u64 last_sackt;
1017a71d77e6SPriyaranjan Jha u32 reord;
1018a71d77e6SPriyaranjan Jha u32 sack_delivered;
1019a71d77e6SPriyaranjan Jha int flag;
1020a71d77e6SPriyaranjan Jha unsigned int mss_now;
1021a71d77e6SPriyaranjan Jha struct rate_sample *rate;
1022a71d77e6SPriyaranjan Jha };
1023a71d77e6SPriyaranjan Jha
1024ad2b9b0fSPriyaranjan Jha /* Take a notice that peer is sending D-SACKs. Skip update of data delivery
1025ad2b9b0fSPriyaranjan Jha * and spurious retransmission information if this DSACK is unlikely caused by
1026ad2b9b0fSPriyaranjan Jha * sender's action:
1027ad2b9b0fSPriyaranjan Jha * - DSACKed sequence range is larger than maximum receiver's window.
1028ad2b9b0fSPriyaranjan Jha * - Total no. of DSACKed segments exceed the total no. of retransmitted segs.
1029ad2b9b0fSPriyaranjan Jha */
tcp_dsack_seen(struct tcp_sock * tp,u32 start_seq,u32 end_seq,struct tcp_sacktag_state * state)1030a71d77e6SPriyaranjan Jha static u32 tcp_dsack_seen(struct tcp_sock *tp, u32 start_seq,
1031a71d77e6SPriyaranjan Jha u32 end_seq, struct tcp_sacktag_state *state)
1032e60402d0SIlpo Järvinen {
1033a71d77e6SPriyaranjan Jha u32 seq_len, dup_segs = 1;
1034a71d77e6SPriyaranjan Jha
1035ad2b9b0fSPriyaranjan Jha if (!before(start_seq, end_seq))
1036ad2b9b0fSPriyaranjan Jha return 0;
1037ad2b9b0fSPriyaranjan Jha
1038a71d77e6SPriyaranjan Jha seq_len = end_seq - start_seq;
1039ad2b9b0fSPriyaranjan Jha /* Dubious DSACK: DSACKed range greater than maximum advertised rwnd */
1040ad2b9b0fSPriyaranjan Jha if (seq_len > tp->max_window)
1041ad2b9b0fSPriyaranjan Jha return 0;
1042a71d77e6SPriyaranjan Jha if (seq_len > tp->mss_cache)
1043a71d77e6SPriyaranjan Jha dup_segs = DIV_ROUND_UP(seq_len, tp->mss_cache);
104463f367d9SYuchung Cheng else if (tp->tlp_high_seq && tp->tlp_high_seq == end_seq)
104563f367d9SYuchung Cheng state->flag |= FLAG_DSACK_TLP;
1046ad2b9b0fSPriyaranjan Jha
1047ad2b9b0fSPriyaranjan Jha tp->dsack_dups += dup_segs;
1048ad2b9b0fSPriyaranjan Jha /* Skip the DSACK if dup segs weren't retransmitted by sender */
1049ad2b9b0fSPriyaranjan Jha if (tp->dsack_dups > tp->total_retrans)
1050ad2b9b0fSPriyaranjan Jha return 0;
1051a71d77e6SPriyaranjan Jha
1052ab56222aSVijay Subramanian tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;
1053a657db03SNeal Cardwell /* We increase the RACK ordering window in rounds where we receive
1054a657db03SNeal Cardwell * DSACKs that may have been due to reordering causing RACK to trigger
1055a657db03SNeal Cardwell * a spurious fast recovery. Thus RACK ignores DSACKs that happen
1056a657db03SNeal Cardwell * without having seen reordering, or that match TLP probes (TLP
1057a657db03SNeal Cardwell * is timer-driven, not triggered by RACK).
1058a657db03SNeal Cardwell */
1059a657db03SNeal Cardwell if (tp->reord_seen && !(state->flag & FLAG_DSACK_TLP))
10601f255691SPriyaranjan Jha tp->rack.dsack_seen = 1;
1061a71d77e6SPriyaranjan Jha
1062a71d77e6SPriyaranjan Jha state->flag |= FLAG_DSACKING_ACK;
1063a71d77e6SPriyaranjan Jha /* A spurious retransmission is delivered */
1064a71d77e6SPriyaranjan Jha state->sack_delivered += dup_segs;
1065a71d77e6SPriyaranjan Jha
1066a71d77e6SPriyaranjan Jha return dup_segs;
1067e60402d0SIlpo Järvinen }
1068e60402d0SIlpo Järvinen
1069737ff314SYuchung Cheng /* It's reordering when higher sequence was delivered (i.e. sacked) before
1070737ff314SYuchung Cheng * some lower never-retransmitted sequence ("low_seq"). The maximum reordering
1071737ff314SYuchung Cheng * distance is approximated in full-mss packet distance ("reordering").
1072737ff314SYuchung Cheng */
tcp_check_sack_reordering(struct sock * sk,const u32 low_seq,const int ts)1073737ff314SYuchung Cheng static void tcp_check_sack_reordering(struct sock *sk, const u32 low_seq,
10746687e988SArnaldo Carvalho de Melo const int ts)
10751da177e4SLinus Torvalds {
10766687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk);
1077737ff314SYuchung Cheng const u32 mss = tp->mss_cache;
1078737ff314SYuchung Cheng u32 fack, metric;
107940b215e5SPavel Emelyanov
1080737ff314SYuchung Cheng fack = tcp_highest_sack_seq(tp);
1081737ff314SYuchung Cheng if (!before(low_seq, fack))
10826f5b24eeSSoheil Hassas Yeganeh return;
10836f5b24eeSSoheil Hassas Yeganeh
1084737ff314SYuchung Cheng metric = fack - low_seq;
1085737ff314SYuchung Cheng if ((metric > tp->reordering * mss) && mss) {
10861da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 1
108791df42beSJoe Perches pr_debug("Disorder%d %d %u f%u s%u rr%d\n",
10886687e988SArnaldo Carvalho de Melo tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
10891da177e4SLinus Torvalds tp->reordering,
1090737ff314SYuchung Cheng 0,
10911da177e4SLinus Torvalds tp->sacked_out,
10921da177e4SLinus Torvalds tp->undo_marker ? tp->undo_retrans : 0);
10931da177e4SLinus Torvalds #endif
1094737ff314SYuchung Cheng tp->reordering = min_t(u32, (metric + mss - 1) / mss,
1095a11e5b3eSKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_max_reordering));
10961da177e4SLinus Torvalds }
1097eed530b6SYuchung Cheng
10982d2517eeSYuchung Cheng /* This exciting event is worth to be remembered. 8) */
10997ec65372SWei Wang tp->reord_seen++;
1100737ff314SYuchung Cheng NET_INC_STATS(sock_net(sk),
1101737ff314SYuchung Cheng ts ? LINUX_MIB_TCPTSREORDER : LINUX_MIB_TCPSACKREORDER);
11021da177e4SLinus Torvalds }
11031da177e4SLinus Torvalds
110468698970SYuchung Cheng /* This must be called before lost_out or retrans_out are updated
110568698970SYuchung Cheng * on a new loss, because we want to know if all skbs previously
110668698970SYuchung Cheng * known to be lost have already been retransmitted, indicating
110768698970SYuchung Cheng * that this newly lost skb is our next skb to retransmit.
110868698970SYuchung Cheng */
tcp_verify_retransmit_hint(struct tcp_sock * tp,struct sk_buff * skb)1109c8c213f2SIlpo Järvinen static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
1110c8c213f2SIlpo Järvinen {
1111e176b1baSPengcheng Yang if ((!tp->retransmit_skb_hint && tp->retrans_out >= tp->lost_out) ||
1112e176b1baSPengcheng Yang (tp->retransmit_skb_hint &&
1113c8c213f2SIlpo Järvinen before(TCP_SKB_CB(skb)->seq,
1114e176b1baSPengcheng Yang TCP_SKB_CB(tp->retransmit_skb_hint)->seq)))
1115006f582cSIlpo Järvinen tp->retransmit_skb_hint = skb;
1116c8c213f2SIlpo Järvinen }
1117c8c213f2SIlpo Järvinen
11189cd8b6c9SYuchung Cheng /* Sum the number of packets on the wire we have marked as lost, and
11199cd8b6c9SYuchung Cheng * notify the congestion control module that the given skb was marked lost.
11209cd8b6c9SYuchung Cheng */
tcp_notify_skb_loss_event(struct tcp_sock * tp,const struct sk_buff * skb)11219cd8b6c9SYuchung Cheng static void tcp_notify_skb_loss_event(struct tcp_sock *tp, const struct sk_buff *skb)
11229cd8b6c9SYuchung Cheng {
11239cd8b6c9SYuchung Cheng tp->lost += tcp_skb_pcount(skb);
11249cd8b6c9SYuchung Cheng }
11259cd8b6c9SYuchung Cheng
tcp_mark_skb_lost(struct sock * sk,struct sk_buff * skb)1126fd214674SYuchung Cheng void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb)
1127fd214674SYuchung Cheng {
112868698970SYuchung Cheng __u8 sacked = TCP_SKB_CB(skb)->sacked;
1129fd214674SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
1130fd214674SYuchung Cheng
113168698970SYuchung Cheng if (sacked & TCPCB_SACKED_ACKED)
113268698970SYuchung Cheng return;
113368698970SYuchung Cheng
113468698970SYuchung Cheng tcp_verify_retransmit_hint(tp, skb);
113568698970SYuchung Cheng if (sacked & TCPCB_LOST) {
113668698970SYuchung Cheng if (sacked & TCPCB_SACKED_RETRANS) {
1137fd214674SYuchung Cheng /* Account for retransmits that are lost again */
1138fd214674SYuchung Cheng TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
1139fd214674SYuchung Cheng tp->retrans_out -= tcp_skb_pcount(skb);
1140fd214674SYuchung Cheng NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT,
1141fd214674SYuchung Cheng tcp_skb_pcount(skb));
11429cd8b6c9SYuchung Cheng tcp_notify_skb_loss_event(tp, skb);
1143fd214674SYuchung Cheng }
114468698970SYuchung Cheng } else {
114568698970SYuchung Cheng tp->lost_out += tcp_skb_pcount(skb);
114668698970SYuchung Cheng TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
11479cd8b6c9SYuchung Cheng tcp_notify_skb_loss_event(tp, skb);
1148fd214674SYuchung Cheng }
11490682e690SNeal Cardwell }
11500682e690SNeal Cardwell
1151082d4fa9SYousuk Seung /* Updates the delivered and delivered_ce counts */
tcp_count_delivered(struct tcp_sock * tp,u32 delivered,bool ece_ack)1152082d4fa9SYousuk Seung static void tcp_count_delivered(struct tcp_sock *tp, u32 delivered,
1153082d4fa9SYousuk Seung bool ece_ack)
1154082d4fa9SYousuk Seung {
1155082d4fa9SYousuk Seung tp->delivered += delivered;
1156082d4fa9SYousuk Seung if (ece_ack)
1157082d4fa9SYousuk Seung tp->delivered_ce += delivered;
1158082d4fa9SYousuk Seung }
1159082d4fa9SYousuk Seung
11601da177e4SLinus Torvalds /* This procedure tags the retransmission queue when SACKs arrive.
11611da177e4SLinus Torvalds *
11621da177e4SLinus Torvalds * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
11631da177e4SLinus Torvalds * Packets in queue with these bits set are counted in variables
11641da177e4SLinus Torvalds * sacked_out, retrans_out and lost_out, correspondingly.
11651da177e4SLinus Torvalds *
11661da177e4SLinus Torvalds * Valid combinations are:
11671da177e4SLinus Torvalds * Tag InFlight Description
11681da177e4SLinus Torvalds * 0 1 - orig segment is in flight.
11691da177e4SLinus Torvalds * S 0 - nothing flies, orig reached receiver.
11701da177e4SLinus Torvalds * L 0 - nothing flies, orig lost by net.
11711da177e4SLinus Torvalds * R 2 - both orig and retransmit are in flight.
11721da177e4SLinus Torvalds * L|R 1 - orig is lost, retransmit is in flight.
11731da177e4SLinus Torvalds * S|R 1 - orig reached receiver, retrans is still in flight.
11741da177e4SLinus Torvalds * (L|S|R is logically valid, it could occur when L|R is sacked,
117521bd52eaSGeert Uytterhoeven * but it is equivalent to plain S and code short-circuits it to S.
11761da177e4SLinus Torvalds * L|S is logically invalid, it would mean -1 packet in flight 8))
11771da177e4SLinus Torvalds *
11781da177e4SLinus Torvalds * These 6 states form finite state machine, controlled by the following events:
11791da177e4SLinus Torvalds * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
11801da177e4SLinus Torvalds * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
1181974c1236SYuchung Cheng * 3. Loss detection event of two flavors:
11821da177e4SLinus Torvalds * A. Scoreboard estimator decided the packet is lost.
11831da177e4SLinus Torvalds * A'. Reno "three dupacks" marks head of queue lost.
1184974c1236SYuchung Cheng * B. SACK arrives sacking SND.NXT at the moment, when the
11851da177e4SLinus Torvalds * segment was retransmitted.
11861da177e4SLinus Torvalds * 4. D-SACK added new rule: D-SACK changes any tag to S.
11871da177e4SLinus Torvalds *
11881da177e4SLinus Torvalds * It is pleasant to note, that state diagram turns out to be commutative,
11891da177e4SLinus Torvalds * so that we are allowed not to be bothered by order of our actions,
11901da177e4SLinus Torvalds * when multiple events arrive simultaneously. (see the function below).
11911da177e4SLinus Torvalds *
11921da177e4SLinus Torvalds * Reordering detection.
11931da177e4SLinus Torvalds * --------------------
11941da177e4SLinus Torvalds * Reordering metric is maximal distance, which a packet can be displaced
11951da177e4SLinus Torvalds * in packet stream. With SACKs we can estimate it:
11961da177e4SLinus Torvalds *
11971da177e4SLinus Torvalds * 1. SACK fills old hole and the corresponding segment was not
11981da177e4SLinus Torvalds * ever retransmitted -> reordering. Alas, we cannot use it
11991da177e4SLinus Torvalds * when segment was retransmitted.
12001da177e4SLinus Torvalds * 2. The last flaw is solved with D-SACK. D-SACK arrives
12011da177e4SLinus Torvalds * for retransmitted and already SACKed segment -> reordering..
12021da177e4SLinus Torvalds * Both of these heuristics are not used in Loss state, when we cannot
12031da177e4SLinus Torvalds * account for retransmits accurately.
12045b3c9882SIlpo Järvinen *
12055b3c9882SIlpo Järvinen * SACK block validation.
12065b3c9882SIlpo Järvinen * ----------------------
12075b3c9882SIlpo Järvinen *
12085b3c9882SIlpo Järvinen * SACK block range validation checks that the received SACK block fits to
12095b3c9882SIlpo Järvinen * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
12105b3c9882SIlpo Järvinen * Note that SND.UNA is not included to the range though being valid because
12110e835331SIlpo Järvinen * it means that the receiver is rather inconsistent with itself reporting
12120e835331SIlpo Järvinen * SACK reneging when it should advance SND.UNA. Such SACK block this is
12130e835331SIlpo Järvinen * perfectly valid, however, in light of RFC2018 which explicitly states
12140e835331SIlpo Järvinen * that "SACK block MUST reflect the newest segment. Even if the newest
12150e835331SIlpo Järvinen * segment is going to be discarded ...", not that it looks very clever
12160e835331SIlpo Järvinen * in case of head skb. Due to potentional receiver driven attacks, we
12170e835331SIlpo Järvinen * choose to avoid immediate execution of a walk in write queue due to
12180e835331SIlpo Järvinen * reneging and defer head skb's loss recovery to standard loss recovery
12190e835331SIlpo Järvinen * procedure that will eventually trigger (nothing forbids us doing this).
12205b3c9882SIlpo Järvinen *
12215b3c9882SIlpo Järvinen * Implements also blockage to start_seq wrap-around. Problem lies in the
12225b3c9882SIlpo Järvinen * fact that though start_seq (s) is before end_seq (i.e., not reversed),
12235b3c9882SIlpo Järvinen * there's no guarantee that it will be before snd_nxt (n). The problem
12245b3c9882SIlpo Järvinen * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
12255b3c9882SIlpo Järvinen * wrap (s_w):
12265b3c9882SIlpo Järvinen *
12275b3c9882SIlpo Järvinen * <- outs wnd -> <- wrapzone ->
12285b3c9882SIlpo Järvinen * u e n u_w e_w s n_w
12295b3c9882SIlpo Järvinen * | | | | | | |
12305b3c9882SIlpo Järvinen * |<------------+------+----- TCP seqno space --------------+---------->|
12315b3c9882SIlpo Järvinen * ...-- <2^31 ->| |<--------...
12325b3c9882SIlpo Järvinen * ...---- >2^31 ------>| |<--------...
12335b3c9882SIlpo Järvinen *
12345b3c9882SIlpo Järvinen * Current code wouldn't be vulnerable but it's better still to discard such
12355b3c9882SIlpo Järvinen * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
12365b3c9882SIlpo Järvinen * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
12375b3c9882SIlpo Järvinen * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
12385b3c9882SIlpo Järvinen * equal to the ideal case (infinite seqno space without wrap caused issues).
12395b3c9882SIlpo Järvinen *
12405b3c9882SIlpo Järvinen * With D-SACK the lower bound is extended to cover sequence space below
12415b3c9882SIlpo Järvinen * SND.UNA down to undo_marker, which is the last point of interest. Yet
1242564262c1SRyousei Takano * again, D-SACK block must not to go across snd_una (for the same reason as
12435b3c9882SIlpo Järvinen * for the normal SACK blocks, explained above). But there all simplicity
12445b3c9882SIlpo Järvinen * ends, TCP might receive valid D-SACKs below that. As long as they reside
12455b3c9882SIlpo Järvinen * fully below undo_marker they do not affect behavior in anyway and can
12465b3c9882SIlpo Järvinen * therefore be safely ignored. In rare cases (which are more or less
12475b3c9882SIlpo Järvinen * theoretical ones), the D-SACK will nicely cross that boundary due to skb
12485b3c9882SIlpo Järvinen * fragmentation and packet reordering past skb's retransmission. To consider
12495b3c9882SIlpo Järvinen * them correctly, the acceptable range must be extended even more though
12505b3c9882SIlpo Järvinen * the exact amount is rather hard to quantify. However, tp->max_window can
12515b3c9882SIlpo Järvinen * be used as an exaggerated estimate.
12521da177e4SLinus Torvalds */
tcp_is_sackblock_valid(struct tcp_sock * tp,bool is_dsack,u32 start_seq,u32 end_seq)1253a2a385d6SEric Dumazet static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack,
12545b3c9882SIlpo Järvinen u32 start_seq, u32 end_seq)
12555b3c9882SIlpo Järvinen {
12565b3c9882SIlpo Järvinen /* Too far in future, or reversed (interpretation is ambiguous) */
12575b3c9882SIlpo Järvinen if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
1258a2a385d6SEric Dumazet return false;
12595b3c9882SIlpo Järvinen
12605b3c9882SIlpo Järvinen /* Nasty start_seq wrap-around check (see comments above) */
12615b3c9882SIlpo Järvinen if (!before(start_seq, tp->snd_nxt))
1262a2a385d6SEric Dumazet return false;
12635b3c9882SIlpo Järvinen
1264564262c1SRyousei Takano /* In outstanding window? ...This is valid exit for D-SACKs too.
12655b3c9882SIlpo Järvinen * start_seq == snd_una is non-sensical (see comments above)
12665b3c9882SIlpo Järvinen */
12675b3c9882SIlpo Järvinen if (after(start_seq, tp->snd_una))
1268a2a385d6SEric Dumazet return true;
12695b3c9882SIlpo Järvinen
12705b3c9882SIlpo Järvinen if (!is_dsack || !tp->undo_marker)
1271a2a385d6SEric Dumazet return false;
12725b3c9882SIlpo Järvinen
12735b3c9882SIlpo Järvinen /* ...Then it's D-SACK, and must reside below snd_una completely */
1274f779b2d6SZheng Yan if (after(end_seq, tp->snd_una))
1275a2a385d6SEric Dumazet return false;
12765b3c9882SIlpo Järvinen
12775b3c9882SIlpo Järvinen if (!before(start_seq, tp->undo_marker))
1278a2a385d6SEric Dumazet return true;
12795b3c9882SIlpo Järvinen
12805b3c9882SIlpo Järvinen /* Too old */
12815b3c9882SIlpo Järvinen if (!after(end_seq, tp->undo_marker))
1282a2a385d6SEric Dumazet return false;
12835b3c9882SIlpo Järvinen
12845b3c9882SIlpo Järvinen /* Undo_marker boundary crossing (overestimates a lot). Known already:
12855b3c9882SIlpo Järvinen * start_seq < undo_marker and end_seq >= undo_marker.
12865b3c9882SIlpo Järvinen */
12875b3c9882SIlpo Järvinen return !before(start_seq, end_seq - tp->max_window);
12885b3c9882SIlpo Järvinen }
12895b3c9882SIlpo Järvinen
tcp_check_dsack(struct sock * sk,const struct sk_buff * ack_skb,struct tcp_sack_block_wire * sp,int num_sacks,u32 prior_snd_una,struct tcp_sacktag_state * state)1290a2a385d6SEric Dumazet static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb,
1291d06e021dSDavid S. Miller struct tcp_sack_block_wire *sp, int num_sacks,
1292a71d77e6SPriyaranjan Jha u32 prior_snd_una, struct tcp_sacktag_state *state)
1293d06e021dSDavid S. Miller {
12941ed83465SPavel Emelyanov struct tcp_sock *tp = tcp_sk(sk);
1295d3e2ce3bSHarvey Harrison u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
1296d3e2ce3bSHarvey Harrison u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
1297a71d77e6SPriyaranjan Jha u32 dup_segs;
1298d06e021dSDavid S. Miller
1299d06e021dSDavid S. Miller if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
1300c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
1301d06e021dSDavid S. Miller } else if (num_sacks > 1) {
1302d3e2ce3bSHarvey Harrison u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
1303d3e2ce3bSHarvey Harrison u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
1304d06e021dSDavid S. Miller
1305a71d77e6SPriyaranjan Jha if (after(end_seq_0, end_seq_1) || before(start_seq_0, start_seq_1))
1306a71d77e6SPriyaranjan Jha return false;
1307a71d77e6SPriyaranjan Jha NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKOFORECV);
1308a71d77e6SPriyaranjan Jha } else {
1309a71d77e6SPriyaranjan Jha return false;
1310d06e021dSDavid S. Miller }
1311a71d77e6SPriyaranjan Jha
1312a71d77e6SPriyaranjan Jha dup_segs = tcp_dsack_seen(tp, start_seq_0, end_seq_0, state);
1313ad2b9b0fSPriyaranjan Jha if (!dup_segs) { /* Skip dubious DSACK */
1314ad2b9b0fSPriyaranjan Jha NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKIGNOREDDUBIOUS);
1315ad2b9b0fSPriyaranjan Jha return false;
1316ad2b9b0fSPriyaranjan Jha }
1317ad2b9b0fSPriyaranjan Jha
1318e3a5a1e8SPriyaranjan Jha NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPDSACKRECVSEGS, dup_segs);
1319d06e021dSDavid S. Miller
1320d06e021dSDavid S. Miller /* D-SACK for already forgotten data... Do dumb counting. */
1321a71d77e6SPriyaranjan Jha if (tp->undo_marker && tp->undo_retrans > 0 &&
1322d06e021dSDavid S. Miller !after(end_seq_0, prior_snd_una) &&
1323d06e021dSDavid S. Miller after(end_seq_0, tp->undo_marker))
1324a71d77e6SPriyaranjan Jha tp->undo_retrans = max_t(int, 0, tp->undo_retrans - dup_segs);
1325d06e021dSDavid S. Miller
1326a71d77e6SPriyaranjan Jha return true;
1327d06e021dSDavid S. Miller }
1328d06e021dSDavid S. Miller
1329d1935942SIlpo Järvinen /* Check if skb is fully within the SACK block. In presence of GSO skbs,
1330d1935942SIlpo Järvinen * the incoming SACK may not exactly match but we can find smaller MSS
1331d1935942SIlpo Järvinen * aligned portion of it that matches. Therefore we might need to fragment
1332d1935942SIlpo Järvinen * which may fail and creates some hassle (caller must handle error case
1333d1935942SIlpo Järvinen * returns).
1334832d11c5SIlpo Järvinen *
1335832d11c5SIlpo Järvinen * FIXME: this could be merged to shift decision code
1336d1935942SIlpo Järvinen */
tcp_match_skb_to_sack(struct sock * sk,struct sk_buff * skb,u32 start_seq,u32 end_seq)13370f79efdcSAdrian Bunk static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
1338d1935942SIlpo Järvinen u32 start_seq, u32 end_seq)
1339d1935942SIlpo Järvinen {
1340a2a385d6SEric Dumazet int err;
1341a2a385d6SEric Dumazet bool in_sack;
1342d1935942SIlpo Järvinen unsigned int pkt_len;
1343adb92db8SIlpo Järvinen unsigned int mss;
1344d1935942SIlpo Järvinen
1345d1935942SIlpo Järvinen in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1346d1935942SIlpo Järvinen !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1347d1935942SIlpo Järvinen
1348d1935942SIlpo Järvinen if (tcp_skb_pcount(skb) > 1 && !in_sack &&
1349d1935942SIlpo Järvinen after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
1350adb92db8SIlpo Järvinen mss = tcp_skb_mss(skb);
1351d1935942SIlpo Järvinen in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1352d1935942SIlpo Järvinen
1353adb92db8SIlpo Järvinen if (!in_sack) {
1354d1935942SIlpo Järvinen pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
1355adb92db8SIlpo Järvinen if (pkt_len < mss)
1356adb92db8SIlpo Järvinen pkt_len = mss;
1357adb92db8SIlpo Järvinen } else {
1358d1935942SIlpo Järvinen pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
1359adb92db8SIlpo Järvinen if (pkt_len < mss)
1360adb92db8SIlpo Järvinen return -EINVAL;
1361adb92db8SIlpo Järvinen }
1362adb92db8SIlpo Järvinen
1363adb92db8SIlpo Järvinen /* Round if necessary so that SACKs cover only full MSSes
1364adb92db8SIlpo Järvinen * and/or the remaining small portion (if present)
1365adb92db8SIlpo Järvinen */
1366adb92db8SIlpo Järvinen if (pkt_len > mss) {
1367adb92db8SIlpo Järvinen unsigned int new_len = (pkt_len / mss) * mss;
1368b451e5d2SYuchung Cheng if (!in_sack && new_len < pkt_len)
1369adb92db8SIlpo Järvinen new_len += mss;
1370adb92db8SIlpo Järvinen pkt_len = new_len;
1371adb92db8SIlpo Järvinen }
1372b451e5d2SYuchung Cheng
1373b451e5d2SYuchung Cheng if (pkt_len >= skb->len && !in_sack)
1374b451e5d2SYuchung Cheng return 0;
1375b451e5d2SYuchung Cheng
137675c119afSEric Dumazet err = tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb,
137775c119afSEric Dumazet pkt_len, mss, GFP_ATOMIC);
1378d1935942SIlpo Järvinen if (err < 0)
1379d1935942SIlpo Järvinen return err;
1380d1935942SIlpo Järvinen }
1381d1935942SIlpo Järvinen
1382d1935942SIlpo Järvinen return in_sack;
1383d1935942SIlpo Järvinen }
1384d1935942SIlpo Järvinen
1385cc9a672eSNeal Cardwell /* Mark the given newly-SACKed range as such, adjusting counters and hints. */
tcp_sacktag_one(struct sock * sk,struct tcp_sacktag_state * state,u8 sacked,u32 start_seq,u32 end_seq,int dup_sack,int pcount,u64 xmit_time)1386cc9a672eSNeal Cardwell static u8 tcp_sacktag_one(struct sock *sk,
1387cc9a672eSNeal Cardwell struct tcp_sacktag_state *state, u8 sacked,
1388cc9a672eSNeal Cardwell u32 start_seq, u32 end_seq,
1389740b0f18SEric Dumazet int dup_sack, int pcount,
13909a568de4SEric Dumazet u64 xmit_time)
13919e10c47cSIlpo Järvinen {
13926859d494SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
13939e10c47cSIlpo Järvinen
13949e10c47cSIlpo Järvinen /* Account D-SACK for retransmitted packet. */
13959e10c47cSIlpo Järvinen if (dup_sack && (sacked & TCPCB_RETRANS)) {
13966e08d5e3SYuchung Cheng if (tp->undo_marker && tp->undo_retrans > 0 &&
1397cc9a672eSNeal Cardwell after(end_seq, tp->undo_marker))
13984f884f39Szhenggy tp->undo_retrans = max_t(int, 0, tp->undo_retrans - pcount);
1399737ff314SYuchung Cheng if ((sacked & TCPCB_SACKED_ACKED) &&
1400737ff314SYuchung Cheng before(start_seq, state->reord))
1401737ff314SYuchung Cheng state->reord = start_seq;
14029e10c47cSIlpo Järvinen }
14039e10c47cSIlpo Järvinen
14049e10c47cSIlpo Järvinen /* Nothing to do; acked frame is about to be dropped (was ACKed). */
1405cc9a672eSNeal Cardwell if (!after(end_seq, tp->snd_una))
1406a1197f5aSIlpo Järvinen return sacked;
14079e10c47cSIlpo Järvinen
14089e10c47cSIlpo Järvinen if (!(sacked & TCPCB_SACKED_ACKED)) {
1409d2329f10SEric Dumazet tcp_rack_advance(tp, sacked, end_seq, xmit_time);
1410659a8ad5SYuchung Cheng
14119e10c47cSIlpo Järvinen if (sacked & TCPCB_SACKED_RETRANS) {
14129e10c47cSIlpo Järvinen /* If the segment is not tagged as lost,
14139e10c47cSIlpo Järvinen * we do not clear RETRANS, believing
14149e10c47cSIlpo Järvinen * that retransmission is still in flight.
14159e10c47cSIlpo Järvinen */
14169e10c47cSIlpo Järvinen if (sacked & TCPCB_LOST) {
1417a1197f5aSIlpo Järvinen sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
1418f58b22fdSIlpo Järvinen tp->lost_out -= pcount;
1419f58b22fdSIlpo Järvinen tp->retrans_out -= pcount;
14209e10c47cSIlpo Järvinen }
14219e10c47cSIlpo Järvinen } else {
14229e10c47cSIlpo Järvinen if (!(sacked & TCPCB_RETRANS)) {
14239e10c47cSIlpo Järvinen /* New sack for not retransmitted frame,
14249e10c47cSIlpo Järvinen * which was in hole. It is reordering.
14259e10c47cSIlpo Järvinen */
1426cc9a672eSNeal Cardwell if (before(start_seq,
1427737ff314SYuchung Cheng tcp_highest_sack_seq(tp)) &&
1428737ff314SYuchung Cheng before(start_seq, state->reord))
1429737ff314SYuchung Cheng state->reord = start_seq;
1430737ff314SYuchung Cheng
1431e33099f9SYuchung Cheng if (!after(end_seq, tp->high_seq))
1432e33099f9SYuchung Cheng state->flag |= FLAG_ORIG_SACK_ACKED;
14339a568de4SEric Dumazet if (state->first_sackt == 0)
14349a568de4SEric Dumazet state->first_sackt = xmit_time;
14359a568de4SEric Dumazet state->last_sackt = xmit_time;
14369e10c47cSIlpo Järvinen }
14379e10c47cSIlpo Järvinen
14389e10c47cSIlpo Järvinen if (sacked & TCPCB_LOST) {
1439a1197f5aSIlpo Järvinen sacked &= ~TCPCB_LOST;
1440f58b22fdSIlpo Järvinen tp->lost_out -= pcount;
14419e10c47cSIlpo Järvinen }
14429e10c47cSIlpo Järvinen }
14439e10c47cSIlpo Järvinen
1444a1197f5aSIlpo Järvinen sacked |= TCPCB_SACKED_ACKED;
1445a1197f5aSIlpo Järvinen state->flag |= FLAG_DATA_SACKED;
1446f58b22fdSIlpo Järvinen tp->sacked_out += pcount;
1447082d4fa9SYousuk Seung /* Out-of-order packets delivered */
1448f00394ceSYousuk Seung state->sack_delivered += pcount;
14499e10c47cSIlpo Järvinen
14509e10c47cSIlpo Järvinen /* Lost marker hint past SACKed? Tweak RFC3517 cnt */
1451713bafeaSYuchung Cheng if (tp->lost_skb_hint &&
1452cc9a672eSNeal Cardwell before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))
1453f58b22fdSIlpo Järvinen tp->lost_cnt_hint += pcount;
14549e10c47cSIlpo Järvinen }
14559e10c47cSIlpo Järvinen
14569e10c47cSIlpo Järvinen /* D-SACK. We can detect redundant retransmission in S|R and plain R
14579e10c47cSIlpo Järvinen * frames and clear it. undo_retrans is decreased above, L|R frames
14589e10c47cSIlpo Järvinen * are accounted above as well.
14599e10c47cSIlpo Järvinen */
1460a1197f5aSIlpo Järvinen if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
1461a1197f5aSIlpo Järvinen sacked &= ~TCPCB_SACKED_RETRANS;
1462f58b22fdSIlpo Järvinen tp->retrans_out -= pcount;
14639e10c47cSIlpo Järvinen }
14649e10c47cSIlpo Järvinen
1465a1197f5aSIlpo Järvinen return sacked;
14669e10c47cSIlpo Järvinen }
14679e10c47cSIlpo Järvinen
1468daef52baSNeal Cardwell /* Shift newly-SACKed bytes from this skb to the immediately previous
1469daef52baSNeal Cardwell * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
1470daef52baSNeal Cardwell */
tcp_shifted_skb(struct sock * sk,struct sk_buff * prev,struct sk_buff * skb,struct tcp_sacktag_state * state,unsigned int pcount,int shifted,int mss,bool dup_sack)1471f3319816SEric Dumazet static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev,
1472f3319816SEric Dumazet struct sk_buff *skb,
1473a1197f5aSIlpo Järvinen struct tcp_sacktag_state *state,
14749ec06ff5SIlpo Järvinen unsigned int pcount, int shifted, int mss,
1475a2a385d6SEric Dumazet bool dup_sack)
1476832d11c5SIlpo Järvinen {
1477832d11c5SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
1478daef52baSNeal Cardwell u32 start_seq = TCP_SKB_CB(skb)->seq; /* start of newly-SACKed */
1479daef52baSNeal Cardwell u32 end_seq = start_seq + shifted; /* end of newly-SACKed */
1480832d11c5SIlpo Järvinen
1481832d11c5SIlpo Järvinen BUG_ON(!pcount);
1482832d11c5SIlpo Järvinen
14834c90d3b3SNeal Cardwell /* Adjust counters and hints for the newly sacked sequence
14844c90d3b3SNeal Cardwell * range but discard the return value since prev is already
14854c90d3b3SNeal Cardwell * marked. We must tag the range first because the seq
14864c90d3b3SNeal Cardwell * advancement below implicitly advances
14874c90d3b3SNeal Cardwell * tcp_highest_sack_seq() when skb is highest_sack.
14884c90d3b3SNeal Cardwell */
14894c90d3b3SNeal Cardwell tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked,
149059c9af42SYuchung Cheng start_seq, end_seq, dup_sack, pcount,
14912fd66ffbSEric Dumazet tcp_skb_timestamp_us(skb));
1492b9f64820SYuchung Cheng tcp_rate_skb_delivered(sk, skb, state->rate);
14934c90d3b3SNeal Cardwell
14944c90d3b3SNeal Cardwell if (skb == tp->lost_skb_hint)
14950af2a0d0SNeal Cardwell tp->lost_cnt_hint += pcount;
14960af2a0d0SNeal Cardwell
1497832d11c5SIlpo Järvinen TCP_SKB_CB(prev)->end_seq += shifted;
1498832d11c5SIlpo Järvinen TCP_SKB_CB(skb)->seq += shifted;
1499832d11c5SIlpo Järvinen
1500cd7d8498SEric Dumazet tcp_skb_pcount_add(prev, pcount);
15013b4929f6SEric Dumazet WARN_ON_ONCE(tcp_skb_pcount(skb) < pcount);
1502cd7d8498SEric Dumazet tcp_skb_pcount_add(skb, -pcount);
1503832d11c5SIlpo Järvinen
1504832d11c5SIlpo Järvinen /* When we're adding to gso_segs == 1, gso_size will be zero,
1505832d11c5SIlpo Järvinen * in theory this shouldn't be necessary but as long as DSACK
1506832d11c5SIlpo Järvinen * code can come after this skb later on it's better to keep
1507832d11c5SIlpo Järvinen * setting gso_size to something.
1508832d11c5SIlpo Järvinen */
1509f69ad292SEric Dumazet if (!TCP_SKB_CB(prev)->tcp_gso_size)
1510f69ad292SEric Dumazet TCP_SKB_CB(prev)->tcp_gso_size = mss;
1511832d11c5SIlpo Järvinen
1512832d11c5SIlpo Järvinen /* CHECKME: To clear or not to clear? Mimics normal skb currently */
151351466a75SEric Dumazet if (tcp_skb_pcount(skb) <= 1)
1514f69ad292SEric Dumazet TCP_SKB_CB(skb)->tcp_gso_size = 0;
1515832d11c5SIlpo Järvinen
1516832d11c5SIlpo Järvinen /* Difference in this won't matter, both ACKed by the same cumul. ACK */
1517832d11c5SIlpo Järvinen TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
1518832d11c5SIlpo Järvinen
1519832d11c5SIlpo Järvinen if (skb->len > 0) {
1520832d11c5SIlpo Järvinen BUG_ON(!tcp_skb_pcount(skb));
1521c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTED);
1522a2a385d6SEric Dumazet return false;
1523832d11c5SIlpo Järvinen }
1524832d11c5SIlpo Järvinen
1525832d11c5SIlpo Järvinen /* Whole SKB was eaten :-) */
1526832d11c5SIlpo Järvinen
152792ee76b6SIlpo Järvinen if (skb == tp->retransmit_skb_hint)
152892ee76b6SIlpo Järvinen tp->retransmit_skb_hint = prev;
152992ee76b6SIlpo Järvinen if (skb == tp->lost_skb_hint) {
153092ee76b6SIlpo Järvinen tp->lost_skb_hint = prev;
153192ee76b6SIlpo Järvinen tp->lost_cnt_hint -= tcp_skb_pcount(prev);
153292ee76b6SIlpo Järvinen }
153392ee76b6SIlpo Järvinen
15345e8a402fSEric Dumazet TCP_SKB_CB(prev)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
1535a643b5d4SMartin KaFai Lau TCP_SKB_CB(prev)->eor = TCP_SKB_CB(skb)->eor;
15365e8a402fSEric Dumazet if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
15375e8a402fSEric Dumazet TCP_SKB_CB(prev)->end_seq++;
15385e8a402fSEric Dumazet
1539832d11c5SIlpo Järvinen if (skb == tcp_highest_sack(sk))
1540832d11c5SIlpo Järvinen tcp_advance_highest_sack(sk, skb);
1541832d11c5SIlpo Järvinen
1542cfea5a68SMartin KaFai Lau tcp_skb_collapse_tstamp(prev, skb);
15439a568de4SEric Dumazet if (unlikely(TCP_SKB_CB(prev)->tx.delivered_mstamp))
15449a568de4SEric Dumazet TCP_SKB_CB(prev)->tx.delivered_mstamp = 0;
1545b9f64820SYuchung Cheng
154675c119afSEric Dumazet tcp_rtx_queue_unlink_and_free(skb, sk);
1547832d11c5SIlpo Järvinen
1548c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKMERGED);
1549111cc8b9SIlpo Järvinen
1550a2a385d6SEric Dumazet return true;
1551832d11c5SIlpo Järvinen }
1552832d11c5SIlpo Järvinen
1553832d11c5SIlpo Järvinen /* I wish gso_size would have a bit more sane initialization than
1554832d11c5SIlpo Järvinen * something-or-zero which complicates things
1555832d11c5SIlpo Järvinen */
tcp_skb_seglen(const struct sk_buff * skb)1556cf533ea5SEric Dumazet static int tcp_skb_seglen(const struct sk_buff *skb)
1557832d11c5SIlpo Järvinen {
1558775ffabfSIlpo Järvinen return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
1559832d11c5SIlpo Järvinen }
1560832d11c5SIlpo Järvinen
1561832d11c5SIlpo Järvinen /* Shifting pages past head area doesn't work */
skb_can_shift(const struct sk_buff * skb)1562cf533ea5SEric Dumazet static int skb_can_shift(const struct sk_buff *skb)
1563832d11c5SIlpo Järvinen {
1564832d11c5SIlpo Järvinen return !skb_headlen(skb) && skb_is_nonlinear(skb);
1565832d11c5SIlpo Järvinen }
1566832d11c5SIlpo Järvinen
tcp_skb_shift(struct sk_buff * to,struct sk_buff * from,int pcount,int shiftlen)15673b4929f6SEric Dumazet int tcp_skb_shift(struct sk_buff *to, struct sk_buff *from,
15683b4929f6SEric Dumazet int pcount, int shiftlen)
15693b4929f6SEric Dumazet {
15703b4929f6SEric Dumazet /* TCP min gso_size is 8 bytes (TCP_MIN_GSO_SIZE)
15713b4929f6SEric Dumazet * Since TCP_SKB_CB(skb)->tcp_gso_segs is 16 bits, we need
15723b4929f6SEric Dumazet * to make sure not storing more than 65535 * 8 bytes per skb,
15733b4929f6SEric Dumazet * even if current MSS is bigger.
15743b4929f6SEric Dumazet */
15753b4929f6SEric Dumazet if (unlikely(to->len + shiftlen >= 65535 * TCP_MIN_GSO_SIZE))
15763b4929f6SEric Dumazet return 0;
15773b4929f6SEric Dumazet if (unlikely(tcp_skb_pcount(to) + pcount > 65535))
15783b4929f6SEric Dumazet return 0;
15793b4929f6SEric Dumazet return skb_shift(to, from, shiftlen);
15803b4929f6SEric Dumazet }
15813b4929f6SEric Dumazet
1582832d11c5SIlpo Järvinen /* Try collapsing SACK blocks spanning across multiple skbs to a single
1583832d11c5SIlpo Järvinen * skb.
1584832d11c5SIlpo Järvinen */
tcp_shift_skb_data(struct sock * sk,struct sk_buff * skb,struct tcp_sacktag_state * state,u32 start_seq,u32 end_seq,bool dup_sack)1585832d11c5SIlpo Järvinen static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
1586a1197f5aSIlpo Järvinen struct tcp_sacktag_state *state,
1587832d11c5SIlpo Järvinen u32 start_seq, u32 end_seq,
1588a2a385d6SEric Dumazet bool dup_sack)
1589832d11c5SIlpo Järvinen {
1590832d11c5SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
1591832d11c5SIlpo Järvinen struct sk_buff *prev;
1592832d11c5SIlpo Järvinen int mss;
1593832d11c5SIlpo Järvinen int pcount = 0;
1594832d11c5SIlpo Järvinen int len;
1595832d11c5SIlpo Järvinen int in_sack;
1596832d11c5SIlpo Järvinen
1597832d11c5SIlpo Järvinen /* Normally R but no L won't result in plain S */
1598832d11c5SIlpo Järvinen if (!dup_sack &&
15999969ca5fSIlpo Järvinen (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
1600832d11c5SIlpo Järvinen goto fallback;
1601832d11c5SIlpo Järvinen if (!skb_can_shift(skb))
1602832d11c5SIlpo Järvinen goto fallback;
1603832d11c5SIlpo Järvinen /* This frame is about to be dropped (was ACKed). */
1604832d11c5SIlpo Järvinen if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
1605832d11c5SIlpo Järvinen goto fallback;
1606832d11c5SIlpo Järvinen
1607832d11c5SIlpo Järvinen /* Can only happen with delayed DSACK + discard craziness */
160875c119afSEric Dumazet prev = skb_rb_prev(skb);
160975c119afSEric Dumazet if (!prev)
1610832d11c5SIlpo Järvinen goto fallback;
1611832d11c5SIlpo Järvinen
1612832d11c5SIlpo Järvinen if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
1613832d11c5SIlpo Järvinen goto fallback;
1614832d11c5SIlpo Järvinen
161585712484SMat Martineau if (!tcp_skb_can_collapse(prev, skb))
1616a643b5d4SMartin KaFai Lau goto fallback;
1617a643b5d4SMartin KaFai Lau
1618832d11c5SIlpo Järvinen in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
1619832d11c5SIlpo Järvinen !before(end_seq, TCP_SKB_CB(skb)->end_seq);
1620832d11c5SIlpo Järvinen
1621832d11c5SIlpo Järvinen if (in_sack) {
1622832d11c5SIlpo Järvinen len = skb->len;
1623832d11c5SIlpo Järvinen pcount = tcp_skb_pcount(skb);
1624775ffabfSIlpo Järvinen mss = tcp_skb_seglen(skb);
1625832d11c5SIlpo Järvinen
1626832d11c5SIlpo Järvinen /* TODO: Fix DSACKs to not fragment already SACKed and we can
1627832d11c5SIlpo Järvinen * drop this restriction as unnecessary
1628832d11c5SIlpo Järvinen */
1629775ffabfSIlpo Järvinen if (mss != tcp_skb_seglen(prev))
1630832d11c5SIlpo Järvinen goto fallback;
1631832d11c5SIlpo Järvinen } else {
1632832d11c5SIlpo Järvinen if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
1633832d11c5SIlpo Järvinen goto noop;
1634832d11c5SIlpo Järvinen /* CHECKME: This is non-MSS split case only?, this will
1635832d11c5SIlpo Järvinen * cause skipped skbs due to advancing loop btw, original
1636832d11c5SIlpo Järvinen * has that feature too
1637832d11c5SIlpo Järvinen */
1638832d11c5SIlpo Järvinen if (tcp_skb_pcount(skb) <= 1)
1639832d11c5SIlpo Järvinen goto noop;
1640832d11c5SIlpo Järvinen
1641832d11c5SIlpo Järvinen in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
1642832d11c5SIlpo Järvinen if (!in_sack) {
1643832d11c5SIlpo Järvinen /* TODO: head merge to next could be attempted here
1644832d11c5SIlpo Järvinen * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
1645832d11c5SIlpo Järvinen * though it might not be worth of the additional hassle
1646832d11c5SIlpo Järvinen *
1647832d11c5SIlpo Järvinen * ...we can probably just fallback to what was done
1648832d11c5SIlpo Järvinen * previously. We could try merging non-SACKed ones
1649832d11c5SIlpo Järvinen * as well but it probably isn't going to buy off
1650832d11c5SIlpo Järvinen * because later SACKs might again split them, and
1651832d11c5SIlpo Järvinen * it would make skb timestamp tracking considerably
1652832d11c5SIlpo Järvinen * harder problem.
1653832d11c5SIlpo Järvinen */
1654832d11c5SIlpo Järvinen goto fallback;
1655832d11c5SIlpo Järvinen }
1656832d11c5SIlpo Järvinen
1657832d11c5SIlpo Järvinen len = end_seq - TCP_SKB_CB(skb)->seq;
1658832d11c5SIlpo Järvinen BUG_ON(len < 0);
1659832d11c5SIlpo Järvinen BUG_ON(len > skb->len);
1660832d11c5SIlpo Järvinen
1661832d11c5SIlpo Järvinen /* MSS boundaries should be honoured or else pcount will
1662832d11c5SIlpo Järvinen * severely break even though it makes things bit trickier.
1663832d11c5SIlpo Järvinen * Optimize common case to avoid most of the divides
1664832d11c5SIlpo Järvinen */
1665832d11c5SIlpo Järvinen mss = tcp_skb_mss(skb);
1666832d11c5SIlpo Järvinen
1667832d11c5SIlpo Järvinen /* TODO: Fix DSACKs to not fragment already SACKed and we can
1668832d11c5SIlpo Järvinen * drop this restriction as unnecessary
1669832d11c5SIlpo Järvinen */
1670775ffabfSIlpo Järvinen if (mss != tcp_skb_seglen(prev))
1671832d11c5SIlpo Järvinen goto fallback;
1672832d11c5SIlpo Järvinen
1673832d11c5SIlpo Järvinen if (len == mss) {
1674832d11c5SIlpo Järvinen pcount = 1;
1675832d11c5SIlpo Järvinen } else if (len < mss) {
1676832d11c5SIlpo Järvinen goto noop;
1677832d11c5SIlpo Järvinen } else {
1678832d11c5SIlpo Järvinen pcount = len / mss;
1679832d11c5SIlpo Järvinen len = pcount * mss;
1680832d11c5SIlpo Järvinen }
1681832d11c5SIlpo Järvinen }
1682832d11c5SIlpo Järvinen
16834648dc97SNeal Cardwell /* tcp_sacktag_one() won't SACK-tag ranges below snd_una */
16844648dc97SNeal Cardwell if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
16854648dc97SNeal Cardwell goto fallback;
16864648dc97SNeal Cardwell
16873b4929f6SEric Dumazet if (!tcp_skb_shift(prev, skb, pcount, len))
1688832d11c5SIlpo Järvinen goto fallback;
1689f3319816SEric Dumazet if (!tcp_shifted_skb(sk, prev, skb, state, pcount, len, mss, dup_sack))
1690832d11c5SIlpo Järvinen goto out;
1691832d11c5SIlpo Järvinen
1692832d11c5SIlpo Järvinen /* Hole filled allows collapsing with the next as well, this is very
1693832d11c5SIlpo Järvinen * useful when hole on every nth skb pattern happens
1694832d11c5SIlpo Järvinen */
169575c119afSEric Dumazet skb = skb_rb_next(prev);
169675c119afSEric Dumazet if (!skb)
1697832d11c5SIlpo Järvinen goto out;
1698832d11c5SIlpo Järvinen
1699f0bc52f3SIlpo Järvinen if (!skb_can_shift(skb) ||
1700f0bc52f3SIlpo Järvinen ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
1701775ffabfSIlpo Järvinen (mss != tcp_skb_seglen(skb)))
1702832d11c5SIlpo Järvinen goto out;
1703832d11c5SIlpo Järvinen
1704b67985beSEric Dumazet if (!tcp_skb_can_collapse(prev, skb))
1705b67985beSEric Dumazet goto out;
1706832d11c5SIlpo Järvinen len = skb->len;
17073b4929f6SEric Dumazet pcount = tcp_skb_pcount(skb);
17083b4929f6SEric Dumazet if (tcp_skb_shift(prev, skb, pcount, len))
17093b4929f6SEric Dumazet tcp_shifted_skb(sk, prev, skb, state, pcount,
1710f3319816SEric Dumazet len, mss, 0);
1711832d11c5SIlpo Järvinen
1712832d11c5SIlpo Järvinen out:
1713832d11c5SIlpo Järvinen return prev;
1714832d11c5SIlpo Järvinen
1715832d11c5SIlpo Järvinen noop:
1716832d11c5SIlpo Järvinen return skb;
1717832d11c5SIlpo Järvinen
1718832d11c5SIlpo Järvinen fallback:
1719c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
1720832d11c5SIlpo Järvinen return NULL;
1721832d11c5SIlpo Järvinen }
1722832d11c5SIlpo Järvinen
tcp_sacktag_walk(struct sk_buff * skb,struct sock * sk,struct tcp_sack_block * next_dup,struct tcp_sacktag_state * state,u32 start_seq,u32 end_seq,bool dup_sack_in)172368f8353bSIlpo Järvinen static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
172468f8353bSIlpo Järvinen struct tcp_sack_block *next_dup,
1725a1197f5aSIlpo Järvinen struct tcp_sacktag_state *state,
172668f8353bSIlpo Järvinen u32 start_seq, u32 end_seq,
1727a2a385d6SEric Dumazet bool dup_sack_in)
172868f8353bSIlpo Järvinen {
1729832d11c5SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
1730832d11c5SIlpo Järvinen struct sk_buff *tmp;
1731832d11c5SIlpo Järvinen
173275c119afSEric Dumazet skb_rbtree_walk_from(skb) {
173368f8353bSIlpo Järvinen int in_sack = 0;
1734a2a385d6SEric Dumazet bool dup_sack = dup_sack_in;
173568f8353bSIlpo Järvinen
173668f8353bSIlpo Järvinen /* queue is in-order => we can short-circuit the walk early */
173768f8353bSIlpo Järvinen if (!before(TCP_SKB_CB(skb)->seq, end_seq))
173868f8353bSIlpo Järvinen break;
173968f8353bSIlpo Järvinen
174000db4124SIan Morris if (next_dup &&
174168f8353bSIlpo Järvinen before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
174268f8353bSIlpo Järvinen in_sack = tcp_match_skb_to_sack(sk, skb,
174368f8353bSIlpo Järvinen next_dup->start_seq,
174468f8353bSIlpo Järvinen next_dup->end_seq);
174568f8353bSIlpo Järvinen if (in_sack > 0)
1746a2a385d6SEric Dumazet dup_sack = true;
174768f8353bSIlpo Järvinen }
174868f8353bSIlpo Järvinen
1749832d11c5SIlpo Järvinen /* skb reference here is a bit tricky to get right, since
1750832d11c5SIlpo Järvinen * shifting can eat and free both this skb and the next,
1751832d11c5SIlpo Järvinen * so not even _safe variant of the loop is enough.
1752832d11c5SIlpo Järvinen */
1753832d11c5SIlpo Järvinen if (in_sack <= 0) {
1754a1197f5aSIlpo Järvinen tmp = tcp_shift_skb_data(sk, skb, state,
1755a1197f5aSIlpo Järvinen start_seq, end_seq, dup_sack);
175600db4124SIan Morris if (tmp) {
1757832d11c5SIlpo Järvinen if (tmp != skb) {
1758832d11c5SIlpo Järvinen skb = tmp;
1759832d11c5SIlpo Järvinen continue;
1760832d11c5SIlpo Järvinen }
1761832d11c5SIlpo Järvinen
1762832d11c5SIlpo Järvinen in_sack = 0;
1763832d11c5SIlpo Järvinen } else {
1764832d11c5SIlpo Järvinen in_sack = tcp_match_skb_to_sack(sk, skb,
1765832d11c5SIlpo Järvinen start_seq,
1766056834d9SIlpo Järvinen end_seq);
1767832d11c5SIlpo Järvinen }
1768832d11c5SIlpo Järvinen }
1769832d11c5SIlpo Järvinen
177068f8353bSIlpo Järvinen if (unlikely(in_sack < 0))
177168f8353bSIlpo Järvinen break;
177268f8353bSIlpo Järvinen
1773832d11c5SIlpo Järvinen if (in_sack) {
1774cc9a672eSNeal Cardwell TCP_SKB_CB(skb)->sacked =
1775cc9a672eSNeal Cardwell tcp_sacktag_one(sk,
1776a1197f5aSIlpo Järvinen state,
1777cc9a672eSNeal Cardwell TCP_SKB_CB(skb)->sacked,
1778cc9a672eSNeal Cardwell TCP_SKB_CB(skb)->seq,
1779cc9a672eSNeal Cardwell TCP_SKB_CB(skb)->end_seq,
1780a1197f5aSIlpo Järvinen dup_sack,
178159c9af42SYuchung Cheng tcp_skb_pcount(skb),
17822fd66ffbSEric Dumazet tcp_skb_timestamp_us(skb));
1783b9f64820SYuchung Cheng tcp_rate_skb_delivered(sk, skb, state->rate);
1784e2080072SEric Dumazet if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
1785e2080072SEric Dumazet list_del_init(&skb->tcp_tsorted_anchor);
178668f8353bSIlpo Järvinen
1787832d11c5SIlpo Järvinen if (!before(TCP_SKB_CB(skb)->seq,
1788832d11c5SIlpo Järvinen tcp_highest_sack_seq(tp)))
1789832d11c5SIlpo Järvinen tcp_advance_highest_sack(sk, skb);
1790832d11c5SIlpo Järvinen }
179168f8353bSIlpo Järvinen }
179268f8353bSIlpo Järvinen return skb;
179368f8353bSIlpo Järvinen }
179468f8353bSIlpo Järvinen
tcp_sacktag_bsearch(struct sock * sk,u32 seq)17954bfabc46STaehee Yoo static struct sk_buff *tcp_sacktag_bsearch(struct sock *sk, u32 seq)
179675c119afSEric Dumazet {
179775c119afSEric Dumazet struct rb_node *parent, **p = &sk->tcp_rtx_queue.rb_node;
179875c119afSEric Dumazet struct sk_buff *skb;
179975c119afSEric Dumazet
180075c119afSEric Dumazet while (*p) {
180175c119afSEric Dumazet parent = *p;
180275c119afSEric Dumazet skb = rb_to_skb(parent);
180375c119afSEric Dumazet if (before(seq, TCP_SKB_CB(skb)->seq)) {
180475c119afSEric Dumazet p = &parent->rb_left;
180575c119afSEric Dumazet continue;
180675c119afSEric Dumazet }
180775c119afSEric Dumazet if (!before(seq, TCP_SKB_CB(skb)->end_seq)) {
180875c119afSEric Dumazet p = &parent->rb_right;
180975c119afSEric Dumazet continue;
181075c119afSEric Dumazet }
181175c119afSEric Dumazet return skb;
181275c119afSEric Dumazet }
181375c119afSEric Dumazet return NULL;
181475c119afSEric Dumazet }
181575c119afSEric Dumazet
tcp_sacktag_skip(struct sk_buff * skb,struct sock * sk,u32 skip_to_seq)181668f8353bSIlpo Järvinen static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
1817a1197f5aSIlpo Järvinen u32 skip_to_seq)
181868f8353bSIlpo Järvinen {
181975c119afSEric Dumazet if (skb && after(TCP_SKB_CB(skb)->seq, skip_to_seq))
182068f8353bSIlpo Järvinen return skb;
182175c119afSEric Dumazet
18224bfabc46STaehee Yoo return tcp_sacktag_bsearch(sk, skip_to_seq);
182368f8353bSIlpo Järvinen }
182468f8353bSIlpo Järvinen
tcp_maybe_skipping_dsack(struct sk_buff * skb,struct sock * sk,struct tcp_sack_block * next_dup,struct tcp_sacktag_state * state,u32 skip_to_seq)182568f8353bSIlpo Järvinen static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
182668f8353bSIlpo Järvinen struct sock *sk,
182768f8353bSIlpo Järvinen struct tcp_sack_block *next_dup,
1828a1197f5aSIlpo Järvinen struct tcp_sacktag_state *state,
1829a1197f5aSIlpo Järvinen u32 skip_to_seq)
183068f8353bSIlpo Järvinen {
183151456b29SIan Morris if (!next_dup)
183268f8353bSIlpo Järvinen return skb;
183368f8353bSIlpo Järvinen
183468f8353bSIlpo Järvinen if (before(next_dup->start_seq, skip_to_seq)) {
18354bfabc46STaehee Yoo skb = tcp_sacktag_skip(skb, sk, next_dup->start_seq);
1836a1197f5aSIlpo Järvinen skb = tcp_sacktag_walk(skb, sk, NULL, state,
183768f8353bSIlpo Järvinen next_dup->start_seq, next_dup->end_seq,
1838a1197f5aSIlpo Järvinen 1);
183968f8353bSIlpo Järvinen }
184068f8353bSIlpo Järvinen
184168f8353bSIlpo Järvinen return skb;
184268f8353bSIlpo Järvinen }
184368f8353bSIlpo Järvinen
tcp_sack_cache_ok(const struct tcp_sock * tp,const struct tcp_sack_block * cache)1844cf533ea5SEric Dumazet static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
184568f8353bSIlpo Järvinen {
184668f8353bSIlpo Järvinen return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
184768f8353bSIlpo Järvinen }
184868f8353bSIlpo Järvinen
18491da177e4SLinus Torvalds static int
tcp_sacktag_write_queue(struct sock * sk,const struct sk_buff * ack_skb,u32 prior_snd_una,struct tcp_sacktag_state * state)1850cf533ea5SEric Dumazet tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
1851196da974SKenneth Klette Jonassen u32 prior_snd_una, struct tcp_sacktag_state *state)
18521da177e4SLinus Torvalds {
18531da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
1854cf533ea5SEric Dumazet const unsigned char *ptr = (skb_transport_header(ack_skb) +
18559c70220bSArnaldo Carvalho de Melo TCP_SKB_CB(ack_skb)->sacked);
1856fd6dad61SIlpo Järvinen struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
18574389ddedSAdam Langley struct tcp_sack_block sp[TCP_NUM_SACKS];
185868f8353bSIlpo Järvinen struct tcp_sack_block *cache;
185968f8353bSIlpo Järvinen struct sk_buff *skb;
18604389ddedSAdam Langley int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
1861fd6dad61SIlpo Järvinen int used_sacks;
1862a2a385d6SEric Dumazet bool found_dup_sack = false;
186368f8353bSIlpo Järvinen int i, j;
1864fda03fbbSBaruch Even int first_sack_index;
18651da177e4SLinus Torvalds
1866196da974SKenneth Klette Jonassen state->flag = 0;
1867737ff314SYuchung Cheng state->reord = tp->snd_nxt;
1868a1197f5aSIlpo Järvinen
1869737ff314SYuchung Cheng if (!tp->sacked_out)
18706859d494SIlpo Järvinen tcp_highest_sack_reset(sk);
18711da177e4SLinus Torvalds
18721ed83465SPavel Emelyanov found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
1873a71d77e6SPriyaranjan Jha num_sacks, prior_snd_una, state);
18746f74651aSBaruch Even
18756f74651aSBaruch Even /* Eliminate too old ACKs, but take into
18766f74651aSBaruch Even * account more or less fresh ones, they can
18776f74651aSBaruch Even * contain valid SACK info.
18786f74651aSBaruch Even */
18796f74651aSBaruch Even if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
18806f74651aSBaruch Even return 0;
18816f74651aSBaruch Even
188296a2d41aSIlpo Järvinen if (!tp->packets_out)
188396a2d41aSIlpo Järvinen goto out;
188496a2d41aSIlpo Järvinen
1885fd6dad61SIlpo Järvinen used_sacks = 0;
1886fd6dad61SIlpo Järvinen first_sack_index = 0;
1887fd6dad61SIlpo Järvinen for (i = 0; i < num_sacks; i++) {
1888a2a385d6SEric Dumazet bool dup_sack = !i && found_dup_sack;
1889fd6dad61SIlpo Järvinen
1890d3e2ce3bSHarvey Harrison sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq);
1891d3e2ce3bSHarvey Harrison sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq);
1892fd6dad61SIlpo Järvinen
1893fd6dad61SIlpo Järvinen if (!tcp_is_sackblock_valid(tp, dup_sack,
1894fd6dad61SIlpo Järvinen sp[used_sacks].start_seq,
1895fd6dad61SIlpo Järvinen sp[used_sacks].end_seq)) {
189640b215e5SPavel Emelyanov int mib_idx;
189740b215e5SPavel Emelyanov
1898fd6dad61SIlpo Järvinen if (dup_sack) {
1899fd6dad61SIlpo Järvinen if (!tp->undo_marker)
190040b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO;
1901fd6dad61SIlpo Järvinen else
190240b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD;
1903fd6dad61SIlpo Järvinen } else {
1904fd6dad61SIlpo Järvinen /* Don't count olds caused by ACK reordering */
1905fd6dad61SIlpo Järvinen if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) &&
1906fd6dad61SIlpo Järvinen !after(sp[used_sacks].end_seq, tp->snd_una))
1907fd6dad61SIlpo Järvinen continue;
190840b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPSACKDISCARD;
1909fd6dad61SIlpo Järvinen }
191040b215e5SPavel Emelyanov
1911c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), mib_idx);
1912fd6dad61SIlpo Järvinen if (i == 0)
1913fd6dad61SIlpo Järvinen first_sack_index = -1;
1914fd6dad61SIlpo Järvinen continue;
1915fd6dad61SIlpo Järvinen }
1916fd6dad61SIlpo Järvinen
1917fd6dad61SIlpo Järvinen /* Ignore very old stuff early */
1918c9655008SPengcheng Yang if (!after(sp[used_sacks].end_seq, prior_snd_una)) {
1919c9655008SPengcheng Yang if (i == 0)
1920c9655008SPengcheng Yang first_sack_index = -1;
1921fd6dad61SIlpo Järvinen continue;
1922c9655008SPengcheng Yang }
1923fd6dad61SIlpo Järvinen
1924fd6dad61SIlpo Järvinen used_sacks++;
1925fd6dad61SIlpo Järvinen }
1926fd6dad61SIlpo Järvinen
19276a438bbeSStephen Hemminger /* order SACK blocks to allow in order walk of the retrans queue */
1928fd6dad61SIlpo Järvinen for (i = used_sacks - 1; i > 0; i--) {
19296a438bbeSStephen Hemminger for (j = 0; j < i; j++) {
1930fd6dad61SIlpo Järvinen if (after(sp[j].start_seq, sp[j + 1].start_seq)) {
1931a0bffffcSIlpo Järvinen swap(sp[j], sp[j + 1]);
1932fda03fbbSBaruch Even
1933fda03fbbSBaruch Even /* Track where the first SACK block goes to */
1934fda03fbbSBaruch Even if (j == first_sack_index)
1935fda03fbbSBaruch Even first_sack_index = j + 1;
19366a438bbeSStephen Hemminger }
19376a438bbeSStephen Hemminger }
19386a438bbeSStephen Hemminger }
19396a438bbeSStephen Hemminger
194075c119afSEric Dumazet state->mss_now = tcp_current_mss(sk);
194175c119afSEric Dumazet skb = NULL;
194268f8353bSIlpo Järvinen i = 0;
194368f8353bSIlpo Järvinen
194468f8353bSIlpo Järvinen if (!tp->sacked_out) {
194568f8353bSIlpo Järvinen /* It's already past, so skip checking against it */
194668f8353bSIlpo Järvinen cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
194768f8353bSIlpo Järvinen } else {
194868f8353bSIlpo Järvinen cache = tp->recv_sack_cache;
194968f8353bSIlpo Järvinen /* Skip empty blocks in at head of the cache */
195068f8353bSIlpo Järvinen while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq &&
195168f8353bSIlpo Järvinen !cache->end_seq)
195268f8353bSIlpo Järvinen cache++;
1953fda03fbbSBaruch Even }
1954fda03fbbSBaruch Even
195568f8353bSIlpo Järvinen while (i < used_sacks) {
1956fd6dad61SIlpo Järvinen u32 start_seq = sp[i].start_seq;
1957fd6dad61SIlpo Järvinen u32 end_seq = sp[i].end_seq;
1958a2a385d6SEric Dumazet bool dup_sack = (found_dup_sack && (i == first_sack_index));
195968f8353bSIlpo Järvinen struct tcp_sack_block *next_dup = NULL;
1960e56d6cd6SIlpo Järvinen
196168f8353bSIlpo Järvinen if (found_dup_sack && ((i + 1) == first_sack_index))
196268f8353bSIlpo Järvinen next_dup = &sp[i + 1];
19631da177e4SLinus Torvalds
196468f8353bSIlpo Järvinen /* Skip too early cached blocks */
196568f8353bSIlpo Järvinen while (tcp_sack_cache_ok(tp, cache) &&
196668f8353bSIlpo Järvinen !before(start_seq, cache->end_seq))
196768f8353bSIlpo Järvinen cache++;
19681da177e4SLinus Torvalds
196968f8353bSIlpo Järvinen /* Can skip some work by looking recv_sack_cache? */
197068f8353bSIlpo Järvinen if (tcp_sack_cache_ok(tp, cache) && !dup_sack &&
197168f8353bSIlpo Järvinen after(end_seq, cache->start_seq)) {
1972fe067e8aSDavid S. Miller
197368f8353bSIlpo Järvinen /* Head todo? */
197468f8353bSIlpo Järvinen if (before(start_seq, cache->start_seq)) {
19754bfabc46STaehee Yoo skb = tcp_sacktag_skip(skb, sk, start_seq);
1976056834d9SIlpo Järvinen skb = tcp_sacktag_walk(skb, sk, next_dup,
1977196da974SKenneth Klette Jonassen state,
1978056834d9SIlpo Järvinen start_seq,
1979056834d9SIlpo Järvinen cache->start_seq,
1980a1197f5aSIlpo Järvinen dup_sack);
1981fda03fbbSBaruch Even }
19826a438bbeSStephen Hemminger
198368f8353bSIlpo Järvinen /* Rest of the block already fully processed? */
198420de20beSIlpo Järvinen if (!after(end_seq, cache->end_seq))
198520de20beSIlpo Järvinen goto advance_sp;
198620de20beSIlpo Järvinen
1987056834d9SIlpo Järvinen skb = tcp_maybe_skipping_dsack(skb, sk, next_dup,
1988196da974SKenneth Klette Jonassen state,
1989a1197f5aSIlpo Järvinen cache->end_seq);
199068f8353bSIlpo Järvinen
199168f8353bSIlpo Järvinen /* ...tail remains todo... */
19926859d494SIlpo Järvinen if (tcp_highest_sack_seq(tp) == cache->end_seq) {
199320de20beSIlpo Järvinen /* ...but better entrypoint exists! */
19946859d494SIlpo Järvinen skb = tcp_highest_sack(sk);
199551456b29SIan Morris if (!skb)
19966859d494SIlpo Järvinen break;
199768f8353bSIlpo Järvinen cache++;
199868f8353bSIlpo Järvinen goto walk;
1999e56d6cd6SIlpo Järvinen }
2000e56d6cd6SIlpo Järvinen
20014bfabc46STaehee Yoo skb = tcp_sacktag_skip(skb, sk, cache->end_seq);
200268f8353bSIlpo Järvinen /* Check overlap against next cached too (past this one already) */
200368f8353bSIlpo Järvinen cache++;
200468f8353bSIlpo Järvinen continue;
20051da177e4SLinus Torvalds }
2006fbd52eb2SIlpo Järvinen
20076859d494SIlpo Järvinen if (!before(start_seq, tcp_highest_sack_seq(tp))) {
20086859d494SIlpo Järvinen skb = tcp_highest_sack(sk);
200951456b29SIan Morris if (!skb)
20106859d494SIlpo Järvinen break;
201168f8353bSIlpo Järvinen }
20124bfabc46STaehee Yoo skb = tcp_sacktag_skip(skb, sk, start_seq);
201368f8353bSIlpo Järvinen
201468f8353bSIlpo Järvinen walk:
2015196da974SKenneth Klette Jonassen skb = tcp_sacktag_walk(skb, sk, next_dup, state,
2016a1197f5aSIlpo Järvinen start_seq, end_seq, dup_sack);
201768f8353bSIlpo Järvinen
201868f8353bSIlpo Järvinen advance_sp:
201968f8353bSIlpo Järvinen i++;
20201da177e4SLinus Torvalds }
20211da177e4SLinus Torvalds
202268f8353bSIlpo Järvinen /* Clear the head of the cache sack blocks so we can skip it next time */
202368f8353bSIlpo Järvinen for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) {
202468f8353bSIlpo Järvinen tp->recv_sack_cache[i].start_seq = 0;
202568f8353bSIlpo Järvinen tp->recv_sack_cache[i].end_seq = 0;
202668f8353bSIlpo Järvinen }
202768f8353bSIlpo Järvinen for (j = 0; j < used_sacks; j++)
202868f8353bSIlpo Järvinen tp->recv_sack_cache[i++] = sp[j];
202968f8353bSIlpo Järvinen
2030737ff314SYuchung Cheng if (inet_csk(sk)->icsk_ca_state != TCP_CA_Loss || tp->undo_marker)
2031737ff314SYuchung Cheng tcp_check_sack_reordering(sk, state->reord, 0);
20321da177e4SLinus Torvalds
20339dac8835SYuchung Cheng tcp_verify_left_out(tp);
203496a2d41aSIlpo Järvinen out:
203596a2d41aSIlpo Järvinen
20361da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0
2037547b792cSIlpo Järvinen WARN_ON((int)tp->sacked_out < 0);
2038547b792cSIlpo Järvinen WARN_ON((int)tp->lost_out < 0);
2039547b792cSIlpo Järvinen WARN_ON((int)tp->retrans_out < 0);
2040547b792cSIlpo Järvinen WARN_ON((int)tcp_packets_in_flight(tp) < 0);
20411da177e4SLinus Torvalds #endif
2042196da974SKenneth Klette Jonassen return state->flag;
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds
2045882bebaaSIlpo Järvinen /* Limits sacked_out so that sum with lost_out isn't ever larger than
2046a2a385d6SEric Dumazet * packets_out. Returns false if sacked_out adjustement wasn't necessary.
204730935cf4SIlpo Järvinen */
tcp_limit_reno_sacked(struct tcp_sock * tp)2048a2a385d6SEric Dumazet static bool tcp_limit_reno_sacked(struct tcp_sock *tp)
20494ddf6676SIlpo Järvinen {
20504ddf6676SIlpo Järvinen u32 holes;
20514ddf6676SIlpo Järvinen
20524ddf6676SIlpo Järvinen holes = max(tp->lost_out, 1U);
20534ddf6676SIlpo Järvinen holes = min(holes, tp->packets_out);
20544ddf6676SIlpo Järvinen
20554ddf6676SIlpo Järvinen if ((tp->sacked_out + holes) > tp->packets_out) {
20564ddf6676SIlpo Järvinen tp->sacked_out = tp->packets_out - holes;
2057a2a385d6SEric Dumazet return true;
20584ddf6676SIlpo Järvinen }
2059a2a385d6SEric Dumazet return false;
2060882bebaaSIlpo Järvinen }
2061882bebaaSIlpo Järvinen
2062882bebaaSIlpo Järvinen /* If we receive more dupacks than we expected counting segments
2063882bebaaSIlpo Järvinen * in assumption of absent reordering, interpret this as reordering.
2064882bebaaSIlpo Järvinen * The only another reason could be bug in receiver TCP.
2065882bebaaSIlpo Järvinen */
tcp_check_reno_reordering(struct sock * sk,const int addend)2066882bebaaSIlpo Järvinen static void tcp_check_reno_reordering(struct sock *sk, const int addend)
2067882bebaaSIlpo Järvinen {
2068882bebaaSIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
2069737ff314SYuchung Cheng
2070737ff314SYuchung Cheng if (!tcp_limit_reno_sacked(tp))
2071737ff314SYuchung Cheng return;
2072737ff314SYuchung Cheng
2073737ff314SYuchung Cheng tp->reordering = min_t(u32, tp->packets_out + addend,
2074a11e5b3eSKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_max_reordering));
20757ec65372SWei Wang tp->reord_seen++;
2076737ff314SYuchung Cheng NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRENOREORDER);
20774ddf6676SIlpo Järvinen }
20784ddf6676SIlpo Järvinen
20794ddf6676SIlpo Järvinen /* Emulate SACKs for SACKless connection: account for a new dupack. */
20804ddf6676SIlpo Järvinen
tcp_add_reno_sack(struct sock * sk,int num_dupack,bool ece_ack)2081c634e34fSYousuk Seung static void tcp_add_reno_sack(struct sock *sk, int num_dupack, bool ece_ack)
20824ddf6676SIlpo Järvinen {
208319119f29SEric Dumazet if (num_dupack) {
20844ddf6676SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
2085ddf1af6fSYuchung Cheng u32 prior_sacked = tp->sacked_out;
208619119f29SEric Dumazet s32 delivered;
2087ddf1af6fSYuchung Cheng
208819119f29SEric Dumazet tp->sacked_out += num_dupack;
20894ddf6676SIlpo Järvinen tcp_check_reno_reordering(sk, 0);
209019119f29SEric Dumazet delivered = tp->sacked_out - prior_sacked;
209119119f29SEric Dumazet if (delivered > 0)
2092082d4fa9SYousuk Seung tcp_count_delivered(tp, delivered, ece_ack);
2093005903bcSIlpo Järvinen tcp_verify_left_out(tp);
20944ddf6676SIlpo Järvinen }
209519119f29SEric Dumazet }
20964ddf6676SIlpo Järvinen
20974ddf6676SIlpo Järvinen /* Account for ACK, ACKing some data in Reno Recovery phase. */
20984ddf6676SIlpo Järvinen
tcp_remove_reno_sacks(struct sock * sk,int acked,bool ece_ack)2099c634e34fSYousuk Seung static void tcp_remove_reno_sacks(struct sock *sk, int acked, bool ece_ack)
21004ddf6676SIlpo Järvinen {
21014ddf6676SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
21024ddf6676SIlpo Järvinen
21034ddf6676SIlpo Järvinen if (acked > 0) {
21044ddf6676SIlpo Järvinen /* One ACK acked hole. The rest eat duplicate ACKs. */
2105082d4fa9SYousuk Seung tcp_count_delivered(tp, max_t(int, acked - tp->sacked_out, 1),
2106082d4fa9SYousuk Seung ece_ack);
21074ddf6676SIlpo Järvinen if (acked - 1 >= tp->sacked_out)
21084ddf6676SIlpo Järvinen tp->sacked_out = 0;
21094ddf6676SIlpo Järvinen else
21104ddf6676SIlpo Järvinen tp->sacked_out -= acked - 1;
21114ddf6676SIlpo Järvinen }
21124ddf6676SIlpo Järvinen tcp_check_reno_reordering(sk, acked);
2113005903bcSIlpo Järvinen tcp_verify_left_out(tp);
21144ddf6676SIlpo Järvinen }
21154ddf6676SIlpo Järvinen
tcp_reset_reno_sack(struct tcp_sock * tp)21164ddf6676SIlpo Järvinen static inline void tcp_reset_reno_sack(struct tcp_sock *tp)
21174ddf6676SIlpo Järvinen {
21184ddf6676SIlpo Järvinen tp->sacked_out = 0;
21194ddf6676SIlpo Järvinen }
21204ddf6676SIlpo Järvinen
tcp_clear_retrans(struct tcp_sock * tp)2121989e04c5SYuchung Cheng void tcp_clear_retrans(struct tcp_sock *tp)
21221da177e4SLinus Torvalds {
21231da177e4SLinus Torvalds tp->retrans_out = 0;
21241da177e4SLinus Torvalds tp->lost_out = 0;
21251da177e4SLinus Torvalds tp->undo_marker = 0;
21266e08d5e3SYuchung Cheng tp->undo_retrans = -1;
21274cd82999SIlpo Järvinen tp->sacked_out = 0;
21283868ab0fSAananth V tp->rto_stamp = 0;
21293868ab0fSAananth V tp->total_rto = 0;
21303868ab0fSAananth V tp->total_rto_recoveries = 0;
21313868ab0fSAananth V tp->total_rto_time = 0;
21324cd82999SIlpo Järvinen }
21334cd82999SIlpo Järvinen
tcp_init_undo(struct tcp_sock * tp)2134989e04c5SYuchung Cheng static inline void tcp_init_undo(struct tcp_sock *tp)
2135989e04c5SYuchung Cheng {
2136989e04c5SYuchung Cheng tp->undo_marker = tp->snd_una;
21370ec986edSNeal Cardwell
2138989e04c5SYuchung Cheng /* Retransmission still in flight may cause DSACKs later. */
21390ec986edSNeal Cardwell /* First, account for regular retransmits in flight: */
21400ec986edSNeal Cardwell tp->undo_retrans = tp->retrans_out;
21410ec986edSNeal Cardwell /* Next, account for TLP retransmits in flight: */
21420ec986edSNeal Cardwell if (tp->tlp_high_seq && tp->tlp_retrans)
21430ec986edSNeal Cardwell tp->undo_retrans++;
21440ec986edSNeal Cardwell /* Finally, avoid 0, because undo_retrans==0 means "can undo now": */
21450ec986edSNeal Cardwell if (!tp->undo_retrans)
21460ec986edSNeal Cardwell tp->undo_retrans = -1;
2147989e04c5SYuchung Cheng }
2148989e04c5SYuchung Cheng
tcp_is_rack(const struct sock * sk)2149b8fef65aSYuchung Cheng static bool tcp_is_rack(const struct sock *sk)
2150b8fef65aSYuchung Cheng {
2151e7d2ef83SKuniyuki Iwashima return READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_recovery) &
2152e7d2ef83SKuniyuki Iwashima TCP_RACK_LOSS_DETECTION;
2153b8fef65aSYuchung Cheng }
2154b8fef65aSYuchung Cheng
21552ad55f56SYuchung Cheng /* If we detect SACK reneging, forget all SACK information
21561da177e4SLinus Torvalds * and reset tags completely, otherwise preserve SACKs. If receiver
21571da177e4SLinus Torvalds * dropped its ofo queue, we will know this due to reneging detection.
21581da177e4SLinus Torvalds */
tcp_timeout_mark_lost(struct sock * sk)21592ad55f56SYuchung Cheng static void tcp_timeout_mark_lost(struct sock *sk)
21602ad55f56SYuchung Cheng {
21612ad55f56SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
216256f8c5d7SYuchung Cheng struct sk_buff *skb, *head;
21632ad55f56SYuchung Cheng bool is_reneg; /* is receiver reneging on SACKs? */
21642ad55f56SYuchung Cheng
216556f8c5d7SYuchung Cheng head = tcp_rtx_queue_head(sk);
216656f8c5d7SYuchung Cheng is_reneg = head && (TCP_SKB_CB(head)->sacked & TCPCB_SACKED_ACKED);
21672ad55f56SYuchung Cheng if (is_reneg) {
21682ad55f56SYuchung Cheng NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSACKRENEGING);
21692ad55f56SYuchung Cheng tp->sacked_out = 0;
21702ad55f56SYuchung Cheng /* Mark SACK reneging until we recover from this loss event. */
21712ad55f56SYuchung Cheng tp->is_sack_reneg = 1;
21722ad55f56SYuchung Cheng } else if (tcp_is_reno(tp)) {
21732ad55f56SYuchung Cheng tcp_reset_reno_sack(tp);
21742ad55f56SYuchung Cheng }
21752ad55f56SYuchung Cheng
217656f8c5d7SYuchung Cheng skb = head;
21772ad55f56SYuchung Cheng skb_rbtree_walk_from(skb) {
21782ad55f56SYuchung Cheng if (is_reneg)
21792ad55f56SYuchung Cheng TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
218056f8c5d7SYuchung Cheng else if (tcp_is_rack(sk) && skb != head &&
218156f8c5d7SYuchung Cheng tcp_rack_skb_timeout(tp, skb, 0) > 0)
218256f8c5d7SYuchung Cheng continue; /* Don't mark recently sent ones lost yet */
21832ad55f56SYuchung Cheng tcp_mark_skb_lost(sk, skb);
21842ad55f56SYuchung Cheng }
21852ad55f56SYuchung Cheng tcp_verify_left_out(tp);
21862ad55f56SYuchung Cheng tcp_clear_all_retrans_hints(tp);
21872ad55f56SYuchung Cheng }
21882ad55f56SYuchung Cheng
21892ad55f56SYuchung Cheng /* Enter Loss state. */
tcp_enter_loss(struct sock * sk)21905ae344c9SNeal Cardwell void tcp_enter_loss(struct sock *sk)
21911da177e4SLinus Torvalds {
21926687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk);
21931da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
21941043e25fSNikolay Borisov struct net *net = sock_net(sk);
2195cc663f4dSYuchung Cheng bool new_recovery = icsk->icsk_ca_state < TCP_CA_Recovery;
219646778cd1SKuniyuki Iwashima u8 reordering;
21971da177e4SLinus Torvalds
2198c77d62ffSYuchung Cheng tcp_timeout_mark_lost(sk);
2199c77d62ffSYuchung Cheng
22001da177e4SLinus Torvalds /* Reduce ssthresh if it has not yet been made inside this window. */
2201e33099f9SYuchung Cheng if (icsk->icsk_ca_state <= TCP_CA_Disorder ||
2202e33099f9SYuchung Cheng !after(tp->high_seq, tp->snd_una) ||
22036687e988SArnaldo Carvalho de Melo (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) {
22046687e988SArnaldo Carvalho de Melo tp->prior_ssthresh = tcp_current_ssthresh(sk);
220540570375SEric Dumazet tp->prior_cwnd = tcp_snd_cwnd(tp);
22066687e988SArnaldo Carvalho de Melo tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
22076687e988SArnaldo Carvalho de Melo tcp_ca_event(sk, CA_EVENT_LOSS);
2208989e04c5SYuchung Cheng tcp_init_undo(tp);
22091da177e4SLinus Torvalds }
221040570375SEric Dumazet tcp_snd_cwnd_set(tp, tcp_packets_in_flight(tp) + 1);
22111da177e4SLinus Torvalds tp->snd_cwnd_cnt = 0;
2212c2203cf7SEric Dumazet tp->snd_cwnd_stamp = tcp_jiffies32;
22131da177e4SLinus Torvalds
221474c181d5SYuchung Cheng /* Timeout in disordered state after receiving substantial DUPACKs
221574c181d5SYuchung Cheng * suggests that the degree of reordering is over-estimated.
221674c181d5SYuchung Cheng */
221746778cd1SKuniyuki Iwashima reordering = READ_ONCE(net->ipv4.sysctl_tcp_reordering);
221874c181d5SYuchung Cheng if (icsk->icsk_ca_state <= TCP_CA_Disorder &&
221946778cd1SKuniyuki Iwashima tp->sacked_out >= reordering)
22201da177e4SLinus Torvalds tp->reordering = min_t(unsigned int, tp->reordering,
222146778cd1SKuniyuki Iwashima reordering);
222246778cd1SKuniyuki Iwashima
22236687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Loss);
22241da177e4SLinus Torvalds tp->high_seq = tp->snd_nxt;
22250ec986edSNeal Cardwell tp->tlp_high_seq = 0;
2226735d3831SFlorian Westphal tcp_ecn_queue_cwr(tp);
2227e33099f9SYuchung Cheng
2228cc663f4dSYuchung Cheng /* F-RTO RFC5682 sec 3.1 step 1: retransmit SND.UNA if no previous
2229cc663f4dSYuchung Cheng * loss recovery is underway except recurring timeout(s) on
2230cc663f4dSYuchung Cheng * the same SND.UNA (sec 3.2). Disable F-RTO on path MTU probing
2231e33099f9SYuchung Cheng */
2232706c6202SKuniyuki Iwashima tp->frto = READ_ONCE(net->ipv4.sysctl_tcp_frto) &&
2233cc663f4dSYuchung Cheng (new_recovery || icsk->icsk_retransmits) &&
2234cc663f4dSYuchung Cheng !inet_csk(sk)->icsk_mtup.probe_size;
22351da177e4SLinus Torvalds }
22361da177e4SLinus Torvalds
2237cadbd031SIlpo Järvinen /* If ACK arrived pointing to a remembered SACK, it means that our
2238cadbd031SIlpo Järvinen * remembered SACKs do not reflect real state of receiver i.e.
22391da177e4SLinus Torvalds * receiver _host_ is heavily congested (or buggy).
2240cadbd031SIlpo Järvinen *
22415ae344c9SNeal Cardwell * To avoid big spurious retransmission bursts due to transient SACK
22425ae344c9SNeal Cardwell * scoreboard oddities that look like reneging, we give the receiver a
22435ae344c9SNeal Cardwell * little time (max(RTT/2, 10ms)) to send us some more ACKs that will
22445ae344c9SNeal Cardwell * restore sanity to the SACK scoreboard. If the apparent reneging
22455ae344c9SNeal Cardwell * persists until this RTO then we'll clear the SACK scoreboard.
22461da177e4SLinus Torvalds */
tcp_check_sack_reneging(struct sock * sk,int * ack_flag)2247d2a0fc37SFred Chen static bool tcp_check_sack_reneging(struct sock *sk, int *ack_flag)
2248cadbd031SIlpo Järvinen {
2249d2a0fc37SFred Chen if (*ack_flag & FLAG_SACK_RENEGING &&
2250d2a0fc37SFred Chen *ack_flag & FLAG_SND_UNA_ADVANCED) {
22515ae344c9SNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
22525ae344c9SNeal Cardwell unsigned long delay = max(usecs_to_jiffies(tp->srtt_us >> 4),
22535ae344c9SNeal Cardwell msecs_to_jiffies(10));
22541da177e4SLinus Torvalds
2255463c84b9SArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
22565ae344c9SNeal Cardwell delay, TCP_RTO_MAX);
2257d2a0fc37SFred Chen *ack_flag &= ~FLAG_SET_XMIT_TIMER;
2258a2a385d6SEric Dumazet return true;
22591da177e4SLinus Torvalds }
2260a2a385d6SEric Dumazet return false;
22611da177e4SLinus Torvalds }
22621da177e4SLinus Torvalds
226385cc391cSIlpo Järvinen /* Heurestics to calculate number of duplicate ACKs. There's no dupACKs
226485cc391cSIlpo Järvinen * counter when SACK is enabled (without SACK, sacked_out is used for
226585cc391cSIlpo Järvinen * that purpose).
226685cc391cSIlpo Järvinen *
226785cc391cSIlpo Järvinen * With reordering, holes may still be in flight, so RFC3517 recovery
226885cc391cSIlpo Järvinen * uses pure sacked_out (total number of SACKed segments) even though
226985cc391cSIlpo Järvinen * it violates the RFC that uses duplicate ACKs, often these are equal
227085cc391cSIlpo Järvinen * but when e.g. out-of-window ACKs or packet duplication occurs,
227185cc391cSIlpo Järvinen * they differ. Since neither occurs due to loss, TCP should really
227285cc391cSIlpo Järvinen * ignore them.
227385cc391cSIlpo Järvinen */
tcp_dupack_heuristics(const struct tcp_sock * tp)2274cf533ea5SEric Dumazet static inline int tcp_dupack_heuristics(const struct tcp_sock *tp)
227585cc391cSIlpo Järvinen {
2276713bafeaSYuchung Cheng return tp->sacked_out + 1;
227785cc391cSIlpo Järvinen }
227885cc391cSIlpo Järvinen
2279713bafeaSYuchung Cheng /* Linux NewReno/SACK/ECN state machine.
22801da177e4SLinus Torvalds * --------------------------------------
22811da177e4SLinus Torvalds *
22821da177e4SLinus Torvalds * "Open" Normal state, no dubious events, fast path.
22831da177e4SLinus Torvalds * "Disorder" In all the respects it is "Open",
22841da177e4SLinus Torvalds * but requires a bit more attention. It is entered when
22851da177e4SLinus Torvalds * we see some SACKs or dupacks. It is split of "Open"
22861da177e4SLinus Torvalds * mainly to move some processing from fast path to slow one.
22871da177e4SLinus Torvalds * "CWR" CWND was reduced due to some Congestion Notification event.
22881da177e4SLinus Torvalds * It can be ECN, ICMP source quench, local device congestion.
22891da177e4SLinus Torvalds * "Recovery" CWND was reduced, we are fast-retransmitting.
22901da177e4SLinus Torvalds * "Loss" CWND was reduced due to RTO timeout or SACK reneging.
22911da177e4SLinus Torvalds *
22921da177e4SLinus Torvalds * tcp_fastretrans_alert() is entered:
22931da177e4SLinus Torvalds * - each incoming ACK, if state is not "Open"
22941da177e4SLinus Torvalds * - when arrived ACK is unusual, namely:
22951da177e4SLinus Torvalds * * SACK
22961da177e4SLinus Torvalds * * Duplicate ACK.
22971da177e4SLinus Torvalds * * ECN ECE.
22981da177e4SLinus Torvalds *
22991da177e4SLinus Torvalds * Counting packets in flight is pretty simple.
23001da177e4SLinus Torvalds *
23011da177e4SLinus Torvalds * in_flight = packets_out - left_out + retrans_out
23021da177e4SLinus Torvalds *
23031da177e4SLinus Torvalds * packets_out is SND.NXT-SND.UNA counted in packets.
23041da177e4SLinus Torvalds *
23051da177e4SLinus Torvalds * retrans_out is number of retransmitted segments.
23061da177e4SLinus Torvalds *
23071da177e4SLinus Torvalds * left_out is number of segments left network, but not ACKed yet.
23081da177e4SLinus Torvalds *
23091da177e4SLinus Torvalds * left_out = sacked_out + lost_out
23101da177e4SLinus Torvalds *
23111da177e4SLinus Torvalds * sacked_out: Packets, which arrived to receiver out of order
23121da177e4SLinus Torvalds * and hence not ACKed. With SACKs this number is simply
23131da177e4SLinus Torvalds * amount of SACKed data. Even without SACKs
23141da177e4SLinus Torvalds * it is easy to give pretty reliable estimate of this number,
23151da177e4SLinus Torvalds * counting duplicate ACKs.
23161da177e4SLinus Torvalds *
23171da177e4SLinus Torvalds * lost_out: Packets lost by network. TCP has no explicit
23181da177e4SLinus Torvalds * "loss notification" feedback from network (for now).
23191da177e4SLinus Torvalds * It means that this number can be only _guessed_.
23201da177e4SLinus Torvalds * Actually, it is the heuristics to predict lossage that
23211da177e4SLinus Torvalds * distinguishes different algorithms.
23221da177e4SLinus Torvalds *
23231da177e4SLinus Torvalds * F.e. after RTO, when all the queue is considered as lost,
23241da177e4SLinus Torvalds * lost_out = packets_out and in_flight = retrans_out.
23251da177e4SLinus Torvalds *
2326a0370b3fSYuchung Cheng * Essentially, we have now a few algorithms detecting
23271da177e4SLinus Torvalds * lost packets.
23281da177e4SLinus Torvalds *
2329a0370b3fSYuchung Cheng * If the receiver supports SACK:
2330a0370b3fSYuchung Cheng *
2331a0370b3fSYuchung Cheng * RFC6675/3517: It is the conventional algorithm. A packet is
2332a0370b3fSYuchung Cheng * considered lost if the number of higher sequence packets
2333a0370b3fSYuchung Cheng * SACKed is greater than or equal the DUPACK thoreshold
2334a0370b3fSYuchung Cheng * (reordering). This is implemented in tcp_mark_head_lost and
2335a0370b3fSYuchung Cheng * tcp_update_scoreboard.
2336a0370b3fSYuchung Cheng *
2337a0370b3fSYuchung Cheng * RACK (draft-ietf-tcpm-rack-01): it is a newer algorithm
2338a0370b3fSYuchung Cheng * (2017-) that checks timing instead of counting DUPACKs.
2339a0370b3fSYuchung Cheng * Essentially a packet is considered lost if it's not S/ACKed
2340a0370b3fSYuchung Cheng * after RTT + reordering_window, where both metrics are
2341a0370b3fSYuchung Cheng * dynamically measured and adjusted. This is implemented in
2342a0370b3fSYuchung Cheng * tcp_rack_mark_lost.
2343a0370b3fSYuchung Cheng *
2344a0370b3fSYuchung Cheng * If the receiver does not support SACK:
2345a0370b3fSYuchung Cheng *
2346a0370b3fSYuchung Cheng * NewReno (RFC6582): in Recovery we assume that one segment
23471da177e4SLinus Torvalds * is lost (classic Reno). While we are in Recovery and
23481da177e4SLinus Torvalds * a partial ACK arrives, we assume that one more packet
23491da177e4SLinus Torvalds * is lost (NewReno). This heuristics are the same in NewReno
23501da177e4SLinus Torvalds * and SACK.
23511da177e4SLinus Torvalds *
23521da177e4SLinus Torvalds * Really tricky (and requiring careful tuning) part of algorithm
23531da177e4SLinus Torvalds * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue().
23541da177e4SLinus Torvalds * The first determines the moment _when_ we should reduce CWND and,
23551da177e4SLinus Torvalds * hence, slow down forward transmission. In fact, it determines the moment
23561da177e4SLinus Torvalds * when we decide that hole is caused by loss, rather than by a reorder.
23571da177e4SLinus Torvalds *
23581da177e4SLinus Torvalds * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill
23591da177e4SLinus Torvalds * holes, caused by lost packets.
23601da177e4SLinus Torvalds *
23611da177e4SLinus Torvalds * And the most logically complicated part of algorithm is undo
23621da177e4SLinus Torvalds * heuristics. We detect false retransmits due to both too early
23631da177e4SLinus Torvalds * fast retransmit (reordering) and underestimated RTO, analyzing
23641da177e4SLinus Torvalds * timestamps and D-SACKs. When we detect that some segments were
23651da177e4SLinus Torvalds * retransmitted by mistake and CWND reduction was wrong, we undo
23661da177e4SLinus Torvalds * window reduction and abort recovery phase. This logic is hidden
23671da177e4SLinus Torvalds * inside several functions named tcp_try_undo_<something>.
23681da177e4SLinus Torvalds */
23691da177e4SLinus Torvalds
23701da177e4SLinus Torvalds /* This function decides, when we should leave Disordered state
23711da177e4SLinus Torvalds * and enter Recovery phase, reducing congestion window.
23721da177e4SLinus Torvalds *
23731da177e4SLinus Torvalds * Main question: may we further continue forward transmission
23741da177e4SLinus Torvalds * with the same cwnd?
23751da177e4SLinus Torvalds */
tcp_time_to_recover(struct sock * sk,int flag)2376a2a385d6SEric Dumazet static bool tcp_time_to_recover(struct sock *sk, int flag)
23771da177e4SLinus Torvalds {
23789e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
23791da177e4SLinus Torvalds
23801da177e4SLinus Torvalds /* Trick#1: The loss is proven. */
23811da177e4SLinus Torvalds if (tp->lost_out)
2382a2a385d6SEric Dumazet return true;
23831da177e4SLinus Torvalds
23841da177e4SLinus Torvalds /* Not-A-Trick#2 : Classic rule... */
2385b38a51feSYuchung Cheng if (!tcp_is_rack(sk) && tcp_dupack_heuristics(tp) > tp->reordering)
2386a2a385d6SEric Dumazet return true;
23871da177e4SLinus Torvalds
2388a2a385d6SEric Dumazet return false;
23891da177e4SLinus Torvalds }
23901da177e4SLinus Torvalds
2391974c1236SYuchung Cheng /* Detect loss in event "A" above by marking head of queue up as lost.
2392636ef28dSzhang kai * For RFC3517 SACK, a segment is considered lost if it
2393974c1236SYuchung Cheng * has at least tp->reordering SACKed seqments above it; "packets" refers to
2394974c1236SYuchung Cheng * the maximum SACKed segments to pass before reaching this limit.
239585cc391cSIlpo Järvinen */
tcp_mark_head_lost(struct sock * sk,int packets,int mark_head)23961fdb9361SIlpo Järvinen static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)
23971da177e4SLinus Torvalds {
23989e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
23991da177e4SLinus Torvalds struct sk_buff *skb;
2400636ef28dSzhang kai int cnt;
2401974c1236SYuchung Cheng /* Use SACK to deduce losses of new sequences sent during recovery */
2402636ef28dSzhang kai const u32 loss_high = tp->snd_nxt;
24031da177e4SLinus Torvalds
2404547b792cSIlpo Järvinen WARN_ON(packets > tp->packets_out);
24056a438bbeSStephen Hemminger skb = tp->lost_skb_hint;
24065e76ee4bSEric Dumazet if (skb) {
24071fdb9361SIlpo Järvinen /* Head already handled? */
24085e76ee4bSEric Dumazet if (mark_head && after(TCP_SKB_CB(skb)->seq, tp->snd_una))
24091fdb9361SIlpo Järvinen return;
24105e76ee4bSEric Dumazet cnt = tp->lost_cnt_hint;
24116a438bbeSStephen Hemminger } else {
241275c119afSEric Dumazet skb = tcp_rtx_queue_head(sk);
24136a438bbeSStephen Hemminger cnt = 0;
24146a438bbeSStephen Hemminger }
24151da177e4SLinus Torvalds
241675c119afSEric Dumazet skb_rbtree_walk_from(skb) {
24176a438bbeSStephen Hemminger /* TODO: do this better */
24186a438bbeSStephen Hemminger /* this is not the most efficient way to do this... */
24196a438bbeSStephen Hemminger tp->lost_skb_hint = skb;
24206a438bbeSStephen Hemminger tp->lost_cnt_hint = cnt;
242185cc391cSIlpo Järvinen
2422974c1236SYuchung Cheng if (after(TCP_SKB_CB(skb)->end_seq, loss_high))
2423c137f3ddSIlpo Järvinen break;
2424c137f3ddSIlpo Järvinen
2425636ef28dSzhang kai if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
24266a438bbeSStephen Hemminger cnt += tcp_skb_pcount(skb);
242785cc391cSIlpo Järvinen
2428636ef28dSzhang kai if (cnt > packets)
24291da177e4SLinus Torvalds break;
2430c137f3ddSIlpo Järvinen
2431534a2109SYuchung Cheng if (!(TCP_SKB_CB(skb)->sacked & TCPCB_LOST))
2432534a2109SYuchung Cheng tcp_mark_skb_lost(sk, skb);
24331fdb9361SIlpo Järvinen
24341fdb9361SIlpo Järvinen if (mark_head)
24351fdb9361SIlpo Järvinen break;
24361da177e4SLinus Torvalds }
2437005903bcSIlpo Järvinen tcp_verify_left_out(tp);
24381da177e4SLinus Torvalds }
24391da177e4SLinus Torvalds
24401da177e4SLinus Torvalds /* Account newly detected lost packet(s) */
24411da177e4SLinus Torvalds
tcp_update_scoreboard(struct sock * sk,int fast_rexmit)244285cc391cSIlpo Järvinen static void tcp_update_scoreboard(struct sock *sk, int fast_rexmit)
24431da177e4SLinus Torvalds {
24449e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
24459e412ba7SIlpo Järvinen
24466ac06ecdSYuchung Cheng if (tcp_is_sack(tp)) {
244785cc391cSIlpo Järvinen int sacked_upto = tp->sacked_out - tp->reordering;
24481fdb9361SIlpo Järvinen if (sacked_upto >= 0)
24491fdb9361SIlpo Järvinen tcp_mark_head_lost(sk, sacked_upto, 0);
24501fdb9361SIlpo Järvinen else if (fast_rexmit)
24511fdb9361SIlpo Järvinen tcp_mark_head_lost(sk, 1, 1);
24521da177e4SLinus Torvalds }
24531da177e4SLinus Torvalds }
24541da177e4SLinus Torvalds
tcp_tsopt_ecr_before(const struct tcp_sock * tp,u32 when)245577c63127SYuchung Cheng static bool tcp_tsopt_ecr_before(const struct tcp_sock *tp, u32 when)
245677c63127SYuchung Cheng {
245777c63127SYuchung Cheng return tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
245877c63127SYuchung Cheng before(tp->rx_opt.rcv_tsecr, when);
245977c63127SYuchung Cheng }
246077c63127SYuchung Cheng
2461659a8ad5SYuchung Cheng /* skb is spurious retransmitted if the returned timestamp echo
2462659a8ad5SYuchung Cheng * reply is prior to the skb transmission time
2463659a8ad5SYuchung Cheng */
tcp_skb_spurious_retrans(const struct tcp_sock * tp,const struct sk_buff * skb)2464659a8ad5SYuchung Cheng static bool tcp_skb_spurious_retrans(const struct tcp_sock *tp,
2465659a8ad5SYuchung Cheng const struct sk_buff *skb)
2466659a8ad5SYuchung Cheng {
2467659a8ad5SYuchung Cheng return (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS) &&
2468614e8316SEric Dumazet tcp_tsopt_ecr_before(tp, tcp_skb_timestamp_ts(tp->tcp_usec_ts, skb));
2469659a8ad5SYuchung Cheng }
2470659a8ad5SYuchung Cheng
24711da177e4SLinus Torvalds /* Nothing was retransmitted or returned timestamp is less
24721da177e4SLinus Torvalds * than timestamp of the first retransmission.
24731da177e4SLinus Torvalds */
tcp_packet_delayed(const struct tcp_sock * tp)247467b95bd7SVijay Subramanian static inline bool tcp_packet_delayed(const struct tcp_sock *tp)
24751da177e4SLinus Torvalds {
2476e37ab737SNeal Cardwell const struct sock *sk = (const struct sock *)tp;
2477e37ab737SNeal Cardwell
2478e37ab737SNeal Cardwell if (tp->retrans_stamp &&
2479e37ab737SNeal Cardwell tcp_tsopt_ecr_before(tp, tp->retrans_stamp))
2480e37ab737SNeal Cardwell return true; /* got echoed TS before first retransmission */
2481e37ab737SNeal Cardwell
2482e37ab737SNeal Cardwell /* Check if nothing was retransmitted (retrans_stamp==0), which may
2483e37ab737SNeal Cardwell * happen in fast recovery due to TSQ. But we ignore zero retrans_stamp
2484e37ab737SNeal Cardwell * in TCP_SYN_SENT, since when we set FLAG_SYN_ACKED we also clear
2485e37ab737SNeal Cardwell * retrans_stamp even if we had retransmitted the SYN.
2486e37ab737SNeal Cardwell */
2487e37ab737SNeal Cardwell if (!tp->retrans_stamp && /* no record of a retransmit/SYN? */
2488e37ab737SNeal Cardwell sk->sk_state != TCP_SYN_SENT) /* not the FLAG_SYN_ACKED case? */
2489e37ab737SNeal Cardwell return true; /* nothing was retransmitted */
2490e37ab737SNeal Cardwell
2491e37ab737SNeal Cardwell return false;
24921da177e4SLinus Torvalds }
24931da177e4SLinus Torvalds
24941da177e4SLinus Torvalds /* Undo procedures. */
24951da177e4SLinus Torvalds
24961f37bf87SMarcelo Leitner /* We can clear retrans_stamp when there are no retransmissions in the
24971f37bf87SMarcelo Leitner * window. It would seem that it is trivially available for us in
24981f37bf87SMarcelo Leitner * tp->retrans_out, however, that kind of assumptions doesn't consider
24991f37bf87SMarcelo Leitner * what will happen if errors occur when sending retransmission for the
25001f37bf87SMarcelo Leitner * second time. ...It could the that such segment has only
25011f37bf87SMarcelo Leitner * TCPCB_EVER_RETRANS set at the present time. It seems that checking
25021f37bf87SMarcelo Leitner * the head skb is enough except for some reneging corner cases that
25031f37bf87SMarcelo Leitner * are not worth the effort.
25041f37bf87SMarcelo Leitner *
25051f37bf87SMarcelo Leitner * Main reason for all this complexity is the fact that connection dying
25061f37bf87SMarcelo Leitner * time now depends on the validity of the retrans_stamp, in particular,
25071f37bf87SMarcelo Leitner * that successive retransmissions of a segment must not advance
25081f37bf87SMarcelo Leitner * retrans_stamp under any conditions.
25091f37bf87SMarcelo Leitner */
tcp_any_retrans_done(const struct sock * sk)25101f37bf87SMarcelo Leitner static bool tcp_any_retrans_done(const struct sock *sk)
25111f37bf87SMarcelo Leitner {
25121f37bf87SMarcelo Leitner const struct tcp_sock *tp = tcp_sk(sk);
25131f37bf87SMarcelo Leitner struct sk_buff *skb;
25141f37bf87SMarcelo Leitner
25151f37bf87SMarcelo Leitner if (tp->retrans_out)
25161f37bf87SMarcelo Leitner return true;
25171f37bf87SMarcelo Leitner
251875c119afSEric Dumazet skb = tcp_rtx_queue_head(sk);
25191f37bf87SMarcelo Leitner if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS))
25201f37bf87SMarcelo Leitner return true;
25211f37bf87SMarcelo Leitner
25221f37bf87SMarcelo Leitner return false;
25231f37bf87SMarcelo Leitner }
25241f37bf87SMarcelo Leitner
2525b41b4cbdSNeal Cardwell /* If loss recovery is finished and there are no retransmits out in the
2526b41b4cbdSNeal Cardwell * network, then we clear retrans_stamp so that upon the next loss recovery
2527b41b4cbdSNeal Cardwell * retransmits_timed_out() and timestamp-undo are using the correct value.
2528b41b4cbdSNeal Cardwell */
tcp_retrans_stamp_cleanup(struct sock * sk)2529b41b4cbdSNeal Cardwell static void tcp_retrans_stamp_cleanup(struct sock *sk)
2530b41b4cbdSNeal Cardwell {
2531b41b4cbdSNeal Cardwell if (!tcp_any_retrans_done(sk))
2532b41b4cbdSNeal Cardwell tcp_sk(sk)->retrans_stamp = 0;
2533b41b4cbdSNeal Cardwell }
2534b41b4cbdSNeal Cardwell
DBGUNDO(struct sock * sk,const char * msg)25359e412ba7SIlpo Järvinen static void DBGUNDO(struct sock *sk, const char *msg)
25361da177e4SLinus Torvalds {
25373934788aSJoe Perches #if FASTRETRANS_DEBUG > 1
25389e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
25391da177e4SLinus Torvalds struct inet_sock *inet = inet_sk(sk);
25409e412ba7SIlpo Järvinen
2541569508c9SYOSHIFUJI Hideaki if (sk->sk_family == AF_INET) {
254291df42beSJoe Perches pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n",
25431da177e4SLinus Torvalds msg,
2544288fcee8SJoe Perches &inet->inet_daddr, ntohs(inet->inet_dport),
254540570375SEric Dumazet tcp_snd_cwnd(tp), tcp_left_out(tp),
25461da177e4SLinus Torvalds tp->snd_ssthresh, tp->prior_ssthresh,
25471da177e4SLinus Torvalds tp->packets_out);
25481da177e4SLinus Torvalds }
2549dfd56b8bSEric Dumazet #if IS_ENABLED(CONFIG_IPV6)
2550569508c9SYOSHIFUJI Hideaki else if (sk->sk_family == AF_INET6) {
255191df42beSJoe Perches pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n",
2552569508c9SYOSHIFUJI Hideaki msg,
2553019b1c9fSEric Dumazet &sk->sk_v6_daddr, ntohs(inet->inet_dport),
255440570375SEric Dumazet tcp_snd_cwnd(tp), tcp_left_out(tp),
2555569508c9SYOSHIFUJI Hideaki tp->snd_ssthresh, tp->prior_ssthresh,
2556569508c9SYOSHIFUJI Hideaki tp->packets_out);
2557569508c9SYOSHIFUJI Hideaki }
2558569508c9SYOSHIFUJI Hideaki #endif
25591da177e4SLinus Torvalds #endif
25603934788aSJoe Perches }
25611da177e4SLinus Torvalds
tcp_undo_cwnd_reduction(struct sock * sk,bool unmark_loss)25627026b912SYuchung Cheng static void tcp_undo_cwnd_reduction(struct sock *sk, bool unmark_loss)
25631da177e4SLinus Torvalds {
25646687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk);
25656687e988SArnaldo Carvalho de Melo
25666a63df46SYuchung Cheng if (unmark_loss) {
25676a63df46SYuchung Cheng struct sk_buff *skb;
25686a63df46SYuchung Cheng
256975c119afSEric Dumazet skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
25706a63df46SYuchung Cheng TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
25716a63df46SYuchung Cheng }
25726a63df46SYuchung Cheng tp->lost_out = 0;
25736a63df46SYuchung Cheng tcp_clear_all_retrans_hints(tp);
25746a63df46SYuchung Cheng }
25756a63df46SYuchung Cheng
25761da177e4SLinus Torvalds if (tp->prior_ssthresh) {
25776687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk);
25786687e988SArnaldo Carvalho de Melo
257940570375SEric Dumazet tcp_snd_cwnd_set(tp, icsk->icsk_ca_ops->undo_cwnd(sk));
25801da177e4SLinus Torvalds
25817026b912SYuchung Cheng if (tp->prior_ssthresh > tp->snd_ssthresh) {
25821da177e4SLinus Torvalds tp->snd_ssthresh = tp->prior_ssthresh;
2583735d3831SFlorian Westphal tcp_ecn_withdraw_cwr(tp);
25841da177e4SLinus Torvalds }
25851da177e4SLinus Torvalds }
2586c2203cf7SEric Dumazet tp->snd_cwnd_stamp = tcp_jiffies32;
25876a63df46SYuchung Cheng tp->undo_marker = 0;
2588cd1fc85bSYuchung Cheng tp->rack.advanced = 1; /* Force RACK to re-exam losses */
25891da177e4SLinus Torvalds }
25901da177e4SLinus Torvalds
tcp_may_undo(const struct tcp_sock * tp)259167b95bd7SVijay Subramanian static inline bool tcp_may_undo(const struct tcp_sock *tp)
25921da177e4SLinus Torvalds {
2593056834d9SIlpo Järvinen return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp));
25941da177e4SLinus Torvalds }
25951da177e4SLinus Torvalds
tcp_is_non_sack_preventing_reopen(struct sock * sk)2596686dc2dbSNeal Cardwell static bool tcp_is_non_sack_preventing_reopen(struct sock *sk)
2597686dc2dbSNeal Cardwell {
2598686dc2dbSNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
2599686dc2dbSNeal Cardwell
2600686dc2dbSNeal Cardwell if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) {
2601686dc2dbSNeal Cardwell /* Hold old state until something *above* high_seq
2602686dc2dbSNeal Cardwell * is ACKed. For Reno it is MUST to prevent false
2603686dc2dbSNeal Cardwell * fast retransmits (RFC2582). SACK TCP is safe. */
2604686dc2dbSNeal Cardwell if (!tcp_any_retrans_done(sk))
2605686dc2dbSNeal Cardwell tp->retrans_stamp = 0;
2606686dc2dbSNeal Cardwell return true;
2607686dc2dbSNeal Cardwell }
2608686dc2dbSNeal Cardwell return false;
2609686dc2dbSNeal Cardwell }
2610686dc2dbSNeal Cardwell
26111da177e4SLinus Torvalds /* People celebrate: "We love our President!" */
tcp_try_undo_recovery(struct sock * sk)2612a2a385d6SEric Dumazet static bool tcp_try_undo_recovery(struct sock *sk)
26131da177e4SLinus Torvalds {
26149e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
26159e412ba7SIlpo Järvinen
26161da177e4SLinus Torvalds if (tcp_may_undo(tp)) {
261740b215e5SPavel Emelyanov int mib_idx;
261840b215e5SPavel Emelyanov
26191da177e4SLinus Torvalds /* Happy end! We did not retransmit anything
26201da177e4SLinus Torvalds * or our original transmission succeeded.
26211da177e4SLinus Torvalds */
26229e412ba7SIlpo Järvinen DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans");
26237026b912SYuchung Cheng tcp_undo_cwnd_reduction(sk, false);
26246687e988SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss)
262540b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPLOSSUNDO;
26261da177e4SLinus Torvalds else
262740b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPFULLUNDO;
262840b215e5SPavel Emelyanov
2629c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), mib_idx);
26301f255691SPriyaranjan Jha } else if (tp->rack.reo_wnd_persist) {
26311f255691SPriyaranjan Jha tp->rack.reo_wnd_persist--;
26321da177e4SLinus Torvalds }
2633686dc2dbSNeal Cardwell if (tcp_is_non_sack_preventing_reopen(sk))
2634a2a385d6SEric Dumazet return true;
26356687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open);
2636d4761754SYousuk Seung tp->is_sack_reneg = 0;
2637a2a385d6SEric Dumazet return false;
26381da177e4SLinus Torvalds }
26391da177e4SLinus Torvalds
26401da177e4SLinus Torvalds /* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */
tcp_try_undo_dsack(struct sock * sk)2641c7d9d6a1SYuchung Cheng static bool tcp_try_undo_dsack(struct sock *sk)
26421da177e4SLinus Torvalds {
26439e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
26449e412ba7SIlpo Järvinen
26451da177e4SLinus Torvalds if (tp->undo_marker && !tp->undo_retrans) {
26461f255691SPriyaranjan Jha tp->rack.reo_wnd_persist = min(TCP_RACK_RECOVERY_THRESH,
26471f255691SPriyaranjan Jha tp->rack.reo_wnd_persist + 1);
26489e412ba7SIlpo Järvinen DBGUNDO(sk, "D-SACK");
26497026b912SYuchung Cheng tcp_undo_cwnd_reduction(sk, false);
2650c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKUNDO);
2651c7d9d6a1SYuchung Cheng return true;
26521da177e4SLinus Torvalds }
2653c7d9d6a1SYuchung Cheng return false;
26541da177e4SLinus Torvalds }
26551da177e4SLinus Torvalds
2656e33099f9SYuchung Cheng /* Undo during loss recovery after partial ACK or using F-RTO. */
tcp_try_undo_loss(struct sock * sk,bool frto_undo)2657e33099f9SYuchung Cheng static bool tcp_try_undo_loss(struct sock *sk, bool frto_undo)
26581da177e4SLinus Torvalds {
26599e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
26609e412ba7SIlpo Järvinen
2661e33099f9SYuchung Cheng if (frto_undo || tcp_may_undo(tp)) {
26627026b912SYuchung Cheng tcp_undo_cwnd_reduction(sk, true);
26636a438bbeSStephen Hemminger
26649e412ba7SIlpo Järvinen DBGUNDO(sk, "partial loss");
2665c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSUNDO);
2666e33099f9SYuchung Cheng if (frto_undo)
2667c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk),
2668e33099f9SYuchung Cheng LINUX_MIB_TCPSPURIOUSRTOS);
2669463c84b9SArnaldo Carvalho de Melo inet_csk(sk)->icsk_retransmits = 0;
2670686dc2dbSNeal Cardwell if (tcp_is_non_sack_preventing_reopen(sk))
2671686dc2dbSNeal Cardwell return true;
2672d4761754SYousuk Seung if (frto_undo || tcp_is_sack(tp)) {
26736687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open);
2674d4761754SYousuk Seung tp->is_sack_reneg = 0;
2675d4761754SYousuk Seung }
2676a2a385d6SEric Dumazet return true;
26771da177e4SLinus Torvalds }
2678a2a385d6SEric Dumazet return false;
26791da177e4SLinus Torvalds }
26801da177e4SLinus Torvalds
26813759824dSYuchung Cheng /* The cwnd reduction in CWR and Recovery uses the PRR algorithm in RFC 6937.
2682fb4d3d1dSYuchung Cheng * It computes the number of packets to send (sndcnt) based on packets newly
2683fb4d3d1dSYuchung Cheng * delivered:
2684fb4d3d1dSYuchung Cheng * 1) If the packets in flight is larger than ssthresh, PRR spreads the
2685fb4d3d1dSYuchung Cheng * cwnd reductions across a full RTT.
26863759824dSYuchung Cheng * 2) Otherwise PRR uses packet conservation to send as much as delivered.
26877e901ee7SYuchung Cheng * But when SND_UNA is acked without further losses,
26883759824dSYuchung Cheng * slow starts cwnd up to ssthresh to speed up the recovery.
2689fb4d3d1dSYuchung Cheng */
tcp_init_cwnd_reduction(struct sock * sk)26905ee2c941SChristoph Paasch static void tcp_init_cwnd_reduction(struct sock *sk)
2691684bad11SYuchung Cheng {
2692684bad11SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
2693684bad11SYuchung Cheng
2694684bad11SYuchung Cheng tp->high_seq = tp->snd_nxt;
26959b717a8dSNandita Dukkipati tp->tlp_high_seq = 0;
2696684bad11SYuchung Cheng tp->snd_cwnd_cnt = 0;
269740570375SEric Dumazet tp->prior_cwnd = tcp_snd_cwnd(tp);
2698684bad11SYuchung Cheng tp->prr_delivered = 0;
2699684bad11SYuchung Cheng tp->prr_out = 0;
2700684bad11SYuchung Cheng tp->snd_ssthresh = inet_csk(sk)->icsk_ca_ops->ssthresh(sk);
2701735d3831SFlorian Westphal tcp_ecn_queue_cwr(tp);
2702684bad11SYuchung Cheng }
2703684bad11SYuchung Cheng
tcp_cwnd_reduction(struct sock * sk,int newly_acked_sacked,int newly_lost,int flag)27047e901ee7SYuchung Cheng void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int newly_lost, int flag)
2705fb4d3d1dSYuchung Cheng {
2706fb4d3d1dSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
2707fb4d3d1dSYuchung Cheng int sndcnt = 0;
2708fb4d3d1dSYuchung Cheng int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp);
2709fb4d3d1dSYuchung Cheng
27108b8a321fSYuchung Cheng if (newly_acked_sacked <= 0 || WARN_ON_ONCE(!tp->prior_cwnd))
27118b8a321fSYuchung Cheng return;
27128b8a321fSYuchung Cheng
2713684bad11SYuchung Cheng tp->prr_delivered += newly_acked_sacked;
27143759824dSYuchung Cheng if (delta < 0) {
2715fb4d3d1dSYuchung Cheng u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered +
2716fb4d3d1dSYuchung Cheng tp->prior_cwnd - 1;
2717fb4d3d1dSYuchung Cheng sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out;
27183759824dSYuchung Cheng } else {
27199ad084d6SYuchung Cheng sndcnt = max_t(int, tp->prr_delivered - tp->prr_out,
27209ad084d6SYuchung Cheng newly_acked_sacked);
27219ad084d6SYuchung Cheng if (flag & FLAG_SND_UNA_ADVANCED && !newly_lost)
27229ad084d6SYuchung Cheng sndcnt++;
27239ad084d6SYuchung Cheng sndcnt = min(delta, sndcnt);
2724fb4d3d1dSYuchung Cheng }
272531ba0c10SYuchung Cheng /* Force a fast retransmit upon entering fast recovery */
272631ba0c10SYuchung Cheng sndcnt = max(sndcnt, (tp->prr_out ? 0 : 1));
272740570375SEric Dumazet tcp_snd_cwnd_set(tp, tcp_packets_in_flight(tp) + sndcnt);
2728fb4d3d1dSYuchung Cheng }
2729fb4d3d1dSYuchung Cheng
tcp_end_cwnd_reduction(struct sock * sk)2730684bad11SYuchung Cheng static inline void tcp_end_cwnd_reduction(struct sock *sk)
27311da177e4SLinus Torvalds {
27326687e988SArnaldo Carvalho de Melo struct tcp_sock *tp = tcp_sk(sk);
2733a262f0cdSNandita Dukkipati
2734c0402760SYuchung Cheng if (inet_csk(sk)->icsk_ca_ops->cong_control)
2735c0402760SYuchung Cheng return;
2736c0402760SYuchung Cheng
2737684bad11SYuchung Cheng /* Reset cwnd to ssthresh in CWR or Recovery (unless it's undone) */
2738ed254971SYuchung Cheng if (tp->snd_ssthresh < TCP_INFINITE_SSTHRESH &&
2739ed254971SYuchung Cheng (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR || tp->undo_marker)) {
274040570375SEric Dumazet tcp_snd_cwnd_set(tp, tp->snd_ssthresh);
2741c2203cf7SEric Dumazet tp->snd_cwnd_stamp = tcp_jiffies32;
274267d4120aSYuchung Cheng }
27436687e988SArnaldo Carvalho de Melo tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR);
27441da177e4SLinus Torvalds }
27451da177e4SLinus Torvalds
2746684bad11SYuchung Cheng /* Enter CWR state. Disable cwnd undo since congestion is proven with ECN */
tcp_enter_cwr(struct sock * sk)27475ee2c941SChristoph Paasch void tcp_enter_cwr(struct sock *sk)
274809484d1fSYuchung Cheng {
274909484d1fSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
275009484d1fSYuchung Cheng
275109484d1fSYuchung Cheng tp->prior_ssthresh = 0;
2752684bad11SYuchung Cheng if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
275309484d1fSYuchung Cheng tp->undo_marker = 0;
27545ee2c941SChristoph Paasch tcp_init_cwnd_reduction(sk);
275509484d1fSYuchung Cheng tcp_set_ca_state(sk, TCP_CA_CWR);
275609484d1fSYuchung Cheng }
275709484d1fSYuchung Cheng }
27587782ad8bSKenneth Klette Jonassen EXPORT_SYMBOL(tcp_enter_cwr);
275909484d1fSYuchung Cheng
tcp_try_keep_open(struct sock * sk)27608aca6cb1SIlpo Järvinen static void tcp_try_keep_open(struct sock *sk)
27618aca6cb1SIlpo Järvinen {
27628aca6cb1SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
27638aca6cb1SIlpo Järvinen int state = TCP_CA_Open;
27648aca6cb1SIlpo Järvinen
2765f698204bSNeal Cardwell if (tcp_left_out(tp) || tcp_any_retrans_done(sk))
27668aca6cb1SIlpo Järvinen state = TCP_CA_Disorder;
27678aca6cb1SIlpo Järvinen
27688aca6cb1SIlpo Järvinen if (inet_csk(sk)->icsk_ca_state != state) {
27698aca6cb1SIlpo Järvinen tcp_set_ca_state(sk, state);
27708aca6cb1SIlpo Järvinen tp->high_seq = tp->snd_nxt;
27718aca6cb1SIlpo Järvinen }
27728aca6cb1SIlpo Järvinen }
27738aca6cb1SIlpo Järvinen
tcp_try_to_open(struct sock * sk,int flag)277431ba0c10SYuchung Cheng static void tcp_try_to_open(struct sock *sk, int flag)
27751da177e4SLinus Torvalds {
27769e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
27779e412ba7SIlpo Järvinen
277886426c22SIlpo Järvinen tcp_verify_left_out(tp);
277986426c22SIlpo Järvinen
27809b44190dSYuchung Cheng if (!tcp_any_retrans_done(sk))
27811da177e4SLinus Torvalds tp->retrans_stamp = 0;
27821da177e4SLinus Torvalds
27831da177e4SLinus Torvalds if (flag & FLAG_ECE)
27845ee2c941SChristoph Paasch tcp_enter_cwr(sk);
27851da177e4SLinus Torvalds
27866687e988SArnaldo Carvalho de Melo if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) {
27878aca6cb1SIlpo Järvinen tcp_try_keep_open(sk);
27881da177e4SLinus Torvalds }
27891da177e4SLinus Torvalds }
27901da177e4SLinus Torvalds
tcp_mtup_probe_failed(struct sock * sk)27915d424d5aSJohn Heffner static void tcp_mtup_probe_failed(struct sock *sk)
27925d424d5aSJohn Heffner {
27935d424d5aSJohn Heffner struct inet_connection_sock *icsk = inet_csk(sk);
27945d424d5aSJohn Heffner
27955d424d5aSJohn Heffner icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1;
27965d424d5aSJohn Heffner icsk->icsk_mtup.probe_size = 0;
2797c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPFAIL);
27985d424d5aSJohn Heffner }
27995d424d5aSJohn Heffner
tcp_mtup_probe_success(struct sock * sk)280072211e90SIlpo Järvinen static void tcp_mtup_probe_success(struct sock *sk)
28015d424d5aSJohn Heffner {
28025d424d5aSJohn Heffner struct tcp_sock *tp = tcp_sk(sk);
28035d424d5aSJohn Heffner struct inet_connection_sock *icsk = inet_csk(sk);
280411825765SEric Dumazet u64 val;
28055d424d5aSJohn Heffner
28065d424d5aSJohn Heffner tp->prior_ssthresh = tcp_current_ssthresh(sk);
280711825765SEric Dumazet
280811825765SEric Dumazet val = (u64)tcp_snd_cwnd(tp) * tcp_mss_to_mtu(sk, tp->mss_cache);
280911825765SEric Dumazet do_div(val, icsk->icsk_mtup.probe_size);
281011825765SEric Dumazet DEBUG_NET_WARN_ON_ONCE((u32)val != val);
281111825765SEric Dumazet tcp_snd_cwnd_set(tp, max_t(u32, 1U, val));
281211825765SEric Dumazet
28135d424d5aSJohn Heffner tp->snd_cwnd_cnt = 0;
2814c2203cf7SEric Dumazet tp->snd_cwnd_stamp = tcp_jiffies32;
28159c6d5e55SJohn Heffner tp->snd_ssthresh = tcp_current_ssthresh(sk);
28165d424d5aSJohn Heffner
28175d424d5aSJohn Heffner icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size;
28185d424d5aSJohn Heffner icsk->icsk_mtup.probe_size = 0;
28195d424d5aSJohn Heffner tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
2820c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPSUCCESS);
28215d424d5aSJohn Heffner }
28225d424d5aSJohn Heffner
28235dfe9d27SNeal Cardwell /* Sometimes we deduce that packets have been dropped due to reasons other than
28245dfe9d27SNeal Cardwell * congestion, like path MTU reductions or failed client TFO attempts. In these
28255dfe9d27SNeal Cardwell * cases we call this function to retransmit as many packets as cwnd allows,
28265dfe9d27SNeal Cardwell * without reducing cwnd. Given that retransmits will set retrans_stamp to a
28275dfe9d27SNeal Cardwell * non-zero value (and may do so in a later calling context due to TSQ), we
28285dfe9d27SNeal Cardwell * also enter CA_Loss so that we track when all retransmitted packets are ACKed
28295dfe9d27SNeal Cardwell * and clear retrans_stamp when that happens (to ensure later recurring RTOs
28305dfe9d27SNeal Cardwell * are using the correct retrans_stamp and don't declare ETIMEDOUT
28315dfe9d27SNeal Cardwell * prematurely).
28325dfe9d27SNeal Cardwell */
tcp_non_congestion_loss_retransmit(struct sock * sk)28335dfe9d27SNeal Cardwell static void tcp_non_congestion_loss_retransmit(struct sock *sk)
28345dfe9d27SNeal Cardwell {
28355dfe9d27SNeal Cardwell const struct inet_connection_sock *icsk = inet_csk(sk);
28365dfe9d27SNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
28375dfe9d27SNeal Cardwell
28385dfe9d27SNeal Cardwell if (icsk->icsk_ca_state != TCP_CA_Loss) {
28395dfe9d27SNeal Cardwell tp->high_seq = tp->snd_nxt;
28405dfe9d27SNeal Cardwell tp->snd_ssthresh = tcp_current_ssthresh(sk);
28415dfe9d27SNeal Cardwell tp->prior_ssthresh = 0;
28425dfe9d27SNeal Cardwell tp->undo_marker = 0;
28435dfe9d27SNeal Cardwell tcp_set_ca_state(sk, TCP_CA_Loss);
28445dfe9d27SNeal Cardwell }
28455dfe9d27SNeal Cardwell tcp_xmit_retransmit_queue(sk);
28465dfe9d27SNeal Cardwell }
28475dfe9d27SNeal Cardwell
2848e1aa680fSIlpo Järvinen /* Do a simple retransmit without using the backoff mechanisms in
2849e1aa680fSIlpo Järvinen * tcp_timer. This is used for path mtu discovery.
2850e1aa680fSIlpo Järvinen * The socket is already locked here.
2851e1aa680fSIlpo Järvinen */
tcp_simple_retransmit(struct sock * sk)2852e1aa680fSIlpo Järvinen void tcp_simple_retransmit(struct sock *sk)
2853e1aa680fSIlpo Järvinen {
2854e1aa680fSIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
2855e1aa680fSIlpo Järvinen struct sk_buff *skb;
2856c31b70c9SAlexander Duyck int mss;
2857c31b70c9SAlexander Duyck
2858c31b70c9SAlexander Duyck /* A fastopen SYN request is stored as two separate packets within
2859c31b70c9SAlexander Duyck * the retransmit queue, this is done by tcp_send_syn_data().
2860c31b70c9SAlexander Duyck * As a result simply checking the MSS of the frames in the queue
2861c31b70c9SAlexander Duyck * will not work for the SYN packet.
2862c31b70c9SAlexander Duyck *
2863c31b70c9SAlexander Duyck * Us being here is an indication of a path MTU issue so we can
2864c31b70c9SAlexander Duyck * assume that the fastopen SYN was lost and just mark all the
2865c31b70c9SAlexander Duyck * frames in the retransmit queue as lost. We will use an MSS of
2866c31b70c9SAlexander Duyck * -1 to mark all frames as lost, otherwise compute the current MSS.
2867c31b70c9SAlexander Duyck */
2868c31b70c9SAlexander Duyck if (tp->syn_data && sk->sk_state == TCP_SYN_SENT)
2869c31b70c9SAlexander Duyck mss = -1;
2870c31b70c9SAlexander Duyck else
2871c31b70c9SAlexander Duyck mss = tcp_current_mss(sk);
2872e1aa680fSIlpo Järvinen
287375c119afSEric Dumazet skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
287468698970SYuchung Cheng if (tcp_skb_seglen(skb) > mss)
2875179ac35fSYuchung Cheng tcp_mark_skb_lost(sk, skb);
2876e1aa680fSIlpo Järvinen }
2877e1aa680fSIlpo Järvinen
2878e1aa680fSIlpo Järvinen tcp_clear_retrans_hints_partial(tp);
2879e1aa680fSIlpo Järvinen
28800eb96bf7SYuchung Cheng if (!tp->lost_out)
2881e1aa680fSIlpo Järvinen return;
2882e1aa680fSIlpo Järvinen
2883e1aa680fSIlpo Järvinen if (tcp_is_reno(tp))
2884e1aa680fSIlpo Järvinen tcp_limit_reno_sacked(tp);
2885e1aa680fSIlpo Järvinen
2886e1aa680fSIlpo Järvinen tcp_verify_left_out(tp);
2887e1aa680fSIlpo Järvinen
2888e1aa680fSIlpo Järvinen /* Don't muck with the congestion window here.
2889e1aa680fSIlpo Järvinen * Reason is that we do not increase amount of _data_
2890e1aa680fSIlpo Järvinen * in network, but units changed and effective
2891e1aa680fSIlpo Järvinen * cwnd/ssthresh really reduced now.
2892e1aa680fSIlpo Järvinen */
28935dfe9d27SNeal Cardwell tcp_non_congestion_loss_retransmit(sk);
2894e1aa680fSIlpo Järvinen }
28954bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_simple_retransmit);
2896e1aa680fSIlpo Järvinen
tcp_enter_recovery(struct sock * sk,bool ece_ack)289757dde7f7SYuchung Cheng void tcp_enter_recovery(struct sock *sk, bool ece_ack)
28981fbc3405SYuchung Cheng {
28991fbc3405SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
29001fbc3405SYuchung Cheng int mib_idx;
29011fbc3405SYuchung Cheng
2902b41b4cbdSNeal Cardwell /* Start the clock with our fast retransmit, for undo and ETIMEDOUT. */
2903b41b4cbdSNeal Cardwell tcp_retrans_stamp_cleanup(sk);
2904b41b4cbdSNeal Cardwell
29051fbc3405SYuchung Cheng if (tcp_is_reno(tp))
29061fbc3405SYuchung Cheng mib_idx = LINUX_MIB_TCPRENORECOVERY;
29071fbc3405SYuchung Cheng else
29081fbc3405SYuchung Cheng mib_idx = LINUX_MIB_TCPSACKRECOVERY;
29091fbc3405SYuchung Cheng
2910c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), mib_idx);
29111fbc3405SYuchung Cheng
29121fbc3405SYuchung Cheng tp->prior_ssthresh = 0;
2913989e04c5SYuchung Cheng tcp_init_undo(tp);
29141fbc3405SYuchung Cheng
2915291a00d1SYuchung Cheng if (!tcp_in_cwnd_reduction(sk)) {
29161fbc3405SYuchung Cheng if (!ece_ack)
29171fbc3405SYuchung Cheng tp->prior_ssthresh = tcp_current_ssthresh(sk);
29185ee2c941SChristoph Paasch tcp_init_cwnd_reduction(sk);
29191fbc3405SYuchung Cheng }
29201fbc3405SYuchung Cheng tcp_set_ca_state(sk, TCP_CA_Recovery);
29211fbc3405SYuchung Cheng }
29221fbc3405SYuchung Cheng
tcp_update_rto_time(struct tcp_sock * tp)29233868ab0fSAananth V static void tcp_update_rto_time(struct tcp_sock *tp)
29243868ab0fSAananth V {
29253868ab0fSAananth V if (tp->rto_stamp) {
292699d67955SEric Dumazet tp->total_rto_time += tcp_time_stamp_ms(tp) - tp->rto_stamp;
29273868ab0fSAananth V tp->rto_stamp = 0;
29283868ab0fSAananth V }
29293868ab0fSAananth V }
29303868ab0fSAananth V
2931ab42d9eeSYuchung Cheng /* Process an ACK in CA_Loss state. Move to CA_Open if lost data are
2932ab42d9eeSYuchung Cheng * recovered or spurious. Otherwise retransmits more on partial ACKs.
2933ab42d9eeSYuchung Cheng */
tcp_process_loss(struct sock * sk,int flag,int num_dupack,int * rexmit)293419119f29SEric Dumazet static void tcp_process_loss(struct sock *sk, int flag, int num_dupack,
2935e662ca40SYuchung Cheng int *rexmit)
2936ab42d9eeSYuchung Cheng {
2937ab42d9eeSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
2938e33099f9SYuchung Cheng bool recovered = !before(tp->snd_una, tp->high_seq);
2939ab42d9eeSYuchung Cheng
2940d983ea6fSEric Dumazet if ((flag & FLAG_SND_UNA_ADVANCED || rcu_access_pointer(tp->fastopen_rsk)) &&
2941da34ac76SYuchung Cheng tcp_try_undo_loss(sk, false))
2942da34ac76SYuchung Cheng return;
2943da34ac76SYuchung Cheng
2944fc68e171SYuchung Cheng if (tp->frto) { /* F-RTO RFC5682 sec 3.1 (sack enhanced version). */
2945fc68e171SYuchung Cheng /* Step 3.b. A timeout is spurious if not all data are
2946fc68e171SYuchung Cheng * lost, i.e., never-retransmitted data are (s)acked.
2947e33099f9SYuchung Cheng */
2948da34ac76SYuchung Cheng if ((flag & FLAG_ORIG_SACK_ACKED) &&
2949fc68e171SYuchung Cheng tcp_try_undo_loss(sk, true))
2950e33099f9SYuchung Cheng return;
29510cfa5c07SYuchung Cheng
2952b7b0ed91SYuchung Cheng if (after(tp->snd_nxt, tp->high_seq)) {
295319119f29SEric Dumazet if (flag & FLAG_DATA_SACKED || num_dupack)
2954b7b0ed91SYuchung Cheng tp->frto = 0; /* Step 3.a. loss was real */
2955e33099f9SYuchung Cheng } else if (flag & FLAG_SND_UNA_ADVANCED && !recovered) {
2956e33099f9SYuchung Cheng tp->high_seq = tp->snd_nxt;
2957e662ca40SYuchung Cheng /* Step 2.b. Try send new data (but deferred until cwnd
2958e662ca40SYuchung Cheng * is updated in tcp_ack()). Otherwise fall back to
2959e662ca40SYuchung Cheng * the conventional recovery.
2960e662ca40SYuchung Cheng */
296175c119afSEric Dumazet if (!tcp_write_queue_empty(sk) &&
2962e662ca40SYuchung Cheng after(tcp_wnd_end(tp), tp->snd_nxt)) {
2963e662ca40SYuchung Cheng *rexmit = REXMIT_NEW;
2964e662ca40SYuchung Cheng return;
2965e662ca40SYuchung Cheng }
2966e33099f9SYuchung Cheng tp->frto = 0;
2967e33099f9SYuchung Cheng }
2968e33099f9SYuchung Cheng }
2969e33099f9SYuchung Cheng
2970e33099f9SYuchung Cheng if (recovered) {
2971e33099f9SYuchung Cheng /* F-RTO RFC5682 sec 3.1 step 2.a and 1st part of step 3.a */
2972ab42d9eeSYuchung Cheng tcp_try_undo_recovery(sk);
2973ab42d9eeSYuchung Cheng return;
2974ab42d9eeSYuchung Cheng }
2975e33099f9SYuchung Cheng if (tcp_is_reno(tp)) {
2976e33099f9SYuchung Cheng /* A Reno DUPACK means new data in F-RTO step 2.b above are
2977304b1875SYueh-Shun Li * delivered. Lower inflight to clock out (re)transmissions.
2978e33099f9SYuchung Cheng */
297919119f29SEric Dumazet if (after(tp->snd_nxt, tp->high_seq) && num_dupack)
2980c634e34fSYousuk Seung tcp_add_reno_sack(sk, num_dupack, flag & FLAG_ECE);
2981e33099f9SYuchung Cheng else if (flag & FLAG_SND_UNA_ADVANCED)
2982ab42d9eeSYuchung Cheng tcp_reset_reno_sack(tp);
2983e33099f9SYuchung Cheng }
2984e662ca40SYuchung Cheng *rexmit = REXMIT_LOST;
2985ab42d9eeSYuchung Cheng }
2986ab42d9eeSYuchung Cheng
tcp_force_fast_retransmit(struct sock * sk)2987a29cb691SYuchung Cheng static bool tcp_force_fast_retransmit(struct sock *sk)
2988a29cb691SYuchung Cheng {
2989a29cb691SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
2990a29cb691SYuchung Cheng
2991a29cb691SYuchung Cheng return after(tcp_highest_sack_seq(tp),
2992a29cb691SYuchung Cheng tp->snd_una + tp->reordering * tp->mss_cache);
2993a29cb691SYuchung Cheng }
2994a29cb691SYuchung Cheng
29956a63df46SYuchung Cheng /* Undo during fast recovery after partial ACK. */
tcp_try_undo_partial(struct sock * sk,u32 prior_snd_una,bool * do_lost)2996a29cb691SYuchung Cheng static bool tcp_try_undo_partial(struct sock *sk, u32 prior_snd_una,
2997a29cb691SYuchung Cheng bool *do_lost)
29986a63df46SYuchung Cheng {
29996a63df46SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
30006a63df46SYuchung Cheng
30017026b912SYuchung Cheng if (tp->undo_marker && tcp_packet_delayed(tp)) {
30026a63df46SYuchung Cheng /* Plain luck! Hole if filled with delayed
3003737ff314SYuchung Cheng * packet, rather than with a retransmit. Check reordering.
30046a63df46SYuchung Cheng */
3005737ff314SYuchung Cheng tcp_check_sack_reordering(sk, prior_snd_una, 1);
30067026b912SYuchung Cheng
30077026b912SYuchung Cheng /* We are getting evidence that the reordering degree is higher
30087026b912SYuchung Cheng * than we realized. If there are no retransmits out then we
30097026b912SYuchung Cheng * can undo. Otherwise we clock out new packets but do not
30107026b912SYuchung Cheng * mark more packets lost or retransmit more.
30117026b912SYuchung Cheng */
301231ba0c10SYuchung Cheng if (tp->retrans_out)
30137026b912SYuchung Cheng return true;
30147026b912SYuchung Cheng
30156a63df46SYuchung Cheng if (!tcp_any_retrans_done(sk))
30166a63df46SYuchung Cheng tp->retrans_stamp = 0;
30176a63df46SYuchung Cheng
30187026b912SYuchung Cheng DBGUNDO(sk, "partial recovery");
30197026b912SYuchung Cheng tcp_undo_cwnd_reduction(sk, true);
3020c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO);
30217026b912SYuchung Cheng tcp_try_keep_open(sk);
3022a29cb691SYuchung Cheng } else {
3023a29cb691SYuchung Cheng /* Partial ACK arrived. Force fast retransmit. */
3024a29cb691SYuchung Cheng *do_lost = tcp_force_fast_retransmit(sk);
30256a63df46SYuchung Cheng }
30267026b912SYuchung Cheng return false;
30276a63df46SYuchung Cheng }
30286a63df46SYuchung Cheng
tcp_identify_packet_loss(struct sock * sk,int * ack_flag)30296ac06ecdSYuchung Cheng static void tcp_identify_packet_loss(struct sock *sk, int *ack_flag)
303098e36d44SYuchung Cheng {
303198e36d44SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
303298e36d44SYuchung Cheng
30336ac06ecdSYuchung Cheng if (tcp_rtx_queue_empty(sk))
30346ac06ecdSYuchung Cheng return;
30356ac06ecdSYuchung Cheng
30366ac06ecdSYuchung Cheng if (unlikely(tcp_is_reno(tp))) {
30376ac06ecdSYuchung Cheng tcp_newreno_mark_lost(sk, *ack_flag & FLAG_SND_UNA_ADVANCED);
30386ac06ecdSYuchung Cheng } else if (tcp_is_rack(sk)) {
303998e36d44SYuchung Cheng u32 prior_retrans = tp->retrans_out;
304098e36d44SYuchung Cheng
304162d9f1a6SPengcheng Yang if (tcp_rack_mark_lost(sk))
304262d9f1a6SPengcheng Yang *ack_flag &= ~FLAG_SET_XMIT_TIMER;
304398e36d44SYuchung Cheng if (prior_retrans > tp->retrans_out)
304498e36d44SYuchung Cheng *ack_flag |= FLAG_LOST_RETRANS;
304598e36d44SYuchung Cheng }
304698e36d44SYuchung Cheng }
304798e36d44SYuchung Cheng
30481da177e4SLinus Torvalds /* Process an event, which can update packets-in-flight not trivially.
30491da177e4SLinus Torvalds * Main goal of this function is to calculate new estimate for left_out,
30501da177e4SLinus Torvalds * taking into account both packets sitting in receiver's buffer and
30511da177e4SLinus Torvalds * packets lost by network.
30521da177e4SLinus Torvalds *
305331ba0c10SYuchung Cheng * Besides that it updates the congestion state when packet loss or ECN
305431ba0c10SYuchung Cheng * is detected. But it does not reduce the cwnd, it is done by the
305531ba0c10SYuchung Cheng * congestion control later.
30561da177e4SLinus Torvalds *
30571da177e4SLinus Torvalds * It does _not_ decide what to send, it is made in function
30581da177e4SLinus Torvalds * tcp_xmit_retransmit_queue().
30591da177e4SLinus Torvalds */
tcp_fastretrans_alert(struct sock * sk,const u32 prior_snd_una,int num_dupack,int * ack_flag,int * rexmit)3060737ff314SYuchung Cheng static void tcp_fastretrans_alert(struct sock *sk, const u32 prior_snd_una,
306119119f29SEric Dumazet int num_dupack, int *ack_flag, int *rexmit)
30621da177e4SLinus Torvalds {
30636687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
30641da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
306531ba0c10SYuchung Cheng int fast_rexmit = 0, flag = *ack_flag;
3066c634e34fSYousuk Seung bool ece_ack = flag & FLAG_ECE;
306719119f29SEric Dumazet bool do_lost = num_dupack || ((flag & FLAG_DATA_SACKED) &&
3068737ff314SYuchung Cheng tcp_force_fast_retransmit(sk));
30691da177e4SLinus Torvalds
30708ba6ddaaSEric Dumazet if (!tp->packets_out && tp->sacked_out)
30711da177e4SLinus Torvalds tp->sacked_out = 0;
30721da177e4SLinus Torvalds
30731da177e4SLinus Torvalds /* Now state machine starts.
30741da177e4SLinus Torvalds * A. ECE, hence prohibit cwnd undoing, the reduction is required. */
3075c634e34fSYousuk Seung if (ece_ack)
30761da177e4SLinus Torvalds tp->prior_ssthresh = 0;
30771da177e4SLinus Torvalds
30781da177e4SLinus Torvalds /* B. In all the states check for reneging SACKs. */
3079d2a0fc37SFred Chen if (tcp_check_sack_reneging(sk, ack_flag))
30801da177e4SLinus Torvalds return;
30811da177e4SLinus Torvalds
3082974c1236SYuchung Cheng /* C. Check consistency of the current state. */
3083005903bcSIlpo Järvinen tcp_verify_left_out(tp);
30841da177e4SLinus Torvalds
3085974c1236SYuchung Cheng /* D. Check state exit conditions. State can be terminated
30861da177e4SLinus Torvalds * when high_seq is ACKed. */
30876687e988SArnaldo Carvalho de Melo if (icsk->icsk_ca_state == TCP_CA_Open) {
3088a7abf3cdSEric Dumazet WARN_ON(tp->retrans_out != 0 && !tp->syn_data);
30891da177e4SLinus Torvalds tp->retrans_stamp = 0;
30901da177e4SLinus Torvalds } else if (!before(tp->snd_una, tp->high_seq)) {
30916687e988SArnaldo Carvalho de Melo switch (icsk->icsk_ca_state) {
30921da177e4SLinus Torvalds case TCP_CA_CWR:
30931da177e4SLinus Torvalds /* CWR is to be held something *above* high_seq
30941da177e4SLinus Torvalds * is ACKed for CWR bit to reach receiver. */
30951da177e4SLinus Torvalds if (tp->snd_una != tp->high_seq) {
3096684bad11SYuchung Cheng tcp_end_cwnd_reduction(sk);
30976687e988SArnaldo Carvalho de Melo tcp_set_ca_state(sk, TCP_CA_Open);
30981da177e4SLinus Torvalds }
30991da177e4SLinus Torvalds break;
31001da177e4SLinus Torvalds
31011da177e4SLinus Torvalds case TCP_CA_Recovery:
3102e60402d0SIlpo Järvinen if (tcp_is_reno(tp))
31031da177e4SLinus Torvalds tcp_reset_reno_sack(tp);
31049e412ba7SIlpo Järvinen if (tcp_try_undo_recovery(sk))
31051da177e4SLinus Torvalds return;
3106684bad11SYuchung Cheng tcp_end_cwnd_reduction(sk);
31071da177e4SLinus Torvalds break;
31081da177e4SLinus Torvalds }
31091da177e4SLinus Torvalds }
31101da177e4SLinus Torvalds
3111974c1236SYuchung Cheng /* E. Process state. */
31126687e988SArnaldo Carvalho de Melo switch (icsk->icsk_ca_state) {
31131da177e4SLinus Torvalds case TCP_CA_Recovery:
31142e605294SIlpo Järvinen if (!(flag & FLAG_SND_UNA_ADVANCED)) {
311519119f29SEric Dumazet if (tcp_is_reno(tp))
3116c634e34fSYousuk Seung tcp_add_reno_sack(sk, num_dupack, ece_ack);
3117a29cb691SYuchung Cheng } else if (tcp_try_undo_partial(sk, prior_snd_una, &do_lost))
31187026b912SYuchung Cheng return;
3119a29cb691SYuchung Cheng
3120a29cb691SYuchung Cheng if (tcp_try_undo_dsack(sk))
3121a6458ab7SNeal Cardwell tcp_try_to_open(sk, flag);
3122a29cb691SYuchung Cheng
31236ac06ecdSYuchung Cheng tcp_identify_packet_loss(sk, ack_flag);
3124a29cb691SYuchung Cheng if (icsk->icsk_ca_state != TCP_CA_Recovery) {
3125a29cb691SYuchung Cheng if (!tcp_time_to_recover(sk, flag))
3126a29cb691SYuchung Cheng return;
3127a29cb691SYuchung Cheng /* Undo reverts the recovery state. If loss is evident,
3128a29cb691SYuchung Cheng * starts a new recovery (e.g. reordering then loss);
3129a29cb691SYuchung Cheng */
3130a29cb691SYuchung Cheng tcp_enter_recovery(sk, ece_ack);
3131a29cb691SYuchung Cheng }
31321da177e4SLinus Torvalds break;
31331da177e4SLinus Torvalds case TCP_CA_Loss:
313419119f29SEric Dumazet tcp_process_loss(sk, flag, num_dupack, rexmit);
31353868ab0fSAananth V if (icsk->icsk_ca_state != TCP_CA_Loss)
31363868ab0fSAananth V tcp_update_rto_time(tp);
31376ac06ecdSYuchung Cheng tcp_identify_packet_loss(sk, ack_flag);
313898e36d44SYuchung Cheng if (!(icsk->icsk_ca_state == TCP_CA_Open ||
313998e36d44SYuchung Cheng (*ack_flag & FLAG_LOST_RETRANS)))
31401da177e4SLinus Torvalds return;
3141291a00d1SYuchung Cheng /* Change state if cwnd is undone or retransmits are lost */
3142a8eceea8SJoe Perches fallthrough;
31431da177e4SLinus Torvalds default:
3144e60402d0SIlpo Järvinen if (tcp_is_reno(tp)) {
31452e605294SIlpo Järvinen if (flag & FLAG_SND_UNA_ADVANCED)
31461da177e4SLinus Torvalds tcp_reset_reno_sack(tp);
3147c634e34fSYousuk Seung tcp_add_reno_sack(sk, num_dupack, ece_ack);
31481da177e4SLinus Torvalds }
31491da177e4SLinus Torvalds
3150f698204bSNeal Cardwell if (icsk->icsk_ca_state <= TCP_CA_Disorder)
31519e412ba7SIlpo Järvinen tcp_try_undo_dsack(sk);
31521da177e4SLinus Torvalds
31536ac06ecdSYuchung Cheng tcp_identify_packet_loss(sk, ack_flag);
3154750ea2baSYuchung Cheng if (!tcp_time_to_recover(sk, flag)) {
315531ba0c10SYuchung Cheng tcp_try_to_open(sk, flag);
31561da177e4SLinus Torvalds return;
31571da177e4SLinus Torvalds }
31581da177e4SLinus Torvalds
31595d424d5aSJohn Heffner /* MTU probe failure: don't reduce cwnd */
31605d424d5aSJohn Heffner if (icsk->icsk_ca_state < TCP_CA_CWR &&
31615d424d5aSJohn Heffner icsk->icsk_mtup.probe_size &&
31620e7b1368SJohn Heffner tp->snd_una == tp->mtu_probe.probe_seq_start) {
31635d424d5aSJohn Heffner tcp_mtup_probe_failed(sk);
31645d424d5aSJohn Heffner /* Restores the reduction we did in tcp_mtup_probe() */
316540570375SEric Dumazet tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
31665d424d5aSJohn Heffner tcp_simple_retransmit(sk);
31675d424d5aSJohn Heffner return;
31685d424d5aSJohn Heffner }
31695d424d5aSJohn Heffner
31701da177e4SLinus Torvalds /* Otherwise enter Recovery state */
3171c634e34fSYousuk Seung tcp_enter_recovery(sk, ece_ack);
317285cc391cSIlpo Järvinen fast_rexmit = 1;
31731da177e4SLinus Torvalds }
31741da177e4SLinus Torvalds
3175b38a51feSYuchung Cheng if (!tcp_is_rack(sk) && do_lost)
317685cc391cSIlpo Järvinen tcp_update_scoreboard(sk, fast_rexmit);
3177e662ca40SYuchung Cheng *rexmit = REXMIT_LOST;
31781da177e4SLinus Torvalds }
31791da177e4SLinus Torvalds
tcp_update_rtt_min(struct sock * sk,u32 rtt_us,const int flag)3180eb36be0fSYuchung Cheng static void tcp_update_rtt_min(struct sock *sk, u32 rtt_us, const int flag)
3181f6722583SYuchung Cheng {
31821330ffacSKuniyuki Iwashima u32 wlen = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_rtt_wlen) * HZ;
318364033892SNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
3184f6722583SYuchung Cheng
3185eb36be0fSYuchung Cheng if ((flag & FLAG_ACK_MAYBE_DELAYED) && rtt_us > tcp_min_rtt(tp)) {
3186eb36be0fSYuchung Cheng /* If the remote keeps returning delayed ACKs, eventually
3187eb36be0fSYuchung Cheng * the min filter would pick it up and overestimate the
3188eb36be0fSYuchung Cheng * prop. delay when it expires. Skip suspected delayed ACKs.
3189eb36be0fSYuchung Cheng */
3190eb36be0fSYuchung Cheng return;
3191eb36be0fSYuchung Cheng }
3192ac9517fcSEric Dumazet minmax_running_min(&tp->rtt_min, wlen, tcp_jiffies32,
319364033892SNeal Cardwell rtt_us ? : jiffies_to_usecs(1));
3194f6722583SYuchung Cheng }
3195f6722583SYuchung Cheng
tcp_ack_update_rtt(struct sock * sk,const int flag,long seq_rtt_us,long sack_rtt_us,long ca_rtt_us,struct rate_sample * rs)3196775e68a9SYuchung Cheng static bool tcp_ack_update_rtt(struct sock *sk, const int flag,
3197f6722583SYuchung Cheng long seq_rtt_us, long sack_rtt_us,
3198775e68a9SYuchung Cheng long ca_rtt_us, struct rate_sample *rs)
319941834b73SIlpo Järvinen {
32005b08e47cSYuchung Cheng const struct tcp_sock *tp = tcp_sk(sk);
320141834b73SIlpo Järvinen
32025b08e47cSYuchung Cheng /* Prefer RTT measured from ACK's timing to TS-ECR. This is because
32035b08e47cSYuchung Cheng * broken middle-boxes or peers may corrupt TS-ECR fields. But
32045b08e47cSYuchung Cheng * Karn's algorithm forbids taking RTT if some retransmitted data
32055b08e47cSYuchung Cheng * is acked (RFC6298).
32061da177e4SLinus Torvalds */
3207740b0f18SEric Dumazet if (seq_rtt_us < 0)
3208740b0f18SEric Dumazet seq_rtt_us = sack_rtt_us;
3209ed08495cSYuchung Cheng
32101da177e4SLinus Torvalds /* RTTM Rule: A TSecr value received in a segment is used to
32111da177e4SLinus Torvalds * update the averaged RTT measurement only if the segment
32121da177e4SLinus Torvalds * acknowledges some new data, i.e., only if it advances the
32131da177e4SLinus Torvalds * left edge of the send window.
32141da177e4SLinus Torvalds * See draft-ietf-tcplw-high-performance-00, section 3.3.
32151da177e4SLinus Torvalds */
3216b04c3320SEric Dumazet if (seq_rtt_us < 0 && tp->rx_opt.saw_tstamp &&
3217b04c3320SEric Dumazet tp->rx_opt.rcv_tsecr && flag & FLAG_ACKED)
3218b04c3320SEric Dumazet seq_rtt_us = ca_rtt_us = tcp_rtt_tsopt_us(tp);
32199a568de4SEric Dumazet
3220775e68a9SYuchung Cheng rs->rtt_us = ca_rtt_us; /* RTT of last (S)ACKed packet (or -1) */
3221740b0f18SEric Dumazet if (seq_rtt_us < 0)
3222ed08495cSYuchung Cheng return false;
32231da177e4SLinus Torvalds
3224f6722583SYuchung Cheng /* ca_rtt_us >= 0 is counting on the invariant that ca_rtt_us is
3225f6722583SYuchung Cheng * always taken together with ACK, SACK, or TS-opts. Any negative
3226f6722583SYuchung Cheng * values will be skipped with the seq_rtt_us < 0 check above.
3227f6722583SYuchung Cheng */
3228eb36be0fSYuchung Cheng tcp_update_rtt_min(sk, ca_rtt_us, flag);
3229740b0f18SEric Dumazet tcp_rtt_estimator(sk, seq_rtt_us);
32305b08e47cSYuchung Cheng tcp_set_rto(sk);
32311da177e4SLinus Torvalds
32325b08e47cSYuchung Cheng /* RFC6298: only reset backoff on valid RTT measurement. */
32335b08e47cSYuchung Cheng inet_csk(sk)->icsk_backoff = 0;
3234ed08495cSYuchung Cheng return true;
32351da177e4SLinus Torvalds }
32361da177e4SLinus Torvalds
3237375fe02cSYuchung Cheng /* Compute time elapsed between (last) SYNACK and the ACK completing 3WHS. */
tcp_synack_rtt_meas(struct sock * sk,struct request_sock * req)32380f1c28aeSYuchung Cheng void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req)
3239375fe02cSYuchung Cheng {
3240775e68a9SYuchung Cheng struct rate_sample rs;
32410f1c28aeSYuchung Cheng long rtt_us = -1L;
3242375fe02cSYuchung Cheng
32439a568de4SEric Dumazet if (req && !req->num_retrans && tcp_rsk(req)->snt_synack)
32449a568de4SEric Dumazet rtt_us = tcp_stamp_us_delta(tcp_clock_us(), tcp_rsk(req)->snt_synack);
3245375fe02cSYuchung Cheng
3246775e68a9SYuchung Cheng tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, rtt_us, -1L, rtt_us, &rs);
32470f1c28aeSYuchung Cheng }
32480f1c28aeSYuchung Cheng
32490f1c28aeSYuchung Cheng
tcp_cong_avoid(struct sock * sk,u32 ack,u32 acked)325024901551SEric Dumazet static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
32511da177e4SLinus Torvalds {
32526687e988SArnaldo Carvalho de Melo const struct inet_connection_sock *icsk = inet_csk(sk);
325324901551SEric Dumazet
325424901551SEric Dumazet icsk->icsk_ca_ops->cong_avoid(sk, ack, acked);
3255c2203cf7SEric Dumazet tcp_sk(sk)->snd_cwnd_stamp = tcp_jiffies32;
32561da177e4SLinus Torvalds }
32571da177e4SLinus Torvalds
32581da177e4SLinus Torvalds /* Restart timer after forward progress on connection.
32591da177e4SLinus Torvalds * RFC2988 recommends to restart timer to now+rto.
32601da177e4SLinus Torvalds */
tcp_rearm_rto(struct sock * sk)3261750ea2baSYuchung Cheng void tcp_rearm_rto(struct sock *sk)
32621da177e4SLinus Torvalds {
32636ba8a3b1SNandita Dukkipati const struct inet_connection_sock *icsk = inet_csk(sk);
3264750ea2baSYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
32659e412ba7SIlpo Järvinen
3266168a8f58SJerry Chu /* If the retrans timer is currently being used by Fast Open
3267168a8f58SJerry Chu * for SYN-ACK retrans purpose, stay put.
3268168a8f58SJerry Chu */
3269d983ea6fSEric Dumazet if (rcu_access_pointer(tp->fastopen_rsk))
3270168a8f58SJerry Chu return;
3271168a8f58SJerry Chu
32721da177e4SLinus Torvalds if (!tp->packets_out) {
3273463c84b9SArnaldo Carvalho de Melo inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
32741da177e4SLinus Torvalds } else {
3275750ea2baSYuchung Cheng u32 rto = inet_csk(sk)->icsk_rto;
3276750ea2baSYuchung Cheng /* Offset the time elapsed after installing regular RTO */
3277bec41a11SYuchung Cheng if (icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
32786ba8a3b1SNandita Dukkipati icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
3279e1a10ef7SNeal Cardwell s64 delta_us = tcp_rto_delta_us(sk);
3280b17b8a20SEric Dumazet /* delta_us may not be positive if the socket is locked
32816ba8a3b1SNandita Dukkipati * when the retrans timer fires and is rescheduled.
3282750ea2baSYuchung Cheng */
3283cdbeb633SNeal Cardwell rto = usecs_to_jiffies(max_t(int, delta_us, 1));
32841da177e4SLinus Torvalds }
32853f80e08fSEric Dumazet tcp_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto,
32868dc242adSEric Dumazet TCP_RTO_MAX);
3287750ea2baSYuchung Cheng }
3288750ea2baSYuchung Cheng }
3289750ea2baSYuchung Cheng
3290df92c839SNeal Cardwell /* Try to schedule a loss probe; if that doesn't work, then schedule an RTO. */
tcp_set_xmit_timer(struct sock * sk)3291df92c839SNeal Cardwell static void tcp_set_xmit_timer(struct sock *sk)
3292df92c839SNeal Cardwell {
3293ed66dfafSNeal Cardwell if (!tcp_schedule_loss_probe(sk, true))
3294df92c839SNeal Cardwell tcp_rearm_rto(sk);
3295df92c839SNeal Cardwell }
3296df92c839SNeal Cardwell
32977c46a03eSIlpo Järvinen /* If we get here, the whole TSO packet has not been acked. */
tcp_tso_acked(struct sock * sk,struct sk_buff * skb)329813fcf850SIlpo Järvinen static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)
32991da177e4SLinus Torvalds {
33001da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
33017c46a03eSIlpo Järvinen u32 packets_acked;
33021da177e4SLinus Torvalds
33037c46a03eSIlpo Järvinen BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una));
33041da177e4SLinus Torvalds
33051da177e4SLinus Torvalds packets_acked = tcp_skb_pcount(skb);
33067c46a03eSIlpo Järvinen if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
33071da177e4SLinus Torvalds return 0;
33081da177e4SLinus Torvalds packets_acked -= tcp_skb_pcount(skb);
33091da177e4SLinus Torvalds
33101da177e4SLinus Torvalds if (packets_acked) {
33111da177e4SLinus Torvalds BUG_ON(tcp_skb_pcount(skb) == 0);
33127c46a03eSIlpo Järvinen BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq));
33131da177e4SLinus Torvalds }
33141da177e4SLinus Torvalds
331513fcf850SIlpo Järvinen return packets_acked;
33161da177e4SLinus Torvalds }
33171da177e4SLinus Torvalds
tcp_ack_tstamp(struct sock * sk,struct sk_buff * skb,const struct sk_buff * ack_skb,u32 prior_snd_una)3318ad971f61SEric Dumazet static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb,
3319e7ed11eeSYousuk Seung const struct sk_buff *ack_skb, u32 prior_snd_una)
3320ad971f61SEric Dumazet {
3321ad971f61SEric Dumazet const struct skb_shared_info *shinfo;
3322ad971f61SEric Dumazet
3323ad971f61SEric Dumazet /* Avoid cache line misses to get skb_shinfo() and shinfo->tx_flags */
33246b084928SSoheil Hassas Yeganeh if (likely(!TCP_SKB_CB(skb)->txstamp_ack))
3325ad971f61SEric Dumazet return;
3326ad971f61SEric Dumazet
3327ad971f61SEric Dumazet shinfo = skb_shinfo(skb);
33280a2cf20cSSoheil Hassas Yeganeh if (!before(shinfo->tskey, prior_snd_una) &&
3329e2080072SEric Dumazet before(shinfo->tskey, tcp_sk(sk)->snd_una)) {
3330e2080072SEric Dumazet tcp_skb_tsorted_save(skb) {
3331e7ed11eeSYousuk Seung __skb_tstamp_tx(skb, ack_skb, NULL, sk, SCM_TSTAMP_ACK);
3332e2080072SEric Dumazet } tcp_skb_tsorted_restore(skb);
3333e2080072SEric Dumazet }
3334ad971f61SEric Dumazet }
3335ad971f61SEric Dumazet
33367c46a03eSIlpo Järvinen /* Remove acknowledged frames from the retransmission queue. If our packet
33377c46a03eSIlpo Järvinen * is before the ack sequence we can discard it as it's confirmed to have
33387c46a03eSIlpo Järvinen * arrived at the other end.
33397c46a03eSIlpo Järvinen */
tcp_clean_rtx_queue(struct sock * sk,const struct sk_buff * ack_skb,u32 prior_fack,u32 prior_snd_una,struct tcp_sacktag_state * sack,bool ece_ack)3340e7ed11eeSYousuk Seung static int tcp_clean_rtx_queue(struct sock *sk, const struct sk_buff *ack_skb,
3341e7ed11eeSYousuk Seung u32 prior_fack, u32 prior_snd_una,
3342c634e34fSYousuk Seung struct tcp_sacktag_state *sack, bool ece_ack)
33431da177e4SLinus Torvalds {
33442d2abbabSStephen Hemminger const struct inet_connection_sock *icsk = inet_csk(sk);
33459a568de4SEric Dumazet u64 first_ackt, last_ackt;
3346740b0f18SEric Dumazet struct tcp_sock *tp = tcp_sk(sk);
334790638a04SIlpo Järvinen u32 prior_sacked = tp->sacked_out;
3348737ff314SYuchung Cheng u32 reord = tp->snd_nxt; /* lowest acked un-retx un-sacked seq */
334975c119afSEric Dumazet struct sk_buff *skb, *next;
3350740b0f18SEric Dumazet bool fully_acked = true;
335131231a8aSKenneth Klette Jonassen long sack_rtt_us = -1L;
3352740b0f18SEric Dumazet long seq_rtt_us = -1L;
335331231a8aSKenneth Klette Jonassen long ca_rtt_us = -1L;
3354740b0f18SEric Dumazet u32 pkts_acked = 0;
33552f715c1dSYuchung Cheng bool rtt_update;
3356740b0f18SEric Dumazet int flag = 0;
3357740b0f18SEric Dumazet
33589a568de4SEric Dumazet first_ackt = 0;
33591da177e4SLinus Torvalds
336075c119afSEric Dumazet for (skb = skb_rb_first(&sk->tcp_rtx_queue); skb; skb = next) {
33611da177e4SLinus Torvalds struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
3362737ff314SYuchung Cheng const u32 start_seq = scb->seq;
33637c46a03eSIlpo Järvinen u8 sacked = scb->sacked;
3364740b0f18SEric Dumazet u32 acked_pcount;
33651da177e4SLinus Torvalds
33662072c228SGavin McCullagh /* Determine how many packets and what bytes were acked, tso and else */
33671da177e4SLinus Torvalds if (after(scb->end_seq, tp->snd_una)) {
336813fcf850SIlpo Järvinen if (tcp_skb_pcount(skb) == 1 ||
336913fcf850SIlpo Järvinen !after(tp->snd_una, scb->seq))
33701da177e4SLinus Torvalds break;
337113fcf850SIlpo Järvinen
337272018835SIlpo Järvinen acked_pcount = tcp_tso_acked(sk, skb);
337372018835SIlpo Järvinen if (!acked_pcount)
337413fcf850SIlpo Järvinen break;
3375a2a385d6SEric Dumazet fully_acked = false;
337613fcf850SIlpo Järvinen } else {
337772018835SIlpo Järvinen acked_pcount = tcp_skb_pcount(skb);
33781da177e4SLinus Torvalds }
33791da177e4SLinus Torvalds
3380ad971f61SEric Dumazet if (unlikely(sacked & TCPCB_RETRANS)) {
33811da177e4SLinus Torvalds if (sacked & TCPCB_SACKED_RETRANS)
338272018835SIlpo Järvinen tp->retrans_out -= acked_pcount;
33837c46a03eSIlpo Järvinen flag |= FLAG_RETRANS_DATA_ACKED;
33843d0d26c7SKenneth Klette Jonassen } else if (!(sacked & TCPCB_SACKED_ACKED)) {
33852fd66ffbSEric Dumazet last_ackt = tcp_skb_timestamp_us(skb);
33869a568de4SEric Dumazet WARN_ON_ONCE(last_ackt == 0);
33879a568de4SEric Dumazet if (!first_ackt)
3388740b0f18SEric Dumazet first_ackt = last_ackt;
3389740b0f18SEric Dumazet
3390737ff314SYuchung Cheng if (before(start_seq, reord))
3391737ff314SYuchung Cheng reord = start_seq;
3392e33099f9SYuchung Cheng if (!after(scb->end_seq, tp->high_seq))
3393e33099f9SYuchung Cheng flag |= FLAG_ORIG_SACK_ACKED;
3394c7caf8d3SIlpo Järvinen }
33957c46a03eSIlpo Järvinen
3396ddf1af6fSYuchung Cheng if (sacked & TCPCB_SACKED_ACKED) {
339772018835SIlpo Järvinen tp->sacked_out -= acked_pcount;
3398ddf1af6fSYuchung Cheng } else if (tcp_is_sack(tp)) {
3399082d4fa9SYousuk Seung tcp_count_delivered(tp, acked_pcount, ece_ack);
3400ddf1af6fSYuchung Cheng if (!tcp_skb_spurious_retrans(tp, skb))
34011d0833dfSYuchung Cheng tcp_rack_advance(tp, sacked, scb->end_seq,
34022fd66ffbSEric Dumazet tcp_skb_timestamp_us(skb));
3403ddf1af6fSYuchung Cheng }
34041da177e4SLinus Torvalds if (sacked & TCPCB_LOST)
340572018835SIlpo Järvinen tp->lost_out -= acked_pcount;
34067c46a03eSIlpo Järvinen
340772018835SIlpo Järvinen tp->packets_out -= acked_pcount;
340872018835SIlpo Järvinen pkts_acked += acked_pcount;
3409b9f64820SYuchung Cheng tcp_rate_skb_delivered(sk, skb, sack->rate);
341013fcf850SIlpo Järvinen
3411009a2e3eSIlpo Järvinen /* Initial outgoing SYN's get put onto the write_queue
3412009a2e3eSIlpo Järvinen * just like anything else we transmit. It is not
3413009a2e3eSIlpo Järvinen * true data, and if we misinform our callers that
3414009a2e3eSIlpo Järvinen * this ACK acks real data, we will erroneously exit
3415009a2e3eSIlpo Järvinen * connection startup slow start one packet too
3416009a2e3eSIlpo Järvinen * quickly. This is severely frowned upon behavior.
3417009a2e3eSIlpo Järvinen */
3418ad971f61SEric Dumazet if (likely(!(scb->tcp_flags & TCPHDR_SYN))) {
3419009a2e3eSIlpo Järvinen flag |= FLAG_DATA_ACKED;
3420009a2e3eSIlpo Järvinen } else {
3421009a2e3eSIlpo Järvinen flag |= FLAG_SYN_ACKED;
3422009a2e3eSIlpo Järvinen tp->retrans_stamp = 0;
3423009a2e3eSIlpo Järvinen }
3424009a2e3eSIlpo Järvinen
342513fcf850SIlpo Järvinen if (!fully_acked)
342613fcf850SIlpo Järvinen break;
342713fcf850SIlpo Järvinen
3428e7ed11eeSYousuk Seung tcp_ack_tstamp(sk, skb, ack_skb, prior_snd_una);
3429fdb7eb21SYousuk Seung
343075c119afSEric Dumazet next = skb_rb_next(skb);
3431ad971f61SEric Dumazet if (unlikely(skb == tp->retransmit_skb_hint))
3432ef9da47cSIlpo Järvinen tp->retransmit_skb_hint = NULL;
3433ad971f61SEric Dumazet if (unlikely(skb == tp->lost_skb_hint))
343490638a04SIlpo Järvinen tp->lost_skb_hint = NULL;
34352bec445fSEric Dumazet tcp_highest_sack_replace(sk, skb, next);
343675c119afSEric Dumazet tcp_rtx_queue_unlink_and_free(skb, sk);
34371da177e4SLinus Torvalds }
34381da177e4SLinus Torvalds
34390f87230dSFrancis Yan if (!skb)
34400f87230dSFrancis Yan tcp_chrono_stop(sk, TCP_CHRONO_BUSY);
34410f87230dSFrancis Yan
344233f5f57eSIlpo Järvinen if (likely(between(tp->snd_up, prior_snd_una, tp->snd_una)))
344333f5f57eSIlpo Järvinen tp->snd_up = tp->snd_una;
344433f5f57eSIlpo Järvinen
3445ff91e929SYousuk Seung if (skb) {
3446e7ed11eeSYousuk Seung tcp_ack_tstamp(sk, skb, ack_skb, prior_snd_una);
3447ff91e929SYousuk Seung if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
3448cadbd031SIlpo Järvinen flag |= FLAG_SACK_RENEGING;
3449ff91e929SYousuk Seung }
3450cadbd031SIlpo Järvinen
34519a568de4SEric Dumazet if (likely(first_ackt) && !(flag & FLAG_RETRANS_DATA_ACKED)) {
34529a568de4SEric Dumazet seq_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, first_ackt);
34539a568de4SEric Dumazet ca_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, last_ackt);
3454eb36be0fSYuchung Cheng
345540bc6063SYuchung Cheng if (pkts_acked == 1 && fully_acked && !prior_sacked &&
345640bc6063SYuchung Cheng (tp->snd_una - prior_snd_una) < tp->mss_cache &&
3457eb36be0fSYuchung Cheng sack->rate->prior_delivered + 1 == tp->delivered &&
3458eb36be0fSYuchung Cheng !(flag & (FLAG_CA_ALERT | FLAG_SYN_ACKED))) {
3459eb36be0fSYuchung Cheng /* Conservatively mark a delayed ACK. It's typically
3460eb36be0fSYuchung Cheng * from a lone runt packet over the round trip to
3461eb36be0fSYuchung Cheng * a receiver w/o out-of-order or CE events.
3462eb36be0fSYuchung Cheng */
3463eb36be0fSYuchung Cheng flag |= FLAG_ACK_MAYBE_DELAYED;
3464eb36be0fSYuchung Cheng }
346531231a8aSKenneth Klette Jonassen }
34669a568de4SEric Dumazet if (sack->first_sackt) {
34679a568de4SEric Dumazet sack_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, sack->first_sackt);
34689a568de4SEric Dumazet ca_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, sack->last_sackt);
3469740b0f18SEric Dumazet }
3470f6722583SYuchung Cheng rtt_update = tcp_ack_update_rtt(sk, flag, seq_rtt_us, sack_rtt_us,
3471775e68a9SYuchung Cheng ca_rtt_us, sack->rate);
3472ed08495cSYuchung Cheng
34737c46a03eSIlpo Järvinen if (flag & FLAG_ACKED) {
3474df92c839SNeal Cardwell flag |= FLAG_SET_XMIT_TIMER; /* set TLP or RTO timer */
347572211e90SIlpo Järvinen if (unlikely(icsk->icsk_mtup.probe_size &&
347672211e90SIlpo Järvinen !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {
347772211e90SIlpo Järvinen tcp_mtup_probe_success(sk);
347872211e90SIlpo Järvinen }
347972211e90SIlpo Järvinen
3480c7caf8d3SIlpo Järvinen if (tcp_is_reno(tp)) {
3481c634e34fSYousuk Seung tcp_remove_reno_sacks(sk, pkts_acked, ece_ack);
34821236f22fSIlpo Järvinen
34831236f22fSIlpo Järvinen /* If any of the cumulatively ACKed segments was
34841236f22fSIlpo Järvinen * retransmitted, non-SACK case cannot confirm that
34851236f22fSIlpo Järvinen * progress was due to original transmission due to
34861236f22fSIlpo Järvinen * lack of TCPCB_SACKED_ACKED bits even if some of
34871236f22fSIlpo Järvinen * the packets may have been never retransmitted.
34881236f22fSIlpo Järvinen */
34891236f22fSIlpo Järvinen if (flag & FLAG_RETRANS_DATA_ACKED)
34901236f22fSIlpo Järvinen flag &= ~FLAG_ORIG_SACK_ACKED;
3491c7caf8d3SIlpo Järvinen } else {
349259a08cbaSIlpo Järvinen int delta;
349359a08cbaSIlpo Järvinen
3494c7caf8d3SIlpo Järvinen /* Non-retransmitted hole got filled? That's reordering */
3495737ff314SYuchung Cheng if (before(reord, prior_fack))
3496737ff314SYuchung Cheng tcp_check_sack_reordering(sk, reord, 0);
349790638a04SIlpo Järvinen
3498713bafeaSYuchung Cheng delta = prior_sacked - tp->sacked_out;
349959a08cbaSIlpo Järvinen tp->lost_cnt_hint -= min(tp->lost_cnt_hint, delta);
3500c7caf8d3SIlpo Järvinen }
3501740b0f18SEric Dumazet } else if (skb && rtt_update && sack_rtt_us >= 0 &&
35022fd66ffbSEric Dumazet sack_rtt_us > tcp_stamp_us_delta(tp->tcp_mstamp,
35032fd66ffbSEric Dumazet tcp_skb_timestamp_us(skb))) {
35042f715c1dSYuchung Cheng /* Do not re-arm RTO if the sack RTT is measured from data sent
35052f715c1dSYuchung Cheng * after when the head was last (re)transmitted. Otherwise the
35062f715c1dSYuchung Cheng * timeout may continue to extend in loss recovery.
35072f715c1dSYuchung Cheng */
3508df92c839SNeal Cardwell flag |= FLAG_SET_XMIT_TIMER; /* set TLP or RTO timer */
35091da177e4SLinus Torvalds }
35101da177e4SLinus Torvalds
3511756ee172SLawrence Brakmo if (icsk->icsk_ca_ops->pkts_acked) {
3512756ee172SLawrence Brakmo struct ack_sample sample = { .pkts_acked = pkts_acked,
351340bc6063SYuchung Cheng .rtt_us = sack->rate->rtt_us };
3514756ee172SLawrence Brakmo
351540bc6063SYuchung Cheng sample.in_flight = tp->mss_cache *
351640bc6063SYuchung Cheng (tp->delivered - sack->rate->prior_delivered);
3517756ee172SLawrence Brakmo icsk->icsk_ca_ops->pkts_acked(sk, &sample);
3518756ee172SLawrence Brakmo }
3519138998fdSKenneth Klette Jonassen
35201da177e4SLinus Torvalds #if FASTRETRANS_DEBUG > 0
3521547b792cSIlpo Järvinen WARN_ON((int)tp->sacked_out < 0);
3522547b792cSIlpo Järvinen WARN_ON((int)tp->lost_out < 0);
3523547b792cSIlpo Järvinen WARN_ON((int)tp->retrans_out < 0);
3524e60402d0SIlpo Järvinen if (!tp->packets_out && tcp_is_sack(tp)) {
3525cfcabdccSStephen Hemminger icsk = inet_csk(sk);
35261da177e4SLinus Torvalds if (tp->lost_out) {
352791df42beSJoe Perches pr_debug("Leak l=%u %d\n",
35286687e988SArnaldo Carvalho de Melo tp->lost_out, icsk->icsk_ca_state);
35291da177e4SLinus Torvalds tp->lost_out = 0;
35301da177e4SLinus Torvalds }
35311da177e4SLinus Torvalds if (tp->sacked_out) {
353291df42beSJoe Perches pr_debug("Leak s=%u %d\n",
35336687e988SArnaldo Carvalho de Melo tp->sacked_out, icsk->icsk_ca_state);
35341da177e4SLinus Torvalds tp->sacked_out = 0;
35351da177e4SLinus Torvalds }
35361da177e4SLinus Torvalds if (tp->retrans_out) {
353791df42beSJoe Perches pr_debug("Leak r=%u %d\n",
35386687e988SArnaldo Carvalho de Melo tp->retrans_out, icsk->icsk_ca_state);
35391da177e4SLinus Torvalds tp->retrans_out = 0;
35401da177e4SLinus Torvalds }
35411da177e4SLinus Torvalds }
35421da177e4SLinus Torvalds #endif
35437c46a03eSIlpo Järvinen return flag;
35441da177e4SLinus Torvalds }
35451da177e4SLinus Torvalds
tcp_ack_probe(struct sock * sk)35461da177e4SLinus Torvalds static void tcp_ack_probe(struct sock *sk)
35471da177e4SLinus Torvalds {
3548463c84b9SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
354975c119afSEric Dumazet struct sk_buff *head = tcp_send_head(sk);
355075c119afSEric Dumazet const struct tcp_sock *tp = tcp_sk(sk);
35511da177e4SLinus Torvalds
35521da177e4SLinus Torvalds /* Was it a usable window open? */
355375c119afSEric Dumazet if (!head)
355475c119afSEric Dumazet return;
355575c119afSEric Dumazet if (!after(TCP_SKB_CB(head)->end_seq, tcp_wnd_end(tp))) {
3556463c84b9SArnaldo Carvalho de Melo icsk->icsk_backoff = 0;
35579d9b1ee0SEnke Chen icsk->icsk_probes_tstamp = 0;
3558463c84b9SArnaldo Carvalho de Melo inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0);
35591da177e4SLinus Torvalds /* Socket must be waked up by subsequent tcp_data_snd_check().
35601da177e4SLinus Torvalds * This function is not for random using!
35611da177e4SLinus Torvalds */
35621da177e4SLinus Torvalds } else {
356321c8fe99SEric Dumazet unsigned long when = tcp_probe0_when(sk, TCP_RTO_MAX);
3564fcdd1cf4SEric Dumazet
3565344db93aSEnke Chen when = tcp_clamp_probe0_to_user_timeout(sk, when);
3566344db93aSEnke Chen tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0, when, TCP_RTO_MAX);
35671da177e4SLinus Torvalds }
35681da177e4SLinus Torvalds }
35691da177e4SLinus Torvalds
tcp_ack_is_dubious(const struct sock * sk,const int flag)357067b95bd7SVijay Subramanian static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag)
35711da177e4SLinus Torvalds {
3572a02cec21SEric Dumazet return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||
3573a02cec21SEric Dumazet inet_csk(sk)->icsk_ca_state != TCP_CA_Open;
35741da177e4SLinus Torvalds }
35751da177e4SLinus Torvalds
35760f7cc9a3SYuchung Cheng /* Decide wheather to run the increase function of congestion control. */
tcp_may_raise_cwnd(const struct sock * sk,const int flag)357767b95bd7SVijay Subramanian static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
35781da177e4SLinus Torvalds {
35790f7cc9a3SYuchung Cheng /* If reordering is high then always grow cwnd whenever data is
35800f7cc9a3SYuchung Cheng * delivered regardless of its ordering. Otherwise stay conservative
358116edfe7eSYuchung Cheng * and only grow cwnd on in-order delivery (RFC5681). A stretched ACK w/
35820f7cc9a3SYuchung Cheng * new SACK or ECE mark may first advance cwnd here and later reduce
35830f7cc9a3SYuchung Cheng * cwnd in tcp_fastretrans_alert() based on more states.
35840f7cc9a3SYuchung Cheng */
358546778cd1SKuniyuki Iwashima if (tcp_sk(sk)->reordering >
358646778cd1SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reordering))
35870f7cc9a3SYuchung Cheng return flag & FLAG_FORWARD_PROGRESS;
35880f7cc9a3SYuchung Cheng
358916edfe7eSYuchung Cheng return flag & FLAG_DATA_ACKED;
35901da177e4SLinus Torvalds }
35911da177e4SLinus Torvalds
3592d452e6caSYuchung Cheng /* The "ultimate" congestion control function that aims to replace the rigid
3593d452e6caSYuchung Cheng * cwnd increase and decrease control (tcp_cong_avoid,tcp_*cwnd_reduction).
3594d452e6caSYuchung Cheng * It's called toward the end of processing an ACK with precise rate
3595d452e6caSYuchung Cheng * information. All transmission or retransmission are delayed afterwards.
3596d452e6caSYuchung Cheng */
tcp_cong_control(struct sock * sk,u32 ack,u32 acked_sacked,int flag,const struct rate_sample * rs)3597d452e6caSYuchung Cheng static void tcp_cong_control(struct sock *sk, u32 ack, u32 acked_sacked,
3598c0402760SYuchung Cheng int flag, const struct rate_sample *rs)
3599d452e6caSYuchung Cheng {
3600c0402760SYuchung Cheng const struct inet_connection_sock *icsk = inet_csk(sk);
3601c0402760SYuchung Cheng
3602c0402760SYuchung Cheng if (icsk->icsk_ca_ops->cong_control) {
360357bfc760SMiao Xu icsk->icsk_ca_ops->cong_control(sk, ack, flag, rs);
3604c0402760SYuchung Cheng return;
3605c0402760SYuchung Cheng }
3606c0402760SYuchung Cheng
3607d452e6caSYuchung Cheng if (tcp_in_cwnd_reduction(sk)) {
3608d452e6caSYuchung Cheng /* Reduce cwnd if state mandates */
36097e901ee7SYuchung Cheng tcp_cwnd_reduction(sk, acked_sacked, rs->losses, flag);
3610d452e6caSYuchung Cheng } else if (tcp_may_raise_cwnd(sk, flag)) {
3611d452e6caSYuchung Cheng /* Advance cwnd if state allows */
3612d452e6caSYuchung Cheng tcp_cong_avoid(sk, ack, acked_sacked);
3613d452e6caSYuchung Cheng }
3614d452e6caSYuchung Cheng tcp_update_pacing_rate(sk);
3615d452e6caSYuchung Cheng }
3616d452e6caSYuchung Cheng
36171da177e4SLinus Torvalds /* Check that window update is acceptable.
36181da177e4SLinus Torvalds * The function assumes that snd_una<=ack<=snd_next.
36191da177e4SLinus Torvalds */
tcp_may_update_window(const struct tcp_sock * tp,const u32 ack,const u32 ack_seq,const u32 nwin)362067b95bd7SVijay Subramanian static inline bool tcp_may_update_window(const struct tcp_sock *tp,
3621056834d9SIlpo Järvinen const u32 ack, const u32 ack_seq,
3622056834d9SIlpo Järvinen const u32 nwin)
36231da177e4SLinus Torvalds {
3624a02cec21SEric Dumazet return after(ack, tp->snd_una) ||
36251da177e4SLinus Torvalds after(ack_seq, tp->snd_wl1) ||
3626800a6661SMenglong Dong (ack_seq == tp->snd_wl1 && (nwin > tp->snd_wnd || !nwin));
36271da177e4SLinus Torvalds }
36281da177e4SLinus Torvalds
tcp_snd_sne_update(struct tcp_sock * tp,u32 ack)362967fa83f7SDmitry Safonov static void tcp_snd_sne_update(struct tcp_sock *tp, u32 ack)
36300df48c26SEric Dumazet {
363164382c71SDmitry Safonov #ifdef CONFIG_TCP_AO
363264382c71SDmitry Safonov struct tcp_ao_info *ao;
36330df48c26SEric Dumazet
363467fa83f7SDmitry Safonov if (!static_branch_unlikely(&tcp_ao_needed.key))
363567fa83f7SDmitry Safonov return;
363667fa83f7SDmitry Safonov
363764382c71SDmitry Safonov ao = rcu_dereference_protected(tp->ao_info,
363864382c71SDmitry Safonov lockdep_sock_is_held((struct sock *)tp));
363996be3dcdSDmitry Safonov if (ao && ack < tp->snd_una) {
364064382c71SDmitry Safonov ao->snd_sne++;
364196be3dcdSDmitry Safonov trace_tcp_ao_snd_sne_update((struct sock *)tp, ao->snd_sne);
364296be3dcdSDmitry Safonov }
364364382c71SDmitry Safonov #endif
364467fa83f7SDmitry Safonov }
364567fa83f7SDmitry Safonov
364667fa83f7SDmitry Safonov /* If we update tp->snd_una, also update tp->bytes_acked */
tcp_snd_una_update(struct tcp_sock * tp,u32 ack)364767fa83f7SDmitry Safonov static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
364867fa83f7SDmitry Safonov {
364967fa83f7SDmitry Safonov u32 delta = ack - tp->snd_una;
365067fa83f7SDmitry Safonov
365167fa83f7SDmitry Safonov sock_owned_by_me((struct sock *)tp);
365267fa83f7SDmitry Safonov tp->bytes_acked += delta;
365367fa83f7SDmitry Safonov tcp_snd_sne_update(tp, ack);
36540df48c26SEric Dumazet tp->snd_una = ack;
36550df48c26SEric Dumazet }
36560df48c26SEric Dumazet
tcp_rcv_sne_update(struct tcp_sock * tp,u32 seq)365767fa83f7SDmitry Safonov static void tcp_rcv_sne_update(struct tcp_sock *tp, u32 seq)
365867fa83f7SDmitry Safonov {
365967fa83f7SDmitry Safonov #ifdef CONFIG_TCP_AO
366067fa83f7SDmitry Safonov struct tcp_ao_info *ao;
366167fa83f7SDmitry Safonov
366267fa83f7SDmitry Safonov if (!static_branch_unlikely(&tcp_ao_needed.key))
366367fa83f7SDmitry Safonov return;
366467fa83f7SDmitry Safonov
366567fa83f7SDmitry Safonov ao = rcu_dereference_protected(tp->ao_info,
366667fa83f7SDmitry Safonov lockdep_sock_is_held((struct sock *)tp));
366796be3dcdSDmitry Safonov if (ao && seq < tp->rcv_nxt) {
366867fa83f7SDmitry Safonov ao->rcv_sne++;
366996be3dcdSDmitry Safonov trace_tcp_ao_rcv_sne_update((struct sock *)tp, ao->rcv_sne);
367096be3dcdSDmitry Safonov }
367167fa83f7SDmitry Safonov #endif
367267fa83f7SDmitry Safonov }
367367fa83f7SDmitry Safonov
3674bdd1f9edSEric Dumazet /* If we update tp->rcv_nxt, also update tp->bytes_received */
tcp_rcv_nxt_update(struct tcp_sock * tp,u32 seq)3675bdd1f9edSEric Dumazet static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
3676bdd1f9edSEric Dumazet {
3677bdd1f9edSEric Dumazet u32 delta = seq - tp->rcv_nxt;
3678bdd1f9edSEric Dumazet
367946cc6e49SEric Dumazet sock_owned_by_me((struct sock *)tp);
3680bdd1f9edSEric Dumazet tp->bytes_received += delta;
368167fa83f7SDmitry Safonov tcp_rcv_sne_update(tp, seq);
3682dba7d9b8SEric Dumazet WRITE_ONCE(tp->rcv_nxt, seq);
3683bdd1f9edSEric Dumazet }
3684bdd1f9edSEric Dumazet
36851da177e4SLinus Torvalds /* Update our send window.
36861da177e4SLinus Torvalds *
36871da177e4SLinus Torvalds * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
36881da177e4SLinus Torvalds * and in FreeBSD. NetBSD's one is even worse.) is wrong.
36891da177e4SLinus Torvalds */
tcp_ack_update_window(struct sock * sk,const struct sk_buff * skb,u32 ack,u32 ack_seq)3690cf533ea5SEric Dumazet static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,
36919e412ba7SIlpo Järvinen u32 ack_seq)
36921da177e4SLinus Torvalds {
36939e412ba7SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
36941da177e4SLinus Torvalds int flag = 0;
3695aa8223c7SArnaldo Carvalho de Melo u32 nwin = ntohs(tcp_hdr(skb)->window);
36961da177e4SLinus Torvalds
3697aa8223c7SArnaldo Carvalho de Melo if (likely(!tcp_hdr(skb)->syn))
36981da177e4SLinus Torvalds nwin <<= tp->rx_opt.snd_wscale;
36991da177e4SLinus Torvalds
37001da177e4SLinus Torvalds if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {
37011da177e4SLinus Torvalds flag |= FLAG_WIN_UPDATE;
3702ee7537b6SHantzis Fotis tcp_update_wl(tp, ack_seq);
37031da177e4SLinus Torvalds
37041da177e4SLinus Torvalds if (tp->snd_wnd != nwin) {
37051da177e4SLinus Torvalds tp->snd_wnd = nwin;
37061da177e4SLinus Torvalds
370731770e34SFlorian Westphal /* Note, it is the only place, where
370831770e34SFlorian Westphal * fast path is recovered for sending TCP.
370931770e34SFlorian Westphal */
371031770e34SFlorian Westphal tp->pred_flags = 0;
371131770e34SFlorian Westphal tcp_fast_path_check(sk);
371231770e34SFlorian Westphal
371375c119afSEric Dumazet if (!tcp_write_queue_empty(sk))
37146f021c62SEric Dumazet tcp_slow_start_after_idle_check(sk);
37156f021c62SEric Dumazet
37161da177e4SLinus Torvalds if (nwin > tp->max_window) {
37171da177e4SLinus Torvalds tp->max_window = nwin;
3718d83d8461SArnaldo Carvalho de Melo tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie);
37191da177e4SLinus Torvalds }
37201da177e4SLinus Torvalds }
37211da177e4SLinus Torvalds }
37221da177e4SLinus Torvalds
37230df48c26SEric Dumazet tcp_snd_una_update(tp, ack);
37241da177e4SLinus Torvalds
37251da177e4SLinus Torvalds return flag;
37261da177e4SLinus Torvalds }
37271da177e4SLinus Torvalds
__tcp_oow_rate_limited(struct net * net,int mib_idx,u32 * last_oow_ack_time)3728083ae308SJason Baron static bool __tcp_oow_rate_limited(struct net *net, int mib_idx,
3729083ae308SJason Baron u32 *last_oow_ack_time)
3730083ae308SJason Baron {
3731998127cdSEric Dumazet /* Paired with the WRITE_ONCE() in this function. */
3732998127cdSEric Dumazet u32 val = READ_ONCE(*last_oow_ack_time);
3733998127cdSEric Dumazet
3734998127cdSEric Dumazet if (val) {
3735998127cdSEric Dumazet s32 elapsed = (s32)(tcp_jiffies32 - val);
3736083ae308SJason Baron
37372afdbe7bSKuniyuki Iwashima if (0 <= elapsed &&
37382afdbe7bSKuniyuki Iwashima elapsed < READ_ONCE(net->ipv4.sysctl_tcp_invalid_ratelimit)) {
3739083ae308SJason Baron NET_INC_STATS(net, mib_idx);
3740083ae308SJason Baron return true; /* rate-limited: don't send yet! */
3741083ae308SJason Baron }
3742083ae308SJason Baron }
3743083ae308SJason Baron
3744998127cdSEric Dumazet /* Paired with the prior READ_ONCE() and with itself,
3745998127cdSEric Dumazet * as we might be lockless.
3746998127cdSEric Dumazet */
3747998127cdSEric Dumazet WRITE_ONCE(*last_oow_ack_time, tcp_jiffies32);
3748083ae308SJason Baron
3749083ae308SJason Baron return false; /* not rate-limited: go ahead, send dupack now! */
3750083ae308SJason Baron }
3751083ae308SJason Baron
37527970ddc8SEric Dumazet /* Return true if we're currently rate-limiting out-of-window ACKs and
37537970ddc8SEric Dumazet * thus shouldn't send a dupack right now. We rate-limit dupacks in
37547970ddc8SEric Dumazet * response to out-of-window SYNs or ACKs to mitigate ACK loops or DoS
37557970ddc8SEric Dumazet * attacks that send repeated SYNs or ACKs for the same connection. To
37567970ddc8SEric Dumazet * do this, we do not send a duplicate SYNACK or ACK if the remote
37577970ddc8SEric Dumazet * endpoint is sending out-of-window SYNs or pure ACKs at a high rate.
37587970ddc8SEric Dumazet */
tcp_oow_rate_limited(struct net * net,const struct sk_buff * skb,int mib_idx,u32 * last_oow_ack_time)37597970ddc8SEric Dumazet bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
37607970ddc8SEric Dumazet int mib_idx, u32 *last_oow_ack_time)
37617970ddc8SEric Dumazet {
37627970ddc8SEric Dumazet /* Data packets without SYNs are not likely part of an ACK loop. */
37637970ddc8SEric Dumazet if ((TCP_SKB_CB(skb)->seq != TCP_SKB_CB(skb)->end_seq) &&
37647970ddc8SEric Dumazet !tcp_hdr(skb)->syn)
3765083ae308SJason Baron return false;
37667970ddc8SEric Dumazet
3767083ae308SJason Baron return __tcp_oow_rate_limited(net, mib_idx, last_oow_ack_time);
37687970ddc8SEric Dumazet }
37697970ddc8SEric Dumazet
3770354e4aa3SEric Dumazet /* RFC 5961 7 [ACK Throttling] */
tcp_send_challenge_ack(struct sock * sk)3771208dd45dSBenjamin Yim static void tcp_send_challenge_ack(struct sock *sk)
3772354e4aa3SEric Dumazet {
3773f2b2c582SNeal Cardwell struct tcp_sock *tp = tcp_sk(sk);
3774b530b681SEric Dumazet struct net *net = sock_net(sk);
377579e3602cSEric Dumazet u32 count, now, ack_limit;
3776354e4aa3SEric Dumazet
3777f2b2c582SNeal Cardwell /* First check our per-socket dupack rate limit. */
3778b530b681SEric Dumazet if (__tcp_oow_rate_limited(net,
3779f2b2c582SNeal Cardwell LINUX_MIB_TCPACKSKIPPEDCHALLENGE,
3780f2b2c582SNeal Cardwell &tp->last_oow_ack_time))
3781f2b2c582SNeal Cardwell return;
3782f2b2c582SNeal Cardwell
378379e3602cSEric Dumazet ack_limit = READ_ONCE(net->ipv4.sysctl_tcp_challenge_ack_limit);
378479e3602cSEric Dumazet if (ack_limit == INT_MAX)
378579e3602cSEric Dumazet goto send_ack;
378679e3602cSEric Dumazet
378775ff39ccSEric Dumazet /* Then check host-wide RFC 5961 rate limit. */
3788f2b2c582SNeal Cardwell now = jiffies / HZ;
378979e3602cSEric Dumazet if (now != READ_ONCE(net->ipv4.tcp_challenge_timestamp)) {
3790b530b681SEric Dumazet u32 half = (ack_limit + 1) >> 1;
379175ff39ccSEric Dumazet
379279e3602cSEric Dumazet WRITE_ONCE(net->ipv4.tcp_challenge_timestamp, now);
37938032bf12SJason A. Donenfeld WRITE_ONCE(net->ipv4.tcp_challenge_count,
3794e8a533cbSJason A. Donenfeld get_random_u32_inclusive(half, ack_limit + half - 1));
3795354e4aa3SEric Dumazet }
379679e3602cSEric Dumazet count = READ_ONCE(net->ipv4.tcp_challenge_count);
379775ff39ccSEric Dumazet if (count > 0) {
379879e3602cSEric Dumazet WRITE_ONCE(net->ipv4.tcp_challenge_count, count - 1);
379979e3602cSEric Dumazet send_ack:
3800b530b681SEric Dumazet NET_INC_STATS(net, LINUX_MIB_TCPCHALLENGEACK);
3801354e4aa3SEric Dumazet tcp_send_ack(sk);
3802354e4aa3SEric Dumazet }
3803354e4aa3SEric Dumazet }
3804354e4aa3SEric Dumazet
tcp_store_ts_recent(struct tcp_sock * tp)380512fb3dd9SEric Dumazet static void tcp_store_ts_recent(struct tcp_sock *tp)
380612fb3dd9SEric Dumazet {
380712fb3dd9SEric Dumazet tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
3808cca9bab1SArnd Bergmann tp->rx_opt.ts_recent_stamp = ktime_get_seconds();
380912fb3dd9SEric Dumazet }
381012fb3dd9SEric Dumazet
tcp_replace_ts_recent(struct tcp_sock * tp,u32 seq)381112fb3dd9SEric Dumazet static void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
381212fb3dd9SEric Dumazet {
381312fb3dd9SEric Dumazet if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) {
381412fb3dd9SEric Dumazet /* PAWS bug workaround wrt. ACK frames, the PAWS discard
381512fb3dd9SEric Dumazet * extra check below makes sure this can only happen
381612fb3dd9SEric Dumazet * for pure ACK frames. -DaveM
381712fb3dd9SEric Dumazet *
381812fb3dd9SEric Dumazet * Not only, also it occurs for expired timestamps.
381912fb3dd9SEric Dumazet */
382012fb3dd9SEric Dumazet
382112fb3dd9SEric Dumazet if (tcp_paws_check(&tp->rx_opt, 0))
382212fb3dd9SEric Dumazet tcp_store_ts_recent(tp);
382312fb3dd9SEric Dumazet }
382412fb3dd9SEric Dumazet }
382512fb3dd9SEric Dumazet
382676be93fcSYuchung Cheng /* This routine deals with acks during a TLP episode and ends an episode by
382776be93fcSYuchung Cheng * resetting tlp_high_seq. Ref: TLP algorithm in draft-ietf-tcpm-rack
38289b717a8dSNandita Dukkipati */
tcp_process_tlp_ack(struct sock * sk,u32 ack,int flag)38299b717a8dSNandita Dukkipati static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
38309b717a8dSNandita Dukkipati {
38319b717a8dSNandita Dukkipati struct tcp_sock *tp = tcp_sk(sk);
38329b717a8dSNandita Dukkipati
383308abdffaSSébastien Barré if (before(ack, tp->tlp_high_seq))
38349b717a8dSNandita Dukkipati return;
38359b717a8dSNandita Dukkipati
383676be93fcSYuchung Cheng if (!tp->tlp_retrans) {
383776be93fcSYuchung Cheng /* TLP of new data has been acknowledged */
383876be93fcSYuchung Cheng tp->tlp_high_seq = 0;
383963f367d9SYuchung Cheng } else if (flag & FLAG_DSACK_TLP) {
384008abdffaSSébastien Barré /* This DSACK means original and TLP probe arrived; no loss */
38419b717a8dSNandita Dukkipati tp->tlp_high_seq = 0;
384208abdffaSSébastien Barré } else if (after(ack, tp->tlp_high_seq)) {
384308abdffaSSébastien Barré /* ACK advances: there was a loss, so reduce cwnd. Reset
384408abdffaSSébastien Barré * tlp_high_seq in tcp_init_cwnd_reduction()
384508abdffaSSébastien Barré */
38465ee2c941SChristoph Paasch tcp_init_cwnd_reduction(sk);
38479b717a8dSNandita Dukkipati tcp_set_ca_state(sk, TCP_CA_CWR);
38489b717a8dSNandita Dukkipati tcp_end_cwnd_reduction(sk);
3849031afe49SYuchung Cheng tcp_try_keep_open(sk);
3850c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk),
38519b717a8dSNandita Dukkipati LINUX_MIB_TCPLOSSPROBERECOVERY);
385208abdffaSSébastien Barré } else if (!(flag & (FLAG_SND_UNA_ADVANCED |
385308abdffaSSébastien Barré FLAG_NOT_DUP | FLAG_DATA_SACKED))) {
385408abdffaSSébastien Barré /* Pure dupack: original and TLP probe arrived; no loss */
385508abdffaSSébastien Barré tp->tlp_high_seq = 0;
38569b717a8dSNandita Dukkipati }
38579b717a8dSNandita Dukkipati }
38589b717a8dSNandita Dukkipati
tcp_in_ack_event(struct sock * sk,u32 flags)38597354c8c3SFlorian Westphal static inline void tcp_in_ack_event(struct sock *sk, u32 flags)
38607354c8c3SFlorian Westphal {
38617354c8c3SFlorian Westphal const struct inet_connection_sock *icsk = inet_csk(sk);
38627354c8c3SFlorian Westphal
38637354c8c3SFlorian Westphal if (icsk->icsk_ca_ops->in_ack_event)
38647354c8c3SFlorian Westphal icsk->icsk_ca_ops->in_ack_event(sk, flags);
38657354c8c3SFlorian Westphal }
38667354c8c3SFlorian Westphal
3867e662ca40SYuchung Cheng /* Congestion control has updated the cwnd already. So if we're in
3868e662ca40SYuchung Cheng * loss recovery then now we do any new sends (for FRTO) or
3869e662ca40SYuchung Cheng * retransmits (for CA_Loss or CA_recovery) that make sense.
3870e662ca40SYuchung Cheng */
tcp_xmit_recovery(struct sock * sk,int rexmit)3871e662ca40SYuchung Cheng static void tcp_xmit_recovery(struct sock *sk, int rexmit)
3872e662ca40SYuchung Cheng {
3873e662ca40SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
3874e662ca40SYuchung Cheng
3875bc9f38c8SYuchung Cheng if (rexmit == REXMIT_NONE || sk->sk_state == TCP_SYN_SENT)
3876e662ca40SYuchung Cheng return;
3877e662ca40SYuchung Cheng
3878d0e8bcafSMao Wenan if (unlikely(rexmit == REXMIT_NEW)) {
3879e662ca40SYuchung Cheng __tcp_push_pending_frames(sk, tcp_current_mss(sk),
3880e662ca40SYuchung Cheng TCP_NAGLE_OFF);
3881e662ca40SYuchung Cheng if (after(tp->snd_nxt, tp->high_seq))
3882e662ca40SYuchung Cheng return;
3883e662ca40SYuchung Cheng tp->frto = 0;
3884e662ca40SYuchung Cheng }
3885e662ca40SYuchung Cheng tcp_xmit_retransmit_queue(sk);
3886e662ca40SYuchung Cheng }
3887e662ca40SYuchung Cheng
3888a77fa010SYuchung Cheng /* Returns the number of packets newly acked or sacked by the current ACK */
tcp_newly_delivered(struct sock * sk,u32 prior_delivered,int flag)3889a77fa010SYuchung Cheng static u32 tcp_newly_delivered(struct sock *sk, u32 prior_delivered, int flag)
3890a77fa010SYuchung Cheng {
3891feb5f2ecSYuchung Cheng const struct net *net = sock_net(sk);
3892a77fa010SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
3893a77fa010SYuchung Cheng u32 delivered;
3894a77fa010SYuchung Cheng
3895a77fa010SYuchung Cheng delivered = tp->delivered - prior_delivered;
3896feb5f2ecSYuchung Cheng NET_ADD_STATS(net, LINUX_MIB_TCPDELIVERED, delivered);
3897082d4fa9SYousuk Seung if (flag & FLAG_ECE)
3898feb5f2ecSYuchung Cheng NET_ADD_STATS(net, LINUX_MIB_TCPDELIVEREDCE, delivered);
3899082d4fa9SYousuk Seung
3900a77fa010SYuchung Cheng return delivered;
3901a77fa010SYuchung Cheng }
3902a77fa010SYuchung Cheng
39031da177e4SLinus Torvalds /* This routine deals with incoming acks, but not outgoing ones. */
tcp_ack(struct sock * sk,const struct sk_buff * skb,int flag)3904cf533ea5SEric Dumazet static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
39051da177e4SLinus Torvalds {
39066687e988SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
39071da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
3908196da974SKenneth Klette Jonassen struct tcp_sacktag_state sack_state;
3909b9f64820SYuchung Cheng struct rate_sample rs = { .prior_delivered = 0 };
39101da177e4SLinus Torvalds u32 prior_snd_una = tp->snd_una;
3911d4761754SYousuk Seung bool is_sack_reneg = tp->is_sack_reneg;
39121da177e4SLinus Torvalds u32 ack_seq = TCP_SKB_CB(skb)->seq;
39131da177e4SLinus Torvalds u32 ack = TCP_SKB_CB(skb)->ack_seq;
391419119f29SEric Dumazet int num_dupack = 0;
391535f079ebSNandita Dukkipati int prior_packets = tp->packets_out;
3916b9f64820SYuchung Cheng u32 delivered = tp->delivered;
3917b9f64820SYuchung Cheng u32 lost = tp->lost;
3918e662ca40SYuchung Cheng int rexmit = REXMIT_NONE; /* Flag to (re)transmit to recover losses */
3919737ff314SYuchung Cheng u32 prior_fack;
3920196da974SKenneth Klette Jonassen
39219a568de4SEric Dumazet sack_state.first_sackt = 0;
3922b9f64820SYuchung Cheng sack_state.rate = &rs;
3923f00394ceSYousuk Seung sack_state.sack_delivered = 0;
39241da177e4SLinus Torvalds
392575c119afSEric Dumazet /* We very likely will need to access rtx queue. */
392675c119afSEric Dumazet prefetch(sk->tcp_rtx_queue.rb_node);
3927ad971f61SEric Dumazet
392896e0bf4bSJohn Dykstra /* If the ack is older than previous acks
39291da177e4SLinus Torvalds * then we can probably ignore it.
39301da177e4SLinus Torvalds */
3931354e4aa3SEric Dumazet if (before(ack, prior_snd_una)) {
39323d501dd3SEric Dumazet u32 max_window;
39333d501dd3SEric Dumazet
39343d501dd3SEric Dumazet /* do not accept ACK for bytes we never sent. */
39353d501dd3SEric Dumazet max_window = min_t(u64, tp->max_window, tp->bytes_acked);
3936354e4aa3SEric Dumazet /* RFC 5961 5.2 [Blind Data Injection Attack].[Mitigation] */
39373d501dd3SEric Dumazet if (before(ack, prior_snd_una - max_window)) {
3938d0e1a1b5SEric Dumazet if (!(flag & FLAG_NO_CHALLENGE_ACK))
3939208dd45dSBenjamin Yim tcp_send_challenge_ack(sk);
39404b506af9SEric Dumazet return -SKB_DROP_REASON_TCP_TOO_OLD_ACK;
3941354e4aa3SEric Dumazet }
39421da177e4SLinus Torvalds goto old_ack;
3943354e4aa3SEric Dumazet }
39441da177e4SLinus Torvalds
394596e0bf4bSJohn Dykstra /* If the ack includes data we haven't sent yet, discard
394696e0bf4bSJohn Dykstra * this segment (RFC793 Section 3.9).
394796e0bf4bSJohn Dykstra */
394896e0bf4bSJohn Dykstra if (after(ack, tp->snd_nxt))
39494b506af9SEric Dumazet return -SKB_DROP_REASON_TCP_ACK_UNSENT_DATA;
395096e0bf4bSJohn Dykstra
39510c9ab092SNeal Cardwell if (after(ack, prior_snd_una)) {
39522e605294SIlpo Järvinen flag |= FLAG_SND_UNA_ADVANCED;
39530c9ab092SNeal Cardwell icsk->icsk_retransmits = 0;
39546dac1523SIlya Lesokhin
39556dac1523SIlya Lesokhin #if IS_ENABLED(CONFIG_TLS_DEVICE)
3956494bc1d2SJakub Kicinski if (static_branch_unlikely(&clean_acked_data_enabled.key))
39576dac1523SIlya Lesokhin if (icsk->icsk_clean_acked)
39586dac1523SIlya Lesokhin icsk->icsk_clean_acked(sk, ack);
39596dac1523SIlya Lesokhin #endif
39600c9ab092SNeal Cardwell }
39612e605294SIlpo Järvinen
396250895b9dSEric Dumazet prior_fack = tcp_is_sack(tp) ? tcp_highest_sack_seq(tp) : tp->snd_una;
3963b9f64820SYuchung Cheng rs.prior_in_flight = tcp_packets_in_flight(tp);
3964c7caf8d3SIlpo Järvinen
396512fb3dd9SEric Dumazet /* ts_recent update must be made after we are sure that the packet
396612fb3dd9SEric Dumazet * is in window.
396712fb3dd9SEric Dumazet */
396812fb3dd9SEric Dumazet if (flag & FLAG_UPDATE_TS_RECENT)
396912fb3dd9SEric Dumazet tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
397012fb3dd9SEric Dumazet
39715e13a0d3SYafang Shao if ((flag & (FLAG_SLOWPATH | FLAG_SND_UNA_ADVANCED)) ==
39725e13a0d3SYafang Shao FLAG_SND_UNA_ADVANCED) {
397331770e34SFlorian Westphal /* Window is constant, pure forward advance.
397431770e34SFlorian Westphal * No more checks are required.
397531770e34SFlorian Westphal * Note, we use the fact that SND.UNA>=SND.WL2.
397631770e34SFlorian Westphal */
397731770e34SFlorian Westphal tcp_update_wl(tp, ack_seq);
397831770e34SFlorian Westphal tcp_snd_una_update(tp, ack);
397931770e34SFlorian Westphal flag |= FLAG_WIN_UPDATE;
398031770e34SFlorian Westphal
398131770e34SFlorian Westphal tcp_in_ack_event(sk, CA_ACK_WIN_UPDATE);
398231770e34SFlorian Westphal
398331770e34SFlorian Westphal NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPACKS);
398431770e34SFlorian Westphal } else {
3985c1d2b4c3SFlorian Westphal u32 ack_ev_flags = CA_ACK_SLOWPATH;
3986c1d2b4c3SFlorian Westphal
39871da177e4SLinus Torvalds if (ack_seq != TCP_SKB_CB(skb)->end_seq)
39881da177e4SLinus Torvalds flag |= FLAG_DATA;
39891da177e4SLinus Torvalds else
3990c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPUREACKS);
39911da177e4SLinus Torvalds
39929e412ba7SIlpo Järvinen flag |= tcp_ack_update_window(sk, skb, ack, ack_seq);
39931da177e4SLinus Torvalds
39941da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->sacked)
399559c9af42SYuchung Cheng flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
3996196da974SKenneth Klette Jonassen &sack_state);
39971da177e4SLinus Torvalds
3998735d3831SFlorian Westphal if (tcp_ecn_rcv_ecn_echo(tp, tcp_hdr(skb))) {
39991da177e4SLinus Torvalds flag |= FLAG_ECE;
4000c1d2b4c3SFlorian Westphal ack_ev_flags |= CA_ACK_ECE;
40019890092eSFlorian Westphal }
40021da177e4SLinus Torvalds
4003082d4fa9SYousuk Seung if (sack_state.sack_delivered)
4004082d4fa9SYousuk Seung tcp_count_delivered(tp, sack_state.sack_delivered,
4005082d4fa9SYousuk Seung flag & FLAG_ECE);
4006082d4fa9SYousuk Seung
40079890092eSFlorian Westphal if (flag & FLAG_WIN_UPDATE)
40089890092eSFlorian Westphal ack_ev_flags |= CA_ACK_WIN_UPDATE;
40099890092eSFlorian Westphal
40109890092eSFlorian Westphal tcp_in_ack_event(sk, ack_ev_flags);
4011c1d2b4c3SFlorian Westphal }
40121da177e4SLinus Torvalds
401325702840SDenis Kirjanov /* This is a deviation from RFC3168 since it states that:
401425702840SDenis Kirjanov * "When the TCP data sender is ready to set the CWR bit after reducing
401525702840SDenis Kirjanov * the congestion window, it SHOULD set the CWR bit only on the first
401625702840SDenis Kirjanov * new data packet that it transmits."
401725702840SDenis Kirjanov * We accept CWR on pure ACKs to be more robust
401825702840SDenis Kirjanov * with widely-deployed TCP implementations that do this.
401925702840SDenis Kirjanov */
402025702840SDenis Kirjanov tcp_ecn_accept_cwr(sk, skb);
402125702840SDenis Kirjanov
40221da177e4SLinus Torvalds /* We passed data and got it acked, remove any soft error
40231da177e4SLinus Torvalds * log. Something worked...
40241da177e4SLinus Torvalds */
4025cee1af82SEric Dumazet WRITE_ONCE(sk->sk_err_soft, 0);
40264b53fb67SDavid S. Miller icsk->icsk_probes_out = 0;
402770eabf0eSEric Dumazet tp->rcv_tstamp = tcp_jiffies32;
40281da177e4SLinus Torvalds if (!prior_packets)
40291da177e4SLinus Torvalds goto no_queue;
40301da177e4SLinus Torvalds
40311da177e4SLinus Torvalds /* See if we can take anything off of the retransmit queue. */
4032e7ed11eeSYousuk Seung flag |= tcp_clean_rtx_queue(sk, skb, prior_fack, prior_snd_una,
4033e7ed11eeSYousuk Seung &sack_state, flag & FLAG_ECE);
4034a262f0cdSNandita Dukkipati
40351f255691SPriyaranjan Jha tcp_rack_update_reo_wnd(sk, &rs);
40361da177e4SLinus Torvalds
4037df92c839SNeal Cardwell if (tp->tlp_high_seq)
4038df92c839SNeal Cardwell tcp_process_tlp_ack(sk, ack, flag);
4039df92c839SNeal Cardwell
40400f7cc9a3SYuchung Cheng if (tcp_ack_is_dubious(sk, flag)) {
4041d9157f68SPengcheng Yang if (!(flag & (FLAG_SND_UNA_ADVANCED |
4042d9157f68SPengcheng Yang FLAG_NOT_DUP | FLAG_DSACKING_ACK))) {
404319119f29SEric Dumazet num_dupack = 1;
404419119f29SEric Dumazet /* Consider if pure acks were aggregated in tcp_add_backlog() */
404519119f29SEric Dumazet if (!(flag & FLAG_DATA))
404619119f29SEric Dumazet num_dupack = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
404719119f29SEric Dumazet }
404819119f29SEric Dumazet tcp_fastretrans_alert(sk, prior_snd_una, num_dupack, &flag,
4049737ff314SYuchung Cheng &rexmit);
40501da177e4SLinus Torvalds }
40519b717a8dSNandita Dukkipati
405262d9f1a6SPengcheng Yang /* If needed, reset TLP/RTO timer when RACK doesn't set. */
405362d9f1a6SPengcheng Yang if (flag & FLAG_SET_XMIT_TIMER)
405462d9f1a6SPengcheng Yang tcp_set_xmit_timer(sk);
405562d9f1a6SPengcheng Yang
4056c3a2e837SJulian Anastasov if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP))
4057c3a2e837SJulian Anastasov sk_dst_confirm(sk);
40586ba8a3b1SNandita Dukkipati
4059a77fa010SYuchung Cheng delivered = tcp_newly_delivered(sk, delivered, flag);
4060b9f64820SYuchung Cheng lost = tp->lost - lost; /* freshly marked lost */
4061e4286603SYuchung Cheng rs.is_ack_delayed = !!(flag & FLAG_ACK_MAYBE_DELAYED);
4062d4761754SYousuk Seung tcp_rate_gen(sk, delivered, lost, is_sack_reneg, sack_state.rate);
4063deed7be7SYuchung Cheng tcp_cong_control(sk, ack, delivered, flag, sack_state.rate);
4064e662ca40SYuchung Cheng tcp_xmit_recovery(sk, rexmit);
40651da177e4SLinus Torvalds return 1;
40661da177e4SLinus Torvalds
40671da177e4SLinus Torvalds no_queue:
40685628adf1SNeal Cardwell /* If data was DSACKed, see if we can undo a cwnd reduction. */
4069a77fa010SYuchung Cheng if (flag & FLAG_DSACKING_ACK) {
407019119f29SEric Dumazet tcp_fastretrans_alert(sk, prior_snd_una, num_dupack, &flag,
4071737ff314SYuchung Cheng &rexmit);
4072a77fa010SYuchung Cheng tcp_newly_delivered(sk, delivered, flag);
4073a77fa010SYuchung Cheng }
40741da177e4SLinus Torvalds /* If this ack opens up a zero window, clear backoff. It was
40751da177e4SLinus Torvalds * being used to time the probes, and is probably far higher than
40761da177e4SLinus Torvalds * it needs to be for normal retransmission.
40771da177e4SLinus Torvalds */
40781da177e4SLinus Torvalds tcp_ack_probe(sk);
40799b717a8dSNandita Dukkipati
40809b717a8dSNandita Dukkipati if (tp->tlp_high_seq)
40819b717a8dSNandita Dukkipati tcp_process_tlp_ack(sk, ack, flag);
40821da177e4SLinus Torvalds return 1;
40831da177e4SLinus Torvalds
40841da177e4SLinus Torvalds old_ack:
4085e95ae2f2SNeal Cardwell /* If data was SACKed, tag it and see if we should send more data.
4086e95ae2f2SNeal Cardwell * If data was DSACKed, see if we can undo a cwnd reduction.
4087e95ae2f2SNeal Cardwell */
40888aca6cb1SIlpo Järvinen if (TCP_SKB_CB(skb)->sacked) {
408959c9af42SYuchung Cheng flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
4090196da974SKenneth Klette Jonassen &sack_state);
409119119f29SEric Dumazet tcp_fastretrans_alert(sk, prior_snd_una, num_dupack, &flag,
4092737ff314SYuchung Cheng &rexmit);
4093a77fa010SYuchung Cheng tcp_newly_delivered(sk, delivered, flag);
4094e662ca40SYuchung Cheng tcp_xmit_recovery(sk, rexmit);
40958aca6cb1SIlpo Järvinen }
40961da177e4SLinus Torvalds
40971da177e4SLinus Torvalds return 0;
40981da177e4SLinus Torvalds }
40991da177e4SLinus Torvalds
tcp_parse_fastopen_option(int len,const unsigned char * cookie,bool syn,struct tcp_fastopen_cookie * foc,bool exp_opt)41007f9b838bSDaniel Lee static void tcp_parse_fastopen_option(int len, const unsigned char *cookie,
41017f9b838bSDaniel Lee bool syn, struct tcp_fastopen_cookie *foc,
41027f9b838bSDaniel Lee bool exp_opt)
41037f9b838bSDaniel Lee {
41047f9b838bSDaniel Lee /* Valid only in SYN or SYN-ACK with an even length. */
41057f9b838bSDaniel Lee if (!foc || !syn || len < 0 || (len & 1))
41067f9b838bSDaniel Lee return;
41077f9b838bSDaniel Lee
41087f9b838bSDaniel Lee if (len >= TCP_FASTOPEN_COOKIE_MIN &&
41097f9b838bSDaniel Lee len <= TCP_FASTOPEN_COOKIE_MAX)
41107f9b838bSDaniel Lee memcpy(foc->val, cookie, len);
41117f9b838bSDaniel Lee else if (len != 0)
41127f9b838bSDaniel Lee len = -1;
41137f9b838bSDaniel Lee foc->len = len;
41147f9b838bSDaniel Lee foc->exp = exp_opt;
41157f9b838bSDaniel Lee }
41167f9b838bSDaniel Lee
smc_parse_options(const struct tcphdr * th,struct tcp_options_received * opt_rx,const unsigned char * ptr,int opsize)41177656d684SMartin KaFai Lau static bool smc_parse_options(const struct tcphdr *th,
411860e2a778SUrsula Braun struct tcp_options_received *opt_rx,
411960e2a778SUrsula Braun const unsigned char *ptr,
412060e2a778SUrsula Braun int opsize)
412160e2a778SUrsula Braun {
412260e2a778SUrsula Braun #if IS_ENABLED(CONFIG_SMC)
412360e2a778SUrsula Braun if (static_branch_unlikely(&tcp_have_smc)) {
412460e2a778SUrsula Braun if (th->syn && !(opsize & 1) &&
412560e2a778SUrsula Braun opsize >= TCPOLEN_EXP_SMC_BASE &&
41267656d684SMartin KaFai Lau get_unaligned_be32(ptr) == TCPOPT_SMC_MAGIC) {
412760e2a778SUrsula Braun opt_rx->smc_ok = 1;
41287656d684SMartin KaFai Lau return true;
41297656d684SMartin KaFai Lau }
413060e2a778SUrsula Braun }
413160e2a778SUrsula Braun #endif
41327656d684SMartin KaFai Lau return false;
413360e2a778SUrsula Braun }
413460e2a778SUrsula Braun
41359349d600SPetar Penkov /* Try to parse the MSS option from the TCP header. Return 0 on failure, clamped
41369349d600SPetar Penkov * value on success.
41379349d600SPetar Penkov */
tcp_parse_mss_option(const struct tcphdr * th,u16 user_mss)413833bf9885SMaxim Mikityanskiy u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss)
41399349d600SPetar Penkov {
41409349d600SPetar Penkov const unsigned char *ptr = (const unsigned char *)(th + 1);
41419349d600SPetar Penkov int length = (th->doff * 4) - sizeof(struct tcphdr);
41429349d600SPetar Penkov u16 mss = 0;
41439349d600SPetar Penkov
41449349d600SPetar Penkov while (length > 0) {
41459349d600SPetar Penkov int opcode = *ptr++;
41469349d600SPetar Penkov int opsize;
41479349d600SPetar Penkov
41489349d600SPetar Penkov switch (opcode) {
41499349d600SPetar Penkov case TCPOPT_EOL:
41509349d600SPetar Penkov return mss;
41519349d600SPetar Penkov case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
41529349d600SPetar Penkov length--;
41539349d600SPetar Penkov continue;
41549349d600SPetar Penkov default:
41559349d600SPetar Penkov if (length < 2)
41569349d600SPetar Penkov return mss;
41579349d600SPetar Penkov opsize = *ptr++;
41589349d600SPetar Penkov if (opsize < 2) /* "silly options" */
41599349d600SPetar Penkov return mss;
41609349d600SPetar Penkov if (opsize > length)
41619349d600SPetar Penkov return mss; /* fail on partial options */
41629349d600SPetar Penkov if (opcode == TCPOPT_MSS && opsize == TCPOLEN_MSS) {
41639349d600SPetar Penkov u16 in_mss = get_unaligned_be16(ptr);
41649349d600SPetar Penkov
41659349d600SPetar Penkov if (in_mss) {
41669349d600SPetar Penkov if (user_mss && user_mss < in_mss)
41679349d600SPetar Penkov in_mss = user_mss;
41689349d600SPetar Penkov mss = in_mss;
41699349d600SPetar Penkov }
41709349d600SPetar Penkov }
41719349d600SPetar Penkov ptr += opsize - 2;
41729349d600SPetar Penkov length -= opsize;
41739349d600SPetar Penkov }
41749349d600SPetar Penkov }
41759349d600SPetar Penkov return mss;
41769349d600SPetar Penkov }
417733bf9885SMaxim Mikityanskiy EXPORT_SYMBOL_GPL(tcp_parse_mss_option);
41789349d600SPetar Penkov
41791da177e4SLinus Torvalds /* Look for tcp options. Normally only called on SYN and SYNACK packets.
41801da177e4SLinus Torvalds * But, this can also be called on packets in the established flow when
41811da177e4SLinus Torvalds * the fast version below fails.
41821da177e4SLinus Torvalds */
tcp_parse_options(const struct net * net,const struct sk_buff * skb,struct tcp_options_received * opt_rx,int estab,struct tcp_fastopen_cookie * foc)4183eed29f17SEric Dumazet void tcp_parse_options(const struct net *net,
4184eed29f17SEric Dumazet const struct sk_buff *skb,
41851a2c6181SChristoph Paasch struct tcp_options_received *opt_rx, int estab,
41862100c8d2SYuchung Cheng struct tcp_fastopen_cookie *foc)
41871da177e4SLinus Torvalds {
4188cf533ea5SEric Dumazet const unsigned char *ptr;
4189cf533ea5SEric Dumazet const struct tcphdr *th = tcp_hdr(skb);
41901da177e4SLinus Torvalds int length = (th->doff * 4) - sizeof(struct tcphdr);
41911da177e4SLinus Torvalds
4192cf533ea5SEric Dumazet ptr = (const unsigned char *)(th + 1);
41931da177e4SLinus Torvalds opt_rx->saw_tstamp = 0;
41947656d684SMartin KaFai Lau opt_rx->saw_unknown = 0;
41951da177e4SLinus Torvalds
41961da177e4SLinus Torvalds while (length > 0) {
41971da177e4SLinus Torvalds int opcode = *ptr++;
41981da177e4SLinus Torvalds int opsize;
41991da177e4SLinus Torvalds
42001da177e4SLinus Torvalds switch (opcode) {
42011da177e4SLinus Torvalds case TCPOPT_EOL:
42021da177e4SLinus Torvalds return;
42031da177e4SLinus Torvalds case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
42041da177e4SLinus Torvalds length--;
42051da177e4SLinus Torvalds continue;
42061da177e4SLinus Torvalds default:
42079609dad2SYoung Xiao if (length < 2)
42089609dad2SYoung Xiao return;
42091da177e4SLinus Torvalds opsize = *ptr++;
42101da177e4SLinus Torvalds if (opsize < 2) /* "silly options" */
42111da177e4SLinus Torvalds return;
42121da177e4SLinus Torvalds if (opsize > length)
42131da177e4SLinus Torvalds return; /* don't parse partial options */
42141da177e4SLinus Torvalds switch (opcode) {
42151da177e4SLinus Torvalds case TCPOPT_MSS:
42161da177e4SLinus Torvalds if (opsize == TCPOLEN_MSS && th->syn && !estab) {
4217d3e2ce3bSHarvey Harrison u16 in_mss = get_unaligned_be16(ptr);
42181da177e4SLinus Torvalds if (in_mss) {
4219f038ac8fSIlpo Järvinen if (opt_rx->user_mss &&
4220f038ac8fSIlpo Järvinen opt_rx->user_mss < in_mss)
42211da177e4SLinus Torvalds in_mss = opt_rx->user_mss;
42221da177e4SLinus Torvalds opt_rx->mss_clamp = in_mss;
42231da177e4SLinus Torvalds }
42241da177e4SLinus Torvalds }
42251da177e4SLinus Torvalds break;
42261da177e4SLinus Torvalds case TCPOPT_WINDOW:
4227f038ac8fSIlpo Järvinen if (opsize == TCPOLEN_WINDOW && th->syn &&
42283666f666SKuniyuki Iwashima !estab && READ_ONCE(net->ipv4.sysctl_tcp_window_scaling)) {
42291da177e4SLinus Torvalds __u8 snd_wscale = *(__u8 *)ptr;
42301da177e4SLinus Torvalds opt_rx->wscale_ok = 1;
4231589c49cbSGao Feng if (snd_wscale > TCP_MAX_WSCALE) {
4232589c49cbSGao Feng net_info_ratelimited("%s: Illegal window scaling value %d > %u received\n",
4233058bd4d2SJoe Perches __func__,
4234589c49cbSGao Feng snd_wscale,
4235589c49cbSGao Feng TCP_MAX_WSCALE);
4236589c49cbSGao Feng snd_wscale = TCP_MAX_WSCALE;
42371da177e4SLinus Torvalds }
42381da177e4SLinus Torvalds opt_rx->snd_wscale = snd_wscale;
42391da177e4SLinus Torvalds }
42401da177e4SLinus Torvalds break;
42411da177e4SLinus Torvalds case TCPOPT_TIMESTAMP:
4242f038ac8fSIlpo Järvinen if ((opsize == TCPOLEN_TIMESTAMP) &&
4243f038ac8fSIlpo Järvinen ((estab && opt_rx->tstamp_ok) ||
42443666f666SKuniyuki Iwashima (!estab && READ_ONCE(net->ipv4.sysctl_tcp_timestamps)))) {
42451da177e4SLinus Torvalds opt_rx->saw_tstamp = 1;
4246d3e2ce3bSHarvey Harrison opt_rx->rcv_tsval = get_unaligned_be32(ptr);
4247d3e2ce3bSHarvey Harrison opt_rx->rcv_tsecr = get_unaligned_be32(ptr + 4);
42481da177e4SLinus Torvalds }
42491da177e4SLinus Torvalds break;
42501da177e4SLinus Torvalds case TCPOPT_SACK_PERM:
4251f038ac8fSIlpo Järvinen if (opsize == TCPOLEN_SACK_PERM && th->syn &&
42523666f666SKuniyuki Iwashima !estab && READ_ONCE(net->ipv4.sysctl_tcp_sack)) {
4253ab56222aSVijay Subramanian opt_rx->sack_ok = TCP_SACK_SEEN;
42541da177e4SLinus Torvalds tcp_sack_reset(opt_rx);
42551da177e4SLinus Torvalds }
42561da177e4SLinus Torvalds break;
42571da177e4SLinus Torvalds
42581da177e4SLinus Torvalds case TCPOPT_SACK:
42591da177e4SLinus Torvalds if ((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) &&
42601da177e4SLinus Torvalds !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) &&
42611da177e4SLinus Torvalds opt_rx->sack_ok) {
42621da177e4SLinus Torvalds TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th;
42631da177e4SLinus Torvalds }
4264d7ea5b91SIlpo Järvinen break;
4265cfb6eeb4SYOSHIFUJI Hideaki #ifdef CONFIG_TCP_MD5SIG
4266cfb6eeb4SYOSHIFUJI Hideaki case TCPOPT_MD5SIG:
4267b2051536SKuniyuki Iwashima /* The MD5 Hash has already been
4268b2051536SKuniyuki Iwashima * checked (see tcp_v{4,6}_rcv()).
4269cfb6eeb4SYOSHIFUJI Hideaki */
4270cfb6eeb4SYOSHIFUJI Hideaki break;
4271cfb6eeb4SYOSHIFUJI Hideaki #endif
42724b74726cSKuniyuki Iwashima #ifdef CONFIG_TCP_AO
42734b74726cSKuniyuki Iwashima case TCPOPT_AO:
42744b74726cSKuniyuki Iwashima /* TCP AO has already been checked
42754b74726cSKuniyuki Iwashima * (see tcp_inbound_ao_hash()).
42764b74726cSKuniyuki Iwashima */
42774b74726cSKuniyuki Iwashima break;
42784b74726cSKuniyuki Iwashima #endif
42797f9b838bSDaniel Lee case TCPOPT_FASTOPEN:
42807f9b838bSDaniel Lee tcp_parse_fastopen_option(
42817f9b838bSDaniel Lee opsize - TCPOLEN_FASTOPEN_BASE,
42827f9b838bSDaniel Lee ptr, th->syn, foc, false);
42837f9b838bSDaniel Lee break;
42847f9b838bSDaniel Lee
42852100c8d2SYuchung Cheng case TCPOPT_EXP:
42862100c8d2SYuchung Cheng /* Fast Open option shares code 254 using a
42877f9b838bSDaniel Lee * 16 bits magic number.
42882100c8d2SYuchung Cheng */
42897f9b838bSDaniel Lee if (opsize >= TCPOLEN_EXP_FASTOPEN_BASE &&
42907f9b838bSDaniel Lee get_unaligned_be16(ptr) ==
42917656d684SMartin KaFai Lau TCPOPT_FASTOPEN_MAGIC) {
42927f9b838bSDaniel Lee tcp_parse_fastopen_option(opsize -
42937f9b838bSDaniel Lee TCPOLEN_EXP_FASTOPEN_BASE,
42947f9b838bSDaniel Lee ptr + 2, th->syn, foc, true);
42957656d684SMartin KaFai Lau break;
42967656d684SMartin KaFai Lau }
42977656d684SMartin KaFai Lau
42987656d684SMartin KaFai Lau if (smc_parse_options(th, opt_rx, ptr, opsize))
42992100c8d2SYuchung Cheng break;
43002100c8d2SYuchung Cheng
43017656d684SMartin KaFai Lau opt_rx->saw_unknown = 1;
43027656d684SMartin KaFai Lau break;
43037656d684SMartin KaFai Lau
43047656d684SMartin KaFai Lau default:
43057656d684SMartin KaFai Lau opt_rx->saw_unknown = 1;
43062100c8d2SYuchung Cheng }
43071da177e4SLinus Torvalds ptr += opsize-2;
43081da177e4SLinus Torvalds length -= opsize;
43093ff50b79SStephen Hemminger }
43101da177e4SLinus Torvalds }
43111da177e4SLinus Torvalds }
43124bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_parse_options);
43131da177e4SLinus Torvalds
tcp_parse_aligned_timestamp(struct tcp_sock * tp,const struct tcphdr * th)4314a2a385d6SEric Dumazet static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr *th)
4315a4356b29SIlpo Järvinen {
4316cf533ea5SEric Dumazet const __be32 *ptr = (const __be32 *)(th + 1);
4317a4356b29SIlpo Järvinen
4318a4356b29SIlpo Järvinen if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
4319a4356b29SIlpo Järvinen | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
4320a4356b29SIlpo Järvinen tp->rx_opt.saw_tstamp = 1;
4321a4356b29SIlpo Järvinen ++ptr;
4322a4356b29SIlpo Järvinen tp->rx_opt.rcv_tsval = ntohl(*ptr);
4323a4356b29SIlpo Järvinen ++ptr;
4324e3e12028SAndrew Vagin if (*ptr)
4325ee684b6fSAndrey Vagin tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset;
4326e3e12028SAndrew Vagin else
4327e3e12028SAndrew Vagin tp->rx_opt.rcv_tsecr = 0;
4328a2a385d6SEric Dumazet return true;
4329a4356b29SIlpo Järvinen }
4330a2a385d6SEric Dumazet return false;
4331a4356b29SIlpo Järvinen }
4332a4356b29SIlpo Järvinen
43331da177e4SLinus Torvalds /* Fast parse options. This hopes to only see timestamps.
43341da177e4SLinus Torvalds * If it is wrong it falls back on tcp_parse_options().
43351da177e4SLinus Torvalds */
tcp_fast_parse_options(const struct net * net,const struct sk_buff * skb,const struct tcphdr * th,struct tcp_sock * tp)4336eed29f17SEric Dumazet static bool tcp_fast_parse_options(const struct net *net,
4337eed29f17SEric Dumazet const struct sk_buff *skb,
43381a2c6181SChristoph Paasch const struct tcphdr *th, struct tcp_sock *tp)
43391da177e4SLinus Torvalds {
43404957faadSWilliam Allen Simpson /* In the spirit of fast parsing, compare doff directly to constant
43414957faadSWilliam Allen Simpson * values. Because equality is used, short doff can be ignored here.
43424957faadSWilliam Allen Simpson */
43434957faadSWilliam Allen Simpson if (th->doff == (sizeof(*th) / 4)) {
43441da177e4SLinus Torvalds tp->rx_opt.saw_tstamp = 0;
4345a2a385d6SEric Dumazet return false;
43461da177e4SLinus Torvalds } else if (tp->rx_opt.tstamp_ok &&
43474957faadSWilliam Allen Simpson th->doff == ((sizeof(*th) + TCPOLEN_TSTAMP_ALIGNED) / 4)) {
4348a4356b29SIlpo Järvinen if (tcp_parse_aligned_timestamp(tp, th))
4349a2a385d6SEric Dumazet return true;
43501da177e4SLinus Torvalds }
4351ee684b6fSAndrey Vagin
4352eed29f17SEric Dumazet tcp_parse_options(net, skb, &tp->rx_opt, 1, NULL);
4353e3e12028SAndrew Vagin if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
4354ee684b6fSAndrey Vagin tp->rx_opt.rcv_tsecr -= tp->tsoffset;
4355ee684b6fSAndrey Vagin
4356a2a385d6SEric Dumazet return true;
43571da177e4SLinus Torvalds }
43581da177e4SLinus Torvalds
4359f7dca36fSDmitry Safonov #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
43607d5d5525SYOSHIFUJI Hideaki /*
4361f7dca36fSDmitry Safonov * Parse Signature options
43627d5d5525SYOSHIFUJI Hideaki */
tcp_do_parse_auth_options(const struct tcphdr * th,const u8 ** md5_hash,const u8 ** ao_hash)4363f7dca36fSDmitry Safonov int tcp_do_parse_auth_options(const struct tcphdr *th,
4364f7dca36fSDmitry Safonov const u8 **md5_hash, const u8 **ao_hash)
43657d5d5525SYOSHIFUJI Hideaki {
43667d5d5525SYOSHIFUJI Hideaki int length = (th->doff << 2) - sizeof(*th);
4367cf533ea5SEric Dumazet const u8 *ptr = (const u8 *)(th + 1);
4368f7dca36fSDmitry Safonov unsigned int minlen = TCPOLEN_MD5SIG;
4369f7dca36fSDmitry Safonov
4370f7dca36fSDmitry Safonov if (IS_ENABLED(CONFIG_TCP_AO))
4371f7dca36fSDmitry Safonov minlen = sizeof(struct tcp_ao_hdr) + 1;
4372f7dca36fSDmitry Safonov
4373f7dca36fSDmitry Safonov *md5_hash = NULL;
4374f7dca36fSDmitry Safonov *ao_hash = NULL;
43757d5d5525SYOSHIFUJI Hideaki
43767e5a206aSJann Horn /* If not enough data remaining, we can short cut */
4377f7dca36fSDmitry Safonov while (length >= minlen) {
43787d5d5525SYOSHIFUJI Hideaki int opcode = *ptr++;
43797d5d5525SYOSHIFUJI Hideaki int opsize;
43807d5d5525SYOSHIFUJI Hideaki
43817d5d5525SYOSHIFUJI Hideaki switch (opcode) {
43827d5d5525SYOSHIFUJI Hideaki case TCPOPT_EOL:
4383f7dca36fSDmitry Safonov return 0;
43847d5d5525SYOSHIFUJI Hideaki case TCPOPT_NOP:
43857d5d5525SYOSHIFUJI Hideaki length--;
43867d5d5525SYOSHIFUJI Hideaki continue;
43877d5d5525SYOSHIFUJI Hideaki default:
43887d5d5525SYOSHIFUJI Hideaki opsize = *ptr++;
43897d5d5525SYOSHIFUJI Hideaki if (opsize < 2 || opsize > length)
4390f7dca36fSDmitry Safonov return -EINVAL;
4391f7dca36fSDmitry Safonov if (opcode == TCPOPT_MD5SIG) {
4392f7dca36fSDmitry Safonov if (opsize != TCPOLEN_MD5SIG)
4393f7dca36fSDmitry Safonov return -EINVAL;
4394f7dca36fSDmitry Safonov if (unlikely(*md5_hash || *ao_hash))
4395f7dca36fSDmitry Safonov return -EEXIST;
4396f7dca36fSDmitry Safonov *md5_hash = ptr;
4397f7dca36fSDmitry Safonov } else if (opcode == TCPOPT_AO) {
4398f7dca36fSDmitry Safonov if (opsize <= sizeof(struct tcp_ao_hdr))
4399f7dca36fSDmitry Safonov return -EINVAL;
4400f7dca36fSDmitry Safonov if (unlikely(*md5_hash || *ao_hash))
4401f7dca36fSDmitry Safonov return -EEXIST;
4402f7dca36fSDmitry Safonov *ao_hash = ptr;
4403f7dca36fSDmitry Safonov }
44047d5d5525SYOSHIFUJI Hideaki }
44057d5d5525SYOSHIFUJI Hideaki ptr += opsize - 2;
44067d5d5525SYOSHIFUJI Hideaki length -= opsize;
44077d5d5525SYOSHIFUJI Hideaki }
4408f7dca36fSDmitry Safonov return 0;
44097d5d5525SYOSHIFUJI Hideaki }
4410f7dca36fSDmitry Safonov EXPORT_SYMBOL(tcp_do_parse_auth_options);
44117d5d5525SYOSHIFUJI Hideaki #endif
44127d5d5525SYOSHIFUJI Hideaki
44131da177e4SLinus Torvalds /* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM
44141da177e4SLinus Torvalds *
44151da177e4SLinus Torvalds * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
44161da177e4SLinus Torvalds * it can pass through stack. So, the following predicate verifies that
44171da177e4SLinus Torvalds * this segment is not used for anything but congestion avoidance or
44181da177e4SLinus Torvalds * fast retransmit. Moreover, we even are able to eliminate most of such
44191da177e4SLinus Torvalds * second order effects, if we apply some small "replay" window (~RTO)
44201da177e4SLinus Torvalds * to timestamp space.
44211da177e4SLinus Torvalds *
44221da177e4SLinus Torvalds * All these measures still do not guarantee that we reject wrapped ACKs
44231da177e4SLinus Torvalds * on networks with high bandwidth, when sequence space is recycled fastly,
44241da177e4SLinus Torvalds * but it guarantees that such events will be very rare and do not affect
44251da177e4SLinus Torvalds * connection seriously. This doesn't look nice, but alas, PAWS is really
44261da177e4SLinus Torvalds * buggy extension.
44271da177e4SLinus Torvalds *
44281da177e4SLinus Torvalds * [ Later note. Even worse! It is buggy for segments _with_ data. RFC
44291da177e4SLinus Torvalds * states that events when retransmit arrives after original data are rare.
44301da177e4SLinus Torvalds * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is
44311da177e4SLinus Torvalds * the biggest problem on large power networks even with minor reordering.
44321da177e4SLinus Torvalds * OK, let's give it small replay window. If peer clock is even 1hz, it is safe
44331da177e4SLinus Torvalds * up to bandwidth of 18Gigabit/sec. 8) ]
44341da177e4SLinus Torvalds */
44351da177e4SLinus Torvalds
44369c25aae0SEric Dumazet /* Estimates max number of increments of remote peer TSval in
44379c25aae0SEric Dumazet * a replay window (based on our current RTO estimation).
44389c25aae0SEric Dumazet */
tcp_tsval_replay(const struct sock * sk)44399c25aae0SEric Dumazet static u32 tcp_tsval_replay(const struct sock *sk)
44409c25aae0SEric Dumazet {
44419c25aae0SEric Dumazet /* If we use usec TS resolution,
44429c25aae0SEric Dumazet * then expect the remote peer to use the same resolution.
44439c25aae0SEric Dumazet */
44449c25aae0SEric Dumazet if (tcp_sk(sk)->tcp_usec_ts)
44459c25aae0SEric Dumazet return inet_csk(sk)->icsk_rto * (USEC_PER_SEC / HZ);
44469c25aae0SEric Dumazet
44479c25aae0SEric Dumazet /* RFC 7323 recommends a TSval clock between 1ms and 1sec.
44489c25aae0SEric Dumazet * We know that some OS (including old linux) can use 1200 Hz.
44499c25aae0SEric Dumazet */
44509c25aae0SEric Dumazet return inet_csk(sk)->icsk_rto * 1200 / HZ;
44519c25aae0SEric Dumazet }
44529c25aae0SEric Dumazet
tcp_disordered_ack(const struct sock * sk,const struct sk_buff * skb)4453463c84b9SArnaldo Carvalho de Melo static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb)
44541da177e4SLinus Torvalds {
4455cf533ea5SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk);
4456cf533ea5SEric Dumazet const struct tcphdr *th = tcp_hdr(skb);
44571da177e4SLinus Torvalds u32 seq = TCP_SKB_CB(skb)->seq;
44581da177e4SLinus Torvalds u32 ack = TCP_SKB_CB(skb)->ack_seq;
44591da177e4SLinus Torvalds
44609c25aae0SEric Dumazet return /* 1. Pure ACK with correct sequence number. */
44611da177e4SLinus Torvalds (th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) &&
44621da177e4SLinus Torvalds
44631da177e4SLinus Torvalds /* 2. ... and duplicate ACK. */
44641da177e4SLinus Torvalds ack == tp->snd_una &&
44651da177e4SLinus Torvalds
44661da177e4SLinus Torvalds /* 3. ... and does not update window. */
44671da177e4SLinus Torvalds !tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) &&
44681da177e4SLinus Torvalds
44691da177e4SLinus Torvalds /* 4. ... and sits in replay window. */
44709c25aae0SEric Dumazet (s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) <=
44719c25aae0SEric Dumazet tcp_tsval_replay(sk);
44721da177e4SLinus Torvalds }
44731da177e4SLinus Torvalds
tcp_paws_discard(const struct sock * sk,const struct sk_buff * skb)447467b95bd7SVijay Subramanian static inline bool tcp_paws_discard(const struct sock *sk,
4475056834d9SIlpo Järvinen const struct sk_buff *skb)
44761da177e4SLinus Torvalds {
4477463c84b9SArnaldo Carvalho de Melo const struct tcp_sock *tp = tcp_sk(sk);
4478c887e6d2SIlpo Järvinen
4479c887e6d2SIlpo Järvinen return !tcp_paws_check(&tp->rx_opt, TCP_PAWS_WINDOW) &&
4480c887e6d2SIlpo Järvinen !tcp_disordered_ack(sk, skb);
44811da177e4SLinus Torvalds }
44821da177e4SLinus Torvalds
44831da177e4SLinus Torvalds /* Check segment sequence number for validity.
44841da177e4SLinus Torvalds *
44851da177e4SLinus Torvalds * Segment controls are considered valid, if the segment
44861da177e4SLinus Torvalds * fits to the window after truncation to the window. Acceptability
44871da177e4SLinus Torvalds * of data (and SYN, FIN, of course) is checked separately.
44881da177e4SLinus Torvalds * See tcp_data_queue(), for example.
44891da177e4SLinus Torvalds *
44901da177e4SLinus Torvalds * Also, controls (RST is main one) are accepted using RCV.WUP instead
44911da177e4SLinus Torvalds * of RCV.NXT. Peer still did not advance his SND.UNA when we
44921da177e4SLinus Torvalds * delayed ACK, so that hisSND.UNA<=ourRCV.WUP.
44931da177e4SLinus Torvalds * (borrowed from freebsd)
44941da177e4SLinus Torvalds */
44951da177e4SLinus Torvalds
tcp_sequence(const struct tcp_sock * tp,u32 seq,u32 end_seq)4496b4469349SEric Dumazet static enum skb_drop_reason tcp_sequence(const struct tcp_sock *tp,
4497b4469349SEric Dumazet u32 seq, u32 end_seq)
44981da177e4SLinus Torvalds {
4499b4469349SEric Dumazet if (before(end_seq, tp->rcv_wup))
4500b4469349SEric Dumazet return SKB_DROP_REASON_TCP_OLD_SEQUENCE;
4501b4469349SEric Dumazet
4502b4469349SEric Dumazet if (after(seq, tp->rcv_nxt + tcp_receive_window(tp)))
4503b4469349SEric Dumazet return SKB_DROP_REASON_TCP_INVALID_SEQUENCE;
4504b4469349SEric Dumazet
4505b4469349SEric Dumazet return SKB_NOT_DROPPED_YET;
45061da177e4SLinus Torvalds }
45071da177e4SLinus Torvalds
45085e514f1cSEric Dumazet
tcp_done_with_error(struct sock * sk,int err)45095e514f1cSEric Dumazet void tcp_done_with_error(struct sock *sk, int err)
45105e514f1cSEric Dumazet {
45115e514f1cSEric Dumazet /* This barrier is coupled with smp_rmb() in tcp_poll() */
45125e514f1cSEric Dumazet WRITE_ONCE(sk->sk_err, err);
45135e514f1cSEric Dumazet smp_wmb();
45145e514f1cSEric Dumazet
45155e514f1cSEric Dumazet tcp_write_queue_purge(sk);
45165e514f1cSEric Dumazet tcp_done(sk);
45175e514f1cSEric Dumazet
45185e514f1cSEric Dumazet if (!sock_flag(sk, SOCK_DEAD))
45195e514f1cSEric Dumazet sk_error_report(sk);
45205e514f1cSEric Dumazet }
45215e514f1cSEric Dumazet EXPORT_SYMBOL(tcp_done_with_error);
45225e514f1cSEric Dumazet
45231da177e4SLinus Torvalds /* When we get a reset we do this. */
tcp_reset(struct sock * sk,struct sk_buff * skb)4524049fe386SFlorian Westphal void tcp_reset(struct sock *sk, struct sk_buff *skb)
45251da177e4SLinus Torvalds {
45265e514f1cSEric Dumazet int err;
45275e514f1cSEric Dumazet
45285941521cSSong Liu trace_tcp_receive_reset(sk);
45295941521cSSong Liu
45306787b7e3SJianguo Wu /* mptcp can't tell us to ignore reset pkts,
45316787b7e3SJianguo Wu * so just ignore the return value of mptcp_incoming_options().
45326787b7e3SJianguo Wu */
4533049fe386SFlorian Westphal if (sk_is_mptcp(sk))
4534049fe386SFlorian Westphal mptcp_incoming_options(sk, skb);
4535049fe386SFlorian Westphal
45361da177e4SLinus Torvalds /* We want the right error as BSD sees it (and indeed as we do). */
45371da177e4SLinus Torvalds switch (sk->sk_state) {
45381da177e4SLinus Torvalds case TCP_SYN_SENT:
45395e514f1cSEric Dumazet err = ECONNREFUSED;
45401da177e4SLinus Torvalds break;
45411da177e4SLinus Torvalds case TCP_CLOSE_WAIT:
45425e514f1cSEric Dumazet err = EPIPE;
45431da177e4SLinus Torvalds break;
45441da177e4SLinus Torvalds case TCP_CLOSE:
45451da177e4SLinus Torvalds return;
45461da177e4SLinus Torvalds default:
45475e514f1cSEric Dumazet err = ECONNRESET;
45481da177e4SLinus Torvalds }
45495e514f1cSEric Dumazet tcp_done_with_error(sk, err);
45501da177e4SLinus Torvalds }
45511da177e4SLinus Torvalds
45521da177e4SLinus Torvalds /*
45531da177e4SLinus Torvalds * Process the FIN bit. This now behaves as it is supposed to work
45541da177e4SLinus Torvalds * and the FIN takes effect when it is validly part of sequence
45551da177e4SLinus Torvalds * space. Not before when we get holes.
45561da177e4SLinus Torvalds *
45571da177e4SLinus Torvalds * If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
45581da177e4SLinus Torvalds * (and thence onto LAST-ACK and finally, CLOSE, we never enter
45591da177e4SLinus Torvalds * TIME-WAIT)
45601da177e4SLinus Torvalds *
45611da177e4SLinus Torvalds * If we are in FINWAIT-1, a received FIN indicates simultaneous
45621da177e4SLinus Torvalds * close and we go into CLOSING (and later onto TIME-WAIT)
45631da177e4SLinus Torvalds *
45641da177e4SLinus Torvalds * If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
45651da177e4SLinus Torvalds */
tcp_fin(struct sock * sk)4566e3e17b77SEric Dumazet void tcp_fin(struct sock *sk)
45671da177e4SLinus Torvalds {
45681da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
45691da177e4SLinus Torvalds
4570463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk);
45711da177e4SLinus Torvalds
4572e14cadfdSEric Dumazet WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
45731da177e4SLinus Torvalds sock_set_flag(sk, SOCK_DONE);
45741da177e4SLinus Torvalds
45751da177e4SLinus Torvalds switch (sk->sk_state) {
45761da177e4SLinus Torvalds case TCP_SYN_RECV:
45771da177e4SLinus Torvalds case TCP_ESTABLISHED:
45781da177e4SLinus Torvalds /* Move to CLOSE_WAIT */
45791da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSE_WAIT);
458031954cd8SWei Wang inet_csk_enter_pingpong_mode(sk);
45811da177e4SLinus Torvalds break;
45821da177e4SLinus Torvalds
45831da177e4SLinus Torvalds case TCP_CLOSE_WAIT:
45841da177e4SLinus Torvalds case TCP_CLOSING:
45851da177e4SLinus Torvalds /* Received a retransmission of the FIN, do
45861da177e4SLinus Torvalds * nothing.
45871da177e4SLinus Torvalds */
45881da177e4SLinus Torvalds break;
45891da177e4SLinus Torvalds case TCP_LAST_ACK:
45901da177e4SLinus Torvalds /* RFC793: Remain in the LAST-ACK state. */
45911da177e4SLinus Torvalds break;
45921da177e4SLinus Torvalds
45931da177e4SLinus Torvalds case TCP_FIN_WAIT1:
45941da177e4SLinus Torvalds /* This case occurs when a simultaneous close
45951da177e4SLinus Torvalds * happens, we must ack the received FIN and
45961da177e4SLinus Torvalds * enter the CLOSING state.
45971da177e4SLinus Torvalds */
45981da177e4SLinus Torvalds tcp_send_ack(sk);
45991da177e4SLinus Torvalds tcp_set_state(sk, TCP_CLOSING);
46001da177e4SLinus Torvalds break;
46011da177e4SLinus Torvalds case TCP_FIN_WAIT2:
46021da177e4SLinus Torvalds /* Received a FIN -- send ACK and enter TIME_WAIT. */
46031da177e4SLinus Torvalds tcp_send_ack(sk);
46041da177e4SLinus Torvalds tcp_time_wait(sk, TCP_TIME_WAIT, 0);
46051da177e4SLinus Torvalds break;
46061da177e4SLinus Torvalds default:
46071da177e4SLinus Torvalds /* Only TCP_LISTEN and TCP_CLOSE are left, in these
46081da177e4SLinus Torvalds * cases we should never reach this piece of code.
46091da177e4SLinus Torvalds */
4610058bd4d2SJoe Perches pr_err("%s: Impossible, sk->sk_state=%d\n",
46110dc47877SHarvey Harrison __func__, sk->sk_state);
46121da177e4SLinus Torvalds break;
46133ff50b79SStephen Hemminger }
46141da177e4SLinus Torvalds
46151da177e4SLinus Torvalds /* It _is_ possible, that we have something out-of-order _after_ FIN.
46161da177e4SLinus Torvalds * Probably, we should reset in this case. For now drop them.
46171da177e4SLinus Torvalds */
46189f5afeaeSYaogong Wang skb_rbtree_purge(&tp->out_of_order_queue);
4619e60402d0SIlpo Järvinen if (tcp_is_sack(tp))
46201da177e4SLinus Torvalds tcp_sack_reset(&tp->rx_opt);
46211da177e4SLinus Torvalds
46221da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) {
46231da177e4SLinus Torvalds sk->sk_state_change(sk);
46241da177e4SLinus Torvalds
46251da177e4SLinus Torvalds /* Do not send POLL_HUP for half duplex close. */
46261da177e4SLinus Torvalds if (sk->sk_shutdown == SHUTDOWN_MASK ||
46271da177e4SLinus Torvalds sk->sk_state == TCP_CLOSE)
46288d8ad9d7SPavel Emelyanov sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
46291da177e4SLinus Torvalds else
46308d8ad9d7SPavel Emelyanov sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
46311da177e4SLinus Torvalds }
46321da177e4SLinus Torvalds }
46331da177e4SLinus Torvalds
tcp_sack_extend(struct tcp_sack_block * sp,u32 seq,u32 end_seq)4634a2a385d6SEric Dumazet static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,
4635056834d9SIlpo Järvinen u32 end_seq)
46361da177e4SLinus Torvalds {
46371da177e4SLinus Torvalds if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) {
46381da177e4SLinus Torvalds if (before(seq, sp->start_seq))
46391da177e4SLinus Torvalds sp->start_seq = seq;
46401da177e4SLinus Torvalds if (after(end_seq, sp->end_seq))
46411da177e4SLinus Torvalds sp->end_seq = end_seq;
4642a2a385d6SEric Dumazet return true;
46431da177e4SLinus Torvalds }
4644a2a385d6SEric Dumazet return false;
46451da177e4SLinus Torvalds }
46461da177e4SLinus Torvalds
tcp_dsack_set(struct sock * sk,u32 seq,u32 end_seq)46471ed83465SPavel Emelyanov static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq)
46481da177e4SLinus Torvalds {
46491ed83465SPavel Emelyanov struct tcp_sock *tp = tcp_sk(sk);
46501ed83465SPavel Emelyanov
465158ebb1c8SKuniyuki Iwashima if (tcp_is_sack(tp) && READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_dsack)) {
465240b215e5SPavel Emelyanov int mib_idx;
465340b215e5SPavel Emelyanov
46541da177e4SLinus Torvalds if (before(seq, tp->rcv_nxt))
465540b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPDSACKOLDSENT;
46561da177e4SLinus Torvalds else
465740b215e5SPavel Emelyanov mib_idx = LINUX_MIB_TCPDSACKOFOSENT;
465840b215e5SPavel Emelyanov
4659c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), mib_idx);
46601da177e4SLinus Torvalds
46611da177e4SLinus Torvalds tp->rx_opt.dsack = 1;
46621da177e4SLinus Torvalds tp->duplicate_sack[0].start_seq = seq;
46631da177e4SLinus Torvalds tp->duplicate_sack[0].end_seq = end_seq;
46641da177e4SLinus Torvalds }
46651da177e4SLinus Torvalds }
46661da177e4SLinus Torvalds
tcp_dsack_extend(struct sock * sk,u32 seq,u32 end_seq)46671ed83465SPavel Emelyanov static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq)
46681da177e4SLinus Torvalds {
46691ed83465SPavel Emelyanov struct tcp_sock *tp = tcp_sk(sk);
46701ed83465SPavel Emelyanov
46711da177e4SLinus Torvalds if (!tp->rx_opt.dsack)
46721ed83465SPavel Emelyanov tcp_dsack_set(sk, seq, end_seq);
46731da177e4SLinus Torvalds else
46741da177e4SLinus Torvalds tcp_sack_extend(tp->duplicate_sack, seq, end_seq);
46751da177e4SLinus Torvalds }
46761da177e4SLinus Torvalds
tcp_rcv_spurious_retrans(struct sock * sk,const struct sk_buff * skb)46777788174eSYuchung Cheng static void tcp_rcv_spurious_retrans(struct sock *sk, const struct sk_buff *skb)
46787788174eSYuchung Cheng {
46797788174eSYuchung Cheng /* When the ACK path fails or drops most ACKs, the sender would
46807788174eSYuchung Cheng * timeout and spuriously retransmit the same segment repeatedly.
468193946301SDavid Morley * If it seems our ACKs are not reaching the other side,
468293946301SDavid Morley * based on receiving a duplicate data segment with new flowlabel
468393946301SDavid Morley * (suggesting the sender suffered an RTO), and we are not already
468493946301SDavid Morley * repathing due to our own RTO, then rehash the socket to repath our
468593946301SDavid Morley * packets.
46867788174eSYuchung Cheng */
468793946301SDavid Morley #if IS_ENABLED(CONFIG_IPV6)
468893946301SDavid Morley if (inet_csk(sk)->icsk_ca_state != TCP_CA_Loss &&
468993946301SDavid Morley skb->protocol == htons(ETH_P_IPV6) &&
469093946301SDavid Morley (tcp_sk(sk)->inet_conn.icsk_ack.lrcv_flowlabel !=
469193946301SDavid Morley ntohl(ip6_flowlabel(ipv6_hdr(skb)))) &&
46929c30ae83SYuchung Cheng sk_rethink_txhash(sk))
469332efcc06SAbdul Kabbani NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDUPLICATEDATAREHASH);
469495b9a87cSDavid Morley
469595b9a87cSDavid Morley /* Save last flowlabel after a spurious retrans. */
469695b9a87cSDavid Morley tcp_save_lrcv_flowlabel(sk, skb);
469793946301SDavid Morley #endif
469832efcc06SAbdul Kabbani }
46997788174eSYuchung Cheng
tcp_send_dupack(struct sock * sk,const struct sk_buff * skb)4700cf533ea5SEric Dumazet static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb)
47011da177e4SLinus Torvalds {
47021da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
47031da177e4SLinus Torvalds
47041da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
47051da177e4SLinus Torvalds before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
4706c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
47079a9c9b51SEric Dumazet tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
47081da177e4SLinus Torvalds
470958ebb1c8SKuniyuki Iwashima if (tcp_is_sack(tp) && READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_dsack)) {
47101da177e4SLinus Torvalds u32 end_seq = TCP_SKB_CB(skb)->end_seq;
47111da177e4SLinus Torvalds
47127788174eSYuchung Cheng tcp_rcv_spurious_retrans(sk, skb);
47131da177e4SLinus Torvalds if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))
47141da177e4SLinus Torvalds end_seq = tp->rcv_nxt;
47151ed83465SPavel Emelyanov tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, end_seq);
47161da177e4SLinus Torvalds }
47171da177e4SLinus Torvalds }
47181da177e4SLinus Torvalds
47191da177e4SLinus Torvalds tcp_send_ack(sk);
47201da177e4SLinus Torvalds }
47211da177e4SLinus Torvalds
47221da177e4SLinus Torvalds /* These routines update the SACK block as out-of-order packets arrive or
47231da177e4SLinus Torvalds * in-order packets close up the sequence space.
47241da177e4SLinus Torvalds */
tcp_sack_maybe_coalesce(struct tcp_sock * tp)47251da177e4SLinus Torvalds static void tcp_sack_maybe_coalesce(struct tcp_sock *tp)
47261da177e4SLinus Torvalds {
47271da177e4SLinus Torvalds int this_sack;
47281da177e4SLinus Torvalds struct tcp_sack_block *sp = &tp->selective_acks[0];
47291da177e4SLinus Torvalds struct tcp_sack_block *swalk = sp + 1;
47301da177e4SLinus Torvalds
47311da177e4SLinus Torvalds /* See if the recent change to the first SACK eats into
47321da177e4SLinus Torvalds * or hits the sequence space of other SACK blocks, if so coalesce.
47331da177e4SLinus Torvalds */
47341da177e4SLinus Torvalds for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;) {
47351da177e4SLinus Torvalds if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) {
47361da177e4SLinus Torvalds int i;
47371da177e4SLinus Torvalds
47381da177e4SLinus Torvalds /* Zap SWALK, by moving every further SACK up by one slot.
47391da177e4SLinus Torvalds * Decrease num_sacks.
47401da177e4SLinus Torvalds */
47411da177e4SLinus Torvalds tp->rx_opt.num_sacks--;
47421da177e4SLinus Torvalds for (i = this_sack; i < tp->rx_opt.num_sacks; i++)
47431da177e4SLinus Torvalds sp[i] = sp[i + 1];
47441da177e4SLinus Torvalds continue;
47451da177e4SLinus Torvalds }
474644797589SJulia Lawall this_sack++;
474744797589SJulia Lawall swalk++;
47481da177e4SLinus Torvalds }
47491da177e4SLinus Torvalds }
47501da177e4SLinus Torvalds
tcp_sack_compress_send_ack(struct sock * sk)475130c6f0bfSfuyuanli void tcp_sack_compress_send_ack(struct sock *sk)
47522b195850SEric Dumazet {
47532b195850SEric Dumazet struct tcp_sock *tp = tcp_sk(sk);
47542b195850SEric Dumazet
47552b195850SEric Dumazet if (!tp->compressed_ack)
47562b195850SEric Dumazet return;
47572b195850SEric Dumazet
47582b195850SEric Dumazet if (hrtimer_try_to_cancel(&tp->compressed_ack_timer) == 1)
47592b195850SEric Dumazet __sock_put(sk);
47602b195850SEric Dumazet
47612b195850SEric Dumazet /* Since we have to send one ack finally,
47622b195850SEric Dumazet * substract one from tp->compressed_ack to keep
47632b195850SEric Dumazet * LINUX_MIB_TCPACKCOMPRESSED accurate.
47642b195850SEric Dumazet */
47652b195850SEric Dumazet NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPACKCOMPRESSED,
47662b195850SEric Dumazet tp->compressed_ack - 1);
47672b195850SEric Dumazet
47682b195850SEric Dumazet tp->compressed_ack = 0;
47692b195850SEric Dumazet tcp_send_ack(sk);
47702b195850SEric Dumazet }
47712b195850SEric Dumazet
4772ccd0628fSEric Dumazet /* Reasonable amount of sack blocks included in TCP SACK option
4773ccd0628fSEric Dumazet * The max is 4, but this becomes 3 if TCP timestamps are there.
4774ccd0628fSEric Dumazet * Given that SACK packets might be lost, be conservative and use 2.
4775ccd0628fSEric Dumazet */
4776ccd0628fSEric Dumazet #define TCP_SACK_BLOCKS_EXPECTED 2
4777ccd0628fSEric Dumazet
tcp_sack_new_ofo_skb(struct sock * sk,u32 seq,u32 end_seq)47781da177e4SLinus Torvalds static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq)
47791da177e4SLinus Torvalds {
47801da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
47811da177e4SLinus Torvalds struct tcp_sack_block *sp = &tp->selective_acks[0];
47821da177e4SLinus Torvalds int cur_sacks = tp->rx_opt.num_sacks;
47831da177e4SLinus Torvalds int this_sack;
47841da177e4SLinus Torvalds
47851da177e4SLinus Torvalds if (!cur_sacks)
47861da177e4SLinus Torvalds goto new_sack;
47871da177e4SLinus Torvalds
47881da177e4SLinus Torvalds for (this_sack = 0; this_sack < cur_sacks; this_sack++, sp++) {
47891da177e4SLinus Torvalds if (tcp_sack_extend(sp, seq, end_seq)) {
4790ccd0628fSEric Dumazet if (this_sack >= TCP_SACK_BLOCKS_EXPECTED)
4791ccd0628fSEric Dumazet tcp_sack_compress_send_ack(sk);
47921da177e4SLinus Torvalds /* Rotate this_sack to the first one. */
47931da177e4SLinus Torvalds for (; this_sack > 0; this_sack--, sp--)
4794a0bffffcSIlpo Järvinen swap(*sp, *(sp - 1));
47951da177e4SLinus Torvalds if (cur_sacks > 1)
47961da177e4SLinus Torvalds tcp_sack_maybe_coalesce(tp);
47971da177e4SLinus Torvalds return;
47981da177e4SLinus Torvalds }
47991da177e4SLinus Torvalds }
48001da177e4SLinus Torvalds
4801ccd0628fSEric Dumazet if (this_sack >= TCP_SACK_BLOCKS_EXPECTED)
4802ccd0628fSEric Dumazet tcp_sack_compress_send_ack(sk);
4803ccd0628fSEric Dumazet
48041da177e4SLinus Torvalds /* Could not find an adjacent existing SACK, build a new one,
48051da177e4SLinus Torvalds * put it at the front, and shift everyone else down. We
48061da177e4SLinus Torvalds * always know there is at least one SACK present already here.
48071da177e4SLinus Torvalds *
48081da177e4SLinus Torvalds * If the sack array is full, forget about the last one.
48091da177e4SLinus Torvalds */
48104389ddedSAdam Langley if (this_sack >= TCP_NUM_SACKS) {
48111da177e4SLinus Torvalds this_sack--;
48121da177e4SLinus Torvalds tp->rx_opt.num_sacks--;
48131da177e4SLinus Torvalds sp--;
48141da177e4SLinus Torvalds }
48151da177e4SLinus Torvalds for (; this_sack > 0; this_sack--, sp--)
48161da177e4SLinus Torvalds *sp = *(sp - 1);
48171da177e4SLinus Torvalds
48181da177e4SLinus Torvalds new_sack:
48191da177e4SLinus Torvalds /* Build the new head SACK, and we're done. */
48201da177e4SLinus Torvalds sp->start_seq = seq;
48211da177e4SLinus Torvalds sp->end_seq = end_seq;
48221da177e4SLinus Torvalds tp->rx_opt.num_sacks++;
48231da177e4SLinus Torvalds }
48241da177e4SLinus Torvalds
48251da177e4SLinus Torvalds /* RCV.NXT advances, some SACKs should be eaten. */
48261da177e4SLinus Torvalds
tcp_sack_remove(struct tcp_sock * tp)48271da177e4SLinus Torvalds static void tcp_sack_remove(struct tcp_sock *tp)
48281da177e4SLinus Torvalds {
48291da177e4SLinus Torvalds struct tcp_sack_block *sp = &tp->selective_acks[0];
48301da177e4SLinus Torvalds int num_sacks = tp->rx_opt.num_sacks;
48311da177e4SLinus Torvalds int this_sack;
48321da177e4SLinus Torvalds
48331da177e4SLinus Torvalds /* Empty ofo queue, hence, all the SACKs are eaten. Clear. */
48349f5afeaeSYaogong Wang if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
48351da177e4SLinus Torvalds tp->rx_opt.num_sacks = 0;
48361da177e4SLinus Torvalds return;
48371da177e4SLinus Torvalds }
48381da177e4SLinus Torvalds
48391da177e4SLinus Torvalds for (this_sack = 0; this_sack < num_sacks;) {
48401da177e4SLinus Torvalds /* Check if the start of the sack is covered by RCV.NXT. */
48411da177e4SLinus Torvalds if (!before(tp->rcv_nxt, sp->start_seq)) {
48421da177e4SLinus Torvalds int i;
48431da177e4SLinus Torvalds
48441da177e4SLinus Torvalds /* RCV.NXT must cover all the block! */
4845547b792cSIlpo Järvinen WARN_ON(before(tp->rcv_nxt, sp->end_seq));
48461da177e4SLinus Torvalds
48471da177e4SLinus Torvalds /* Zap this SACK, by moving forward any other SACKS. */
48481da177e4SLinus Torvalds for (i = this_sack+1; i < num_sacks; i++)
48491da177e4SLinus Torvalds tp->selective_acks[i-1] = tp->selective_acks[i];
48501da177e4SLinus Torvalds num_sacks--;
48511da177e4SLinus Torvalds continue;
48521da177e4SLinus Torvalds }
48531da177e4SLinus Torvalds this_sack++;
48541da177e4SLinus Torvalds sp++;
48551da177e4SLinus Torvalds }
48561da177e4SLinus Torvalds tp->rx_opt.num_sacks = num_sacks;
48571da177e4SLinus Torvalds }
48581da177e4SLinus Torvalds
48591402d366SEric Dumazet /**
48601402d366SEric Dumazet * tcp_try_coalesce - try to merge skb to prior one
48611402d366SEric Dumazet * @sk: socket
48621402d366SEric Dumazet * @to: prior buffer
48631402d366SEric Dumazet * @from: buffer to add in queue
4864923dd347SEric Dumazet * @fragstolen: pointer to boolean
48651402d366SEric Dumazet *
48661402d366SEric Dumazet * Before queueing skb @from after @to, try to merge them
48671402d366SEric Dumazet * to reduce overall memory use and queue lengths, if cost is small.
48681402d366SEric Dumazet * Packets in ofo or receive queues can stay a long time.
48691402d366SEric Dumazet * Better try to coalesce them right now to avoid future collapses.
4870783c175fSEric Dumazet * Returns true if caller should free @from instead of queueing it
48711402d366SEric Dumazet */
tcp_try_coalesce(struct sock * sk,struct sk_buff * to,struct sk_buff * from,bool * fragstolen)4872783c175fSEric Dumazet static bool tcp_try_coalesce(struct sock *sk,
48731402d366SEric Dumazet struct sk_buff *to,
4874329033f6SEric Dumazet struct sk_buff *from,
4875329033f6SEric Dumazet bool *fragstolen)
48761402d366SEric Dumazet {
4877bad43ca8SEric Dumazet int delta;
48781402d366SEric Dumazet
4879329033f6SEric Dumazet *fragstolen = false;
488034a802a5SAlexander Duyck
48811ca7ee30SEric Dumazet /* Its possible this segment overlaps with prior segment in queue */
48821ca7ee30SEric Dumazet if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
48831ca7ee30SEric Dumazet return false;
48841ca7ee30SEric Dumazet
488507111530SJakub Kicinski if (!tcp_skb_can_collapse_rx(to, from))
488641ed9c04SBoris Pismenny return false;
488741ed9c04SBoris Pismenny
4888bad43ca8SEric Dumazet if (!skb_try_coalesce(to, from, fragstolen, &delta))
4889783c175fSEric Dumazet return false;
489034a802a5SAlexander Duyck
48911402d366SEric Dumazet atomic_add(delta, &sk->sk_rmem_alloc);
48921402d366SEric Dumazet sk_mem_charge(sk, delta);
4893c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE);
489434a802a5SAlexander Duyck TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
489534a802a5SAlexander Duyck TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
4896e93a0435SEric Dumazet TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags;
489798aaa913SMike Maloney
489898aaa913SMike Maloney if (TCP_SKB_CB(from)->has_rxtstamp) {
489998aaa913SMike Maloney TCP_SKB_CB(to)->has_rxtstamp = true;
490098aaa913SMike Maloney to->tstamp = from->tstamp;
4901cadf9df2SStephen Mallon skb_hwtstamps(to)->hwtstamp = skb_hwtstamps(from)->hwtstamp;
490298aaa913SMike Maloney }
490398aaa913SMike Maloney
490434a802a5SAlexander Duyck return true;
49051402d366SEric Dumazet }
49061402d366SEric Dumazet
tcp_ooo_try_coalesce(struct sock * sk,struct sk_buff * to,struct sk_buff * from,bool * fragstolen)490758152ecbSEric Dumazet static bool tcp_ooo_try_coalesce(struct sock *sk,
490858152ecbSEric Dumazet struct sk_buff *to,
490958152ecbSEric Dumazet struct sk_buff *from,
491058152ecbSEric Dumazet bool *fragstolen)
491158152ecbSEric Dumazet {
491258152ecbSEric Dumazet bool res = tcp_try_coalesce(sk, to, from, fragstolen);
491358152ecbSEric Dumazet
49148fbf1957SEric Dumazet /* In case tcp_drop_reason() is called later, update to->gso_segs */
491558152ecbSEric Dumazet if (res) {
491658152ecbSEric Dumazet u32 gso_segs = max_t(u16, 1, skb_shinfo(to)->gso_segs) +
491758152ecbSEric Dumazet max_t(u16, 1, skb_shinfo(from)->gso_segs);
491858152ecbSEric Dumazet
491958152ecbSEric Dumazet skb_shinfo(to)->gso_segs = min_t(u32, gso_segs, 0xFFFF);
492058152ecbSEric Dumazet }
492158152ecbSEric Dumazet return res;
492258152ecbSEric Dumazet }
492358152ecbSEric Dumazet
tcp_drop_reason(struct sock * sk,struct sk_buff * skb,enum skb_drop_reason reason)4924082116ffSMenglong Dong static void tcp_drop_reason(struct sock *sk, struct sk_buff *skb,
4925082116ffSMenglong Dong enum skb_drop_reason reason)
4926532182cdSEric Dumazet {
4927532182cdSEric Dumazet sk_drops_add(sk, skb);
492846a02aa3SYan Zhai sk_skb_reason_drop(sk, skb, reason);
4929082116ffSMenglong Dong }
4930082116ffSMenglong Dong
49311da177e4SLinus Torvalds /* This one checks to see if we can put data from the
49321da177e4SLinus Torvalds * out_of_order queue into the receive_queue.
49331da177e4SLinus Torvalds */
tcp_ofo_queue(struct sock * sk)49341da177e4SLinus Torvalds static void tcp_ofo_queue(struct sock *sk)
49351da177e4SLinus Torvalds {
49361da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
49371da177e4SLinus Torvalds __u32 dsack_high = tp->rcv_nxt;
49389f5afeaeSYaogong Wang bool fin, fragstolen, eaten;
4939bd1e75abSEric Dumazet struct sk_buff *skb, *tail;
49409f5afeaeSYaogong Wang struct rb_node *p;
49411da177e4SLinus Torvalds
49429f5afeaeSYaogong Wang p = rb_first(&tp->out_of_order_queue);
49439f5afeaeSYaogong Wang while (p) {
494418a4c0eaSEric Dumazet skb = rb_to_skb(p);
49451da177e4SLinus Torvalds if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
49461da177e4SLinus Torvalds break;
49471da177e4SLinus Torvalds
49481da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->seq, dsack_high)) {
49491da177e4SLinus Torvalds __u32 dsack = dsack_high;
49501da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->end_seq, dsack_high))
49511da177e4SLinus Torvalds dsack_high = TCP_SKB_CB(skb)->end_seq;
49521da177e4SLinus Torvalds tcp_dsack_extend(sk, TCP_SKB_CB(skb)->seq, dsack);
49531da177e4SLinus Torvalds }
49549f5afeaeSYaogong Wang p = rb_next(p);
49559f5afeaeSYaogong Wang rb_erase(&skb->rbnode, &tp->out_of_order_queue);
49561da177e4SLinus Torvalds
49579f5afeaeSYaogong Wang if (unlikely(!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))) {
49588fbf1957SEric Dumazet tcp_drop_reason(sk, skb, SKB_DROP_REASON_TCP_OFO_DROP);
49591da177e4SLinus Torvalds continue;
49601da177e4SLinus Torvalds }
49611da177e4SLinus Torvalds
4962bd1e75abSEric Dumazet tail = skb_peek_tail(&sk->sk_receive_queue);
4963bffa72cfSEric Dumazet eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen);
4964bdd1f9edSEric Dumazet tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
49659f5afeaeSYaogong Wang fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
4966bd1e75abSEric Dumazet if (!eaten)
4967bd1e75abSEric Dumazet __skb_queue_tail(&sk->sk_receive_queue, skb);
49689f5afeaeSYaogong Wang else
4969bd1e75abSEric Dumazet kfree_skb_partial(skb, fragstolen);
49709f5afeaeSYaogong Wang
49719f5afeaeSYaogong Wang if (unlikely(fin)) {
49729f5afeaeSYaogong Wang tcp_fin(sk);
49739f5afeaeSYaogong Wang /* tcp_fin() purges tp->out_of_order_queue,
49749f5afeaeSYaogong Wang * so we must end this loop right now.
49759f5afeaeSYaogong Wang */
49769f5afeaeSYaogong Wang break;
49779f5afeaeSYaogong Wang }
49781da177e4SLinus Torvalds }
49791da177e4SLinus Torvalds }
49801da177e4SLinus Torvalds
4981b0e01253SEric Dumazet static bool tcp_prune_ofo_queue(struct sock *sk, const struct sk_buff *in_skb);
4982b0e01253SEric Dumazet static int tcp_prune_queue(struct sock *sk, const struct sk_buff *in_skb);
49831da177e4SLinus Torvalds
tcp_try_rmem_schedule(struct sock * sk,struct sk_buff * skb,unsigned int size)49841da177e4SLinus Torvalds static int tcp_try_rmem_schedule(struct sock *sk, struct sk_buff *skb,
49851da177e4SLinus Torvalds unsigned int size)
49861da177e4SLinus Torvalds {
49871da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
49881da177e4SLinus Torvalds !sk_rmem_schedule(sk, skb, size)) {
49891da177e4SLinus Torvalds
4990b0e01253SEric Dumazet if (tcp_prune_queue(sk, skb) < 0)
49911da177e4SLinus Torvalds return -1;
49921ed83465SPavel Emelyanov
499336a6503fSEric Dumazet while (!sk_rmem_schedule(sk, skb, size)) {
4994b0e01253SEric Dumazet if (!tcp_prune_ofo_queue(sk, skb))
49951da177e4SLinus Torvalds return -1;
49961da177e4SLinus Torvalds }
49971da177e4SLinus Torvalds }
49981da177e4SLinus Torvalds return 0;
49991da177e4SLinus Torvalds }
50001da177e4SLinus Torvalds
tcp_data_queue_ofo(struct sock * sk,struct sk_buff * skb)5001e86b2919SEric Dumazet static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
5002e86b2919SEric Dumazet {
5003e86b2919SEric Dumazet struct tcp_sock *tp = tcp_sk(sk);
500418a4c0eaSEric Dumazet struct rb_node **p, *parent;
5005e86b2919SEric Dumazet struct sk_buff *skb1;
5006e86b2919SEric Dumazet u32 seq, end_seq;
50079f5afeaeSYaogong Wang bool fragstolen;
5008e86b2919SEric Dumazet
500995b9a87cSDavid Morley tcp_save_lrcv_flowlabel(sk, skb);
5010f4c9f85fSYousuk Seung tcp_ecn_check_ce(sk, skb);
5011e86b2919SEric Dumazet
5012c76562b6SMel Gorman if (unlikely(tcp_try_rmem_schedule(sk, skb, skb->truesize))) {
5013c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFODROP);
5014ba3bb0e7SEric Dumazet sk->sk_data_ready(sk);
5015d25e481bSMenglong Dong tcp_drop_reason(sk, skb, SKB_DROP_REASON_PROTO_MEM);
5016e86b2919SEric Dumazet return;
5017e86b2919SEric Dumazet }
5018e86b2919SEric Dumazet
501931770e34SFlorian Westphal /* Disable header prediction. */
502031770e34SFlorian Westphal tp->pred_flags = 0;
5021e86b2919SEric Dumazet inet_csk_schedule_ack(sk);
5022e86b2919SEric Dumazet
5023f9af2dbbSThomas Higdon tp->rcv_ooopack += max_t(u16, 1, skb_shinfo(skb)->gso_segs);
5024c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
50259f5afeaeSYaogong Wang seq = TCP_SKB_CB(skb)->seq;
50269f5afeaeSYaogong Wang end_seq = TCP_SKB_CB(skb)->end_seq;
5027e86b2919SEric Dumazet
50289f5afeaeSYaogong Wang p = &tp->out_of_order_queue.rb_node;
50299f5afeaeSYaogong Wang if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
5030e86b2919SEric Dumazet /* Initial out of order segment, build 1 SACK. */
5031e86b2919SEric Dumazet if (tcp_is_sack(tp)) {
5032e86b2919SEric Dumazet tp->rx_opt.num_sacks = 1;
50339f5afeaeSYaogong Wang tp->selective_acks[0].start_seq = seq;
50349f5afeaeSYaogong Wang tp->selective_acks[0].end_seq = end_seq;
5035e86b2919SEric Dumazet }
50369f5afeaeSYaogong Wang rb_link_node(&skb->rbnode, NULL, p);
50379f5afeaeSYaogong Wang rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
50389f5afeaeSYaogong Wang tp->ooo_last_skb = skb;
5039e86b2919SEric Dumazet goto end;
5040e86b2919SEric Dumazet }
5041e86b2919SEric Dumazet
50429f5afeaeSYaogong Wang /* In the typical case, we are adding an skb to the end of the list.
50439f5afeaeSYaogong Wang * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
50449f5afeaeSYaogong Wang */
504558152ecbSEric Dumazet if (tcp_ooo_try_coalesce(sk, tp->ooo_last_skb,
504698aaa913SMike Maloney skb, &fragstolen)) {
50479f5afeaeSYaogong Wang coalesce_done:
504866205121SEric Dumazet /* For non sack flows, do not grow window to force DUPACK
504966205121SEric Dumazet * and trigger fast retransmit.
505066205121SEric Dumazet */
505166205121SEric Dumazet if (tcp_is_sack(tp))
5052240bfd13SEric Dumazet tcp_grow_window(sk, skb, true);
5053923dd347SEric Dumazet kfree_skb_partial(skb, fragstolen);
5054c8628155SEric Dumazet skb = NULL;
5055e86b2919SEric Dumazet goto add_sack;
5056e86b2919SEric Dumazet }
50572594a2a9SEric Dumazet /* Can avoid an rbtree lookup if we are adding skb after ooo_last_skb */
50582594a2a9SEric Dumazet if (!before(seq, TCP_SKB_CB(tp->ooo_last_skb)->end_seq)) {
50592594a2a9SEric Dumazet parent = &tp->ooo_last_skb->rbnode;
50602594a2a9SEric Dumazet p = &parent->rb_right;
50612594a2a9SEric Dumazet goto insert;
5062e86b2919SEric Dumazet }
5063e86b2919SEric Dumazet
50649f5afeaeSYaogong Wang /* Find place to insert this segment. Handle overlaps on the way. */
50659f5afeaeSYaogong Wang parent = NULL;
50669f5afeaeSYaogong Wang while (*p) {
50679f5afeaeSYaogong Wang parent = *p;
506818a4c0eaSEric Dumazet skb1 = rb_to_skb(parent);
50699f5afeaeSYaogong Wang if (before(seq, TCP_SKB_CB(skb1)->seq)) {
50709f5afeaeSYaogong Wang p = &parent->rb_left;
50719f5afeaeSYaogong Wang continue;
5072e86b2919SEric Dumazet }
50739f5afeaeSYaogong Wang if (before(seq, TCP_SKB_CB(skb1)->end_seq)) {
5074e86b2919SEric Dumazet if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
5075e86b2919SEric Dumazet /* All the bits are present. Drop. */
50769f5afeaeSYaogong Wang NET_INC_STATS(sock_net(sk),
50779f5afeaeSYaogong Wang LINUX_MIB_TCPOFOMERGE);
5078d25e481bSMenglong Dong tcp_drop_reason(sk, skb,
5079d25e481bSMenglong Dong SKB_DROP_REASON_TCP_OFOMERGE);
5080e86b2919SEric Dumazet skb = NULL;
5081e86b2919SEric Dumazet tcp_dsack_set(sk, seq, end_seq);
5082e86b2919SEric Dumazet goto add_sack;
5083e86b2919SEric Dumazet }
5084e86b2919SEric Dumazet if (after(seq, TCP_SKB_CB(skb1)->seq)) {
5085e86b2919SEric Dumazet /* Partial overlap. */
50869f5afeaeSYaogong Wang tcp_dsack_set(sk, seq, TCP_SKB_CB(skb1)->end_seq);
5087e86b2919SEric Dumazet } else {
50889f5afeaeSYaogong Wang /* skb's seq == skb1's seq and skb covers skb1.
50899f5afeaeSYaogong Wang * Replace skb1 with skb.
50909f5afeaeSYaogong Wang */
50919f5afeaeSYaogong Wang rb_replace_node(&skb1->rbnode, &skb->rbnode,
50929f5afeaeSYaogong Wang &tp->out_of_order_queue);
50939f5afeaeSYaogong Wang tcp_dsack_extend(sk,
50949f5afeaeSYaogong Wang TCP_SKB_CB(skb1)->seq,
50959f5afeaeSYaogong Wang TCP_SKB_CB(skb1)->end_seq);
50969f5afeaeSYaogong Wang NET_INC_STATS(sock_net(sk),
50979f5afeaeSYaogong Wang LINUX_MIB_TCPOFOMERGE);
5098d25e481bSMenglong Dong tcp_drop_reason(sk, skb1,
5099d25e481bSMenglong Dong SKB_DROP_REASON_TCP_OFOMERGE);
510076f0dcbbSEric Dumazet goto merge_right;
5101e86b2919SEric Dumazet }
510258152ecbSEric Dumazet } else if (tcp_ooo_try_coalesce(sk, skb1,
510398aaa913SMike Maloney skb, &fragstolen)) {
51049f5afeaeSYaogong Wang goto coalesce_done;
5105e86b2919SEric Dumazet }
51069f5afeaeSYaogong Wang p = &parent->rb_right;
51079f5afeaeSYaogong Wang }
51082594a2a9SEric Dumazet insert:
51099f5afeaeSYaogong Wang /* Insert segment into RB tree. */
51109f5afeaeSYaogong Wang rb_link_node(&skb->rbnode, parent, p);
51119f5afeaeSYaogong Wang rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
5112e86b2919SEric Dumazet
511376f0dcbbSEric Dumazet merge_right:
51149f5afeaeSYaogong Wang /* Remove other segments covered by skb. */
511518a4c0eaSEric Dumazet while ((skb1 = skb_rb_next(skb)) != NULL) {
5116e86b2919SEric Dumazet if (!after(end_seq, TCP_SKB_CB(skb1)->seq))
5117e86b2919SEric Dumazet break;
5118e86b2919SEric Dumazet if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
5119e86b2919SEric Dumazet tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
5120e86b2919SEric Dumazet end_seq);
5121e86b2919SEric Dumazet break;
5122e86b2919SEric Dumazet }
51239f5afeaeSYaogong Wang rb_erase(&skb1->rbnode, &tp->out_of_order_queue);
5124e86b2919SEric Dumazet tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
5125e86b2919SEric Dumazet TCP_SKB_CB(skb1)->end_seq);
5126c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
5127d25e481bSMenglong Dong tcp_drop_reason(sk, skb1, SKB_DROP_REASON_TCP_OFOMERGE);
5128e86b2919SEric Dumazet }
51299f5afeaeSYaogong Wang /* If there is no skb after us, we are the last_skb ! */
513018a4c0eaSEric Dumazet if (!skb1)
51319f5afeaeSYaogong Wang tp->ooo_last_skb = skb;
5132e86b2919SEric Dumazet
5133e86b2919SEric Dumazet add_sack:
5134e86b2919SEric Dumazet if (tcp_is_sack(tp))
5135e86b2919SEric Dumazet tcp_sack_new_ofo_skb(sk, seq, end_seq);
5136e86b2919SEric Dumazet end:
51374e4f1fc2SEric Dumazet if (skb) {
513866205121SEric Dumazet /* For non sack flows, do not grow window to force DUPACK
513966205121SEric Dumazet * and trigger fast retransmit.
514066205121SEric Dumazet */
514166205121SEric Dumazet if (tcp_is_sack(tp))
5142240bfd13SEric Dumazet tcp_grow_window(sk, skb, false);
514360b1af33SEric Dumazet skb_condense(skb);
5144e86b2919SEric Dumazet skb_set_owner_r(skb, sk);
5145e86b2919SEric Dumazet }
51464e4f1fc2SEric Dumazet }
5147e86b2919SEric Dumazet
tcp_queue_rcv(struct sock * sk,struct sk_buff * skb,bool * fragstolen)5148e7395f1fSEric Dumazet static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb,
5149b081f85cSEric Dumazet bool *fragstolen)
5150b081f85cSEric Dumazet {
5151b081f85cSEric Dumazet int eaten;
5152b081f85cSEric Dumazet struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue);
5153b081f85cSEric Dumazet
5154b081f85cSEric Dumazet eaten = (tail &&
5155bffa72cfSEric Dumazet tcp_try_coalesce(sk, tail,
515698aaa913SMike Maloney skb, fragstolen)) ? 1 : 0;
5157bdd1f9edSEric Dumazet tcp_rcv_nxt_update(tcp_sk(sk), TCP_SKB_CB(skb)->end_seq);
5158b081f85cSEric Dumazet if (!eaten) {
5159b081f85cSEric Dumazet __skb_queue_tail(&sk->sk_receive_queue, skb);
5160b081f85cSEric Dumazet skb_set_owner_r(skb, sk);
5161b081f85cSEric Dumazet }
5162b081f85cSEric Dumazet return eaten;
5163b081f85cSEric Dumazet }
5164e86b2919SEric Dumazet
tcp_send_rcvq(struct sock * sk,struct msghdr * msg,size_t size)5165292e8d8cSPavel Emelyanov int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
5166292e8d8cSPavel Emelyanov {
5167cb93471aSEric Dumazet struct sk_buff *skb;
51685d4c9bfbSEric Dumazet int err = -ENOMEM;
51695d4c9bfbSEric Dumazet int data_len = 0;
5170292e8d8cSPavel Emelyanov bool fragstolen;
5171292e8d8cSPavel Emelyanov
5172c454e611SPavel Emelyanov if (size == 0)
5173c454e611SPavel Emelyanov return 0;
5174c454e611SPavel Emelyanov
51755d4c9bfbSEric Dumazet if (size > PAGE_SIZE) {
51765d4c9bfbSEric Dumazet int npages = min_t(size_t, size >> PAGE_SHIFT, MAX_SKB_FRAGS);
51775d4c9bfbSEric Dumazet
51785d4c9bfbSEric Dumazet data_len = npages << PAGE_SHIFT;
51795d4c9bfbSEric Dumazet size = data_len + (size & ~PAGE_MASK);
51805d4c9bfbSEric Dumazet }
51815d4c9bfbSEric Dumazet skb = alloc_skb_with_frags(size - data_len, data_len,
51825d4c9bfbSEric Dumazet PAGE_ALLOC_COSTLY_ORDER,
51835d4c9bfbSEric Dumazet &err, sk->sk_allocation);
5184292e8d8cSPavel Emelyanov if (!skb)
5185292e8d8cSPavel Emelyanov goto err;
5186292e8d8cSPavel Emelyanov
51875d4c9bfbSEric Dumazet skb_put(skb, size - data_len);
51885d4c9bfbSEric Dumazet skb->data_len = data_len;
51895d4c9bfbSEric Dumazet skb->len = size;
51905d4c9bfbSEric Dumazet
5191ea5d0c32SYafang Shao if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
5192ea5d0c32SYafang Shao NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVQDROP);
5193c76562b6SMel Gorman goto err_free;
5194ea5d0c32SYafang Shao }
5195c76562b6SMel Gorman
51965d4c9bfbSEric Dumazet err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
51975d4c9bfbSEric Dumazet if (err)
5198292e8d8cSPavel Emelyanov goto err_free;
5199292e8d8cSPavel Emelyanov
5200292e8d8cSPavel Emelyanov TCP_SKB_CB(skb)->seq = tcp_sk(sk)->rcv_nxt;
5201292e8d8cSPavel Emelyanov TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size;
5202292e8d8cSPavel Emelyanov TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1;
5203292e8d8cSPavel Emelyanov
5204e7395f1fSEric Dumazet if (tcp_queue_rcv(sk, skb, &fragstolen)) {
5205292e8d8cSPavel Emelyanov WARN_ON_ONCE(fragstolen); /* should not happen */
5206292e8d8cSPavel Emelyanov __kfree_skb(skb);
5207292e8d8cSPavel Emelyanov }
5208292e8d8cSPavel Emelyanov return size;
5209292e8d8cSPavel Emelyanov
5210292e8d8cSPavel Emelyanov err_free:
5211292e8d8cSPavel Emelyanov kfree_skb(skb);
5212292e8d8cSPavel Emelyanov err:
52135d4c9bfbSEric Dumazet return err;
52145d4c9bfbSEric Dumazet
5215292e8d8cSPavel Emelyanov }
5216292e8d8cSPavel Emelyanov
tcp_data_ready(struct sock * sk)521703f45c88SEric Dumazet void tcp_data_ready(struct sock *sk)
521803f45c88SEric Dumazet {
521939354eb2SEric Dumazet if (tcp_epollin_ready(sk, sk->sk_rcvlowat) || sock_flag(sk, SOCK_DONE))
522003f45c88SEric Dumazet sk->sk_data_ready(sk);
522103f45c88SEric Dumazet }
522203f45c88SEric Dumazet
tcp_data_queue(struct sock * sk,struct sk_buff * skb)52231da177e4SLinus Torvalds static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
52241da177e4SLinus Torvalds {
52251da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
5226a7ec3810SMenglong Dong enum skb_drop_reason reason;
52275357f0bdSEric Dumazet bool fragstolen;
52285357f0bdSEric Dumazet int eaten;
52291da177e4SLinus Torvalds
52306787b7e3SJianguo Wu /* If a subflow has been reset, the packet should not continue
52316787b7e3SJianguo Wu * to be processed, drop the packet.
52326787b7e3SJianguo Wu */
52336787b7e3SJianguo Wu if (sk_is_mptcp(sk) && !mptcp_incoming_options(sk, skb)) {
52346787b7e3SJianguo Wu __kfree_skb(skb);
52356787b7e3SJianguo Wu return;
52366787b7e3SJianguo Wu }
5237648ef4b8SMat Martineau
5238532182cdSEric Dumazet if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
5239532182cdSEric Dumazet __kfree_skb(skb);
5240532182cdSEric Dumazet return;
5241532182cdSEric Dumazet }
5242f84af32cSEric Dumazet skb_dst_drop(skb);
5243155c6e1aSPeter Pan(潘卫平) __skb_pull(skb, tcp_hdr(skb)->doff * 4);
52441da177e4SLinus Torvalds
5245a7ec3810SMenglong Dong reason = SKB_DROP_REASON_NOT_SPECIFIED;
52461da177e4SLinus Torvalds tp->rx_opt.dsack = 0;
52471da177e4SLinus Torvalds
52481da177e4SLinus Torvalds /* Queue data for delivery to the user.
52491da177e4SLinus Torvalds * Packets in sequence go to the receive queue.
52501da177e4SLinus Torvalds * Out of sequence packets to the out_of_order_queue.
52511da177e4SLinus Torvalds */
52521da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
5253fb223502SYafang Shao if (tcp_receive_window(tp) == 0) {
52542bd99aefSEric Dumazet /* Some stacks are known to send bare FIN packets
52552bd99aefSEric Dumazet * in a loop even if we send RWIN 0 in our ACK.
52562bd99aefSEric Dumazet * Accepting this FIN does not hurt memory pressure
52572bd99aefSEric Dumazet * because the FIN flag will simply be merged to the
52582bd99aefSEric Dumazet * receive queue tail skb in most cases.
52592bd99aefSEric Dumazet */
52602bd99aefSEric Dumazet if (!skb->len &&
52612bd99aefSEric Dumazet (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
52622bd99aefSEric Dumazet goto queue_and_out;
52632bd99aefSEric Dumazet
5264a7ec3810SMenglong Dong reason = SKB_DROP_REASON_TCP_ZEROWINDOW;
5265fb223502SYafang Shao NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPZEROWINDOWDROP);
52661da177e4SLinus Torvalds goto out_of_window;
5267fb223502SYafang Shao }
52681da177e4SLinus Torvalds
52691da177e4SLinus Torvalds /* Ok. In sequence. In window. */
52701da177e4SLinus Torvalds queue_and_out:
5271e2142825SMenglong Dong if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
5272e2142825SMenglong Dong /* TODO: maybe ratelimit these WIN 0 ACK ? */
5273e2142825SMenglong Dong inet_csk(sk)->icsk_ack.pending |=
5274e2142825SMenglong Dong (ICSK_ACK_NOMEM | ICSK_ACK_NOW);
5275e2142825SMenglong Dong inet_csk_schedule_ack(sk);
5276e2142825SMenglong Dong sk->sk_data_ready(sk);
5277e2142825SMenglong Dong
52782bd99aefSEric Dumazet if (skb_queue_len(&sk->sk_receive_queue) && skb->len) {
5279a7ec3810SMenglong Dong reason = SKB_DROP_REASON_PROTO_MEM;
5280ea5d0c32SYafang Shao NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVQDROP);
52811da177e4SLinus Torvalds goto drop;
5282ea5d0c32SYafang Shao }
5283e2142825SMenglong Dong sk_forced_mem_schedule(sk, skb->truesize);
5284e2142825SMenglong Dong }
52855357f0bdSEric Dumazet
5286e7395f1fSEric Dumazet eaten = tcp_queue_rcv(sk, skb, &fragstolen);
52871da177e4SLinus Torvalds if (skb->len)
52889e412ba7SIlpo Järvinen tcp_event_data_recv(sk, skb);
5289155c6e1aSPeter Pan(潘卫平) if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
529020c4cb79SEric Dumazet tcp_fin(sk);
52911da177e4SLinus Torvalds
52929f5afeaeSYaogong Wang if (!RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
52931da177e4SLinus Torvalds tcp_ofo_queue(sk);
52941da177e4SLinus Torvalds
529515bdd568SYuchung Cheng /* RFC5681. 4.2. SHOULD send immediate ACK, when
52961da177e4SLinus Torvalds * gap in queue is filled.
52971da177e4SLinus Torvalds */
52989f5afeaeSYaogong Wang if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
529915bdd568SYuchung Cheng inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW;
53001da177e4SLinus Torvalds }
53011da177e4SLinus Torvalds
53021da177e4SLinus Torvalds if (tp->rx_opt.num_sacks)
53031da177e4SLinus Torvalds tcp_sack_remove(tp);
53041da177e4SLinus Torvalds
530531770e34SFlorian Westphal tcp_fast_path_check(sk);
530631770e34SFlorian Westphal
5307923dd347SEric Dumazet if (eaten > 0)
5308923dd347SEric Dumazet kfree_skb_partial(skb, fragstolen);
53091d57f195SEric Dumazet if (!sock_flag(sk, SOCK_DEAD))
531003f45c88SEric Dumazet tcp_data_ready(sk);
53111da177e4SLinus Torvalds return;
53121da177e4SLinus Torvalds }
53131da177e4SLinus Torvalds
53141da177e4SLinus Torvalds if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
53157788174eSYuchung Cheng tcp_rcv_spurious_retrans(sk, skb);
53161da177e4SLinus Torvalds /* A retransmit, 2nd most common case. Force an immediate ack. */
5317a7ec3810SMenglong Dong reason = SKB_DROP_REASON_TCP_OLD_DATA;
5318c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
53191ed83465SPavel Emelyanov tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
53201da177e4SLinus Torvalds
53211da177e4SLinus Torvalds out_of_window:
53229a9c9b51SEric Dumazet tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
5323463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk);
53241da177e4SLinus Torvalds drop:
5325a7ec3810SMenglong Dong tcp_drop_reason(sk, skb, reason);
53261da177e4SLinus Torvalds return;
53271da177e4SLinus Torvalds }
53281da177e4SLinus Torvalds
53291da177e4SLinus Torvalds /* Out of window. F.e. zero window probe. */
5330a7ec3810SMenglong Dong if (!before(TCP_SKB_CB(skb)->seq,
5331a7ec3810SMenglong Dong tp->rcv_nxt + tcp_receive_window(tp))) {
5332a7ec3810SMenglong Dong reason = SKB_DROP_REASON_TCP_OVERWINDOW;
53331da177e4SLinus Torvalds goto out_of_window;
5334a7ec3810SMenglong Dong }
53351da177e4SLinus Torvalds
53361da177e4SLinus Torvalds if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
53371da177e4SLinus Torvalds /* Partial packet, seq < rcv_next < end_seq */
53381ed83465SPavel Emelyanov tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
53391da177e4SLinus Torvalds
53401da177e4SLinus Torvalds /* If window is closed, drop tail of packet. But after
53411da177e4SLinus Torvalds * remembering D-SACK for its head made in previous line.
53421da177e4SLinus Torvalds */
5343fb223502SYafang Shao if (!tcp_receive_window(tp)) {
5344a7ec3810SMenglong Dong reason = SKB_DROP_REASON_TCP_ZEROWINDOW;
5345fb223502SYafang Shao NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPZEROWINDOWDROP);
53461da177e4SLinus Torvalds goto out_of_window;
5347fb223502SYafang Shao }
53481da177e4SLinus Torvalds goto queue_and_out;
53491da177e4SLinus Torvalds }
53501da177e4SLinus Torvalds
5351e86b2919SEric Dumazet tcp_data_queue_ofo(sk, skb);
53521da177e4SLinus Torvalds }
53531da177e4SLinus Torvalds
tcp_skb_next(struct sk_buff * skb,struct sk_buff_head * list)53549f5afeaeSYaogong Wang static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *list)
53552cf46637SIlpo Järvinen {
53569f5afeaeSYaogong Wang if (list)
53579f5afeaeSYaogong Wang return !skb_queue_is_last(list, skb) ? skb->next : NULL;
535891521944SDavid S. Miller
535918a4c0eaSEric Dumazet return skb_rb_next(skb);
53609f5afeaeSYaogong Wang }
53612cf46637SIlpo Järvinen
tcp_collapse_one(struct sock * sk,struct sk_buff * skb,struct sk_buff_head * list,struct rb_root * root)53629f5afeaeSYaogong Wang static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
53639f5afeaeSYaogong Wang struct sk_buff_head *list,
53649f5afeaeSYaogong Wang struct rb_root *root)
53659f5afeaeSYaogong Wang {
53669f5afeaeSYaogong Wang struct sk_buff *next = tcp_skb_next(skb, list);
53679f5afeaeSYaogong Wang
53689f5afeaeSYaogong Wang if (list)
53692cf46637SIlpo Järvinen __skb_unlink(skb, list);
53709f5afeaeSYaogong Wang else
53719f5afeaeSYaogong Wang rb_erase(&skb->rbnode, root);
53729f5afeaeSYaogong Wang
53732cf46637SIlpo Järvinen __kfree_skb(skb);
5374c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOLLAPSED);
53752cf46637SIlpo Järvinen
53762cf46637SIlpo Järvinen return next;
53772cf46637SIlpo Järvinen }
53782cf46637SIlpo Järvinen
53799f5afeaeSYaogong Wang /* Insert skb into rb tree, ordered by TCP_SKB_CB(skb)->seq */
tcp_rbtree_insert(struct rb_root * root,struct sk_buff * skb)538075c119afSEric Dumazet void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb)
53819f5afeaeSYaogong Wang {
53829f5afeaeSYaogong Wang struct rb_node **p = &root->rb_node;
53839f5afeaeSYaogong Wang struct rb_node *parent = NULL;
53849f5afeaeSYaogong Wang struct sk_buff *skb1;
53859f5afeaeSYaogong Wang
53869f5afeaeSYaogong Wang while (*p) {
53879f5afeaeSYaogong Wang parent = *p;
538818a4c0eaSEric Dumazet skb1 = rb_to_skb(parent);
53899f5afeaeSYaogong Wang if (before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb1)->seq))
53909f5afeaeSYaogong Wang p = &parent->rb_left;
53919f5afeaeSYaogong Wang else
53929f5afeaeSYaogong Wang p = &parent->rb_right;
53939f5afeaeSYaogong Wang }
53949f5afeaeSYaogong Wang rb_link_node(&skb->rbnode, parent, p);
53959f5afeaeSYaogong Wang rb_insert_color(&skb->rbnode, root);
53969f5afeaeSYaogong Wang }
53979f5afeaeSYaogong Wang
53981da177e4SLinus Torvalds /* Collapse contiguous sequence of skbs head..tail with
53991da177e4SLinus Torvalds * sequence numbers start..end.
540091521944SDavid S. Miller *
54019f5afeaeSYaogong Wang * If tail is NULL, this means until the end of the queue.
540291521944SDavid S. Miller *
54031da177e4SLinus Torvalds * Segments with FIN/SYN are not collapsed (only because this
54041da177e4SLinus Torvalds * simplifies code)
54051da177e4SLinus Torvalds */
54061da177e4SLinus Torvalds static void
tcp_collapse(struct sock * sk,struct sk_buff_head * list,struct rb_root * root,struct sk_buff * head,struct sk_buff * tail,u32 start,u32 end)54079f5afeaeSYaogong Wang tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
54089f5afeaeSYaogong Wang struct sk_buff *head, struct sk_buff *tail, u32 start, u32 end)
54091da177e4SLinus Torvalds {
54109f5afeaeSYaogong Wang struct sk_buff *skb = head, *n;
54119f5afeaeSYaogong Wang struct sk_buff_head tmp;
541291521944SDavid S. Miller bool end_of_skbs;
54131da177e4SLinus Torvalds
5414caa20d9aSStephen Hemminger /* First, check that queue is collapsible and find
54159f5afeaeSYaogong Wang * the point where collapsing can be useful.
54169f5afeaeSYaogong Wang */
541791521944SDavid S. Miller restart:
54189f5afeaeSYaogong Wang for (end_of_skbs = true; skb != NULL && skb != tail; skb = n) {
54199f5afeaeSYaogong Wang n = tcp_skb_next(skb, list);
54209f5afeaeSYaogong Wang
542165249febSMina Almasry if (!skb_frags_readable(skb))
542265249febSMina Almasry goto skip_this;
542365249febSMina Almasry
54241da177e4SLinus Torvalds /* No new bits? It is possible on ofo queue. */
54251da177e4SLinus Torvalds if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
54269f5afeaeSYaogong Wang skb = tcp_collapse_one(sk, skb, list, root);
542791521944SDavid S. Miller if (!skb)
542891521944SDavid S. Miller break;
542991521944SDavid S. Miller goto restart;
54301da177e4SLinus Torvalds }
54311da177e4SLinus Torvalds
54321da177e4SLinus Torvalds /* The first skb to collapse is:
54331da177e4SLinus Torvalds * - not SYN/FIN and
54341da177e4SLinus Torvalds * - bloated or contains data before "start" or
543585712484SMat Martineau * overlaps to the next one and mptcp allow collapsing.
54361da177e4SLinus Torvalds */
5437e11ecddfSEric Dumazet if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) &&
543894f0893eSEric Dumazet (tcp_win_from_space(sk, skb->truesize) > skb->len ||
543991521944SDavid S. Miller before(TCP_SKB_CB(skb)->seq, start))) {
544091521944SDavid S. Miller end_of_skbs = false;
54411da177e4SLinus Torvalds break;
544291521944SDavid S. Miller }
544391521944SDavid S. Miller
544465249febSMina Almasry if (n && n != tail && skb_frags_readable(n) &&
544565249febSMina Almasry tcp_skb_can_collapse_rx(skb, n) &&
54469f5afeaeSYaogong Wang TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(n)->seq) {
544791521944SDavid S. Miller end_of_skbs = false;
544891521944SDavid S. Miller break;
544991521944SDavid S. Miller }
54501da177e4SLinus Torvalds
545165249febSMina Almasry skip_this:
54521da177e4SLinus Torvalds /* Decided to skip this, advance start seq. */
54531da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->end_seq;
54541da177e4SLinus Torvalds }
5455e11ecddfSEric Dumazet if (end_of_skbs ||
545665249febSMina Almasry (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) ||
545765249febSMina Almasry !skb_frags_readable(skb))
54581da177e4SLinus Torvalds return;
54591da177e4SLinus Torvalds
54609f5afeaeSYaogong Wang __skb_queue_head_init(&tmp);
54619f5afeaeSYaogong Wang
54621da177e4SLinus Torvalds while (before(start, end)) {
5463b3d6cb92SEric Dumazet int copy = min_t(int, SKB_MAX_ORDER(0, 0), end - start);
54641da177e4SLinus Torvalds struct sk_buff *nskb;
54651da177e4SLinus Torvalds
5466b3d6cb92SEric Dumazet nskb = alloc_skb(copy, GFP_ATOMIC);
54671da177e4SLinus Torvalds if (!nskb)
54689f5afeaeSYaogong Wang break;
5469c51957daSArnaldo Carvalho de Melo
54701da177e4SLinus Torvalds memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
54719f06f87fSJakub Kicinski skb_copy_decrypted(nskb, skb);
54721da177e4SLinus Torvalds TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
54739f5afeaeSYaogong Wang if (list)
547443f59c89SDavid S. Miller __skb_queue_before(list, skb, nskb);
54759f5afeaeSYaogong Wang else
54769f5afeaeSYaogong Wang __skb_queue_tail(&tmp, nskb); /* defer rbtree insertion */
54773ab224beSHideo Aoki skb_set_owner_r(nskb, sk);
547885712484SMat Martineau mptcp_skb_ext_move(nskb, skb);
54791da177e4SLinus Torvalds
54801da177e4SLinus Torvalds /* Copy data, releasing collapsed skbs. */
54811da177e4SLinus Torvalds while (copy > 0) {
54821da177e4SLinus Torvalds int offset = start - TCP_SKB_CB(skb)->seq;
54831da177e4SLinus Torvalds int size = TCP_SKB_CB(skb)->end_seq - start;
54841da177e4SLinus Torvalds
548509a62660SKris Katterjohn BUG_ON(offset < 0);
54861da177e4SLinus Torvalds if (size > 0) {
54871da177e4SLinus Torvalds size = min(copy, size);
54881da177e4SLinus Torvalds if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
54891da177e4SLinus Torvalds BUG();
54901da177e4SLinus Torvalds TCP_SKB_CB(nskb)->end_seq += size;
54911da177e4SLinus Torvalds copy -= size;
54921da177e4SLinus Torvalds start += size;
54931da177e4SLinus Torvalds }
54941da177e4SLinus Torvalds if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
54959f5afeaeSYaogong Wang skb = tcp_collapse_one(sk, skb, list, root);
549691521944SDavid S. Miller if (!skb ||
549791521944SDavid S. Miller skb == tail ||
549807111530SJakub Kicinski !tcp_skb_can_collapse_rx(nskb, skb) ||
549965249febSMina Almasry (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) ||
550065249febSMina Almasry !skb_frags_readable(skb))
55019f5afeaeSYaogong Wang goto end;
55021da177e4SLinus Torvalds }
55031da177e4SLinus Torvalds }
55041da177e4SLinus Torvalds }
55059f5afeaeSYaogong Wang end:
55069f5afeaeSYaogong Wang skb_queue_walk_safe(&tmp, skb, n)
55079f5afeaeSYaogong Wang tcp_rbtree_insert(root, skb);
55081da177e4SLinus Torvalds }
55091da177e4SLinus Torvalds
55101da177e4SLinus Torvalds /* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
55111da177e4SLinus Torvalds * and tcp_collapse() them until all the queue is collapsed.
55121da177e4SLinus Torvalds */
tcp_collapse_ofo_queue(struct sock * sk)55131da177e4SLinus Torvalds static void tcp_collapse_ofo_queue(struct sock *sk)
55141da177e4SLinus Torvalds {
55151da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
55163d4bf93aSEric Dumazet u32 range_truesize, sum_tiny = 0;
55179f5afeaeSYaogong Wang struct sk_buff *skb, *head;
55181da177e4SLinus Torvalds u32 start, end;
55191da177e4SLinus Torvalds
552018a4c0eaSEric Dumazet skb = skb_rb_first(&tp->out_of_order_queue);
55219f5afeaeSYaogong Wang new_range:
55229f5afeaeSYaogong Wang if (!skb) {
552318a4c0eaSEric Dumazet tp->ooo_last_skb = skb_rb_last(&tp->out_of_order_queue);
55241da177e4SLinus Torvalds return;
55259f5afeaeSYaogong Wang }
55261da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->seq;
55271da177e4SLinus Torvalds end = TCP_SKB_CB(skb)->end_seq;
55283d4bf93aSEric Dumazet range_truesize = skb->truesize;
55291da177e4SLinus Torvalds
55309f5afeaeSYaogong Wang for (head = skb;;) {
553118a4c0eaSEric Dumazet skb = skb_rb_next(skb);
553291521944SDavid S. Miller
55339f5afeaeSYaogong Wang /* Range is terminated when we see a gap or when
55349f5afeaeSYaogong Wang * we are at the queue end.
55359f5afeaeSYaogong Wang */
553691521944SDavid S. Miller if (!skb ||
55371da177e4SLinus Torvalds after(TCP_SKB_CB(skb)->seq, end) ||
55381da177e4SLinus Torvalds before(TCP_SKB_CB(skb)->end_seq, start)) {
55393d4bf93aSEric Dumazet /* Do not attempt collapsing tiny skbs */
55403d4bf93aSEric Dumazet if (range_truesize != head->truesize ||
5541100fdd1fSEric Dumazet end - start >= SKB_WITH_OVERHEAD(PAGE_SIZE)) {
55429f5afeaeSYaogong Wang tcp_collapse(sk, NULL, &tp->out_of_order_queue,
55438728b834SDavid S. Miller head, skb, start, end);
55443d4bf93aSEric Dumazet } else {
55453d4bf93aSEric Dumazet sum_tiny += range_truesize;
55463d4bf93aSEric Dumazet if (sum_tiny > sk->sk_rcvbuf >> 3)
55473d4bf93aSEric Dumazet return;
55483d4bf93aSEric Dumazet }
55499f5afeaeSYaogong Wang goto new_range;
55509f5afeaeSYaogong Wang }
55519f5afeaeSYaogong Wang
55523d4bf93aSEric Dumazet range_truesize += skb->truesize;
55539f5afeaeSYaogong Wang if (unlikely(before(TCP_SKB_CB(skb)->seq, start)))
55541da177e4SLinus Torvalds start = TCP_SKB_CB(skb)->seq;
55551da177e4SLinus Torvalds if (after(TCP_SKB_CB(skb)->end_seq, end))
55561da177e4SLinus Torvalds end = TCP_SKB_CB(skb)->end_seq;
55571da177e4SLinus Torvalds }
55581da177e4SLinus Torvalds }
55591da177e4SLinus Torvalds
5560b000cd37SVitaliy Gusev /*
556136a6503fSEric Dumazet * Clean the out-of-order queue to make room.
556236a6503fSEric Dumazet * We drop high sequences packets to :
556336a6503fSEric Dumazet * 1) Let a chance for holes to be filled.
5564b0e01253SEric Dumazet * This means we do not drop packets from ooo queue if their sequence
5565b0e01253SEric Dumazet * is before incoming packet sequence.
556636a6503fSEric Dumazet * 2) not add too big latencies if thousands of packets sit there.
556736a6503fSEric Dumazet * (But if application shrinks SO_RCVBUF, we could still end up
556836a6503fSEric Dumazet * freeing whole queue here)
556972cd43baSEric Dumazet * 3) Drop at least 12.5 % of sk_rcvbuf to avoid malicious attacks.
557036a6503fSEric Dumazet *
557136a6503fSEric Dumazet * Return true if queue has shrunk.
5572b000cd37SVitaliy Gusev */
tcp_prune_ofo_queue(struct sock * sk,const struct sk_buff * in_skb)5573b0e01253SEric Dumazet static bool tcp_prune_ofo_queue(struct sock *sk, const struct sk_buff *in_skb)
5574b000cd37SVitaliy Gusev {
5575b000cd37SVitaliy Gusev struct tcp_sock *tp = tcp_sk(sk);
55769f5afeaeSYaogong Wang struct rb_node *node, *prev;
5577b0e01253SEric Dumazet bool pruned = false;
557872cd43baSEric Dumazet int goal;
5579b000cd37SVitaliy Gusev
55809f5afeaeSYaogong Wang if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
558136a6503fSEric Dumazet return false;
558236a6503fSEric Dumazet
558372cd43baSEric Dumazet goal = sk->sk_rcvbuf >> 3;
55849f5afeaeSYaogong Wang node = &tp->ooo_last_skb->rbnode;
5585b0e01253SEric Dumazet
55869f5afeaeSYaogong Wang do {
5587b0e01253SEric Dumazet struct sk_buff *skb = rb_to_skb(node);
5588b0e01253SEric Dumazet
5589b0e01253SEric Dumazet /* If incoming skb would land last in ofo queue, stop pruning. */
5590b0e01253SEric Dumazet if (after(TCP_SKB_CB(in_skb)->seq, TCP_SKB_CB(skb)->seq))
5591b0e01253SEric Dumazet break;
5592b0e01253SEric Dumazet pruned = true;
55939f5afeaeSYaogong Wang prev = rb_prev(node);
55949f5afeaeSYaogong Wang rb_erase(node, &tp->out_of_order_queue);
5595b0e01253SEric Dumazet goal -= skb->truesize;
5596b0e01253SEric Dumazet tcp_drop_reason(sk, skb, SKB_DROP_REASON_TCP_OFO_QUEUE_PRUNE);
5597b0e01253SEric Dumazet tp->ooo_last_skb = rb_to_skb(prev);
559872cd43baSEric Dumazet if (!prev || goal <= 0) {
559936a6503fSEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
560036a6503fSEric Dumazet !tcp_under_memory_pressure(sk))
560136a6503fSEric Dumazet break;
560272cd43baSEric Dumazet goal = sk->sk_rcvbuf >> 3;
560372cd43baSEric Dumazet }
56049f5afeaeSYaogong Wang node = prev;
56059f5afeaeSYaogong Wang } while (node);
5606b000cd37SVitaliy Gusev
5607b0e01253SEric Dumazet if (pruned) {
5608b0e01253SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_OFOPRUNED);
5609b000cd37SVitaliy Gusev /* Reset SACK state. A conforming SACK implementation will
5610b000cd37SVitaliy Gusev * do the same at a timeout based retransmit. When a connection
5611b000cd37SVitaliy Gusev * is in a sad state like this, we care only about integrity
5612b000cd37SVitaliy Gusev * of the connection not performance.
5613b000cd37SVitaliy Gusev */
5614b000cd37SVitaliy Gusev if (tp->rx_opt.sack_ok)
5615b000cd37SVitaliy Gusev tcp_sack_reset(&tp->rx_opt);
5616b0e01253SEric Dumazet }
5617b0e01253SEric Dumazet return pruned;
5618b000cd37SVitaliy Gusev }
5619b000cd37SVitaliy Gusev
56201da177e4SLinus Torvalds /* Reduce allocated memory if we can, trying to get
56211da177e4SLinus Torvalds * the socket within its memory limits again.
56221da177e4SLinus Torvalds *
56231da177e4SLinus Torvalds * Return less than zero if we should start dropping frames
56241da177e4SLinus Torvalds * until the socket owning process reads some of the data
56251da177e4SLinus Torvalds * to stabilize the situation.
56261da177e4SLinus Torvalds */
tcp_prune_queue(struct sock * sk,const struct sk_buff * in_skb)5627b0e01253SEric Dumazet static int tcp_prune_queue(struct sock *sk, const struct sk_buff *in_skb)
56281da177e4SLinus Torvalds {
56291da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
56301da177e4SLinus Torvalds
5631c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_PRUNECALLED);
56321da177e4SLinus Torvalds
56331da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
56349e412ba7SIlpo Järvinen tcp_clamp_window(sk);
5635b8da51ebSEric Dumazet else if (tcp_under_memory_pressure(sk))
5636053f3684SWei Wang tcp_adjust_rcv_ssthresh(sk);
56371da177e4SLinus Torvalds
5638f4a3313dSEric Dumazet if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
5639f4a3313dSEric Dumazet return 0;
5640f4a3313dSEric Dumazet
56411da177e4SLinus Torvalds tcp_collapse_ofo_queue(sk);
564291521944SDavid S. Miller if (!skb_queue_empty(&sk->sk_receive_queue))
56439f5afeaeSYaogong Wang tcp_collapse(sk, &sk->sk_receive_queue, NULL,
564491521944SDavid S. Miller skb_peek(&sk->sk_receive_queue),
564591521944SDavid S. Miller NULL,
56461da177e4SLinus Torvalds tp->copied_seq, tp->rcv_nxt);
56471da177e4SLinus Torvalds
56481da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
56491da177e4SLinus Torvalds return 0;
56501da177e4SLinus Torvalds
56511da177e4SLinus Torvalds /* Collapsing did not help, destructive actions follow.
56521da177e4SLinus Torvalds * This must not ever occur. */
56531da177e4SLinus Torvalds
5654b0e01253SEric Dumazet tcp_prune_ofo_queue(sk, in_skb);
56551da177e4SLinus Torvalds
56561da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
56571da177e4SLinus Torvalds return 0;
56581da177e4SLinus Torvalds
56591da177e4SLinus Torvalds /* If we are really being abused, tell the caller to silently
56601da177e4SLinus Torvalds * drop receive data on the floor. It will get retransmitted
56611da177e4SLinus Torvalds * and hopefully then we'll have sufficient space.
56621da177e4SLinus Torvalds */
5663c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_RCVPRUNED);
56641da177e4SLinus Torvalds
56651da177e4SLinus Torvalds /* Massive buffer overcommit. */
566631770e34SFlorian Westphal tp->pred_flags = 0;
56671da177e4SLinus Torvalds return -1;
56681da177e4SLinus Torvalds }
56691da177e4SLinus Torvalds
tcp_should_expand_sndbuf(struct sock * sk)5670ca057051SWei Wang static bool tcp_should_expand_sndbuf(struct sock *sk)
56710d9901dfSDavid S. Miller {
5672cf533ea5SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk);
56739e412ba7SIlpo Järvinen
56740d9901dfSDavid S. Miller /* If the user specified a specific send buffer setting, do
56750d9901dfSDavid S. Miller * not modify it.
56760d9901dfSDavid S. Miller */
56770d9901dfSDavid S. Miller if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
5678a2a385d6SEric Dumazet return false;
56790d9901dfSDavid S. Miller
56800d9901dfSDavid S. Miller /* If we are under global TCP memory pressure, do not expand. */
5681ca057051SWei Wang if (tcp_under_memory_pressure(sk)) {
5682ca057051SWei Wang int unused_mem = sk_unused_reserved_mem(sk);
5683ca057051SWei Wang
5684ca057051SWei Wang /* Adjust sndbuf according to reserved mem. But make sure
5685ca057051SWei Wang * it never goes below SOCK_MIN_SNDBUF.
5686ca057051SWei Wang * See sk_stream_moderate_sndbuf() for more details.
5687ca057051SWei Wang */
5688ca057051SWei Wang if (unused_mem > SOCK_MIN_SNDBUF)
5689ca057051SWei Wang WRITE_ONCE(sk->sk_sndbuf, unused_mem);
5690ca057051SWei Wang
5691a2a385d6SEric Dumazet return false;
5692ca057051SWei Wang }
56930d9901dfSDavid S. Miller
56940d9901dfSDavid S. Miller /* If we are under soft global TCP memory pressure, do not expand. */
5695180d8cd9SGlauber Costa if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0))
5696a2a385d6SEric Dumazet return false;
56970d9901dfSDavid S. Miller
56980d9901dfSDavid S. Miller /* If we filled the congestion window, do not expand. */
569940570375SEric Dumazet if (tcp_packets_in_flight(tp) >= tcp_snd_cwnd(tp))
5700a2a385d6SEric Dumazet return false;
57010d9901dfSDavid S. Miller
5702a2a385d6SEric Dumazet return true;
57030d9901dfSDavid S. Miller }
57041da177e4SLinus Torvalds
tcp_new_space(struct sock * sk)57051da177e4SLinus Torvalds static void tcp_new_space(struct sock *sk)
57061da177e4SLinus Torvalds {
57071da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
57081da177e4SLinus Torvalds
57099e412ba7SIlpo Järvinen if (tcp_should_expand_sndbuf(sk)) {
57106ae70532SEric Dumazet tcp_sndbuf_expand(sk);
5711c2203cf7SEric Dumazet tp->snd_cwnd_stamp = tcp_jiffies32;
57121da177e4SLinus Torvalds }
57131da177e4SLinus Torvalds
5714739b2adfSEric Dumazet INDIRECT_CALL_1(sk->sk_write_space, sk_stream_write_space, sk);
57151da177e4SLinus Torvalds }
57161da177e4SLinus Torvalds
57174bfe744fSEric Dumazet /* Caller made space either from:
57184bfe744fSEric Dumazet * 1) Freeing skbs in rtx queues (after tp->snd_una has advanced)
57194bfe744fSEric Dumazet * 2) Sent skbs from output queue (and thus advancing tp->snd_nxt)
57204bfe744fSEric Dumazet *
57214bfe744fSEric Dumazet * We might be able to generate EPOLLOUT to the application if:
57224bfe744fSEric Dumazet * 1) Space consumed in output/rtx queues is below sk->sk_sndbuf/2
57234bfe744fSEric Dumazet * 2) notsent amount (tp->write_seq - tp->snd_nxt) became
57244bfe744fSEric Dumazet * small enough that tcp_stream_memory_free() decides it
57254bfe744fSEric Dumazet * is time to generate EPOLLOUT.
57264bfe744fSEric Dumazet */
tcp_check_space(struct sock * sk)57274bfe744fSEric Dumazet void tcp_check_space(struct sock *sk)
57281da177e4SLinus Torvalds {
57293c715127Sjbaron@akamai.com /* pairs with tcp_poll() */
573056d80622SJason Baron smp_mb();
57311da177e4SLinus Torvalds if (sk->sk_socket &&
5732b0f71bd3SFrancis Yan test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
57331da177e4SLinus Torvalds tcp_new_space(sk);
5734b0f71bd3SFrancis Yan if (!test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
5735b0f71bd3SFrancis Yan tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
5736b0f71bd3SFrancis Yan }
57371da177e4SLinus Torvalds }
57381da177e4SLinus Torvalds
tcp_data_snd_check(struct sock * sk)57399e412ba7SIlpo Järvinen static inline void tcp_data_snd_check(struct sock *sk)
57401da177e4SLinus Torvalds {
57419e412ba7SIlpo Järvinen tcp_push_pending_frames(sk);
57421da177e4SLinus Torvalds tcp_check_space(sk);
57431da177e4SLinus Torvalds }
57441da177e4SLinus Torvalds
57451da177e4SLinus Torvalds /*
57461da177e4SLinus Torvalds * Check if sending an ack is needed.
57471da177e4SLinus Torvalds */
__tcp_ack_snd_check(struct sock * sk,int ofo_possible)57481da177e4SLinus Torvalds static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
57491da177e4SLinus Torvalds {
57501da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
57515d9f4262SEric Dumazet unsigned long rtt, delay;
57521da177e4SLinus Torvalds
57531da177e4SLinus Torvalds /* More than one full frame received... */
57549d4fb27dSJoe Perches if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
57551da177e4SLinus Torvalds /* ... and right edge of window advances far enough.
5756796f82eaSEric Dumazet * (tcp_recvmsg() will send ACK otherwise).
5757796f82eaSEric Dumazet * If application uses SO_RCVLOWAT, we want send ack now if
5758796f82eaSEric Dumazet * we have not received enough bytes to satisfy the condition.
57591da177e4SLinus Torvalds */
5760796f82eaSEric Dumazet (tp->rcv_nxt - tp->copied_seq < sk->sk_rcvlowat ||
5761796f82eaSEric Dumazet __tcp_select_window(sk) >= tp->rcv_wnd)) ||
57621da177e4SLinus Torvalds /* We ACK each frame or... */
5763466466dcSYuchung Cheng tcp_in_quickack_mode(sk) ||
5764466466dcSYuchung Cheng /* Protocol state mandates a one-time immediate ACK */
5765466466dcSYuchung Cheng inet_csk(sk)->icsk_ack.pending & ICSK_ACK_NOW) {
5766133c4c0dSEric Dumazet /* If we are running from __release_sock() in user context,
5767133c4c0dSEric Dumazet * Defer the ack until tcp_release_cb().
5768133c4c0dSEric Dumazet */
5769133c4c0dSEric Dumazet if (sock_owned_by_user_nocheck(sk) &&
5770133c4c0dSEric Dumazet READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_backlog_ack_defer)) {
5771133c4c0dSEric Dumazet set_bit(TCP_ACK_DEFERRED, &sk->sk_tsq_flags);
5772133c4c0dSEric Dumazet return;
5773133c4c0dSEric Dumazet }
57745d9f4262SEric Dumazet send_now:
57751da177e4SLinus Torvalds tcp_send_ack(sk);
57765d9f4262SEric Dumazet return;
57771da177e4SLinus Torvalds }
57785d9f4262SEric Dumazet
57795d9f4262SEric Dumazet if (!ofo_possible || RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
57805d9f4262SEric Dumazet tcp_send_delayed_ack(sk);
57815d9f4262SEric Dumazet return;
57825d9f4262SEric Dumazet }
57835d9f4262SEric Dumazet
57849c21d2fcSEric Dumazet if (!tcp_is_sack(tp) ||
578579f55473SKuniyuki Iwashima tp->compressed_ack >= READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_comp_sack_nr))
57865d9f4262SEric Dumazet goto send_now;
578786de5921SEric Dumazet
578886de5921SEric Dumazet if (tp->compressed_ack_rcv_nxt != tp->rcv_nxt) {
578986de5921SEric Dumazet tp->compressed_ack_rcv_nxt = tp->rcv_nxt;
57902b195850SEric Dumazet tp->dup_ack_counter = 0;
579186de5921SEric Dumazet }
57922b195850SEric Dumazet if (tp->dup_ack_counter < TCP_FASTRETRANS_THRESH) {
57932b195850SEric Dumazet tp->dup_ack_counter++;
579486de5921SEric Dumazet goto send_now;
57952b195850SEric Dumazet }
57962b195850SEric Dumazet tp->compressed_ack++;
57975d9f4262SEric Dumazet if (hrtimer_is_queued(&tp->compressed_ack_timer))
57985d9f4262SEric Dumazet return;
57995d9f4262SEric Dumazet
58006d82aa24SEric Dumazet /* compress ack timer : 5 % of rtt, but no more than tcp_comp_sack_delay_ns */
58015d9f4262SEric Dumazet
58025d9f4262SEric Dumazet rtt = tp->rcv_rtt_est.rtt_us;
58035d9f4262SEric Dumazet if (tp->srtt_us && tp->srtt_us < rtt)
58045d9f4262SEric Dumazet rtt = tp->srtt_us;
58055d9f4262SEric Dumazet
58064866b2b0SKuniyuki Iwashima delay = min_t(unsigned long,
58074866b2b0SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_comp_sack_delay_ns),
58085d9f4262SEric Dumazet rtt * (NSEC_PER_USEC >> 3)/20);
58095d9f4262SEric Dumazet sock_hold(sk);
5810a70437ccSEric Dumazet hrtimer_start_range_ns(&tp->compressed_ack_timer, ns_to_ktime(delay),
581122396941SKuniyuki Iwashima READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_comp_sack_slack_ns),
58125d9f4262SEric Dumazet HRTIMER_MODE_REL_PINNED_SOFT);
58131da177e4SLinus Torvalds }
58141da177e4SLinus Torvalds
tcp_ack_snd_check(struct sock * sk)581540efc6faSStephen Hemminger static inline void tcp_ack_snd_check(struct sock *sk)
58161da177e4SLinus Torvalds {
5817463c84b9SArnaldo Carvalho de Melo if (!inet_csk_ack_scheduled(sk)) {
58181da177e4SLinus Torvalds /* We sent a data segment already. */
58191da177e4SLinus Torvalds return;
58201da177e4SLinus Torvalds }
58211da177e4SLinus Torvalds __tcp_ack_snd_check(sk, 1);
58221da177e4SLinus Torvalds }
58231da177e4SLinus Torvalds
58241da177e4SLinus Torvalds /*
58251da177e4SLinus Torvalds * This routine is only called when we have urgent data
5826caa20d9aSStephen Hemminger * signaled. Its the 'slow' part of tcp_urg. It could be
58271da177e4SLinus Torvalds * moved inline now as tcp_urg is only called from one
58281da177e4SLinus Torvalds * place. We handle URGent data wrong. We have to - as
58291da177e4SLinus Torvalds * BSD still doesn't use the correction from RFC961.
58301da177e4SLinus Torvalds * For 1003.1g we should support a new option TCP_STDURG to permit
58311da177e4SLinus Torvalds * either form (or just set the sysctl tcp_stdurg).
58321da177e4SLinus Torvalds */
58331da177e4SLinus Torvalds
tcp_check_urg(struct sock * sk,const struct tcphdr * th)5834cf533ea5SEric Dumazet static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
58351da177e4SLinus Torvalds {
58361da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
58371da177e4SLinus Torvalds u32 ptr = ntohs(th->urg_ptr);
58381da177e4SLinus Torvalds
58394e08ed41SKuniyuki Iwashima if (ptr && !READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_stdurg))
58401da177e4SLinus Torvalds ptr--;
58411da177e4SLinus Torvalds ptr += ntohl(th->seq);
58421da177e4SLinus Torvalds
58431da177e4SLinus Torvalds /* Ignore urgent data that we've already seen and read. */
58441da177e4SLinus Torvalds if (after(tp->copied_seq, ptr))
58451da177e4SLinus Torvalds return;
58461da177e4SLinus Torvalds
58471da177e4SLinus Torvalds /* Do not replay urg ptr.
58481da177e4SLinus Torvalds *
58491da177e4SLinus Torvalds * NOTE: interesting situation not covered by specs.
58501da177e4SLinus Torvalds * Misbehaving sender may send urg ptr, pointing to segment,
58511da177e4SLinus Torvalds * which we already have in ofo queue. We are not able to fetch
58521da177e4SLinus Torvalds * such data and will stay in TCP_URG_NOTYET until will be eaten
58531da177e4SLinus Torvalds * by recvmsg(). Seems, we are not obliged to handle such wicked
58541da177e4SLinus Torvalds * situations. But it is worth to think about possibility of some
58551da177e4SLinus Torvalds * DoSes using some hypothetical application level deadlock.
58561da177e4SLinus Torvalds */
58571da177e4SLinus Torvalds if (before(ptr, tp->rcv_nxt))
58581da177e4SLinus Torvalds return;
58591da177e4SLinus Torvalds
58601da177e4SLinus Torvalds /* Do we already have a newer (or duplicate) urgent pointer? */
58611da177e4SLinus Torvalds if (tp->urg_data && !after(ptr, tp->urg_seq))
58621da177e4SLinus Torvalds return;
58631da177e4SLinus Torvalds
58641da177e4SLinus Torvalds /* Tell the world about our new urgent pointer. */
58651da177e4SLinus Torvalds sk_send_sigurg(sk);
58661da177e4SLinus Torvalds
58671da177e4SLinus Torvalds /* We may be adding urgent data when the last byte read was
58681da177e4SLinus Torvalds * urgent. To do this requires some care. We cannot just ignore
58691da177e4SLinus Torvalds * tp->copied_seq since we would read the last urgent byte again
58701da177e4SLinus Torvalds * as data, nor can we alter copied_seq until this data arrives
5871caa20d9aSStephen Hemminger * or we break the semantics of SIOCATMARK (and thus sockatmark())
58721da177e4SLinus Torvalds *
58731da177e4SLinus Torvalds * NOTE. Double Dutch. Rendering to plain English: author of comment
58741da177e4SLinus Torvalds * above did something sort of send("A", MSG_OOB); send("B", MSG_OOB);
58751da177e4SLinus Torvalds * and expect that both A and B disappear from stream. This is _wrong_.
58761da177e4SLinus Torvalds * Though this happens in BSD with high probability, this is occasional.
58771da177e4SLinus Torvalds * Any application relying on this is buggy. Note also, that fix "works"
58781da177e4SLinus Torvalds * only in this artificial test. Insert some normal data between A and B and we will
58791da177e4SLinus Torvalds * decline of BSD again. Verdict: it is better to remove to trap
58801da177e4SLinus Torvalds * buggy users.
58811da177e4SLinus Torvalds */
58821da177e4SLinus Torvalds if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
5883056834d9SIlpo Järvinen !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
58841da177e4SLinus Torvalds struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
58851da177e4SLinus Torvalds tp->copied_seq++;
58861da177e4SLinus Torvalds if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
58878728b834SDavid S. Miller __skb_unlink(skb, &sk->sk_receive_queue);
58881da177e4SLinus Torvalds __kfree_skb(skb);
58891da177e4SLinus Torvalds }
58901da177e4SLinus Torvalds }
58911da177e4SLinus Torvalds
58927b6a893aSEric Dumazet WRITE_ONCE(tp->urg_data, TCP_URG_NOTYET);
5893d9b55bf7SEric Dumazet WRITE_ONCE(tp->urg_seq, ptr);
589431770e34SFlorian Westphal
589531770e34SFlorian Westphal /* Disable header prediction. */
589631770e34SFlorian Westphal tp->pred_flags = 0;
58971da177e4SLinus Torvalds }
58981da177e4SLinus Torvalds
58991da177e4SLinus Torvalds /* This is the 'fast' part of urgent handling. */
tcp_urg(struct sock * sk,struct sk_buff * skb,const struct tcphdr * th)5900cf533ea5SEric Dumazet static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th)
59011da177e4SLinus Torvalds {
59021da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
59031da177e4SLinus Torvalds
59041da177e4SLinus Torvalds /* Check if we get a new urgent pointer - normally not. */
5905b96c51bdSEric Dumazet if (unlikely(th->urg))
59061da177e4SLinus Torvalds tcp_check_urg(sk, th);
59071da177e4SLinus Torvalds
59081da177e4SLinus Torvalds /* Do we wait for any urgent data? - normally not... */
5909b96c51bdSEric Dumazet if (unlikely(tp->urg_data == TCP_URG_NOTYET)) {
59101da177e4SLinus Torvalds u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) -
59111da177e4SLinus Torvalds th->syn;
59121da177e4SLinus Torvalds
59131da177e4SLinus Torvalds /* Is the urgent pointer pointing into this packet? */
59141da177e4SLinus Torvalds if (ptr < skb->len) {
59151da177e4SLinus Torvalds u8 tmp;
59161da177e4SLinus Torvalds if (skb_copy_bits(skb, ptr, &tmp, 1))
59171da177e4SLinus Torvalds BUG();
59187b6a893aSEric Dumazet WRITE_ONCE(tp->urg_data, TCP_URG_VALID | tmp);
59191da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD))
5920676d2369SDavid S. Miller sk->sk_data_ready(sk);
59211da177e4SLinus Torvalds }
59221da177e4SLinus Torvalds }
59231da177e4SLinus Torvalds }
59241da177e4SLinus Torvalds
59250e40f4c9SJason Baron /* Accept RST for rcv_nxt - 1 after a FIN.
59260e40f4c9SJason Baron * When tcp connections are abruptly terminated from Mac OSX (via ^C), a
59270e40f4c9SJason Baron * FIN is sent followed by a RST packet. The RST is sent with the same
59280e40f4c9SJason Baron * sequence number as the FIN, and thus according to RFC 5961 a challenge
59290e40f4c9SJason Baron * ACK should be sent. However, Mac OSX rate limits replies to challenge
59300e40f4c9SJason Baron * ACKs on the closed socket. In addition middleboxes can drop either the
59310e40f4c9SJason Baron * challenge ACK or a subsequent RST.
59320e40f4c9SJason Baron */
tcp_reset_check(const struct sock * sk,const struct sk_buff * skb)59330e40f4c9SJason Baron static bool tcp_reset_check(const struct sock *sk, const struct sk_buff *skb)
59340e40f4c9SJason Baron {
5935e9d9da91SEric Dumazet const struct tcp_sock *tp = tcp_sk(sk);
59360e40f4c9SJason Baron
59370e40f4c9SJason Baron return unlikely(TCP_SKB_CB(skb)->seq == (tp->rcv_nxt - 1) &&
59380e40f4c9SJason Baron (1 << sk->sk_state) & (TCPF_CLOSE_WAIT | TCPF_LAST_ACK |
59390e40f4c9SJason Baron TCPF_CLOSING));
59400e40f4c9SJason Baron }
59410e40f4c9SJason Baron
5942cbe2d128SIlpo Järvinen /* Does PAWS and seqno based validation of an incoming segment, flags will
5943cbe2d128SIlpo Järvinen * play significant role here.
5944cbe2d128SIlpo Järvinen */
tcp_validate_incoming(struct sock * sk,struct sk_buff * skb,const struct tcphdr * th,int syn_inerr)59450c24604bSEric Dumazet static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
5946cf533ea5SEric Dumazet const struct tcphdr *th, int syn_inerr)
5947cbe2d128SIlpo Järvinen {
5948cbe2d128SIlpo Järvinen struct tcp_sock *tp = tcp_sk(sk);
5949da40b613SEric Dumazet SKB_DR(reason);
5950cbe2d128SIlpo Järvinen
5951cbe2d128SIlpo Järvinen /* RFC1323: H1. Apply PAWS check first. */
5952eed29f17SEric Dumazet if (tcp_fast_parse_options(sock_net(sk), skb, th, tp) &&
5953eed29f17SEric Dumazet tp->rx_opt.saw_tstamp &&
5954cbe2d128SIlpo Järvinen tcp_paws_discard(sk, skb)) {
5955cbe2d128SIlpo Järvinen if (!th->rst) {
5956ee05d90dSKuniyuki Iwashima if (unlikely(th->syn))
5957ee05d90dSKuniyuki Iwashima goto syn_challenge;
5958c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
5959f2b2c582SNeal Cardwell if (!tcp_oow_rate_limited(sock_net(sk), skb,
5960f2b2c582SNeal Cardwell LINUX_MIB_TCPACKSKIPPEDPAWS,
5961f2b2c582SNeal Cardwell &tp->last_oow_ack_time))
5962cbe2d128SIlpo Järvinen tcp_send_dupack(sk, skb);
5963da40b613SEric Dumazet SKB_DR_SET(reason, TCP_RFC7323_PAWS);
5964cbe2d128SIlpo Järvinen goto discard;
5965cbe2d128SIlpo Järvinen }
5966cbe2d128SIlpo Järvinen /* Reset is accepted even if it did not pass PAWS. */
5967cbe2d128SIlpo Järvinen }
5968cbe2d128SIlpo Järvinen
5969cbe2d128SIlpo Järvinen /* Step 1: check sequence number */
5970b4469349SEric Dumazet reason = tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
5971b4469349SEric Dumazet if (reason) {
5972cbe2d128SIlpo Järvinen /* RFC793, page 37: "In all states except SYN-SENT, all reset
5973cbe2d128SIlpo Järvinen * (RST) segments are validated by checking their SEQ-fields."
5974cbe2d128SIlpo Järvinen * And page 69: "If an incoming segment is not acceptable,
5975cbe2d128SIlpo Järvinen * an acknowledgment should be sent in reply (unless the RST
5976cbe2d128SIlpo Järvinen * bit is set, if so drop the segment and return)".
5977cbe2d128SIlpo Järvinen */
5978e3715899SEric Dumazet if (!th->rst) {
5979e3715899SEric Dumazet if (th->syn)
5980e3715899SEric Dumazet goto syn_challenge;
5981f2b2c582SNeal Cardwell if (!tcp_oow_rate_limited(sock_net(sk), skb,
5982f2b2c582SNeal Cardwell LINUX_MIB_TCPACKSKIPPEDSEQ,
5983f2b2c582SNeal Cardwell &tp->last_oow_ack_time))
5984cbe2d128SIlpo Järvinen tcp_send_dupack(sk, skb);
59850e40f4c9SJason Baron } else if (tcp_reset_check(sk, skb)) {
5986d9d024f9SEric Dumazet goto reset;
5987e3715899SEric Dumazet }
5988cbe2d128SIlpo Järvinen goto discard;
5989cbe2d128SIlpo Järvinen }
5990cbe2d128SIlpo Järvinen
5991cbe2d128SIlpo Järvinen /* Step 2: check RST bit */
5992cbe2d128SIlpo Järvinen if (th->rst) {
59930e40f4c9SJason Baron /* RFC 5961 3.2 (extend to match against (RCV.NXT - 1) after a
59940e40f4c9SJason Baron * FIN and SACK too if available):
59950e40f4c9SJason Baron * If seq num matches RCV.NXT or (RCV.NXT - 1) after a FIN, or
59960e40f4c9SJason Baron * the right-most SACK block,
5997e00431bcSPau Espin Pedrol * then
5998282f23c6SEric Dumazet * RESET the connection
5999282f23c6SEric Dumazet * else
6000282f23c6SEric Dumazet * Send a challenge ACK
6001282f23c6SEric Dumazet */
60020e40f4c9SJason Baron if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt ||
6003b5ec1e62SEric Dumazet tcp_reset_check(sk, skb))
6004b5ec1e62SEric Dumazet goto reset;
6005b5ec1e62SEric Dumazet
6006b5ec1e62SEric Dumazet if (tcp_is_sack(tp) && tp->rx_opt.num_sacks > 0) {
6007e00431bcSPau Espin Pedrol struct tcp_sack_block *sp = &tp->selective_acks[0];
6008e00431bcSPau Espin Pedrol int max_sack = sp[0].end_seq;
6009e00431bcSPau Espin Pedrol int this_sack;
6010e00431bcSPau Espin Pedrol
6011e00431bcSPau Espin Pedrol for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;
6012e00431bcSPau Espin Pedrol ++this_sack) {
6013e00431bcSPau Espin Pedrol max_sack = after(sp[this_sack].end_seq,
6014e00431bcSPau Espin Pedrol max_sack) ?
6015e00431bcSPau Espin Pedrol sp[this_sack].end_seq : max_sack;
6016e00431bcSPau Espin Pedrol }
6017e00431bcSPau Espin Pedrol
6018e00431bcSPau Espin Pedrol if (TCP_SKB_CB(skb)->seq == max_sack)
6019d9d024f9SEric Dumazet goto reset;
6020b5ec1e62SEric Dumazet }
6021d9d024f9SEric Dumazet
6022cf1ef3f0SWei Wang /* Disable TFO if RST is out-of-order
6023cf1ef3f0SWei Wang * and no data has been received
6024cf1ef3f0SWei Wang * for current active TFO socket
6025cf1ef3f0SWei Wang */
6026cf1ef3f0SWei Wang if (tp->syn_fastopen && !tp->data_segs_in &&
6027cf1ef3f0SWei Wang sk->sk_state == TCP_ESTABLISHED)
602846c2fa39SWei Wang tcp_fastopen_active_disable(sk);
6029208dd45dSBenjamin Yim tcp_send_challenge_ack(sk);
6030da40b613SEric Dumazet SKB_DR_SET(reason, TCP_RESET);
6031cbe2d128SIlpo Järvinen goto discard;
6032cbe2d128SIlpo Järvinen }
6033cbe2d128SIlpo Järvinen
6034cbe2d128SIlpo Järvinen /* step 3: check security and precedence [ignored] */
6035cbe2d128SIlpo Järvinen
60360c24604bSEric Dumazet /* step 4: Check for a SYN
6037cd214535SSowmini Varadhan * RFC 5961 4.2 : Send a challenge ack
60380c24604bSEric Dumazet */
60390c24604bSEric Dumazet if (th->syn) {
604023e89e8eSKuniyuki Iwashima if (sk->sk_state == TCP_SYN_RECV && sk->sk_socket && th->ack &&
604123e89e8eSKuniyuki Iwashima TCP_SKB_CB(skb)->seq + 1 == TCP_SKB_CB(skb)->end_seq &&
604223e89e8eSKuniyuki Iwashima TCP_SKB_CB(skb)->seq + 1 == tp->rcv_nxt &&
604323e89e8eSKuniyuki Iwashima TCP_SKB_CB(skb)->ack_seq == tp->snd_nxt)
604423e89e8eSKuniyuki Iwashima goto pass;
6045e3715899SEric Dumazet syn_challenge:
6046cbe2d128SIlpo Järvinen if (syn_inerr)
6047c10d9310SEric Dumazet TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
6048c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE);
6049208dd45dSBenjamin Yim tcp_send_challenge_ack(sk);
6050da40b613SEric Dumazet SKB_DR_SET(reason, TCP_INVALID_SYN);
60510c24604bSEric Dumazet goto discard;
6052cbe2d128SIlpo Järvinen }
6053cbe2d128SIlpo Järvinen
605423e89e8eSKuniyuki Iwashima pass:
605500d211a4SMartin KaFai Lau bpf_skops_parse_hdr(sk, skb);
605600d211a4SMartin KaFai Lau
60570c24604bSEric Dumazet return true;
6058cbe2d128SIlpo Järvinen
6059cbe2d128SIlpo Järvinen discard:
6060da40b613SEric Dumazet tcp_drop_reason(sk, skb, reason);
60610c24604bSEric Dumazet return false;
6062d9d024f9SEric Dumazet
6063d9d024f9SEric Dumazet reset:
6064d9d024f9SEric Dumazet tcp_reset(sk, skb);
6065d9d024f9SEric Dumazet __kfree_skb(skb);
6066d9d024f9SEric Dumazet return false;
6067cbe2d128SIlpo Järvinen }
6068cbe2d128SIlpo Järvinen
60691da177e4SLinus Torvalds /*
60701da177e4SLinus Torvalds * TCP receive function for the ESTABLISHED state.
607131770e34SFlorian Westphal *
607231770e34SFlorian Westphal * It is split into a fast path and a slow path. The fast path is
607331770e34SFlorian Westphal * disabled when:
607431770e34SFlorian Westphal * - A zero window was announced from us - zero window probing
607531770e34SFlorian Westphal * is only handled properly in the slow path.
607631770e34SFlorian Westphal * - Out of order segments arrived.
607731770e34SFlorian Westphal * - Urgent data is expected.
607831770e34SFlorian Westphal * - There is no buffer space left
607931770e34SFlorian Westphal * - Unexpected TCP flags/window values/header lengths are received
608031770e34SFlorian Westphal * (detected by checking the TCP header against pred_flags)
608131770e34SFlorian Westphal * - Data is sent in both directions. Fast path only supports pure senders
608231770e34SFlorian Westphal * or pure receivers (this means either the sequence number or the ack
608331770e34SFlorian Westphal * value must stay constant)
608431770e34SFlorian Westphal * - Unexpected TCP option.
608531770e34SFlorian Westphal *
608631770e34SFlorian Westphal * When these conditions are not satisfied it drops into a standard
608731770e34SFlorian Westphal * receive procedure patterned after RFC793 to handle all cases.
608831770e34SFlorian Westphal * The first three cases are guaranteed by proper pred_flags setting,
608931770e34SFlorian Westphal * the rest is checked inline. Fast processing is turned on in
609031770e34SFlorian Westphal * tcp_data_queue when everything is OK.
60911da177e4SLinus Torvalds */
tcp_rcv_established(struct sock * sk,struct sk_buff * skb)60923d97d88eSYafang Shao void tcp_rcv_established(struct sock *sk, struct sk_buff *skb)
60931da177e4SLinus Torvalds {
60942a968ef6SMenglong Dong enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
60953d97d88eSYafang Shao const struct tcphdr *th = (const struct tcphdr *)skb->data;
60961da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
60973d97d88eSYafang Shao unsigned int len = skb->len;
60981da177e4SLinus Torvalds
6099c3fde1bdSMasami Hiramatsu /* TCP congestion window tracking */
6100c3fde1bdSMasami Hiramatsu trace_tcp_probe(sk, skb);
6101c3fde1bdSMasami Hiramatsu
61029a568de4SEric Dumazet tcp_mstamp_refresh(tp);
61038f905c0eSEric Dumazet if (unlikely(!rcu_access_pointer(sk->sk_rx_dst)))
61045d299f3dSEric Dumazet inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
610531770e34SFlorian Westphal /*
610631770e34SFlorian Westphal * Header prediction.
610731770e34SFlorian Westphal * The code loosely follows the one in the famous
610831770e34SFlorian Westphal * "30 instruction TCP receive" Van Jacobson mail.
610931770e34SFlorian Westphal *
611031770e34SFlorian Westphal * Van's trick is to deposit buffers into socket queue
611131770e34SFlorian Westphal * on a device interrupt, to call tcp_recv function
611231770e34SFlorian Westphal * on the receive process context and checksum and copy
611331770e34SFlorian Westphal * the buffer to user space. smart...
611431770e34SFlorian Westphal *
611531770e34SFlorian Westphal * Our current scheme is not silly either but we take the
611631770e34SFlorian Westphal * extra cost of the net_bh soft interrupt processing...
611731770e34SFlorian Westphal * We do checksum and copy also but from device to kernel.
611831770e34SFlorian Westphal */
61191da177e4SLinus Torvalds
61201da177e4SLinus Torvalds tp->rx_opt.saw_tstamp = 0;
61211da177e4SLinus Torvalds
612231770e34SFlorian Westphal /* pred_flags is 0xS?10 << 16 + snd_wnd
612331770e34SFlorian Westphal * if header_prediction is to be made
612431770e34SFlorian Westphal * 'S' will always be tp->tcp_header_len >> 2
612531770e34SFlorian Westphal * '?' will be 0 for the fast path, otherwise pred_flags is 0 to
612631770e34SFlorian Westphal * turn it off (when there are holes in the receive
612731770e34SFlorian Westphal * space for instance)
612831770e34SFlorian Westphal * PSH flag is ignored.
612931770e34SFlorian Westphal */
613031770e34SFlorian Westphal
613131770e34SFlorian Westphal if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
613231770e34SFlorian Westphal TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&
613331770e34SFlorian Westphal !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
613431770e34SFlorian Westphal int tcp_header_len = tp->tcp_header_len;
613531770e34SFlorian Westphal
613631770e34SFlorian Westphal /* Timestamp header prediction: tcp_header_len
613731770e34SFlorian Westphal * is automatically equal to th->doff*4 due to pred_flags
613831770e34SFlorian Westphal * match.
613931770e34SFlorian Westphal */
614031770e34SFlorian Westphal
614131770e34SFlorian Westphal /* Check timestamp */
614231770e34SFlorian Westphal if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
614331770e34SFlorian Westphal /* No? Slow path! */
614431770e34SFlorian Westphal if (!tcp_parse_aligned_timestamp(tp, th))
614531770e34SFlorian Westphal goto slow_path;
614631770e34SFlorian Westphal
614731770e34SFlorian Westphal /* If PAWS failed, check it more carefully in slow path */
614831770e34SFlorian Westphal if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0)
614931770e34SFlorian Westphal goto slow_path;
615031770e34SFlorian Westphal
615131770e34SFlorian Westphal /* DO NOT update ts_recent here, if checksum fails
615231770e34SFlorian Westphal * and timestamp was corrupted part, it will result
615331770e34SFlorian Westphal * in a hung connection since we will drop all
615431770e34SFlorian Westphal * future packets due to the PAWS test.
615531770e34SFlorian Westphal */
615631770e34SFlorian Westphal }
615731770e34SFlorian Westphal
615831770e34SFlorian Westphal if (len <= tcp_header_len) {
615931770e34SFlorian Westphal /* Bulk data transfer: sender */
616031770e34SFlorian Westphal if (len == tcp_header_len) {
616131770e34SFlorian Westphal /* Predicted packet is in window by definition.
616231770e34SFlorian Westphal * seq == rcv_nxt and rcv_wup <= rcv_nxt.
616331770e34SFlorian Westphal * Hence, check seq<=rcv_wup reduces to:
616431770e34SFlorian Westphal */
616531770e34SFlorian Westphal if (tcp_header_len ==
616631770e34SFlorian Westphal (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
616731770e34SFlorian Westphal tp->rcv_nxt == tp->rcv_wup)
616831770e34SFlorian Westphal tcp_store_ts_recent(tp);
616931770e34SFlorian Westphal
617031770e34SFlorian Westphal /* We know that such packets are checksummed
617131770e34SFlorian Westphal * on entry.
617231770e34SFlorian Westphal */
617331770e34SFlorian Westphal tcp_ack(sk, skb, 0);
617431770e34SFlorian Westphal __kfree_skb(skb);
617531770e34SFlorian Westphal tcp_data_snd_check(sk);
61763f6c65d6SWei Wang /* When receiving pure ack in fast path, update
61773f6c65d6SWei Wang * last ts ecr directly instead of calling
61783f6c65d6SWei Wang * tcp_rcv_rtt_measure_ts()
61793f6c65d6SWei Wang */
61803f6c65d6SWei Wang tp->rcv_rtt_last_tsecr = tp->rx_opt.rcv_tsecr;
618131770e34SFlorian Westphal return;
618231770e34SFlorian Westphal } else { /* Header too small */
61832a968ef6SMenglong Dong reason = SKB_DROP_REASON_PKT_TOO_SMALL;
618431770e34SFlorian Westphal TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
618531770e34SFlorian Westphal goto discard;
618631770e34SFlorian Westphal }
618731770e34SFlorian Westphal } else {
618831770e34SFlorian Westphal int eaten = 0;
618931770e34SFlorian Westphal bool fragstolen = false;
619031770e34SFlorian Westphal
619131770e34SFlorian Westphal if (tcp_checksum_complete(skb))
619231770e34SFlorian Westphal goto csum_error;
619331770e34SFlorian Westphal
619431770e34SFlorian Westphal if ((int)skb->truesize > sk->sk_forward_alloc)
619531770e34SFlorian Westphal goto step5;
619631770e34SFlorian Westphal
619731770e34SFlorian Westphal /* Predicted packet is in window by definition.
619831770e34SFlorian Westphal * seq == rcv_nxt and rcv_wup <= rcv_nxt.
619931770e34SFlorian Westphal * Hence, check seq<=rcv_wup reduces to:
620031770e34SFlorian Westphal */
620131770e34SFlorian Westphal if (tcp_header_len ==
620231770e34SFlorian Westphal (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
620331770e34SFlorian Westphal tp->rcv_nxt == tp->rcv_wup)
620431770e34SFlorian Westphal tcp_store_ts_recent(tp);
620531770e34SFlorian Westphal
620631770e34SFlorian Westphal tcp_rcv_rtt_measure_ts(sk, skb);
620731770e34SFlorian Westphal
620831770e34SFlorian Westphal NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPHITS);
620931770e34SFlorian Westphal
621031770e34SFlorian Westphal /* Bulk data transfer: receiver */
6211783d108dSEric Dumazet skb_dst_drop(skb);
6212e7395f1fSEric Dumazet __skb_pull(skb, tcp_header_len);
6213e7395f1fSEric Dumazet eaten = tcp_queue_rcv(sk, skb, &fragstolen);
621431770e34SFlorian Westphal
621531770e34SFlorian Westphal tcp_event_data_recv(sk, skb);
621631770e34SFlorian Westphal
621731770e34SFlorian Westphal if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
621831770e34SFlorian Westphal /* Well, only one small jumplet in fast path... */
621931770e34SFlorian Westphal tcp_ack(sk, skb, FLAG_DATA);
622031770e34SFlorian Westphal tcp_data_snd_check(sk);
622131770e34SFlorian Westphal if (!inet_csk_ack_scheduled(sk))
622231770e34SFlorian Westphal goto no_ack;
622318ded910SNeal Cardwell } else {
622418ded910SNeal Cardwell tcp_update_wl(tp, TCP_SKB_CB(skb)->seq);
622531770e34SFlorian Westphal }
622631770e34SFlorian Westphal
622731770e34SFlorian Westphal __tcp_ack_snd_check(sk, 0);
622831770e34SFlorian Westphal no_ack:
622931770e34SFlorian Westphal if (eaten)
623031770e34SFlorian Westphal kfree_skb_partial(skb, fragstolen);
623103f45c88SEric Dumazet tcp_data_ready(sk);
623231770e34SFlorian Westphal return;
623331770e34SFlorian Westphal }
623431770e34SFlorian Westphal }
623531770e34SFlorian Westphal
623631770e34SFlorian Westphal slow_path:
6237fb3477c0SEric Dumazet if (len < (th->doff << 2) || tcp_checksum_complete(skb))
62381da177e4SLinus Torvalds goto csum_error;
62391da177e4SLinus Torvalds
62402a968ef6SMenglong Dong if (!th->ack && !th->rst && !th->syn) {
62412a968ef6SMenglong Dong reason = SKB_DROP_REASON_TCP_FLAGS;
6242c3ae62afSEric Dumazet goto discard;
62432a968ef6SMenglong Dong }
6244c3ae62afSEric Dumazet
624531770e34SFlorian Westphal /*
624631770e34SFlorian Westphal * Standard slow path.
624731770e34SFlorian Westphal */
624831770e34SFlorian Westphal
62490c24604bSEric Dumazet if (!tcp_validate_incoming(sk, skb, th, 1))
6250c995ae22SVijay Subramanian return;
62511da177e4SLinus Torvalds
625231770e34SFlorian Westphal step5:
62534b506af9SEric Dumazet reason = tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT);
6254843f7740SEric Dumazet if ((int)reason < 0) {
6255843f7740SEric Dumazet reason = -reason;
625696e0bf4bSJohn Dykstra goto discard;
6257843f7740SEric Dumazet }
6258463c84b9SArnaldo Carvalho de Melo tcp_rcv_rtt_measure_ts(sk, skb);
62591da177e4SLinus Torvalds
62601da177e4SLinus Torvalds /* Process urgent data. */
62611da177e4SLinus Torvalds tcp_urg(sk, skb, th);
62621da177e4SLinus Torvalds
62631da177e4SLinus Torvalds /* step 7: process the segment text */
62641da177e4SLinus Torvalds tcp_data_queue(sk, skb);
62651da177e4SLinus Torvalds
62669e412ba7SIlpo Järvinen tcp_data_snd_check(sk);
62671da177e4SLinus Torvalds tcp_ack_snd_check(sk);
6268c995ae22SVijay Subramanian return;
62691da177e4SLinus Torvalds
62701da177e4SLinus Torvalds csum_error:
62712a968ef6SMenglong Dong reason = SKB_DROP_REASON_TCP_CSUM;
6272709c0314SJakub Kicinski trace_tcp_bad_csum(skb);
6273c10d9310SEric Dumazet TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
6274c10d9310SEric Dumazet TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
62751da177e4SLinus Torvalds
62761da177e4SLinus Torvalds discard:
62772a968ef6SMenglong Dong tcp_drop_reason(sk, skb, reason);
62781da177e4SLinus Torvalds }
62794bc2f18bSEric Dumazet EXPORT_SYMBOL(tcp_rcv_established);
62801da177e4SLinus Torvalds
tcp_init_transfer(struct sock * sk,int bpf_op,struct sk_buff * skb)628172be0fe6SMartin KaFai Lau void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb)
628298fa6271SYuchung Cheng {
628398fa6271SYuchung Cheng struct inet_connection_sock *icsk = inet_csk(sk);
628498fa6271SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
628598fa6271SYuchung Cheng
628698fa6271SYuchung Cheng tcp_mtup_init(sk);
628798fa6271SYuchung Cheng icsk->icsk_af_ops->rebuild_header(sk);
628898fa6271SYuchung Cheng tcp_init_metrics(sk);
628998fa6271SYuchung Cheng
629098fa6271SYuchung Cheng /* Initialize the congestion window to start the transfer.
629198fa6271SYuchung Cheng * Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
629298fa6271SYuchung Cheng * retransmitted. In light of RFC6298 more aggressive 1sec
629398fa6271SYuchung Cheng * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
629498fa6271SYuchung Cheng * retransmission has occurred.
629598fa6271SYuchung Cheng */
629698fa6271SYuchung Cheng if (tp->total_retrans > 1 && tp->undo_marker)
629740570375SEric Dumazet tcp_snd_cwnd_set(tp, 1);
629898fa6271SYuchung Cheng else
629940570375SEric Dumazet tcp_snd_cwnd_set(tp, tcp_init_cwnd(tp, __sk_dst_get(sk)));
630098fa6271SYuchung Cheng tp->snd_cwnd_stamp = tcp_jiffies32;
630198fa6271SYuchung Cheng
630272be0fe6SMartin KaFai Lau bpf_skops_established(sk, bpf_op, skb);
6303be5d1b61SNguyen Dinh Phi /* Initialize congestion control unless BPF initialized it already: */
63048919a9b3SNeal Cardwell if (!icsk->icsk_ca_initialized)
630598fa6271SYuchung Cheng tcp_init_congestion_control(sk);
630698fa6271SYuchung Cheng tcp_init_buffer_space(sk);
630798fa6271SYuchung Cheng }
630898fa6271SYuchung Cheng
tcp_finish_connect(struct sock * sk,struct sk_buff * skb)6309370816aeSPavel Emelyanov void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
6310370816aeSPavel Emelyanov {
6311370816aeSPavel Emelyanov struct tcp_sock *tp = tcp_sk(sk);
6312370816aeSPavel Emelyanov struct inet_connection_sock *icsk = inet_csk(sk);
6313370816aeSPavel Emelyanov
63147c2ffaf2SDmitry Safonov tcp_ao_finish_connect(sk, skb);
6315370816aeSPavel Emelyanov tcp_set_state(sk, TCP_ESTABLISHED);
631670eabf0eSEric Dumazet icsk->icsk_ack.lrcvtime = tcp_jiffies32;
6317370816aeSPavel Emelyanov
631800db4124SIan Morris if (skb) {
63195d299f3dSEric Dumazet icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
6320370816aeSPavel Emelyanov security_inet_conn_established(sk, skb);
6321c6345ce7SAmritha Nambiar sk_mark_napi_id(sk, skb);
632241063e9dSDavid S. Miller }
6323370816aeSPavel Emelyanov
632472be0fe6SMartin KaFai Lau tcp_init_transfer(sk, BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, skb);
6325370816aeSPavel Emelyanov
6326370816aeSPavel Emelyanov /* Prevent spurious tcp_cwnd_restart() on first data
6327370816aeSPavel Emelyanov * packet.
6328370816aeSPavel Emelyanov */
6329d635fbe2SEric Dumazet tp->lsndtime = tcp_jiffies32;
6330370816aeSPavel Emelyanov
6331370816aeSPavel Emelyanov if (sock_flag(sk, SOCK_KEEPOPEN))
6332370816aeSPavel Emelyanov inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp));
633331770e34SFlorian Westphal
633431770e34SFlorian Westphal if (!tp->rx_opt.snd_wscale)
633531770e34SFlorian Westphal __tcp_fast_path_on(tp, tp->snd_wnd);
633631770e34SFlorian Westphal else
633731770e34SFlorian Westphal tp->pred_flags = 0;
6338370816aeSPavel Emelyanov }
6339370816aeSPavel Emelyanov
tcp_rcv_fastopen_synack(struct sock * sk,struct sk_buff * synack,struct tcp_fastopen_cookie * cookie)63408e4178c1SYuchung Cheng static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
63418e4178c1SYuchung Cheng struct tcp_fastopen_cookie *cookie)
63428e4178c1SYuchung Cheng {
63438e4178c1SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
634475c119afSEric Dumazet struct sk_buff *data = tp->syn_data ? tcp_rtx_queue_head(sk) : NULL;
63452646c831SDaniel Lee u16 mss = tp->rx_opt.mss_clamp, try_exp = 0;
63462646c831SDaniel Lee bool syn_drop = false;
63478e4178c1SYuchung Cheng
63488e4178c1SYuchung Cheng if (mss == tp->rx_opt.user_mss) {
63498e4178c1SYuchung Cheng struct tcp_options_received opt;
63508e4178c1SYuchung Cheng
63518e4178c1SYuchung Cheng /* Get original SYNACK MSS value if user MSS sets mss_clamp */
63528e4178c1SYuchung Cheng tcp_clear_options(&opt);
63538e4178c1SYuchung Cheng opt.user_mss = opt.mss_clamp = 0;
6354eed29f17SEric Dumazet tcp_parse_options(sock_net(sk), synack, &opt, 0, NULL);
63558e4178c1SYuchung Cheng mss = opt.mss_clamp;
63568e4178c1SYuchung Cheng }
63578e4178c1SYuchung Cheng
63582646c831SDaniel Lee if (!tp->syn_fastopen) {
63592646c831SDaniel Lee /* Ignore an unsolicited cookie */
636067da22d2SYuchung Cheng cookie->len = -1;
63612646c831SDaniel Lee } else if (tp->total_retrans) {
63622646c831SDaniel Lee /* SYN timed out and the SYN-ACK neither has a cookie nor
63632646c831SDaniel Lee * acknowledges data. Presumably the remote received only
63642646c831SDaniel Lee * the retransmitted (regular) SYNs: either the original
63652646c831SDaniel Lee * SYN-data or the corresponding SYN-ACK was dropped.
6366aab48743SYuchung Cheng */
63672646c831SDaniel Lee syn_drop = (cookie->len < 0 && data);
63682646c831SDaniel Lee } else if (cookie->len < 0 && !tp->syn_data) {
63692646c831SDaniel Lee /* We requested a cookie but didn't get it. If we did not use
63702646c831SDaniel Lee * the (old) exp opt format then try so next time (try_exp=1).
63712646c831SDaniel Lee * Otherwise we go back to use the RFC7413 opt (try_exp=2).
63722646c831SDaniel Lee */
63732646c831SDaniel Lee try_exp = tp->syn_fastopen_exp ? 2 : 1;
63742646c831SDaniel Lee }
6375aab48743SYuchung Cheng
63762646c831SDaniel Lee tcp_fastopen_cache_set(sk, mss, cookie, syn_drop, try_exp);
63778e4178c1SYuchung Cheng
63788e4178c1SYuchung Cheng if (data) { /* Retransmit unacked data in SYN */
637948027478SJason Baron if (tp->total_retrans)
638048027478SJason Baron tp->fastopen_client_fail = TFO_SYN_RETRANSMITTED;
638148027478SJason Baron else
638248027478SJason Baron tp->fastopen_client_fail = TFO_DATA_NOT_ACKED;
6383a7abf3cdSEric Dumazet skb_rbtree_walk_from(data)
6384a7abf3cdSEric Dumazet tcp_mark_skb_lost(sk, data);
63855dfe9d27SNeal Cardwell tcp_non_congestion_loss_retransmit(sk);
6386c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk),
638702a1d6e7SEric Dumazet LINUX_MIB_TCPFASTOPENACTIVEFAIL);
63888e4178c1SYuchung Cheng return true;
63898e4178c1SYuchung Cheng }
63906f73601eSYuchung Cheng tp->syn_data_acked = tp->syn_data;
6391bef5767fSYuchung Cheng if (tp->syn_data_acked) {
6392bef5767fSYuchung Cheng NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVE);
6393bef5767fSYuchung Cheng /* SYN-data is counted as two separate packets in tcp_ack() */
6394bef5767fSYuchung Cheng if (tp->delivered > 1)
6395bef5767fSYuchung Cheng --tp->delivered;
6396bef5767fSYuchung Cheng }
639761d2bcaeSEric Dumazet
639861d2bcaeSEric Dumazet tcp_fastopen_add_skb(sk, synack);
639961d2bcaeSEric Dumazet
64008e4178c1SYuchung Cheng return false;
64018e4178c1SYuchung Cheng }
64028e4178c1SYuchung Cheng
smc_check_reset_syn(struct tcp_sock * tp)640360e2a778SUrsula Braun static void smc_check_reset_syn(struct tcp_sock *tp)
640460e2a778SUrsula Braun {
640560e2a778SUrsula Braun #if IS_ENABLED(CONFIG_SMC)
640660e2a778SUrsula Braun if (static_branch_unlikely(&tcp_have_smc)) {
640760e2a778SUrsula Braun if (tp->syn_smc && !tp->rx_opt.smc_ok)
640860e2a778SUrsula Braun tp->syn_smc = 0;
640960e2a778SUrsula Braun }
641060e2a778SUrsula Braun #endif
641160e2a778SUrsula Braun }
641260e2a778SUrsula Braun
tcp_try_undo_spurious_syn(struct sock * sk)64137c1f0815SYuchung Cheng static void tcp_try_undo_spurious_syn(struct sock *sk)
64147c1f0815SYuchung Cheng {
64157c1f0815SYuchung Cheng struct tcp_sock *tp = tcp_sk(sk);
64167c1f0815SYuchung Cheng u32 syn_stamp;
64177c1f0815SYuchung Cheng
64187c1f0815SYuchung Cheng /* undo_marker is set when SYN or SYNACK times out. The timeout is
64197c1f0815SYuchung Cheng * spurious if the ACK's timestamp option echo value matches the
64207c1f0815SYuchung Cheng * original SYN timestamp.
64217c1f0815SYuchung Cheng */
64227c1f0815SYuchung Cheng syn_stamp = tp->retrans_stamp;
64237c1f0815SYuchung Cheng if (tp->undo_marker && syn_stamp && tp->rx_opt.saw_tstamp &&
64247c1f0815SYuchung Cheng syn_stamp == tp->rx_opt.rcv_tsecr)
64257c1f0815SYuchung Cheng tp->undo_marker = 0;
64267c1f0815SYuchung Cheng }
64277c1f0815SYuchung Cheng
tcp_rcv_synsent_state_process(struct sock * sk,struct sk_buff * skb,const struct tcphdr * th)64281da177e4SLinus Torvalds static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
6429bda07a64SEric Dumazet const struct tcphdr *th)
64301da177e4SLinus Torvalds {
6431d83d8461SArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
64324957faadSWilliam Allen Simpson struct tcp_sock *tp = tcp_sk(sk);
64338e4178c1SYuchung Cheng struct tcp_fastopen_cookie foc = { .len = -1 };
64344957faadSWilliam Allen Simpson int saved_clamp = tp->rx_opt.mss_clamp;
64350f9fa831SEric Dumazet bool fastopen_fail;
6436659affdbSEric Dumazet SKB_DR(reason);
64371da177e4SLinus Torvalds
6438eed29f17SEric Dumazet tcp_parse_options(sock_net(sk), skb, &tp->rx_opt, 0, &foc);
6439e3e12028SAndrew Vagin if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
6440ee684b6fSAndrey Vagin tp->rx_opt.rcv_tsecr -= tp->tsoffset;
64411da177e4SLinus Torvalds
64421da177e4SLinus Torvalds if (th->ack) {
64431da177e4SLinus Torvalds /* rfc793:
64441da177e4SLinus Torvalds * "If the state is SYN-SENT then
64451da177e4SLinus Torvalds * first check the ACK bit
64461da177e4SLinus Torvalds * If the ACK bit is set
64471da177e4SLinus Torvalds * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
64481da177e4SLinus Torvalds * a reset (unless the RST bit is set, if so drop
64491da177e4SLinus Torvalds * the segment and return)"
64501da177e4SLinus Torvalds */
64518e4178c1SYuchung Cheng if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
64529603d47bSSeongJae Park after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
64539603d47bSSeongJae Park /* Previous FIN/ACK or RST/ACK might be ignored. */
64549603d47bSSeongJae Park if (icsk->icsk_retransmits == 0)
64559603d47bSSeongJae Park inet_csk_reset_xmit_timer(sk,
64569603d47bSSeongJae Park ICSK_TIME_RETRANS,
64579603d47bSSeongJae Park TCP_TIMEOUT_MIN, TCP_RTO_MAX);
6458e615e3a2SJason Xing SKB_DR_SET(reason, TCP_INVALID_ACK_SEQUENCE);
64591da177e4SLinus Torvalds goto reset_and_undo;
64609603d47bSSeongJae Park }
64611da177e4SLinus Torvalds
64621da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
64631da177e4SLinus Torvalds !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp,
64649d0c00f5SEric Dumazet tcp_time_stamp_ts(tp))) {
6465c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk),
646602a1d6e7SEric Dumazet LINUX_MIB_PAWSACTIVEREJECTED);
6467e615e3a2SJason Xing SKB_DR_SET(reason, TCP_RFC7323_PAWS);
64681da177e4SLinus Torvalds goto reset_and_undo;
64691da177e4SLinus Torvalds }
64701da177e4SLinus Torvalds
64711da177e4SLinus Torvalds /* Now ACK is acceptable.
64721da177e4SLinus Torvalds *
64731da177e4SLinus Torvalds * "If the RST bit is set
64741da177e4SLinus Torvalds * If the ACK was acceptable then signal the user "error:
64751da177e4SLinus Torvalds * connection reset", drop the segment, enter CLOSED state,
64761da177e4SLinus Torvalds * delete TCB, and return."
64771da177e4SLinus Torvalds */
64781da177e4SLinus Torvalds
64791da177e4SLinus Torvalds if (th->rst) {
6480049fe386SFlorian Westphal tcp_reset(sk, skb);
6481c337578aSEric Dumazet consume:
6482c337578aSEric Dumazet __kfree_skb(skb);
6483c337578aSEric Dumazet return 0;
64841da177e4SLinus Torvalds }
64851da177e4SLinus Torvalds
64861da177e4SLinus Torvalds /* rfc793:
64871da177e4SLinus Torvalds * "fifth, if neither of the SYN or RST bits is set then
64881da177e4SLinus Torvalds * drop the segment and return."
64891da177e4SLinus Torvalds *
64901da177e4SLinus Torvalds * See note below!
64911da177e4SLinus Torvalds * --ANK(990513)
64921da177e4SLinus Torvalds */
6493659affdbSEric Dumazet if (!th->syn) {
6494659affdbSEric Dumazet SKB_DR_SET(reason, TCP_FLAGS);
64951da177e4SLinus Torvalds goto discard_and_undo;
6496659affdbSEric Dumazet }
64971da177e4SLinus Torvalds /* rfc793:
64981da177e4SLinus Torvalds * "If the SYN bit is on ...
64991da177e4SLinus Torvalds * are acceptable then ...
65001da177e4SLinus Torvalds * (our SYN has been ACKed), change the connection
65011da177e4SLinus Torvalds * state to ESTABLISHED..."
65021da177e4SLinus Torvalds */
65031da177e4SLinus Torvalds
6504735d3831SFlorian Westphal tcp_ecn_rcv_synack(tp, th);
65051da177e4SLinus Torvalds
65061b3a6926SRazvan Ghitulete tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
65077c1f0815SYuchung Cheng tcp_try_undo_spurious_syn(sk);
650831770e34SFlorian Westphal tcp_ack(sk, skb, FLAG_SLOWPATH);
65091da177e4SLinus Torvalds
65101da177e4SLinus Torvalds /* Ok.. it's good. Set up sequence numbers and
65111da177e4SLinus Torvalds * move to established.
65121da177e4SLinus Torvalds */
6513dba7d9b8SEric Dumazet WRITE_ONCE(tp->rcv_nxt, TCP_SKB_CB(skb)->seq + 1);
65141da177e4SLinus Torvalds tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
65151da177e4SLinus Torvalds
65161da177e4SLinus Torvalds /* RFC1323: The window in SYN & SYN/ACK segments is
65171da177e4SLinus Torvalds * never scaled.
65181da177e4SLinus Torvalds */
65191da177e4SLinus Torvalds tp->snd_wnd = ntohs(th->window);
65201da177e4SLinus Torvalds
65211da177e4SLinus Torvalds if (!tp->rx_opt.wscale_ok) {
65221da177e4SLinus Torvalds tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0;
6523f410cbeaSEric Dumazet WRITE_ONCE(tp->window_clamp,
6524f410cbeaSEric Dumazet min(tp->window_clamp, 65535U));
65251da177e4SLinus Torvalds }
65261da177e4SLinus Torvalds
65271da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp) {
65281da177e4SLinus Torvalds tp->rx_opt.tstamp_ok = 1;
65291da177e4SLinus Torvalds tp->tcp_header_len =
65301da177e4SLinus Torvalds sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
65311da177e4SLinus Torvalds tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
65321da177e4SLinus Torvalds tcp_store_ts_recent(tp);
65331da177e4SLinus Torvalds } else {
65341da177e4SLinus Torvalds tp->tcp_header_len = sizeof(struct tcphdr);
65351da177e4SLinus Torvalds }
65361da177e4SLinus Torvalds
6537d83d8461SArnaldo Carvalho de Melo tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
65381da177e4SLinus Torvalds tcp_initialize_rcv_mss(sk);
65391da177e4SLinus Torvalds
65401da177e4SLinus Torvalds /* Remember, tcp_poll() does not lock socket!
65411da177e4SLinus Torvalds * Change state from SYN-SENT only after copied_seq
65421da177e4SLinus Torvalds * is initialized. */
65437db48e98SEric Dumazet WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
65444957faadSWilliam Allen Simpson
654560e2a778SUrsula Braun smc_check_reset_syn(tp);
654660e2a778SUrsula Braun
6547e16aa207SRalf Baechle smp_mb();
65481da177e4SLinus Torvalds
6549370816aeSPavel Emelyanov tcp_finish_connect(sk, skb);
65501da177e4SLinus Torvalds
65510f9fa831SEric Dumazet fastopen_fail = (tp->syn_fastopen || tp->syn_data) &&
65520f9fa831SEric Dumazet tcp_rcv_fastopen_synack(sk, skb, &foc);
65538e4178c1SYuchung Cheng
65540f9fa831SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) {
65550f9fa831SEric Dumazet sk->sk_state_change(sk);
65560f9fa831SEric Dumazet sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
65570f9fa831SEric Dumazet }
65580f9fa831SEric Dumazet if (fastopen_fail)
65590f9fa831SEric Dumazet return -1;
6560295f7324SArnaldo Carvalho de Melo if (sk->sk_write_pending ||
65616e97ba55SEric Dumazet READ_ONCE(icsk->icsk_accept_queue.rskq_defer_accept) ||
656231954cd8SWei Wang inet_csk_in_pingpong_mode(sk)) {
65631da177e4SLinus Torvalds /* Save one ACK. Data will be ready after
65641da177e4SLinus Torvalds * several ticks, if write_pending is set.
65651da177e4SLinus Torvalds *
65661da177e4SLinus Torvalds * It may be deleted, but with this feature tcpdumps
65671da177e4SLinus Torvalds * look so _wonderfully_ clever, that I was not able
65681da177e4SLinus Torvalds * to stand against the temptation 8) --ANK
65691da177e4SLinus Torvalds */
6570463c84b9SArnaldo Carvalho de Melo inet_csk_schedule_ack(sk);
65719a9c9b51SEric Dumazet tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
65723f421baaSArnaldo Carvalho de Melo inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
65733f421baaSArnaldo Carvalho de Melo TCP_DELACK_MAX, TCP_RTO_MAX);
6574c337578aSEric Dumazet goto consume;
65751da177e4SLinus Torvalds }
6576c337578aSEric Dumazet tcp_send_ack(sk);
65771da177e4SLinus Torvalds return -1;
65781da177e4SLinus Torvalds }
65791da177e4SLinus Torvalds
65801da177e4SLinus Torvalds /* No ACK in the segment */
65811da177e4SLinus Torvalds
65821da177e4SLinus Torvalds if (th->rst) {
65831da177e4SLinus Torvalds /* rfc793:
65841da177e4SLinus Torvalds * "If the RST bit is set
65851da177e4SLinus Torvalds *
65861da177e4SLinus Torvalds * Otherwise (no ACK) drop the segment and return."
65871da177e4SLinus Torvalds */
6588659affdbSEric Dumazet SKB_DR_SET(reason, TCP_RESET);
65891da177e4SLinus Torvalds goto discard_and_undo;
65901da177e4SLinus Torvalds }
65911da177e4SLinus Torvalds
65921da177e4SLinus Torvalds /* PAWS check. */
6593056834d9SIlpo Järvinen if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp &&
6594659affdbSEric Dumazet tcp_paws_reject(&tp->rx_opt, 0)) {
6595659affdbSEric Dumazet SKB_DR_SET(reason, TCP_RFC7323_PAWS);
65961da177e4SLinus Torvalds goto discard_and_undo;
6597659affdbSEric Dumazet }
65981da177e4SLinus Torvalds if (th->syn) {
65991da177e4SLinus Torvalds /* We see SYN without ACK. It is attempt of
66001da177e4SLinus Torvalds * simultaneous connect with crossed SYNs.
66011da177e4SLinus Torvalds * Particularly, it can be connect to self.
66021da177e4SLinus Torvalds */
660364382c71SDmitry Safonov #ifdef CONFIG_TCP_AO
660464382c71SDmitry Safonov struct tcp_ao_info *ao;
660564382c71SDmitry Safonov
660664382c71SDmitry Safonov ao = rcu_dereference_protected(tp->ao_info,
660764382c71SDmitry Safonov lockdep_sock_is_held(sk));
660864382c71SDmitry Safonov if (ao) {
660964382c71SDmitry Safonov WRITE_ONCE(ao->risn, th->seq);
661064382c71SDmitry Safonov ao->rcv_sne = 0;
661164382c71SDmitry Safonov }
661264382c71SDmitry Safonov #endif
66131da177e4SLinus Torvalds tcp_set_state(sk, TCP_SYN_RECV);
66141da177e4SLinus Torvalds
66151da177e4SLinus Torvalds if (tp->rx_opt.saw_tstamp) {
66161da177e4SLinus Torvalds tp->rx_opt.tstamp_ok = 1;
66171da177e4SLinus Torvalds tcp_store_ts_recent(tp);
66181da177e4SLinus Torvalds tp->tcp_header_len =
66191da177e4SLinus Torvalds sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
66201da177e4SLinus Torvalds } else {
66211da177e4SLinus Torvalds tp->tcp_header_len = sizeof(struct tcphdr);
66221da177e4SLinus Torvalds }
66231da177e4SLinus Torvalds
6624dba7d9b8SEric Dumazet WRITE_ONCE(tp->rcv_nxt, TCP_SKB_CB(skb)->seq + 1);
66257db48e98SEric Dumazet WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
66261da177e4SLinus Torvalds tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
66271da177e4SLinus Torvalds
66281da177e4SLinus Torvalds /* RFC1323: The window in SYN & SYN/ACK segments is
66291da177e4SLinus Torvalds * never scaled.
66301da177e4SLinus Torvalds */
66311da177e4SLinus Torvalds tp->snd_wnd = ntohs(th->window);
66321da177e4SLinus Torvalds tp->snd_wl1 = TCP_SKB_CB(skb)->seq;
66331da177e4SLinus Torvalds tp->max_window = tp->snd_wnd;
66341da177e4SLinus Torvalds
6635735d3831SFlorian Westphal tcp_ecn_rcv_syn(tp, th);
66361da177e4SLinus Torvalds
66375d424d5aSJohn Heffner tcp_mtup_init(sk);
6638d83d8461SArnaldo Carvalho de Melo tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
66391da177e4SLinus Torvalds tcp_initialize_rcv_mss(sk);
66401da177e4SLinus Torvalds
66411da177e4SLinus Torvalds tcp_send_synack(sk);
66421da177e4SLinus Torvalds #if 0
66431da177e4SLinus Torvalds /* Note, we could accept data and URG from this segment.
6644168a8f58SJerry Chu * There are no obstacles to make this (except that we must
6645168a8f58SJerry Chu * either change tcp_recvmsg() to prevent it from returning data
6646168a8f58SJerry Chu * before 3WHS completes per RFC793, or employ TCP Fast Open).
66471da177e4SLinus Torvalds *
66481da177e4SLinus Torvalds * However, if we ignore data in ACKless segments sometimes,
66491da177e4SLinus Torvalds * we have no reasons to accept it sometimes.
66501da177e4SLinus Torvalds * Also, seems the code doing it in step6 of tcp_rcv_state_process
66511da177e4SLinus Torvalds * is not flawless. So, discard packet for sanity.
66521da177e4SLinus Torvalds * Uncomment this return to process the data.
66531da177e4SLinus Torvalds */
66541da177e4SLinus Torvalds return -1;
66551da177e4SLinus Torvalds #else
6656c337578aSEric Dumazet goto consume;
66571da177e4SLinus Torvalds #endif
66581da177e4SLinus Torvalds }
66591da177e4SLinus Torvalds /* "fifth, if neither of the SYN or RST bits is set then
66601da177e4SLinus Torvalds * drop the segment and return."
66611da177e4SLinus Torvalds */
66621da177e4SLinus Torvalds
66631da177e4SLinus Torvalds discard_and_undo:
66641da177e4SLinus Torvalds tcp_clear_options(&tp->rx_opt);
66651da177e4SLinus Torvalds tp->rx_opt.mss_clamp = saved_clamp;
6666659affdbSEric Dumazet tcp_drop_reason(sk, skb, reason);
6667c337578aSEric Dumazet return 0;
66681da177e4SLinus Torvalds
66691da177e4SLinus Torvalds reset_and_undo:
66701da177e4SLinus Torvalds tcp_clear_options(&tp->rx_opt);
66711da177e4SLinus Torvalds tp->rx_opt.mss_clamp = saved_clamp;
6672e615e3a2SJason Xing /* we can reuse/return @reason to its caller to handle the exception */
6673e615e3a2SJason Xing return reason;
66741da177e4SLinus Torvalds }
66751da177e4SLinus Torvalds
tcp_rcv_synrecv_state_fastopen(struct sock * sk)66766b94b1c8SYuchung Cheng static void tcp_rcv_synrecv_state_fastopen(struct sock *sk)
66776b94b1c8SYuchung Cheng {
6678e326578aSAananth V struct tcp_sock *tp = tcp_sk(sk);
6679d983ea6fSEric Dumazet struct request_sock *req;
6680d983ea6fSEric Dumazet
6681dad8cea7SNeal Cardwell /* If we are still handling the SYNACK RTO, see if timestamp ECR allows
6682dad8cea7SNeal Cardwell * undo. If peer SACKs triggered fast recovery, we can't undo here.
6683dad8cea7SNeal Cardwell */
6684e326578aSAananth V if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss && !tp->packets_out)
6685e326578aSAananth V tcp_try_undo_recovery(sk);
6686cd736d8bSYuchung Cheng
66873868ab0fSAananth V tcp_update_rto_time(tp);
66886b94b1c8SYuchung Cheng inet_csk(sk)->icsk_retransmits = 0;
6689*27c80efcSNeal Cardwell /* In tcp_fastopen_synack_timer() on the first SYNACK RTO we set
6690*27c80efcSNeal Cardwell * retrans_stamp but don't enter CA_Loss, so in case that happened we
6691*27c80efcSNeal Cardwell * need to zero retrans_stamp here to prevent spurious
6692*27c80efcSNeal Cardwell * retransmits_timed_out(). However, if the ACK of our SYNACK caused us
6693*27c80efcSNeal Cardwell * to enter CA_Recovery then we need to leave retrans_stamp as it was
6694*27c80efcSNeal Cardwell * set entering CA_Recovery, for correct retransmits_timed_out() and
6695*27c80efcSNeal Cardwell * undo behavior.
6696*27c80efcSNeal Cardwell */
6697*27c80efcSNeal Cardwell tcp_retrans_stamp_cleanup(sk);
66986b94b1c8SYuchung Cheng
66996b94b1c8SYuchung Cheng /* Once we leave TCP_SYN_RECV or TCP_FIN_WAIT_1,
67006b94b1c8SYuchung Cheng * we no longer need req so release it.
67016b94b1c8SYuchung Cheng */
6702e326578aSAananth V req = rcu_dereference_protected(tp->fastopen_rsk,
6703d983ea6fSEric Dumazet lockdep_sock_is_held(sk));
6704d983ea6fSEric Dumazet reqsk_fastopen_remove(sk, req, false);
67056b94b1c8SYuchung Cheng
67066b94b1c8SYuchung Cheng /* Re-arm the timer because data may have been sent out.
67076b94b1c8SYuchung Cheng * This is similar to the regular data transmission case
67086b94b1c8SYuchung Cheng * when new data has just been ack'ed.
67096b94b1c8SYuchung Cheng *
67106b94b1c8SYuchung Cheng * (TFO) - we could try to be more aggressive and
67116b94b1c8SYuchung Cheng * retransmitting any data sooner based on when they
67126b94b1c8SYuchung Cheng * are sent out.
67136b94b1c8SYuchung Cheng */
67146b94b1c8SYuchung Cheng tcp_rearm_rto(sk);
67156b94b1c8SYuchung Cheng }
67166b94b1c8SYuchung Cheng
67171da177e4SLinus Torvalds /*
67181da177e4SLinus Torvalds * This function implements the receiving procedure of RFC 793 for
67191da177e4SLinus Torvalds * all states except ESTABLISHED and TIME_WAIT.
67201da177e4SLinus Torvalds * It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
67211da177e4SLinus Torvalds * address independent.
67221da177e4SLinus Torvalds */
67231da177e4SLinus Torvalds
67247d6ed9afSJason Xing enum skb_drop_reason
tcp_rcv_state_process(struct sock * sk,struct sk_buff * skb)67257d6ed9afSJason Xing tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
67261da177e4SLinus Torvalds {
67271da177e4SLinus Torvalds struct tcp_sock *tp = tcp_sk(sk);
67288292a17aSArnaldo Carvalho de Melo struct inet_connection_sock *icsk = inet_csk(sk);
672972ab4a86SEric Dumazet const struct tcphdr *th = tcp_hdr(skb);
6730168a8f58SJerry Chu struct request_sock *req;
67311da177e4SLinus Torvalds int queued = 0;
6732669da7a7SEric Dumazet SKB_DR(reason);
67331da177e4SLinus Torvalds
67341da177e4SLinus Torvalds switch (sk->sk_state) {
67351da177e4SLinus Torvalds case TCP_CLOSE:
6736669da7a7SEric Dumazet SKB_DR_SET(reason, TCP_CLOSE);
67371da177e4SLinus Torvalds goto discard;
67381da177e4SLinus Torvalds
67391da177e4SLinus Torvalds case TCP_LISTEN:
67401da177e4SLinus Torvalds if (th->ack)
67417d6ed9afSJason Xing return SKB_DROP_REASON_TCP_FLAGS;
67421da177e4SLinus Torvalds
6743669da7a7SEric Dumazet if (th->rst) {
6744669da7a7SEric Dumazet SKB_DR_SET(reason, TCP_RESET);
67451da177e4SLinus Torvalds goto discard;
6746669da7a7SEric Dumazet }
67471da177e4SLinus Torvalds if (th->syn) {
6748669da7a7SEric Dumazet if (th->fin) {
6749669da7a7SEric Dumazet SKB_DR_SET(reason, TCP_FLAGS);
6750fdf5af0dSEric Dumazet goto discard;
6751669da7a7SEric Dumazet }
6752449809a6SEric Dumazet /* It is possible that we process SYN packets from backlog,
67531ad98e9dSEric Dumazet * so we need to make sure to disable BH and RCU right there.
6754449809a6SEric Dumazet */
67551ad98e9dSEric Dumazet rcu_read_lock();
6756449809a6SEric Dumazet local_bh_disable();
6757d25f3272SJason Xing icsk->icsk_af_ops->conn_request(sk, skb);
6758449809a6SEric Dumazet local_bh_enable();
67591ad98e9dSEric Dumazet rcu_read_unlock();
67601da177e4SLinus Torvalds
67610aea76d3SEric Dumazet consume_skb(skb);
6762fb7e2399SMasayuki Nakagawa return 0;
67631da177e4SLinus Torvalds }
6764669da7a7SEric Dumazet SKB_DR_SET(reason, TCP_FLAGS);
67651da177e4SLinus Torvalds goto discard;
67661da177e4SLinus Torvalds
67671da177e4SLinus Torvalds case TCP_SYN_SENT:
67688804b272SEric Dumazet tp->rx_opt.saw_tstamp = 0;
67699a568de4SEric Dumazet tcp_mstamp_refresh(tp);
6770bda07a64SEric Dumazet queued = tcp_rcv_synsent_state_process(sk, skb, th);
67711da177e4SLinus Torvalds if (queued >= 0)
67721da177e4SLinus Torvalds return queued;
67731da177e4SLinus Torvalds
67741da177e4SLinus Torvalds /* Do step6 onward by hand. */
67751da177e4SLinus Torvalds tcp_urg(sk, skb, th);
67761da177e4SLinus Torvalds __kfree_skb(skb);
67779e412ba7SIlpo Järvinen tcp_data_snd_check(sk);
67781da177e4SLinus Torvalds return 0;
67791da177e4SLinus Torvalds }
67801da177e4SLinus Torvalds
67819a568de4SEric Dumazet tcp_mstamp_refresh(tp);
67828804b272SEric Dumazet tp->rx_opt.saw_tstamp = 0;
6783d983ea6fSEric Dumazet req = rcu_dereference_protected(tp->fastopen_rsk,
6784d983ea6fSEric Dumazet lockdep_sock_is_held(sk));
678500db4124SIan Morris if (req) {
6786e0f9759fSEric Dumazet bool req_stolen;
6787e0f9759fSEric Dumazet
678837561f68SJerry Chu WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
6789168a8f58SJerry Chu sk->sk_state != TCP_FIN_WAIT1);
6790168a8f58SJerry Chu
6791669da7a7SEric Dumazet if (!tcp_check_req(sk, skb, req, true, &req_stolen)) {
6792669da7a7SEric Dumazet SKB_DR_SET(reason, TCP_FASTOPEN);
6793168a8f58SJerry Chu goto discard;
6794e69bebdeSNeal Cardwell }
6795669da7a7SEric Dumazet }
6796c3ae62afSEric Dumazet
6797669da7a7SEric Dumazet if (!th->ack && !th->rst && !th->syn) {
6798669da7a7SEric Dumazet SKB_DR_SET(reason, TCP_FLAGS);
6799c3ae62afSEric Dumazet goto discard;
6800669da7a7SEric Dumazet }
6801e69bebdeSNeal Cardwell if (!tcp_validate_incoming(sk, skb, th, 0))
68020c24604bSEric Dumazet return 0;
68031da177e4SLinus Torvalds
68041da177e4SLinus Torvalds /* step 5: check the ACK field */
6805795a7dfbSMenglong Dong reason = tcp_ack(sk, skb, FLAG_SLOWPATH |
680631770e34SFlorian Westphal FLAG_UPDATE_TS_RECENT |
6807795a7dfbSMenglong Dong FLAG_NO_CHALLENGE_ACK);
68081da177e4SLinus Torvalds
6809795a7dfbSMenglong Dong if ((int)reason <= 0) {
68107d6ed9afSJason Xing if (sk->sk_state == TCP_SYN_RECV) {
68117d6ed9afSJason Xing /* send one RST */
68127d6ed9afSJason Xing if (!reason)
68137d6ed9afSJason Xing return SKB_DROP_REASON_TCP_OLD_ACK;
68147d6ed9afSJason Xing return -reason;
68157d6ed9afSJason Xing }
6816795a7dfbSMenglong Dong /* accept old ack during closing */
6817795a7dfbSMenglong Dong if ((int)reason < 0) {
6818208dd45dSBenjamin Yim tcp_send_challenge_ack(sk);
6819795a7dfbSMenglong Dong reason = -reason;
6820d0e1a1b5SEric Dumazet goto discard;
6821d0e1a1b5SEric Dumazet }
6822795a7dfbSMenglong Dong }
6823795a7dfbSMenglong Dong SKB_DR_SET(reason, NOT_SPECIFIED);
68241da177e4SLinus Torvalds switch (sk->sk_state) {
68251da177e4SLinus Torvalds case TCP_SYN_RECV:
6826bef5767fSYuchung Cheng tp->delivered++; /* SYN-ACK delivery isn't tracked in tcp_ack */
68270f1c28aeSYuchung Cheng if (!tp->srtt_us)
68280f1c28aeSYuchung Cheng tcp_synack_rtt_meas(sk, req);
68290f1c28aeSYuchung Cheng
6830168a8f58SJerry Chu if (req) {
68316b94b1c8SYuchung Cheng tcp_rcv_synrecv_state_fastopen(sk);
6832168a8f58SJerry Chu } else {
6833336c39a0SYuchung Cheng tcp_try_undo_spurious_syn(sk);
6834336c39a0SYuchung Cheng tp->retrans_stamp = 0;
683572be0fe6SMartin KaFai Lau tcp_init_transfer(sk, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB,
683672be0fe6SMartin KaFai Lau skb);
68377db48e98SEric Dumazet WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
6838168a8f58SJerry Chu }
68397c2ffaf2SDmitry Safonov tcp_ao_established(sk);
6840e16aa207SRalf Baechle smp_mb();
68411da177e4SLinus Torvalds tcp_set_state(sk, TCP_ESTABLISHED);
68421da177e4SLinus Torvalds sk->sk_state_change(sk);
68431da177e4SLinus Torvalds
684461eb9003SJoe Perches /* Note, that this wakeup is only for marginal crossed SYN case.
684561eb9003SJoe Perches * Passively open sockets are not waked up, because
684661eb9003SJoe Perches * sk->sk_sleep == NULL and sk->sk_socket == NULL.
68471da177e4SLinus Torvalds */
68488d8ad9d7SPavel Emelyanov if (sk->sk_socket)
68491f6afc81SEric Dumazet sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
68501da177e4SLinus Torvalds
68511da177e4SLinus Torvalds tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
685261eb9003SJoe Perches tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
6853ee7537b6SHantzis Fotis tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
68541da177e4SLinus Torvalds
68551da177e4SLinus Torvalds if (tp->rx_opt.tstamp_ok)
68561da177e4SLinus Torvalds tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
68571da177e4SLinus Torvalds
6858c0402760SYuchung Cheng if (!inet_csk(sk)->icsk_ca_ops->cong_control)
685902cf4ebdSNeal Cardwell tcp_update_pacing_rate(sk);
686002cf4ebdSNeal Cardwell
686161eb9003SJoe Perches /* Prevent spurious tcp_cwnd_restart() on first data packet */
6862d635fbe2SEric Dumazet tp->lsndtime = tcp_jiffies32;
68631da177e4SLinus Torvalds
68641da177e4SLinus Torvalds tcp_initialize_rcv_mss(sk);
686531770e34SFlorian Westphal tcp_fast_path_on(tp);
686694062790SEric Dumazet if (sk->sk_shutdown & SEND_SHUTDOWN)
686794062790SEric Dumazet tcp_shutdown(sk, SEND_SHUTDOWN);
68681da177e4SLinus Torvalds break;
68691da177e4SLinus Torvalds
6870c48b22daSJoe Perches case TCP_FIN_WAIT1: {
6871c48b22daSJoe Perches int tmo;
6872c48b22daSJoe Perches
68736b94b1c8SYuchung Cheng if (req)
68746b94b1c8SYuchung Cheng tcp_rcv_synrecv_state_fastopen(sk);
68756b94b1c8SYuchung Cheng
6876c48b22daSJoe Perches if (tp->snd_una != tp->write_seq)
6877c48b22daSJoe Perches break;
68785110effeSDavid S. Miller
68791da177e4SLinus Torvalds tcp_set_state(sk, TCP_FIN_WAIT2);
6880e14cadfdSEric Dumazet WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | SEND_SHUTDOWN);
68815110effeSDavid S. Miller
6882c3a2e837SJulian Anastasov sk_dst_confirm(sk);
68831da177e4SLinus Torvalds
68841f6afc81SEric Dumazet if (!sock_flag(sk, SOCK_DEAD)) {
68851da177e4SLinus Torvalds /* Wake up lingering close() */
68861da177e4SLinus Torvalds sk->sk_state_change(sk);
6887c48b22daSJoe Perches break;
6888c48b22daSJoe Perches }
68891da177e4SLinus Torvalds
6890a81722ddSEric Dumazet if (READ_ONCE(tp->linger2) < 0) {
6891cf1ef3f0SWei Wang tcp_done(sk);
6892cf1ef3f0SWei Wang NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
68937d6ed9afSJason Xing return SKB_DROP_REASON_TCP_ABORT_ON_DATA;
6894cf1ef3f0SWei Wang }
6895cf1ef3f0SWei Wang if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
6896cf1ef3f0SWei Wang after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
6897cf1ef3f0SWei Wang /* Receive out of order FIN after close() */
6898cf1ef3f0SWei Wang if (tp->syn_fastopen && th->fin)
689946c2fa39SWei Wang tcp_fastopen_active_disable(sk);
69001da177e4SLinus Torvalds tcp_done(sk);
6901c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
69027d6ed9afSJason Xing return SKB_DROP_REASON_TCP_ABORT_ON_DATA;
69031da177e4SLinus Torvalds }
69041da177e4SLinus Torvalds
6905463c84b9SArnaldo Carvalho de Melo tmo = tcp_fin_time(sk);
69061da177e4SLinus Torvalds if (tmo > TCP_TIMEWAIT_LEN) {
6907463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
69081da177e4SLinus Torvalds } else if (th->fin || sock_owned_by_user(sk)) {
69091da177e4SLinus Torvalds /* Bad case. We could lose such FIN otherwise.
69101da177e4SLinus Torvalds * It is not a big problem, but it looks confusing
69111da177e4SLinus Torvalds * and not so rare event. We still can lose it now,
69121da177e4SLinus Torvalds * if it spins in bh_lock_sock(), but it is really
69131da177e4SLinus Torvalds * marginal case.
69141da177e4SLinus Torvalds */
6915463c84b9SArnaldo Carvalho de Melo inet_csk_reset_keepalive_timer(sk, tmo);
69161da177e4SLinus Torvalds } else {
69171da177e4SLinus Torvalds tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
691837fd4e84SEric Dumazet goto consume;
69191da177e4SLinus Torvalds }
69201da177e4SLinus Torvalds break;
6921c48b22daSJoe Perches }
69221da177e4SLinus Torvalds
69231da177e4SLinus Torvalds case TCP_CLOSING:
69241da177e4SLinus Torvalds if (tp->snd_una == tp->write_seq) {
69251da177e4SLinus Torvalds tcp_time_wait(sk, TCP_TIME_WAIT, 0);
692637fd4e84SEric Dumazet goto consume;
69271da177e4SLinus Torvalds }
69281da177e4SLinus Torvalds break;
69291da177e4SLinus Torvalds
69301da177e4SLinus Torvalds case TCP_LAST_ACK:
69311da177e4SLinus Torvalds if (tp->snd_una == tp->write_seq) {
69321da177e4SLinus Torvalds tcp_update_metrics(sk);
69331da177e4SLinus Torvalds tcp_done(sk);
693437fd4e84SEric Dumazet goto consume;
69351da177e4SLinus Torvalds }
69361da177e4SLinus Torvalds break;
69371da177e4SLinus Torvalds }
69381da177e4SLinus Torvalds
69391da177e4SLinus Torvalds /* step 6: check the URG bit */
69401da177e4SLinus Torvalds tcp_urg(sk, skb, th);
69411da177e4SLinus Torvalds
69421da177e4SLinus Torvalds /* step 7: process the segment text */
69431da177e4SLinus Torvalds switch (sk->sk_state) {
69441da177e4SLinus Torvalds case TCP_CLOSE_WAIT:
69451da177e4SLinus Torvalds case TCP_CLOSING:
69461da177e4SLinus Torvalds case TCP_LAST_ACK:
6947648ef4b8SMat Martineau if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
69486787b7e3SJianguo Wu /* If a subflow has been reset, the packet should not
69496787b7e3SJianguo Wu * continue to be processed, drop the packet.
69506787b7e3SJianguo Wu */
69516787b7e3SJianguo Wu if (sk_is_mptcp(sk) && !mptcp_incoming_options(sk, skb))
69526787b7e3SJianguo Wu goto discard;
69531da177e4SLinus Torvalds break;
6954648ef4b8SMat Martineau }
6955a8eceea8SJoe Perches fallthrough;
69561da177e4SLinus Torvalds case TCP_FIN_WAIT1:
69571da177e4SLinus Torvalds case TCP_FIN_WAIT2:
69581da177e4SLinus Torvalds /* RFC 793 says to queue data in these states,
69591da177e4SLinus Torvalds * RFC 1122 says we MUST send a reset.
69601da177e4SLinus Torvalds * BSD 4.4 also does reset.
69611da177e4SLinus Torvalds */
69621da177e4SLinus Torvalds if (sk->sk_shutdown & RCV_SHUTDOWN) {
69631da177e4SLinus Torvalds if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
69641da177e4SLinus Torvalds after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
6965c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
6966049fe386SFlorian Westphal tcp_reset(sk, skb);
69677d6ed9afSJason Xing return SKB_DROP_REASON_TCP_ABORT_ON_DATA;
69681da177e4SLinus Torvalds }
69691da177e4SLinus Torvalds }
6970a8eceea8SJoe Perches fallthrough;
69711da177e4SLinus Torvalds case TCP_ESTABLISHED:
69721da177e4SLinus Torvalds tcp_data_queue(sk, skb);
69731da177e4SLinus Torvalds queued = 1;
69741da177e4SLinus Torvalds break;
69751da177e4SLinus Torvalds }
69761da177e4SLinus Torvalds
69771da177e4SLinus Torvalds /* tcp_data could move socket to TIME-WAIT */
69781da177e4SLinus Torvalds if (sk->sk_state != TCP_CLOSE) {
69799e412ba7SIlpo Järvinen tcp_data_snd_check(sk);
69801da177e4SLinus Torvalds tcp_ack_snd_check(sk);
69811da177e4SLinus Torvalds }
69821da177e4SLinus Torvalds
69831da177e4SLinus Torvalds if (!queued) {
69841da177e4SLinus Torvalds discard:
6985669da7a7SEric Dumazet tcp_drop_reason(sk, skb, reason);
69861da177e4SLinus Torvalds }
69871da177e4SLinus Torvalds return 0;
698837fd4e84SEric Dumazet
698937fd4e84SEric Dumazet consume:
699037fd4e84SEric Dumazet __kfree_skb(skb);
699137fd4e84SEric Dumazet return 0;
69921da177e4SLinus Torvalds }
69931da177e4SLinus Torvalds EXPORT_SYMBOL(tcp_rcv_state_process);
69941fb6f159SOctavian Purdila
pr_drop_req(struct request_sock * req,__u16 port,int family)69951fb6f159SOctavian Purdila static inline void pr_drop_req(struct request_sock *req, __u16 port, int family)
69961fb6f159SOctavian Purdila {
69971fb6f159SOctavian Purdila struct inet_request_sock *ireq = inet_rsk(req);
69981fb6f159SOctavian Purdila
69991fb6f159SOctavian Purdila if (family == AF_INET)
7000ba7a46f1SJoe Perches net_dbg_ratelimited("drop open request from %pI4/%u\n",
70011fb6f159SOctavian Purdila &ireq->ir_rmt_addr, port);
70024135ab82SOctavian Purdila #if IS_ENABLED(CONFIG_IPV6)
70034135ab82SOctavian Purdila else if (family == AF_INET6)
7004ba7a46f1SJoe Perches net_dbg_ratelimited("drop open request from %pI6/%u\n",
70051fb6f159SOctavian Purdila &ireq->ir_v6_rmt_addr, port);
70064135ab82SOctavian Purdila #endif
70071fb6f159SOctavian Purdila }
70081fb6f159SOctavian Purdila
7009d82bd122SFlorian Westphal /* RFC3168 : 6.1.1 SYN packets must not have ECT/ECN bits set
7010d82bd122SFlorian Westphal *
7011d82bd122SFlorian Westphal * If we receive a SYN packet with these bits set, it means a
7012d82bd122SFlorian Westphal * network is playing bad games with TOS bits. In order to
7013d82bd122SFlorian Westphal * avoid possible false congestion notifications, we disable
7014f4e715c3Sstephen hemminger * TCP ECN negotiation.
7015d82bd122SFlorian Westphal *
7016d82bd122SFlorian Westphal * Exception: tcp_ca wants ECN. This is required for DCTCP
7017843c2fdfSFlorian Westphal * congestion control: Linux DCTCP asserts ECT on all packets,
7018843c2fdfSFlorian Westphal * including SYN, which is most optimal solution; however,
7019843c2fdfSFlorian Westphal * others, such as FreeBSD do not.
7020f6fee16dSTilmans, Olivier (Nokia - BE/Antwerp) *
7021f6fee16dSTilmans, Olivier (Nokia - BE/Antwerp) * Exception: At least one of the reserved bits of the TCP header (th->res1) is
7022f6fee16dSTilmans, Olivier (Nokia - BE/Antwerp) * set, indicating the use of a future TCP extension (such as AccECN). See
7023f6fee16dSTilmans, Olivier (Nokia - BE/Antwerp) * RFC8311 §4.3 which updates RFC3168 to allow the development of such
7024f6fee16dSTilmans, Olivier (Nokia - BE/Antwerp) * extensions.
7025d82bd122SFlorian Westphal */
tcp_ecn_create_request(struct request_sock * req,const struct sk_buff * skb,const struct sock * listen_sk,const struct dst_entry * dst)7026d82bd122SFlorian Westphal static void tcp_ecn_create_request(struct request_sock *req,
7027d82bd122SFlorian Westphal const struct sk_buff *skb,
7028f7b3bec6SFlorian Westphal const struct sock *listen_sk,
7029f7b3bec6SFlorian Westphal const struct dst_entry *dst)
7030d82bd122SFlorian Westphal {
7031d82bd122SFlorian Westphal const struct tcphdr *th = tcp_hdr(skb);
7032d82bd122SFlorian Westphal const struct net *net = sock_net(listen_sk);
7033d82bd122SFlorian Westphal bool th_ecn = th->ece && th->cwr;
7034843c2fdfSFlorian Westphal bool ect, ecn_ok;
7035c3a8d947SDaniel Borkmann u32 ecn_ok_dst;
7036d82bd122SFlorian Westphal
7037d82bd122SFlorian Westphal if (!th_ecn)
7038d82bd122SFlorian Westphal return;
7039d82bd122SFlorian Westphal
7040d82bd122SFlorian Westphal ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
7041c3a8d947SDaniel Borkmann ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
70424785a667SKuniyuki Iwashima ecn_ok = READ_ONCE(net->ipv4.sysctl_tcp_ecn) || ecn_ok_dst;
7043d82bd122SFlorian Westphal
7044f6fee16dSTilmans, Olivier (Nokia - BE/Antwerp) if (((!ect || th->res1) && ecn_ok) || tcp_ca_needs_ecn(listen_sk) ||
704591b5b21cSLawrence Brakmo (ecn_ok_dst & DST_FEATURE_ECN_CA) ||
704691b5b21cSLawrence Brakmo tcp_bpf_ca_needs_ecn((struct sock *)req))
7047d82bd122SFlorian Westphal inet_rsk(req)->ecn_ok = 1;
7048d82bd122SFlorian Westphal }
7049d82bd122SFlorian Westphal
tcp_openreq_init(struct request_sock * req,const struct tcp_options_received * rx_opt,struct sk_buff * skb,const struct sock * sk)70501bfc4438SEric Dumazet static void tcp_openreq_init(struct request_sock *req,
70511bfc4438SEric Dumazet const struct tcp_options_received *rx_opt,
70521bfc4438SEric Dumazet struct sk_buff *skb, const struct sock *sk)
70531bfc4438SEric Dumazet {
70541bfc4438SEric Dumazet struct inet_request_sock *ireq = inet_rsk(req);
70551bfc4438SEric Dumazet
7056ed53d0abSEric Dumazet req->rsk_rcv_wnd = 0; /* So that tcp_send_synack() knows! */
70571bfc4438SEric Dumazet tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
70581bfc4438SEric Dumazet tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
70599e450c1eSYuchung Cheng tcp_rsk(req)->snt_synack = 0;
70601bfc4438SEric Dumazet tcp_rsk(req)->last_oow_ack_time = 0;
70611bfc4438SEric Dumazet req->mss = rx_opt->mss_clamp;
70621bfc4438SEric Dumazet req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
70631bfc4438SEric Dumazet ireq->tstamp_ok = rx_opt->tstamp_ok;
70641bfc4438SEric Dumazet ireq->sack_ok = rx_opt->sack_ok;
70651bfc4438SEric Dumazet ireq->snd_wscale = rx_opt->snd_wscale;
70661bfc4438SEric Dumazet ireq->wscale_ok = rx_opt->wscale_ok;
70671bfc4438SEric Dumazet ireq->acked = 0;
70681bfc4438SEric Dumazet ireq->ecn_ok = 0;
70691bfc4438SEric Dumazet ireq->ir_rmt_port = tcp_hdr(skb)->source;
70701bfc4438SEric Dumazet ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
70711bfc4438SEric Dumazet ireq->ir_mark = inet_request_mark(sk, skb);
707260e2a778SUrsula Braun #if IS_ENABLED(CONFIG_SMC)
707348b6190aSD. Wythe ireq->smc_ok = rx_opt->smc_ok && !(tcp_sk(sk)->smc_hs_congested &&
707448b6190aSD. Wythe tcp_sk(sk)->smc_hs_congested(sk));
707560e2a778SUrsula Braun #endif
70761bfc4438SEric Dumazet }
70771bfc4438SEric Dumazet
707841d25fe0SEric Dumazet /*
707941d25fe0SEric Dumazet * Return true if a syncookie should be sent
708041d25fe0SEric Dumazet */
tcp_syn_flood_action(struct sock * sk,const char * proto)708158169ec9SEric Dumazet static bool tcp_syn_flood_action(struct sock *sk, const char *proto)
708241d25fe0SEric Dumazet {
70838d2675f1SEric Dumazet struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
708441d25fe0SEric Dumazet const char *msg = "Dropping request";
708512ed8244SNikolay Borisov struct net *net = sock_net(sk);
7086f2e383b5SKuniyuki Iwashima bool want_cookie = false;
7087f2e383b5SKuniyuki Iwashima u8 syncookies;
7088f2e383b5SKuniyuki Iwashima
7089f2e383b5SKuniyuki Iwashima syncookies = READ_ONCE(net->ipv4.sysctl_tcp_syncookies);
709041d25fe0SEric Dumazet
709141d25fe0SEric Dumazet #ifdef CONFIG_SYN_COOKIES
7092f2e383b5SKuniyuki Iwashima if (syncookies) {
709341d25fe0SEric Dumazet msg = "Sending cookies";
709441d25fe0SEric Dumazet want_cookie = true;
709502a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
709641d25fe0SEric Dumazet } else
709741d25fe0SEric Dumazet #endif
709802a1d6e7SEric Dumazet __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
709941d25fe0SEric Dumazet
7100bf36267eSEric Dumazet if (!READ_ONCE(queue->synflood_warned) && syncookies != 2 &&
7101d9282e48SJamie Bainbridge xchg(&queue->synflood_warned, 1) == 0) {
7102d9282e48SJamie Bainbridge if (IS_ENABLED(CONFIG_IPV6) && sk->sk_family == AF_INET6) {
7103d9282e48SJamie Bainbridge net_info_ratelimited("%s: Possible SYN flooding on port [%pI6c]:%u. %s.\n",
7104c90b6b10SSaeed Mahameed proto, inet6_rcv_saddr(sk),
7105d9282e48SJamie Bainbridge sk->sk_num, msg);
7106d9282e48SJamie Bainbridge } else {
7107d9282e48SJamie Bainbridge net_info_ratelimited("%s: Possible SYN flooding on port %pI4:%u. %s.\n",
7108d9282e48SJamie Bainbridge proto, &sk->sk_rcv_saddr,
7109d9282e48SJamie Bainbridge sk->sk_num, msg);
7110d9282e48SJamie Bainbridge }
7111d9282e48SJamie Bainbridge }
71122985aaacSEric Dumazet
711341d25fe0SEric Dumazet return want_cookie;
711441d25fe0SEric Dumazet }
711541d25fe0SEric Dumazet
tcp_reqsk_record_syn(const struct sock * sk,struct request_sock * req,const struct sk_buff * skb)7116cd8ae852SEric Dumazet static void tcp_reqsk_record_syn(const struct sock *sk,
7117cd8ae852SEric Dumazet struct request_sock *req,
7118cd8ae852SEric Dumazet const struct sk_buff *skb)
7119cd8ae852SEric Dumazet {
7120cd8ae852SEric Dumazet if (tcp_sk(sk)->save_syn) {
7121cd8ae852SEric Dumazet u32 len = skb_network_header_len(skb) + tcp_hdrlen(skb);
712270a217f1SMartin KaFai Lau struct saved_syn *saved_syn;
7123267cf9faSMartin KaFai Lau u32 mac_hdrlen;
7124267cf9faSMartin KaFai Lau void *base;
7125267cf9faSMartin KaFai Lau
7126267cf9faSMartin KaFai Lau if (tcp_sk(sk)->save_syn == 2) { /* Save full header. */
7127267cf9faSMartin KaFai Lau base = skb_mac_header(skb);
7128267cf9faSMartin KaFai Lau mac_hdrlen = skb_mac_header_len(skb);
7129267cf9faSMartin KaFai Lau len += mac_hdrlen;
7130267cf9faSMartin KaFai Lau } else {
7131267cf9faSMartin KaFai Lau base = skb_network_header(skb);
7132267cf9faSMartin KaFai Lau mac_hdrlen = 0;
7133267cf9faSMartin KaFai Lau }
7134cd8ae852SEric Dumazet
713570a217f1SMartin KaFai Lau saved_syn = kmalloc(struct_size(saved_syn, data, len),
713670a217f1SMartin KaFai Lau GFP_ATOMIC);
713770a217f1SMartin KaFai Lau if (saved_syn) {
7138267cf9faSMartin KaFai Lau saved_syn->mac_hdrlen = mac_hdrlen;
713970a217f1SMartin KaFai Lau saved_syn->network_hdrlen = skb_network_header_len(skb);
714070a217f1SMartin KaFai Lau saved_syn->tcp_hdrlen = tcp_hdrlen(skb);
7141267cf9faSMartin KaFai Lau memcpy(saved_syn->data, base, len);
714270a217f1SMartin KaFai Lau req->saved_syn = saved_syn;
7143cd8ae852SEric Dumazet }
7144cd8ae852SEric Dumazet }
7145cd8ae852SEric Dumazet }
7146cd8ae852SEric Dumazet
71479349d600SPetar Penkov /* If a SYN cookie is required and supported, returns a clamped MSS value to be
71489349d600SPetar Penkov * used for SYN cookie generation.
71499349d600SPetar Penkov */
tcp_get_syncookie_mss(struct request_sock_ops * rsk_ops,const struct tcp_request_sock_ops * af_ops,struct sock * sk,struct tcphdr * th)71509349d600SPetar Penkov u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
71519349d600SPetar Penkov const struct tcp_request_sock_ops *af_ops,
71529349d600SPetar Penkov struct sock *sk, struct tcphdr *th)
71539349d600SPetar Penkov {
71549349d600SPetar Penkov struct tcp_sock *tp = tcp_sk(sk);
71559349d600SPetar Penkov u16 mss;
71569349d600SPetar Penkov
7157f2e383b5SKuniyuki Iwashima if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies) != 2 &&
71589349d600SPetar Penkov !inet_csk_reqsk_queue_is_full(sk))
71599349d600SPetar Penkov return 0;
71609349d600SPetar Penkov
71619349d600SPetar Penkov if (!tcp_syn_flood_action(sk, rsk_ops->slab_name))
71629349d600SPetar Penkov return 0;
71639349d600SPetar Penkov
71649349d600SPetar Penkov if (sk_acceptq_is_full(sk)) {
71659349d600SPetar Penkov NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
71669349d600SPetar Penkov return 0;
71679349d600SPetar Penkov }
71689349d600SPetar Penkov
71699349d600SPetar Penkov mss = tcp_parse_mss_option(th, tp->rx_opt.user_mss);
71709349d600SPetar Penkov if (!mss)
71719349d600SPetar Penkov mss = af_ops->mss_clamp;
71729349d600SPetar Penkov
71739349d600SPetar Penkov return mss;
71749349d600SPetar Penkov }
71759349d600SPetar Penkov EXPORT_SYMBOL_GPL(tcp_get_syncookie_mss);
71769349d600SPetar Penkov
tcp_conn_request(struct request_sock_ops * rsk_ops,const struct tcp_request_sock_ops * af_ops,struct sock * sk,struct sk_buff * skb)71771fb6f159SOctavian Purdila int tcp_conn_request(struct request_sock_ops *rsk_ops,
71781fb6f159SOctavian Purdila const struct tcp_request_sock_ops *af_ops,
71791fb6f159SOctavian Purdila struct sock *sk, struct sk_buff *skb)
71801fb6f159SOctavian Purdila {
71811fb6f159SOctavian Purdila struct tcp_fastopen_cookie foc = { .len = -1 };
71827c85af88SEric Dumazet struct tcp_options_received tmp_opt;
71837c85af88SEric Dumazet struct tcp_sock *tp = tcp_sk(sk);
718412ed8244SNikolay Borisov struct net *net = sock_net(sk);
71857c85af88SEric Dumazet struct sock *fastopen_sk = NULL;
71867c85af88SEric Dumazet struct request_sock *req;
71877c85af88SEric Dumazet bool want_cookie = false;
7188eaa72dc4SEric Dumazet struct dst_entry *dst;
71897c85af88SEric Dumazet struct flowi fl;
7190f2e383b5SKuniyuki Iwashima u8 syncookies;
719141eecbd7SEric Dumazet u32 isn;
7192f2e383b5SKuniyuki Iwashima
719306b22ef2SDmitry Safonov #ifdef CONFIG_TCP_AO
719406b22ef2SDmitry Safonov const struct tcp_ao_hdr *aoh;
719506b22ef2SDmitry Safonov #endif
719606b22ef2SDmitry Safonov
719741eecbd7SEric Dumazet isn = __this_cpu_read(tcp_tw_isn);
719841eecbd7SEric Dumazet if (isn) {
71991fb6f159SOctavian Purdila /* TW buckets are converted to open requests without
72001fb6f159SOctavian Purdila * limitations, they conserve resources and peer is
72011fb6f159SOctavian Purdila * evidently real one.
72021fb6f159SOctavian Purdila */
720341eecbd7SEric Dumazet __this_cpu_write(tcp_tw_isn, 0);
720441eecbd7SEric Dumazet } else {
720541eecbd7SEric Dumazet syncookies = READ_ONCE(net->ipv4.sysctl_tcp_syncookies);
720641eecbd7SEric Dumazet
720741eecbd7SEric Dumazet if (syncookies == 2 || inet_csk_reqsk_queue_is_full(sk)) {
720841eecbd7SEric Dumazet want_cookie = tcp_syn_flood_action(sk,
720941eecbd7SEric Dumazet rsk_ops->slab_name);
72101fb6f159SOctavian Purdila if (!want_cookie)
72111fb6f159SOctavian Purdila goto drop;
72121fb6f159SOctavian Purdila }
721341eecbd7SEric Dumazet }
72141fb6f159SOctavian Purdila
72155ea8ea2cSEric Dumazet if (sk_acceptq_is_full(sk)) {
7216c10d9310SEric Dumazet NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
72171fb6f159SOctavian Purdila goto drop;
72181fb6f159SOctavian Purdila }
72191fb6f159SOctavian Purdila
7220a1a5344dSEric Dumazet req = inet_reqsk_alloc(rsk_ops, sk, !want_cookie);
72211fb6f159SOctavian Purdila if (!req)
72221fb6f159SOctavian Purdila goto drop;
72231fb6f159SOctavian Purdila
7224f8ace8d9SFlorian Westphal req->syncookie = want_cookie;
72251fb6f159SOctavian Purdila tcp_rsk(req)->af_specific = af_ops;
722695a22caeSFlorian Westphal tcp_rsk(req)->ts_off = 0;
7227cdbab623SEric Dumazet tcp_rsk(req)->req_usec_ts = false;
7228cec37a6eSPeter Krystad #if IS_ENABLED(CONFIG_MPTCP)
7229cec37a6eSPeter Krystad tcp_rsk(req)->is_mptcp = 0;
7230cec37a6eSPeter Krystad #endif
72311fb6f159SOctavian Purdila
72321fb6f159SOctavian Purdila tcp_clear_options(&tmp_opt);
72331fb6f159SOctavian Purdila tmp_opt.mss_clamp = af_ops->mss_clamp;
72341fb6f159SOctavian Purdila tmp_opt.user_mss = tp->rx_opt.user_mss;
7235eed29f17SEric Dumazet tcp_parse_options(sock_net(sk), skb, &tmp_opt, 0,
7236eed29f17SEric Dumazet want_cookie ? NULL : &foc);
72371fb6f159SOctavian Purdila
72381fb6f159SOctavian Purdila if (want_cookie && !tmp_opt.saw_tstamp)
72391fb6f159SOctavian Purdila tcp_clear_options(&tmp_opt);
72401fb6f159SOctavian Purdila
7241bc58a1baSHans Wippel if (IS_ENABLED(CONFIG_SMC) && want_cookie)
7242bc58a1baSHans Wippel tmp_opt.smc_ok = 0;
7243bc58a1baSHans Wippel
72441fb6f159SOctavian Purdila tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
72451fb6f159SOctavian Purdila tcp_openreq_init(req, &tmp_opt, skb, sk);
72464bd0623fSEric Dumazet inet_rsk(req)->no_srccheck = inet_test_bit(TRANSPARENT, sk);
72471fb6f159SOctavian Purdila
724816f86165SEric Dumazet /* Note: tcp_v6_init_req() might override ir_iif for link locals */
72496dd9a14eSDavid Ahern inet_rsk(req)->ir_iif = inet_request_bound_dev_if(sk, skb);
725016f86165SEric Dumazet
7251b9e81040SEric Dumazet dst = af_ops->route_req(sk, skb, &fl, req, isn);
72527ea851d1SFlorian Westphal if (!dst)
72531fb6f159SOctavian Purdila goto drop_and_free;
72541fb6f159SOctavian Purdila
7255cdbab623SEric Dumazet if (tmp_opt.tstamp_ok) {
7256cdbab623SEric Dumazet tcp_rsk(req)->req_usec_ts = dst_tcp_usec_ts(dst);
72575d2ed052SEric Dumazet tcp_rsk(req)->ts_off = af_ops->init_ts_off(net, skb);
7258cdbab623SEric Dumazet }
7259f7b3bec6SFlorian Westphal if (!want_cookie && !isn) {
726079539f34SKuniyuki Iwashima int max_syn_backlog = READ_ONCE(net->ipv4.sysctl_max_syn_backlog);
726179539f34SKuniyuki Iwashima
72621fb6f159SOctavian Purdila /* Kill the following clause, if you dislike this way. */
7263f2e383b5SKuniyuki Iwashima if (!syncookies &&
726479539f34SKuniyuki Iwashima (max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
726579539f34SKuniyuki Iwashima (max_syn_backlog >> 2)) &&
7266d82bae12SSoheil Hassas Yeganeh !tcp_peer_is_proven(req, dst)) {
72671fb6f159SOctavian Purdila /* Without syncookies last quarter of
72681fb6f159SOctavian Purdila * backlog is filled with destinations,
72691fb6f159SOctavian Purdila * proven to be alive.
72701fb6f159SOctavian Purdila * It means that we continue to communicate
72711fb6f159SOctavian Purdila * to destinations, already remembered
72721fb6f159SOctavian Purdila * to the moment of synflood.
72731fb6f159SOctavian Purdila */
72741fb6f159SOctavian Purdila pr_drop_req(req, ntohs(tcp_hdr(skb)->source),
72751fb6f159SOctavian Purdila rsk_ops->family);
72761fb6f159SOctavian Purdila goto drop_and_release;
72771fb6f159SOctavian Purdila }
72781fb6f159SOctavian Purdila
727984b114b9SEric Dumazet isn = af_ops->init_seq(skb);
72801fb6f159SOctavian Purdila }
72811fb6f159SOctavian Purdila
7282f7b3bec6SFlorian Westphal tcp_ecn_create_request(req, skb, sk, dst);
7283f7b3bec6SFlorian Westphal
7284f7b3bec6SFlorian Westphal if (want_cookie) {
7285f7b3bec6SFlorian Westphal isn = cookie_init_sequence(af_ops, sk, skb, &req->mss);
7286f7b3bec6SFlorian Westphal if (!tmp_opt.tstamp_ok)
7287f7b3bec6SFlorian Westphal inet_rsk(req)->ecn_ok = 0;
7288f7b3bec6SFlorian Westphal }
7289f7b3bec6SFlorian Westphal
729006b22ef2SDmitry Safonov #ifdef CONFIG_TCP_AO
729106b22ef2SDmitry Safonov if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh))
729206b22ef2SDmitry Safonov goto drop_and_release; /* Invalid TCP options */
729306b22ef2SDmitry Safonov if (aoh) {
72949396c4eeSDmitry Safonov tcp_rsk(req)->used_tcp_ao = true;
729506b22ef2SDmitry Safonov tcp_rsk(req)->ao_rcv_next = aoh->keyid;
729606b22ef2SDmitry Safonov tcp_rsk(req)->ao_keyid = aoh->rnext_keyid;
72979396c4eeSDmitry Safonov
729806b22ef2SDmitry Safonov } else {
72999396c4eeSDmitry Safonov tcp_rsk(req)->used_tcp_ao = false;
730006b22ef2SDmitry Safonov }
730106b22ef2SDmitry Safonov #endif
73021fb6f159SOctavian Purdila tcp_rsk(req)->snt_isn = isn;
730358d607d3SEric Dumazet tcp_rsk(req)->txhash = net_tx_rndhash();
7304e9b12edcSWei Wang tcp_rsk(req)->syn_tos = TCP_SKB_CB(skb)->ip_dsfield;
73051fb6f159SOctavian Purdila tcp_openreq_init_rwin(req, sk, dst);
7306c6345ce7SAmritha Nambiar sk_rx_queue_set(req_to_sk(req), skb);
7307ca6fb065SEric Dumazet if (!want_cookie) {
7308ca6fb065SEric Dumazet tcp_reqsk_record_syn(sk, req, skb);
730971c02379SChristoph Paasch fastopen_sk = tcp_try_fastopen(sk, skb, req, &foc, dst);
7310ca6fb065SEric Dumazet }
73117c85af88SEric Dumazet if (fastopen_sk) {
7312ca6fb065SEric Dumazet af_ops->send_synack(fastopen_sk, dst, &fl, req,
7313331fca43SMartin KaFai Lau &foc, TCP_SYNACK_FASTOPEN, skb);
73147656d842SEric Dumazet /* Add the child socket directly into the accept queue */
73159d3e1368SGuillaume Nault if (!inet_csk_reqsk_queue_add(sk, req, fastopen_sk)) {
73169d3e1368SGuillaume Nault reqsk_fastopen_remove(fastopen_sk, req, false);
73179d3e1368SGuillaume Nault bh_unlock_sock(fastopen_sk);
73189d3e1368SGuillaume Nault sock_put(fastopen_sk);
73199403cf23SGuillaume Nault goto drop_and_free;
73209d3e1368SGuillaume Nault }
73217656d842SEric Dumazet sk->sk_data_ready(sk);
73227656d842SEric Dumazet bh_unlock_sock(fastopen_sk);
73237c85af88SEric Dumazet sock_put(fastopen_sk);
73247c85af88SEric Dumazet } else {
73259439ce00SEric Dumazet tcp_rsk(req)->tfo_listener = false;
73265903123fSAkhmat Karakotov if (!want_cookie) {
73275903123fSAkhmat Karakotov req->timeout = tcp_timeout_init((struct sock *)req);
7328ff46e3b4Sluoxuanqiang if (unlikely(!inet_csk_reqsk_queue_hash_add(sk, req,
7329ff46e3b4Sluoxuanqiang req->timeout))) {
7330ff46e3b4Sluoxuanqiang reqsk_free(req);
7331ff46e3b4Sluoxuanqiang return 0;
7332ff46e3b4Sluoxuanqiang }
7333ff46e3b4Sluoxuanqiang
73345903123fSAkhmat Karakotov }
7335b3d05147SEric Dumazet af_ops->send_synack(sk, dst, &fl, req, &foc,
7336b3d05147SEric Dumazet !want_cookie ? TCP_SYNACK_NORMAL :
7337331fca43SMartin KaFai Lau TCP_SYNACK_COOKIE,
7338331fca43SMartin KaFai Lau skb);
73399caad864SEric Dumazet if (want_cookie) {
73409caad864SEric Dumazet reqsk_free(req);
73419caad864SEric Dumazet return 0;
73429caad864SEric Dumazet }
73431fb6f159SOctavian Purdila }
7344ca6fb065SEric Dumazet reqsk_put(req);
73451fb6f159SOctavian Purdila return 0;
73461fb6f159SOctavian Purdila
73471fb6f159SOctavian Purdila drop_and_release:
73481fb6f159SOctavian Purdila dst_release(dst);
73491fb6f159SOctavian Purdila drop_and_free:
73509403cf23SGuillaume Nault __reqsk_free(req);
73511fb6f159SOctavian Purdila drop:
73529caad864SEric Dumazet tcp_listendrop(sk);
73531fb6f159SOctavian Purdila return 0;
73541fb6f159SOctavian Purdila }
73551fb6f159SOctavian Purdila EXPORT_SYMBOL(tcp_conn_request);
7356