| 1 | |
| 2 | |
| 3 | * INET An implementation of the TCP/IP protocol suite for the LINUX
|
| 4 | * operating system. INET is implemented using the BSD Socket
|
| 5 | * interface as the means of communication with the user level.
|
| 6 | *
|
| 7 | * Implementation of the Transmission Control Protocol(TCP).
|
| 8 | *
|
| 9 | * Authors: Ross Biro
|
| 10 | * Fred N. van Kempen, <[email protected]>
|
| 11 | * Mark Evans, <[email protected]>
|
| 12 | * Corey Minyard <[email protected]>
|
| 13 | * Florian La Roche, <[email protected]>
|
| 14 | * Charles Hedrick, <[email protected]>
|
| 15 | * Linus Torvalds, <[email protected]>
|
| 16 | * Alan Cox, <[email protected]>
|
| 17 | * Matthew Dillon, <[email protected]>
|
| 18 | * Arnt Gulbrandsen, <[email protected]>
|
| 19 | * Jorge Cwik, <[email protected]>
|
| 20 |
|
| 21 |
|
| 22 | |
| 23 | * Changes:
|
| 24 | * Pedro Roque : Fast Retransmit/Recovery.
|
| 25 | * Two receive queues.
|
| 26 | * Retransmit queue handled by TCP.
|
| 27 | * Better retransmit timer handling.
|
| 28 | * New congestion avoidance.
|
| 29 | * Header prediction.
|
| 30 | * Variable renaming.
|
| 31 | *
|
| 32 | * Eric : Fast Retransmit.
|
| 33 | * Randy Scott : MSS option defines.
|
| 34 | * Eric Schenk : Fixes to slow start algorithm.
|
| 35 | * Eric Schenk : Yet another double ACK bug.
|
| 36 | * Eric Schenk : Delayed ACK bug fixes.
|
| 37 | * Eric Schenk : Floyd style fast retrans war avoidance.
|
| 38 | * David S. Miller : Don't allow zero congestion window.
|
| 39 | * Eric Schenk : Fix retransmitter so that it sends
|
| 40 | * next packet on ack of previous packet.
|
| 41 | * Andi Kleen : Moved open_request checking here
|
| 42 | * and process RSTs for open_requests.
|
| 43 | * Andi Kleen : Better prune_queue, and other fixes.
|
| 44 | * Andrey Savochkin: Fix RTT measurements in the presence of
|
| 45 | * timestamps.
|
| 46 | * Andrey Savochkin: Check sequence numbers correctly when
|
| 47 | * removing SACKs due to in sequence incoming
|
| 48 | * data segments.
|
| 49 | * Andi Kleen: Make sure we never ack data there is not
|
| 50 | * enough room for. Also make this condition
|
| 51 | * a fatal error if it might still happen.
|
| 52 | * Andi Kleen: Add tcp_measure_rcv_mss to make
|
| 53 | * connections with MSS<min(MTU,ann. MSS)
|
| 54 | * work without delayed acks.
|
| 55 | * Andi Kleen: Process packets with PSH set in the
|
| 56 | * fast path.
|
| 57 | * J Hadi Salim: ECN support
|
| 58 | * Andrei Gurtov,
|
| 59 | * Pasi Sarolahti,
|
| 60 | * Panu Kuhlberg: Experimental audit of TCP (re)transmission
|
| 61 | * engine. Lots of bugs are found.
|
| 62 | * Pasi Sarolahti: F-RTO for dealing with spurious RTOs
|
| 63 |
|
| 64 |
|
| 65 | #define pr_fmt(fmt) "TCP: " fmt
|
| 66 |
|
| 67 | #include <linux/mm.h>
|
| 68 | #include <linux/slab.h>
|
| 69 | #include <linux/module.h>
|
| 70 | #include <linux/sysctl.h>
|
| 71 | #include <linux/kernel.h>
|
| 72 | #include <linux/prefetch.h>
|
| 73 | #include <linux/bitops.h>
|
| 74 | #include <net/dst.h>
|
| 75 | #include <net/tcp.h>
|
| 76 | #include <net/tcp_ecn.h>
|
| 77 | #include <net/proto_memory.h>
|
| 78 | #include <net/inet_common.h>
|
| 79 | #include <linux/ipsec.h>
|
| 80 | #include <linux/unaligned.h>
|
| 81 | #include <linux/errqueue.h>
|
| 82 | #include <trace/events/tcp.h>
|
| 83 | #include <linux/jump_label_ratelimit.h>
|
| 84 | #include <net/busy_poll.h>
|
| 85 | #include <net/mptcp.h>
|
| 86 |
|
| 87 | int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
|
| 88 |
|
| 89 | #define FLAG_DATA 0x01
|
| 90 | #define FLAG_WIN_UPDATE 0x02
|
| 91 | #define FLAG_DATA_ACKED 0x04
|
| 92 | #define FLAG_RETRANS_DATA_ACKED 0x08
|
| 93 | #define FLAG_SYN_ACKED 0x10
|
| 94 | #define FLAG_DATA_SACKED 0x20
|
| 95 | #define FLAG_ECE 0x40
|
| 96 | #define FLAG_LOST_RETRANS 0x80
|
| 97 | #define FLAG_SLOWPATH 0x100
|
| 98 | #define FLAG_ORIG_SACK_ACKED 0x200
|
| 99 | #define FLAG_SND_UNA_ADVANCED 0x400
|
| 100 | #define FLAG_DSACKING_ACK 0x800
|
| 101 | #define FLAG_SET_XMIT_TIMER 0x1000
|
| 102 | #define FLAG_SACK_RENEGING 0x2000
|
| 103 | #define FLAG_UPDATE_TS_RECENT 0x4000
|
| 104 | #define FLAG_NO_CHALLENGE_ACK 0x8000
|
| 105 | #define FLAG_ACK_MAYBE_DELAYED 0x10000
|
| 106 | #define FLAG_DSACK_TLP 0x20000
|
| 107 | #define FLAG_TS_PROGRESS 0x40000
|
| 108 |
|
| 109 | #define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
|
| 110 | #define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
|
| 111 | #define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE|FLAG_DSACKING_ACK)
|
| 112 | #define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
|
| 113 |
|
| 114 | #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
|
| 115 | #define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
|
| 116 |
|
| 117 | #define REXMIT_NONE 0
|
| 118 | #define REXMIT_LOST 1
|
| 119 | #define REXMIT_NEW 2
|
| 120 |
|
| 121 | #if IS_ENABLED(CONFIG_TLS_DEVICE)
|
| 122 | static DEFINE_STATIC_KEY_DEFERRED_FALSE(clean_acked_data_enabled, HZ);
|
| 123 |
|
| 124 | void clean_acked_data_enable(struct tcp_sock *tp,
|
| 125 | void (*cad)(struct sock *sk, u32 ack_seq))
|
| 126 | {
|
| 127 | tp->tcp_clean_acked = cad;
|
| 128 | static_branch_deferred_inc(&clean_acked_data_enabled);
|
| 129 | }
|
| 130 | EXPORT_SYMBOL_GPL(clean_acked_data_enable);
|
| 131 |
|
| 132 | void clean_acked_data_disable(struct tcp_sock *tp)
|
| 133 | {
|
| 134 | static_branch_slow_dec_deferred(&clean_acked_data_enabled);
|
| 135 | tp->tcp_clean_acked = NULL;
|
| 136 | }
|
| 137 | EXPORT_SYMBOL_GPL(clean_acked_data_disable);
|
| 138 |
|
| 139 | void clean_acked_data_flush(void)
|
| 140 | {
|
| 141 | static_key_deferred_flush(&clean_acked_data_enabled);
|
| 142 | }
|
| 143 | EXPORT_SYMBOL_GPL(clean_acked_data_flush);
|
| 144 | #endif
|
| 145 |
|
| 146 | #ifdef CONFIG_CGROUP_BPF
|
| 147 | static void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb)
|
| 148 | {
|
| 149 | bool unknown_opt = tcp_sk(sk)->rx_opt.saw_unknown &&
|
| 150 | BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk),
|
| 151 | BPF_SOCK_OPS_PARSE_UNKNOWN_HDR_OPT_CB_FLAG);
|
| 152 | bool parse_all_opt = BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk),
|
| 153 | BPF_SOCK_OPS_PARSE_ALL_HDR_OPT_CB_FLAG);
|
| 154 | struct bpf_sock_ops_kern sock_ops;
|
| 155 |
|
| 156 | if (likely(!unknown_opt && !parse_all_opt))
|
| 157 | return;
|
| 158 |
|
| 159 | |
| 160 | * bpf_skops_established() or
|
| 161 | * bpf_skops_write_hdr_opt().
|
| 162 |
|
| 163 | switch (sk->sk_state) {
|
| 164 | case TCP_SYN_RECV:
|
| 165 | case TCP_SYN_SENT:
|
| 166 | case TCP_LISTEN:
|
| 167 | return;
|
| 168 | }
|
| 169 |
|
| 170 | sock_owned_by_me(sk);
|
| 171 |
|
| 172 | memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
|
| 173 | sock_ops.op = BPF_SOCK_OPS_PARSE_HDR_OPT_CB;
|
| 174 | sock_ops.is_fullsock = 1;
|
| 175 | sock_ops.is_locked_tcp_sock = 1;
|
| 176 | sock_ops.sk = sk;
|
| 177 | bpf_skops_init_skb(&sock_ops, skb, tcp_hdrlen(skb));
|
| 178 |
|
| 179 | BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
|
| 180 | }
|
| 181 |
|
| 182 | static void bpf_skops_established(struct sock *sk, int bpf_op,
|
| 183 | struct sk_buff *skb)
|
| 184 | {
|
| 185 | struct bpf_sock_ops_kern sock_ops;
|
| 186 |
|
| 187 | sock_owned_by_me(sk);
|
| 188 |
|
| 189 | memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp));
|
| 190 | sock_ops.op = bpf_op;
|
| 191 | sock_ops.is_fullsock = 1;
|
| 192 | sock_ops.is_locked_tcp_sock = 1;
|
| 193 | sock_ops.sk = sk;
|
| 194 |
|
| 195 | if (skb)
|
| 196 | bpf_skops_init_skb(&sock_ops, skb, tcp_hdrlen(skb));
|
| 197 |
|
| 198 | BPF_CGROUP_RUN_PROG_SOCK_OPS(&sock_ops);
|
| 199 | }
|
| 200 | #else
|
| 201 | static void bpf_skops_parse_hdr(struct sock *sk, struct sk_buff *skb)
|
| 202 | {
|
| 203 | }
|
| 204 |
|
| 205 | static void bpf_skops_established(struct sock *sk, int bpf_op,
|
| 206 | struct sk_buff *skb)
|
| 207 | {
|
| 208 | }
|
| 209 | #endif
|
| 210 |
|
| 211 | static __cold void tcp_gro_dev_warn(const struct sock *sk, const struct sk_buff *skb,
|
| 212 | unsigned int len)
|
| 213 | {
|
| 214 | struct net_device *dev;
|
| 215 |
|
| 216 | rcu_read_lock();
|
| 217 | dev = dev_get_by_index_rcu(sock_net(sk), skb->skb_iif);
|
| 218 | if (!dev || len >= READ_ONCE(dev->mtu))
|
| 219 | pr_warn("%s: Driver has suspect GRO implementation, TCP performance may be compromised.\n",
|
| 220 | dev ? dev->name : "Unknown driver");
|
| 221 | rcu_read_unlock();
|
| 222 | }
|
| 223 |
|
| 224 | |
| 225 | * real world.
|
| 226 |
|
| 227 | static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
|
| 228 | {
|
| 229 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 230 | const unsigned int lss = icsk->icsk_ack.last_seg_size;
|
| 231 | unsigned int len;
|
| 232 |
|
| 233 | icsk->icsk_ack.last_seg_size = 0;
|
| 234 |
|
| 235 | |
| 236 | * sends good full-sized frames.
|
| 237 |
|
| 238 | len = skb_shinfo(skb)->gso_size ? : skb->len;
|
| 239 | if (len >= icsk->icsk_ack.rcv_mss) {
|
| 240 | |
| 241 | * For the moment, only adjust scaling_ratio
|
| 242 | * when we update icsk_ack.rcv_mss.
|
| 243 |
|
| 244 | if (unlikely(len != icsk->icsk_ack.rcv_mss)) {
|
| 245 | u64 val = (u64)skb->len << TCP_RMEM_TO_WIN_SCALE;
|
| 246 | u8 old_ratio = tcp_sk(sk)->scaling_ratio;
|
| 247 |
|
| 248 | do_div(val, skb->truesize);
|
| 249 | tcp_sk(sk)->scaling_ratio = val ? val : 1;
|
| 250 |
|
| 251 | if (old_ratio != tcp_sk(sk)->scaling_ratio) {
|
| 252 | struct tcp_sock *tp = tcp_sk(sk);
|
| 253 |
|
| 254 | val = tcp_win_from_space(sk, sk->sk_rcvbuf);
|
| 255 | WRITE_ONCE(tp->window_clamp, val);
|
| 256 |
|
| 257 | if (tp->window_clamp < tp->rcvq_space.space)
|
| 258 | tp->rcvq_space.space = tp->window_clamp;
|
| 259 | }
|
| 260 | }
|
| 261 | icsk->icsk_ack.rcv_mss = min_t(unsigned int, len,
|
| 262 | tcp_sk(sk)->advmss);
|
| 263 |
|
| 264 | DO_ONCE_LITE_IF(len > icsk->icsk_ack.rcv_mss + MAX_TCP_OPTION_SPACE,
|
| 265 | tcp_gro_dev_warn, sk, skb, len);
|
| 266 | |
| 267 | * set then it is likely the end of an application write. So
|
| 268 | * more data may not be arriving soon, and yet the data sender
|
| 269 | * may be waiting for an ACK if cwnd-bound or using TX zero
|
| 270 | * copy. So we set ICSK_ACK_PUSHED here so that
|
| 271 | * tcp_cleanup_rbuf() will send an ACK immediately if the app
|
| 272 | * reads all of the data and is not ping-pong. If len > MSS
|
| 273 | * then this logic does not matter (and does not hurt) because
|
| 274 | * tcp_cleanup_rbuf() will always ACK immediately if the app
|
| 275 | * reads data and there is more than an MSS of unACKed data.
|
| 276 |
|
| 277 | if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_PSH)
|
| 278 | icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
|
| 279 | } else {
|
| 280 | |
| 281 | * that SACKs block is variable.
|
| 282 | *
|
| 283 | * "len" is invariant segment length, including TCP header.
|
| 284 |
|
| 285 | len += skb->data - skb_transport_header(skb);
|
| 286 | if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
|
| 287 | |
| 288 | * full sized, provided peer TCP is not badly broken.
|
| 289 | * This observation (if it is correct 8)) allows
|
| 290 | * to handle super-low mtu links fairly.
|
| 291 |
|
| 292 | (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
|
| 293 | !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
|
| 294 | |
| 295 | * tcp header plus fixed timestamp option length.
|
| 296 | * Resulting "len" is MSS free of SACK jitter.
|
| 297 |
|
| 298 | len -= tcp_sk(sk)->tcp_header_len;
|
| 299 | icsk->icsk_ack.last_seg_size = len;
|
| 300 | if (len == lss) {
|
| 301 | icsk->icsk_ack.rcv_mss = len;
|
| 302 | return;
|
| 303 | }
|
| 304 | }
|
| 305 | if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
|
| 306 | icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
|
| 307 | icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
|
| 308 | }
|
| 309 | }
|
| 310 |
|
| 311 | static void tcp_incr_quickack(struct sock *sk, unsigned int max_quickacks)
|
| 312 | {
|
| 313 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 314 | unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
|
| 315 |
|
| 316 | if (quickacks == 0)
|
| 317 | quickacks = 2;
|
| 318 | quickacks = min(quickacks, max_quickacks);
|
| 319 | if (quickacks > icsk->icsk_ack.quick)
|
| 320 | icsk->icsk_ack.quick = quickacks;
|
| 321 | }
|
| 322 |
|
| 323 | static void tcp_enter_quickack_mode(struct sock *sk, unsigned int max_quickacks)
|
| 324 | {
|
| 325 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 326 |
|
| 327 | tcp_incr_quickack(sk, max_quickacks);
|
| 328 | inet_csk_exit_pingpong_mode(sk);
|
| 329 | icsk->icsk_ack.ato = TCP_ATO_MIN;
|
| 330 | }
|
| 331 |
|
| 332 | |
| 333 | * and the session is not interactive.
|
| 334 |
|
| 335 |
|
| 336 | static bool tcp_in_quickack_mode(struct sock *sk)
|
| 337 | {
|
| 338 | const struct inet_connection_sock *icsk = inet_csk(sk);
|
| 339 |
|
| 340 | return icsk->icsk_ack.dst_quick_ack ||
|
| 341 | (icsk->icsk_ack.quick && !inet_csk_in_pingpong_mode(sk));
|
| 342 | }
|
| 343 |
|
| 344 | static void tcp_data_ecn_check(struct sock *sk, const struct sk_buff *skb)
|
| 345 | {
|
| 346 | struct tcp_sock *tp = tcp_sk(sk);
|
| 347 |
|
| 348 | if (tcp_ecn_disabled(tp))
|
| 349 | return;
|
| 350 |
|
| 351 | switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
|
| 352 | case INET_ECN_NOT_ECT:
|
| 353 | |
| 354 | * and we already seen ECT on a previous segment,
|
| 355 | * it is probably a retransmit.
|
| 356 |
|
| 357 | if (tp->ecn_flags & TCP_ECN_SEEN)
|
| 358 | tcp_enter_quickack_mode(sk, 2);
|
| 359 | break;
|
| 360 | case INET_ECN_CE:
|
| 361 | if (tcp_ca_needs_ecn(sk))
|
| 362 | tcp_ca_event(sk, CA_EVENT_ECN_IS_CE);
|
| 363 |
|
| 364 | if (!(tp->ecn_flags & TCP_ECN_DEMAND_CWR) &&
|
| 365 | tcp_ecn_mode_rfc3168(tp)) {
|
| 366 |
|
| 367 | tcp_enter_quickack_mode(sk, 2);
|
| 368 | tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
|
| 369 | }
|
| 370 | |
| 371 | * tcp_data_ecn_check() when the ECN codepoint of
|
| 372 | * received TCP data contains ECT(0), ECT(1), or CE.
|
| 373 |
|
| 374 | if (!tcp_ecn_mode_rfc3168(tp))
|
| 375 | break;
|
| 376 | tp->ecn_flags |= TCP_ECN_SEEN;
|
| 377 | break;
|
| 378 | default:
|
| 379 | if (tcp_ca_needs_ecn(sk))
|
| 380 | tcp_ca_event(sk, CA_EVENT_ECN_NO_CE);
|
| 381 | if (!tcp_ecn_mode_rfc3168(tp))
|
| 382 | break;
|
| 383 | tp->ecn_flags |= TCP_ECN_SEEN;
|
| 384 | break;
|
| 385 | }
|
| 386 | }
|
| 387 |
|
| 388 |
|
| 389 | static bool tcp_accecn_process_option(struct tcp_sock *tp,
|
| 390 | const struct sk_buff *skb,
|
| 391 | u32 delivered_bytes, int flag)
|
| 392 | {
|
| 393 | u8 estimate_ecnfield = tp->est_ecnfield;
|
| 394 | bool ambiguous_ecn_bytes_incr = false;
|
| 395 | bool first_changed = false;
|
| 396 | unsigned int optlen;
|
| 397 | bool order1, res;
|
| 398 | unsigned int i;
|
| 399 | u8 *ptr;
|
| 400 |
|
| 401 | if (tcp_accecn_opt_fail_recv(tp))
|
| 402 | return false;
|
| 403 |
|
| 404 | if (!(flag & FLAG_SLOWPATH) || !tp->rx_opt.accecn) {
|
| 405 | if (!tp->saw_accecn_opt) {
|
| 406 | |
| 407 | * potential counter wraps
|
| 408 |
|
| 409 | if (tp->bytes_sent >= (1 << 23) - 1) {
|
| 410 | u8 saw_opt = TCP_ACCECN_OPT_FAIL_SEEN;
|
| 411 |
|
| 412 | tcp_accecn_saw_opt_fail_recv(tp, saw_opt);
|
| 413 | }
|
| 414 | return false;
|
| 415 | }
|
| 416 |
|
| 417 | if (estimate_ecnfield) {
|
| 418 | u8 ecnfield = estimate_ecnfield - 1;
|
| 419 |
|
| 420 | tp->delivered_ecn_bytes[ecnfield] += delivered_bytes;
|
| 421 | return true;
|
| 422 | }
|
| 423 | return false;
|
| 424 | }
|
| 425 |
|
| 426 | ptr = skb_transport_header(skb) + tp->rx_opt.accecn;
|
| 427 | optlen = ptr[1] - 2;
|
| 428 | if (WARN_ON_ONCE(ptr[0] != TCPOPT_ACCECN0 && ptr[0] != TCPOPT_ACCECN1))
|
| 429 | return false;
|
| 430 | order1 = (ptr[0] == TCPOPT_ACCECN1);
|
| 431 | ptr += 2;
|
| 432 |
|
| 433 | if (tp->saw_accecn_opt < TCP_ACCECN_OPT_COUNTER_SEEN) {
|
| 434 | tp->saw_accecn_opt = tcp_accecn_option_init(skb,
|
| 435 | tp->rx_opt.accecn);
|
| 436 | if (tp->saw_accecn_opt == TCP_ACCECN_OPT_FAIL_SEEN)
|
| 437 | tcp_accecn_fail_mode_set(tp, TCP_ACCECN_OPT_FAIL_RECV);
|
| 438 | }
|
| 439 |
|
| 440 | res = !!estimate_ecnfield;
|
| 441 | for (i = 0; i < 3; i++) {
|
| 442 | u32 init_offset;
|
| 443 | u8 ecnfield;
|
| 444 | s32 delta;
|
| 445 | u32 *cnt;
|
| 446 |
|
| 447 | if (optlen < TCPOLEN_ACCECN_PERFIELD)
|
| 448 | break;
|
| 449 |
|
| 450 | ecnfield = tcp_accecn_optfield_to_ecnfield(i, order1);
|
| 451 | init_offset = tcp_accecn_field_init_offset(ecnfield);
|
| 452 | cnt = &tp->delivered_ecn_bytes[ecnfield - 1];
|
| 453 | delta = tcp_update_ecn_bytes(cnt, ptr, init_offset);
|
| 454 | if (delta && delta < 0) {
|
| 455 | res = false;
|
| 456 | ambiguous_ecn_bytes_incr = true;
|
| 457 | }
|
| 458 | if (delta && ecnfield != estimate_ecnfield) {
|
| 459 | if (!first_changed) {
|
| 460 | tp->est_ecnfield = ecnfield;
|
| 461 | first_changed = true;
|
| 462 | } else {
|
| 463 | res = false;
|
| 464 | ambiguous_ecn_bytes_incr = true;
|
| 465 | }
|
| 466 | }
|
| 467 |
|
| 468 | optlen -= TCPOLEN_ACCECN_PERFIELD;
|
| 469 | ptr += TCPOLEN_ACCECN_PERFIELD;
|
| 470 | }
|
| 471 | if (ambiguous_ecn_bytes_incr)
|
| 472 | tp->est_ecnfield = 0;
|
| 473 |
|
| 474 | return res;
|
| 475 | }
|
| 476 |
|
| 477 | static void tcp_count_delivered_ce(struct tcp_sock *tp, u32 ecn_count)
|
| 478 | {
|
| 479 | WRITE_ONCE(tp->delivered_ce, tp->delivered_ce + ecn_count);
|
| 480 | }
|
| 481 |
|
| 482 |
|
| 483 | static void tcp_count_delivered(struct tcp_sock *tp, u32 delivered,
|
| 484 | bool ece_ack)
|
| 485 | {
|
| 486 | WRITE_ONCE(tp->delivered, tp->delivered + delivered);
|
| 487 | if (tcp_ecn_mode_rfc3168(tp) && ece_ack)
|
| 488 | tcp_count_delivered_ce(tp, delivered);
|
| 489 | }
|
| 490 |
|
| 491 | #define PKTS_ACKED_WEIGHT 6
|
| 492 | #define PKTS_ACKED_PREC 6
|
| 493 | #define ACK_COMP_THRESH 4
|
| 494 |
|
| 495 |
|
| 496 | static u32 __tcp_accecn_process(struct sock *sk, const struct sk_buff *skb,
|
| 497 | u32 delivered_pkts, u32 delivered_bytes,
|
| 498 | int flag)
|
| 499 | {
|
| 500 | u32 old_ceb = tcp_sk(sk)->delivered_ecn_bytes[INET_ECN_CE - 1];
|
| 501 | const struct tcphdr *th = tcp_hdr(skb);
|
| 502 | struct tcp_sock *tp = tcp_sk(sk);
|
| 503 | u32 delta, safe_delta, d_ceb;
|
| 504 | bool opt_deltas_valid;
|
| 505 | u32 corrected_ace;
|
| 506 | u32 ewma;
|
| 507 |
|
| 508 |
|
| 509 | if (!(flag & (FLAG_FORWARD_PROGRESS | FLAG_TS_PROGRESS)))
|
| 510 | return 0;
|
| 511 |
|
| 512 | opt_deltas_valid = tcp_accecn_process_option(tp, skb,
|
| 513 | delivered_bytes, flag);
|
| 514 |
|
| 515 | if (delivered_pkts) {
|
| 516 | if (!tp->pkts_acked_ewma) {
|
| 517 | ewma = delivered_pkts << PKTS_ACKED_PREC;
|
| 518 | } else {
|
| 519 | ewma = tp->pkts_acked_ewma;
|
| 520 | ewma = (((ewma << PKTS_ACKED_WEIGHT) - ewma) +
|
| 521 | (delivered_pkts << PKTS_ACKED_PREC)) >>
|
| 522 | PKTS_ACKED_WEIGHT;
|
| 523 | }
|
| 524 | tp->pkts_acked_ewma = min_t(u32, ewma, 0xFFFFU);
|
| 525 | }
|
| 526 |
|
| 527 | if (!(flag & FLAG_SLOWPATH)) {
|
| 528 |
|
| 529 | if (delivered_pkts <= TCP_ACCECN_CEP_ACE_MASK)
|
| 530 | return 0;
|
| 531 | }
|
| 532 |
|
| 533 |
|
| 534 | if (flag & FLAG_SYN_ACKED)
|
| 535 | return 0;
|
| 536 |
|
| 537 | if (tp->received_ce_pending >= TCP_ACCECN_ACE_MAX_DELTA)
|
| 538 | inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW;
|
| 539 |
|
| 540 | corrected_ace = tcp_accecn_ace(th) - TCP_ACCECN_CEP_INIT_OFFSET;
|
| 541 | delta = (corrected_ace - tp->delivered_ce) & TCP_ACCECN_CEP_ACE_MASK;
|
| 542 | if (delivered_pkts <= TCP_ACCECN_CEP_ACE_MASK)
|
| 543 | return delta;
|
| 544 |
|
| 545 | safe_delta = delivered_pkts -
|
| 546 | ((delivered_pkts - delta) & TCP_ACCECN_CEP_ACE_MASK);
|
| 547 |
|
| 548 | if (opt_deltas_valid) {
|
| 549 | d_ceb = tp->delivered_ecn_bytes[INET_ECN_CE - 1] - old_ceb;
|
| 550 | if (!d_ceb)
|
| 551 | return delta;
|
| 552 |
|
| 553 | if ((delivered_pkts >= (TCP_ACCECN_CEP_ACE_MASK + 1) * 2) &&
|
| 554 | (tcp_is_sack(tp) ||
|
| 555 | ((1 << inet_csk(sk)->icsk_ca_state) &
|
| 556 | (TCPF_CA_Open | TCPF_CA_CWR)))) {
|
| 557 | u32 est_d_cep;
|
| 558 |
|
| 559 | if (delivered_bytes <= d_ceb)
|
| 560 | return safe_delta;
|
| 561 |
|
| 562 | est_d_cep = DIV_ROUND_UP_ULL((u64)d_ceb *
|
| 563 | delivered_pkts,
|
| 564 | delivered_bytes);
|
| 565 | return min(safe_delta,
|
| 566 | delta +
|
| 567 | (est_d_cep & ~TCP_ACCECN_CEP_ACE_MASK));
|
| 568 | }
|
| 569 |
|
| 570 | if (d_ceb > delta * tp->mss_cache)
|
| 571 | return safe_delta;
|
| 572 | if (d_ceb <
|
| 573 | safe_delta * tp->mss_cache >> TCP_ACCECN_SAFETY_SHIFT)
|
| 574 | return delta;
|
| 575 | } else if (tp->pkts_acked_ewma > (ACK_COMP_THRESH << PKTS_ACKED_PREC))
|
| 576 | return delta;
|
| 577 |
|
| 578 | return safe_delta;
|
| 579 | }
|
| 580 |
|
| 581 | static u32 tcp_accecn_process(struct sock *sk, const struct sk_buff *skb,
|
| 582 | u32 delivered_pkts, u32 delivered_bytes,
|
| 583 | int *flag)
|
| 584 | {
|
| 585 | struct tcp_sock *tp = tcp_sk(sk);
|
| 586 | u32 delta;
|
| 587 |
|
| 588 | delta = __tcp_accecn_process(sk, skb, delivered_pkts,
|
| 589 | delivered_bytes, *flag);
|
| 590 | if (delta > 0) {
|
| 591 | tcp_count_delivered_ce(tp, delta);
|
| 592 | *flag |= FLAG_ECE;
|
| 593 |
|
| 594 | if (tp->pred_flags)
|
| 595 | tcp_fast_path_on(tp);
|
| 596 | }
|
| 597 | return delta;
|
| 598 | }
|
| 599 |
|
| 600 | |
| 601 | *
|
| 602 | * 1. Tuning sk->sk_sndbuf, when connection enters established state.
|
| 603 |
|
| 604 |
|
| 605 | static void tcp_sndbuf_expand(struct sock *sk)
|
| 606 | {
|
| 607 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 608 | const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
|
| 609 | int sndmem, per_mss;
|
| 610 | u32 nr_segs;
|
| 611 |
|
| 612 | |
| 613 | * and skb->head is kmalloced using power of two area of memory
|
| 614 |
|
| 615 | per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) +
|
| 616 | MAX_TCP_HEADER +
|
| 617 | SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
|
| 618 |
|
| 619 | per_mss = roundup_pow_of_two(per_mss) +
|
| 620 | SKB_DATA_ALIGN(sizeof(struct sk_buff));
|
| 621 |
|
| 622 | nr_segs = max_t(u32, TCP_INIT_CWND, tcp_snd_cwnd(tp));
|
| 623 | nr_segs = max_t(u32, nr_segs, tp->reordering + 1);
|
| 624 |
|
| 625 | |
| 626 | * Cubic needs 1.7 factor, rounded to 2 to include
|
| 627 | * extra cushion (application might react slowly to EPOLLOUT)
|
| 628 |
|
| 629 | sndmem = ca_ops->sndbuf_expand ? ca_ops->sndbuf_expand(sk) : 2;
|
| 630 | sndmem *= nr_segs * per_mss;
|
| 631 |
|
| 632 | if (sk->sk_sndbuf < sndmem)
|
| 633 | WRITE_ONCE(sk->sk_sndbuf,
|
| 634 | min(sndmem, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_wmem[2])));
|
| 635 | }
|
| 636 |
|
| 637 | |
| 638 | *
|
| 639 | * All tcp_full_space() is split to two parts: "network" buffer, allocated
|
| 640 | * forward and advertised in receiver window (tp->rcv_wnd) and
|
| 641 | * "application buffer", required to isolate scheduling/application
|
| 642 | * latencies from network.
|
| 643 | * window_clamp is maximal advertised window. It can be less than
|
| 644 | * tcp_full_space(), in this case tcp_full_space() - window_clamp
|
| 645 | * is reserved for "application" buffer. The less window_clamp is
|
| 646 | * the smoother our behaviour from viewpoint of network, but the lower
|
| 647 | * throughput and the higher sensitivity of the connection to losses. 8)
|
| 648 | *
|
| 649 | * rcv_ssthresh is more strict window_clamp used at "slow start"
|
| 650 | * phase to predict further behaviour of this connection.
|
| 651 | * It is used for two goals:
|
| 652 | * - to enforce header prediction at sender, even when application
|
| 653 | * requires some significant "application buffer". It is check #1.
|
| 654 | * - to prevent pruning of receive queue because of misprediction
|
| 655 | * of receiver window. Check #2.
|
| 656 | *
|
| 657 | * The scheme does not work when sender sends good segments opening
|
| 658 | * window and then starts to feed us spaghetti. But it should work
|
| 659 | * in common situations. Otherwise, we have to rely on queue collapsing.
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 | static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb,
|
| 664 | unsigned int skbtruesize)
|
| 665 | {
|
| 666 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 667 |
|
| 668 | int truesize = tcp_win_from_space(sk, skbtruesize) >> 1;
|
| 669 | int window = tcp_win_from_space(sk, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])) >> 1;
|
| 670 |
|
| 671 | while (tp->rcv_ssthresh <= window) {
|
| 672 | if (truesize <= skb->len)
|
| 673 | return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
|
| 674 |
|
| 675 | truesize >>= 1;
|
| 676 | window >>= 1;
|
| 677 | }
|
| 678 | return 0;
|
| 679 | }
|
| 680 |
|
| 681 | |
| 682 | * can play nice with us, as sk_buff and skb->head might be either
|
| 683 | * freed or shared with up to MAX_SKB_FRAGS segments.
|
| 684 | * Only give a boost to drivers using page frag(s) to hold the frame(s),
|
| 685 | * and if no payload was pulled in skb->head before reaching us.
|
| 686 |
|
| 687 | static u32 truesize_adjust(bool adjust, const struct sk_buff *skb)
|
| 688 | {
|
| 689 | u32 truesize = skb->truesize;
|
| 690 |
|
| 691 | if (adjust && !skb_headlen(skb)) {
|
| 692 | truesize -= SKB_TRUESIZE(skb_end_offset(skb));
|
| 693 |
|
| 694 | if (unlikely((int)truesize < (int)skb->len))
|
| 695 | truesize = skb->truesize;
|
| 696 | }
|
| 697 | return truesize;
|
| 698 | }
|
| 699 |
|
| 700 | static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb,
|
| 701 | bool adjust)
|
| 702 | {
|
| 703 | struct tcp_sock *tp = tcp_sk(sk);
|
| 704 | int room;
|
| 705 |
|
| 706 | room = min_t(int, tp->window_clamp, tcp_space(sk)) - tp->rcv_ssthresh;
|
| 707 |
|
| 708 | if (room <= 0)
|
| 709 | return;
|
| 710 |
|
| 711 |
|
| 712 | if (!tcp_under_memory_pressure(sk)) {
|
| 713 | unsigned int truesize = truesize_adjust(adjust, skb);
|
| 714 | int incr;
|
| 715 |
|
| 716 | |
| 717 | * will fit to rcvbuf in future.
|
| 718 |
|
| 719 | if (tcp_win_from_space(sk, truesize) <= skb->len)
|
| 720 | incr = 2 * tp->advmss;
|
| 721 | else
|
| 722 | incr = __tcp_grow_window(sk, skb, truesize);
|
| 723 |
|
| 724 | if (incr) {
|
| 725 | incr = max_t(int, incr, 2 * skb->len);
|
| 726 | tp->rcv_ssthresh += min(room, incr);
|
| 727 | inet_csk(sk)->icsk_ack.quick |= 1;
|
| 728 | }
|
| 729 | } else {
|
| 730 | |
| 731 | * Adjust rcv_ssthresh according to reserved mem
|
| 732 |
|
| 733 | tcp_adjust_rcv_ssthresh(sk);
|
| 734 | }
|
| 735 | }
|
| 736 |
|
| 737 | |
| 738 | * established state.
|
| 739 |
|
| 740 | static void tcp_init_buffer_space(struct sock *sk)
|
| 741 | {
|
| 742 | int tcp_app_win = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_app_win);
|
| 743 | struct tcp_sock *tp = tcp_sk(sk);
|
| 744 | int maxwin;
|
| 745 |
|
| 746 | if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
|
| 747 | tcp_sndbuf_expand(sk);
|
| 748 |
|
| 749 | tcp_mstamp_refresh(tp);
|
| 750 | tp->rcvq_space.time = tp->tcp_mstamp;
|
| 751 | tp->rcvq_space.seq = tp->copied_seq;
|
| 752 |
|
| 753 | maxwin = tcp_full_space(sk);
|
| 754 |
|
| 755 | if (tp->window_clamp >= maxwin) {
|
| 756 | WRITE_ONCE(tp->window_clamp, maxwin);
|
| 757 |
|
| 758 | if (tcp_app_win && maxwin > 4 * tp->advmss)
|
| 759 | WRITE_ONCE(tp->window_clamp,
|
| 760 | max(maxwin - (maxwin >> tcp_app_win),
|
| 761 | 4 * tp->advmss));
|
| 762 | }
|
| 763 |
|
| 764 |
|
| 765 | if (tcp_app_win &&
|
| 766 | tp->window_clamp > 2 * tp->advmss &&
|
| 767 | tp->window_clamp + tp->advmss > maxwin)
|
| 768 | WRITE_ONCE(tp->window_clamp,
|
| 769 | max(2 * tp->advmss, maxwin - tp->advmss));
|
| 770 |
|
| 771 | tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
|
| 772 | tp->snd_cwnd_stamp = tcp_jiffies32;
|
| 773 | tp->rcvq_space.space = min3(tp->rcv_ssthresh, tp->rcv_wnd,
|
| 774 | (u32)TCP_INIT_CWND * tp->advmss);
|
| 775 | }
|
| 776 |
|
| 777 |
|
| 778 | static void tcp_clamp_window(struct sock *sk)
|
| 779 | {
|
| 780 | struct tcp_sock *tp = tcp_sk(sk);
|
| 781 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 782 | struct net *net = sock_net(sk);
|
| 783 | int rmem2;
|
| 784 |
|
| 785 | icsk->icsk_ack.quick = 0;
|
| 786 | rmem2 = READ_ONCE(net->ipv4.sysctl_tcp_rmem[2]);
|
| 787 |
|
| 788 | if (sk->sk_rcvbuf < rmem2 &&
|
| 789 | !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
|
| 790 | !tcp_under_memory_pressure(sk) &&
|
| 791 | sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {
|
| 792 | WRITE_ONCE(sk->sk_rcvbuf,
|
| 793 | min(atomic_read(&sk->sk_rmem_alloc), rmem2));
|
| 794 | }
|
| 795 | if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
|
| 796 | tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
|
| 797 | }
|
| 798 |
|
| 799 | |
| 800 | * RCV_MSS is an our guess about MSS used by the peer.
|
| 801 | * We haven't any direct information about the MSS.
|
| 802 | * It's better to underestimate the RCV_MSS rather than overestimate.
|
| 803 | * Overestimations make us ACKing less frequently than needed.
|
| 804 | * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
|
| 805 |
|
| 806 | void tcp_initialize_rcv_mss(struct sock *sk)
|
| 807 | {
|
| 808 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 809 | unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
|
| 810 |
|
| 811 | hint = min(hint, tp->rcv_wnd / 2);
|
| 812 | hint = min(hint, TCP_MSS_DEFAULT);
|
| 813 | hint = max(hint, TCP_MIN_MSS);
|
| 814 |
|
| 815 | inet_csk(sk)->icsk_ack.rcv_mss = hint;
|
| 816 | }
|
| 817 |
|
| 818 | |
| 819 | *
|
| 820 | * The algorithm for RTT estimation w/o timestamps is based on
|
| 821 | * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
|
| 822 | * <https://public.lanl.gov/radiant/pubs.html#DRS>
|
| 823 | *
|
| 824 | * More detail on this code can be found at
|
| 825 | * <http://staff.psc.edu/jheffner/>,
|
| 826 | * though this reference is out of date. A new paper
|
| 827 | * is pending.
|
| 828 |
|
| 829 | static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
|
| 830 | {
|
| 831 | u32 new_sample, old_sample = tp->rcv_rtt_est.rtt_us;
|
| 832 | long m = sample << 3;
|
| 833 |
|
| 834 | if (old_sample == 0 || m < old_sample) {
|
| 835 | new_sample = m;
|
| 836 | } else {
|
| 837 | |
| 838 | * case, we could grossly overestimate the RTT especially
|
| 839 | * with chatty applications or bulk transfer apps which
|
| 840 | * are stalled on filesystem I/O.
|
| 841 | *
|
| 842 | * Also, since we are only going for a minimum in the
|
| 843 | * non-timestamp case, we do not smooth things out
|
| 844 | * else with timestamps disabled convergence takes too
|
| 845 | * long.
|
| 846 |
|
| 847 | if (win_dep)
|
| 848 | return;
|
| 849 |
|
| 850 | if (tp->rcv_nxt != tp->copied_seq)
|
| 851 | return;
|
| 852 | new_sample = old_sample - (old_sample >> 3) + sample;
|
| 853 | }
|
| 854 |
|
| 855 | tp->rcv_rtt_est.rtt_us = new_sample;
|
| 856 | }
|
| 857 |
|
| 858 | static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
|
| 859 | {
|
| 860 | u32 delta_us;
|
| 861 |
|
| 862 | if (tp->rcv_rtt_est.time == 0)
|
| 863 | goto new_measure;
|
| 864 | if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
|
| 865 | return;
|
| 866 | delta_us = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcv_rtt_est.time);
|
| 867 | if (!delta_us)
|
| 868 | delta_us = 1;
|
| 869 | tcp_rcv_rtt_update(tp, delta_us, 1);
|
| 870 |
|
| 871 | new_measure:
|
| 872 | tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
|
| 873 | tp->rcv_rtt_est.time = tp->tcp_mstamp;
|
| 874 | }
|
| 875 |
|
| 876 | static s32 tcp_rtt_tsopt_us(const struct tcp_sock *tp, u32 min_delta)
|
| 877 | {
|
| 878 | u32 delta, delta_us;
|
| 879 |
|
| 880 | delta = tcp_time_stamp_ts(tp) - tp->rx_opt.rcv_tsecr;
|
| 881 | if (tp->tcp_usec_ts)
|
| 882 | return delta;
|
| 883 |
|
| 884 | if (likely(delta < INT_MAX / (USEC_PER_SEC / TCP_TS_HZ))) {
|
| 885 | if (!delta)
|
| 886 | delta = min_delta;
|
| 887 | delta_us = delta * (USEC_PER_SEC / TCP_TS_HZ);
|
| 888 | return delta_us;
|
| 889 | }
|
| 890 | return -1;
|
| 891 | }
|
| 892 |
|
| 893 | static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
|
| 894 | const struct sk_buff *skb)
|
| 895 | {
|
| 896 | struct tcp_sock *tp = tcp_sk(sk);
|
| 897 |
|
| 898 | if (tp->rx_opt.rcv_tsecr == tp->rcv_rtt_last_tsecr)
|
| 899 | return;
|
| 900 | tp->rcv_rtt_last_tsecr = tp->rx_opt.rcv_tsecr;
|
| 901 |
|
| 902 | if (TCP_SKB_CB(skb)->end_seq -
|
| 903 | TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss) {
|
| 904 | s32 delta = tcp_rtt_tsopt_us(tp, 0);
|
| 905 |
|
| 906 | if (delta > 0)
|
| 907 | tcp_rcv_rtt_update(tp, delta, 0);
|
| 908 | }
|
| 909 | }
|
| 910 |
|
| 911 | void tcp_rcvbuf_grow(struct sock *sk, u32 newval)
|
| 912 | {
|
| 913 | const struct net *net = sock_net(sk);
|
| 914 | struct tcp_sock *tp = tcp_sk(sk);
|
| 915 | u32 rcvwin, rcvbuf, cap, oldval;
|
| 916 | u32 rtt_threshold, rtt_us;
|
| 917 | u64 grow;
|
| 918 |
|
| 919 | oldval = tp->rcvq_space.space;
|
| 920 | tp->rcvq_space.space = newval;
|
| 921 |
|
| 922 | if (!READ_ONCE(net->ipv4.sysctl_tcp_moderate_rcvbuf) ||
|
| 923 | (sk->sk_userlocks & SOCK_RCVBUF_LOCK))
|
| 924 | return;
|
| 925 |
|
| 926 |
|
| 927 | rcvwin = newval << 1;
|
| 928 |
|
| 929 | rtt_us = tp->rcv_rtt_est.rtt_us >> 3;
|
| 930 | rtt_threshold = READ_ONCE(net->ipv4.sysctl_tcp_rcvbuf_low_rtt);
|
| 931 | if (rtt_us < rtt_threshold) {
|
| 932 | |
| 933 | * It might take few additional ms to reach 'line rate',
|
| 934 | * but will avoid sk_rcvbuf inflation and poor cache use.
|
| 935 |
|
| 936 | grow = div_u64((u64)rcvwin * rtt_us, rtt_threshold);
|
| 937 | } else {
|
| 938 |
|
| 939 | grow = div_u64(((u64)rcvwin << 1) * (newval - oldval), oldval);
|
| 940 | }
|
| 941 | rcvwin += grow;
|
| 942 |
|
| 943 | if (!RB_EMPTY_ROOT(&tp->out_of_order_queue))
|
| 944 | rcvwin += TCP_SKB_CB(tp->ooo_last_skb)->end_seq - tp->rcv_nxt;
|
| 945 |
|
| 946 | cap = READ_ONCE(net->ipv4.sysctl_tcp_rmem[2]);
|
| 947 |
|
| 948 | rcvbuf = min_t(u32, tcp_space_from_win(sk, rcvwin), cap);
|
| 949 | if (rcvbuf > sk->sk_rcvbuf) {
|
| 950 | WRITE_ONCE(sk->sk_rcvbuf, rcvbuf);
|
| 951 |
|
| 952 | WRITE_ONCE(tp->window_clamp,
|
| 953 | tcp_win_from_space(sk, rcvbuf));
|
| 954 | }
|
| 955 | }
|
| 956 | |
| 957 | * This function should be called every time data is copied to user space.
|
| 958 | * It calculates the appropriate TCP receive buffer space.
|
| 959 |
|
| 960 | void tcp_rcv_space_adjust(struct sock *sk)
|
| 961 | {
|
| 962 | struct tcp_sock *tp = tcp_sk(sk);
|
| 963 | int time, inq, copied;
|
| 964 |
|
| 965 | trace_tcp_rcv_space_adjust(sk);
|
| 966 |
|
| 967 | if (unlikely(!tp->rcv_rtt_est.rtt_us))
|
| 968 | return;
|
| 969 |
|
| 970 | |
| 971 | * Some platforms have expensive ktime_get() implementations.
|
| 972 | * Using the last cached value is enough for DRS.
|
| 973 |
|
| 974 | time = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcvq_space.time);
|
| 975 | if (time < (tp->rcv_rtt_est.rtt_us >> 3))
|
| 976 | return;
|
| 977 |
|
| 978 |
|
| 979 | copied = tp->copied_seq - tp->rcvq_space.seq;
|
| 980 |
|
| 981 | inq = tp->rcv_nxt - tp->copied_seq;
|
| 982 | copied -= inq;
|
| 983 | if (copied <= tp->rcvq_space.space)
|
| 984 | goto new_measure;
|
| 985 |
|
| 986 | trace_tcp_rcvbuf_grow(sk, time);
|
| 987 |
|
| 988 | tcp_rcvbuf_grow(sk, copied);
|
| 989 |
|
| 990 | new_measure:
|
| 991 | tp->rcvq_space.seq = tp->copied_seq;
|
| 992 | tp->rcvq_space.time = tp->tcp_mstamp;
|
| 993 | }
|
| 994 |
|
| 995 | static void tcp_save_lrcv_flowlabel(struct sock *sk, const struct sk_buff *skb)
|
| 996 | {
|
| 997 | #if IS_ENABLED(CONFIG_IPV6)
|
| 998 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 999 |
|
| 1000 | if (skb->protocol == htons(ETH_P_IPV6))
|
| 1001 | icsk->icsk_ack.lrcv_flowlabel = ntohl(ip6_flowlabel(ipv6_hdr(skb)));
|
| 1002 | #endif
|
| 1003 | }
|
| 1004 |
|
| 1005 | |
| 1006 | * behavior of the tp->ato delayed ack timeout interval. When a
|
| 1007 | * connection starts up, we want to ack as quickly as possible. The
|
| 1008 | * problem is that "good" TCP's do slow start at the beginning of data
|
| 1009 | * transmission. The means that until we send the first few ACK's the
|
| 1010 | * sender will sit on his end and only queue most of his data, because
|
| 1011 | * he can only send snd_cwnd unacked packets at any given time. For
|
| 1012 | * each ACK we send, he increments snd_cwnd and transmits more of his
|
| 1013 | * queue. -DaveM
|
| 1014 |
|
| 1015 | static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
|
| 1016 | {
|
| 1017 | struct tcp_sock *tp = tcp_sk(sk);
|
| 1018 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 1019 | u32 now;
|
| 1020 |
|
| 1021 | inet_csk_schedule_ack(sk);
|
| 1022 |
|
| 1023 | tcp_measure_rcv_mss(sk, skb);
|
| 1024 |
|
| 1025 | tcp_rcv_rtt_measure(tp);
|
| 1026 |
|
| 1027 | now = tcp_jiffies32;
|
| 1028 |
|
| 1029 | if (!icsk->icsk_ack.ato) {
|
| 1030 | |
| 1031 | * delayed ACK engine.
|
| 1032 |
|
| 1033 | tcp_incr_quickack(sk, TCP_MAX_QUICKACKS);
|
| 1034 | icsk->icsk_ack.ato = TCP_ATO_MIN;
|
| 1035 | } else {
|
| 1036 | int m = now - icsk->icsk_ack.lrcvtime;
|
| 1037 |
|
| 1038 | if (m <= TCP_ATO_MIN / 2) {
|
| 1039 |
|
| 1040 | icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
|
| 1041 | } else if (m < icsk->icsk_ack.ato) {
|
| 1042 | icsk->icsk_ack.ato = min3((icsk->icsk_ack.ato >> 1) + (u32)m,
|
| 1043 | icsk->icsk_rto,
|
| 1044 | (u32)TCP_DELACK_MAX);
|
| 1045 | } else if (m > icsk->icsk_rto) {
|
| 1046 | |
| 1047 | * restart window, so that we send ACKs quickly.
|
| 1048 |
|
| 1049 | tcp_incr_quickack(sk, TCP_MAX_QUICKACKS);
|
| 1050 | }
|
| 1051 | }
|
| 1052 | icsk->icsk_ack.lrcvtime = now;
|
| 1053 | tcp_save_lrcv_flowlabel(sk, skb);
|
| 1054 |
|
| 1055 | tcp_data_ecn_check(sk, skb);
|
| 1056 |
|
| 1057 | if (skb->len >= 128)
|
| 1058 | tcp_grow_window(sk, skb, true);
|
| 1059 | }
|
| 1060 |
|
| 1061 | |
| 1062 | * routine either comes from timestamps, or from segments that were
|
| 1063 | * known _not_ to have been retransmitted [see Karn/Partridge
|
| 1064 | * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
|
| 1065 | * piece by Van Jacobson.
|
| 1066 | * NOTE: the next three routines used to be one big routine.
|
| 1067 | * To save cycles in the RFC 1323 implementation it was better to break
|
| 1068 | * it up into three procedures. -- erics
|
| 1069 |
|
| 1070 | static void tcp_rtt_estimator(struct sock *sk, long mrtt_us)
|
| 1071 | {
|
| 1072 | struct tcp_sock *tp = tcp_sk(sk);
|
| 1073 | long m = mrtt_us;
|
| 1074 | u32 srtt = tp->srtt_us;
|
| 1075 |
|
| 1076 | |
| 1077 | * article in SIGCOMM '88. Note that rtt and mdev
|
| 1078 | * are scaled versions of rtt and mean deviation.
|
| 1079 | * This is designed to be as fast as possible
|
| 1080 | * m stands for "measurement".
|
| 1081 | *
|
| 1082 | * On a 1990 paper the rto value is changed to:
|
| 1083 | * RTO = rtt + 4 * mdev
|
| 1084 | *
|
| 1085 | * Funny. This algorithm seems to be very broken.
|
| 1086 | * These formulae increase RTO, when it should be decreased, increase
|
| 1087 | * too slowly, when it should be increased quickly, decrease too quickly
|
| 1088 | * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
|
| 1089 | * does not matter how to _calculate_ it. Seems, it was trap
|
| 1090 | * that VJ failed to avoid. 8)
|
| 1091 |
|
| 1092 | if (srtt != 0) {
|
| 1093 | m -= (srtt >> 3);
|
| 1094 | srtt += m;
|
| 1095 | if (m < 0) {
|
| 1096 | m = -m;
|
| 1097 | m -= (tp->mdev_us >> 2);
|
| 1098 | |
| 1099 | * Eifel blocks mdev updates when rtt decreases.
|
| 1100 | * This solution is a bit different: we use finer gain
|
| 1101 | * for mdev in this case (alpha*beta).
|
| 1102 | * Like Eifel it also prevents growth of rto,
|
| 1103 | * but also it limits too fast rto decreases,
|
| 1104 | * happening in pure Eifel.
|
| 1105 |
|
| 1106 | if (m > 0)
|
| 1107 | m >>= 3;
|
| 1108 | } else {
|
| 1109 | m -= (tp->mdev_us >> 2);
|
| 1110 | }
|
| 1111 | tp->mdev_us += m;
|
| 1112 | if (tp->mdev_us > tp->mdev_max_us) {
|
| 1113 | tp->mdev_max_us = tp->mdev_us;
|
| 1114 | if (tp->mdev_max_us > tp->rttvar_us)
|
| 1115 | tp->rttvar_us = tp->mdev_max_us;
|
| 1116 | }
|
| 1117 | if (after(tp->snd_una, tp->rtt_seq)) {
|
| 1118 | if (tp->mdev_max_us < tp->rttvar_us)
|
| 1119 | tp->rttvar_us -= (tp->rttvar_us - tp->mdev_max_us) >> 2;
|
| 1120 | tp->rtt_seq = tp->snd_nxt;
|
| 1121 | tp->mdev_max_us = tcp_rto_min_us(sk);
|
| 1122 |
|
| 1123 | tcp_bpf_rtt(sk, mrtt_us, srtt);
|
| 1124 | }
|
| 1125 | } else {
|
| 1126 |
|
| 1127 | srtt = m << 3;
|
| 1128 | tp->mdev_us = m << 1;
|
| 1129 | tp->rttvar_us = max(tp->mdev_us, tcp_rto_min_us(sk));
|
| 1130 | tp->mdev_max_us = tp->rttvar_us;
|
| 1131 | tp->rtt_seq = tp->snd_nxt;
|
| 1132 |
|
| 1133 | tcp_bpf_rtt(sk, mrtt_us, srtt);
|
| 1134 | }
|
| 1135 | WRITE_ONCE(tp->srtt_us, max(1U, srtt));
|
| 1136 | }
|
| 1137 |
|
| 1138 | void tcp_update_pacing_rate(struct sock *sk)
|
| 1139 | {
|
| 1140 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 1141 | u64 rate;
|
| 1142 |
|
| 1143 |
|
| 1144 | rate = (u64)tp->mss_cache * ((USEC_PER_SEC / 100) << 3);
|
| 1145 |
|
| 1146 | |
| 1147 | * In Slow Start [1], set sk_pacing_rate to 200 % the current rate.
|
| 1148 | * In Congestion Avoidance phase, set it to 120 % the current rate.
|
| 1149 | *
|
| 1150 | * [1] : Normal Slow Start condition is (tp->snd_cwnd < tp->snd_ssthresh)
|
| 1151 | * If snd_cwnd >= (tp->snd_ssthresh / 2), we are approaching
|
| 1152 | * end of slow start and should slow down.
|
| 1153 |
|
| 1154 | if (tcp_snd_cwnd(tp) < tp->snd_ssthresh / 2)
|
| 1155 | rate *= READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_pacing_ss_ratio);
|
| 1156 | else
|
| 1157 | rate *= READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_pacing_ca_ratio);
|
| 1158 |
|
| 1159 | rate *= max(tcp_snd_cwnd(tp), tp->packets_out);
|
| 1160 |
|
| 1161 | if (likely(tp->srtt_us))
|
| 1162 | do_div(rate, tp->srtt_us);
|
| 1163 |
|
| 1164 | |
| 1165 | * without any lock. We want to make sure compiler wont store
|
| 1166 | * intermediate values in this location.
|
| 1167 |
|
| 1168 | WRITE_ONCE(sk->sk_pacing_rate,
|
| 1169 | min_t(u64, rate, READ_ONCE(sk->sk_max_pacing_rate)));
|
| 1170 | }
|
| 1171 |
|
| 1172 | |
| 1173 | * routine referred to above.
|
| 1174 |
|
| 1175 | void tcp_set_rto(struct sock *sk)
|
| 1176 | {
|
| 1177 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 1178 | |
| 1179 | *
|
| 1180 | * More seriously:
|
| 1181 | * 1. If rtt variance happened to be less 50msec, it is hallucination.
|
| 1182 | * It cannot be less due to utterly erratic ACK generation made
|
| 1183 | * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
|
| 1184 | * to do with delayed acks, because at cwnd>2 true delack timeout
|
| 1185 | * is invisible. Actually, Linux-2.4 also generates erratic
|
| 1186 | * ACKs in some circumstances.
|
| 1187 |
|
| 1188 | inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
|
| 1189 |
|
| 1190 | |
| 1191 | * If we do not estimate RTO correctly without them,
|
| 1192 | * all the algo is pure shit and should be replaced
|
| 1193 | * with correct one. It is exactly, which we pretend to do.
|
| 1194 |
|
| 1195 |
|
| 1196 | |
| 1197 | * guarantees that rto is higher.
|
| 1198 |
|
| 1199 | tcp_bound_rto(sk);
|
| 1200 | }
|
| 1201 |
|
| 1202 | __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)
|
| 1203 | {
|
| 1204 | __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
|
| 1205 |
|
| 1206 | if (!cwnd)
|
| 1207 | cwnd = TCP_INIT_CWND;
|
| 1208 | return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
|
| 1209 | }
|
| 1210 |
|
| 1211 | struct tcp_sacktag_state {
|
| 1212 | |
| 1213 | * that was SACKed. RTO needs the earliest RTT to stay conservative,
|
| 1214 | * but congestion control should still get an accurate delay signal.
|
| 1215 |
|
| 1216 | u64 first_sackt;
|
| 1217 | u64 last_sackt;
|
| 1218 | u32 reord;
|
| 1219 | u32 sack_delivered;
|
| 1220 | u32 delivered_bytes;
|
| 1221 | int flag;
|
| 1222 | unsigned int mss_now;
|
| 1223 | struct rate_sample *rate;
|
| 1224 | };
|
| 1225 |
|
| 1226 | |
| 1227 | * and spurious retransmission information if this DSACK is unlikely caused by
|
| 1228 | * sender's action:
|
| 1229 | * - DSACKed sequence range is larger than maximum receiver's window.
|
| 1230 | * - Total no. of DSACKed segments exceed the total no. of retransmitted segs.
|
| 1231 |
|
| 1232 | static u32 tcp_dsack_seen(struct tcp_sock *tp, u32 start_seq,
|
| 1233 | u32 end_seq, struct tcp_sacktag_state *state)
|
| 1234 | {
|
| 1235 | u32 seq_len, dup_segs = 1;
|
| 1236 |
|
| 1237 | if (!before(start_seq, end_seq))
|
| 1238 | return 0;
|
| 1239 |
|
| 1240 | seq_len = end_seq - start_seq;
|
| 1241 |
|
| 1242 | if (seq_len > tp->max_window)
|
| 1243 | return 0;
|
| 1244 | if (seq_len > tp->mss_cache)
|
| 1245 | dup_segs = DIV_ROUND_UP(seq_len, tp->mss_cache);
|
| 1246 | else if (tp->tlp_high_seq && tp->tlp_high_seq == end_seq)
|
| 1247 | state->flag |= FLAG_DSACK_TLP;
|
| 1248 |
|
| 1249 | WRITE_ONCE(tp->dsack_dups, tp->dsack_dups + dup_segs);
|
| 1250 |
|
| 1251 | if (tp->dsack_dups > tp->total_retrans)
|
| 1252 | return 0;
|
| 1253 |
|
| 1254 | tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;
|
| 1255 | |
| 1256 | * DSACKs that may have been due to reordering causing RACK to trigger
|
| 1257 | * a spurious fast recovery. Thus RACK ignores DSACKs that happen
|
| 1258 | * without having seen reordering, or that match TLP probes (TLP
|
| 1259 | * is timer-driven, not triggered by RACK).
|
| 1260 |
|
| 1261 | if (tp->reord_seen && !(state->flag & FLAG_DSACK_TLP))
|
| 1262 | tp->rack.dsack_seen = 1;
|
| 1263 |
|
| 1264 | state->flag |= FLAG_DSACKING_ACK;
|
| 1265 |
|
| 1266 | state->sack_delivered += dup_segs;
|
| 1267 |
|
| 1268 | return dup_segs;
|
| 1269 | }
|
| 1270 |
|
| 1271 | |
| 1272 | * some lower never-retransmitted sequence ("low_seq"). The maximum reordering
|
| 1273 | * distance is approximated in full-mss packet distance ("reordering").
|
| 1274 |
|
| 1275 | static void tcp_check_sack_reordering(struct sock *sk, const u32 low_seq,
|
| 1276 | const int ts)
|
| 1277 | {
|
| 1278 | struct tcp_sock *tp = tcp_sk(sk);
|
| 1279 | const u32 mss = tp->mss_cache;
|
| 1280 | u32 fack, metric;
|
| 1281 |
|
| 1282 | fack = tcp_highest_sack_seq(tp);
|
| 1283 | if (!before(low_seq, fack))
|
| 1284 | return;
|
| 1285 |
|
| 1286 | metric = fack - low_seq;
|
| 1287 | if ((metric > tp->reordering * mss) && mss) {
|
| 1288 | #if FASTRETRANS_DEBUG > 1
|
| 1289 | pr_debug("Disorder%d %d %u f%u s%u rr%d\n",
|
| 1290 | tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
|
| 1291 | tp->reordering,
|
| 1292 | 0,
|
| 1293 | tp->sacked_out,
|
| 1294 | tp->undo_marker ? tp->undo_retrans : 0);
|
| 1295 | #endif
|
| 1296 | WRITE_ONCE(tp->reordering,
|
| 1297 | min_t(u32, (metric + mss - 1) / mss,
|
| 1298 | READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_max_reordering)));
|
| 1299 | }
|
| 1300 |
|
| 1301 |
|
| 1302 | WRITE_ONCE(tp->reord_seen, tp->reord_seen + 1);
|
| 1303 | NET_INC_STATS(sock_net(sk),
|
| 1304 | ts ? LINUX_MIB_TCPTSREORDER : LINUX_MIB_TCPSACKREORDER);
|
| 1305 | }
|
| 1306 |
|
| 1307 | |
| 1308 | * on a new loss, because we want to know if all skbs previously
|
| 1309 | * known to be lost have already been retransmitted, indicating
|
| 1310 | * that this newly lost skb is our next skb to retransmit.
|
| 1311 |
|
| 1312 | static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
|
| 1313 | {
|
| 1314 | if ((!tp->retransmit_skb_hint && tp->retrans_out >= tp->lost_out) ||
|
| 1315 | (tp->retransmit_skb_hint &&
|
| 1316 | before(TCP_SKB_CB(skb)->seq,
|
| 1317 | TCP_SKB_CB(tp->retransmit_skb_hint)->seq)))
|
| 1318 | tp->retransmit_skb_hint = skb;
|
| 1319 | }
|
| 1320 |
|
| 1321 | |
| 1322 | * notify the congestion control module that the given skb was marked lost.
|
| 1323 |
|
| 1324 | static void tcp_notify_skb_loss_event(struct tcp_sock *tp, const struct sk_buff *skb)
|
| 1325 | {
|
| 1326 | tp->lost += tcp_skb_pcount(skb);
|
| 1327 | }
|
| 1328 |
|
| 1329 | void tcp_mark_skb_lost(struct sock *sk, struct sk_buff *skb)
|
| 1330 | {
|
| 1331 | __u8 sacked = TCP_SKB_CB(skb)->sacked;
|
| 1332 | struct tcp_sock *tp = tcp_sk(sk);
|
| 1333 |
|
| 1334 | if (sacked & TCPCB_SACKED_ACKED)
|
| 1335 | return;
|
| 1336 |
|
| 1337 | tcp_verify_retransmit_hint(tp, skb);
|
| 1338 | if (sacked & TCPCB_LOST) {
|
| 1339 | if (sacked & TCPCB_SACKED_RETRANS) {
|
| 1340 |
|
| 1341 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
|
| 1342 | tp->retrans_out -= tcp_skb_pcount(skb);
|
| 1343 | NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT,
|
| 1344 | tcp_skb_pcount(skb));
|
| 1345 | tcp_notify_skb_loss_event(tp, skb);
|
| 1346 | }
|
| 1347 | } else {
|
| 1348 | tp->lost_out += tcp_skb_pcount(skb);
|
| 1349 | TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
|
| 1350 | tcp_notify_skb_loss_event(tp, skb);
|
| 1351 | }
|
| 1352 | }
|
| 1353 |
|
| 1354 | |
| 1355 | *
|
| 1356 | * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
|
| 1357 | * Packets in queue with these bits set are counted in variables
|
| 1358 | * sacked_out, retrans_out and lost_out, correspondingly.
|
| 1359 | *
|
| 1360 | * Valid combinations are:
|
| 1361 | * Tag InFlight Description
|
| 1362 | * 0 1 - orig segment is in flight.
|
| 1363 | * S 0 - nothing flies, orig reached receiver.
|
| 1364 | * L 0 - nothing flies, orig lost by net.
|
| 1365 | * R 2 - both orig and retransmit are in flight.
|
| 1366 | * L|R 1 - orig is lost, retransmit is in flight.
|
| 1367 | * S|R 1 - orig reached receiver, retrans is still in flight.
|
| 1368 | * (L|S|R is logically valid, it could occur when L|R is sacked,
|
| 1369 | * but it is equivalent to plain S and code short-circuits it to S.
|
| 1370 | * L|S is logically invalid, it would mean -1 packet in flight 8))
|
| 1371 | *
|
| 1372 | * These 6 states form finite state machine, controlled by the following events:
|
| 1373 | * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
|
| 1374 | * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
|
| 1375 | * 3. Loss detection event of two flavors:
|
| 1376 | * A. Scoreboard estimator decided the packet is lost.
|
| 1377 | * A'. Reno "three dupacks" marks head of queue lost.
|
| 1378 | * B. SACK arrives sacking SND.NXT at the moment, when the
|
| 1379 | * segment was retransmitted.
|
| 1380 | * 4. D-SACK added new rule: D-SACK changes any tag to S.
|
| 1381 | *
|
| 1382 | * It is pleasant to note, that state diagram turns out to be commutative,
|
| 1383 | * so that we are allowed not to be bothered by order of our actions,
|
| 1384 | * when multiple events arrive simultaneously. (see the function below).
|
| 1385 | *
|
| 1386 | * Reordering detection.
|
| 1387 | * --------------------
|
| 1388 | * Reordering metric is maximal distance, which a packet can be displaced
|
| 1389 | * in packet stream. With SACKs we can estimate it:
|
| 1390 | *
|
| 1391 | * 1. SACK fills old hole and the corresponding segment was not
|
| 1392 | * ever retransmitted -> reordering. Alas, we cannot use it
|
| 1393 | * when segment was retransmitted.
|
| 1394 | * 2. The last flaw is solved with D-SACK. D-SACK arrives
|
| 1395 | * for retransmitted and already SACKed segment -> reordering..
|
| 1396 | * Both of these heuristics are not used in Loss state, when we cannot
|
| 1397 | * account for retransmits accurately.
|
| 1398 | *
|
| 1399 | * SACK block validation.
|
| 1400 | * ----------------------
|
| 1401 | *
|
| 1402 | * SACK block range validation checks that the received SACK block fits to
|
| 1403 | * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
|
| 1404 | * Note that SND.UNA is not included to the range though being valid because
|
| 1405 | * it means that the receiver is rather inconsistent with itself reporting
|
| 1406 | * SACK reneging when it should advance SND.UNA. Such SACK block this is
|
| 1407 | * perfectly valid, however, in light of RFC2018 which explicitly states
|
| 1408 | * that "SACK block MUST reflect the newest segment. Even if the newest
|
| 1409 | * segment is going to be discarded ...", not that it looks very clever
|
| 1410 | * in case of head skb. Due to potentional receiver driven attacks, we
|
| 1411 | * choose to avoid immediate execution of a walk in write queue due to
|
| 1412 | * reneging and defer head skb's loss recovery to standard loss recovery
|
| 1413 | * procedure that will eventually trigger (nothing forbids us doing this).
|
| 1414 | *
|
| 1415 | * Implements also blockage to start_seq wrap-around. Problem lies in the
|
| 1416 | * fact that though start_seq (s) is before end_seq (i.e., not reversed),
|
| 1417 | * there's no guarantee that it will be before snd_nxt (n). The problem
|
| 1418 | * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
|
| 1419 | * wrap (s_w):
|
| 1420 | *
|
| 1421 | * <- outs wnd -> <- wrapzone ->
|
| 1422 | * u e n u_w e_w s n_w
|
| 1423 | * | | | | | | |
|
| 1424 | * |<------------+------+----- TCP seqno space --------------+---------->|
|
| 1425 | * ...-- <2^31 ->| |<--------...
|
| 1426 | * ...---- >2^31 ------>| |<--------...
|
| 1427 | *
|
| 1428 | * Current code wouldn't be vulnerable but it's better still to discard such
|
| 1429 | * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
|
| 1430 | * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
|
| 1431 | * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
|
| 1432 | * equal to the ideal case (infinite seqno space without wrap caused issues).
|
| 1433 | *
|
| 1434 | * With D-SACK the lower bound is extended to cover sequence space below
|
| 1435 | * SND.UNA down to undo_marker, which is the last point of interest. Yet
|
| 1436 | * again, D-SACK block must not to go across snd_una (for the same reason as
|
| 1437 | * for the normal SACK blocks, explained above). But there all simplicity
|
| 1438 | * ends, TCP might receive valid D-SACKs below that. As long as they reside
|
| 1439 | * fully below undo_marker they do not affect behavior in anyway and can
|
| 1440 | * therefore be safely ignored. In rare cases (which are more or less
|
| 1441 | * theoretical ones), the D-SACK will nicely cross that boundary due to skb
|
| 1442 | * fragmentation and packet reordering past skb's retransmission. To consider
|
| 1443 | * them correctly, the acceptable range must be extended even more though
|
| 1444 | * the exact amount is rather hard to quantify. However, tp->max_window can
|
| 1445 | * be used as an exaggerated estimate.
|
| 1446 |
|
| 1447 | static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack,
|
| 1448 | u32 start_seq, u32 end_seq)
|
| 1449 | {
|
| 1450 |
|
| 1451 | if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
|
| 1452 | return false;
|
| 1453 |
|
| 1454 |
|
| 1455 | if (!before(start_seq, tp->snd_nxt))
|
| 1456 | return false;
|
| 1457 |
|
| 1458 | |
| 1459 | * start_seq == snd_una is non-sensical (see comments above)
|
| 1460 |
|
| 1461 | if (after(start_seq, tp->snd_una))
|
| 1462 | return true;
|
| 1463 |
|
| 1464 | if (!is_dsack || !tp->undo_marker)
|
| 1465 | return false;
|
| 1466 |
|
| 1467 |
|
| 1468 | if (after(end_seq, tp->snd_una))
|
| 1469 | return false;
|
| 1470 |
|
| 1471 | if (!before(start_seq, tp->undo_marker))
|
| 1472 | return true;
|
| 1473 |
|
| 1474 |
|
| 1475 | if (!after(end_seq, tp->undo_marker))
|
| 1476 | return false;
|
| 1477 |
|
| 1478 | |
| 1479 | * start_seq < undo_marker and end_seq >= undo_marker.
|
| 1480 |
|
| 1481 | return !before(start_seq, end_seq - tp->max_window);
|
| 1482 | }
|
| 1483 |
|
| 1484 | static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb,
|
| 1485 | struct tcp_sack_block_wire *sp, int num_sacks,
|
| 1486 | u32 prior_snd_una, struct tcp_sacktag_state *state)
|
| 1487 | {
|
| 1488 | struct tcp_sock *tp = tcp_sk(sk);
|
| 1489 | u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
|
| 1490 | u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
|
| 1491 | u32 dup_segs;
|
| 1492 |
|
| 1493 | if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
|
| 1494 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
|
| 1495 | } else if (num_sacks > 1) {
|
| 1496 | u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
|
| 1497 | u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
|
| 1498 |
|
| 1499 | if (after(end_seq_0, end_seq_1) || before(start_seq_0, start_seq_1))
|
| 1500 | return false;
|
| 1501 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKOFORECV);
|
| 1502 | } else {
|
| 1503 | return false;
|
| 1504 | }
|
| 1505 |
|
| 1506 | dup_segs = tcp_dsack_seen(tp, start_seq_0, end_seq_0, state);
|
| 1507 | if (!dup_segs) {
|
| 1508 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKIGNOREDDUBIOUS);
|
| 1509 | return false;
|
| 1510 | }
|
| 1511 |
|
| 1512 | NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPDSACKRECVSEGS, dup_segs);
|
| 1513 |
|
| 1514 |
|
| 1515 | if (tp->undo_marker && tp->undo_retrans > 0 &&
|
| 1516 | !after(end_seq_0, prior_snd_una) &&
|
| 1517 | after(end_seq_0, tp->undo_marker))
|
| 1518 | tp->undo_retrans = max_t(int, 0, tp->undo_retrans - dup_segs);
|
| 1519 |
|
| 1520 | return true;
|
| 1521 | }
|
| 1522 |
|
| 1523 | |
| 1524 | * the incoming SACK may not exactly match but we can find smaller MSS
|
| 1525 | * aligned portion of it that matches. Therefore we might need to fragment
|
| 1526 | * which may fail and creates some hassle (caller must handle error case
|
| 1527 | * returns).
|
| 1528 | *
|
| 1529 | * FIXME: this could be merged to shift decision code
|
| 1530 |
|
| 1531 | static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
|
| 1532 | u32 start_seq, u32 end_seq)
|
| 1533 | {
|
| 1534 | int err;
|
| 1535 | bool in_sack;
|
| 1536 | unsigned int pkt_len;
|
| 1537 | unsigned int mss;
|
| 1538 |
|
| 1539 | in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
|
| 1540 | !before(end_seq, TCP_SKB_CB(skb)->end_seq);
|
| 1541 |
|
| 1542 | if (tcp_skb_pcount(skb) > 1 && !in_sack &&
|
| 1543 | after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
|
| 1544 | mss = tcp_skb_mss(skb);
|
| 1545 | in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
|
| 1546 |
|
| 1547 | if (!in_sack) {
|
| 1548 | pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
|
| 1549 | if (pkt_len < mss)
|
| 1550 | pkt_len = mss;
|
| 1551 | } else {
|
| 1552 | pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
|
| 1553 | if (pkt_len < mss)
|
| 1554 | return -EINVAL;
|
| 1555 | }
|
| 1556 |
|
| 1557 | |
| 1558 | * and/or the remaining small portion (if present)
|
| 1559 |
|
| 1560 | if (pkt_len > mss) {
|
| 1561 | unsigned int new_len = (pkt_len / mss) * mss;
|
| 1562 | if (!in_sack && new_len < pkt_len)
|
| 1563 | new_len += mss;
|
| 1564 | pkt_len = new_len;
|
| 1565 | }
|
| 1566 |
|
| 1567 | if (pkt_len >= skb->len && !in_sack)
|
| 1568 | return 0;
|
| 1569 |
|
| 1570 | err = tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb,
|
| 1571 | pkt_len, mss, GFP_ATOMIC);
|
| 1572 | if (err < 0)
|
| 1573 | return err;
|
| 1574 | }
|
| 1575 |
|
| 1576 | return in_sack;
|
| 1577 | }
|
| 1578 |
|
| 1579 | |
| 1580 | * This is "Step 3: Advance RACK.xmit_time and update RACK.RTT" from
|
| 1581 | * draft-cheng-tcpm-rack-00.txt
|
| 1582 |
|
| 1583 | static void tcp_rack_advance(struct tcp_sock *tp, u8 sacked,
|
| 1584 | u32 end_seq, u64 xmit_time)
|
| 1585 | {
|
| 1586 | u32 rtt_us;
|
| 1587 |
|
| 1588 | rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, xmit_time);
|
| 1589 | if (rtt_us < tcp_min_rtt(tp) && (sacked & TCPCB_RETRANS)) {
|
| 1590 | |
| 1591 | * whether the retransmission or the original (or the prior
|
| 1592 | * retransmission) was sacked.
|
| 1593 | *
|
| 1594 | * If the original is lost, there is no ambiguity. Otherwise
|
| 1595 | * we assume the original can be delayed up to aRTT + min_rtt.
|
| 1596 | * the aRTT term is bounded by the fast recovery or timeout,
|
| 1597 | * so it's at least one RTT (i.e., retransmission is at least
|
| 1598 | * an RTT later).
|
| 1599 |
|
| 1600 | return;
|
| 1601 | }
|
| 1602 | tp->rack.advanced = 1;
|
| 1603 | tp->rack.rtt_us = rtt_us;
|
| 1604 | if (tcp_skb_sent_after(xmit_time, tp->rack.mstamp,
|
| 1605 | end_seq, tp->rack.end_seq)) {
|
| 1606 | tp->rack.mstamp = xmit_time;
|
| 1607 | tp->rack.end_seq = end_seq;
|
| 1608 | }
|
| 1609 | }
|
| 1610 |
|
| 1611 |
|
| 1612 | static u8 tcp_sacktag_one(struct sock *sk,
|
| 1613 | struct tcp_sacktag_state *state, u8 sacked,
|
| 1614 | u32 start_seq, u32 end_seq,
|
| 1615 | int dup_sack, int pcount, u32 plen,
|
| 1616 | u64 xmit_time)
|
| 1617 | {
|
| 1618 | struct tcp_sock *tp = tcp_sk(sk);
|
| 1619 |
|
| 1620 |
|
| 1621 | if (dup_sack && (sacked & TCPCB_RETRANS)) {
|
| 1622 | if (tp->undo_marker && tp->undo_retrans > 0 &&
|
| 1623 | after(end_seq, tp->undo_marker))
|
| 1624 | tp->undo_retrans = max_t(int, 0, tp->undo_retrans - pcount);
|
| 1625 | if ((sacked & TCPCB_SACKED_ACKED) &&
|
| 1626 | before(start_seq, state->reord))
|
| 1627 | state->reord = start_seq;
|
| 1628 | }
|
| 1629 |
|
| 1630 |
|
| 1631 | if (!after(end_seq, tp->snd_una))
|
| 1632 | return sacked;
|
| 1633 |
|
| 1634 | if (!(sacked & TCPCB_SACKED_ACKED)) {
|
| 1635 | tcp_rack_advance(tp, sacked, end_seq, xmit_time);
|
| 1636 |
|
| 1637 | if (sacked & TCPCB_SACKED_RETRANS) {
|
| 1638 | |
| 1639 | * we do not clear RETRANS, believing
|
| 1640 | * that retransmission is still in flight.
|
| 1641 |
|
| 1642 | if (sacked & TCPCB_LOST) {
|
| 1643 | sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
|
| 1644 | tp->lost_out -= pcount;
|
| 1645 | tp->retrans_out -= pcount;
|
| 1646 | }
|
| 1647 | } else {
|
| 1648 | if (!(sacked & TCPCB_RETRANS)) {
|
| 1649 | |
| 1650 | * which was in hole. It is reordering.
|
| 1651 |
|
| 1652 | if (before(start_seq,
|
| 1653 | tcp_highest_sack_seq(tp)) &&
|
| 1654 | before(start_seq, state->reord))
|
| 1655 | state->reord = start_seq;
|
| 1656 |
|
| 1657 | if (!after(end_seq, tp->high_seq))
|
| 1658 | state->flag |= FLAG_ORIG_SACK_ACKED;
|
| 1659 | if (state->first_sackt == 0)
|
| 1660 | state->first_sackt = xmit_time;
|
| 1661 | state->last_sackt = xmit_time;
|
| 1662 | }
|
| 1663 |
|
| 1664 | if (sacked & TCPCB_LOST) {
|
| 1665 | sacked &= ~TCPCB_LOST;
|
| 1666 | tp->lost_out -= pcount;
|
| 1667 | }
|
| 1668 | }
|
| 1669 |
|
| 1670 | sacked |= TCPCB_SACKED_ACKED;
|
| 1671 | state->flag |= FLAG_DATA_SACKED;
|
| 1672 | tp->sacked_out += pcount;
|
| 1673 |
|
| 1674 | state->sack_delivered += pcount;
|
| 1675 | state->delivered_bytes += plen;
|
| 1676 | }
|
| 1677 |
|
| 1678 | |
| 1679 | * frames and clear it. undo_retrans is decreased above, L|R frames
|
| 1680 | * are accounted above as well.
|
| 1681 |
|
| 1682 | if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
|
| 1683 | sacked &= ~TCPCB_SACKED_RETRANS;
|
| 1684 | tp->retrans_out -= pcount;
|
| 1685 | }
|
| 1686 |
|
| 1687 | return sacked;
|
| 1688 | }
|
| 1689 |
|
| 1690 | |
| 1691 | * can currently deliver outbound data packets for this flow. At a high
|
| 1692 | * level, it operates by taking a delivery rate sample for each ACK.
|
| 1693 | *
|
| 1694 | * A rate sample records the rate at which the network delivered packets
|
| 1695 | * for this flow, calculated over the time interval between the transmission
|
| 1696 | * of a data packet and the acknowledgment of that packet.
|
| 1697 | *
|
| 1698 | * Specifically, over the interval between each transmit and corresponding ACK,
|
| 1699 | * the estimator generates a delivery rate sample. Typically it uses the rate
|
| 1700 | * at which packets were acknowledged. However, the approach of using only the
|
| 1701 | * acknowledgment rate faces a challenge under the prevalent ACK decimation or
|
| 1702 | * compression: packets can temporarily appear to be delivered much quicker
|
| 1703 | * than the bottleneck rate. Since it is physically impossible to do that in a
|
| 1704 | * sustained fashion, when the estimator notices that the ACK rate is faster
|
| 1705 | * than the transmit rate, it uses the latter:
|
| 1706 | *
|
| 1707 | * send_rate = #pkts_delivered/(last_snd_time - first_snd_time)
|
| 1708 | * ack_rate = #pkts_delivered/(last_ack_time - first_ack_time)
|
| 1709 | * bw = min(send_rate, ack_rate)
|
| 1710 | *
|
| 1711 | * Notice the estimator essentially estimates the goodput, not always the
|
| 1712 | * network bottleneck link rate when the sending or receiving is limited by
|
| 1713 | * other factors like applications or receiver window limits. The estimator
|
| 1714 | * deliberately avoids using the inter-packet spacing approach because that
|
| 1715 | * approach requires a large number of samples and sophisticated filtering.
|
| 1716 | *
|
| 1717 | * TCP flows can often be application-limited in request/response workloads.
|
| 1718 | * The estimator marks a bandwidth sample as application-limited if there
|
| 1719 | * was some moment during the sampled window of packets when there was no data
|
| 1720 | * ready to send in the write queue.
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 | static void tcp_rate_gen(struct sock *sk, u32 delivered, u32 lost,
|
| 1725 | bool is_sack_reneg, struct rate_sample *rs)
|
| 1726 | {
|
| 1727 | struct tcp_sock *tp = tcp_sk(sk);
|
| 1728 | u32 snd_us, ack_us;
|
| 1729 |
|
| 1730 |
|
| 1731 | if (tp->app_limited && after(tp->delivered, tp->app_limited))
|
| 1732 | tp->app_limited = 0;
|
| 1733 |
|
| 1734 | |
| 1735 | * current time. Refactor the code using a new "tcp_acktag_state"
|
| 1736 | * to carry current time, flags, stats like "tcp_sacktag_state".
|
| 1737 |
|
| 1738 | if (delivered)
|
| 1739 | tp->delivered_mstamp = tp->tcp_mstamp;
|
| 1740 |
|
| 1741 | rs->acked_sacked = delivered;
|
| 1742 | rs->losses = lost;
|
| 1743 | |
| 1744 | * in recovery from loss with SACK reneging. Rate samples taken during
|
| 1745 | * a SACK reneging event may overestimate bw by including packets that
|
| 1746 | * were SACKed before the reneg.
|
| 1747 |
|
| 1748 | if (!rs->prior_mstamp || is_sack_reneg) {
|
| 1749 | rs->delivered = -1;
|
| 1750 | rs->interval_us = -1;
|
| 1751 | return;
|
| 1752 | }
|
| 1753 | rs->delivered = tp->delivered - rs->prior_delivered;
|
| 1754 |
|
| 1755 | rs->delivered_ce = tp->delivered_ce - rs->prior_delivered_ce;
|
| 1756 |
|
| 1757 | rs->delivered_ce &= TCPCB_DELIVERED_CE_MASK;
|
| 1758 |
|
| 1759 | |
| 1760 | * for a window. Usually the ACK phase is longer, but with ACK
|
| 1761 | * compression the send phase can be longer. To be safe we use the
|
| 1762 | * longer phase.
|
| 1763 |
|
| 1764 | snd_us = rs->interval_us;
|
| 1765 | ack_us = tcp_stamp_us_delta(tp->tcp_mstamp,
|
| 1766 | rs->prior_mstamp);
|
| 1767 | rs->interval_us = max(snd_us, ack_us);
|
| 1768 |
|
| 1769 |
|
| 1770 | rs->snd_interval_us = snd_us;
|
| 1771 | rs->rcv_interval_us = ack_us;
|
| 1772 |
|
| 1773 | |
| 1774 | * Note that rate may still be over-estimated when a spuriously
|
| 1775 | * retransmistted skb was first (s)acked because "interval_us"
|
| 1776 | * is under-estimated (up to an RTT). However continuously
|
| 1777 | * measuring the delivery rate during loss recovery is crucial
|
| 1778 | * for connections suffer heavy or prolonged losses.
|
| 1779 |
|
| 1780 | if (unlikely(rs->interval_us < tcp_min_rtt(tp))) {
|
| 1781 | if (!rs->is_retrans)
|
| 1782 | pr_debug("tcp rate: %ld %d %u %u %u\n",
|
| 1783 | rs->interval_us, rs->delivered,
|
| 1784 | inet_csk(sk)->icsk_ca_state,
|
| 1785 | tp->rx_opt.sack_ok, tcp_min_rtt(tp));
|
| 1786 | rs->interval_us = -1;
|
| 1787 | return;
|
| 1788 | }
|
| 1789 |
|
| 1790 |
|
| 1791 | if (!rs->is_app_limited ||
|
| 1792 | ((u64)rs->delivered * tp->rate_interval_us >=
|
| 1793 | (u64)tp->rate_delivered * rs->interval_us)) {
|
| 1794 | tp->rate_delivered = rs->delivered;
|
| 1795 | tp->rate_interval_us = rs->interval_us;
|
| 1796 | tp->rate_app_limited = rs->is_app_limited;
|
| 1797 | }
|
| 1798 | }
|
| 1799 |
|
| 1800 | |
| 1801 | * delivery information when the skb was last transmitted.
|
| 1802 | *
|
| 1803 | * If an ACK (s)acks multiple skbs (e.g., stretched-acks), this function is
|
| 1804 | * called multiple times. We favor the information from the most recently
|
| 1805 | * sent skb, i.e., the skb with the most recently sent time and the highest
|
| 1806 | * sequence.
|
| 1807 |
|
| 1808 | static void tcp_rate_skb_delivered(struct sock *sk, struct sk_buff *skb,
|
| 1809 | struct rate_sample *rs)
|
| 1810 | {
|
| 1811 | struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
|
| 1812 | struct tcp_sock *tp = tcp_sk(sk);
|
| 1813 | u64 tx_tstamp;
|
| 1814 |
|
| 1815 | if (!scb->tx.delivered_mstamp)
|
| 1816 | return;
|
| 1817 |
|
| 1818 | tx_tstamp = tcp_skb_timestamp_us(skb);
|
| 1819 | if (!rs->prior_delivered ||
|
| 1820 | tcp_skb_sent_after(tx_tstamp, tp->first_tx_mstamp,
|
| 1821 | scb->end_seq, rs->last_end_seq)) {
|
| 1822 | rs->prior_delivered_ce = scb->tx.delivered_ce;
|
| 1823 | rs->prior_delivered = scb->tx.delivered;
|
| 1824 | rs->prior_mstamp = scb->tx.delivered_mstamp;
|
| 1825 | rs->is_app_limited = scb->tx.is_app_limited;
|
| 1826 | rs->is_retrans = scb->sacked & TCPCB_RETRANS;
|
| 1827 | rs->last_end_seq = scb->end_seq;
|
| 1828 |
|
| 1829 |
|
| 1830 | tp->first_tx_mstamp = tx_tstamp;
|
| 1831 |
|
| 1832 | rs->interval_us = tcp_stamp_us_delta(tp->first_tx_mstamp,
|
| 1833 | scb->tx.first_tx_mstamp);
|
| 1834 |
|
| 1835 | }
|
| 1836 | |
| 1837 | * used again when it's cumulatively acked. For acked packets
|
| 1838 | * we don't need to reset since it'll be freed soon.
|
| 1839 |
|
| 1840 | if (scb->sacked & TCPCB_SACKED_ACKED)
|
| 1841 | scb->tx.delivered_mstamp = 0;
|
| 1842 | }
|
| 1843 |
|
| 1844 | |
| 1845 | * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
|
| 1846 |
|
| 1847 | static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *prev,
|
| 1848 | struct sk_buff *skb,
|
| 1849 | struct tcp_sacktag_state *state,
|
| 1850 | unsigned int pcount, int shifted, int mss,
|
| 1851 | bool dup_sack)
|
| 1852 | {
|
| 1853 | struct tcp_sock *tp = tcp_sk(sk);
|
| 1854 | u32 start_seq = TCP_SKB_CB(skb)->seq;
|
| 1855 | u32 end_seq = start_seq + shifted;
|
| 1856 |
|
| 1857 | BUG_ON(!pcount);
|
| 1858 |
|
| 1859 | |
| 1860 | * range but discard the return value since prev is already
|
| 1861 | * marked. We must tag the range first because the seq
|
| 1862 | * advancement below implicitly advances
|
| 1863 | * tcp_highest_sack_seq() when skb is highest_sack.
|
| 1864 |
|
| 1865 | tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked,
|
| 1866 | start_seq, end_seq, dup_sack, pcount, skb->len,
|
| 1867 | tcp_skb_timestamp_us(skb));
|
| 1868 | tcp_rate_skb_delivered(sk, skb, state->rate);
|
| 1869 |
|
| 1870 | TCP_SKB_CB(prev)->end_seq += shifted;
|
| 1871 | TCP_SKB_CB(skb)->seq += shifted;
|
| 1872 |
|
| 1873 | tcp_skb_pcount_add(prev, pcount);
|
| 1874 | WARN_ON_ONCE(tcp_skb_pcount(skb) < pcount);
|
| 1875 | tcp_skb_pcount_add(skb, -pcount);
|
| 1876 |
|
| 1877 | |
| 1878 | * in theory this shouldn't be necessary but as long as DSACK
|
| 1879 | * code can come after this skb later on it's better to keep
|
| 1880 | * setting gso_size to something.
|
| 1881 |
|
| 1882 | if (!TCP_SKB_CB(prev)->tcp_gso_size)
|
| 1883 | TCP_SKB_CB(prev)->tcp_gso_size = mss;
|
| 1884 |
|
| 1885 |
|
| 1886 | if (tcp_skb_pcount(skb) <= 1)
|
| 1887 | TCP_SKB_CB(skb)->tcp_gso_size = 0;
|
| 1888 |
|
| 1889 |
|
| 1890 | TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
|
| 1891 |
|
| 1892 | if (skb->len > 0) {
|
| 1893 | BUG_ON(!tcp_skb_pcount(skb));
|
| 1894 | NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTED);
|
| 1895 | return false;
|
| 1896 | }
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 | if (skb == tp->retransmit_skb_hint)
|
| 1901 | tp->retransmit_skb_hint = prev;
|
| 1902 |
|
| 1903 | TCP_SKB_CB(prev)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags;
|
| 1904 | TCP_SKB_CB(prev)->eor = TCP_SKB_CB(skb)->eor;
|
| 1905 | if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
|
| 1906 | TCP_SKB_CB(prev)->end_seq++;
|
| 1907 |
|
| 1908 | if (skb == tcp_highest_sack(sk))
|
| 1909 | tcp_advance_highest_sack(sk, skb);
|
| 1910 |
|
| 1911 | tcp_skb_collapse_tstamp(prev, skb);
|
| 1912 | if (unlikely(TCP_SKB_CB(prev)->tx.delivered_mstamp))
|
| 1913 | TCP_SKB_CB(prev)->tx.delivered_mstamp = 0;
|
| 1914 |
|
| 1915 | tcp_rtx_queue_unlink_and_free(skb, sk);
|
| 1916 |
|
| 1917 | NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKMERGED);
|
| 1918 |
|
| 1919 | return true;
|
| 1920 | }
|
| 1921 |
|
| 1922 | |
| 1923 | * something-or-zero which complicates things
|
| 1924 |
|
| 1925 | static int tcp_skb_seglen(const struct sk_buff *skb)
|
| 1926 | {
|
| 1927 | return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
|
| 1928 | }
|
| 1929 |
|
| 1930 |
|
| 1931 | static int skb_can_shift(const struct sk_buff *skb)
|
| 1932 | {
|
| 1933 | return !skb_headlen(skb) && skb_is_nonlinear(skb);
|
| 1934 | }
|
| 1935 |
|
| 1936 | int tcp_skb_shift(struct sk_buff *to, struct sk_buff *from,
|
| 1937 | int pcount, int shiftlen)
|
| 1938 | {
|
| 1939 | |
| 1940 | * Since TCP_SKB_CB(skb)->tcp_gso_segs is 16 bits, we need
|
| 1941 | * to make sure not storing more than 65535 * 8 bytes per skb,
|
| 1942 | * even if current MSS is bigger.
|
| 1943 |
|
| 1944 | if (unlikely(to->len + shiftlen >= 65535 * TCP_MIN_GSO_SIZE))
|
| 1945 | return 0;
|
| 1946 | if (unlikely(tcp_skb_pcount(to) + pcount > 65535))
|
| 1947 | return 0;
|
| 1948 | return skb_shift(to, from, shiftlen);
|
| 1949 | }
|
| 1950 |
|
| 1951 | |
| 1952 | * skb.
|
| 1953 |
|
| 1954 | static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
|
| 1955 | struct tcp_sacktag_state *state,
|
| 1956 | u32 start_seq, u32 end_seq,
|
| 1957 | bool dup_sack)
|
| 1958 | {
|
| 1959 | struct tcp_sock *tp = tcp_sk(sk);
|
| 1960 | struct sk_buff *prev;
|
| 1961 | int mss;
|
| 1962 | int pcount = 0;
|
| 1963 | int len;
|
| 1964 | int in_sack;
|
| 1965 |
|
| 1966 |
|
| 1967 | if (!dup_sack &&
|
| 1968 | (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
|
| 1969 | goto fallback;
|
| 1970 | if (!skb_can_shift(skb))
|
| 1971 | goto fallback;
|
| 1972 |
|
| 1973 | if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
|
| 1974 | goto fallback;
|
| 1975 |
|
| 1976 |
|
| 1977 | prev = skb_rb_prev(skb);
|
| 1978 | if (!prev)
|
| 1979 | goto fallback;
|
| 1980 |
|
| 1981 | if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
|
| 1982 | goto fallback;
|
| 1983 |
|
| 1984 | if (!tcp_skb_can_collapse(prev, skb))
|
| 1985 | goto fallback;
|
| 1986 |
|
| 1987 | in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
|
| 1988 | !before(end_seq, TCP_SKB_CB(skb)->end_seq);
|
| 1989 |
|
| 1990 | if (in_sack) {
|
| 1991 | len = skb->len;
|
| 1992 | pcount = tcp_skb_pcount(skb);
|
| 1993 | mss = tcp_skb_seglen(skb);
|
| 1994 |
|
| 1995 | |
| 1996 | * drop this restriction as unnecessary
|
| 1997 |
|
| 1998 | if (mss != tcp_skb_seglen(prev))
|
| 1999 | goto fallback;
|
| 2000 | } else {
|
| 2001 | if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
|
| 2002 | goto noop;
|
| 2003 | |
| 2004 | * cause skipped skbs due to advancing loop btw, original
|
| 2005 | * has that feature too
|
| 2006 |
|
| 2007 | if (tcp_skb_pcount(skb) <= 1)
|
| 2008 | goto noop;
|
| 2009 |
|
| 2010 | in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
|
| 2011 | if (!in_sack) {
|
| 2012 | |
| 2013 | * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
|
| 2014 | * though it might not be worth of the additional hassle
|
| 2015 | *
|
| 2016 | * ...we can probably just fallback to what was done
|
| 2017 | * previously. We could try merging non-SACKed ones
|
| 2018 | * as well but it probably isn't going to buy off
|
| 2019 | * because later SACKs might again split them, and
|
| 2020 | * it would make skb timestamp tracking considerably
|
| 2021 | * harder problem.
|
| 2022 |
|
| 2023 | goto fallback;
|
| 2024 | }
|
| 2025 |
|
| 2026 | len = end_seq - TCP_SKB_CB(skb)->seq;
|
| 2027 | BUG_ON(len < 0);
|
| 2028 | BUG_ON(len > skb->len);
|
| 2029 |
|
| 2030 | |
| 2031 | * severely break even though it makes things bit trickier.
|
| 2032 | * Optimize common case to avoid most of the divides
|
| 2033 |
|
| 2034 | mss = tcp_skb_mss(skb);
|
| 2035 |
|
| 2036 | |
| 2037 | * drop this restriction as unnecessary
|
| 2038 |
|
| 2039 | if (mss != tcp_skb_seglen(prev))
|
| 2040 | goto fallback;
|
| 2041 |
|
| 2042 | if (len == mss) {
|
| 2043 | pcount = 1;
|
| 2044 | } else if (len < mss) {
|
| 2045 | goto noop;
|
| 2046 | } else {
|
| 2047 | pcount = len / mss;
|
| 2048 | len = pcount * mss;
|
| 2049 | }
|
| 2050 | }
|
| 2051 |
|
| 2052 |
|
| 2053 | if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
|
| 2054 | goto fallback;
|
| 2055 |
|
| 2056 | if (!tcp_skb_shift(prev, skb, pcount, len))
|
| 2057 | goto fallback;
|
| 2058 | if (!tcp_shifted_skb(sk, prev, skb, state, pcount, len, mss, dup_sack))
|
| 2059 | goto out;
|
| 2060 |
|
| 2061 | |
| 2062 | * useful when hole on every nth skb pattern happens
|
| 2063 |
|
| 2064 | skb = skb_rb_next(prev);
|
| 2065 | if (!skb)
|
| 2066 | goto out;
|
| 2067 |
|
| 2068 | if (!skb_can_shift(skb) ||
|
| 2069 | ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
|
| 2070 | (mss != tcp_skb_seglen(skb)))
|
| 2071 | goto out;
|
| 2072 |
|
| 2073 | if (!tcp_skb_can_collapse(prev, skb))
|
| 2074 | goto out;
|
| 2075 | len = skb->len;
|
| 2076 | pcount = tcp_skb_pcount(skb);
|
| 2077 | if (tcp_skb_shift(prev, skb, pcount, len))
|
| 2078 | tcp_shifted_skb(sk, prev, skb, state, pcount,
|
| 2079 | len, mss, 0);
|
| 2080 |
|
| 2081 | out:
|
| 2082 | return prev;
|
| 2083 |
|
| 2084 | noop:
|
| 2085 | return skb;
|
| 2086 |
|
| 2087 | fallback:
|
| 2088 | NET_INC_STATS(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
|
| 2089 | return NULL;
|
| 2090 | }
|
| 2091 |
|
| 2092 | static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
|
| 2093 | struct tcp_sack_block *next_dup,
|
| 2094 | struct tcp_sacktag_state *state,
|
| 2095 | u32 start_seq, u32 end_seq,
|
| 2096 | bool dup_sack_in)
|
| 2097 | {
|
| 2098 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2099 | struct sk_buff *tmp;
|
| 2100 |
|
| 2101 | skb_rbtree_walk_from(skb) {
|
| 2102 | int in_sack = 0;
|
| 2103 | bool dup_sack = dup_sack_in;
|
| 2104 |
|
| 2105 |
|
| 2106 | if (!before(TCP_SKB_CB(skb)->seq, end_seq))
|
| 2107 | break;
|
| 2108 |
|
| 2109 | if (next_dup &&
|
| 2110 | before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
|
| 2111 | in_sack = tcp_match_skb_to_sack(sk, skb,
|
| 2112 | next_dup->start_seq,
|
| 2113 | next_dup->end_seq);
|
| 2114 | if (in_sack > 0)
|
| 2115 | dup_sack = true;
|
| 2116 | }
|
| 2117 |
|
| 2118 | |
| 2119 | * shifting can eat and free both this skb and the next,
|
| 2120 | * so not even _safe variant of the loop is enough.
|
| 2121 |
|
| 2122 | if (in_sack <= 0) {
|
| 2123 | tmp = tcp_shift_skb_data(sk, skb, state,
|
| 2124 | start_seq, end_seq, dup_sack);
|
| 2125 | if (tmp) {
|
| 2126 | if (tmp != skb) {
|
| 2127 | skb = tmp;
|
| 2128 | continue;
|
| 2129 | }
|
| 2130 |
|
| 2131 | in_sack = 0;
|
| 2132 | } else {
|
| 2133 | in_sack = tcp_match_skb_to_sack(sk, skb,
|
| 2134 | start_seq,
|
| 2135 | end_seq);
|
| 2136 | }
|
| 2137 | }
|
| 2138 |
|
| 2139 | if (unlikely(in_sack < 0))
|
| 2140 | break;
|
| 2141 |
|
| 2142 | if (in_sack) {
|
| 2143 | TCP_SKB_CB(skb)->sacked =
|
| 2144 | tcp_sacktag_one(sk,
|
| 2145 | state,
|
| 2146 | TCP_SKB_CB(skb)->sacked,
|
| 2147 | TCP_SKB_CB(skb)->seq,
|
| 2148 | TCP_SKB_CB(skb)->end_seq,
|
| 2149 | dup_sack,
|
| 2150 | tcp_skb_pcount(skb),
|
| 2151 | skb->len,
|
| 2152 | tcp_skb_timestamp_us(skb));
|
| 2153 | tcp_rate_skb_delivered(sk, skb, state->rate);
|
| 2154 | if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
|
| 2155 | list_del_init(&skb->tcp_tsorted_anchor);
|
| 2156 |
|
| 2157 | if (!before(TCP_SKB_CB(skb)->seq,
|
| 2158 | tcp_highest_sack_seq(tp)))
|
| 2159 | tcp_advance_highest_sack(sk, skb);
|
| 2160 | }
|
| 2161 | }
|
| 2162 | return skb;
|
| 2163 | }
|
| 2164 |
|
| 2165 | static struct sk_buff *tcp_sacktag_bsearch(struct sock *sk, u32 seq)
|
| 2166 | {
|
| 2167 | struct rb_node *parent, **p = &sk->tcp_rtx_queue.rb_node;
|
| 2168 | struct sk_buff *skb;
|
| 2169 |
|
| 2170 | while (*p) {
|
| 2171 | parent = *p;
|
| 2172 | skb = rb_to_skb(parent);
|
| 2173 | if (before(seq, TCP_SKB_CB(skb)->seq)) {
|
| 2174 | p = &parent->rb_left;
|
| 2175 | continue;
|
| 2176 | }
|
| 2177 | if (!before(seq, TCP_SKB_CB(skb)->end_seq)) {
|
| 2178 | p = &parent->rb_right;
|
| 2179 | continue;
|
| 2180 | }
|
| 2181 | return skb;
|
| 2182 | }
|
| 2183 | return NULL;
|
| 2184 | }
|
| 2185 |
|
| 2186 | static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
|
| 2187 | u32 skip_to_seq)
|
| 2188 | {
|
| 2189 | if (skb && after(TCP_SKB_CB(skb)->seq, skip_to_seq))
|
| 2190 | return skb;
|
| 2191 |
|
| 2192 | return tcp_sacktag_bsearch(sk, skip_to_seq);
|
| 2193 | }
|
| 2194 |
|
| 2195 | static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
|
| 2196 | struct sock *sk,
|
| 2197 | struct tcp_sack_block *next_dup,
|
| 2198 | struct tcp_sacktag_state *state,
|
| 2199 | u32 skip_to_seq)
|
| 2200 | {
|
| 2201 | if (!next_dup)
|
| 2202 | return skb;
|
| 2203 |
|
| 2204 | if (before(next_dup->start_seq, skip_to_seq)) {
|
| 2205 | skb = tcp_sacktag_skip(skb, sk, next_dup->start_seq);
|
| 2206 | skb = tcp_sacktag_walk(skb, sk, NULL, state,
|
| 2207 | next_dup->start_seq, next_dup->end_seq,
|
| 2208 | 1);
|
| 2209 | }
|
| 2210 |
|
| 2211 | return skb;
|
| 2212 | }
|
| 2213 |
|
| 2214 | static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
|
| 2215 | {
|
| 2216 | return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
|
| 2217 | }
|
| 2218 |
|
| 2219 | static int
|
| 2220 | tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
|
| 2221 | u32 prior_snd_una, struct tcp_sacktag_state *state)
|
| 2222 | {
|
| 2223 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2224 | const unsigned char *ptr = (skb_transport_header(ack_skb) +
|
| 2225 | TCP_SKB_CB(ack_skb)->sacked);
|
| 2226 | struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
|
| 2227 | struct tcp_sack_block sp[TCP_NUM_SACKS];
|
| 2228 | struct tcp_sack_block *cache;
|
| 2229 | struct sk_buff *skb;
|
| 2230 | int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
|
| 2231 | int used_sacks;
|
| 2232 | bool found_dup_sack = false;
|
| 2233 | int i, j;
|
| 2234 | int first_sack_index;
|
| 2235 |
|
| 2236 | state->flag = 0;
|
| 2237 | state->reord = tp->snd_nxt;
|
| 2238 |
|
| 2239 | if (!tp->sacked_out)
|
| 2240 | tcp_highest_sack_reset(sk);
|
| 2241 |
|
| 2242 | found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
|
| 2243 | num_sacks, prior_snd_una, state);
|
| 2244 |
|
| 2245 | |
| 2246 | * account more or less fresh ones, they can
|
| 2247 | * contain valid SACK info.
|
| 2248 |
|
| 2249 | if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
|
| 2250 | return 0;
|
| 2251 |
|
| 2252 | if (!tp->packets_out)
|
| 2253 | goto out;
|
| 2254 |
|
| 2255 | used_sacks = 0;
|
| 2256 | first_sack_index = 0;
|
| 2257 | for (i = 0; i < num_sacks; i++) {
|
| 2258 | bool dup_sack = !i && found_dup_sack;
|
| 2259 |
|
| 2260 | sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq);
|
| 2261 | sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq);
|
| 2262 |
|
| 2263 | if (!tcp_is_sackblock_valid(tp, dup_sack,
|
| 2264 | sp[used_sacks].start_seq,
|
| 2265 | sp[used_sacks].end_seq)) {
|
| 2266 | int mib_idx;
|
| 2267 |
|
| 2268 | if (dup_sack) {
|
| 2269 | if (!tp->undo_marker)
|
| 2270 | mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO;
|
| 2271 | else
|
| 2272 | mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD;
|
| 2273 | } else {
|
| 2274 |
|
| 2275 | if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) &&
|
| 2276 | !after(sp[used_sacks].end_seq, tp->snd_una))
|
| 2277 | continue;
|
| 2278 | mib_idx = LINUX_MIB_TCPSACKDISCARD;
|
| 2279 | }
|
| 2280 |
|
| 2281 | NET_INC_STATS(sock_net(sk), mib_idx);
|
| 2282 | if (i == 0)
|
| 2283 | first_sack_index = -1;
|
| 2284 | continue;
|
| 2285 | }
|
| 2286 |
|
| 2287 |
|
| 2288 | if (!after(sp[used_sacks].end_seq, prior_snd_una)) {
|
| 2289 | if (i == 0)
|
| 2290 | first_sack_index = -1;
|
| 2291 | continue;
|
| 2292 | }
|
| 2293 |
|
| 2294 | used_sacks++;
|
| 2295 | }
|
| 2296 |
|
| 2297 |
|
| 2298 | for (i = used_sacks - 1; i > 0; i--) {
|
| 2299 | for (j = 0; j < i; j++) {
|
| 2300 | if (after(sp[j].start_seq, sp[j + 1].start_seq)) {
|
| 2301 | swap(sp[j], sp[j + 1]);
|
| 2302 |
|
| 2303 |
|
| 2304 | if (j == first_sack_index)
|
| 2305 | first_sack_index = j + 1;
|
| 2306 | }
|
| 2307 | }
|
| 2308 | }
|
| 2309 |
|
| 2310 | state->mss_now = tcp_current_mss(sk);
|
| 2311 | skb = NULL;
|
| 2312 | i = 0;
|
| 2313 |
|
| 2314 | if (!tp->sacked_out) {
|
| 2315 |
|
| 2316 | cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
|
| 2317 | } else {
|
| 2318 | cache = tp->recv_sack_cache;
|
| 2319 |
|
| 2320 | while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq &&
|
| 2321 | !cache->end_seq)
|
| 2322 | cache++;
|
| 2323 | }
|
| 2324 |
|
| 2325 | while (i < used_sacks) {
|
| 2326 | u32 start_seq = sp[i].start_seq;
|
| 2327 | u32 end_seq = sp[i].end_seq;
|
| 2328 | bool dup_sack = (found_dup_sack && (i == first_sack_index));
|
| 2329 | struct tcp_sack_block *next_dup = NULL;
|
| 2330 |
|
| 2331 | if (found_dup_sack && ((i + 1) == first_sack_index))
|
| 2332 | next_dup = &sp[i + 1];
|
| 2333 |
|
| 2334 |
|
| 2335 | while (tcp_sack_cache_ok(tp, cache) &&
|
| 2336 | !before(start_seq, cache->end_seq))
|
| 2337 | cache++;
|
| 2338 |
|
| 2339 |
|
| 2340 | if (tcp_sack_cache_ok(tp, cache) && !dup_sack &&
|
| 2341 | after(end_seq, cache->start_seq)) {
|
| 2342 |
|
| 2343 |
|
| 2344 | if (before(start_seq, cache->start_seq)) {
|
| 2345 | skb = tcp_sacktag_skip(skb, sk, start_seq);
|
| 2346 | skb = tcp_sacktag_walk(skb, sk, next_dup,
|
| 2347 | state,
|
| 2348 | start_seq,
|
| 2349 | cache->start_seq,
|
| 2350 | dup_sack);
|
| 2351 | }
|
| 2352 |
|
| 2353 |
|
| 2354 | if (!after(end_seq, cache->end_seq))
|
| 2355 | goto advance_sp;
|
| 2356 |
|
| 2357 | skb = tcp_maybe_skipping_dsack(skb, sk, next_dup,
|
| 2358 | state,
|
| 2359 | cache->end_seq);
|
| 2360 |
|
| 2361 |
|
| 2362 | if (tcp_highest_sack_seq(tp) == cache->end_seq) {
|
| 2363 |
|
| 2364 | skb = tcp_highest_sack(sk);
|
| 2365 | if (!skb)
|
| 2366 | break;
|
| 2367 | cache++;
|
| 2368 | goto walk;
|
| 2369 | }
|
| 2370 |
|
| 2371 | skb = tcp_sacktag_skip(skb, sk, cache->end_seq);
|
| 2372 |
|
| 2373 | cache++;
|
| 2374 | continue;
|
| 2375 | }
|
| 2376 |
|
| 2377 | if (!before(start_seq, tcp_highest_sack_seq(tp))) {
|
| 2378 | skb = tcp_highest_sack(sk);
|
| 2379 | if (!skb)
|
| 2380 | break;
|
| 2381 | }
|
| 2382 | skb = tcp_sacktag_skip(skb, sk, start_seq);
|
| 2383 |
|
| 2384 | walk:
|
| 2385 | skb = tcp_sacktag_walk(skb, sk, next_dup, state,
|
| 2386 | start_seq, end_seq, dup_sack);
|
| 2387 |
|
| 2388 | advance_sp:
|
| 2389 | i++;
|
| 2390 | }
|
| 2391 |
|
| 2392 |
|
| 2393 | for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) {
|
| 2394 | tp->recv_sack_cache[i].start_seq = 0;
|
| 2395 | tp->recv_sack_cache[i].end_seq = 0;
|
| 2396 | }
|
| 2397 | for (j = 0; j < used_sacks; j++)
|
| 2398 | tp->recv_sack_cache[i++] = sp[j];
|
| 2399 |
|
| 2400 | if (inet_csk(sk)->icsk_ca_state != TCP_CA_Loss || tp->undo_marker)
|
| 2401 | tcp_check_sack_reordering(sk, state->reord, 0);
|
| 2402 |
|
| 2403 | tcp_verify_left_out(tp);
|
| 2404 | out:
|
| 2405 |
|
| 2406 | #if FASTRETRANS_DEBUG > 0
|
| 2407 | WARN_ON((int)tp->sacked_out < 0);
|
| 2408 | WARN_ON((int)tp->lost_out < 0);
|
| 2409 | WARN_ON((int)tp->retrans_out < 0);
|
| 2410 | WARN_ON((int)tcp_packets_in_flight(tp) < 0);
|
| 2411 | #endif
|
| 2412 | return state->flag;
|
| 2413 | }
|
| 2414 |
|
| 2415 | |
| 2416 | * packets_out. Returns false if sacked_out adjustement wasn't necessary.
|
| 2417 |
|
| 2418 | static bool tcp_limit_reno_sacked(struct tcp_sock *tp)
|
| 2419 | {
|
| 2420 | u32 holes;
|
| 2421 |
|
| 2422 | holes = max(tp->lost_out, 1U);
|
| 2423 | holes = min(holes, tp->packets_out);
|
| 2424 |
|
| 2425 | if ((tp->sacked_out + holes) > tp->packets_out) {
|
| 2426 | tp->sacked_out = tp->packets_out - holes;
|
| 2427 | return true;
|
| 2428 | }
|
| 2429 | return false;
|
| 2430 | }
|
| 2431 |
|
| 2432 | |
| 2433 | * in assumption of absent reordering, interpret this as reordering.
|
| 2434 | * The only another reason could be bug in receiver TCP.
|
| 2435 |
|
| 2436 | static void tcp_check_reno_reordering(struct sock *sk, const int addend)
|
| 2437 | {
|
| 2438 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2439 |
|
| 2440 | if (!tcp_limit_reno_sacked(tp))
|
| 2441 | return;
|
| 2442 |
|
| 2443 | WRITE_ONCE(tp->reordering,
|
| 2444 | min_t(u32, tp->packets_out + addend,
|
| 2445 | READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_max_reordering)));
|
| 2446 | WRITE_ONCE(tp->reord_seen, tp->reord_seen + 1);
|
| 2447 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRENOREORDER);
|
| 2448 | }
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 | static void tcp_add_reno_sack(struct sock *sk, int num_dupack, bool ece_ack)
|
| 2453 | {
|
| 2454 | if (num_dupack) {
|
| 2455 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2456 | u32 prior_sacked = tp->sacked_out;
|
| 2457 | s32 delivered;
|
| 2458 |
|
| 2459 | tp->sacked_out += num_dupack;
|
| 2460 | tcp_check_reno_reordering(sk, 0);
|
| 2461 | delivered = tp->sacked_out - prior_sacked;
|
| 2462 | if (delivered > 0)
|
| 2463 | tcp_count_delivered(tp, delivered, ece_ack);
|
| 2464 | tcp_verify_left_out(tp);
|
| 2465 | }
|
| 2466 | }
|
| 2467 |
|
| 2468 |
|
| 2469 |
|
| 2470 | static void tcp_remove_reno_sacks(struct sock *sk, int acked, bool ece_ack)
|
| 2471 | {
|
| 2472 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2473 |
|
| 2474 | if (acked > 0) {
|
| 2475 |
|
| 2476 | tcp_count_delivered(tp, max_t(int, acked - tp->sacked_out, 1),
|
| 2477 | ece_ack);
|
| 2478 | if (acked - 1 >= tp->sacked_out)
|
| 2479 | tp->sacked_out = 0;
|
| 2480 | else
|
| 2481 | tp->sacked_out -= acked - 1;
|
| 2482 | }
|
| 2483 | tcp_check_reno_reordering(sk, acked);
|
| 2484 | tcp_verify_left_out(tp);
|
| 2485 | }
|
| 2486 |
|
| 2487 | static inline void tcp_reset_reno_sack(struct tcp_sock *tp)
|
| 2488 | {
|
| 2489 | tp->sacked_out = 0;
|
| 2490 | }
|
| 2491 |
|
| 2492 | void tcp_clear_retrans(struct tcp_sock *tp)
|
| 2493 | {
|
| 2494 | tp->retrans_out = 0;
|
| 2495 | tp->lost_out = 0;
|
| 2496 | tp->undo_marker = 0;
|
| 2497 | tp->undo_retrans = -1;
|
| 2498 | tp->sacked_out = 0;
|
| 2499 | tp->rto_stamp = 0;
|
| 2500 | tp->total_rto = 0;
|
| 2501 | tp->total_rto_recoveries = 0;
|
| 2502 | tp->total_rto_time = 0;
|
| 2503 | }
|
| 2504 |
|
| 2505 | static inline void tcp_init_undo(struct tcp_sock *tp)
|
| 2506 | {
|
| 2507 | tp->undo_marker = tp->snd_una;
|
| 2508 |
|
| 2509 |
|
| 2510 |
|
| 2511 | tp->undo_retrans = tp->retrans_out;
|
| 2512 |
|
| 2513 | if (tp->tlp_high_seq && tp->tlp_retrans)
|
| 2514 | tp->undo_retrans++;
|
| 2515 |
|
| 2516 | if (!tp->undo_retrans)
|
| 2517 | tp->undo_retrans = -1;
|
| 2518 | }
|
| 2519 |
|
| 2520 | |
| 2521 | * and reset tags completely, otherwise preserve SACKs. If receiver
|
| 2522 | * dropped its ofo queue, we will know this due to reneging detection.
|
| 2523 |
|
| 2524 | static void tcp_timeout_mark_lost(struct sock *sk)
|
| 2525 | {
|
| 2526 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2527 | struct sk_buff *skb, *head;
|
| 2528 | bool is_reneg;
|
| 2529 |
|
| 2530 | head = tcp_rtx_queue_head(sk);
|
| 2531 | is_reneg = head && (TCP_SKB_CB(head)->sacked & TCPCB_SACKED_ACKED);
|
| 2532 | if (is_reneg) {
|
| 2533 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSACKRENEGING);
|
| 2534 | tp->sacked_out = 0;
|
| 2535 |
|
| 2536 | tp->is_sack_reneg = 1;
|
| 2537 | } else if (tcp_is_reno(tp)) {
|
| 2538 | tcp_reset_reno_sack(tp);
|
| 2539 | }
|
| 2540 |
|
| 2541 | skb = head;
|
| 2542 | skb_rbtree_walk_from(skb) {
|
| 2543 | if (is_reneg)
|
| 2544 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
|
| 2545 | else if (skb != head && tcp_rack_skb_timeout(tp, skb, 0) > 0)
|
| 2546 | continue;
|
| 2547 | tcp_mark_skb_lost(sk, skb);
|
| 2548 | }
|
| 2549 | tcp_verify_left_out(tp);
|
| 2550 | tcp_clear_all_retrans_hints(tp);
|
| 2551 | }
|
| 2552 |
|
| 2553 |
|
| 2554 | void tcp_enter_loss(struct sock *sk)
|
| 2555 | {
|
| 2556 | const struct inet_connection_sock *icsk = inet_csk(sk);
|
| 2557 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2558 | struct net *net = sock_net(sk);
|
| 2559 | bool new_recovery = icsk->icsk_ca_state < TCP_CA_Recovery;
|
| 2560 | u8 reordering;
|
| 2561 |
|
| 2562 | tcp_timeout_mark_lost(sk);
|
| 2563 |
|
| 2564 |
|
| 2565 | if (icsk->icsk_ca_state <= TCP_CA_Disorder ||
|
| 2566 | !after(tp->high_seq, tp->snd_una) ||
|
| 2567 | (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) {
|
| 2568 | tp->prior_ssthresh = tcp_current_ssthresh(sk);
|
| 2569 | tp->prior_cwnd = tcp_snd_cwnd(tp);
|
| 2570 | WRITE_ONCE(tp->snd_ssthresh, icsk->icsk_ca_ops->ssthresh(sk));
|
| 2571 | tcp_ca_event(sk, CA_EVENT_LOSS);
|
| 2572 | tcp_init_undo(tp);
|
| 2573 | }
|
| 2574 | tcp_snd_cwnd_set(tp, tcp_packets_in_flight(tp) + 1);
|
| 2575 | tp->snd_cwnd_cnt = 0;
|
| 2576 | tp->snd_cwnd_stamp = tcp_jiffies32;
|
| 2577 |
|
| 2578 | |
| 2579 | * suggests that the degree of reordering is over-estimated.
|
| 2580 |
|
| 2581 | reordering = READ_ONCE(net->ipv4.sysctl_tcp_reordering);
|
| 2582 | if (icsk->icsk_ca_state <= TCP_CA_Disorder &&
|
| 2583 | tp->sacked_out >= reordering)
|
| 2584 | WRITE_ONCE(tp->reordering,
|
| 2585 | min_t(unsigned int, tp->reordering, reordering));
|
| 2586 |
|
| 2587 | tcp_set_ca_state(sk, TCP_CA_Loss);
|
| 2588 | tp->high_seq = tp->snd_nxt;
|
| 2589 | tp->tlp_high_seq = 0;
|
| 2590 | tcp_ecn_queue_cwr(tp);
|
| 2591 |
|
| 2592 | |
| 2593 | * loss recovery is underway except recurring timeout(s) on
|
| 2594 | * the same SND.UNA (sec 3.2). Disable F-RTO on path MTU probing
|
| 2595 |
|
| 2596 | tp->frto = READ_ONCE(net->ipv4.sysctl_tcp_frto) &&
|
| 2597 | (new_recovery || icsk->icsk_retransmits) &&
|
| 2598 | !inet_csk(sk)->icsk_mtup.probe_size;
|
| 2599 | }
|
| 2600 |
|
| 2601 | |
| 2602 | * remembered SACKs do not reflect real state of receiver i.e.
|
| 2603 | * receiver _host_ is heavily congested (or buggy).
|
| 2604 | *
|
| 2605 | * To avoid big spurious retransmission bursts due to transient SACK
|
| 2606 | * scoreboard oddities that look like reneging, we give the receiver a
|
| 2607 | * little time (max(RTT/2, 10ms)) to send us some more ACKs that will
|
| 2608 | * restore sanity to the SACK scoreboard. If the apparent reneging
|
| 2609 | * persists until this RTO then we'll clear the SACK scoreboard.
|
| 2610 |
|
| 2611 | static bool tcp_check_sack_reneging(struct sock *sk, int *ack_flag)
|
| 2612 | {
|
| 2613 | if (*ack_flag & FLAG_SACK_RENEGING &&
|
| 2614 | *ack_flag & FLAG_SND_UNA_ADVANCED) {
|
| 2615 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2616 | unsigned long delay = max(usecs_to_jiffies(tp->srtt_us >> 4),
|
| 2617 | msecs_to_jiffies(10));
|
| 2618 |
|
| 2619 | tcp_reset_xmit_timer(sk, ICSK_TIME_RETRANS, delay, false);
|
| 2620 | *ack_flag &= ~FLAG_SET_XMIT_TIMER;
|
| 2621 | return true;
|
| 2622 | }
|
| 2623 | return false;
|
| 2624 | }
|
| 2625 |
|
| 2626 | |
| 2627 | * --------------------------------------
|
| 2628 | *
|
| 2629 | * "Open" Normal state, no dubious events, fast path.
|
| 2630 | * "Disorder" In all the respects it is "Open",
|
| 2631 | * but requires a bit more attention. It is entered when
|
| 2632 | * we see some SACKs or dupacks. It is split of "Open"
|
| 2633 | * mainly to move some processing from fast path to slow one.
|
| 2634 | * "CWR" CWND was reduced due to some Congestion Notification event.
|
| 2635 | * It can be ECN, ICMP source quench, local device congestion.
|
| 2636 | * "Recovery" CWND was reduced, we are fast-retransmitting.
|
| 2637 | * "Loss" CWND was reduced due to RTO timeout or SACK reneging.
|
| 2638 | *
|
| 2639 | * tcp_fastretrans_alert() is entered:
|
| 2640 | * - each incoming ACK, if state is not "Open"
|
| 2641 | * - when arrived ACK is unusual, namely:
|
| 2642 | * * SACK
|
| 2643 | * * Duplicate ACK.
|
| 2644 | * * ECN ECE.
|
| 2645 | *
|
| 2646 | * Counting packets in flight is pretty simple.
|
| 2647 | *
|
| 2648 | * in_flight = packets_out - left_out + retrans_out
|
| 2649 | *
|
| 2650 | * packets_out is SND.NXT-SND.UNA counted in packets.
|
| 2651 | *
|
| 2652 | * retrans_out is number of retransmitted segments.
|
| 2653 | *
|
| 2654 | * left_out is number of segments left network, but not ACKed yet.
|
| 2655 | *
|
| 2656 | * left_out = sacked_out + lost_out
|
| 2657 | *
|
| 2658 | * sacked_out: Packets, which arrived to receiver out of order
|
| 2659 | * and hence not ACKed. With SACKs this number is simply
|
| 2660 | * amount of SACKed data. Even without SACKs
|
| 2661 | * it is easy to give pretty reliable estimate of this number,
|
| 2662 | * counting duplicate ACKs.
|
| 2663 | *
|
| 2664 | * lost_out: Packets lost by network. TCP has no explicit
|
| 2665 | * "loss notification" feedback from network (for now).
|
| 2666 | * It means that this number can be only _guessed_.
|
| 2667 | * Actually, it is the heuristics to predict lossage that
|
| 2668 | * distinguishes different algorithms.
|
| 2669 | *
|
| 2670 | * F.e. after RTO, when all the queue is considered as lost,
|
| 2671 | * lost_out = packets_out and in_flight = retrans_out.
|
| 2672 | *
|
| 2673 | * Essentially, we have now a few algorithms detecting
|
| 2674 | * lost packets.
|
| 2675 | *
|
| 2676 | * If the receiver supports SACK:
|
| 2677 | *
|
| 2678 | * RACK (RFC8985): RACK is a newer loss detection algorithm
|
| 2679 | * (2017-) that checks timing instead of counting DUPACKs.
|
| 2680 | * Essentially a packet is considered lost if it's not S/ACKed
|
| 2681 | * after RTT + reordering_window, where both metrics are
|
| 2682 | * dynamically measured and adjusted. This is implemented in
|
| 2683 | * tcp_rack_mark_lost.
|
| 2684 | *
|
| 2685 | * If the receiver does not support SACK:
|
| 2686 | *
|
| 2687 | * NewReno (RFC6582): in Recovery we assume that one segment
|
| 2688 | * is lost (classic Reno). While we are in Recovery and
|
| 2689 | * a partial ACK arrives, we assume that one more packet
|
| 2690 | * is lost (NewReno). This heuristics are the same in NewReno
|
| 2691 | * and SACK.
|
| 2692 | *
|
| 2693 | * The really tricky (and requiring careful tuning) part of the algorithm
|
| 2694 | * is hidden in the RACK code in tcp_recovery.c and tcp_xmit_retransmit_queue().
|
| 2695 | * The first determines the moment _when_ we should reduce CWND and,
|
| 2696 | * hence, slow down forward transmission. In fact, it determines the moment
|
| 2697 | * when we decide that hole is caused by loss, rather than by a reorder.
|
| 2698 | *
|
| 2699 | * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill
|
| 2700 | * holes, caused by lost packets.
|
| 2701 | *
|
| 2702 | * And the most logically complicated part of algorithm is undo
|
| 2703 | * heuristics. We detect false retransmits due to both too early
|
| 2704 | * fast retransmit (reordering) and underestimated RTO, analyzing
|
| 2705 | * timestamps and D-SACKs. When we detect that some segments were
|
| 2706 | * retransmitted by mistake and CWND reduction was wrong, we undo
|
| 2707 | * window reduction and abort recovery phase. This logic is hidden
|
| 2708 | * inside several functions named tcp_try_undo_<something>.
|
| 2709 |
|
| 2710 |
|
| 2711 | |
| 2712 | * and enter Recovery phase, reducing congestion window.
|
| 2713 | *
|
| 2714 | * Main question: may we further continue forward transmission
|
| 2715 | * with the same cwnd?
|
| 2716 |
|
| 2717 | static bool tcp_time_to_recover(const struct tcp_sock *tp)
|
| 2718 | {
|
| 2719 |
|
| 2720 | return tp->lost_out != 0;
|
| 2721 | }
|
| 2722 |
|
| 2723 | static bool tcp_tsopt_ecr_before(const struct tcp_sock *tp, u32 when)
|
| 2724 | {
|
| 2725 | return tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
|
| 2726 | before(tp->rx_opt.rcv_tsecr, when);
|
| 2727 | }
|
| 2728 |
|
| 2729 | |
| 2730 | * reply is prior to the skb transmission time
|
| 2731 |
|
| 2732 | static bool tcp_skb_spurious_retrans(const struct tcp_sock *tp,
|
| 2733 | const struct sk_buff *skb)
|
| 2734 | {
|
| 2735 | return (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS) &&
|
| 2736 | tcp_tsopt_ecr_before(tp, tcp_skb_timestamp_ts(tp->tcp_usec_ts, skb));
|
| 2737 | }
|
| 2738 |
|
| 2739 | |
| 2740 | * than timestamp of the first retransmission.
|
| 2741 |
|
| 2742 | static inline bool tcp_packet_delayed(const struct tcp_sock *tp)
|
| 2743 | {
|
| 2744 | const struct sock *sk = (const struct sock *)tp;
|
| 2745 |
|
| 2746 |
|
| 2747 | if (tp->retrans_stamp)
|
| 2748 | return tcp_tsopt_ecr_before(tp, tp->retrans_stamp);
|
| 2749 |
|
| 2750 | |
| 2751 | * recovery episode, so normally if tp->retrans_stamp is 0 then no
|
| 2752 | * retransmission has happened yet (likely due to TSQ, which can cause
|
| 2753 | * fast retransmits to be delayed). So if snd_una advanced while
|
| 2754 | * (tp->retrans_stamp is 0 then apparently a packet was merely delayed,
|
| 2755 | * not lost. But there are exceptions where we retransmit but then
|
| 2756 | * clear tp->retrans_stamp, so we check for those exceptions.
|
| 2757 |
|
| 2758 |
|
| 2759 | |
| 2760 | * clears tp->retrans_stamp when snd_una == high_seq.
|
| 2761 |
|
| 2762 | if (!tcp_is_sack(tp) && !before(tp->snd_una, tp->high_seq))
|
| 2763 | return false;
|
| 2764 |
|
| 2765 | |
| 2766 | * when setting FLAG_SYN_ACKED is set, even if the SYN was
|
| 2767 | * retransmitted.
|
| 2768 |
|
| 2769 | if (sk->sk_state == TCP_SYN_SENT)
|
| 2770 | return false;
|
| 2771 |
|
| 2772 | return true;
|
| 2773 | }
|
| 2774 |
|
| 2775 |
|
| 2776 |
|
| 2777 | |
| 2778 | * window. It would seem that it is trivially available for us in
|
| 2779 | * tp->retrans_out, however, that kind of assumptions doesn't consider
|
| 2780 | * what will happen if errors occur when sending retransmission for the
|
| 2781 | * second time. ...It could the that such segment has only
|
| 2782 | * TCPCB_EVER_RETRANS set at the present time. It seems that checking
|
| 2783 | * the head skb is enough except for some reneging corner cases that
|
| 2784 | * are not worth the effort.
|
| 2785 | *
|
| 2786 | * Main reason for all this complexity is the fact that connection dying
|
| 2787 | * time now depends on the validity of the retrans_stamp, in particular,
|
| 2788 | * that successive retransmissions of a segment must not advance
|
| 2789 | * retrans_stamp under any conditions.
|
| 2790 |
|
| 2791 | static bool tcp_any_retrans_done(const struct sock *sk)
|
| 2792 | {
|
| 2793 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 2794 | struct sk_buff *skb;
|
| 2795 |
|
| 2796 | if (tp->retrans_out)
|
| 2797 | return true;
|
| 2798 |
|
| 2799 | skb = tcp_rtx_queue_head(sk);
|
| 2800 | if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS))
|
| 2801 | return true;
|
| 2802 |
|
| 2803 | return false;
|
| 2804 | }
|
| 2805 |
|
| 2806 | |
| 2807 | * network, then we clear retrans_stamp so that upon the next loss recovery
|
| 2808 | * retransmits_timed_out() and timestamp-undo are using the correct value.
|
| 2809 |
|
| 2810 | static void tcp_retrans_stamp_cleanup(struct sock *sk)
|
| 2811 | {
|
| 2812 | if (!tcp_any_retrans_done(sk))
|
| 2813 | tcp_sk(sk)->retrans_stamp = 0;
|
| 2814 | }
|
| 2815 |
|
| 2816 | static void DBGUNDO(struct sock *sk, const char *msg)
|
| 2817 | {
|
| 2818 | #if FASTRETRANS_DEBUG > 1
|
| 2819 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2820 | struct inet_sock *inet = inet_sk(sk);
|
| 2821 |
|
| 2822 | if (sk->sk_family == AF_INET) {
|
| 2823 | pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n",
|
| 2824 | msg,
|
| 2825 | &inet->inet_daddr, ntohs(inet->inet_dport),
|
| 2826 | tcp_snd_cwnd(tp), tcp_left_out(tp),
|
| 2827 | tp->snd_ssthresh, tp->prior_ssthresh,
|
| 2828 | tp->packets_out);
|
| 2829 | }
|
| 2830 | #if IS_ENABLED(CONFIG_IPV6)
|
| 2831 | else if (sk->sk_family == AF_INET6) {
|
| 2832 | pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n",
|
| 2833 | msg,
|
| 2834 | &sk->sk_v6_daddr, ntohs(inet->inet_dport),
|
| 2835 | tcp_snd_cwnd(tp), tcp_left_out(tp),
|
| 2836 | tp->snd_ssthresh, tp->prior_ssthresh,
|
| 2837 | tp->packets_out);
|
| 2838 | }
|
| 2839 | #endif
|
| 2840 | #endif
|
| 2841 | }
|
| 2842 |
|
| 2843 | static void tcp_undo_cwnd_reduction(struct sock *sk, bool unmark_loss)
|
| 2844 | {
|
| 2845 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2846 |
|
| 2847 | if (unmark_loss) {
|
| 2848 | struct sk_buff *skb;
|
| 2849 |
|
| 2850 | skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
|
| 2851 | TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
|
| 2852 | }
|
| 2853 | tp->lost_out = 0;
|
| 2854 | tcp_clear_all_retrans_hints(tp);
|
| 2855 | }
|
| 2856 |
|
| 2857 | if (tp->prior_ssthresh) {
|
| 2858 | const struct inet_connection_sock *icsk = inet_csk(sk);
|
| 2859 |
|
| 2860 | tcp_snd_cwnd_set(tp, icsk->icsk_ca_ops->undo_cwnd(sk));
|
| 2861 |
|
| 2862 | if (tp->prior_ssthresh > tp->snd_ssthresh) {
|
| 2863 | WRITE_ONCE(tp->snd_ssthresh, tp->prior_ssthresh);
|
| 2864 | tcp_ecn_withdraw_cwr(tp);
|
| 2865 | }
|
| 2866 | }
|
| 2867 | tp->snd_cwnd_stamp = tcp_jiffies32;
|
| 2868 | tp->undo_marker = 0;
|
| 2869 | tp->rack.advanced = 1;
|
| 2870 | }
|
| 2871 |
|
| 2872 | static inline bool tcp_may_undo(const struct tcp_sock *tp)
|
| 2873 | {
|
| 2874 | return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp));
|
| 2875 | }
|
| 2876 |
|
| 2877 | static bool tcp_is_non_sack_preventing_reopen(struct sock *sk)
|
| 2878 | {
|
| 2879 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2880 |
|
| 2881 | if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) {
|
| 2882 | |
| 2883 | * is ACKed. For Reno it is MUST to prevent false
|
| 2884 | * fast retransmits (RFC2582). SACK TCP is safe.
|
| 2885 | if (!tcp_any_retrans_done(sk))
|
| 2886 | tp->retrans_stamp = 0;
|
| 2887 | return true;
|
| 2888 | }
|
| 2889 | return false;
|
| 2890 | }
|
| 2891 |
|
| 2892 |
|
| 2893 | static bool tcp_try_undo_recovery(struct sock *sk)
|
| 2894 | {
|
| 2895 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2896 |
|
| 2897 | if (tcp_may_undo(tp)) {
|
| 2898 | int mib_idx;
|
| 2899 |
|
| 2900 | |
| 2901 | * or our original transmission succeeded.
|
| 2902 |
|
| 2903 | DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans");
|
| 2904 | tcp_undo_cwnd_reduction(sk, false);
|
| 2905 | if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss)
|
| 2906 | mib_idx = LINUX_MIB_TCPLOSSUNDO;
|
| 2907 | else
|
| 2908 | mib_idx = LINUX_MIB_TCPFULLUNDO;
|
| 2909 |
|
| 2910 | NET_INC_STATS(sock_net(sk), mib_idx);
|
| 2911 | } else if (tp->rack.reo_wnd_persist) {
|
| 2912 | tp->rack.reo_wnd_persist--;
|
| 2913 | }
|
| 2914 | if (tcp_is_non_sack_preventing_reopen(sk))
|
| 2915 | return true;
|
| 2916 | tcp_set_ca_state(sk, TCP_CA_Open);
|
| 2917 | tp->is_sack_reneg = 0;
|
| 2918 | return false;
|
| 2919 | }
|
| 2920 |
|
| 2921 |
|
| 2922 | static bool tcp_try_undo_dsack(struct sock *sk)
|
| 2923 | {
|
| 2924 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2925 |
|
| 2926 | if (tp->undo_marker && !tp->undo_retrans) {
|
| 2927 | tp->rack.reo_wnd_persist = min(TCP_RACK_RECOVERY_THRESH,
|
| 2928 | tp->rack.reo_wnd_persist + 1);
|
| 2929 | DBGUNDO(sk, "D-SACK");
|
| 2930 | tcp_undo_cwnd_reduction(sk, false);
|
| 2931 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDSACKUNDO);
|
| 2932 | return true;
|
| 2933 | }
|
| 2934 | return false;
|
| 2935 | }
|
| 2936 |
|
| 2937 |
|
| 2938 | static bool tcp_try_undo_loss(struct sock *sk, bool frto_undo)
|
| 2939 | {
|
| 2940 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2941 |
|
| 2942 | if (frto_undo || tcp_may_undo(tp)) {
|
| 2943 | tcp_undo_cwnd_reduction(sk, true);
|
| 2944 |
|
| 2945 | DBGUNDO(sk, "partial loss");
|
| 2946 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSUNDO);
|
| 2947 | if (frto_undo)
|
| 2948 | NET_INC_STATS(sock_net(sk),
|
| 2949 | LINUX_MIB_TCPSPURIOUSRTOS);
|
| 2950 | WRITE_ONCE(inet_csk(sk)->icsk_retransmits, 0);
|
| 2951 | if (tcp_is_non_sack_preventing_reopen(sk))
|
| 2952 | return true;
|
| 2953 | if (frto_undo || tcp_is_sack(tp)) {
|
| 2954 | tcp_set_ca_state(sk, TCP_CA_Open);
|
| 2955 | tp->is_sack_reneg = 0;
|
| 2956 | }
|
| 2957 | return true;
|
| 2958 | }
|
| 2959 | return false;
|
| 2960 | }
|
| 2961 |
|
| 2962 | |
| 2963 | * It computes the number of packets to send (sndcnt) based on packets newly
|
| 2964 | * delivered:
|
| 2965 | * 1) If the packets in flight is larger than ssthresh, PRR spreads the
|
| 2966 | * cwnd reductions across a full RTT.
|
| 2967 | * 2) Otherwise PRR uses packet conservation to send as much as delivered.
|
| 2968 | * But when SND_UNA is acked without further losses,
|
| 2969 | * slow starts cwnd up to ssthresh to speed up the recovery.
|
| 2970 |
|
| 2971 | static void tcp_init_cwnd_reduction(struct sock *sk)
|
| 2972 | {
|
| 2973 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2974 |
|
| 2975 | tp->high_seq = tp->snd_nxt;
|
| 2976 | tp->tlp_high_seq = 0;
|
| 2977 | tp->snd_cwnd_cnt = 0;
|
| 2978 | tp->prior_cwnd = tcp_snd_cwnd(tp);
|
| 2979 | tp->prr_delivered = 0;
|
| 2980 | tp->prr_out = 0;
|
| 2981 | WRITE_ONCE(tp->snd_ssthresh, inet_csk(sk)->icsk_ca_ops->ssthresh(sk));
|
| 2982 | tcp_ecn_queue_cwr(tp);
|
| 2983 | }
|
| 2984 |
|
| 2985 | void tcp_cwnd_reduction(struct sock *sk, int newly_acked_sacked, int newly_lost, int flag)
|
| 2986 | {
|
| 2987 | struct tcp_sock *tp = tcp_sk(sk);
|
| 2988 | int sndcnt = 0;
|
| 2989 | int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp);
|
| 2990 |
|
| 2991 | if (newly_acked_sacked <= 0 || WARN_ON_ONCE(!tp->prior_cwnd))
|
| 2992 | return;
|
| 2993 |
|
| 2994 | trace_tcp_cwnd_reduction_tp(sk, newly_acked_sacked, newly_lost, flag);
|
| 2995 |
|
| 2996 | tp->prr_delivered += newly_acked_sacked;
|
| 2997 | if (delta < 0) {
|
| 2998 | u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered +
|
| 2999 | tp->prior_cwnd - 1;
|
| 3000 | sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out;
|
| 3001 | } else {
|
| 3002 | sndcnt = max_t(int, tp->prr_delivered - tp->prr_out,
|
| 3003 | newly_acked_sacked);
|
| 3004 | if (flag & FLAG_SND_UNA_ADVANCED && !newly_lost)
|
| 3005 | sndcnt++;
|
| 3006 | sndcnt = min(delta, sndcnt);
|
| 3007 | }
|
| 3008 |
|
| 3009 | sndcnt = max(sndcnt, (tp->prr_out ? 0 : 1));
|
| 3010 | tcp_snd_cwnd_set(tp, tcp_packets_in_flight(tp) + sndcnt);
|
| 3011 | }
|
| 3012 |
|
| 3013 | static inline void tcp_end_cwnd_reduction(struct sock *sk)
|
| 3014 | {
|
| 3015 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3016 |
|
| 3017 | if (inet_csk(sk)->icsk_ca_ops->cong_control)
|
| 3018 | return;
|
| 3019 |
|
| 3020 |
|
| 3021 | if (tp->snd_ssthresh < TCP_INFINITE_SSTHRESH &&
|
| 3022 | (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR || tp->undo_marker)) {
|
| 3023 | tcp_snd_cwnd_set(tp, tp->snd_ssthresh);
|
| 3024 | tp->snd_cwnd_stamp = tcp_jiffies32;
|
| 3025 | }
|
| 3026 | tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR);
|
| 3027 | }
|
| 3028 |
|
| 3029 |
|
| 3030 | void tcp_enter_cwr(struct sock *sk)
|
| 3031 | {
|
| 3032 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3033 |
|
| 3034 | tp->prior_ssthresh = 0;
|
| 3035 | if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
|
| 3036 | tp->undo_marker = 0;
|
| 3037 | tcp_init_cwnd_reduction(sk);
|
| 3038 | tcp_set_ca_state(sk, TCP_CA_CWR);
|
| 3039 | }
|
| 3040 | }
|
| 3041 | EXPORT_SYMBOL(tcp_enter_cwr);
|
| 3042 |
|
| 3043 | static void tcp_try_keep_open(struct sock *sk)
|
| 3044 | {
|
| 3045 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3046 | int state = TCP_CA_Open;
|
| 3047 |
|
| 3048 | if (tcp_left_out(tp) || tcp_any_retrans_done(sk))
|
| 3049 | state = TCP_CA_Disorder;
|
| 3050 |
|
| 3051 | if (inet_csk(sk)->icsk_ca_state != state) {
|
| 3052 | tcp_set_ca_state(sk, state);
|
| 3053 | tp->high_seq = tp->snd_nxt;
|
| 3054 | }
|
| 3055 | }
|
| 3056 |
|
| 3057 | static void tcp_try_to_open(struct sock *sk, int flag)
|
| 3058 | {
|
| 3059 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3060 |
|
| 3061 | tcp_verify_left_out(tp);
|
| 3062 |
|
| 3063 | if (!tcp_any_retrans_done(sk))
|
| 3064 | tp->retrans_stamp = 0;
|
| 3065 |
|
| 3066 | if (flag & FLAG_ECE)
|
| 3067 | tcp_enter_cwr(sk);
|
| 3068 |
|
| 3069 | if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) {
|
| 3070 | tcp_try_keep_open(sk);
|
| 3071 | }
|
| 3072 | }
|
| 3073 |
|
| 3074 | static void tcp_mtup_probe_failed(struct sock *sk)
|
| 3075 | {
|
| 3076 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 3077 |
|
| 3078 | icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1;
|
| 3079 | icsk->icsk_mtup.probe_size = 0;
|
| 3080 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPFAIL);
|
| 3081 | }
|
| 3082 |
|
| 3083 | static void tcp_mtup_probe_success(struct sock *sk)
|
| 3084 | {
|
| 3085 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3086 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 3087 | u64 val;
|
| 3088 |
|
| 3089 | tp->prior_ssthresh = tcp_current_ssthresh(sk);
|
| 3090 |
|
| 3091 | val = (u64)tcp_snd_cwnd(tp) * tcp_mss_to_mtu(sk, tp->mss_cache);
|
| 3092 | do_div(val, icsk->icsk_mtup.probe_size);
|
| 3093 | DEBUG_NET_WARN_ON_ONCE((u32)val != val);
|
| 3094 | tcp_snd_cwnd_set(tp, max_t(u32, 1U, val));
|
| 3095 |
|
| 3096 | tp->snd_cwnd_cnt = 0;
|
| 3097 | tp->snd_cwnd_stamp = tcp_jiffies32;
|
| 3098 | tp->snd_ssthresh = tcp_current_ssthresh(sk);
|
| 3099 |
|
| 3100 | icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size;
|
| 3101 | icsk->icsk_mtup.probe_size = 0;
|
| 3102 | tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
|
| 3103 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMTUPSUCCESS);
|
| 3104 | }
|
| 3105 |
|
| 3106 | |
| 3107 | * congestion, like path MTU reductions or failed client TFO attempts. In these
|
| 3108 | * cases we call this function to retransmit as many packets as cwnd allows,
|
| 3109 | * without reducing cwnd. Given that retransmits will set retrans_stamp to a
|
| 3110 | * non-zero value (and may do so in a later calling context due to TSQ), we
|
| 3111 | * also enter CA_Loss so that we track when all retransmitted packets are ACKed
|
| 3112 | * and clear retrans_stamp when that happens (to ensure later recurring RTOs
|
| 3113 | * are using the correct retrans_stamp and don't declare ETIMEDOUT
|
| 3114 | * prematurely).
|
| 3115 |
|
| 3116 | static void tcp_non_congestion_loss_retransmit(struct sock *sk)
|
| 3117 | {
|
| 3118 | const struct inet_connection_sock *icsk = inet_csk(sk);
|
| 3119 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3120 |
|
| 3121 | if (icsk->icsk_ca_state != TCP_CA_Loss) {
|
| 3122 | tp->high_seq = tp->snd_nxt;
|
| 3123 | WRITE_ONCE(tp->snd_ssthresh, tcp_current_ssthresh(sk));
|
| 3124 | tp->prior_ssthresh = 0;
|
| 3125 | tp->undo_marker = 0;
|
| 3126 | tcp_set_ca_state(sk, TCP_CA_Loss);
|
| 3127 | }
|
| 3128 | tcp_xmit_retransmit_queue(sk);
|
| 3129 | }
|
| 3130 |
|
| 3131 | |
| 3132 | * tcp_timer. This is used for path mtu discovery.
|
| 3133 | * The socket is already locked here.
|
| 3134 |
|
| 3135 | void tcp_simple_retransmit(struct sock *sk)
|
| 3136 | {
|
| 3137 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3138 | struct sk_buff *skb;
|
| 3139 | int mss;
|
| 3140 |
|
| 3141 | |
| 3142 | * the retransmit queue, this is done by tcp_send_syn_data().
|
| 3143 | * As a result simply checking the MSS of the frames in the queue
|
| 3144 | * will not work for the SYN packet.
|
| 3145 | *
|
| 3146 | * Us being here is an indication of a path MTU issue so we can
|
| 3147 | * assume that the fastopen SYN was lost and just mark all the
|
| 3148 | * frames in the retransmit queue as lost. We will use an MSS of
|
| 3149 | * -1 to mark all frames as lost, otherwise compute the current MSS.
|
| 3150 |
|
| 3151 | if (tp->syn_data && sk->sk_state == TCP_SYN_SENT)
|
| 3152 | mss = -1;
|
| 3153 | else
|
| 3154 | mss = tcp_current_mss(sk);
|
| 3155 |
|
| 3156 | skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
|
| 3157 | if (tcp_skb_seglen(skb) > mss)
|
| 3158 | tcp_mark_skb_lost(sk, skb);
|
| 3159 | }
|
| 3160 |
|
| 3161 | if (!tp->lost_out)
|
| 3162 | return;
|
| 3163 |
|
| 3164 | if (tcp_is_reno(tp))
|
| 3165 | tcp_limit_reno_sacked(tp);
|
| 3166 |
|
| 3167 | tcp_verify_left_out(tp);
|
| 3168 |
|
| 3169 | |
| 3170 | * Reason is that we do not increase amount of _data_
|
| 3171 | * in network, but units changed and effective
|
| 3172 | * cwnd/ssthresh really reduced now.
|
| 3173 |
|
| 3174 | tcp_non_congestion_loss_retransmit(sk);
|
| 3175 | }
|
| 3176 |
|
| 3177 | void tcp_enter_recovery(struct sock *sk, bool ece_ack)
|
| 3178 | {
|
| 3179 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3180 | int mib_idx;
|
| 3181 |
|
| 3182 |
|
| 3183 | tcp_retrans_stamp_cleanup(sk);
|
| 3184 |
|
| 3185 | if (tcp_is_reno(tp))
|
| 3186 | mib_idx = LINUX_MIB_TCPRENORECOVERY;
|
| 3187 | else
|
| 3188 | mib_idx = LINUX_MIB_TCPSACKRECOVERY;
|
| 3189 |
|
| 3190 | NET_INC_STATS(sock_net(sk), mib_idx);
|
| 3191 |
|
| 3192 | tp->prior_ssthresh = 0;
|
| 3193 | tcp_init_undo(tp);
|
| 3194 |
|
| 3195 | if (!tcp_in_cwnd_reduction(sk)) {
|
| 3196 | if (!ece_ack)
|
| 3197 | tp->prior_ssthresh = tcp_current_ssthresh(sk);
|
| 3198 | tcp_init_cwnd_reduction(sk);
|
| 3199 | }
|
| 3200 | tcp_set_ca_state(sk, TCP_CA_Recovery);
|
| 3201 | }
|
| 3202 |
|
| 3203 | static void tcp_update_rto_time(struct tcp_sock *tp)
|
| 3204 | {
|
| 3205 | if (tp->rto_stamp) {
|
| 3206 | tp->total_rto_time += tcp_time_stamp_ms(tp) - tp->rto_stamp;
|
| 3207 | tp->rto_stamp = 0;
|
| 3208 | }
|
| 3209 | }
|
| 3210 |
|
| 3211 | |
| 3212 | * recovered or spurious. Otherwise retransmits more on partial ACKs.
|
| 3213 |
|
| 3214 | static void tcp_process_loss(struct sock *sk, int flag, int num_dupack,
|
| 3215 | int *rexmit)
|
| 3216 | {
|
| 3217 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3218 | bool recovered = !before(tp->snd_una, tp->high_seq);
|
| 3219 |
|
| 3220 | if ((flag & FLAG_SND_UNA_ADVANCED || rcu_access_pointer(tp->fastopen_rsk)) &&
|
| 3221 | tcp_try_undo_loss(sk, false))
|
| 3222 | return;
|
| 3223 |
|
| 3224 | if (tp->frto) {
|
| 3225 | |
| 3226 | * lost, i.e., never-retransmitted data are (s)acked.
|
| 3227 |
|
| 3228 | if ((flag & FLAG_ORIG_SACK_ACKED) &&
|
| 3229 | tcp_try_undo_loss(sk, true))
|
| 3230 | return;
|
| 3231 |
|
| 3232 | if (after(tp->snd_nxt, tp->high_seq)) {
|
| 3233 | if (flag & FLAG_DATA_SACKED || num_dupack)
|
| 3234 | tp->frto = 0;
|
| 3235 | } else if (flag & FLAG_SND_UNA_ADVANCED && !recovered) {
|
| 3236 | tp->high_seq = tp->snd_nxt;
|
| 3237 | |
| 3238 | * is updated in tcp_ack()). Otherwise fall back to
|
| 3239 | * the conventional recovery.
|
| 3240 |
|
| 3241 | if (!tcp_write_queue_empty(sk) &&
|
| 3242 | after(tcp_wnd_end(tp), tp->snd_nxt)) {
|
| 3243 | *rexmit = REXMIT_NEW;
|
| 3244 | return;
|
| 3245 | }
|
| 3246 | tp->frto = 0;
|
| 3247 | }
|
| 3248 | }
|
| 3249 |
|
| 3250 | if (recovered) {
|
| 3251 |
|
| 3252 | tcp_try_undo_recovery(sk);
|
| 3253 | return;
|
| 3254 | }
|
| 3255 | if (tcp_is_reno(tp)) {
|
| 3256 | |
| 3257 | * delivered. Lower inflight to clock out (re)transmissions.
|
| 3258 |
|
| 3259 | if (after(tp->snd_nxt, tp->high_seq) && num_dupack)
|
| 3260 | tcp_add_reno_sack(sk, num_dupack, flag & FLAG_ECE);
|
| 3261 | else if (flag & FLAG_SND_UNA_ADVANCED)
|
| 3262 | tcp_reset_reno_sack(tp);
|
| 3263 | }
|
| 3264 | *rexmit = REXMIT_LOST;
|
| 3265 | }
|
| 3266 |
|
| 3267 |
|
| 3268 | static bool tcp_try_undo_partial(struct sock *sk, u32 prior_snd_una)
|
| 3269 | {
|
| 3270 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3271 |
|
| 3272 | if (tp->undo_marker && tcp_packet_delayed(tp)) {
|
| 3273 | |
| 3274 | * packet, rather than with a retransmit. Check reordering.
|
| 3275 |
|
| 3276 | tcp_check_sack_reordering(sk, prior_snd_una, 1);
|
| 3277 |
|
| 3278 | |
| 3279 | * than we realized. If there are no retransmits out then we
|
| 3280 | * can undo. Otherwise we clock out new packets but do not
|
| 3281 | * mark more packets lost or retransmit more.
|
| 3282 |
|
| 3283 | if (tp->retrans_out)
|
| 3284 | return true;
|
| 3285 |
|
| 3286 | if (!tcp_any_retrans_done(sk))
|
| 3287 | tp->retrans_stamp = 0;
|
| 3288 |
|
| 3289 | DBGUNDO(sk, "partial recovery");
|
| 3290 | tcp_undo_cwnd_reduction(sk, true);
|
| 3291 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO);
|
| 3292 | tcp_try_keep_open(sk);
|
| 3293 | }
|
| 3294 | return false;
|
| 3295 | }
|
| 3296 |
|
| 3297 | static void tcp_identify_packet_loss(struct sock *sk, int *ack_flag)
|
| 3298 | {
|
| 3299 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3300 |
|
| 3301 | if (tcp_rtx_queue_empty(sk))
|
| 3302 | return;
|
| 3303 |
|
| 3304 | if (unlikely(tcp_is_reno(tp))) {
|
| 3305 | tcp_newreno_mark_lost(sk, *ack_flag & FLAG_SND_UNA_ADVANCED);
|
| 3306 | } else {
|
| 3307 | u32 prior_retrans = tp->retrans_out;
|
| 3308 |
|
| 3309 | if (tcp_rack_mark_lost(sk))
|
| 3310 | *ack_flag &= ~FLAG_SET_XMIT_TIMER;
|
| 3311 | if (prior_retrans > tp->retrans_out)
|
| 3312 | *ack_flag |= FLAG_LOST_RETRANS;
|
| 3313 | }
|
| 3314 | }
|
| 3315 |
|
| 3316 | |
| 3317 | * Main goal of this function is to calculate new estimate for left_out,
|
| 3318 | * taking into account both packets sitting in receiver's buffer and
|
| 3319 | * packets lost by network.
|
| 3320 | *
|
| 3321 | * Besides that it updates the congestion state when packet loss or ECN
|
| 3322 | * is detected. But it does not reduce the cwnd, it is done by the
|
| 3323 | * congestion control later.
|
| 3324 | *
|
| 3325 | * It does _not_ decide what to send, it is made in function
|
| 3326 | * tcp_xmit_retransmit_queue().
|
| 3327 |
|
| 3328 | static void tcp_fastretrans_alert(struct sock *sk, const u32 prior_snd_una,
|
| 3329 | int num_dupack, int *ack_flag, int *rexmit)
|
| 3330 | {
|
| 3331 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 3332 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3333 | int flag = *ack_flag;
|
| 3334 | bool ece_ack = flag & FLAG_ECE;
|
| 3335 |
|
| 3336 | if (!tp->packets_out && tp->sacked_out)
|
| 3337 | tp->sacked_out = 0;
|
| 3338 |
|
| 3339 | |
| 3340 | * A. ECE, hence prohibit cwnd undoing, the reduction is required.
|
| 3341 | if (ece_ack)
|
| 3342 | tp->prior_ssthresh = 0;
|
| 3343 |
|
| 3344 |
|
| 3345 | if (tcp_check_sack_reneging(sk, ack_flag))
|
| 3346 | return;
|
| 3347 |
|
| 3348 |
|
| 3349 | tcp_verify_left_out(tp);
|
| 3350 |
|
| 3351 | |
| 3352 | * when high_seq is ACKed.
|
| 3353 | if (icsk->icsk_ca_state == TCP_CA_Open) {
|
| 3354 | WARN_ON(tp->retrans_out != 0 && !tp->syn_data);
|
| 3355 | tp->retrans_stamp = 0;
|
| 3356 | } else if (!before(tp->snd_una, tp->high_seq)) {
|
| 3357 | switch (icsk->icsk_ca_state) {
|
| 3358 | case TCP_CA_CWR:
|
| 3359 | |
| 3360 | * is ACKed for CWR bit to reach receiver.
|
| 3361 | if (tp->snd_una != tp->high_seq) {
|
| 3362 | tcp_end_cwnd_reduction(sk);
|
| 3363 | tcp_set_ca_state(sk, TCP_CA_Open);
|
| 3364 | }
|
| 3365 | break;
|
| 3366 |
|
| 3367 | case TCP_CA_Recovery:
|
| 3368 | if (tcp_is_reno(tp))
|
| 3369 | tcp_reset_reno_sack(tp);
|
| 3370 | if (tcp_try_undo_recovery(sk))
|
| 3371 | return;
|
| 3372 | tcp_end_cwnd_reduction(sk);
|
| 3373 | break;
|
| 3374 | }
|
| 3375 | }
|
| 3376 |
|
| 3377 |
|
| 3378 | switch (icsk->icsk_ca_state) {
|
| 3379 | case TCP_CA_Recovery:
|
| 3380 | if (!(flag & FLAG_SND_UNA_ADVANCED)) {
|
| 3381 | if (tcp_is_reno(tp))
|
| 3382 | tcp_add_reno_sack(sk, num_dupack, ece_ack);
|
| 3383 | } else if (tcp_try_undo_partial(sk, prior_snd_una))
|
| 3384 | return;
|
| 3385 |
|
| 3386 | if (tcp_try_undo_dsack(sk))
|
| 3387 | tcp_try_to_open(sk, flag);
|
| 3388 |
|
| 3389 | tcp_identify_packet_loss(sk, ack_flag);
|
| 3390 | if (icsk->icsk_ca_state != TCP_CA_Recovery) {
|
| 3391 | if (!tcp_time_to_recover(tp))
|
| 3392 | return;
|
| 3393 | |
| 3394 | * starts a new recovery (e.g. reordering then loss);
|
| 3395 |
|
| 3396 | tcp_enter_recovery(sk, ece_ack);
|
| 3397 | }
|
| 3398 | break;
|
| 3399 | case TCP_CA_Loss:
|
| 3400 | tcp_process_loss(sk, flag, num_dupack, rexmit);
|
| 3401 | if (icsk->icsk_ca_state != TCP_CA_Loss)
|
| 3402 | tcp_update_rto_time(tp);
|
| 3403 | tcp_identify_packet_loss(sk, ack_flag);
|
| 3404 | if (!(icsk->icsk_ca_state == TCP_CA_Open ||
|
| 3405 | (*ack_flag & FLAG_LOST_RETRANS)))
|
| 3406 | return;
|
| 3407 |
|
| 3408 | fallthrough;
|
| 3409 | default:
|
| 3410 | if (tcp_is_reno(tp)) {
|
| 3411 | if (flag & FLAG_SND_UNA_ADVANCED)
|
| 3412 | tcp_reset_reno_sack(tp);
|
| 3413 | tcp_add_reno_sack(sk, num_dupack, ece_ack);
|
| 3414 | }
|
| 3415 |
|
| 3416 | if (icsk->icsk_ca_state <= TCP_CA_Disorder)
|
| 3417 | tcp_try_undo_dsack(sk);
|
| 3418 |
|
| 3419 | tcp_identify_packet_loss(sk, ack_flag);
|
| 3420 | if (!tcp_time_to_recover(tp)) {
|
| 3421 | tcp_try_to_open(sk, flag);
|
| 3422 | return;
|
| 3423 | }
|
| 3424 |
|
| 3425 |
|
| 3426 | if (icsk->icsk_ca_state < TCP_CA_CWR &&
|
| 3427 | icsk->icsk_mtup.probe_size &&
|
| 3428 | tp->snd_una == tp->mtu_probe.probe_seq_start) {
|
| 3429 | tcp_mtup_probe_failed(sk);
|
| 3430 |
|
| 3431 | tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) + 1);
|
| 3432 | tcp_simple_retransmit(sk);
|
| 3433 | return;
|
| 3434 | }
|
| 3435 |
|
| 3436 |
|
| 3437 | tcp_enter_recovery(sk, ece_ack);
|
| 3438 | }
|
| 3439 |
|
| 3440 | *rexmit = REXMIT_LOST;
|
| 3441 | }
|
| 3442 |
|
| 3443 | static void tcp_update_rtt_min(struct sock *sk, u32 rtt_us, const int flag)
|
| 3444 | {
|
| 3445 | u32 wlen = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_rtt_wlen) * HZ;
|
| 3446 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3447 |
|
| 3448 | if ((flag & FLAG_ACK_MAYBE_DELAYED) && rtt_us > tcp_min_rtt(tp)) {
|
| 3449 | |
| 3450 | * the min filter would pick it up and overestimate the
|
| 3451 | * prop. delay when it expires. Skip suspected delayed ACKs.
|
| 3452 |
|
| 3453 | return;
|
| 3454 | }
|
| 3455 | minmax_running_min(&tp->rtt_min, wlen, tcp_jiffies32,
|
| 3456 | rtt_us ? : jiffies_to_usecs(1));
|
| 3457 | }
|
| 3458 |
|
| 3459 | static bool tcp_ack_update_rtt(struct sock *sk, const int flag,
|
| 3460 | long seq_rtt_us, long sack_rtt_us,
|
| 3461 | long ca_rtt_us, struct rate_sample *rs)
|
| 3462 | {
|
| 3463 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 3464 |
|
| 3465 | |
| 3466 | * broken middle-boxes or peers may corrupt TS-ECR fields. But
|
| 3467 | * Karn's algorithm forbids taking RTT if some retransmitted data
|
| 3468 | * is acked (RFC6298).
|
| 3469 |
|
| 3470 | if (seq_rtt_us < 0)
|
| 3471 | seq_rtt_us = sack_rtt_us;
|
| 3472 |
|
| 3473 | |
| 3474 | * update the averaged RTT measurement only if the segment
|
| 3475 | * acknowledges some new data, i.e., only if it advances the
|
| 3476 | * left edge of the send window.
|
| 3477 | * See draft-ietf-tcplw-high-performance-00, section 3.3.
|
| 3478 |
|
| 3479 | if (seq_rtt_us < 0 && tp->rx_opt.saw_tstamp &&
|
| 3480 | tp->rx_opt.rcv_tsecr && flag & FLAG_ACKED)
|
| 3481 | seq_rtt_us = ca_rtt_us = tcp_rtt_tsopt_us(tp, 1);
|
| 3482 |
|
| 3483 | rs->rtt_us = ca_rtt_us;
|
| 3484 | if (seq_rtt_us < 0)
|
| 3485 | return false;
|
| 3486 |
|
| 3487 | |
| 3488 | * always taken together with ACK, SACK, or TS-opts. Any negative
|
| 3489 | * values will be skipped with the seq_rtt_us < 0 check above.
|
| 3490 |
|
| 3491 | tcp_update_rtt_min(sk, ca_rtt_us, flag);
|
| 3492 | tcp_rtt_estimator(sk, seq_rtt_us);
|
| 3493 | tcp_set_rto(sk);
|
| 3494 |
|
| 3495 |
|
| 3496 | inet_csk(sk)->icsk_backoff = 0;
|
| 3497 | return true;
|
| 3498 | }
|
| 3499 |
|
| 3500 |
|
| 3501 | void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req)
|
| 3502 | {
|
| 3503 | struct rate_sample rs;
|
| 3504 | long rtt_us = -1L;
|
| 3505 |
|
| 3506 | if (req && !req->num_retrans && tcp_rsk(req)->snt_synack)
|
| 3507 | rtt_us = tcp_stamp_us_delta(tcp_clock_us(), tcp_rsk(req)->snt_synack);
|
| 3508 |
|
| 3509 | tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, rtt_us, -1L, rtt_us, &rs);
|
| 3510 | }
|
| 3511 |
|
| 3512 |
|
| 3513 | static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 acked)
|
| 3514 | {
|
| 3515 | const struct inet_connection_sock *icsk = inet_csk(sk);
|
| 3516 |
|
| 3517 | icsk->icsk_ca_ops->cong_avoid(sk, ack, acked);
|
| 3518 | tcp_sk(sk)->snd_cwnd_stamp = tcp_jiffies32;
|
| 3519 | }
|
| 3520 |
|
| 3521 | |
| 3522 | * RFC2988 recommends to restart timer to now+rto.
|
| 3523 |
|
| 3524 | void tcp_rearm_rto(struct sock *sk)
|
| 3525 | {
|
| 3526 | const struct inet_connection_sock *icsk = inet_csk(sk);
|
| 3527 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3528 |
|
| 3529 | |
| 3530 | * for SYN-ACK retrans purpose, stay put.
|
| 3531 |
|
| 3532 | if (rcu_access_pointer(tp->fastopen_rsk))
|
| 3533 | return;
|
| 3534 |
|
| 3535 | if (!tp->packets_out) {
|
| 3536 | inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
|
| 3537 | } else {
|
| 3538 | u32 rto = inet_csk(sk)->icsk_rto;
|
| 3539 |
|
| 3540 | if (icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
|
| 3541 | icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
|
| 3542 | s64 delta_us = tcp_rto_delta_us(sk);
|
| 3543 | |
| 3544 | * when the retrans timer fires and is rescheduled.
|
| 3545 |
|
| 3546 | rto = usecs_to_jiffies(max_t(int, delta_us, 1));
|
| 3547 | }
|
| 3548 | tcp_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto, true);
|
| 3549 | }
|
| 3550 | }
|
| 3551 |
|
| 3552 |
|
| 3553 | static void tcp_set_xmit_timer(struct sock *sk)
|
| 3554 | {
|
| 3555 | if (!tcp_sk(sk)->packets_out || !tcp_schedule_loss_probe(sk, true))
|
| 3556 | tcp_rearm_rto(sk);
|
| 3557 | }
|
| 3558 |
|
| 3559 |
|
| 3560 | static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)
|
| 3561 | {
|
| 3562 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3563 | u32 packets_acked;
|
| 3564 |
|
| 3565 | BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una));
|
| 3566 |
|
| 3567 | packets_acked = tcp_skb_pcount(skb);
|
| 3568 | if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
|
| 3569 | return 0;
|
| 3570 | packets_acked -= tcp_skb_pcount(skb);
|
| 3571 |
|
| 3572 | if (packets_acked) {
|
| 3573 | BUG_ON(tcp_skb_pcount(skb) == 0);
|
| 3574 | BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq));
|
| 3575 | }
|
| 3576 |
|
| 3577 | return packets_acked;
|
| 3578 | }
|
| 3579 |
|
| 3580 | static void tcp_ack_tstamp(struct sock *sk, struct sk_buff *skb,
|
| 3581 | const struct sk_buff *ack_skb, u32 prior_snd_una)
|
| 3582 | {
|
| 3583 | const struct skb_shared_info *shinfo;
|
| 3584 |
|
| 3585 |
|
| 3586 | if (likely(!TCP_SKB_CB(skb)->txstamp_ack))
|
| 3587 | return;
|
| 3588 |
|
| 3589 | shinfo = skb_shinfo(skb);
|
| 3590 | if (!before(shinfo->tskey, prior_snd_una) &&
|
| 3591 | before(shinfo->tskey, tcp_sk(sk)->snd_una)) {
|
| 3592 | tcp_skb_tsorted_save(skb) {
|
| 3593 | __skb_tstamp_tx(skb, ack_skb, NULL, sk, SCM_TSTAMP_ACK);
|
| 3594 | } tcp_skb_tsorted_restore(skb);
|
| 3595 | }
|
| 3596 | }
|
| 3597 |
|
| 3598 | |
| 3599 | * is before the ack sequence we can discard it as it's confirmed to have
|
| 3600 | * arrived at the other end.
|
| 3601 |
|
| 3602 | static int tcp_clean_rtx_queue(struct sock *sk, const struct sk_buff *ack_skb,
|
| 3603 | u32 prior_fack, u32 prior_snd_una,
|
| 3604 | struct tcp_sacktag_state *sack, bool ece_ack)
|
| 3605 | {
|
| 3606 | const struct inet_connection_sock *icsk = inet_csk(sk);
|
| 3607 | u64 first_ackt, last_ackt;
|
| 3608 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3609 | u32 prior_sacked = tp->sacked_out;
|
| 3610 | u32 reord = tp->snd_nxt;
|
| 3611 | struct sk_buff *skb, *next;
|
| 3612 | bool fully_acked = true;
|
| 3613 | long sack_rtt_us = -1L;
|
| 3614 | long seq_rtt_us = -1L;
|
| 3615 | long ca_rtt_us = -1L;
|
| 3616 | u32 pkts_acked = 0;
|
| 3617 | bool rtt_update;
|
| 3618 | int flag = 0;
|
| 3619 |
|
| 3620 | first_ackt = 0;
|
| 3621 |
|
| 3622 | for (skb = skb_rb_first(&sk->tcp_rtx_queue); skb; skb = next) {
|
| 3623 | struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
|
| 3624 | const u32 start_seq = scb->seq;
|
| 3625 | u8 sacked = scb->sacked;
|
| 3626 | u32 acked_pcount;
|
| 3627 |
|
| 3628 |
|
| 3629 | if (after(scb->end_seq, tp->snd_una)) {
|
| 3630 | if (tcp_skb_pcount(skb) == 1 ||
|
| 3631 | !after(tp->snd_una, scb->seq))
|
| 3632 | break;
|
| 3633 |
|
| 3634 | acked_pcount = tcp_tso_acked(sk, skb);
|
| 3635 | if (!acked_pcount)
|
| 3636 | break;
|
| 3637 | fully_acked = false;
|
| 3638 | } else {
|
| 3639 | acked_pcount = tcp_skb_pcount(skb);
|
| 3640 | }
|
| 3641 |
|
| 3642 | if (unlikely(sacked & TCPCB_RETRANS)) {
|
| 3643 | if (sacked & TCPCB_SACKED_RETRANS)
|
| 3644 | tp->retrans_out -= acked_pcount;
|
| 3645 | flag |= FLAG_RETRANS_DATA_ACKED;
|
| 3646 | } else if (!(sacked & TCPCB_SACKED_ACKED)) {
|
| 3647 | last_ackt = tcp_skb_timestamp_us(skb);
|
| 3648 | WARN_ON_ONCE(last_ackt == 0);
|
| 3649 | if (!first_ackt)
|
| 3650 | first_ackt = last_ackt;
|
| 3651 |
|
| 3652 | if (before(start_seq, reord))
|
| 3653 | reord = start_seq;
|
| 3654 | if (!after(scb->end_seq, tp->high_seq))
|
| 3655 | flag |= FLAG_ORIG_SACK_ACKED;
|
| 3656 | }
|
| 3657 |
|
| 3658 | if (sacked & TCPCB_SACKED_ACKED) {
|
| 3659 | tp->sacked_out -= acked_pcount;
|
| 3660 |
|
| 3661 | sack->delivered_bytes -= skb->len;
|
| 3662 | } else if (tcp_is_sack(tp)) {
|
| 3663 | tcp_count_delivered(tp, acked_pcount, ece_ack);
|
| 3664 | if (!tcp_skb_spurious_retrans(tp, skb))
|
| 3665 | tcp_rack_advance(tp, sacked, scb->end_seq,
|
| 3666 | tcp_skb_timestamp_us(skb));
|
| 3667 | }
|
| 3668 | if (sacked & TCPCB_LOST)
|
| 3669 | tp->lost_out -= acked_pcount;
|
| 3670 |
|
| 3671 | tp->packets_out -= acked_pcount;
|
| 3672 | pkts_acked += acked_pcount;
|
| 3673 | tcp_rate_skb_delivered(sk, skb, sack->rate);
|
| 3674 |
|
| 3675 | |
| 3676 | * just like anything else we transmit. It is not
|
| 3677 | * true data, and if we misinform our callers that
|
| 3678 | * this ACK acks real data, we will erroneously exit
|
| 3679 | * connection startup slow start one packet too
|
| 3680 | * quickly. This is severely frowned upon behavior.
|
| 3681 |
|
| 3682 | if (likely(!(scb->tcp_flags & TCPHDR_SYN))) {
|
| 3683 | flag |= FLAG_DATA_ACKED;
|
| 3684 | } else {
|
| 3685 | flag |= FLAG_SYN_ACKED;
|
| 3686 | tp->retrans_stamp = 0;
|
| 3687 | }
|
| 3688 |
|
| 3689 | if (!fully_acked)
|
| 3690 | break;
|
| 3691 |
|
| 3692 | tcp_ack_tstamp(sk, skb, ack_skb, prior_snd_una);
|
| 3693 |
|
| 3694 | next = skb_rb_next(skb);
|
| 3695 | if (unlikely(skb == tp->retransmit_skb_hint))
|
| 3696 | tp->retransmit_skb_hint = NULL;
|
| 3697 | tcp_highest_sack_replace(sk, skb, next);
|
| 3698 | tcp_rtx_queue_unlink_and_free(skb, sk);
|
| 3699 | }
|
| 3700 |
|
| 3701 | if (!skb)
|
| 3702 | tcp_chrono_stop(sk, TCP_CHRONO_BUSY);
|
| 3703 |
|
| 3704 | if (likely(between(tp->snd_up, prior_snd_una, tp->snd_una)))
|
| 3705 | tp->snd_up = tp->snd_una;
|
| 3706 |
|
| 3707 | if (skb) {
|
| 3708 | tcp_ack_tstamp(sk, skb, ack_skb, prior_snd_una);
|
| 3709 | if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)
|
| 3710 | flag |= FLAG_SACK_RENEGING;
|
| 3711 | }
|
| 3712 |
|
| 3713 | if (likely(first_ackt) && !(flag & FLAG_RETRANS_DATA_ACKED)) {
|
| 3714 | seq_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, first_ackt);
|
| 3715 | ca_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, last_ackt);
|
| 3716 |
|
| 3717 | if (pkts_acked == 1 && fully_acked && !prior_sacked &&
|
| 3718 | (tp->snd_una - prior_snd_una) < tp->mss_cache &&
|
| 3719 | sack->rate->prior_delivered + 1 == tp->delivered &&
|
| 3720 | !(flag & (FLAG_CA_ALERT | FLAG_SYN_ACKED))) {
|
| 3721 | |
| 3722 | * from a lone runt packet over the round trip to
|
| 3723 | * a receiver w/o out-of-order or CE events.
|
| 3724 |
|
| 3725 | flag |= FLAG_ACK_MAYBE_DELAYED;
|
| 3726 | }
|
| 3727 | }
|
| 3728 | if (sack->first_sackt) {
|
| 3729 | sack_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, sack->first_sackt);
|
| 3730 | ca_rtt_us = tcp_stamp_us_delta(tp->tcp_mstamp, sack->last_sackt);
|
| 3731 | }
|
| 3732 | rtt_update = tcp_ack_update_rtt(sk, flag, seq_rtt_us, sack_rtt_us,
|
| 3733 | ca_rtt_us, sack->rate);
|
| 3734 |
|
| 3735 | if (flag & FLAG_ACKED) {
|
| 3736 | flag |= FLAG_SET_XMIT_TIMER;
|
| 3737 | if (unlikely(icsk->icsk_mtup.probe_size &&
|
| 3738 | !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {
|
| 3739 | tcp_mtup_probe_success(sk);
|
| 3740 | }
|
| 3741 |
|
| 3742 | if (tcp_is_reno(tp)) {
|
| 3743 | tcp_remove_reno_sacks(sk, pkts_acked, ece_ack);
|
| 3744 |
|
| 3745 | |
| 3746 | * retransmitted, non-SACK case cannot confirm that
|
| 3747 | * progress was due to original transmission due to
|
| 3748 | * lack of TCPCB_SACKED_ACKED bits even if some of
|
| 3749 | * the packets may have been never retransmitted.
|
| 3750 |
|
| 3751 | if (flag & FLAG_RETRANS_DATA_ACKED)
|
| 3752 | flag &= ~FLAG_ORIG_SACK_ACKED;
|
| 3753 | } else {
|
| 3754 |
|
| 3755 | if (before(reord, prior_fack))
|
| 3756 | tcp_check_sack_reordering(sk, reord, 0);
|
| 3757 | }
|
| 3758 |
|
| 3759 | sack->delivered_bytes = (skb ?
|
| 3760 | TCP_SKB_CB(skb)->seq : tp->snd_una) -
|
| 3761 | prior_snd_una;
|
| 3762 | } else if (skb && rtt_update && sack_rtt_us >= 0 &&
|
| 3763 | sack_rtt_us > tcp_stamp_us_delta(tp->tcp_mstamp,
|
| 3764 | tcp_skb_timestamp_us(skb))) {
|
| 3765 | |
| 3766 | * after when the head was last (re)transmitted. Otherwise the
|
| 3767 | * timeout may continue to extend in loss recovery.
|
| 3768 |
|
| 3769 | flag |= FLAG_SET_XMIT_TIMER;
|
| 3770 | }
|
| 3771 |
|
| 3772 | if (icsk->icsk_ca_ops->pkts_acked) {
|
| 3773 | struct ack_sample sample = { .pkts_acked = pkts_acked,
|
| 3774 | .rtt_us = sack->rate->rtt_us };
|
| 3775 |
|
| 3776 | sample.in_flight = tp->mss_cache *
|
| 3777 | (tp->delivered - sack->rate->prior_delivered);
|
| 3778 | icsk->icsk_ca_ops->pkts_acked(sk, &sample);
|
| 3779 | }
|
| 3780 |
|
| 3781 | #if FASTRETRANS_DEBUG > 0
|
| 3782 | WARN_ON((int)tp->sacked_out < 0);
|
| 3783 | WARN_ON((int)tp->lost_out < 0);
|
| 3784 | WARN_ON((int)tp->retrans_out < 0);
|
| 3785 | if (!tp->packets_out && tcp_is_sack(tp)) {
|
| 3786 | icsk = inet_csk(sk);
|
| 3787 | if (tp->lost_out) {
|
| 3788 | pr_debug("Leak l=%u %d\n",
|
| 3789 | tp->lost_out, icsk->icsk_ca_state);
|
| 3790 | tp->lost_out = 0;
|
| 3791 | }
|
| 3792 | if (tp->sacked_out) {
|
| 3793 | pr_debug("Leak s=%u %d\n",
|
| 3794 | tp->sacked_out, icsk->icsk_ca_state);
|
| 3795 | tp->sacked_out = 0;
|
| 3796 | }
|
| 3797 | if (tp->retrans_out) {
|
| 3798 | pr_debug("Leak r=%u %d\n",
|
| 3799 | tp->retrans_out, icsk->icsk_ca_state);
|
| 3800 | tp->retrans_out = 0;
|
| 3801 | }
|
| 3802 | }
|
| 3803 | #endif
|
| 3804 | return flag;
|
| 3805 | }
|
| 3806 |
|
| 3807 | static void tcp_ack_probe(struct sock *sk)
|
| 3808 | {
|
| 3809 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 3810 | struct sk_buff *head = tcp_send_head(sk);
|
| 3811 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 3812 |
|
| 3813 |
|
| 3814 | if (!head)
|
| 3815 | return;
|
| 3816 | if (!after(TCP_SKB_CB(head)->end_seq, tcp_wnd_end(tp))) {
|
| 3817 | icsk->icsk_backoff = 0;
|
| 3818 | icsk->icsk_probes_tstamp = 0;
|
| 3819 | inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0);
|
| 3820 | |
| 3821 | * This function is not for random using!
|
| 3822 |
|
| 3823 | } else {
|
| 3824 | unsigned long when = tcp_probe0_when(sk, tcp_rto_max(sk));
|
| 3825 |
|
| 3826 | when = tcp_clamp_probe0_to_user_timeout(sk, when);
|
| 3827 | tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0, when, true);
|
| 3828 | }
|
| 3829 | }
|
| 3830 |
|
| 3831 | static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag)
|
| 3832 | {
|
| 3833 | return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||
|
| 3834 | inet_csk(sk)->icsk_ca_state != TCP_CA_Open;
|
| 3835 | }
|
| 3836 |
|
| 3837 |
|
| 3838 | static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
|
| 3839 | {
|
| 3840 | |
| 3841 | * delivered regardless of its ordering. Otherwise stay conservative
|
| 3842 | * and only grow cwnd on in-order delivery (RFC5681). A stretched ACK w/
|
| 3843 | * new SACK or ECE mark may first advance cwnd here and later reduce
|
| 3844 | * cwnd in tcp_fastretrans_alert() based on more states.
|
| 3845 |
|
| 3846 | if (tcp_sk(sk)->reordering >
|
| 3847 | READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reordering))
|
| 3848 | return flag & FLAG_FORWARD_PROGRESS;
|
| 3849 |
|
| 3850 | return flag & FLAG_DATA_ACKED;
|
| 3851 | }
|
| 3852 |
|
| 3853 | |
| 3854 | * cwnd increase and decrease control (tcp_cong_avoid,tcp_*cwnd_reduction).
|
| 3855 | * It's called toward the end of processing an ACK with precise rate
|
| 3856 | * information. All transmission or retransmission are delayed afterwards.
|
| 3857 |
|
| 3858 | static void tcp_cong_control(struct sock *sk, u32 ack, u32 acked_sacked,
|
| 3859 | int flag, const struct rate_sample *rs)
|
| 3860 | {
|
| 3861 | const struct inet_connection_sock *icsk = inet_csk(sk);
|
| 3862 |
|
| 3863 | if (icsk->icsk_ca_ops->cong_control) {
|
| 3864 | icsk->icsk_ca_ops->cong_control(sk, ack, flag, rs);
|
| 3865 | return;
|
| 3866 | }
|
| 3867 |
|
| 3868 | if (tcp_in_cwnd_reduction(sk)) {
|
| 3869 |
|
| 3870 | tcp_cwnd_reduction(sk, acked_sacked, rs->losses, flag);
|
| 3871 | } else if (tcp_may_raise_cwnd(sk, flag)) {
|
| 3872 |
|
| 3873 | tcp_cong_avoid(sk, ack, acked_sacked);
|
| 3874 | }
|
| 3875 | tcp_update_pacing_rate(sk);
|
| 3876 | }
|
| 3877 |
|
| 3878 | |
| 3879 | * The function assumes that snd_una<=ack<=snd_next.
|
| 3880 |
|
| 3881 | static inline bool tcp_may_update_window(const struct tcp_sock *tp,
|
| 3882 | const u32 ack, const u32 ack_seq,
|
| 3883 | const u32 nwin)
|
| 3884 | {
|
| 3885 | return after(ack, tp->snd_una) ||
|
| 3886 | after(ack_seq, tp->snd_wl1) ||
|
| 3887 | (ack_seq == tp->snd_wl1 && (nwin > tp->snd_wnd || !nwin));
|
| 3888 | }
|
| 3889 |
|
| 3890 | static void tcp_snd_sne_update(struct tcp_sock *tp, u32 ack)
|
| 3891 | {
|
| 3892 | #ifdef CONFIG_TCP_AO
|
| 3893 | struct tcp_ao_info *ao;
|
| 3894 |
|
| 3895 | if (!static_branch_unlikely(&tcp_ao_needed.key))
|
| 3896 | return;
|
| 3897 |
|
| 3898 | ao = rcu_dereference_protected(tp->ao_info,
|
| 3899 | lockdep_sock_is_held((struct sock *)tp));
|
| 3900 | if (ao && ack < tp->snd_una) {
|
| 3901 | ao->snd_sne++;
|
| 3902 | trace_tcp_ao_snd_sne_update((struct sock *)tp, ao->snd_sne);
|
| 3903 | }
|
| 3904 | #endif
|
| 3905 | }
|
| 3906 |
|
| 3907 |
|
| 3908 | static void tcp_snd_una_update(struct tcp_sock *tp, u32 ack)
|
| 3909 | {
|
| 3910 | u32 delta = ack - tp->snd_una;
|
| 3911 |
|
| 3912 | sock_owned_by_me((struct sock *)tp);
|
| 3913 | tp->bytes_acked += delta;
|
| 3914 | tcp_snd_sne_update(tp, ack);
|
| 3915 | WRITE_ONCE(tp->snd_una, ack);
|
| 3916 | }
|
| 3917 |
|
| 3918 | static void tcp_rcv_sne_update(struct tcp_sock *tp, u32 seq)
|
| 3919 | {
|
| 3920 | #ifdef CONFIG_TCP_AO
|
| 3921 | struct tcp_ao_info *ao;
|
| 3922 |
|
| 3923 | if (!static_branch_unlikely(&tcp_ao_needed.key))
|
| 3924 | return;
|
| 3925 |
|
| 3926 | ao = rcu_dereference_protected(tp->ao_info,
|
| 3927 | lockdep_sock_is_held((struct sock *)tp));
|
| 3928 | if (ao && seq < tp->rcv_nxt) {
|
| 3929 | ao->rcv_sne++;
|
| 3930 | trace_tcp_ao_rcv_sne_update((struct sock *)tp, ao->rcv_sne);
|
| 3931 | }
|
| 3932 | #endif
|
| 3933 | }
|
| 3934 |
|
| 3935 |
|
| 3936 | static void tcp_rcv_nxt_update(struct tcp_sock *tp, u32 seq)
|
| 3937 | {
|
| 3938 | u32 delta = seq - tp->rcv_nxt;
|
| 3939 |
|
| 3940 | sock_owned_by_me((struct sock *)tp);
|
| 3941 | tp->bytes_received += delta;
|
| 3942 | tcp_rcv_sne_update(tp, seq);
|
| 3943 | WRITE_ONCE(tp->rcv_nxt, seq);
|
| 3944 | }
|
| 3945 |
|
| 3946 | |
| 3947 | *
|
| 3948 | * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
|
| 3949 | * and in FreeBSD. NetBSD's one is even worse.) is wrong.
|
| 3950 |
|
| 3951 | static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,
|
| 3952 | u32 ack_seq)
|
| 3953 | {
|
| 3954 | struct tcp_sock *tp = tcp_sk(sk);
|
| 3955 | int flag = 0;
|
| 3956 | u32 nwin = ntohs(tcp_hdr(skb)->window);
|
| 3957 |
|
| 3958 | if (likely(!tcp_hdr(skb)->syn))
|
| 3959 | nwin <<= tp->rx_opt.snd_wscale;
|
| 3960 |
|
| 3961 | if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {
|
| 3962 | flag |= FLAG_WIN_UPDATE;
|
| 3963 | tcp_update_wl(tp, ack_seq);
|
| 3964 |
|
| 3965 | if (tp->snd_wnd != nwin) {
|
| 3966 | tp->snd_wnd = nwin;
|
| 3967 |
|
| 3968 | |
| 3969 | * fast path is recovered for sending TCP.
|
| 3970 |
|
| 3971 | tp->pred_flags = 0;
|
| 3972 | tcp_fast_path_check(sk);
|
| 3973 |
|
| 3974 | if (!tcp_write_queue_empty(sk))
|
| 3975 | tcp_slow_start_after_idle_check(sk);
|
| 3976 |
|
| 3977 | if (nwin > tp->max_window) {
|
| 3978 | tp->max_window = nwin;
|
| 3979 | tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie);
|
| 3980 | }
|
| 3981 | }
|
| 3982 | }
|
| 3983 |
|
| 3984 | tcp_snd_una_update(tp, ack);
|
| 3985 |
|
| 3986 | return flag;
|
| 3987 | }
|
| 3988 |
|
| 3989 | static bool __tcp_oow_rate_limited(struct net *net, int mib_idx,
|
| 3990 | u32 *last_oow_ack_time)
|
| 3991 | {
|
| 3992 |
|
| 3993 | u32 val = READ_ONCE(*last_oow_ack_time);
|
| 3994 |
|
| 3995 | if (val) {
|
| 3996 | s32 elapsed = (s32)(tcp_jiffies32 - val);
|
| 3997 |
|
| 3998 | if (0 <= elapsed &&
|
| 3999 | elapsed < READ_ONCE(net->ipv4.sysctl_tcp_invalid_ratelimit)) {
|
| 4000 | NET_INC_STATS(net, mib_idx);
|
| 4001 | return true;
|
| 4002 | }
|
| 4003 | }
|
| 4004 |
|
| 4005 | |
| 4006 | * as we might be lockless.
|
| 4007 |
|
| 4008 | WRITE_ONCE(*last_oow_ack_time, tcp_jiffies32);
|
| 4009 |
|
| 4010 | return false;
|
| 4011 | }
|
| 4012 |
|
| 4013 | |
| 4014 | * thus shouldn't send a dupack right now. We rate-limit dupacks in
|
| 4015 | * response to out-of-window SYNs or ACKs to mitigate ACK loops or DoS
|
| 4016 | * attacks that send repeated SYNs or ACKs for the same connection. To
|
| 4017 | * do this, we do not send a duplicate SYNACK or ACK if the remote
|
| 4018 | * endpoint is sending out-of-window SYNs or pure ACKs at a high rate.
|
| 4019 |
|
| 4020 | bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
|
| 4021 | int mib_idx, u32 *last_oow_ack_time)
|
| 4022 | {
|
| 4023 |
|
| 4024 | if ((TCP_SKB_CB(skb)->seq != TCP_SKB_CB(skb)->end_seq) &&
|
| 4025 | !tcp_hdr(skb)->syn)
|
| 4026 | return false;
|
| 4027 |
|
| 4028 | return __tcp_oow_rate_limited(net, mib_idx, last_oow_ack_time);
|
| 4029 | }
|
| 4030 |
|
| 4031 | static void tcp_send_ack_reflect_ect(struct sock *sk, bool accecn_reflector)
|
| 4032 | {
|
| 4033 | struct tcp_sock *tp = tcp_sk(sk);
|
| 4034 | u16 flags = 0;
|
| 4035 |
|
| 4036 | if (accecn_reflector)
|
| 4037 | flags = tcp_accecn_reflector_flags(tp->syn_ect_rcv);
|
| 4038 | __tcp_send_ack(sk, tp->rcv_nxt, flags);
|
| 4039 | }
|
| 4040 |
|
| 4041 | |
| 4042 | * Returns true if a challenge ACK may be sent.
|
| 4043 |
|
| 4044 | static bool tcp_challenge_ack_allowed(struct net *net)
|
| 4045 | {
|
| 4046 | u32 count, now, ack_limit;
|
| 4047 |
|
| 4048 | ack_limit = READ_ONCE(net->ipv4.sysctl_tcp_challenge_ack_limit);
|
| 4049 | if (ack_limit == INT_MAX)
|
| 4050 | return true;
|
| 4051 |
|
| 4052 | now = jiffies / HZ;
|
| 4053 | if (now != READ_ONCE(net->ipv4.tcp_challenge_timestamp)) {
|
| 4054 | u32 half = (ack_limit + 1) >> 1;
|
| 4055 |
|
| 4056 | WRITE_ONCE(net->ipv4.tcp_challenge_timestamp, now);
|
| 4057 | WRITE_ONCE(net->ipv4.tcp_challenge_count,
|
| 4058 | get_random_u32_inclusive(half, ack_limit + half - 1));
|
| 4059 | }
|
| 4060 | count = READ_ONCE(net->ipv4.tcp_challenge_count);
|
| 4061 | if (count > 0) {
|
| 4062 | WRITE_ONCE(net->ipv4.tcp_challenge_count, count - 1);
|
| 4063 | return true;
|
| 4064 | }
|
| 4065 | return false;
|
| 4066 | }
|
| 4067 |
|
| 4068 |
|
| 4069 | static void tcp_send_challenge_ack(struct sock *sk, bool accecn_reflector)
|
| 4070 | {
|
| 4071 | struct tcp_sock *tp = tcp_sk(sk);
|
| 4072 | struct net *net = sock_net(sk);
|
| 4073 |
|
| 4074 |
|
| 4075 | if (__tcp_oow_rate_limited(net,
|
| 4076 | LINUX_MIB_TCPACKSKIPPEDCHALLENGE,
|
| 4077 | &tp->last_oow_ack_time))
|
| 4078 | return;
|
| 4079 |
|
| 4080 |
|
| 4081 | if (tcp_challenge_ack_allowed(net)) {
|
| 4082 | NET_INC_STATS(net, LINUX_MIB_TCPCHALLENGEACK);
|
| 4083 | tcp_send_ack_reflect_ect(sk, accecn_reflector);
|
| 4084 | }
|
| 4085 | }
|
| 4086 |
|
| 4087 | |
| 4088 | * __tcp_oow_rate_limited() directly so that an RST carrying payload
|
| 4089 | * cannot bypass the per-request rate limit.
|
| 4090 |
|
| 4091 | void tcp_reqsk_send_challenge_ack(struct sock *sk, struct sk_buff *skb,
|
| 4092 | struct request_sock *req)
|
| 4093 | {
|
| 4094 | struct net *net = sock_net(sk);
|
| 4095 |
|
| 4096 | if (__tcp_oow_rate_limited(net, LINUX_MIB_TCPACKSKIPPEDCHALLENGE,
|
| 4097 | &tcp_rsk(req)->last_oow_ack_time))
|
| 4098 | return;
|
| 4099 |
|
| 4100 | if (tcp_challenge_ack_allowed(net)) {
|
| 4101 | NET_INC_STATS(net, LINUX_MIB_TCPCHALLENGEACK);
|
| 4102 | req->rsk_ops->send_ack(sk, skb, req);
|
| 4103 | }
|
| 4104 | }
|
| 4105 |
|
| 4106 | static void tcp_store_ts_recent(struct tcp_sock *tp)
|
| 4107 | {
|
| 4108 | tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
|
| 4109 | tp->rx_opt.ts_recent_stamp = ktime_get_seconds();
|
| 4110 | }
|
| 4111 |
|
| 4112 | static int __tcp_replace_ts_recent(struct tcp_sock *tp, s32 tstamp_delta)
|
| 4113 | {
|
| 4114 | tcp_store_ts_recent(tp);
|
| 4115 | return tstamp_delta > 0 ? FLAG_TS_PROGRESS : 0;
|
| 4116 | }
|
| 4117 |
|
| 4118 | static int tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
|
| 4119 | {
|
| 4120 | s32 delta;
|
| 4121 |
|
| 4122 | if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) {
|
| 4123 | |
| 4124 | * extra check below makes sure this can only happen
|
| 4125 | * for pure ACK frames. -DaveM
|
| 4126 | *
|
| 4127 | * Not only, also it occurs for expired timestamps.
|
| 4128 |
|
| 4129 |
|
| 4130 | if (tcp_paws_check(&tp->rx_opt, 0)) {
|
| 4131 | delta = tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent;
|
| 4132 | return __tcp_replace_ts_recent(tp, delta);
|
| 4133 | }
|
| 4134 | }
|
| 4135 |
|
| 4136 | return 0;
|
| 4137 | }
|
| 4138 |
|
| 4139 | |
| 4140 | * resetting tlp_high_seq. Ref: TLP algorithm in RFC8985
|
| 4141 |
|
| 4142 | static void tcp_process_tlp_ack(struct sock *sk, u32 ack, int flag)
|
| 4143 | {
|
| 4144 | struct tcp_sock *tp = tcp_sk(sk);
|
| 4145 |
|
| 4146 | if (before(ack, tp->tlp_high_seq))
|
| 4147 | return;
|
| 4148 |
|
| 4149 | if (!tp->tlp_retrans) {
|
| 4150 |
|
| 4151 | tp->tlp_high_seq = 0;
|
| 4152 | } else if (flag & FLAG_DSACK_TLP) {
|
| 4153 |
|
| 4154 | tp->tlp_high_seq = 0;
|
| 4155 | } else if (after(ack, tp->tlp_high_seq)) {
|
| 4156 | |
| 4157 | * tlp_high_seq in tcp_init_cwnd_reduction()
|
| 4158 |
|
| 4159 | tcp_init_cwnd_reduction(sk);
|
| 4160 | tcp_set_ca_state(sk, TCP_CA_CWR);
|
| 4161 | tcp_end_cwnd_reduction(sk);
|
| 4162 | tcp_try_keep_open(sk);
|
| 4163 | NET_INC_STATS(sock_net(sk),
|
| 4164 | LINUX_MIB_TCPLOSSPROBERECOVERY);
|
| 4165 | } else if (!(flag & (FLAG_SND_UNA_ADVANCED |
|
| 4166 | FLAG_NOT_DUP | FLAG_DATA_SACKED))) {
|
| 4167 |
|
| 4168 | tp->tlp_high_seq = 0;
|
| 4169 | }
|
| 4170 | }
|
| 4171 |
|
| 4172 | static void tcp_in_ack_event(struct sock *sk, int flag)
|
| 4173 | {
|
| 4174 | const struct inet_connection_sock *icsk = inet_csk(sk);
|
| 4175 |
|
| 4176 | if (icsk->icsk_ca_ops->in_ack_event) {
|
| 4177 | u32 ack_ev_flags = 0;
|
| 4178 |
|
| 4179 | if (flag & FLAG_WIN_UPDATE)
|
| 4180 | ack_ev_flags |= CA_ACK_WIN_UPDATE;
|
| 4181 | if (flag & FLAG_SLOWPATH) {
|
| 4182 | ack_ev_flags |= CA_ACK_SLOWPATH;
|
| 4183 | if (flag & FLAG_ECE)
|
| 4184 | ack_ev_flags |= CA_ACK_ECE;
|
| 4185 | }
|
| 4186 |
|
| 4187 | icsk->icsk_ca_ops->in_ack_event(sk, ack_ev_flags);
|
| 4188 | }
|
| 4189 | }
|
| 4190 |
|
| 4191 | |
| 4192 | * loss recovery then now we do any new sends (for FRTO) or
|
| 4193 | * retransmits (for CA_Loss or CA_recovery) that make sense.
|
| 4194 |
|
| 4195 | static void tcp_xmit_recovery(struct sock *sk, int rexmit)
|
| 4196 | {
|
| 4197 | struct tcp_sock *tp = tcp_sk(sk);
|
| 4198 |
|
| 4199 | if (rexmit == REXMIT_NONE || sk->sk_state == TCP_SYN_SENT)
|
| 4200 | return;
|
| 4201 |
|
| 4202 | if (unlikely(rexmit == REXMIT_NEW)) {
|
| 4203 | __tcp_push_pending_frames(sk, tcp_current_mss(sk),
|
| 4204 | TCP_NAGLE_OFF);
|
| 4205 | if (after(tp->snd_nxt, tp->high_seq))
|
| 4206 | return;
|
| 4207 | tp->frto = 0;
|
| 4208 | }
|
| 4209 | tcp_xmit_retransmit_queue(sk);
|
| 4210 | }
|
| 4211 |
|
| 4212 |
|
| 4213 | static u32 tcp_newly_delivered(struct sock *sk, u32 prior_delivered,
|
| 4214 | u32 ecn_count, int flag)
|
| 4215 | {
|
| 4216 | const struct net *net = sock_net(sk);
|
| 4217 | struct tcp_sock *tp = tcp_sk(sk);
|
| 4218 | u32 delivered;
|
| 4219 |
|
| 4220 | delivered = tp->delivered - prior_delivered;
|
| 4221 | NET_ADD_STATS(net, LINUX_MIB_TCPDELIVERED, delivered);
|
| 4222 |
|
| 4223 | if (flag & FLAG_ECE) {
|
| 4224 | if (tcp_ecn_mode_rfc3168(tp))
|
| 4225 | ecn_count = delivered;
|
| 4226 | NET_ADD_STATS(net, LINUX_MIB_TCPDELIVEREDCE, ecn_count);
|
| 4227 | }
|
| 4228 |
|
| 4229 | return delivered;
|
| 4230 | }
|
| 4231 |
|
| 4232 | |
| 4233 | *
|
| 4234 | * If a DSACK is received that seems like it may have been due to reordering
|
| 4235 | * triggering fast recovery, increment reo_wnd by min_rtt/4 (upper bounded
|
| 4236 | * by srtt), since there is possibility that spurious retransmission was
|
| 4237 | * due to reordering delay longer than reo_wnd.
|
| 4238 | *
|
| 4239 | * Persist the current reo_wnd value for TCP_RACK_RECOVERY_THRESH (16)
|
| 4240 | * no. of successful recoveries (accounts for full DSACK-based loss
|
| 4241 | * recovery undo). After that, reset it to default (min_rtt/4).
|
| 4242 | *
|
| 4243 | * At max, reo_wnd is incremented only once per rtt. So that the new
|
| 4244 | * DSACK on which we are reacting, is due to the spurious retx (approx)
|
| 4245 | * after the reo_wnd has been updated last time.
|
| 4246 | *
|
| 4247 | * reo_wnd is tracked in terms of steps (of min_rtt/4), rather than
|
| 4248 | * absolute value to account for change in rtt.
|
| 4249 |
|
| 4250 | static void tcp_rack_update_reo_wnd(struct sock *sk, struct rate_sample *rs)
|
| 4251 | {
|
| 4252 | struct tcp_sock *tp = tcp_sk(sk);
|
| 4253 |
|
| 4254 | if ((READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_recovery) &
|
| 4255 | TCP_RACK_STATIC_REO_WND) ||
|
| 4256 | !rs->prior_delivered)
|
| 4257 | return;
|
| 4258 |
|
| 4259 |
|
| 4260 | if (before(rs->prior_delivered, tp->rack.last_delivered))
|
| 4261 | tp->rack.dsack_seen = 0;
|
| 4262 |
|
| 4263 |
|
| 4264 | if (tp->rack.dsack_seen) {
|
| 4265 | tp->rack.reo_wnd_steps = min_t(u32, 0xFF,
|
| 4266 | tp->rack.reo_wnd_steps + 1);
|
| 4267 | tp->rack.dsack_seen = 0;
|
| 4268 | tp->rack.last_delivered = tp->delivered;
|
| 4269 | tp->rack.reo_wnd_persist = TCP_RACK_RECOVERY_THRESH;
|
| 4270 | } else if (!tp->rack.reo_wnd_persist) {
|
| 4271 | tp->rack.reo_wnd_steps = 1;
|
| 4272 | }
|
| 4273 | }
|
| 4274 |
|
| 4275 |
|
| 4276 | static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
|
| 4277 | {
|
| 4278 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 4279 | struct tcp_sock *tp = tcp_sk(sk);
|
| 4280 | struct tcp_sacktag_state sack_state;
|
| 4281 | struct rate_sample rs = { .prior_delivered = 0 };
|
| 4282 | u32 prior_snd_una = tp->snd_una;
|
| 4283 | bool is_sack_reneg = tp->is_sack_reneg;
|
| 4284 | u32 ack_seq = TCP_SKB_CB(skb)->seq;
|
| 4285 | u32 ack = TCP_SKB_CB(skb)->ack_seq;
|
| 4286 | int num_dupack = 0;
|
| 4287 | int prior_packets = tp->packets_out;
|
| 4288 | u32 delivered = tp->delivered;
|
| 4289 | u32 lost = tp->lost;
|
| 4290 | int rexmit = REXMIT_NONE;
|
| 4291 | u32 ecn_count = 0;
|
| 4292 | u32 prior_fack;
|
| 4293 |
|
| 4294 | sack_state.first_sackt = 0;
|
| 4295 | sack_state.rate = &rs;
|
| 4296 | sack_state.sack_delivered = 0;
|
| 4297 | sack_state.delivered_bytes = 0;
|
| 4298 |
|
| 4299 |
|
| 4300 | prefetch(sk->tcp_rtx_queue.rb_node);
|
| 4301 |
|
| 4302 | |
| 4303 | * then we can probably ignore it.
|
| 4304 |
|
| 4305 | if (before(ack, prior_snd_una)) {
|
| 4306 | u32 max_window;
|
| 4307 |
|
| 4308 |
|
| 4309 | max_window = min_t(u64, tp->max_window, tp->bytes_acked);
|
| 4310 |
|
| 4311 | if (before(ack, prior_snd_una - max_window)) {
|
| 4312 | if (!(flag & FLAG_NO_CHALLENGE_ACK))
|
| 4313 | tcp_send_challenge_ack(sk, false);
|
| 4314 | return -SKB_DROP_REASON_TCP_TOO_OLD_ACK;
|
| 4315 | }
|
| 4316 | goto old_ack;
|
| 4317 | }
|
| 4318 |
|
| 4319 | |
| 4320 | * segment. RFC 793 Section 3.9 and RFC 5961 Section 5.2
|
| 4321 | * require us to send an ACK back in that case.
|
| 4322 |
|
| 4323 | if (after(ack, tp->snd_nxt)) {
|
| 4324 | if (!(flag & FLAG_NO_CHALLENGE_ACK))
|
| 4325 | tcp_send_challenge_ack(sk, false);
|
| 4326 | return -SKB_DROP_REASON_TCP_ACK_UNSENT_DATA;
|
| 4327 | }
|
| 4328 |
|
| 4329 | if (after(ack, prior_snd_una)) {
|
| 4330 | flag |= FLAG_SND_UNA_ADVANCED;
|
| 4331 | WRITE_ONCE(icsk->icsk_retransmits, 0);
|
| 4332 |
|
| 4333 | #if IS_ENABLED(CONFIG_TLS_DEVICE)
|
| 4334 | if (static_branch_unlikely(&clean_acked_data_enabled.key))
|
| 4335 | if (tp->tcp_clean_acked)
|
| 4336 | tp->tcp_clean_acked(sk, ack);
|
| 4337 | #endif
|
| 4338 | }
|
| 4339 |
|
| 4340 | prior_fack = tcp_is_sack(tp) ? tcp_highest_sack_seq(tp) : tp->snd_una;
|
| 4341 | rs.prior_in_flight = tcp_packets_in_flight(tp);
|
| 4342 |
|
| 4343 | |
| 4344 | * is in window.
|
| 4345 |
|
| 4346 | if (flag & FLAG_UPDATE_TS_RECENT)
|
| 4347 | flag |= tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
|
| 4348 |
|
| 4349 | if ((flag & (FLAG_SLOWPATH | FLAG_SND_UNA_ADVANCED)) ==
|
| 4350 | FLAG_SND_UNA_ADVANCED) {
|
| 4351 | |
| 4352 | * No more checks are required.
|
| 4353 | * Note, we use the fact that SND.UNA>=SND.WL2.
|
| 4354 |
|
| 4355 | tcp_update_wl(tp, ack_seq);
|
| 4356 | tcp_snd_una_update(tp, ack);
|
| 4357 | flag |= FLAG_WIN_UPDATE;
|
| 4358 |
|
| 4359 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPACKS);
|
| 4360 | } else {
|
| 4361 | if (ack_seq != TCP_SKB_CB(skb)->end_seq)
|
| 4362 | flag |= FLAG_DATA;
|
| 4363 | else
|
| 4364 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPPUREACKS);
|
| 4365 |
|
| 4366 | flag |= tcp_ack_update_window(sk, skb, ack, ack_seq);
|
| 4367 |
|
| 4368 | if (TCP_SKB_CB(skb)->sacked)
|
| 4369 | flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
|
| 4370 | &sack_state);
|
| 4371 |
|
| 4372 | if (tcp_ecn_rcv_ecn_echo(tp, tcp_hdr(skb)))
|
| 4373 | flag |= FLAG_ECE;
|
| 4374 |
|
| 4375 | if (sack_state.sack_delivered)
|
| 4376 | tcp_count_delivered(tp, sack_state.sack_delivered,
|
| 4377 | flag & FLAG_ECE);
|
| 4378 | }
|
| 4379 |
|
| 4380 | |
| 4381 | * "When the TCP data sender is ready to set the CWR bit after reducing
|
| 4382 | * the congestion window, it SHOULD set the CWR bit only on the first
|
| 4383 | * new data packet that it transmits."
|
| 4384 | * We accept CWR on pure ACKs to be more robust
|
| 4385 | * with widely-deployed TCP implementations that do this.
|
| 4386 |
|
| 4387 | tcp_ecn_accept_cwr(sk, skb);
|
| 4388 |
|
| 4389 | |
| 4390 | * log. Something worked...
|
| 4391 |
|
| 4392 | if (READ_ONCE(sk->sk_err_soft))
|
| 4393 | WRITE_ONCE(sk->sk_err_soft, 0);
|
| 4394 | WRITE_ONCE(icsk->icsk_probes_out, 0);
|
| 4395 | tp->rcv_tstamp = tcp_jiffies32;
|
| 4396 | if (!prior_packets)
|
| 4397 | goto no_queue;
|
| 4398 |
|
| 4399 |
|
| 4400 | flag |= tcp_clean_rtx_queue(sk, skb, prior_fack, prior_snd_una,
|
| 4401 | &sack_state, flag & FLAG_ECE);
|
| 4402 |
|
| 4403 | tcp_rack_update_reo_wnd(sk, &rs);
|
| 4404 |
|
| 4405 | if (tcp_ecn_mode_accecn(tp))
|
| 4406 | ecn_count = tcp_accecn_process(sk, skb,
|
| 4407 | tp->delivered - delivered,
|
| 4408 | sack_state.delivered_bytes,
|
| 4409 | &flag);
|
| 4410 |
|
| 4411 | tcp_in_ack_event(sk, flag);
|
| 4412 |
|
| 4413 | if (unlikely(tp->tlp_high_seq))
|
| 4414 | tcp_process_tlp_ack(sk, ack, flag);
|
| 4415 |
|
| 4416 | if (tcp_ack_is_dubious(sk, flag)) {
|
| 4417 | if (!(flag & (FLAG_SND_UNA_ADVANCED |
|
| 4418 | FLAG_NOT_DUP | FLAG_DSACKING_ACK))) {
|
| 4419 | num_dupack = 1;
|
| 4420 |
|
| 4421 | if (!(flag & FLAG_DATA))
|
| 4422 | num_dupack = max_t(u16, 1, skb_shinfo(skb)->gso_segs);
|
| 4423 | }
|
| 4424 | tcp_fastretrans_alert(sk, prior_snd_una, num_dupack, &flag,
|
| 4425 | &rexmit);
|
| 4426 | }
|
| 4427 |
|
| 4428 |
|
| 4429 | if (flag & FLAG_SET_XMIT_TIMER)
|
| 4430 | tcp_set_xmit_timer(sk);
|
| 4431 |
|
| 4432 | if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP))
|
| 4433 | sk_dst_confirm(sk);
|
| 4434 |
|
| 4435 | delivered = tcp_newly_delivered(sk, delivered, ecn_count, flag);
|
| 4436 |
|
| 4437 | lost = tp->lost - lost;
|
| 4438 | rs.is_ack_delayed = !!(flag & FLAG_ACK_MAYBE_DELAYED);
|
| 4439 | tcp_rate_gen(sk, delivered, lost, is_sack_reneg, sack_state.rate);
|
| 4440 | tcp_cong_control(sk, ack, delivered, flag, sack_state.rate);
|
| 4441 | tcp_xmit_recovery(sk, rexmit);
|
| 4442 | return 1;
|
| 4443 |
|
| 4444 | no_queue:
|
| 4445 | if (tcp_ecn_mode_accecn(tp))
|
| 4446 | ecn_count = tcp_accecn_process(sk, skb,
|
| 4447 | tp->delivered - delivered,
|
| 4448 | sack_state.delivered_bytes,
|
| 4449 | &flag);
|
| 4450 | tcp_in_ack_event(sk, flag);
|
| 4451 |
|
| 4452 | if (flag & FLAG_DSACKING_ACK) {
|
| 4453 | tcp_fastretrans_alert(sk, prior_snd_una, num_dupack, &flag,
|
| 4454 | &rexmit);
|
| 4455 | tcp_newly_delivered(sk, delivered, ecn_count, flag);
|
| 4456 | }
|
| 4457 | |
| 4458 | * being used to time the probes, and is probably far higher than
|
| 4459 | * it needs to be for normal retransmission.
|
| 4460 |
|
| 4461 | tcp_ack_probe(sk);
|
| 4462 |
|
| 4463 | if (unlikely(tp->tlp_high_seq))
|
| 4464 | tcp_process_tlp_ack(sk, ack, flag);
|
| 4465 | return 1;
|
| 4466 |
|
| 4467 | old_ack:
|
| 4468 | |
| 4469 | * processing can trigger a retransmission.
|
| 4470 |
|
| 4471 | if (flag & FLAG_UPDATE_TS_RECENT)
|
| 4472 | tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
|
| 4473 |
|
| 4474 | |
| 4475 | * If data was DSACKed, see if we can undo a cwnd reduction.
|
| 4476 |
|
| 4477 | if (TCP_SKB_CB(skb)->sacked) {
|
| 4478 | flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una,
|
| 4479 | &sack_state);
|
| 4480 | tcp_fastretrans_alert(sk, prior_snd_una, num_dupack, &flag,
|
| 4481 | &rexmit);
|
| 4482 | tcp_newly_delivered(sk, delivered, ecn_count, flag);
|
| 4483 | tcp_xmit_recovery(sk, rexmit);
|
| 4484 | }
|
| 4485 |
|
| 4486 | return 0;
|
| 4487 | }
|
| 4488 |
|
| 4489 | static void tcp_parse_fastopen_option(int len, const unsigned char *cookie,
|
| 4490 | bool syn, struct tcp_fastopen_cookie *foc,
|
| 4491 | bool exp_opt)
|
| 4492 | {
|
| 4493 |
|
| 4494 | if (!foc || !syn || len < 0 || (len & 1))
|
| 4495 | return;
|
| 4496 |
|
| 4497 | if (len >= TCP_FASTOPEN_COOKIE_MIN &&
|
| 4498 | len <= TCP_FASTOPEN_COOKIE_MAX)
|
| 4499 | memcpy(foc->val, cookie, len);
|
| 4500 | else if (len != 0)
|
| 4501 | len = -1;
|
| 4502 | foc->len = len;
|
| 4503 | foc->exp = exp_opt;
|
| 4504 | }
|
| 4505 |
|
| 4506 | static bool smc_parse_options(const struct tcphdr *th,
|
| 4507 | struct tcp_options_received *opt_rx,
|
| 4508 | const unsigned char *ptr,
|
| 4509 | int opsize)
|
| 4510 | {
|
| 4511 | #if IS_ENABLED(CONFIG_SMC)
|
| 4512 | if (static_branch_unlikely(&tcp_have_smc)) {
|
| 4513 | if (th->syn && !(opsize & 1) &&
|
| 4514 | opsize >= TCPOLEN_EXP_SMC_BASE &&
|
| 4515 | get_unaligned_be32(ptr) == TCPOPT_SMC_MAGIC) {
|
| 4516 | opt_rx->smc_ok = 1;
|
| 4517 | return true;
|
| 4518 | }
|
| 4519 | }
|
| 4520 | #endif
|
| 4521 | return false;
|
| 4522 | }
|
| 4523 |
|
| 4524 | |
| 4525 | * value on success.
|
| 4526 |
|
| 4527 | u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss)
|
| 4528 | {
|
| 4529 | const unsigned char *ptr = (const unsigned char *)(th + 1);
|
| 4530 | int length = (th->doff * 4) - sizeof(struct tcphdr);
|
| 4531 | u16 mss = 0;
|
| 4532 |
|
| 4533 | while (length > 0) {
|
| 4534 | int opcode = *ptr++;
|
| 4535 | int opsize;
|
| 4536 |
|
| 4537 | switch (opcode) {
|
| 4538 | case TCPOPT_EOL:
|
| 4539 | return mss;
|
| 4540 | case TCPOPT_NOP:
|
| 4541 | length--;
|
| 4542 | continue;
|
| 4543 | default:
|
| 4544 | if (length < 2)
|
| 4545 | return mss;
|
| 4546 | opsize = *ptr++;
|
| 4547 | if (opsize < 2)
|
| 4548 | return mss;
|
| 4549 | if (opsize > length)
|
| 4550 | return mss;
|
| 4551 | if (opcode == TCPOPT_MSS && opsize == TCPOLEN_MSS) {
|
| 4552 | u16 in_mss = get_unaligned_be16(ptr);
|
| 4553 |
|
| 4554 | if (in_mss) {
|
| 4555 | if (user_mss && user_mss < in_mss)
|
| 4556 | in_mss = user_mss;
|
| 4557 | mss = in_mss;
|
| 4558 | }
|
| 4559 | }
|
| 4560 | ptr += opsize - 2;
|
| 4561 | length -= opsize;
|
| 4562 | }
|
| 4563 | }
|
| 4564 | return mss;
|
| 4565 | }
|
| 4566 |
|
| 4567 | |
| 4568 | * But, this can also be called on packets in the established flow when
|
| 4569 | * the fast version below fails.
|
| 4570 |
|
| 4571 | void tcp_parse_options(const struct net *net,
|
| 4572 | const struct sk_buff *skb,
|
| 4573 | struct tcp_options_received *opt_rx, int estab,
|
| 4574 | struct tcp_fastopen_cookie *foc)
|
| 4575 | {
|
| 4576 | const unsigned char *ptr;
|
| 4577 | const struct tcphdr *th = tcp_hdr(skb);
|
| 4578 | int length = (th->doff * 4) - sizeof(struct tcphdr);
|
| 4579 |
|
| 4580 | ptr = (const unsigned char *)(th + 1);
|
| 4581 | opt_rx->saw_tstamp = 0;
|
| 4582 | opt_rx->accecn = 0;
|
| 4583 | opt_rx->saw_unknown = 0;
|
| 4584 |
|
| 4585 | while (length > 0) {
|
| 4586 | int opcode = *ptr++;
|
| 4587 | int opsize;
|
| 4588 |
|
| 4589 | switch (opcode) {
|
| 4590 | case TCPOPT_EOL:
|
| 4591 | return;
|
| 4592 | case TCPOPT_NOP:
|
| 4593 | length--;
|
| 4594 | continue;
|
| 4595 | default:
|
| 4596 | if (length < 2)
|
| 4597 | return;
|
| 4598 | opsize = *ptr++;
|
| 4599 | if (opsize < 2)
|
| 4600 | return;
|
| 4601 | if (opsize > length)
|
| 4602 | return;
|
| 4603 | switch (opcode) {
|
| 4604 | case TCPOPT_MSS:
|
| 4605 | if (opsize == TCPOLEN_MSS && th->syn && !estab) {
|
| 4606 | u16 in_mss = get_unaligned_be16(ptr);
|
| 4607 | if (in_mss) {
|
| 4608 | if (opt_rx->user_mss &&
|
| 4609 | opt_rx->user_mss < in_mss)
|
| 4610 | in_mss = opt_rx->user_mss;
|
| 4611 | opt_rx->mss_clamp = in_mss;
|
| 4612 | }
|
| 4613 | }
|
| 4614 | break;
|
| 4615 | case TCPOPT_WINDOW:
|
| 4616 | if (opsize == TCPOLEN_WINDOW && th->syn &&
|
| 4617 | !estab && READ_ONCE(net->ipv4.sysctl_tcp_window_scaling)) {
|
| 4618 | __u8 snd_wscale = *(__u8 *)ptr;
|
| 4619 | opt_rx->wscale_ok = 1;
|
| 4620 | if (snd_wscale > TCP_MAX_WSCALE) {
|
| 4621 | net_info_ratelimited("%s: Illegal window scaling value %d > %u received\n",
|
| 4622 | __func__,
|
| 4623 | snd_wscale,
|
| 4624 | TCP_MAX_WSCALE);
|
| 4625 | snd_wscale = TCP_MAX_WSCALE;
|
| 4626 | }
|
| 4627 | opt_rx->snd_wscale = snd_wscale;
|
| 4628 | }
|
| 4629 | break;
|
| 4630 | case TCPOPT_TIMESTAMP:
|
| 4631 | if ((opsize == TCPOLEN_TIMESTAMP) &&
|
| 4632 | ((estab && opt_rx->tstamp_ok) ||
|
| 4633 | (!estab && READ_ONCE(net->ipv4.sysctl_tcp_timestamps)))) {
|
| 4634 | opt_rx->saw_tstamp = 1;
|
| 4635 | opt_rx->rcv_tsval = get_unaligned_be32(ptr);
|
| 4636 | opt_rx->rcv_tsecr = get_unaligned_be32(ptr + 4);
|
| 4637 | }
|
| 4638 | break;
|
| 4639 | case TCPOPT_SACK_PERM:
|
| 4640 | if (opsize == TCPOLEN_SACK_PERM && th->syn &&
|
| 4641 | !estab && READ_ONCE(net->ipv4.sysctl_tcp_sack)) {
|
| 4642 | opt_rx->sack_ok = TCP_SACK_SEEN;
|
| 4643 | tcp_sack_reset(opt_rx);
|
| 4644 | }
|
| 4645 | break;
|
| 4646 |
|
| 4647 | case TCPOPT_SACK:
|
| 4648 | if ((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) &&
|
| 4649 | !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) &&
|
| 4650 | opt_rx->sack_ok) {
|
| 4651 | TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th;
|
| 4652 | }
|
| 4653 | break;
|
| 4654 | #ifdef CONFIG_TCP_MD5SIG
|
| 4655 | case TCPOPT_MD5SIG:
|
| 4656 | |
| 4657 | * checked (see tcp_v{4,6}_rcv()).
|
| 4658 |
|
| 4659 | break;
|
| 4660 | #endif
|
| 4661 | #ifdef CONFIG_TCP_AO
|
| 4662 | case TCPOPT_AO:
|
| 4663 | |
| 4664 | * (see tcp_inbound_ao_hash()).
|
| 4665 |
|
| 4666 | break;
|
| 4667 | #endif
|
| 4668 | case TCPOPT_FASTOPEN:
|
| 4669 | tcp_parse_fastopen_option(
|
| 4670 | opsize - TCPOLEN_FASTOPEN_BASE,
|
| 4671 | ptr, th->syn, foc, false);
|
| 4672 | break;
|
| 4673 |
|
| 4674 | case TCPOPT_ACCECN0:
|
| 4675 | case TCPOPT_ACCECN1:
|
| 4676 |
|
| 4677 | opt_rx->accecn = (ptr - 2) - (__u8 *)th;
|
| 4678 | break;
|
| 4679 |
|
| 4680 | case TCPOPT_EXP:
|
| 4681 | |
| 4682 | * 16 bits magic number.
|
| 4683 |
|
| 4684 | if (opsize >= TCPOLEN_EXP_FASTOPEN_BASE &&
|
| 4685 | get_unaligned_be16(ptr) ==
|
| 4686 | TCPOPT_FASTOPEN_MAGIC) {
|
| 4687 | tcp_parse_fastopen_option(opsize -
|
| 4688 | TCPOLEN_EXP_FASTOPEN_BASE,
|
| 4689 | ptr + 2, th->syn, foc, true);
|
| 4690 | break;
|
| 4691 | }
|
| 4692 |
|
| 4693 | if (smc_parse_options(th, opt_rx, ptr, opsize))
|
| 4694 | break;
|
| 4695 |
|
| 4696 | opt_rx->saw_unknown = 1;
|
| 4697 | break;
|
| 4698 |
|
| 4699 | default:
|
| 4700 | opt_rx->saw_unknown = 1;
|
| 4701 | }
|
| 4702 | ptr += opsize-2;
|
| 4703 | length -= opsize;
|
| 4704 | }
|
| 4705 | }
|
| 4706 | }
|
| 4707 | EXPORT_SYMBOL(tcp_parse_options);
|
| 4708 |
|
| 4709 | static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr *th)
|
| 4710 | {
|
| 4711 | const __be32 *ptr = (const __be32 *)(th + 1);
|
| 4712 |
|
| 4713 | if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
|
| 4714 | | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
|
| 4715 | tp->rx_opt.saw_tstamp = 1;
|
| 4716 | ++ptr;
|
| 4717 | tp->rx_opt.rcv_tsval = ntohl(*ptr);
|
| 4718 | ++ptr;
|
| 4719 | if (*ptr)
|
| 4720 | tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset;
|
| 4721 | else
|
| 4722 | tp->rx_opt.rcv_tsecr = 0;
|
| 4723 | return true;
|
| 4724 | }
|
| 4725 | return false;
|
| 4726 | }
|
| 4727 |
|
| 4728 | |
| 4729 | * If it is wrong it falls back on tcp_parse_options().
|
| 4730 |
|
| 4731 | static bool tcp_fast_parse_options(const struct net *net,
|
| 4732 | const struct sk_buff *skb,
|
| 4733 | const struct tcphdr *th, struct tcp_sock *tp)
|
| 4734 | {
|
| 4735 | |
| 4736 | * values. Because equality is used, short doff can be ignored here.
|
| 4737 |
|
| 4738 | if (th->doff == (sizeof(*th) / 4)) {
|
| 4739 | tp->rx_opt.saw_tstamp = 0;
|
| 4740 | tp->rx_opt.accecn = 0;
|
| 4741 | return false;
|
| 4742 | } else if (tp->rx_opt.tstamp_ok &&
|
| 4743 | th->doff == ((sizeof(*th) + TCPOLEN_TSTAMP_ALIGNED) / 4)) {
|
| 4744 | if (tcp_parse_aligned_timestamp(tp, th)) {
|
| 4745 | tp->rx_opt.accecn = 0;
|
| 4746 | return true;
|
| 4747 | }
|
| 4748 | }
|
| 4749 |
|
| 4750 | tcp_parse_options(net, skb, &tp->rx_opt, 1, NULL);
|
| 4751 | if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
|
| 4752 | tp->rx_opt.rcv_tsecr -= tp->tsoffset;
|
| 4753 |
|
| 4754 | return true;
|
| 4755 | }
|
| 4756 |
|
| 4757 | |
| 4758 | *
|
| 4759 | * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
|
| 4760 | * it can pass through stack. So, the following predicate verifies that
|
| 4761 | * this segment is not used for anything but congestion avoidance or
|
| 4762 | * fast retransmit. Moreover, we even are able to eliminate most of such
|
| 4763 | * second order effects, if we apply some small "replay" window (~RTO)
|
| 4764 | * to timestamp space.
|
| 4765 | *
|
| 4766 | * All these measures still do not guarantee that we reject wrapped ACKs
|
| 4767 | * on networks with high bandwidth, when sequence space is recycled fastly,
|
| 4768 | * but it guarantees that such events will be very rare and do not affect
|
| 4769 | * connection seriously. This doesn't look nice, but alas, PAWS is really
|
| 4770 | * buggy extension.
|
| 4771 | *
|
| 4772 | * [ Later note. Even worse! It is buggy for segments _with_ data. RFC
|
| 4773 | * states that events when retransmit arrives after original data are rare.
|
| 4774 | * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is
|
| 4775 | * the biggest problem on large power networks even with minor reordering.
|
| 4776 | * OK, let's give it small replay window. If peer clock is even 1hz, it is safe
|
| 4777 | * up to bandwidth of 18Gigabit/sec. 8) ]
|
| 4778 |
|
| 4779 |
|
| 4780 | |
| 4781 | * a replay window (based on our current RTO estimation).
|
| 4782 |
|
| 4783 | static u32 tcp_tsval_replay(const struct sock *sk)
|
| 4784 | {
|
| 4785 | |
| 4786 | * then expect the remote peer to use the same resolution.
|
| 4787 |
|
| 4788 | if (tcp_sk(sk)->tcp_usec_ts)
|
| 4789 | return inet_csk(sk)->icsk_rto * (USEC_PER_SEC / HZ);
|
| 4790 |
|
| 4791 | |
| 4792 | * We know that some OS (including old linux) can use 1200 Hz.
|
| 4793 |
|
| 4794 | return inet_csk(sk)->icsk_rto * 1200 / HZ;
|
| 4795 | }
|
| 4796 |
|
| 4797 | static enum skb_drop_reason tcp_disordered_ack_check(const struct sock *sk,
|
| 4798 | const struct sk_buff *skb)
|
| 4799 | {
|
| 4800 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 4801 | const struct tcphdr *th = tcp_hdr(skb);
|
| 4802 | SKB_DR_INIT(reason, TCP_RFC7323_PAWS);
|
| 4803 | u32 ack = TCP_SKB_CB(skb)->ack_seq;
|
| 4804 | u32 seq = TCP_SKB_CB(skb)->seq;
|
| 4805 |
|
| 4806 |
|
| 4807 | if (!th->ack || seq != TCP_SKB_CB(skb)->end_seq)
|
| 4808 | return reason;
|
| 4809 |
|
| 4810 |
|
| 4811 | if (seq != tp->rcv_nxt)
|
| 4812 | return before(seq, tp->rcv_nxt) ?
|
| 4813 | SKB_DROP_REASON_TCP_RFC7323_PAWS_ACK :
|
| 4814 | reason;
|
| 4815 |
|
| 4816 |
|
| 4817 | if (ack != tp->snd_una)
|
| 4818 | return reason;
|
| 4819 |
|
| 4820 |
|
| 4821 | if (tcp_may_update_window(tp, ack, seq, ntohs(th->window) <<
|
| 4822 | tp->rx_opt.snd_wscale))
|
| 4823 | return reason;
|
| 4824 |
|
| 4825 |
|
| 4826 | if ((s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) >
|
| 4827 | tcp_tsval_replay(sk))
|
| 4828 | return reason;
|
| 4829 |
|
| 4830 | return 0;
|
| 4831 | }
|
| 4832 |
|
| 4833 | |
| 4834 | *
|
| 4835 | * Segment controls are considered valid, if the segment
|
| 4836 | * fits to the window after truncation to the window. Acceptability
|
| 4837 | * of data (and SYN, FIN, of course) is checked separately.
|
| 4838 | * See tcp_data_queue(), for example.
|
| 4839 | *
|
| 4840 | * Also, controls (RST is main one) are accepted using RCV.WUP instead
|
| 4841 | * of RCV.NXT. Peer still did not advance his SND.UNA when we
|
| 4842 | * delayed ACK, so that hisSND.UNA<=ourRCV.WUP.
|
| 4843 | * (borrowed from freebsd)
|
| 4844 |
|
| 4845 |
|
| 4846 | static enum skb_drop_reason tcp_sequence(const struct sock *sk,
|
| 4847 | u32 seq, u32 end_seq,
|
| 4848 | const struct tcphdr *th)
|
| 4849 | {
|
| 4850 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 4851 | u32 seq_limit;
|
| 4852 |
|
| 4853 | if (before(end_seq, tp->rcv_wup))
|
| 4854 | return SKB_DROP_REASON_TCP_OLD_SEQUENCE;
|
| 4855 |
|
| 4856 | seq_limit = tp->rcv_nxt + tcp_max_receive_window(tp);
|
| 4857 | if (unlikely(after(end_seq, seq_limit))) {
|
| 4858 | |
| 4859 | * FIN to extend beyond the window and check it in detail later.
|
| 4860 |
|
| 4861 | if (!after(end_seq - th->fin, seq_limit))
|
| 4862 | return SKB_NOT_DROPPED_YET;
|
| 4863 |
|
| 4864 | if (after(seq, seq_limit))
|
| 4865 | return SKB_DROP_REASON_TCP_INVALID_SEQUENCE;
|
| 4866 |
|
| 4867 |
|
| 4868 | if (skb_queue_len(&sk->sk_receive_queue))
|
| 4869 | return SKB_DROP_REASON_TCP_INVALID_END_SEQUENCE;
|
| 4870 | }
|
| 4871 |
|
| 4872 | return SKB_NOT_DROPPED_YET;
|
| 4873 | }
|
| 4874 |
|
| 4875 |
|
| 4876 | void tcp_done_with_error(struct sock *sk, int err)
|
| 4877 | {
|
| 4878 |
|
| 4879 | WRITE_ONCE(sk->sk_err, err);
|
| 4880 | smp_wmb();
|
| 4881 |
|
| 4882 | tcp_write_queue_purge(sk);
|
| 4883 | tcp_done(sk);
|
| 4884 |
|
| 4885 | if (!sock_flag(sk, SOCK_DEAD))
|
| 4886 | sk_error_report(sk);
|
| 4887 | }
|
| 4888 |
|
| 4889 |
|
| 4890 | void tcp_reset(struct sock *sk, struct sk_buff *skb)
|
| 4891 | {
|
| 4892 | int err;
|
| 4893 |
|
| 4894 | trace_tcp_receive_reset(sk);
|
| 4895 |
|
| 4896 | |
| 4897 | * so just ignore the return value of mptcp_incoming_options().
|
| 4898 |
|
| 4899 | if (sk_is_mptcp(sk))
|
| 4900 | mptcp_incoming_options(sk, skb);
|
| 4901 |
|
| 4902 |
|
| 4903 | switch (sk->sk_state) {
|
| 4904 | case TCP_SYN_SENT:
|
| 4905 | err = ECONNREFUSED;
|
| 4906 | break;
|
| 4907 | case TCP_CLOSE_WAIT:
|
| 4908 | err = EPIPE;
|
| 4909 | break;
|
| 4910 | case TCP_CLOSE:
|
| 4911 | return;
|
| 4912 | default:
|
| 4913 | err = ECONNRESET;
|
| 4914 | }
|
| 4915 | tcp_done_with_error(sk, err);
|
| 4916 | }
|
| 4917 |
|
| 4918 | |
| 4919 | * Process the FIN bit. This now behaves as it is supposed to work
|
| 4920 | * and the FIN takes effect when it is validly part of sequence
|
| 4921 | * space. Not before when we get holes.
|
| 4922 | *
|
| 4923 | * If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
|
| 4924 | * (and thence onto LAST-ACK and finally, CLOSE, we never enter
|
| 4925 | * TIME-WAIT)
|
| 4926 | *
|
| 4927 | * If we are in FINWAIT-1, a received FIN indicates simultaneous
|
| 4928 | * close and we go into CLOSING (and later onto TIME-WAIT)
|
| 4929 | *
|
| 4930 | * If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
|
| 4931 |
|
| 4932 | void tcp_fin(struct sock *sk)
|
| 4933 | {
|
| 4934 | struct tcp_sock *tp = tcp_sk(sk);
|
| 4935 |
|
| 4936 | inet_csk_schedule_ack(sk);
|
| 4937 |
|
| 4938 | WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | RCV_SHUTDOWN);
|
| 4939 | sock_set_flag(sk, SOCK_DONE);
|
| 4940 |
|
| 4941 | switch (sk->sk_state) {
|
| 4942 | case TCP_SYN_RECV:
|
| 4943 | case TCP_ESTABLISHED:
|
| 4944 |
|
| 4945 | tcp_set_state(sk, TCP_CLOSE_WAIT);
|
| 4946 | inet_csk_enter_pingpong_mode(sk);
|
| 4947 | break;
|
| 4948 |
|
| 4949 | case TCP_CLOSE_WAIT:
|
| 4950 | case TCP_CLOSING:
|
| 4951 | |
| 4952 | * nothing.
|
| 4953 |
|
| 4954 | break;
|
| 4955 | case TCP_LAST_ACK:
|
| 4956 |
|
| 4957 | break;
|
| 4958 |
|
| 4959 | case TCP_FIN_WAIT1:
|
| 4960 | |
| 4961 | * happens, we must ack the received FIN and
|
| 4962 | * enter the CLOSING state.
|
| 4963 |
|
| 4964 | tcp_send_ack(sk);
|
| 4965 | tcp_set_state(sk, TCP_CLOSING);
|
| 4966 | break;
|
| 4967 | case TCP_FIN_WAIT2:
|
| 4968 |
|
| 4969 | tcp_send_ack(sk);
|
| 4970 | tcp_time_wait(sk, TCP_TIME_WAIT, 0);
|
| 4971 | break;
|
| 4972 | default:
|
| 4973 | |
| 4974 | * cases we should never reach this piece of code.
|
| 4975 |
|
| 4976 | pr_err("%s: Impossible, sk->sk_state=%d\n",
|
| 4977 | __func__, sk->sk_state);
|
| 4978 | break;
|
| 4979 | }
|
| 4980 |
|
| 4981 | |
| 4982 | * Probably, we should reset in this case. For now drop them.
|
| 4983 |
|
| 4984 | skb_rbtree_purge(&tp->out_of_order_queue);
|
| 4985 | if (tcp_is_sack(tp))
|
| 4986 | tcp_sack_reset(&tp->rx_opt);
|
| 4987 |
|
| 4988 | if (!sock_flag(sk, SOCK_DEAD)) {
|
| 4989 | sk->sk_state_change(sk);
|
| 4990 |
|
| 4991 |
|
| 4992 | if (sk->sk_shutdown == SHUTDOWN_MASK ||
|
| 4993 | sk->sk_state == TCP_CLOSE)
|
| 4994 | sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
|
| 4995 | else
|
| 4996 | sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
|
| 4997 | }
|
| 4998 | }
|
| 4999 |
|
| 5000 | static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,
|
| 5001 | u32 end_seq)
|
| 5002 | {
|
| 5003 | if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) {
|
| 5004 | if (before(seq, sp->start_seq))
|
| 5005 | sp->start_seq = seq;
|
| 5006 | if (after(end_seq, sp->end_seq))
|
| 5007 | sp->end_seq = end_seq;
|
| 5008 | return true;
|
| 5009 | }
|
| 5010 | return false;
|
| 5011 | }
|
| 5012 |
|
| 5013 | static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq)
|
| 5014 | {
|
| 5015 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5016 |
|
| 5017 | if (tcp_is_sack(tp) && READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_dsack)) {
|
| 5018 | int mib_idx;
|
| 5019 |
|
| 5020 | if (before(seq, tp->rcv_nxt))
|
| 5021 | mib_idx = LINUX_MIB_TCPDSACKOLDSENT;
|
| 5022 | else
|
| 5023 | mib_idx = LINUX_MIB_TCPDSACKOFOSENT;
|
| 5024 |
|
| 5025 | NET_INC_STATS(sock_net(sk), mib_idx);
|
| 5026 |
|
| 5027 | tp->rx_opt.dsack = 1;
|
| 5028 | tp->duplicate_sack[0].start_seq = seq;
|
| 5029 | tp->duplicate_sack[0].end_seq = end_seq;
|
| 5030 | }
|
| 5031 | }
|
| 5032 |
|
| 5033 | static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq)
|
| 5034 | {
|
| 5035 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5036 |
|
| 5037 | if (!tp->rx_opt.dsack)
|
| 5038 | tcp_dsack_set(sk, seq, end_seq);
|
| 5039 | else
|
| 5040 | tcp_sack_extend(tp->duplicate_sack, seq, end_seq);
|
| 5041 | }
|
| 5042 |
|
| 5043 | static void tcp_rcv_spurious_retrans(struct sock *sk,
|
| 5044 | const struct sk_buff *skb)
|
| 5045 | {
|
| 5046 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5047 |
|
| 5048 | |
| 5049 | * timeout and spuriously retransmit the same segment repeatedly.
|
| 5050 | * If it seems our ACKs are not reaching the other side,
|
| 5051 | * based on receiving a duplicate data segment with new flowlabel
|
| 5052 | * (suggesting the sender suffered an RTO), and we are not already
|
| 5053 | * repathing due to our own RTO, then rehash the socket to repath our
|
| 5054 | * packets.
|
| 5055 |
|
| 5056 | #if IS_ENABLED(CONFIG_IPV6)
|
| 5057 | if (inet_csk(sk)->icsk_ca_state != TCP_CA_Loss &&
|
| 5058 | skb->protocol == htons(ETH_P_IPV6) &&
|
| 5059 | (tcp_sk(sk)->inet_conn.icsk_ack.lrcv_flowlabel !=
|
| 5060 | ntohl(ip6_flowlabel(ipv6_hdr(skb)))) &&
|
| 5061 | __sk_rethink_txhash_reset_dst(sk)) {
|
| 5062 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPDUPLICATEDATAREHASH);
|
| 5063 | }
|
| 5064 |
|
| 5065 |
|
| 5066 | tcp_save_lrcv_flowlabel(sk, skb);
|
| 5067 | #endif
|
| 5068 | |
| 5069 | * AccECN option was lost after the second retransmision, and then
|
| 5070 | * stop sending AccECN option in all subsequent ACKs.
|
| 5071 |
|
| 5072 | if (tcp_ecn_mode_accecn(tp) &&
|
| 5073 | tp->accecn_opt_sent_w_dsack &&
|
| 5074 | TCP_SKB_CB(skb)->seq == tp->duplicate_sack[0].start_seq)
|
| 5075 | tcp_accecn_fail_mode_set(tp, TCP_ACCECN_OPT_FAIL_SEND);
|
| 5076 | }
|
| 5077 |
|
| 5078 | static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb)
|
| 5079 | {
|
| 5080 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5081 |
|
| 5082 | if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
|
| 5083 | before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
|
| 5084 | NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
|
| 5085 | tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
|
| 5086 |
|
| 5087 | if (tcp_is_sack(tp) && READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_dsack)) {
|
| 5088 | u32 end_seq = TCP_SKB_CB(skb)->end_seq;
|
| 5089 |
|
| 5090 | tcp_rcv_spurious_retrans(sk, skb);
|
| 5091 | if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))
|
| 5092 | end_seq = tp->rcv_nxt;
|
| 5093 | tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, end_seq);
|
| 5094 | }
|
| 5095 | }
|
| 5096 |
|
| 5097 | tcp_send_ack(sk);
|
| 5098 | }
|
| 5099 |
|
| 5100 | |
| 5101 | * in-order packets close up the sequence space.
|
| 5102 |
|
| 5103 | static void tcp_sack_maybe_coalesce(struct tcp_sock *tp)
|
| 5104 | {
|
| 5105 | int this_sack;
|
| 5106 | struct tcp_sack_block *sp = &tp->selective_acks[0];
|
| 5107 | struct tcp_sack_block *swalk = sp + 1;
|
| 5108 |
|
| 5109 | |
| 5110 | * or hits the sequence space of other SACK blocks, if so coalesce.
|
| 5111 |
|
| 5112 | for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;) {
|
| 5113 | if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) {
|
| 5114 | int i;
|
| 5115 |
|
| 5116 | |
| 5117 | * Decrease num_sacks.
|
| 5118 |
|
| 5119 | tp->rx_opt.num_sacks--;
|
| 5120 | for (i = this_sack; i < tp->rx_opt.num_sacks; i++)
|
| 5121 | sp[i] = sp[i + 1];
|
| 5122 | continue;
|
| 5123 | }
|
| 5124 | this_sack++;
|
| 5125 | swalk++;
|
| 5126 | }
|
| 5127 | }
|
| 5128 |
|
| 5129 | void tcp_sack_compress_send_ack(struct sock *sk)
|
| 5130 | {
|
| 5131 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5132 |
|
| 5133 | if (!tp->compressed_ack)
|
| 5134 | return;
|
| 5135 |
|
| 5136 | if (hrtimer_try_to_cancel(&tp->compressed_ack_timer) == 1)
|
| 5137 | __sock_put(sk);
|
| 5138 |
|
| 5139 | |
| 5140 | * substract one from tp->compressed_ack to keep
|
| 5141 | * LINUX_MIB_TCPACKCOMPRESSED accurate.
|
| 5142 |
|
| 5143 | NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPACKCOMPRESSED,
|
| 5144 | tp->compressed_ack - 1);
|
| 5145 |
|
| 5146 | tp->compressed_ack = 0;
|
| 5147 | tcp_send_ack(sk);
|
| 5148 | }
|
| 5149 |
|
| 5150 | |
| 5151 | * The max is 4, but this becomes 3 if TCP timestamps are there.
|
| 5152 | * Given that SACK packets might be lost, be conservative and use 2.
|
| 5153 |
|
| 5154 | #define TCP_SACK_BLOCKS_EXPECTED 2
|
| 5155 |
|
| 5156 | static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq)
|
| 5157 | {
|
| 5158 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5159 | struct tcp_sack_block *sp = &tp->selective_acks[0];
|
| 5160 | int cur_sacks = tp->rx_opt.num_sacks;
|
| 5161 | int this_sack;
|
| 5162 |
|
| 5163 | if (!cur_sacks)
|
| 5164 | goto new_sack;
|
| 5165 |
|
| 5166 | for (this_sack = 0; this_sack < cur_sacks; this_sack++, sp++) {
|
| 5167 | if (tcp_sack_extend(sp, seq, end_seq)) {
|
| 5168 | if (this_sack >= TCP_SACK_BLOCKS_EXPECTED)
|
| 5169 | tcp_sack_compress_send_ack(sk);
|
| 5170 |
|
| 5171 | for (; this_sack > 0; this_sack--, sp--)
|
| 5172 | swap(*sp, *(sp - 1));
|
| 5173 | if (cur_sacks > 1)
|
| 5174 | tcp_sack_maybe_coalesce(tp);
|
| 5175 | return;
|
| 5176 | }
|
| 5177 | }
|
| 5178 |
|
| 5179 | if (this_sack >= TCP_SACK_BLOCKS_EXPECTED)
|
| 5180 | tcp_sack_compress_send_ack(sk);
|
| 5181 |
|
| 5182 | |
| 5183 | * put it at the front, and shift everyone else down. We
|
| 5184 | * always know there is at least one SACK present already here.
|
| 5185 | *
|
| 5186 | * If the sack array is full, forget about the last one.
|
| 5187 |
|
| 5188 | if (this_sack >= TCP_NUM_SACKS) {
|
| 5189 | this_sack--;
|
| 5190 | tp->rx_opt.num_sacks--;
|
| 5191 | sp--;
|
| 5192 | }
|
| 5193 | for (; this_sack > 0; this_sack--, sp--)
|
| 5194 | *sp = *(sp - 1);
|
| 5195 |
|
| 5196 | new_sack:
|
| 5197 |
|
| 5198 | sp->start_seq = seq;
|
| 5199 | sp->end_seq = end_seq;
|
| 5200 | tp->rx_opt.num_sacks++;
|
| 5201 | }
|
| 5202 |
|
| 5203 |
|
| 5204 |
|
| 5205 | static void tcp_sack_remove(struct tcp_sock *tp)
|
| 5206 | {
|
| 5207 | struct tcp_sack_block *sp = &tp->selective_acks[0];
|
| 5208 | int num_sacks = tp->rx_opt.num_sacks;
|
| 5209 | int this_sack;
|
| 5210 |
|
| 5211 |
|
| 5212 | if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
|
| 5213 | tp->rx_opt.num_sacks = 0;
|
| 5214 | return;
|
| 5215 | }
|
| 5216 |
|
| 5217 | for (this_sack = 0; this_sack < num_sacks;) {
|
| 5218 |
|
| 5219 | if (!before(tp->rcv_nxt, sp->start_seq)) {
|
| 5220 | int i;
|
| 5221 |
|
| 5222 |
|
| 5223 | WARN_ON(before(tp->rcv_nxt, sp->end_seq));
|
| 5224 |
|
| 5225 |
|
| 5226 | for (i = this_sack+1; i < num_sacks; i++)
|
| 5227 | tp->selective_acks[i-1] = tp->selective_acks[i];
|
| 5228 | num_sacks--;
|
| 5229 | continue;
|
| 5230 | }
|
| 5231 | this_sack++;
|
| 5232 | sp++;
|
| 5233 | }
|
| 5234 | tp->rx_opt.num_sacks = num_sacks;
|
| 5235 | }
|
| 5236 |
|
| 5237 | |
| 5238 | * tcp_try_coalesce - try to merge skb to prior one
|
| 5239 | * @sk: socket
|
| 5240 | * @to: prior buffer
|
| 5241 | * @from: buffer to add in queue
|
| 5242 | * @fragstolen: pointer to boolean
|
| 5243 | *
|
| 5244 | * Before queueing skb @from after @to, try to merge them
|
| 5245 | * to reduce overall memory use and queue lengths, if cost is small.
|
| 5246 | * Packets in ofo or receive queues can stay a long time.
|
| 5247 | * Better try to coalesce them right now to avoid future collapses.
|
| 5248 | * Returns true if caller should free @from instead of queueing it
|
| 5249 |
|
| 5250 | static bool tcp_try_coalesce(struct sock *sk,
|
| 5251 | struct sk_buff *to,
|
| 5252 | struct sk_buff *from,
|
| 5253 | bool *fragstolen)
|
| 5254 | {
|
| 5255 | int delta;
|
| 5256 |
|
| 5257 | *fragstolen = false;
|
| 5258 |
|
| 5259 |
|
| 5260 | if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
|
| 5261 | return false;
|
| 5262 |
|
| 5263 | if (!tcp_skb_can_collapse_rx(to, from))
|
| 5264 | return false;
|
| 5265 |
|
| 5266 | if (!skb_try_coalesce(to, from, fragstolen, &delta))
|
| 5267 | return false;
|
| 5268 |
|
| 5269 | atomic_add(delta, &sk->sk_rmem_alloc);
|
| 5270 | sk_mem_charge(sk, delta);
|
| 5271 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE);
|
| 5272 | TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
|
| 5273 | TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
|
| 5274 | TCP_SKB_CB(to)->tcp_flags |= TCP_SKB_CB(from)->tcp_flags;
|
| 5275 |
|
| 5276 | if (TCP_SKB_CB(from)->has_rxtstamp) {
|
| 5277 | TCP_SKB_CB(to)->has_rxtstamp = true;
|
| 5278 | to->tstamp = from->tstamp;
|
| 5279 | skb_hwtstamps(to)->hwtstamp = skb_hwtstamps(from)->hwtstamp;
|
| 5280 | }
|
| 5281 |
|
| 5282 | return true;
|
| 5283 | }
|
| 5284 |
|
| 5285 | static bool tcp_ooo_try_coalesce(struct sock *sk,
|
| 5286 | struct sk_buff *to,
|
| 5287 | struct sk_buff *from,
|
| 5288 | bool *fragstolen)
|
| 5289 | {
|
| 5290 | bool res = tcp_try_coalesce(sk, to, from, fragstolen);
|
| 5291 |
|
| 5292 |
|
| 5293 | if (res) {
|
| 5294 | u32 gso_segs = max_t(u16, 1, skb_shinfo(to)->gso_segs) +
|
| 5295 | max_t(u16, 1, skb_shinfo(from)->gso_segs);
|
| 5296 |
|
| 5297 | skb_shinfo(to)->gso_segs = min_t(u32, gso_segs, 0xFFFF);
|
| 5298 | }
|
| 5299 | return res;
|
| 5300 | }
|
| 5301 |
|
| 5302 | noinline_for_tracing static void
|
| 5303 | tcp_drop_reason(struct sock *sk, struct sk_buff *skb, enum skb_drop_reason reason)
|
| 5304 | {
|
| 5305 | sk_drops_skbadd(sk, skb);
|
| 5306 | sk_skb_reason_drop(sk, skb, reason);
|
| 5307 | }
|
| 5308 |
|
| 5309 | |
| 5310 | * out_of_order queue into the receive_queue.
|
| 5311 |
|
| 5312 | static void tcp_ofo_queue(struct sock *sk)
|
| 5313 | {
|
| 5314 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5315 | __u32 dsack_high = tp->rcv_nxt;
|
| 5316 | bool fin, fragstolen, eaten;
|
| 5317 | struct sk_buff *skb, *tail;
|
| 5318 | struct rb_node *p;
|
| 5319 |
|
| 5320 | p = rb_first(&tp->out_of_order_queue);
|
| 5321 | while (p) {
|
| 5322 | skb = rb_to_skb(p);
|
| 5323 | if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
|
| 5324 | break;
|
| 5325 |
|
| 5326 | if (before(TCP_SKB_CB(skb)->seq, dsack_high)) {
|
| 5327 | __u32 dsack = dsack_high;
|
| 5328 |
|
| 5329 | if (before(TCP_SKB_CB(skb)->end_seq, dsack_high))
|
| 5330 | dsack = TCP_SKB_CB(skb)->end_seq;
|
| 5331 | tcp_dsack_extend(sk, TCP_SKB_CB(skb)->seq, dsack);
|
| 5332 | }
|
| 5333 | p = rb_next(p);
|
| 5334 | rb_erase(&skb->rbnode, &tp->out_of_order_queue);
|
| 5335 |
|
| 5336 | if (unlikely(!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))) {
|
| 5337 | tcp_drop_reason(sk, skb, SKB_DROP_REASON_TCP_OFO_DROP);
|
| 5338 | continue;
|
| 5339 | }
|
| 5340 |
|
| 5341 | tail = skb_peek_tail(&sk->sk_receive_queue);
|
| 5342 | eaten = tail && tcp_try_coalesce(sk, tail, skb, &fragstolen);
|
| 5343 | tcp_rcv_nxt_update(tp, TCP_SKB_CB(skb)->end_seq);
|
| 5344 | fin = TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN;
|
| 5345 | if (!eaten)
|
| 5346 | tcp_add_receive_queue(sk, skb);
|
| 5347 | else
|
| 5348 | kfree_skb_partial(skb, fragstolen);
|
| 5349 |
|
| 5350 | if (unlikely(fin)) {
|
| 5351 | tcp_fin(sk);
|
| 5352 | |
| 5353 | * so we must end this loop right now.
|
| 5354 |
|
| 5355 | break;
|
| 5356 | }
|
| 5357 | }
|
| 5358 | }
|
| 5359 |
|
| 5360 | static bool tcp_prune_ofo_queue(struct sock *sk, const struct sk_buff *in_skb);
|
| 5361 | static int tcp_prune_queue(struct sock *sk, const struct sk_buff *in_skb);
|
| 5362 |
|
| 5363 | static bool tcp_can_ingest(const struct sock *sk, const struct sk_buff *skb)
|
| 5364 | {
|
| 5365 | unsigned int rmem = atomic_read(&sk->sk_rmem_alloc);
|
| 5366 |
|
| 5367 | return rmem <= sk->sk_rcvbuf;
|
| 5368 | }
|
| 5369 |
|
| 5370 | static int tcp_try_rmem_schedule(struct sock *sk, const struct sk_buff *skb,
|
| 5371 | unsigned int size)
|
| 5372 | {
|
| 5373 | if (!tcp_can_ingest(sk, skb) ||
|
| 5374 | !sk_rmem_schedule(sk, skb, size)) {
|
| 5375 |
|
| 5376 | if (tcp_prune_queue(sk, skb) < 0)
|
| 5377 | return -1;
|
| 5378 |
|
| 5379 | while (!sk_rmem_schedule(sk, skb, size)) {
|
| 5380 | if (!tcp_prune_ofo_queue(sk, skb))
|
| 5381 | return -1;
|
| 5382 | }
|
| 5383 | }
|
| 5384 | return 0;
|
| 5385 | }
|
| 5386 |
|
| 5387 | static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
|
| 5388 | {
|
| 5389 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5390 | struct rb_node **p, *parent;
|
| 5391 | struct sk_buff *skb1;
|
| 5392 | u32 seq, end_seq;
|
| 5393 | bool fragstolen;
|
| 5394 |
|
| 5395 | tcp_save_lrcv_flowlabel(sk, skb);
|
| 5396 | tcp_data_ecn_check(sk, skb);
|
| 5397 |
|
| 5398 | if (unlikely(tcp_try_rmem_schedule(sk, skb, skb->truesize))) {
|
| 5399 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFODROP);
|
| 5400 | READ_ONCE(sk->sk_data_ready)(sk);
|
| 5401 | tcp_drop_reason(sk, skb, SKB_DROP_REASON_PROTO_MEM);
|
| 5402 | return;
|
| 5403 | }
|
| 5404 |
|
| 5405 | tcp_measure_rcv_mss(sk, skb);
|
| 5406 |
|
| 5407 | tp->pred_flags = 0;
|
| 5408 | inet_csk_schedule_ack(sk);
|
| 5409 |
|
| 5410 | tp->rcv_ooopack += max_t(u16, 1, skb_shinfo(skb)->gso_segs);
|
| 5411 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
|
| 5412 | seq = TCP_SKB_CB(skb)->seq;
|
| 5413 | end_seq = TCP_SKB_CB(skb)->end_seq;
|
| 5414 |
|
| 5415 | p = &tp->out_of_order_queue.rb_node;
|
| 5416 | if (RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
|
| 5417 |
|
| 5418 | if (tcp_is_sack(tp)) {
|
| 5419 | tp->rx_opt.num_sacks = 1;
|
| 5420 | tp->selective_acks[0].start_seq = seq;
|
| 5421 | tp->selective_acks[0].end_seq = end_seq;
|
| 5422 | }
|
| 5423 | rb_link_node(&skb->rbnode, NULL, p);
|
| 5424 | rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
|
| 5425 | tp->ooo_last_skb = skb;
|
| 5426 | goto end;
|
| 5427 | }
|
| 5428 |
|
| 5429 | |
| 5430 | * Use of ooo_last_skb avoids the O(Log(N)) rbtree lookup.
|
| 5431 |
|
| 5432 | if (tcp_ooo_try_coalesce(sk, tp->ooo_last_skb,
|
| 5433 | skb, &fragstolen)) {
|
| 5434 | coalesce_done:
|
| 5435 | |
| 5436 | * and trigger fast retransmit.
|
| 5437 |
|
| 5438 | if (tcp_is_sack(tp))
|
| 5439 | tcp_grow_window(sk, skb, true);
|
| 5440 | kfree_skb_partial(skb, fragstolen);
|
| 5441 | skb = NULL;
|
| 5442 | goto add_sack;
|
| 5443 | }
|
| 5444 |
|
| 5445 | if (!before(seq, TCP_SKB_CB(tp->ooo_last_skb)->end_seq)) {
|
| 5446 | parent = &tp->ooo_last_skb->rbnode;
|
| 5447 | p = &parent->rb_right;
|
| 5448 | goto insert;
|
| 5449 | }
|
| 5450 |
|
| 5451 |
|
| 5452 | parent = NULL;
|
| 5453 | while (*p) {
|
| 5454 | parent = *p;
|
| 5455 | skb1 = rb_to_skb(parent);
|
| 5456 | if (before(seq, TCP_SKB_CB(skb1)->seq)) {
|
| 5457 | p = &parent->rb_left;
|
| 5458 | continue;
|
| 5459 | }
|
| 5460 | if (before(seq, TCP_SKB_CB(skb1)->end_seq)) {
|
| 5461 | if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
|
| 5462 |
|
| 5463 | NET_INC_STATS(sock_net(sk),
|
| 5464 | LINUX_MIB_TCPOFOMERGE);
|
| 5465 | tcp_drop_reason(sk, skb,
|
| 5466 | SKB_DROP_REASON_TCP_OFOMERGE);
|
| 5467 | skb = NULL;
|
| 5468 | tcp_dsack_set(sk, seq, end_seq);
|
| 5469 | goto add_sack;
|
| 5470 | }
|
| 5471 | if (after(seq, TCP_SKB_CB(skb1)->seq)) {
|
| 5472 |
|
| 5473 | tcp_dsack_set(sk, seq, TCP_SKB_CB(skb1)->end_seq);
|
| 5474 | } else {
|
| 5475 | |
| 5476 | * Replace skb1 with skb.
|
| 5477 |
|
| 5478 | rb_replace_node(&skb1->rbnode, &skb->rbnode,
|
| 5479 | &tp->out_of_order_queue);
|
| 5480 | tcp_dsack_extend(sk,
|
| 5481 | TCP_SKB_CB(skb1)->seq,
|
| 5482 | TCP_SKB_CB(skb1)->end_seq);
|
| 5483 | NET_INC_STATS(sock_net(sk),
|
| 5484 | LINUX_MIB_TCPOFOMERGE);
|
| 5485 | tcp_drop_reason(sk, skb1,
|
| 5486 | SKB_DROP_REASON_TCP_OFOMERGE);
|
| 5487 | goto merge_right;
|
| 5488 | }
|
| 5489 | } else if (tcp_ooo_try_coalesce(sk, skb1,
|
| 5490 | skb, &fragstolen)) {
|
| 5491 | goto coalesce_done;
|
| 5492 | }
|
| 5493 | p = &parent->rb_right;
|
| 5494 | }
|
| 5495 | insert:
|
| 5496 |
|
| 5497 | rb_link_node(&skb->rbnode, parent, p);
|
| 5498 | rb_insert_color(&skb->rbnode, &tp->out_of_order_queue);
|
| 5499 |
|
| 5500 | merge_right:
|
| 5501 |
|
| 5502 | while ((skb1 = skb_rb_next(skb)) != NULL) {
|
| 5503 | if (!after(end_seq, TCP_SKB_CB(skb1)->seq))
|
| 5504 | break;
|
| 5505 | if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
|
| 5506 | tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
|
| 5507 | end_seq);
|
| 5508 | break;
|
| 5509 | }
|
| 5510 | rb_erase(&skb1->rbnode, &tp->out_of_order_queue);
|
| 5511 | tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
|
| 5512 | TCP_SKB_CB(skb1)->end_seq);
|
| 5513 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
|
| 5514 | tcp_drop_reason(sk, skb1, SKB_DROP_REASON_TCP_OFOMERGE);
|
| 5515 | }
|
| 5516 |
|
| 5517 | if (!skb1)
|
| 5518 | tp->ooo_last_skb = skb;
|
| 5519 |
|
| 5520 | add_sack:
|
| 5521 | if (tcp_is_sack(tp))
|
| 5522 | tcp_sack_new_ofo_skb(sk, seq, end_seq);
|
| 5523 | end:
|
| 5524 | if (skb) {
|
| 5525 | |
| 5526 | * and trigger fast retransmit.
|
| 5527 |
|
| 5528 | if (tcp_is_sack(tp))
|
| 5529 | tcp_grow_window(sk, skb, false);
|
| 5530 | skb_condense(skb);
|
| 5531 | skb_set_owner_r(skb, sk);
|
| 5532 | }
|
| 5533 |
|
| 5534 | if (sk->sk_socket)
|
| 5535 | tcp_rcvbuf_grow(sk, tp->rcvq_space.space);
|
| 5536 | }
|
| 5537 |
|
| 5538 | static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb,
|
| 5539 | bool *fragstolen)
|
| 5540 | {
|
| 5541 | int eaten;
|
| 5542 | struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue);
|
| 5543 |
|
| 5544 | eaten = (tail &&
|
| 5545 | tcp_try_coalesce(sk, tail,
|
| 5546 | skb, fragstolen)) ? 1 : 0;
|
| 5547 | tcp_rcv_nxt_update(tcp_sk(sk), TCP_SKB_CB(skb)->end_seq);
|
| 5548 | if (!eaten) {
|
| 5549 | tcp_add_receive_queue(sk, skb);
|
| 5550 | skb_set_owner_r(skb, sk);
|
| 5551 | }
|
| 5552 | return eaten;
|
| 5553 | }
|
| 5554 |
|
| 5555 | int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
|
| 5556 | {
|
| 5557 | struct sk_buff *skb;
|
| 5558 | int err = -ENOMEM;
|
| 5559 | int data_len = 0;
|
| 5560 | bool fragstolen;
|
| 5561 |
|
| 5562 | if (size == 0)
|
| 5563 | return 0;
|
| 5564 |
|
| 5565 | if (size > PAGE_SIZE) {
|
| 5566 | int npages = min_t(size_t, size >> PAGE_SHIFT, MAX_SKB_FRAGS);
|
| 5567 |
|
| 5568 | data_len = npages << PAGE_SHIFT;
|
| 5569 | size = data_len + (size & ~PAGE_MASK);
|
| 5570 | }
|
| 5571 | skb = alloc_skb_with_frags(size - data_len, data_len,
|
| 5572 | PAGE_ALLOC_COSTLY_ORDER,
|
| 5573 | &err, sk->sk_allocation);
|
| 5574 | if (!skb)
|
| 5575 | goto err;
|
| 5576 |
|
| 5577 | skb_put(skb, size - data_len);
|
| 5578 | skb->data_len = data_len;
|
| 5579 | skb->len = size;
|
| 5580 |
|
| 5581 | if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
|
| 5582 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVQDROP);
|
| 5583 | goto err_free;
|
| 5584 | }
|
| 5585 |
|
| 5586 | err = skb_copy_datagram_from_iter(skb, 0, &msg->msg_iter, size);
|
| 5587 | if (err)
|
| 5588 | goto err_free;
|
| 5589 |
|
| 5590 | TCP_SKB_CB(skb)->seq = tcp_sk(sk)->rcv_nxt;
|
| 5591 | TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size;
|
| 5592 | TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1;
|
| 5593 |
|
| 5594 | if (tcp_queue_rcv(sk, skb, &fragstolen)) {
|
| 5595 | WARN_ON_ONCE(fragstolen);
|
| 5596 | __kfree_skb(skb);
|
| 5597 | }
|
| 5598 | return size;
|
| 5599 |
|
| 5600 | err_free:
|
| 5601 | kfree_skb(skb);
|
| 5602 | err:
|
| 5603 | return err;
|
| 5604 |
|
| 5605 | }
|
| 5606 |
|
| 5607 | void tcp_data_ready(struct sock *sk)
|
| 5608 | {
|
| 5609 | if (tcp_epollin_ready(sk, sk->sk_rcvlowat) || sock_flag(sk, SOCK_DONE))
|
| 5610 | READ_ONCE(sk->sk_data_ready)(sk);
|
| 5611 | }
|
| 5612 |
|
| 5613 | static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
|
| 5614 | {
|
| 5615 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5616 | enum skb_drop_reason reason;
|
| 5617 | bool fragstolen;
|
| 5618 | int eaten;
|
| 5619 |
|
| 5620 | |
| 5621 | * to be processed, drop the packet.
|
| 5622 |
|
| 5623 | if (sk_is_mptcp(sk) && !mptcp_incoming_options(sk, skb)) {
|
| 5624 | __kfree_skb(skb);
|
| 5625 | return;
|
| 5626 | }
|
| 5627 |
|
| 5628 | if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq) {
|
| 5629 | __kfree_skb(skb);
|
| 5630 | return;
|
| 5631 | }
|
| 5632 | tcp_cleanup_skb(skb);
|
| 5633 | __skb_pull(skb, tcp_hdr(skb)->doff * 4);
|
| 5634 |
|
| 5635 | reason = SKB_DROP_REASON_NOT_SPECIFIED;
|
| 5636 | tp->rx_opt.dsack = 0;
|
| 5637 |
|
| 5638 | |
| 5639 | * Packets in sequence go to the receive queue.
|
| 5640 | * Out of sequence packets to the out_of_order_queue.
|
| 5641 |
|
| 5642 | if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
|
| 5643 | if (tcp_receive_window(tp) == 0) {
|
| 5644 | |
| 5645 | * in a loop even if we send RWIN 0 in our ACK.
|
| 5646 | * Accepting this FIN does not hurt memory pressure
|
| 5647 | * because the FIN flag will simply be merged to the
|
| 5648 | * receive queue tail skb in most cases.
|
| 5649 |
|
| 5650 | if (!skb->len &&
|
| 5651 | (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN))
|
| 5652 | goto queue_and_out;
|
| 5653 |
|
| 5654 | reason = SKB_DROP_REASON_TCP_ZEROWINDOW;
|
| 5655 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPZEROWINDOWDROP);
|
| 5656 | goto out_of_window;
|
| 5657 | }
|
| 5658 |
|
| 5659 |
|
| 5660 | queue_and_out:
|
| 5661 | if (tcp_try_rmem_schedule(sk, skb, skb->truesize)) {
|
| 5662 |
|
| 5663 | inet_csk(sk)->icsk_ack.pending |=
|
| 5664 | (ICSK_ACK_NOMEM | ICSK_ACK_NOW);
|
| 5665 | inet_csk_schedule_ack(sk);
|
| 5666 | READ_ONCE(sk->sk_data_ready)(sk);
|
| 5667 |
|
| 5668 | if (skb_queue_len(&sk->sk_receive_queue) && skb->len) {
|
| 5669 | reason = SKB_DROP_REASON_PROTO_MEM;
|
| 5670 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVQDROP);
|
| 5671 | goto drop;
|
| 5672 | }
|
| 5673 | sk_forced_mem_schedule(sk, skb->truesize);
|
| 5674 | }
|
| 5675 |
|
| 5676 | eaten = tcp_queue_rcv(sk, skb, &fragstolen);
|
| 5677 | if (skb->len)
|
| 5678 | tcp_event_data_recv(sk, skb);
|
| 5679 | if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
|
| 5680 | tcp_fin(sk);
|
| 5681 |
|
| 5682 | if (!RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
|
| 5683 | tcp_ofo_queue(sk);
|
| 5684 |
|
| 5685 | |
| 5686 | * gap in queue is filled.
|
| 5687 |
|
| 5688 | if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
|
| 5689 | inet_csk(sk)->icsk_ack.pending |= ICSK_ACK_NOW;
|
| 5690 | }
|
| 5691 |
|
| 5692 | if (tp->rx_opt.num_sacks)
|
| 5693 | tcp_sack_remove(tp);
|
| 5694 |
|
| 5695 | tcp_fast_path_check(sk);
|
| 5696 |
|
| 5697 | if (eaten > 0)
|
| 5698 | kfree_skb_partial(skb, fragstolen);
|
| 5699 | if (!sock_flag(sk, SOCK_DEAD))
|
| 5700 | tcp_data_ready(sk);
|
| 5701 | return;
|
| 5702 | }
|
| 5703 |
|
| 5704 | if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
|
| 5705 | tcp_rcv_spurious_retrans(sk, skb);
|
| 5706 |
|
| 5707 | reason = SKB_DROP_REASON_TCP_OLD_DATA;
|
| 5708 | NET_INC_STATS(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
|
| 5709 | tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
|
| 5710 |
|
| 5711 | out_of_window:
|
| 5712 | tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
|
| 5713 | inet_csk_schedule_ack(sk);
|
| 5714 | drop:
|
| 5715 | tcp_drop_reason(sk, skb, reason);
|
| 5716 | return;
|
| 5717 | }
|
| 5718 |
|
| 5719 |
|
| 5720 | if (!before(TCP_SKB_CB(skb)->seq,
|
| 5721 | tp->rcv_nxt + tcp_receive_window(tp))) {
|
| 5722 | reason = SKB_DROP_REASON_TCP_OVERWINDOW;
|
| 5723 | NET_INC_STATS(sock_net(sk), LINUX_MIB_BEYOND_WINDOW);
|
| 5724 | goto out_of_window;
|
| 5725 | }
|
| 5726 |
|
| 5727 | if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
|
| 5728 |
|
| 5729 | tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
|
| 5730 |
|
| 5731 | |
| 5732 | * remembering D-SACK for its head made in previous line.
|
| 5733 |
|
| 5734 | if (!tcp_receive_window(tp)) {
|
| 5735 | reason = SKB_DROP_REASON_TCP_ZEROWINDOW;
|
| 5736 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPZEROWINDOWDROP);
|
| 5737 | goto out_of_window;
|
| 5738 | }
|
| 5739 | goto queue_and_out;
|
| 5740 | }
|
| 5741 |
|
| 5742 | tcp_data_queue_ofo(sk, skb);
|
| 5743 | }
|
| 5744 |
|
| 5745 | static struct sk_buff *tcp_skb_next(struct sk_buff *skb, struct sk_buff_head *list)
|
| 5746 | {
|
| 5747 | if (list)
|
| 5748 | return !skb_queue_is_last(list, skb) ? skb->next : NULL;
|
| 5749 |
|
| 5750 | return skb_rb_next(skb);
|
| 5751 | }
|
| 5752 |
|
| 5753 | static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
|
| 5754 | struct sk_buff_head *list,
|
| 5755 | struct rb_root *root)
|
| 5756 | {
|
| 5757 | struct sk_buff *next = tcp_skb_next(skb, list);
|
| 5758 |
|
| 5759 | if (list)
|
| 5760 | __skb_unlink(skb, list);
|
| 5761 | else
|
| 5762 | rb_erase(&skb->rbnode, root);
|
| 5763 |
|
| 5764 | __kfree_skb(skb);
|
| 5765 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPRCVCOLLAPSED);
|
| 5766 |
|
| 5767 | return next;
|
| 5768 | }
|
| 5769 |
|
| 5770 | |
| 5771 | * sequence numbers start..end.
|
| 5772 | *
|
| 5773 | * If tail is NULL, this means until the end of the queue.
|
| 5774 | *
|
| 5775 | * Segments with FIN/SYN are not collapsed (only because this
|
| 5776 | * simplifies code)
|
| 5777 |
|
| 5778 | static void
|
| 5779 | tcp_collapse(struct sock *sk, struct sk_buff_head *list, struct rb_root *root,
|
| 5780 | struct sk_buff *head, struct sk_buff *tail, u32 start, u32 end)
|
| 5781 | {
|
| 5782 | struct sk_buff *skb = head, *n;
|
| 5783 | struct sk_buff_head tmp;
|
| 5784 | bool end_of_skbs;
|
| 5785 |
|
| 5786 | |
| 5787 | * the point where collapsing can be useful.
|
| 5788 |
|
| 5789 | restart:
|
| 5790 | for (end_of_skbs = true; skb != NULL && skb != tail; skb = n) {
|
| 5791 | n = tcp_skb_next(skb, list);
|
| 5792 |
|
| 5793 | if (!skb_frags_readable(skb))
|
| 5794 | goto skip_this;
|
| 5795 |
|
| 5796 |
|
| 5797 | if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
|
| 5798 | skb = tcp_collapse_one(sk, skb, list, root);
|
| 5799 | if (!skb)
|
| 5800 | break;
|
| 5801 | goto restart;
|
| 5802 | }
|
| 5803 |
|
| 5804 | |
| 5805 | * - not SYN/FIN and
|
| 5806 | * - bloated or contains data before "start" or
|
| 5807 | * overlaps to the next one and mptcp allow collapsing.
|
| 5808 |
|
| 5809 | if (!(TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) &&
|
| 5810 | (tcp_win_from_space(sk, skb->truesize) > skb->len ||
|
| 5811 | before(TCP_SKB_CB(skb)->seq, start))) {
|
| 5812 | end_of_skbs = false;
|
| 5813 | break;
|
| 5814 | }
|
| 5815 |
|
| 5816 | if (n && n != tail && skb_frags_readable(n) &&
|
| 5817 | tcp_skb_can_collapse_rx(skb, n) &&
|
| 5818 | TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(n)->seq) {
|
| 5819 | end_of_skbs = false;
|
| 5820 | break;
|
| 5821 | }
|
| 5822 |
|
| 5823 | skip_this:
|
| 5824 |
|
| 5825 | start = TCP_SKB_CB(skb)->end_seq;
|
| 5826 | }
|
| 5827 | if (end_of_skbs ||
|
| 5828 | (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) ||
|
| 5829 | !skb_frags_readable(skb))
|
| 5830 | return;
|
| 5831 |
|
| 5832 | __skb_queue_head_init(&tmp);
|
| 5833 |
|
| 5834 | while (before(start, end)) {
|
| 5835 | int copy = min_t(int, SKB_MAX_ORDER(0, 0), end - start);
|
| 5836 | struct sk_buff *nskb;
|
| 5837 |
|
| 5838 | nskb = alloc_skb(copy, GFP_ATOMIC);
|
| 5839 | if (!nskb)
|
| 5840 | break;
|
| 5841 |
|
| 5842 | memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
|
| 5843 | skb_copy_decrypted(nskb, skb);
|
| 5844 | TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
|
| 5845 | if (list)
|
| 5846 | __skb_queue_before(list, skb, nskb);
|
| 5847 | else
|
| 5848 | __skb_queue_tail(&tmp, nskb);
|
| 5849 | skb_set_owner_r(nskb, sk);
|
| 5850 | mptcp_skb_ext_move(nskb, skb);
|
| 5851 |
|
| 5852 |
|
| 5853 | while (copy > 0) {
|
| 5854 | int offset = start - TCP_SKB_CB(skb)->seq;
|
| 5855 | int size = TCP_SKB_CB(skb)->end_seq - start;
|
| 5856 |
|
| 5857 | BUG_ON(offset < 0);
|
| 5858 | if (size > 0) {
|
| 5859 | size = min(copy, size);
|
| 5860 | if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
|
| 5861 | BUG();
|
| 5862 | TCP_SKB_CB(nskb)->end_seq += size;
|
| 5863 | copy -= size;
|
| 5864 | start += size;
|
| 5865 | }
|
| 5866 | if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
|
| 5867 | skb = tcp_collapse_one(sk, skb, list, root);
|
| 5868 | if (!skb ||
|
| 5869 | skb == tail ||
|
| 5870 | !tcp_skb_can_collapse_rx(nskb, skb) ||
|
| 5871 | (TCP_SKB_CB(skb)->tcp_flags & (TCPHDR_SYN | TCPHDR_FIN)) ||
|
| 5872 | !skb_frags_readable(skb))
|
| 5873 | goto end;
|
| 5874 | }
|
| 5875 | }
|
| 5876 | }
|
| 5877 | end:
|
| 5878 | skb_queue_walk_safe(&tmp, skb, n)
|
| 5879 | tcp_rbtree_insert(root, skb);
|
| 5880 | }
|
| 5881 |
|
| 5882 | |
| 5883 | * and tcp_collapse() them until all the queue is collapsed.
|
| 5884 |
|
| 5885 | static void tcp_collapse_ofo_queue(struct sock *sk)
|
| 5886 | {
|
| 5887 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5888 | u32 range_truesize, sum_tiny = 0;
|
| 5889 | struct sk_buff *skb, *head;
|
| 5890 | u32 start, end;
|
| 5891 |
|
| 5892 | skb = skb_rb_first(&tp->out_of_order_queue);
|
| 5893 | new_range:
|
| 5894 | if (!skb) {
|
| 5895 | tp->ooo_last_skb = skb_rb_last(&tp->out_of_order_queue);
|
| 5896 | return;
|
| 5897 | }
|
| 5898 | start = TCP_SKB_CB(skb)->seq;
|
| 5899 | end = TCP_SKB_CB(skb)->end_seq;
|
| 5900 | range_truesize = skb->truesize;
|
| 5901 |
|
| 5902 | for (head = skb;;) {
|
| 5903 | skb = skb_rb_next(skb);
|
| 5904 |
|
| 5905 | |
| 5906 | * we are at the queue end.
|
| 5907 |
|
| 5908 | if (!skb ||
|
| 5909 | after(TCP_SKB_CB(skb)->seq, end) ||
|
| 5910 | before(TCP_SKB_CB(skb)->end_seq, start)) {
|
| 5911 |
|
| 5912 | if (range_truesize != head->truesize ||
|
| 5913 | end - start >= SKB_WITH_OVERHEAD(PAGE_SIZE)) {
|
| 5914 | tcp_collapse(sk, NULL, &tp->out_of_order_queue,
|
| 5915 | head, skb, start, end);
|
| 5916 | } else {
|
| 5917 | sum_tiny += range_truesize;
|
| 5918 | if (sum_tiny > sk->sk_rcvbuf >> 3)
|
| 5919 | return;
|
| 5920 | }
|
| 5921 | goto new_range;
|
| 5922 | }
|
| 5923 |
|
| 5924 | range_truesize += skb->truesize;
|
| 5925 | if (unlikely(before(TCP_SKB_CB(skb)->seq, start)))
|
| 5926 | start = TCP_SKB_CB(skb)->seq;
|
| 5927 | if (after(TCP_SKB_CB(skb)->end_seq, end))
|
| 5928 | end = TCP_SKB_CB(skb)->end_seq;
|
| 5929 | }
|
| 5930 | }
|
| 5931 |
|
| 5932 | |
| 5933 | * Clean the out-of-order queue to make room.
|
| 5934 | * We drop high sequences packets to :
|
| 5935 | * 1) Let a chance for holes to be filled.
|
| 5936 | * This means we do not drop packets from ooo queue if their sequence
|
| 5937 | * is before incoming packet sequence.
|
| 5938 | * 2) not add too big latencies if thousands of packets sit there.
|
| 5939 | * (But if application shrinks SO_RCVBUF, we could still end up
|
| 5940 | * freeing whole queue here)
|
| 5941 | * 3) Drop at least 12.5 % of sk_rcvbuf to avoid malicious attacks.
|
| 5942 | *
|
| 5943 | * Return true if queue has shrunk.
|
| 5944 |
|
| 5945 | static bool tcp_prune_ofo_queue(struct sock *sk, const struct sk_buff *in_skb)
|
| 5946 | {
|
| 5947 | struct tcp_sock *tp = tcp_sk(sk);
|
| 5948 | struct rb_node *node, *prev;
|
| 5949 | bool pruned = false;
|
| 5950 | int goal;
|
| 5951 |
|
| 5952 | if (RB_EMPTY_ROOT(&tp->out_of_order_queue))
|
| 5953 | return false;
|
| 5954 |
|
| 5955 | goal = sk->sk_rcvbuf >> 3;
|
| 5956 | node = &tp->ooo_last_skb->rbnode;
|
| 5957 |
|
| 5958 | do {
|
| 5959 | struct sk_buff *skb = rb_to_skb(node);
|
| 5960 |
|
| 5961 |
|
| 5962 | if (after(TCP_SKB_CB(in_skb)->seq, TCP_SKB_CB(skb)->seq))
|
| 5963 | break;
|
| 5964 | pruned = true;
|
| 5965 | prev = rb_prev(node);
|
| 5966 | rb_erase(node, &tp->out_of_order_queue);
|
| 5967 | goal -= skb->truesize;
|
| 5968 | tcp_drop_reason(sk, skb, SKB_DROP_REASON_TCP_OFO_QUEUE_PRUNE);
|
| 5969 | tp->ooo_last_skb = rb_to_skb(prev);
|
| 5970 | if (!prev || goal <= 0) {
|
| 5971 | if (tcp_can_ingest(sk, in_skb) &&
|
| 5972 | !tcp_under_memory_pressure(sk))
|
| 5973 | break;
|
| 5974 | goal = sk->sk_rcvbuf >> 3;
|
| 5975 | }
|
| 5976 | node = prev;
|
| 5977 | } while (node);
|
| 5978 |
|
| 5979 | if (pruned) {
|
| 5980 | NET_INC_STATS(sock_net(sk), LINUX_MIB_OFOPRUNED);
|
| 5981 | |
| 5982 | * do the same at a timeout based retransmit. When a connection
|
| 5983 | * is in a sad state like this, we care only about integrity
|
| 5984 | * of the connection not performance.
|
| 5985 |
|
| 5986 | if (tp->rx_opt.sack_ok)
|
| 5987 | tcp_sack_reset(&tp->rx_opt);
|
| 5988 | }
|
| 5989 | return pruned;
|
| 5990 | }
|
| 5991 |
|
| 5992 | |
| 5993 | * the socket within its memory limits again.
|
| 5994 | *
|
| 5995 | * Return less than zero if we should start dropping frames
|
| 5996 | * until the socket owning process reads some of the data
|
| 5997 | * to stabilize the situation.
|
| 5998 |
|
| 5999 | static int tcp_prune_queue(struct sock *sk, const struct sk_buff *in_skb)
|
| 6000 | {
|
| 6001 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6002 |
|
| 6003 |
|
| 6004 | if (!atomic_read(&sk->sk_rmem_alloc))
|
| 6005 | return -1;
|
| 6006 |
|
| 6007 | NET_INC_STATS(sock_net(sk), LINUX_MIB_PRUNECALLED);
|
| 6008 |
|
| 6009 | if (!tcp_can_ingest(sk, in_skb))
|
| 6010 | tcp_clamp_window(sk);
|
| 6011 | else if (tcp_under_memory_pressure(sk))
|
| 6012 | tcp_adjust_rcv_ssthresh(sk);
|
| 6013 |
|
| 6014 | if (tcp_can_ingest(sk, in_skb))
|
| 6015 | return 0;
|
| 6016 |
|
| 6017 | tcp_collapse_ofo_queue(sk);
|
| 6018 | if (!skb_queue_empty(&sk->sk_receive_queue))
|
| 6019 | tcp_collapse(sk, &sk->sk_receive_queue, NULL,
|
| 6020 | skb_peek(&sk->sk_receive_queue),
|
| 6021 | NULL,
|
| 6022 | tp->copied_seq, tp->rcv_nxt);
|
| 6023 |
|
| 6024 | if (tcp_can_ingest(sk, in_skb))
|
| 6025 | return 0;
|
| 6026 |
|
| 6027 | |
| 6028 | * This must not ever occur.
|
| 6029 |
|
| 6030 | tcp_prune_ofo_queue(sk, in_skb);
|
| 6031 |
|
| 6032 | if (tcp_can_ingest(sk, in_skb))
|
| 6033 | return 0;
|
| 6034 |
|
| 6035 | |
| 6036 | * drop receive data on the floor. It will get retransmitted
|
| 6037 | * and hopefully then we'll have sufficient space.
|
| 6038 |
|
| 6039 | NET_INC_STATS(sock_net(sk), LINUX_MIB_RCVPRUNED);
|
| 6040 |
|
| 6041 |
|
| 6042 | tp->pred_flags = 0;
|
| 6043 | return -1;
|
| 6044 | }
|
| 6045 |
|
| 6046 | static bool tcp_should_expand_sndbuf(struct sock *sk)
|
| 6047 | {
|
| 6048 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 6049 |
|
| 6050 | |
| 6051 | * not modify it.
|
| 6052 |
|
| 6053 | if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
|
| 6054 | return false;
|
| 6055 |
|
| 6056 |
|
| 6057 | if (tcp_under_memory_pressure(sk)) {
|
| 6058 | int unused_mem = sk_unused_reserved_mem(sk);
|
| 6059 |
|
| 6060 | |
| 6061 | * it never goes below SOCK_MIN_SNDBUF.
|
| 6062 | * See sk_stream_moderate_sndbuf() for more details.
|
| 6063 |
|
| 6064 | if (unused_mem > SOCK_MIN_SNDBUF)
|
| 6065 | WRITE_ONCE(sk->sk_sndbuf, unused_mem);
|
| 6066 |
|
| 6067 | return false;
|
| 6068 | }
|
| 6069 |
|
| 6070 |
|
| 6071 | if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0))
|
| 6072 | return false;
|
| 6073 |
|
| 6074 |
|
| 6075 | if (tcp_packets_in_flight(tp) >= tcp_snd_cwnd(tp))
|
| 6076 | return false;
|
| 6077 |
|
| 6078 | return true;
|
| 6079 | }
|
| 6080 |
|
| 6081 | static void tcp_new_space(struct sock *sk)
|
| 6082 | {
|
| 6083 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6084 |
|
| 6085 | if (tcp_should_expand_sndbuf(sk)) {
|
| 6086 | tcp_sndbuf_expand(sk);
|
| 6087 | tp->snd_cwnd_stamp = tcp_jiffies32;
|
| 6088 | }
|
| 6089 |
|
| 6090 | INDIRECT_CALL_1(READ_ONCE(sk->sk_write_space),
|
| 6091 | sk_stream_write_space,
|
| 6092 | sk);
|
| 6093 | }
|
| 6094 |
|
| 6095 | |
| 6096 | * 1) Freeing skbs in rtx queues (after tp->snd_una has advanced)
|
| 6097 | * 2) Sent skbs from output queue (and thus advancing tp->snd_nxt)
|
| 6098 | *
|
| 6099 | * We might be able to generate EPOLLOUT to the application if:
|
| 6100 | * 1) Space consumed in output/rtx queues is below sk->sk_sndbuf/2
|
| 6101 | * 2) notsent amount (tp->write_seq - tp->snd_nxt) became
|
| 6102 | * small enough that tcp_stream_memory_free() decides it
|
| 6103 | * is time to generate EPOLLOUT.
|
| 6104 |
|
| 6105 | void __tcp_check_space(struct sock *sk)
|
| 6106 | {
|
| 6107 | tcp_new_space(sk);
|
| 6108 | if (!test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
|
| 6109 | tcp_chrono_stop(sk, TCP_CHRONO_SNDBUF_LIMITED);
|
| 6110 | }
|
| 6111 |
|
| 6112 | static inline void tcp_data_snd_check(struct sock *sk)
|
| 6113 | {
|
| 6114 | tcp_push_pending_frames(sk);
|
| 6115 | tcp_check_space(sk);
|
| 6116 | }
|
| 6117 |
|
| 6118 | |
| 6119 | * Check if sending an ack is needed.
|
| 6120 |
|
| 6121 | static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
|
| 6122 | {
|
| 6123 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6124 | struct net *net = sock_net(sk);
|
| 6125 | unsigned long rtt;
|
| 6126 | u64 delay;
|
| 6127 |
|
| 6128 |
|
| 6129 | if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
|
| 6130 | |
| 6131 | * (tcp_recvmsg() will send ACK otherwise).
|
| 6132 | * If application uses SO_RCVLOWAT, we want send ack now if
|
| 6133 | * we have not received enough bytes to satisfy the condition.
|
| 6134 |
|
| 6135 | (tp->rcv_nxt - tp->copied_seq < sk->sk_rcvlowat ||
|
| 6136 | __tcp_select_window(sk) >= tp->rcv_wnd)) ||
|
| 6137 |
|
| 6138 | tcp_in_quickack_mode(sk) ||
|
| 6139 |
|
| 6140 | inet_csk(sk)->icsk_ack.pending & ICSK_ACK_NOW) {
|
| 6141 | |
| 6142 | * Defer the ack until tcp_release_cb().
|
| 6143 |
|
| 6144 | if (sock_owned_by_user_nocheck(sk) &&
|
| 6145 | READ_ONCE(net->ipv4.sysctl_tcp_backlog_ack_defer)) {
|
| 6146 | set_bit(TCP_ACK_DEFERRED, &sk->sk_tsq_flags);
|
| 6147 | return;
|
| 6148 | }
|
| 6149 | send_now:
|
| 6150 | tcp_send_ack(sk);
|
| 6151 | return;
|
| 6152 | }
|
| 6153 |
|
| 6154 | if (!ofo_possible || RB_EMPTY_ROOT(&tp->out_of_order_queue)) {
|
| 6155 | tcp_send_delayed_ack(sk);
|
| 6156 | return;
|
| 6157 | }
|
| 6158 |
|
| 6159 | if (!tcp_is_sack(tp) ||
|
| 6160 | tp->compressed_ack >= READ_ONCE(net->ipv4.sysctl_tcp_comp_sack_nr))
|
| 6161 | goto send_now;
|
| 6162 |
|
| 6163 | if (tp->compressed_ack_rcv_nxt != tp->rcv_nxt) {
|
| 6164 | tp->compressed_ack_rcv_nxt = tp->rcv_nxt;
|
| 6165 | tp->dup_ack_counter = 0;
|
| 6166 | }
|
| 6167 | if (tp->dup_ack_counter < TCP_FASTRETRANS_THRESH) {
|
| 6168 | tp->dup_ack_counter++;
|
| 6169 | goto send_now;
|
| 6170 | }
|
| 6171 | tp->compressed_ack++;
|
| 6172 | if (hrtimer_is_queued(&tp->compressed_ack_timer))
|
| 6173 | return;
|
| 6174 |
|
| 6175 | |
| 6176 | * but no more than tcp_comp_sack_delay_ns.
|
| 6177 |
|
| 6178 |
|
| 6179 | rtt = tp->rcv_rtt_est.rtt_us;
|
| 6180 | if (tp->srtt_us && tp->srtt_us < rtt)
|
| 6181 | rtt = tp->srtt_us;
|
| 6182 |
|
| 6183 | |
| 6184 | * ->
|
| 6185 | * delay = rtt * 1.25 * comp_sack_rtt_percent
|
| 6186 |
|
| 6187 | delay = (u64)(rtt + (rtt >> 2)) *
|
| 6188 | READ_ONCE(net->ipv4.sysctl_tcp_comp_sack_rtt_percent);
|
| 6189 |
|
| 6190 | delay = min(delay, READ_ONCE(net->ipv4.sysctl_tcp_comp_sack_delay_ns));
|
| 6191 |
|
| 6192 | sock_hold(sk);
|
| 6193 | hrtimer_start_range_ns(&tp->compressed_ack_timer, ns_to_ktime(delay),
|
| 6194 | READ_ONCE(net->ipv4.sysctl_tcp_comp_sack_slack_ns),
|
| 6195 | HRTIMER_MODE_REL_PINNED_SOFT);
|
| 6196 | }
|
| 6197 |
|
| 6198 | static inline void tcp_ack_snd_check(struct sock *sk)
|
| 6199 | {
|
| 6200 | if (!inet_csk_ack_scheduled(sk)) {
|
| 6201 |
|
| 6202 | return;
|
| 6203 | }
|
| 6204 | __tcp_ack_snd_check(sk, 1);
|
| 6205 | }
|
| 6206 |
|
| 6207 | |
| 6208 | * This routine is only called when we have urgent data
|
| 6209 | * signaled. Its the 'slow' part of tcp_urg. It could be
|
| 6210 | * moved inline now as tcp_urg is only called from one
|
| 6211 | * place. We handle URGent data wrong. We have to - as
|
| 6212 | * BSD still doesn't use the correction from RFC961.
|
| 6213 | * For 1003.1g we should support a new option TCP_STDURG to permit
|
| 6214 | * either form (or just set the sysctl tcp_stdurg).
|
| 6215 |
|
| 6216 |
|
| 6217 | static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
|
| 6218 | {
|
| 6219 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6220 | u32 ptr = ntohs(th->urg_ptr);
|
| 6221 |
|
| 6222 | if (ptr && !READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_stdurg))
|
| 6223 | ptr--;
|
| 6224 | ptr += ntohl(th->seq);
|
| 6225 |
|
| 6226 |
|
| 6227 | if (after(tp->copied_seq, ptr))
|
| 6228 | return;
|
| 6229 |
|
| 6230 | |
| 6231 | *
|
| 6232 | * NOTE: interesting situation not covered by specs.
|
| 6233 | * Misbehaving sender may send urg ptr, pointing to segment,
|
| 6234 | * which we already have in ofo queue. We are not able to fetch
|
| 6235 | * such data and will stay in TCP_URG_NOTYET until will be eaten
|
| 6236 | * by recvmsg(). Seems, we are not obliged to handle such wicked
|
| 6237 | * situations. But it is worth to think about possibility of some
|
| 6238 | * DoSes using some hypothetical application level deadlock.
|
| 6239 |
|
| 6240 | if (before(ptr, tp->rcv_nxt))
|
| 6241 | return;
|
| 6242 |
|
| 6243 |
|
| 6244 | if (tp->urg_data && !after(ptr, tp->urg_seq))
|
| 6245 | return;
|
| 6246 |
|
| 6247 |
|
| 6248 | sk_send_sigurg(sk);
|
| 6249 |
|
| 6250 | |
| 6251 | * urgent. To do this requires some care. We cannot just ignore
|
| 6252 | * tp->copied_seq since we would read the last urgent byte again
|
| 6253 | * as data, nor can we alter copied_seq until this data arrives
|
| 6254 | * or we break the semantics of SIOCATMARK (and thus sockatmark())
|
| 6255 | *
|
| 6256 | * NOTE. Double Dutch. Rendering to plain English: author of comment
|
| 6257 | * above did something sort of send("A", MSG_OOB); send("B", MSG_OOB);
|
| 6258 | * and expect that both A and B disappear from stream. This is _wrong_.
|
| 6259 | * Though this happens in BSD with high probability, this is occasional.
|
| 6260 | * Any application relying on this is buggy. Note also, that fix "works"
|
| 6261 | * only in this artificial test. Insert some normal data between A and B and we will
|
| 6262 | * decline of BSD again. Verdict: it is better to remove to trap
|
| 6263 | * buggy users.
|
| 6264 |
|
| 6265 | if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
|
| 6266 | !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
|
| 6267 | struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
|
| 6268 | tp->copied_seq++;
|
| 6269 | if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
|
| 6270 | __skb_unlink(skb, &sk->sk_receive_queue);
|
| 6271 | __kfree_skb(skb);
|
| 6272 | }
|
| 6273 | }
|
| 6274 |
|
| 6275 | WRITE_ONCE(tp->urg_data, TCP_URG_NOTYET);
|
| 6276 | WRITE_ONCE(tp->urg_seq, ptr);
|
| 6277 |
|
| 6278 |
|
| 6279 | tp->pred_flags = 0;
|
| 6280 | }
|
| 6281 |
|
| 6282 |
|
| 6283 | static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th)
|
| 6284 | {
|
| 6285 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6286 |
|
| 6287 |
|
| 6288 | if (unlikely(th->urg))
|
| 6289 | tcp_check_urg(sk, th);
|
| 6290 |
|
| 6291 |
|
| 6292 | if (unlikely(tp->urg_data == TCP_URG_NOTYET)) {
|
| 6293 | u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) -
|
| 6294 | th->syn;
|
| 6295 |
|
| 6296 |
|
| 6297 | if (ptr < skb->len) {
|
| 6298 | u8 tmp;
|
| 6299 | if (skb_copy_bits(skb, ptr, &tmp, 1))
|
| 6300 | BUG();
|
| 6301 | WRITE_ONCE(tp->urg_data, TCP_URG_VALID | tmp);
|
| 6302 | if (!sock_flag(sk, SOCK_DEAD))
|
| 6303 | READ_ONCE(sk->sk_data_ready)(sk);
|
| 6304 | }
|
| 6305 | }
|
| 6306 | }
|
| 6307 |
|
| 6308 | |
| 6309 | * When tcp connections are abruptly terminated from Mac OSX (via ^C), a
|
| 6310 | * FIN is sent followed by a RST packet. The RST is sent with the same
|
| 6311 | * sequence number as the FIN, and thus according to RFC 5961 a challenge
|
| 6312 | * ACK should be sent. However, Mac OSX rate limits replies to challenge
|
| 6313 | * ACKs on the closed socket. In addition middleboxes can drop either the
|
| 6314 | * challenge ACK or a subsequent RST.
|
| 6315 |
|
| 6316 | static bool tcp_reset_check(const struct sock *sk, const struct sk_buff *skb)
|
| 6317 | {
|
| 6318 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 6319 |
|
| 6320 | return unlikely(TCP_SKB_CB(skb)->seq == (tp->rcv_nxt - 1) &&
|
| 6321 | (1 << sk->sk_state) & (TCPF_CLOSE_WAIT | TCPF_LAST_ACK |
|
| 6322 | TCPF_CLOSING));
|
| 6323 | }
|
| 6324 |
|
| 6325 | |
| 6326 | * play significant role here.
|
| 6327 |
|
| 6328 | static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
|
| 6329 | const struct tcphdr *th, int syn_inerr)
|
| 6330 | {
|
| 6331 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6332 | bool accecn_reflector = false;
|
| 6333 | SKB_DR(reason);
|
| 6334 |
|
| 6335 |
|
| 6336 | if (!tcp_fast_parse_options(sock_net(sk), skb, th, tp) ||
|
| 6337 | !tp->rx_opt.saw_tstamp ||
|
| 6338 | tcp_paws_check(&tp->rx_opt, TCP_PAWS_WINDOW))
|
| 6339 | goto step1;
|
| 6340 |
|
| 6341 | reason = tcp_disordered_ack_check(sk, skb);
|
| 6342 | if (!reason)
|
| 6343 | goto step1;
|
| 6344 |
|
| 6345 | if (th->rst)
|
| 6346 | goto step1;
|
| 6347 | if (unlikely(th->syn))
|
| 6348 | goto syn_challenge;
|
| 6349 |
|
| 6350 | |
| 6351 | * and do not send a dupack.
|
| 6352 |
|
| 6353 | if (reason == SKB_DROP_REASON_TCP_RFC7323_PAWS_ACK) {
|
| 6354 | NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWS_OLD_ACK);
|
| 6355 | goto discard;
|
| 6356 | }
|
| 6357 | NET_INC_STATS(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
|
| 6358 | if (!tcp_oow_rate_limited(sock_net(sk), skb,
|
| 6359 | LINUX_MIB_TCPACKSKIPPEDPAWS,
|
| 6360 | &tp->last_oow_ack_time))
|
| 6361 | tcp_send_dupack(sk, skb);
|
| 6362 | goto discard;
|
| 6363 |
|
| 6364 | step1:
|
| 6365 |
|
| 6366 | reason = tcp_sequence(sk, TCP_SKB_CB(skb)->seq,
|
| 6367 | TCP_SKB_CB(skb)->end_seq, th);
|
| 6368 | if (reason) {
|
| 6369 | |
| 6370 | * (RST) segments are validated by checking their SEQ-fields."
|
| 6371 | * And page 69: "If an incoming segment is not acceptable,
|
| 6372 | * an acknowledgment should be sent in reply (unless the RST
|
| 6373 | * bit is set, if so drop the segment and return)".
|
| 6374 |
|
| 6375 | if (!th->rst) {
|
| 6376 | if (th->syn)
|
| 6377 | goto syn_challenge;
|
| 6378 |
|
| 6379 | if (reason == SKB_DROP_REASON_TCP_INVALID_SEQUENCE ||
|
| 6380 | reason == SKB_DROP_REASON_TCP_INVALID_END_SEQUENCE)
|
| 6381 | NET_INC_STATS(sock_net(sk),
|
| 6382 | LINUX_MIB_BEYOND_WINDOW);
|
| 6383 | if (!tcp_oow_rate_limited(sock_net(sk), skb,
|
| 6384 | LINUX_MIB_TCPACKSKIPPEDSEQ,
|
| 6385 | &tp->last_oow_ack_time))
|
| 6386 | tcp_send_dupack(sk, skb);
|
| 6387 | } else if (tcp_reset_check(sk, skb)) {
|
| 6388 | goto reset;
|
| 6389 | }
|
| 6390 | goto discard;
|
| 6391 | }
|
| 6392 |
|
| 6393 |
|
| 6394 | if (th->rst) {
|
| 6395 | |
| 6396 | * FIN and SACK too if available):
|
| 6397 | * If seq num matches RCV.NXT or (RCV.NXT - 1) after a FIN, or
|
| 6398 | * the right-most SACK block,
|
| 6399 | * then
|
| 6400 | * RESET the connection
|
| 6401 | * else
|
| 6402 | * Send a challenge ACK
|
| 6403 |
|
| 6404 | if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt ||
|
| 6405 | tcp_reset_check(sk, skb))
|
| 6406 | goto reset;
|
| 6407 |
|
| 6408 | if (tcp_is_sack(tp) && tp->rx_opt.num_sacks > 0) {
|
| 6409 | struct tcp_sack_block *sp = &tp->selective_acks[0];
|
| 6410 | int max_sack = sp[0].end_seq;
|
| 6411 | int this_sack;
|
| 6412 |
|
| 6413 | for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;
|
| 6414 | ++this_sack) {
|
| 6415 | max_sack = after(sp[this_sack].end_seq,
|
| 6416 | max_sack) ?
|
| 6417 | sp[this_sack].end_seq : max_sack;
|
| 6418 | }
|
| 6419 |
|
| 6420 | if (TCP_SKB_CB(skb)->seq == max_sack)
|
| 6421 | goto reset;
|
| 6422 | }
|
| 6423 |
|
| 6424 | |
| 6425 | * and no data has been received
|
| 6426 | * for current active TFO socket
|
| 6427 |
|
| 6428 | if (tp->syn_fastopen && !tp->data_segs_in &&
|
| 6429 | sk->sk_state == TCP_ESTABLISHED)
|
| 6430 | tcp_fastopen_active_disable(sk);
|
| 6431 | tcp_send_challenge_ack(sk, false);
|
| 6432 | SKB_DR_SET(reason, TCP_RESET);
|
| 6433 | goto discard;
|
| 6434 | }
|
| 6435 |
|
| 6436 |
|
| 6437 |
|
| 6438 | |
| 6439 | * RFC 5961 4.2 : Send a challenge ack
|
| 6440 |
|
| 6441 | if (th->syn) {
|
| 6442 | if (tcp_ecn_mode_accecn(tp)) {
|
| 6443 | accecn_reflector = true;
|
| 6444 | tp->syn_ect_rcv = TCP_SKB_CB(skb)->ip_dsfield &
|
| 6445 | INET_ECN_MASK;
|
| 6446 | if (tp->rx_opt.accecn &&
|
| 6447 | tp->saw_accecn_opt < TCP_ACCECN_OPT_COUNTER_SEEN) {
|
| 6448 | u8 saw_opt = tcp_accecn_option_init(skb, tp->rx_opt.accecn);
|
| 6449 |
|
| 6450 | tcp_accecn_saw_opt_fail_recv(tp, saw_opt);
|
| 6451 | tcp_accecn_opt_demand_min(sk, 1);
|
| 6452 | }
|
| 6453 | }
|
| 6454 | if (sk->sk_state == TCP_SYN_RECV && sk->sk_socket && th->ack &&
|
| 6455 | TCP_SKB_CB(skb)->seq + 1 == TCP_SKB_CB(skb)->end_seq &&
|
| 6456 | TCP_SKB_CB(skb)->seq + 1 == tp->rcv_nxt &&
|
| 6457 | TCP_SKB_CB(skb)->ack_seq == tp->snd_nxt)
|
| 6458 | goto pass;
|
| 6459 | syn_challenge:
|
| 6460 | if (syn_inerr)
|
| 6461 | TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
|
| 6462 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE);
|
| 6463 | tcp_send_challenge_ack(sk, accecn_reflector);
|
| 6464 | SKB_DR_SET(reason, TCP_INVALID_SYN);
|
| 6465 | goto discard;
|
| 6466 | }
|
| 6467 |
|
| 6468 | pass:
|
| 6469 | bpf_skops_parse_hdr(sk, skb);
|
| 6470 |
|
| 6471 | return true;
|
| 6472 |
|
| 6473 | discard:
|
| 6474 | tcp_drop_reason(sk, skb, reason);
|
| 6475 | return false;
|
| 6476 |
|
| 6477 | reset:
|
| 6478 | tcp_reset(sk, skb);
|
| 6479 | __kfree_skb(skb);
|
| 6480 | return false;
|
| 6481 | }
|
| 6482 |
|
| 6483 | |
| 6484 | * TCP receive function for the ESTABLISHED state.
|
| 6485 | *
|
| 6486 | * It is split into a fast path and a slow path. The fast path is
|
| 6487 | * disabled when:
|
| 6488 | * - A zero window was announced from us - zero window probing
|
| 6489 | * is only handled properly in the slow path.
|
| 6490 | * - Out of order segments arrived.
|
| 6491 | * - Urgent data is expected.
|
| 6492 | * - There is no buffer space left
|
| 6493 | * - Unexpected TCP flags/window values/header lengths are received
|
| 6494 | * (detected by checking the TCP header against pred_flags)
|
| 6495 | * - Data is sent in both directions. Fast path only supports pure senders
|
| 6496 | * or pure receivers (this means either the sequence number or the ack
|
| 6497 | * value must stay constant)
|
| 6498 | * - Unexpected TCP option.
|
| 6499 | * - ACK sequence number is outside [SND.UNA, SND.NXT].
|
| 6500 | *
|
| 6501 | * When these conditions are not satisfied it drops into a standard
|
| 6502 | * receive procedure patterned after RFC793 to handle all cases.
|
| 6503 | * The first three cases are guaranteed by proper pred_flags setting,
|
| 6504 | * the rest is checked inline. Fast processing is turned on in
|
| 6505 | * tcp_data_queue when everything is OK.
|
| 6506 |
|
| 6507 | void tcp_rcv_established(struct sock *sk, struct sk_buff *skb)
|
| 6508 | {
|
| 6509 | enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
|
| 6510 | const struct tcphdr *th = (const struct tcphdr *)skb->data;
|
| 6511 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6512 | unsigned int len = skb->len;
|
| 6513 |
|
| 6514 |
|
| 6515 | trace_tcp_probe(sk, skb);
|
| 6516 |
|
| 6517 | tcp_mstamp_refresh_inline(tp);
|
| 6518 | if (unlikely(!rcu_access_pointer(sk->sk_rx_dst)))
|
| 6519 | inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
|
| 6520 | |
| 6521 | * Header prediction.
|
| 6522 | * The code loosely follows the one in the famous
|
| 6523 | * "30 instruction TCP receive" Van Jacobson mail.
|
| 6524 | *
|
| 6525 | * Van's trick is to deposit buffers into socket queue
|
| 6526 | * on a device interrupt, to call tcp_recv function
|
| 6527 | * on the receive process context and checksum and copy
|
| 6528 | * the buffer to user space. smart...
|
| 6529 | *
|
| 6530 | * Our current scheme is not silly either but we take the
|
| 6531 | * extra cost of the net_bh soft interrupt processing...
|
| 6532 | * We do checksum and copy also but from device to kernel.
|
| 6533 |
|
| 6534 |
|
| 6535 | tp->rx_opt.saw_tstamp = 0;
|
| 6536 | tp->rx_opt.accecn = 0;
|
| 6537 |
|
| 6538 | |
| 6539 | * if header_prediction is to be made
|
| 6540 | * 'S' will always be tp->tcp_header_len >> 2
|
| 6541 | * '?' will be 0 for the fast path, otherwise pred_flags is 0 to
|
| 6542 | * turn it off (when there are holes in the receive
|
| 6543 | * space for instance)
|
| 6544 | * PSH flag is ignored.
|
| 6545 |
|
| 6546 |
|
| 6547 | if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
|
| 6548 | TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&
|
| 6549 | between(TCP_SKB_CB(skb)->ack_seq, tp->snd_una, tp->snd_nxt)) {
|
| 6550 | int tcp_header_len = tp->tcp_header_len;
|
| 6551 | s32 delta = 0;
|
| 6552 | int flag = 0;
|
| 6553 |
|
| 6554 | |
| 6555 | * is automatically equal to th->doff*4 due to pred_flags
|
| 6556 | * match.
|
| 6557 |
|
| 6558 |
|
| 6559 |
|
| 6560 | if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
|
| 6561 |
|
| 6562 | if (!tcp_parse_aligned_timestamp(tp, th))
|
| 6563 | goto slow_path;
|
| 6564 |
|
| 6565 | delta = tp->rx_opt.rcv_tsval -
|
| 6566 | tp->rx_opt.ts_recent;
|
| 6567 |
|
| 6568 | if (delta < 0)
|
| 6569 | goto slow_path;
|
| 6570 |
|
| 6571 | |
| 6572 | * and timestamp was corrupted part, it will result
|
| 6573 | * in a hung connection since we will drop all
|
| 6574 | * future packets due to the PAWS test.
|
| 6575 |
|
| 6576 | }
|
| 6577 |
|
| 6578 | if (len <= tcp_header_len) {
|
| 6579 |
|
| 6580 | if (len == tcp_header_len) {
|
| 6581 | |
| 6582 | * seq == rcv_nxt and rcv_wup <= rcv_nxt.
|
| 6583 | * Hence, check seq<=rcv_wup reduces to:
|
| 6584 |
|
| 6585 | if (tcp_header_len ==
|
| 6586 | (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
|
| 6587 | tp->rcv_nxt == tp->rcv_wup)
|
| 6588 | flag |= __tcp_replace_ts_recent(tp,
|
| 6589 | delta);
|
| 6590 |
|
| 6591 | tcp_ecn_received_counters(sk, skb, 0);
|
| 6592 |
|
| 6593 | |
| 6594 | * on entry.
|
| 6595 |
|
| 6596 | tcp_ack(sk, skb, flag);
|
| 6597 | __kfree_skb(skb);
|
| 6598 | tcp_data_snd_check(sk);
|
| 6599 | |
| 6600 | * last ts ecr directly instead of calling
|
| 6601 | * tcp_rcv_rtt_measure_ts()
|
| 6602 |
|
| 6603 | tp->rcv_rtt_last_tsecr = tp->rx_opt.rcv_tsecr;
|
| 6604 | return;
|
| 6605 | } else {
|
| 6606 | reason = SKB_DROP_REASON_PKT_TOO_SMALL;
|
| 6607 | TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
|
| 6608 | goto discard;
|
| 6609 | }
|
| 6610 | } else {
|
| 6611 | int eaten = 0;
|
| 6612 | bool fragstolen = false;
|
| 6613 |
|
| 6614 | if (tcp_checksum_complete(skb))
|
| 6615 | goto csum_error;
|
| 6616 |
|
| 6617 | if (after(TCP_SKB_CB(skb)->end_seq,
|
| 6618 | tp->rcv_nxt + tcp_receive_window(tp)))
|
| 6619 | goto validate;
|
| 6620 |
|
| 6621 | if ((int)skb->truesize > sk->sk_forward_alloc)
|
| 6622 | goto step5;
|
| 6623 |
|
| 6624 | |
| 6625 | * seq == rcv_nxt and rcv_wup <= rcv_nxt.
|
| 6626 | * Hence, check seq<=rcv_wup reduces to:
|
| 6627 |
|
| 6628 | if (tcp_header_len ==
|
| 6629 | (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
|
| 6630 | tp->rcv_nxt == tp->rcv_wup)
|
| 6631 | flag |= __tcp_replace_ts_recent(tp,
|
| 6632 | delta);
|
| 6633 |
|
| 6634 | tcp_rcv_rtt_measure_ts(sk, skb);
|
| 6635 |
|
| 6636 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPHPHITS);
|
| 6637 |
|
| 6638 |
|
| 6639 | tcp_cleanup_skb(skb);
|
| 6640 | __skb_pull(skb, tcp_header_len);
|
| 6641 | tcp_ecn_received_counters(sk, skb,
|
| 6642 | len - tcp_header_len);
|
| 6643 | eaten = tcp_queue_rcv(sk, skb, &fragstolen);
|
| 6644 |
|
| 6645 | tcp_event_data_recv(sk, skb);
|
| 6646 |
|
| 6647 | if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
|
| 6648 |
|
| 6649 | tcp_ack(sk, skb, flag | FLAG_DATA);
|
| 6650 | tcp_data_snd_check(sk);
|
| 6651 | if (!inet_csk_ack_scheduled(sk))
|
| 6652 | goto no_ack;
|
| 6653 | } else {
|
| 6654 | tcp_update_wl(tp, TCP_SKB_CB(skb)->seq);
|
| 6655 | }
|
| 6656 |
|
| 6657 | __tcp_ack_snd_check(sk, 0);
|
| 6658 | no_ack:
|
| 6659 | if (eaten)
|
| 6660 | kfree_skb_partial(skb, fragstolen);
|
| 6661 | tcp_data_ready(sk);
|
| 6662 | return;
|
| 6663 | }
|
| 6664 | }
|
| 6665 |
|
| 6666 | slow_path:
|
| 6667 | if (len < (th->doff << 2) || tcp_checksum_complete(skb))
|
| 6668 | goto csum_error;
|
| 6669 |
|
| 6670 | if (!th->ack && !th->rst && !th->syn) {
|
| 6671 | reason = SKB_DROP_REASON_TCP_FLAGS;
|
| 6672 | goto discard;
|
| 6673 | }
|
| 6674 |
|
| 6675 | |
| 6676 | * Standard slow path.
|
| 6677 |
|
| 6678 | validate:
|
| 6679 | if (!tcp_validate_incoming(sk, skb, th, 1))
|
| 6680 | return;
|
| 6681 |
|
| 6682 | step5:
|
| 6683 | tcp_ecn_received_counters_payload(sk, skb);
|
| 6684 |
|
| 6685 | reason = tcp_ack(sk, skb, FLAG_SLOWPATH | FLAG_UPDATE_TS_RECENT);
|
| 6686 | if ((int)reason < 0) {
|
| 6687 | reason = -reason;
|
| 6688 | goto discard;
|
| 6689 | }
|
| 6690 | tcp_rcv_rtt_measure_ts(sk, skb);
|
| 6691 |
|
| 6692 |
|
| 6693 | tcp_urg(sk, skb, th);
|
| 6694 |
|
| 6695 |
|
| 6696 | tcp_data_queue(sk, skb);
|
| 6697 |
|
| 6698 | tcp_data_snd_check(sk);
|
| 6699 | tcp_ack_snd_check(sk);
|
| 6700 | return;
|
| 6701 |
|
| 6702 | csum_error:
|
| 6703 | reason = SKB_DROP_REASON_TCP_CSUM;
|
| 6704 | trace_tcp_bad_csum(skb);
|
| 6705 | TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
|
| 6706 | TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
|
| 6707 |
|
| 6708 | discard:
|
| 6709 | tcp_drop_reason(sk, skb, reason);
|
| 6710 | }
|
| 6711 |
|
| 6712 | void tcp_init_transfer(struct sock *sk, int bpf_op, struct sk_buff *skb)
|
| 6713 | {
|
| 6714 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 6715 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6716 |
|
| 6717 | tcp_mtup_init(sk);
|
| 6718 | icsk->icsk_af_ops->rebuild_header(sk);
|
| 6719 | tcp_init_metrics(sk);
|
| 6720 |
|
| 6721 | |
| 6722 | * Cut cwnd down to 1 per RFC5681 if SYN or SYN-ACK has been
|
| 6723 | * retransmitted. In light of RFC6298 more aggressive 1sec
|
| 6724 | * initRTO, we only reset cwnd when more than 1 SYN/SYN-ACK
|
| 6725 | * retransmission has occurred.
|
| 6726 |
|
| 6727 | if (tp->total_retrans > 1 && tp->undo_marker)
|
| 6728 | tcp_snd_cwnd_set(tp, 1);
|
| 6729 | else
|
| 6730 | tcp_snd_cwnd_set(tp, tcp_init_cwnd(tp, __sk_dst_get(sk)));
|
| 6731 | tp->snd_cwnd_stamp = tcp_jiffies32;
|
| 6732 |
|
| 6733 | bpf_skops_established(sk, bpf_op, skb);
|
| 6734 |
|
| 6735 | if (!icsk->icsk_ca_initialized)
|
| 6736 | tcp_init_congestion_control(sk);
|
| 6737 | tcp_init_buffer_space(sk);
|
| 6738 | }
|
| 6739 |
|
| 6740 | void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
|
| 6741 | {
|
| 6742 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6743 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 6744 |
|
| 6745 | tcp_ao_finish_connect(sk, skb);
|
| 6746 | tcp_set_state(sk, TCP_ESTABLISHED);
|
| 6747 | icsk->icsk_ack.lrcvtime = tcp_jiffies32;
|
| 6748 |
|
| 6749 | if (skb) {
|
| 6750 | icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
|
| 6751 | security_inet_conn_established(sk, skb);
|
| 6752 | sk_mark_napi_id(sk, skb);
|
| 6753 | }
|
| 6754 |
|
| 6755 | tcp_init_transfer(sk, BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, skb);
|
| 6756 |
|
| 6757 | |
| 6758 | * packet.
|
| 6759 |
|
| 6760 | tp->lsndtime = tcp_jiffies32;
|
| 6761 |
|
| 6762 | if (sock_flag(sk, SOCK_KEEPOPEN))
|
| 6763 | tcp_reset_keepalive_timer(sk, keepalive_time_when(tp));
|
| 6764 |
|
| 6765 | if (!tp->rx_opt.snd_wscale)
|
| 6766 | __tcp_fast_path_on(tp, tp->snd_wnd);
|
| 6767 | else
|
| 6768 | tp->pred_flags = 0;
|
| 6769 | }
|
| 6770 |
|
| 6771 | static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
|
| 6772 | struct tcp_fastopen_cookie *cookie)
|
| 6773 | {
|
| 6774 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6775 | struct sk_buff *data = tp->syn_data ? tcp_rtx_queue_head(sk) : NULL;
|
| 6776 | u16 mss = tp->rx_opt.mss_clamp, try_exp = 0;
|
| 6777 | bool syn_drop = false;
|
| 6778 |
|
| 6779 | if (mss == READ_ONCE(tp->rx_opt.user_mss)) {
|
| 6780 | struct tcp_options_received opt;
|
| 6781 |
|
| 6782 |
|
| 6783 | tcp_clear_options(&opt);
|
| 6784 | opt.user_mss = opt.mss_clamp = 0;
|
| 6785 | tcp_parse_options(sock_net(sk), synack, &opt, 0, NULL);
|
| 6786 | mss = opt.mss_clamp;
|
| 6787 | }
|
| 6788 |
|
| 6789 | if (!tp->syn_fastopen) {
|
| 6790 |
|
| 6791 | cookie->len = -1;
|
| 6792 | } else if (tp->total_retrans) {
|
| 6793 | |
| 6794 | * acknowledges data. Presumably the remote received only
|
| 6795 | * the retransmitted (regular) SYNs: either the original
|
| 6796 | * SYN-data or the corresponding SYN-ACK was dropped.
|
| 6797 |
|
| 6798 | syn_drop = (cookie->len < 0 && data);
|
| 6799 | } else if (cookie->len < 0 && !tp->syn_data) {
|
| 6800 | |
| 6801 | * the (old) exp opt format then try so next time (try_exp=1).
|
| 6802 | * Otherwise we go back to use the RFC7413 opt (try_exp=2).
|
| 6803 |
|
| 6804 | try_exp = tp->syn_fastopen_exp ? 2 : 1;
|
| 6805 | }
|
| 6806 |
|
| 6807 | tcp_fastopen_cache_set(sk, mss, cookie, syn_drop, try_exp);
|
| 6808 |
|
| 6809 | if (data) {
|
| 6810 | if (tp->total_retrans)
|
| 6811 | tp->fastopen_client_fail = TFO_SYN_RETRANSMITTED;
|
| 6812 | else
|
| 6813 | tp->fastopen_client_fail = TFO_DATA_NOT_ACKED;
|
| 6814 | skb_rbtree_walk_from(data)
|
| 6815 | tcp_mark_skb_lost(sk, data);
|
| 6816 | tcp_non_congestion_loss_retransmit(sk);
|
| 6817 | NET_INC_STATS(sock_net(sk),
|
| 6818 | LINUX_MIB_TCPFASTOPENACTIVEFAIL);
|
| 6819 | return true;
|
| 6820 | }
|
| 6821 | tp->syn_data_acked = tp->syn_data;
|
| 6822 | if (tp->syn_data_acked) {
|
| 6823 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFASTOPENACTIVE);
|
| 6824 |
|
| 6825 | if (tp->delivered > 1)
|
| 6826 | WRITE_ONCE(tp->delivered, tp->delivered - 1);
|
| 6827 | }
|
| 6828 |
|
| 6829 | tcp_fastopen_add_skb(sk, synack);
|
| 6830 |
|
| 6831 | return false;
|
| 6832 | }
|
| 6833 |
|
| 6834 | static void smc_check_reset_syn(struct tcp_sock *tp)
|
| 6835 | {
|
| 6836 | #if IS_ENABLED(CONFIG_SMC)
|
| 6837 | if (static_branch_unlikely(&tcp_have_smc)) {
|
| 6838 | if (tp->syn_smc && !tp->rx_opt.smc_ok)
|
| 6839 | tp->syn_smc = 0;
|
| 6840 | }
|
| 6841 | #endif
|
| 6842 | }
|
| 6843 |
|
| 6844 | static void tcp_try_undo_spurious_syn(struct sock *sk)
|
| 6845 | {
|
| 6846 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6847 | u32 syn_stamp;
|
| 6848 |
|
| 6849 | |
| 6850 | * spurious if the ACK's timestamp option echo value matches the
|
| 6851 | * original SYN timestamp.
|
| 6852 |
|
| 6853 | syn_stamp = tp->retrans_stamp;
|
| 6854 | if (tp->undo_marker && syn_stamp && tp->rx_opt.saw_tstamp &&
|
| 6855 | syn_stamp == tp->rx_opt.rcv_tsecr)
|
| 6856 | tp->undo_marker = 0;
|
| 6857 | }
|
| 6858 |
|
| 6859 | static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
|
| 6860 | const struct tcphdr *th)
|
| 6861 | {
|
| 6862 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 6863 | struct tcp_sock *tp = tcp_sk(sk);
|
| 6864 | struct tcp_fastopen_cookie foc = { .len = -1 };
|
| 6865 | int saved_clamp = tp->rx_opt.mss_clamp;
|
| 6866 | bool fastopen_fail;
|
| 6867 | SKB_DR(reason);
|
| 6868 |
|
| 6869 | tcp_parse_options(sock_net(sk), skb, &tp->rx_opt, 0, &foc);
|
| 6870 | if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
|
| 6871 | tp->rx_opt.rcv_tsecr -= tp->tsoffset;
|
| 6872 |
|
| 6873 | if (th->ack) {
|
| 6874 | |
| 6875 | * "If the state is SYN-SENT then
|
| 6876 | * first check the ACK bit
|
| 6877 | * If the ACK bit is set
|
| 6878 | * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
|
| 6879 | * a reset (unless the RST bit is set, if so drop
|
| 6880 | * the segment and return)"
|
| 6881 |
|
| 6882 | if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
|
| 6883 | after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
|
| 6884 |
|
| 6885 | if (icsk->icsk_retransmits == 0)
|
| 6886 | tcp_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
|
| 6887 | TCP_TIMEOUT_MIN, false);
|
| 6888 | SKB_DR_SET(reason, TCP_INVALID_ACK_SEQUENCE);
|
| 6889 | goto reset_and_undo;
|
| 6890 | }
|
| 6891 |
|
| 6892 | if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
|
| 6893 | !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp,
|
| 6894 | tcp_time_stamp_ts(tp))) {
|
| 6895 | NET_INC_STATS(sock_net(sk),
|
| 6896 | LINUX_MIB_PAWSACTIVEREJECTED);
|
| 6897 | SKB_DR_SET(reason, TCP_RFC7323_PAWS);
|
| 6898 | goto reset_and_undo;
|
| 6899 | }
|
| 6900 |
|
| 6901 | |
| 6902 | *
|
| 6903 | * "If the RST bit is set
|
| 6904 | * If the ACK was acceptable then signal the user "error:
|
| 6905 | * connection reset", drop the segment, enter CLOSED state,
|
| 6906 | * delete TCB, and return."
|
| 6907 |
|
| 6908 |
|
| 6909 | if (th->rst) {
|
| 6910 | tcp_reset(sk, skb);
|
| 6911 | consume:
|
| 6912 | __kfree_skb(skb);
|
| 6913 | return 0;
|
| 6914 | }
|
| 6915 |
|
| 6916 | |
| 6917 | * "fifth, if neither of the SYN or RST bits is set then
|
| 6918 | * drop the segment and return."
|
| 6919 | *
|
| 6920 | * See note below!
|
| 6921 | * --ANK(990513)
|
| 6922 |
|
| 6923 | if (!th->syn) {
|
| 6924 | SKB_DR_SET(reason, TCP_FLAGS);
|
| 6925 | goto discard_and_undo;
|
| 6926 | }
|
| 6927 | |
| 6928 | * "If the SYN bit is on ...
|
| 6929 | * are acceptable then ...
|
| 6930 | * (our SYN has been ACKed), change the connection
|
| 6931 | * state to ESTABLISHED..."
|
| 6932 |
|
| 6933 |
|
| 6934 | if (tcp_ecn_mode_any(tp))
|
| 6935 | tcp_ecn_rcv_synack(sk, skb, th,
|
| 6936 | TCP_SKB_CB(skb)->ip_dsfield);
|
| 6937 |
|
| 6938 | tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
|
| 6939 | tcp_try_undo_spurious_syn(sk);
|
| 6940 | tcp_ack(sk, skb, FLAG_SLOWPATH);
|
| 6941 |
|
| 6942 | |
| 6943 | * move to established.
|
| 6944 |
|
| 6945 | WRITE_ONCE(tp->rcv_nxt, TCP_SKB_CB(skb)->seq + 1);
|
| 6946 | tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
|
| 6947 | tp->rcv_mwnd_seq = tp->rcv_wup + tp->rcv_wnd;
|
| 6948 |
|
| 6949 | |
| 6950 | * never scaled.
|
| 6951 |
|
| 6952 | tp->snd_wnd = ntohs(th->window);
|
| 6953 |
|
| 6954 | if (!tp->rx_opt.wscale_ok) {
|
| 6955 | tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0;
|
| 6956 | WRITE_ONCE(tp->window_clamp,
|
| 6957 | min(tp->window_clamp, 65535U));
|
| 6958 | }
|
| 6959 |
|
| 6960 | if (tp->rx_opt.saw_tstamp) {
|
| 6961 | tp->rx_opt.tstamp_ok = 1;
|
| 6962 | tp->tcp_header_len =
|
| 6963 | sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
|
| 6964 | tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
|
| 6965 | tcp_store_ts_recent(tp);
|
| 6966 | } else {
|
| 6967 | tp->tcp_header_len = sizeof(struct tcphdr);
|
| 6968 | }
|
| 6969 |
|
| 6970 | tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
|
| 6971 | tcp_initialize_rcv_mss(sk);
|
| 6972 |
|
| 6973 | |
| 6974 | * Change state from SYN-SENT only after copied_seq
|
| 6975 | * is initialized.
|
| 6976 | WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
|
| 6977 |
|
| 6978 | smc_check_reset_syn(tp);
|
| 6979 |
|
| 6980 | smp_mb();
|
| 6981 |
|
| 6982 | tcp_finish_connect(sk, skb);
|
| 6983 |
|
| 6984 | fastopen_fail = (tp->syn_fastopen || tp->syn_data) &&
|
| 6985 | tcp_rcv_fastopen_synack(sk, skb, &foc);
|
| 6986 |
|
| 6987 | if (!sock_flag(sk, SOCK_DEAD)) {
|
| 6988 | sk->sk_state_change(sk);
|
| 6989 | sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
|
| 6990 | }
|
| 6991 | if (fastopen_fail)
|
| 6992 | return -1;
|
| 6993 | if (sk->sk_write_pending ||
|
| 6994 | READ_ONCE(icsk->icsk_accept_queue.rskq_defer_accept) ||
|
| 6995 | inet_csk_in_pingpong_mode(sk)) {
|
| 6996 | |
| 6997 | * several ticks, if write_pending is set.
|
| 6998 | *
|
| 6999 | * It may be deleted, but with this feature tcpdumps
|
| 7000 | * look so _wonderfully_ clever, that I was not able
|
| 7001 | * to stand against the temptation 8) --ANK
|
| 7002 |
|
| 7003 | inet_csk_schedule_ack(sk);
|
| 7004 | tcp_enter_quickack_mode(sk, TCP_MAX_QUICKACKS);
|
| 7005 | tcp_reset_xmit_timer(sk, ICSK_TIME_DACK,
|
| 7006 | TCP_DELACK_MAX, false);
|
| 7007 | goto consume;
|
| 7008 | }
|
| 7009 | tcp_send_ack_reflect_ect(sk, tcp_ecn_mode_accecn(tp));
|
| 7010 | return -1;
|
| 7011 | }
|
| 7012 |
|
| 7013 |
|
| 7014 |
|
| 7015 | if (th->rst) {
|
| 7016 | |
| 7017 | * "If the RST bit is set
|
| 7018 | *
|
| 7019 | * Otherwise (no ACK) drop the segment and return."
|
| 7020 |
|
| 7021 | SKB_DR_SET(reason, TCP_RESET);
|
| 7022 | goto discard_and_undo;
|
| 7023 | }
|
| 7024 |
|
| 7025 |
|
| 7026 | if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp &&
|
| 7027 | tcp_paws_reject(&tp->rx_opt, 0)) {
|
| 7028 | SKB_DR_SET(reason, TCP_RFC7323_PAWS);
|
| 7029 | goto discard_and_undo;
|
| 7030 | }
|
| 7031 | if (th->syn) {
|
| 7032 | |
| 7033 | * simultaneous connect with crossed SYNs.
|
| 7034 | * Particularly, it can be connect to self.
|
| 7035 |
|
| 7036 | #ifdef CONFIG_TCP_AO
|
| 7037 | struct tcp_ao_info *ao;
|
| 7038 |
|
| 7039 | ao = rcu_dereference_protected(tp->ao_info,
|
| 7040 | lockdep_sock_is_held(sk));
|
| 7041 | if (ao) {
|
| 7042 | WRITE_ONCE(ao->risn, th->seq);
|
| 7043 | ao->rcv_sne = 0;
|
| 7044 | }
|
| 7045 | #endif
|
| 7046 | tcp_set_state(sk, TCP_SYN_RECV);
|
| 7047 |
|
| 7048 | if (tp->rx_opt.saw_tstamp) {
|
| 7049 | tp->rx_opt.tstamp_ok = 1;
|
| 7050 | tcp_store_ts_recent(tp);
|
| 7051 | tp->tcp_header_len =
|
| 7052 | sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
|
| 7053 | } else {
|
| 7054 | tp->tcp_header_len = sizeof(struct tcphdr);
|
| 7055 | }
|
| 7056 |
|
| 7057 | WRITE_ONCE(tp->rcv_nxt, TCP_SKB_CB(skb)->seq + 1);
|
| 7058 | WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
|
| 7059 | tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
|
| 7060 | tp->rcv_mwnd_seq = tp->rcv_wup + tp->rcv_wnd;
|
| 7061 |
|
| 7062 | |
| 7063 | * never scaled.
|
| 7064 |
|
| 7065 | tp->snd_wnd = ntohs(th->window);
|
| 7066 | tp->snd_wl1 = TCP_SKB_CB(skb)->seq;
|
| 7067 | tp->max_window = tp->snd_wnd;
|
| 7068 |
|
| 7069 | tcp_ecn_rcv_syn(sk, th, skb);
|
| 7070 |
|
| 7071 | tcp_mtup_init(sk);
|
| 7072 | tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
|
| 7073 | tcp_initialize_rcv_mss(sk);
|
| 7074 |
|
| 7075 | tcp_send_synack(sk);
|
| 7076 | #if 0 |
| 7077 | /* Note, we could accept data and URG from this segment.
|
| 7078 | * There are no obstacles to make this (except that we must
|
| 7079 | * either change tcp_recvmsg() to prevent it from returning data
|
| 7080 | * before 3WHS completes per RFC793, or employ TCP Fast Open).
|
| 7081 | *
|
| 7082 | * However, if we ignore data in ACKless segments sometimes,
|
| 7083 | * we have no reasons to accept it sometimes.
|
| 7084 | * Also, seems the code doing it in step6 of tcp_rcv_state_process
|
| 7085 | * is not flawless. So, discard packet for sanity.
|
| 7086 | * Uncomment this return to process the data.
|
| 7087 | */
|
| 7088 | return -1;
|
| 7089 | #else
|
| 7090 | goto consume;
|
| 7091 | #endif
|
| 7092 | }
|
| 7093 | |
| 7094 | * drop the segment and return."
|
| 7095 |
|
| 7096 |
|
| 7097 | discard_and_undo:
|
| 7098 | tcp_clear_options(&tp->rx_opt);
|
| 7099 | tp->rx_opt.mss_clamp = saved_clamp;
|
| 7100 | tcp_drop_reason(sk, skb, reason);
|
| 7101 | return 0;
|
| 7102 |
|
| 7103 | reset_and_undo:
|
| 7104 | tcp_clear_options(&tp->rx_opt);
|
| 7105 | tp->rx_opt.mss_clamp = saved_clamp;
|
| 7106 |
|
| 7107 | return reason;
|
| 7108 | }
|
| 7109 |
|
| 7110 | static void tcp_rcv_synrecv_state_fastopen(struct sock *sk)
|
| 7111 | {
|
| 7112 | struct tcp_sock *tp = tcp_sk(sk);
|
| 7113 | struct request_sock *req;
|
| 7114 |
|
| 7115 | |
| 7116 | * undo. If peer SACKs triggered fast recovery, we can't undo here.
|
| 7117 |
|
| 7118 | if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss && !tp->packets_out)
|
| 7119 | tcp_try_undo_recovery(sk);
|
| 7120 |
|
| 7121 | tcp_update_rto_time(tp);
|
| 7122 | WRITE_ONCE(inet_csk(sk)->icsk_retransmits, 0);
|
| 7123 | |
| 7124 | * retrans_stamp but don't enter CA_Loss, so in case that happened we
|
| 7125 | * need to zero retrans_stamp here to prevent spurious
|
| 7126 | * retransmits_timed_out(). However, if the ACK of our SYNACK caused us
|
| 7127 | * to enter CA_Recovery then we need to leave retrans_stamp as it was
|
| 7128 | * set entering CA_Recovery, for correct retransmits_timed_out() and
|
| 7129 | * undo behavior.
|
| 7130 |
|
| 7131 | tcp_retrans_stamp_cleanup(sk);
|
| 7132 |
|
| 7133 | |
| 7134 | * we no longer need req so release it.
|
| 7135 |
|
| 7136 | req = rcu_dereference_protected(tp->fastopen_rsk,
|
| 7137 | lockdep_sock_is_held(sk));
|
| 7138 | reqsk_fastopen_remove(sk, req, false);
|
| 7139 |
|
| 7140 | |
| 7141 | * This is similar to the regular data transmission case
|
| 7142 | * when new data has just been ack'ed.
|
| 7143 | *
|
| 7144 | * (TFO) - we could try to be more aggressive and
|
| 7145 | * retransmitting any data sooner based on when they
|
| 7146 | * are sent out.
|
| 7147 |
|
| 7148 | tcp_rearm_rto(sk);
|
| 7149 | }
|
| 7150 |
|
| 7151 | |
| 7152 | * This function implements the receiving procedure of RFC 793 for
|
| 7153 | * all states except ESTABLISHED and TIME_WAIT.
|
| 7154 | * It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
|
| 7155 | * address independent.
|
| 7156 |
|
| 7157 |
|
| 7158 | enum skb_drop_reason
|
| 7159 | tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb)
|
| 7160 | {
|
| 7161 | struct tcp_sock *tp = tcp_sk(sk);
|
| 7162 | struct inet_connection_sock *icsk = inet_csk(sk);
|
| 7163 | const struct tcphdr *th = tcp_hdr(skb);
|
| 7164 | struct request_sock *req;
|
| 7165 | int queued = 0;
|
| 7166 | SKB_DR(reason);
|
| 7167 |
|
| 7168 | switch (sk->sk_state) {
|
| 7169 | case TCP_CLOSE:
|
| 7170 | SKB_DR_SET(reason, TCP_CLOSE);
|
| 7171 | goto discard;
|
| 7172 |
|
| 7173 | case TCP_LISTEN:
|
| 7174 | if (th->ack)
|
| 7175 | return SKB_DROP_REASON_TCP_FLAGS;
|
| 7176 |
|
| 7177 | if (th->rst) {
|
| 7178 | SKB_DR_SET(reason, TCP_RESET);
|
| 7179 | goto discard;
|
| 7180 | }
|
| 7181 | if (th->syn) {
|
| 7182 | if (th->fin) {
|
| 7183 | SKB_DR_SET(reason, TCP_FLAGS);
|
| 7184 | goto discard;
|
| 7185 | }
|
| 7186 | |
| 7187 | * so we need to make sure to disable BH and RCU right there.
|
| 7188 |
|
| 7189 | rcu_read_lock();
|
| 7190 | local_bh_disable();
|
| 7191 | icsk->icsk_af_ops->conn_request(sk, skb);
|
| 7192 | local_bh_enable();
|
| 7193 | rcu_read_unlock();
|
| 7194 |
|
| 7195 | consume_skb(skb);
|
| 7196 | return 0;
|
| 7197 | }
|
| 7198 | SKB_DR_SET(reason, TCP_FLAGS);
|
| 7199 | goto discard;
|
| 7200 |
|
| 7201 | case TCP_SYN_SENT:
|
| 7202 | tp->rx_opt.saw_tstamp = 0;
|
| 7203 | tcp_mstamp_refresh(tp);
|
| 7204 | queued = tcp_rcv_synsent_state_process(sk, skb, th);
|
| 7205 | if (queued >= 0)
|
| 7206 | return queued;
|
| 7207 |
|
| 7208 |
|
| 7209 | tcp_urg(sk, skb, th);
|
| 7210 | __kfree_skb(skb);
|
| 7211 | tcp_data_snd_check(sk);
|
| 7212 | return 0;
|
| 7213 | }
|
| 7214 |
|
| 7215 | tcp_mstamp_refresh(tp);
|
| 7216 | tp->rx_opt.saw_tstamp = 0;
|
| 7217 | req = rcu_dereference_protected(tp->fastopen_rsk,
|
| 7218 | lockdep_sock_is_held(sk));
|
| 7219 | if (req) {
|
| 7220 | bool req_stolen;
|
| 7221 |
|
| 7222 | WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
|
| 7223 | sk->sk_state != TCP_FIN_WAIT1);
|
| 7224 |
|
| 7225 | SKB_DR_SET(reason, TCP_FASTOPEN);
|
| 7226 | if (!tcp_check_req(sk, skb, req, true, &req_stolen, &reason))
|
| 7227 | goto discard;
|
| 7228 | }
|
| 7229 |
|
| 7230 | if (!th->ack && !th->rst && !th->syn) {
|
| 7231 | SKB_DR_SET(reason, TCP_FLAGS);
|
| 7232 | goto discard;
|
| 7233 | }
|
| 7234 | if (!tcp_validate_incoming(sk, skb, th, 0))
|
| 7235 | return 0;
|
| 7236 |
|
| 7237 |
|
| 7238 | reason = tcp_ack(sk, skb, FLAG_SLOWPATH |
|
| 7239 | FLAG_UPDATE_TS_RECENT |
|
| 7240 | FLAG_NO_CHALLENGE_ACK);
|
| 7241 |
|
| 7242 | if ((int)reason <= 0) {
|
| 7243 | if (sk->sk_state == TCP_SYN_RECV) {
|
| 7244 |
|
| 7245 | if (!reason)
|
| 7246 | return SKB_DROP_REASON_TCP_OLD_ACK;
|
| 7247 | return -reason;
|
| 7248 | }
|
| 7249 |
|
| 7250 | if ((int)reason < 0) {
|
| 7251 | tcp_send_challenge_ack(sk, false);
|
| 7252 | reason = -reason;
|
| 7253 | goto discard;
|
| 7254 | }
|
| 7255 | }
|
| 7256 | SKB_DR_SET(reason, NOT_SPECIFIED);
|
| 7257 | switch (sk->sk_state) {
|
| 7258 | case TCP_SYN_RECV:
|
| 7259 | WRITE_ONCE(tp->delivered, tp->delivered + 1);
|
| 7260 | if (!tp->srtt_us)
|
| 7261 | tcp_synack_rtt_meas(sk, req);
|
| 7262 |
|
| 7263 | if (tp->rx_opt.tstamp_ok)
|
| 7264 | tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
|
| 7265 |
|
| 7266 | if (req) {
|
| 7267 | tcp_rcv_synrecv_state_fastopen(sk);
|
| 7268 | } else {
|
| 7269 | tcp_try_undo_spurious_syn(sk);
|
| 7270 | tp->retrans_stamp = 0;
|
| 7271 | tcp_init_transfer(sk, BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB,
|
| 7272 | skb);
|
| 7273 | WRITE_ONCE(tp->copied_seq, tp->rcv_nxt);
|
| 7274 | }
|
| 7275 | tcp_ao_established(sk);
|
| 7276 | smp_mb();
|
| 7277 | tcp_set_state(sk, TCP_ESTABLISHED);
|
| 7278 | sk->sk_state_change(sk);
|
| 7279 |
|
| 7280 | |
| 7281 | * Passively open sockets are not waked up, because
|
| 7282 | * sk->sk_sleep == NULL and sk->sk_socket == NULL.
|
| 7283 |
|
| 7284 | if (sk->sk_socket)
|
| 7285 | sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
|
| 7286 |
|
| 7287 | WRITE_ONCE(tp->snd_una, TCP_SKB_CB(skb)->ack_seq);
|
| 7288 | tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
|
| 7289 | tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
|
| 7290 |
|
| 7291 | if (!inet_csk(sk)->icsk_ca_ops->cong_control)
|
| 7292 | tcp_update_pacing_rate(sk);
|
| 7293 |
|
| 7294 |
|
| 7295 | tp->lsndtime = tcp_jiffies32;
|
| 7296 |
|
| 7297 | tcp_initialize_rcv_mss(sk);
|
| 7298 | if (tcp_ecn_mode_accecn(tp))
|
| 7299 | tcp_accecn_third_ack(sk, skb, tp->syn_ect_snt);
|
| 7300 | tcp_fast_path_on(tp);
|
| 7301 | if (sk->sk_shutdown & SEND_SHUTDOWN)
|
| 7302 | tcp_shutdown(sk, SEND_SHUTDOWN);
|
| 7303 |
|
| 7304 | break;
|
| 7305 |
|
| 7306 | case TCP_FIN_WAIT1: {
|
| 7307 | int tmo;
|
| 7308 |
|
| 7309 | if (req)
|
| 7310 | tcp_rcv_synrecv_state_fastopen(sk);
|
| 7311 |
|
| 7312 | if (tp->snd_una != tp->write_seq)
|
| 7313 | break;
|
| 7314 |
|
| 7315 | tcp_set_state(sk, TCP_FIN_WAIT2);
|
| 7316 | WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | SEND_SHUTDOWN);
|
| 7317 |
|
| 7318 | sk_dst_confirm(sk);
|
| 7319 |
|
| 7320 | if (!sock_flag(sk, SOCK_DEAD)) {
|
| 7321 |
|
| 7322 | sk->sk_state_change(sk);
|
| 7323 | break;
|
| 7324 | }
|
| 7325 |
|
| 7326 | if (READ_ONCE(tp->linger2) < 0) {
|
| 7327 | tcp_done(sk);
|
| 7328 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
|
| 7329 | return SKB_DROP_REASON_TCP_ABORT_ON_DATA;
|
| 7330 | }
|
| 7331 | if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
|
| 7332 | after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
|
| 7333 |
|
| 7334 | if (tp->syn_fastopen && th->fin)
|
| 7335 | tcp_fastopen_active_disable(sk);
|
| 7336 | tcp_done(sk);
|
| 7337 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
|
| 7338 | return SKB_DROP_REASON_TCP_ABORT_ON_DATA;
|
| 7339 | }
|
| 7340 |
|
| 7341 | tmo = tcp_fin_time(sk);
|
| 7342 | if (tmo > TCP_TIMEWAIT_LEN) {
|
| 7343 | tcp_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
|
| 7344 | } else if (th->fin || sock_owned_by_user(sk)) {
|
| 7345 | |
| 7346 | * It is not a big problem, but it looks confusing
|
| 7347 | * and not so rare event. We still can lose it now,
|
| 7348 | * if it spins in bh_lock_sock(), but it is really
|
| 7349 | * marginal case.
|
| 7350 |
|
| 7351 | tcp_reset_keepalive_timer(sk, tmo);
|
| 7352 | } else {
|
| 7353 | tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
|
| 7354 | goto consume;
|
| 7355 | }
|
| 7356 | break;
|
| 7357 | }
|
| 7358 |
|
| 7359 | case TCP_CLOSING:
|
| 7360 | if (tp->snd_una == tp->write_seq) {
|
| 7361 | tcp_time_wait(sk, TCP_TIME_WAIT, 0);
|
| 7362 | goto consume;
|
| 7363 | }
|
| 7364 | break;
|
| 7365 |
|
| 7366 | case TCP_LAST_ACK:
|
| 7367 | if (tp->snd_una == tp->write_seq) {
|
| 7368 | tcp_update_metrics(sk);
|
| 7369 | tcp_done(sk);
|
| 7370 | goto consume;
|
| 7371 | }
|
| 7372 | break;
|
| 7373 | }
|
| 7374 |
|
| 7375 |
|
| 7376 | tcp_urg(sk, skb, th);
|
| 7377 |
|
| 7378 |
|
| 7379 | switch (sk->sk_state) {
|
| 7380 | case TCP_CLOSE_WAIT:
|
| 7381 | case TCP_CLOSING:
|
| 7382 | case TCP_LAST_ACK:
|
| 7383 | if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
|
| 7384 | |
| 7385 | * continue to be processed, drop the packet.
|
| 7386 |
|
| 7387 | if (sk_is_mptcp(sk) && !mptcp_incoming_options(sk, skb))
|
| 7388 | goto discard;
|
| 7389 | break;
|
| 7390 | }
|
| 7391 | fallthrough;
|
| 7392 | case TCP_FIN_WAIT1:
|
| 7393 | case TCP_FIN_WAIT2:
|
| 7394 | |
| 7395 | * RFC 1122 says we MUST send a reset.
|
| 7396 | * BSD 4.4 also does reset.
|
| 7397 |
|
| 7398 | if (sk->sk_shutdown & RCV_SHUTDOWN) {
|
| 7399 | if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
|
| 7400 | after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
|
| 7401 | NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
|
| 7402 | tcp_reset(sk, skb);
|
| 7403 | return SKB_DROP_REASON_TCP_ABORT_ON_DATA;
|
| 7404 | }
|
| 7405 | }
|
| 7406 | fallthrough;
|
| 7407 | case TCP_ESTABLISHED:
|
| 7408 | tcp_data_queue(sk, skb);
|
| 7409 | queued = 1;
|
| 7410 | break;
|
| 7411 | }
|
| 7412 |
|
| 7413 |
|
| 7414 | if (sk->sk_state != TCP_CLOSE) {
|
| 7415 | tcp_data_snd_check(sk);
|
| 7416 | tcp_ack_snd_check(sk);
|
| 7417 | }
|
| 7418 |
|
| 7419 | if (!queued) {
|
| 7420 | discard:
|
| 7421 | tcp_drop_reason(sk, skb, reason);
|
| 7422 | }
|
| 7423 | return 0;
|
| 7424 |
|
| 7425 | consume:
|
| 7426 | __kfree_skb(skb);
|
| 7427 | return 0;
|
| 7428 | }
|
| 7429 |
|
| 7430 | static inline void pr_drop_req(struct request_sock *req, __u16 port, int family)
|
| 7431 | {
|
| 7432 | struct inet_request_sock *ireq = inet_rsk(req);
|
| 7433 |
|
| 7434 | if (family == AF_INET)
|
| 7435 | net_dbg_ratelimited("drop open request from %pI4/%u\n",
|
| 7436 | &ireq->ir_rmt_addr, port);
|
| 7437 | #if IS_ENABLED(CONFIG_IPV6)
|
| 7438 | else if (family == AF_INET6)
|
| 7439 | net_dbg_ratelimited("drop open request from %pI6/%u\n",
|
| 7440 | &ireq->ir_v6_rmt_addr, port);
|
| 7441 | #endif
|
| 7442 | }
|
| 7443 |
|
| 7444 | |
| 7445 | *
|
| 7446 | * If we receive a SYN packet with these bits set, it means a
|
| 7447 | * network is playing bad games with TOS bits. In order to
|
| 7448 | * avoid possible false congestion notifications, we disable
|
| 7449 | * TCP ECN negotiation.
|
| 7450 | *
|
| 7451 | * Exception: tcp_ca wants ECN. This is required for DCTCP
|
| 7452 | * congestion control: Linux DCTCP asserts ECT on all packets,
|
| 7453 | * including SYN, which is most optimal solution; however,
|
| 7454 | * others, such as FreeBSD do not.
|
| 7455 | *
|
| 7456 | * Exception: At least one of the reserved bits of the TCP header (th->res1) is
|
| 7457 | * set, indicating the use of a future TCP extension (such as AccECN). See
|
| 7458 | * RFC8311 §4.3 which updates RFC3168 to allow the development of such
|
| 7459 | * extensions.
|
| 7460 |
|
| 7461 | static void tcp_ecn_create_request(struct request_sock *req,
|
| 7462 | const struct sk_buff *skb,
|
| 7463 | const struct sock *listen_sk,
|
| 7464 | const struct dst_entry *dst)
|
| 7465 | {
|
| 7466 | const struct tcphdr *th = tcp_hdr(skb);
|
| 7467 | const struct net *net = sock_net(listen_sk);
|
| 7468 | bool th_ecn = th->ece && th->cwr;
|
| 7469 | bool ect, ecn_ok;
|
| 7470 | u32 ecn_ok_dst;
|
| 7471 |
|
| 7472 | if (tcp_accecn_syn_requested(th) &&
|
| 7473 | (READ_ONCE(net->ipv4.sysctl_tcp_ecn) >= 3 ||
|
| 7474 | tcp_ca_needs_accecn(listen_sk))) {
|
| 7475 | inet_rsk(req)->ecn_ok = 1;
|
| 7476 | tcp_rsk(req)->accecn_ok = 1;
|
| 7477 | tcp_rsk(req)->syn_ect_rcv = TCP_SKB_CB(skb)->ip_dsfield &
|
| 7478 | INET_ECN_MASK;
|
| 7479 | return;
|
| 7480 | }
|
| 7481 |
|
| 7482 | if (!th_ecn)
|
| 7483 | return;
|
| 7484 |
|
| 7485 | ect = !INET_ECN_is_not_ect(TCP_SKB_CB(skb)->ip_dsfield);
|
| 7486 | ecn_ok_dst = dst_feature(dst, DST_FEATURE_ECN_MASK);
|
| 7487 | ecn_ok = READ_ONCE(net->ipv4.sysctl_tcp_ecn) || ecn_ok_dst;
|
| 7488 |
|
| 7489 | if (((!ect || th->res1 || th->ae) && ecn_ok) ||
|
| 7490 | tcp_ca_needs_ecn(listen_sk) ||
|
| 7491 | (ecn_ok_dst & DST_FEATURE_ECN_CA) ||
|
| 7492 | tcp_bpf_ca_needs_ecn((struct sock *)req))
|
| 7493 | inet_rsk(req)->ecn_ok = 1;
|
| 7494 | }
|
| 7495 |
|
| 7496 | static void tcp_openreq_init(struct request_sock *req,
|
| 7497 | const struct tcp_options_received *rx_opt,
|
| 7498 | struct sk_buff *skb, const struct sock *sk)
|
| 7499 | {
|
| 7500 | struct inet_request_sock *ireq = inet_rsk(req);
|
| 7501 |
|
| 7502 | req->rsk_rcv_wnd = 0;
|
| 7503 | tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
|
| 7504 | tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
|
| 7505 | tcp_rsk(req)->snt_synack = 0;
|
| 7506 | tcp_rsk(req)->snt_tsval_first = 0;
|
| 7507 | tcp_rsk(req)->last_oow_ack_time = 0;
|
| 7508 | tcp_rsk(req)->accecn_ok = 0;
|
| 7509 | tcp_rsk(req)->saw_accecn_opt = TCP_ACCECN_OPT_NOT_SEEN;
|
| 7510 | tcp_rsk(req)->accecn_fail_mode = 0;
|
| 7511 | tcp_rsk(req)->syn_ect_rcv = 0;
|
| 7512 | tcp_rsk(req)->syn_ect_snt = 0;
|
| 7513 | req->mss = rx_opt->mss_clamp;
|
| 7514 | req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
|
| 7515 | ireq->tstamp_ok = rx_opt->tstamp_ok;
|
| 7516 | ireq->sack_ok = rx_opt->sack_ok;
|
| 7517 | ireq->snd_wscale = rx_opt->snd_wscale;
|
| 7518 | ireq->wscale_ok = rx_opt->wscale_ok;
|
| 7519 | ireq->acked = 0;
|
| 7520 | ireq->ecn_ok = 0;
|
| 7521 | ireq->ir_rmt_port = tcp_hdr(skb)->source;
|
| 7522 | ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
|
| 7523 | ireq->ir_mark = inet_request_mark(sk, skb);
|
| 7524 | #if IS_ENABLED(CONFIG_SMC)
|
| 7525 | ireq->smc_ok = rx_opt->smc_ok && !(tcp_sk(sk)->smc_hs_congested &&
|
| 7526 | tcp_sk(sk)->smc_hs_congested(sk));
|
| 7527 | #endif
|
| 7528 | }
|
| 7529 |
|
| 7530 | |
| 7531 | * Return true if a syncookie should be sent
|
| 7532 |
|
| 7533 | static bool tcp_syn_flood_action(struct sock *sk, const char *proto)
|
| 7534 | {
|
| 7535 | struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
|
| 7536 | const char *msg = "Dropping request";
|
| 7537 | struct net *net = sock_net(sk);
|
| 7538 | bool want_cookie = false;
|
| 7539 | u8 syncookies;
|
| 7540 |
|
| 7541 | syncookies = READ_ONCE(net->ipv4.sysctl_tcp_syncookies);
|
| 7542 |
|
| 7543 | #ifdef CONFIG_SYN_COOKIES
|
| 7544 | if (syncookies) {
|
| 7545 | msg = "Sending cookies";
|
| 7546 | want_cookie = true;
|
| 7547 | __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
|
| 7548 | } else
|
| 7549 | #endif
|
| 7550 | __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
|
| 7551 |
|
| 7552 | if (syncookies != 2 && !READ_ONCE(queue->synflood_warned)) {
|
| 7553 | WRITE_ONCE(queue->synflood_warned, 1);
|
| 7554 | if (IS_ENABLED(CONFIG_IPV6) && sk->sk_family == AF_INET6) {
|
| 7555 | net_info_ratelimited("%s: Possible SYN flooding on port [%pI6c]:%u. %s.\n",
|
| 7556 | proto, inet6_rcv_saddr(sk),
|
| 7557 | sk->sk_num, msg);
|
| 7558 | } else {
|
| 7559 | net_info_ratelimited("%s: Possible SYN flooding on port %pI4:%u. %s.\n",
|
| 7560 | proto, &sk->sk_rcv_saddr,
|
| 7561 | sk->sk_num, msg);
|
| 7562 | }
|
| 7563 | }
|
| 7564 |
|
| 7565 | return want_cookie;
|
| 7566 | }
|
| 7567 |
|
| 7568 | static void tcp_reqsk_record_syn(const struct sock *sk,
|
| 7569 | struct request_sock *req,
|
| 7570 | const struct sk_buff *skb)
|
| 7571 | {
|
| 7572 | if (tcp_sk(sk)->save_syn) {
|
| 7573 | u32 len = skb_network_header_len(skb) + tcp_hdrlen(skb);
|
| 7574 | struct saved_syn *saved_syn;
|
| 7575 | u32 mac_hdrlen;
|
| 7576 | void *base;
|
| 7577 |
|
| 7578 | if (tcp_sk(sk)->save_syn == 2) {
|
| 7579 | base = skb_mac_header(skb);
|
| 7580 | mac_hdrlen = skb_mac_header_len(skb);
|
| 7581 | len += mac_hdrlen;
|
| 7582 | } else {
|
| 7583 | base = skb_network_header(skb);
|
| 7584 | mac_hdrlen = 0;
|
| 7585 | }
|
| 7586 |
|
| 7587 | saved_syn = kmalloc_flex(*saved_syn, data, len, GFP_ATOMIC);
|
| 7588 | if (saved_syn) {
|
| 7589 | saved_syn->mac_hdrlen = mac_hdrlen;
|
| 7590 | saved_syn->network_hdrlen = skb_network_header_len(skb);
|
| 7591 | saved_syn->tcp_hdrlen = tcp_hdrlen(skb);
|
| 7592 | memcpy(saved_syn->data, base, len);
|
| 7593 | req->saved_syn = saved_syn;
|
| 7594 | }
|
| 7595 | }
|
| 7596 | }
|
| 7597 |
|
| 7598 | |
| 7599 | * used for SYN cookie generation.
|
| 7600 |
|
| 7601 | u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops,
|
| 7602 | const struct tcp_request_sock_ops *af_ops,
|
| 7603 | struct sock *sk, struct tcphdr *th)
|
| 7604 | {
|
| 7605 | struct tcp_sock *tp = tcp_sk(sk);
|
| 7606 | u16 mss;
|
| 7607 |
|
| 7608 | if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_syncookies) != 2 &&
|
| 7609 | !inet_csk_reqsk_queue_is_full(sk))
|
| 7610 | return 0;
|
| 7611 |
|
| 7612 | if (!tcp_syn_flood_action(sk, rsk_ops->slab_name))
|
| 7613 | return 0;
|
| 7614 |
|
| 7615 | if (sk_acceptq_is_full(sk)) {
|
| 7616 | NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
|
| 7617 | return 0;
|
| 7618 | }
|
| 7619 |
|
| 7620 | mss = tcp_parse_mss_option(th, READ_ONCE(tp->rx_opt.user_mss));
|
| 7621 | if (!mss)
|
| 7622 | mss = af_ops->mss_clamp;
|
| 7623 |
|
| 7624 | return mss;
|
| 7625 | }
|
| 7626 |
|
| 7627 | int tcp_conn_request(struct request_sock_ops *rsk_ops,
|
| 7628 | const struct tcp_request_sock_ops *af_ops,
|
| 7629 | struct sock *sk, struct sk_buff *skb)
|
| 7630 | {
|
| 7631 | struct tcp_fastopen_cookie foc = { .len = -1 };
|
| 7632 | u32 isn = TCP_SKB_CB(skb)->tcp_tw_isn;
|
| 7633 | struct tcp_options_received tmp_opt;
|
| 7634 | const struct tcp_sock *tp = tcp_sk(sk);
|
| 7635 | struct net *net = sock_net(sk);
|
| 7636 | struct sock *fastopen_sk = NULL;
|
| 7637 | union tcp_seq_and_ts_off st;
|
| 7638 | struct request_sock *req;
|
| 7639 | bool want_cookie = false;
|
| 7640 | struct dst_entry *dst;
|
| 7641 | struct flowi fl;
|
| 7642 | u8 syncookies;
|
| 7643 |
|
| 7644 | #ifdef CONFIG_TCP_AO
|
| 7645 | const struct tcp_ao_hdr *aoh;
|
| 7646 | #endif
|
| 7647 |
|
| 7648 | |
| 7649 | * TW sockets are converted to open requests without limitations,
|
| 7650 | * we skip the queue limits and syncookie checks in the block below.
|
| 7651 |
|
| 7652 | if (!isn) {
|
| 7653 | syncookies = READ_ONCE(net->ipv4.sysctl_tcp_syncookies);
|
| 7654 |
|
| 7655 | if (syncookies == 2 || inet_csk_reqsk_queue_is_full(sk)) {
|
| 7656 | want_cookie = tcp_syn_flood_action(sk,
|
| 7657 | rsk_ops->slab_name);
|
| 7658 | if (!want_cookie)
|
| 7659 | goto drop;
|
| 7660 | }
|
| 7661 | }
|
| 7662 |
|
| 7663 | if (sk_acceptq_is_full(sk)) {
|
| 7664 | NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
|
| 7665 | goto drop;
|
| 7666 | }
|
| 7667 |
|
| 7668 | req = inet_reqsk_alloc(rsk_ops, sk, !want_cookie);
|
| 7669 | if (!req)
|
| 7670 | goto drop;
|
| 7671 |
|
| 7672 | req->syncookie = want_cookie;
|
| 7673 | tcp_rsk(req)->af_specific = af_ops;
|
| 7674 | tcp_rsk(req)->ts_off = 0;
|
| 7675 | tcp_rsk(req)->req_usec_ts = false;
|
| 7676 | tcp_rsk(req)->txhash = net_tx_rndhash();
|
| 7677 | #if IS_ENABLED(CONFIG_MPTCP)
|
| 7678 | tcp_rsk(req)->is_mptcp = 0;
|
| 7679 | #endif
|
| 7680 |
|
| 7681 | tcp_clear_options(&tmp_opt);
|
| 7682 | tmp_opt.mss_clamp = af_ops->mss_clamp;
|
| 7683 | tmp_opt.user_mss = READ_ONCE(tp->rx_opt.user_mss);
|
| 7684 | tcp_parse_options(sock_net(sk), skb, &tmp_opt, 0,
|
| 7685 | want_cookie ? NULL : &foc);
|
| 7686 |
|
| 7687 | if (want_cookie && !tmp_opt.saw_tstamp)
|
| 7688 | tcp_clear_options(&tmp_opt);
|
| 7689 |
|
| 7690 | if (IS_ENABLED(CONFIG_SMC) && want_cookie)
|
| 7691 | tmp_opt.smc_ok = 0;
|
| 7692 |
|
| 7693 | tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
|
| 7694 | tcp_openreq_init(req, &tmp_opt, skb, sk);
|
| 7695 | inet_rsk(req)->no_srccheck = inet_test_bit(TRANSPARENT, sk);
|
| 7696 |
|
| 7697 |
|
| 7698 | inet_rsk(req)->ir_iif = inet_request_bound_dev_if(sk, skb);
|
| 7699 |
|
| 7700 | if (want_cookie) {
|
| 7701 | isn = cookie_init_sequence(af_ops, skb, &req->mss);
|
| 7702 | |
| 7703 | * socket make the same egress choice (IPv6 ECMP path; IPv4 TX queue).
|
| 7704 |
|
| 7705 | tcp_rsk(req)->txhash = isn;
|
| 7706 | }
|
| 7707 |
|
| 7708 | dst = af_ops->route_req(sk, skb, &fl, req, want_cookie ? 0 : isn);
|
| 7709 | if (!dst)
|
| 7710 | goto drop_and_free;
|
| 7711 |
|
| 7712 | if (tmp_opt.tstamp_ok || (!want_cookie && !isn))
|
| 7713 | st = INDIRECT_CALL_INET(af_ops->init_seq_and_ts_off,
|
| 7714 | tcp_v6_init_seq_and_ts_off,
|
| 7715 | tcp_v4_init_seq_and_ts_off,
|
| 7716 | net, skb);
|
| 7717 |
|
| 7718 | if (tmp_opt.tstamp_ok) {
|
| 7719 | tcp_rsk(req)->req_usec_ts = dst_tcp_usec_ts(dst);
|
| 7720 | tcp_rsk(req)->ts_off = st.ts_off;
|
| 7721 | }
|
| 7722 | if (!want_cookie && !isn) {
|
| 7723 | int max_syn_backlog = READ_ONCE(net->ipv4.sysctl_max_syn_backlog);
|
| 7724 |
|
| 7725 |
|
| 7726 | if (!syncookies &&
|
| 7727 | (max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
|
| 7728 | (max_syn_backlog >> 2)) &&
|
| 7729 | !tcp_peer_is_proven(req, dst)) {
|
| 7730 | |
| 7731 | * backlog is filled with destinations,
|
| 7732 | * proven to be alive.
|
| 7733 | * It means that we continue to communicate
|
| 7734 | * to destinations, already remembered
|
| 7735 | * to the moment of synflood.
|
| 7736 |
|
| 7737 | pr_drop_req(req, ntohs(tcp_hdr(skb)->source),
|
| 7738 | rsk_ops->family);
|
| 7739 | goto drop_and_release;
|
| 7740 | }
|
| 7741 |
|
| 7742 | isn = st.seq;
|
| 7743 | }
|
| 7744 |
|
| 7745 | tcp_ecn_create_request(req, skb, sk, dst);
|
| 7746 |
|
| 7747 | if (want_cookie) {
|
| 7748 | cookie_record_sent(sk);
|
| 7749 | if (!tmp_opt.tstamp_ok)
|
| 7750 | inet_rsk(req)->ecn_ok = 0;
|
| 7751 | }
|
| 7752 |
|
| 7753 | #ifdef CONFIG_TCP_AO
|
| 7754 | if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh))
|
| 7755 | goto drop_and_release;
|
| 7756 | if (aoh) {
|
| 7757 | tcp_rsk(req)->used_tcp_ao = true;
|
| 7758 | tcp_rsk(req)->ao_rcv_next = aoh->keyid;
|
| 7759 | tcp_rsk(req)->ao_keyid = aoh->rnext_keyid;
|
| 7760 |
|
| 7761 | } else {
|
| 7762 | tcp_rsk(req)->used_tcp_ao = false;
|
| 7763 | }
|
| 7764 | #endif
|
| 7765 | tcp_rsk(req)->snt_isn = isn;
|
| 7766 | tcp_rsk(req)->syn_tos = TCP_SKB_CB(skb)->ip_dsfield;
|
| 7767 | tcp_openreq_init_rwin(req, sk, dst);
|
| 7768 | sk_rx_queue_set(req_to_sk(req), skb);
|
| 7769 | if (!want_cookie) {
|
| 7770 | tcp_reqsk_record_syn(sk, req, skb);
|
| 7771 | fastopen_sk = tcp_try_fastopen(sk, skb, req, &foc, dst);
|
| 7772 | }
|
| 7773 | if (fastopen_sk) {
|
| 7774 | af_ops->send_synack(fastopen_sk, dst, &fl, req,
|
| 7775 | &foc, TCP_SYNACK_FASTOPEN, skb);
|
| 7776 |
|
| 7777 | if (!inet_csk_reqsk_queue_add(sk, req, fastopen_sk)) {
|
| 7778 | bh_unlock_sock(fastopen_sk);
|
| 7779 | sock_put(fastopen_sk);
|
| 7780 | goto drop_and_free;
|
| 7781 | }
|
| 7782 | READ_ONCE(sk->sk_data_ready)(sk);
|
| 7783 | bh_unlock_sock(fastopen_sk);
|
| 7784 | sock_put(fastopen_sk);
|
| 7785 | } else {
|
| 7786 | tcp_rsk(req)->tfo_listener = false;
|
| 7787 | if (!want_cookie &&
|
| 7788 | unlikely(!inet_csk_reqsk_queue_hash_add(sk, req))) {
|
| 7789 | reqsk_free(req);
|
| 7790 | dst_release(dst);
|
| 7791 | return 0;
|
| 7792 | }
|
| 7793 | af_ops->send_synack(sk, dst, &fl, req, &foc,
|
| 7794 | !want_cookie ? TCP_SYNACK_NORMAL :
|
| 7795 | TCP_SYNACK_COOKIE,
|
| 7796 | skb);
|
| 7797 | if (want_cookie) {
|
| 7798 | reqsk_free(req);
|
| 7799 | return 0;
|
| 7800 | }
|
| 7801 | }
|
| 7802 | reqsk_put(req);
|
| 7803 | return 0;
|
| 7804 |
|
| 7805 | drop_and_release:
|
| 7806 | dst_release(dst);
|
| 7807 | drop_and_free:
|
| 7808 | __reqsk_free(req);
|
| 7809 | drop:
|
| 7810 | tcp_listendrop(sk);
|
| 7811 | return 0;
|
| 7812 | }
|