18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * INET An implementation of the TCP/IP protocol suite for the LINUX 48c2ecf20Sopenharmony_ci * operating system. INET is implemented using the BSD Socket 58c2ecf20Sopenharmony_ci * interface as the means of communication with the user level. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Implementation of the Transmission Control Protocol(TCP). 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Authors: Ross Biro 108c2ecf20Sopenharmony_ci * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> 118c2ecf20Sopenharmony_ci * Mark Evans, <evansmp@uhura.aston.ac.uk> 128c2ecf20Sopenharmony_ci * Corey Minyard <wf-rch!minyard@relay.EU.net> 138c2ecf20Sopenharmony_ci * Florian La Roche, <flla@stud.uni-sb.de> 148c2ecf20Sopenharmony_ci * Charles Hedrick, <hedrick@klinzhai.rutgers.edu> 158c2ecf20Sopenharmony_ci * Linus Torvalds, <torvalds@cs.helsinki.fi> 168c2ecf20Sopenharmony_ci * Alan Cox, <gw4pts@gw4pts.ampr.org> 178c2ecf20Sopenharmony_ci * Matthew Dillon, <dillon@apollo.west.oic.com> 188c2ecf20Sopenharmony_ci * Arnt Gulbrandsen, <agulbra@nvg.unit.no> 198c2ecf20Sopenharmony_ci * Jorge Cwik, <jorge@laser.satlink.net> 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/* 238c2ecf20Sopenharmony_ci * Changes: Pedro Roque : Retransmit queue handled by TCP. 248c2ecf20Sopenharmony_ci * : Fragmentation on mtu decrease 258c2ecf20Sopenharmony_ci * : Segment collapse on retransmit 268c2ecf20Sopenharmony_ci * : AF independence 278c2ecf20Sopenharmony_ci * 288c2ecf20Sopenharmony_ci * Linus Torvalds : send_delayed_ack 298c2ecf20Sopenharmony_ci * David S. Miller : Charge memory using the right skb 308c2ecf20Sopenharmony_ci * during syn/ack processing. 318c2ecf20Sopenharmony_ci * David S. Miller : Output engine completely rewritten. 328c2ecf20Sopenharmony_ci * Andrea Arcangeli: SYNACK carry ts_recent in tsecr. 338c2ecf20Sopenharmony_ci * Cacophonix Gaul : draft-minshall-nagle-01 348c2ecf20Sopenharmony_ci * J Hadi Salim : ECN support 358c2ecf20Sopenharmony_ci * 368c2ecf20Sopenharmony_ci */ 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "TCP: " fmt 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci#include <net/tcp.h> 418c2ecf20Sopenharmony_ci#include <net/mptcp.h> 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci#include <linux/compiler.h> 448c2ecf20Sopenharmony_ci#include <linux/gfp.h> 458c2ecf20Sopenharmony_ci#include <linux/module.h> 468c2ecf20Sopenharmony_ci#include <linux/static_key.h> 478c2ecf20Sopenharmony_ci#ifdef CONFIG_LOWPOWER_PROTOCOL 488c2ecf20Sopenharmony_ci#include <net/lowpower_protocol.h> 498c2ecf20Sopenharmony_ci#endif /* CONFIG_LOWPOWER_PROTOCOL */ 508c2ecf20Sopenharmony_ci#include <trace/events/tcp.h> 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci/* Refresh clocks of a TCP socket, 538c2ecf20Sopenharmony_ci * ensuring monotically increasing values. 548c2ecf20Sopenharmony_ci */ 558c2ecf20Sopenharmony_civoid tcp_mstamp_refresh(struct tcp_sock *tp) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci u64 val = tcp_clock_ns(); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci tp->tcp_clock_cache = val; 608c2ecf20Sopenharmony_ci tp->tcp_mstamp = div_u64(val, NSEC_PER_USEC); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle, 648c2ecf20Sopenharmony_ci int push_one, gfp_t gfp); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/* Account for new data that has been sent to the network. */ 678c2ecf20Sopenharmony_cistatic void tcp_event_new_data_sent(struct sock *sk, struct sk_buff *skb) 688c2ecf20Sopenharmony_ci{ 698c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 708c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 718c2ecf20Sopenharmony_ci unsigned int prior_packets = tp->packets_out; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci WRITE_ONCE(tp->snd_nxt, TCP_SKB_CB(skb)->end_seq); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci __skb_unlink(skb, &sk->sk_write_queue); 768c2ecf20Sopenharmony_ci tcp_rbtree_insert(&sk->tcp_rtx_queue, skb); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci if (tp->highest_sack == NULL) 798c2ecf20Sopenharmony_ci tp->highest_sack = skb; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci tp->packets_out += tcp_skb_pcount(skb); 828c2ecf20Sopenharmony_ci if (!prior_packets || icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) 838c2ecf20Sopenharmony_ci tcp_rearm_rto(sk); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPORIGDATASENT, 868c2ecf20Sopenharmony_ci tcp_skb_pcount(skb)); 878c2ecf20Sopenharmony_ci tcp_check_space(sk); 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci/* SND.NXT, if window was not shrunk or the amount of shrunk was less than one 918c2ecf20Sopenharmony_ci * window scaling factor due to loss of precision. 928c2ecf20Sopenharmony_ci * If window has been shrunk, what should we make? It is not clear at all. 938c2ecf20Sopenharmony_ci * Using SND.UNA we will fail to open window, SND.NXT is out of window. :-( 948c2ecf20Sopenharmony_ci * Anything in between SND.UNA...SND.UNA+SND.WND also can be already 958c2ecf20Sopenharmony_ci * invalid. OK, let's make this for now: 968c2ecf20Sopenharmony_ci */ 978c2ecf20Sopenharmony_cistatic inline __u32 tcp_acceptable_seq(const struct sock *sk) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci const struct tcp_sock *tp = tcp_sk(sk); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci if (!before(tcp_wnd_end(tp), tp->snd_nxt) || 1028c2ecf20Sopenharmony_ci (tp->rx_opt.wscale_ok && 1038c2ecf20Sopenharmony_ci ((tp->snd_nxt - tcp_wnd_end(tp)) < (1 << tp->rx_opt.rcv_wscale)))) 1048c2ecf20Sopenharmony_ci return tp->snd_nxt; 1058c2ecf20Sopenharmony_ci else 1068c2ecf20Sopenharmony_ci return tcp_wnd_end(tp); 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci/* Calculate mss to advertise in SYN segment. 1108c2ecf20Sopenharmony_ci * RFC1122, RFC1063, draft-ietf-tcpimpl-pmtud-01 state that: 1118c2ecf20Sopenharmony_ci * 1128c2ecf20Sopenharmony_ci * 1. It is independent of path mtu. 1138c2ecf20Sopenharmony_ci * 2. Ideally, it is maximal possible segment size i.e. 65535-40. 1148c2ecf20Sopenharmony_ci * 3. For IPv4 it is reasonable to calculate it from maximal MTU of 1158c2ecf20Sopenharmony_ci * attached devices, because some buggy hosts are confused by 1168c2ecf20Sopenharmony_ci * large MSS. 1178c2ecf20Sopenharmony_ci * 4. We do not make 3, we advertise MSS, calculated from first 1188c2ecf20Sopenharmony_ci * hop device mtu, but allow to raise it to ip_rt_min_advmss. 1198c2ecf20Sopenharmony_ci * This may be overridden via information stored in routing table. 1208c2ecf20Sopenharmony_ci * 5. Value 65535 for MSS is valid in IPv6 and means "as large as possible, 1218c2ecf20Sopenharmony_ci * probably even Jumbo". 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_cistatic __u16 tcp_advertise_mss(struct sock *sk) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 1268c2ecf20Sopenharmony_ci const struct dst_entry *dst = __sk_dst_get(sk); 1278c2ecf20Sopenharmony_ci int mss = tp->advmss; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci if (dst) { 1308c2ecf20Sopenharmony_ci unsigned int metric = dst_metric_advmss(dst); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci if (metric < mss) { 1338c2ecf20Sopenharmony_ci mss = metric; 1348c2ecf20Sopenharmony_ci tp->advmss = mss; 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci } 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci return (__u16)mss; 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci/* RFC2861. Reset CWND after idle period longer RTO to "restart window". 1428c2ecf20Sopenharmony_ci * This is the first part of cwnd validation mechanism. 1438c2ecf20Sopenharmony_ci */ 1448c2ecf20Sopenharmony_civoid tcp_cwnd_restart(struct sock *sk, s32 delta) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 1478c2ecf20Sopenharmony_ci u32 restart_cwnd = tcp_init_cwnd(tp, __sk_dst_get(sk)); 1488c2ecf20Sopenharmony_ci u32 cwnd = tp->snd_cwnd; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci tcp_ca_event(sk, CA_EVENT_CWND_RESTART); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci tp->snd_ssthresh = tcp_current_ssthresh(sk); 1538c2ecf20Sopenharmony_ci restart_cwnd = min(restart_cwnd, cwnd); 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci while ((delta -= inet_csk(sk)->icsk_rto) > 0 && cwnd > restart_cwnd) 1568c2ecf20Sopenharmony_ci cwnd >>= 1; 1578c2ecf20Sopenharmony_ci tp->snd_cwnd = max(cwnd, restart_cwnd); 1588c2ecf20Sopenharmony_ci tp->snd_cwnd_stamp = tcp_jiffies32; 1598c2ecf20Sopenharmony_ci tp->snd_cwnd_used = 0; 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci/* Congestion state accounting after a packet has been sent. */ 1638c2ecf20Sopenharmony_cistatic void tcp_event_data_sent(struct tcp_sock *tp, 1648c2ecf20Sopenharmony_ci struct sock *sk) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 1678c2ecf20Sopenharmony_ci const u32 now = tcp_jiffies32; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci if (tcp_packets_in_flight(tp) == 0) 1708c2ecf20Sopenharmony_ci tcp_ca_event(sk, CA_EVENT_TX_START); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci tp->lsndtime = now; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci /* If it is a reply for ato after last received 1758c2ecf20Sopenharmony_ci * packet, enter pingpong mode. 1768c2ecf20Sopenharmony_ci */ 1778c2ecf20Sopenharmony_ci if ((u32)(now - icsk->icsk_ack.lrcvtime) < icsk->icsk_ack.ato) 1788c2ecf20Sopenharmony_ci inet_csk_enter_pingpong_mode(sk); 1798c2ecf20Sopenharmony_ci} 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci/* Account for an ACK we sent. */ 1828c2ecf20Sopenharmony_cistatic inline void tcp_event_ack_sent(struct sock *sk, u32 rcv_nxt) 1838c2ecf20Sopenharmony_ci{ 1848c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci if (unlikely(tp->compressed_ack)) { 1878c2ecf20Sopenharmony_ci NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPACKCOMPRESSED, 1888c2ecf20Sopenharmony_ci tp->compressed_ack); 1898c2ecf20Sopenharmony_ci tp->compressed_ack = 0; 1908c2ecf20Sopenharmony_ci if (hrtimer_try_to_cancel(&tp->compressed_ack_timer) == 1) 1918c2ecf20Sopenharmony_ci __sock_put(sk); 1928c2ecf20Sopenharmony_ci } 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci if (unlikely(rcv_nxt != tp->rcv_nxt)) 1958c2ecf20Sopenharmony_ci return; /* Special ACK sent by DCTCP to reflect ECN */ 1968c2ecf20Sopenharmony_ci tcp_dec_quickack_mode(sk); 1978c2ecf20Sopenharmony_ci inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK); 1988c2ecf20Sopenharmony_ci} 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci/* Determine a window scaling and initial window to offer. 2018c2ecf20Sopenharmony_ci * Based on the assumption that the given amount of space 2028c2ecf20Sopenharmony_ci * will be offered. Store the results in the tp structure. 2038c2ecf20Sopenharmony_ci * NOTE: for smooth operation initial space offering should 2048c2ecf20Sopenharmony_ci * be a multiple of mss if possible. We assume here that mss >= 1. 2058c2ecf20Sopenharmony_ci * This MUST be enforced by all callers. 2068c2ecf20Sopenharmony_ci */ 2078c2ecf20Sopenharmony_civoid tcp_select_initial_window(const struct sock *sk, int __space, __u32 mss, 2088c2ecf20Sopenharmony_ci __u32 *rcv_wnd, __u32 *window_clamp, 2098c2ecf20Sopenharmony_ci int wscale_ok, __u8 *rcv_wscale, 2108c2ecf20Sopenharmony_ci __u32 init_rcv_wnd) 2118c2ecf20Sopenharmony_ci{ 2128c2ecf20Sopenharmony_ci unsigned int space = (__space < 0 ? 0 : __space); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci /* If no clamp set the clamp to the max possible scaled window */ 2158c2ecf20Sopenharmony_ci if (*window_clamp == 0) 2168c2ecf20Sopenharmony_ci (*window_clamp) = (U16_MAX << TCP_MAX_WSCALE); 2178c2ecf20Sopenharmony_ci space = min(*window_clamp, space); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci /* Quantize space offering to a multiple of mss if possible. */ 2208c2ecf20Sopenharmony_ci if (space > mss) 2218c2ecf20Sopenharmony_ci space = rounddown(space, mss); 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci /* NOTE: offering an initial window larger than 32767 2248c2ecf20Sopenharmony_ci * will break some buggy TCP stacks. If the admin tells us 2258c2ecf20Sopenharmony_ci * it is likely we could be speaking with such a buggy stack 2268c2ecf20Sopenharmony_ci * we will truncate our initial window offering to 32K-1 2278c2ecf20Sopenharmony_ci * unless the remote has sent us a window scaling option, 2288c2ecf20Sopenharmony_ci * which we interpret as a sign the remote TCP is not 2298c2ecf20Sopenharmony_ci * misinterpreting the window field as a signed quantity. 2308c2ecf20Sopenharmony_ci */ 2318c2ecf20Sopenharmony_ci if (sock_net(sk)->ipv4.sysctl_tcp_workaround_signed_windows) 2328c2ecf20Sopenharmony_ci (*rcv_wnd) = min(space, MAX_TCP_WINDOW); 2338c2ecf20Sopenharmony_ci else 2348c2ecf20Sopenharmony_ci (*rcv_wnd) = min_t(u32, space, U16_MAX); 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci if (init_rcv_wnd) 2378c2ecf20Sopenharmony_ci *rcv_wnd = min(*rcv_wnd, init_rcv_wnd * mss); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci *rcv_wscale = 0; 2408c2ecf20Sopenharmony_ci if (wscale_ok) { 2418c2ecf20Sopenharmony_ci /* Set window scaling on max possible window */ 2428c2ecf20Sopenharmony_ci space = max_t(u32, space, READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_rmem[2])); 2438c2ecf20Sopenharmony_ci space = max_t(u32, space, READ_ONCE(sysctl_rmem_max)); 2448c2ecf20Sopenharmony_ci space = min_t(u32, space, *window_clamp); 2458c2ecf20Sopenharmony_ci *rcv_wscale = clamp_t(int, ilog2(space) - 15, 2468c2ecf20Sopenharmony_ci 0, TCP_MAX_WSCALE); 2478c2ecf20Sopenharmony_ci } 2488c2ecf20Sopenharmony_ci /* Set the clamp no higher than max representable value */ 2498c2ecf20Sopenharmony_ci (*window_clamp) = min_t(__u32, U16_MAX << (*rcv_wscale), *window_clamp); 2508c2ecf20Sopenharmony_ci} 2518c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tcp_select_initial_window); 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci/* Chose a new window to advertise, update state in tcp_sock for the 2548c2ecf20Sopenharmony_ci * socket, and return result with RFC1323 scaling applied. The return 2558c2ecf20Sopenharmony_ci * value can be stuffed directly into th->window for an outgoing 2568c2ecf20Sopenharmony_ci * frame. 2578c2ecf20Sopenharmony_ci */ 2588c2ecf20Sopenharmony_cistatic u16 tcp_select_window(struct sock *sk) 2598c2ecf20Sopenharmony_ci{ 2608c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 2618c2ecf20Sopenharmony_ci u32 old_win = tp->rcv_wnd; 2628c2ecf20Sopenharmony_ci u32 cur_win = tcp_receive_window(tp); 2638c2ecf20Sopenharmony_ci u32 new_win = __tcp_select_window(sk); 2648c2ecf20Sopenharmony_ci 2658c2ecf20Sopenharmony_ci /* Never shrink the offered window */ 2668c2ecf20Sopenharmony_ci if (new_win < cur_win) { 2678c2ecf20Sopenharmony_ci /* Danger Will Robinson! 2688c2ecf20Sopenharmony_ci * Don't update rcv_wup/rcv_wnd here or else 2698c2ecf20Sopenharmony_ci * we will not be able to advertise a zero 2708c2ecf20Sopenharmony_ci * window in time. --DaveM 2718c2ecf20Sopenharmony_ci * 2728c2ecf20Sopenharmony_ci * Relax Will Robinson. 2738c2ecf20Sopenharmony_ci */ 2748c2ecf20Sopenharmony_ci if (new_win == 0) 2758c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), 2768c2ecf20Sopenharmony_ci LINUX_MIB_TCPWANTZEROWINDOWADV); 2778c2ecf20Sopenharmony_ci new_win = ALIGN(cur_win, 1 << tp->rx_opt.rcv_wscale); 2788c2ecf20Sopenharmony_ci } 2798c2ecf20Sopenharmony_ci tp->rcv_wnd = new_win; 2808c2ecf20Sopenharmony_ci tp->rcv_wup = tp->rcv_nxt; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci /* Make sure we do not exceed the maximum possible 2838c2ecf20Sopenharmony_ci * scaled window. 2848c2ecf20Sopenharmony_ci */ 2858c2ecf20Sopenharmony_ci if (!tp->rx_opt.rcv_wscale && 2868c2ecf20Sopenharmony_ci sock_net(sk)->ipv4.sysctl_tcp_workaround_signed_windows) 2878c2ecf20Sopenharmony_ci new_win = min(new_win, MAX_TCP_WINDOW); 2888c2ecf20Sopenharmony_ci else 2898c2ecf20Sopenharmony_ci new_win = min(new_win, (65535U << tp->rx_opt.rcv_wscale)); 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci /* RFC1323 scaling applied */ 2928c2ecf20Sopenharmony_ci new_win >>= tp->rx_opt.rcv_wscale; 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci /* If we advertise zero window, disable fast path. */ 2958c2ecf20Sopenharmony_ci if (new_win == 0) { 2968c2ecf20Sopenharmony_ci tp->pred_flags = 0; 2978c2ecf20Sopenharmony_ci if (old_win) 2988c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), 2998c2ecf20Sopenharmony_ci LINUX_MIB_TCPTOZEROWINDOWADV); 3008c2ecf20Sopenharmony_ci } else if (old_win == 0) { 3018c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPFROMZEROWINDOWADV); 3028c2ecf20Sopenharmony_ci } 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci return new_win; 3058c2ecf20Sopenharmony_ci} 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci/* Packet ECN state for a SYN-ACK */ 3088c2ecf20Sopenharmony_cistatic void tcp_ecn_send_synack(struct sock *sk, struct sk_buff *skb) 3098c2ecf20Sopenharmony_ci{ 3108c2ecf20Sopenharmony_ci const struct tcp_sock *tp = tcp_sk(sk); 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_CWR; 3138c2ecf20Sopenharmony_ci if (!(tp->ecn_flags & TCP_ECN_OK)) 3148c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_ECE; 3158c2ecf20Sopenharmony_ci else if (tcp_ca_needs_ecn(sk) || 3168c2ecf20Sopenharmony_ci tcp_bpf_ca_needs_ecn(sk)) 3178c2ecf20Sopenharmony_ci INET_ECN_xmit(sk); 3188c2ecf20Sopenharmony_ci} 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci/* Packet ECN state for a SYN. */ 3218c2ecf20Sopenharmony_cistatic void tcp_ecn_send_syn(struct sock *sk, struct sk_buff *skb) 3228c2ecf20Sopenharmony_ci{ 3238c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 3248c2ecf20Sopenharmony_ci bool bpf_needs_ecn = tcp_bpf_ca_needs_ecn(sk); 3258c2ecf20Sopenharmony_ci bool use_ecn = sock_net(sk)->ipv4.sysctl_tcp_ecn == 1 || 3268c2ecf20Sopenharmony_ci tcp_ca_needs_ecn(sk) || bpf_needs_ecn; 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci if (!use_ecn) { 3298c2ecf20Sopenharmony_ci const struct dst_entry *dst = __sk_dst_get(sk); 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci if (dst && dst_feature(dst, RTAX_FEATURE_ECN)) 3328c2ecf20Sopenharmony_ci use_ecn = true; 3338c2ecf20Sopenharmony_ci } 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ci tp->ecn_flags = 0; 3368c2ecf20Sopenharmony_ci 3378c2ecf20Sopenharmony_ci if (use_ecn) { 3388c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ECE | TCPHDR_CWR; 3398c2ecf20Sopenharmony_ci tp->ecn_flags = TCP_ECN_OK; 3408c2ecf20Sopenharmony_ci if (tcp_ca_needs_ecn(sk) || bpf_needs_ecn) 3418c2ecf20Sopenharmony_ci INET_ECN_xmit(sk); 3428c2ecf20Sopenharmony_ci } 3438c2ecf20Sopenharmony_ci} 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_cistatic void tcp_ecn_clear_syn(struct sock *sk, struct sk_buff *skb) 3468c2ecf20Sopenharmony_ci{ 3478c2ecf20Sopenharmony_ci if (sock_net(sk)->ipv4.sysctl_tcp_ecn_fallback) 3488c2ecf20Sopenharmony_ci /* tp->ecn_flags are cleared at a later point in time when 3498c2ecf20Sopenharmony_ci * SYN ACK is ultimatively being received. 3508c2ecf20Sopenharmony_ci */ 3518c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags &= ~(TCPHDR_ECE | TCPHDR_CWR); 3528c2ecf20Sopenharmony_ci} 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_cistatic void 3558c2ecf20Sopenharmony_citcp_ecn_make_synack(const struct request_sock *req, struct tcphdr *th) 3568c2ecf20Sopenharmony_ci{ 3578c2ecf20Sopenharmony_ci if (inet_rsk(req)->ecn_ok) 3588c2ecf20Sopenharmony_ci th->ece = 1; 3598c2ecf20Sopenharmony_ci} 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci/* Set up ECN state for a packet on a ESTABLISHED socket that is about to 3628c2ecf20Sopenharmony_ci * be sent. 3638c2ecf20Sopenharmony_ci */ 3648c2ecf20Sopenharmony_cistatic void tcp_ecn_send(struct sock *sk, struct sk_buff *skb, 3658c2ecf20Sopenharmony_ci struct tcphdr *th, int tcp_header_len) 3668c2ecf20Sopenharmony_ci{ 3678c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci if (tp->ecn_flags & TCP_ECN_OK) { 3708c2ecf20Sopenharmony_ci /* Not-retransmitted data segment: set ECT and inject CWR. */ 3718c2ecf20Sopenharmony_ci if (skb->len != tcp_header_len && 3728c2ecf20Sopenharmony_ci !before(TCP_SKB_CB(skb)->seq, tp->snd_nxt)) { 3738c2ecf20Sopenharmony_ci INET_ECN_xmit(sk); 3748c2ecf20Sopenharmony_ci if (tp->ecn_flags & TCP_ECN_QUEUE_CWR) { 3758c2ecf20Sopenharmony_ci tp->ecn_flags &= ~TCP_ECN_QUEUE_CWR; 3768c2ecf20Sopenharmony_ci th->cwr = 1; 3778c2ecf20Sopenharmony_ci skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN; 3788c2ecf20Sopenharmony_ci } 3798c2ecf20Sopenharmony_ci } else if (!tcp_ca_needs_ecn(sk)) { 3808c2ecf20Sopenharmony_ci /* ACK or retransmitted segment: clear ECT|CE */ 3818c2ecf20Sopenharmony_ci INET_ECN_dontxmit(sk); 3828c2ecf20Sopenharmony_ci } 3838c2ecf20Sopenharmony_ci if (tp->ecn_flags & TCP_ECN_DEMAND_CWR) 3848c2ecf20Sopenharmony_ci th->ece = 1; 3858c2ecf20Sopenharmony_ci } 3868c2ecf20Sopenharmony_ci} 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci/* Constructs common control bits of non-data skb. If SYN/FIN is present, 3898c2ecf20Sopenharmony_ci * auto increment end seqno. 3908c2ecf20Sopenharmony_ci */ 3918c2ecf20Sopenharmony_cistatic void tcp_init_nondata_skb(struct sk_buff *skb, u32 seq, u8 flags) 3928c2ecf20Sopenharmony_ci{ 3938c2ecf20Sopenharmony_ci skb->ip_summed = CHECKSUM_PARTIAL; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags = flags; 3968c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->sacked = 0; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci tcp_skb_pcount_set(skb, 1); 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->seq = seq; 4018c2ecf20Sopenharmony_ci if (flags & (TCPHDR_SYN | TCPHDR_FIN)) 4028c2ecf20Sopenharmony_ci seq++; 4038c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->end_seq = seq; 4048c2ecf20Sopenharmony_ci} 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_cistatic inline bool tcp_urg_mode(const struct tcp_sock *tp) 4078c2ecf20Sopenharmony_ci{ 4088c2ecf20Sopenharmony_ci return tp->snd_una != tp->snd_up; 4098c2ecf20Sopenharmony_ci} 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci#define OPTION_SACK_ADVERTISE (1 << 0) 4128c2ecf20Sopenharmony_ci#define OPTION_TS (1 << 1) 4138c2ecf20Sopenharmony_ci#define OPTION_MD5 (1 << 2) 4148c2ecf20Sopenharmony_ci#define OPTION_WSCALE (1 << 3) 4158c2ecf20Sopenharmony_ci#define OPTION_FAST_OPEN_COOKIE (1 << 8) 4168c2ecf20Sopenharmony_ci#define OPTION_SMC (1 << 9) 4178c2ecf20Sopenharmony_ci#define OPTION_MPTCP (1 << 10) 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_cistatic void smc_options_write(__be32 *ptr, u16 *options) 4208c2ecf20Sopenharmony_ci{ 4218c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_SMC) 4228c2ecf20Sopenharmony_ci if (static_branch_unlikely(&tcp_have_smc)) { 4238c2ecf20Sopenharmony_ci if (unlikely(OPTION_SMC & *options)) { 4248c2ecf20Sopenharmony_ci *ptr++ = htonl((TCPOPT_NOP << 24) | 4258c2ecf20Sopenharmony_ci (TCPOPT_NOP << 16) | 4268c2ecf20Sopenharmony_ci (TCPOPT_EXP << 8) | 4278c2ecf20Sopenharmony_ci (TCPOLEN_EXP_SMC_BASE)); 4288c2ecf20Sopenharmony_ci *ptr++ = htonl(TCPOPT_SMC_MAGIC); 4298c2ecf20Sopenharmony_ci } 4308c2ecf20Sopenharmony_ci } 4318c2ecf20Sopenharmony_ci#endif 4328c2ecf20Sopenharmony_ci} 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_cistruct tcp_out_options { 4358c2ecf20Sopenharmony_ci u16 options; /* bit field of OPTION_* */ 4368c2ecf20Sopenharmony_ci u16 mss; /* 0 to disable */ 4378c2ecf20Sopenharmony_ci u8 ws; /* window scale, 0 to disable */ 4388c2ecf20Sopenharmony_ci u8 num_sack_blocks; /* number of SACK blocks to include */ 4398c2ecf20Sopenharmony_ci u8 hash_size; /* bytes in hash_location */ 4408c2ecf20Sopenharmony_ci u8 bpf_opt_len; /* length of BPF hdr option */ 4418c2ecf20Sopenharmony_ci __u8 *hash_location; /* temporary pointer, overloaded */ 4428c2ecf20Sopenharmony_ci __u32 tsval, tsecr; /* need to include OPTION_TS */ 4438c2ecf20Sopenharmony_ci struct tcp_fastopen_cookie *fastopen_cookie; /* Fast open cookie */ 4448c2ecf20Sopenharmony_ci struct mptcp_out_options mptcp; 4458c2ecf20Sopenharmony_ci}; 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_cistatic void mptcp_options_write(__be32 *ptr, struct tcp_out_options *opts) 4488c2ecf20Sopenharmony_ci{ 4498c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_MPTCP) 4508c2ecf20Sopenharmony_ci if (unlikely(OPTION_MPTCP & opts->options)) 4518c2ecf20Sopenharmony_ci mptcp_write_options(ptr, &opts->mptcp); 4528c2ecf20Sopenharmony_ci#endif 4538c2ecf20Sopenharmony_ci} 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci#ifdef CONFIG_CGROUP_BPF 4568c2ecf20Sopenharmony_cistatic int bpf_skops_write_hdr_opt_arg0(struct sk_buff *skb, 4578c2ecf20Sopenharmony_ci enum tcp_synack_type synack_type) 4588c2ecf20Sopenharmony_ci{ 4598c2ecf20Sopenharmony_ci if (unlikely(!skb)) 4608c2ecf20Sopenharmony_ci return BPF_WRITE_HDR_TCP_CURRENT_MSS; 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci if (unlikely(synack_type == TCP_SYNACK_COOKIE)) 4638c2ecf20Sopenharmony_ci return BPF_WRITE_HDR_TCP_SYNACK_COOKIE; 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci return 0; 4668c2ecf20Sopenharmony_ci} 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci/* req, syn_skb and synack_type are used when writing synack */ 4698c2ecf20Sopenharmony_cistatic void bpf_skops_hdr_opt_len(struct sock *sk, struct sk_buff *skb, 4708c2ecf20Sopenharmony_ci struct request_sock *req, 4718c2ecf20Sopenharmony_ci struct sk_buff *syn_skb, 4728c2ecf20Sopenharmony_ci enum tcp_synack_type synack_type, 4738c2ecf20Sopenharmony_ci struct tcp_out_options *opts, 4748c2ecf20Sopenharmony_ci unsigned int *remaining) 4758c2ecf20Sopenharmony_ci{ 4768c2ecf20Sopenharmony_ci struct bpf_sock_ops_kern sock_ops; 4778c2ecf20Sopenharmony_ci int err; 4788c2ecf20Sopenharmony_ci 4798c2ecf20Sopenharmony_ci if (likely(!BPF_SOCK_OPS_TEST_FLAG(tcp_sk(sk), 4808c2ecf20Sopenharmony_ci BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG)) || 4818c2ecf20Sopenharmony_ci !*remaining) 4828c2ecf20Sopenharmony_ci return; 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_ci /* *remaining has already been aligned to 4 bytes, so *remaining >= 4 */ 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci /* init sock_ops */ 4878c2ecf20Sopenharmony_ci memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci sock_ops.op = BPF_SOCK_OPS_HDR_OPT_LEN_CB; 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci if (req) { 4928c2ecf20Sopenharmony_ci /* The listen "sk" cannot be passed here because 4938c2ecf20Sopenharmony_ci * it is not locked. It would not make too much 4948c2ecf20Sopenharmony_ci * sense to do bpf_setsockopt(listen_sk) based 4958c2ecf20Sopenharmony_ci * on individual connection request also. 4968c2ecf20Sopenharmony_ci * 4978c2ecf20Sopenharmony_ci * Thus, "req" is passed here and the cgroup-bpf-progs 4988c2ecf20Sopenharmony_ci * of the listen "sk" will be run. 4998c2ecf20Sopenharmony_ci * 5008c2ecf20Sopenharmony_ci * "req" is also used here for fastopen even the "sk" here is 5018c2ecf20Sopenharmony_ci * a fullsock "child" sk. It is to keep the behavior 5028c2ecf20Sopenharmony_ci * consistent between fastopen and non-fastopen on 5038c2ecf20Sopenharmony_ci * the bpf programming side. 5048c2ecf20Sopenharmony_ci */ 5058c2ecf20Sopenharmony_ci sock_ops.sk = (struct sock *)req; 5068c2ecf20Sopenharmony_ci sock_ops.syn_skb = syn_skb; 5078c2ecf20Sopenharmony_ci } else { 5088c2ecf20Sopenharmony_ci sock_owned_by_me(sk); 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci sock_ops.is_fullsock = 1; 5118c2ecf20Sopenharmony_ci sock_ops.sk = sk; 5128c2ecf20Sopenharmony_ci } 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ci sock_ops.args[0] = bpf_skops_write_hdr_opt_arg0(skb, synack_type); 5158c2ecf20Sopenharmony_ci sock_ops.remaining_opt_len = *remaining; 5168c2ecf20Sopenharmony_ci /* tcp_current_mss() does not pass a skb */ 5178c2ecf20Sopenharmony_ci if (skb) 5188c2ecf20Sopenharmony_ci bpf_skops_init_skb(&sock_ops, skb, 0); 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_ci err = BPF_CGROUP_RUN_PROG_SOCK_OPS_SK(&sock_ops, sk); 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci if (err || sock_ops.remaining_opt_len == *remaining) 5238c2ecf20Sopenharmony_ci return; 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci opts->bpf_opt_len = *remaining - sock_ops.remaining_opt_len; 5268c2ecf20Sopenharmony_ci /* round up to 4 bytes */ 5278c2ecf20Sopenharmony_ci opts->bpf_opt_len = (opts->bpf_opt_len + 3) & ~3; 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci *remaining -= opts->bpf_opt_len; 5308c2ecf20Sopenharmony_ci} 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_cistatic void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb, 5338c2ecf20Sopenharmony_ci struct request_sock *req, 5348c2ecf20Sopenharmony_ci struct sk_buff *syn_skb, 5358c2ecf20Sopenharmony_ci enum tcp_synack_type synack_type, 5368c2ecf20Sopenharmony_ci struct tcp_out_options *opts) 5378c2ecf20Sopenharmony_ci{ 5388c2ecf20Sopenharmony_ci u8 first_opt_off, nr_written, max_opt_len = opts->bpf_opt_len; 5398c2ecf20Sopenharmony_ci struct bpf_sock_ops_kern sock_ops; 5408c2ecf20Sopenharmony_ci int err; 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci if (likely(!max_opt_len)) 5438c2ecf20Sopenharmony_ci return; 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci memset(&sock_ops, 0, offsetof(struct bpf_sock_ops_kern, temp)); 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci sock_ops.op = BPF_SOCK_OPS_WRITE_HDR_OPT_CB; 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci if (req) { 5508c2ecf20Sopenharmony_ci sock_ops.sk = (struct sock *)req; 5518c2ecf20Sopenharmony_ci sock_ops.syn_skb = syn_skb; 5528c2ecf20Sopenharmony_ci } else { 5538c2ecf20Sopenharmony_ci sock_owned_by_me(sk); 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ci sock_ops.is_fullsock = 1; 5568c2ecf20Sopenharmony_ci sock_ops.sk = sk; 5578c2ecf20Sopenharmony_ci } 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci sock_ops.args[0] = bpf_skops_write_hdr_opt_arg0(skb, synack_type); 5608c2ecf20Sopenharmony_ci sock_ops.remaining_opt_len = max_opt_len; 5618c2ecf20Sopenharmony_ci first_opt_off = tcp_hdrlen(skb) - max_opt_len; 5628c2ecf20Sopenharmony_ci bpf_skops_init_skb(&sock_ops, skb, first_opt_off); 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci err = BPF_CGROUP_RUN_PROG_SOCK_OPS_SK(&sock_ops, sk); 5658c2ecf20Sopenharmony_ci 5668c2ecf20Sopenharmony_ci if (err) 5678c2ecf20Sopenharmony_ci nr_written = 0; 5688c2ecf20Sopenharmony_ci else 5698c2ecf20Sopenharmony_ci nr_written = max_opt_len - sock_ops.remaining_opt_len; 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci if (nr_written < max_opt_len) 5728c2ecf20Sopenharmony_ci memset(skb->data + first_opt_off + nr_written, TCPOPT_NOP, 5738c2ecf20Sopenharmony_ci max_opt_len - nr_written); 5748c2ecf20Sopenharmony_ci} 5758c2ecf20Sopenharmony_ci#else 5768c2ecf20Sopenharmony_cistatic void bpf_skops_hdr_opt_len(struct sock *sk, struct sk_buff *skb, 5778c2ecf20Sopenharmony_ci struct request_sock *req, 5788c2ecf20Sopenharmony_ci struct sk_buff *syn_skb, 5798c2ecf20Sopenharmony_ci enum tcp_synack_type synack_type, 5808c2ecf20Sopenharmony_ci struct tcp_out_options *opts, 5818c2ecf20Sopenharmony_ci unsigned int *remaining) 5828c2ecf20Sopenharmony_ci{ 5838c2ecf20Sopenharmony_ci} 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_cistatic void bpf_skops_write_hdr_opt(struct sock *sk, struct sk_buff *skb, 5868c2ecf20Sopenharmony_ci struct request_sock *req, 5878c2ecf20Sopenharmony_ci struct sk_buff *syn_skb, 5888c2ecf20Sopenharmony_ci enum tcp_synack_type synack_type, 5898c2ecf20Sopenharmony_ci struct tcp_out_options *opts) 5908c2ecf20Sopenharmony_ci{ 5918c2ecf20Sopenharmony_ci} 5928c2ecf20Sopenharmony_ci#endif 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci/* Write previously computed TCP options to the packet. 5958c2ecf20Sopenharmony_ci * 5968c2ecf20Sopenharmony_ci * Beware: Something in the Internet is very sensitive to the ordering of 5978c2ecf20Sopenharmony_ci * TCP options, we learned this through the hard way, so be careful here. 5988c2ecf20Sopenharmony_ci * Luckily we can at least blame others for their non-compliance but from 5998c2ecf20Sopenharmony_ci * inter-operability perspective it seems that we're somewhat stuck with 6008c2ecf20Sopenharmony_ci * the ordering which we have been using if we want to keep working with 6018c2ecf20Sopenharmony_ci * those broken things (not that it currently hurts anybody as there isn't 6028c2ecf20Sopenharmony_ci * particular reason why the ordering would need to be changed). 6038c2ecf20Sopenharmony_ci * 6048c2ecf20Sopenharmony_ci * At least SACK_PERM as the first option is known to lead to a disaster 6058c2ecf20Sopenharmony_ci * (but it may well be that other scenarios fail similarly). 6068c2ecf20Sopenharmony_ci */ 6078c2ecf20Sopenharmony_cistatic void tcp_options_write(__be32 *ptr, struct tcp_sock *tp, 6088c2ecf20Sopenharmony_ci struct tcp_out_options *opts) 6098c2ecf20Sopenharmony_ci{ 6108c2ecf20Sopenharmony_ci u16 options = opts->options; /* mungable copy */ 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci if (unlikely(OPTION_MD5 & options)) { 6138c2ecf20Sopenharmony_ci *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | 6148c2ecf20Sopenharmony_ci (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG); 6158c2ecf20Sopenharmony_ci /* overload cookie hash location */ 6168c2ecf20Sopenharmony_ci opts->hash_location = (__u8 *)ptr; 6178c2ecf20Sopenharmony_ci ptr += 4; 6188c2ecf20Sopenharmony_ci } 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_ci if (unlikely(opts->mss)) { 6218c2ecf20Sopenharmony_ci *ptr++ = htonl((TCPOPT_MSS << 24) | 6228c2ecf20Sopenharmony_ci (TCPOLEN_MSS << 16) | 6238c2ecf20Sopenharmony_ci opts->mss); 6248c2ecf20Sopenharmony_ci } 6258c2ecf20Sopenharmony_ci 6268c2ecf20Sopenharmony_ci if (likely(OPTION_TS & options)) { 6278c2ecf20Sopenharmony_ci if (unlikely(OPTION_SACK_ADVERTISE & options)) { 6288c2ecf20Sopenharmony_ci *ptr++ = htonl((TCPOPT_SACK_PERM << 24) | 6298c2ecf20Sopenharmony_ci (TCPOLEN_SACK_PERM << 16) | 6308c2ecf20Sopenharmony_ci (TCPOPT_TIMESTAMP << 8) | 6318c2ecf20Sopenharmony_ci TCPOLEN_TIMESTAMP); 6328c2ecf20Sopenharmony_ci options &= ~OPTION_SACK_ADVERTISE; 6338c2ecf20Sopenharmony_ci } else { 6348c2ecf20Sopenharmony_ci *ptr++ = htonl((TCPOPT_NOP << 24) | 6358c2ecf20Sopenharmony_ci (TCPOPT_NOP << 16) | 6368c2ecf20Sopenharmony_ci (TCPOPT_TIMESTAMP << 8) | 6378c2ecf20Sopenharmony_ci TCPOLEN_TIMESTAMP); 6388c2ecf20Sopenharmony_ci } 6398c2ecf20Sopenharmony_ci *ptr++ = htonl(opts->tsval); 6408c2ecf20Sopenharmony_ci *ptr++ = htonl(opts->tsecr); 6418c2ecf20Sopenharmony_ci } 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci if (unlikely(OPTION_SACK_ADVERTISE & options)) { 6448c2ecf20Sopenharmony_ci *ptr++ = htonl((TCPOPT_NOP << 24) | 6458c2ecf20Sopenharmony_ci (TCPOPT_NOP << 16) | 6468c2ecf20Sopenharmony_ci (TCPOPT_SACK_PERM << 8) | 6478c2ecf20Sopenharmony_ci TCPOLEN_SACK_PERM); 6488c2ecf20Sopenharmony_ci } 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci if (unlikely(OPTION_WSCALE & options)) { 6518c2ecf20Sopenharmony_ci *ptr++ = htonl((TCPOPT_NOP << 24) | 6528c2ecf20Sopenharmony_ci (TCPOPT_WINDOW << 16) | 6538c2ecf20Sopenharmony_ci (TCPOLEN_WINDOW << 8) | 6548c2ecf20Sopenharmony_ci opts->ws); 6558c2ecf20Sopenharmony_ci } 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci if (unlikely(opts->num_sack_blocks)) { 6588c2ecf20Sopenharmony_ci struct tcp_sack_block *sp = tp->rx_opt.dsack ? 6598c2ecf20Sopenharmony_ci tp->duplicate_sack : tp->selective_acks; 6608c2ecf20Sopenharmony_ci int this_sack; 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci *ptr++ = htonl((TCPOPT_NOP << 24) | 6638c2ecf20Sopenharmony_ci (TCPOPT_NOP << 16) | 6648c2ecf20Sopenharmony_ci (TCPOPT_SACK << 8) | 6658c2ecf20Sopenharmony_ci (TCPOLEN_SACK_BASE + (opts->num_sack_blocks * 6668c2ecf20Sopenharmony_ci TCPOLEN_SACK_PERBLOCK))); 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci for (this_sack = 0; this_sack < opts->num_sack_blocks; 6698c2ecf20Sopenharmony_ci ++this_sack) { 6708c2ecf20Sopenharmony_ci *ptr++ = htonl(sp[this_sack].start_seq); 6718c2ecf20Sopenharmony_ci *ptr++ = htonl(sp[this_sack].end_seq); 6728c2ecf20Sopenharmony_ci } 6738c2ecf20Sopenharmony_ci 6748c2ecf20Sopenharmony_ci tp->rx_opt.dsack = 0; 6758c2ecf20Sopenharmony_ci } 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci if (unlikely(OPTION_FAST_OPEN_COOKIE & options)) { 6788c2ecf20Sopenharmony_ci struct tcp_fastopen_cookie *foc = opts->fastopen_cookie; 6798c2ecf20Sopenharmony_ci u8 *p = (u8 *)ptr; 6808c2ecf20Sopenharmony_ci u32 len; /* Fast Open option length */ 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci if (foc->exp) { 6838c2ecf20Sopenharmony_ci len = TCPOLEN_EXP_FASTOPEN_BASE + foc->len; 6848c2ecf20Sopenharmony_ci *ptr = htonl((TCPOPT_EXP << 24) | (len << 16) | 6858c2ecf20Sopenharmony_ci TCPOPT_FASTOPEN_MAGIC); 6868c2ecf20Sopenharmony_ci p += TCPOLEN_EXP_FASTOPEN_BASE; 6878c2ecf20Sopenharmony_ci } else { 6888c2ecf20Sopenharmony_ci len = TCPOLEN_FASTOPEN_BASE + foc->len; 6898c2ecf20Sopenharmony_ci *p++ = TCPOPT_FASTOPEN; 6908c2ecf20Sopenharmony_ci *p++ = len; 6918c2ecf20Sopenharmony_ci } 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_ci memcpy(p, foc->val, foc->len); 6948c2ecf20Sopenharmony_ci if ((len & 3) == 2) { 6958c2ecf20Sopenharmony_ci p[foc->len] = TCPOPT_NOP; 6968c2ecf20Sopenharmony_ci p[foc->len + 1] = TCPOPT_NOP; 6978c2ecf20Sopenharmony_ci } 6988c2ecf20Sopenharmony_ci ptr += (len + 3) >> 2; 6998c2ecf20Sopenharmony_ci } 7008c2ecf20Sopenharmony_ci 7018c2ecf20Sopenharmony_ci smc_options_write(ptr, &options); 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci mptcp_options_write(ptr, opts); 7048c2ecf20Sopenharmony_ci} 7058c2ecf20Sopenharmony_ci 7068c2ecf20Sopenharmony_cistatic void smc_set_option(const struct tcp_sock *tp, 7078c2ecf20Sopenharmony_ci struct tcp_out_options *opts, 7088c2ecf20Sopenharmony_ci unsigned int *remaining) 7098c2ecf20Sopenharmony_ci{ 7108c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_SMC) 7118c2ecf20Sopenharmony_ci if (static_branch_unlikely(&tcp_have_smc)) { 7128c2ecf20Sopenharmony_ci if (tp->syn_smc) { 7138c2ecf20Sopenharmony_ci if (*remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) { 7148c2ecf20Sopenharmony_ci opts->options |= OPTION_SMC; 7158c2ecf20Sopenharmony_ci *remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED; 7168c2ecf20Sopenharmony_ci } 7178c2ecf20Sopenharmony_ci } 7188c2ecf20Sopenharmony_ci } 7198c2ecf20Sopenharmony_ci#endif 7208c2ecf20Sopenharmony_ci} 7218c2ecf20Sopenharmony_ci 7228c2ecf20Sopenharmony_cistatic void smc_set_option_cond(const struct tcp_sock *tp, 7238c2ecf20Sopenharmony_ci const struct inet_request_sock *ireq, 7248c2ecf20Sopenharmony_ci struct tcp_out_options *opts, 7258c2ecf20Sopenharmony_ci unsigned int *remaining) 7268c2ecf20Sopenharmony_ci{ 7278c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_SMC) 7288c2ecf20Sopenharmony_ci if (static_branch_unlikely(&tcp_have_smc)) { 7298c2ecf20Sopenharmony_ci if (tp->syn_smc && ireq->smc_ok) { 7308c2ecf20Sopenharmony_ci if (*remaining >= TCPOLEN_EXP_SMC_BASE_ALIGNED) { 7318c2ecf20Sopenharmony_ci opts->options |= OPTION_SMC; 7328c2ecf20Sopenharmony_ci *remaining -= TCPOLEN_EXP_SMC_BASE_ALIGNED; 7338c2ecf20Sopenharmony_ci } 7348c2ecf20Sopenharmony_ci } 7358c2ecf20Sopenharmony_ci } 7368c2ecf20Sopenharmony_ci#endif 7378c2ecf20Sopenharmony_ci} 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_cistatic void mptcp_set_option_cond(const struct request_sock *req, 7408c2ecf20Sopenharmony_ci struct tcp_out_options *opts, 7418c2ecf20Sopenharmony_ci unsigned int *remaining) 7428c2ecf20Sopenharmony_ci{ 7438c2ecf20Sopenharmony_ci if (rsk_is_mptcp(req)) { 7448c2ecf20Sopenharmony_ci unsigned int size; 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_ci if (mptcp_synack_options(req, &size, &opts->mptcp)) { 7478c2ecf20Sopenharmony_ci if (*remaining >= size) { 7488c2ecf20Sopenharmony_ci opts->options |= OPTION_MPTCP; 7498c2ecf20Sopenharmony_ci *remaining -= size; 7508c2ecf20Sopenharmony_ci } 7518c2ecf20Sopenharmony_ci } 7528c2ecf20Sopenharmony_ci } 7538c2ecf20Sopenharmony_ci} 7548c2ecf20Sopenharmony_ci 7558c2ecf20Sopenharmony_ci/* Compute TCP options for SYN packets. This is not the final 7568c2ecf20Sopenharmony_ci * network wire format yet. 7578c2ecf20Sopenharmony_ci */ 7588c2ecf20Sopenharmony_cistatic unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb, 7598c2ecf20Sopenharmony_ci struct tcp_out_options *opts, 7608c2ecf20Sopenharmony_ci struct tcp_md5sig_key **md5) 7618c2ecf20Sopenharmony_ci{ 7628c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 7638c2ecf20Sopenharmony_ci unsigned int remaining = MAX_TCP_OPTION_SPACE; 7648c2ecf20Sopenharmony_ci struct tcp_fastopen_request *fastopen = tp->fastopen_req; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci *md5 = NULL; 7678c2ecf20Sopenharmony_ci#ifdef CONFIG_TCP_MD5SIG 7688c2ecf20Sopenharmony_ci if (static_branch_unlikely(&tcp_md5_needed) && 7698c2ecf20Sopenharmony_ci rcu_access_pointer(tp->md5sig_info)) { 7708c2ecf20Sopenharmony_ci *md5 = tp->af_specific->md5_lookup(sk, sk); 7718c2ecf20Sopenharmony_ci if (*md5) { 7728c2ecf20Sopenharmony_ci opts->options |= OPTION_MD5; 7738c2ecf20Sopenharmony_ci remaining -= TCPOLEN_MD5SIG_ALIGNED; 7748c2ecf20Sopenharmony_ci } 7758c2ecf20Sopenharmony_ci } 7768c2ecf20Sopenharmony_ci#endif 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci /* We always get an MSS option. The option bytes which will be seen in 7798c2ecf20Sopenharmony_ci * normal data packets should timestamps be used, must be in the MSS 7808c2ecf20Sopenharmony_ci * advertised. But we subtract them from tp->mss_cache so that 7818c2ecf20Sopenharmony_ci * calculations in tcp_sendmsg are simpler etc. So account for this 7828c2ecf20Sopenharmony_ci * fact here if necessary. If we don't do this correctly, as a 7838c2ecf20Sopenharmony_ci * receiver we won't recognize data packets as being full sized when we 7848c2ecf20Sopenharmony_ci * should, and thus we won't abide by the delayed ACK rules correctly. 7858c2ecf20Sopenharmony_ci * SACKs don't matter, we never delay an ACK when we have any of those 7868c2ecf20Sopenharmony_ci * going out. */ 7878c2ecf20Sopenharmony_ci opts->mss = tcp_advertise_mss(sk); 7888c2ecf20Sopenharmony_ci remaining -= TCPOLEN_MSS_ALIGNED; 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci if (likely(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps) && !*md5)) { 7918c2ecf20Sopenharmony_ci opts->options |= OPTION_TS; 7928c2ecf20Sopenharmony_ci opts->tsval = tcp_skb_timestamp(skb) + tp->tsoffset; 7938c2ecf20Sopenharmony_ci opts->tsecr = tp->rx_opt.ts_recent; 7948c2ecf20Sopenharmony_ci remaining -= TCPOLEN_TSTAMP_ALIGNED; 7958c2ecf20Sopenharmony_ci } 7968c2ecf20Sopenharmony_ci if (likely(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_window_scaling))) { 7978c2ecf20Sopenharmony_ci opts->ws = tp->rx_opt.rcv_wscale; 7988c2ecf20Sopenharmony_ci opts->options |= OPTION_WSCALE; 7998c2ecf20Sopenharmony_ci remaining -= TCPOLEN_WSCALE_ALIGNED; 8008c2ecf20Sopenharmony_ci } 8018c2ecf20Sopenharmony_ci if (likely(READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_sack))) { 8028c2ecf20Sopenharmony_ci opts->options |= OPTION_SACK_ADVERTISE; 8038c2ecf20Sopenharmony_ci if (unlikely(!(OPTION_TS & opts->options))) 8048c2ecf20Sopenharmony_ci remaining -= TCPOLEN_SACKPERM_ALIGNED; 8058c2ecf20Sopenharmony_ci } 8068c2ecf20Sopenharmony_ci 8078c2ecf20Sopenharmony_ci if (fastopen && fastopen->cookie.len >= 0) { 8088c2ecf20Sopenharmony_ci u32 need = fastopen->cookie.len; 8098c2ecf20Sopenharmony_ci 8108c2ecf20Sopenharmony_ci need += fastopen->cookie.exp ? TCPOLEN_EXP_FASTOPEN_BASE : 8118c2ecf20Sopenharmony_ci TCPOLEN_FASTOPEN_BASE; 8128c2ecf20Sopenharmony_ci need = (need + 3) & ~3U; /* Align to 32 bits */ 8138c2ecf20Sopenharmony_ci if (remaining >= need) { 8148c2ecf20Sopenharmony_ci opts->options |= OPTION_FAST_OPEN_COOKIE; 8158c2ecf20Sopenharmony_ci opts->fastopen_cookie = &fastopen->cookie; 8168c2ecf20Sopenharmony_ci remaining -= need; 8178c2ecf20Sopenharmony_ci tp->syn_fastopen = 1; 8188c2ecf20Sopenharmony_ci tp->syn_fastopen_exp = fastopen->cookie.exp ? 1 : 0; 8198c2ecf20Sopenharmony_ci } 8208c2ecf20Sopenharmony_ci } 8218c2ecf20Sopenharmony_ci 8228c2ecf20Sopenharmony_ci smc_set_option(tp, opts, &remaining); 8238c2ecf20Sopenharmony_ci 8248c2ecf20Sopenharmony_ci if (sk_is_mptcp(sk)) { 8258c2ecf20Sopenharmony_ci unsigned int size; 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci if (mptcp_syn_options(sk, skb, &size, &opts->mptcp)) { 8288c2ecf20Sopenharmony_ci opts->options |= OPTION_MPTCP; 8298c2ecf20Sopenharmony_ci remaining -= size; 8308c2ecf20Sopenharmony_ci } 8318c2ecf20Sopenharmony_ci } 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_ci bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts, &remaining); 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_ci return MAX_TCP_OPTION_SPACE - remaining; 8368c2ecf20Sopenharmony_ci} 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ci/* Set up TCP options for SYN-ACKs. */ 8398c2ecf20Sopenharmony_cistatic unsigned int tcp_synack_options(const struct sock *sk, 8408c2ecf20Sopenharmony_ci struct request_sock *req, 8418c2ecf20Sopenharmony_ci unsigned int mss, struct sk_buff *skb, 8428c2ecf20Sopenharmony_ci struct tcp_out_options *opts, 8438c2ecf20Sopenharmony_ci const struct tcp_md5sig_key *md5, 8448c2ecf20Sopenharmony_ci struct tcp_fastopen_cookie *foc, 8458c2ecf20Sopenharmony_ci enum tcp_synack_type synack_type, 8468c2ecf20Sopenharmony_ci struct sk_buff *syn_skb) 8478c2ecf20Sopenharmony_ci{ 8488c2ecf20Sopenharmony_ci struct inet_request_sock *ireq = inet_rsk(req); 8498c2ecf20Sopenharmony_ci unsigned int remaining = MAX_TCP_OPTION_SPACE; 8508c2ecf20Sopenharmony_ci 8518c2ecf20Sopenharmony_ci#ifdef CONFIG_TCP_MD5SIG 8528c2ecf20Sopenharmony_ci if (md5) { 8538c2ecf20Sopenharmony_ci opts->options |= OPTION_MD5; 8548c2ecf20Sopenharmony_ci remaining -= TCPOLEN_MD5SIG_ALIGNED; 8558c2ecf20Sopenharmony_ci 8568c2ecf20Sopenharmony_ci /* We can't fit any SACK blocks in a packet with MD5 + TS 8578c2ecf20Sopenharmony_ci * options. There was discussion about disabling SACK 8588c2ecf20Sopenharmony_ci * rather than TS in order to fit in better with old, 8598c2ecf20Sopenharmony_ci * buggy kernels, but that was deemed to be unnecessary. 8608c2ecf20Sopenharmony_ci */ 8618c2ecf20Sopenharmony_ci if (synack_type != TCP_SYNACK_COOKIE) 8628c2ecf20Sopenharmony_ci ireq->tstamp_ok &= !ireq->sack_ok; 8638c2ecf20Sopenharmony_ci } 8648c2ecf20Sopenharmony_ci#endif 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci /* We always send an MSS option. */ 8678c2ecf20Sopenharmony_ci opts->mss = mss; 8688c2ecf20Sopenharmony_ci remaining -= TCPOLEN_MSS_ALIGNED; 8698c2ecf20Sopenharmony_ci 8708c2ecf20Sopenharmony_ci if (likely(ireq->wscale_ok)) { 8718c2ecf20Sopenharmony_ci opts->ws = ireq->rcv_wscale; 8728c2ecf20Sopenharmony_ci opts->options |= OPTION_WSCALE; 8738c2ecf20Sopenharmony_ci remaining -= TCPOLEN_WSCALE_ALIGNED; 8748c2ecf20Sopenharmony_ci } 8758c2ecf20Sopenharmony_ci if (likely(ireq->tstamp_ok)) { 8768c2ecf20Sopenharmony_ci opts->options |= OPTION_TS; 8778c2ecf20Sopenharmony_ci opts->tsval = tcp_skb_timestamp(skb) + tcp_rsk(req)->ts_off; 8788c2ecf20Sopenharmony_ci opts->tsecr = READ_ONCE(req->ts_recent); 8798c2ecf20Sopenharmony_ci remaining -= TCPOLEN_TSTAMP_ALIGNED; 8808c2ecf20Sopenharmony_ci } 8818c2ecf20Sopenharmony_ci if (likely(ireq->sack_ok)) { 8828c2ecf20Sopenharmony_ci opts->options |= OPTION_SACK_ADVERTISE; 8838c2ecf20Sopenharmony_ci if (unlikely(!ireq->tstamp_ok)) 8848c2ecf20Sopenharmony_ci remaining -= TCPOLEN_SACKPERM_ALIGNED; 8858c2ecf20Sopenharmony_ci } 8868c2ecf20Sopenharmony_ci if (foc != NULL && foc->len >= 0) { 8878c2ecf20Sopenharmony_ci u32 need = foc->len; 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci need += foc->exp ? TCPOLEN_EXP_FASTOPEN_BASE : 8908c2ecf20Sopenharmony_ci TCPOLEN_FASTOPEN_BASE; 8918c2ecf20Sopenharmony_ci need = (need + 3) & ~3U; /* Align to 32 bits */ 8928c2ecf20Sopenharmony_ci if (remaining >= need) { 8938c2ecf20Sopenharmony_ci opts->options |= OPTION_FAST_OPEN_COOKIE; 8948c2ecf20Sopenharmony_ci opts->fastopen_cookie = foc; 8958c2ecf20Sopenharmony_ci remaining -= need; 8968c2ecf20Sopenharmony_ci } 8978c2ecf20Sopenharmony_ci } 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_ci mptcp_set_option_cond(req, opts, &remaining); 9008c2ecf20Sopenharmony_ci 9018c2ecf20Sopenharmony_ci smc_set_option_cond(tcp_sk(sk), ireq, opts, &remaining); 9028c2ecf20Sopenharmony_ci 9038c2ecf20Sopenharmony_ci bpf_skops_hdr_opt_len((struct sock *)sk, skb, req, syn_skb, 9048c2ecf20Sopenharmony_ci synack_type, opts, &remaining); 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci return MAX_TCP_OPTION_SPACE - remaining; 9078c2ecf20Sopenharmony_ci} 9088c2ecf20Sopenharmony_ci 9098c2ecf20Sopenharmony_ci/* Compute TCP options for ESTABLISHED sockets. This is not the 9108c2ecf20Sopenharmony_ci * final wire format yet. 9118c2ecf20Sopenharmony_ci */ 9128c2ecf20Sopenharmony_cistatic unsigned int tcp_established_options(struct sock *sk, struct sk_buff *skb, 9138c2ecf20Sopenharmony_ci struct tcp_out_options *opts, 9148c2ecf20Sopenharmony_ci struct tcp_md5sig_key **md5) 9158c2ecf20Sopenharmony_ci{ 9168c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 9178c2ecf20Sopenharmony_ci unsigned int size = 0; 9188c2ecf20Sopenharmony_ci unsigned int eff_sacks; 9198c2ecf20Sopenharmony_ci 9208c2ecf20Sopenharmony_ci opts->options = 0; 9218c2ecf20Sopenharmony_ci 9228c2ecf20Sopenharmony_ci *md5 = NULL; 9238c2ecf20Sopenharmony_ci#ifdef CONFIG_TCP_MD5SIG 9248c2ecf20Sopenharmony_ci if (static_branch_unlikely(&tcp_md5_needed) && 9258c2ecf20Sopenharmony_ci rcu_access_pointer(tp->md5sig_info)) { 9268c2ecf20Sopenharmony_ci *md5 = tp->af_specific->md5_lookup(sk, sk); 9278c2ecf20Sopenharmony_ci if (*md5) { 9288c2ecf20Sopenharmony_ci opts->options |= OPTION_MD5; 9298c2ecf20Sopenharmony_ci size += TCPOLEN_MD5SIG_ALIGNED; 9308c2ecf20Sopenharmony_ci } 9318c2ecf20Sopenharmony_ci } 9328c2ecf20Sopenharmony_ci#endif 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_ci if (likely(tp->rx_opt.tstamp_ok)) { 9358c2ecf20Sopenharmony_ci opts->options |= OPTION_TS; 9368c2ecf20Sopenharmony_ci opts->tsval = skb ? tcp_skb_timestamp(skb) + tp->tsoffset : 0; 9378c2ecf20Sopenharmony_ci opts->tsecr = tp->rx_opt.ts_recent; 9388c2ecf20Sopenharmony_ci size += TCPOLEN_TSTAMP_ALIGNED; 9398c2ecf20Sopenharmony_ci } 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_ci /* MPTCP options have precedence over SACK for the limited TCP 9428c2ecf20Sopenharmony_ci * option space because a MPTCP connection would be forced to 9438c2ecf20Sopenharmony_ci * fall back to regular TCP if a required multipath option is 9448c2ecf20Sopenharmony_ci * missing. SACK still gets a chance to use whatever space is 9458c2ecf20Sopenharmony_ci * left. 9468c2ecf20Sopenharmony_ci */ 9478c2ecf20Sopenharmony_ci if (sk_is_mptcp(sk)) { 9488c2ecf20Sopenharmony_ci unsigned int remaining = MAX_TCP_OPTION_SPACE - size; 9498c2ecf20Sopenharmony_ci unsigned int opt_size = 0; 9508c2ecf20Sopenharmony_ci 9518c2ecf20Sopenharmony_ci if (mptcp_established_options(sk, skb, &opt_size, remaining, 9528c2ecf20Sopenharmony_ci &opts->mptcp)) { 9538c2ecf20Sopenharmony_ci opts->options |= OPTION_MPTCP; 9548c2ecf20Sopenharmony_ci size += opt_size; 9558c2ecf20Sopenharmony_ci } 9568c2ecf20Sopenharmony_ci } 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_ci eff_sacks = tp->rx_opt.num_sacks + tp->rx_opt.dsack; 9598c2ecf20Sopenharmony_ci if (unlikely(eff_sacks)) { 9608c2ecf20Sopenharmony_ci const unsigned int remaining = MAX_TCP_OPTION_SPACE - size; 9618c2ecf20Sopenharmony_ci if (unlikely(remaining < TCPOLEN_SACK_BASE_ALIGNED + 9628c2ecf20Sopenharmony_ci TCPOLEN_SACK_PERBLOCK)) 9638c2ecf20Sopenharmony_ci return size; 9648c2ecf20Sopenharmony_ci 9658c2ecf20Sopenharmony_ci opts->num_sack_blocks = 9668c2ecf20Sopenharmony_ci min_t(unsigned int, eff_sacks, 9678c2ecf20Sopenharmony_ci (remaining - TCPOLEN_SACK_BASE_ALIGNED) / 9688c2ecf20Sopenharmony_ci TCPOLEN_SACK_PERBLOCK); 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_ci size += TCPOLEN_SACK_BASE_ALIGNED + 9718c2ecf20Sopenharmony_ci opts->num_sack_blocks * TCPOLEN_SACK_PERBLOCK; 9728c2ecf20Sopenharmony_ci } 9738c2ecf20Sopenharmony_ci 9748c2ecf20Sopenharmony_ci if (unlikely(BPF_SOCK_OPS_TEST_FLAG(tp, 9758c2ecf20Sopenharmony_ci BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG))) { 9768c2ecf20Sopenharmony_ci unsigned int remaining = MAX_TCP_OPTION_SPACE - size; 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_ci bpf_skops_hdr_opt_len(sk, skb, NULL, NULL, 0, opts, &remaining); 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_ci size = MAX_TCP_OPTION_SPACE - remaining; 9818c2ecf20Sopenharmony_ci } 9828c2ecf20Sopenharmony_ci 9838c2ecf20Sopenharmony_ci return size; 9848c2ecf20Sopenharmony_ci} 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_ci 9878c2ecf20Sopenharmony_ci/* TCP SMALL QUEUES (TSQ) 9888c2ecf20Sopenharmony_ci * 9898c2ecf20Sopenharmony_ci * TSQ goal is to keep small amount of skbs per tcp flow in tx queues (qdisc+dev) 9908c2ecf20Sopenharmony_ci * to reduce RTT and bufferbloat. 9918c2ecf20Sopenharmony_ci * We do this using a special skb destructor (tcp_wfree). 9928c2ecf20Sopenharmony_ci * 9938c2ecf20Sopenharmony_ci * Its important tcp_wfree() can be replaced by sock_wfree() in the event skb 9948c2ecf20Sopenharmony_ci * needs to be reallocated in a driver. 9958c2ecf20Sopenharmony_ci * The invariant being skb->truesize subtracted from sk->sk_wmem_alloc 9968c2ecf20Sopenharmony_ci * 9978c2ecf20Sopenharmony_ci * Since transmit from skb destructor is forbidden, we use a tasklet 9988c2ecf20Sopenharmony_ci * to process all sockets that eventually need to send more skbs. 9998c2ecf20Sopenharmony_ci * We use one tasklet per cpu, with its own queue of sockets. 10008c2ecf20Sopenharmony_ci */ 10018c2ecf20Sopenharmony_cistruct tsq_tasklet { 10028c2ecf20Sopenharmony_ci struct tasklet_struct tasklet; 10038c2ecf20Sopenharmony_ci struct list_head head; /* queue of tcp sockets */ 10048c2ecf20Sopenharmony_ci}; 10058c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(struct tsq_tasklet, tsq_tasklet); 10068c2ecf20Sopenharmony_ci 10078c2ecf20Sopenharmony_cistatic void tcp_tsq_write(struct sock *sk) 10088c2ecf20Sopenharmony_ci{ 10098c2ecf20Sopenharmony_ci if ((1 << sk->sk_state) & 10108c2ecf20Sopenharmony_ci (TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_CLOSING | 10118c2ecf20Sopenharmony_ci TCPF_CLOSE_WAIT | TCPF_LAST_ACK)) { 10128c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 10138c2ecf20Sopenharmony_ci 10148c2ecf20Sopenharmony_ci if (tp->lost_out > tp->retrans_out && 10158c2ecf20Sopenharmony_ci tp->snd_cwnd > tcp_packets_in_flight(tp)) { 10168c2ecf20Sopenharmony_ci tcp_mstamp_refresh(tp); 10178c2ecf20Sopenharmony_ci tcp_xmit_retransmit_queue(sk); 10188c2ecf20Sopenharmony_ci } 10198c2ecf20Sopenharmony_ci 10208c2ecf20Sopenharmony_ci tcp_write_xmit(sk, tcp_current_mss(sk), tp->nonagle, 10218c2ecf20Sopenharmony_ci 0, GFP_ATOMIC); 10228c2ecf20Sopenharmony_ci } 10238c2ecf20Sopenharmony_ci} 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_cistatic void tcp_tsq_handler(struct sock *sk) 10268c2ecf20Sopenharmony_ci{ 10278c2ecf20Sopenharmony_ci bh_lock_sock(sk); 10288c2ecf20Sopenharmony_ci if (!sock_owned_by_user(sk)) 10298c2ecf20Sopenharmony_ci tcp_tsq_write(sk); 10308c2ecf20Sopenharmony_ci else if (!test_and_set_bit(TCP_TSQ_DEFERRED, &sk->sk_tsq_flags)) 10318c2ecf20Sopenharmony_ci sock_hold(sk); 10328c2ecf20Sopenharmony_ci bh_unlock_sock(sk); 10338c2ecf20Sopenharmony_ci} 10348c2ecf20Sopenharmony_ci/* 10358c2ecf20Sopenharmony_ci * One tasklet per cpu tries to send more skbs. 10368c2ecf20Sopenharmony_ci * We run in tasklet context but need to disable irqs when 10378c2ecf20Sopenharmony_ci * transferring tsq->head because tcp_wfree() might 10388c2ecf20Sopenharmony_ci * interrupt us (non NAPI drivers) 10398c2ecf20Sopenharmony_ci */ 10408c2ecf20Sopenharmony_cistatic void tcp_tasklet_func(unsigned long data) 10418c2ecf20Sopenharmony_ci{ 10428c2ecf20Sopenharmony_ci struct tsq_tasklet *tsq = (struct tsq_tasklet *)data; 10438c2ecf20Sopenharmony_ci LIST_HEAD(list); 10448c2ecf20Sopenharmony_ci unsigned long flags; 10458c2ecf20Sopenharmony_ci struct list_head *q, *n; 10468c2ecf20Sopenharmony_ci struct tcp_sock *tp; 10478c2ecf20Sopenharmony_ci struct sock *sk; 10488c2ecf20Sopenharmony_ci 10498c2ecf20Sopenharmony_ci local_irq_save(flags); 10508c2ecf20Sopenharmony_ci list_splice_init(&tsq->head, &list); 10518c2ecf20Sopenharmony_ci local_irq_restore(flags); 10528c2ecf20Sopenharmony_ci 10538c2ecf20Sopenharmony_ci list_for_each_safe(q, n, &list) { 10548c2ecf20Sopenharmony_ci tp = list_entry(q, struct tcp_sock, tsq_node); 10558c2ecf20Sopenharmony_ci list_del(&tp->tsq_node); 10568c2ecf20Sopenharmony_ci 10578c2ecf20Sopenharmony_ci sk = (struct sock *)tp; 10588c2ecf20Sopenharmony_ci smp_mb__before_atomic(); 10598c2ecf20Sopenharmony_ci clear_bit(TSQ_QUEUED, &sk->sk_tsq_flags); 10608c2ecf20Sopenharmony_ci 10618c2ecf20Sopenharmony_ci tcp_tsq_handler(sk); 10628c2ecf20Sopenharmony_ci sk_free(sk); 10638c2ecf20Sopenharmony_ci } 10648c2ecf20Sopenharmony_ci} 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci#define TCP_DEFERRED_ALL (TCPF_TSQ_DEFERRED | \ 10678c2ecf20Sopenharmony_ci TCPF_WRITE_TIMER_DEFERRED | \ 10688c2ecf20Sopenharmony_ci TCPF_DELACK_TIMER_DEFERRED | \ 10698c2ecf20Sopenharmony_ci TCPF_MTU_REDUCED_DEFERRED) 10708c2ecf20Sopenharmony_ci/** 10718c2ecf20Sopenharmony_ci * tcp_release_cb - tcp release_sock() callback 10728c2ecf20Sopenharmony_ci * @sk: socket 10738c2ecf20Sopenharmony_ci * 10748c2ecf20Sopenharmony_ci * called from release_sock() to perform protocol dependent 10758c2ecf20Sopenharmony_ci * actions before socket release. 10768c2ecf20Sopenharmony_ci */ 10778c2ecf20Sopenharmony_civoid tcp_release_cb(struct sock *sk) 10788c2ecf20Sopenharmony_ci{ 10798c2ecf20Sopenharmony_ci unsigned long flags, nflags; 10808c2ecf20Sopenharmony_ci 10818c2ecf20Sopenharmony_ci /* perform an atomic operation only if at least one flag is set */ 10828c2ecf20Sopenharmony_ci do { 10838c2ecf20Sopenharmony_ci flags = sk->sk_tsq_flags; 10848c2ecf20Sopenharmony_ci if (!(flags & TCP_DEFERRED_ALL)) 10858c2ecf20Sopenharmony_ci return; 10868c2ecf20Sopenharmony_ci nflags = flags & ~TCP_DEFERRED_ALL; 10878c2ecf20Sopenharmony_ci } while (cmpxchg(&sk->sk_tsq_flags, flags, nflags) != flags); 10888c2ecf20Sopenharmony_ci 10898c2ecf20Sopenharmony_ci if (flags & TCPF_TSQ_DEFERRED) { 10908c2ecf20Sopenharmony_ci tcp_tsq_write(sk); 10918c2ecf20Sopenharmony_ci __sock_put(sk); 10928c2ecf20Sopenharmony_ci } 10938c2ecf20Sopenharmony_ci /* Here begins the tricky part : 10948c2ecf20Sopenharmony_ci * We are called from release_sock() with : 10958c2ecf20Sopenharmony_ci * 1) BH disabled 10968c2ecf20Sopenharmony_ci * 2) sk_lock.slock spinlock held 10978c2ecf20Sopenharmony_ci * 3) socket owned by us (sk->sk_lock.owned == 1) 10988c2ecf20Sopenharmony_ci * 10998c2ecf20Sopenharmony_ci * But following code is meant to be called from BH handlers, 11008c2ecf20Sopenharmony_ci * so we should keep BH disabled, but early release socket ownership 11018c2ecf20Sopenharmony_ci */ 11028c2ecf20Sopenharmony_ci sock_release_ownership(sk); 11038c2ecf20Sopenharmony_ci 11048c2ecf20Sopenharmony_ci if (flags & TCPF_WRITE_TIMER_DEFERRED) { 11058c2ecf20Sopenharmony_ci tcp_write_timer_handler(sk); 11068c2ecf20Sopenharmony_ci __sock_put(sk); 11078c2ecf20Sopenharmony_ci } 11088c2ecf20Sopenharmony_ci if (flags & TCPF_DELACK_TIMER_DEFERRED) { 11098c2ecf20Sopenharmony_ci tcp_delack_timer_handler(sk); 11108c2ecf20Sopenharmony_ci __sock_put(sk); 11118c2ecf20Sopenharmony_ci } 11128c2ecf20Sopenharmony_ci if (flags & TCPF_MTU_REDUCED_DEFERRED) { 11138c2ecf20Sopenharmony_ci inet_csk(sk)->icsk_af_ops->mtu_reduced(sk); 11148c2ecf20Sopenharmony_ci __sock_put(sk); 11158c2ecf20Sopenharmony_ci } 11168c2ecf20Sopenharmony_ci} 11178c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tcp_release_cb); 11188c2ecf20Sopenharmony_ci 11198c2ecf20Sopenharmony_civoid __init tcp_tasklet_init(void) 11208c2ecf20Sopenharmony_ci{ 11218c2ecf20Sopenharmony_ci int i; 11228c2ecf20Sopenharmony_ci 11238c2ecf20Sopenharmony_ci for_each_possible_cpu(i) { 11248c2ecf20Sopenharmony_ci struct tsq_tasklet *tsq = &per_cpu(tsq_tasklet, i); 11258c2ecf20Sopenharmony_ci 11268c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&tsq->head); 11278c2ecf20Sopenharmony_ci tasklet_init(&tsq->tasklet, 11288c2ecf20Sopenharmony_ci tcp_tasklet_func, 11298c2ecf20Sopenharmony_ci (unsigned long)tsq); 11308c2ecf20Sopenharmony_ci } 11318c2ecf20Sopenharmony_ci} 11328c2ecf20Sopenharmony_ci 11338c2ecf20Sopenharmony_ci/* 11348c2ecf20Sopenharmony_ci * Write buffer destructor automatically called from kfree_skb. 11358c2ecf20Sopenharmony_ci * We can't xmit new skbs from this context, as we might already 11368c2ecf20Sopenharmony_ci * hold qdisc lock. 11378c2ecf20Sopenharmony_ci */ 11388c2ecf20Sopenharmony_civoid tcp_wfree(struct sk_buff *skb) 11398c2ecf20Sopenharmony_ci{ 11408c2ecf20Sopenharmony_ci struct sock *sk = skb->sk; 11418c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 11428c2ecf20Sopenharmony_ci unsigned long flags, nval, oval; 11438c2ecf20Sopenharmony_ci 11448c2ecf20Sopenharmony_ci /* Keep one reference on sk_wmem_alloc. 11458c2ecf20Sopenharmony_ci * Will be released by sk_free() from here or tcp_tasklet_func() 11468c2ecf20Sopenharmony_ci */ 11478c2ecf20Sopenharmony_ci WARN_ON(refcount_sub_and_test(skb->truesize - 1, &sk->sk_wmem_alloc)); 11488c2ecf20Sopenharmony_ci 11498c2ecf20Sopenharmony_ci /* If this softirq is serviced by ksoftirqd, we are likely under stress. 11508c2ecf20Sopenharmony_ci * Wait until our queues (qdisc + devices) are drained. 11518c2ecf20Sopenharmony_ci * This gives : 11528c2ecf20Sopenharmony_ci * - less callbacks to tcp_write_xmit(), reducing stress (batches) 11538c2ecf20Sopenharmony_ci * - chance for incoming ACK (processed by another cpu maybe) 11548c2ecf20Sopenharmony_ci * to migrate this flow (skb->ooo_okay will be eventually set) 11558c2ecf20Sopenharmony_ci */ 11568c2ecf20Sopenharmony_ci if (refcount_read(&sk->sk_wmem_alloc) >= SKB_TRUESIZE(1) && this_cpu_ksoftirqd() == current) 11578c2ecf20Sopenharmony_ci goto out; 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_ci for (oval = READ_ONCE(sk->sk_tsq_flags);; oval = nval) { 11608c2ecf20Sopenharmony_ci struct tsq_tasklet *tsq; 11618c2ecf20Sopenharmony_ci bool empty; 11628c2ecf20Sopenharmony_ci 11638c2ecf20Sopenharmony_ci if (!(oval & TSQF_THROTTLED) || (oval & TSQF_QUEUED)) 11648c2ecf20Sopenharmony_ci goto out; 11658c2ecf20Sopenharmony_ci 11668c2ecf20Sopenharmony_ci nval = (oval & ~TSQF_THROTTLED) | TSQF_QUEUED; 11678c2ecf20Sopenharmony_ci nval = cmpxchg(&sk->sk_tsq_flags, oval, nval); 11688c2ecf20Sopenharmony_ci if (nval != oval) 11698c2ecf20Sopenharmony_ci continue; 11708c2ecf20Sopenharmony_ci 11718c2ecf20Sopenharmony_ci /* queue this socket to tasklet queue */ 11728c2ecf20Sopenharmony_ci local_irq_save(flags); 11738c2ecf20Sopenharmony_ci tsq = this_cpu_ptr(&tsq_tasklet); 11748c2ecf20Sopenharmony_ci empty = list_empty(&tsq->head); 11758c2ecf20Sopenharmony_ci list_add(&tp->tsq_node, &tsq->head); 11768c2ecf20Sopenharmony_ci if (empty) 11778c2ecf20Sopenharmony_ci tasklet_schedule(&tsq->tasklet); 11788c2ecf20Sopenharmony_ci local_irq_restore(flags); 11798c2ecf20Sopenharmony_ci return; 11808c2ecf20Sopenharmony_ci } 11818c2ecf20Sopenharmony_ciout: 11828c2ecf20Sopenharmony_ci sk_free(sk); 11838c2ecf20Sopenharmony_ci} 11848c2ecf20Sopenharmony_ci 11858c2ecf20Sopenharmony_ci/* Note: Called under soft irq. 11868c2ecf20Sopenharmony_ci * We can call TCP stack right away, unless socket is owned by user. 11878c2ecf20Sopenharmony_ci */ 11888c2ecf20Sopenharmony_cienum hrtimer_restart tcp_pace_kick(struct hrtimer *timer) 11898c2ecf20Sopenharmony_ci{ 11908c2ecf20Sopenharmony_ci struct tcp_sock *tp = container_of(timer, struct tcp_sock, pacing_timer); 11918c2ecf20Sopenharmony_ci struct sock *sk = (struct sock *)tp; 11928c2ecf20Sopenharmony_ci 11938c2ecf20Sopenharmony_ci tcp_tsq_handler(sk); 11948c2ecf20Sopenharmony_ci sock_put(sk); 11958c2ecf20Sopenharmony_ci 11968c2ecf20Sopenharmony_ci return HRTIMER_NORESTART; 11978c2ecf20Sopenharmony_ci} 11988c2ecf20Sopenharmony_ci 11998c2ecf20Sopenharmony_cistatic void tcp_update_skb_after_send(struct sock *sk, struct sk_buff *skb, 12008c2ecf20Sopenharmony_ci u64 prior_wstamp) 12018c2ecf20Sopenharmony_ci{ 12028c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 12038c2ecf20Sopenharmony_ci 12048c2ecf20Sopenharmony_ci if (sk->sk_pacing_status != SK_PACING_NONE) { 12058c2ecf20Sopenharmony_ci unsigned long rate = sk->sk_pacing_rate; 12068c2ecf20Sopenharmony_ci 12078c2ecf20Sopenharmony_ci /* Original sch_fq does not pace first 10 MSS 12088c2ecf20Sopenharmony_ci * Note that tp->data_segs_out overflows after 2^32 packets, 12098c2ecf20Sopenharmony_ci * this is a minor annoyance. 12108c2ecf20Sopenharmony_ci */ 12118c2ecf20Sopenharmony_ci if (rate != ~0UL && rate && tp->data_segs_out >= 10) { 12128c2ecf20Sopenharmony_ci u64 len_ns = div64_ul((u64)skb->len * NSEC_PER_SEC, rate); 12138c2ecf20Sopenharmony_ci u64 credit = tp->tcp_wstamp_ns - prior_wstamp; 12148c2ecf20Sopenharmony_ci 12158c2ecf20Sopenharmony_ci /* take into account OS jitter */ 12168c2ecf20Sopenharmony_ci len_ns -= min_t(u64, len_ns / 2, credit); 12178c2ecf20Sopenharmony_ci tp->tcp_wstamp_ns += len_ns; 12188c2ecf20Sopenharmony_ci } 12198c2ecf20Sopenharmony_ci } 12208c2ecf20Sopenharmony_ci list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue); 12218c2ecf20Sopenharmony_ci} 12228c2ecf20Sopenharmony_ci 12238c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)); 12248c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(int inet6_csk_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)); 12258c2ecf20Sopenharmony_ciINDIRECT_CALLABLE_DECLARE(void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)); 12268c2ecf20Sopenharmony_ci 12278c2ecf20Sopenharmony_ci/* This routine actually transmits TCP packets queued in by 12288c2ecf20Sopenharmony_ci * tcp_do_sendmsg(). This is used by both the initial 12298c2ecf20Sopenharmony_ci * transmission and possible later retransmissions. 12308c2ecf20Sopenharmony_ci * All SKB's seen here are completely headerless. It is our 12318c2ecf20Sopenharmony_ci * job to build the TCP header, and pass the packet down to 12328c2ecf20Sopenharmony_ci * IP so it can do the same plus pass the packet off to the 12338c2ecf20Sopenharmony_ci * device. 12348c2ecf20Sopenharmony_ci * 12358c2ecf20Sopenharmony_ci * We are working here with either a clone of the original 12368c2ecf20Sopenharmony_ci * SKB, or a fresh unique copy made by the retransmit engine. 12378c2ecf20Sopenharmony_ci */ 12388c2ecf20Sopenharmony_cistatic int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, 12398c2ecf20Sopenharmony_ci int clone_it, gfp_t gfp_mask, u32 rcv_nxt) 12408c2ecf20Sopenharmony_ci{ 12418c2ecf20Sopenharmony_ci const struct inet_connection_sock *icsk = inet_csk(sk); 12428c2ecf20Sopenharmony_ci struct inet_sock *inet; 12438c2ecf20Sopenharmony_ci struct tcp_sock *tp; 12448c2ecf20Sopenharmony_ci struct tcp_skb_cb *tcb; 12458c2ecf20Sopenharmony_ci struct tcp_out_options opts; 12468c2ecf20Sopenharmony_ci unsigned int tcp_options_size, tcp_header_size; 12478c2ecf20Sopenharmony_ci struct sk_buff *oskb = NULL; 12488c2ecf20Sopenharmony_ci struct tcp_md5sig_key *md5; 12498c2ecf20Sopenharmony_ci struct tcphdr *th; 12508c2ecf20Sopenharmony_ci u64 prior_wstamp; 12518c2ecf20Sopenharmony_ci int err; 12528c2ecf20Sopenharmony_ci 12538c2ecf20Sopenharmony_ci BUG_ON(!skb || !tcp_skb_pcount(skb)); 12548c2ecf20Sopenharmony_ci tp = tcp_sk(sk); 12558c2ecf20Sopenharmony_ci prior_wstamp = tp->tcp_wstamp_ns; 12568c2ecf20Sopenharmony_ci tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache); 12578c2ecf20Sopenharmony_ci skb->skb_mstamp_ns = tp->tcp_wstamp_ns; 12588c2ecf20Sopenharmony_ci if (clone_it) { 12598c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tx.in_flight = TCP_SKB_CB(skb)->end_seq 12608c2ecf20Sopenharmony_ci - tp->snd_una; 12618c2ecf20Sopenharmony_ci oskb = skb; 12628c2ecf20Sopenharmony_ci 12638c2ecf20Sopenharmony_ci tcp_skb_tsorted_save(oskb) { 12648c2ecf20Sopenharmony_ci if (unlikely(skb_cloned(oskb))) 12658c2ecf20Sopenharmony_ci skb = pskb_copy(oskb, gfp_mask); 12668c2ecf20Sopenharmony_ci else 12678c2ecf20Sopenharmony_ci skb = skb_clone(oskb, gfp_mask); 12688c2ecf20Sopenharmony_ci } tcp_skb_tsorted_restore(oskb); 12698c2ecf20Sopenharmony_ci 12708c2ecf20Sopenharmony_ci if (unlikely(!skb)) 12718c2ecf20Sopenharmony_ci return -ENOBUFS; 12728c2ecf20Sopenharmony_ci /* retransmit skbs might have a non zero value in skb->dev 12738c2ecf20Sopenharmony_ci * because skb->dev is aliased with skb->rbnode.rb_left 12748c2ecf20Sopenharmony_ci */ 12758c2ecf20Sopenharmony_ci skb->dev = NULL; 12768c2ecf20Sopenharmony_ci } 12778c2ecf20Sopenharmony_ci 12788c2ecf20Sopenharmony_ci inet = inet_sk(sk); 12798c2ecf20Sopenharmony_ci tcb = TCP_SKB_CB(skb); 12808c2ecf20Sopenharmony_ci memset(&opts, 0, sizeof(opts)); 12818c2ecf20Sopenharmony_ci 12828c2ecf20Sopenharmony_ci if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) { 12838c2ecf20Sopenharmony_ci tcp_options_size = tcp_syn_options(sk, skb, &opts, &md5); 12848c2ecf20Sopenharmony_ci } else { 12858c2ecf20Sopenharmony_ci tcp_options_size = tcp_established_options(sk, skb, &opts, 12868c2ecf20Sopenharmony_ci &md5); 12878c2ecf20Sopenharmony_ci /* Force a PSH flag on all (GSO) packets to expedite GRO flush 12888c2ecf20Sopenharmony_ci * at receiver : This slightly improve GRO performance. 12898c2ecf20Sopenharmony_ci * Note that we do not force the PSH flag for non GSO packets, 12908c2ecf20Sopenharmony_ci * because they might be sent under high congestion events, 12918c2ecf20Sopenharmony_ci * and in this case it is better to delay the delivery of 1-MSS 12928c2ecf20Sopenharmony_ci * packets and thus the corresponding ACK packet that would 12938c2ecf20Sopenharmony_ci * release the following packet. 12948c2ecf20Sopenharmony_ci */ 12958c2ecf20Sopenharmony_ci if (tcp_skb_pcount(skb) > 1) 12968c2ecf20Sopenharmony_ci tcb->tcp_flags |= TCPHDR_PSH; 12978c2ecf20Sopenharmony_ci } 12988c2ecf20Sopenharmony_ci tcp_header_size = tcp_options_size + sizeof(struct tcphdr); 12998c2ecf20Sopenharmony_ci 13008c2ecf20Sopenharmony_ci /* if no packet is in qdisc/device queue, then allow XPS to select 13018c2ecf20Sopenharmony_ci * another queue. We can be called from tcp_tsq_handler() 13028c2ecf20Sopenharmony_ci * which holds one reference to sk. 13038c2ecf20Sopenharmony_ci * 13048c2ecf20Sopenharmony_ci * TODO: Ideally, in-flight pure ACK packets should not matter here. 13058c2ecf20Sopenharmony_ci * One way to get this would be to set skb->truesize = 2 on them. 13068c2ecf20Sopenharmony_ci */ 13078c2ecf20Sopenharmony_ci skb->ooo_okay = sk_wmem_alloc_get(sk) < SKB_TRUESIZE(1); 13088c2ecf20Sopenharmony_ci 13098c2ecf20Sopenharmony_ci /* If we had to use memory reserve to allocate this skb, 13108c2ecf20Sopenharmony_ci * this might cause drops if packet is looped back : 13118c2ecf20Sopenharmony_ci * Other socket might not have SOCK_MEMALLOC. 13128c2ecf20Sopenharmony_ci * Packets not looped back do not care about pfmemalloc. 13138c2ecf20Sopenharmony_ci */ 13148c2ecf20Sopenharmony_ci skb->pfmemalloc = 0; 13158c2ecf20Sopenharmony_ci 13168c2ecf20Sopenharmony_ci skb_push(skb, tcp_header_size); 13178c2ecf20Sopenharmony_ci skb_reset_transport_header(skb); 13188c2ecf20Sopenharmony_ci 13198c2ecf20Sopenharmony_ci skb_orphan(skb); 13208c2ecf20Sopenharmony_ci skb->sk = sk; 13218c2ecf20Sopenharmony_ci skb->destructor = skb_is_tcp_pure_ack(skb) ? __sock_wfree : tcp_wfree; 13228c2ecf20Sopenharmony_ci skb_set_hash_from_sk(skb, sk); 13238c2ecf20Sopenharmony_ci refcount_add(skb->truesize, &sk->sk_wmem_alloc); 13248c2ecf20Sopenharmony_ci 13258c2ecf20Sopenharmony_ci skb_set_dst_pending_confirm(skb, READ_ONCE(sk->sk_dst_pending_confirm)); 13268c2ecf20Sopenharmony_ci 13278c2ecf20Sopenharmony_ci /* Build TCP header and checksum it. */ 13288c2ecf20Sopenharmony_ci th = (struct tcphdr *)skb->data; 13298c2ecf20Sopenharmony_ci th->source = inet->inet_sport; 13308c2ecf20Sopenharmony_ci th->dest = inet->inet_dport; 13318c2ecf20Sopenharmony_ci th->seq = htonl(tcb->seq); 13328c2ecf20Sopenharmony_ci th->ack_seq = htonl(rcv_nxt); 13338c2ecf20Sopenharmony_ci *(((__be16 *)th) + 6) = htons(((tcp_header_size >> 2) << 12) | 13348c2ecf20Sopenharmony_ci tcb->tcp_flags); 13358c2ecf20Sopenharmony_ci 13368c2ecf20Sopenharmony_ci th->check = 0; 13378c2ecf20Sopenharmony_ci th->urg_ptr = 0; 13388c2ecf20Sopenharmony_ci 13398c2ecf20Sopenharmony_ci /* The urg_mode check is necessary during a below snd_una win probe */ 13408c2ecf20Sopenharmony_ci if (unlikely(tcp_urg_mode(tp) && before(tcb->seq, tp->snd_up))) { 13418c2ecf20Sopenharmony_ci if (before(tp->snd_up, tcb->seq + 0x10000)) { 13428c2ecf20Sopenharmony_ci th->urg_ptr = htons(tp->snd_up - tcb->seq); 13438c2ecf20Sopenharmony_ci th->urg = 1; 13448c2ecf20Sopenharmony_ci } else if (after(tcb->seq + 0xFFFF, tp->snd_nxt)) { 13458c2ecf20Sopenharmony_ci th->urg_ptr = htons(0xFFFF); 13468c2ecf20Sopenharmony_ci th->urg = 1; 13478c2ecf20Sopenharmony_ci } 13488c2ecf20Sopenharmony_ci } 13498c2ecf20Sopenharmony_ci 13508c2ecf20Sopenharmony_ci tcp_options_write((__be32 *)(th + 1), tp, &opts); 13518c2ecf20Sopenharmony_ci skb_shinfo(skb)->gso_type = sk->sk_gso_type; 13528c2ecf20Sopenharmony_ci if (likely(!(tcb->tcp_flags & TCPHDR_SYN))) { 13538c2ecf20Sopenharmony_ci th->window = htons(tcp_select_window(sk)); 13548c2ecf20Sopenharmony_ci tcp_ecn_send(sk, skb, th, tcp_header_size); 13558c2ecf20Sopenharmony_ci } else { 13568c2ecf20Sopenharmony_ci /* RFC1323: The window in SYN & SYN/ACK segments 13578c2ecf20Sopenharmony_ci * is never scaled. 13588c2ecf20Sopenharmony_ci */ 13598c2ecf20Sopenharmony_ci th->window = htons(min(tp->rcv_wnd, 65535U)); 13608c2ecf20Sopenharmony_ci } 13618c2ecf20Sopenharmony_ci#ifdef CONFIG_TCP_MD5SIG 13628c2ecf20Sopenharmony_ci /* Calculate the MD5 hash, as we have all we need now */ 13638c2ecf20Sopenharmony_ci if (md5) { 13648c2ecf20Sopenharmony_ci sk_nocaps_add(sk, NETIF_F_GSO_MASK); 13658c2ecf20Sopenharmony_ci tp->af_specific->calc_md5_hash(opts.hash_location, 13668c2ecf20Sopenharmony_ci md5, sk, skb); 13678c2ecf20Sopenharmony_ci } 13688c2ecf20Sopenharmony_ci#endif 13698c2ecf20Sopenharmony_ci 13708c2ecf20Sopenharmony_ci /* BPF prog is the last one writing header option */ 13718c2ecf20Sopenharmony_ci bpf_skops_write_hdr_opt(sk, skb, NULL, NULL, 0, &opts); 13728c2ecf20Sopenharmony_ci 13738c2ecf20Sopenharmony_ci INDIRECT_CALL_INET(icsk->icsk_af_ops->send_check, 13748c2ecf20Sopenharmony_ci tcp_v6_send_check, tcp_v4_send_check, 13758c2ecf20Sopenharmony_ci sk, skb); 13768c2ecf20Sopenharmony_ci 13778c2ecf20Sopenharmony_ci if (likely(tcb->tcp_flags & TCPHDR_ACK)) 13788c2ecf20Sopenharmony_ci tcp_event_ack_sent(sk, rcv_nxt); 13798c2ecf20Sopenharmony_ci 13808c2ecf20Sopenharmony_ci if (skb->len != tcp_header_size) { 13818c2ecf20Sopenharmony_ci tcp_event_data_sent(tp, sk); 13828c2ecf20Sopenharmony_ci tp->data_segs_out += tcp_skb_pcount(skb); 13838c2ecf20Sopenharmony_ci tp->bytes_sent += skb->len - tcp_header_size; 13848c2ecf20Sopenharmony_ci } 13858c2ecf20Sopenharmony_ci 13868c2ecf20Sopenharmony_ci if (after(tcb->end_seq, tp->snd_nxt) || tcb->seq == tcb->end_seq) 13878c2ecf20Sopenharmony_ci TCP_ADD_STATS(sock_net(sk), TCP_MIB_OUTSEGS, 13888c2ecf20Sopenharmony_ci tcp_skb_pcount(skb)); 13898c2ecf20Sopenharmony_ci 13908c2ecf20Sopenharmony_ci tp->segs_out += tcp_skb_pcount(skb); 13918c2ecf20Sopenharmony_ci /* OK, its time to fill skb_shinfo(skb)->gso_{segs|size} */ 13928c2ecf20Sopenharmony_ci skb_shinfo(skb)->gso_segs = tcp_skb_pcount(skb); 13938c2ecf20Sopenharmony_ci skb_shinfo(skb)->gso_size = tcp_skb_mss(skb); 13948c2ecf20Sopenharmony_ci 13958c2ecf20Sopenharmony_ci /* Leave earliest departure time in skb->tstamp (skb->skb_mstamp_ns) */ 13968c2ecf20Sopenharmony_ci 13978c2ecf20Sopenharmony_ci /* Cleanup our debris for IP stacks */ 13988c2ecf20Sopenharmony_ci memset(skb->cb, 0, max(sizeof(struct inet_skb_parm), 13998c2ecf20Sopenharmony_ci sizeof(struct inet6_skb_parm))); 14008c2ecf20Sopenharmony_ci 14018c2ecf20Sopenharmony_ci tcp_add_tx_delay(skb, tp); 14028c2ecf20Sopenharmony_ci 14038c2ecf20Sopenharmony_ci err = INDIRECT_CALL_INET(icsk->icsk_af_ops->queue_xmit, 14048c2ecf20Sopenharmony_ci inet6_csk_xmit, ip_queue_xmit, 14058c2ecf20Sopenharmony_ci sk, skb, &inet->cork.fl); 14068c2ecf20Sopenharmony_ci 14078c2ecf20Sopenharmony_ci if (unlikely(err > 0)) { 14088c2ecf20Sopenharmony_ci tcp_enter_cwr(sk); 14098c2ecf20Sopenharmony_ci err = net_xmit_eval(err); 14108c2ecf20Sopenharmony_ci } 14118c2ecf20Sopenharmony_ci if (!err && oskb) { 14128c2ecf20Sopenharmony_ci tcp_update_skb_after_send(sk, oskb, prior_wstamp); 14138c2ecf20Sopenharmony_ci tcp_rate_skb_sent(sk, oskb); 14148c2ecf20Sopenharmony_ci } 14158c2ecf20Sopenharmony_ci return err; 14168c2ecf20Sopenharmony_ci} 14178c2ecf20Sopenharmony_ci 14188c2ecf20Sopenharmony_cistatic int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it, 14198c2ecf20Sopenharmony_ci gfp_t gfp_mask) 14208c2ecf20Sopenharmony_ci{ 14218c2ecf20Sopenharmony_ci return __tcp_transmit_skb(sk, skb, clone_it, gfp_mask, 14228c2ecf20Sopenharmony_ci tcp_sk(sk)->rcv_nxt); 14238c2ecf20Sopenharmony_ci} 14248c2ecf20Sopenharmony_ci 14258c2ecf20Sopenharmony_ci/* This routine just queues the buffer for sending. 14268c2ecf20Sopenharmony_ci * 14278c2ecf20Sopenharmony_ci * NOTE: probe0 timer is not checked, do not forget tcp_push_pending_frames, 14288c2ecf20Sopenharmony_ci * otherwise socket can stall. 14298c2ecf20Sopenharmony_ci */ 14308c2ecf20Sopenharmony_cistatic void tcp_queue_skb(struct sock *sk, struct sk_buff *skb) 14318c2ecf20Sopenharmony_ci{ 14328c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 14338c2ecf20Sopenharmony_ci 14348c2ecf20Sopenharmony_ci /* Advance write_seq and place onto the write_queue. */ 14358c2ecf20Sopenharmony_ci WRITE_ONCE(tp->write_seq, TCP_SKB_CB(skb)->end_seq); 14368c2ecf20Sopenharmony_ci __skb_header_release(skb); 14378c2ecf20Sopenharmony_ci tcp_add_write_queue_tail(sk, skb); 14388c2ecf20Sopenharmony_ci sk_wmem_queued_add(sk, skb->truesize); 14398c2ecf20Sopenharmony_ci sk_mem_charge(sk, skb->truesize); 14408c2ecf20Sopenharmony_ci} 14418c2ecf20Sopenharmony_ci 14428c2ecf20Sopenharmony_ci/* Initialize TSO segments for a packet. */ 14438c2ecf20Sopenharmony_cistatic void tcp_set_skb_tso_segs(struct sk_buff *skb, unsigned int mss_now) 14448c2ecf20Sopenharmony_ci{ 14458c2ecf20Sopenharmony_ci if (skb->len <= mss_now) { 14468c2ecf20Sopenharmony_ci /* Avoid the costly divide in the normal 14478c2ecf20Sopenharmony_ci * non-TSO case. 14488c2ecf20Sopenharmony_ci */ 14498c2ecf20Sopenharmony_ci tcp_skb_pcount_set(skb, 1); 14508c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_gso_size = 0; 14518c2ecf20Sopenharmony_ci } else { 14528c2ecf20Sopenharmony_ci tcp_skb_pcount_set(skb, DIV_ROUND_UP(skb->len, mss_now)); 14538c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_gso_size = mss_now; 14548c2ecf20Sopenharmony_ci } 14558c2ecf20Sopenharmony_ci} 14568c2ecf20Sopenharmony_ci 14578c2ecf20Sopenharmony_ci/* Pcount in the middle of the write queue got changed, we need to do various 14588c2ecf20Sopenharmony_ci * tweaks to fix counters 14598c2ecf20Sopenharmony_ci */ 14608c2ecf20Sopenharmony_cistatic void tcp_adjust_pcount(struct sock *sk, const struct sk_buff *skb, int decr) 14618c2ecf20Sopenharmony_ci{ 14628c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 14638c2ecf20Sopenharmony_ci 14648c2ecf20Sopenharmony_ci tp->packets_out -= decr; 14658c2ecf20Sopenharmony_ci 14668c2ecf20Sopenharmony_ci if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) 14678c2ecf20Sopenharmony_ci tp->sacked_out -= decr; 14688c2ecf20Sopenharmony_ci if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) 14698c2ecf20Sopenharmony_ci tp->retrans_out -= decr; 14708c2ecf20Sopenharmony_ci if (TCP_SKB_CB(skb)->sacked & TCPCB_LOST) 14718c2ecf20Sopenharmony_ci tp->lost_out -= decr; 14728c2ecf20Sopenharmony_ci 14738c2ecf20Sopenharmony_ci /* Reno case is special. Sigh... */ 14748c2ecf20Sopenharmony_ci if (tcp_is_reno(tp) && decr > 0) 14758c2ecf20Sopenharmony_ci tp->sacked_out -= min_t(u32, tp->sacked_out, decr); 14768c2ecf20Sopenharmony_ci 14778c2ecf20Sopenharmony_ci if (tp->lost_skb_hint && 14788c2ecf20Sopenharmony_ci before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(tp->lost_skb_hint)->seq) && 14798c2ecf20Sopenharmony_ci (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) 14808c2ecf20Sopenharmony_ci tp->lost_cnt_hint -= decr; 14818c2ecf20Sopenharmony_ci 14828c2ecf20Sopenharmony_ci tcp_verify_left_out(tp); 14838c2ecf20Sopenharmony_ci} 14848c2ecf20Sopenharmony_ci 14858c2ecf20Sopenharmony_cistatic bool tcp_has_tx_tstamp(const struct sk_buff *skb) 14868c2ecf20Sopenharmony_ci{ 14878c2ecf20Sopenharmony_ci return TCP_SKB_CB(skb)->txstamp_ack || 14888c2ecf20Sopenharmony_ci (skb_shinfo(skb)->tx_flags & SKBTX_ANY_TSTAMP); 14898c2ecf20Sopenharmony_ci} 14908c2ecf20Sopenharmony_ci 14918c2ecf20Sopenharmony_cistatic void tcp_fragment_tstamp(struct sk_buff *skb, struct sk_buff *skb2) 14928c2ecf20Sopenharmony_ci{ 14938c2ecf20Sopenharmony_ci struct skb_shared_info *shinfo = skb_shinfo(skb); 14948c2ecf20Sopenharmony_ci 14958c2ecf20Sopenharmony_ci if (unlikely(tcp_has_tx_tstamp(skb)) && 14968c2ecf20Sopenharmony_ci !before(shinfo->tskey, TCP_SKB_CB(skb2)->seq)) { 14978c2ecf20Sopenharmony_ci struct skb_shared_info *shinfo2 = skb_shinfo(skb2); 14988c2ecf20Sopenharmony_ci u8 tsflags = shinfo->tx_flags & SKBTX_ANY_TSTAMP; 14998c2ecf20Sopenharmony_ci 15008c2ecf20Sopenharmony_ci shinfo->tx_flags &= ~tsflags; 15018c2ecf20Sopenharmony_ci shinfo2->tx_flags |= tsflags; 15028c2ecf20Sopenharmony_ci swap(shinfo->tskey, shinfo2->tskey); 15038c2ecf20Sopenharmony_ci TCP_SKB_CB(skb2)->txstamp_ack = TCP_SKB_CB(skb)->txstamp_ack; 15048c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->txstamp_ack = 0; 15058c2ecf20Sopenharmony_ci } 15068c2ecf20Sopenharmony_ci} 15078c2ecf20Sopenharmony_ci 15088c2ecf20Sopenharmony_cistatic void tcp_skb_fragment_eor(struct sk_buff *skb, struct sk_buff *skb2) 15098c2ecf20Sopenharmony_ci{ 15108c2ecf20Sopenharmony_ci TCP_SKB_CB(skb2)->eor = TCP_SKB_CB(skb)->eor; 15118c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->eor = 0; 15128c2ecf20Sopenharmony_ci} 15138c2ecf20Sopenharmony_ci 15148c2ecf20Sopenharmony_ci/* Insert buff after skb on the write or rtx queue of sk. */ 15158c2ecf20Sopenharmony_cistatic void tcp_insert_write_queue_after(struct sk_buff *skb, 15168c2ecf20Sopenharmony_ci struct sk_buff *buff, 15178c2ecf20Sopenharmony_ci struct sock *sk, 15188c2ecf20Sopenharmony_ci enum tcp_queue tcp_queue) 15198c2ecf20Sopenharmony_ci{ 15208c2ecf20Sopenharmony_ci if (tcp_queue == TCP_FRAG_IN_WRITE_QUEUE) 15218c2ecf20Sopenharmony_ci __skb_queue_after(&sk->sk_write_queue, skb, buff); 15228c2ecf20Sopenharmony_ci else 15238c2ecf20Sopenharmony_ci tcp_rbtree_insert(&sk->tcp_rtx_queue, buff); 15248c2ecf20Sopenharmony_ci} 15258c2ecf20Sopenharmony_ci 15268c2ecf20Sopenharmony_ci/* Function to create two new TCP segments. Shrinks the given segment 15278c2ecf20Sopenharmony_ci * to the specified size and appends a new segment with the rest of the 15288c2ecf20Sopenharmony_ci * packet to the list. This won't be called frequently, I hope. 15298c2ecf20Sopenharmony_ci * Remember, these are still headerless SKBs at this point. 15308c2ecf20Sopenharmony_ci */ 15318c2ecf20Sopenharmony_ciint tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue, 15328c2ecf20Sopenharmony_ci struct sk_buff *skb, u32 len, 15338c2ecf20Sopenharmony_ci unsigned int mss_now, gfp_t gfp) 15348c2ecf20Sopenharmony_ci{ 15358c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 15368c2ecf20Sopenharmony_ci struct sk_buff *buff; 15378c2ecf20Sopenharmony_ci int nsize, old_factor; 15388c2ecf20Sopenharmony_ci long limit; 15398c2ecf20Sopenharmony_ci int nlen; 15408c2ecf20Sopenharmony_ci u8 flags; 15418c2ecf20Sopenharmony_ci 15428c2ecf20Sopenharmony_ci if (WARN_ON(len > skb->len)) 15438c2ecf20Sopenharmony_ci return -EINVAL; 15448c2ecf20Sopenharmony_ci 15458c2ecf20Sopenharmony_ci nsize = skb_headlen(skb) - len; 15468c2ecf20Sopenharmony_ci if (nsize < 0) 15478c2ecf20Sopenharmony_ci nsize = 0; 15488c2ecf20Sopenharmony_ci 15498c2ecf20Sopenharmony_ci /* tcp_sendmsg() can overshoot sk_wmem_queued by one full size skb. 15508c2ecf20Sopenharmony_ci * We need some allowance to not penalize applications setting small 15518c2ecf20Sopenharmony_ci * SO_SNDBUF values. 15528c2ecf20Sopenharmony_ci * Also allow first and last skb in retransmit queue to be split. 15538c2ecf20Sopenharmony_ci */ 15548c2ecf20Sopenharmony_ci limit = sk->sk_sndbuf + 2 * SKB_TRUESIZE(GSO_MAX_SIZE); 15558c2ecf20Sopenharmony_ci if (unlikely((sk->sk_wmem_queued >> 1) > limit && 15568c2ecf20Sopenharmony_ci tcp_queue != TCP_FRAG_IN_WRITE_QUEUE && 15578c2ecf20Sopenharmony_ci skb != tcp_rtx_queue_head(sk) && 15588c2ecf20Sopenharmony_ci skb != tcp_rtx_queue_tail(sk))) { 15598c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPWQUEUETOOBIG); 15608c2ecf20Sopenharmony_ci return -ENOMEM; 15618c2ecf20Sopenharmony_ci } 15628c2ecf20Sopenharmony_ci 15638c2ecf20Sopenharmony_ci if (skb_unclone(skb, gfp)) 15648c2ecf20Sopenharmony_ci return -ENOMEM; 15658c2ecf20Sopenharmony_ci 15668c2ecf20Sopenharmony_ci /* Get a new skb... force flag on. */ 15678c2ecf20Sopenharmony_ci buff = sk_stream_alloc_skb(sk, nsize, gfp, true); 15688c2ecf20Sopenharmony_ci if (!buff) 15698c2ecf20Sopenharmony_ci return -ENOMEM; /* We'll just try again later. */ 15708c2ecf20Sopenharmony_ci skb_copy_decrypted(buff, skb); 15718c2ecf20Sopenharmony_ci 15728c2ecf20Sopenharmony_ci sk_wmem_queued_add(sk, buff->truesize); 15738c2ecf20Sopenharmony_ci sk_mem_charge(sk, buff->truesize); 15748c2ecf20Sopenharmony_ci nlen = skb->len - len - nsize; 15758c2ecf20Sopenharmony_ci buff->truesize += nlen; 15768c2ecf20Sopenharmony_ci skb->truesize -= nlen; 15778c2ecf20Sopenharmony_ci 15788c2ecf20Sopenharmony_ci /* Correct the sequence numbers. */ 15798c2ecf20Sopenharmony_ci TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len; 15808c2ecf20Sopenharmony_ci TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq; 15818c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq; 15828c2ecf20Sopenharmony_ci 15838c2ecf20Sopenharmony_ci /* PSH and FIN should only be set in the second packet. */ 15848c2ecf20Sopenharmony_ci flags = TCP_SKB_CB(skb)->tcp_flags; 15858c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH); 15868c2ecf20Sopenharmony_ci TCP_SKB_CB(buff)->tcp_flags = flags; 15878c2ecf20Sopenharmony_ci TCP_SKB_CB(buff)->sacked = TCP_SKB_CB(skb)->sacked; 15888c2ecf20Sopenharmony_ci tcp_skb_fragment_eor(skb, buff); 15898c2ecf20Sopenharmony_ci 15908c2ecf20Sopenharmony_ci skb_split(skb, buff, len); 15918c2ecf20Sopenharmony_ci 15928c2ecf20Sopenharmony_ci buff->ip_summed = CHECKSUM_PARTIAL; 15938c2ecf20Sopenharmony_ci 15948c2ecf20Sopenharmony_ci buff->tstamp = skb->tstamp; 15958c2ecf20Sopenharmony_ci tcp_fragment_tstamp(skb, buff); 15968c2ecf20Sopenharmony_ci 15978c2ecf20Sopenharmony_ci old_factor = tcp_skb_pcount(skb); 15988c2ecf20Sopenharmony_ci 15998c2ecf20Sopenharmony_ci /* Fix up tso_factor for both original and new SKB. */ 16008c2ecf20Sopenharmony_ci tcp_set_skb_tso_segs(skb, mss_now); 16018c2ecf20Sopenharmony_ci tcp_set_skb_tso_segs(buff, mss_now); 16028c2ecf20Sopenharmony_ci 16038c2ecf20Sopenharmony_ci /* Update delivered info for the new segment */ 16048c2ecf20Sopenharmony_ci TCP_SKB_CB(buff)->tx = TCP_SKB_CB(skb)->tx; 16058c2ecf20Sopenharmony_ci 16068c2ecf20Sopenharmony_ci /* If this packet has been sent out already, we must 16078c2ecf20Sopenharmony_ci * adjust the various packet counters. 16088c2ecf20Sopenharmony_ci */ 16098c2ecf20Sopenharmony_ci if (!before(tp->snd_nxt, TCP_SKB_CB(buff)->end_seq)) { 16108c2ecf20Sopenharmony_ci int diff = old_factor - tcp_skb_pcount(skb) - 16118c2ecf20Sopenharmony_ci tcp_skb_pcount(buff); 16128c2ecf20Sopenharmony_ci 16138c2ecf20Sopenharmony_ci if (diff) 16148c2ecf20Sopenharmony_ci tcp_adjust_pcount(sk, skb, diff); 16158c2ecf20Sopenharmony_ci } 16168c2ecf20Sopenharmony_ci 16178c2ecf20Sopenharmony_ci /* Link BUFF into the send queue. */ 16188c2ecf20Sopenharmony_ci __skb_header_release(buff); 16198c2ecf20Sopenharmony_ci tcp_insert_write_queue_after(skb, buff, sk, tcp_queue); 16208c2ecf20Sopenharmony_ci if (tcp_queue == TCP_FRAG_IN_RTX_QUEUE) 16218c2ecf20Sopenharmony_ci list_add(&buff->tcp_tsorted_anchor, &skb->tcp_tsorted_anchor); 16228c2ecf20Sopenharmony_ci 16238c2ecf20Sopenharmony_ci return 0; 16248c2ecf20Sopenharmony_ci} 16258c2ecf20Sopenharmony_ci 16268c2ecf20Sopenharmony_ci/* This is similar to __pskb_pull_tail(). The difference is that pulled 16278c2ecf20Sopenharmony_ci * data is not copied, but immediately discarded. 16288c2ecf20Sopenharmony_ci */ 16298c2ecf20Sopenharmony_cistatic int __pskb_trim_head(struct sk_buff *skb, int len) 16308c2ecf20Sopenharmony_ci{ 16318c2ecf20Sopenharmony_ci struct skb_shared_info *shinfo; 16328c2ecf20Sopenharmony_ci int i, k, eat; 16338c2ecf20Sopenharmony_ci 16348c2ecf20Sopenharmony_ci eat = min_t(int, len, skb_headlen(skb)); 16358c2ecf20Sopenharmony_ci if (eat) { 16368c2ecf20Sopenharmony_ci __skb_pull(skb, eat); 16378c2ecf20Sopenharmony_ci len -= eat; 16388c2ecf20Sopenharmony_ci if (!len) 16398c2ecf20Sopenharmony_ci return 0; 16408c2ecf20Sopenharmony_ci } 16418c2ecf20Sopenharmony_ci eat = len; 16428c2ecf20Sopenharmony_ci k = 0; 16438c2ecf20Sopenharmony_ci shinfo = skb_shinfo(skb); 16448c2ecf20Sopenharmony_ci for (i = 0; i < shinfo->nr_frags; i++) { 16458c2ecf20Sopenharmony_ci int size = skb_frag_size(&shinfo->frags[i]); 16468c2ecf20Sopenharmony_ci 16478c2ecf20Sopenharmony_ci if (size <= eat) { 16488c2ecf20Sopenharmony_ci skb_frag_unref(skb, i); 16498c2ecf20Sopenharmony_ci eat -= size; 16508c2ecf20Sopenharmony_ci } else { 16518c2ecf20Sopenharmony_ci shinfo->frags[k] = shinfo->frags[i]; 16528c2ecf20Sopenharmony_ci if (eat) { 16538c2ecf20Sopenharmony_ci skb_frag_off_add(&shinfo->frags[k], eat); 16548c2ecf20Sopenharmony_ci skb_frag_size_sub(&shinfo->frags[k], eat); 16558c2ecf20Sopenharmony_ci eat = 0; 16568c2ecf20Sopenharmony_ci } 16578c2ecf20Sopenharmony_ci k++; 16588c2ecf20Sopenharmony_ci } 16598c2ecf20Sopenharmony_ci } 16608c2ecf20Sopenharmony_ci shinfo->nr_frags = k; 16618c2ecf20Sopenharmony_ci 16628c2ecf20Sopenharmony_ci skb->data_len -= len; 16638c2ecf20Sopenharmony_ci skb->len = skb->data_len; 16648c2ecf20Sopenharmony_ci return len; 16658c2ecf20Sopenharmony_ci} 16668c2ecf20Sopenharmony_ci 16678c2ecf20Sopenharmony_ci/* Remove acked data from a packet in the transmit queue. */ 16688c2ecf20Sopenharmony_ciint tcp_trim_head(struct sock *sk, struct sk_buff *skb, u32 len) 16698c2ecf20Sopenharmony_ci{ 16708c2ecf20Sopenharmony_ci u32 delta_truesize; 16718c2ecf20Sopenharmony_ci 16728c2ecf20Sopenharmony_ci if (skb_unclone(skb, GFP_ATOMIC)) 16738c2ecf20Sopenharmony_ci return -ENOMEM; 16748c2ecf20Sopenharmony_ci 16758c2ecf20Sopenharmony_ci delta_truesize = __pskb_trim_head(skb, len); 16768c2ecf20Sopenharmony_ci 16778c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->seq += len; 16788c2ecf20Sopenharmony_ci skb->ip_summed = CHECKSUM_PARTIAL; 16798c2ecf20Sopenharmony_ci 16808c2ecf20Sopenharmony_ci if (delta_truesize) { 16818c2ecf20Sopenharmony_ci skb->truesize -= delta_truesize; 16828c2ecf20Sopenharmony_ci sk_wmem_queued_add(sk, -delta_truesize); 16838c2ecf20Sopenharmony_ci sk_mem_uncharge(sk, delta_truesize); 16848c2ecf20Sopenharmony_ci } 16858c2ecf20Sopenharmony_ci 16868c2ecf20Sopenharmony_ci /* Any change of skb->len requires recalculation of tso factor. */ 16878c2ecf20Sopenharmony_ci if (tcp_skb_pcount(skb) > 1) 16888c2ecf20Sopenharmony_ci tcp_set_skb_tso_segs(skb, tcp_skb_mss(skb)); 16898c2ecf20Sopenharmony_ci 16908c2ecf20Sopenharmony_ci return 0; 16918c2ecf20Sopenharmony_ci} 16928c2ecf20Sopenharmony_ci 16938c2ecf20Sopenharmony_ci/* Calculate MSS not accounting any TCP options. */ 16948c2ecf20Sopenharmony_cistatic inline int __tcp_mtu_to_mss(struct sock *sk, int pmtu) 16958c2ecf20Sopenharmony_ci{ 16968c2ecf20Sopenharmony_ci const struct tcp_sock *tp = tcp_sk(sk); 16978c2ecf20Sopenharmony_ci const struct inet_connection_sock *icsk = inet_csk(sk); 16988c2ecf20Sopenharmony_ci int mss_now; 16998c2ecf20Sopenharmony_ci 17008c2ecf20Sopenharmony_ci /* Calculate base mss without TCP options: 17018c2ecf20Sopenharmony_ci It is MMS_S - sizeof(tcphdr) of rfc1122 17028c2ecf20Sopenharmony_ci */ 17038c2ecf20Sopenharmony_ci mss_now = pmtu - icsk->icsk_af_ops->net_header_len - sizeof(struct tcphdr); 17048c2ecf20Sopenharmony_ci 17058c2ecf20Sopenharmony_ci /* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */ 17068c2ecf20Sopenharmony_ci if (icsk->icsk_af_ops->net_frag_header_len) { 17078c2ecf20Sopenharmony_ci const struct dst_entry *dst = __sk_dst_get(sk); 17088c2ecf20Sopenharmony_ci 17098c2ecf20Sopenharmony_ci if (dst && dst_allfrag(dst)) 17108c2ecf20Sopenharmony_ci mss_now -= icsk->icsk_af_ops->net_frag_header_len; 17118c2ecf20Sopenharmony_ci } 17128c2ecf20Sopenharmony_ci 17138c2ecf20Sopenharmony_ci /* Clamp it (mss_clamp does not include tcp options) */ 17148c2ecf20Sopenharmony_ci if (mss_now > tp->rx_opt.mss_clamp) 17158c2ecf20Sopenharmony_ci mss_now = tp->rx_opt.mss_clamp; 17168c2ecf20Sopenharmony_ci 17178c2ecf20Sopenharmony_ci /* Now subtract optional transport overhead */ 17188c2ecf20Sopenharmony_ci mss_now -= icsk->icsk_ext_hdr_len; 17198c2ecf20Sopenharmony_ci 17208c2ecf20Sopenharmony_ci /* Then reserve room for full set of TCP options and 8 bytes of data */ 17218c2ecf20Sopenharmony_ci mss_now = max(mss_now, 17228c2ecf20Sopenharmony_ci READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_snd_mss)); 17238c2ecf20Sopenharmony_ci return mss_now; 17248c2ecf20Sopenharmony_ci} 17258c2ecf20Sopenharmony_ci 17268c2ecf20Sopenharmony_ci/* Calculate MSS. Not accounting for SACKs here. */ 17278c2ecf20Sopenharmony_ciint tcp_mtu_to_mss(struct sock *sk, int pmtu) 17288c2ecf20Sopenharmony_ci{ 17298c2ecf20Sopenharmony_ci /* Subtract TCP options size, not including SACKs */ 17308c2ecf20Sopenharmony_ci return __tcp_mtu_to_mss(sk, pmtu) - 17318c2ecf20Sopenharmony_ci (tcp_sk(sk)->tcp_header_len - sizeof(struct tcphdr)); 17328c2ecf20Sopenharmony_ci} 17338c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tcp_mtu_to_mss); 17348c2ecf20Sopenharmony_ci 17358c2ecf20Sopenharmony_ci/* Inverse of above */ 17368c2ecf20Sopenharmony_ciint tcp_mss_to_mtu(struct sock *sk, int mss) 17378c2ecf20Sopenharmony_ci{ 17388c2ecf20Sopenharmony_ci const struct tcp_sock *tp = tcp_sk(sk); 17398c2ecf20Sopenharmony_ci const struct inet_connection_sock *icsk = inet_csk(sk); 17408c2ecf20Sopenharmony_ci int mtu; 17418c2ecf20Sopenharmony_ci 17428c2ecf20Sopenharmony_ci mtu = mss + 17438c2ecf20Sopenharmony_ci tp->tcp_header_len + 17448c2ecf20Sopenharmony_ci icsk->icsk_ext_hdr_len + 17458c2ecf20Sopenharmony_ci icsk->icsk_af_ops->net_header_len; 17468c2ecf20Sopenharmony_ci 17478c2ecf20Sopenharmony_ci /* IPv6 adds a frag_hdr in case RTAX_FEATURE_ALLFRAG is set */ 17488c2ecf20Sopenharmony_ci if (icsk->icsk_af_ops->net_frag_header_len) { 17498c2ecf20Sopenharmony_ci const struct dst_entry *dst = __sk_dst_get(sk); 17508c2ecf20Sopenharmony_ci 17518c2ecf20Sopenharmony_ci if (dst && dst_allfrag(dst)) 17528c2ecf20Sopenharmony_ci mtu += icsk->icsk_af_ops->net_frag_header_len; 17538c2ecf20Sopenharmony_ci } 17548c2ecf20Sopenharmony_ci return mtu; 17558c2ecf20Sopenharmony_ci} 17568c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tcp_mss_to_mtu); 17578c2ecf20Sopenharmony_ci 17588c2ecf20Sopenharmony_ci/* MTU probing init per socket */ 17598c2ecf20Sopenharmony_civoid tcp_mtup_init(struct sock *sk) 17608c2ecf20Sopenharmony_ci{ 17618c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 17628c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 17638c2ecf20Sopenharmony_ci struct net *net = sock_net(sk); 17648c2ecf20Sopenharmony_ci 17658c2ecf20Sopenharmony_ci icsk->icsk_mtup.enabled = READ_ONCE(net->ipv4.sysctl_tcp_mtu_probing) > 1; 17668c2ecf20Sopenharmony_ci icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + sizeof(struct tcphdr) + 17678c2ecf20Sopenharmony_ci icsk->icsk_af_ops->net_header_len; 17688c2ecf20Sopenharmony_ci icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, READ_ONCE(net->ipv4.sysctl_tcp_base_mss)); 17698c2ecf20Sopenharmony_ci icsk->icsk_mtup.probe_size = 0; 17708c2ecf20Sopenharmony_ci if (icsk->icsk_mtup.enabled) 17718c2ecf20Sopenharmony_ci icsk->icsk_mtup.probe_timestamp = tcp_jiffies32; 17728c2ecf20Sopenharmony_ci} 17738c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tcp_mtup_init); 17748c2ecf20Sopenharmony_ci 17758c2ecf20Sopenharmony_ci/* This function synchronize snd mss to current pmtu/exthdr set. 17768c2ecf20Sopenharmony_ci 17778c2ecf20Sopenharmony_ci tp->rx_opt.user_mss is mss set by user by TCP_MAXSEG. It does NOT counts 17788c2ecf20Sopenharmony_ci for TCP options, but includes only bare TCP header. 17798c2ecf20Sopenharmony_ci 17808c2ecf20Sopenharmony_ci tp->rx_opt.mss_clamp is mss negotiated at connection setup. 17818c2ecf20Sopenharmony_ci It is minimum of user_mss and mss received with SYN. 17828c2ecf20Sopenharmony_ci It also does not include TCP options. 17838c2ecf20Sopenharmony_ci 17848c2ecf20Sopenharmony_ci inet_csk(sk)->icsk_pmtu_cookie is last pmtu, seen by this function. 17858c2ecf20Sopenharmony_ci 17868c2ecf20Sopenharmony_ci tp->mss_cache is current effective sending mss, including 17878c2ecf20Sopenharmony_ci all tcp options except for SACKs. It is evaluated, 17888c2ecf20Sopenharmony_ci taking into account current pmtu, but never exceeds 17898c2ecf20Sopenharmony_ci tp->rx_opt.mss_clamp. 17908c2ecf20Sopenharmony_ci 17918c2ecf20Sopenharmony_ci NOTE1. rfc1122 clearly states that advertised MSS 17928c2ecf20Sopenharmony_ci DOES NOT include either tcp or ip options. 17938c2ecf20Sopenharmony_ci 17948c2ecf20Sopenharmony_ci NOTE2. inet_csk(sk)->icsk_pmtu_cookie and tp->mss_cache 17958c2ecf20Sopenharmony_ci are READ ONLY outside this function. --ANK (980731) 17968c2ecf20Sopenharmony_ci */ 17978c2ecf20Sopenharmony_ciunsigned int tcp_sync_mss(struct sock *sk, u32 pmtu) 17988c2ecf20Sopenharmony_ci{ 17998c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 18008c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 18018c2ecf20Sopenharmony_ci int mss_now; 18028c2ecf20Sopenharmony_ci 18038c2ecf20Sopenharmony_ci if (icsk->icsk_mtup.search_high > pmtu) 18048c2ecf20Sopenharmony_ci icsk->icsk_mtup.search_high = pmtu; 18058c2ecf20Sopenharmony_ci 18068c2ecf20Sopenharmony_ci mss_now = tcp_mtu_to_mss(sk, pmtu); 18078c2ecf20Sopenharmony_ci mss_now = tcp_bound_to_half_wnd(tp, mss_now); 18088c2ecf20Sopenharmony_ci 18098c2ecf20Sopenharmony_ci /* And store cached results */ 18108c2ecf20Sopenharmony_ci icsk->icsk_pmtu_cookie = pmtu; 18118c2ecf20Sopenharmony_ci if (icsk->icsk_mtup.enabled) 18128c2ecf20Sopenharmony_ci mss_now = min(mss_now, tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_low)); 18138c2ecf20Sopenharmony_ci tp->mss_cache = mss_now; 18148c2ecf20Sopenharmony_ci 18158c2ecf20Sopenharmony_ci return mss_now; 18168c2ecf20Sopenharmony_ci} 18178c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tcp_sync_mss); 18188c2ecf20Sopenharmony_ci 18198c2ecf20Sopenharmony_ci/* Compute the current effective MSS, taking SACKs and IP options, 18208c2ecf20Sopenharmony_ci * and even PMTU discovery events into account. 18218c2ecf20Sopenharmony_ci */ 18228c2ecf20Sopenharmony_ciunsigned int tcp_current_mss(struct sock *sk) 18238c2ecf20Sopenharmony_ci{ 18248c2ecf20Sopenharmony_ci const struct tcp_sock *tp = tcp_sk(sk); 18258c2ecf20Sopenharmony_ci const struct dst_entry *dst = __sk_dst_get(sk); 18268c2ecf20Sopenharmony_ci u32 mss_now; 18278c2ecf20Sopenharmony_ci unsigned int header_len; 18288c2ecf20Sopenharmony_ci struct tcp_out_options opts; 18298c2ecf20Sopenharmony_ci struct tcp_md5sig_key *md5; 18308c2ecf20Sopenharmony_ci 18318c2ecf20Sopenharmony_ci mss_now = tp->mss_cache; 18328c2ecf20Sopenharmony_ci 18338c2ecf20Sopenharmony_ci if (dst) { 18348c2ecf20Sopenharmony_ci u32 mtu = dst_mtu(dst); 18358c2ecf20Sopenharmony_ci if (mtu != inet_csk(sk)->icsk_pmtu_cookie) 18368c2ecf20Sopenharmony_ci mss_now = tcp_sync_mss(sk, mtu); 18378c2ecf20Sopenharmony_ci } 18388c2ecf20Sopenharmony_ci 18398c2ecf20Sopenharmony_ci header_len = tcp_established_options(sk, NULL, &opts, &md5) + 18408c2ecf20Sopenharmony_ci sizeof(struct tcphdr); 18418c2ecf20Sopenharmony_ci /* The mss_cache is sized based on tp->tcp_header_len, which assumes 18428c2ecf20Sopenharmony_ci * some common options. If this is an odd packet (because we have SACK 18438c2ecf20Sopenharmony_ci * blocks etc) then our calculated header_len will be different, and 18448c2ecf20Sopenharmony_ci * we have to adjust mss_now correspondingly */ 18458c2ecf20Sopenharmony_ci if (header_len != tp->tcp_header_len) { 18468c2ecf20Sopenharmony_ci int delta = (int) header_len - tp->tcp_header_len; 18478c2ecf20Sopenharmony_ci mss_now -= delta; 18488c2ecf20Sopenharmony_ci } 18498c2ecf20Sopenharmony_ci 18508c2ecf20Sopenharmony_ci return mss_now; 18518c2ecf20Sopenharmony_ci} 18528c2ecf20Sopenharmony_ci 18538c2ecf20Sopenharmony_ci/* RFC2861, slow part. Adjust cwnd, after it was not full during one rto. 18548c2ecf20Sopenharmony_ci * As additional protections, we do not touch cwnd in retransmission phases, 18558c2ecf20Sopenharmony_ci * and if application hit its sndbuf limit recently. 18568c2ecf20Sopenharmony_ci */ 18578c2ecf20Sopenharmony_cistatic void tcp_cwnd_application_limited(struct sock *sk) 18588c2ecf20Sopenharmony_ci{ 18598c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 18608c2ecf20Sopenharmony_ci 18618c2ecf20Sopenharmony_ci if (inet_csk(sk)->icsk_ca_state == TCP_CA_Open && 18628c2ecf20Sopenharmony_ci sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) { 18638c2ecf20Sopenharmony_ci /* Limited by application or receiver window. */ 18648c2ecf20Sopenharmony_ci u32 init_win = tcp_init_cwnd(tp, __sk_dst_get(sk)); 18658c2ecf20Sopenharmony_ci u32 win_used = max(tp->snd_cwnd_used, init_win); 18668c2ecf20Sopenharmony_ci if (win_used < tp->snd_cwnd) { 18678c2ecf20Sopenharmony_ci tp->snd_ssthresh = tcp_current_ssthresh(sk); 18688c2ecf20Sopenharmony_ci tp->snd_cwnd = (tp->snd_cwnd + win_used) >> 1; 18698c2ecf20Sopenharmony_ci } 18708c2ecf20Sopenharmony_ci tp->snd_cwnd_used = 0; 18718c2ecf20Sopenharmony_ci } 18728c2ecf20Sopenharmony_ci tp->snd_cwnd_stamp = tcp_jiffies32; 18738c2ecf20Sopenharmony_ci} 18748c2ecf20Sopenharmony_ci 18758c2ecf20Sopenharmony_cistatic void tcp_cwnd_validate(struct sock *sk, bool is_cwnd_limited) 18768c2ecf20Sopenharmony_ci{ 18778c2ecf20Sopenharmony_ci const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; 18788c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 18798c2ecf20Sopenharmony_ci 18808c2ecf20Sopenharmony_ci /* Track the strongest available signal of the degree to which the cwnd 18818c2ecf20Sopenharmony_ci * is fully utilized. If cwnd-limited then remember that fact for the 18828c2ecf20Sopenharmony_ci * current window. If not cwnd-limited then track the maximum number of 18838c2ecf20Sopenharmony_ci * outstanding packets in the current window. (If cwnd-limited then we 18848c2ecf20Sopenharmony_ci * chose to not update tp->max_packets_out to avoid an extra else 18858c2ecf20Sopenharmony_ci * clause with no functional impact.) 18868c2ecf20Sopenharmony_ci */ 18878c2ecf20Sopenharmony_ci if (!before(tp->snd_una, tp->cwnd_usage_seq) || 18888c2ecf20Sopenharmony_ci is_cwnd_limited || 18898c2ecf20Sopenharmony_ci (!tp->is_cwnd_limited && 18908c2ecf20Sopenharmony_ci tp->packets_out > tp->max_packets_out)) { 18918c2ecf20Sopenharmony_ci tp->is_cwnd_limited = is_cwnd_limited; 18928c2ecf20Sopenharmony_ci tp->max_packets_out = tp->packets_out; 18938c2ecf20Sopenharmony_ci tp->cwnd_usage_seq = tp->snd_nxt; 18948c2ecf20Sopenharmony_ci } 18958c2ecf20Sopenharmony_ci 18968c2ecf20Sopenharmony_ci if (tcp_is_cwnd_limited(sk)) { 18978c2ecf20Sopenharmony_ci /* Network is feed fully. */ 18988c2ecf20Sopenharmony_ci tp->snd_cwnd_used = 0; 18998c2ecf20Sopenharmony_ci tp->snd_cwnd_stamp = tcp_jiffies32; 19008c2ecf20Sopenharmony_ci } else { 19018c2ecf20Sopenharmony_ci /* Network starves. */ 19028c2ecf20Sopenharmony_ci if (tp->packets_out > tp->snd_cwnd_used) 19038c2ecf20Sopenharmony_ci tp->snd_cwnd_used = tp->packets_out; 19048c2ecf20Sopenharmony_ci 19058c2ecf20Sopenharmony_ci if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_slow_start_after_idle) && 19068c2ecf20Sopenharmony_ci (s32)(tcp_jiffies32 - tp->snd_cwnd_stamp) >= inet_csk(sk)->icsk_rto && 19078c2ecf20Sopenharmony_ci !ca_ops->cong_control) 19088c2ecf20Sopenharmony_ci tcp_cwnd_application_limited(sk); 19098c2ecf20Sopenharmony_ci 19108c2ecf20Sopenharmony_ci /* The following conditions together indicate the starvation 19118c2ecf20Sopenharmony_ci * is caused by insufficient sender buffer: 19128c2ecf20Sopenharmony_ci * 1) just sent some data (see tcp_write_xmit) 19138c2ecf20Sopenharmony_ci * 2) not cwnd limited (this else condition) 19148c2ecf20Sopenharmony_ci * 3) no more data to send (tcp_write_queue_empty()) 19158c2ecf20Sopenharmony_ci * 4) application is hitting buffer limit (SOCK_NOSPACE) 19168c2ecf20Sopenharmony_ci */ 19178c2ecf20Sopenharmony_ci if (tcp_write_queue_empty(sk) && sk->sk_socket && 19188c2ecf20Sopenharmony_ci test_bit(SOCK_NOSPACE, &sk->sk_socket->flags) && 19198c2ecf20Sopenharmony_ci (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)) 19208c2ecf20Sopenharmony_ci tcp_chrono_start(sk, TCP_CHRONO_SNDBUF_LIMITED); 19218c2ecf20Sopenharmony_ci } 19228c2ecf20Sopenharmony_ci} 19238c2ecf20Sopenharmony_ci 19248c2ecf20Sopenharmony_ci/* Minshall's variant of the Nagle send check. */ 19258c2ecf20Sopenharmony_cistatic bool tcp_minshall_check(const struct tcp_sock *tp) 19268c2ecf20Sopenharmony_ci{ 19278c2ecf20Sopenharmony_ci return after(tp->snd_sml, tp->snd_una) && 19288c2ecf20Sopenharmony_ci !after(tp->snd_sml, tp->snd_nxt); 19298c2ecf20Sopenharmony_ci} 19308c2ecf20Sopenharmony_ci 19318c2ecf20Sopenharmony_ci/* Update snd_sml if this skb is under mss 19328c2ecf20Sopenharmony_ci * Note that a TSO packet might end with a sub-mss segment 19338c2ecf20Sopenharmony_ci * The test is really : 19348c2ecf20Sopenharmony_ci * if ((skb->len % mss) != 0) 19358c2ecf20Sopenharmony_ci * tp->snd_sml = TCP_SKB_CB(skb)->end_seq; 19368c2ecf20Sopenharmony_ci * But we can avoid doing the divide again given we already have 19378c2ecf20Sopenharmony_ci * skb_pcount = skb->len / mss_now 19388c2ecf20Sopenharmony_ci */ 19398c2ecf20Sopenharmony_cistatic void tcp_minshall_update(struct tcp_sock *tp, unsigned int mss_now, 19408c2ecf20Sopenharmony_ci const struct sk_buff *skb) 19418c2ecf20Sopenharmony_ci{ 19428c2ecf20Sopenharmony_ci if (skb->len < tcp_skb_pcount(skb) * mss_now) 19438c2ecf20Sopenharmony_ci tp->snd_sml = TCP_SKB_CB(skb)->end_seq; 19448c2ecf20Sopenharmony_ci} 19458c2ecf20Sopenharmony_ci 19468c2ecf20Sopenharmony_ci/* Return false, if packet can be sent now without violation Nagle's rules: 19478c2ecf20Sopenharmony_ci * 1. It is full sized. (provided by caller in %partial bool) 19488c2ecf20Sopenharmony_ci * 2. Or it contains FIN. (already checked by caller) 19498c2ecf20Sopenharmony_ci * 3. Or TCP_CORK is not set, and TCP_NODELAY is set. 19508c2ecf20Sopenharmony_ci * 4. Or TCP_CORK is not set, and all sent packets are ACKed. 19518c2ecf20Sopenharmony_ci * With Minshall's modification: all sent small packets are ACKed. 19528c2ecf20Sopenharmony_ci */ 19538c2ecf20Sopenharmony_cistatic bool tcp_nagle_check(bool partial, const struct tcp_sock *tp, 19548c2ecf20Sopenharmony_ci int nonagle) 19558c2ecf20Sopenharmony_ci{ 19568c2ecf20Sopenharmony_ci return partial && 19578c2ecf20Sopenharmony_ci ((nonagle & TCP_NAGLE_CORK) || 19588c2ecf20Sopenharmony_ci (!nonagle && tp->packets_out && tcp_minshall_check(tp))); 19598c2ecf20Sopenharmony_ci} 19608c2ecf20Sopenharmony_ci 19618c2ecf20Sopenharmony_ci/* Return how many segs we'd like on a TSO packet, 19628c2ecf20Sopenharmony_ci * to send one TSO packet per ms 19638c2ecf20Sopenharmony_ci */ 19648c2ecf20Sopenharmony_cistatic u32 tcp_tso_autosize(const struct sock *sk, unsigned int mss_now, 19658c2ecf20Sopenharmony_ci int min_tso_segs) 19668c2ecf20Sopenharmony_ci{ 19678c2ecf20Sopenharmony_ci u32 bytes, segs; 19688c2ecf20Sopenharmony_ci 19698c2ecf20Sopenharmony_ci bytes = min_t(unsigned long, 19708c2ecf20Sopenharmony_ci sk->sk_pacing_rate >> READ_ONCE(sk->sk_pacing_shift), 19718c2ecf20Sopenharmony_ci sk->sk_gso_max_size - 1 - MAX_TCP_HEADER); 19728c2ecf20Sopenharmony_ci 19738c2ecf20Sopenharmony_ci /* Goal is to send at least one packet per ms, 19748c2ecf20Sopenharmony_ci * not one big TSO packet every 100 ms. 19758c2ecf20Sopenharmony_ci * This preserves ACK clocking and is consistent 19768c2ecf20Sopenharmony_ci * with tcp_tso_should_defer() heuristic. 19778c2ecf20Sopenharmony_ci */ 19788c2ecf20Sopenharmony_ci segs = max_t(u32, bytes / mss_now, min_tso_segs); 19798c2ecf20Sopenharmony_ci 19808c2ecf20Sopenharmony_ci return segs; 19818c2ecf20Sopenharmony_ci} 19828c2ecf20Sopenharmony_ci 19838c2ecf20Sopenharmony_ci/* Return the number of segments we want in the skb we are transmitting. 19848c2ecf20Sopenharmony_ci * See if congestion control module wants to decide; otherwise, autosize. 19858c2ecf20Sopenharmony_ci */ 19868c2ecf20Sopenharmony_cistatic u32 tcp_tso_segs(struct sock *sk, unsigned int mss_now) 19878c2ecf20Sopenharmony_ci{ 19888c2ecf20Sopenharmony_ci const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops; 19898c2ecf20Sopenharmony_ci u32 min_tso, tso_segs; 19908c2ecf20Sopenharmony_ci 19918c2ecf20Sopenharmony_ci min_tso = ca_ops->min_tso_segs ? 19928c2ecf20Sopenharmony_ci ca_ops->min_tso_segs(sk) : 19938c2ecf20Sopenharmony_ci READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_min_tso_segs); 19948c2ecf20Sopenharmony_ci 19958c2ecf20Sopenharmony_ci tso_segs = tcp_tso_autosize(sk, mss_now, min_tso); 19968c2ecf20Sopenharmony_ci return min_t(u32, tso_segs, sk->sk_gso_max_segs); 19978c2ecf20Sopenharmony_ci} 19988c2ecf20Sopenharmony_ci 19998c2ecf20Sopenharmony_ci/* Returns the portion of skb which can be sent right away */ 20008c2ecf20Sopenharmony_cistatic unsigned int tcp_mss_split_point(const struct sock *sk, 20018c2ecf20Sopenharmony_ci const struct sk_buff *skb, 20028c2ecf20Sopenharmony_ci unsigned int mss_now, 20038c2ecf20Sopenharmony_ci unsigned int max_segs, 20048c2ecf20Sopenharmony_ci int nonagle) 20058c2ecf20Sopenharmony_ci{ 20068c2ecf20Sopenharmony_ci const struct tcp_sock *tp = tcp_sk(sk); 20078c2ecf20Sopenharmony_ci u32 partial, needed, window, max_len; 20088c2ecf20Sopenharmony_ci 20098c2ecf20Sopenharmony_ci window = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; 20108c2ecf20Sopenharmony_ci max_len = mss_now * max_segs; 20118c2ecf20Sopenharmony_ci 20128c2ecf20Sopenharmony_ci if (likely(max_len <= window && skb != tcp_write_queue_tail(sk))) 20138c2ecf20Sopenharmony_ci return max_len; 20148c2ecf20Sopenharmony_ci 20158c2ecf20Sopenharmony_ci needed = min(skb->len, window); 20168c2ecf20Sopenharmony_ci 20178c2ecf20Sopenharmony_ci if (max_len <= needed) 20188c2ecf20Sopenharmony_ci return max_len; 20198c2ecf20Sopenharmony_ci 20208c2ecf20Sopenharmony_ci partial = needed % mss_now; 20218c2ecf20Sopenharmony_ci /* If last segment is not a full MSS, check if Nagle rules allow us 20228c2ecf20Sopenharmony_ci * to include this last segment in this skb. 20238c2ecf20Sopenharmony_ci * Otherwise, we'll split the skb at last MSS boundary 20248c2ecf20Sopenharmony_ci */ 20258c2ecf20Sopenharmony_ci if (tcp_nagle_check(partial != 0, tp, nonagle)) 20268c2ecf20Sopenharmony_ci return needed - partial; 20278c2ecf20Sopenharmony_ci 20288c2ecf20Sopenharmony_ci return needed; 20298c2ecf20Sopenharmony_ci} 20308c2ecf20Sopenharmony_ci 20318c2ecf20Sopenharmony_ci/* Can at least one segment of SKB be sent right now, according to the 20328c2ecf20Sopenharmony_ci * congestion window rules? If so, return how many segments are allowed. 20338c2ecf20Sopenharmony_ci */ 20348c2ecf20Sopenharmony_cistatic inline unsigned int tcp_cwnd_test(const struct tcp_sock *tp, 20358c2ecf20Sopenharmony_ci const struct sk_buff *skb) 20368c2ecf20Sopenharmony_ci{ 20378c2ecf20Sopenharmony_ci u32 in_flight, cwnd, halfcwnd; 20388c2ecf20Sopenharmony_ci 20398c2ecf20Sopenharmony_ci /* Don't be strict about the congestion window for the final FIN. */ 20408c2ecf20Sopenharmony_ci if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) && 20418c2ecf20Sopenharmony_ci tcp_skb_pcount(skb) == 1) 20428c2ecf20Sopenharmony_ci return 1; 20438c2ecf20Sopenharmony_ci 20448c2ecf20Sopenharmony_ci in_flight = tcp_packets_in_flight(tp); 20458c2ecf20Sopenharmony_ci cwnd = tp->snd_cwnd; 20468c2ecf20Sopenharmony_ci if (in_flight >= cwnd) 20478c2ecf20Sopenharmony_ci return 0; 20488c2ecf20Sopenharmony_ci 20498c2ecf20Sopenharmony_ci /* For better scheduling, ensure we have at least 20508c2ecf20Sopenharmony_ci * 2 GSO packets in flight. 20518c2ecf20Sopenharmony_ci */ 20528c2ecf20Sopenharmony_ci halfcwnd = max(cwnd >> 1, 1U); 20538c2ecf20Sopenharmony_ci return min(halfcwnd, cwnd - in_flight); 20548c2ecf20Sopenharmony_ci} 20558c2ecf20Sopenharmony_ci 20568c2ecf20Sopenharmony_ci/* Initialize TSO state of a skb. 20578c2ecf20Sopenharmony_ci * This must be invoked the first time we consider transmitting 20588c2ecf20Sopenharmony_ci * SKB onto the wire. 20598c2ecf20Sopenharmony_ci */ 20608c2ecf20Sopenharmony_cistatic int tcp_init_tso_segs(struct sk_buff *skb, unsigned int mss_now) 20618c2ecf20Sopenharmony_ci{ 20628c2ecf20Sopenharmony_ci int tso_segs = tcp_skb_pcount(skb); 20638c2ecf20Sopenharmony_ci 20648c2ecf20Sopenharmony_ci if (!tso_segs || (tso_segs > 1 && tcp_skb_mss(skb) != mss_now)) { 20658c2ecf20Sopenharmony_ci tcp_set_skb_tso_segs(skb, mss_now); 20668c2ecf20Sopenharmony_ci tso_segs = tcp_skb_pcount(skb); 20678c2ecf20Sopenharmony_ci } 20688c2ecf20Sopenharmony_ci return tso_segs; 20698c2ecf20Sopenharmony_ci} 20708c2ecf20Sopenharmony_ci 20718c2ecf20Sopenharmony_ci 20728c2ecf20Sopenharmony_ci/* Return true if the Nagle test allows this packet to be 20738c2ecf20Sopenharmony_ci * sent now. 20748c2ecf20Sopenharmony_ci */ 20758c2ecf20Sopenharmony_cistatic inline bool tcp_nagle_test(const struct tcp_sock *tp, const struct sk_buff *skb, 20768c2ecf20Sopenharmony_ci unsigned int cur_mss, int nonagle) 20778c2ecf20Sopenharmony_ci{ 20788c2ecf20Sopenharmony_ci /* Nagle rule does not apply to frames, which sit in the middle of the 20798c2ecf20Sopenharmony_ci * write_queue (they have no chances to get new data). 20808c2ecf20Sopenharmony_ci * 20818c2ecf20Sopenharmony_ci * This is implemented in the callers, where they modify the 'nonagle' 20828c2ecf20Sopenharmony_ci * argument based upon the location of SKB in the send queue. 20838c2ecf20Sopenharmony_ci */ 20848c2ecf20Sopenharmony_ci if (nonagle & TCP_NAGLE_PUSH) 20858c2ecf20Sopenharmony_ci return true; 20868c2ecf20Sopenharmony_ci 20878c2ecf20Sopenharmony_ci /* Don't use the nagle rule for urgent data (or for the final FIN). */ 20888c2ecf20Sopenharmony_ci if (tcp_urg_mode(tp) || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) 20898c2ecf20Sopenharmony_ci return true; 20908c2ecf20Sopenharmony_ci 20918c2ecf20Sopenharmony_ci if (!tcp_nagle_check(skb->len < cur_mss, tp, nonagle)) 20928c2ecf20Sopenharmony_ci return true; 20938c2ecf20Sopenharmony_ci 20948c2ecf20Sopenharmony_ci return false; 20958c2ecf20Sopenharmony_ci} 20968c2ecf20Sopenharmony_ci 20978c2ecf20Sopenharmony_ci/* Does at least the first segment of SKB fit into the send window? */ 20988c2ecf20Sopenharmony_cistatic bool tcp_snd_wnd_test(const struct tcp_sock *tp, 20998c2ecf20Sopenharmony_ci const struct sk_buff *skb, 21008c2ecf20Sopenharmony_ci unsigned int cur_mss) 21018c2ecf20Sopenharmony_ci{ 21028c2ecf20Sopenharmony_ci u32 end_seq = TCP_SKB_CB(skb)->end_seq; 21038c2ecf20Sopenharmony_ci 21048c2ecf20Sopenharmony_ci if (skb->len > cur_mss) 21058c2ecf20Sopenharmony_ci end_seq = TCP_SKB_CB(skb)->seq + cur_mss; 21068c2ecf20Sopenharmony_ci 21078c2ecf20Sopenharmony_ci return !after(end_seq, tcp_wnd_end(tp)); 21088c2ecf20Sopenharmony_ci} 21098c2ecf20Sopenharmony_ci 21108c2ecf20Sopenharmony_ci/* Trim TSO SKB to LEN bytes, put the remaining data into a new packet 21118c2ecf20Sopenharmony_ci * which is put after SKB on the list. It is very much like 21128c2ecf20Sopenharmony_ci * tcp_fragment() except that it may make several kinds of assumptions 21138c2ecf20Sopenharmony_ci * in order to speed up the splitting operation. In particular, we 21148c2ecf20Sopenharmony_ci * know that all the data is in scatter-gather pages, and that the 21158c2ecf20Sopenharmony_ci * packet has never been sent out before (and thus is not cloned). 21168c2ecf20Sopenharmony_ci */ 21178c2ecf20Sopenharmony_cistatic int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len, 21188c2ecf20Sopenharmony_ci unsigned int mss_now, gfp_t gfp) 21198c2ecf20Sopenharmony_ci{ 21208c2ecf20Sopenharmony_ci int nlen = skb->len - len; 21218c2ecf20Sopenharmony_ci struct sk_buff *buff; 21228c2ecf20Sopenharmony_ci u8 flags; 21238c2ecf20Sopenharmony_ci 21248c2ecf20Sopenharmony_ci /* All of a TSO frame must be composed of paged data. */ 21258c2ecf20Sopenharmony_ci if (skb->len != skb->data_len) 21268c2ecf20Sopenharmony_ci return tcp_fragment(sk, TCP_FRAG_IN_WRITE_QUEUE, 21278c2ecf20Sopenharmony_ci skb, len, mss_now, gfp); 21288c2ecf20Sopenharmony_ci 21298c2ecf20Sopenharmony_ci buff = sk_stream_alloc_skb(sk, 0, gfp, true); 21308c2ecf20Sopenharmony_ci if (unlikely(!buff)) 21318c2ecf20Sopenharmony_ci return -ENOMEM; 21328c2ecf20Sopenharmony_ci skb_copy_decrypted(buff, skb); 21338c2ecf20Sopenharmony_ci 21348c2ecf20Sopenharmony_ci sk_wmem_queued_add(sk, buff->truesize); 21358c2ecf20Sopenharmony_ci sk_mem_charge(sk, buff->truesize); 21368c2ecf20Sopenharmony_ci buff->truesize += nlen; 21378c2ecf20Sopenharmony_ci skb->truesize -= nlen; 21388c2ecf20Sopenharmony_ci 21398c2ecf20Sopenharmony_ci /* Correct the sequence numbers. */ 21408c2ecf20Sopenharmony_ci TCP_SKB_CB(buff)->seq = TCP_SKB_CB(skb)->seq + len; 21418c2ecf20Sopenharmony_ci TCP_SKB_CB(buff)->end_seq = TCP_SKB_CB(skb)->end_seq; 21428c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(buff)->seq; 21438c2ecf20Sopenharmony_ci 21448c2ecf20Sopenharmony_ci /* PSH and FIN should only be set in the second packet. */ 21458c2ecf20Sopenharmony_ci flags = TCP_SKB_CB(skb)->tcp_flags; 21468c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags = flags & ~(TCPHDR_FIN | TCPHDR_PSH); 21478c2ecf20Sopenharmony_ci TCP_SKB_CB(buff)->tcp_flags = flags; 21488c2ecf20Sopenharmony_ci 21498c2ecf20Sopenharmony_ci /* This packet was never sent out yet, so no SACK bits. */ 21508c2ecf20Sopenharmony_ci TCP_SKB_CB(buff)->sacked = 0; 21518c2ecf20Sopenharmony_ci 21528c2ecf20Sopenharmony_ci tcp_skb_fragment_eor(skb, buff); 21538c2ecf20Sopenharmony_ci 21548c2ecf20Sopenharmony_ci buff->ip_summed = CHECKSUM_PARTIAL; 21558c2ecf20Sopenharmony_ci skb_split(skb, buff, len); 21568c2ecf20Sopenharmony_ci tcp_fragment_tstamp(skb, buff); 21578c2ecf20Sopenharmony_ci 21588c2ecf20Sopenharmony_ci /* Fix up tso_factor for both original and new SKB. */ 21598c2ecf20Sopenharmony_ci tcp_set_skb_tso_segs(skb, mss_now); 21608c2ecf20Sopenharmony_ci tcp_set_skb_tso_segs(buff, mss_now); 21618c2ecf20Sopenharmony_ci 21628c2ecf20Sopenharmony_ci /* Link BUFF into the send queue. */ 21638c2ecf20Sopenharmony_ci __skb_header_release(buff); 21648c2ecf20Sopenharmony_ci tcp_insert_write_queue_after(skb, buff, sk, TCP_FRAG_IN_WRITE_QUEUE); 21658c2ecf20Sopenharmony_ci 21668c2ecf20Sopenharmony_ci return 0; 21678c2ecf20Sopenharmony_ci} 21688c2ecf20Sopenharmony_ci 21698c2ecf20Sopenharmony_ci/* Try to defer sending, if possible, in order to minimize the amount 21708c2ecf20Sopenharmony_ci * of TSO splitting we do. View it as a kind of TSO Nagle test. 21718c2ecf20Sopenharmony_ci * 21728c2ecf20Sopenharmony_ci * This algorithm is from John Heffner. 21738c2ecf20Sopenharmony_ci */ 21748c2ecf20Sopenharmony_cistatic bool tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb, 21758c2ecf20Sopenharmony_ci bool *is_cwnd_limited, 21768c2ecf20Sopenharmony_ci bool *is_rwnd_limited, 21778c2ecf20Sopenharmony_ci u32 max_segs) 21788c2ecf20Sopenharmony_ci{ 21798c2ecf20Sopenharmony_ci const struct inet_connection_sock *icsk = inet_csk(sk); 21808c2ecf20Sopenharmony_ci u32 send_win, cong_win, limit, in_flight; 21818c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 21828c2ecf20Sopenharmony_ci struct sk_buff *head; 21838c2ecf20Sopenharmony_ci int win_divisor; 21848c2ecf20Sopenharmony_ci s64 delta; 21858c2ecf20Sopenharmony_ci 21868c2ecf20Sopenharmony_ci if (icsk->icsk_ca_state >= TCP_CA_Recovery) 21878c2ecf20Sopenharmony_ci goto send_now; 21888c2ecf20Sopenharmony_ci 21898c2ecf20Sopenharmony_ci /* Avoid bursty behavior by allowing defer 21908c2ecf20Sopenharmony_ci * only if the last write was recent (1 ms). 21918c2ecf20Sopenharmony_ci * Note that tp->tcp_wstamp_ns can be in the future if we have 21928c2ecf20Sopenharmony_ci * packets waiting in a qdisc or device for EDT delivery. 21938c2ecf20Sopenharmony_ci */ 21948c2ecf20Sopenharmony_ci delta = tp->tcp_clock_cache - tp->tcp_wstamp_ns - NSEC_PER_MSEC; 21958c2ecf20Sopenharmony_ci if (delta > 0) 21968c2ecf20Sopenharmony_ci goto send_now; 21978c2ecf20Sopenharmony_ci 21988c2ecf20Sopenharmony_ci in_flight = tcp_packets_in_flight(tp); 21998c2ecf20Sopenharmony_ci 22008c2ecf20Sopenharmony_ci BUG_ON(tcp_skb_pcount(skb) <= 1); 22018c2ecf20Sopenharmony_ci BUG_ON(tp->snd_cwnd <= in_flight); 22028c2ecf20Sopenharmony_ci 22038c2ecf20Sopenharmony_ci send_win = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; 22048c2ecf20Sopenharmony_ci 22058c2ecf20Sopenharmony_ci /* From in_flight test above, we know that cwnd > in_flight. */ 22068c2ecf20Sopenharmony_ci cong_win = (tp->snd_cwnd - in_flight) * tp->mss_cache; 22078c2ecf20Sopenharmony_ci 22088c2ecf20Sopenharmony_ci limit = min(send_win, cong_win); 22098c2ecf20Sopenharmony_ci 22108c2ecf20Sopenharmony_ci /* If a full-sized TSO skb can be sent, do it. */ 22118c2ecf20Sopenharmony_ci if (limit >= max_segs * tp->mss_cache) 22128c2ecf20Sopenharmony_ci goto send_now; 22138c2ecf20Sopenharmony_ci 22148c2ecf20Sopenharmony_ci /* Middle in queue won't get any more data, full sendable already? */ 22158c2ecf20Sopenharmony_ci if ((skb != tcp_write_queue_tail(sk)) && (limit >= skb->len)) 22168c2ecf20Sopenharmony_ci goto send_now; 22178c2ecf20Sopenharmony_ci 22188c2ecf20Sopenharmony_ci win_divisor = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_tso_win_divisor); 22198c2ecf20Sopenharmony_ci if (win_divisor) { 22208c2ecf20Sopenharmony_ci u32 chunk = min(tp->snd_wnd, tp->snd_cwnd * tp->mss_cache); 22218c2ecf20Sopenharmony_ci 22228c2ecf20Sopenharmony_ci /* If at least some fraction of a window is available, 22238c2ecf20Sopenharmony_ci * just use it. 22248c2ecf20Sopenharmony_ci */ 22258c2ecf20Sopenharmony_ci chunk /= win_divisor; 22268c2ecf20Sopenharmony_ci if (limit >= chunk) 22278c2ecf20Sopenharmony_ci goto send_now; 22288c2ecf20Sopenharmony_ci } else { 22298c2ecf20Sopenharmony_ci /* Different approach, try not to defer past a single 22308c2ecf20Sopenharmony_ci * ACK. Receiver should ACK every other full sized 22318c2ecf20Sopenharmony_ci * frame, so if we have space for more than 3 frames 22328c2ecf20Sopenharmony_ci * then send now. 22338c2ecf20Sopenharmony_ci */ 22348c2ecf20Sopenharmony_ci if (limit > tcp_max_tso_deferred_mss(tp) * tp->mss_cache) 22358c2ecf20Sopenharmony_ci goto send_now; 22368c2ecf20Sopenharmony_ci } 22378c2ecf20Sopenharmony_ci 22388c2ecf20Sopenharmony_ci /* TODO : use tsorted_sent_queue ? */ 22398c2ecf20Sopenharmony_ci head = tcp_rtx_queue_head(sk); 22408c2ecf20Sopenharmony_ci if (!head) 22418c2ecf20Sopenharmony_ci goto send_now; 22428c2ecf20Sopenharmony_ci delta = tp->tcp_clock_cache - head->tstamp; 22438c2ecf20Sopenharmony_ci /* If next ACK is likely to come too late (half srtt), do not defer */ 22448c2ecf20Sopenharmony_ci if ((s64)(delta - (u64)NSEC_PER_USEC * (tp->srtt_us >> 4)) < 0) 22458c2ecf20Sopenharmony_ci goto send_now; 22468c2ecf20Sopenharmony_ci 22478c2ecf20Sopenharmony_ci /* Ok, it looks like it is advisable to defer. 22488c2ecf20Sopenharmony_ci * Three cases are tracked : 22498c2ecf20Sopenharmony_ci * 1) We are cwnd-limited 22508c2ecf20Sopenharmony_ci * 2) We are rwnd-limited 22518c2ecf20Sopenharmony_ci * 3) We are application limited. 22528c2ecf20Sopenharmony_ci */ 22538c2ecf20Sopenharmony_ci if (cong_win < send_win) { 22548c2ecf20Sopenharmony_ci if (cong_win <= skb->len) { 22558c2ecf20Sopenharmony_ci *is_cwnd_limited = true; 22568c2ecf20Sopenharmony_ci return true; 22578c2ecf20Sopenharmony_ci } 22588c2ecf20Sopenharmony_ci } else { 22598c2ecf20Sopenharmony_ci if (send_win <= skb->len) { 22608c2ecf20Sopenharmony_ci *is_rwnd_limited = true; 22618c2ecf20Sopenharmony_ci return true; 22628c2ecf20Sopenharmony_ci } 22638c2ecf20Sopenharmony_ci } 22648c2ecf20Sopenharmony_ci 22658c2ecf20Sopenharmony_ci /* If this packet won't get more data, do not wait. */ 22668c2ecf20Sopenharmony_ci if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) || 22678c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->eor) 22688c2ecf20Sopenharmony_ci goto send_now; 22698c2ecf20Sopenharmony_ci 22708c2ecf20Sopenharmony_ci return true; 22718c2ecf20Sopenharmony_ci 22728c2ecf20Sopenharmony_cisend_now: 22738c2ecf20Sopenharmony_ci return false; 22748c2ecf20Sopenharmony_ci} 22758c2ecf20Sopenharmony_ci 22768c2ecf20Sopenharmony_cistatic inline void tcp_mtu_check_reprobe(struct sock *sk) 22778c2ecf20Sopenharmony_ci{ 22788c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 22798c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 22808c2ecf20Sopenharmony_ci struct net *net = sock_net(sk); 22818c2ecf20Sopenharmony_ci u32 interval; 22828c2ecf20Sopenharmony_ci s32 delta; 22838c2ecf20Sopenharmony_ci 22848c2ecf20Sopenharmony_ci interval = READ_ONCE(net->ipv4.sysctl_tcp_probe_interval); 22858c2ecf20Sopenharmony_ci delta = tcp_jiffies32 - icsk->icsk_mtup.probe_timestamp; 22868c2ecf20Sopenharmony_ci if (unlikely(delta >= interval * HZ)) { 22878c2ecf20Sopenharmony_ci int mss = tcp_current_mss(sk); 22888c2ecf20Sopenharmony_ci 22898c2ecf20Sopenharmony_ci /* Update current search range */ 22908c2ecf20Sopenharmony_ci icsk->icsk_mtup.probe_size = 0; 22918c2ecf20Sopenharmony_ci icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp + 22928c2ecf20Sopenharmony_ci sizeof(struct tcphdr) + 22938c2ecf20Sopenharmony_ci icsk->icsk_af_ops->net_header_len; 22948c2ecf20Sopenharmony_ci icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss); 22958c2ecf20Sopenharmony_ci 22968c2ecf20Sopenharmony_ci /* Update probe time stamp */ 22978c2ecf20Sopenharmony_ci icsk->icsk_mtup.probe_timestamp = tcp_jiffies32; 22988c2ecf20Sopenharmony_ci } 22998c2ecf20Sopenharmony_ci} 23008c2ecf20Sopenharmony_ci 23018c2ecf20Sopenharmony_cistatic bool tcp_can_coalesce_send_queue_head(struct sock *sk, int len) 23028c2ecf20Sopenharmony_ci{ 23038c2ecf20Sopenharmony_ci struct sk_buff *skb, *next; 23048c2ecf20Sopenharmony_ci 23058c2ecf20Sopenharmony_ci skb = tcp_send_head(sk); 23068c2ecf20Sopenharmony_ci tcp_for_write_queue_from_safe(skb, next, sk) { 23078c2ecf20Sopenharmony_ci if (len <= skb->len) 23088c2ecf20Sopenharmony_ci break; 23098c2ecf20Sopenharmony_ci 23108c2ecf20Sopenharmony_ci if (unlikely(TCP_SKB_CB(skb)->eor) || tcp_has_tx_tstamp(skb)) 23118c2ecf20Sopenharmony_ci return false; 23128c2ecf20Sopenharmony_ci 23138c2ecf20Sopenharmony_ci len -= skb->len; 23148c2ecf20Sopenharmony_ci } 23158c2ecf20Sopenharmony_ci 23168c2ecf20Sopenharmony_ci return true; 23178c2ecf20Sopenharmony_ci} 23188c2ecf20Sopenharmony_ci 23198c2ecf20Sopenharmony_ci/* Create a new MTU probe if we are ready. 23208c2ecf20Sopenharmony_ci * MTU probe is regularly attempting to increase the path MTU by 23218c2ecf20Sopenharmony_ci * deliberately sending larger packets. This discovers routing 23228c2ecf20Sopenharmony_ci * changes resulting in larger path MTUs. 23238c2ecf20Sopenharmony_ci * 23248c2ecf20Sopenharmony_ci * Returns 0 if we should wait to probe (no cwnd available), 23258c2ecf20Sopenharmony_ci * 1 if a probe was sent, 23268c2ecf20Sopenharmony_ci * -1 otherwise 23278c2ecf20Sopenharmony_ci */ 23288c2ecf20Sopenharmony_cistatic int tcp_mtu_probe(struct sock *sk) 23298c2ecf20Sopenharmony_ci{ 23308c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 23318c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 23328c2ecf20Sopenharmony_ci struct sk_buff *skb, *nskb, *next; 23338c2ecf20Sopenharmony_ci struct net *net = sock_net(sk); 23348c2ecf20Sopenharmony_ci int probe_size; 23358c2ecf20Sopenharmony_ci int size_needed; 23368c2ecf20Sopenharmony_ci int copy, len; 23378c2ecf20Sopenharmony_ci int mss_now; 23388c2ecf20Sopenharmony_ci int interval; 23398c2ecf20Sopenharmony_ci 23408c2ecf20Sopenharmony_ci /* Not currently probing/verifying, 23418c2ecf20Sopenharmony_ci * not in recovery, 23428c2ecf20Sopenharmony_ci * have enough cwnd, and 23438c2ecf20Sopenharmony_ci * not SACKing (the variable headers throw things off) 23448c2ecf20Sopenharmony_ci */ 23458c2ecf20Sopenharmony_ci if (likely(!icsk->icsk_mtup.enabled || 23468c2ecf20Sopenharmony_ci icsk->icsk_mtup.probe_size || 23478c2ecf20Sopenharmony_ci inet_csk(sk)->icsk_ca_state != TCP_CA_Open || 23488c2ecf20Sopenharmony_ci tp->snd_cwnd < 11 || 23498c2ecf20Sopenharmony_ci tp->rx_opt.num_sacks || tp->rx_opt.dsack)) 23508c2ecf20Sopenharmony_ci return -1; 23518c2ecf20Sopenharmony_ci 23528c2ecf20Sopenharmony_ci /* Use binary search for probe_size between tcp_mss_base, 23538c2ecf20Sopenharmony_ci * and current mss_clamp. if (search_high - search_low) 23548c2ecf20Sopenharmony_ci * smaller than a threshold, backoff from probing. 23558c2ecf20Sopenharmony_ci */ 23568c2ecf20Sopenharmony_ci mss_now = tcp_current_mss(sk); 23578c2ecf20Sopenharmony_ci probe_size = tcp_mtu_to_mss(sk, (icsk->icsk_mtup.search_high + 23588c2ecf20Sopenharmony_ci icsk->icsk_mtup.search_low) >> 1); 23598c2ecf20Sopenharmony_ci size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache; 23608c2ecf20Sopenharmony_ci interval = icsk->icsk_mtup.search_high - icsk->icsk_mtup.search_low; 23618c2ecf20Sopenharmony_ci /* When misfortune happens, we are reprobing actively, 23628c2ecf20Sopenharmony_ci * and then reprobe timer has expired. We stick with current 23638c2ecf20Sopenharmony_ci * probing process by not resetting search range to its orignal. 23648c2ecf20Sopenharmony_ci */ 23658c2ecf20Sopenharmony_ci if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high) || 23668c2ecf20Sopenharmony_ci interval < READ_ONCE(net->ipv4.sysctl_tcp_probe_threshold)) { 23678c2ecf20Sopenharmony_ci /* Check whether enough time has elaplased for 23688c2ecf20Sopenharmony_ci * another round of probing. 23698c2ecf20Sopenharmony_ci */ 23708c2ecf20Sopenharmony_ci tcp_mtu_check_reprobe(sk); 23718c2ecf20Sopenharmony_ci return -1; 23728c2ecf20Sopenharmony_ci } 23738c2ecf20Sopenharmony_ci 23748c2ecf20Sopenharmony_ci /* Have enough data in the send queue to probe? */ 23758c2ecf20Sopenharmony_ci if (tp->write_seq - tp->snd_nxt < size_needed) 23768c2ecf20Sopenharmony_ci return -1; 23778c2ecf20Sopenharmony_ci 23788c2ecf20Sopenharmony_ci if (tp->snd_wnd < size_needed) 23798c2ecf20Sopenharmony_ci return -1; 23808c2ecf20Sopenharmony_ci if (after(tp->snd_nxt + size_needed, tcp_wnd_end(tp))) 23818c2ecf20Sopenharmony_ci return 0; 23828c2ecf20Sopenharmony_ci 23838c2ecf20Sopenharmony_ci /* Do we need to wait to drain cwnd? With none in flight, don't stall */ 23848c2ecf20Sopenharmony_ci if (tcp_packets_in_flight(tp) + 2 > tp->snd_cwnd) { 23858c2ecf20Sopenharmony_ci if (!tcp_packets_in_flight(tp)) 23868c2ecf20Sopenharmony_ci return -1; 23878c2ecf20Sopenharmony_ci else 23888c2ecf20Sopenharmony_ci return 0; 23898c2ecf20Sopenharmony_ci } 23908c2ecf20Sopenharmony_ci 23918c2ecf20Sopenharmony_ci if (!tcp_can_coalesce_send_queue_head(sk, probe_size)) 23928c2ecf20Sopenharmony_ci return -1; 23938c2ecf20Sopenharmony_ci 23948c2ecf20Sopenharmony_ci /* We're allowed to probe. Build it now. */ 23958c2ecf20Sopenharmony_ci nskb = sk_stream_alloc_skb(sk, probe_size, GFP_ATOMIC, false); 23968c2ecf20Sopenharmony_ci if (!nskb) 23978c2ecf20Sopenharmony_ci return -1; 23988c2ecf20Sopenharmony_ci sk_wmem_queued_add(sk, nskb->truesize); 23998c2ecf20Sopenharmony_ci sk_mem_charge(sk, nskb->truesize); 24008c2ecf20Sopenharmony_ci 24018c2ecf20Sopenharmony_ci skb = tcp_send_head(sk); 24028c2ecf20Sopenharmony_ci skb_copy_decrypted(nskb, skb); 24038c2ecf20Sopenharmony_ci 24048c2ecf20Sopenharmony_ci TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(skb)->seq; 24058c2ecf20Sopenharmony_ci TCP_SKB_CB(nskb)->end_seq = TCP_SKB_CB(skb)->seq + probe_size; 24068c2ecf20Sopenharmony_ci TCP_SKB_CB(nskb)->tcp_flags = TCPHDR_ACK; 24078c2ecf20Sopenharmony_ci TCP_SKB_CB(nskb)->sacked = 0; 24088c2ecf20Sopenharmony_ci nskb->csum = 0; 24098c2ecf20Sopenharmony_ci nskb->ip_summed = CHECKSUM_PARTIAL; 24108c2ecf20Sopenharmony_ci 24118c2ecf20Sopenharmony_ci tcp_insert_write_queue_before(nskb, skb, sk); 24128c2ecf20Sopenharmony_ci tcp_highest_sack_replace(sk, skb, nskb); 24138c2ecf20Sopenharmony_ci 24148c2ecf20Sopenharmony_ci len = 0; 24158c2ecf20Sopenharmony_ci tcp_for_write_queue_from_safe(skb, next, sk) { 24168c2ecf20Sopenharmony_ci copy = min_t(int, skb->len, probe_size - len); 24178c2ecf20Sopenharmony_ci skb_copy_bits(skb, 0, skb_put(nskb, copy), copy); 24188c2ecf20Sopenharmony_ci 24198c2ecf20Sopenharmony_ci if (skb->len <= copy) { 24208c2ecf20Sopenharmony_ci /* We've eaten all the data from this skb. 24218c2ecf20Sopenharmony_ci * Throw it away. */ 24228c2ecf20Sopenharmony_ci TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags; 24238c2ecf20Sopenharmony_ci /* If this is the last SKB we copy and eor is set 24248c2ecf20Sopenharmony_ci * we need to propagate it to the new skb. 24258c2ecf20Sopenharmony_ci */ 24268c2ecf20Sopenharmony_ci TCP_SKB_CB(nskb)->eor = TCP_SKB_CB(skb)->eor; 24278c2ecf20Sopenharmony_ci tcp_skb_collapse_tstamp(nskb, skb); 24288c2ecf20Sopenharmony_ci tcp_unlink_write_queue(skb, sk); 24298c2ecf20Sopenharmony_ci sk_wmem_free_skb(sk, skb); 24308c2ecf20Sopenharmony_ci } else { 24318c2ecf20Sopenharmony_ci TCP_SKB_CB(nskb)->tcp_flags |= TCP_SKB_CB(skb)->tcp_flags & 24328c2ecf20Sopenharmony_ci ~(TCPHDR_FIN|TCPHDR_PSH); 24338c2ecf20Sopenharmony_ci if (!skb_shinfo(skb)->nr_frags) { 24348c2ecf20Sopenharmony_ci skb_pull(skb, copy); 24358c2ecf20Sopenharmony_ci } else { 24368c2ecf20Sopenharmony_ci __pskb_trim_head(skb, copy); 24378c2ecf20Sopenharmony_ci tcp_set_skb_tso_segs(skb, mss_now); 24388c2ecf20Sopenharmony_ci } 24398c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->seq += copy; 24408c2ecf20Sopenharmony_ci } 24418c2ecf20Sopenharmony_ci 24428c2ecf20Sopenharmony_ci len += copy; 24438c2ecf20Sopenharmony_ci 24448c2ecf20Sopenharmony_ci if (len >= probe_size) 24458c2ecf20Sopenharmony_ci break; 24468c2ecf20Sopenharmony_ci } 24478c2ecf20Sopenharmony_ci tcp_init_tso_segs(nskb, nskb->len); 24488c2ecf20Sopenharmony_ci 24498c2ecf20Sopenharmony_ci /* We're ready to send. If this fails, the probe will 24508c2ecf20Sopenharmony_ci * be resegmented into mss-sized pieces by tcp_write_xmit(). 24518c2ecf20Sopenharmony_ci */ 24528c2ecf20Sopenharmony_ci if (!tcp_transmit_skb(sk, nskb, 1, GFP_ATOMIC)) { 24538c2ecf20Sopenharmony_ci /* Decrement cwnd here because we are sending 24548c2ecf20Sopenharmony_ci * effectively two packets. */ 24558c2ecf20Sopenharmony_ci tp->snd_cwnd--; 24568c2ecf20Sopenharmony_ci tcp_event_new_data_sent(sk, nskb); 24578c2ecf20Sopenharmony_ci 24588c2ecf20Sopenharmony_ci icsk->icsk_mtup.probe_size = tcp_mss_to_mtu(sk, nskb->len); 24598c2ecf20Sopenharmony_ci tp->mtu_probe.probe_seq_start = TCP_SKB_CB(nskb)->seq; 24608c2ecf20Sopenharmony_ci tp->mtu_probe.probe_seq_end = TCP_SKB_CB(nskb)->end_seq; 24618c2ecf20Sopenharmony_ci 24628c2ecf20Sopenharmony_ci return 1; 24638c2ecf20Sopenharmony_ci } 24648c2ecf20Sopenharmony_ci 24658c2ecf20Sopenharmony_ci return -1; 24668c2ecf20Sopenharmony_ci} 24678c2ecf20Sopenharmony_ci 24688c2ecf20Sopenharmony_cistatic bool tcp_pacing_check(struct sock *sk) 24698c2ecf20Sopenharmony_ci{ 24708c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 24718c2ecf20Sopenharmony_ci 24728c2ecf20Sopenharmony_ci if (!tcp_needs_internal_pacing(sk)) 24738c2ecf20Sopenharmony_ci return false; 24748c2ecf20Sopenharmony_ci 24758c2ecf20Sopenharmony_ci if (tp->tcp_wstamp_ns <= tp->tcp_clock_cache) 24768c2ecf20Sopenharmony_ci return false; 24778c2ecf20Sopenharmony_ci 24788c2ecf20Sopenharmony_ci if (!hrtimer_is_queued(&tp->pacing_timer)) { 24798c2ecf20Sopenharmony_ci hrtimer_start(&tp->pacing_timer, 24808c2ecf20Sopenharmony_ci ns_to_ktime(tp->tcp_wstamp_ns), 24818c2ecf20Sopenharmony_ci HRTIMER_MODE_ABS_PINNED_SOFT); 24828c2ecf20Sopenharmony_ci sock_hold(sk); 24838c2ecf20Sopenharmony_ci } 24848c2ecf20Sopenharmony_ci return true; 24858c2ecf20Sopenharmony_ci} 24868c2ecf20Sopenharmony_ci 24878c2ecf20Sopenharmony_cistatic bool tcp_rtx_queue_empty_or_single_skb(const struct sock *sk) 24888c2ecf20Sopenharmony_ci{ 24898c2ecf20Sopenharmony_ci const struct rb_node *node = sk->tcp_rtx_queue.rb_node; 24908c2ecf20Sopenharmony_ci 24918c2ecf20Sopenharmony_ci /* No skb in the rtx queue. */ 24928c2ecf20Sopenharmony_ci if (!node) 24938c2ecf20Sopenharmony_ci return true; 24948c2ecf20Sopenharmony_ci 24958c2ecf20Sopenharmony_ci /* Only one skb in rtx queue. */ 24968c2ecf20Sopenharmony_ci return !node->rb_left && !node->rb_right; 24978c2ecf20Sopenharmony_ci} 24988c2ecf20Sopenharmony_ci 24998c2ecf20Sopenharmony_ci/* TCP Small Queues : 25008c2ecf20Sopenharmony_ci * Control number of packets in qdisc/devices to two packets / or ~1 ms. 25018c2ecf20Sopenharmony_ci * (These limits are doubled for retransmits) 25028c2ecf20Sopenharmony_ci * This allows for : 25038c2ecf20Sopenharmony_ci * - better RTT estimation and ACK scheduling 25048c2ecf20Sopenharmony_ci * - faster recovery 25058c2ecf20Sopenharmony_ci * - high rates 25068c2ecf20Sopenharmony_ci * Alas, some drivers / subsystems require a fair amount 25078c2ecf20Sopenharmony_ci * of queued bytes to ensure line rate. 25088c2ecf20Sopenharmony_ci * One example is wifi aggregation (802.11 AMPDU) 25098c2ecf20Sopenharmony_ci */ 25108c2ecf20Sopenharmony_cistatic bool tcp_small_queue_check(struct sock *sk, const struct sk_buff *skb, 25118c2ecf20Sopenharmony_ci unsigned int factor) 25128c2ecf20Sopenharmony_ci{ 25138c2ecf20Sopenharmony_ci unsigned long limit; 25148c2ecf20Sopenharmony_ci 25158c2ecf20Sopenharmony_ci limit = max_t(unsigned long, 25168c2ecf20Sopenharmony_ci 2 * skb->truesize, 25178c2ecf20Sopenharmony_ci sk->sk_pacing_rate >> READ_ONCE(sk->sk_pacing_shift)); 25188c2ecf20Sopenharmony_ci if (sk->sk_pacing_status == SK_PACING_NONE) 25198c2ecf20Sopenharmony_ci limit = min_t(unsigned long, limit, 25208c2ecf20Sopenharmony_ci READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_limit_output_bytes)); 25218c2ecf20Sopenharmony_ci limit <<= factor; 25228c2ecf20Sopenharmony_ci 25238c2ecf20Sopenharmony_ci if (static_branch_unlikely(&tcp_tx_delay_enabled) && 25248c2ecf20Sopenharmony_ci tcp_sk(sk)->tcp_tx_delay) { 25258c2ecf20Sopenharmony_ci u64 extra_bytes = (u64)sk->sk_pacing_rate * tcp_sk(sk)->tcp_tx_delay; 25268c2ecf20Sopenharmony_ci 25278c2ecf20Sopenharmony_ci /* TSQ is based on skb truesize sum (sk_wmem_alloc), so we 25288c2ecf20Sopenharmony_ci * approximate our needs assuming an ~100% skb->truesize overhead. 25298c2ecf20Sopenharmony_ci * USEC_PER_SEC is approximated by 2^20. 25308c2ecf20Sopenharmony_ci * do_div(extra_bytes, USEC_PER_SEC/2) is replaced by a right shift. 25318c2ecf20Sopenharmony_ci */ 25328c2ecf20Sopenharmony_ci extra_bytes >>= (20 - 1); 25338c2ecf20Sopenharmony_ci limit += extra_bytes; 25348c2ecf20Sopenharmony_ci } 25358c2ecf20Sopenharmony_ci if (refcount_read(&sk->sk_wmem_alloc) > limit) { 25368c2ecf20Sopenharmony_ci /* Always send skb if rtx queue is empty or has one skb. 25378c2ecf20Sopenharmony_ci * No need to wait for TX completion to call us back, 25388c2ecf20Sopenharmony_ci * after softirq/tasklet schedule. 25398c2ecf20Sopenharmony_ci * This helps when TX completions are delayed too much. 25408c2ecf20Sopenharmony_ci */ 25418c2ecf20Sopenharmony_ci if (tcp_rtx_queue_empty_or_single_skb(sk)) 25428c2ecf20Sopenharmony_ci return false; 25438c2ecf20Sopenharmony_ci 25448c2ecf20Sopenharmony_ci set_bit(TSQ_THROTTLED, &sk->sk_tsq_flags); 25458c2ecf20Sopenharmony_ci /* It is possible TX completion already happened 25468c2ecf20Sopenharmony_ci * before we set TSQ_THROTTLED, so we must 25478c2ecf20Sopenharmony_ci * test again the condition. 25488c2ecf20Sopenharmony_ci */ 25498c2ecf20Sopenharmony_ci smp_mb__after_atomic(); 25508c2ecf20Sopenharmony_ci if (refcount_read(&sk->sk_wmem_alloc) > limit) 25518c2ecf20Sopenharmony_ci return true; 25528c2ecf20Sopenharmony_ci } 25538c2ecf20Sopenharmony_ci return false; 25548c2ecf20Sopenharmony_ci} 25558c2ecf20Sopenharmony_ci 25568c2ecf20Sopenharmony_cistatic void tcp_chrono_set(struct tcp_sock *tp, const enum tcp_chrono new) 25578c2ecf20Sopenharmony_ci{ 25588c2ecf20Sopenharmony_ci const u32 now = tcp_jiffies32; 25598c2ecf20Sopenharmony_ci enum tcp_chrono old = tp->chrono_type; 25608c2ecf20Sopenharmony_ci 25618c2ecf20Sopenharmony_ci if (old > TCP_CHRONO_UNSPEC) 25628c2ecf20Sopenharmony_ci tp->chrono_stat[old - 1] += now - tp->chrono_start; 25638c2ecf20Sopenharmony_ci tp->chrono_start = now; 25648c2ecf20Sopenharmony_ci tp->chrono_type = new; 25658c2ecf20Sopenharmony_ci} 25668c2ecf20Sopenharmony_ci 25678c2ecf20Sopenharmony_civoid tcp_chrono_start(struct sock *sk, const enum tcp_chrono type) 25688c2ecf20Sopenharmony_ci{ 25698c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 25708c2ecf20Sopenharmony_ci 25718c2ecf20Sopenharmony_ci /* If there are multiple conditions worthy of tracking in a 25728c2ecf20Sopenharmony_ci * chronograph then the highest priority enum takes precedence 25738c2ecf20Sopenharmony_ci * over the other conditions. So that if something "more interesting" 25748c2ecf20Sopenharmony_ci * starts happening, stop the previous chrono and start a new one. 25758c2ecf20Sopenharmony_ci */ 25768c2ecf20Sopenharmony_ci if (type > tp->chrono_type) 25778c2ecf20Sopenharmony_ci tcp_chrono_set(tp, type); 25788c2ecf20Sopenharmony_ci} 25798c2ecf20Sopenharmony_ci 25808c2ecf20Sopenharmony_civoid tcp_chrono_stop(struct sock *sk, const enum tcp_chrono type) 25818c2ecf20Sopenharmony_ci{ 25828c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 25838c2ecf20Sopenharmony_ci 25848c2ecf20Sopenharmony_ci 25858c2ecf20Sopenharmony_ci /* There are multiple conditions worthy of tracking in a 25868c2ecf20Sopenharmony_ci * chronograph, so that the highest priority enum takes 25878c2ecf20Sopenharmony_ci * precedence over the other conditions (see tcp_chrono_start). 25888c2ecf20Sopenharmony_ci * If a condition stops, we only stop chrono tracking if 25898c2ecf20Sopenharmony_ci * it's the "most interesting" or current chrono we are 25908c2ecf20Sopenharmony_ci * tracking and starts busy chrono if we have pending data. 25918c2ecf20Sopenharmony_ci */ 25928c2ecf20Sopenharmony_ci if (tcp_rtx_and_write_queues_empty(sk)) 25938c2ecf20Sopenharmony_ci tcp_chrono_set(tp, TCP_CHRONO_UNSPEC); 25948c2ecf20Sopenharmony_ci else if (type == tp->chrono_type) 25958c2ecf20Sopenharmony_ci tcp_chrono_set(tp, TCP_CHRONO_BUSY); 25968c2ecf20Sopenharmony_ci} 25978c2ecf20Sopenharmony_ci 25988c2ecf20Sopenharmony_ci/* This routine writes packets to the network. It advances the 25998c2ecf20Sopenharmony_ci * send_head. This happens as incoming acks open up the remote 26008c2ecf20Sopenharmony_ci * window for us. 26018c2ecf20Sopenharmony_ci * 26028c2ecf20Sopenharmony_ci * LARGESEND note: !tcp_urg_mode is overkill, only frames between 26038c2ecf20Sopenharmony_ci * snd_up-64k-mss .. snd_up cannot be large. However, taking into 26048c2ecf20Sopenharmony_ci * account rare use of URG, this is not a big flaw. 26058c2ecf20Sopenharmony_ci * 26068c2ecf20Sopenharmony_ci * Send at most one packet when push_one > 0. Temporarily ignore 26078c2ecf20Sopenharmony_ci * cwnd limit to force at most one packet out when push_one == 2. 26088c2ecf20Sopenharmony_ci 26098c2ecf20Sopenharmony_ci * Returns true, if no segments are in flight and we have queued segments, 26108c2ecf20Sopenharmony_ci * but cannot send anything now because of SWS or another problem. 26118c2ecf20Sopenharmony_ci */ 26128c2ecf20Sopenharmony_cistatic bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle, 26138c2ecf20Sopenharmony_ci int push_one, gfp_t gfp) 26148c2ecf20Sopenharmony_ci{ 26158c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 26168c2ecf20Sopenharmony_ci struct sk_buff *skb; 26178c2ecf20Sopenharmony_ci unsigned int tso_segs, sent_pkts; 26188c2ecf20Sopenharmony_ci int cwnd_quota; 26198c2ecf20Sopenharmony_ci int result; 26208c2ecf20Sopenharmony_ci bool is_cwnd_limited = false, is_rwnd_limited = false; 26218c2ecf20Sopenharmony_ci u32 max_segs; 26228c2ecf20Sopenharmony_ci 26238c2ecf20Sopenharmony_ci sent_pkts = 0; 26248c2ecf20Sopenharmony_ci 26258c2ecf20Sopenharmony_ci tcp_mstamp_refresh(tp); 26268c2ecf20Sopenharmony_ci if (!push_one) { 26278c2ecf20Sopenharmony_ci /* Do MTU probing. */ 26288c2ecf20Sopenharmony_ci result = tcp_mtu_probe(sk); 26298c2ecf20Sopenharmony_ci if (!result) { 26308c2ecf20Sopenharmony_ci return false; 26318c2ecf20Sopenharmony_ci } else if (result > 0) { 26328c2ecf20Sopenharmony_ci sent_pkts = 1; 26338c2ecf20Sopenharmony_ci } 26348c2ecf20Sopenharmony_ci } 26358c2ecf20Sopenharmony_ci 26368c2ecf20Sopenharmony_ci max_segs = tcp_tso_segs(sk, mss_now); 26378c2ecf20Sopenharmony_ci while ((skb = tcp_send_head(sk))) { 26388c2ecf20Sopenharmony_ci unsigned int limit; 26398c2ecf20Sopenharmony_ci 26408c2ecf20Sopenharmony_ci if (unlikely(tp->repair) && tp->repair_queue == TCP_SEND_QUEUE) { 26418c2ecf20Sopenharmony_ci /* "skb_mstamp_ns" is used as a start point for the retransmit timer */ 26428c2ecf20Sopenharmony_ci skb->skb_mstamp_ns = tp->tcp_wstamp_ns = tp->tcp_clock_cache; 26438c2ecf20Sopenharmony_ci list_move_tail(&skb->tcp_tsorted_anchor, &tp->tsorted_sent_queue); 26448c2ecf20Sopenharmony_ci tcp_init_tso_segs(skb, mss_now); 26458c2ecf20Sopenharmony_ci goto repair; /* Skip network transmission */ 26468c2ecf20Sopenharmony_ci } 26478c2ecf20Sopenharmony_ci 26488c2ecf20Sopenharmony_ci if (tcp_pacing_check(sk)) 26498c2ecf20Sopenharmony_ci break; 26508c2ecf20Sopenharmony_ci 26518c2ecf20Sopenharmony_ci tso_segs = tcp_init_tso_segs(skb, mss_now); 26528c2ecf20Sopenharmony_ci BUG_ON(!tso_segs); 26538c2ecf20Sopenharmony_ci 26548c2ecf20Sopenharmony_ci cwnd_quota = tcp_cwnd_test(tp, skb); 26558c2ecf20Sopenharmony_ci if (!cwnd_quota) { 26568c2ecf20Sopenharmony_ci if (push_one == 2) 26578c2ecf20Sopenharmony_ci /* Force out a loss probe pkt. */ 26588c2ecf20Sopenharmony_ci cwnd_quota = 1; 26598c2ecf20Sopenharmony_ci else 26608c2ecf20Sopenharmony_ci break; 26618c2ecf20Sopenharmony_ci } 26628c2ecf20Sopenharmony_ci 26638c2ecf20Sopenharmony_ci if (unlikely(!tcp_snd_wnd_test(tp, skb, mss_now))) { 26648c2ecf20Sopenharmony_ci is_rwnd_limited = true; 26658c2ecf20Sopenharmony_ci break; 26668c2ecf20Sopenharmony_ci } 26678c2ecf20Sopenharmony_ci 26688c2ecf20Sopenharmony_ci if (tso_segs == 1) { 26698c2ecf20Sopenharmony_ci if (unlikely(!tcp_nagle_test(tp, skb, mss_now, 26708c2ecf20Sopenharmony_ci (tcp_skb_is_last(sk, skb) ? 26718c2ecf20Sopenharmony_ci nonagle : TCP_NAGLE_PUSH)))) 26728c2ecf20Sopenharmony_ci break; 26738c2ecf20Sopenharmony_ci } else { 26748c2ecf20Sopenharmony_ci if (!push_one && 26758c2ecf20Sopenharmony_ci tcp_tso_should_defer(sk, skb, &is_cwnd_limited, 26768c2ecf20Sopenharmony_ci &is_rwnd_limited, max_segs)) 26778c2ecf20Sopenharmony_ci break; 26788c2ecf20Sopenharmony_ci } 26798c2ecf20Sopenharmony_ci 26808c2ecf20Sopenharmony_ci limit = mss_now; 26818c2ecf20Sopenharmony_ci if (tso_segs > 1 && !tcp_urg_mode(tp)) 26828c2ecf20Sopenharmony_ci limit = tcp_mss_split_point(sk, skb, mss_now, 26838c2ecf20Sopenharmony_ci min_t(unsigned int, 26848c2ecf20Sopenharmony_ci cwnd_quota, 26858c2ecf20Sopenharmony_ci max_segs), 26868c2ecf20Sopenharmony_ci nonagle); 26878c2ecf20Sopenharmony_ci 26888c2ecf20Sopenharmony_ci if (skb->len > limit && 26898c2ecf20Sopenharmony_ci unlikely(tso_fragment(sk, skb, limit, mss_now, gfp))) 26908c2ecf20Sopenharmony_ci break; 26918c2ecf20Sopenharmony_ci 26928c2ecf20Sopenharmony_ci if (tcp_small_queue_check(sk, skb, 0)) 26938c2ecf20Sopenharmony_ci break; 26948c2ecf20Sopenharmony_ci 26958c2ecf20Sopenharmony_ci /* Argh, we hit an empty skb(), presumably a thread 26968c2ecf20Sopenharmony_ci * is sleeping in sendmsg()/sk_stream_wait_memory(). 26978c2ecf20Sopenharmony_ci * We do not want to send a pure-ack packet and have 26988c2ecf20Sopenharmony_ci * a strange looking rtx queue with empty packet(s). 26998c2ecf20Sopenharmony_ci */ 27008c2ecf20Sopenharmony_ci if (TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq) 27018c2ecf20Sopenharmony_ci break; 27028c2ecf20Sopenharmony_ci 27038c2ecf20Sopenharmony_ci if (unlikely(tcp_transmit_skb(sk, skb, 1, gfp))) 27048c2ecf20Sopenharmony_ci break; 27058c2ecf20Sopenharmony_ci 27068c2ecf20Sopenharmony_cirepair: 27078c2ecf20Sopenharmony_ci /* Advance the send_head. This one is sent out. 27088c2ecf20Sopenharmony_ci * This call will increment packets_out. 27098c2ecf20Sopenharmony_ci */ 27108c2ecf20Sopenharmony_ci tcp_event_new_data_sent(sk, skb); 27118c2ecf20Sopenharmony_ci 27128c2ecf20Sopenharmony_ci tcp_minshall_update(tp, mss_now, skb); 27138c2ecf20Sopenharmony_ci sent_pkts += tcp_skb_pcount(skb); 27148c2ecf20Sopenharmony_ci 27158c2ecf20Sopenharmony_ci if (push_one) 27168c2ecf20Sopenharmony_ci break; 27178c2ecf20Sopenharmony_ci } 27188c2ecf20Sopenharmony_ci 27198c2ecf20Sopenharmony_ci if (is_rwnd_limited) 27208c2ecf20Sopenharmony_ci tcp_chrono_start(sk, TCP_CHRONO_RWND_LIMITED); 27218c2ecf20Sopenharmony_ci else 27228c2ecf20Sopenharmony_ci tcp_chrono_stop(sk, TCP_CHRONO_RWND_LIMITED); 27238c2ecf20Sopenharmony_ci 27248c2ecf20Sopenharmony_ci is_cwnd_limited |= (tcp_packets_in_flight(tp) >= tp->snd_cwnd); 27258c2ecf20Sopenharmony_ci if (likely(sent_pkts || is_cwnd_limited)) 27268c2ecf20Sopenharmony_ci tcp_cwnd_validate(sk, is_cwnd_limited); 27278c2ecf20Sopenharmony_ci 27288c2ecf20Sopenharmony_ci if (likely(sent_pkts)) { 27298c2ecf20Sopenharmony_ci if (tcp_in_cwnd_reduction(sk)) 27308c2ecf20Sopenharmony_ci tp->prr_out += sent_pkts; 27318c2ecf20Sopenharmony_ci 27328c2ecf20Sopenharmony_ci /* Send one loss probe per tail loss episode. */ 27338c2ecf20Sopenharmony_ci if (push_one != 2) 27348c2ecf20Sopenharmony_ci tcp_schedule_loss_probe(sk, false); 27358c2ecf20Sopenharmony_ci return false; 27368c2ecf20Sopenharmony_ci } 27378c2ecf20Sopenharmony_ci return !tp->packets_out && !tcp_write_queue_empty(sk); 27388c2ecf20Sopenharmony_ci} 27398c2ecf20Sopenharmony_ci 27408c2ecf20Sopenharmony_cibool tcp_schedule_loss_probe(struct sock *sk, bool advancing_rto) 27418c2ecf20Sopenharmony_ci{ 27428c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 27438c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 27448c2ecf20Sopenharmony_ci u32 timeout, timeout_us, rto_delta_us; 27458c2ecf20Sopenharmony_ci int early_retrans; 27468c2ecf20Sopenharmony_ci 27478c2ecf20Sopenharmony_ci /* Don't do any loss probe on a Fast Open connection before 3WHS 27488c2ecf20Sopenharmony_ci * finishes. 27498c2ecf20Sopenharmony_ci */ 27508c2ecf20Sopenharmony_ci if (rcu_access_pointer(tp->fastopen_rsk)) 27518c2ecf20Sopenharmony_ci return false; 27528c2ecf20Sopenharmony_ci 27538c2ecf20Sopenharmony_ci early_retrans = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_early_retrans); 27548c2ecf20Sopenharmony_ci /* Schedule a loss probe in 2*RTT for SACK capable connections 27558c2ecf20Sopenharmony_ci * not in loss recovery, that are either limited by cwnd or application. 27568c2ecf20Sopenharmony_ci */ 27578c2ecf20Sopenharmony_ci if ((early_retrans != 3 && early_retrans != 4) || 27588c2ecf20Sopenharmony_ci !tp->packets_out || !tcp_is_sack(tp) || 27598c2ecf20Sopenharmony_ci (icsk->icsk_ca_state != TCP_CA_Open && 27608c2ecf20Sopenharmony_ci icsk->icsk_ca_state != TCP_CA_CWR)) 27618c2ecf20Sopenharmony_ci return false; 27628c2ecf20Sopenharmony_ci 27638c2ecf20Sopenharmony_ci /* Probe timeout is 2*rtt. Add minimum RTO to account 27648c2ecf20Sopenharmony_ci * for delayed ack when there's one outstanding packet. If no RTT 27658c2ecf20Sopenharmony_ci * sample is available then probe after TCP_TIMEOUT_INIT. 27668c2ecf20Sopenharmony_ci */ 27678c2ecf20Sopenharmony_ci if (tp->srtt_us) { 27688c2ecf20Sopenharmony_ci timeout_us = tp->srtt_us >> 2; 27698c2ecf20Sopenharmony_ci if (tp->packets_out == 1) 27708c2ecf20Sopenharmony_ci timeout_us += tcp_rto_min_us(sk); 27718c2ecf20Sopenharmony_ci else 27728c2ecf20Sopenharmony_ci timeout_us += TCP_TIMEOUT_MIN_US; 27738c2ecf20Sopenharmony_ci timeout = usecs_to_jiffies(timeout_us); 27748c2ecf20Sopenharmony_ci } else { 27758c2ecf20Sopenharmony_ci timeout = TCP_TIMEOUT_INIT; 27768c2ecf20Sopenharmony_ci } 27778c2ecf20Sopenharmony_ci 27788c2ecf20Sopenharmony_ci /* If the RTO formula yields an earlier time, then use that time. */ 27798c2ecf20Sopenharmony_ci rto_delta_us = advancing_rto ? 27808c2ecf20Sopenharmony_ci jiffies_to_usecs(inet_csk(sk)->icsk_rto) : 27818c2ecf20Sopenharmony_ci tcp_rto_delta_us(sk); /* How far in future is RTO? */ 27828c2ecf20Sopenharmony_ci if (rto_delta_us > 0) 27838c2ecf20Sopenharmony_ci timeout = min_t(u32, timeout, usecs_to_jiffies(rto_delta_us)); 27848c2ecf20Sopenharmony_ci 27858c2ecf20Sopenharmony_ci tcp_reset_xmit_timer(sk, ICSK_TIME_LOSS_PROBE, timeout, TCP_RTO_MAX); 27868c2ecf20Sopenharmony_ci return true; 27878c2ecf20Sopenharmony_ci} 27888c2ecf20Sopenharmony_ci 27898c2ecf20Sopenharmony_ci/* Thanks to skb fast clones, we can detect if a prior transmit of 27908c2ecf20Sopenharmony_ci * a packet is still in a qdisc or driver queue. 27918c2ecf20Sopenharmony_ci * In this case, there is very little point doing a retransmit ! 27928c2ecf20Sopenharmony_ci */ 27938c2ecf20Sopenharmony_cistatic bool skb_still_in_host_queue(const struct sock *sk, 27948c2ecf20Sopenharmony_ci const struct sk_buff *skb) 27958c2ecf20Sopenharmony_ci{ 27968c2ecf20Sopenharmony_ci if (unlikely(skb_fclone_busy(sk, skb))) { 27978c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), 27988c2ecf20Sopenharmony_ci LINUX_MIB_TCPSPURIOUS_RTX_HOSTQUEUES); 27998c2ecf20Sopenharmony_ci return true; 28008c2ecf20Sopenharmony_ci } 28018c2ecf20Sopenharmony_ci return false; 28028c2ecf20Sopenharmony_ci} 28038c2ecf20Sopenharmony_ci 28048c2ecf20Sopenharmony_ci/* When probe timeout (PTO) fires, try send a new segment if possible, else 28058c2ecf20Sopenharmony_ci * retransmit the last segment. 28068c2ecf20Sopenharmony_ci */ 28078c2ecf20Sopenharmony_civoid tcp_send_loss_probe(struct sock *sk) 28088c2ecf20Sopenharmony_ci{ 28098c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 28108c2ecf20Sopenharmony_ci struct sk_buff *skb; 28118c2ecf20Sopenharmony_ci int pcount; 28128c2ecf20Sopenharmony_ci int mss = tcp_current_mss(sk); 28138c2ecf20Sopenharmony_ci 28148c2ecf20Sopenharmony_ci /* At most one outstanding TLP */ 28158c2ecf20Sopenharmony_ci if (tp->tlp_high_seq) 28168c2ecf20Sopenharmony_ci goto rearm_timer; 28178c2ecf20Sopenharmony_ci 28188c2ecf20Sopenharmony_ci tp->tlp_retrans = 0; 28198c2ecf20Sopenharmony_ci skb = tcp_send_head(sk); 28208c2ecf20Sopenharmony_ci if (skb && tcp_snd_wnd_test(tp, skb, mss)) { 28218c2ecf20Sopenharmony_ci pcount = tp->packets_out; 28228c2ecf20Sopenharmony_ci tcp_write_xmit(sk, mss, TCP_NAGLE_OFF, 2, GFP_ATOMIC); 28238c2ecf20Sopenharmony_ci if (tp->packets_out > pcount) 28248c2ecf20Sopenharmony_ci goto probe_sent; 28258c2ecf20Sopenharmony_ci goto rearm_timer; 28268c2ecf20Sopenharmony_ci } 28278c2ecf20Sopenharmony_ci skb = skb_rb_last(&sk->tcp_rtx_queue); 28288c2ecf20Sopenharmony_ci if (unlikely(!skb)) { 28298c2ecf20Sopenharmony_ci WARN_ONCE(tp->packets_out, 28308c2ecf20Sopenharmony_ci "invalid inflight: %u state %u cwnd %u mss %d\n", 28318c2ecf20Sopenharmony_ci tp->packets_out, sk->sk_state, tp->snd_cwnd, mss); 28328c2ecf20Sopenharmony_ci inet_csk(sk)->icsk_pending = 0; 28338c2ecf20Sopenharmony_ci return; 28348c2ecf20Sopenharmony_ci } 28358c2ecf20Sopenharmony_ci 28368c2ecf20Sopenharmony_ci if (skb_still_in_host_queue(sk, skb)) 28378c2ecf20Sopenharmony_ci goto rearm_timer; 28388c2ecf20Sopenharmony_ci 28398c2ecf20Sopenharmony_ci pcount = tcp_skb_pcount(skb); 28408c2ecf20Sopenharmony_ci if (WARN_ON(!pcount)) 28418c2ecf20Sopenharmony_ci goto rearm_timer; 28428c2ecf20Sopenharmony_ci 28438c2ecf20Sopenharmony_ci if ((pcount > 1) && (skb->len > (pcount - 1) * mss)) { 28448c2ecf20Sopenharmony_ci if (unlikely(tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb, 28458c2ecf20Sopenharmony_ci (pcount - 1) * mss, mss, 28468c2ecf20Sopenharmony_ci GFP_ATOMIC))) 28478c2ecf20Sopenharmony_ci goto rearm_timer; 28488c2ecf20Sopenharmony_ci skb = skb_rb_next(skb); 28498c2ecf20Sopenharmony_ci } 28508c2ecf20Sopenharmony_ci 28518c2ecf20Sopenharmony_ci if (WARN_ON(!skb || !tcp_skb_pcount(skb))) 28528c2ecf20Sopenharmony_ci goto rearm_timer; 28538c2ecf20Sopenharmony_ci 28548c2ecf20Sopenharmony_ci if (__tcp_retransmit_skb(sk, skb, 1)) 28558c2ecf20Sopenharmony_ci goto rearm_timer; 28568c2ecf20Sopenharmony_ci 28578c2ecf20Sopenharmony_ci tp->tlp_retrans = 1; 28588c2ecf20Sopenharmony_ci 28598c2ecf20Sopenharmony_ciprobe_sent: 28608c2ecf20Sopenharmony_ci /* Record snd_nxt for loss detection. */ 28618c2ecf20Sopenharmony_ci tp->tlp_high_seq = tp->snd_nxt; 28628c2ecf20Sopenharmony_ci 28638c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPLOSSPROBES); 28648c2ecf20Sopenharmony_ci /* Reset s.t. tcp_rearm_rto will restart timer from now */ 28658c2ecf20Sopenharmony_ci inet_csk(sk)->icsk_pending = 0; 28668c2ecf20Sopenharmony_cirearm_timer: 28678c2ecf20Sopenharmony_ci tcp_rearm_rto(sk); 28688c2ecf20Sopenharmony_ci} 28698c2ecf20Sopenharmony_ci 28708c2ecf20Sopenharmony_ci/* Push out any pending frames which were held back due to 28718c2ecf20Sopenharmony_ci * TCP_CORK or attempt at coalescing tiny packets. 28728c2ecf20Sopenharmony_ci * The socket must be locked by the caller. 28738c2ecf20Sopenharmony_ci */ 28748c2ecf20Sopenharmony_civoid __tcp_push_pending_frames(struct sock *sk, unsigned int cur_mss, 28758c2ecf20Sopenharmony_ci int nonagle) 28768c2ecf20Sopenharmony_ci{ 28778c2ecf20Sopenharmony_ci /* If we are closed, the bytes will have to remain here. 28788c2ecf20Sopenharmony_ci * In time closedown will finish, we empty the write queue and 28798c2ecf20Sopenharmony_ci * all will be happy. 28808c2ecf20Sopenharmony_ci */ 28818c2ecf20Sopenharmony_ci if (unlikely(sk->sk_state == TCP_CLOSE)) 28828c2ecf20Sopenharmony_ci return; 28838c2ecf20Sopenharmony_ci 28848c2ecf20Sopenharmony_ci if (tcp_write_xmit(sk, cur_mss, nonagle, 0, 28858c2ecf20Sopenharmony_ci sk_gfp_mask(sk, GFP_ATOMIC))) 28868c2ecf20Sopenharmony_ci tcp_check_probe_timer(sk); 28878c2ecf20Sopenharmony_ci} 28888c2ecf20Sopenharmony_ci 28898c2ecf20Sopenharmony_ci/* Send _single_ skb sitting at the send head. This function requires 28908c2ecf20Sopenharmony_ci * true push pending frames to setup probe timer etc. 28918c2ecf20Sopenharmony_ci */ 28928c2ecf20Sopenharmony_civoid tcp_push_one(struct sock *sk, unsigned int mss_now) 28938c2ecf20Sopenharmony_ci{ 28948c2ecf20Sopenharmony_ci struct sk_buff *skb = tcp_send_head(sk); 28958c2ecf20Sopenharmony_ci 28968c2ecf20Sopenharmony_ci BUG_ON(!skb || skb->len < mss_now); 28978c2ecf20Sopenharmony_ci 28988c2ecf20Sopenharmony_ci tcp_write_xmit(sk, mss_now, TCP_NAGLE_PUSH, 1, sk->sk_allocation); 28998c2ecf20Sopenharmony_ci} 29008c2ecf20Sopenharmony_ci 29018c2ecf20Sopenharmony_ci/* This function returns the amount that we can raise the 29028c2ecf20Sopenharmony_ci * usable window based on the following constraints 29038c2ecf20Sopenharmony_ci * 29048c2ecf20Sopenharmony_ci * 1. The window can never be shrunk once it is offered (RFC 793) 29058c2ecf20Sopenharmony_ci * 2. We limit memory per socket 29068c2ecf20Sopenharmony_ci * 29078c2ecf20Sopenharmony_ci * RFC 1122: 29088c2ecf20Sopenharmony_ci * "the suggested [SWS] avoidance algorithm for the receiver is to keep 29098c2ecf20Sopenharmony_ci * RECV.NEXT + RCV.WIN fixed until: 29108c2ecf20Sopenharmony_ci * RCV.BUFF - RCV.USER - RCV.WINDOW >= min(1/2 RCV.BUFF, MSS)" 29118c2ecf20Sopenharmony_ci * 29128c2ecf20Sopenharmony_ci * i.e. don't raise the right edge of the window until you can raise 29138c2ecf20Sopenharmony_ci * it at least MSS bytes. 29148c2ecf20Sopenharmony_ci * 29158c2ecf20Sopenharmony_ci * Unfortunately, the recommended algorithm breaks header prediction, 29168c2ecf20Sopenharmony_ci * since header prediction assumes th->window stays fixed. 29178c2ecf20Sopenharmony_ci * 29188c2ecf20Sopenharmony_ci * Strictly speaking, keeping th->window fixed violates the receiver 29198c2ecf20Sopenharmony_ci * side SWS prevention criteria. The problem is that under this rule 29208c2ecf20Sopenharmony_ci * a stream of single byte packets will cause the right side of the 29218c2ecf20Sopenharmony_ci * window to always advance by a single byte. 29228c2ecf20Sopenharmony_ci * 29238c2ecf20Sopenharmony_ci * Of course, if the sender implements sender side SWS prevention 29248c2ecf20Sopenharmony_ci * then this will not be a problem. 29258c2ecf20Sopenharmony_ci * 29268c2ecf20Sopenharmony_ci * BSD seems to make the following compromise: 29278c2ecf20Sopenharmony_ci * 29288c2ecf20Sopenharmony_ci * If the free space is less than the 1/4 of the maximum 29298c2ecf20Sopenharmony_ci * space available and the free space is less than 1/2 mss, 29308c2ecf20Sopenharmony_ci * then set the window to 0. 29318c2ecf20Sopenharmony_ci * [ Actually, bsd uses MSS and 1/4 of maximal _window_ ] 29328c2ecf20Sopenharmony_ci * Otherwise, just prevent the window from shrinking 29338c2ecf20Sopenharmony_ci * and from being larger than the largest representable value. 29348c2ecf20Sopenharmony_ci * 29358c2ecf20Sopenharmony_ci * This prevents incremental opening of the window in the regime 29368c2ecf20Sopenharmony_ci * where TCP is limited by the speed of the reader side taking 29378c2ecf20Sopenharmony_ci * data out of the TCP receive queue. It does nothing about 29388c2ecf20Sopenharmony_ci * those cases where the window is constrained on the sender side 29398c2ecf20Sopenharmony_ci * because the pipeline is full. 29408c2ecf20Sopenharmony_ci * 29418c2ecf20Sopenharmony_ci * BSD also seems to "accidentally" limit itself to windows that are a 29428c2ecf20Sopenharmony_ci * multiple of MSS, at least until the free space gets quite small. 29438c2ecf20Sopenharmony_ci * This would appear to be a side effect of the mbuf implementation. 29448c2ecf20Sopenharmony_ci * Combining these two algorithms results in the observed behavior 29458c2ecf20Sopenharmony_ci * of having a fixed window size at almost all times. 29468c2ecf20Sopenharmony_ci * 29478c2ecf20Sopenharmony_ci * Below we obtain similar behavior by forcing the offered window to 29488c2ecf20Sopenharmony_ci * a multiple of the mss when it is feasible to do so. 29498c2ecf20Sopenharmony_ci * 29508c2ecf20Sopenharmony_ci * Note, we don't "adjust" for TIMESTAMP or SACK option bytes. 29518c2ecf20Sopenharmony_ci * Regular options like TIMESTAMP are taken into account. 29528c2ecf20Sopenharmony_ci */ 29538c2ecf20Sopenharmony_ciu32 __tcp_select_window(struct sock *sk) 29548c2ecf20Sopenharmony_ci{ 29558c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 29568c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 29578c2ecf20Sopenharmony_ci /* MSS for the peer's data. Previous versions used mss_clamp 29588c2ecf20Sopenharmony_ci * here. I don't know if the value based on our guesses 29598c2ecf20Sopenharmony_ci * of peer's MSS is better for the performance. It's more correct 29608c2ecf20Sopenharmony_ci * but may be worse for the performance because of rcv_mss 29618c2ecf20Sopenharmony_ci * fluctuations. --SAW 1998/11/1 29628c2ecf20Sopenharmony_ci */ 29638c2ecf20Sopenharmony_ci int mss = icsk->icsk_ack.rcv_mss; 29648c2ecf20Sopenharmony_ci int free_space = tcp_space(sk); 29658c2ecf20Sopenharmony_ci int allowed_space = tcp_full_space(sk); 29668c2ecf20Sopenharmony_ci int full_space, window; 29678c2ecf20Sopenharmony_ci 29688c2ecf20Sopenharmony_ci if (sk_is_mptcp(sk)) 29698c2ecf20Sopenharmony_ci mptcp_space(sk, &free_space, &allowed_space); 29708c2ecf20Sopenharmony_ci 29718c2ecf20Sopenharmony_ci full_space = min_t(int, tp->window_clamp, allowed_space); 29728c2ecf20Sopenharmony_ci 29738c2ecf20Sopenharmony_ci if (unlikely(mss > full_space)) { 29748c2ecf20Sopenharmony_ci mss = full_space; 29758c2ecf20Sopenharmony_ci if (mss <= 0) 29768c2ecf20Sopenharmony_ci return 0; 29778c2ecf20Sopenharmony_ci } 29788c2ecf20Sopenharmony_ci if (free_space < (full_space >> 1)) { 29798c2ecf20Sopenharmony_ci icsk->icsk_ack.quick = 0; 29808c2ecf20Sopenharmony_ci 29818c2ecf20Sopenharmony_ci if (tcp_under_memory_pressure(sk)) 29828c2ecf20Sopenharmony_ci tp->rcv_ssthresh = min(tp->rcv_ssthresh, 29838c2ecf20Sopenharmony_ci 4U * tp->advmss); 29848c2ecf20Sopenharmony_ci 29858c2ecf20Sopenharmony_ci /* free_space might become our new window, make sure we don't 29868c2ecf20Sopenharmony_ci * increase it due to wscale. 29878c2ecf20Sopenharmony_ci */ 29888c2ecf20Sopenharmony_ci free_space = round_down(free_space, 1 << tp->rx_opt.rcv_wscale); 29898c2ecf20Sopenharmony_ci 29908c2ecf20Sopenharmony_ci /* if free space is less than mss estimate, or is below 1/16th 29918c2ecf20Sopenharmony_ci * of the maximum allowed, try to move to zero-window, else 29928c2ecf20Sopenharmony_ci * tcp_clamp_window() will grow rcv buf up to tcp_rmem[2], and 29938c2ecf20Sopenharmony_ci * new incoming data is dropped due to memory limits. 29948c2ecf20Sopenharmony_ci * With large window, mss test triggers way too late in order 29958c2ecf20Sopenharmony_ci * to announce zero window in time before rmem limit kicks in. 29968c2ecf20Sopenharmony_ci */ 29978c2ecf20Sopenharmony_ci if (free_space < (allowed_space >> 4) || free_space < mss) 29988c2ecf20Sopenharmony_ci return 0; 29998c2ecf20Sopenharmony_ci } 30008c2ecf20Sopenharmony_ci 30018c2ecf20Sopenharmony_ci if (free_space > tp->rcv_ssthresh) 30028c2ecf20Sopenharmony_ci free_space = tp->rcv_ssthresh; 30038c2ecf20Sopenharmony_ci 30048c2ecf20Sopenharmony_ci /* Don't do rounding if we are using window scaling, since the 30058c2ecf20Sopenharmony_ci * scaled window will not line up with the MSS boundary anyway. 30068c2ecf20Sopenharmony_ci */ 30078c2ecf20Sopenharmony_ci if (tp->rx_opt.rcv_wscale) { 30088c2ecf20Sopenharmony_ci window = free_space; 30098c2ecf20Sopenharmony_ci 30108c2ecf20Sopenharmony_ci /* Advertise enough space so that it won't get scaled away. 30118c2ecf20Sopenharmony_ci * Import case: prevent zero window announcement if 30128c2ecf20Sopenharmony_ci * 1<<rcv_wscale > mss. 30138c2ecf20Sopenharmony_ci */ 30148c2ecf20Sopenharmony_ci window = ALIGN(window, (1 << tp->rx_opt.rcv_wscale)); 30158c2ecf20Sopenharmony_ci } else { 30168c2ecf20Sopenharmony_ci window = tp->rcv_wnd; 30178c2ecf20Sopenharmony_ci /* Get the largest window that is a nice multiple of mss. 30188c2ecf20Sopenharmony_ci * Window clamp already applied above. 30198c2ecf20Sopenharmony_ci * If our current window offering is within 1 mss of the 30208c2ecf20Sopenharmony_ci * free space we just keep it. This prevents the divide 30218c2ecf20Sopenharmony_ci * and multiply from happening most of the time. 30228c2ecf20Sopenharmony_ci * We also don't do any window rounding when the free space 30238c2ecf20Sopenharmony_ci * is too small. 30248c2ecf20Sopenharmony_ci */ 30258c2ecf20Sopenharmony_ci if (window <= free_space - mss || window > free_space) 30268c2ecf20Sopenharmony_ci window = rounddown(free_space, mss); 30278c2ecf20Sopenharmony_ci else if (mss == full_space && 30288c2ecf20Sopenharmony_ci free_space > window + (full_space >> 1)) 30298c2ecf20Sopenharmony_ci window = free_space; 30308c2ecf20Sopenharmony_ci } 30318c2ecf20Sopenharmony_ci 30328c2ecf20Sopenharmony_ci return window; 30338c2ecf20Sopenharmony_ci} 30348c2ecf20Sopenharmony_ci 30358c2ecf20Sopenharmony_civoid tcp_skb_collapse_tstamp(struct sk_buff *skb, 30368c2ecf20Sopenharmony_ci const struct sk_buff *next_skb) 30378c2ecf20Sopenharmony_ci{ 30388c2ecf20Sopenharmony_ci if (unlikely(tcp_has_tx_tstamp(next_skb))) { 30398c2ecf20Sopenharmony_ci const struct skb_shared_info *next_shinfo = 30408c2ecf20Sopenharmony_ci skb_shinfo(next_skb); 30418c2ecf20Sopenharmony_ci struct skb_shared_info *shinfo = skb_shinfo(skb); 30428c2ecf20Sopenharmony_ci 30438c2ecf20Sopenharmony_ci shinfo->tx_flags |= next_shinfo->tx_flags & SKBTX_ANY_TSTAMP; 30448c2ecf20Sopenharmony_ci shinfo->tskey = next_shinfo->tskey; 30458c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->txstamp_ack |= 30468c2ecf20Sopenharmony_ci TCP_SKB_CB(next_skb)->txstamp_ack; 30478c2ecf20Sopenharmony_ci } 30488c2ecf20Sopenharmony_ci} 30498c2ecf20Sopenharmony_ci 30508c2ecf20Sopenharmony_ci/* Collapses two adjacent SKB's during retransmission. */ 30518c2ecf20Sopenharmony_cistatic bool tcp_collapse_retrans(struct sock *sk, struct sk_buff *skb) 30528c2ecf20Sopenharmony_ci{ 30538c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 30548c2ecf20Sopenharmony_ci struct sk_buff *next_skb = skb_rb_next(skb); 30558c2ecf20Sopenharmony_ci int next_skb_size; 30568c2ecf20Sopenharmony_ci 30578c2ecf20Sopenharmony_ci next_skb_size = next_skb->len; 30588c2ecf20Sopenharmony_ci 30598c2ecf20Sopenharmony_ci BUG_ON(tcp_skb_pcount(skb) != 1 || tcp_skb_pcount(next_skb) != 1); 30608c2ecf20Sopenharmony_ci 30618c2ecf20Sopenharmony_ci if (next_skb_size) { 30628c2ecf20Sopenharmony_ci if (next_skb_size <= skb_availroom(skb)) 30638c2ecf20Sopenharmony_ci skb_copy_bits(next_skb, 0, skb_put(skb, next_skb_size), 30648c2ecf20Sopenharmony_ci next_skb_size); 30658c2ecf20Sopenharmony_ci else if (!tcp_skb_shift(skb, next_skb, 1, next_skb_size)) 30668c2ecf20Sopenharmony_ci return false; 30678c2ecf20Sopenharmony_ci } 30688c2ecf20Sopenharmony_ci tcp_highest_sack_replace(sk, next_skb, skb); 30698c2ecf20Sopenharmony_ci 30708c2ecf20Sopenharmony_ci /* Update sequence range on original skb. */ 30718c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(next_skb)->end_seq; 30728c2ecf20Sopenharmony_ci 30738c2ecf20Sopenharmony_ci /* Merge over control information. This moves PSH/FIN etc. over */ 30748c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(next_skb)->tcp_flags; 30758c2ecf20Sopenharmony_ci 30768c2ecf20Sopenharmony_ci /* All done, get rid of second SKB and account for it so 30778c2ecf20Sopenharmony_ci * packet counting does not break. 30788c2ecf20Sopenharmony_ci */ 30798c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->sacked |= TCP_SKB_CB(next_skb)->sacked & TCPCB_EVER_RETRANS; 30808c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->eor = TCP_SKB_CB(next_skb)->eor; 30818c2ecf20Sopenharmony_ci 30828c2ecf20Sopenharmony_ci /* changed transmit queue under us so clear hints */ 30838c2ecf20Sopenharmony_ci tcp_clear_retrans_hints_partial(tp); 30848c2ecf20Sopenharmony_ci if (next_skb == tp->retransmit_skb_hint) 30858c2ecf20Sopenharmony_ci tp->retransmit_skb_hint = skb; 30868c2ecf20Sopenharmony_ci 30878c2ecf20Sopenharmony_ci tcp_adjust_pcount(sk, next_skb, tcp_skb_pcount(next_skb)); 30888c2ecf20Sopenharmony_ci 30898c2ecf20Sopenharmony_ci tcp_skb_collapse_tstamp(skb, next_skb); 30908c2ecf20Sopenharmony_ci 30918c2ecf20Sopenharmony_ci tcp_rtx_queue_unlink_and_free(next_skb, sk); 30928c2ecf20Sopenharmony_ci return true; 30938c2ecf20Sopenharmony_ci} 30948c2ecf20Sopenharmony_ci 30958c2ecf20Sopenharmony_ci/* Check if coalescing SKBs is legal. */ 30968c2ecf20Sopenharmony_cistatic bool tcp_can_collapse(const struct sock *sk, const struct sk_buff *skb) 30978c2ecf20Sopenharmony_ci{ 30988c2ecf20Sopenharmony_ci if (tcp_skb_pcount(skb) > 1) 30998c2ecf20Sopenharmony_ci return false; 31008c2ecf20Sopenharmony_ci if (skb_cloned(skb)) 31018c2ecf20Sopenharmony_ci return false; 31028c2ecf20Sopenharmony_ci /* Some heuristics for collapsing over SACK'd could be invented */ 31038c2ecf20Sopenharmony_ci if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) 31048c2ecf20Sopenharmony_ci return false; 31058c2ecf20Sopenharmony_ci 31068c2ecf20Sopenharmony_ci return true; 31078c2ecf20Sopenharmony_ci} 31088c2ecf20Sopenharmony_ci 31098c2ecf20Sopenharmony_ci/* Collapse packets in the retransmit queue to make to create 31108c2ecf20Sopenharmony_ci * less packets on the wire. This is only done on retransmission. 31118c2ecf20Sopenharmony_ci */ 31128c2ecf20Sopenharmony_cistatic void tcp_retrans_try_collapse(struct sock *sk, struct sk_buff *to, 31138c2ecf20Sopenharmony_ci int space) 31148c2ecf20Sopenharmony_ci{ 31158c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 31168c2ecf20Sopenharmony_ci struct sk_buff *skb = to, *tmp; 31178c2ecf20Sopenharmony_ci bool first = true; 31188c2ecf20Sopenharmony_ci 31198c2ecf20Sopenharmony_ci if (!READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_retrans_collapse)) 31208c2ecf20Sopenharmony_ci return; 31218c2ecf20Sopenharmony_ci if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN) 31228c2ecf20Sopenharmony_ci return; 31238c2ecf20Sopenharmony_ci 31248c2ecf20Sopenharmony_ci skb_rbtree_walk_from_safe(skb, tmp) { 31258c2ecf20Sopenharmony_ci if (!tcp_can_collapse(sk, skb)) 31268c2ecf20Sopenharmony_ci break; 31278c2ecf20Sopenharmony_ci 31288c2ecf20Sopenharmony_ci if (!tcp_skb_can_collapse(to, skb)) 31298c2ecf20Sopenharmony_ci break; 31308c2ecf20Sopenharmony_ci 31318c2ecf20Sopenharmony_ci space -= skb->len; 31328c2ecf20Sopenharmony_ci 31338c2ecf20Sopenharmony_ci if (first) { 31348c2ecf20Sopenharmony_ci first = false; 31358c2ecf20Sopenharmony_ci continue; 31368c2ecf20Sopenharmony_ci } 31378c2ecf20Sopenharmony_ci 31388c2ecf20Sopenharmony_ci if (space < 0) 31398c2ecf20Sopenharmony_ci break; 31408c2ecf20Sopenharmony_ci 31418c2ecf20Sopenharmony_ci if (after(TCP_SKB_CB(skb)->end_seq, tcp_wnd_end(tp))) 31428c2ecf20Sopenharmony_ci break; 31438c2ecf20Sopenharmony_ci 31448c2ecf20Sopenharmony_ci if (!tcp_collapse_retrans(sk, to)) 31458c2ecf20Sopenharmony_ci break; 31468c2ecf20Sopenharmony_ci } 31478c2ecf20Sopenharmony_ci} 31488c2ecf20Sopenharmony_ci 31498c2ecf20Sopenharmony_ci/* This retransmits one SKB. Policy decisions and retransmit queue 31508c2ecf20Sopenharmony_ci * state updates are done by the caller. Returns non-zero if an 31518c2ecf20Sopenharmony_ci * error occurred which prevented the send. 31528c2ecf20Sopenharmony_ci */ 31538c2ecf20Sopenharmony_ciint __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs) 31548c2ecf20Sopenharmony_ci{ 31558c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 31568c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 31578c2ecf20Sopenharmony_ci unsigned int cur_mss; 31588c2ecf20Sopenharmony_ci int diff, len, err; 31598c2ecf20Sopenharmony_ci int avail_wnd; 31608c2ecf20Sopenharmony_ci 31618c2ecf20Sopenharmony_ci /* Inconclusive MTU probe */ 31628c2ecf20Sopenharmony_ci if (icsk->icsk_mtup.probe_size) 31638c2ecf20Sopenharmony_ci icsk->icsk_mtup.probe_size = 0; 31648c2ecf20Sopenharmony_ci 31658c2ecf20Sopenharmony_ci /* Do not sent more than we queued. 1/4 is reserved for possible 31668c2ecf20Sopenharmony_ci * copying overhead: fragmentation, tunneling, mangling etc. 31678c2ecf20Sopenharmony_ci */ 31688c2ecf20Sopenharmony_ci if (refcount_read(&sk->sk_wmem_alloc) > 31698c2ecf20Sopenharmony_ci min_t(u32, sk->sk_wmem_queued + (sk->sk_wmem_queued >> 2), 31708c2ecf20Sopenharmony_ci sk->sk_sndbuf)) 31718c2ecf20Sopenharmony_ci return -EAGAIN; 31728c2ecf20Sopenharmony_ci 31738c2ecf20Sopenharmony_ci if (skb_still_in_host_queue(sk, skb)) 31748c2ecf20Sopenharmony_ci return -EBUSY; 31758c2ecf20Sopenharmony_ci 31768c2ecf20Sopenharmony_cistart: 31778c2ecf20Sopenharmony_ci if (before(TCP_SKB_CB(skb)->seq, tp->snd_una)) { 31788c2ecf20Sopenharmony_ci if (unlikely(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { 31798c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags &= ~TCPHDR_SYN; 31808c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->seq++; 31818c2ecf20Sopenharmony_ci goto start; 31828c2ecf20Sopenharmony_ci } 31838c2ecf20Sopenharmony_ci if (unlikely(before(TCP_SKB_CB(skb)->end_seq, tp->snd_una))) { 31848c2ecf20Sopenharmony_ci WARN_ON_ONCE(1); 31858c2ecf20Sopenharmony_ci return -EINVAL; 31868c2ecf20Sopenharmony_ci } 31878c2ecf20Sopenharmony_ci if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq)) 31888c2ecf20Sopenharmony_ci return -ENOMEM; 31898c2ecf20Sopenharmony_ci } 31908c2ecf20Sopenharmony_ci 31918c2ecf20Sopenharmony_ci if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk)) 31928c2ecf20Sopenharmony_ci return -EHOSTUNREACH; /* Routing failure or similar. */ 31938c2ecf20Sopenharmony_ci 31948c2ecf20Sopenharmony_ci cur_mss = tcp_current_mss(sk); 31958c2ecf20Sopenharmony_ci avail_wnd = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; 31968c2ecf20Sopenharmony_ci 31978c2ecf20Sopenharmony_ci /* If receiver has shrunk his window, and skb is out of 31988c2ecf20Sopenharmony_ci * new window, do not retransmit it. The exception is the 31998c2ecf20Sopenharmony_ci * case, when window is shrunk to zero. In this case 32008c2ecf20Sopenharmony_ci * our retransmit of one segment serves as a zero window probe. 32018c2ecf20Sopenharmony_ci */ 32028c2ecf20Sopenharmony_ci if (avail_wnd <= 0) { 32038c2ecf20Sopenharmony_ci if (TCP_SKB_CB(skb)->seq != tp->snd_una) 32048c2ecf20Sopenharmony_ci return -EAGAIN; 32058c2ecf20Sopenharmony_ci avail_wnd = cur_mss; 32068c2ecf20Sopenharmony_ci } 32078c2ecf20Sopenharmony_ci 32088c2ecf20Sopenharmony_ci len = cur_mss * segs; 32098c2ecf20Sopenharmony_ci if (len > avail_wnd) { 32108c2ecf20Sopenharmony_ci len = rounddown(avail_wnd, cur_mss); 32118c2ecf20Sopenharmony_ci if (!len) 32128c2ecf20Sopenharmony_ci len = avail_wnd; 32138c2ecf20Sopenharmony_ci } 32148c2ecf20Sopenharmony_ci if (skb->len > len) { 32158c2ecf20Sopenharmony_ci if (tcp_fragment(sk, TCP_FRAG_IN_RTX_QUEUE, skb, len, 32168c2ecf20Sopenharmony_ci cur_mss, GFP_ATOMIC)) 32178c2ecf20Sopenharmony_ci return -ENOMEM; /* We'll try again later. */ 32188c2ecf20Sopenharmony_ci } else { 32198c2ecf20Sopenharmony_ci if (skb_unclone(skb, GFP_ATOMIC)) 32208c2ecf20Sopenharmony_ci return -ENOMEM; 32218c2ecf20Sopenharmony_ci 32228c2ecf20Sopenharmony_ci diff = tcp_skb_pcount(skb); 32238c2ecf20Sopenharmony_ci tcp_set_skb_tso_segs(skb, cur_mss); 32248c2ecf20Sopenharmony_ci diff -= tcp_skb_pcount(skb); 32258c2ecf20Sopenharmony_ci if (diff) 32268c2ecf20Sopenharmony_ci tcp_adjust_pcount(sk, skb, diff); 32278c2ecf20Sopenharmony_ci avail_wnd = min_t(int, avail_wnd, cur_mss); 32288c2ecf20Sopenharmony_ci if (skb->len < avail_wnd) 32298c2ecf20Sopenharmony_ci tcp_retrans_try_collapse(sk, skb, avail_wnd); 32308c2ecf20Sopenharmony_ci } 32318c2ecf20Sopenharmony_ci 32328c2ecf20Sopenharmony_ci /* RFC3168, section 6.1.1.1. ECN fallback */ 32338c2ecf20Sopenharmony_ci if ((TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN_ECN) == TCPHDR_SYN_ECN) 32348c2ecf20Sopenharmony_ci tcp_ecn_clear_syn(sk, skb); 32358c2ecf20Sopenharmony_ci 32368c2ecf20Sopenharmony_ci /* Update global and local TCP statistics. */ 32378c2ecf20Sopenharmony_ci segs = tcp_skb_pcount(skb); 32388c2ecf20Sopenharmony_ci TCP_ADD_STATS(sock_net(sk), TCP_MIB_RETRANSSEGS, segs); 32398c2ecf20Sopenharmony_ci if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN) 32408c2ecf20Sopenharmony_ci __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNRETRANS); 32418c2ecf20Sopenharmony_ci tp->total_retrans += segs; 32428c2ecf20Sopenharmony_ci tp->bytes_retrans += skb->len; 32438c2ecf20Sopenharmony_ci 32448c2ecf20Sopenharmony_ci /* make sure skb->data is aligned on arches that require it 32458c2ecf20Sopenharmony_ci * and check if ack-trimming & collapsing extended the headroom 32468c2ecf20Sopenharmony_ci * beyond what csum_start can cover. 32478c2ecf20Sopenharmony_ci */ 32488c2ecf20Sopenharmony_ci if (unlikely((NET_IP_ALIGN && ((unsigned long)skb->data & 3)) || 32498c2ecf20Sopenharmony_ci skb_headroom(skb) >= 0xFFFF)) { 32508c2ecf20Sopenharmony_ci struct sk_buff *nskb; 32518c2ecf20Sopenharmony_ci 32528c2ecf20Sopenharmony_ci tcp_skb_tsorted_save(skb) { 32538c2ecf20Sopenharmony_ci nskb = __pskb_copy(skb, MAX_TCP_HEADER, GFP_ATOMIC); 32548c2ecf20Sopenharmony_ci if (nskb) { 32558c2ecf20Sopenharmony_ci nskb->dev = NULL; 32568c2ecf20Sopenharmony_ci err = tcp_transmit_skb(sk, nskb, 0, GFP_ATOMIC); 32578c2ecf20Sopenharmony_ci } else { 32588c2ecf20Sopenharmony_ci err = -ENOBUFS; 32598c2ecf20Sopenharmony_ci } 32608c2ecf20Sopenharmony_ci } tcp_skb_tsorted_restore(skb); 32618c2ecf20Sopenharmony_ci 32628c2ecf20Sopenharmony_ci if (!err) { 32638c2ecf20Sopenharmony_ci tcp_update_skb_after_send(sk, skb, tp->tcp_wstamp_ns); 32648c2ecf20Sopenharmony_ci tcp_rate_skb_sent(sk, skb); 32658c2ecf20Sopenharmony_ci } 32668c2ecf20Sopenharmony_ci } else { 32678c2ecf20Sopenharmony_ci err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC); 32688c2ecf20Sopenharmony_ci } 32698c2ecf20Sopenharmony_ci 32708c2ecf20Sopenharmony_ci /* To avoid taking spuriously low RTT samples based on a timestamp 32718c2ecf20Sopenharmony_ci * for a transmit that never happened, always mark EVER_RETRANS 32728c2ecf20Sopenharmony_ci */ 32738c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->sacked |= TCPCB_EVER_RETRANS; 32748c2ecf20Sopenharmony_ci 32758c2ecf20Sopenharmony_ci if (BPF_SOCK_OPS_TEST_FLAG(tp, BPF_SOCK_OPS_RETRANS_CB_FLAG)) 32768c2ecf20Sopenharmony_ci tcp_call_bpf_3arg(sk, BPF_SOCK_OPS_RETRANS_CB, 32778c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->seq, segs, err); 32788c2ecf20Sopenharmony_ci 32798c2ecf20Sopenharmony_ci if (likely(!err)) { 32808c2ecf20Sopenharmony_ci trace_tcp_retransmit_skb(sk, skb); 32818c2ecf20Sopenharmony_ci } else if (err != -EBUSY) { 32828c2ecf20Sopenharmony_ci NET_ADD_STATS(sock_net(sk), LINUX_MIB_TCPRETRANSFAIL, segs); 32838c2ecf20Sopenharmony_ci } 32848c2ecf20Sopenharmony_ci return err; 32858c2ecf20Sopenharmony_ci} 32868c2ecf20Sopenharmony_ci 32878c2ecf20Sopenharmony_ciint tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb, int segs) 32888c2ecf20Sopenharmony_ci{ 32898c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 32908c2ecf20Sopenharmony_ci int err = __tcp_retransmit_skb(sk, skb, segs); 32918c2ecf20Sopenharmony_ci 32928c2ecf20Sopenharmony_ci if (err == 0) { 32938c2ecf20Sopenharmony_ci#if FASTRETRANS_DEBUG > 0 32948c2ecf20Sopenharmony_ci if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) { 32958c2ecf20Sopenharmony_ci net_dbg_ratelimited("retrans_out leaked\n"); 32968c2ecf20Sopenharmony_ci } 32978c2ecf20Sopenharmony_ci#endif 32988c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->sacked |= TCPCB_RETRANS; 32998c2ecf20Sopenharmony_ci tp->retrans_out += tcp_skb_pcount(skb); 33008c2ecf20Sopenharmony_ci } 33018c2ecf20Sopenharmony_ci 33028c2ecf20Sopenharmony_ci /* Save stamp of the first (attempted) retransmit. */ 33038c2ecf20Sopenharmony_ci if (!tp->retrans_stamp) 33048c2ecf20Sopenharmony_ci tp->retrans_stamp = tcp_skb_timestamp(skb); 33058c2ecf20Sopenharmony_ci 33068c2ecf20Sopenharmony_ci if (tp->undo_retrans < 0) 33078c2ecf20Sopenharmony_ci tp->undo_retrans = 0; 33088c2ecf20Sopenharmony_ci tp->undo_retrans += tcp_skb_pcount(skb); 33098c2ecf20Sopenharmony_ci return err; 33108c2ecf20Sopenharmony_ci} 33118c2ecf20Sopenharmony_ci 33128c2ecf20Sopenharmony_ci/* This gets called after a retransmit timeout, and the initially 33138c2ecf20Sopenharmony_ci * retransmitted data is acknowledged. It tries to continue 33148c2ecf20Sopenharmony_ci * resending the rest of the retransmit queue, until either 33158c2ecf20Sopenharmony_ci * we've sent it all or the congestion window limit is reached. 33168c2ecf20Sopenharmony_ci */ 33178c2ecf20Sopenharmony_civoid tcp_xmit_retransmit_queue(struct sock *sk) 33188c2ecf20Sopenharmony_ci{ 33198c2ecf20Sopenharmony_ci const struct inet_connection_sock *icsk = inet_csk(sk); 33208c2ecf20Sopenharmony_ci struct sk_buff *skb, *rtx_head, *hole = NULL; 33218c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 33228c2ecf20Sopenharmony_ci bool rearm_timer = false; 33238c2ecf20Sopenharmony_ci u32 max_segs; 33248c2ecf20Sopenharmony_ci int mib_idx; 33258c2ecf20Sopenharmony_ci 33268c2ecf20Sopenharmony_ci if (!tp->packets_out) 33278c2ecf20Sopenharmony_ci return; 33288c2ecf20Sopenharmony_ci 33298c2ecf20Sopenharmony_ci rtx_head = tcp_rtx_queue_head(sk); 33308c2ecf20Sopenharmony_ci skb = tp->retransmit_skb_hint ?: rtx_head; 33318c2ecf20Sopenharmony_ci max_segs = tcp_tso_segs(sk, tcp_current_mss(sk)); 33328c2ecf20Sopenharmony_ci skb_rbtree_walk_from(skb) { 33338c2ecf20Sopenharmony_ci __u8 sacked; 33348c2ecf20Sopenharmony_ci int segs; 33358c2ecf20Sopenharmony_ci 33368c2ecf20Sopenharmony_ci if (tcp_pacing_check(sk)) 33378c2ecf20Sopenharmony_ci break; 33388c2ecf20Sopenharmony_ci 33398c2ecf20Sopenharmony_ci /* we could do better than to assign each time */ 33408c2ecf20Sopenharmony_ci if (!hole) 33418c2ecf20Sopenharmony_ci tp->retransmit_skb_hint = skb; 33428c2ecf20Sopenharmony_ci 33438c2ecf20Sopenharmony_ci segs = tp->snd_cwnd - tcp_packets_in_flight(tp); 33448c2ecf20Sopenharmony_ci if (segs <= 0) 33458c2ecf20Sopenharmony_ci break; 33468c2ecf20Sopenharmony_ci sacked = TCP_SKB_CB(skb)->sacked; 33478c2ecf20Sopenharmony_ci /* In case tcp_shift_skb_data() have aggregated large skbs, 33488c2ecf20Sopenharmony_ci * we need to make sure not sending too bigs TSO packets 33498c2ecf20Sopenharmony_ci */ 33508c2ecf20Sopenharmony_ci segs = min_t(int, segs, max_segs); 33518c2ecf20Sopenharmony_ci 33528c2ecf20Sopenharmony_ci if (tp->retrans_out >= tp->lost_out) { 33538c2ecf20Sopenharmony_ci break; 33548c2ecf20Sopenharmony_ci } else if (!(sacked & TCPCB_LOST)) { 33558c2ecf20Sopenharmony_ci if (!hole && !(sacked & (TCPCB_SACKED_RETRANS|TCPCB_SACKED_ACKED))) 33568c2ecf20Sopenharmony_ci hole = skb; 33578c2ecf20Sopenharmony_ci continue; 33588c2ecf20Sopenharmony_ci 33598c2ecf20Sopenharmony_ci } else { 33608c2ecf20Sopenharmony_ci if (icsk->icsk_ca_state != TCP_CA_Loss) 33618c2ecf20Sopenharmony_ci mib_idx = LINUX_MIB_TCPFASTRETRANS; 33628c2ecf20Sopenharmony_ci else 33638c2ecf20Sopenharmony_ci mib_idx = LINUX_MIB_TCPSLOWSTARTRETRANS; 33648c2ecf20Sopenharmony_ci } 33658c2ecf20Sopenharmony_ci 33668c2ecf20Sopenharmony_ci if (sacked & (TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS)) 33678c2ecf20Sopenharmony_ci continue; 33688c2ecf20Sopenharmony_ci 33698c2ecf20Sopenharmony_ci if (tcp_small_queue_check(sk, skb, 1)) 33708c2ecf20Sopenharmony_ci break; 33718c2ecf20Sopenharmony_ci 33728c2ecf20Sopenharmony_ci if (tcp_retransmit_skb(sk, skb, segs)) 33738c2ecf20Sopenharmony_ci break; 33748c2ecf20Sopenharmony_ci 33758c2ecf20Sopenharmony_ci NET_ADD_STATS(sock_net(sk), mib_idx, tcp_skb_pcount(skb)); 33768c2ecf20Sopenharmony_ci 33778c2ecf20Sopenharmony_ci if (tcp_in_cwnd_reduction(sk)) 33788c2ecf20Sopenharmony_ci tp->prr_out += tcp_skb_pcount(skb); 33798c2ecf20Sopenharmony_ci 33808c2ecf20Sopenharmony_ci if (skb == rtx_head && 33818c2ecf20Sopenharmony_ci icsk->icsk_pending != ICSK_TIME_REO_TIMEOUT) 33828c2ecf20Sopenharmony_ci rearm_timer = true; 33838c2ecf20Sopenharmony_ci 33848c2ecf20Sopenharmony_ci } 33858c2ecf20Sopenharmony_ci if (rearm_timer) 33868c2ecf20Sopenharmony_ci tcp_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 33878c2ecf20Sopenharmony_ci inet_csk(sk)->icsk_rto, 33888c2ecf20Sopenharmony_ci TCP_RTO_MAX); 33898c2ecf20Sopenharmony_ci} 33908c2ecf20Sopenharmony_ci 33918c2ecf20Sopenharmony_ci/* We allow to exceed memory limits for FIN packets to expedite 33928c2ecf20Sopenharmony_ci * connection tear down and (memory) recovery. 33938c2ecf20Sopenharmony_ci * Otherwise tcp_send_fin() could be tempted to either delay FIN 33948c2ecf20Sopenharmony_ci * or even be forced to close flow without any FIN. 33958c2ecf20Sopenharmony_ci * In general, we want to allow one skb per socket to avoid hangs 33968c2ecf20Sopenharmony_ci * with edge trigger epoll() 33978c2ecf20Sopenharmony_ci */ 33988c2ecf20Sopenharmony_civoid sk_forced_mem_schedule(struct sock *sk, int size) 33998c2ecf20Sopenharmony_ci{ 34008c2ecf20Sopenharmony_ci int delta, amt; 34018c2ecf20Sopenharmony_ci 34028c2ecf20Sopenharmony_ci delta = size - sk->sk_forward_alloc; 34038c2ecf20Sopenharmony_ci if (delta <= 0) 34048c2ecf20Sopenharmony_ci return; 34058c2ecf20Sopenharmony_ci amt = sk_mem_pages(delta); 34068c2ecf20Sopenharmony_ci sk->sk_forward_alloc += amt * SK_MEM_QUANTUM; 34078c2ecf20Sopenharmony_ci sk_memory_allocated_add(sk, amt); 34088c2ecf20Sopenharmony_ci 34098c2ecf20Sopenharmony_ci if (mem_cgroup_sockets_enabled && sk->sk_memcg) 34108c2ecf20Sopenharmony_ci mem_cgroup_charge_skmem(sk->sk_memcg, amt); 34118c2ecf20Sopenharmony_ci} 34128c2ecf20Sopenharmony_ci 34138c2ecf20Sopenharmony_ci/* Send a FIN. The caller locks the socket for us. 34148c2ecf20Sopenharmony_ci * We should try to send a FIN packet really hard, but eventually give up. 34158c2ecf20Sopenharmony_ci */ 34168c2ecf20Sopenharmony_civoid tcp_send_fin(struct sock *sk) 34178c2ecf20Sopenharmony_ci{ 34188c2ecf20Sopenharmony_ci struct sk_buff *skb, *tskb, *tail = tcp_write_queue_tail(sk); 34198c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 34208c2ecf20Sopenharmony_ci 34218c2ecf20Sopenharmony_ci /* Optimization, tack on the FIN if we have one skb in write queue and 34228c2ecf20Sopenharmony_ci * this skb was not yet sent, or we are under memory pressure. 34238c2ecf20Sopenharmony_ci * Note: in the latter case, FIN packet will be sent after a timeout, 34248c2ecf20Sopenharmony_ci * as TCP stack thinks it has already been transmitted. 34258c2ecf20Sopenharmony_ci */ 34268c2ecf20Sopenharmony_ci tskb = tail; 34278c2ecf20Sopenharmony_ci if (!tskb && tcp_under_memory_pressure(sk)) 34288c2ecf20Sopenharmony_ci tskb = skb_rb_last(&sk->tcp_rtx_queue); 34298c2ecf20Sopenharmony_ci 34308c2ecf20Sopenharmony_ci if (tskb) { 34318c2ecf20Sopenharmony_ci TCP_SKB_CB(tskb)->tcp_flags |= TCPHDR_FIN; 34328c2ecf20Sopenharmony_ci TCP_SKB_CB(tskb)->end_seq++; 34338c2ecf20Sopenharmony_ci tp->write_seq++; 34348c2ecf20Sopenharmony_ci if (!tail) { 34358c2ecf20Sopenharmony_ci /* This means tskb was already sent. 34368c2ecf20Sopenharmony_ci * Pretend we included the FIN on previous transmit. 34378c2ecf20Sopenharmony_ci * We need to set tp->snd_nxt to the value it would have 34388c2ecf20Sopenharmony_ci * if FIN had been sent. This is because retransmit path 34398c2ecf20Sopenharmony_ci * does not change tp->snd_nxt. 34408c2ecf20Sopenharmony_ci */ 34418c2ecf20Sopenharmony_ci WRITE_ONCE(tp->snd_nxt, tp->snd_nxt + 1); 34428c2ecf20Sopenharmony_ci return; 34438c2ecf20Sopenharmony_ci } 34448c2ecf20Sopenharmony_ci } else { 34458c2ecf20Sopenharmony_ci skb = alloc_skb_fclone(MAX_TCP_HEADER, 34468c2ecf20Sopenharmony_ci sk_gfp_mask(sk, GFP_ATOMIC | 34478c2ecf20Sopenharmony_ci __GFP_NOWARN)); 34488c2ecf20Sopenharmony_ci if (unlikely(!skb)) 34498c2ecf20Sopenharmony_ci return; 34508c2ecf20Sopenharmony_ci 34518c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&skb->tcp_tsorted_anchor); 34528c2ecf20Sopenharmony_ci skb_reserve(skb, MAX_TCP_HEADER); 34538c2ecf20Sopenharmony_ci sk_forced_mem_schedule(sk, skb->truesize); 34548c2ecf20Sopenharmony_ci /* FIN eats a sequence byte, write_seq advanced by tcp_queue_skb(). */ 34558c2ecf20Sopenharmony_ci tcp_init_nondata_skb(skb, tp->write_seq, 34568c2ecf20Sopenharmony_ci TCPHDR_ACK | TCPHDR_FIN); 34578c2ecf20Sopenharmony_ci tcp_queue_skb(sk, skb); 34588c2ecf20Sopenharmony_ci } 34598c2ecf20Sopenharmony_ci __tcp_push_pending_frames(sk, tcp_current_mss(sk), TCP_NAGLE_OFF); 34608c2ecf20Sopenharmony_ci} 34618c2ecf20Sopenharmony_ci 34628c2ecf20Sopenharmony_ci/* We get here when a process closes a file descriptor (either due to 34638c2ecf20Sopenharmony_ci * an explicit close() or as a byproduct of exit()'ing) and there 34648c2ecf20Sopenharmony_ci * was unread data in the receive queue. This behavior is recommended 34658c2ecf20Sopenharmony_ci * by RFC 2525, section 2.17. -DaveM 34668c2ecf20Sopenharmony_ci */ 34678c2ecf20Sopenharmony_civoid tcp_send_active_reset(struct sock *sk, gfp_t priority) 34688c2ecf20Sopenharmony_ci{ 34698c2ecf20Sopenharmony_ci struct sk_buff *skb; 34708c2ecf20Sopenharmony_ci 34718c2ecf20Sopenharmony_ci TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTRSTS); 34728c2ecf20Sopenharmony_ci 34738c2ecf20Sopenharmony_ci /* NOTE: No TCP options attached and we never retransmit this. */ 34748c2ecf20Sopenharmony_ci skb = alloc_skb(MAX_TCP_HEADER, priority); 34758c2ecf20Sopenharmony_ci if (!skb) { 34768c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED); 34778c2ecf20Sopenharmony_ci return; 34788c2ecf20Sopenharmony_ci } 34798c2ecf20Sopenharmony_ci 34808c2ecf20Sopenharmony_ci /* Reserve space for headers and prepare control bits. */ 34818c2ecf20Sopenharmony_ci skb_reserve(skb, MAX_TCP_HEADER); 34828c2ecf20Sopenharmony_ci tcp_init_nondata_skb(skb, tcp_acceptable_seq(sk), 34838c2ecf20Sopenharmony_ci TCPHDR_ACK | TCPHDR_RST); 34848c2ecf20Sopenharmony_ci tcp_mstamp_refresh(tcp_sk(sk)); 34858c2ecf20Sopenharmony_ci /* Send it off. */ 34868c2ecf20Sopenharmony_ci if (tcp_transmit_skb(sk, skb, 0, priority)) 34878c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPABORTFAILED); 34888c2ecf20Sopenharmony_ci 34898c2ecf20Sopenharmony_ci /* skb of trace_tcp_send_reset() keeps the skb that caused RST, 34908c2ecf20Sopenharmony_ci * skb here is different to the troublesome skb, so use NULL 34918c2ecf20Sopenharmony_ci */ 34928c2ecf20Sopenharmony_ci trace_tcp_send_reset(sk, NULL); 34938c2ecf20Sopenharmony_ci} 34948c2ecf20Sopenharmony_ci 34958c2ecf20Sopenharmony_ci/* Send a crossed SYN-ACK during socket establishment. 34968c2ecf20Sopenharmony_ci * WARNING: This routine must only be called when we have already sent 34978c2ecf20Sopenharmony_ci * a SYN packet that crossed the incoming SYN that caused this routine 34988c2ecf20Sopenharmony_ci * to get called. If this assumption fails then the initial rcv_wnd 34998c2ecf20Sopenharmony_ci * and rcv_wscale values will not be correct. 35008c2ecf20Sopenharmony_ci */ 35018c2ecf20Sopenharmony_ciint tcp_send_synack(struct sock *sk) 35028c2ecf20Sopenharmony_ci{ 35038c2ecf20Sopenharmony_ci struct sk_buff *skb; 35048c2ecf20Sopenharmony_ci 35058c2ecf20Sopenharmony_ci skb = tcp_rtx_queue_head(sk); 35068c2ecf20Sopenharmony_ci if (!skb || !(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { 35078c2ecf20Sopenharmony_ci pr_err("%s: wrong queue state\n", __func__); 35088c2ecf20Sopenharmony_ci return -EFAULT; 35098c2ecf20Sopenharmony_ci } 35108c2ecf20Sopenharmony_ci if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)) { 35118c2ecf20Sopenharmony_ci if (skb_cloned(skb)) { 35128c2ecf20Sopenharmony_ci struct sk_buff *nskb; 35138c2ecf20Sopenharmony_ci 35148c2ecf20Sopenharmony_ci tcp_skb_tsorted_save(skb) { 35158c2ecf20Sopenharmony_ci nskb = skb_copy(skb, GFP_ATOMIC); 35168c2ecf20Sopenharmony_ci } tcp_skb_tsorted_restore(skb); 35178c2ecf20Sopenharmony_ci if (!nskb) 35188c2ecf20Sopenharmony_ci return -ENOMEM; 35198c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&nskb->tcp_tsorted_anchor); 35208c2ecf20Sopenharmony_ci tcp_highest_sack_replace(sk, skb, nskb); 35218c2ecf20Sopenharmony_ci tcp_rtx_queue_unlink_and_free(skb, sk); 35228c2ecf20Sopenharmony_ci __skb_header_release(nskb); 35238c2ecf20Sopenharmony_ci tcp_rbtree_insert(&sk->tcp_rtx_queue, nskb); 35248c2ecf20Sopenharmony_ci sk_wmem_queued_add(sk, nskb->truesize); 35258c2ecf20Sopenharmony_ci sk_mem_charge(sk, nskb->truesize); 35268c2ecf20Sopenharmony_ci skb = nskb; 35278c2ecf20Sopenharmony_ci } 35288c2ecf20Sopenharmony_ci 35298c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_ACK; 35308c2ecf20Sopenharmony_ci tcp_ecn_send_synack(sk, skb); 35318c2ecf20Sopenharmony_ci } 35328c2ecf20Sopenharmony_ci return tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC); 35338c2ecf20Sopenharmony_ci} 35348c2ecf20Sopenharmony_ci 35358c2ecf20Sopenharmony_ci/** 35368c2ecf20Sopenharmony_ci * tcp_make_synack - Allocate one skb and build a SYNACK packet. 35378c2ecf20Sopenharmony_ci * @sk: listener socket 35388c2ecf20Sopenharmony_ci * @dst: dst entry attached to the SYNACK. It is consumed and caller 35398c2ecf20Sopenharmony_ci * should not use it again. 35408c2ecf20Sopenharmony_ci * @req: request_sock pointer 35418c2ecf20Sopenharmony_ci * @foc: cookie for tcp fast open 35428c2ecf20Sopenharmony_ci * @synack_type: Type of synack to prepare 35438c2ecf20Sopenharmony_ci * @syn_skb: SYN packet just received. It could be NULL for rtx case. 35448c2ecf20Sopenharmony_ci */ 35458c2ecf20Sopenharmony_cistruct sk_buff *tcp_make_synack(const struct sock *sk, struct dst_entry *dst, 35468c2ecf20Sopenharmony_ci struct request_sock *req, 35478c2ecf20Sopenharmony_ci struct tcp_fastopen_cookie *foc, 35488c2ecf20Sopenharmony_ci enum tcp_synack_type synack_type, 35498c2ecf20Sopenharmony_ci struct sk_buff *syn_skb) 35508c2ecf20Sopenharmony_ci{ 35518c2ecf20Sopenharmony_ci struct inet_request_sock *ireq = inet_rsk(req); 35528c2ecf20Sopenharmony_ci const struct tcp_sock *tp = tcp_sk(sk); 35538c2ecf20Sopenharmony_ci struct tcp_md5sig_key *md5 = NULL; 35548c2ecf20Sopenharmony_ci struct tcp_out_options opts; 35558c2ecf20Sopenharmony_ci struct sk_buff *skb; 35568c2ecf20Sopenharmony_ci int tcp_header_size; 35578c2ecf20Sopenharmony_ci struct tcphdr *th; 35588c2ecf20Sopenharmony_ci int mss; 35598c2ecf20Sopenharmony_ci u64 now; 35608c2ecf20Sopenharmony_ci 35618c2ecf20Sopenharmony_ci skb = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC); 35628c2ecf20Sopenharmony_ci if (unlikely(!skb)) { 35638c2ecf20Sopenharmony_ci dst_release(dst); 35648c2ecf20Sopenharmony_ci return NULL; 35658c2ecf20Sopenharmony_ci } 35668c2ecf20Sopenharmony_ci /* Reserve space for headers. */ 35678c2ecf20Sopenharmony_ci skb_reserve(skb, MAX_TCP_HEADER); 35688c2ecf20Sopenharmony_ci 35698c2ecf20Sopenharmony_ci switch (synack_type) { 35708c2ecf20Sopenharmony_ci case TCP_SYNACK_NORMAL: 35718c2ecf20Sopenharmony_ci skb_set_owner_w(skb, req_to_sk(req)); 35728c2ecf20Sopenharmony_ci break; 35738c2ecf20Sopenharmony_ci case TCP_SYNACK_COOKIE: 35748c2ecf20Sopenharmony_ci /* Under synflood, we do not attach skb to a socket, 35758c2ecf20Sopenharmony_ci * to avoid false sharing. 35768c2ecf20Sopenharmony_ci */ 35778c2ecf20Sopenharmony_ci break; 35788c2ecf20Sopenharmony_ci case TCP_SYNACK_FASTOPEN: 35798c2ecf20Sopenharmony_ci /* sk is a const pointer, because we want to express multiple 35808c2ecf20Sopenharmony_ci * cpu might call us concurrently. 35818c2ecf20Sopenharmony_ci * sk->sk_wmem_alloc in an atomic, we can promote to rw. 35828c2ecf20Sopenharmony_ci */ 35838c2ecf20Sopenharmony_ci skb_set_owner_w(skb, (struct sock *)sk); 35848c2ecf20Sopenharmony_ci break; 35858c2ecf20Sopenharmony_ci } 35868c2ecf20Sopenharmony_ci skb_dst_set(skb, dst); 35878c2ecf20Sopenharmony_ci 35888c2ecf20Sopenharmony_ci mss = tcp_mss_clamp(tp, dst_metric_advmss(dst)); 35898c2ecf20Sopenharmony_ci 35908c2ecf20Sopenharmony_ci memset(&opts, 0, sizeof(opts)); 35918c2ecf20Sopenharmony_ci now = tcp_clock_ns(); 35928c2ecf20Sopenharmony_ci#ifdef CONFIG_SYN_COOKIES 35938c2ecf20Sopenharmony_ci if (unlikely(synack_type == TCP_SYNACK_COOKIE && ireq->tstamp_ok)) 35948c2ecf20Sopenharmony_ci skb->skb_mstamp_ns = cookie_init_timestamp(req, now); 35958c2ecf20Sopenharmony_ci else 35968c2ecf20Sopenharmony_ci#endif 35978c2ecf20Sopenharmony_ci { 35988c2ecf20Sopenharmony_ci skb->skb_mstamp_ns = now; 35998c2ecf20Sopenharmony_ci if (!tcp_rsk(req)->snt_synack) /* Timestamp first SYNACK */ 36008c2ecf20Sopenharmony_ci tcp_rsk(req)->snt_synack = tcp_skb_timestamp_us(skb); 36018c2ecf20Sopenharmony_ci } 36028c2ecf20Sopenharmony_ci 36038c2ecf20Sopenharmony_ci#ifdef CONFIG_TCP_MD5SIG 36048c2ecf20Sopenharmony_ci rcu_read_lock(); 36058c2ecf20Sopenharmony_ci md5 = tcp_rsk(req)->af_specific->req_md5_lookup(sk, req_to_sk(req)); 36068c2ecf20Sopenharmony_ci#endif 36078c2ecf20Sopenharmony_ci skb_set_hash(skb, tcp_rsk(req)->txhash, PKT_HASH_TYPE_L4); 36088c2ecf20Sopenharmony_ci /* bpf program will be interested in the tcp_flags */ 36098c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags = TCPHDR_SYN | TCPHDR_ACK; 36108c2ecf20Sopenharmony_ci tcp_header_size = tcp_synack_options(sk, req, mss, skb, &opts, md5, 36118c2ecf20Sopenharmony_ci foc, synack_type, 36128c2ecf20Sopenharmony_ci syn_skb) + sizeof(*th); 36138c2ecf20Sopenharmony_ci 36148c2ecf20Sopenharmony_ci skb_push(skb, tcp_header_size); 36158c2ecf20Sopenharmony_ci skb_reset_transport_header(skb); 36168c2ecf20Sopenharmony_ci 36178c2ecf20Sopenharmony_ci th = (struct tcphdr *)skb->data; 36188c2ecf20Sopenharmony_ci memset(th, 0, sizeof(struct tcphdr)); 36198c2ecf20Sopenharmony_ci th->syn = 1; 36208c2ecf20Sopenharmony_ci th->ack = 1; 36218c2ecf20Sopenharmony_ci tcp_ecn_make_synack(req, th); 36228c2ecf20Sopenharmony_ci th->source = htons(ireq->ir_num); 36238c2ecf20Sopenharmony_ci th->dest = ireq->ir_rmt_port; 36248c2ecf20Sopenharmony_ci skb->mark = ireq->ir_mark; 36258c2ecf20Sopenharmony_ci skb->ip_summed = CHECKSUM_PARTIAL; 36268c2ecf20Sopenharmony_ci th->seq = htonl(tcp_rsk(req)->snt_isn); 36278c2ecf20Sopenharmony_ci /* XXX data is queued and acked as is. No buffer/window check */ 36288c2ecf20Sopenharmony_ci th->ack_seq = htonl(tcp_rsk(req)->rcv_nxt); 36298c2ecf20Sopenharmony_ci 36308c2ecf20Sopenharmony_ci /* RFC1323: The window in SYN & SYN/ACK segments is never scaled. */ 36318c2ecf20Sopenharmony_ci th->window = htons(min(req->rsk_rcv_wnd, 65535U)); 36328c2ecf20Sopenharmony_ci tcp_options_write((__be32 *)(th + 1), NULL, &opts); 36338c2ecf20Sopenharmony_ci th->doff = (tcp_header_size >> 2); 36348c2ecf20Sopenharmony_ci TCP_INC_STATS(sock_net(sk), TCP_MIB_OUTSEGS); 36358c2ecf20Sopenharmony_ci 36368c2ecf20Sopenharmony_ci#ifdef CONFIG_TCP_MD5SIG 36378c2ecf20Sopenharmony_ci /* Okay, we have all we need - do the md5 hash if needed */ 36388c2ecf20Sopenharmony_ci if (md5) 36398c2ecf20Sopenharmony_ci tcp_rsk(req)->af_specific->calc_md5_hash(opts.hash_location, 36408c2ecf20Sopenharmony_ci md5, req_to_sk(req), skb); 36418c2ecf20Sopenharmony_ci rcu_read_unlock(); 36428c2ecf20Sopenharmony_ci#endif 36438c2ecf20Sopenharmony_ci 36448c2ecf20Sopenharmony_ci bpf_skops_write_hdr_opt((struct sock *)sk, skb, req, syn_skb, 36458c2ecf20Sopenharmony_ci synack_type, &opts); 36468c2ecf20Sopenharmony_ci 36478c2ecf20Sopenharmony_ci skb->skb_mstamp_ns = now; 36488c2ecf20Sopenharmony_ci tcp_add_tx_delay(skb, tp); 36498c2ecf20Sopenharmony_ci 36508c2ecf20Sopenharmony_ci return skb; 36518c2ecf20Sopenharmony_ci} 36528c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tcp_make_synack); 36538c2ecf20Sopenharmony_ci 36548c2ecf20Sopenharmony_cistatic void tcp_ca_dst_init(struct sock *sk, const struct dst_entry *dst) 36558c2ecf20Sopenharmony_ci{ 36568c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 36578c2ecf20Sopenharmony_ci const struct tcp_congestion_ops *ca; 36588c2ecf20Sopenharmony_ci u32 ca_key = dst_metric(dst, RTAX_CC_ALGO); 36598c2ecf20Sopenharmony_ci 36608c2ecf20Sopenharmony_ci if (ca_key == TCP_CA_UNSPEC) 36618c2ecf20Sopenharmony_ci return; 36628c2ecf20Sopenharmony_ci 36638c2ecf20Sopenharmony_ci rcu_read_lock(); 36648c2ecf20Sopenharmony_ci ca = tcp_ca_find_key(ca_key); 36658c2ecf20Sopenharmony_ci if (likely(ca && bpf_try_module_get(ca, ca->owner))) { 36668c2ecf20Sopenharmony_ci bpf_module_put(icsk->icsk_ca_ops, icsk->icsk_ca_ops->owner); 36678c2ecf20Sopenharmony_ci icsk->icsk_ca_dst_locked = tcp_ca_dst_locked(dst); 36688c2ecf20Sopenharmony_ci icsk->icsk_ca_ops = ca; 36698c2ecf20Sopenharmony_ci } 36708c2ecf20Sopenharmony_ci rcu_read_unlock(); 36718c2ecf20Sopenharmony_ci} 36728c2ecf20Sopenharmony_ci 36738c2ecf20Sopenharmony_ci/* Do all connect socket setups that can be done AF independent. */ 36748c2ecf20Sopenharmony_cistatic void tcp_connect_init(struct sock *sk) 36758c2ecf20Sopenharmony_ci{ 36768c2ecf20Sopenharmony_ci const struct dst_entry *dst = __sk_dst_get(sk); 36778c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 36788c2ecf20Sopenharmony_ci __u8 rcv_wscale; 36798c2ecf20Sopenharmony_ci u32 rcv_wnd; 36808c2ecf20Sopenharmony_ci 36818c2ecf20Sopenharmony_ci /* We'll fix this up when we get a response from the other end. 36828c2ecf20Sopenharmony_ci * See tcp_input.c:tcp_rcv_state_process case TCP_SYN_SENT. 36838c2ecf20Sopenharmony_ci */ 36848c2ecf20Sopenharmony_ci tp->tcp_header_len = sizeof(struct tcphdr); 36858c2ecf20Sopenharmony_ci if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_timestamps)) 36868c2ecf20Sopenharmony_ci tp->tcp_header_len += TCPOLEN_TSTAMP_ALIGNED; 36878c2ecf20Sopenharmony_ci 36888c2ecf20Sopenharmony_ci#ifdef CONFIG_TCP_MD5SIG 36898c2ecf20Sopenharmony_ci if (tp->af_specific->md5_lookup(sk, sk)) 36908c2ecf20Sopenharmony_ci tp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED; 36918c2ecf20Sopenharmony_ci#endif 36928c2ecf20Sopenharmony_ci 36938c2ecf20Sopenharmony_ci /* If user gave his TCP_MAXSEG, record it to clamp */ 36948c2ecf20Sopenharmony_ci if (tp->rx_opt.user_mss) 36958c2ecf20Sopenharmony_ci tp->rx_opt.mss_clamp = tp->rx_opt.user_mss; 36968c2ecf20Sopenharmony_ci tp->max_window = 0; 36978c2ecf20Sopenharmony_ci tcp_mtup_init(sk); 36988c2ecf20Sopenharmony_ci tcp_sync_mss(sk, dst_mtu(dst)); 36998c2ecf20Sopenharmony_ci 37008c2ecf20Sopenharmony_ci tcp_ca_dst_init(sk, dst); 37018c2ecf20Sopenharmony_ci 37028c2ecf20Sopenharmony_ci if (!tp->window_clamp) 37038c2ecf20Sopenharmony_ci tp->window_clamp = dst_metric(dst, RTAX_WINDOW); 37048c2ecf20Sopenharmony_ci tp->advmss = tcp_mss_clamp(tp, dst_metric_advmss(dst)); 37058c2ecf20Sopenharmony_ci 37068c2ecf20Sopenharmony_ci tcp_initialize_rcv_mss(sk); 37078c2ecf20Sopenharmony_ci 37088c2ecf20Sopenharmony_ci /* limit the window selection if the user enforce a smaller rx buffer */ 37098c2ecf20Sopenharmony_ci if (sk->sk_userlocks & SOCK_RCVBUF_LOCK && 37108c2ecf20Sopenharmony_ci (tp->window_clamp > tcp_full_space(sk) || tp->window_clamp == 0)) 37118c2ecf20Sopenharmony_ci tp->window_clamp = tcp_full_space(sk); 37128c2ecf20Sopenharmony_ci 37138c2ecf20Sopenharmony_ci rcv_wnd = tcp_rwnd_init_bpf(sk); 37148c2ecf20Sopenharmony_ci if (rcv_wnd == 0) 37158c2ecf20Sopenharmony_ci rcv_wnd = dst_metric(dst, RTAX_INITRWND); 37168c2ecf20Sopenharmony_ci 37178c2ecf20Sopenharmony_ci tcp_select_initial_window(sk, tcp_full_space(sk), 37188c2ecf20Sopenharmony_ci tp->advmss - (tp->rx_opt.ts_recent_stamp ? tp->tcp_header_len - sizeof(struct tcphdr) : 0), 37198c2ecf20Sopenharmony_ci &tp->rcv_wnd, 37208c2ecf20Sopenharmony_ci &tp->window_clamp, 37218c2ecf20Sopenharmony_ci READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_window_scaling), 37228c2ecf20Sopenharmony_ci &rcv_wscale, 37238c2ecf20Sopenharmony_ci rcv_wnd); 37248c2ecf20Sopenharmony_ci 37258c2ecf20Sopenharmony_ci#ifdef CONFIG_LOWPOWER_PROTOCOL 37268c2ecf20Sopenharmony_ci tp->rcv_wnd = TCP_RCV_WND_INIT; 37278c2ecf20Sopenharmony_ci#endif /* CONFIG_LOWPOWER_PROTOCOL */ 37288c2ecf20Sopenharmony_ci tp->rx_opt.rcv_wscale = rcv_wscale; 37298c2ecf20Sopenharmony_ci tp->rcv_ssthresh = tp->rcv_wnd; 37308c2ecf20Sopenharmony_ci 37318c2ecf20Sopenharmony_ci sk->sk_err = 0; 37328c2ecf20Sopenharmony_ci sock_reset_flag(sk, SOCK_DONE); 37338c2ecf20Sopenharmony_ci tp->snd_wnd = 0; 37348c2ecf20Sopenharmony_ci tcp_init_wl(tp, 0); 37358c2ecf20Sopenharmony_ci tcp_write_queue_purge(sk); 37368c2ecf20Sopenharmony_ci tp->snd_una = tp->write_seq; 37378c2ecf20Sopenharmony_ci tp->snd_sml = tp->write_seq; 37388c2ecf20Sopenharmony_ci tp->snd_up = tp->write_seq; 37398c2ecf20Sopenharmony_ci WRITE_ONCE(tp->snd_nxt, tp->write_seq); 37408c2ecf20Sopenharmony_ci 37418c2ecf20Sopenharmony_ci if (likely(!tp->repair)) 37428c2ecf20Sopenharmony_ci tp->rcv_nxt = 0; 37438c2ecf20Sopenharmony_ci else 37448c2ecf20Sopenharmony_ci tp->rcv_tstamp = tcp_jiffies32; 37458c2ecf20Sopenharmony_ci tp->rcv_wup = tp->rcv_nxt; 37468c2ecf20Sopenharmony_ci WRITE_ONCE(tp->copied_seq, tp->rcv_nxt); 37478c2ecf20Sopenharmony_ci 37488c2ecf20Sopenharmony_ci inet_csk(sk)->icsk_rto = tcp_timeout_init(sk); 37498c2ecf20Sopenharmony_ci inet_csk(sk)->icsk_retransmits = 0; 37508c2ecf20Sopenharmony_ci tcp_clear_retrans(tp); 37518c2ecf20Sopenharmony_ci} 37528c2ecf20Sopenharmony_ci 37538c2ecf20Sopenharmony_cistatic void tcp_connect_queue_skb(struct sock *sk, struct sk_buff *skb) 37548c2ecf20Sopenharmony_ci{ 37558c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 37568c2ecf20Sopenharmony_ci struct tcp_skb_cb *tcb = TCP_SKB_CB(skb); 37578c2ecf20Sopenharmony_ci 37588c2ecf20Sopenharmony_ci tcb->end_seq += skb->len; 37598c2ecf20Sopenharmony_ci __skb_header_release(skb); 37608c2ecf20Sopenharmony_ci sk_wmem_queued_add(sk, skb->truesize); 37618c2ecf20Sopenharmony_ci sk_mem_charge(sk, skb->truesize); 37628c2ecf20Sopenharmony_ci WRITE_ONCE(tp->write_seq, tcb->end_seq); 37638c2ecf20Sopenharmony_ci tp->packets_out += tcp_skb_pcount(skb); 37648c2ecf20Sopenharmony_ci} 37658c2ecf20Sopenharmony_ci 37668c2ecf20Sopenharmony_ci/* Build and send a SYN with data and (cached) Fast Open cookie. However, 37678c2ecf20Sopenharmony_ci * queue a data-only packet after the regular SYN, such that regular SYNs 37688c2ecf20Sopenharmony_ci * are retransmitted on timeouts. Also if the remote SYN-ACK acknowledges 37698c2ecf20Sopenharmony_ci * only the SYN sequence, the data are retransmitted in the first ACK. 37708c2ecf20Sopenharmony_ci * If cookie is not cached or other error occurs, falls back to send a 37718c2ecf20Sopenharmony_ci * regular SYN with Fast Open cookie request option. 37728c2ecf20Sopenharmony_ci */ 37738c2ecf20Sopenharmony_cistatic int tcp_send_syn_data(struct sock *sk, struct sk_buff *syn) 37748c2ecf20Sopenharmony_ci{ 37758c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 37768c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 37778c2ecf20Sopenharmony_ci struct tcp_fastopen_request *fo = tp->fastopen_req; 37788c2ecf20Sopenharmony_ci int space, err = 0; 37798c2ecf20Sopenharmony_ci struct sk_buff *syn_data; 37808c2ecf20Sopenharmony_ci 37818c2ecf20Sopenharmony_ci tp->rx_opt.mss_clamp = tp->advmss; /* If MSS is not cached */ 37828c2ecf20Sopenharmony_ci if (!tcp_fastopen_cookie_check(sk, &tp->rx_opt.mss_clamp, &fo->cookie)) 37838c2ecf20Sopenharmony_ci goto fallback; 37848c2ecf20Sopenharmony_ci 37858c2ecf20Sopenharmony_ci /* MSS for SYN-data is based on cached MSS and bounded by PMTU and 37868c2ecf20Sopenharmony_ci * user-MSS. Reserve maximum option space for middleboxes that add 37878c2ecf20Sopenharmony_ci * private TCP options. The cost is reduced data space in SYN :( 37888c2ecf20Sopenharmony_ci */ 37898c2ecf20Sopenharmony_ci tp->rx_opt.mss_clamp = tcp_mss_clamp(tp, tp->rx_opt.mss_clamp); 37908c2ecf20Sopenharmony_ci /* Sync mss_cache after updating the mss_clamp */ 37918c2ecf20Sopenharmony_ci tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 37928c2ecf20Sopenharmony_ci 37938c2ecf20Sopenharmony_ci space = __tcp_mtu_to_mss(sk, icsk->icsk_pmtu_cookie) - 37948c2ecf20Sopenharmony_ci MAX_TCP_OPTION_SPACE; 37958c2ecf20Sopenharmony_ci 37968c2ecf20Sopenharmony_ci space = min_t(size_t, space, fo->size); 37978c2ecf20Sopenharmony_ci 37988c2ecf20Sopenharmony_ci /* limit to order-0 allocations */ 37998c2ecf20Sopenharmony_ci space = min_t(size_t, space, SKB_MAX_HEAD(MAX_TCP_HEADER)); 38008c2ecf20Sopenharmony_ci 38018c2ecf20Sopenharmony_ci syn_data = sk_stream_alloc_skb(sk, space, sk->sk_allocation, false); 38028c2ecf20Sopenharmony_ci if (!syn_data) 38038c2ecf20Sopenharmony_ci goto fallback; 38048c2ecf20Sopenharmony_ci syn_data->ip_summed = CHECKSUM_PARTIAL; 38058c2ecf20Sopenharmony_ci memcpy(syn_data->cb, syn->cb, sizeof(syn->cb)); 38068c2ecf20Sopenharmony_ci if (space) { 38078c2ecf20Sopenharmony_ci int copied = copy_from_iter(skb_put(syn_data, space), space, 38088c2ecf20Sopenharmony_ci &fo->data->msg_iter); 38098c2ecf20Sopenharmony_ci if (unlikely(!copied)) { 38108c2ecf20Sopenharmony_ci tcp_skb_tsorted_anchor_cleanup(syn_data); 38118c2ecf20Sopenharmony_ci kfree_skb(syn_data); 38128c2ecf20Sopenharmony_ci goto fallback; 38138c2ecf20Sopenharmony_ci } 38148c2ecf20Sopenharmony_ci if (copied != space) { 38158c2ecf20Sopenharmony_ci skb_trim(syn_data, copied); 38168c2ecf20Sopenharmony_ci space = copied; 38178c2ecf20Sopenharmony_ci } 38188c2ecf20Sopenharmony_ci skb_zcopy_set(syn_data, fo->uarg, NULL); 38198c2ecf20Sopenharmony_ci } 38208c2ecf20Sopenharmony_ci /* No more data pending in inet_wait_for_connect() */ 38218c2ecf20Sopenharmony_ci if (space == fo->size) 38228c2ecf20Sopenharmony_ci fo->data = NULL; 38238c2ecf20Sopenharmony_ci fo->copied = space; 38248c2ecf20Sopenharmony_ci 38258c2ecf20Sopenharmony_ci tcp_connect_queue_skb(sk, syn_data); 38268c2ecf20Sopenharmony_ci if (syn_data->len) 38278c2ecf20Sopenharmony_ci tcp_chrono_start(sk, TCP_CHRONO_BUSY); 38288c2ecf20Sopenharmony_ci 38298c2ecf20Sopenharmony_ci err = tcp_transmit_skb(sk, syn_data, 1, sk->sk_allocation); 38308c2ecf20Sopenharmony_ci 38318c2ecf20Sopenharmony_ci syn->skb_mstamp_ns = syn_data->skb_mstamp_ns; 38328c2ecf20Sopenharmony_ci 38338c2ecf20Sopenharmony_ci /* Now full SYN+DATA was cloned and sent (or not), 38348c2ecf20Sopenharmony_ci * remove the SYN from the original skb (syn_data) 38358c2ecf20Sopenharmony_ci * we keep in write queue in case of a retransmit, as we 38368c2ecf20Sopenharmony_ci * also have the SYN packet (with no data) in the same queue. 38378c2ecf20Sopenharmony_ci */ 38388c2ecf20Sopenharmony_ci TCP_SKB_CB(syn_data)->seq++; 38398c2ecf20Sopenharmony_ci TCP_SKB_CB(syn_data)->tcp_flags = TCPHDR_ACK | TCPHDR_PSH; 38408c2ecf20Sopenharmony_ci if (!err) { 38418c2ecf20Sopenharmony_ci tp->syn_data = (fo->copied > 0); 38428c2ecf20Sopenharmony_ci tcp_rbtree_insert(&sk->tcp_rtx_queue, syn_data); 38438c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPORIGDATASENT); 38448c2ecf20Sopenharmony_ci goto done; 38458c2ecf20Sopenharmony_ci } 38468c2ecf20Sopenharmony_ci 38478c2ecf20Sopenharmony_ci /* data was not sent, put it in write_queue */ 38488c2ecf20Sopenharmony_ci __skb_queue_tail(&sk->sk_write_queue, syn_data); 38498c2ecf20Sopenharmony_ci tp->packets_out -= tcp_skb_pcount(syn_data); 38508c2ecf20Sopenharmony_ci 38518c2ecf20Sopenharmony_cifallback: 38528c2ecf20Sopenharmony_ci /* Send a regular SYN with Fast Open cookie request option */ 38538c2ecf20Sopenharmony_ci if (fo->cookie.len > 0) 38548c2ecf20Sopenharmony_ci fo->cookie.len = 0; 38558c2ecf20Sopenharmony_ci err = tcp_transmit_skb(sk, syn, 1, sk->sk_allocation); 38568c2ecf20Sopenharmony_ci if (err) 38578c2ecf20Sopenharmony_ci tp->syn_fastopen = 0; 38588c2ecf20Sopenharmony_cidone: 38598c2ecf20Sopenharmony_ci fo->cookie.len = -1; /* Exclude Fast Open option for SYN retries */ 38608c2ecf20Sopenharmony_ci return err; 38618c2ecf20Sopenharmony_ci} 38628c2ecf20Sopenharmony_ci 38638c2ecf20Sopenharmony_ci/* Build a SYN and send it off. */ 38648c2ecf20Sopenharmony_ciint tcp_connect(struct sock *sk) 38658c2ecf20Sopenharmony_ci{ 38668c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 38678c2ecf20Sopenharmony_ci struct sk_buff *buff; 38688c2ecf20Sopenharmony_ci int err; 38698c2ecf20Sopenharmony_ci 38708c2ecf20Sopenharmony_ci tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_CONNECT_CB, 0, NULL); 38718c2ecf20Sopenharmony_ci 38728c2ecf20Sopenharmony_ci if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk)) 38738c2ecf20Sopenharmony_ci return -EHOSTUNREACH; /* Routing failure or similar. */ 38748c2ecf20Sopenharmony_ci 38758c2ecf20Sopenharmony_ci tcp_connect_init(sk); 38768c2ecf20Sopenharmony_ci 38778c2ecf20Sopenharmony_ci if (unlikely(tp->repair)) { 38788c2ecf20Sopenharmony_ci tcp_finish_connect(sk, NULL); 38798c2ecf20Sopenharmony_ci return 0; 38808c2ecf20Sopenharmony_ci } 38818c2ecf20Sopenharmony_ci 38828c2ecf20Sopenharmony_ci buff = sk_stream_alloc_skb(sk, 0, sk->sk_allocation, true); 38838c2ecf20Sopenharmony_ci if (unlikely(!buff)) 38848c2ecf20Sopenharmony_ci return -ENOBUFS; 38858c2ecf20Sopenharmony_ci 38868c2ecf20Sopenharmony_ci tcp_init_nondata_skb(buff, tp->write_seq++, TCPHDR_SYN); 38878c2ecf20Sopenharmony_ci tcp_mstamp_refresh(tp); 38888c2ecf20Sopenharmony_ci tp->retrans_stamp = tcp_time_stamp(tp); 38898c2ecf20Sopenharmony_ci tcp_connect_queue_skb(sk, buff); 38908c2ecf20Sopenharmony_ci tcp_ecn_send_syn(sk, buff); 38918c2ecf20Sopenharmony_ci tcp_rbtree_insert(&sk->tcp_rtx_queue, buff); 38928c2ecf20Sopenharmony_ci 38938c2ecf20Sopenharmony_ci /* Send off SYN; include data in Fast Open. */ 38948c2ecf20Sopenharmony_ci err = tp->fastopen_req ? tcp_send_syn_data(sk, buff) : 38958c2ecf20Sopenharmony_ci tcp_transmit_skb(sk, buff, 1, sk->sk_allocation); 38968c2ecf20Sopenharmony_ci if (err == -ECONNREFUSED) 38978c2ecf20Sopenharmony_ci return err; 38988c2ecf20Sopenharmony_ci 38998c2ecf20Sopenharmony_ci /* We change tp->snd_nxt after the tcp_transmit_skb() call 39008c2ecf20Sopenharmony_ci * in order to make this packet get counted in tcpOutSegs. 39018c2ecf20Sopenharmony_ci */ 39028c2ecf20Sopenharmony_ci WRITE_ONCE(tp->snd_nxt, tp->write_seq); 39038c2ecf20Sopenharmony_ci tp->pushed_seq = tp->write_seq; 39048c2ecf20Sopenharmony_ci buff = tcp_send_head(sk); 39058c2ecf20Sopenharmony_ci if (unlikely(buff)) { 39068c2ecf20Sopenharmony_ci WRITE_ONCE(tp->snd_nxt, TCP_SKB_CB(buff)->seq); 39078c2ecf20Sopenharmony_ci tp->pushed_seq = TCP_SKB_CB(buff)->seq; 39088c2ecf20Sopenharmony_ci } 39098c2ecf20Sopenharmony_ci TCP_INC_STATS(sock_net(sk), TCP_MIB_ACTIVEOPENS); 39108c2ecf20Sopenharmony_ci 39118c2ecf20Sopenharmony_ci /* Timer for repeating the SYN until an answer. */ 39128c2ecf20Sopenharmony_ci inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, 39138c2ecf20Sopenharmony_ci inet_csk(sk)->icsk_rto, TCP_RTO_MAX); 39148c2ecf20Sopenharmony_ci return 0; 39158c2ecf20Sopenharmony_ci} 39168c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tcp_connect); 39178c2ecf20Sopenharmony_ci 39188c2ecf20Sopenharmony_ci/* Send out a delayed ack, the caller does the policy checking 39198c2ecf20Sopenharmony_ci * to see if we should even be here. See tcp_input.c:tcp_ack_snd_check() 39208c2ecf20Sopenharmony_ci * for details. 39218c2ecf20Sopenharmony_ci */ 39228c2ecf20Sopenharmony_civoid tcp_send_delayed_ack(struct sock *sk) 39238c2ecf20Sopenharmony_ci{ 39248c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 39258c2ecf20Sopenharmony_ci int ato = icsk->icsk_ack.ato; 39268c2ecf20Sopenharmony_ci unsigned long timeout; 39278c2ecf20Sopenharmony_ci 39288c2ecf20Sopenharmony_ci if (ato > TCP_DELACK_MIN) { 39298c2ecf20Sopenharmony_ci const struct tcp_sock *tp = tcp_sk(sk); 39308c2ecf20Sopenharmony_ci int max_ato = HZ / 2; 39318c2ecf20Sopenharmony_ci 39328c2ecf20Sopenharmony_ci if (inet_csk_in_pingpong_mode(sk) || 39338c2ecf20Sopenharmony_ci (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)) 39348c2ecf20Sopenharmony_ci max_ato = TCP_DELACK_MAX; 39358c2ecf20Sopenharmony_ci 39368c2ecf20Sopenharmony_ci /* Slow path, intersegment interval is "high". */ 39378c2ecf20Sopenharmony_ci 39388c2ecf20Sopenharmony_ci /* If some rtt estimate is known, use it to bound delayed ack. 39398c2ecf20Sopenharmony_ci * Do not use inet_csk(sk)->icsk_rto here, use results of rtt measurements 39408c2ecf20Sopenharmony_ci * directly. 39418c2ecf20Sopenharmony_ci */ 39428c2ecf20Sopenharmony_ci if (tp->srtt_us) { 39438c2ecf20Sopenharmony_ci int rtt = max_t(int, usecs_to_jiffies(tp->srtt_us >> 3), 39448c2ecf20Sopenharmony_ci TCP_DELACK_MIN); 39458c2ecf20Sopenharmony_ci 39468c2ecf20Sopenharmony_ci if (rtt < max_ato) 39478c2ecf20Sopenharmony_ci max_ato = rtt; 39488c2ecf20Sopenharmony_ci } 39498c2ecf20Sopenharmony_ci 39508c2ecf20Sopenharmony_ci ato = min(ato, max_ato); 39518c2ecf20Sopenharmony_ci } 39528c2ecf20Sopenharmony_ci 39538c2ecf20Sopenharmony_ci ato = min_t(u32, ato, inet_csk(sk)->icsk_delack_max); 39548c2ecf20Sopenharmony_ci 39558c2ecf20Sopenharmony_ci /* Stay within the limit we were given */ 39568c2ecf20Sopenharmony_ci timeout = jiffies + ato; 39578c2ecf20Sopenharmony_ci 39588c2ecf20Sopenharmony_ci /* Use new timeout only if there wasn't a older one earlier. */ 39598c2ecf20Sopenharmony_ci if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) { 39608c2ecf20Sopenharmony_ci /* If delack timer is about to expire, send ACK now. */ 39618c2ecf20Sopenharmony_ci if (time_before_eq(icsk->icsk_ack.timeout, jiffies + (ato >> 2))) { 39628c2ecf20Sopenharmony_ci tcp_send_ack(sk); 39638c2ecf20Sopenharmony_ci return; 39648c2ecf20Sopenharmony_ci } 39658c2ecf20Sopenharmony_ci 39668c2ecf20Sopenharmony_ci if (!time_before(timeout, icsk->icsk_ack.timeout)) 39678c2ecf20Sopenharmony_ci timeout = icsk->icsk_ack.timeout; 39688c2ecf20Sopenharmony_ci } 39698c2ecf20Sopenharmony_ci icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER; 39708c2ecf20Sopenharmony_ci icsk->icsk_ack.timeout = timeout; 39718c2ecf20Sopenharmony_ci sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout); 39728c2ecf20Sopenharmony_ci} 39738c2ecf20Sopenharmony_ci 39748c2ecf20Sopenharmony_ci/* This routine sends an ack and also updates the window. */ 39758c2ecf20Sopenharmony_civoid __tcp_send_ack(struct sock *sk, u32 rcv_nxt) 39768c2ecf20Sopenharmony_ci{ 39778c2ecf20Sopenharmony_ci struct sk_buff *buff; 39788c2ecf20Sopenharmony_ci 39798c2ecf20Sopenharmony_ci /* If we have been reset, we may not send again. */ 39808c2ecf20Sopenharmony_ci if (sk->sk_state == TCP_CLOSE) 39818c2ecf20Sopenharmony_ci return; 39828c2ecf20Sopenharmony_ci 39838c2ecf20Sopenharmony_ci /* We are not putting this on the write queue, so 39848c2ecf20Sopenharmony_ci * tcp_transmit_skb() will set the ownership to this 39858c2ecf20Sopenharmony_ci * sock. 39868c2ecf20Sopenharmony_ci */ 39878c2ecf20Sopenharmony_ci buff = alloc_skb(MAX_TCP_HEADER, 39888c2ecf20Sopenharmony_ci sk_gfp_mask(sk, GFP_ATOMIC | __GFP_NOWARN)); 39898c2ecf20Sopenharmony_ci if (unlikely(!buff)) { 39908c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 39918c2ecf20Sopenharmony_ci unsigned long delay; 39928c2ecf20Sopenharmony_ci 39938c2ecf20Sopenharmony_ci delay = TCP_DELACK_MAX << icsk->icsk_ack.retry; 39948c2ecf20Sopenharmony_ci if (delay < TCP_RTO_MAX) 39958c2ecf20Sopenharmony_ci icsk->icsk_ack.retry++; 39968c2ecf20Sopenharmony_ci inet_csk_schedule_ack(sk); 39978c2ecf20Sopenharmony_ci icsk->icsk_ack.ato = TCP_ATO_MIN; 39988c2ecf20Sopenharmony_ci inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, delay, TCP_RTO_MAX); 39998c2ecf20Sopenharmony_ci return; 40008c2ecf20Sopenharmony_ci } 40018c2ecf20Sopenharmony_ci 40028c2ecf20Sopenharmony_ci /* Reserve space for headers and prepare control bits. */ 40038c2ecf20Sopenharmony_ci skb_reserve(buff, MAX_TCP_HEADER); 40048c2ecf20Sopenharmony_ci tcp_init_nondata_skb(buff, tcp_acceptable_seq(sk), TCPHDR_ACK); 40058c2ecf20Sopenharmony_ci 40068c2ecf20Sopenharmony_ci /* We do not want pure acks influencing TCP Small Queues or fq/pacing 40078c2ecf20Sopenharmony_ci * too much. 40088c2ecf20Sopenharmony_ci * SKB_TRUESIZE(max(1 .. 66, MAX_TCP_HEADER)) is unfortunately ~784 40098c2ecf20Sopenharmony_ci */ 40108c2ecf20Sopenharmony_ci skb_set_tcp_pure_ack(buff); 40118c2ecf20Sopenharmony_ci 40128c2ecf20Sopenharmony_ci /* Send it off, this clears delayed acks for us. */ 40138c2ecf20Sopenharmony_ci __tcp_transmit_skb(sk, buff, 0, (__force gfp_t)0, rcv_nxt); 40148c2ecf20Sopenharmony_ci} 40158c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(__tcp_send_ack); 40168c2ecf20Sopenharmony_ci 40178c2ecf20Sopenharmony_civoid tcp_send_ack(struct sock *sk) 40188c2ecf20Sopenharmony_ci{ 40198c2ecf20Sopenharmony_ci __tcp_send_ack(sk, tcp_sk(sk)->rcv_nxt); 40208c2ecf20Sopenharmony_ci} 40218c2ecf20Sopenharmony_ci 40228c2ecf20Sopenharmony_ci/* This routine sends a packet with an out of date sequence 40238c2ecf20Sopenharmony_ci * number. It assumes the other end will try to ack it. 40248c2ecf20Sopenharmony_ci * 40258c2ecf20Sopenharmony_ci * Question: what should we make while urgent mode? 40268c2ecf20Sopenharmony_ci * 4.4BSD forces sending single byte of data. We cannot send 40278c2ecf20Sopenharmony_ci * out of window data, because we have SND.NXT==SND.MAX... 40288c2ecf20Sopenharmony_ci * 40298c2ecf20Sopenharmony_ci * Current solution: to send TWO zero-length segments in urgent mode: 40308c2ecf20Sopenharmony_ci * one is with SEG.SEQ=SND.UNA to deliver urgent pointer, another is 40318c2ecf20Sopenharmony_ci * out-of-date with SND.UNA-1 to probe window. 40328c2ecf20Sopenharmony_ci */ 40338c2ecf20Sopenharmony_cistatic int tcp_xmit_probe_skb(struct sock *sk, int urgent, int mib) 40348c2ecf20Sopenharmony_ci{ 40358c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 40368c2ecf20Sopenharmony_ci struct sk_buff *skb; 40378c2ecf20Sopenharmony_ci 40388c2ecf20Sopenharmony_ci /* We don't queue it, tcp_transmit_skb() sets ownership. */ 40398c2ecf20Sopenharmony_ci skb = alloc_skb(MAX_TCP_HEADER, 40408c2ecf20Sopenharmony_ci sk_gfp_mask(sk, GFP_ATOMIC | __GFP_NOWARN)); 40418c2ecf20Sopenharmony_ci if (!skb) 40428c2ecf20Sopenharmony_ci return -1; 40438c2ecf20Sopenharmony_ci 40448c2ecf20Sopenharmony_ci /* Reserve space for headers and set control bits. */ 40458c2ecf20Sopenharmony_ci skb_reserve(skb, MAX_TCP_HEADER); 40468c2ecf20Sopenharmony_ci /* Use a previous sequence. This should cause the other 40478c2ecf20Sopenharmony_ci * end to send an ack. Don't queue or clone SKB, just 40488c2ecf20Sopenharmony_ci * send it. 40498c2ecf20Sopenharmony_ci */ 40508c2ecf20Sopenharmony_ci tcp_init_nondata_skb(skb, tp->snd_una - !urgent, TCPHDR_ACK); 40518c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), mib); 40528c2ecf20Sopenharmony_ci return tcp_transmit_skb(sk, skb, 0, (__force gfp_t)0); 40538c2ecf20Sopenharmony_ci} 40548c2ecf20Sopenharmony_ci 40558c2ecf20Sopenharmony_ci/* Called from setsockopt( ... TCP_REPAIR ) */ 40568c2ecf20Sopenharmony_civoid tcp_send_window_probe(struct sock *sk) 40578c2ecf20Sopenharmony_ci{ 40588c2ecf20Sopenharmony_ci if (sk->sk_state == TCP_ESTABLISHED) { 40598c2ecf20Sopenharmony_ci tcp_sk(sk)->snd_wl1 = tcp_sk(sk)->rcv_nxt - 1; 40608c2ecf20Sopenharmony_ci tcp_mstamp_refresh(tcp_sk(sk)); 40618c2ecf20Sopenharmony_ci tcp_xmit_probe_skb(sk, 0, LINUX_MIB_TCPWINPROBE); 40628c2ecf20Sopenharmony_ci } 40638c2ecf20Sopenharmony_ci} 40648c2ecf20Sopenharmony_ci 40658c2ecf20Sopenharmony_ci/* Initiate keepalive or window probe from timer. */ 40668c2ecf20Sopenharmony_ciint tcp_write_wakeup(struct sock *sk, int mib) 40678c2ecf20Sopenharmony_ci{ 40688c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 40698c2ecf20Sopenharmony_ci struct sk_buff *skb; 40708c2ecf20Sopenharmony_ci 40718c2ecf20Sopenharmony_ci if (sk->sk_state == TCP_CLOSE) 40728c2ecf20Sopenharmony_ci return -1; 40738c2ecf20Sopenharmony_ci 40748c2ecf20Sopenharmony_ci skb = tcp_send_head(sk); 40758c2ecf20Sopenharmony_ci if (skb && before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) { 40768c2ecf20Sopenharmony_ci int err; 40778c2ecf20Sopenharmony_ci unsigned int mss = tcp_current_mss(sk); 40788c2ecf20Sopenharmony_ci unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; 40798c2ecf20Sopenharmony_ci 40808c2ecf20Sopenharmony_ci if (before(tp->pushed_seq, TCP_SKB_CB(skb)->end_seq)) 40818c2ecf20Sopenharmony_ci tp->pushed_seq = TCP_SKB_CB(skb)->end_seq; 40828c2ecf20Sopenharmony_ci 40838c2ecf20Sopenharmony_ci /* We are probing the opening of a window 40848c2ecf20Sopenharmony_ci * but the window size is != 0 40858c2ecf20Sopenharmony_ci * must have been a result SWS avoidance ( sender ) 40868c2ecf20Sopenharmony_ci */ 40878c2ecf20Sopenharmony_ci if (seg_size < TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq || 40888c2ecf20Sopenharmony_ci skb->len > mss) { 40898c2ecf20Sopenharmony_ci seg_size = min(seg_size, mss); 40908c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH; 40918c2ecf20Sopenharmony_ci if (tcp_fragment(sk, TCP_FRAG_IN_WRITE_QUEUE, 40928c2ecf20Sopenharmony_ci skb, seg_size, mss, GFP_ATOMIC)) 40938c2ecf20Sopenharmony_ci return -1; 40948c2ecf20Sopenharmony_ci } else if (!tcp_skb_pcount(skb)) 40958c2ecf20Sopenharmony_ci tcp_set_skb_tso_segs(skb, mss); 40968c2ecf20Sopenharmony_ci 40978c2ecf20Sopenharmony_ci TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_PSH; 40988c2ecf20Sopenharmony_ci err = tcp_transmit_skb(sk, skb, 1, GFP_ATOMIC); 40998c2ecf20Sopenharmony_ci if (!err) 41008c2ecf20Sopenharmony_ci tcp_event_new_data_sent(sk, skb); 41018c2ecf20Sopenharmony_ci return err; 41028c2ecf20Sopenharmony_ci } else { 41038c2ecf20Sopenharmony_ci if (between(tp->snd_up, tp->snd_una + 1, tp->snd_una + 0xFFFF)) 41048c2ecf20Sopenharmony_ci tcp_xmit_probe_skb(sk, 1, mib); 41058c2ecf20Sopenharmony_ci return tcp_xmit_probe_skb(sk, 0, mib); 41068c2ecf20Sopenharmony_ci } 41078c2ecf20Sopenharmony_ci} 41088c2ecf20Sopenharmony_ci 41098c2ecf20Sopenharmony_ci/* A window probe timeout has occurred. If window is not closed send 41108c2ecf20Sopenharmony_ci * a partial packet else a zero probe. 41118c2ecf20Sopenharmony_ci */ 41128c2ecf20Sopenharmony_civoid tcp_send_probe0(struct sock *sk) 41138c2ecf20Sopenharmony_ci{ 41148c2ecf20Sopenharmony_ci struct inet_connection_sock *icsk = inet_csk(sk); 41158c2ecf20Sopenharmony_ci struct tcp_sock *tp = tcp_sk(sk); 41168c2ecf20Sopenharmony_ci unsigned long timeout; 41178c2ecf20Sopenharmony_ci int err; 41188c2ecf20Sopenharmony_ci 41198c2ecf20Sopenharmony_ci err = tcp_write_wakeup(sk, LINUX_MIB_TCPWINPROBE); 41208c2ecf20Sopenharmony_ci 41218c2ecf20Sopenharmony_ci if (tp->packets_out || tcp_write_queue_empty(sk)) { 41228c2ecf20Sopenharmony_ci /* Cancel probe timer, if it is not required. */ 41238c2ecf20Sopenharmony_ci icsk->icsk_probes_out = 0; 41248c2ecf20Sopenharmony_ci icsk->icsk_backoff = 0; 41258c2ecf20Sopenharmony_ci icsk->icsk_probes_tstamp = 0; 41268c2ecf20Sopenharmony_ci return; 41278c2ecf20Sopenharmony_ci } 41288c2ecf20Sopenharmony_ci 41298c2ecf20Sopenharmony_ci icsk->icsk_probes_out++; 41308c2ecf20Sopenharmony_ci if (err <= 0) { 41318c2ecf20Sopenharmony_ci if (icsk->icsk_backoff < READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_retries2)) 41328c2ecf20Sopenharmony_ci icsk->icsk_backoff++; 41338c2ecf20Sopenharmony_ci timeout = tcp_probe0_when(sk, TCP_RTO_MAX); 41348c2ecf20Sopenharmony_ci } else { 41358c2ecf20Sopenharmony_ci /* If packet was not sent due to local congestion, 41368c2ecf20Sopenharmony_ci * Let senders fight for local resources conservatively. 41378c2ecf20Sopenharmony_ci */ 41388c2ecf20Sopenharmony_ci timeout = TCP_RESOURCE_PROBE_INTERVAL; 41398c2ecf20Sopenharmony_ci } 41408c2ecf20Sopenharmony_ci 41418c2ecf20Sopenharmony_ci timeout = tcp_clamp_probe0_to_user_timeout(sk, timeout); 41428c2ecf20Sopenharmony_ci tcp_reset_xmit_timer(sk, ICSK_TIME_PROBE0, timeout, TCP_RTO_MAX); 41438c2ecf20Sopenharmony_ci} 41448c2ecf20Sopenharmony_ci 41458c2ecf20Sopenharmony_ciint tcp_rtx_synack(const struct sock *sk, struct request_sock *req) 41468c2ecf20Sopenharmony_ci{ 41478c2ecf20Sopenharmony_ci const struct tcp_request_sock_ops *af_ops = tcp_rsk(req)->af_specific; 41488c2ecf20Sopenharmony_ci struct flowi fl; 41498c2ecf20Sopenharmony_ci int res; 41508c2ecf20Sopenharmony_ci 41518c2ecf20Sopenharmony_ci tcp_rsk(req)->txhash = net_tx_rndhash(); 41528c2ecf20Sopenharmony_ci res = af_ops->send_synack(sk, NULL, &fl, req, NULL, TCP_SYNACK_NORMAL, 41538c2ecf20Sopenharmony_ci NULL); 41548c2ecf20Sopenharmony_ci if (!res) { 41558c2ecf20Sopenharmony_ci TCP_INC_STATS(sock_net(sk), TCP_MIB_RETRANSSEGS); 41568c2ecf20Sopenharmony_ci NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPSYNRETRANS); 41578c2ecf20Sopenharmony_ci if (unlikely(tcp_passive_fastopen(sk))) 41588c2ecf20Sopenharmony_ci tcp_sk(sk)->total_retrans++; 41598c2ecf20Sopenharmony_ci trace_tcp_retransmit_synack(sk, req); 41608c2ecf20Sopenharmony_ci } 41618c2ecf20Sopenharmony_ci return res; 41628c2ecf20Sopenharmony_ci} 41638c2ecf20Sopenharmony_ciEXPORT_SYMBOL(tcp_rtx_synack); 4164