1c398230bSWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4d7f570e6SGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31d7f570e6SGarrett Wollman * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 344b421e2dSMike Silbersack #include <sys/cdefs.h> 354b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 364b421e2dSMike Silbersack 371cfd4b53SBruce M Simpson #include "opt_inet.h" 38fb59c426SYoshinobu Inoue #include "opt_inet6.h" 393a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 40b2e60773SJohn Baldwin #include "opt_kern_tls.h" 410cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 420cc12cc5SJoerg Wunsch 43df8bae1dSRodney W. Grimes #include <sys/param.h> 44df8bae1dSRodney W. Grimes #include <sys/systm.h> 45686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h> 46bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 4739bc9de5SLawrence Stewart #include <sys/hhook.h> 48bd79708dSJonathan T. Looney #endif 49fb919e4dSMark Murray #include <sys/kernel.h> 50b2e60773SJohn Baldwin #ifdef KERN_TLS 51b2e60773SJohn Baldwin #include <sys/ktls.h> 52b2e60773SJohn Baldwin #endif 53fb919e4dSMark Murray #include <sys/lock.h> 54fb919e4dSMark Murray #include <sys/mbuf.h> 55fb919e4dSMark Murray #include <sys/mutex.h> 56df8bae1dSRodney W. Grimes #include <sys/protosw.h> 5757f60867SMark Johnston #include <sys/sdt.h> 58df8bae1dSRodney W. Grimes #include <sys/socket.h> 59df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 60fb919e4dSMark Murray #include <sys/sysctl.h> 61df8bae1dSRodney W. Grimes 624b79449eSBjoern A. Zeeb #include <net/if.h> 63df8bae1dSRodney W. Grimes #include <net/route.h> 64530c0060SRobert Watson #include <net/vnet.h> 65df8bae1dSRodney W. Grimes 66df8bae1dSRodney W. Grimes #include <netinet/in.h> 6757f60867SMark Johnston #include <netinet/in_kdtrace.h> 68df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 69df8bae1dSRodney W. Grimes #include <netinet/ip.h> 70df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 71df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 72ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 73fb59c426SYoshinobu Inoue #ifdef INET6 74686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 75686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 76fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 77fb59c426SYoshinobu Inoue #endif 782de3e790SGleb Smirnoff #include <netinet/tcp.h> 79a00f4ac2SGleb Smirnoff #define TCPOUTFLAGS 80df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 812529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 82df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 83df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 84df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 85df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 864644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 87c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 8886a996e6SHiren Panchasara #ifdef TCPPCAP 8986a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 9086a996e6SHiren Panchasara #endif 91610ee2f9SDavid Greenman #ifdef TCPDEBUG 92df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 93610ee2f9SDavid Greenman #endif 9409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 9509fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 9609fe6320SNavdeep Parhar #endif 97df8bae1dSRodney W. Grimes 98fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 99b9234fafSSam Leffler 100db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 101db4f9cc7SJonathan Lemon 102aed55708SRobert Watson #include <security/mac/mac_framework.h> 103aed55708SRobert Watson 10482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, path_mtu_discovery) = 1; 1056df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_VNET | CTLFLAG_RW, 106eddfbb76SRobert Watson &VNET_NAME(path_mtu_discovery), 1, 107eddfbb76SRobert Watson "Enable Path MTU Discovery"); 1089a039a5fSDavid Greenman 10982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_tso) = 1; 1106df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_VNET | CTLFLAG_RW, 111eddfbb76SRobert Watson &VNET_NAME(tcp_do_tso), 0, 112eddfbb76SRobert Watson "Enable TCP Segmentation Offload"); 113b3c0f300SAndre Oppermann 114873789cbSAndre Oppermann VNET_DEFINE(int, tcp_sendspace) = 1024*32; 115873789cbSAndre Oppermann #define V_tcp_sendspace VNET(tcp_sendspace) 1166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_VNET | CTLFLAG_RW, 117873789cbSAndre Oppermann &VNET_NAME(tcp_sendspace), 0, "Initial send socket buffer size"); 118873789cbSAndre Oppermann 11982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autosndbuf) = 1; 1206df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 121eddfbb76SRobert Watson &VNET_NAME(tcp_do_autosndbuf), 0, 122eddfbb76SRobert Watson "Enable automatic send buffer sizing"); 1236741ecf5SAndre Oppermann 12482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_inc) = 8*1024; 1256df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 126eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_inc), 0, 1278b615593SMarko Zec "Incrementor step size of automatic send buffer"); 1286741ecf5SAndre Oppermann 129b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_max) = 2*1024*1024; 1306df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 131eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_max), 0, 1328b615593SMarko Zec "Max size of automatic send buffer"); 1336741ecf5SAndre Oppermann 134ac952dd2SSean Bruno VNET_DEFINE(int, tcp_sendbuf_auto_lowat) = 0; 135ac952dd2SSean Bruno #define V_tcp_sendbuf_auto_lowat VNET(tcp_sendbuf_auto_lowat) 136ac952dd2SSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto_lowat, CTLFLAG_VNET | CTLFLAG_RW, 137ac952dd2SSean Bruno &VNET_NAME(tcp_sendbuf_auto_lowat), 0, 138ac952dd2SSean Bruno "Modify threshold for auto send buffer growth to account for SO_SNDLOWAT"); 139ac952dd2SSean Bruno 140425b7639SSepherosa Ziehau /* 141425b7639SSepherosa Ziehau * Make sure that either retransmit or persist timer is set for SYN, FIN and 142425b7639SSepherosa Ziehau * non-ACK. 143425b7639SSepherosa Ziehau */ 144425b7639SSepherosa Ziehau #define TCP_XMIT_TIMER_ASSERT(tp, len, th_flags) \ 145fd29ff5dSRandall Stewart KASSERT(((len) == 0 && ((th_flags) & (TH_SYN | TH_FIN)) == 0) ||\ 146425b7639SSepherosa Ziehau tcp_timer_active((tp), TT_REXMT) || \ 147425b7639SSepherosa Ziehau tcp_timer_active((tp), TT_PERSIST), \ 148425b7639SSepherosa Ziehau ("neither rexmt nor persist timer is set")) 149425b7639SSepherosa Ziehau 150dbc42409SLawrence Stewart static void inline cc_after_idle(struct tcpcb *tp); 151dbc42409SLawrence Stewart 152bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 153dbc42409SLawrence Stewart /* 1540f5e7edcSKevin Lo * Wrapper for the TCP established output helper hook. 15539bc9de5SLawrence Stewart */ 15689e560f4SRandall Stewart void 15739bc9de5SLawrence Stewart hhook_run_tcp_est_out(struct tcpcb *tp, struct tcphdr *th, 1583ac12506SJonathan T. Looney struct tcpopt *to, uint32_t len, int tso) 15939bc9de5SLawrence Stewart { 16039bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 16139bc9de5SLawrence Stewart 16239bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_OUT]->hhh_nhooks > 0) { 16339bc9de5SLawrence Stewart hhook_data.tp = tp; 16439bc9de5SLawrence Stewart hhook_data.th = th; 16539bc9de5SLawrence Stewart hhook_data.to = to; 16639bc9de5SLawrence Stewart hhook_data.len = len; 16739bc9de5SLawrence Stewart hhook_data.tso = tso; 16839bc9de5SLawrence Stewart 16939bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_OUT], &hhook_data, 17039bc9de5SLawrence Stewart tp->osd); 17139bc9de5SLawrence Stewart } 17239bc9de5SLawrence Stewart } 173bd79708dSJonathan T. Looney #endif 17439bc9de5SLawrence Stewart 17539bc9de5SLawrence Stewart /* 176dbc42409SLawrence Stewart * CC wrapper hook functions 177dbc42409SLawrence Stewart */ 178dbc42409SLawrence Stewart static void inline 179dbc42409SLawrence Stewart cc_after_idle(struct tcpcb *tp) 180dbc42409SLawrence Stewart { 181dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 182dbc42409SLawrence Stewart 183dbc42409SLawrence Stewart if (CC_ALGO(tp)->after_idle != NULL) 184dbc42409SLawrence Stewart CC_ALGO(tp)->after_idle(tp->ccv); 185dbc42409SLawrence Stewart } 1866741ecf5SAndre Oppermann 187df8bae1dSRodney W. Grimes /* 188df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 189df8bae1dSRodney W. Grimes */ 190df8bae1dSRodney W. Grimes int 191f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp) 192df8bae1dSRodney W. Grimes { 193f10e85d7SLuigi Rizzo struct socket *so = tp->t_inpcb->inp_socket; 1943ac12506SJonathan T. Looney int32_t len; 1953ac12506SJonathan T. Looney uint32_t recwin, sendwin; 196b287c6c7SBjoern A. Zeeb int off, flags, error = 0; /* Keep compiler happy */ 197581a046aSRandall Stewart u_int if_hw_tsomaxsegcount = 0; 198*82e837f8SWarner Losh u_int if_hw_tsomaxsegsize = 0; 199f10e85d7SLuigi Rizzo struct mbuf *m; 200fb59c426SYoshinobu Inoue struct ip *ip = NULL; 201151ba793SAlexander Kabaev #ifdef TCPDEBUG 202f10e85d7SLuigi Rizzo struct ipovly *ipov = NULL; 203151ba793SAlexander Kabaev #endif 204f10e85d7SLuigi Rizzo struct tcphdr *th; 205a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 206a04884fcSBill Fenner unsigned ipoptlen, optlen, hdrlen; 207fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 2089ad0173dSBjoern A. Zeeb unsigned ipsec_optlen = 0; 2099ad0173dSBjoern A. Zeeb #endif 21010d20c84SMatt Macy int idle, sendalot, curticks; 21102a1a643SAndre Oppermann int sack_rxmit, sack_bytes_rxmt; 2126d90faf3SPaul Saab struct sackhole *p; 213df0633a1SGleb Smirnoff int tso, mtu; 21402a1a643SAndre Oppermann struct tcpopt to; 215c560df6fSPatrick Kelsey unsigned int wanted_cookie = 0; 216c560df6fSPatrick Kelsey unsigned int dont_sendalot = 0; 217262c1c1aSMatthew Dillon #if 0 21846f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 219262c1c1aSMatthew Dillon #endif 220fb59c426SYoshinobu Inoue #ifdef INET6 221f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 222fb59c426SYoshinobu Inoue int isipv6; 223fb59c426SYoshinobu Inoue 224fb59c426SYoshinobu Inoue isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 225fb59c426SYoshinobu Inoue #endif 226b2e60773SJohn Baldwin #ifdef KERN_TLS 227b2e60773SJohn Baldwin const bool hw_tls = (so->so_snd.sb_flags & SB_TLS_IFNET) != 0; 228b2e60773SJohn Baldwin #else 229b2e60773SJohn Baldwin const bool hw_tls = false; 230b2e60773SJohn Baldwin #endif 231df8bae1dSRodney W. Grimes 2328501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2333d6ade3aSJennifer Yang 23409fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 23509fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 23609fe6320SNavdeep Parhar return (tcp_offload_output(tp)); 23709fe6320SNavdeep Parhar #endif 23809fe6320SNavdeep Parhar 239281a0fd4SPatrick Kelsey /* 2408db239dcSMichael Tuexen * For TFO connections in SYN_SENT or SYN_RECEIVED, 2418db239dcSMichael Tuexen * only allow the initial SYN or SYN|ACK and those sent 2428db239dcSMichael Tuexen * by the retransmit timer. 243281a0fd4SPatrick Kelsey */ 24468bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 2458db239dcSMichael Tuexen ((tp->t_state == TCPS_SYN_SENT) || 2468db239dcSMichael Tuexen (tp->t_state == TCPS_SYN_RECEIVED)) && 2478db239dcSMichael Tuexen SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 248281a0fd4SPatrick Kelsey (tp->snd_nxt != tp->snd_una)) /* not a retransmit */ 249281a0fd4SPatrick Kelsey return (0); 25018a75309SPatrick Kelsey 251df8bae1dSRodney W. Grimes /* 252df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 253df8bae1dSRodney W. Grimes * and flags that will be used. 254df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 255df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 256df8bae1dSRodney W. Grimes */ 257c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 2582ea8da28SLawrence Stewart if (idle && ticks - tp->t_rcvtime >= tp->t_rxtcur) 2592ea8da28SLawrence Stewart cc_after_idle(tp); 260c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 261c24d5daeSJayanth Vijayaraghavan if (idle) { 262c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 263c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 264c24d5daeSJayanth Vijayaraghavan idle = 0; 265c24d5daeSJayanth Vijayaraghavan } 266c24d5daeSJayanth Vijayaraghavan } 267df8bae1dSRodney W. Grimes again: 2686d90faf3SPaul Saab /* 2696d90faf3SPaul Saab * If we've recently taken a timeout, snd_max will be greater than 2706d90faf3SPaul Saab * snd_nxt. There may be SACK information that allows us to avoid 2716d90faf3SPaul Saab * resending already delivered data. Adjust snd_nxt accordingly. 2726d90faf3SPaul Saab */ 2733529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2743529149eSAndre Oppermann SEQ_LT(tp->snd_nxt, tp->snd_max)) 2756d90faf3SPaul Saab tcp_sack_adjust(tp); 276df8bae1dSRodney W. Grimes sendalot = 0; 277153e5b57SAndre Oppermann tso = 0; 278df0633a1SGleb Smirnoff mtu = 0; 279df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 280201d185bSAndre Oppermann sendwin = min(tp->snd_wnd, tp->snd_cwnd); 281df8bae1dSRodney W. Grimes 282df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 283a0292f23SGarrett Wollman /* 2846d90faf3SPaul Saab * Send any SACK-generated retransmissions. If we're explicitly trying 2856d90faf3SPaul Saab * to send out new data (when sendalot is 1), bypass this function. 2866d90faf3SPaul Saab * If we retransmit in fast recovery mode, decrement snd_cwnd, since 2876d90faf3SPaul Saab * we're replacing a (future) new transmission with a retransmission 2886d90faf3SPaul Saab * now, and we previously incremented snd_cwnd in tcp_input(). 2896d90faf3SPaul Saab */ 2906d90faf3SPaul Saab /* 2916d90faf3SPaul Saab * Still in sack recovery , reset rxmit flag to zero. 2926d90faf3SPaul Saab */ 2936d90faf3SPaul Saab sack_rxmit = 0; 294a55db2b6SPaul Saab sack_bytes_rxmt = 0; 2956d90faf3SPaul Saab len = 0; 2966d90faf3SPaul Saab p = NULL; 297dbc42409SLawrence Stewart if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp->t_flags) && 298a55db2b6SPaul Saab (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 2993ac12506SJonathan T. Looney uint32_t cwin; 300a55db2b6SPaul Saab 3013ac12506SJonathan T. Looney cwin = 3023ac12506SJonathan T. Looney imax(min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt, 0); 303f787edd8SJayanth Vijayaraghavan /* Do not retransmit SACK segments beyond snd_recover */ 304f787edd8SJayanth Vijayaraghavan if (SEQ_GT(p->end, tp->snd_recover)) { 305f787edd8SJayanth Vijayaraghavan /* 306f787edd8SJayanth Vijayaraghavan * (At least) part of sack hole extends beyond 307f787edd8SJayanth Vijayaraghavan * snd_recover. Check to see if we can rexmit data 308f787edd8SJayanth Vijayaraghavan * for this hole. 309f787edd8SJayanth Vijayaraghavan */ 310f787edd8SJayanth Vijayaraghavan if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 311f787edd8SJayanth Vijayaraghavan /* 312f787edd8SJayanth Vijayaraghavan * Can't rexmit any more data for this hole. 313f787edd8SJayanth Vijayaraghavan * That data will be rexmitted in the next 314f787edd8SJayanth Vijayaraghavan * sack recovery episode, when snd_recover 315f787edd8SJayanth Vijayaraghavan * moves past p->rxmit. 316f787edd8SJayanth Vijayaraghavan */ 317f787edd8SJayanth Vijayaraghavan p = NULL; 318f787edd8SJayanth Vijayaraghavan goto after_sack_rexmit; 319f787edd8SJayanth Vijayaraghavan } else 320f787edd8SJayanth Vijayaraghavan /* Can rexmit part of the current hole */ 3213ac12506SJonathan T. Looney len = ((int32_t)ulmin(cwin, 322f787edd8SJayanth Vijayaraghavan tp->snd_recover - p->rxmit)); 323f787edd8SJayanth Vijayaraghavan } else 3243ac12506SJonathan T. Looney len = ((int32_t)ulmin(cwin, p->end - p->rxmit)); 3256d90faf3SPaul Saab off = p->rxmit - tp->snd_una; 326f787edd8SJayanth Vijayaraghavan KASSERT(off >= 0,("%s: sack block to the left of una : %d", 327f787edd8SJayanth Vijayaraghavan __func__, off)); 3286d90faf3SPaul Saab if (len > 0) { 329ab5c14d8SRobert Watson sack_rxmit = 1; 330ab5c14d8SRobert Watson sendalot = 1; 33178b50714SRobert Watson TCPSTAT_INC(tcps_sack_rexmits); 33278b50714SRobert Watson TCPSTAT_ADD(tcps_sack_rexmit_bytes, 33378b50714SRobert Watson min(len, tp->t_maxseg)); 3346d90faf3SPaul Saab } 3356d90faf3SPaul Saab } 336f787edd8SJayanth Vijayaraghavan after_sack_rexmit: 3376d90faf3SPaul Saab /* 338a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 339a0292f23SGarrett Wollman * state flags. 340a0292f23SGarrett Wollman */ 341a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 342a0292f23SGarrett Wollman flags |= TH_FIN; 343a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 344a0292f23SGarrett Wollman flags |= TH_SYN; 345a0292f23SGarrett Wollman 346cf2942b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 347df8bae1dSRodney W. Grimes /* 348df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 349df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 350df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 351df8bae1dSRodney W. Grimes * and go to transmit state. 352df8bae1dSRodney W. Grimes */ 3532cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) { 354201d185bSAndre Oppermann if (sendwin == 0) { 355df8bae1dSRodney W. Grimes /* 356df8bae1dSRodney W. Grimes * If we still have some data to send, then 357df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 358df8bae1dSRodney W. Grimes * happen below when it realizes that we 359df8bae1dSRodney W. Grimes * aren't sending all the data. However, 36029089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 361df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 362df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 363df8bae1dSRodney W. Grimes * up sending the packet without recording 364df8bae1dSRodney W. Grimes * that we sent the FIN bit. 365df8bae1dSRodney W. Grimes * 366df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 367df8bae1dSRodney W. Grimes * because if we don't have any more data 368df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 369df8bae1dSRodney W. Grimes * itself. 370df8bae1dSRodney W. Grimes */ 371cfa6009eSGleb Smirnoff if (off < sbused(&so->so_snd)) 372df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 373201d185bSAndre Oppermann sendwin = 1; 374df8bae1dSRodney W. Grimes } else { 375b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, 0); 376df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 377df8bae1dSRodney W. Grimes } 378df8bae1dSRodney W. Grimes } 379df8bae1dSRodney W. Grimes 38028257b5cSMatthew Dillon /* 38128257b5cSMatthew Dillon * If snd_nxt == snd_max and we have transmitted a FIN, the 38228257b5cSMatthew Dillon * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 383201d185bSAndre Oppermann * a negative length. This can also occur when TCP opens up 38428257b5cSMatthew Dillon * its congestion window while receiving additional duplicate 38528257b5cSMatthew Dillon * acks after fast-retransmit because TCP will reset snd_nxt 38628257b5cSMatthew Dillon * to snd_max after the fast-retransmit. 38728257b5cSMatthew Dillon * 38828257b5cSMatthew Dillon * In the normal retransmit-FIN-only case, however, snd_nxt will 38928257b5cSMatthew Dillon * be set to snd_una, the offset will be 0, and the length may 39028257b5cSMatthew Dillon * wind up 0. 3916d90faf3SPaul Saab * 3926d90faf3SPaul Saab * If sack_rxmit is true we are retransmitting from the scoreboard 3936d90faf3SPaul Saab * in which case len is already set. 39428257b5cSMatthew Dillon */ 395a55db2b6SPaul Saab if (sack_rxmit == 0) { 396a55db2b6SPaul Saab if (sack_bytes_rxmt == 0) 397cb503ae2SJonathan T. Looney len = ((int32_t)min(sbavail(&so->so_snd), sendwin) - 398cfa6009eSGleb Smirnoff off); 399a55db2b6SPaul Saab else { 4003ac12506SJonathan T. Looney int32_t cwin; 401a55db2b6SPaul Saab 402a55db2b6SPaul Saab /* 403a55db2b6SPaul Saab * We are inside of a SACK recovery episode and are 404a55db2b6SPaul Saab * sending new data, having retransmitted all the 405a55db2b6SPaul Saab * data possible in the scoreboard. 406a55db2b6SPaul Saab */ 4073ac12506SJonathan T. Looney len = ((int32_t)min(sbavail(&so->so_snd), tp->snd_wnd) - 408cfa6009eSGleb Smirnoff off); 4098d03f2b5SPaul Saab /* 4108d03f2b5SPaul Saab * Don't remove this (len > 0) check ! 4118d03f2b5SPaul Saab * We explicitly check for len > 0 here (although it 4128d03f2b5SPaul Saab * isn't really necessary), to work around a gcc 4138d03f2b5SPaul Saab * optimization issue - to force gcc to compute 4148d03f2b5SPaul Saab * len above. Without this check, the computation 4158d03f2b5SPaul Saab * of len is bungled by the optimizer. 4168d03f2b5SPaul Saab */ 4178d03f2b5SPaul Saab if (len > 0) { 4187d5ed1ceSPaul Saab cwin = tp->snd_cwnd - 4197d5ed1ceSPaul Saab (tp->snd_nxt - tp->sack_newdata) - 420a55db2b6SPaul Saab sack_bytes_rxmt; 421a55db2b6SPaul Saab if (cwin < 0) 422a55db2b6SPaul Saab cwin = 0; 4233ac12506SJonathan T. Looney len = imin(len, cwin); 424a55db2b6SPaul Saab } 425a55db2b6SPaul Saab } 4268d03f2b5SPaul Saab } 427a0292f23SGarrett Wollman 428a0292f23SGarrett Wollman /* 429a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 430a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 431a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 432a0292f23SGarrett Wollman */ 433a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 4344b8e98d6SQing Li if (tp->t_state != TCPS_SYN_RECEIVED) 435a0292f23SGarrett Wollman flags &= ~TH_SYN; 436281a0fd4SPatrick Kelsey /* 437281a0fd4SPatrick Kelsey * When sending additional segments following a TFO SYN|ACK, 438281a0fd4SPatrick Kelsey * do not include the SYN bit. 439281a0fd4SPatrick Kelsey */ 44068bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 441281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 442281a0fd4SPatrick Kelsey flags &= ~TH_SYN; 443a0292f23SGarrett Wollman off--, len++; 444a0292f23SGarrett Wollman } 445a0292f23SGarrett Wollman 44681165e48SAndras Olah /* 447c94c54e4SAndre Oppermann * Be careful not to send data and/or FIN on SYN segments. 44881165e48SAndras Olah * This measure is needed to prevent interoperability problems 44981165e48SAndras Olah * with not fully conformant TCP implementations. 45081165e48SAndras Olah */ 451c94c54e4SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 45281165e48SAndras Olah len = 0; 45381165e48SAndras Olah flags &= ~TH_FIN; 45481165e48SAndras Olah } 45581165e48SAndras Olah 456281a0fd4SPatrick Kelsey /* 457c560df6fSPatrick Kelsey * On TFO sockets, ensure no data is sent in the following cases: 458c560df6fSPatrick Kelsey * 459c560df6fSPatrick Kelsey * - When retransmitting SYN|ACK on a passively-created socket 460c560df6fSPatrick Kelsey * 461c560df6fSPatrick Kelsey * - When retransmitting SYN on an actively created socket 462c560df6fSPatrick Kelsey * 463c560df6fSPatrick Kelsey * - When sending a zero-length cookie (cookie request) on an 464c560df6fSPatrick Kelsey * actively created socket 465c560df6fSPatrick Kelsey * 466c560df6fSPatrick Kelsey * - When the socket is in the CLOSED state (RST is being sent) 467281a0fd4SPatrick Kelsey */ 46868bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 469c560df6fSPatrick Kelsey (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 470c560df6fSPatrick Kelsey ((tp->t_state == TCPS_SYN_SENT) && 471c560df6fSPatrick Kelsey (tp->t_tfo_client_cookie_len == 0)) || 472281a0fd4SPatrick Kelsey (flags & TH_RST))) 473281a0fd4SPatrick Kelsey len = 0; 47447a8e865SXin LI if (len <= 0) { 475df8bae1dSRodney W. Grimes /* 476df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 477df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 47828257b5cSMatthew Dillon * len will be < 0. Otherwise, window shrank 479df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 4802d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 4812d8266afSDavid Greenman * to (closed) window, and set the persist timer 4822d8266afSDavid Greenman * if it isn't already going. If the window didn't 4832d8266afSDavid Greenman * close completely, just wait for an ACK. 48447a8e865SXin LI * 48547a8e865SXin LI * We also do a general check here to ensure that 48647a8e865SXin LI * we will set the persist timer when we have data 48747a8e865SXin LI * to send, but a 0-byte window. This makes sure 48847a8e865SXin LI * the persist timer is set even if the packet 48947a8e865SXin LI * hits one of the "goto send" lines below. 490df8bae1dSRodney W. Grimes */ 491df8bae1dSRodney W. Grimes len = 0; 49247a8e865SXin LI if ((sendwin == 0) && (TCPS_HAVEESTABLISHED(tp->t_state)) && 49347a8e865SXin LI (off < (int) sbavail(&so->so_snd))) { 494b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 4952d8266afSDavid Greenman tp->t_rxtshift = 0; 496df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 497b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_PERSIST)) 4982d8266afSDavid Greenman tcp_setpersist(tp); 499df8bae1dSRodney W. Grimes } 500df8bae1dSRodney W. Grimes } 50128257b5cSMatthew Dillon 5026741ecf5SAndre Oppermann /* len will be >= 0 after this point. */ 503c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 5046741ecf5SAndre Oppermann 50566492feaSGleb Smirnoff tcp_sndbuf_autoscale(tp, so, sendwin); 5066741ecf5SAndre Oppermann 5076741ecf5SAndre Oppermann /* 508ed420311SAndre Oppermann * Decide if we can use TCP Segmentation Offloading (if supported by 509ed420311SAndre Oppermann * hardware). 510b3c0f300SAndre Oppermann * 511b3c0f300SAndre Oppermann * TSO may only be used if we are in a pure bulk sending state. The 512b3c0f300SAndre Oppermann * presence of TCP-MD5, SACK retransmits, SACK advertizements and 513b3c0f300SAndre Oppermann * IP options prevent using TSO. With TSO the TCP header is the same 514b3c0f300SAndre Oppermann * (except for the sequence number) for all generated packets. This 515b3c0f300SAndre Oppermann * makes it impossible to transmit any options which vary per generated 516b3c0f300SAndre Oppermann * segment or packet. 517b6ff6724SHiren Panchasara * 518b6ff6724SHiren Panchasara * IPv4 handling has a clear separation of ip options and ip header 519b6ff6724SHiren Panchasara * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does 520b6ff6724SHiren Panchasara * the right thing below to provide length of just ip options and thus 521b6ff6724SHiren Panchasara * checking for ipoptlen is enough to decide if ip options are present. 52228257b5cSMatthew Dillon */ 523fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 5249ad0173dSBjoern A. Zeeb /* 5259ad0173dSBjoern A. Zeeb * Pre-calculate here as we save another lookup into the darknesses 5269ad0173dSBjoern A. Zeeb * of IPsec that way and can actually decide if TSO is ok. 5279ad0173dSBjoern A. Zeeb */ 528fcf59617SAndrey V. Elsukov #ifdef INET6 529fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6)) 530fcf59617SAndrey V. Elsukov ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb); 531fcf59617SAndrey V. Elsukov #ifdef INET 532fcf59617SAndrey V. Elsukov else 5339ad0173dSBjoern A. Zeeb #endif 534fcf59617SAndrey V. Elsukov #endif /* INET6 */ 535fcf59617SAndrey V. Elsukov #ifdef INET 536fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) 537fcf59617SAndrey V. Elsukov ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb); 538fcf59617SAndrey V. Elsukov #endif /* INET */ 539fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 540b6ff6724SHiren Panchasara #ifdef INET6 541b6ff6724SHiren Panchasara if (isipv6) 542b6ff6724SHiren Panchasara ipoptlen = ip6_optlen(tp->t_inpcb); 543b6ff6724SHiren Panchasara else 544b6ff6724SHiren Panchasara #endif 545b6ff6724SHiren Panchasara if (tp->t_inpcb->inp_options) 546b6ff6724SHiren Panchasara ipoptlen = tp->t_inpcb->inp_options->m_len - 547b6ff6724SHiren Panchasara offsetof(struct ipoption, ipopt_list); 548b6ff6724SHiren Panchasara else 549b6ff6724SHiren Panchasara ipoptlen = 0; 550fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 551b6ff6724SHiren Panchasara ipoptlen += ipsec_optlen; 552b6ff6724SHiren Panchasara #endif 553b6ff6724SHiren Panchasara 554ed420311SAndre Oppermann if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg && 555b3c0f300SAndre Oppermann ((tp->t_flags & TF_SIGNATURE) == 0) && 556b3c0f300SAndre Oppermann tp->rcv_numsacks == 0 && sack_rxmit == 0 && 557c560df6fSPatrick Kelsey ipoptlen == 0 && !(flags & TH_SYN)) 558b3c0f300SAndre Oppermann tso = 1; 559153e5b57SAndre Oppermann 5605d3b1b75SJayanth Vijayaraghavan if (sack_rxmit) { 561cfa6009eSGleb Smirnoff if (SEQ_LT(p->rxmit + len, tp->snd_una + sbused(&so->so_snd))) 562df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 5635d3b1b75SJayanth Vijayaraghavan } else { 564cfa6009eSGleb Smirnoff if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + 565cfa6009eSGleb Smirnoff sbused(&so->so_snd))) 5665d3b1b75SJayanth Vijayaraghavan flags &= ~TH_FIN; 5675d3b1b75SJayanth Vijayaraghavan } 568df8bae1dSRodney W. Grimes 5693ac12506SJonathan T. Looney recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 5703ac12506SJonathan T. Looney (long)TCP_MAXWIN << tp->rcv_scale); 571df8bae1dSRodney W. Grimes 572df8bae1dSRodney W. Grimes /* 573262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 574262c1c1aSMatthew Dillon * conditions when len is non-zero: 575262c1c1aSMatthew Dillon * 576b3c0f300SAndre Oppermann * - We have a full segment (or more with TSO) 577262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 578262c1c1aSMatthew Dillon * either idle or running NODELAY 579262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 580262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 581262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 582262c1c1aSMatthew Dillon * - we need to retransmit 583df8bae1dSRodney W. Grimes */ 584df8bae1dSRodney W. Grimes if (len) { 585b3c0f300SAndre Oppermann if (len >= tp->t_maxseg) 586df8bae1dSRodney W. Grimes goto send; 587262c1c1aSMatthew Dillon /* 588262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 589262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 590262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 591262c1c1aSMatthew Dillon * 592262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 593262c1c1aSMatthew Dillon */ 594262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 595262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 5963ac12506SJonathan T. Looney (uint32_t)len + (uint32_t)off >= sbavail(&so->so_snd) && 597262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 598df8bae1dSRodney W. Grimes goto send; 599262c1c1aSMatthew Dillon } 6002cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */ 601df8bae1dSRodney W. Grimes goto send; 602a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 603df8bae1dSRodney W. Grimes goto send; 604262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 605df8bae1dSRodney W. Grimes goto send; 6066d90faf3SPaul Saab if (sack_rxmit) 6076d90faf3SPaul Saab goto send; 608df8bae1dSRodney W. Grimes } 609df8bae1dSRodney W. Grimes 610df8bae1dSRodney W. Grimes /* 611f62563d3SAndre Oppermann * Sending of standalone window updates. 612f62563d3SAndre Oppermann * 61378f59b4bSAndre Oppermann * Window updates are important when we close our window due to a 61478f59b4bSAndre Oppermann * full socket buffer and are opening it again after the application 615f62563d3SAndre Oppermann * reads data from it. Once the window has opened again and the 616f62563d3SAndre Oppermann * remote end starts to send again the ACK clock takes over and 617f62563d3SAndre Oppermann * provides the most current window information. 618f62563d3SAndre Oppermann * 61978f59b4bSAndre Oppermann * We must avoid the silly window syndrome whereas every read 620f62563d3SAndre Oppermann * from the receive buffer, no matter how small, causes a window 621f62563d3SAndre Oppermann * update to be sent. We also should avoid sending a flurry of 622f62563d3SAndre Oppermann * window updates when the socket buffer had queued a lot of data 623f62563d3SAndre Oppermann * and the application is doing small reads. 624f62563d3SAndre Oppermann * 625f62563d3SAndre Oppermann * Prevent a flurry of pointless window updates by only sending 626f62563d3SAndre Oppermann * an update when we can increase the advertized window by more 627f62563d3SAndre Oppermann * than 1/4th of the socket buffer capacity. When the buffer is 628f62563d3SAndre Oppermann * getting full or is very small be more aggressive and send an 629f62563d3SAndre Oppermann * update whenever we can increase by two mss sized segments. 630f62563d3SAndre Oppermann * In all other situations the ACK's to new incoming data will 631f62563d3SAndre Oppermann * carry further window increases. 6324249614cSAndre Oppermann * 6334249614cSAndre Oppermann * Don't send an independent window update if a delayed 6344249614cSAndre Oppermann * ACK is pending (it will get piggy-backed on it) or the 6354249614cSAndre Oppermann * remote side already has done a half-close and won't send 636f62563d3SAndre Oppermann * more data. Skip this if the connection is in T/TCP 637f62563d3SAndre Oppermann * half-open state. 638df8bae1dSRodney W. Grimes */ 639b7de7d87SAndre Oppermann if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 6404249614cSAndre Oppermann !(tp->t_flags & TF_DELACK) && 641b7de7d87SAndre Oppermann !TCPS_HAVERCVDFIN(tp->t_state)) { 642df8bae1dSRodney W. Grimes /* 643f62563d3SAndre Oppermann * "adv" is the amount we could increase the window, 644df8bae1dSRodney W. Grimes * taking into account that we are limited by 645df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 646df8bae1dSRodney W. Grimes */ 6473ac12506SJonathan T. Looney int32_t adv; 648f701e30dSJohn Baldwin int oldwin; 649f701e30dSJohn Baldwin 6503ac12506SJonathan T. Looney adv = recwin; 651f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 652f701e30dSJohn Baldwin oldwin = (tp->rcv_adv - tp->rcv_nxt); 653f701e30dSJohn Baldwin adv -= oldwin; 654f701e30dSJohn Baldwin } else 655f701e30dSJohn Baldwin oldwin = 0; 656df8bae1dSRodney W. Grimes 657da84b2e6SJohn Baldwin /* 6580dda76b8SJonathan T. Looney * If the new window size ends up being the same as or less 6590dda76b8SJonathan T. Looney * than the old size when it is scaled, then don't force 6600dda76b8SJonathan T. Looney * a window update. 661da84b2e6SJohn Baldwin */ 6620dda76b8SJonathan T. Looney if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 663da84b2e6SJohn Baldwin goto dontupdate; 664f62563d3SAndre Oppermann 6653ac12506SJonathan T. Looney if (adv >= (int32_t)(2 * tp->t_maxseg) && 6663ac12506SJonathan T. Looney (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) || 6673ac12506SJonathan T. Looney recwin <= (so->so_rcv.sb_hiwat / 8) || 66850075939SStephen Hurd so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg || 66950075939SStephen Hurd adv >= TCP_MAXWIN << tp->rcv_scale)) 670df8bae1dSRodney W. Grimes goto send; 6718d62aae8SMichael Tuexen if (2 * adv >= (int32_t)so->so_rcv.sb_hiwat) 6728d62aae8SMichael Tuexen goto send; 673df8bae1dSRodney W. Grimes } 674da84b2e6SJohn Baldwin dontupdate: 675df8bae1dSRodney W. Grimes 676df8bae1dSRodney W. Grimes /* 67728257b5cSMatthew Dillon * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 67828257b5cSMatthew Dillon * is also a catch-all for the retransmit timer timeout case. 679df8bae1dSRodney W. Grimes */ 680df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 681df8bae1dSRodney W. Grimes goto send; 682a0292f23SGarrett Wollman if ((flags & TH_RST) || 683a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 684df8bae1dSRodney W. Grimes goto send; 685df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 686df8bae1dSRodney W. Grimes goto send; 687df8bae1dSRodney W. Grimes /* 688df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 68928257b5cSMatthew Dillon * and we have not yet done so, then we need to send. 690df8bae1dSRodney W. Grimes */ 691df8bae1dSRodney W. Grimes if (flags & TH_FIN && 692df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 693df8bae1dSRodney W. Grimes goto send; 6946d90faf3SPaul Saab /* 6956d90faf3SPaul Saab * In SACK, it is possible for tcp_output to fail to send a segment 6966d90faf3SPaul Saab * after the retransmission timer has been turned off. Make sure 6976d90faf3SPaul Saab * that the retransmission timer is set. 6986d90faf3SPaul Saab */ 6993529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 7003529149eSAndre Oppermann SEQ_GT(tp->snd_max, tp->snd_una) && 701b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_REXMT) && 702b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 703b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 704cf2942b6SRobert Watson goto just_return; 7056d90faf3SPaul Saab } 706df8bae1dSRodney W. Grimes /* 707df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 708df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 709df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 710df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 711df8bae1dSRodney W. Grimes * persisting to move a small or zero window 712df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 713df8bae1dSRodney W. Grimes * 714b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_PERSIST) 7159b8b58e0SJonathan Lemon * is true when we are in persist state. 7162cdbfa66SPaul Saab * (tp->t_flags & TF_FORCEDATA) 717df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 718b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_REXMT) 719df8bae1dSRodney W. Grimes * is set when we are retransmitting 720df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 721df8bae1dSRodney W. Grimes * 722df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 723df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 724df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 725df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 726df8bae1dSRodney W. Grimes * otherwise force out a byte. 727df8bae1dSRodney W. Grimes */ 728cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd) && !tcp_timer_active(tp, TT_REXMT) && 729b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 730df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 731df8bae1dSRodney W. Grimes tcp_setpersist(tp); 732df8bae1dSRodney W. Grimes } 733df8bae1dSRodney W. Grimes 734df8bae1dSRodney W. Grimes /* 735df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 736df8bae1dSRodney W. Grimes */ 737cf2942b6SRobert Watson just_return: 738cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 739df8bae1dSRodney W. Grimes return (0); 740df8bae1dSRodney W. Grimes 741df8bae1dSRodney W. Grimes send: 742cf2942b6SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 743f6f6703fSSean Bruno if (len > 0) { 744f6f6703fSSean Bruno if (len >= tp->t_maxseg) 745f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 746f6f6703fSSean Bruno else 747f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 748f6f6703fSSean Bruno } 749df8bae1dSRodney W. Grimes /* 750df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 751df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 752df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 753df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 754df8bae1dSRodney W. Grimes * link header, i.e. 75533841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 756df8bae1dSRodney W. Grimes */ 757df8bae1dSRodney W. Grimes optlen = 0; 758fb59c426SYoshinobu Inoue #ifdef INET6 759fb59c426SYoshinobu Inoue if (isipv6) 760fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 761fb59c426SYoshinobu Inoue else 762fb59c426SYoshinobu Inoue #endif 763df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 76402a1a643SAndre Oppermann 76502a1a643SAndre Oppermann /* 76602a1a643SAndre Oppermann * Compute options for segment. 76702a1a643SAndre Oppermann * We only have to care about SYN and established connection 76802a1a643SAndre Oppermann * segments. Options for SYN-ACK segments are handled in TCP 76902a1a643SAndre Oppermann * syncache. 77002a1a643SAndre Oppermann */ 77102a1a643SAndre Oppermann to.to_flags = 0; 772f73d9fd2SGleb Smirnoff if ((tp->t_flags & TF_NOOPT) == 0) { 77302a1a643SAndre Oppermann /* Maximum segment size. */ 774df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 775df8bae1dSRodney W. Grimes tp->snd_nxt = tp->iss; 77602a1a643SAndre Oppermann to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc); 77702a1a643SAndre Oppermann to.to_flags |= TOF_MSS; 77818a75309SPatrick Kelsey 779281a0fd4SPatrick Kelsey /* 780c560df6fSPatrick Kelsey * On SYN or SYN|ACK transmits on TFO connections, 781c560df6fSPatrick Kelsey * only include the TFO option if it is not a 782c560df6fSPatrick Kelsey * retransmit, as the presence of the TFO option may 783c560df6fSPatrick Kelsey * have caused the original SYN or SYN|ACK to have 784c560df6fSPatrick Kelsey * been dropped by a middlebox. 785281a0fd4SPatrick Kelsey */ 78668bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 787281a0fd4SPatrick Kelsey (tp->t_rxtshift == 0)) { 788c560df6fSPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED) { 789281a0fd4SPatrick Kelsey to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 790c560df6fSPatrick Kelsey to.to_tfo_cookie = 791c560df6fSPatrick Kelsey (u_int8_t *)&tp->t_tfo_cookie.server; 792281a0fd4SPatrick Kelsey to.to_flags |= TOF_FASTOPEN; 793c560df6fSPatrick Kelsey wanted_cookie = 1; 794c560df6fSPatrick Kelsey } else if (tp->t_state == TCPS_SYN_SENT) { 795c560df6fSPatrick Kelsey to.to_tfo_len = 796c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len; 797c560df6fSPatrick Kelsey to.to_tfo_cookie = 798c560df6fSPatrick Kelsey tp->t_tfo_cookie.client; 799c560df6fSPatrick Kelsey to.to_flags |= TOF_FASTOPEN; 800c560df6fSPatrick Kelsey wanted_cookie = 1; 801c560df6fSPatrick Kelsey /* 802c560df6fSPatrick Kelsey * If we wind up having more data to 803c560df6fSPatrick Kelsey * send with the SYN than can fit in 804c560df6fSPatrick Kelsey * one segment, don't send any more 805c560df6fSPatrick Kelsey * until the SYN|ACK comes back from 806c560df6fSPatrick Kelsey * the other end. 807c560df6fSPatrick Kelsey */ 808c560df6fSPatrick Kelsey dont_sendalot = 1; 809c560df6fSPatrick Kelsey } 810281a0fd4SPatrick Kelsey } 811df8bae1dSRodney W. Grimes } 81202a1a643SAndre Oppermann /* Window scaling. */ 81302a1a643SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 81402a1a643SAndre Oppermann to.to_wscale = tp->request_r_scale; 81502a1a643SAndre Oppermann to.to_flags |= TOF_SCALE; 816df8bae1dSRodney W. Grimes } 81702a1a643SAndre Oppermann /* Timestamps. */ 81802a1a643SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) || 81902a1a643SAndre Oppermann ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 82010d20c84SMatt Macy curticks = tcp_ts_getticks(); 82110d20c84SMatt Macy to.to_tsval = curticks + tp->ts_offset; 82202a1a643SAndre Oppermann to.to_tsecr = tp->ts_recent; 82302a1a643SAndre Oppermann to.to_flags |= TOF_TS; 82410d20c84SMatt Macy if (tp->t_rxtshift == 1) 82510d20c84SMatt Macy tp->t_badrxtwin = curticks; 826e44c1887SSteven Hartland } 827e44c1887SSteven Hartland 8286741ecf5SAndre Oppermann /* Set receive buffer autosizing timestamp. */ 82902a1a643SAndre Oppermann if (tp->rfbuf_ts == 0 && 83002a1a643SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) 831d8951c8aSBjoern A. Zeeb tp->rfbuf_ts = tcp_ts_getticks(); 832e44c1887SSteven Hartland 83302a1a643SAndre Oppermann /* Selective ACK's. */ 8343529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 83502a1a643SAndre Oppermann if (flags & TH_SYN) 83602a1a643SAndre Oppermann to.to_flags |= TOF_SACKPERM; 83702a1a643SAndre Oppermann else if (TCPS_HAVEESTABLISHED(tp->t_state) && 83802a1a643SAndre Oppermann (tp->t_flags & TF_SACK_PERMIT) && 83902a1a643SAndre Oppermann tp->rcv_numsacks > 0) { 84002a1a643SAndre Oppermann to.to_flags |= TOF_SACK; 84102a1a643SAndre Oppermann to.to_nsacks = tp->rcv_numsacks; 84202a1a643SAndre Oppermann to.to_sacks = (u_char *)tp->sackblks; 84302a1a643SAndre Oppermann } 84402a1a643SAndre Oppermann } 845fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 84602a1a643SAndre Oppermann /* TCP-MD5 (RFC2385). */ 847fcf59617SAndrey V. Elsukov /* 848fcf59617SAndrey V. Elsukov * Check that TCP_MD5SIG is enabled in tcpcb to 849fcf59617SAndrey V. Elsukov * account the size needed to set this TCP option. 850fcf59617SAndrey V. Elsukov */ 85102a1a643SAndre Oppermann if (tp->t_flags & TF_SIGNATURE) 85202a1a643SAndre Oppermann to.to_flags |= TOF_SIGNATURE; 8531cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 8541cfd4b53SBruce M Simpson 85502a1a643SAndre Oppermann /* Processing the options. */ 8564a411b9fSBjoern A. Zeeb hdrlen += optlen = tcp_addoptions(&to, opt); 857c560df6fSPatrick Kelsey /* 858c560df6fSPatrick Kelsey * If we wanted a TFO option to be added, but it was unable 859c560df6fSPatrick Kelsey * to fit, ensure no data is sent. 860c560df6fSPatrick Kelsey */ 861c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && wanted_cookie && 862c560df6fSPatrick Kelsey !(to.to_flags & TOF_FASTOPEN)) 863c560df6fSPatrick Kelsey len = 0; 864be3f3b5eSPaul Saab } 865be3f3b5eSPaul Saab 866df8bae1dSRodney W. Grimes /* 867df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 8680c39d38dSGleb Smirnoff * bump the packet length beyond the t_maxseg length. 869a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 870a0292f23SGarrett Wollman * the segment. 871df8bae1dSRodney W. Grimes */ 8720c39d38dSGleb Smirnoff if (len + optlen + ipoptlen > tp->t_maxseg) { 873297a37f3SDavid Greenman flags &= ~TH_FIN; 874ed420311SAndre Oppermann 875b3c0f300SAndre Oppermann if (tso) { 8769fd573c3SHans Petter Selasky u_int if_hw_tsomax; 8779fd573c3SHans Petter Selasky u_int moff; 8789fd573c3SHans Petter Selasky int max_len; 8799fd573c3SHans Petter Selasky 8809fd573c3SHans Petter Selasky /* extract TSO information */ 8819fd573c3SHans Petter Selasky if_hw_tsomax = tp->t_tsomax; 8829fd573c3SHans Petter Selasky if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 8839fd573c3SHans Petter Selasky if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 8849fd573c3SHans Petter Selasky 8859fd573c3SHans Petter Selasky /* 8869fd573c3SHans Petter Selasky * Limit a TSO burst to prevent it from 8879fd573c3SHans Petter Selasky * overflowing or exceeding the maximum length 8889fd573c3SHans Petter Selasky * allowed by the network interface: 8899fd573c3SHans Petter Selasky */ 890ed420311SAndre Oppermann KASSERT(ipoptlen == 0, 891ed420311SAndre Oppermann ("%s: TSO can't do IP options", __func__)); 892ed420311SAndre Oppermann 893ed420311SAndre Oppermann /* 8949fd573c3SHans Petter Selasky * Check if we should limit by maximum payload 8959fd573c3SHans Petter Selasky * length: 896ed420311SAndre Oppermann */ 8979fd573c3SHans Petter Selasky if (if_hw_tsomax != 0) { 8989fd573c3SHans Petter Selasky /* compute maximum TSO length */ 899d76d4012SHans Petter Selasky max_len = (if_hw_tsomax - hdrlen - 900d76d4012SHans Petter Selasky max_linkhdr); 9019fd573c3SHans Petter Selasky if (max_len <= 0) { 9029fd573c3SHans Petter Selasky len = 0; 9033c7c188cSHans Petter Selasky } else if (len > max_len) { 904b3c0f300SAndre Oppermann sendalot = 1; 9053c7c188cSHans Petter Selasky len = max_len; 9069fd573c3SHans Petter Selasky } 9079fd573c3SHans Petter Selasky } 90874e10fb6SJohn Baldwin 909ed420311SAndre Oppermann /* 910ed420311SAndre Oppermann * Prevent the last segment from being 9119fd573c3SHans Petter Selasky * fractional unless the send sockbuf can be 9129fd573c3SHans Petter Selasky * emptied: 913ed420311SAndre Oppermann */ 9140c39d38dSGleb Smirnoff max_len = (tp->t_maxseg - optlen); 9153ac12506SJonathan T. Looney if (((uint32_t)off + (uint32_t)len) < 9163ac12506SJonathan T. Looney sbavail(&so->so_snd)) { 9173c7c188cSHans Petter Selasky moff = len % max_len; 9189fd573c3SHans Petter Selasky if (moff != 0) { 9199fd573c3SHans Petter Selasky len -= moff; 920b3c0f300SAndre Oppermann sendalot = 1; 921ed420311SAndre Oppermann } 9229fd573c3SHans Petter Selasky } 9239fd573c3SHans Petter Selasky 9249fd573c3SHans Petter Selasky /* 9259fd573c3SHans Petter Selasky * In case there are too many small fragments 9269fd573c3SHans Petter Selasky * don't use TSO: 9279fd573c3SHans Petter Selasky */ 9283c7c188cSHans Petter Selasky if (len <= max_len) { 9293c7c188cSHans Petter Selasky len = max_len; 9309fd573c3SHans Petter Selasky sendalot = 1; 9319fd573c3SHans Petter Selasky tso = 0; 9329fd573c3SHans Petter Selasky } 933ed420311SAndre Oppermann 934ed420311SAndre Oppermann /* 935ed420311SAndre Oppermann * Send the FIN in a separate segment 936ed420311SAndre Oppermann * after the bulk sending is done. 937ed420311SAndre Oppermann * We don't trust the TSO implementations 938ed420311SAndre Oppermann * to clear the FIN flag on all but the 939ed420311SAndre Oppermann * last segment. 940ed420311SAndre Oppermann */ 941ed420311SAndre Oppermann if (tp->t_flags & TF_NEEDFIN) 942ed420311SAndre Oppermann sendalot = 1; 943b3c0f300SAndre Oppermann } else { 9440c39d38dSGleb Smirnoff len = tp->t_maxseg - optlen - ipoptlen; 945df8bae1dSRodney W. Grimes sendalot = 1; 946c560df6fSPatrick Kelsey if (dont_sendalot) 947c560df6fSPatrick Kelsey sendalot = 0; 948df8bae1dSRodney W. Grimes } 949ed420311SAndre Oppermann } else 950ed420311SAndre Oppermann tso = 0; 951ed420311SAndre Oppermann 952ed420311SAndre Oppermann KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 953ed420311SAndre Oppermann ("%s: len > IP_MAXPACKET", __func__)); 954df8bae1dSRodney W. Grimes 955a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 956a683a7ddSYoshinobu Inoue #ifdef INET6 957a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 958a683a7ddSYoshinobu Inoue #else 959df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 960a683a7ddSYoshinobu Inoue #endif 961f10e85d7SLuigi Rizzo panic("tcphdr too big"); 962a0292f23SGarrett Wollman /*#endif*/ 963df8bae1dSRodney W. Grimes 964df8bae1dSRodney W. Grimes /* 965c4982faeSBjoern A. Zeeb * This KASSERT is here to catch edge cases at a well defined place. 966c4982faeSBjoern A. Zeeb * Before, those had triggered (random) panic conditions further down. 967c4982faeSBjoern A. Zeeb */ 968c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 969c4982faeSBjoern A. Zeeb 970c4982faeSBjoern A. Zeeb /* 971df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 972df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 973df8bae1dSRodney W. Grimes * the template for sends on this connection. 974df8bae1dSRodney W. Grimes */ 975df8bae1dSRodney W. Grimes if (len) { 9764e023759SAndre Oppermann struct mbuf *mb; 977581a046aSRandall Stewart struct sockbuf *msb; 9784e023759SAndre Oppermann u_int moff; 9794e023759SAndre Oppermann 9802cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) && len == 1) 98178b50714SRobert Watson TCPSTAT_INC(tcps_sndprobe); 9820ba5d2eeSJohn Baldwin else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) { 983f5d34df5SGeorge V. Neville-Neil tp->t_sndrexmitpack++; 98478b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitpack); 98578b50714SRobert Watson TCPSTAT_ADD(tcps_sndrexmitbyte, len); 986df8bae1dSRodney W. Grimes } else { 98778b50714SRobert Watson TCPSTAT_INC(tcps_sndpack); 98878b50714SRobert Watson TCPSTAT_ADD(tcps_sndbyte, len); 989df8bae1dSRodney W. Grimes } 99039f6074eSGleb Smirnoff #ifdef INET6 99139f6074eSGleb Smirnoff if (MHLEN < hdrlen + max_linkhdr) 99239f6074eSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 99339f6074eSGleb Smirnoff else 99439f6074eSGleb Smirnoff #endif 99539f6074eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 99639f6074eSGleb Smirnoff 997df8bae1dSRodney W. Grimes if (m == NULL) { 998cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 999df8bae1dSRodney W. Grimes error = ENOBUFS; 10000e2bc05cSGleb Smirnoff sack_rxmit = 0; 1001df8bae1dSRodney W. Grimes goto out; 1002df8bae1dSRodney W. Grimes } 100339f6074eSGleb Smirnoff 1004df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1005df8bae1dSRodney W. Grimes m->m_len = hdrlen; 10064e023759SAndre Oppermann 10074e023759SAndre Oppermann /* 10084e023759SAndre Oppermann * Start the m_copy functions from the closest mbuf 10094e023759SAndre Oppermann * to the offset in the socket buffer chain. 10104e023759SAndre Oppermann */ 1011581a046aSRandall Stewart mb = sbsndptr_noadv(&so->so_snd, off, &moff); 1012b2e60773SJohn Baldwin if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 10133ac12506SJonathan T. Looney m_copydata(mb, moff, len, 1014df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 1015581a046aSRandall Stewart if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1016581a046aSRandall Stewart sbsndptr_adv(&so->so_snd, mb, len); 1017df8bae1dSRodney W. Grimes m->m_len += len; 1018df8bae1dSRodney W. Grimes } else { 1019581a046aSRandall Stewart if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1020581a046aSRandall Stewart msb = NULL; 1021581a046aSRandall Stewart else 1022581a046aSRandall Stewart msb = &so->so_snd; 1023581a046aSRandall Stewart m->m_next = tcp_m_copym(mb, moff, 1024581a046aSRandall Stewart &len, if_hw_tsomaxsegcount, 1025b2e60773SJohn Baldwin if_hw_tsomaxsegsize, msb, hw_tls); 1026581a046aSRandall Stewart if (len <= (tp->t_maxseg - optlen)) { 1027581a046aSRandall Stewart /* 1028581a046aSRandall Stewart * Must have ran out of mbufs for the copy 1029581a046aSRandall Stewart * shorten it to no longer need tso. Lets 1030581a046aSRandall Stewart * not put on sendalot since we are low on 1031581a046aSRandall Stewart * mbufs. 1032581a046aSRandall Stewart */ 1033581a046aSRandall Stewart tso = 0; 1034581a046aSRandall Stewart } 10354e023759SAndre Oppermann if (m->m_next == NULL) { 1036cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1037d7f570e6SGarrett Wollman (void) m_free(m); 103851823c3aSGarrett Wollman error = ENOBUFS; 10390e2bc05cSGleb Smirnoff sack_rxmit = 0; 104051823c3aSGarrett Wollman goto out; 104151823c3aSGarrett Wollman } 1042df8bae1dSRodney W. Grimes } 1043472ea5beSColin Percival 1044df8bae1dSRodney W. Grimes /* 1045df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 1046df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 1047df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 1048df8bae1dSRodney W. Grimes * a PUSH comes in.) 1049df8bae1dSRodney W. Grimes */ 10503ac12506SJonathan T. Looney if (((uint32_t)off + (uint32_t)len == sbused(&so->so_snd)) && 10513ac12506SJonathan T. Looney !(flags & TH_SYN)) 1052df8bae1dSRodney W. Grimes flags |= TH_PUSH; 1053cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1054df8bae1dSRodney W. Grimes } else { 1055cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1056df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 105778b50714SRobert Watson TCPSTAT_INC(tcps_sndacks); 1058df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 105978b50714SRobert Watson TCPSTAT_INC(tcps_sndctrl); 1060df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 106178b50714SRobert Watson TCPSTAT_INC(tcps_sndurg); 1062df8bae1dSRodney W. Grimes else 106378b50714SRobert Watson TCPSTAT_INC(tcps_sndwinup); 1064df8bae1dSRodney W. Grimes 1065aa8bd99dSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 1066df8bae1dSRodney W. Grimes if (m == NULL) { 1067df8bae1dSRodney W. Grimes error = ENOBUFS; 10680e2bc05cSGleb Smirnoff sack_rxmit = 0; 1069df8bae1dSRodney W. Grimes goto out; 1070df8bae1dSRodney W. Grimes } 1071fb59c426SYoshinobu Inoue #ifdef INET6 1072fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 1073fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 1074ed6a66caSRobert Watson M_ALIGN(m, hdrlen); 1075fb59c426SYoshinobu Inoue } else 1076fb59c426SYoshinobu Inoue #endif 1077df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1078df8bae1dSRodney W. Grimes m->m_len = hdrlen; 1079df8bae1dSRodney W. Grimes } 1080cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); 1081df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 1082c488362eSRobert Watson #ifdef MAC 108330d239bcSRobert Watson mac_inpcb_create_mbuf(tp->t_inpcb, m); 1084c488362eSRobert Watson #endif 1085fb59c426SYoshinobu Inoue #ifdef INET6 1086fb59c426SYoshinobu Inoue if (isipv6) { 1087fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 1088fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 108979909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip6, th); 1090fb59c426SYoshinobu Inoue } else 1091fb59c426SYoshinobu Inoue #endif /* INET6 */ 1092fb59c426SYoshinobu Inoue { 1093fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 1094151ba793SAlexander Kabaev #ifdef TCPDEBUG 1095fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 1096151ba793SAlexander Kabaev #endif 1097fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 109879909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip, th); 1099fb59c426SYoshinobu Inoue } 1100df8bae1dSRodney W. Grimes 1101df8bae1dSRodney W. Grimes /* 1102df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 1103df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 1104df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 1105df8bae1dSRodney W. Grimes */ 1106df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 1107df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 1108df8bae1dSRodney W. Grimes tp->snd_nxt--; 1109df8bae1dSRodney W. Grimes /* 1110f2512ba1SRui Paulo * If we are starting a connection, send ECN setup 1111f2512ba1SRui Paulo * SYN packet. If we are on a retransmit, we may 1112f2512ba1SRui Paulo * resend those bits a number of times as per 1113f2512ba1SRui Paulo * RFC 3168. 1114f2512ba1SRui Paulo */ 1115883054b4SDon Lewis if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn == 1) { 1116f2512ba1SRui Paulo if (tp->t_rxtshift >= 1) { 1117603724d3SBjoern A. Zeeb if (tp->t_rxtshift <= V_tcp_ecn_maxretries) 1118f2512ba1SRui Paulo flags |= TH_ECE|TH_CWR; 1119f2512ba1SRui Paulo } else 1120f2512ba1SRui Paulo flags |= TH_ECE|TH_CWR; 1121f2512ba1SRui Paulo } 1122f2512ba1SRui Paulo 1123f2512ba1SRui Paulo if (tp->t_state == TCPS_ESTABLISHED && 1124f2512ba1SRui Paulo (tp->t_flags & TF_ECN_PERMIT)) { 1125f2512ba1SRui Paulo /* 1126f2512ba1SRui Paulo * If the peer has ECN, mark data packets with 1127f2512ba1SRui Paulo * ECN capable transmission (ECT). 1128f2512ba1SRui Paulo * Ignore pure ack packets, retransmissions and window probes. 1129f2512ba1SRui Paulo */ 1130f2512ba1SRui Paulo if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) && 1131f2512ba1SRui Paulo !((tp->t_flags & TF_FORCEDATA) && len == 1)) { 1132f2512ba1SRui Paulo #ifdef INET6 1133f2512ba1SRui Paulo if (isipv6) 1134f2512ba1SRui Paulo ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 1135f2512ba1SRui Paulo else 1136f2512ba1SRui Paulo #endif 1137f2512ba1SRui Paulo ip->ip_tos |= IPTOS_ECN_ECT0; 113878b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1139f2512ba1SRui Paulo } 1140f2512ba1SRui Paulo 1141f2512ba1SRui Paulo /* 1142f2512ba1SRui Paulo * Reply with proper ECN notifications. 1143f2512ba1SRui Paulo */ 1144f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_SND_CWR) { 1145f2512ba1SRui Paulo flags |= TH_CWR; 1146f2512ba1SRui Paulo tp->t_flags &= ~TF_ECN_SND_CWR; 1147f2512ba1SRui Paulo } 1148f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_SND_ECE) 1149f2512ba1SRui Paulo flags |= TH_ECE; 1150f2512ba1SRui Paulo } 1151f2512ba1SRui Paulo 1152f2512ba1SRui Paulo /* 1153df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 1154df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 1155df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 1156df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 1157df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 1158df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 1159df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 1160df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 1161df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 1162df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 1163df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 1164df8bae1dSRodney W. Grimes */ 1165a55db2b6SPaul Saab if (sack_rxmit == 0) { 1166b8152ba7SAndre Oppermann if (len || (flags & (TH_SYN|TH_FIN)) || 1167b8152ba7SAndre Oppermann tcp_timer_active(tp, TT_PERSIST)) 1168fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 1169df8bae1dSRodney W. Grimes else 1170fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 1171a55db2b6SPaul Saab } else { 11726d90faf3SPaul Saab th->th_seq = htonl(p->rxmit); 11736d90faf3SPaul Saab p->rxmit += len; 11740077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += len; 11756d90faf3SPaul Saab } 1176fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 1177df8bae1dSRodney W. Grimes if (optlen) { 1178fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 1179fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 1180df8bae1dSRodney W. Grimes } 1181fb59c426SYoshinobu Inoue th->th_flags = flags; 1182df8bae1dSRodney W. Grimes /* 1183df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 1184df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 118579410718SMichael Tuexen * If a RST segment is sent, advertise a window of zero. 1186df8bae1dSRodney W. Grimes */ 118779410718SMichael Tuexen if (flags & TH_RST) { 118879410718SMichael Tuexen recwin = 0; 118979410718SMichael Tuexen } else { 11903ac12506SJonathan T. Looney if (recwin < (so->so_rcv.sb_hiwat / 4) && 11913ac12506SJonathan T. Looney recwin < tp->t_maxseg) 1192201d185bSAndre Oppermann recwin = 0; 1193f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 11943ac12506SJonathan T. Looney recwin < (tp->rcv_adv - tp->rcv_nxt)) 11953ac12506SJonathan T. Looney recwin = (tp->rcv_adv - tp->rcv_nxt); 119679410718SMichael Tuexen } 1197104ebb2aSAndre Oppermann /* 1198104ebb2aSAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1199104ebb2aSAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> 1200104ebb2aSAndre Oppermann * case is handled in syncache. 1201104ebb2aSAndre Oppermann */ 1202104ebb2aSAndre Oppermann if (flags & TH_SYN) 1203104ebb2aSAndre Oppermann th->th_win = htons((u_short) 1204104ebb2aSAndre Oppermann (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 1205104ebb2aSAndre Oppermann else 1206104ebb2aSAndre Oppermann th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 1207262c1c1aSMatthew Dillon 1208262c1c1aSMatthew Dillon /* 1209262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 1210262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 1211262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 1212262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 1213b2722702SRui Paulo * to read more data than can be buffered prior to transmitting on 1214262c1c1aSMatthew Dillon * the connection. 1215262c1c1aSMatthew Dillon */ 1216f5d34df5SGeorge V. Neville-Neil if (th->th_win == 0) { 1217f5d34df5SGeorge V. Neville-Neil tp->t_sndzerowin++; 1218262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 1219f5d34df5SGeorge V. Neville-Neil } else 1220262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 1221df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 1222fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 1223fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 1224df8bae1dSRodney W. Grimes } else 1225df8bae1dSRodney W. Grimes /* 1226df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 1227df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 1228df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 1229df8bae1dSRodney W. Grimes * number wraparound. 1230df8bae1dSRodney W. Grimes */ 1231df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 1232df8bae1dSRodney W. Grimes 1233df8bae1dSRodney W. Grimes /* 1234df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 1235df8bae1dSRodney W. Grimes * checksum extended header and data. 1236df8bae1dSRodney W. Grimes */ 1237fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 123845747ba5SBjoern A. Zeeb m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 1239fcf59617SAndrey V. Elsukov 1240fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1241fcf59617SAndrey V. Elsukov if (to.to_flags & TOF_SIGNATURE) { 1242fcf59617SAndrey V. Elsukov /* 1243fcf59617SAndrey V. Elsukov * Calculate MD5 signature and put it into the place 1244fcf59617SAndrey V. Elsukov * determined before. 1245fcf59617SAndrey V. Elsukov * NOTE: since TCP options buffer doesn't point into 1246fcf59617SAndrey V. Elsukov * mbuf's data, calculate offset and use it. 1247fcf59617SAndrey V. Elsukov */ 12482aad6240SAndrey V. Elsukov if (!TCPMD5_ENABLED() || (error = TCPMD5_OUTPUT(m, th, 12492aad6240SAndrey V. Elsukov (u_char *)(th + 1) + (to.to_signature - opt))) != 0) { 1250fcf59617SAndrey V. Elsukov /* 1251fcf59617SAndrey V. Elsukov * Do not send segment if the calculation of MD5 1252fcf59617SAndrey V. Elsukov * digest has failed. 1253fcf59617SAndrey V. Elsukov */ 12542aad6240SAndrey V. Elsukov m_freem(m); 1255fcf59617SAndrey V. Elsukov goto out; 1256fcf59617SAndrey V. Elsukov } 1257fcf59617SAndrey V. Elsukov } 1258fcf59617SAndrey V. Elsukov #endif 1259fb59c426SYoshinobu Inoue #ifdef INET6 126045747ba5SBjoern A. Zeeb if (isipv6) { 1261fb59c426SYoshinobu Inoue /* 12623df96ee6SCy Schubert * There is no need to fill in ip6_plen right now. 12633df96ee6SCy Schubert * It will be filled later by ip6_output. 1264fb59c426SYoshinobu Inoue */ 1265356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 126645747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) + 126745747ba5SBjoern A. Zeeb optlen + len, IPPROTO_TCP, 0); 126845747ba5SBjoern A. Zeeb } 126945747ba5SBjoern A. Zeeb #endif 127045747ba5SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1271fb59c426SYoshinobu Inoue else 127245747ba5SBjoern A. Zeeb #endif 127345747ba5SBjoern A. Zeeb #ifdef INET 1274fb59c426SYoshinobu Inoue { 1275356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP; 127679909384SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 127779909384SJonathan Lemon htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen)); 1278fb59c426SYoshinobu Inoue 1279db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 1280db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 12816e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 1282fb59c426SYoshinobu Inoue } 128345747ba5SBjoern A. Zeeb #endif 1284df8bae1dSRodney W. Grimes 1285df8bae1dSRodney W. Grimes /* 1286b3c0f300SAndre Oppermann * Enable TSO and specify the size of the segments. 1287b3c0f300SAndre Oppermann * The TCP pseudo header checksum is always provided. 1288b3c0f300SAndre Oppermann */ 1289b3c0f300SAndre Oppermann if (tso) { 12900c39d38dSGleb Smirnoff KASSERT(len > tp->t_maxseg - optlen, 1291153e5b57SAndre Oppermann ("%s: len <= tso_segsz", __func__)); 12923579cf4cSKenneth D. Merry m->m_pkthdr.csum_flags |= CSUM_TSO; 12930c39d38dSGleb Smirnoff m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen; 1294b3c0f300SAndre Oppermann } 1295b3c0f300SAndre Oppermann 129605fb056cSMichael Tuexen KASSERT(len + hdrlen == m_length(m, NULL), 129705fb056cSMichael Tuexen ("%s: mbuf chain shorter than expected: %d + %u != %u", 129805fb056cSMichael Tuexen __func__, len, hdrlen, m_length(m, NULL))); 1299ed420311SAndre Oppermann 1300bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 130139bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */ 130239bc9de5SLawrence Stewart hhook_run_tcp_est_out(tp, th, &to, len, tso); 1303bd79708dSJonathan T. Looney #endif 130439bc9de5SLawrence Stewart 1305610ee2f9SDavid Greenman #ifdef TCPDEBUG 1306df8bae1dSRodney W. Grimes /* 1307df8bae1dSRodney W. Grimes * Trace. 1308df8bae1dSRodney W. Grimes */ 130991f467d5SHartmut Brandt if (so->so_options & SO_DEBUG) { 1310f3e0b7efSBruce M Simpson u_short save = 0; 13115214cb3fSBruce M Simpson #ifdef INET6 13125214cb3fSBruce M Simpson if (!isipv6) 13135214cb3fSBruce M Simpson #endif 13145214cb3fSBruce M Simpson { 13155214cb3fSBruce M Simpson save = ipov->ih_len; 131691f467d5SHartmut Brandt ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */); 13175214cb3fSBruce M Simpson } 1318fb59c426SYoshinobu Inoue tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 13195214cb3fSBruce M Simpson #ifdef INET6 13205214cb3fSBruce M Simpson if (!isipv6) 13215214cb3fSBruce M Simpson #endif 132291f467d5SHartmut Brandt ipov->ih_len = save; 132391f467d5SHartmut Brandt } 1324b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 13252b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__output, tp, th, m); 1326df8bae1dSRodney W. Grimes 1327dcaffbd6SJonathan T. Looney /* We're getting ready to send; log now. */ 1328dcaffbd6SJonathan T. Looney TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 1329dcaffbd6SJonathan T. Looney len, NULL, false); 1330dcaffbd6SJonathan T. Looney 1331df8bae1dSRodney W. Grimes /* 1332df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 1333df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 1334df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 1335df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 1336df8bae1dSRodney W. Grimes */ 1337fb59c426SYoshinobu Inoue /* 133843630e62SHiren Panchasara * m->m_pkthdr.len should have been set before checksum calculation, 1339fb59c426SYoshinobu Inoue * because in6_cksum() need it. 1340fb59c426SYoshinobu Inoue */ 1341fb59c426SYoshinobu Inoue #ifdef INET6 1342fb59c426SYoshinobu Inoue if (isipv6) { 1343fb59c426SYoshinobu Inoue /* 1344fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 1345fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 1346fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 1347fb59c426SYoshinobu Inoue * Neighbor Discovery. 1348fb59c426SYoshinobu Inoue */ 134997d8d152SAndre Oppermann ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1350fb59c426SYoshinobu Inoue 135157f60867SMark Johnston /* 135257f60867SMark Johnston * Set the packet size here for the benefit of DTrace probes. 135357f60867SMark Johnston * ip6_output() will set it properly; it's supposed to include 135457f60867SMark Johnston * the option header lengths as well. 135557f60867SMark Johnston */ 135657f60867SMark Johnston ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 135757f60867SMark Johnston 13580c39d38dSGleb Smirnoff if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 13590f3e3bc5SSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 13600f3e3bc5SSean Bruno else 13610f3e3bc5SSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 13620f3e3bc5SSean Bruno 136357f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1364d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 136557f60867SMark Johnston 136657f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip6, tp, th); 136757f60867SMark Johnston 136886a996e6SHiren Panchasara #ifdef TCPPCAP 136986a996e6SHiren Panchasara /* Save packet, if requested. */ 137086a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_outpkts)); 137186a996e6SHiren Panchasara #endif 137286a996e6SHiren Panchasara 1373fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 13744a5c6c6aSMike Karels error = ip6_output(m, tp->t_inpcb->in6p_outputopts, 13754a5c6c6aSMike Karels &tp->t_inpcb->inp_route6, 1376df0633a1SGleb Smirnoff ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 1377df0633a1SGleb Smirnoff NULL, NULL, tp->t_inpcb); 1378df0633a1SGleb Smirnoff 13794a5c6c6aSMike Karels if (error == EMSGSIZE && tp->t_inpcb->inp_route6.ro_rt != NULL) 13804a5c6c6aSMike Karels mtu = tp->t_inpcb->inp_route6.ro_rt->rt_mtu; 1381b287c6c7SBjoern A. Zeeb } 1382fb59c426SYoshinobu Inoue #endif /* INET6 */ 1383b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1384b287c6c7SBjoern A. Zeeb else 1385b287c6c7SBjoern A. Zeeb #endif 1386b287c6c7SBjoern A. Zeeb #ifdef INET 1387df8bae1dSRodney W. Grimes { 13888f134647SGleb Smirnoff ip->ip_len = htons(m->m_pkthdr.len); 1389686cdd19SJun-ichiro itojun Hagino #ifdef INET6 13905cd54324SBjoern A. Zeeb if (tp->t_inpcb->inp_vflag & INP_IPV6PROTO) 139197d8d152SAndre Oppermann ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL); 1392686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 1393f138387aSGarrett Wollman /* 139497d8d152SAndre Oppermann * If we do path MTU discovery, then we set DF on every packet. 139597d8d152SAndre Oppermann * This might not be the best thing to do according to RFC3390 139697d8d152SAndre Oppermann * Section 2. However the tcp hostcache migitates the problem 139797d8d152SAndre Oppermann * so it affects only the first tcp connection with a host. 1398e4e92660SAndre Oppermann * 1399e4e92660SAndre Oppermann * NB: Don't set DF on small MTU/MSS to have a safe fallback. 1400f138387aSGarrett Wollman */ 14010c39d38dSGleb Smirnoff if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 14028f134647SGleb Smirnoff ip->ip_off |= htons(IP_DF); 1403f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 1404f6f6703fSSean Bruno } else { 1405f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 1406f6f6703fSSean Bruno } 140797d8d152SAndre Oppermann 140857f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1409d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 141057f60867SMark Johnston 141157f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip, tp, th); 141257f60867SMark Johnston 141386a996e6SHiren Panchasara #ifdef TCPPCAP 141486a996e6SHiren Panchasara /* Save packet, if requested. */ 141586a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_outpkts)); 141686a996e6SHiren Panchasara #endif 141786a996e6SHiren Panchasara 141884cc0778SGeorge V. Neville-Neil error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 1419b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 1420b5d47ff5SJohn-Mark Gurney tp->t_inpcb); 1421df0633a1SGleb Smirnoff 142284cc0778SGeorge V. Neville-Neil if (error == EMSGSIZE && tp->t_inpcb->inp_route.ro_rt != NULL) 142384cc0778SGeorge V. Neville-Neil mtu = tp->t_inpcb->inp_route.ro_rt->rt_mtu; 1424df8bae1dSRodney W. Grimes } 1425b287c6c7SBjoern A. Zeeb #endif /* INET */ 14260e2bc05cSGleb Smirnoff 14270e2bc05cSGleb Smirnoff out: 14280e2bc05cSGleb Smirnoff /* 14290e2bc05cSGleb Smirnoff * In transmit state, time the transmission and arrange for 14300e2bc05cSGleb Smirnoff * the retransmit. In persist state, just set snd_max. 14310e2bc05cSGleb Smirnoff */ 14320e2bc05cSGleb Smirnoff if ((tp->t_flags & TF_FORCEDATA) == 0 || 14330e2bc05cSGleb Smirnoff !tcp_timer_active(tp, TT_PERSIST)) { 14340e2bc05cSGleb Smirnoff tcp_seq startseq = tp->snd_nxt; 14350e2bc05cSGleb Smirnoff 14360e2bc05cSGleb Smirnoff /* 14370e2bc05cSGleb Smirnoff * Advance snd_nxt over sequence space of this segment. 14380e2bc05cSGleb Smirnoff */ 14390e2bc05cSGleb Smirnoff if (flags & (TH_SYN|TH_FIN)) { 14400e2bc05cSGleb Smirnoff if (flags & TH_SYN) 14410e2bc05cSGleb Smirnoff tp->snd_nxt++; 14420e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 14430e2bc05cSGleb Smirnoff tp->snd_nxt++; 14440e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 14450e2bc05cSGleb Smirnoff } 14460e2bc05cSGleb Smirnoff } 14470e2bc05cSGleb Smirnoff if (sack_rxmit) 14480e2bc05cSGleb Smirnoff goto timer; 14490e2bc05cSGleb Smirnoff tp->snd_nxt += len; 14500e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 14510e2bc05cSGleb Smirnoff tp->snd_max = tp->snd_nxt; 14520e2bc05cSGleb Smirnoff /* 14530e2bc05cSGleb Smirnoff * Time this transmission if not a retransmission and 14540e2bc05cSGleb Smirnoff * not currently timing anything. 14550e2bc05cSGleb Smirnoff */ 14560e2bc05cSGleb Smirnoff if (tp->t_rtttime == 0) { 14570e2bc05cSGleb Smirnoff tp->t_rtttime = ticks; 14580e2bc05cSGleb Smirnoff tp->t_rtseq = startseq; 14590e2bc05cSGleb Smirnoff TCPSTAT_INC(tcps_segstimed); 14600e2bc05cSGleb Smirnoff } 14610e2bc05cSGleb Smirnoff } 14620e2bc05cSGleb Smirnoff 14630e2bc05cSGleb Smirnoff /* 14640e2bc05cSGleb Smirnoff * Set retransmit timer if not currently set, 14650e2bc05cSGleb Smirnoff * and not doing a pure ack or a keep-alive probe. 14660e2bc05cSGleb Smirnoff * Initial value for retransmit timer is smoothed 14670e2bc05cSGleb Smirnoff * round-trip time + 2 * round-trip time variance. 14680e2bc05cSGleb Smirnoff * Initialize shift counter which is used for backoff 14690e2bc05cSGleb Smirnoff * of retransmit time. 14700e2bc05cSGleb Smirnoff */ 14710e2bc05cSGleb Smirnoff timer: 14720e2bc05cSGleb Smirnoff if (!tcp_timer_active(tp, TT_REXMT) && 14730e2bc05cSGleb Smirnoff ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 14740e2bc05cSGleb Smirnoff (tp->snd_nxt != tp->snd_una))) { 14750e2bc05cSGleb Smirnoff if (tcp_timer_active(tp, TT_PERSIST)) { 14760e2bc05cSGleb Smirnoff tcp_timer_activate(tp, TT_PERSIST, 0); 14770e2bc05cSGleb Smirnoff tp->t_rxtshift = 0; 14780e2bc05cSGleb Smirnoff } 14790e2bc05cSGleb Smirnoff tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 1480f8568079SHiren Panchasara } else if (len == 0 && sbavail(&so->so_snd) && 1481f8568079SHiren Panchasara !tcp_timer_active(tp, TT_REXMT) && 1482f8568079SHiren Panchasara !tcp_timer_active(tp, TT_PERSIST)) { 1483f8568079SHiren Panchasara /* 1484f8568079SHiren Panchasara * Avoid a situation where we do not set persist timer 1485f8568079SHiren Panchasara * after a zero window condition. For example: 1486f8568079SHiren Panchasara * 1) A -> B: packet with enough data to fill the window 1487f8568079SHiren Panchasara * 2) B -> A: ACK for #1 + new data (0 window 1488f8568079SHiren Panchasara * advertisement) 1489f8568079SHiren Panchasara * 3) A -> B: ACK for #2, 0 len packet 1490f8568079SHiren Panchasara * 1491f8568079SHiren Panchasara * In this case, A will not activate the persist timer, 1492f8568079SHiren Panchasara * because it chose to send a packet. Unless tcp_output 1493f8568079SHiren Panchasara * is called for some other reason (delayed ack timer, 1494f8568079SHiren Panchasara * another input packet from B, socket syscall), A will 1495f8568079SHiren Panchasara * not send zero window probes. 1496f8568079SHiren Panchasara * 1497f8568079SHiren Panchasara * So, if you send a 0-length packet, but there is data 1498f8568079SHiren Panchasara * in the socket buffer, and neither the rexmt or 1499f8568079SHiren Panchasara * persist timer is already set, then activate the 1500f8568079SHiren Panchasara * persist timer. 1501f8568079SHiren Panchasara */ 1502f8568079SHiren Panchasara tp->t_rxtshift = 0; 1503f8568079SHiren Panchasara tcp_setpersist(tp); 15040e2bc05cSGleb Smirnoff } 15050e2bc05cSGleb Smirnoff } else { 15060e2bc05cSGleb Smirnoff /* 15070e2bc05cSGleb Smirnoff * Persist case, update snd_max but since we are in 15080e2bc05cSGleb Smirnoff * persist mode (no window) we do not update snd_nxt. 15090e2bc05cSGleb Smirnoff */ 15100e2bc05cSGleb Smirnoff int xlen = len; 15110e2bc05cSGleb Smirnoff if (flags & TH_SYN) 15120e2bc05cSGleb Smirnoff ++xlen; 15130e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 15140e2bc05cSGleb Smirnoff ++xlen; 15150e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 15160e2bc05cSGleb Smirnoff } 15170e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 151815c82571SJonathan T. Looney tp->snd_max = tp->snd_nxt + xlen; 15190e2bc05cSGleb Smirnoff } 1520e5926fd3SRandall Stewart if ((error == 0) && 1521e5926fd3SRandall Stewart (TCPS_HAVEESTABLISHED(tp->t_state) && 1522e5926fd3SRandall Stewart (tp->t_flags & TF_SACK_PERMIT) && 1523e5926fd3SRandall Stewart tp->rcv_numsacks > 0)) { 1524e5926fd3SRandall Stewart /* Clean up any DSACK's sent */ 1525e5926fd3SRandall Stewart tcp_clean_dsack_blocks(tp); 1526e5926fd3SRandall Stewart } 1527df8bae1dSRodney W. Grimes if (error) { 15282529f56eSJonathan T. Looney /* Record the error. */ 15292529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, 15302529f56eSJonathan T. Looney error, 0, NULL, false); 15317734ea06SArchie Cobbs 15327734ea06SArchie Cobbs /* 15337734ea06SArchie Cobbs * We know that the packet was lost, so back out the 15347734ea06SArchie Cobbs * sequence number advance, if any. 15352c30ec0aSAndre Oppermann * 15362c30ec0aSAndre Oppermann * If the error is EPERM the packet got blocked by the 15372c30ec0aSAndre Oppermann * local firewall. Normally we should terminate the 15382c30ec0aSAndre Oppermann * connection but the blocking may have been spurious 15392c30ec0aSAndre Oppermann * due to a firewall reconfiguration cycle. So we treat 15402c30ec0aSAndre Oppermann * it like a packet loss and let the retransmit timer and 15412c30ec0aSAndre Oppermann * timeouts do their work over time. 15422c30ec0aSAndre Oppermann * XXX: It is a POLA question whether calling tcp_drop right 15432c30ec0aSAndre Oppermann * away would be the really correct behavior instead. 15447734ea06SArchie Cobbs */ 154572757d9aSGleb Smirnoff if (((tp->t_flags & TF_FORCEDATA) == 0 || 1546b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) && 154772757d9aSGleb Smirnoff ((flags & TH_SYN) == 0) && 154872757d9aSGleb Smirnoff (error != EPERM)) { 15490077b016SPaul Saab if (sack_rxmit) { 15505d3b1b75SJayanth Vijayaraghavan p->rxmit -= len; 15510077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= len; 155272757d9aSGleb Smirnoff KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 15530077b016SPaul Saab ("sackhint bytes rtx >= 0")); 15540077b016SPaul Saab } else 15557734ea06SArchie Cobbs tp->snd_nxt -= len; 15567734ea06SArchie Cobbs } 1557cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */ 155872757d9aSGleb Smirnoff switch (error) { 1559fcf59617SAndrey V. Elsukov case EACCES: 156072757d9aSGleb Smirnoff case EPERM: 156172757d9aSGleb Smirnoff tp->t_softerror = error; 156272757d9aSGleb Smirnoff return (error); 156372757d9aSGleb Smirnoff case ENOBUFS: 1564425b7639SSepherosa Ziehau TCP_XMIT_TIMER_ASSERT(tp, len, flags); 15651600372bSAndre Oppermann tp->snd_cwnd = tp->t_maxseg; 1566df8bae1dSRodney W. Grimes return (0); 156772757d9aSGleb Smirnoff case EMSGSIZE: 15683d1f141bSGarrett Wollman /* 1569b3c0f300SAndre Oppermann * For some reason the interface we used initially 1570b3c0f300SAndre Oppermann * to send segments changed to another or lowered 1571b3c0f300SAndre Oppermann * its MTU. 1572b3c0f300SAndre Oppermann * If TSO was active we either got an interface 1573b3c0f300SAndre Oppermann * without TSO capabilits or TSO was turned off. 1574df0633a1SGleb Smirnoff * If we obtained mtu from ip_output() then update 1575df0633a1SGleb Smirnoff * it and try again. 15763d1f141bSGarrett Wollman */ 1577b3c0f300SAndre Oppermann if (tso) 1578b3c0f300SAndre Oppermann tp->t_flags &= ~TF_TSO; 1579df0633a1SGleb Smirnoff if (mtu != 0) { 1580df0633a1SGleb Smirnoff tcp_mss_update(tp, -1, mtu, NULL, NULL); 1581df0633a1SGleb Smirnoff goto again; 1582df0633a1SGleb Smirnoff } 1583df0633a1SGleb Smirnoff return (error); 15848bec3467SGleb Smirnoff case EHOSTDOWN: 158572757d9aSGleb Smirnoff case EHOSTUNREACH: 158672757d9aSGleb Smirnoff case ENETDOWN: 15878bec3467SGleb Smirnoff case ENETUNREACH: 158872757d9aSGleb Smirnoff if (TCPS_HAVERCVDSYN(tp->t_state)) { 1589df8bae1dSRodney W. Grimes tp->t_softerror = error; 1590df8bae1dSRodney W. Grimes return (0); 1591df8bae1dSRodney W. Grimes } 159272757d9aSGleb Smirnoff /* FALLTHROUGH */ 159372757d9aSGleb Smirnoff default: 1594df8bae1dSRodney W. Grimes return (error); 1595df8bae1dSRodney W. Grimes } 159672757d9aSGleb Smirnoff } 159778b50714SRobert Watson TCPSTAT_INC(tcps_sndtotal); 1598df8bae1dSRodney W. Grimes 1599df8bae1dSRodney W. Grimes /* 1600df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 1601df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 1602df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 1603df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 1604df8bae1dSRodney W. Grimes */ 16053ac12506SJonathan T. Looney if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1606201d185bSAndre Oppermann tp->rcv_adv = tp->rcv_nxt + recwin; 1607df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 16083bfd6421SJonathan Lemon tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 1609b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_DELACK)) 1610b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 0); 1611d912c694SMatthew Dillon #if 0 1612d912c694SMatthew Dillon /* 1613d912c694SMatthew Dillon * This completely breaks TCP if newreno is turned on. What happens 1614d912c694SMatthew Dillon * is that if delayed-acks are turned on on the receiver, this code 1615d912c694SMatthew Dillon * on the transmitter effectively destroys the TCP window, forcing 1616d912c694SMatthew Dillon * it to four packets (1.5Kx4 = 6K window). 1617d912c694SMatthew Dillon */ 1618dbc42409SLawrence Stewart if (sendalot && --maxburst) 1619df8bae1dSRodney W. Grimes goto again; 1620d912c694SMatthew Dillon #endif 1621d912c694SMatthew Dillon if (sendalot) 1622d912c694SMatthew Dillon goto again; 1623df8bae1dSRodney W. Grimes return (0); 1624df8bae1dSRodney W. Grimes } 1625df8bae1dSRodney W. Grimes 1626df8bae1dSRodney W. Grimes void 1627ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp) 1628df8bae1dSRodney W. Grimes { 16299b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 16309b8b58e0SJonathan Lemon int tt; 1631df8bae1dSRodney W. Grimes 1632672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 1633b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_REXMT)) 16349b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 1635df8bae1dSRodney W. Grimes /* 1636a4641f4eSPedro F. Giffuni * Start/restart persistence timer. 1637df8bae1dSRodney W. Grimes */ 16389b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 16390645c604SHiren Panchasara tcp_persmin, tcp_persmax); 1640b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, tt); 1641df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1642df8bae1dSRodney W. Grimes tp->t_rxtshift++; 1643df8bae1dSRodney W. Grimes } 164402a1a643SAndre Oppermann 164502a1a643SAndre Oppermann /* 164602a1a643SAndre Oppermann * Insert TCP options according to the supplied parameters to the place 164702a1a643SAndre Oppermann * optp in a consistent way. Can handle unaligned destinations. 164802a1a643SAndre Oppermann * 164902a1a643SAndre Oppermann * The order of the option processing is crucial for optimal packing and 165002a1a643SAndre Oppermann * alignment for the scarce option space. 165102a1a643SAndre Oppermann * 165202a1a643SAndre Oppermann * The optimal order for a SYN/SYN-ACK segment is: 165302a1a643SAndre Oppermann * MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) + 165402a1a643SAndre Oppermann * Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40. 165502a1a643SAndre Oppermann * 165602a1a643SAndre Oppermann * The SACK options should be last. SACK blocks consume 8*n+2 bytes. 165702a1a643SAndre Oppermann * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks). 165802a1a643SAndre Oppermann * At minimum we need 10 bytes (to generate 1 SACK block). If both 165902a1a643SAndre Oppermann * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present, 166002a1a643SAndre Oppermann * we only have 10 bytes for SACK options (40 - (12 + 18)). 166102a1a643SAndre Oppermann */ 166202a1a643SAndre Oppermann int 166302a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp) 166402a1a643SAndre Oppermann { 16655d20f974SJonathan T. Looney u_int32_t mask, optlen = 0; 166602a1a643SAndre Oppermann 166702a1a643SAndre Oppermann for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) { 166802a1a643SAndre Oppermann if ((to->to_flags & mask) != mask) 166902a1a643SAndre Oppermann continue; 16703a4018c4SAndre Oppermann if (optlen == TCP_MAXOLEN) 16713a4018c4SAndre Oppermann break; 167202a1a643SAndre Oppermann switch (to->to_flags & mask) { 167302a1a643SAndre Oppermann case TOF_MSS: 167402a1a643SAndre Oppermann while (optlen % 4) { 167502a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 167602a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 167702a1a643SAndre Oppermann } 16783a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG) 16793a4018c4SAndre Oppermann continue; 168002a1a643SAndre Oppermann optlen += TCPOLEN_MAXSEG; 168102a1a643SAndre Oppermann *optp++ = TCPOPT_MAXSEG; 168202a1a643SAndre Oppermann *optp++ = TCPOLEN_MAXSEG; 168302a1a643SAndre Oppermann to->to_mss = htons(to->to_mss); 168402a1a643SAndre Oppermann bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss)); 168502a1a643SAndre Oppermann optp += sizeof(to->to_mss); 168602a1a643SAndre Oppermann break; 168702a1a643SAndre Oppermann case TOF_SCALE: 168802a1a643SAndre Oppermann while (!optlen || optlen % 2 != 1) { 168902a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 169002a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 169102a1a643SAndre Oppermann } 16923a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW) 16933a4018c4SAndre Oppermann continue; 169402a1a643SAndre Oppermann optlen += TCPOLEN_WINDOW; 169502a1a643SAndre Oppermann *optp++ = TCPOPT_WINDOW; 169602a1a643SAndre Oppermann *optp++ = TCPOLEN_WINDOW; 169702a1a643SAndre Oppermann *optp++ = to->to_wscale; 169802a1a643SAndre Oppermann break; 169902a1a643SAndre Oppermann case TOF_SACKPERM: 170002a1a643SAndre Oppermann while (optlen % 2) { 170102a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 170202a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 170302a1a643SAndre Oppermann } 17043a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED) 17053a4018c4SAndre Oppermann continue; 170602a1a643SAndre Oppermann optlen += TCPOLEN_SACK_PERMITTED; 170702a1a643SAndre Oppermann *optp++ = TCPOPT_SACK_PERMITTED; 170802a1a643SAndre Oppermann *optp++ = TCPOLEN_SACK_PERMITTED; 170902a1a643SAndre Oppermann break; 171002a1a643SAndre Oppermann case TOF_TS: 171102a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 171202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 171302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 171402a1a643SAndre Oppermann } 17153a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP) 17163a4018c4SAndre Oppermann continue; 171702a1a643SAndre Oppermann optlen += TCPOLEN_TIMESTAMP; 171802a1a643SAndre Oppermann *optp++ = TCPOPT_TIMESTAMP; 171902a1a643SAndre Oppermann *optp++ = TCPOLEN_TIMESTAMP; 172002a1a643SAndre Oppermann to->to_tsval = htonl(to->to_tsval); 172102a1a643SAndre Oppermann to->to_tsecr = htonl(to->to_tsecr); 172202a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval)); 172302a1a643SAndre Oppermann optp += sizeof(to->to_tsval); 172402a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr)); 172502a1a643SAndre Oppermann optp += sizeof(to->to_tsecr); 172602a1a643SAndre Oppermann break; 172702a1a643SAndre Oppermann case TOF_SIGNATURE: 172802a1a643SAndre Oppermann { 172902a1a643SAndre Oppermann int siglen = TCPOLEN_SIGNATURE - 2; 173002a1a643SAndre Oppermann 173102a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 173202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 173302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 173402a1a643SAndre Oppermann } 1735fcf59617SAndrey V. Elsukov if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE) { 1736fcf59617SAndrey V. Elsukov to->to_flags &= ~TOF_SIGNATURE; 173702a1a643SAndre Oppermann continue; 1738fcf59617SAndrey V. Elsukov } 173902a1a643SAndre Oppermann optlen += TCPOLEN_SIGNATURE; 174002a1a643SAndre Oppermann *optp++ = TCPOPT_SIGNATURE; 174102a1a643SAndre Oppermann *optp++ = TCPOLEN_SIGNATURE; 174202a1a643SAndre Oppermann to->to_signature = optp; 174302a1a643SAndre Oppermann while (siglen--) 174402a1a643SAndre Oppermann *optp++ = 0; 174502a1a643SAndre Oppermann break; 174602a1a643SAndre Oppermann } 174702a1a643SAndre Oppermann case TOF_SACK: 174802a1a643SAndre Oppermann { 174902a1a643SAndre Oppermann int sackblks = 0; 175002a1a643SAndre Oppermann struct sackblk *sack = (struct sackblk *)to->to_sacks; 175102a1a643SAndre Oppermann tcp_seq sack_seq; 175202a1a643SAndre Oppermann 175302a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 175402a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 175502a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 175602a1a643SAndre Oppermann } 17573a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK) 175802a1a643SAndre Oppermann continue; 175902a1a643SAndre Oppermann optlen += TCPOLEN_SACKHDR; 176002a1a643SAndre Oppermann *optp++ = TCPOPT_SACK; 176102a1a643SAndre Oppermann sackblks = min(to->to_nsacks, 17620d957bbaSAndre Oppermann (TCP_MAXOLEN - optlen) / TCPOLEN_SACK); 176302a1a643SAndre Oppermann *optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK; 176402a1a643SAndre Oppermann while (sackblks--) { 176502a1a643SAndre Oppermann sack_seq = htonl(sack->start); 176602a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 176702a1a643SAndre Oppermann optp += sizeof(sack_seq); 176802a1a643SAndre Oppermann sack_seq = htonl(sack->end); 176902a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 177002a1a643SAndre Oppermann optp += sizeof(sack_seq); 177102a1a643SAndre Oppermann optlen += TCPOLEN_SACK; 177202a1a643SAndre Oppermann sack++; 177302a1a643SAndre Oppermann } 177478b50714SRobert Watson TCPSTAT_INC(tcps_sack_send_blocks); 177502a1a643SAndre Oppermann break; 177602a1a643SAndre Oppermann } 1777281a0fd4SPatrick Kelsey case TOF_FASTOPEN: 1778281a0fd4SPatrick Kelsey { 1779281a0fd4SPatrick Kelsey int total_len; 1780281a0fd4SPatrick Kelsey 1781281a0fd4SPatrick Kelsey /* XXX is there any point to aligning this option? */ 1782281a0fd4SPatrick Kelsey total_len = TCPOLEN_FAST_OPEN_EMPTY + to->to_tfo_len; 1783c560df6fSPatrick Kelsey if (TCP_MAXOLEN - optlen < total_len) { 1784c560df6fSPatrick Kelsey to->to_flags &= ~TOF_FASTOPEN; 1785281a0fd4SPatrick Kelsey continue; 1786c560df6fSPatrick Kelsey } 1787281a0fd4SPatrick Kelsey *optp++ = TCPOPT_FAST_OPEN; 1788281a0fd4SPatrick Kelsey *optp++ = total_len; 1789281a0fd4SPatrick Kelsey if (to->to_tfo_len > 0) { 1790281a0fd4SPatrick Kelsey bcopy(to->to_tfo_cookie, optp, to->to_tfo_len); 1791281a0fd4SPatrick Kelsey optp += to->to_tfo_len; 1792281a0fd4SPatrick Kelsey } 1793281a0fd4SPatrick Kelsey optlen += total_len; 1794281a0fd4SPatrick Kelsey break; 1795281a0fd4SPatrick Kelsey } 179602a1a643SAndre Oppermann default: 179702a1a643SAndre Oppermann panic("%s: unknown TCP option type", __func__); 179802a1a643SAndre Oppermann break; 179902a1a643SAndre Oppermann } 180002a1a643SAndre Oppermann } 180102a1a643SAndre Oppermann 180202a1a643SAndre Oppermann /* Terminate and pad TCP options to a 4 byte boundary. */ 180302a1a643SAndre Oppermann if (optlen % 4) { 180402a1a643SAndre Oppermann optlen += TCPOLEN_EOL; 180502a1a643SAndre Oppermann *optp++ = TCPOPT_EOL; 180602a1a643SAndre Oppermann } 1807413deb12SBjoern A. Zeeb /* 1808413deb12SBjoern A. Zeeb * According to RFC 793 (STD0007): 1809413deb12SBjoern A. Zeeb * "The content of the header beyond the End-of-Option option 1810413deb12SBjoern A. Zeeb * must be header padding (i.e., zero)." 1811413deb12SBjoern A. Zeeb * and later: "The padding is composed of zeros." 1812413deb12SBjoern A. Zeeb */ 181302a1a643SAndre Oppermann while (optlen % 4) { 1814c343c524SAndre Oppermann optlen += TCPOLEN_PAD; 1815c343c524SAndre Oppermann *optp++ = TCPOPT_PAD; 181602a1a643SAndre Oppermann } 181702a1a643SAndre Oppermann 18180d957bbaSAndre Oppermann KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__)); 181902a1a643SAndre Oppermann return (optlen); 182002a1a643SAndre Oppermann } 182166492feaSGleb Smirnoff 182289e560f4SRandall Stewart /* 182389e560f4SRandall Stewart * This is a copy of m_copym(), taking the TSO segment size/limit 182489e560f4SRandall Stewart * constraints into account, and advancing the sndptr as it goes. 182589e560f4SRandall Stewart */ 182689e560f4SRandall Stewart struct mbuf * 182789e560f4SRandall Stewart tcp_m_copym(struct mbuf *m, int32_t off0, int32_t *plen, 1828b2e60773SJohn Baldwin int32_t seglimit, int32_t segsize, struct sockbuf *sb, bool hw_tls) 182989e560f4SRandall Stewart { 1830b2e60773SJohn Baldwin #ifdef KERN_TLS 1831b2e60773SJohn Baldwin struct ktls_session *tls, *ntls; 1832b2e60773SJohn Baldwin struct mbuf *start; 1833b2e60773SJohn Baldwin #endif 183489e560f4SRandall Stewart struct mbuf *n, **np; 183589e560f4SRandall Stewart struct mbuf *top; 183689e560f4SRandall Stewart int32_t off = off0; 183789e560f4SRandall Stewart int32_t len = *plen; 183889e560f4SRandall Stewart int32_t fragsize; 183989e560f4SRandall Stewart int32_t len_cp = 0; 184089e560f4SRandall Stewart int32_t *pkthdrlen; 184189e560f4SRandall Stewart uint32_t mlen, frags; 184289e560f4SRandall Stewart bool copyhdr; 184389e560f4SRandall Stewart 184489e560f4SRandall Stewart 184589e560f4SRandall Stewart KASSERT(off >= 0, ("tcp_m_copym, negative off %d", off)); 184689e560f4SRandall Stewart KASSERT(len >= 0, ("tcp_m_copym, negative len %d", len)); 184789e560f4SRandall Stewart if (off == 0 && m->m_flags & M_PKTHDR) 184889e560f4SRandall Stewart copyhdr = true; 184989e560f4SRandall Stewart else 185089e560f4SRandall Stewart copyhdr = false; 185189e560f4SRandall Stewart while (off > 0) { 185289e560f4SRandall Stewart KASSERT(m != NULL, ("tcp_m_copym, offset > size of mbuf chain")); 185389e560f4SRandall Stewart if (off < m->m_len) 185489e560f4SRandall Stewart break; 185589e560f4SRandall Stewart off -= m->m_len; 185689e560f4SRandall Stewart if ((sb) && (m == sb->sb_sndptr)) { 185789e560f4SRandall Stewart sb->sb_sndptroff += m->m_len; 185889e560f4SRandall Stewart sb->sb_sndptr = m->m_next; 185989e560f4SRandall Stewart } 186089e560f4SRandall Stewart m = m->m_next; 186189e560f4SRandall Stewart } 186289e560f4SRandall Stewart np = ⊤ 186389e560f4SRandall Stewart top = NULL; 186489e560f4SRandall Stewart pkthdrlen = NULL; 1865b2e60773SJohn Baldwin #ifdef KERN_TLS 1866b2e60773SJohn Baldwin if (m->m_flags & M_NOMAP) 1867b2e60773SJohn Baldwin tls = m->m_ext.ext_pgs->tls; 1868b2e60773SJohn Baldwin else 1869b2e60773SJohn Baldwin tls = NULL; 1870b2e60773SJohn Baldwin start = m; 1871b2e60773SJohn Baldwin #endif 187289e560f4SRandall Stewart while (len > 0) { 187389e560f4SRandall Stewart if (m == NULL) { 187489e560f4SRandall Stewart KASSERT(len == M_COPYALL, 187589e560f4SRandall Stewart ("tcp_m_copym, length > size of mbuf chain")); 187689e560f4SRandall Stewart *plen = len_cp; 187789e560f4SRandall Stewart if (pkthdrlen != NULL) 187889e560f4SRandall Stewart *pkthdrlen = len_cp; 187989e560f4SRandall Stewart break; 188089e560f4SRandall Stewart } 1881b2e60773SJohn Baldwin #ifdef KERN_TLS 1882b2e60773SJohn Baldwin if (hw_tls) { 1883b2e60773SJohn Baldwin if (m->m_flags & M_NOMAP) 1884b2e60773SJohn Baldwin ntls = m->m_ext.ext_pgs->tls; 1885b2e60773SJohn Baldwin else 1886b2e60773SJohn Baldwin ntls = NULL; 1887b2e60773SJohn Baldwin 1888b2e60773SJohn Baldwin /* 1889b2e60773SJohn Baldwin * Avoid mixing TLS records with handshake 1890b2e60773SJohn Baldwin * data or TLS records from different 1891b2e60773SJohn Baldwin * sessions. 1892b2e60773SJohn Baldwin */ 1893b2e60773SJohn Baldwin if (tls != ntls) { 1894b2e60773SJohn Baldwin MPASS(m != start); 1895b2e60773SJohn Baldwin *plen = len_cp; 1896b2e60773SJohn Baldwin if (pkthdrlen != NULL) 1897b2e60773SJohn Baldwin *pkthdrlen = len_cp; 1898b2e60773SJohn Baldwin break; 1899b2e60773SJohn Baldwin } 1900b2e60773SJohn Baldwin 1901b2e60773SJohn Baldwin /* 1902b2e60773SJohn Baldwin * Don't end a send in the middle of a TLS 1903b2e60773SJohn Baldwin * record if it spans multiple TLS records. 1904b2e60773SJohn Baldwin */ 1905b2e60773SJohn Baldwin if (tls != NULL && (m != start) && len < m->m_len) { 1906b2e60773SJohn Baldwin *plen = len_cp; 1907b2e60773SJohn Baldwin if (pkthdrlen != NULL) 1908b2e60773SJohn Baldwin *pkthdrlen = len_cp; 1909b2e60773SJohn Baldwin break; 1910b2e60773SJohn Baldwin } 1911b2e60773SJohn Baldwin } 1912b2e60773SJohn Baldwin #endif 191389e560f4SRandall Stewart mlen = min(len, m->m_len - off); 191489e560f4SRandall Stewart if (seglimit) { 191589e560f4SRandall Stewart /* 191689e560f4SRandall Stewart * For M_NOMAP mbufs, add 3 segments 191789e560f4SRandall Stewart * + 1 in case we are crossing page boundaries 191889e560f4SRandall Stewart * + 2 in case the TLS hdr/trailer are used 191989e560f4SRandall Stewart * It is cheaper to just add the segments 192089e560f4SRandall Stewart * than it is to take the cache miss to look 192189e560f4SRandall Stewart * at the mbuf ext_pgs state in detail. 192289e560f4SRandall Stewart */ 192389e560f4SRandall Stewart if (m->m_flags & M_NOMAP) { 192489e560f4SRandall Stewart fragsize = min(segsize, PAGE_SIZE); 192589e560f4SRandall Stewart frags = 3; 192689e560f4SRandall Stewart } else { 192789e560f4SRandall Stewart fragsize = segsize; 192889e560f4SRandall Stewart frags = 0; 192989e560f4SRandall Stewart } 193089e560f4SRandall Stewart 193189e560f4SRandall Stewart /* Break if we really can't fit anymore. */ 193289e560f4SRandall Stewart if ((frags + 1) >= seglimit) { 193389e560f4SRandall Stewart *plen = len_cp; 193489e560f4SRandall Stewart if (pkthdrlen != NULL) 193589e560f4SRandall Stewart *pkthdrlen = len_cp; 193689e560f4SRandall Stewart break; 193789e560f4SRandall Stewart } 193889e560f4SRandall Stewart 193989e560f4SRandall Stewart /* 194089e560f4SRandall Stewart * Reduce size if you can't copy the whole 194189e560f4SRandall Stewart * mbuf. If we can't copy the whole mbuf, also 194289e560f4SRandall Stewart * adjust len so the loop will end after this 194389e560f4SRandall Stewart * mbuf. 194489e560f4SRandall Stewart */ 194589e560f4SRandall Stewart if ((frags + howmany(mlen, fragsize)) >= seglimit) { 194689e560f4SRandall Stewart mlen = (seglimit - frags - 1) * fragsize; 194789e560f4SRandall Stewart len = mlen; 194889e560f4SRandall Stewart *plen = len_cp + len; 194989e560f4SRandall Stewart if (pkthdrlen != NULL) 195089e560f4SRandall Stewart *pkthdrlen = *plen; 195189e560f4SRandall Stewart } 195289e560f4SRandall Stewart frags += howmany(mlen, fragsize); 195389e560f4SRandall Stewart if (frags == 0) 195489e560f4SRandall Stewart frags++; 195589e560f4SRandall Stewart seglimit -= frags; 195689e560f4SRandall Stewart KASSERT(seglimit > 0, 195789e560f4SRandall Stewart ("%s: seglimit went too low", __func__)); 195889e560f4SRandall Stewart } 195989e560f4SRandall Stewart if (copyhdr) 196089e560f4SRandall Stewart n = m_gethdr(M_NOWAIT, m->m_type); 196189e560f4SRandall Stewart else 196289e560f4SRandall Stewart n = m_get(M_NOWAIT, m->m_type); 196389e560f4SRandall Stewart *np = n; 196489e560f4SRandall Stewart if (n == NULL) 196589e560f4SRandall Stewart goto nospace; 196689e560f4SRandall Stewart if (copyhdr) { 196789e560f4SRandall Stewart if (!m_dup_pkthdr(n, m, M_NOWAIT)) 196889e560f4SRandall Stewart goto nospace; 196989e560f4SRandall Stewart if (len == M_COPYALL) 197089e560f4SRandall Stewart n->m_pkthdr.len -= off0; 197189e560f4SRandall Stewart else 197289e560f4SRandall Stewart n->m_pkthdr.len = len; 197389e560f4SRandall Stewart pkthdrlen = &n->m_pkthdr.len; 197489e560f4SRandall Stewart copyhdr = false; 197589e560f4SRandall Stewart } 197689e560f4SRandall Stewart n->m_len = mlen; 197789e560f4SRandall Stewart len_cp += n->m_len; 197889e560f4SRandall Stewart if (m->m_flags & M_EXT) { 197989e560f4SRandall Stewart n->m_data = m->m_data + off; 198089e560f4SRandall Stewart mb_dupcl(n, m); 198189e560f4SRandall Stewart } else 198289e560f4SRandall Stewart bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 198389e560f4SRandall Stewart (u_int)n->m_len); 198489e560f4SRandall Stewart 198589e560f4SRandall Stewart if (sb && (sb->sb_sndptr == m) && 198689e560f4SRandall Stewart ((n->m_len + off) >= m->m_len) && m->m_next) { 198789e560f4SRandall Stewart sb->sb_sndptroff += m->m_len; 198889e560f4SRandall Stewart sb->sb_sndptr = m->m_next; 198989e560f4SRandall Stewart } 199089e560f4SRandall Stewart off = 0; 199189e560f4SRandall Stewart if (len != M_COPYALL) { 199289e560f4SRandall Stewart len -= n->m_len; 199389e560f4SRandall Stewart } 199489e560f4SRandall Stewart m = m->m_next; 199589e560f4SRandall Stewart np = &n->m_next; 199689e560f4SRandall Stewart } 199789e560f4SRandall Stewart return (top); 199889e560f4SRandall Stewart nospace: 199989e560f4SRandall Stewart m_freem(top); 200089e560f4SRandall Stewart return (NULL); 200189e560f4SRandall Stewart } 200289e560f4SRandall Stewart 200366492feaSGleb Smirnoff void 200466492feaSGleb Smirnoff tcp_sndbuf_autoscale(struct tcpcb *tp, struct socket *so, uint32_t sendwin) 200566492feaSGleb Smirnoff { 200666492feaSGleb Smirnoff 200766492feaSGleb Smirnoff /* 200866492feaSGleb Smirnoff * Automatic sizing of send socket buffer. Often the send buffer 200966492feaSGleb Smirnoff * size is not optimally adjusted to the actual network conditions 201066492feaSGleb Smirnoff * at hand (delay bandwidth product). Setting the buffer size too 201166492feaSGleb Smirnoff * small limits throughput on links with high bandwidth and high 201266492feaSGleb Smirnoff * delay (eg. trans-continental/oceanic links). Setting the 201366492feaSGleb Smirnoff * buffer size too big consumes too much real kernel memory, 201466492feaSGleb Smirnoff * especially with many connections on busy servers. 201566492feaSGleb Smirnoff * 201666492feaSGleb Smirnoff * The criteria to step up the send buffer one notch are: 201766492feaSGleb Smirnoff * 1. receive window of remote host is larger than send buffer 201866492feaSGleb Smirnoff * (with a fudge factor of 5/4th); 201966492feaSGleb Smirnoff * 2. send buffer is filled to 7/8th with data (so we actually 202066492feaSGleb Smirnoff * have data to make use of it); 202166492feaSGleb Smirnoff * 3. send buffer fill has not hit maximal automatic size; 202266492feaSGleb Smirnoff * 4. our send window (slow start and cogestion controlled) is 202366492feaSGleb Smirnoff * larger than sent but unacknowledged data in send buffer. 202466492feaSGleb Smirnoff * 202566492feaSGleb Smirnoff * The remote host receive window scaling factor may limit the 202666492feaSGleb Smirnoff * growing of the send buffer before it reaches its allowed 202766492feaSGleb Smirnoff * maximum. 202866492feaSGleb Smirnoff * 202966492feaSGleb Smirnoff * It scales directly with slow start or congestion window 203066492feaSGleb Smirnoff * and does at most one step per received ACK. This fast 203166492feaSGleb Smirnoff * scaling has the drawback of growing the send buffer beyond 203266492feaSGleb Smirnoff * what is strictly necessary to make full use of a given 203366492feaSGleb Smirnoff * delay*bandwidth product. However testing has shown this not 203466492feaSGleb Smirnoff * to be much of an problem. At worst we are trading wasting 203566492feaSGleb Smirnoff * of available bandwidth (the non-use of it) for wasting some 203666492feaSGleb Smirnoff * socket buffer memory. 203766492feaSGleb Smirnoff * 203866492feaSGleb Smirnoff * TODO: Shrink send buffer during idle periods together 203966492feaSGleb Smirnoff * with congestion window. Requires another timer. Has to 204066492feaSGleb Smirnoff * wait for upcoming tcp timer rewrite. 204166492feaSGleb Smirnoff * 204266492feaSGleb Smirnoff * XXXGL: should there be used sbused() or sbavail()? 204366492feaSGleb Smirnoff */ 204466492feaSGleb Smirnoff if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 204566492feaSGleb Smirnoff int lowat; 204666492feaSGleb Smirnoff 204766492feaSGleb Smirnoff lowat = V_tcp_sendbuf_auto_lowat ? so->so_snd.sb_lowat : 0; 204866492feaSGleb Smirnoff if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat - lowat && 204966492feaSGleb Smirnoff sbused(&so->so_snd) >= 205066492feaSGleb Smirnoff (so->so_snd.sb_hiwat / 8 * 7) - lowat && 205166492feaSGleb Smirnoff sbused(&so->so_snd) < V_tcp_autosndbuf_max && 205266492feaSGleb Smirnoff sendwin >= (sbused(&so->so_snd) - 205366492feaSGleb Smirnoff (tp->snd_nxt - tp->snd_una))) { 205466492feaSGleb Smirnoff if (!sbreserve_locked(&so->so_snd, 205566492feaSGleb Smirnoff min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc, 205666492feaSGleb Smirnoff V_tcp_autosndbuf_max), so, curthread)) 205766492feaSGleb Smirnoff so->so_snd.sb_flags &= ~SB_AUTOSIZE; 205866492feaSGleb Smirnoff } 205966492feaSGleb Smirnoff } 206066492feaSGleb Smirnoff } 2061