1 // SPDX-License-Identifier: GPL-2.0-only 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, <waltje@uWalt.NL.Mugnet.ORG> 11 * Mark Evans, <evansmp@uhura.aston.ac.uk> 12 * Corey Minyard <wf-rch!minyard@relay.EU.net> 13 * Florian La Roche, <flla@stud.uni-sb.de> 14 * Charles Hedrick, <hedrick@klinzhai.rutgers.edu> 15 * Linus Torvalds, <torvalds@cs.helsinki.fi> 16 * Alan Cox, <gw4pts@gw4pts.ampr.org> 17 * Matthew Dillon, <dillon@apollo.west.oic.com> 18 * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 19 * Jorge Cwik, <jorge@laser.satlink.net> 20 */ 21 22 /* 23 * Changes: Pedro Roque : Retransmit queue handled by TCP. 24 * : Fragmentation on mtu decrease 25 * : Segment collapse on retransmit 26 * : AF independence 27 * 28 * Linus Torvalds : send_delayed_ack 29 * David S. Miller : Charge memory using the right skb 30 * during syn/ack processing. 31 * David S. Miller : Output engine completely rewritten. 32 * Andrea Arcangeli: SYNACK carry ts_recent in tsecr. 33 * Cacophonix Gaul : draft-minshall-nagle-01 34 * J Hadi Salim : ECN support 35 * 36 */ 37 38 #define pr_fmt(fmt) "TCP: " fmt 39 40 #include <net/tcp.h> 41 #include <net/tcp_ecn.h> 42 #include <net/mptcp.h> 43 #include <net/smc.h> 44 #include <net/proto_memory.h> 45 #include <net/psp.h> 46 47 #include <linux/compiler.h> 48 #include <linux/gfp.h> 49 #include <linux/module.h> 50 #include <linux/static_key.h> 51 #include <linux/skbuff_ref.h> 52 53 #include <trace/events/tcp.h> 54 55 /* Refresh clocks of a TCP socket, 56 * ensuring monotically increasing values. 57 */ 58 void tcp_mstamp_refresh(struct tcp_sock *tp) 59 { 60 u64 val = tcp_clock_ns(); 61 62 tp->tcp_clock_cache = val; 63 tp->tcp_mstamp = div_u64(val, NSEC_PER_USEC); 64 } 65 66 static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle, 67 int push_one, gfp_t gfp); 68 69 /* Insert skb into rb tree, ordered by TCP_SKB_CB(skb)->seq */ 70 void tcp_rbtree_insert(struct rb_root *root, struct sk_buff *skb) 71 { 72 struct rb_node **p = &root->rb_node; 73 struct rb_node *parent = NULL; 74 struct sk_buff *skb1; 75 76 while (*p) { 77 parent = *p; 78 skb1 = rb_to_skb(parent); 79 if (before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb1)->seq)) 80 p = &parent->rb_left; 81 else 82 p = &parent->rb_right; 83 } 84 rb_link_node(&skb->rbnode, parent, p); 85 rb_insert_color(&skb->rbnode, root); 86 } 87 88 /* Account for new data that has been sent to the network. */ 89 static void tcp_event_new_data_sent(struct sock *sk, struct sk_buff *skb) 90 { 91 struct inet_connection_sock *icsk = inet_csk(sk); 92 struct tcp_sock *tp = tcp_sk(sk); 93 unsigned int prior_packets = tp->packets_out; 94 95 WRITE_ONCE(tp->snd_nxt, TCP_SKB_CB(skb)->end_seq); 96 97 __skb_unlink(skb, &sk->sk_write_queue); 98 tcp_rbtree_insert(&sk->tcp_rtx_queue, skb); 99 100 if (tp->highest_sack == NULL) 101 tp->highest_sack = skb; 102 103 tp->packets_out += tcp_skb_pcount(skb); 104 if (!prior_packets || icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) 105 tcp_rearm_rto(sk); 106 107 NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPORIGDATASENT, 108 tcp_skb_pcount(skb)); 109 tcp_check_space(sk); 110 } 111 112 /* SND.NXT, if window was not shrunk or the amount of shrunk was less than one 113 * window scaling factor due to loss of precision. 114 * If window has been shrunk, what should we make? It is not clear at all. 115 * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-( 116 * Anything in between SND.UNA...SND.UNA+SND.WND also can be already 117 * invalid. OK, let's make this for now: 118 */ 119 static inline __u32 tcp_acceptable_seq(const struct sock *sk) 120 { 121 const struct tcp_sock *tp = tcp_sk(sk); 122 123 if (!before(tcp_wnd_end(tp), tp->snd_nxt) || 124 (tp->rx_opt.wscale_ok && 125 ((tp->snd_nxt - tcp_wnd_end(tp)) < (1 << tp->rx_opt.rcv_wscale)))) 126 return tp->snd_nxt; 127 else 128 return tcp_wnd_end(tp); 129 } 130 131 /* Calculate mss to advertise in SYN segment. 132 * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that: 133 * 134 * 1. It is independent of path mtu. 135 * 2. Ideally, it is maximal possible segment size i.e. 65535-40. 136 * 3. For IPv4 it is reasonable to calculate it from maximal MTU of 137 * attached devices, because some buggy hosts are confused by 138 * large MSS. 139 * 4. We do not make 3, we advertise MSS, calculated from first 140 * hop device mtu, but allow to raise it to ip_rt_min_advmss. 141 * This may be overridden via information stored in routing table. 142 * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible, 143 * probably even Jumbo". 144 */ 145 static __u16 tcp_advertise_mss(struct sock *sk) 146 { 147 struct tcp_sock *tp = tcp_sk(sk); 148 const struct dst_entry *dst = __sk_dst_get(sk); 149 int mss = tp->advmss; 150 151 if (dst) { 152 unsigned int metric = dst_metric_advmss(dst); 153 154 if (metric < mss) { 155 mss = metric; 156 tp->advmss = mss; 157 } 158 } 159 160 return (__u16)mss; 161 } 162 163 /* RFC2861. Reset CWND after idle period longer RTO to "restart window". 164 * This is the first part of cwnd validation mechanism. 165 */ 166 void tcp_cwnd_restart(struct sock *sk, s32 delta) 167 { 168 struct tcp_sock *tp = tcp_sk(sk); 169 u32 restart_cwnd = tcp_init_cwnd(tp, __sk_dst_get(sk)); 170 u32 cwnd = tcp_snd_cwnd(tp); 171 172 tcp_ca_event(sk, CA_EVENT_CWND_RESTART); 173 174 WRITE_ONCE(tp->snd_ssthresh, tcp_current_ssthresh(sk)); 175 restart_cwnd = min(restart_cwnd, cwnd); 176 177 while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd) 178 cwnd >>= 1; 179 tcp_snd_cwnd_set(tp, max(cwnd, restart_cwnd)); 180 tp->snd_cwnd_stamp = tcp_jiffies32; 181 tp->snd_cwnd_used = 0; 182 } 183 184 /* Congestion state accounting after a packet has been sent. */ 185 static void tcp_event_data_sent(struct tcp_sock *tp, 186 struct sock *sk) 187 { 188 struct inet_connection_sock *icsk = inet_csk(sk); 189 const u32 now = tcp_jiffies32; 190 191 if (tcp_packets_in_flight(tp) == 0) 192 tcp_ca_event(sk, CA_EVENT_TX_START); 193 194 tp->lsndtime = now; 195 196 /* If it is a reply for ato after last received 197 * packet, increase pingpong count. 198 */ 199 if ((u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato) 200 inet_csk_inc_pingpong_cnt(sk); 201 } 202 203 /* Account for an ACK we sent. */ 204 static inline void tcp_event_ack_sent(struct sock *sk, u32 rcv_nxt) 205 { 206 struct tcp_sock *tp = tcp_sk(sk); 207 208 if (unlikely(tp->compressed_ack)) { 209 NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPACKCOMPRESSED, 210 tp->compressed_ack); 211 tp->compressed_ack = 0; 212 if (hrtimer_try_to_cancel(&tp->compressed_ack_timer) == 1) 213 __sock_put(sk); 214 } 215 216 if (unlikely(rcv_nxt != tp->rcv_nxt)) 217 return; /* Special ACK sent by DCTCP to reflect ECN */ 218 tcp_dec_quickack_mode(sk); 219 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); 220 } 221 222 /* Determine a window scaling and initial window to offer. 223 * Based on the assumption that the given amount of space 224 * will be offered. Store the results in the tp structure. 225 * NOTE: for smooth operation initial space offering should 226 * be a multiple of mss if possible. We assume here that mss >= 1. 227 * This MUST be enforced by all callers. 228 */ 229 void tcp_select_initial_window(const struct sock *sk, int __space, __u32 mss, 230 __u32 *rcv_wnd, __u32 *__window_clamp, 231 int wscale_ok, __u8 *rcv_wscale, 232 __u32 init_rcv_wnd) 233 { 234 unsigned int space = (__space < 0 ? 0 : __space); 235 u32 window_clamp = READ_ONCE(*__window_clamp); 236 237 /* If no clamp set the clamp to the max possible scaled window */ 238 if (window_clamp == 0) 239 window_clamp = (U16_MAX << TCP_MAX_WSCALE); 240 space = min(window_clamp, space); 241 242 /* Quantize space offering to a multiple of mss if possible. */ 243 if (space > mss) 244 space = rounddown(space, mss); 245 246 /* NOTE: offering an initial window larger than 32767 247 * will break some buggy TCP stacks. If the admin tells us 248 * it is likely we could be speaking with such a buggy stack 249 * we will truncate our initial window offering to 32K-1 250 * unless the remote has sent us a window scaling option, 251 * which we interpret as a sign the remote TCP is not 252 * misinterpreting the window field as a signed quantity. 253 */ 254 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_workaround_signed_windows)) 255 (*rcv_wnd) = min(space, MAX_TCP_WINDOW); 256 else 257 (*rcv_wnd) = space; 258 259 if (init_rcv_wnd) 260 *rcv_wnd = min(*rcv_wnd, init_rcv_wnd * mss); 261 262 *rcv_wscale = 0; 263 if (wscale_ok) { 264 /* Set window scaling on max possible window */ 265 space = max_t(u32, space, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 266 space = max_t(u32, space, READ_ONCE(sysctl_rmem_max)); 267 space = min_t(u32, space, window_clamp); 268 *rcv_wscale = clamp_t(int, ilog2(space) - 15, 269 0, TCP_MAX_WSCALE); 270 } 271 /* Set the clamp no higher than max representable value */ 272 WRITE_ONCE(*__window_clamp, 273 min_t(__u32, U16_MAX << (*rcv_wscale), window_clamp)); 274 } 275 276 /* Chose a new window to advertise, update state in tcp_sock for the 277 * socket, and return result with RFC1323 scaling applied. The return 278 * value can be stuffed directly into th->window for an outgoing 279 * frame. 280 */ 281 static u16 tcp_select_window(struct sock *sk) 282 { 283 struct tcp_sock *tp = tcp_sk(sk); 284 struct net *net = sock_net(sk); 285 u32 old_win = tp->rcv_wnd; 286 u32 cur_win, new_win; 287 288 /* Make the window 0 if we failed to queue the data because we 289 * are out of memory. 290 */ 291 if (unlikely(inet_csk(sk)->icsk_ack.pending & ICSK_ACK_NOMEM)) { 292 tp->pred_flags = 0; 293 tp->rcv_wnd = 0; 294 tp->rcv_wup = tp->rcv_nxt; 295 tcp_update_max_rcv_wnd_seq(tp); 296 return 0; 297 } 298 299 cur_win = tcp_receive_window(tp); 300 new_win = __tcp_select_window(sk); 301 if (new_win < cur_win) { 302 /* Danger Will Robinson! 303 * Don't update rcv_wup/rcv_wnd here or else 304 * we will not be able to advertise a zero 305 * window in time. --DaveM 306 * 307 * Relax Will Robinson. 308 */ 309 if (!READ_ONCE(net->ipv4.sysctl_tcp_shrink_window) || !tp->rx_opt.rcv_wscale) { 310 /* Never shrink the offered window */ 311 if (new_win == 0) 312 NET_INC_STATS(net, LINUX_MIB_TCPWANTZEROWINDOWADV); 313 new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale); 314 } 315 } 316 317 tp->rcv_wnd = new_win; 318 tp->rcv_wup = tp->rcv_nxt; 319 tcp_update_max_rcv_wnd_seq(tp); 320 321 /* Make sure we do not exceed the maximum possible 322 * scaled window. 323 */ 324 if (!tp->rx_opt.rcv_wscale && 325 READ_ONCE(net->ipv4.sysctl_tcp_workaround_signed_windows)) 326 new_win = min(new_win, MAX_TCP_WINDOW); 327 else 328 new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale)); 329 330 /* RFC1323 scaling applied */ 331 new_win >>= tp->rx_opt.rcv_wscale; 332 333 /* If we advertise zero window, disable fast path. */ 334 if (new_win == 0) { 335 tp->pred_flags = 0; 336 if (old_win) 337 NET_INC_STATS(net, LINUX_MIB_TCPTOZEROWINDOWADV); 338 } else if (old_win == 0) { 339 NET_INC_STATS(net, LINUX_MIB_TCPFROMZEROWINDOWADV); 340 } 341 342 return new_win; 343 } 344 345 /* Set up ECN state for a packet on a ESTABLISHED socket that is about to 346 * be sent. 347 */ 348 static void tcp_ecn_send(struct sock *sk, struct sk_buff *skb, 349 struct tcphdr *th, int tcp_header_len) 350 { 351 struct tcp_sock *tp = tcp_sk(sk); 352 353 if (!tcp_ecn_mode_any(tp)) 354 return; 355 356 if (tcp_ecn_mode_accecn(tp)) { 357 if (!tcp_accecn_ace_fail_recv(tp) && 358 !tcp_accecn_ace_fail_send(tp)) 359 INET_ECN_xmit(sk); 360 else 361 INET_ECN_dontxmit(sk); 362 tcp_accecn_set_ace(tp, skb, th); 363 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ACCECN; 364 } else { 365 /* Not-retransmitted data segment: set ECT and inject CWR. */ 366 if (skb->len != tcp_header_len && 367 !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) { 368 INET_ECN_xmit(sk); 369 if (tp->ecn_flags & TCP_ECN_QUEUE_CWR) { 370 tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR; 371 th->cwr = 1; 372 skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 373 } 374 } else if (!tcp_ca_needs_ecn(sk)) { 375 /* ACK or retransmitted segment: clear ECT|CE */ 376 INET_ECN_dontxmit(sk); 377 } 378 if (tp->ecn_flags & TCP_ECN_DEMAND_CWR) 379 th->ece = 1; 380 } 381 } 382 383 /* Constructs common control bits of non-data skb. If SYN/FIN is present, 384 * auto increment end seqno. 385 */ 386 static void tcp_init_nondata_skb(struct sk_buff *skb, struct sock *sk, 387 u32 seq, u16 flags) 388 { 389 skb->ip_summed = CHECKSUM_PARTIAL; 390 391 TCP_SKB_CB(skb)->tcp_flags = flags; 392 393 tcp_skb_pcount_set(skb, 1); 394 psp_enqueue_set_decrypted(sk, skb); 395 396 TCP_SKB_CB(skb)->seq = seq; 397 if (flags & (TCPHDR_SYN | TCPHDR_FIN)) 398 seq++; 399 TCP_SKB_CB(skb)->end_seq = seq; 400 } 401 402 static inline bool tcp_urg_mode(const struct tcp_sock *tp) 403 { 404 return tp->snd_una != tp->snd_up; 405 } 406 407 #define OPTION_SACK_ADVERTISE BIT(0) 408 #define OPTION_TS BIT(1) 409 #define OPTION_MD5 BIT(2) 410 #define OPTION_WSCALE BIT(3) 411 #define OPTION_FAST_OPEN_COOKIE BIT(8) 412 #define OPTION_SMC BIT(9) 413 #define OPTION_MPTCP BIT(10) 414 #define OPTION_AO BIT(11) 415 #define OPTION_ACCECN BIT(12) 416 417 static void smc_options_write(__be32 *ptr, u16 *options) 418 { 419 #if IS_ENABLED(CONFIG_SMC) 420 if (static_branch_unlikely(&tcp_have_smc)) { 421 if (unlikely(OPTION_SMC & *options)) { 422 *ptr++ = htonl((TCPOPT_NOP << 24) | 423 (TCPOPT_NOP << 16) | 424 (TCPOPT_EXP << 8) | 425 (TCPOLEN_EXP_SMC_BASE)); 426 *ptr++ = htonl(TCPOPT_SMC_MAGIC); 427 } 428 } 429 #endif 430 } 431 432 struct tcp_out_options { 433 /* Following group is cleared in __tcp_transmit_skb() */ 434 struct_group(cleared, 435 u16 mss; /* 0 to disable */ 436 u8 bpf_opt_len; /* length of BPF hdr option */ 437 u8 num_sack_blocks; /* number of SACK blocks to include */ 438 ); 439 440 /* Caution: following fields are not cleared in __tcp_transmit_skb() */ 441 u16 options; /* bit field of OPTION_* */ 442 u8 ws; /* window scale, 0 to disable */ 443 u8 num_accecn_fields:7, /* number of AccECN fields needed */ 444 use_synack_ecn_bytes:1; /* Use synack_ecn_bytes or not */ 445 __u8 *hash_location; /* temporary pointer, overloaded */ 446 __u32 tsval, tsecr; /* need to include OPTION_TS */ 447 struct tcp_fastopen_cookie *fastopen_cookie; /* Fast open cookie */ 448 struct mptcp_out_options mptcp; 449 }; 450 451 static void mptcp_options_write(struct tcphdr *th, __be32 *ptr, 452 struct tcp_sock *tp, 453 struct tcp_out_options *opts) 454 { 455 #if IS_ENABLED(CONFIG_MPTCP) 456 if (unlikely(OPTION_MPTCP & opts->options)) 457 mptcp_write_options(th, ptr, tp, &opts->mptcp); 458 #endif 459 } 460 461 #ifdef CONFIG_CGROUP_BPF 462 static int bpf_skops_write_hdr_opt_arg0(struct sk_buff *skb, 463 enum tcp_synack_type synack_type) 464 { 465 if (unlikely(!skb)) 466 return BPF_WRITE_HDR_TCP_CURRENT_MSS; 467 468 if (unlikely(synack_type == TCP_SYNACK_COOKIE)) 469 return BPF_WRITE_HDR_TCP_SYNACK_COOKIE; 470 471 return 0; 472 } 473 474 /* req, syn_skb and synack_type are used when writing synack */ 475 static void bpf_skops_hdr_opt_len(struct sock *sk, struct sk_buff *skb, 476 struct request_sock *req, 477 struct sk_buff *syn_skb, 478 enum tcp_synack_type synack_type, 479 struct tcp_out_options *opts, 480 unsigned int *remaining) 481 { 482 struct bpf_sock_ops_kern sock_ops; 483 int err; 484 485 if (likely(!BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), 486 BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG)) || 487 !*remaining) 488 return; 489 490 /* *remaining has already been aligned to 4 bytes, so *remaining >= 4 */ 491 492 /* init sock_ops */ 493 memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); 494 495 sock_ops.op = BPF_SOCK_OPS_HDR_OPT_LEN_CB; 496 497 if (req) { 498 /* The listen "sk" cannot be passed here because 499 * it is not locked. It would not make too much 500 * sense to do bpf_setsockopt(listen_sk) based 501 * on individual connection request also. 502 * 503 * Thus, "req" is passed here and the cgroup-bpf-progs 504 * of the listen "sk" will be run. 505 * 506 * "req" is also used here for fastopen even the "sk" here is 507 * a fullsock "child" sk. It is to keep the behavior 508 * consistent between fastopen and non-fastopen on 509 * the bpf programming side. 510 */ 511 sock_ops.sk = (struct sock *)req; 512 sock_ops.syn_skb = syn_skb; 513 } else { 514 sock_owned_by_me(sk); 515 516 sock_ops.is_fullsock = 1; 517 sock_ops.is_locked_tcp_sock = 1; 518 sock_ops.sk = sk; 519 } 520 521 sock_ops.args[0] = bpf_skops_write_hdr_opt_arg0(skb, synack_type); 522 sock_ops.remaining_opt_len = *remaining; 523 /* tcp_current_mss() does not pass a skb */ 524 if (skb) 525 bpf_skops_init_skb(&sock_ops, skb, 0); 526 527 err = BPF_CGROUP_RUN_PROG_SOCK_OPS_SK(&sock_ops, sk); 528 529 if (err || sock_ops.remaining_opt_len == *remaining) 530 return; 531 532 opts->bpf_opt_len = *remaining - sock_ops.remaining_opt_len; 533 /* round up to 4 bytes */ 534 opts->bpf_opt_len = (opts->bpf_opt_len + 3) & ~3; 535 536 *remaining -= opts->bpf_opt_len; 537 } 538 539 static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb, 540 struct request_sock *req, 541 struct sk_buff *syn_skb, 542 enum tcp_synack_type synack_type, 543 struct tcp_out_options *opts) 544 { 545 u8 first_opt_off, nr_written, max_opt_len = opts->bpf_opt_len; 546 struct bpf_sock_ops_kern sock_ops; 547 int err; 548 549 if (likely(!max_opt_len)) 550 return; 551 552 memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); 553 554 sock_ops.op = BPF_SOCK_OPS_WRITE_HDR_OPT_CB; 555 556 if (req) { 557 sock_ops.sk = (struct sock *)req; 558 sock_ops.syn_skb = syn_skb; 559 } else { 560 sock_owned_by_me(sk); 561 562 sock_ops.is_fullsock = 1; 563 sock_ops.is_locked_tcp_sock = 1; 564 sock_ops.sk = sk; 565 } 566 567 sock_ops.args[0] = bpf_skops_write_hdr_opt_arg0(skb, synack_type); 568 sock_ops.remaining_opt_len = max_opt_len; 569 first_opt_off = tcp_hdrlen(skb) - max_opt_len; 570 bpf_skops_init_skb(&sock_ops, skb, first_opt_off); 571 572 err = BPF_CGROUP_RUN_PROG_SOCK_OPS_SK(&sock_ops, sk); 573 574 if (err) 575 nr_written = 0; 576 else 577 nr_written = max_opt_len - sock_ops.remaining_opt_len; 578 579 if (nr_written < max_opt_len) 580 memset(skb->data + first_opt_off + nr_written, TCPOPT_NOP, 581 max_opt_len - nr_written); 582 } 583 #else 584 static void bpf_skops_hdr_opt_len(struct sock *sk, struct sk_buff *skb, 585 struct request_sock *req, 586 struct sk_buff *syn_skb, 587 enum tcp_synack_type synack_type, 588 struct tcp_out_options *opts, 589 unsigned int *remaining) 590 { 591 } 592 593 static void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb, 594 struct request_sock *req, 595 struct sk_buff *syn_skb, 596 enum tcp_synack_type synack_type, 597 struct tcp_out_options *opts) 598 { 599 } 600 #endif 601 602 static __be32 *process_tcp_ao_options(struct tcp_sock *tp, 603 const struct tcp_request_sock *tcprsk, 604 struct tcp_out_options *opts, 605 struct tcp_key *key, __be32 *ptr) 606 { 607 #ifdef CONFIG_TCP_AO 608 u8 maclen = tcp_ao_maclen(key->ao_key); 609 610 if (tcprsk) { 611 u8 aolen = maclen + sizeof(struct tcp_ao_hdr); 612 613 *ptr++ = htonl((TCPOPT_AO << 24) | (aolen << 16) | 614 (tcprsk->ao_keyid << 8) | 615 (tcprsk->ao_rcv_next)); 616 } else { 617 struct tcp_ao_key *rnext_key; 618 struct tcp_ao_info *ao_info; 619 620 ao_info = rcu_dereference_check(tp->ao_info, 621 lockdep_sock_is_held(&tp->inet_conn.icsk_inet.sk)); 622 rnext_key = READ_ONCE(ao_info->rnext_key); 623 if (WARN_ON_ONCE(!rnext_key)) 624 return ptr; 625 *ptr++ = htonl((TCPOPT_AO << 24) | 626 (tcp_ao_len(key->ao_key) << 16) | 627 (key->ao_key->sndid << 8) | 628 (rnext_key->rcvid)); 629 } 630 opts->hash_location = (__u8 *)ptr; 631 ptr += maclen / sizeof(*ptr); 632 if (unlikely(maclen % sizeof(*ptr))) { 633 memset(ptr, TCPOPT_NOP, sizeof(*ptr)); 634 ptr++; 635 } 636 #endif 637 return ptr; 638 } 639 640 /* Initial values for AccECN option, ordered is based on ECN field bits 641 * similar to received_ecn_bytes. Used for SYN/ACK AccECN option. 642 */ 643 static const u32 synack_ecn_bytes[3] = { 0, 0, 0 }; 644 645 /* Write previously computed TCP options to the packet. 646 * 647 * Beware: Something in the Internet is very sensitive to the ordering of 648 * TCP options, we learned this through the hard way, so be careful here. 649 * Luckily we can at least blame others for their non-compliance but from 650 * inter-operability perspective it seems that we're somewhat stuck with 651 * the ordering which we have been using if we want to keep working with 652 * those broken things (not that it currently hurts anybody as there isn't 653 * particular reason why the ordering would need to be changed). 654 * 655 * At least SACK_PERM as the first option is known to lead to a disaster 656 * (but it may well be that other scenarios fail similarly). 657 */ 658 static void tcp_options_write(struct tcphdr *th, struct tcp_sock *tp, 659 const struct tcp_request_sock *tcprsk, 660 struct tcp_out_options *opts, 661 struct tcp_key *key) 662 { 663 u8 leftover_highbyte = TCPOPT_NOP; /* replace 1st NOP if avail */ 664 u8 leftover_lowbyte = TCPOPT_NOP; /* replace 2nd NOP in succession */ 665 __be32 *ptr = (__be32 *)(th + 1); 666 u16 options = opts->options; /* mungable copy */ 667 668 if (tcp_key_is_md5(key)) { 669 *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | 670 (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG); 671 /* overload cookie hash location */ 672 opts->hash_location = (__u8 *)ptr; 673 ptr += 4; 674 } else if (tcp_key_is_ao(key)) { 675 ptr = process_tcp_ao_options(tp, tcprsk, opts, key, ptr); 676 } 677 if (unlikely(opts->mss)) { 678 *ptr++ = htonl((TCPOPT_MSS << 24) | 679 (TCPOLEN_MSS << 16) | 680 opts->mss); 681 } 682 683 if (likely(OPTION_TS & options)) { 684 if (unlikely(OPTION_SACK_ADVERTISE & options)) { 685 *ptr++ = htonl((TCPOPT_SACK_PERM << 24) | 686 (TCPOLEN_SACK_PERM << 16) | 687 (TCPOPT_TIMESTAMP << 8) | 688 TCPOLEN_TIMESTAMP); 689 options &= ~OPTION_SACK_ADVERTISE; 690 } else { 691 *ptr++ = htonl((TCPOPT_NOP << 24) | 692 (TCPOPT_NOP << 16) | 693 (TCPOPT_TIMESTAMP << 8) | 694 TCPOLEN_TIMESTAMP); 695 } 696 *ptr++ = htonl(opts->tsval); 697 *ptr++ = htonl(opts->tsecr); 698 } 699 700 if (OPTION_ACCECN & options) { 701 const u32 *ecn_bytes = opts->use_synack_ecn_bytes ? 702 synack_ecn_bytes : 703 tp->received_ecn_bytes; 704 const u8 ect0_idx = INET_ECN_ECT_0 - 1; 705 const u8 ect1_idx = INET_ECN_ECT_1 - 1; 706 const u8 ce_idx = INET_ECN_CE - 1; 707 u32 e0b; 708 u32 e1b; 709 u32 ceb; 710 u8 len; 711 712 e0b = ecn_bytes[ect0_idx] + TCP_ACCECN_E0B_INIT_OFFSET; 713 e1b = ecn_bytes[ect1_idx] + TCP_ACCECN_E1B_INIT_OFFSET; 714 ceb = ecn_bytes[ce_idx] + TCP_ACCECN_CEB_INIT_OFFSET; 715 len = TCPOLEN_ACCECN_BASE + 716 opts->num_accecn_fields * TCPOLEN_ACCECN_PERFIELD; 717 718 if (opts->num_accecn_fields == 2) { 719 *ptr++ = htonl((TCPOPT_ACCECN1 << 24) | (len << 16) | 720 ((e1b >> 8) & 0xffff)); 721 *ptr++ = htonl(((e1b & 0xff) << 24) | 722 (ceb & 0xffffff)); 723 } else if (opts->num_accecn_fields == 1) { 724 *ptr++ = htonl((TCPOPT_ACCECN1 << 24) | (len << 16) | 725 ((e1b >> 8) & 0xffff)); 726 leftover_highbyte = e1b & 0xff; 727 leftover_lowbyte = TCPOPT_NOP; 728 } else if (opts->num_accecn_fields == 0) { 729 leftover_highbyte = TCPOPT_ACCECN1; 730 leftover_lowbyte = len; 731 } else if (opts->num_accecn_fields == 3) { 732 *ptr++ = htonl((TCPOPT_ACCECN1 << 24) | (len << 16) | 733 ((e1b >> 8) & 0xffff)); 734 *ptr++ = htonl(((e1b & 0xff) << 24) | 735 (ceb & 0xffffff)); 736 *ptr++ = htonl(((e0b & 0xffffff) << 8) | 737 TCPOPT_NOP); 738 } 739 if (tp) { 740 tp->accecn_minlen = 0; 741 tp->accecn_opt_tstamp = tp->tcp_mstamp; 742 tp->accecn_opt_sent_w_dsack = tp->rx_opt.dsack; 743 if (tp->accecn_opt_demand) 744 tp->accecn_opt_demand--; 745 } 746 } else if (tp) { 747 tp->accecn_opt_sent_w_dsack = 0; 748 } 749 750 if (unlikely(OPTION_SACK_ADVERTISE & options)) { 751 *ptr++ = htonl((leftover_highbyte << 24) | 752 (leftover_lowbyte << 16) | 753 (TCPOPT_SACK_PERM << 8) | 754 TCPOLEN_SACK_PERM); 755 leftover_highbyte = TCPOPT_NOP; 756 leftover_lowbyte = TCPOPT_NOP; 757 } 758 759 if (unlikely(OPTION_WSCALE & options)) { 760 u8 highbyte = TCPOPT_NOP; 761 762 /* Do not split the leftover 2-byte to fit into a single 763 * NOP, i.e., replace this NOP only when 1 byte is leftover 764 * within leftover_highbyte. 765 */ 766 if (unlikely(leftover_highbyte != TCPOPT_NOP && 767 leftover_lowbyte == TCPOPT_NOP)) { 768 highbyte = leftover_highbyte; 769 leftover_highbyte = TCPOPT_NOP; 770 } 771 *ptr++ = htonl((highbyte << 24) | 772 (TCPOPT_WINDOW << 16) | 773 (TCPOLEN_WINDOW << 8) | 774 opts->ws); 775 } 776 777 if (unlikely(opts->num_sack_blocks)) { 778 struct tcp_sack_block *sp = tp->rx_opt.dsack ? 779 tp->duplicate_sack : tp->selective_acks; 780 int this_sack; 781 782 *ptr++ = htonl((leftover_highbyte << 24) | 783 (leftover_lowbyte << 16) | 784 (TCPOPT_SACK << 8) | 785 (TCPOLEN_SACK_BASE + (opts->num_sack_blocks * 786 TCPOLEN_SACK_PERBLOCK))); 787 leftover_highbyte = TCPOPT_NOP; 788 leftover_lowbyte = TCPOPT_NOP; 789 790 for (this_sack = 0; this_sack < opts->num_sack_blocks; 791 ++this_sack) { 792 *ptr++ = htonl(sp[this_sack].start_seq); 793 *ptr++ = htonl(sp[this_sack].end_seq); 794 } 795 796 tp->rx_opt.dsack = 0; 797 } else if (unlikely(leftover_highbyte != TCPOPT_NOP || 798 leftover_lowbyte != TCPOPT_NOP)) { 799 *ptr++ = htonl((leftover_highbyte << 24) | 800 (leftover_lowbyte << 16) | 801 (TCPOPT_NOP << 8) | 802 TCPOPT_NOP); 803 leftover_highbyte = TCPOPT_NOP; 804 leftover_lowbyte = TCPOPT_NOP; 805 } 806 807 if (unlikely(OPTION_FAST_OPEN_COOKIE & options)) { 808 struct tcp_fastopen_cookie *foc = opts->fastopen_cookie; 809 u8 *p = (u8 *)ptr; 810 u32 len; /* Fast Open option length */ 811 812 if (foc->exp) { 813 len = TCPOLEN_EXP_FASTOPEN_BASE + foc->len; 814 *ptr = htonl((TCPOPT_EXP << 24) | (len << 16) | 815 TCPOPT_FASTOPEN_MAGIC); 816 p += TCPOLEN_EXP_FASTOPEN_BASE; 817 } else { 818 len = TCPOLEN_FASTOPEN_BASE + foc->len; 819 *p++ = TCPOPT_FASTOPEN; 820 *p++ = len; 821 } 822 823 memcpy(p, foc->val, foc->len); 824 if ((len & 3) == 2) { 825 p[foc->len] = TCPOPT_NOP; 826 p[foc->len + 1] = TCPOPT_NOP; 827 } 828 ptr += (len + 3) >> 2; 829 } 830 831 smc_options_write(ptr, &options); 832 833 mptcp_options_write(th, ptr, tp, opts); 834 } 835 836 static void smc_set_option(struct tcp_sock *tp, 837 struct tcp_out_options *opts, 838 unsigned int *remaining) 839 { 840 #if IS_ENABLED(CONFIG_SMC) 841 if (static_branch_unlikely(&tcp_have_smc) && tp->syn_smc) { 842 tp->syn_smc = !!smc_call_hsbpf(1, tp, syn_option); 843 /* re-check syn_smc */ 844 if (tp->syn_smc && 845 *remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) { 846 opts->options |= OPTION_SMC; 847 *remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED; 848 } 849 } 850 #endif 851 } 852 853 static void smc_set_option_cond(const struct tcp_sock *tp, 854 struct inet_request_sock *ireq, 855 struct tcp_out_options *opts, 856 unsigned int *remaining) 857 { 858 #if IS_ENABLED(CONFIG_SMC) 859 if (static_branch_unlikely(&tcp_have_smc) && tp->syn_smc && ireq->smc_ok) { 860 ireq->smc_ok = !!smc_call_hsbpf(1, tp, synack_option, ireq); 861 /* re-check smc_ok */ 862 if (ireq->smc_ok && 863 *remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) { 864 opts->options |= OPTION_SMC; 865 *remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED; 866 } 867 } 868 #endif 869 } 870 871 static void mptcp_set_option_cond(const struct request_sock *req, 872 struct tcp_out_options *opts, 873 unsigned int *remaining) 874 { 875 if (rsk_is_mptcp(req)) { 876 unsigned int size; 877 878 if (mptcp_synack_options(req, &size, &opts->mptcp)) { 879 if (*remaining >= size) { 880 opts->options |= OPTION_MPTCP; 881 *remaining -= size; 882 } 883 } 884 } 885 } 886 887 static u32 tcp_synack_options_combine_saving(struct tcp_out_options *opts) 888 { 889 /* How much there's room for combining with the alignment padding? */ 890 if ((opts->options & (OPTION_SACK_ADVERTISE | OPTION_TS)) == 891 OPTION_SACK_ADVERTISE) 892 return 2; 893 else if (opts->options & OPTION_WSCALE) 894 return 1; 895 return 0; 896 } 897 898 /* Calculates how long AccECN option will fit to @remaining option space. 899 * 900 * AccECN option can sometimes replace NOPs used for alignment of other 901 * TCP options (up to @max_combine_saving available). 902 * 903 * Only solutions with at least @required AccECN fields are accepted. 904 * 905 * Returns: The size of the AccECN option excluding space repurposed from 906 * the alignment of the other options. 907 */ 908 static int tcp_options_fit_accecn(struct tcp_out_options *opts, int required, 909 int remaining) 910 { 911 int size = TCP_ACCECN_MAXSIZE; 912 int sack_blocks_reduce = 0; 913 int max_combine_saving; 914 int rem = remaining; 915 int align_size; 916 917 if (opts->use_synack_ecn_bytes) 918 max_combine_saving = tcp_synack_options_combine_saving(opts); 919 else 920 max_combine_saving = opts->num_sack_blocks > 0 ? 2 : 0; 921 opts->num_accecn_fields = TCP_ACCECN_NUMFIELDS; 922 while (opts->num_accecn_fields >= required) { 923 /* Pad to dword if cannot combine */ 924 if ((size & 0x3) > max_combine_saving) 925 align_size = ALIGN(size, 4); 926 else 927 align_size = ALIGN_DOWN(size, 4); 928 929 if (rem >= align_size) { 930 size = align_size; 931 break; 932 } else if (opts->num_accecn_fields == required && 933 opts->num_sack_blocks > 2 && 934 required > 0) { 935 /* Try to fit the option by removing one SACK block */ 936 opts->num_sack_blocks--; 937 sack_blocks_reduce++; 938 rem = rem + TCPOLEN_SACK_PERBLOCK; 939 940 opts->num_accecn_fields = TCP_ACCECN_NUMFIELDS; 941 size = TCP_ACCECN_MAXSIZE; 942 continue; 943 } 944 945 opts->num_accecn_fields--; 946 size -= TCPOLEN_ACCECN_PERFIELD; 947 } 948 if (sack_blocks_reduce > 0) { 949 if (opts->num_accecn_fields >= required) 950 size -= sack_blocks_reduce * TCPOLEN_SACK_PERBLOCK; 951 else 952 opts->num_sack_blocks += sack_blocks_reduce; 953 } 954 if (opts->num_accecn_fields < required) 955 return 0; 956 957 opts->options |= OPTION_ACCECN; 958 return size; 959 } 960 961 /* Compute TCP options for SYN packets. This is not the final 962 * network wire format yet. 963 */ 964 static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb, 965 struct tcp_out_options *opts, 966 struct tcp_key *key) 967 { 968 struct tcp_sock *tp = tcp_sk(sk); 969 unsigned int remaining = MAX_TCP_OPTION_SPACE; 970 struct tcp_fastopen_request *fastopen = tp->fastopen_req; 971 bool timestamps; 972 973 opts->options = 0; 974 975 /* Better than switch (key.type) as it has static branches */ 976 if (tcp_key_is_md5(key)) { 977 timestamps = false; 978 opts->options |= OPTION_MD5; 979 remaining -= TCPOLEN_MD5SIG_ALIGNED; 980 } else { 981 timestamps = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps); 982 if (tcp_key_is_ao(key)) { 983 opts->options |= OPTION_AO; 984 remaining -= tcp_ao_len_aligned(key->ao_key); 985 } 986 } 987 988 /* We always get an MSS option. The option bytes which will be seen in 989 * normal data packets should timestamps be used, must be in the MSS 990 * advertised. But we subtract them from tp->mss_cache so that 991 * calculations in tcp_sendmsg are simpler etc. So account for this 992 * fact here if necessary. If we don't do this correctly, as a 993 * receiver we won't recognize data packets as being full sized when we 994 * should, and thus we won't abide by the delayed ACK rules correctly. 995 * SACKs don't matter, we never delay an ACK when we have any of those 996 * going out. */ 997 opts->mss = tcp_advertise_mss(sk); 998 remaining -= TCPOLEN_MSS_ALIGNED; 999 1000 if (likely(timestamps)) { 1001 opts->options |= OPTION_TS; 1002 opts->tsval = tcp_skb_timestamp_ts(tp->tcp_usec_ts, skb) + tp->tsoffset; 1003 opts->tsecr = tp->rx_opt.ts_recent; 1004 remaining -= TCPOLEN_TSTAMP_ALIGNED; 1005 } 1006 if (likely(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_window_scaling))) { 1007 opts->ws = tp->rx_opt.rcv_wscale; 1008 opts->options |= OPTION_WSCALE; 1009 remaining -= TCPOLEN_WSCALE_ALIGNED; 1010 } 1011 if (likely(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_sack))) { 1012 opts->options |= OPTION_SACK_ADVERTISE; 1013 if (unlikely(!(OPTION_TS & opts->options))) 1014 remaining -= TCPOLEN_SACKPERM_ALIGNED; 1015 } 1016 1017 if (fastopen && fastopen->cookie.len >= 0) { 1018 u32 need = fastopen->cookie.len; 1019 1020 need += fastopen->cookie.exp ? TCPOLEN_EXP_FASTOPEN_BASE : 1021 TCPOLEN_FASTOPEN_BASE; 1022 need = (need + 3) & ~3U; /* Align to 32 bits */ 1023 if (remaining >= need) { 1024 opts->options |= OPTION_FAST_OPEN_COOKIE; 1025 opts->fastopen_cookie = &fastopen->cookie; 1026 remaining -= need; 1027 tp->syn_fastopen = 1; 1028 tp->syn_fastopen_exp = fastopen->cookie.exp ? 1 : 0; 1029 } 1030 } 1031 1032 smc_set_option(tp, opts, &remaining); 1033 1034 if (sk_is_mptcp(sk)) { 1035 unsigned int size; 1036 1037 if (mptcp_syn_options(sk, skb, &size, &opts->mptcp)) { 1038 if (remaining >= size) { 1039 opts->options |= OPTION_MPTCP; 1040 remaining -= size; 1041 } 1042 } 1043 } 1044 1045 /* Simultaneous open SYN/ACK needs AccECN option but not SYN. 1046 * It is attempted to negotiate the use of AccECN also on the first 1047 * retransmitted SYN, as mentioned in "3.1.4.1. Retransmitted SYNs" 1048 * of AccECN draft. 1049 */ 1050 if (unlikely((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK) && 1051 tcp_ecn_mode_accecn(tp) && 1052 inet_csk(sk)->icsk_retransmits < 2 && 1053 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn_option) && 1054 remaining >= TCPOLEN_ACCECN_BASE)) { 1055 opts->use_synack_ecn_bytes = 1; 1056 remaining -= tcp_options_fit_accecn(opts, 0, remaining); 1057 } 1058 1059 bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts, &remaining); 1060 1061 return MAX_TCP_OPTION_SPACE - remaining; 1062 } 1063 1064 /* Set up TCP options for SYN-ACKs. */ 1065 static unsigned int tcp_synack_options(const struct sock *sk, 1066 struct request_sock *req, 1067 unsigned int mss, struct sk_buff *skb, 1068 struct tcp_out_options *opts, 1069 const struct tcp_key *key, 1070 struct tcp_fastopen_cookie *foc, 1071 enum tcp_synack_type synack_type, 1072 struct sk_buff *syn_skb) 1073 { 1074 struct inet_request_sock *ireq = inet_rsk(req); 1075 unsigned int remaining = MAX_TCP_OPTION_SPACE; 1076 struct tcp_request_sock *treq = tcp_rsk(req); 1077 1078 if (tcp_key_is_md5(key)) { 1079 opts->options |= OPTION_MD5; 1080 remaining -= TCPOLEN_MD5SIG_ALIGNED; 1081 1082 /* We can't fit any SACK blocks in a packet with MD5 + TS 1083 * options. There was discussion about disabling SACK 1084 * rather than TS in order to fit in better with old, 1085 * buggy kernels, but that was deemed to be unnecessary. 1086 */ 1087 if (synack_type != TCP_SYNACK_COOKIE) 1088 ireq->tstamp_ok &= !ireq->sack_ok; 1089 } else if (tcp_key_is_ao(key)) { 1090 opts->options |= OPTION_AO; 1091 remaining -= tcp_ao_len_aligned(key->ao_key); 1092 ireq->tstamp_ok &= !ireq->sack_ok; 1093 } 1094 1095 /* We always send an MSS option. */ 1096 opts->mss = mss; 1097 remaining -= TCPOLEN_MSS_ALIGNED; 1098 1099 if (likely(ireq->wscale_ok)) { 1100 opts->ws = ireq->rcv_wscale; 1101 opts->options |= OPTION_WSCALE; 1102 remaining -= TCPOLEN_WSCALE_ALIGNED; 1103 } 1104 if (likely(ireq->tstamp_ok)) { 1105 opts->options |= OPTION_TS; 1106 opts->tsval = tcp_skb_timestamp_ts(tcp_rsk(req)->req_usec_ts, skb) + 1107 tcp_rsk(req)->ts_off; 1108 if (!tcp_rsk(req)->snt_tsval_first) { 1109 if (!opts->tsval) 1110 opts->tsval = ~0U; 1111 tcp_rsk(req)->snt_tsval_first = opts->tsval; 1112 } 1113 WRITE_ONCE(tcp_rsk(req)->snt_tsval_last, opts->tsval); 1114 opts->tsecr = req->ts_recent; 1115 remaining -= TCPOLEN_TSTAMP_ALIGNED; 1116 } 1117 if (likely(ireq->sack_ok)) { 1118 opts->options |= OPTION_SACK_ADVERTISE; 1119 if (unlikely(!ireq->tstamp_ok)) 1120 remaining -= TCPOLEN_SACKPERM_ALIGNED; 1121 } 1122 if (foc != NULL && foc->len >= 0) { 1123 u32 need = foc->len; 1124 1125 need += foc->exp ? TCPOLEN_EXP_FASTOPEN_BASE : 1126 TCPOLEN_FASTOPEN_BASE; 1127 need = (need + 3) & ~3U; /* Align to 32 bits */ 1128 if (remaining >= need) { 1129 opts->options |= OPTION_FAST_OPEN_COOKIE; 1130 opts->fastopen_cookie = foc; 1131 remaining -= need; 1132 } 1133 } 1134 1135 mptcp_set_option_cond(req, opts, &remaining); 1136 1137 smc_set_option_cond(tcp_sk(sk), ireq, opts, &remaining); 1138 1139 if (treq->accecn_ok && 1140 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn_option) && 1141 synack_type != TCP_SYNACK_RETRANS && remaining >= TCPOLEN_ACCECN_BASE) { 1142 opts->use_synack_ecn_bytes = 1; 1143 remaining -= tcp_options_fit_accecn(opts, 0, remaining); 1144 } 1145 1146 bpf_skops_hdr_opt_len((struct sock *)sk, skb, req, syn_skb, 1147 synack_type, opts, &remaining); 1148 1149 return MAX_TCP_OPTION_SPACE - remaining; 1150 } 1151 1152 /* Compute TCP options for ESTABLISHED sockets. This is not the 1153 * final wire format yet. 1154 */ 1155 static unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb, 1156 struct tcp_out_options *opts, 1157 struct tcp_key *key) 1158 { 1159 struct tcp_sock *tp = tcp_sk(sk); 1160 unsigned int size = 0; 1161 unsigned int eff_sacks; 1162 1163 opts->options = 0; 1164 1165 /* Better than switch (key.type) as it has static branches */ 1166 if (tcp_key_is_md5(key)) { 1167 opts->options |= OPTION_MD5; 1168 size += TCPOLEN_MD5SIG_ALIGNED; 1169 } else if (tcp_key_is_ao(key)) { 1170 opts->options |= OPTION_AO; 1171 size += tcp_ao_len_aligned(key->ao_key); 1172 } 1173 1174 if (likely(tp->rx_opt.tstamp_ok)) { 1175 opts->options |= OPTION_TS; 1176 opts->tsval = skb ? tcp_skb_timestamp_ts(tp->tcp_usec_ts, skb) + 1177 tp->tsoffset : 0; 1178 opts->tsecr = tp->rx_opt.ts_recent; 1179 size += TCPOLEN_TSTAMP_ALIGNED; 1180 } 1181 1182 /* MPTCP options have precedence over SACK for the limited TCP 1183 * option space because a MPTCP connection would be forced to 1184 * fall back to regular TCP if a required multipath option is 1185 * missing. SACK still gets a chance to use whatever space is 1186 * left. 1187 */ 1188 if (sk_is_mptcp(sk)) { 1189 unsigned int remaining = MAX_TCP_OPTION_SPACE - size; 1190 unsigned int opt_size = 0; 1191 1192 if (mptcp_established_options(sk, skb, &opt_size, remaining, 1193 &opts->mptcp)) { 1194 opts->options |= OPTION_MPTCP; 1195 size += opt_size; 1196 } 1197 } 1198 1199 eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack; 1200 if (unlikely(eff_sacks)) { 1201 const unsigned int remaining = MAX_TCP_OPTION_SPACE - size; 1202 if (likely(remaining >= TCPOLEN_SACK_BASE_ALIGNED + 1203 TCPOLEN_SACK_PERBLOCK)) { 1204 opts->num_sack_blocks = 1205 min_t(unsigned int, eff_sacks, 1206 (remaining - TCPOLEN_SACK_BASE_ALIGNED) / 1207 TCPOLEN_SACK_PERBLOCK); 1208 1209 size += TCPOLEN_SACK_BASE_ALIGNED + 1210 opts->num_sack_blocks * TCPOLEN_SACK_PERBLOCK; 1211 } else { 1212 opts->num_sack_blocks = 0; 1213 } 1214 } else { 1215 opts->num_sack_blocks = 0; 1216 } 1217 1218 if (tcp_ecn_mode_accecn(tp)) { 1219 int ecn_opt = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_ecn_option); 1220 1221 if (ecn_opt && tp->saw_accecn_opt && 1222 (ecn_opt >= TCP_ACCECN_OPTION_PERSIST || 1223 !tcp_accecn_opt_fail_send(tp)) && 1224 (ecn_opt >= TCP_ACCECN_OPTION_FULL || tp->accecn_opt_demand || 1225 tcp_accecn_option_beacon_check(sk))) { 1226 opts->use_synack_ecn_bytes = 0; 1227 size += tcp_options_fit_accecn(opts, tp->accecn_minlen, 1228 MAX_TCP_OPTION_SPACE - size); 1229 } 1230 } 1231 1232 if (unlikely(BPF_SOCK_OPS_TEST_FLAG(tp, 1233 BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG))) { 1234 unsigned int remaining = MAX_TCP_OPTION_SPACE - size; 1235 1236 bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts, &remaining); 1237 1238 size = MAX_TCP_OPTION_SPACE - remaining; 1239 } 1240 1241 return size; 1242 } 1243 1244 1245 /* TCP SMALL QUEUES (TSQ) 1246 * 1247 * TSQ goal is to keep small amount of skbs per tcp flow in tx queues (qdisc+dev) 1248 * to reduce RTT and bufferbloat. 1249 * We do this using a special skb destructor (tcp_wfree). 1250 * 1251 * Its important tcp_wfree() can be replaced by sock_wfree() in the event skb 1252 * needs to be reallocated in a driver. 1253 * The invariant being skb->truesize subtracted from sk->sk_wmem_alloc 1254 * 1255 * Since transmit from skb destructor is forbidden, we use a BH work item 1256 * to process all sockets that eventually need to send more skbs. 1257 * We use one work item per cpu, with its own queue of sockets. 1258 */ 1259 struct tsq_work { 1260 struct work_struct work; 1261 struct list_head head; /* queue of tcp sockets */ 1262 }; 1263 static DEFINE_PER_CPU(struct tsq_work, tsq_work); 1264 1265 static void tcp_tsq_write(struct sock *sk) 1266 { 1267 if ((1 << sk->sk_state) & 1268 (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_CLOSING | 1269 TCPF_CLOSE_WAIT | TCPF_LAST_ACK)) { 1270 struct tcp_sock *tp = tcp_sk(sk); 1271 1272 if (tp->lost_out > tp->retrans_out && 1273 tcp_snd_cwnd(tp) > tcp_packets_in_flight(tp)) { 1274 tcp_mstamp_refresh(tp); 1275 tcp_xmit_retransmit_queue(sk); 1276 } 1277 1278 tcp_write_xmit(sk, tcp_current_mss(sk), tp->nonagle, 1279 0, GFP_ATOMIC); 1280 } 1281 } 1282 1283 static void tcp_tsq_handler(struct sock *sk) 1284 { 1285 bh_lock_sock(sk); 1286 if (!sock_owned_by_user(sk)) 1287 tcp_tsq_write(sk); 1288 else if (!test_and_set_bit(TCP_TSQ_DEFERRED, &sk->sk_tsq_flags)) 1289 sock_hold(sk); 1290 bh_unlock_sock(sk); 1291 } 1292 /* 1293 * One work item per cpu tries to send more skbs. 1294 * We run in BH context but need to disable irqs when 1295 * transferring tsq->head because tcp_wfree() might 1296 * interrupt us (non NAPI drivers) 1297 */ 1298 static void tcp_tsq_workfn(struct work_struct *work) 1299 { 1300 struct tsq_work *tsq = container_of(work, struct tsq_work, work); 1301 LIST_HEAD(list); 1302 unsigned long flags; 1303 struct list_head *q, *n; 1304 struct tcp_sock *tp; 1305 struct sock *sk; 1306 1307 local_irq_save(flags); 1308 list_splice_init(&tsq->head, &list); 1309 local_irq_restore(flags); 1310 1311 list_for_each_safe(q, n, &list) { 1312 tp = list_entry(q, struct tcp_sock, tsq_node); 1313 list_del(&tp->tsq_node); 1314 1315 sk = (struct sock *)tp; 1316 smp_mb__before_atomic(); 1317 clear_bit(TSQ_QUEUED, &sk->sk_tsq_flags); 1318 1319 tcp_tsq_handler(sk); 1320 sk_free(sk); 1321 } 1322 } 1323 1324 /** 1325 * tcp_release_cb - tcp release_sock() callback 1326 * @sk: socket 1327 * 1328 * called from release_sock() to perform protocol dependent 1329 * actions before socket release. 1330 */ 1331 void tcp_release_cb(struct sock *sk) 1332 { 1333 unsigned long flags = smp_load_acquire(&sk->sk_tsq_flags); 1334 unsigned long nflags; 1335 1336 /* perform an atomic operation only if at least one flag is set */ 1337 do { 1338 if (!(flags & TCP_DEFERRED_ALL)) 1339 return; 1340 nflags = flags & ~TCP_DEFERRED_ALL; 1341 } while (!try_cmpxchg(&sk->sk_tsq_flags, &flags, nflags)); 1342 1343 if (flags & TCPF_TSQ_DEFERRED) { 1344 tcp_tsq_write(sk); 1345 __sock_put(sk); 1346 } 1347 1348 if (flags & TCPF_WRITE_TIMER_DEFERRED) { 1349 tcp_write_timer_handler(sk); 1350 __sock_put(sk); 1351 } 1352 if (flags & TCPF_DELACK_TIMER_DEFERRED) { 1353 tcp_delack_timer_handler(sk); 1354 __sock_put(sk); 1355 } 1356 if (flags & TCPF_MTU_REDUCED_DEFERRED) { 1357 inet_csk(sk)->icsk_af_ops->mtu_reduced(sk); 1358 __sock_put(sk); 1359 } 1360 if ((flags & TCPF_ACK_DEFERRED) && inet_csk_ack_scheduled(sk)) 1361 tcp_send_ack(sk); 1362 } 1363 1364 void __init tcp_tsq_work_init(void) 1365 { 1366 int i; 1367 1368 for_each_possible_cpu(i) { 1369 struct tsq_work *tsq = &per_cpu(tsq_work, i); 1370 1371 INIT_LIST_HEAD(&tsq->head); 1372 INIT_WORK(&tsq->work, tcp_tsq_workfn); 1373 } 1374 } 1375 1376 /* 1377 * Write buffer destructor automatically called from kfree_skb. 1378 * We can't xmit new skbs from this context, as we might already 1379 * hold qdisc lock. 1380 */ 1381 void tcp_wfree(struct sk_buff *skb) 1382 { 1383 struct sock *sk = skb->sk; 1384 struct tcp_sock *tp = tcp_sk(sk); 1385 unsigned long flags, nval, oval; 1386 struct tsq_work *tsq; 1387 bool empty; 1388 1389 /* Keep one reference on sk_wmem_alloc. 1390 * Will be released by sk_free() from here or tcp_tsq_workfn() 1391 */ 1392 WARN_ON(refcount_sub_and_test(skb->truesize - 1, &sk->sk_wmem_alloc)); 1393 1394 /* If this softirq is serviced by ksoftirqd, we are likely under stress. 1395 * Wait until our queues (qdisc + devices) are drained. 1396 * This gives : 1397 * - less callbacks to tcp_write_xmit(), reducing stress (batches) 1398 * - chance for incoming ACK (processed by another cpu maybe) 1399 * to migrate this flow (skb->ooo_okay will be eventually set) 1400 */ 1401 if (refcount_read(&sk->sk_wmem_alloc) >= SKB_TRUESIZE(1) && this_cpu_ksoftirqd() == current) 1402 goto out; 1403 1404 oval = smp_load_acquire(&sk->sk_tsq_flags); 1405 do { 1406 if (!(oval & TSQF_THROTTLED) || (oval & TSQF_QUEUED)) 1407 goto out; 1408 1409 nval = (oval & ~TSQF_THROTTLED) | TSQF_QUEUED; 1410 } while (!try_cmpxchg(&sk->sk_tsq_flags, &oval, nval)); 1411 1412 /* queue this socket to BH workqueue */ 1413 local_irq_save(flags); 1414 tsq = this_cpu_ptr(&tsq_work); 1415 empty = list_empty(&tsq->head); 1416 list_add(&tp->tsq_node, &tsq->head); 1417 if (empty) 1418 queue_work(system_bh_wq, &tsq->work); 1419 local_irq_restore(flags); 1420 return; 1421 out: 1422 sk_free(sk); 1423 } 1424 1425 /* Note: Called under soft irq. 1426 * We can call TCP stack right away, unless socket is owned by user. 1427 */ 1428 enum hrtimer_restart tcp_pace_kick(struct hrtimer *timer) 1429 { 1430 struct tcp_sock *tp = container_of(timer, struct tcp_sock, pacing_timer); 1431 struct sock *sk = (struct sock *)tp; 1432 1433 tcp_tsq_handler(sk); 1434 sock_put(sk); 1435 1436 return HRTIMER_NORESTART; 1437 } 1438 1439 static void tcp_update_skb_after_send(struct sock *sk, struct sk_buff *skb, 1440 u64 prior_wstamp) 1441 { 1442 struct tcp_sock *tp = tcp_sk(sk); 1443 1444 if (sk->sk_pacing_status != SK_PACING_NONE) { 1445 unsigned long rate = READ_ONCE(sk->sk_pacing_rate); 1446 1447 /* Original sch_fq does not pace first 10 MSS 1448 * Note that tp->data_segs_out overflows after 2^32 packets, 1449 * this is a minor annoyance. 1450 */ 1451 if (rate != ~0UL && rate && tp->data_segs_out >= 10) { 1452 u64 len_ns = div64_ul((u64)skb->len * NSEC_PER_SEC, rate); 1453 u64 credit = tp->tcp_wstamp_ns - prior_wstamp; 1454 1455 /* take into account OS jitter */ 1456 len_ns -= min_t(u64, len_ns / 2, credit); 1457 tp->tcp_wstamp_ns += len_ns; 1458 } 1459 } 1460 list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue); 1461 } 1462 1463 /* Snapshot the current delivery information in the skb, to generate 1464 * a rate sample later when the skb is (s)acked in tcp_rate_skb_delivered(). 1465 */ 1466 static void tcp_rate_skb_sent(struct sock *sk, struct sk_buff *skb) 1467 { 1468 struct tcp_sock *tp = tcp_sk(sk); 1469 1470 /* In general we need to start delivery rate samples from the 1471 * time we received the most recent ACK, to ensure we include 1472 * the full time the network needs to deliver all in-flight 1473 * packets. If there are no packets in flight yet, then we 1474 * know that any ACKs after now indicate that the network was 1475 * able to deliver those packets completely in the sampling 1476 * interval between now and the next ACK. 1477 * 1478 * Note that we use packets_out instead of tcp_packets_in_flight(tp) 1479 * because the latter is a guess based on RTO and loss-marking 1480 * heuristics. We don't want spurious RTOs or loss markings to cause 1481 * a spuriously small time interval, causing a spuriously high 1482 * bandwidth estimate. 1483 */ 1484 if (!tp->packets_out) { 1485 u64 tstamp_us = tcp_skb_timestamp_us(skb); 1486 1487 tp->first_tx_mstamp = tstamp_us; 1488 tp->delivered_mstamp = tstamp_us; 1489 } 1490 1491 TCP_SKB_CB(skb)->tx.first_tx_mstamp = tp->first_tx_mstamp; 1492 TCP_SKB_CB(skb)->tx.delivered_mstamp = tp->delivered_mstamp; 1493 TCP_SKB_CB(skb)->tx.delivered = tp->delivered; 1494 TCP_SKB_CB(skb)->tx.delivered_ce = tp->delivered_ce; 1495 TCP_SKB_CB(skb)->tx.is_app_limited = tp->app_limited ? 1 : 0; 1496 } 1497 1498 INDIRECT_CALLABLE_DECLARE(int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)); 1499 INDIRECT_CALLABLE_DECLARE(int inet6_csk_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)); 1500 1501 /* This routine computes an IPv4 TCP checksum. */ 1502 static void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb) 1503 { 1504 const struct inet_sock *inet = inet_sk(sk); 1505 1506 __tcp_v4_send_check(skb, inet->inet_saddr, inet->inet_daddr); 1507 } 1508 1509 #if IS_ENABLED(CONFIG_IPV6) 1510 #include <net/ip6_checksum.h> 1511 1512 static void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb) 1513 { 1514 __tcp_v6_send_check(skb, &sk->sk_v6_rcv_saddr, &sk->sk_v6_daddr); 1515 } 1516 #endif 1517 1518 /* This routine actually transmits TCP packets queued in by 1519 * tcp_do_sendmsg(). This is used by both the initial 1520 * transmission and possible later retransmissions. 1521 * All SKB's seen here are completely headerless. It is our 1522 * job to build the TCP header, and pass the packet down to 1523 * IP so it can do the same plus pass the packet off to the 1524 * device. 1525 * 1526 * We are working here with either a clone of the original 1527 * SKB, or a fresh unique copy made by the retransmit engine. 1528 */ 1529 static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, 1530 int clone_it, gfp_t gfp_mask, u32 rcv_nxt) 1531 { 1532 const struct inet_connection_sock *icsk = inet_csk(sk); 1533 struct inet_sock *inet; 1534 struct tcp_sock *tp; 1535 struct tcp_skb_cb *tcb; 1536 struct tcp_out_options opts; 1537 unsigned int tcp_options_size, tcp_header_size; 1538 struct sk_buff *oskb = NULL; 1539 struct tcp_key key; 1540 struct tcphdr *th; 1541 u64 prior_wstamp; 1542 int err; 1543 1544 BUG_ON(!skb || !tcp_skb_pcount(skb)); 1545 tp = tcp_sk(sk); 1546 prior_wstamp = tp->tcp_wstamp_ns; 1547 tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache); 1548 skb_set_delivery_time(skb, tp->tcp_wstamp_ns, SKB_CLOCK_MONOTONIC); 1549 if (clone_it) { 1550 oskb = skb; 1551 1552 tcp_skb_tsorted_save(oskb) { 1553 if (unlikely(skb_cloned(oskb))) 1554 skb = pskb_copy(oskb, gfp_mask); 1555 else 1556 skb = skb_clone(oskb, gfp_mask); 1557 } tcp_skb_tsorted_restore(oskb); 1558 1559 if (unlikely(!skb)) 1560 return -ENOBUFS; 1561 /* retransmit skbs might have a non zero value in skb->dev 1562 * because skb->dev is aliased with skb->rbnode.rb_left 1563 */ 1564 skb->dev = NULL; 1565 } 1566 1567 inet = inet_sk(sk); 1568 tcb = TCP_SKB_CB(skb); 1569 memset(&opts.cleared, 0, sizeof(opts.cleared)); 1570 1571 tcp_get_current_key(sk, &key); 1572 if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) { 1573 tcp_options_size = tcp_syn_options(sk, skb, &opts, &key); 1574 } else { 1575 tcp_options_size = tcp_established_options(sk, skb, &opts, &key); 1576 /* Force a PSH flag on all (GSO) packets to expedite GRO flush 1577 * at receiver : This slightly improve GRO performance. 1578 * Note that we do not force the PSH flag for non GSO packets, 1579 * because they might be sent under high congestion events, 1580 * and in this case it is better to delay the delivery of 1-MSS 1581 * packets and thus the corresponding ACK packet that would 1582 * release the following packet. 1583 */ 1584 if (tcp_skb_pcount(skb) > 1) 1585 tcb->tcp_flags |= TCPHDR_PSH; 1586 } 1587 tcp_header_size = tcp_options_size + sizeof(struct tcphdr); 1588 1589 /* We set skb->ooo_okay to one if this packet can select 1590 * a different TX queue than prior packets of this flow, 1591 * to avoid self inflicted reorders. 1592 * The 'other' queue decision is based on current cpu number 1593 * if XPS is enabled, or sk->sk_txhash otherwise. 1594 * We can switch to another (and better) queue if: 1595 * 1) No packet with payload is in qdisc/device queues. 1596 * Delays in TX completion can defeat the test 1597 * even if packets were already sent. 1598 * 2) Or rtx queue is empty. 1599 * This mitigates above case if ACK packets for 1600 * all prior packets were already processed. 1601 */ 1602 skb->ooo_okay = sk_wmem_alloc_get(sk) < SKB_TRUESIZE(1) || 1603 tcp_rtx_queue_empty(sk); 1604 1605 /* If we had to use memory reserve to allocate this skb, 1606 * this might cause drops if packet is looped back : 1607 * Other socket might not have SOCK_MEMALLOC. 1608 * Packets not looped back do not care about pfmemalloc. 1609 */ 1610 skb->pfmemalloc = 0; 1611 1612 __skb_push(skb, tcp_header_size); 1613 skb_reset_transport_header(skb); 1614 1615 skb_orphan(skb); 1616 skb->sk = sk; 1617 skb->destructor = skb_is_tcp_pure_ack(skb) ? __sock_wfree : tcp_wfree; 1618 refcount_add(skb->truesize, &sk->sk_wmem_alloc); 1619 1620 skb_set_dst_pending_confirm(skb, READ_ONCE(sk->sk_dst_pending_confirm)); 1621 1622 /* Build TCP header and checksum it. */ 1623 th = (struct tcphdr *)skb->data; 1624 th->source = inet->inet_sport; 1625 th->dest = inet->inet_dport; 1626 th->seq = htonl(tcb->seq); 1627 th->ack_seq = htonl(rcv_nxt); 1628 *(((__be16 *)th) + 6) = htons(((tcp_header_size >> 2) << 12) | 1629 (tcb->tcp_flags & TCPHDR_FLAGS_MASK)); 1630 1631 th->check = 0; 1632 th->urg_ptr = 0; 1633 1634 /* The urg_mode check is necessary during a below snd_una win probe */ 1635 if (unlikely(tcp_urg_mode(tp) && before(tcb->seq, tp->snd_up))) { 1636 if (before(tp->snd_up, tcb->seq + 0x10000)) { 1637 th->urg_ptr = htons(tp->snd_up - tcb->seq); 1638 th->urg = 1; 1639 } else if (after(tcb->seq + 0xFFFF, tp->snd_nxt)) { 1640 th->urg_ptr = htons(0xFFFF); 1641 th->urg = 1; 1642 } 1643 } 1644 1645 skb_shinfo(skb)->gso_type = sk->sk_gso_type; 1646 if (likely(!(tcb->tcp_flags & TCPHDR_SYN))) { 1647 th->window = htons(tcp_select_window(sk)); 1648 tcp_ecn_send(sk, skb, th, tcp_header_size); 1649 } else { 1650 /* RFC1323: The window in SYN & SYN/ACK segments 1651 * is never scaled. 1652 */ 1653 th->window = htons(min(tp->rcv_wnd, 65535U)); 1654 } 1655 1656 tcp_options_write(th, tp, NULL, &opts, &key); 1657 1658 if (tcp_key_is_md5(&key)) { 1659 #ifdef CONFIG_TCP_MD5SIG 1660 /* Calculate the MD5 hash, as we have all we need now */ 1661 sk_gso_disable(sk); 1662 tp->af_specific->calc_md5_hash(opts.hash_location, 1663 key.md5_key, sk, skb); 1664 #endif 1665 } else if (tcp_key_is_ao(&key)) { 1666 int err; 1667 1668 err = tcp_ao_transmit_skb(sk, skb, key.ao_key, th, 1669 opts.hash_location); 1670 if (err) { 1671 sk_skb_reason_drop(sk, skb, SKB_DROP_REASON_NOT_SPECIFIED); 1672 return -ENOMEM; 1673 } 1674 } 1675 1676 /* BPF prog is the last one writing header option */ 1677 bpf_skops_write_hdr_opt(sk, skb, NULL, NULL, 0, &opts); 1678 1679 #if IS_ENABLED(CONFIG_IPV6) 1680 if (likely(icsk->icsk_af_ops->net_header_len == sizeof(struct ipv6hdr))) 1681 tcp_v6_send_check(sk, skb); 1682 else 1683 #endif 1684 tcp_v4_send_check(sk, skb); 1685 1686 if (likely(tcb->tcp_flags & TCPHDR_ACK)) 1687 tcp_event_ack_sent(sk, rcv_nxt); 1688 1689 if (skb->len != tcp_header_size) { 1690 tcp_event_data_sent(tp, sk); 1691 WRITE_ONCE(tp->data_segs_out, 1692 tp->data_segs_out + tcp_skb_pcount(skb)); 1693 WRITE_ONCE(tp->bytes_sent, 1694 tp->bytes_sent + skb->len - tcp_header_size); 1695 } 1696 1697 if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq) 1698 TCP_ADD_STATS(sock_net(sk), TCP_MIB_OUTSEGS, 1699 tcp_skb_pcount(skb)); 1700 1701 tp->segs_out += tcp_skb_pcount(skb); 1702 skb_set_hash_from_sk(skb, sk); 1703 /* OK, its time to fill skb_shinfo(skb)->gso_{segs|size} */ 1704 skb_shinfo(skb)->gso_segs = tcp_skb_pcount(skb); 1705 skb_shinfo(skb)->gso_size = tcp_skb_mss(skb); 1706 1707 /* Leave earliest departure time in skb->tstamp (skb->skb_mstamp_ns) */ 1708 1709 /* Cleanup our debris for IP stacks */ 1710 memset(skb->cb, 0, max(sizeof(struct inet_skb_parm), 1711 sizeof(struct inet6_skb_parm))); 1712 1713 tcp_add_tx_delay(skb, tp); 1714 1715 err = INDIRECT_CALL_INET(icsk->icsk_af_ops->queue_xmit, 1716 inet6_csk_xmit, ip_queue_xmit, 1717 sk, skb, &inet->cork.fl); 1718 1719 if (unlikely(err > 0)) { 1720 tcp_enter_cwr(sk); 1721 err = net_xmit_eval(err); 1722 } 1723 if (!err && oskb) { 1724 tcp_update_skb_after_send(sk, oskb, prior_wstamp); 1725 tcp_rate_skb_sent(sk, oskb); 1726 } 1727 return err; 1728 } 1729 1730 static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it, 1731 gfp_t gfp_mask) 1732 { 1733 return __tcp_transmit_skb(sk, skb, clone_it, gfp_mask, 1734 tcp_sk(sk)->rcv_nxt); 1735 } 1736 1737 /* This routine just queues the buffer for sending. 1738 * 1739 * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames, 1740 * otherwise socket can stall. 1741 */ 1742 static void tcp_queue_skb(struct sock *sk, struct sk_buff *skb) 1743 { 1744 struct tcp_sock *tp = tcp_sk(sk); 1745 1746 /* Advance write_seq and place onto the write_queue. */ 1747 WRITE_ONCE(tp->write_seq, TCP_SKB_CB(skb)->end_seq); 1748 __skb_header_release(skb); 1749 psp_enqueue_set_decrypted(sk, skb); 1750 tcp_add_write_queue_tail(sk, skb); 1751 sk_wmem_queued_add(sk, skb->truesize); 1752 sk_mem_charge(sk, skb->truesize); 1753 } 1754 1755 /* Initialize TSO segments for a packet. */ 1756 static int tcp_set_skb_tso_segs(struct sk_buff *skb, unsigned int mss_now) 1757 { 1758 int tso_segs; 1759 1760 if (skb->len <= mss_now) { 1761 /* Avoid the costly divide in the normal 1762 * non-TSO case. 1763 */ 1764 TCP_SKB_CB(skb)->tcp_gso_size = 0; 1765 tcp_skb_pcount_set(skb, 1); 1766 return 1; 1767 } 1768 TCP_SKB_CB(skb)->tcp_gso_size = mss_now; 1769 tso_segs = DIV_ROUND_UP(skb->len, mss_now); 1770 tcp_skb_pcount_set(skb, tso_segs); 1771 return tso_segs; 1772 } 1773 1774 /* Pcount in the middle of the write queue got changed, we need to do various 1775 * tweaks to fix counters 1776 */ 1777 static void tcp_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int decr) 1778 { 1779 struct tcp_sock *tp = tcp_sk(sk); 1780 1781 tp->packets_out -= decr; 1782 1783 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) 1784 tp->sacked_out -= decr; 1785 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) 1786 tp->retrans_out -= decr; 1787 if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) 1788 tp->lost_out -= decr; 1789 1790 /* Reno case is special. Sigh... */ 1791 if (tcp_is_reno(tp) && decr > 0) 1792 tp->sacked_out -= min_t(u32, tp->sacked_out, decr); 1793 1794 tcp_verify_left_out(tp); 1795 } 1796 1797 static bool tcp_has_tx_tstamp(const struct sk_buff *skb) 1798 { 1799 return TCP_SKB_CB(skb)->txstamp_ack || 1800 (skb_shinfo(skb)->tx_flags & SKBTX_ANY_TSTAMP); 1801 } 1802 1803 static void tcp_fragment_tstamp(struct sk_buff *skb, struct sk_buff *skb2) 1804 { 1805 struct skb_shared_info *shinfo = skb_shinfo(skb); 1806 1807 if (unlikely(tcp_has_tx_tstamp(skb)) && 1808 !before(shinfo->tskey, TCP_SKB_CB(skb2)->seq)) { 1809 struct skb_shared_info *shinfo2 = skb_shinfo(skb2); 1810 u8 tsflags = shinfo->tx_flags & SKBTX_ANY_TSTAMP; 1811 1812 shinfo->tx_flags &= ~tsflags; 1813 shinfo2->tx_flags |= tsflags; 1814 swap(shinfo->tskey, shinfo2->tskey); 1815 TCP_SKB_CB(skb2)->txstamp_ack = TCP_SKB_CB(skb)->txstamp_ack; 1816 TCP_SKB_CB(skb)->txstamp_ack = 0; 1817 } 1818 } 1819 1820 static void tcp_skb_fragment_eor(struct sk_buff *skb, struct sk_buff *skb2) 1821 { 1822 TCP_SKB_CB(skb2)->eor = TCP_SKB_CB(skb)->eor; 1823 TCP_SKB_CB(skb)->eor = 0; 1824 } 1825 1826 /* Insert buff after skb on the write or rtx queue of sk. */ 1827 static void tcp_insert_write_queue_after(struct sk_buff *skb, 1828 struct sk_buff *buff, 1829 struct sock *sk, 1830 enum tcp_queue tcp_queue) 1831 { 1832 if (tcp_queue == TCP_FRAG_IN_WRITE_QUEUE) 1833 __skb_queue_after(&sk->sk_write_queue, skb, buff); 1834 else 1835 tcp_rbtree_insert(&sk->tcp_rtx_queue, buff); 1836 } 1837 1838 /* Function to create two new TCP segments. Shrinks the given segment 1839 * to the specified size and appends a new segment with the rest of the 1840 * packet to the list. This won't be called frequently, I hope. 1841 * Remember, these are still headerless SKBs at this point. 1842 */ 1843 int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue, 1844 struct sk_buff *skb, u32 len, 1845 unsigned int mss_now, gfp_t gfp) 1846 { 1847 struct tcp_sock *tp = tcp_sk(sk); 1848 struct sk_buff *buff; 1849 int old_factor; 1850 long limit; 1851 u16 flags; 1852 int nlen; 1853 1854 if (WARN_ON(len > skb->len)) 1855 return -EINVAL; 1856 1857 DEBUG_NET_WARN_ON_ONCE(skb_headlen(skb)); 1858 1859 /* tcp_sendmsg() can overshoot sk_wmem_queued by one full size skb. 1860 * We need some allowance to not penalize applications setting small 1861 * SO_SNDBUF values. 1862 * Also allow first and last skb in retransmit queue to be split. 1863 */ 1864 limit = sk->sk_sndbuf + 2 * SKB_TRUESIZE(GSO_LEGACY_MAX_SIZE); 1865 if (unlikely((sk->sk_wmem_queued >> 1) > limit && 1866 tcp_queue != TCP_FRAG_IN_WRITE_QUEUE && 1867 skb != tcp_rtx_queue_head(sk) && 1868 skb != tcp_rtx_queue_tail(sk))) { 1869 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPWQUEUETOOBIG); 1870 return -ENOMEM; 1871 } 1872 1873 if (skb_unclone_keeptruesize(skb, gfp)) 1874 return -ENOMEM; 1875 1876 /* Get a new skb... force flag on. */ 1877 buff = tcp_stream_alloc_skb(sk, gfp, true); 1878 if (!buff) 1879 return -ENOMEM; /* We'll just try again later. */ 1880 skb_copy_decrypted(buff, skb); 1881 mptcp_skb_ext_copy(buff, skb); 1882 1883 sk_wmem_queued_add(sk, buff->truesize); 1884 sk_mem_charge(sk, buff->truesize); 1885 nlen = skb->len - len; 1886 buff->truesize += nlen; 1887 skb->truesize -= nlen; 1888 1889 /* Correct the sequence numbers. */ 1890 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len; 1891 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq; 1892 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq; 1893 1894 /* PSH and FIN should only be set in the second packet. */ 1895 flags = TCP_SKB_CB(skb)->tcp_flags; 1896 TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH); 1897 TCP_SKB_CB(buff)->tcp_flags = flags; 1898 TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked; 1899 tcp_skb_fragment_eor(skb, buff); 1900 1901 skb_split(skb, buff, len); 1902 1903 skb_set_delivery_time(buff, skb->tstamp, SKB_CLOCK_MONOTONIC); 1904 tcp_fragment_tstamp(skb, buff); 1905 1906 old_factor = tcp_skb_pcount(skb); 1907 1908 /* Fix up tso_factor for both original and new SKB. */ 1909 tcp_set_skb_tso_segs(skb, mss_now); 1910 tcp_set_skb_tso_segs(buff, mss_now); 1911 1912 /* Update delivered info for the new segment */ 1913 TCP_SKB_CB(buff)->tx = TCP_SKB_CB(skb)->tx; 1914 1915 /* If this packet has been sent out already, we must 1916 * adjust the various packet counters. 1917 */ 1918 if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) { 1919 int diff = old_factor - tcp_skb_pcount(skb) - 1920 tcp_skb_pcount(buff); 1921 1922 if (diff) 1923 tcp_adjust_pcount(sk, skb, diff); 1924 } 1925 1926 /* Link BUFF into the send queue. */ 1927 __skb_header_release(buff); 1928 tcp_insert_write_queue_after(skb, buff, sk, tcp_queue); 1929 if (tcp_queue == TCP_FRAG_IN_RTX_QUEUE) 1930 list_add(&buff->tcp_tsorted_anchor, &skb->tcp_tsorted_anchor); 1931 1932 return 0; 1933 } 1934 1935 /* This is similar to __pskb_pull_tail(). The difference is that pulled 1936 * data is not copied, but immediately discarded. 1937 */ 1938 static int __pskb_trim_head(struct sk_buff *skb, int len) 1939 { 1940 struct skb_shared_info *shinfo; 1941 int i, k, eat; 1942 1943 DEBUG_NET_WARN_ON_ONCE(skb_headlen(skb)); 1944 eat = len; 1945 k = 0; 1946 shinfo = skb_shinfo(skb); 1947 for (i = 0; i < shinfo->nr_frags; i++) { 1948 int size = skb_frag_size(&shinfo->frags[i]); 1949 1950 if (size <= eat) { 1951 skb_frag_unref(skb, i); 1952 eat -= size; 1953 } else { 1954 shinfo->frags[k] = shinfo->frags[i]; 1955 if (eat) { 1956 skb_frag_off_add(&shinfo->frags[k], eat); 1957 skb_frag_size_sub(&shinfo->frags[k], eat); 1958 eat = 0; 1959 } 1960 k++; 1961 } 1962 } 1963 shinfo->nr_frags = k; 1964 1965 skb->data_len -= len; 1966 skb->len = skb->data_len; 1967 return len; 1968 } 1969 1970 /* Remove acked data from a packet in the transmit queue. */ 1971 int tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len) 1972 { 1973 u32 delta_truesize; 1974 1975 if (skb_unclone_keeptruesize(skb, GFP_ATOMIC)) 1976 return -ENOMEM; 1977 1978 delta_truesize = __pskb_trim_head(skb, len); 1979 1980 TCP_SKB_CB(skb)->seq += len; 1981 1982 skb->truesize -= delta_truesize; 1983 sk_wmem_queued_add(sk, -delta_truesize); 1984 if (!skb_zcopy_pure(skb)) 1985 sk_mem_uncharge(sk, delta_truesize); 1986 1987 /* Any change of skb->len requires recalculation of tso factor. */ 1988 if (tcp_skb_pcount(skb) > 1) 1989 tcp_set_skb_tso_segs(skb, tcp_skb_mss(skb)); 1990 1991 return 0; 1992 } 1993 1994 /* Calculate MSS not accounting any TCP options. */ 1995 static inline int __tcp_mtu_to_mss(struct sock *sk, int pmtu) 1996 { 1997 const struct tcp_sock *tp = tcp_sk(sk); 1998 const struct inet_connection_sock *icsk = inet_csk(sk); 1999 int mss_now; 2000 2001 /* Calculate base mss without TCP options: 2002 It is MMS_S - sizeof(tcphdr) of rfc1122 2003 */ 2004 mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr); 2005 2006 /* Clamp it (mss_clamp does not include tcp options) */ 2007 if (mss_now > tp->rx_opt.mss_clamp) 2008 mss_now = tp->rx_opt.mss_clamp; 2009 2010 /* Now subtract optional transport overhead */ 2011 mss_now -= icsk->icsk_ext_hdr_len; 2012 2013 /* Then reserve room for full set of TCP options and 8 bytes of data */ 2014 mss_now = max(mss_now, 2015 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_snd_mss)); 2016 return mss_now; 2017 } 2018 2019 /* Calculate MSS. Not accounting for SACKs here. */ 2020 int tcp_mtu_to_mss(struct sock *sk, int pmtu) 2021 { 2022 /* Subtract TCP options size, not including SACKs */ 2023 return __tcp_mtu_to_mss(sk, pmtu) - 2024 (tcp_sk(sk)->tcp_header_len - sizeof(struct tcphdr)); 2025 } 2026 2027 /* Inverse of above */ 2028 int tcp_mss_to_mtu(struct sock *sk, int mss) 2029 { 2030 const struct tcp_sock *tp = tcp_sk(sk); 2031 const struct inet_connection_sock *icsk = inet_csk(sk); 2032 2033 return mss + 2034 tp->tcp_header_len + 2035 icsk->icsk_ext_hdr_len + 2036 icsk->icsk_af_ops->net_header_len; 2037 } 2038 EXPORT_SYMBOL(tcp_mss_to_mtu); 2039 2040 /* MTU probing init per socket */ 2041 void tcp_mtup_init(struct sock *sk) 2042 { 2043 struct tcp_sock *tp = tcp_sk(sk); 2044 struct inet_connection_sock *icsk = inet_csk(sk); 2045 struct net *net = sock_net(sk); 2046 2047 icsk->icsk_mtup.enabled = READ_ONCE(net->ipv4.sysctl_tcp_mtu_probing) > 1; 2048 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) + 2049 icsk->icsk_af_ops->net_header_len; 2050 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, READ_ONCE(net->ipv4.sysctl_tcp_base_mss)); 2051 icsk->icsk_mtup.probe_size = 0; 2052 if (icsk->icsk_mtup.enabled) 2053 icsk->icsk_mtup.probe_timestamp = tcp_jiffies32; 2054 } 2055 2056 /* This function synchronize snd mss to current pmtu/exthdr set. 2057 2058 tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts 2059 for TCP options, but includes only bare TCP header. 2060 2061 tp->rx_opt.mss_clamp is mss negotiated at connection setup. 2062 It is minimum of user_mss and mss received with SYN. 2063 It also does not include TCP options. 2064 2065 inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function. 2066 2067 tp->mss_cache is current effective sending mss, including 2068 all tcp options except for SACKs. It is evaluated, 2069 taking into account current pmtu, but never exceeds 2070 tp->rx_opt.mss_clamp. 2071 2072 NOTE1. rfc1122 clearly states that advertised MSS 2073 DOES NOT include either tcp or ip options. 2074 2075 NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache 2076 are READ ONLY outside this function. --ANK (980731) 2077 */ 2078 unsigned int tcp_sync_mss(struct sock *sk, u32 pmtu) 2079 { 2080 struct tcp_sock *tp = tcp_sk(sk); 2081 struct inet_connection_sock *icsk = inet_csk(sk); 2082 int mss_now; 2083 2084 if (icsk->icsk_mtup.search_high > pmtu) 2085 icsk->icsk_mtup.search_high = pmtu; 2086 2087 mss_now = tcp_mtu_to_mss(sk, pmtu); 2088 mss_now = tcp_bound_to_half_wnd(tp, mss_now); 2089 2090 /* And store cached results */ 2091 icsk->icsk_pmtu_cookie = pmtu; 2092 if (icsk->icsk_mtup.enabled) 2093 mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low)); 2094 tp->mss_cache = mss_now; 2095 2096 return mss_now; 2097 } 2098 2099 /* Compute the current effective MSS, taking SACKs and IP options, 2100 * and even PMTU discovery events into account. 2101 */ 2102 unsigned int tcp_current_mss(struct sock *sk) 2103 { 2104 const struct tcp_sock *tp = tcp_sk(sk); 2105 const struct dst_entry *dst = __sk_dst_get(sk); 2106 u32 mss_now; 2107 unsigned int header_len; 2108 struct tcp_out_options opts; 2109 struct tcp_key key; 2110 2111 mss_now = tp->mss_cache; 2112 2113 if (dst) { 2114 u32 mtu = dst_mtu(dst); 2115 if (mtu != inet_csk(sk)->icsk_pmtu_cookie) 2116 mss_now = tcp_sync_mss(sk, mtu); 2117 } 2118 tcp_get_current_key(sk, &key); 2119 header_len = tcp_established_options(sk, NULL, &opts, &key) + 2120 sizeof(struct tcphdr); 2121 /* The mss_cache is sized based on tp->tcp_header_len, which assumes 2122 * some common options. If this is an odd packet (because we have SACK 2123 * blocks etc) then our calculated header_len will be different, and 2124 * we have to adjust mss_now correspondingly */ 2125 if (header_len != tp->tcp_header_len) { 2126 int delta = (int) header_len - tp->tcp_header_len; 2127 mss_now -= delta; 2128 } 2129 2130 return mss_now; 2131 } 2132 2133 /* RFC2861, slow part. Adjust cwnd, after it was not full during one rto. 2134 * As additional protections, we do not touch cwnd in retransmission phases, 2135 * and if application hit its sndbuf limit recently. 2136 */ 2137 static void tcp_cwnd_application_limited(struct sock *sk) 2138 { 2139 struct tcp_sock *tp = tcp_sk(sk); 2140 2141 if (inet_csk(sk)->icsk_ca_state == TCP_CA_Open && 2142 sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 2143 /* Limited by application or receiver window. */ 2144 u32 init_win = tcp_init_cwnd(tp, __sk_dst_get(sk)); 2145 u32 win_used = max(tp->snd_cwnd_used, init_win); 2146 if (win_used < tcp_snd_cwnd(tp)) { 2147 WRITE_ONCE(tp->snd_ssthresh, tcp_current_ssthresh(sk)); 2148 tcp_snd_cwnd_set(tp, (tcp_snd_cwnd(tp) + win_used) >> 1); 2149 } 2150 tp->snd_cwnd_used = 0; 2151 } 2152 tp->snd_cwnd_stamp = tcp_jiffies32; 2153 } 2154 2155 static void tcp_cwnd_validate(struct sock *sk, bool is_cwnd_limited) 2156 { 2157 const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; 2158 struct tcp_sock *tp = tcp_sk(sk); 2159 2160 /* Track the strongest available signal of the degree to which the cwnd 2161 * is fully utilized. If cwnd-limited then remember that fact for the 2162 * current window. If not cwnd-limited then track the maximum number of 2163 * outstanding packets in the current window. (If cwnd-limited then we 2164 * chose to not update tp->max_packets_out to avoid an extra else 2165 * clause with no functional impact.) 2166 */ 2167 if (!before(tp->snd_una, tp->cwnd_usage_seq) || 2168 is_cwnd_limited || 2169 (!tp->is_cwnd_limited && 2170 tp->packets_out > tp->max_packets_out)) { 2171 tp->is_cwnd_limited = is_cwnd_limited; 2172 tp->max_packets_out = tp->packets_out; 2173 tp->cwnd_usage_seq = tp->snd_nxt; 2174 } 2175 2176 if (tcp_is_cwnd_limited(sk)) { 2177 /* Network is feed fully. */ 2178 tp->snd_cwnd_used = 0; 2179 tp->snd_cwnd_stamp = tcp_jiffies32; 2180 } else { 2181 /* Network starves. */ 2182 if (tp->packets_out > tp->snd_cwnd_used) 2183 tp->snd_cwnd_used = tp->packets_out; 2184 2185 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle) && 2186 (s32)(tcp_jiffies32 - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto && 2187 !ca_ops->cong_control) 2188 tcp_cwnd_application_limited(sk); 2189 2190 /* The following conditions together indicate the starvation 2191 * is caused by insufficient sender buffer: 2192 * 1) just sent some data (see tcp_write_xmit) 2193 * 2) not cwnd limited (this else condition) 2194 * 3) no more data to send (tcp_write_queue_empty()) 2195 * 4) application is hitting buffer limit (SOCK_NOSPACE) 2196 */ 2197 if (tcp_write_queue_empty(sk) && sk->sk_socket && 2198 test_bit(SOCK_NOSPACE, &sk->sk_socket->flags) && 2199 (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) 2200 tcp_chrono_start(sk, TCP_CHRONO_SNDBUF_LIMITED); 2201 } 2202 } 2203 2204 /* Minshall's variant of the Nagle send check. */ 2205 static bool tcp_minshall_check(const struct tcp_sock *tp) 2206 { 2207 return after(tp->snd_sml, tp->snd_una) && 2208 !after(tp->snd_sml, tp->snd_nxt); 2209 } 2210 2211 /* Update snd_sml if this skb is under mss 2212 * Note that a TSO packet might end with a sub-mss segment 2213 * The test is really : 2214 * if ((skb->len % mss) != 0) 2215 * tp->snd_sml = TCP_SKB_CB(skb)->end_seq; 2216 * But we can avoid doing the divide again given we already have 2217 * skb_pcount = skb->len / mss_now 2218 */ 2219 static void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss_now, 2220 const struct sk_buff *skb) 2221 { 2222 if (skb->len < tcp_skb_pcount(skb) * mss_now) 2223 tp->snd_sml = TCP_SKB_CB(skb)->end_seq; 2224 } 2225 2226 /* Return false, if packet can be sent now without violation Nagle's rules: 2227 * 1. It is full sized. (provided by caller in %partial bool) 2228 * 2. Or it contains FIN. (already checked by caller) 2229 * 3. Or TCP_CORK is not set, and TCP_NODELAY is set. 2230 * 4. Or TCP_CORK is not set, and all sent packets are ACKed. 2231 * With Minshall's modification: all sent small packets are ACKed. 2232 */ 2233 static bool tcp_nagle_check(bool partial, const struct tcp_sock *tp, 2234 int nonagle) 2235 { 2236 return partial && 2237 ((nonagle & TCP_NAGLE_CORK) || 2238 (!nonagle && tp->packets_out && tcp_minshall_check(tp))); 2239 } 2240 2241 /* Return how many segs we'd like on a TSO packet, 2242 * depending on current pacing rate, and how close the peer is. 2243 * 2244 * Rationale is: 2245 * - For close peers, we rather send bigger packets to reduce 2246 * cpu costs, because occasional losses will be repaired fast. 2247 * - For long distance/rtt flows, we would like to get ACK clocking 2248 * with 1 ACK per ms. 2249 * 2250 * Use min_rtt to help adapt TSO burst size, with smaller min_rtt resulting 2251 * in bigger TSO bursts. We we cut the RTT-based allowance in half 2252 * for every 2^9 usec (aka 512 us) of RTT, so that the RTT-based allowance 2253 * is below 1500 bytes after 6 * ~500 usec = 3ms. 2254 */ 2255 static u32 tcp_tso_autosize(const struct sock *sk, unsigned int mss_now, 2256 int min_tso_segs) 2257 { 2258 unsigned long bytes; 2259 u32 r; 2260 2261 bytes = READ_ONCE(sk->sk_pacing_rate) >> READ_ONCE(sk->sk_pacing_shift); 2262 2263 r = tcp_min_rtt(tcp_sk(sk)) >> READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_tso_rtt_log); 2264 if (r < BITS_PER_TYPE(sk->sk_gso_max_size)) 2265 bytes += sk->sk_gso_max_size >> r; 2266 2267 bytes = min_t(unsigned long, bytes, sk->sk_gso_max_size); 2268 2269 return max_t(u32, bytes / mss_now, min_tso_segs); 2270 } 2271 2272 /* Return the number of segments we want in the skb we are transmitting. 2273 * See if congestion control module wants to decide; otherwise, autosize. 2274 */ 2275 static u32 tcp_tso_segs(struct sock *sk, unsigned int mss_now) 2276 { 2277 const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; 2278 u32 min_tso, tso_segs; 2279 2280 min_tso = ca_ops->min_tso_segs ? 2281 ca_ops->min_tso_segs(sk) : 2282 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_tso_segs); 2283 2284 tso_segs = tcp_tso_autosize(sk, mss_now, min_tso); 2285 return min_t(u32, tso_segs, sk->sk_gso_max_segs); 2286 } 2287 2288 /* Returns the portion of skb which can be sent right away */ 2289 static unsigned int tcp_mss_split_point(const struct sock *sk, 2290 const struct sk_buff *skb, 2291 unsigned int mss_now, 2292 unsigned int max_segs, 2293 int nonagle) 2294 { 2295 const struct tcp_sock *tp = tcp_sk(sk); 2296 u32 partial, needed, window, max_len; 2297 2298 window = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; 2299 max_len = mss_now * max_segs; 2300 2301 if (likely(max_len <= window && skb != tcp_write_queue_tail(sk))) 2302 return max_len; 2303 2304 needed = min(skb->len, window); 2305 2306 if (max_len <= needed) 2307 return max_len; 2308 2309 partial = needed % mss_now; 2310 /* If last segment is not a full MSS, check if Nagle rules allow us 2311 * to include this last segment in this skb. 2312 * Otherwise, we'll split the skb at last MSS boundary 2313 */ 2314 if (tcp_nagle_check(partial != 0, tp, nonagle)) 2315 return needed - partial; 2316 2317 return needed; 2318 } 2319 2320 /* Can at least one segment of SKB be sent right now, according to the 2321 * congestion window rules? If so, return how many segments are allowed. 2322 */ 2323 static u32 tcp_cwnd_test(const struct tcp_sock *tp) 2324 { 2325 u32 in_flight, cwnd, halfcwnd; 2326 2327 in_flight = tcp_packets_in_flight(tp); 2328 cwnd = tcp_snd_cwnd(tp); 2329 if (in_flight >= cwnd) 2330 return 0; 2331 2332 /* For better scheduling, ensure we have at least 2333 * 2 GSO packets in flight. 2334 */ 2335 halfcwnd = max(cwnd >> 1, 1U); 2336 return min(halfcwnd, cwnd - in_flight); 2337 } 2338 2339 /* Initialize TSO state of a skb. 2340 * This must be invoked the first time we consider transmitting 2341 * SKB onto the wire. 2342 */ 2343 static int tcp_init_tso_segs(struct sk_buff *skb, unsigned int mss_now) 2344 { 2345 int tso_segs = tcp_skb_pcount(skb); 2346 2347 if (!tso_segs || (tso_segs > 1 && tcp_skb_mss(skb) != mss_now)) 2348 return tcp_set_skb_tso_segs(skb, mss_now); 2349 2350 return tso_segs; 2351 } 2352 2353 2354 /* Return true if the Nagle test allows this packet to be 2355 * sent now. 2356 */ 2357 static inline bool tcp_nagle_test(const struct tcp_sock *tp, const struct sk_buff *skb, 2358 unsigned int cur_mss, int nonagle) 2359 { 2360 /* Nagle rule does not apply to frames, which sit in the middle of the 2361 * write_queue (they have no chances to get new data). 2362 * 2363 * This is implemented in the callers, where they modify the 'nonagle' 2364 * argument based upon the location of SKB in the send queue. 2365 */ 2366 if (nonagle & TCP_NAGLE_PUSH) 2367 return true; 2368 2369 /* Don't use the nagle rule for urgent data (or for the final FIN). */ 2370 if (tcp_urg_mode(tp) || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) 2371 return true; 2372 2373 if (!tcp_nagle_check(skb->len < cur_mss, tp, nonagle)) 2374 return true; 2375 2376 return false; 2377 } 2378 2379 /* Does at least the first segment of SKB fit into the send window? */ 2380 static bool tcp_snd_wnd_test(const struct tcp_sock *tp, 2381 const struct sk_buff *skb, 2382 unsigned int cur_mss) 2383 { 2384 u32 end_seq = TCP_SKB_CB(skb)->end_seq; 2385 2386 if (skb->len > cur_mss) 2387 end_seq = TCP_SKB_CB(skb)->seq + cur_mss; 2388 2389 return !after(end_seq, tcp_wnd_end(tp)); 2390 } 2391 2392 /* Trim TSO SKB to LEN bytes, put the remaining data into a new packet 2393 * which is put after SKB on the list. It is very much like 2394 * tcp_fragment() except that it may make several kinds of assumptions 2395 * in order to speed up the splitting operation. In particular, we 2396 * know that all the data is in scatter-gather pages, and that the 2397 * packet has never been sent out before (and thus is not cloned). 2398 */ 2399 static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len, 2400 unsigned int mss_now, gfp_t gfp) 2401 { 2402 int nlen = skb->len - len; 2403 struct sk_buff *buff; 2404 u16 flags; 2405 2406 /* All of a TSO frame must be composed of paged data. */ 2407 DEBUG_NET_WARN_ON_ONCE(skb->len != skb->data_len); 2408 2409 buff = tcp_stream_alloc_skb(sk, gfp, true); 2410 if (unlikely(!buff)) 2411 return -ENOMEM; 2412 skb_copy_decrypted(buff, skb); 2413 mptcp_skb_ext_copy(buff, skb); 2414 2415 sk_wmem_queued_add(sk, buff->truesize); 2416 sk_mem_charge(sk, buff->truesize); 2417 buff->truesize += nlen; 2418 skb->truesize -= nlen; 2419 2420 /* Correct the sequence numbers. */ 2421 TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len; 2422 TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq; 2423 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq; 2424 2425 /* PSH and FIN should only be set in the second packet. */ 2426 flags = TCP_SKB_CB(skb)->tcp_flags; 2427 TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH); 2428 TCP_SKB_CB(buff)->tcp_flags = flags; 2429 2430 tcp_skb_fragment_eor(skb, buff); 2431 2432 skb_split(skb, buff, len); 2433 tcp_fragment_tstamp(skb, buff); 2434 2435 /* Fix up tso_factor for both original and new SKB. */ 2436 tcp_set_skb_tso_segs(skb, mss_now); 2437 tcp_set_skb_tso_segs(buff, mss_now); 2438 2439 /* Link BUFF into the send queue. */ 2440 __skb_header_release(buff); 2441 tcp_insert_write_queue_after(skb, buff, sk, TCP_FRAG_IN_WRITE_QUEUE); 2442 2443 return 0; 2444 } 2445 2446 /* Try to defer sending, if possible, in order to minimize the amount 2447 * of TSO splitting we do. View it as a kind of TSO Nagle test. 2448 * 2449 * This algorithm is from John Heffner. 2450 */ 2451 static bool tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb, 2452 bool *is_cwnd_limited, 2453 bool *is_rwnd_limited, 2454 u32 max_segs) 2455 { 2456 const struct inet_connection_sock *icsk = inet_csk(sk); 2457 u32 send_win, cong_win, limit, in_flight, threshold; 2458 u64 srtt_in_ns, expected_ack, how_far_is_the_ack; 2459 struct tcp_sock *tp = tcp_sk(sk); 2460 struct sk_buff *head; 2461 int win_divisor; 2462 s64 delta; 2463 2464 if (icsk->icsk_ca_state >= TCP_CA_Recovery) 2465 goto send_now; 2466 2467 /* Avoid bursty behavior by allowing defer 2468 * only if the last write was recent (1 ms). 2469 * Note that tp->tcp_wstamp_ns can be in the future if we have 2470 * packets waiting in a qdisc or device for EDT delivery. 2471 */ 2472 delta = tp->tcp_clock_cache - tp->tcp_wstamp_ns - NSEC_PER_MSEC; 2473 if (delta > 0) 2474 goto send_now; 2475 2476 in_flight = tcp_packets_in_flight(tp); 2477 2478 BUG_ON(tcp_skb_pcount(skb) <= 1); 2479 BUG_ON(tcp_snd_cwnd(tp) <= in_flight); 2480 2481 send_win = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; 2482 2483 /* From in_flight test above, we know that cwnd > in_flight. */ 2484 cong_win = (tcp_snd_cwnd(tp) - in_flight) * tp->mss_cache; 2485 2486 limit = min(send_win, cong_win); 2487 2488 /* If a full-sized TSO skb can be sent, do it. */ 2489 if (limit >= max_segs * tp->mss_cache) 2490 goto send_now; 2491 2492 /* Middle in queue won't get any more data, full sendable already? */ 2493 if ((skb != tcp_write_queue_tail(sk)) && (limit >= skb->len)) 2494 goto send_now; 2495 2496 win_divisor = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_tso_win_divisor); 2497 if (win_divisor) { 2498 u32 chunk = min(tp->snd_wnd, tcp_snd_cwnd(tp) * tp->mss_cache); 2499 2500 /* If at least some fraction of a window is available, 2501 * just use it. 2502 */ 2503 chunk /= win_divisor; 2504 if (limit >= chunk) 2505 goto send_now; 2506 } else { 2507 /* Different approach, try not to defer past a single 2508 * ACK. Receiver should ACK every other full sized 2509 * frame, so if we have space for more than 3 frames 2510 * then send now. 2511 */ 2512 if (limit > tcp_max_tso_deferred_mss(tp) * tp->mss_cache) 2513 goto send_now; 2514 } 2515 2516 /* TODO : use tsorted_sent_queue ? */ 2517 head = tcp_rtx_queue_head(sk); 2518 if (!head) 2519 goto send_now; 2520 2521 srtt_in_ns = (u64)(NSEC_PER_USEC >> 3) * tp->srtt_us; 2522 /* When is the ACK expected ? */ 2523 expected_ack = head->tstamp + srtt_in_ns; 2524 /* How far from now is the ACK expected ? */ 2525 how_far_is_the_ack = expected_ack - tp->tcp_clock_cache; 2526 2527 /* If next ACK is likely to come too late, 2528 * ie in more than min(1ms, half srtt), do not defer. 2529 */ 2530 threshold = min(srtt_in_ns >> 1, NSEC_PER_MSEC); 2531 2532 if ((s64)(how_far_is_the_ack - threshold) > 0) 2533 goto send_now; 2534 2535 /* Ok, it looks like it is advisable to defer. 2536 * Three cases are tracked : 2537 * 1) We are cwnd-limited 2538 * 2) We are rwnd-limited 2539 * 3) We are application limited. 2540 */ 2541 if (cong_win < send_win) { 2542 if (cong_win <= skb->len) { 2543 *is_cwnd_limited = true; 2544 return true; 2545 } 2546 } else { 2547 if (send_win <= skb->len) { 2548 *is_rwnd_limited = true; 2549 return true; 2550 } 2551 } 2552 2553 /* If this packet won't get more data, do not wait. */ 2554 if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) || 2555 TCP_SKB_CB(skb)->eor) 2556 goto send_now; 2557 2558 return true; 2559 2560 send_now: 2561 return false; 2562 } 2563 2564 static inline void tcp_mtu_check_reprobe(struct sock *sk) 2565 { 2566 struct inet_connection_sock *icsk = inet_csk(sk); 2567 struct tcp_sock *tp = tcp_sk(sk); 2568 struct net *net = sock_net(sk); 2569 u32 interval; 2570 s32 delta; 2571 2572 interval = READ_ONCE(net->ipv4.sysctl_tcp_probe_interval); 2573 delta = tcp_jiffies32 - icsk->icsk_mtup.probe_timestamp; 2574 if (unlikely(delta >= interval * HZ)) { 2575 int mss = tcp_current_mss(sk); 2576 2577 /* Update current search range */ 2578 icsk->icsk_mtup.probe_size = 0; 2579 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + 2580 sizeof(struct tcphdr) + 2581 icsk->icsk_af_ops->net_header_len; 2582 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss); 2583 2584 /* Update probe time stamp */ 2585 icsk->icsk_mtup.probe_timestamp = tcp_jiffies32; 2586 } 2587 } 2588 2589 static bool tcp_can_coalesce_send_queue_head(struct sock *sk, int len) 2590 { 2591 struct sk_buff *skb, *next; 2592 2593 skb = tcp_send_head(sk); 2594 tcp_for_write_queue_from_safe(skb, next, sk) { 2595 if (len <= skb->len) 2596 break; 2597 2598 if (tcp_has_tx_tstamp(skb) || !tcp_skb_can_collapse(skb, next)) 2599 return false; 2600 2601 len -= skb->len; 2602 } 2603 2604 return true; 2605 } 2606 2607 static int tcp_clone_payload(struct sock *sk, struct sk_buff *to, 2608 int probe_size) 2609 { 2610 skb_frag_t *lastfrag = NULL, *fragto = skb_shinfo(to)->frags; 2611 int i, todo, len = 0, nr_frags = 0; 2612 const struct sk_buff *skb; 2613 2614 if (!sk_wmem_schedule(sk, to->truesize + probe_size)) 2615 return -ENOMEM; 2616 2617 skb_queue_walk(&sk->sk_write_queue, skb) { 2618 const skb_frag_t *fragfrom = skb_shinfo(skb)->frags; 2619 2620 if (skb_headlen(skb)) 2621 return -EINVAL; 2622 2623 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, fragfrom++) { 2624 if (len >= probe_size) 2625 goto commit; 2626 todo = min_t(int, skb_frag_size(fragfrom), 2627 probe_size - len); 2628 len += todo; 2629 skb_shinfo(to)->flags |= skb_shinfo(skb)->flags & SKBFL_SHARED_FRAG; 2630 if (lastfrag && 2631 skb_frag_page(fragfrom) == skb_frag_page(lastfrag) && 2632 skb_frag_off(fragfrom) == skb_frag_off(lastfrag) + 2633 skb_frag_size(lastfrag)) { 2634 skb_frag_size_add(lastfrag, todo); 2635 continue; 2636 } 2637 if (unlikely(nr_frags == MAX_SKB_FRAGS)) 2638 return -E2BIG; 2639 skb_frag_page_copy(fragto, fragfrom); 2640 skb_frag_off_copy(fragto, fragfrom); 2641 skb_frag_size_set(fragto, todo); 2642 nr_frags++; 2643 lastfrag = fragto++; 2644 } 2645 } 2646 commit: 2647 WARN_ON_ONCE(len != probe_size); 2648 for (i = 0; i < nr_frags; i++) 2649 skb_frag_ref(to, i); 2650 2651 skb_shinfo(to)->nr_frags = nr_frags; 2652 to->truesize += probe_size; 2653 to->len += probe_size; 2654 to->data_len += probe_size; 2655 __skb_header_release(to); 2656 return 0; 2657 } 2658 2659 /* tcp_mtu_probe() and tcp_grow_skb() can both eat an skb (src) if 2660 * all its payload was moved to another one (dst). 2661 * Make sure to transfer tcp_flags, eor, and tstamp. 2662 */ 2663 static void tcp_eat_one_skb(struct sock *sk, 2664 struct sk_buff *dst, 2665 struct sk_buff *src) 2666 { 2667 TCP_SKB_CB(dst)->tcp_flags |= TCP_SKB_CB(src)->tcp_flags; 2668 TCP_SKB_CB(dst)->eor = TCP_SKB_CB(src)->eor; 2669 tcp_skb_collapse_tstamp(dst, src); 2670 tcp_unlink_write_queue(src, sk); 2671 tcp_wmem_free_skb(sk, src); 2672 } 2673 2674 /* Create a new MTU probe if we are ready. 2675 * MTU probe is regularly attempting to increase the path MTU by 2676 * deliberately sending larger packets. This discovers routing 2677 * changes resulting in larger path MTUs. 2678 * 2679 * Returns 0 if we should wait to probe (no cwnd available), 2680 * 1 if a probe was sent, 2681 * -1 otherwise 2682 */ 2683 static int tcp_mtu_probe(struct sock *sk) 2684 { 2685 struct inet_connection_sock *icsk = inet_csk(sk); 2686 struct tcp_sock *tp = tcp_sk(sk); 2687 struct sk_buff *skb, *nskb, *next; 2688 struct net *net = sock_net(sk); 2689 int probe_size; 2690 int size_needed; 2691 int copy, len; 2692 int mss_now; 2693 int interval; 2694 2695 /* Not currently probing/verifying, 2696 * not in recovery, 2697 * have enough cwnd, and 2698 * not SACKing (the variable headers throw things off) 2699 */ 2700 if (likely(!icsk->icsk_mtup.enabled || 2701 icsk->icsk_mtup.probe_size || 2702 inet_csk(sk)->icsk_ca_state != TCP_CA_Open || 2703 tcp_snd_cwnd(tp) < 11 || 2704 tp->rx_opt.num_sacks || tp->rx_opt.dsack)) 2705 return -1; 2706 2707 /* Use binary search for probe_size between tcp_mss_base, 2708 * and current mss_clamp. if (search_high - search_low) 2709 * smaller than a threshold, backoff from probing. 2710 */ 2711 mss_now = tcp_current_mss(sk); 2712 probe_size = tcp_mtu_to_mss(sk, (icsk->icsk_mtup.search_high + 2713 icsk->icsk_mtup.search_low) >> 1); 2714 size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache; 2715 interval = icsk->icsk_mtup.search_high - icsk->icsk_mtup.search_low; 2716 /* When misfortune happens, we are reprobing actively, 2717 * and then reprobe timer has expired. We stick with current 2718 * probing process by not resetting search range to its orignal. 2719 */ 2720 if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high) || 2721 interval < READ_ONCE(net->ipv4.sysctl_tcp_probe_threshold)) { 2722 /* Check whether enough time has elaplased for 2723 * another round of probing. 2724 */ 2725 tcp_mtu_check_reprobe(sk); 2726 return -1; 2727 } 2728 2729 /* Have enough data in the send queue to probe? */ 2730 if (tp->write_seq - tp->snd_nxt < size_needed) 2731 return -1; 2732 2733 if (tp->snd_wnd < size_needed) 2734 return -1; 2735 if (after(tp->snd_nxt + size_needed, tcp_wnd_end(tp))) 2736 return 0; 2737 2738 /* Do we need to wait to drain cwnd? With none in flight, don't stall */ 2739 if (tcp_packets_in_flight(tp) + 2 > tcp_snd_cwnd(tp)) { 2740 if (!tcp_packets_in_flight(tp)) 2741 return -1; 2742 else 2743 return 0; 2744 } 2745 2746 if (!tcp_can_coalesce_send_queue_head(sk, probe_size)) 2747 return -1; 2748 2749 /* We're allowed to probe. Build it now. */ 2750 nskb = tcp_stream_alloc_skb(sk, GFP_ATOMIC, false); 2751 if (!nskb) 2752 return -1; 2753 2754 /* build the payload, and be prepared to abort if this fails. */ 2755 if (tcp_clone_payload(sk, nskb, probe_size)) { 2756 tcp_skb_tsorted_anchor_cleanup(nskb); 2757 consume_skb(nskb); 2758 return -1; 2759 } 2760 sk_wmem_queued_add(sk, nskb->truesize); 2761 sk_mem_charge(sk, nskb->truesize); 2762 2763 skb = tcp_send_head(sk); 2764 skb_copy_decrypted(nskb, skb); 2765 mptcp_skb_ext_copy(nskb, skb); 2766 2767 TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq; 2768 TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size; 2769 TCP_SKB_CB(nskb)->tcp_flags = TCPHDR_ACK; 2770 2771 tcp_insert_write_queue_before(nskb, skb, sk); 2772 tcp_highest_sack_replace(sk, skb, nskb); 2773 2774 len = 0; 2775 tcp_for_write_queue_from_safe(skb, next, sk) { 2776 copy = min_t(int, skb->len, probe_size - len); 2777 2778 if (skb->len <= copy) { 2779 tcp_eat_one_skb(sk, nskb, skb); 2780 } else { 2781 TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags & 2782 ~(TCPHDR_FIN|TCPHDR_PSH); 2783 __pskb_trim_head(skb, copy); 2784 tcp_set_skb_tso_segs(skb, mss_now); 2785 TCP_SKB_CB(skb)->seq += copy; 2786 } 2787 2788 len += copy; 2789 2790 if (len >= probe_size) 2791 break; 2792 } 2793 tcp_init_tso_segs(nskb, nskb->len); 2794 2795 /* We're ready to send. If this fails, the probe will 2796 * be resegmented into mss-sized pieces by tcp_write_xmit(). 2797 */ 2798 if (!tcp_transmit_skb(sk, nskb, 1, GFP_ATOMIC)) { 2799 /* Decrement cwnd here because we are sending 2800 * effectively two packets. */ 2801 tcp_snd_cwnd_set(tp, tcp_snd_cwnd(tp) - 1); 2802 tcp_event_new_data_sent(sk, nskb); 2803 2804 icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len); 2805 tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq; 2806 tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq; 2807 2808 return 1; 2809 } 2810 2811 return -1; 2812 } 2813 2814 static bool tcp_pacing_check(struct sock *sk) 2815 { 2816 struct tcp_sock *tp = tcp_sk(sk); 2817 2818 if (!tcp_needs_internal_pacing(sk)) 2819 return false; 2820 2821 if (tp->tcp_wstamp_ns <= tp->tcp_clock_cache) 2822 return false; 2823 2824 if (!hrtimer_is_queued(&tp->pacing_timer)) { 2825 hrtimer_start(&tp->pacing_timer, 2826 ns_to_ktime(tp->tcp_wstamp_ns), 2827 HRTIMER_MODE_ABS_PINNED_SOFT); 2828 sock_hold(sk); 2829 } 2830 return true; 2831 } 2832 2833 static bool tcp_rtx_queue_empty_or_single_skb(const struct sock *sk) 2834 { 2835 const struct rb_node *node = sk->tcp_rtx_queue.rb_node; 2836 2837 /* No skb in the rtx queue. */ 2838 if (!node) 2839 return true; 2840 2841 /* Only one skb in rtx queue. */ 2842 return !node->rb_left && !node->rb_right; 2843 } 2844 2845 /* TCP Small Queues : 2846 * Control number of packets in qdisc/devices to two packets / or ~1 ms. 2847 * (These limits are doubled for retransmits) 2848 * This allows for : 2849 * - better RTT estimation and ACK scheduling 2850 * - faster recovery 2851 * - high rates 2852 * Alas, some drivers / subsystems require a fair amount 2853 * of queued bytes to ensure line rate. 2854 * One example is wifi aggregation (802.11 AMPDU) 2855 */ 2856 static bool tcp_small_queue_check(struct sock *sk, const struct sk_buff *skb, 2857 unsigned int factor) 2858 { 2859 unsigned long limit; 2860 2861 limit = max_t(unsigned long, 2862 2 * skb->truesize, 2863 READ_ONCE(sk->sk_pacing_rate) >> READ_ONCE(sk->sk_pacing_shift)); 2864 limit = min_t(unsigned long, limit, 2865 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_limit_output_bytes)); 2866 limit <<= factor; 2867 2868 if (static_branch_unlikely(&tcp_tx_delay_enabled) && 2869 tcp_sk(sk)->tcp_tx_delay) { 2870 u64 extra_bytes = (u64)READ_ONCE(sk->sk_pacing_rate) * 2871 tcp_sk(sk)->tcp_tx_delay; 2872 2873 /* TSQ is based on skb truesize sum (sk_wmem_alloc), so we 2874 * approximate our needs assuming an ~100% skb->truesize overhead. 2875 * USEC_PER_SEC is approximated by 2^20. 2876 * do_div(extra_bytes, USEC_PER_SEC/2) is replaced by a right shift. 2877 */ 2878 extra_bytes >>= (20 - 1); 2879 limit += extra_bytes; 2880 } 2881 if (refcount_read(&sk->sk_wmem_alloc) > limit) { 2882 /* Always send skb if rtx queue is empty or has one skb. 2883 * No need to wait for TX completion to call us back, 2884 * after softirq schedule. 2885 * This helps when TX completions are delayed too much. 2886 */ 2887 if (tcp_rtx_queue_empty_or_single_skb(sk)) 2888 return false; 2889 2890 set_bit(TSQ_THROTTLED, &sk->sk_tsq_flags); 2891 /* It is possible TX completion already happened 2892 * before we set TSQ_THROTTLED, so we must 2893 * test again the condition. 2894 */ 2895 smp_mb__after_atomic(); 2896 if (refcount_read(&sk->sk_wmem_alloc) > limit) 2897 return true; 2898 } 2899 return false; 2900 } 2901 2902 void tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type) 2903 { 2904 struct tcp_sock *tp = tcp_sk(sk); 2905 2906 2907 /* There are multiple conditions worthy of tracking in a 2908 * chronograph, so that the highest priority enum takes 2909 * precedence over the other conditions (see tcp_chrono_start). 2910 * If a condition stops, we only stop chrono tracking if 2911 * it's the "most interesting" or current chrono we are 2912 * tracking and starts busy chrono if we have pending data. 2913 */ 2914 if (tcp_rtx_and_write_queues_empty(sk)) 2915 tcp_chrono_set(tp, TCP_CHRONO_UNSPEC); 2916 else if (type == tp->chrono_type) 2917 tcp_chrono_set(tp, TCP_CHRONO_BUSY); 2918 } 2919 2920 /* First skb in the write queue is smaller than ideal packet size. 2921 * Check if we can move payload from the second skb in the queue. 2922 */ 2923 static void tcp_grow_skb(struct sock *sk, struct sk_buff *skb, int amount) 2924 { 2925 struct sk_buff *next_skb = skb->next; 2926 unsigned int nlen; 2927 2928 if (tcp_skb_is_last(sk, skb)) 2929 return; 2930 2931 if (!tcp_skb_can_collapse(skb, next_skb)) 2932 return; 2933 2934 nlen = min_t(u32, amount, next_skb->len); 2935 if (!nlen || !skb_shift(skb, next_skb, nlen)) 2936 return; 2937 2938 TCP_SKB_CB(skb)->end_seq += nlen; 2939 TCP_SKB_CB(next_skb)->seq += nlen; 2940 2941 if (!next_skb->len) { 2942 /* In case FIN is set, we need to update end_seq */ 2943 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq; 2944 2945 tcp_eat_one_skb(sk, skb, next_skb); 2946 } 2947 } 2948 2949 /* This routine writes packets to the network. It advances the 2950 * send_head. This happens as incoming acks open up the remote 2951 * window for us. 2952 * 2953 * LARGESEND note: !tcp_urg_mode is overkill, only frames between 2954 * snd_up-64k-mss .. snd_up cannot be large. However, taking into 2955 * account rare use of URG, this is not a big flaw. 2956 * 2957 * Send at most one packet when push_one > 0. Temporarily ignore 2958 * cwnd limit to force at most one packet out when push_one == 2. 2959 2960 * Returns true, if no segments are in flight and we have queued segments, 2961 * but cannot send anything now because of SWS or another problem. 2962 */ 2963 static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle, 2964 int push_one, gfp_t gfp) 2965 { 2966 struct tcp_sock *tp = tcp_sk(sk); 2967 struct sk_buff *skb; 2968 unsigned int tso_segs, sent_pkts; 2969 u32 cwnd_quota, max_segs; 2970 int result; 2971 bool is_cwnd_limited = false, is_rwnd_limited = false; 2972 2973 sent_pkts = 0; 2974 2975 tcp_mstamp_refresh(tp); 2976 2977 /* AccECN option beacon depends on mstamp, it may change mss */ 2978 if (tcp_ecn_mode_accecn(tp) && tcp_accecn_option_beacon_check(sk)) 2979 mss_now = tcp_current_mss(sk); 2980 2981 if (!push_one) { 2982 /* Do MTU probing. */ 2983 result = tcp_mtu_probe(sk); 2984 if (!result) { 2985 return false; 2986 } else if (result > 0) { 2987 sent_pkts = 1; 2988 } 2989 } 2990 2991 max_segs = tcp_tso_segs(sk, mss_now); 2992 while ((skb = tcp_send_head(sk))) { 2993 unsigned int limit; 2994 int missing_bytes; 2995 2996 if (unlikely(tp->repair) && tp->repair_queue == TCP_SEND_QUEUE) { 2997 /* "skb_mstamp_ns" is used as a start point for the retransmit timer */ 2998 tp->tcp_wstamp_ns = tp->tcp_clock_cache; 2999 skb_set_delivery_time(skb, tp->tcp_wstamp_ns, SKB_CLOCK_MONOTONIC); 3000 list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue); 3001 tcp_init_tso_segs(skb, mss_now); 3002 goto repair; /* Skip network transmission */ 3003 } 3004 3005 if (tcp_pacing_check(sk)) 3006 break; 3007 3008 cwnd_quota = tcp_cwnd_test(tp); 3009 if (!cwnd_quota) { 3010 if (push_one == 2) 3011 /* Force out a loss probe pkt. */ 3012 cwnd_quota = 1; 3013 else 3014 break; 3015 } 3016 cwnd_quota = min(cwnd_quota, max_segs); 3017 missing_bytes = cwnd_quota * mss_now - skb->len; 3018 if (missing_bytes > 0) 3019 tcp_grow_skb(sk, skb, missing_bytes); 3020 3021 tso_segs = tcp_set_skb_tso_segs(skb, mss_now); 3022 3023 if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now))) { 3024 is_rwnd_limited = true; 3025 break; 3026 } 3027 3028 if (tso_segs == 1) { 3029 if (unlikely(!tcp_nagle_test(tp, skb, mss_now, 3030 (tcp_skb_is_last(sk, skb) ? 3031 nonagle : TCP_NAGLE_PUSH)))) 3032 break; 3033 } else { 3034 if (!push_one && 3035 tcp_tso_should_defer(sk, skb, &is_cwnd_limited, 3036 &is_rwnd_limited, max_segs)) 3037 break; 3038 } 3039 3040 limit = mss_now; 3041 if (tso_segs > 1 && !tcp_urg_mode(tp)) 3042 limit = tcp_mss_split_point(sk, skb, mss_now, 3043 cwnd_quota, 3044 nonagle); 3045 3046 if (skb->len > limit && 3047 unlikely(tso_fragment(sk, skb, limit, mss_now, gfp))) 3048 break; 3049 3050 if (tcp_small_queue_check(sk, skb, 0)) 3051 break; 3052 3053 /* Argh, we hit an empty skb(), presumably a thread 3054 * is sleeping in sendmsg()/sk_stream_wait_memory(). 3055 * We do not want to send a pure-ack packet and have 3056 * a strange looking rtx queue with empty packet(s). 3057 */ 3058 if (TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) 3059 break; 3060 3061 if (unlikely(tcp_transmit_skb(sk, skb, 1, gfp))) 3062 break; 3063 3064 repair: 3065 /* Advance the send_head. This one is sent out. 3066 * This call will increment packets_out. 3067 */ 3068 tcp_event_new_data_sent(sk, skb); 3069 3070 tcp_minshall_update(tp, mss_now, skb); 3071 sent_pkts += tcp_skb_pcount(skb); 3072 3073 if (push_one) 3074 break; 3075 } 3076 3077 if (is_rwnd_limited) 3078 tcp_chrono_start(sk, TCP_CHRONO_RWND_LIMITED); 3079 else 3080 tcp_chrono_stop(sk, TCP_CHRONO_RWND_LIMITED); 3081 3082 is_cwnd_limited |= (tcp_packets_in_flight(tp) >= tcp_snd_cwnd(tp)); 3083 if (likely(sent_pkts || is_cwnd_limited)) 3084 tcp_cwnd_validate(sk, is_cwnd_limited); 3085 3086 if (likely(sent_pkts)) { 3087 if (tcp_in_cwnd_reduction(sk)) 3088 tp->prr_out += sent_pkts; 3089 3090 /* Send one loss probe per tail loss episode. */ 3091 if (push_one != 2) 3092 tcp_schedule_loss_probe(sk, false); 3093 return false; 3094 } 3095 return !tp->packets_out && !tcp_write_queue_empty(sk); 3096 } 3097 3098 bool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto) 3099 { 3100 struct inet_connection_sock *icsk = inet_csk(sk); 3101 struct tcp_sock *tp = tcp_sk(sk); 3102 u32 timeout, timeout_us, rto_delta_us; 3103 int early_retrans; 3104 3105 /* Don't do any loss probe on a Fast Open connection before 3WHS 3106 * finishes. 3107 */ 3108 if (rcu_access_pointer(tp->fastopen_rsk)) 3109 return false; 3110 3111 early_retrans = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_early_retrans); 3112 /* Schedule a loss probe in 2*RTT for SACK capable connections 3113 * not in loss recovery, that are either limited by cwnd or application. 3114 */ 3115 if ((early_retrans != 3 && early_retrans != 4) || 3116 !tcp_is_sack(tp) || 3117 (icsk->icsk_ca_state != TCP_CA_Open && 3118 icsk->icsk_ca_state != TCP_CA_CWR)) 3119 return false; 3120 3121 /* Probe timeout is 2*rtt. Add minimum RTO to account 3122 * for delayed ack when there's one outstanding packet. If no RTT 3123 * sample is available then probe after TCP_TIMEOUT_INIT. 3124 */ 3125 if (tp->srtt_us) { 3126 timeout_us = tp->srtt_us >> 2; 3127 if (tp->packets_out == 1) 3128 timeout_us += tcp_rto_min_us(sk); 3129 else 3130 timeout_us += TCP_TIMEOUT_MIN_US; 3131 timeout = usecs_to_jiffies(timeout_us); 3132 } else { 3133 timeout = TCP_TIMEOUT_INIT; 3134 } 3135 3136 /* If the RTO formula yields an earlier time, then use that time. */ 3137 rto_delta_us = advancing_rto ? 3138 jiffies_to_usecs(inet_csk(sk)->icsk_rto) : 3139 tcp_rto_delta_us(sk); /* How far in future is RTO? */ 3140 if (rto_delta_us > 0) 3141 timeout = min_t(u32, timeout, usecs_to_jiffies(rto_delta_us)); 3142 3143 tcp_reset_xmit_timer(sk, ICSK_TIME_LOSS_PROBE, timeout, true); 3144 return true; 3145 } 3146 3147 /* Thanks to skb fast clones, we can detect if a prior transmit of 3148 * a packet is still in a qdisc or driver queue. 3149 * In this case, there is very little point doing a retransmit ! 3150 */ 3151 static bool skb_still_in_host_queue(struct sock *sk, 3152 const struct sk_buff *skb) 3153 { 3154 if (unlikely(skb_fclone_busy(sk, skb))) { 3155 set_bit(TSQ_THROTTLED, &sk->sk_tsq_flags); 3156 smp_mb__after_atomic(); 3157 if (skb_fclone_busy(sk, skb)) { 3158 NET_INC_STATS(sock_net(sk), 3159 LINUX_MIB_TCPSPURIOUS_RTX_HOSTQUEUES); 3160 return true; 3161 } 3162 } 3163 return false; 3164 } 3165 3166 /* When probe timeout (PTO) fires, try send a new segment if possible, else 3167 * retransmit the last segment. 3168 */ 3169 void tcp_send_loss_probe(struct sock *sk) 3170 { 3171 struct tcp_sock *tp = tcp_sk(sk); 3172 struct sk_buff *skb; 3173 int pcount; 3174 int mss = tcp_current_mss(sk); 3175 3176 /* At most one outstanding TLP */ 3177 if (tp->tlp_high_seq) 3178 goto rearm_timer; 3179 3180 tp->tlp_retrans = 0; 3181 skb = tcp_send_head(sk); 3182 if (skb && tcp_snd_wnd_test(tp, skb, mss)) { 3183 pcount = tp->packets_out; 3184 tcp_write_xmit(sk, mss, TCP_NAGLE_OFF, 2, GFP_ATOMIC); 3185 if (tp->packets_out > pcount) 3186 goto probe_sent; 3187 goto rearm_timer; 3188 } 3189 skb = skb_rb_last(&sk->tcp_rtx_queue); 3190 if (unlikely(!skb)) { 3191 tcp_warn_once(sk, tp->packets_out, "invalid inflight: "); 3192 smp_store_release(&inet_csk(sk)->icsk_pending, 0); 3193 return; 3194 } 3195 3196 if (skb_still_in_host_queue(sk, skb)) 3197 goto rearm_timer; 3198 3199 pcount = tcp_skb_pcount(skb); 3200 if (WARN_ON(!pcount)) 3201 goto rearm_timer; 3202 3203 if ((pcount > 1) && (skb->len > (pcount - 1) * mss)) { 3204 if (unlikely(tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb, 3205 (pcount - 1) * mss, mss, 3206 GFP_ATOMIC))) 3207 goto rearm_timer; 3208 skb = skb_rb_next(skb); 3209 } 3210 3211 if (WARN_ON(!skb || !tcp_skb_pcount(skb))) 3212 goto rearm_timer; 3213 3214 if (__tcp_retransmit_skb(sk, skb, 1)) 3215 goto rearm_timer; 3216 3217 tp->tlp_retrans = 1; 3218 3219 probe_sent: 3220 /* Record snd_nxt for loss detection. */ 3221 tp->tlp_high_seq = tp->snd_nxt; 3222 3223 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSPROBES); 3224 /* Reset s.t. tcp_rearm_rto will restart timer from now */ 3225 smp_store_release(&inet_csk(sk)->icsk_pending, 0); 3226 rearm_timer: 3227 tcp_rearm_rto(sk); 3228 } 3229 3230 /* Push out any pending frames which were held back due to 3231 * TCP_CORK or attempt at coalescing tiny packets. 3232 * The socket must be locked by the caller. 3233 */ 3234 void __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss, 3235 int nonagle) 3236 { 3237 /* If we are closed, the bytes will have to remain here. 3238 * In time closedown will finish, we empty the write queue and 3239 * all will be happy. 3240 */ 3241 if (unlikely(sk->sk_state == TCP_CLOSE)) 3242 return; 3243 3244 if (tcp_write_xmit(sk, cur_mss, nonagle, 0, 3245 sk_gfp_mask(sk, GFP_ATOMIC))) 3246 tcp_check_probe_timer(sk); 3247 } 3248 3249 /* Send _single_ skb sitting at the send head. This function requires 3250 * true push pending frames to setup probe timer etc. 3251 */ 3252 void tcp_push_one(struct sock *sk, unsigned int mss_now) 3253 { 3254 struct sk_buff *skb = tcp_send_head(sk); 3255 3256 BUG_ON(!skb || skb->len < mss_now); 3257 3258 tcp_write_xmit(sk, mss_now, TCP_NAGLE_PUSH, 1, sk->sk_allocation); 3259 } 3260 3261 /* This function returns the amount that we can raise the 3262 * usable window based on the following constraints 3263 * 3264 * 1. The window can never be shrunk once it is offered (RFC 793) 3265 * 2. We limit memory per socket 3266 * 3267 * RFC 1122: 3268 * "the suggested [SWS] avoidance algorithm for the receiver is to keep 3269 * RECV.NEXT + RCV.WIN fixed until: 3270 * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)" 3271 * 3272 * i.e. don't raise the right edge of the window until you can raise 3273 * it at least MSS bytes. 3274 * 3275 * Unfortunately, the recommended algorithm breaks header prediction, 3276 * since header prediction assumes th->window stays fixed. 3277 * 3278 * Strictly speaking, keeping th->window fixed violates the receiver 3279 * side SWS prevention criteria. The problem is that under this rule 3280 * a stream of single byte packets will cause the right side of the 3281 * window to always advance by a single byte. 3282 * 3283 * Of course, if the sender implements sender side SWS prevention 3284 * then this will not be a problem. 3285 * 3286 * BSD seems to make the following compromise: 3287 * 3288 * If the free space is less than the 1/4 of the maximum 3289 * space available and the free space is less than 1/2 mss, 3290 * then set the window to 0. 3291 * [ Actually, bsd uses MSS and 1/4 of maximal _window_ ] 3292 * Otherwise, just prevent the window from shrinking 3293 * and from being larger than the largest representable value. 3294 * 3295 * This prevents incremental opening of the window in the regime 3296 * where TCP is limited by the speed of the reader side taking 3297 * data out of the TCP receive queue. It does nothing about 3298 * those cases where the window is constrained on the sender side 3299 * because the pipeline is full. 3300 * 3301 * BSD also seems to "accidentally" limit itself to windows that are a 3302 * multiple of MSS, at least until the free space gets quite small. 3303 * This would appear to be a side effect of the mbuf implementation. 3304 * Combining these two algorithms results in the observed behavior 3305 * of having a fixed window size at almost all times. 3306 * 3307 * Below we obtain similar behavior by forcing the offered window to 3308 * a multiple of the mss when it is feasible to do so. 3309 * 3310 * Note, we don't "adjust" for TIMESTAMP or SACK option bytes. 3311 * Regular options like TIMESTAMP are taken into account. 3312 */ 3313 u32 __tcp_select_window(struct sock *sk) 3314 { 3315 struct inet_connection_sock *icsk = inet_csk(sk); 3316 struct tcp_sock *tp = tcp_sk(sk); 3317 struct net *net = sock_net(sk); 3318 /* MSS for the peer's data. Previous versions used mss_clamp 3319 * here. I don't know if the value based on our guesses 3320 * of peer's MSS is better for the performance. It's more correct 3321 * but may be worse for the performance because of rcv_mss 3322 * fluctuations. --SAW 1998/11/1 3323 */ 3324 int mss = icsk->icsk_ack.rcv_mss; 3325 int free_space = tcp_space(sk); 3326 int allowed_space = tcp_full_space(sk); 3327 int full_space, window; 3328 3329 if (sk_is_mptcp(sk)) 3330 mptcp_space(sk, &free_space, &allowed_space); 3331 3332 full_space = min_t(int, tp->window_clamp, allowed_space); 3333 3334 if (unlikely(mss > full_space)) { 3335 mss = full_space; 3336 if (mss <= 0) 3337 return 0; 3338 } 3339 3340 /* Only allow window shrink if the sysctl is enabled and we have 3341 * a non-zero scaling factor in effect. 3342 */ 3343 if (READ_ONCE(net->ipv4.sysctl_tcp_shrink_window) && tp->rx_opt.rcv_wscale) 3344 goto shrink_window_allowed; 3345 3346 /* do not allow window to shrink */ 3347 3348 if (free_space < (full_space >> 1)) { 3349 icsk->icsk_ack.quick = 0; 3350 3351 if (tcp_under_memory_pressure(sk)) 3352 tcp_adjust_rcv_ssthresh(sk); 3353 3354 /* free_space might become our new window, make sure we don't 3355 * increase it due to wscale. 3356 */ 3357 free_space = round_down(free_space, 1 << tp->rx_opt.rcv_wscale); 3358 3359 /* if free space is less than mss estimate, or is below 1/16th 3360 * of the maximum allowed, try to move to zero-window, else 3361 * tcp_clamp_window() will grow rcv buf up to tcp_rmem[2], and 3362 * new incoming data is dropped due to memory limits. 3363 * With large window, mss test triggers way too late in order 3364 * to announce zero window in time before rmem limit kicks in. 3365 */ 3366 if (free_space < (allowed_space >> 4) || free_space < mss) 3367 return 0; 3368 } 3369 3370 if (free_space > tp->rcv_ssthresh) 3371 free_space = tp->rcv_ssthresh; 3372 3373 /* Don't do rounding if we are using window scaling, since the 3374 * scaled window will not line up with the MSS boundary anyway. 3375 */ 3376 if (tp->rx_opt.rcv_wscale) { 3377 window = free_space; 3378 3379 /* Advertise enough space so that it won't get scaled away. 3380 * Import case: prevent zero window announcement if 3381 * 1<<rcv_wscale > mss. 3382 */ 3383 window = ALIGN(window, (1 << tp->rx_opt.rcv_wscale)); 3384 } else { 3385 window = tp->rcv_wnd; 3386 /* Get the largest window that is a nice multiple of mss. 3387 * Window clamp already applied above. 3388 * If our current window offering is within 1 mss of the 3389 * free space we just keep it. This prevents the divide 3390 * and multiply from happening most of the time. 3391 * We also don't do any window rounding when the free space 3392 * is too small. 3393 */ 3394 if (window <= free_space - mss || window > free_space) 3395 window = rounddown(free_space, mss); 3396 else if (mss == full_space && 3397 free_space > window + (full_space >> 1)) 3398 window = free_space; 3399 } 3400 3401 return window; 3402 3403 shrink_window_allowed: 3404 /* new window should always be an exact multiple of scaling factor */ 3405 free_space = round_down(free_space, 1 << tp->rx_opt.rcv_wscale); 3406 3407 if (free_space < (full_space >> 1)) { 3408 icsk->icsk_ack.quick = 0; 3409 3410 if (tcp_under_memory_pressure(sk)) 3411 tcp_adjust_rcv_ssthresh(sk); 3412 3413 /* if free space is too low, return a zero window */ 3414 if (free_space < (allowed_space >> 4) || free_space < mss || 3415 free_space < (1 << tp->rx_opt.rcv_wscale)) 3416 return 0; 3417 } 3418 3419 if (free_space > tp->rcv_ssthresh) { 3420 free_space = tp->rcv_ssthresh; 3421 /* new window should always be an exact multiple of scaling factor 3422 * 3423 * For this case, we ALIGN "up" (increase free_space) because 3424 * we know free_space is not zero here, it has been reduced from 3425 * the memory-based limit, and rcv_ssthresh is not a hard limit 3426 * (unlike sk_rcvbuf). 3427 */ 3428 free_space = ALIGN(free_space, (1 << tp->rx_opt.rcv_wscale)); 3429 } 3430 3431 return free_space; 3432 } 3433 3434 void tcp_skb_collapse_tstamp(struct sk_buff *skb, 3435 const struct sk_buff *next_skb) 3436 { 3437 if (unlikely(tcp_has_tx_tstamp(next_skb))) { 3438 const struct skb_shared_info *next_shinfo = 3439 skb_shinfo(next_skb); 3440 struct skb_shared_info *shinfo = skb_shinfo(skb); 3441 3442 shinfo->tx_flags |= next_shinfo->tx_flags & SKBTX_ANY_TSTAMP; 3443 shinfo->tskey = next_shinfo->tskey; 3444 TCP_SKB_CB(skb)->txstamp_ack |= 3445 TCP_SKB_CB(next_skb)->txstamp_ack; 3446 } 3447 } 3448 3449 /* Collapses two adjacent SKB's during retransmission. */ 3450 static bool tcp_collapse_retrans(struct sock *sk, struct sk_buff *skb) 3451 { 3452 struct tcp_sock *tp = tcp_sk(sk); 3453 struct sk_buff *next_skb = skb_rb_next(skb); 3454 int next_skb_size; 3455 3456 next_skb_size = next_skb->len; 3457 3458 BUG_ON(tcp_skb_pcount(skb) != 1 || tcp_skb_pcount(next_skb) != 1); 3459 3460 if (next_skb_size && !tcp_skb_shift(skb, next_skb, 1, next_skb_size)) 3461 return false; 3462 3463 tcp_highest_sack_replace(sk, next_skb, skb); 3464 3465 /* Update sequence range on original skb. */ 3466 TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq; 3467 3468 /* Merge over control information. This moves PSH/FIN etc. over */ 3469 TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(next_skb)->tcp_flags; 3470 3471 /* All done, get rid of second SKB and account for it so 3472 * packet counting does not break. 3473 */ 3474 TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked & TCPCB_EVER_RETRANS; 3475 TCP_SKB_CB(skb)->eor = TCP_SKB_CB(next_skb)->eor; 3476 3477 /* changed transmit queue under us so clear hints */ 3478 if (next_skb == tp->retransmit_skb_hint) 3479 tp->retransmit_skb_hint = skb; 3480 3481 tcp_adjust_pcount(sk, next_skb, tcp_skb_pcount(next_skb)); 3482 3483 tcp_skb_collapse_tstamp(skb, next_skb); 3484 3485 tcp_rtx_queue_unlink_and_free(next_skb, sk); 3486 return true; 3487 } 3488 3489 /* Check if coalescing SKBs is legal. */ 3490 static bool tcp_can_collapse(const struct sock *sk, const struct sk_buff *skb) 3491 { 3492 if (tcp_skb_pcount(skb) > 1) 3493 return false; 3494 if (skb_cloned(skb)) 3495 return false; 3496 if (!skb_frags_readable(skb)) 3497 return false; 3498 /* Some heuristics for collapsing over SACK'd could be invented */ 3499 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) 3500 return false; 3501 3502 return true; 3503 } 3504 3505 /* Collapse packets in the retransmit queue to make to create 3506 * less packets on the wire. This is only done on retransmission. 3507 */ 3508 static void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *to, 3509 int space) 3510 { 3511 struct tcp_sock *tp = tcp_sk(sk); 3512 struct sk_buff *skb = to, *tmp; 3513 bool first = true; 3514 3515 if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_retrans_collapse)) 3516 return; 3517 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN) 3518 return; 3519 3520 skb_rbtree_walk_from_safe(skb, tmp) { 3521 if (!tcp_can_collapse(sk, skb)) 3522 break; 3523 3524 if (!tcp_skb_can_collapse(to, skb)) 3525 break; 3526 3527 space -= skb->len; 3528 3529 if (first) { 3530 first = false; 3531 continue; 3532 } 3533 3534 if (space < 0) 3535 break; 3536 3537 if (after(TCP_SKB_CB(skb)->end_seq, tcp_wnd_end(tp))) 3538 break; 3539 3540 if (!tcp_collapse_retrans(sk, to)) 3541 break; 3542 } 3543 } 3544 3545 /* This retransmits one SKB. Policy decisions and retransmit queue 3546 * state updates are done by the caller. Returns non-zero if an 3547 * error occurred which prevented the send. 3548 */ 3549 int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs) 3550 { 3551 struct inet_connection_sock *icsk = inet_csk(sk); 3552 struct tcp_sock *tp = tcp_sk(sk); 3553 unsigned int cur_mss; 3554 int diff, len, err; 3555 int avail_wnd; 3556 3557 /* Inconclusive MTU probe */ 3558 if (icsk->icsk_mtup.probe_size) 3559 icsk->icsk_mtup.probe_size = 0; 3560 3561 if (skb_still_in_host_queue(sk, skb)) { 3562 err = -EBUSY; 3563 goto out; 3564 } 3565 3566 start: 3567 if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) { 3568 if (unlikely(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { 3569 TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_SYN; 3570 TCP_SKB_CB(skb)->seq++; 3571 goto start; 3572 } 3573 if (unlikely(before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))) { 3574 WARN_ON_ONCE(1); 3575 err = -EINVAL; 3576 goto out; 3577 } 3578 if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq)) { 3579 err = -ENOMEM; 3580 goto out; 3581 } 3582 } 3583 3584 if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk)) { 3585 err = -EHOSTUNREACH; /* Routing failure or similar. */ 3586 goto out; 3587 } 3588 3589 cur_mss = tcp_current_mss(sk); 3590 avail_wnd = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; 3591 3592 /* If receiver has shrunk his window, and skb is out of 3593 * new window, do not retransmit it. The exception is the 3594 * case, when window is shrunk to zero. In this case 3595 * our retransmit of one segment serves as a zero window probe. 3596 */ 3597 if (avail_wnd <= 0) { 3598 if (TCP_SKB_CB(skb)->seq != tp->snd_una) { 3599 err = -EAGAIN; 3600 goto out; 3601 } 3602 avail_wnd = cur_mss; 3603 } 3604 3605 len = cur_mss * segs; 3606 if (len > avail_wnd) { 3607 len = rounddown(avail_wnd, cur_mss); 3608 if (!len) 3609 len = avail_wnd; 3610 } 3611 if (skb->len > len) { 3612 if (tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb, len, 3613 cur_mss, GFP_ATOMIC)) { 3614 err = -ENOMEM; /* We'll try again later. */ 3615 goto out; 3616 } 3617 } else { 3618 if (skb_unclone_keeptruesize(skb, GFP_ATOMIC)) { 3619 err = -ENOMEM; 3620 goto out; 3621 } 3622 3623 diff = tcp_skb_pcount(skb); 3624 tcp_set_skb_tso_segs(skb, cur_mss); 3625 diff -= tcp_skb_pcount(skb); 3626 if (diff) 3627 tcp_adjust_pcount(sk, skb, diff); 3628 avail_wnd = min_t(int, avail_wnd, cur_mss); 3629 if (skb->len < avail_wnd) 3630 tcp_retrans_try_collapse(sk, skb, avail_wnd); 3631 } 3632 3633 if (!tcp_ecn_mode_pending(tp) || icsk->icsk_retransmits > 1) { 3634 /* RFC3168, section 6.1.1.1. ECN fallback 3635 * As AccECN uses the same SYN flags (+ AE), this check 3636 * covers both cases. 3637 */ 3638 if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN_ECN) == 3639 TCPHDR_SYN_ECN) 3640 tcp_ecn_clear_syn(sk, skb); 3641 } 3642 3643 /* Update global and local TCP statistics. */ 3644 segs = tcp_skb_pcount(skb); 3645 TCP_ADD_STATS(sock_net(sk), TCP_MIB_RETRANSSEGS, segs); 3646 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN) 3647 __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNRETRANS); 3648 WRITE_ONCE(tp->total_retrans, tp->total_retrans + segs); 3649 WRITE_ONCE(tp->bytes_retrans, tp->bytes_retrans + skb->len); 3650 3651 /* make sure skb->data is aligned on arches that require it 3652 * and check if ack-trimming & collapsing extended the headroom 3653 * beyond what csum_start can cover. 3654 */ 3655 if (unlikely((NET_IP_ALIGN && ((unsigned long)skb->data & 3)) || 3656 skb_headroom(skb) >= 0xFFFF)) { 3657 struct sk_buff *nskb; 3658 3659 tcp_skb_tsorted_save(skb) { 3660 nskb = __pskb_copy(skb, MAX_TCP_HEADER, GFP_ATOMIC); 3661 if (nskb) { 3662 nskb->dev = NULL; 3663 err = tcp_transmit_skb(sk, nskb, 0, GFP_ATOMIC); 3664 } else { 3665 err = -ENOBUFS; 3666 } 3667 } tcp_skb_tsorted_restore(skb); 3668 3669 if (!err) { 3670 tcp_update_skb_after_send(sk, skb, tp->tcp_wstamp_ns); 3671 tcp_rate_skb_sent(sk, skb); 3672 } 3673 } else { 3674 err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC); 3675 } 3676 3677 if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RETRANS_CB_FLAG)) 3678 tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RETRANS_CB, 3679 TCP_SKB_CB(skb)->seq, segs, err); 3680 3681 if (unlikely(err) && err != -EBUSY) 3682 NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPRETRANSFAIL, segs); 3683 3684 /* To avoid taking spuriously low RTT samples based on a timestamp 3685 * for a transmit that never happened, always mark EVER_RETRANS 3686 */ 3687 TCP_SKB_CB(skb)->sacked |= TCPCB_EVER_RETRANS; 3688 3689 out: 3690 trace_tcp_retransmit_skb(sk, skb, err); 3691 return err; 3692 } 3693 3694 int tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs) 3695 { 3696 struct tcp_sock *tp = tcp_sk(sk); 3697 int err = __tcp_retransmit_skb(sk, skb, segs); 3698 3699 if (err == 0) { 3700 #if FASTRETRANS_DEBUG > 0 3701 if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) { 3702 net_dbg_ratelimited("retrans_out leaked\n"); 3703 } 3704 #endif 3705 TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS; 3706 tp->retrans_out += tcp_skb_pcount(skb); 3707 } 3708 3709 /* Save stamp of the first (attempted) retransmit. */ 3710 if (!tp->retrans_stamp) 3711 tp->retrans_stamp = tcp_skb_timestamp_ts(tp->tcp_usec_ts, skb); 3712 3713 if (tp->undo_retrans < 0) 3714 tp->undo_retrans = 0; 3715 tp->undo_retrans += tcp_skb_pcount(skb); 3716 return err; 3717 } 3718 3719 /* This gets called after a retransmit timeout, and the initially 3720 * retransmitted data is acknowledged. It tries to continue 3721 * resending the rest of the retransmit queue, until either 3722 * we've sent it all or the congestion window limit is reached. 3723 */ 3724 void tcp_xmit_retransmit_queue(struct sock *sk) 3725 { 3726 const struct inet_connection_sock *icsk = inet_csk(sk); 3727 struct sk_buff *skb, *rtx_head, *hole = NULL; 3728 struct tcp_sock *tp = tcp_sk(sk); 3729 bool rearm_timer = false; 3730 u32 max_segs; 3731 int mib_idx; 3732 3733 if (!tp->packets_out) 3734 return; 3735 3736 rtx_head = tcp_rtx_queue_head(sk); 3737 skb = tp->retransmit_skb_hint ?: rtx_head; 3738 max_segs = tcp_tso_segs(sk, tcp_current_mss(sk)); 3739 skb_rbtree_walk_from(skb) { 3740 __u8 sacked; 3741 int segs; 3742 3743 if (tcp_pacing_check(sk)) 3744 break; 3745 3746 /* we could do better than to assign each time */ 3747 if (!hole) 3748 tp->retransmit_skb_hint = skb; 3749 3750 segs = tcp_snd_cwnd(tp) - tcp_packets_in_flight(tp); 3751 if (segs <= 0) 3752 break; 3753 sacked = TCP_SKB_CB(skb)->sacked; 3754 /* In case tcp_shift_skb_data() have aggregated large skbs, 3755 * we need to make sure not sending too bigs TSO packets 3756 */ 3757 segs = min_t(int, segs, max_segs); 3758 3759 if (tp->retrans_out >= tp->lost_out) { 3760 break; 3761 } else if (!(sacked & TCPCB_LOST)) { 3762 if (!hole && !(sacked & (TCPCB_SACKED_RETRANS|TCPCB_SACKED_ACKED))) 3763 hole = skb; 3764 continue; 3765 3766 } else { 3767 if (icsk->icsk_ca_state != TCP_CA_Loss) 3768 mib_idx = LINUX_MIB_TCPFASTRETRANS; 3769 else 3770 mib_idx = LINUX_MIB_TCPSLOWSTARTRETRANS; 3771 } 3772 3773 if (sacked & (TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS)) 3774 continue; 3775 3776 if (tcp_small_queue_check(sk, skb, 1)) 3777 break; 3778 3779 if (tcp_retransmit_skb(sk, skb, segs)) 3780 break; 3781 3782 NET_ADD_STATS(sock_net(sk), mib_idx, tcp_skb_pcount(skb)); 3783 3784 if (tcp_in_cwnd_reduction(sk)) 3785 tp->prr_out += tcp_skb_pcount(skb); 3786 3787 if (skb == rtx_head && 3788 icsk->icsk_pending != ICSK_TIME_REO_TIMEOUT) 3789 rearm_timer = true; 3790 3791 } 3792 if (rearm_timer) 3793 tcp_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 3794 inet_csk(sk)->icsk_rto, true); 3795 } 3796 3797 /* Send a FIN. The caller locks the socket for us. 3798 * We should try to send a FIN packet really hard, but eventually give up. 3799 */ 3800 void tcp_send_fin(struct sock *sk) 3801 { 3802 struct sk_buff *skb, *tskb, *tail = tcp_write_queue_tail(sk); 3803 struct tcp_sock *tp = tcp_sk(sk); 3804 3805 /* Optimization, tack on the FIN if we have one skb in write queue and 3806 * this skb was not yet sent, or we are under memory pressure. 3807 * Note: in the latter case, FIN packet will be sent after a timeout, 3808 * as TCP stack thinks it has already been transmitted. 3809 */ 3810 tskb = tail; 3811 if (!tskb && tcp_under_memory_pressure(sk)) 3812 tskb = skb_rb_last(&sk->tcp_rtx_queue); 3813 3814 if (tskb) { 3815 TCP_SKB_CB(tskb)->tcp_flags |= TCPHDR_FIN; 3816 TCP_SKB_CB(tskb)->end_seq++; 3817 tp->write_seq++; 3818 if (!tail) { 3819 /* This means tskb was already sent. 3820 * Pretend we included the FIN on previous transmit. 3821 * We need to set tp->snd_nxt to the value it would have 3822 * if FIN had been sent. This is because retransmit path 3823 * does not change tp->snd_nxt. 3824 */ 3825 WRITE_ONCE(tp->snd_nxt, tp->snd_nxt + 1); 3826 return; 3827 } 3828 } else { 3829 skb = alloc_skb_fclone(MAX_TCP_HEADER, 3830 sk_gfp_mask(sk, GFP_ATOMIC | 3831 __GFP_NOWARN)); 3832 if (unlikely(!skb)) 3833 return; 3834 3835 INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 3836 skb_reserve(skb, MAX_TCP_HEADER); 3837 sk_forced_mem_schedule(sk, skb->truesize); 3838 /* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */ 3839 tcp_init_nondata_skb(skb, sk, tp->write_seq, 3840 TCPHDR_ACK | TCPHDR_FIN); 3841 tcp_queue_skb(sk, skb); 3842 } 3843 __tcp_push_pending_frames(sk, tcp_current_mss(sk), TCP_NAGLE_OFF); 3844 } 3845 3846 /* We get here when a process closes a file descriptor (either due to 3847 * an explicit close() or as a byproduct of exit()'ing) and there 3848 * was unread data in the receive queue. This behavior is recommended 3849 * by RFC 2525, section 2.17. -DaveM 3850 */ 3851 void tcp_send_active_reset(struct sock *sk, gfp_t priority, 3852 enum sk_rst_reason reason) 3853 { 3854 struct sk_buff *skb; 3855 3856 TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTRSTS); 3857 3858 /* NOTE: No TCP options attached and we never retransmit this. */ 3859 skb = alloc_skb(MAX_TCP_HEADER, priority); 3860 if (!skb) { 3861 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED); 3862 return; 3863 } 3864 3865 /* Reserve space for headers and prepare control bits. */ 3866 skb_reserve(skb, MAX_TCP_HEADER); 3867 tcp_init_nondata_skb(skb, sk, tcp_acceptable_seq(sk), 3868 TCPHDR_ACK | TCPHDR_RST); 3869 tcp_mstamp_refresh(tcp_sk(sk)); 3870 /* Send it off. */ 3871 if (tcp_transmit_skb(sk, skb, 0, priority)) 3872 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED); 3873 3874 /* skb of trace_tcp_send_reset() keeps the skb that caused RST, 3875 * skb here is different to the troublesome skb, so use NULL 3876 */ 3877 trace_tcp_send_reset(sk, NULL, reason); 3878 } 3879 3880 /* Send a crossed SYN-ACK during socket establishment. 3881 * WARNING: This routine must only be called when we have already sent 3882 * a SYN packet that crossed the incoming SYN that caused this routine 3883 * to get called. If this assumption fails then the initial rcv_wnd 3884 * and rcv_wscale values will not be correct. 3885 */ 3886 int tcp_send_synack(struct sock *sk) 3887 { 3888 struct sk_buff *skb; 3889 3890 skb = tcp_rtx_queue_head(sk); 3891 if (!skb || !(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { 3892 pr_err("%s: wrong queue state\n", __func__); 3893 return -EFAULT; 3894 } 3895 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)) { 3896 if (skb_cloned(skb)) { 3897 struct sk_buff *nskb; 3898 3899 tcp_skb_tsorted_save(skb) { 3900 nskb = skb_copy(skb, GFP_ATOMIC); 3901 } tcp_skb_tsorted_restore(skb); 3902 if (!nskb) 3903 return -ENOMEM; 3904 INIT_LIST_HEAD(&nskb->tcp_tsorted_anchor); 3905 tcp_highest_sack_replace(sk, skb, nskb); 3906 tcp_rtx_queue_unlink_and_free(skb, sk); 3907 __skb_header_release(nskb); 3908 tcp_rbtree_insert(&sk->tcp_rtx_queue, nskb); 3909 sk_wmem_queued_add(sk, nskb->truesize); 3910 sk_mem_charge(sk, nskb->truesize); 3911 skb = nskb; 3912 } 3913 3914 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ACK; 3915 tcp_ecn_send_synack(sk, skb); 3916 } 3917 return tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC); 3918 } 3919 3920 /** 3921 * tcp_make_synack - Allocate one skb and build a SYNACK packet. 3922 * @sk: listener socket 3923 * @dst: dst entry attached to the SYNACK. It is consumed and caller 3924 * should not use it again. 3925 * @req: request_sock pointer 3926 * @foc: cookie for tcp fast open 3927 * @synack_type: Type of synack to prepare 3928 * @syn_skb: SYN packet just received. It could be NULL for rtx case. 3929 */ 3930 struct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst, 3931 struct request_sock *req, 3932 struct tcp_fastopen_cookie *foc, 3933 enum tcp_synack_type synack_type, 3934 struct sk_buff *syn_skb) 3935 { 3936 struct inet_request_sock *ireq = inet_rsk(req); 3937 const struct tcp_sock *tp = tcp_sk(sk); 3938 struct tcp_out_options opts; 3939 struct tcp_key key = {}; 3940 struct sk_buff *skb; 3941 int tcp_header_size; 3942 struct tcphdr *th; 3943 int mss; 3944 u64 now; 3945 3946 skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC); 3947 if (unlikely(!skb)) { 3948 dst_release(dst); 3949 return NULL; 3950 } 3951 /* Reserve space for headers. */ 3952 skb_reserve(skb, MAX_TCP_HEADER); 3953 3954 switch (synack_type) { 3955 case TCP_SYNACK_NORMAL: 3956 case TCP_SYNACK_RETRANS: 3957 skb_set_owner_edemux(skb, req_to_sk(req)); 3958 break; 3959 case TCP_SYNACK_COOKIE: 3960 /* Under synflood, we do not attach skb to a socket, 3961 * to avoid false sharing. 3962 */ 3963 break; 3964 case TCP_SYNACK_FASTOPEN: 3965 /* sk is a const pointer, because we want to express multiple 3966 * cpu might call us concurrently. 3967 * sk->sk_wmem_alloc in an atomic, we can promote to rw. 3968 */ 3969 skb_set_owner_w(skb, (struct sock *)sk); 3970 break; 3971 } 3972 skb_dst_set(skb, dst); 3973 3974 mss = tcp_mss_clamp(tp, dst_metric_advmss(dst)); 3975 3976 memset(&opts, 0, sizeof(opts)); 3977 now = tcp_clock_ns(); 3978 #ifdef CONFIG_SYN_COOKIES 3979 if (unlikely(synack_type == TCP_SYNACK_COOKIE && ireq->tstamp_ok)) 3980 skb_set_delivery_time(skb, cookie_init_timestamp(req, now), 3981 SKB_CLOCK_MONOTONIC); 3982 else 3983 #endif 3984 { 3985 skb_set_delivery_time(skb, now, SKB_CLOCK_MONOTONIC); 3986 if (!tcp_rsk(req)->snt_synack) /* Timestamp first SYNACK */ 3987 tcp_rsk(req)->snt_synack = tcp_skb_timestamp_us(skb); 3988 } 3989 3990 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) 3991 rcu_read_lock(); 3992 #endif 3993 if (tcp_rsk_used_ao(req)) { 3994 #ifdef CONFIG_TCP_AO 3995 struct tcp_ao_key *ao_key = NULL; 3996 u8 keyid = tcp_rsk(req)->ao_keyid; 3997 u8 rnext = tcp_rsk(req)->ao_rcv_next; 3998 3999 ao_key = tcp_sk(sk)->af_specific->ao_lookup(sk, req_to_sk(req), 4000 keyid, -1); 4001 /* If there is no matching key - avoid sending anything, 4002 * especially usigned segments. It could try harder and lookup 4003 * for another peer-matching key, but the peer has requested 4004 * ao_keyid (RFC5925 RNextKeyID), so let's keep it simple here. 4005 */ 4006 if (unlikely(!ao_key)) { 4007 trace_tcp_ao_synack_no_key(sk, keyid, rnext); 4008 rcu_read_unlock(); 4009 kfree_skb(skb); 4010 net_warn_ratelimited("TCP-AO: the keyid %u from SYN packet is not present - not sending SYNACK\n", 4011 keyid); 4012 return NULL; 4013 } 4014 key.ao_key = ao_key; 4015 key.type = TCP_KEY_AO; 4016 #endif 4017 } else { 4018 #ifdef CONFIG_TCP_MD5SIG 4019 key.md5_key = tcp_rsk(req)->af_specific->req_md5_lookup(sk, 4020 req_to_sk(req)); 4021 if (key.md5_key) 4022 key.type = TCP_KEY_MD5; 4023 #endif 4024 } 4025 skb_set_hash(skb, READ_ONCE(tcp_rsk(req)->txhash), PKT_HASH_TYPE_L4); 4026 /* bpf program will be interested in the tcp_flags */ 4027 TCP_SKB_CB(skb)->tcp_flags = TCPHDR_SYN | TCPHDR_ACK; 4028 tcp_header_size = tcp_synack_options(sk, req, mss, skb, &opts, 4029 &key, foc, synack_type, syn_skb) 4030 + sizeof(*th); 4031 4032 skb_push(skb, tcp_header_size); 4033 skb_reset_transport_header(skb); 4034 4035 th = (struct tcphdr *)skb->data; 4036 memset(th, 0, sizeof(struct tcphdr)); 4037 th->syn = 1; 4038 th->ack = 1; 4039 tcp_ecn_make_synack(req, th, synack_type); 4040 th->source = htons(ireq->ir_num); 4041 th->dest = ireq->ir_rmt_port; 4042 skb->mark = ireq->ir_mark; 4043 skb->ip_summed = CHECKSUM_PARTIAL; 4044 th->seq = htonl(tcp_rsk(req)->snt_isn); 4045 /* XXX data is queued and acked as is. No buffer/window check */ 4046 th->ack_seq = htonl(tcp_rsk(req)->rcv_nxt); 4047 4048 /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */ 4049 th->window = htons(min(req->rsk_rcv_wnd, 65535U)); 4050 tcp_options_write(th, NULL, tcp_rsk(req), &opts, &key); 4051 th->doff = (tcp_header_size >> 2); 4052 TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS); 4053 4054 /* Okay, we have all we need - do the md5 hash if needed */ 4055 if (tcp_key_is_md5(&key)) { 4056 #ifdef CONFIG_TCP_MD5SIG 4057 tcp_rsk(req)->af_specific->calc_md5_hash(opts.hash_location, 4058 key.md5_key, req_to_sk(req), skb); 4059 #endif 4060 } else if (tcp_key_is_ao(&key)) { 4061 #ifdef CONFIG_TCP_AO 4062 tcp_rsk(req)->af_specific->ao_synack_hash(opts.hash_location, 4063 key.ao_key, req, skb, 4064 opts.hash_location - (u8 *)th, 0); 4065 #endif 4066 } 4067 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO) 4068 rcu_read_unlock(); 4069 #endif 4070 4071 bpf_skops_write_hdr_opt((struct sock *)sk, skb, req, syn_skb, 4072 synack_type, &opts); 4073 4074 skb_set_delivery_time(skb, now, SKB_CLOCK_MONOTONIC); 4075 tcp_add_tx_delay(skb, tp); 4076 4077 return skb; 4078 } 4079 4080 static void tcp_ca_dst_init(struct sock *sk, const struct dst_entry *dst) 4081 { 4082 struct inet_connection_sock *icsk = inet_csk(sk); 4083 const struct tcp_congestion_ops *ca; 4084 u32 ca_key = dst_metric(dst, RTAX_CC_ALGO); 4085 4086 if (ca_key == TCP_CA_UNSPEC) 4087 return; 4088 4089 rcu_read_lock(); 4090 ca = tcp_ca_find_key(ca_key); 4091 if (likely(ca && bpf_try_module_get(ca, ca->owner))) { 4092 bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner); 4093 icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst); 4094 icsk->icsk_ca_ops = ca; 4095 } 4096 rcu_read_unlock(); 4097 } 4098 4099 /* Do all connect socket setups that can be done AF independent. */ 4100 static void tcp_connect_init(struct sock *sk) 4101 { 4102 const struct dst_entry *dst = __sk_dst_get(sk); 4103 struct tcp_sock *tp = tcp_sk(sk); 4104 __u8 rcv_wscale; 4105 u16 user_mss; 4106 u32 rcv_wnd; 4107 4108 /* We'll fix this up when we get a response from the other end. 4109 * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT. 4110 */ 4111 tp->tcp_header_len = sizeof(struct tcphdr); 4112 if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps)) 4113 tp->tcp_header_len += TCPOLEN_TSTAMP_ALIGNED; 4114 4115 tcp_ao_connect_init(sk); 4116 4117 /* If user gave his TCP_MAXSEG, record it to clamp */ 4118 user_mss = READ_ONCE(tp->rx_opt.user_mss); 4119 if (user_mss) 4120 tp->rx_opt.mss_clamp = user_mss; 4121 tp->max_window = 0; 4122 tcp_mtup_init(sk); 4123 tcp_sync_mss(sk, dst_mtu(dst)); 4124 4125 tcp_ca_dst_init(sk, dst); 4126 4127 if (!tp->window_clamp) 4128 WRITE_ONCE(tp->window_clamp, dst_metric(dst, RTAX_WINDOW)); 4129 tp->advmss = tcp_mss_clamp(tp, dst_metric_advmss(dst)); 4130 4131 tcp_initialize_rcv_mss(sk); 4132 4133 /* limit the window selection if the user enforce a smaller rx buffer */ 4134 if (sk->sk_userlocks & SOCK_RCVBUF_LOCK && 4135 (tp->window_clamp > tcp_full_space(sk) || tp->window_clamp == 0)) 4136 WRITE_ONCE(tp->window_clamp, tcp_full_space(sk)); 4137 4138 rcv_wnd = tcp_rwnd_init_bpf(sk); 4139 if (rcv_wnd == 0) 4140 rcv_wnd = dst_metric(dst, RTAX_INITRWND); 4141 4142 tcp_select_initial_window(sk, tcp_full_space(sk), 4143 tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0), 4144 &tp->rcv_wnd, 4145 &tp->window_clamp, 4146 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_window_scaling), 4147 &rcv_wscale, 4148 rcv_wnd); 4149 4150 tp->rx_opt.rcv_wscale = rcv_wscale; 4151 tp->rcv_ssthresh = tp->rcv_wnd; 4152 4153 WRITE_ONCE(sk->sk_err, 0); 4154 sock_reset_flag(sk, SOCK_DONE); 4155 tp->snd_wnd = 0; 4156 tcp_init_wl(tp, 0); 4157 tcp_write_queue_purge(sk); 4158 WRITE_ONCE(tp->snd_una, tp->write_seq); 4159 tp->snd_sml = tp->write_seq; 4160 tp->snd_up = tp->write_seq; 4161 WRITE_ONCE(tp->snd_nxt, tp->write_seq); 4162 4163 if (likely(!tp->repair)) 4164 tp->rcv_nxt = 0; 4165 else 4166 tp->rcv_tstamp = tcp_jiffies32; 4167 tp->rcv_wup = tp->rcv_nxt; 4168 tp->rcv_mwnd_seq = tp->rcv_nxt + tp->rcv_wnd; 4169 WRITE_ONCE(tp->copied_seq, tp->rcv_nxt); 4170 4171 inet_csk(sk)->icsk_rto = tcp_timeout_init(sk); 4172 WRITE_ONCE(inet_csk(sk)->icsk_retransmits, 0); 4173 tcp_clear_retrans(tp); 4174 } 4175 4176 static void tcp_connect_queue_skb(struct sock *sk, struct sk_buff *skb) 4177 { 4178 struct tcp_sock *tp = tcp_sk(sk); 4179 struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); 4180 4181 tcb->end_seq += skb->len; 4182 __skb_header_release(skb); 4183 sk_wmem_queued_add(sk, skb->truesize); 4184 sk_mem_charge(sk, skb->truesize); 4185 WRITE_ONCE(tp->write_seq, tcb->end_seq); 4186 tp->packets_out += tcp_skb_pcount(skb); 4187 } 4188 4189 /* Build and send a SYN with data and (cached) Fast Open cookie. However, 4190 * queue a data-only packet after the regular SYN, such that regular SYNs 4191 * are retransmitted on timeouts. Also if the remote SYN-ACK acknowledges 4192 * only the SYN sequence, the data are retransmitted in the first ACK. 4193 * If cookie is not cached or other error occurs, falls back to send a 4194 * regular SYN with Fast Open cookie request option. 4195 */ 4196 static int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn) 4197 { 4198 struct inet_connection_sock *icsk = inet_csk(sk); 4199 struct tcp_sock *tp = tcp_sk(sk); 4200 struct tcp_fastopen_request *fo = tp->fastopen_req; 4201 struct page_frag *pfrag = sk_page_frag(sk); 4202 struct sk_buff *syn_data; 4203 int space, err = 0; 4204 4205 tp->rx_opt.mss_clamp = tp->advmss; /* If MSS is not cached */ 4206 if (!tcp_fastopen_cookie_check(sk, &tp->rx_opt.mss_clamp, &fo->cookie)) 4207 goto fallback; 4208 4209 /* MSS for SYN-data is based on cached MSS and bounded by PMTU and 4210 * user-MSS. Reserve maximum option space for middleboxes that add 4211 * private TCP options. The cost is reduced data space in SYN :( 4212 */ 4213 tp->rx_opt.mss_clamp = tcp_mss_clamp(tp, tp->rx_opt.mss_clamp); 4214 /* Sync mss_cache after updating the mss_clamp */ 4215 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 4216 4217 space = __tcp_mtu_to_mss(sk, icsk->icsk_pmtu_cookie) - 4218 MAX_TCP_OPTION_SPACE; 4219 4220 space = min_t(size_t, space, fo->size); 4221 4222 if (space && 4223 !skb_page_frag_refill(min_t(size_t, space, PAGE_SIZE), 4224 pfrag, sk->sk_allocation)) 4225 goto fallback; 4226 syn_data = tcp_stream_alloc_skb(sk, sk->sk_allocation, false); 4227 if (!syn_data) 4228 goto fallback; 4229 memcpy(syn_data->cb, syn->cb, sizeof(syn->cb)); 4230 if (space) { 4231 space = min_t(size_t, space, pfrag->size - pfrag->offset); 4232 space = tcp_wmem_schedule(sk, space); 4233 } 4234 if (space) { 4235 space = copy_page_from_iter(pfrag->page, pfrag->offset, 4236 space, &fo->data->msg_iter); 4237 if (unlikely(!space)) { 4238 tcp_skb_tsorted_anchor_cleanup(syn_data); 4239 kfree_skb(syn_data); 4240 goto fallback; 4241 } 4242 skb_fill_page_desc(syn_data, 0, pfrag->page, 4243 pfrag->offset, space); 4244 page_ref_inc(pfrag->page); 4245 pfrag->offset += space; 4246 skb_len_add(syn_data, space); 4247 skb_zcopy_set(syn_data, fo->uarg, NULL); 4248 } 4249 /* No more data pending in inet_wait_for_connect() */ 4250 if (space == fo->size) 4251 fo->data = NULL; 4252 fo->copied = space; 4253 4254 tcp_connect_queue_skb(sk, syn_data); 4255 if (syn_data->len) 4256 tcp_chrono_start(sk, TCP_CHRONO_BUSY); 4257 4258 err = tcp_transmit_skb(sk, syn_data, 1, sk->sk_allocation); 4259 4260 skb_set_delivery_time(syn, syn_data->skb_mstamp_ns, SKB_CLOCK_MONOTONIC); 4261 4262 /* Now full SYN+DATA was cloned and sent (or not), 4263 * remove the SYN from the original skb (syn_data) 4264 * we keep in write queue in case of a retransmit, as we 4265 * also have the SYN packet (with no data) in the same queue. 4266 */ 4267 TCP_SKB_CB(syn_data)->seq++; 4268 TCP_SKB_CB(syn_data)->tcp_flags = TCPHDR_ACK | TCPHDR_PSH; 4269 if (!err) { 4270 tp->syn_data = (fo->copied > 0); 4271 tcp_rbtree_insert(&sk->tcp_rtx_queue, syn_data); 4272 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPORIGDATASENT); 4273 goto done; 4274 } 4275 4276 /* data was not sent, put it in write_queue */ 4277 __skb_queue_tail(&sk->sk_write_queue, syn_data); 4278 tp->packets_out -= tcp_skb_pcount(syn_data); 4279 4280 fallback: 4281 /* Send a regular SYN with Fast Open cookie request option */ 4282 if (fo->cookie.len > 0) 4283 fo->cookie.len = 0; 4284 err = tcp_transmit_skb(sk, syn, 1, sk->sk_allocation); 4285 if (err) 4286 tp->syn_fastopen = 0; 4287 done: 4288 fo->cookie.len = -1; /* Exclude Fast Open option for SYN retries */ 4289 return err; 4290 } 4291 4292 /* Build a SYN and send it off. */ 4293 int tcp_connect(struct sock *sk) 4294 { 4295 struct tcp_sock *tp = tcp_sk(sk); 4296 struct sk_buff *buff; 4297 int err; 4298 4299 tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_CONNECT_CB, 0, NULL); 4300 4301 #if defined(CONFIG_TCP_MD5SIG) && defined(CONFIG_TCP_AO) 4302 /* Has to be checked late, after setting daddr/saddr/ops. 4303 * Return error if the peer has both a md5 and a tcp-ao key 4304 * configured as this is ambiguous. 4305 */ 4306 if (unlikely(rcu_dereference_protected(tp->md5sig_info, 4307 lockdep_sock_is_held(sk)))) { 4308 bool needs_ao = !!tp->af_specific->ao_lookup(sk, sk, -1, -1); 4309 bool needs_md5 = !!tp->af_specific->md5_lookup(sk, sk); 4310 struct tcp_ao_info *ao_info; 4311 4312 ao_info = rcu_dereference_check(tp->ao_info, 4313 lockdep_sock_is_held(sk)); 4314 if (ao_info) { 4315 /* This is an extra check: tcp_ao_required() in 4316 * tcp_v{4,6}_parse_md5_keys() should prevent adding 4317 * md5 keys on ao_required socket. 4318 */ 4319 needs_ao |= ao_info->ao_required; 4320 WARN_ON_ONCE(ao_info->ao_required && needs_md5); 4321 } 4322 if (needs_md5 && needs_ao) 4323 return -EKEYREJECTED; 4324 4325 /* If we have a matching md5 key and no matching tcp-ao key 4326 * then free up ao_info if allocated. 4327 */ 4328 if (needs_md5) { 4329 tcp_ao_destroy_sock(sk, false); 4330 } else if (needs_ao) { 4331 tcp_clear_md5_list(sk); 4332 kfree(rcu_replace_pointer(tp->md5sig_info, NULL, 4333 lockdep_sock_is_held(sk))); 4334 } 4335 } 4336 #endif 4337 #ifdef CONFIG_TCP_AO 4338 if (unlikely(rcu_dereference_protected(tp->ao_info, 4339 lockdep_sock_is_held(sk)))) { 4340 /* Don't allow connecting if ao is configured but no 4341 * matching key is found. 4342 */ 4343 if (!tp->af_specific->ao_lookup(sk, sk, -1, -1)) 4344 return -EKEYREJECTED; 4345 } 4346 #endif 4347 4348 if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk)) 4349 return -EHOSTUNREACH; /* Routing failure or similar. */ 4350 4351 tcp_connect_init(sk); 4352 4353 if (unlikely(tp->repair)) { 4354 tcp_finish_connect(sk, NULL); 4355 return 0; 4356 } 4357 4358 buff = tcp_stream_alloc_skb(sk, sk->sk_allocation, true); 4359 if (unlikely(!buff)) 4360 return -ENOBUFS; 4361 4362 /* SYN eats a sequence byte, write_seq updated by 4363 * tcp_connect_queue_skb(). 4364 */ 4365 tcp_init_nondata_skb(buff, sk, tp->write_seq, TCPHDR_SYN); 4366 tcp_mstamp_refresh(tp); 4367 tp->retrans_stamp = tcp_time_stamp_ts(tp); 4368 tcp_connect_queue_skb(sk, buff); 4369 tcp_ecn_send_syn(sk, buff); 4370 tcp_rbtree_insert(&sk->tcp_rtx_queue, buff); 4371 4372 /* Send off SYN; include data in Fast Open. */ 4373 err = tp->fastopen_req ? tcp_send_syn_data(sk, buff) : 4374 tcp_transmit_skb(sk, buff, 1, sk->sk_allocation); 4375 if (err == -ECONNREFUSED) 4376 return err; 4377 4378 /* We change tp->snd_nxt after the tcp_transmit_skb() call 4379 * in order to make this packet get counted in tcpOutSegs. 4380 */ 4381 WRITE_ONCE(tp->snd_nxt, tp->write_seq); 4382 tp->pushed_seq = tp->write_seq; 4383 buff = tcp_send_head(sk); 4384 if (unlikely(buff)) { 4385 WRITE_ONCE(tp->snd_nxt, TCP_SKB_CB(buff)->seq); 4386 tp->pushed_seq = TCP_SKB_CB(buff)->seq; 4387 } 4388 TCP_INC_STATS(sock_net(sk), TCP_MIB_ACTIVEOPENS); 4389 4390 /* Timer for repeating the SYN until an answer. */ 4391 tcp_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 4392 inet_csk(sk)->icsk_rto, false); 4393 return 0; 4394 } 4395 EXPORT_SYMBOL(tcp_connect); 4396 4397 u32 tcp_delack_max(const struct sock *sk) 4398 { 4399 u32 delack_from_rto_min = max(tcp_rto_min(sk), 2) - 1; 4400 4401 return min(READ_ONCE(inet_csk(sk)->icsk_delack_max), delack_from_rto_min); 4402 } 4403 4404 /* Send out a delayed ack, the caller does the policy checking 4405 * to see if we should even be here. See tcp_input.c:tcp_ack_snd_check() 4406 * for details. 4407 */ 4408 void tcp_send_delayed_ack(struct sock *sk) 4409 { 4410 struct inet_connection_sock *icsk = inet_csk(sk); 4411 int ato = icsk->icsk_ack.ato; 4412 unsigned long timeout; 4413 4414 if (ato > TCP_DELACK_MIN) { 4415 const struct tcp_sock *tp = tcp_sk(sk); 4416 int max_ato = HZ / 2; 4417 4418 if (inet_csk_in_pingpong_mode(sk) || 4419 (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)) 4420 max_ato = TCP_DELACK_MAX; 4421 4422 /* Slow path, intersegment interval is "high". */ 4423 4424 /* If some rtt estimate is known, use it to bound delayed ack. 4425 * Do not use inet_csk(sk)->icsk_rto here, use results of rtt measurements 4426 * directly. 4427 */ 4428 if (tp->srtt_us) { 4429 int rtt = max_t(int, usecs_to_jiffies(tp->srtt_us >> 3), 4430 TCP_DELACK_MIN); 4431 4432 if (rtt < max_ato) 4433 max_ato = rtt; 4434 } 4435 4436 ato = min(ato, max_ato); 4437 } 4438 4439 ato = min_t(u32, ato, tcp_delack_max(sk)); 4440 4441 /* Stay within the limit we were given */ 4442 timeout = jiffies + ato; 4443 4444 /* Use new timeout only if there wasn't a older one earlier. */ 4445 if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) { 4446 /* If delack timer is about to expire, send ACK now. */ 4447 if (time_before_eq(icsk_delack_timeout(icsk), jiffies + (ato >> 2))) { 4448 tcp_send_ack(sk); 4449 return; 4450 } 4451 4452 if (!time_before(timeout, icsk_delack_timeout(icsk))) 4453 timeout = icsk_delack_timeout(icsk); 4454 } 4455 smp_store_release(&icsk->icsk_ack.pending, 4456 icsk->icsk_ack.pending | ICSK_ACK_SCHED | ICSK_ACK_TIMER); 4457 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); 4458 } 4459 4460 /* This routine sends an ack and also updates the window. */ 4461 void __tcp_send_ack(struct sock *sk, u32 rcv_nxt, u16 flags) 4462 { 4463 struct sk_buff *buff; 4464 4465 /* If we have been reset, we may not send again. */ 4466 if (sk->sk_state == TCP_CLOSE) 4467 return; 4468 4469 /* We are not putting this on the write queue, so 4470 * tcp_transmit_skb() will set the ownership to this 4471 * sock. 4472 */ 4473 buff = alloc_skb(MAX_TCP_HEADER, 4474 sk_gfp_mask(sk, GFP_ATOMIC | __GFP_NOWARN)); 4475 if (unlikely(!buff)) { 4476 struct inet_connection_sock *icsk = inet_csk(sk); 4477 unsigned long delay; 4478 4479 delay = TCP_DELACK_MAX << icsk->icsk_ack.retry; 4480 if (delay < tcp_rto_max(sk)) 4481 icsk->icsk_ack.retry++; 4482 inet_csk_schedule_ack(sk); 4483 icsk->icsk_ack.ato = TCP_ATO_MIN; 4484 tcp_reset_xmit_timer(sk, ICSK_TIME_DACK, delay, false); 4485 return; 4486 } 4487 4488 /* Reserve space for headers and prepare control bits. */ 4489 skb_reserve(buff, MAX_TCP_HEADER); 4490 tcp_init_nondata_skb(buff, sk, 4491 tcp_acceptable_seq(sk), TCPHDR_ACK | flags); 4492 4493 /* We do not want pure acks influencing TCP Small Queues or fq/pacing 4494 * too much. 4495 * SKB_TRUESIZE(max(1 .. 66, MAX_TCP_HEADER)) is unfortunately ~784 4496 */ 4497 skb_set_tcp_pure_ack(buff); 4498 4499 /* Send it off, this clears delayed acks for us. */ 4500 __tcp_transmit_skb(sk, buff, 0, (__force gfp_t)0, rcv_nxt); 4501 } 4502 EXPORT_SYMBOL_GPL(__tcp_send_ack); 4503 4504 void tcp_send_ack(struct sock *sk) 4505 { 4506 __tcp_send_ack(sk, tcp_sk(sk)->rcv_nxt, 0); 4507 } 4508 4509 /* This routine sends a packet with an out of date sequence 4510 * number. It assumes the other end will try to ack it. 4511 * 4512 * Question: what should we make while urgent mode? 4513 * 4.4BSD forces sending single byte of data. We cannot send 4514 * out of window data, because we have SND.NXT==SND.MAX... 4515 * 4516 * Current solution: to send TWO zero-length segments in urgent mode: 4517 * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is 4518 * out-of-date with SND.UNA-1 to probe window. 4519 */ 4520 static int tcp_xmit_probe_skb(struct sock *sk, int urgent, int mib) 4521 { 4522 struct tcp_sock *tp = tcp_sk(sk); 4523 struct sk_buff *skb; 4524 4525 /* We don't queue it, tcp_transmit_skb() sets ownership. */ 4526 skb = alloc_skb(MAX_TCP_HEADER, 4527 sk_gfp_mask(sk, GFP_ATOMIC | __GFP_NOWARN)); 4528 if (!skb) 4529 return -1; 4530 4531 /* Reserve space for headers and set control bits. */ 4532 skb_reserve(skb, MAX_TCP_HEADER); 4533 /* Use a previous sequence. This should cause the other 4534 * end to send an ack. Don't queue or clone SKB, just 4535 * send it. 4536 */ 4537 tcp_init_nondata_skb(skb, sk, tp->snd_una - !urgent, TCPHDR_ACK); 4538 NET_INC_STATS(sock_net(sk), mib); 4539 return tcp_transmit_skb(sk, skb, 0, (__force gfp_t)0); 4540 } 4541 4542 /* Called from setsockopt( ... TCP_REPAIR ) */ 4543 void tcp_send_window_probe(struct sock *sk) 4544 { 4545 if (sk->sk_state == TCP_ESTABLISHED) { 4546 tcp_sk(sk)->snd_wl1 = tcp_sk(sk)->rcv_nxt - 1; 4547 tcp_mstamp_refresh(tcp_sk(sk)); 4548 tcp_xmit_probe_skb(sk, 0, LINUX_MIB_TCPWINPROBE); 4549 } 4550 } 4551 4552 /* Initiate keepalive or window probe from timer. */ 4553 int tcp_write_wakeup(struct sock *sk, int mib) 4554 { 4555 struct tcp_sock *tp = tcp_sk(sk); 4556 struct sk_buff *skb; 4557 4558 if (sk->sk_state == TCP_CLOSE) 4559 return -1; 4560 4561 skb = tcp_send_head(sk); 4562 if (skb && before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) { 4563 int err; 4564 unsigned int mss = tcp_current_mss(sk); 4565 unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; 4566 4567 if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq)) 4568 tp->pushed_seq = TCP_SKB_CB(skb)->end_seq; 4569 4570 /* We are probing the opening of a window 4571 * but the window size is != 0 4572 * must have been a result SWS avoidance ( sender ) 4573 */ 4574 if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq || 4575 skb->len > mss) { 4576 seg_size = min(seg_size, mss); 4577 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH; 4578 if (tcp_fragment(sk, TCP_FRAG_IN_WRITE_QUEUE, 4579 skb, seg_size, mss, GFP_ATOMIC)) 4580 return -1; 4581 } else if (!tcp_skb_pcount(skb)) 4582 tcp_set_skb_tso_segs(skb, mss); 4583 4584 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH; 4585 err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC); 4586 if (!err) 4587 tcp_event_new_data_sent(sk, skb); 4588 return err; 4589 } else { 4590 if (between(tp->snd_up, tp->snd_una + 1, tp->snd_una + 0xFFFF)) 4591 tcp_xmit_probe_skb(sk, 1, mib); 4592 return tcp_xmit_probe_skb(sk, 0, mib); 4593 } 4594 } 4595 4596 /* A window probe timeout has occurred. If window is not closed send 4597 * a partial packet else a zero probe. 4598 */ 4599 void tcp_send_probe0(struct sock *sk) 4600 { 4601 struct inet_connection_sock *icsk = inet_csk(sk); 4602 struct tcp_sock *tp = tcp_sk(sk); 4603 struct net *net = sock_net(sk); 4604 unsigned long timeout; 4605 int err; 4606 4607 err = tcp_write_wakeup(sk, LINUX_MIB_TCPWINPROBE); 4608 4609 if (tp->packets_out || tcp_write_queue_empty(sk)) { 4610 /* Cancel probe timer, if it is not required. */ 4611 WRITE_ONCE(icsk->icsk_probes_out, 0); 4612 icsk->icsk_backoff = 0; 4613 icsk->icsk_probes_tstamp = 0; 4614 return; 4615 } 4616 4617 WRITE_ONCE(icsk->icsk_probes_out, icsk->icsk_probes_out + 1); 4618 if (err <= 0) { 4619 if (icsk->icsk_backoff < READ_ONCE(net->ipv4.sysctl_tcp_retries2)) 4620 icsk->icsk_backoff++; 4621 timeout = tcp_probe0_when(sk, tcp_rto_max(sk)); 4622 } else { 4623 /* If packet was not sent due to local congestion, 4624 * Let senders fight for local resources conservatively. 4625 */ 4626 timeout = TCP_RESOURCE_PROBE_INTERVAL; 4627 } 4628 4629 timeout = tcp_clamp_probe0_to_user_timeout(sk, timeout); 4630 tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0, timeout, true); 4631 } 4632 4633 int tcp_rtx_synack(const struct sock *sk, struct request_sock *req) 4634 { 4635 const struct tcp_request_sock_ops *af_ops = tcp_rsk(req)->af_specific; 4636 struct flowi fl; 4637 int res; 4638 4639 /* Paired with WRITE_ONCE() in sock_setsockopt() */ 4640 if (READ_ONCE(sk->sk_txrehash) == SOCK_TXREHASH_ENABLED) 4641 WRITE_ONCE(tcp_rsk(req)->txhash, net_tx_rndhash()); 4642 res = af_ops->send_synack(sk, NULL, &fl, req, NULL, TCP_SYNACK_RETRANS, 4643 NULL); 4644 if (!res) { 4645 TCP_INC_STATS(sock_net(sk), TCP_MIB_RETRANSSEGS); 4646 NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNRETRANS); 4647 if (unlikely(tcp_passive_fastopen(sk))) { 4648 /* sk has const attribute because listeners are lockless. 4649 * However in this case, we are dealing with a passive fastopen 4650 * socket thus we can change total_retrans value. 4651 */ 4652 WRITE_ONCE(tcp_sk_rw(sk)->total_retrans, 4653 tcp_sk_rw(sk)->total_retrans + 1); 4654 } 4655 trace_tcp_retransmit_synack(sk, req); 4656 WRITE_ONCE(req->num_retrans, req->num_retrans + 1); 4657 } 4658 return res; 4659 } 4660