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> 45adc56f5aSEdward Tomasz Napierala #include <sys/arb.h> 46686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h> 47bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 4839bc9de5SLawrence Stewart #include <sys/hhook.h> 49bd79708dSJonathan T. Looney #endif 50fb919e4dSMark Murray #include <sys/kernel.h> 51b2e60773SJohn Baldwin #ifdef KERN_TLS 52b2e60773SJohn Baldwin #include <sys/ktls.h> 53b2e60773SJohn Baldwin #endif 54fb919e4dSMark Murray #include <sys/lock.h> 55fb919e4dSMark Murray #include <sys/mbuf.h> 56fb919e4dSMark Murray #include <sys/mutex.h> 57df8bae1dSRodney W. Grimes #include <sys/protosw.h> 58adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h> 5957f60867SMark Johnston #include <sys/sdt.h> 60df8bae1dSRodney W. Grimes #include <sys/socket.h> 61df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 62fb919e4dSMark Murray #include <sys/sysctl.h> 63adc56f5aSEdward Tomasz Napierala #include <sys/stats.h> 64df8bae1dSRodney W. Grimes 654b79449eSBjoern A. Zeeb #include <net/if.h> 66df8bae1dSRodney W. Grimes #include <net/route.h> 67983066f0SAlexander V. Chernikov #include <net/route/nhop.h> 68530c0060SRobert Watson #include <net/vnet.h> 69df8bae1dSRodney W. Grimes 70df8bae1dSRodney W. Grimes #include <netinet/in.h> 7157f60867SMark Johnston #include <netinet/in_kdtrace.h> 72df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 73df8bae1dSRodney W. Grimes #include <netinet/ip.h> 74df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 75df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 76ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 77fb59c426SYoshinobu Inoue #ifdef INET6 78686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 79686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 80fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 81fb59c426SYoshinobu Inoue #endif 822de3e790SGleb Smirnoff #include <netinet/tcp.h> 83a00f4ac2SGleb Smirnoff #define TCPOUTFLAGS 84df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 852529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 86df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 87df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 88df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 89df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 904644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 91c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 9286a996e6SHiren Panchasara #ifdef TCPPCAP 9386a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 9486a996e6SHiren Panchasara #endif 95610ee2f9SDavid Greenman #ifdef TCPDEBUG 96df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 97610ee2f9SDavid Greenman #endif 9809fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 9909fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 10009fe6320SNavdeep Parhar #endif 101df8bae1dSRodney W. Grimes 102fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 103b9234fafSSam Leffler 104db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 105db4f9cc7SJonathan Lemon 106aed55708SRobert Watson #include <security/mac/mac_framework.h> 107aed55708SRobert Watson 10882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, path_mtu_discovery) = 1; 1096df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_VNET | CTLFLAG_RW, 110eddfbb76SRobert Watson &VNET_NAME(path_mtu_discovery), 1, 111eddfbb76SRobert Watson "Enable Path MTU Discovery"); 1129a039a5fSDavid Greenman 11382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_tso) = 1; 1146df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_VNET | CTLFLAG_RW, 115eddfbb76SRobert Watson &VNET_NAME(tcp_do_tso), 0, 116eddfbb76SRobert Watson "Enable TCP Segmentation Offload"); 117b3c0f300SAndre Oppermann 118873789cbSAndre Oppermann VNET_DEFINE(int, tcp_sendspace) = 1024*32; 119873789cbSAndre Oppermann #define V_tcp_sendspace VNET(tcp_sendspace) 1206df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_VNET | CTLFLAG_RW, 121873789cbSAndre Oppermann &VNET_NAME(tcp_sendspace), 0, "Initial send socket buffer size"); 122873789cbSAndre Oppermann 12382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autosndbuf) = 1; 1246df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 125eddfbb76SRobert Watson &VNET_NAME(tcp_do_autosndbuf), 0, 126eddfbb76SRobert Watson "Enable automatic send buffer sizing"); 1276741ecf5SAndre Oppermann 12882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_inc) = 8*1024; 1296df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 130eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_inc), 0, 1318b615593SMarko Zec "Incrementor step size of automatic send buffer"); 1326741ecf5SAndre Oppermann 133b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_max) = 2*1024*1024; 1346df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 135eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_max), 0, 1368b615593SMarko Zec "Max size of automatic send buffer"); 1376741ecf5SAndre Oppermann 138ac952dd2SSean Bruno VNET_DEFINE(int, tcp_sendbuf_auto_lowat) = 0; 139ac952dd2SSean Bruno #define V_tcp_sendbuf_auto_lowat VNET(tcp_sendbuf_auto_lowat) 140ac952dd2SSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto_lowat, CTLFLAG_VNET | CTLFLAG_RW, 141ac952dd2SSean Bruno &VNET_NAME(tcp_sendbuf_auto_lowat), 0, 142ac952dd2SSean Bruno "Modify threshold for auto send buffer growth to account for SO_SNDLOWAT"); 143ac952dd2SSean Bruno 144425b7639SSepherosa Ziehau /* 145425b7639SSepherosa Ziehau * Make sure that either retransmit or persist timer is set for SYN, FIN and 146425b7639SSepherosa Ziehau * non-ACK. 147425b7639SSepherosa Ziehau */ 148425b7639SSepherosa Ziehau #define TCP_XMIT_TIMER_ASSERT(tp, len, th_flags) \ 149fd29ff5dSRandall Stewart KASSERT(((len) == 0 && ((th_flags) & (TH_SYN | TH_FIN)) == 0) ||\ 150425b7639SSepherosa Ziehau tcp_timer_active((tp), TT_REXMT) || \ 151425b7639SSepherosa Ziehau tcp_timer_active((tp), TT_PERSIST), \ 152425b7639SSepherosa Ziehau ("neither rexmt nor persist timer is set")) 153425b7639SSepherosa Ziehau 154dbc42409SLawrence Stewart static void inline cc_after_idle(struct tcpcb *tp); 155dbc42409SLawrence Stewart 156bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 157dbc42409SLawrence Stewart /* 1580f5e7edcSKevin Lo * Wrapper for the TCP established output helper hook. 15939bc9de5SLawrence Stewart */ 16089e560f4SRandall Stewart void 16139bc9de5SLawrence Stewart hhook_run_tcp_est_out(struct tcpcb *tp, struct tcphdr *th, 1623ac12506SJonathan T. Looney struct tcpopt *to, uint32_t len, int tso) 16339bc9de5SLawrence Stewart { 16439bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 16539bc9de5SLawrence Stewart 16639bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_OUT]->hhh_nhooks > 0) { 16739bc9de5SLawrence Stewart hhook_data.tp = tp; 16839bc9de5SLawrence Stewart hhook_data.th = th; 16939bc9de5SLawrence Stewart hhook_data.to = to; 17039bc9de5SLawrence Stewart hhook_data.len = len; 17139bc9de5SLawrence Stewart hhook_data.tso = tso; 17239bc9de5SLawrence Stewart 17339bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_OUT], &hhook_data, 17439bc9de5SLawrence Stewart tp->osd); 17539bc9de5SLawrence Stewart } 17639bc9de5SLawrence Stewart } 177bd79708dSJonathan T. Looney #endif 17839bc9de5SLawrence Stewart 17939bc9de5SLawrence Stewart /* 180dbc42409SLawrence Stewart * CC wrapper hook functions 181dbc42409SLawrence Stewart */ 182dbc42409SLawrence Stewart static void inline 183dbc42409SLawrence Stewart cc_after_idle(struct tcpcb *tp) 184dbc42409SLawrence Stewart { 185dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 186dbc42409SLawrence Stewart 187dbc42409SLawrence Stewart if (CC_ALGO(tp)->after_idle != NULL) 188dbc42409SLawrence Stewart CC_ALGO(tp)->after_idle(tp->ccv); 189dbc42409SLawrence Stewart } 1906741ecf5SAndre Oppermann 191df8bae1dSRodney W. Grimes /* 192df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 193df8bae1dSRodney W. Grimes */ 194df8bae1dSRodney W. Grimes int 195f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp) 196df8bae1dSRodney W. Grimes { 197f10e85d7SLuigi Rizzo struct socket *so = tp->t_inpcb->inp_socket; 1983ac12506SJonathan T. Looney int32_t len; 1993ac12506SJonathan T. Looney uint32_t recwin, sendwin; 200b287c6c7SBjoern A. Zeeb int off, flags, error = 0; /* Keep compiler happy */ 201581a046aSRandall Stewart u_int if_hw_tsomaxsegcount = 0; 20282e837f8SWarner Losh u_int if_hw_tsomaxsegsize = 0; 203f10e85d7SLuigi Rizzo struct mbuf *m; 204fb59c426SYoshinobu Inoue struct ip *ip = NULL; 205151ba793SAlexander Kabaev #ifdef TCPDEBUG 206f10e85d7SLuigi Rizzo struct ipovly *ipov = NULL; 207151ba793SAlexander Kabaev #endif 208f10e85d7SLuigi Rizzo struct tcphdr *th; 209a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 210a04884fcSBill Fenner unsigned ipoptlen, optlen, hdrlen; 211fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 2129ad0173dSBjoern A. Zeeb unsigned ipsec_optlen = 0; 2139ad0173dSBjoern A. Zeeb #endif 21410d20c84SMatt Macy int idle, sendalot, curticks; 21502a1a643SAndre Oppermann int sack_rxmit, sack_bytes_rxmt; 2166d90faf3SPaul Saab struct sackhole *p; 217df0633a1SGleb Smirnoff int tso, mtu; 21802a1a643SAndre Oppermann struct tcpopt to; 219c560df6fSPatrick Kelsey unsigned int wanted_cookie = 0; 220c560df6fSPatrick Kelsey unsigned int dont_sendalot = 0; 221262c1c1aSMatthew Dillon #if 0 22246f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 223262c1c1aSMatthew Dillon #endif 224fb59c426SYoshinobu Inoue #ifdef INET6 225f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 226fb59c426SYoshinobu Inoue int isipv6; 227fb59c426SYoshinobu Inoue 228fb59c426SYoshinobu Inoue isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 229fb59c426SYoshinobu Inoue #endif 230b2e60773SJohn Baldwin #ifdef KERN_TLS 231b2e60773SJohn Baldwin const bool hw_tls = (so->so_snd.sb_flags & SB_TLS_IFNET) != 0; 232b2e60773SJohn Baldwin #else 233b2e60773SJohn Baldwin const bool hw_tls = false; 234b2e60773SJohn Baldwin #endif 235df8bae1dSRodney W. Grimes 236109eb549SGleb Smirnoff NET_EPOCH_ASSERT(); 2378501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2383d6ade3aSJennifer Yang 23909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 24009fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 24109fe6320SNavdeep Parhar return (tcp_offload_output(tp)); 24209fe6320SNavdeep Parhar #endif 24309fe6320SNavdeep Parhar 244281a0fd4SPatrick Kelsey /* 2458db239dcSMichael Tuexen * For TFO connections in SYN_SENT or SYN_RECEIVED, 2468db239dcSMichael Tuexen * only allow the initial SYN or SYN|ACK and those sent 2478db239dcSMichael Tuexen * by the retransmit timer. 248281a0fd4SPatrick Kelsey */ 24968bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 2508db239dcSMichael Tuexen ((tp->t_state == TCPS_SYN_SENT) || 2518db239dcSMichael Tuexen (tp->t_state == TCPS_SYN_RECEIVED)) && 2528db239dcSMichael Tuexen SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 253281a0fd4SPatrick Kelsey (tp->snd_nxt != tp->snd_una)) /* not a retransmit */ 254281a0fd4SPatrick Kelsey return (0); 25518a75309SPatrick Kelsey 256df8bae1dSRodney W. Grimes /* 257df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 258df8bae1dSRodney W. Grimes * and flags that will be used. 259df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 260df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 261df8bae1dSRodney W. Grimes */ 262c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 263*9dc7d8a2SRichard Scheffenegger if (idle && (((ticks - tp->t_rcvtime) >= tp->t_rxtcur) || 264*9dc7d8a2SRichard Scheffenegger (tp->t_sndtime && ((ticks - tp->t_sndtime) >= tp->t_rxtcur)))) 2652ea8da28SLawrence Stewart cc_after_idle(tp); 266c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 267c24d5daeSJayanth Vijayaraghavan if (idle) { 268c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 269c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 270c24d5daeSJayanth Vijayaraghavan idle = 0; 271c24d5daeSJayanth Vijayaraghavan } 272c24d5daeSJayanth Vijayaraghavan } 273df8bae1dSRodney W. Grimes again: 2746d90faf3SPaul Saab /* 2756d90faf3SPaul Saab * If we've recently taken a timeout, snd_max will be greater than 2766d90faf3SPaul Saab * snd_nxt. There may be SACK information that allows us to avoid 2776d90faf3SPaul Saab * resending already delivered data. Adjust snd_nxt accordingly. 2786d90faf3SPaul Saab */ 2793529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2803529149eSAndre Oppermann SEQ_LT(tp->snd_nxt, tp->snd_max)) 2816d90faf3SPaul Saab tcp_sack_adjust(tp); 282df8bae1dSRodney W. Grimes sendalot = 0; 283153e5b57SAndre Oppermann tso = 0; 284df0633a1SGleb Smirnoff mtu = 0; 285df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 286201d185bSAndre Oppermann sendwin = min(tp->snd_wnd, tp->snd_cwnd); 287df8bae1dSRodney W. Grimes 288df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 289a0292f23SGarrett Wollman /* 2906d90faf3SPaul Saab * Send any SACK-generated retransmissions. If we're explicitly trying 2916d90faf3SPaul Saab * to send out new data (when sendalot is 1), bypass this function. 2926d90faf3SPaul Saab * If we retransmit in fast recovery mode, decrement snd_cwnd, since 2936d90faf3SPaul Saab * we're replacing a (future) new transmission with a retransmission 2946d90faf3SPaul Saab * now, and we previously incremented snd_cwnd in tcp_input(). 2956d90faf3SPaul Saab */ 2966d90faf3SPaul Saab /* 2976d90faf3SPaul Saab * Still in sack recovery , reset rxmit flag to zero. 2986d90faf3SPaul Saab */ 2996d90faf3SPaul Saab sack_rxmit = 0; 300a55db2b6SPaul Saab sack_bytes_rxmt = 0; 3016d90faf3SPaul Saab len = 0; 3026d90faf3SPaul Saab p = NULL; 303dbc42409SLawrence Stewart if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp->t_flags) && 304a55db2b6SPaul Saab (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 3053ac12506SJonathan T. Looney uint32_t cwin; 306a55db2b6SPaul Saab 3073ac12506SJonathan T. Looney cwin = 3083ac12506SJonathan T. Looney imax(min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt, 0); 309f787edd8SJayanth Vijayaraghavan /* Do not retransmit SACK segments beyond snd_recover */ 310f787edd8SJayanth Vijayaraghavan if (SEQ_GT(p->end, tp->snd_recover)) { 311f787edd8SJayanth Vijayaraghavan /* 312f787edd8SJayanth Vijayaraghavan * (At least) part of sack hole extends beyond 313f787edd8SJayanth Vijayaraghavan * snd_recover. Check to see if we can rexmit data 314f787edd8SJayanth Vijayaraghavan * for this hole. 315f787edd8SJayanth Vijayaraghavan */ 316f787edd8SJayanth Vijayaraghavan if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 317f787edd8SJayanth Vijayaraghavan /* 318f787edd8SJayanth Vijayaraghavan * Can't rexmit any more data for this hole. 319f787edd8SJayanth Vijayaraghavan * That data will be rexmitted in the next 320f787edd8SJayanth Vijayaraghavan * sack recovery episode, when snd_recover 321f787edd8SJayanth Vijayaraghavan * moves past p->rxmit. 322f787edd8SJayanth Vijayaraghavan */ 323f787edd8SJayanth Vijayaraghavan p = NULL; 324f787edd8SJayanth Vijayaraghavan goto after_sack_rexmit; 325f787edd8SJayanth Vijayaraghavan } else 326f787edd8SJayanth Vijayaraghavan /* Can rexmit part of the current hole */ 3273ac12506SJonathan T. Looney len = ((int32_t)ulmin(cwin, 328f787edd8SJayanth Vijayaraghavan tp->snd_recover - p->rxmit)); 329f787edd8SJayanth Vijayaraghavan } else 3303ac12506SJonathan T. Looney len = ((int32_t)ulmin(cwin, p->end - p->rxmit)); 3316d90faf3SPaul Saab off = p->rxmit - tp->snd_una; 332f787edd8SJayanth Vijayaraghavan KASSERT(off >= 0,("%s: sack block to the left of una : %d", 333f787edd8SJayanth Vijayaraghavan __func__, off)); 3346d90faf3SPaul Saab if (len > 0) { 335ab5c14d8SRobert Watson sack_rxmit = 1; 336ab5c14d8SRobert Watson sendalot = 1; 33778b50714SRobert Watson TCPSTAT_INC(tcps_sack_rexmits); 33878b50714SRobert Watson TCPSTAT_ADD(tcps_sack_rexmit_bytes, 33978b50714SRobert Watson min(len, tp->t_maxseg)); 3406d90faf3SPaul Saab } 3416d90faf3SPaul Saab } 342f787edd8SJayanth Vijayaraghavan after_sack_rexmit: 3436d90faf3SPaul Saab /* 344a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 345a0292f23SGarrett Wollman * state flags. 346a0292f23SGarrett Wollman */ 347a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 348a0292f23SGarrett Wollman flags |= TH_FIN; 349a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 350a0292f23SGarrett Wollman flags |= TH_SYN; 351a0292f23SGarrett Wollman 352cf2942b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 353df8bae1dSRodney W. Grimes /* 354df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 355df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 356df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 357df8bae1dSRodney W. Grimes * and go to transmit state. 358df8bae1dSRodney W. Grimes */ 3592cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) { 360201d185bSAndre Oppermann if (sendwin == 0) { 361df8bae1dSRodney W. Grimes /* 362df8bae1dSRodney W. Grimes * If we still have some data to send, then 363df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 364df8bae1dSRodney W. Grimes * happen below when it realizes that we 365df8bae1dSRodney W. Grimes * aren't sending all the data. However, 36629089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 367df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 368df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 369df8bae1dSRodney W. Grimes * up sending the packet without recording 370df8bae1dSRodney W. Grimes * that we sent the FIN bit. 371df8bae1dSRodney W. Grimes * 372df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 373df8bae1dSRodney W. Grimes * because if we don't have any more data 374df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 375df8bae1dSRodney W. Grimes * itself. 376df8bae1dSRodney W. Grimes */ 377cfa6009eSGleb Smirnoff if (off < sbused(&so->so_snd)) 378df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 379201d185bSAndre Oppermann sendwin = 1; 380df8bae1dSRodney W. Grimes } else { 381b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, 0); 382df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 383df8bae1dSRodney W. Grimes } 384df8bae1dSRodney W. Grimes } 385df8bae1dSRodney W. Grimes 38628257b5cSMatthew Dillon /* 38728257b5cSMatthew Dillon * If snd_nxt == snd_max and we have transmitted a FIN, the 38828257b5cSMatthew Dillon * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 389201d185bSAndre Oppermann * a negative length. This can also occur when TCP opens up 39028257b5cSMatthew Dillon * its congestion window while receiving additional duplicate 39128257b5cSMatthew Dillon * acks after fast-retransmit because TCP will reset snd_nxt 39228257b5cSMatthew Dillon * to snd_max after the fast-retransmit. 39328257b5cSMatthew Dillon * 39428257b5cSMatthew Dillon * In the normal retransmit-FIN-only case, however, snd_nxt will 39528257b5cSMatthew Dillon * be set to snd_una, the offset will be 0, and the length may 39628257b5cSMatthew Dillon * wind up 0. 3976d90faf3SPaul Saab * 3986d90faf3SPaul Saab * If sack_rxmit is true we are retransmitting from the scoreboard 3996d90faf3SPaul Saab * in which case len is already set. 40028257b5cSMatthew Dillon */ 401a55db2b6SPaul Saab if (sack_rxmit == 0) { 402a55db2b6SPaul Saab if (sack_bytes_rxmt == 0) 403cb503ae2SJonathan T. Looney len = ((int32_t)min(sbavail(&so->so_snd), sendwin) - 404cfa6009eSGleb Smirnoff off); 405a55db2b6SPaul Saab else { 4063ac12506SJonathan T. Looney int32_t cwin; 407a55db2b6SPaul Saab 408a55db2b6SPaul Saab /* 409a55db2b6SPaul Saab * We are inside of a SACK recovery episode and are 410a55db2b6SPaul Saab * sending new data, having retransmitted all the 411a55db2b6SPaul Saab * data possible in the scoreboard. 412a55db2b6SPaul Saab */ 4133ac12506SJonathan T. Looney len = ((int32_t)min(sbavail(&so->so_snd), tp->snd_wnd) - 414cfa6009eSGleb Smirnoff off); 4158d03f2b5SPaul Saab /* 4168d03f2b5SPaul Saab * Don't remove this (len > 0) check ! 4178d03f2b5SPaul Saab * We explicitly check for len > 0 here (although it 4188d03f2b5SPaul Saab * isn't really necessary), to work around a gcc 4198d03f2b5SPaul Saab * optimization issue - to force gcc to compute 4208d03f2b5SPaul Saab * len above. Without this check, the computation 4218d03f2b5SPaul Saab * of len is bungled by the optimizer. 4228d03f2b5SPaul Saab */ 4238d03f2b5SPaul Saab if (len > 0) { 4247d5ed1ceSPaul Saab cwin = tp->snd_cwnd - 425a3574665SMichael Tuexen (tp->snd_nxt - tp->snd_recover) - 426a55db2b6SPaul Saab sack_bytes_rxmt; 427a55db2b6SPaul Saab if (cwin < 0) 428a55db2b6SPaul Saab cwin = 0; 4293ac12506SJonathan T. Looney len = imin(len, cwin); 430a55db2b6SPaul Saab } 431a55db2b6SPaul Saab } 4328d03f2b5SPaul Saab } 433a0292f23SGarrett Wollman 434a0292f23SGarrett Wollman /* 435a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 436a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 437a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 438a0292f23SGarrett Wollman */ 439a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 4404b8e98d6SQing Li if (tp->t_state != TCPS_SYN_RECEIVED) 441a0292f23SGarrett Wollman flags &= ~TH_SYN; 442281a0fd4SPatrick Kelsey /* 443281a0fd4SPatrick Kelsey * When sending additional segments following a TFO SYN|ACK, 444281a0fd4SPatrick Kelsey * do not include the SYN bit. 445281a0fd4SPatrick Kelsey */ 44668bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 447281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 448281a0fd4SPatrick Kelsey flags &= ~TH_SYN; 449a0292f23SGarrett Wollman off--, len++; 450a0292f23SGarrett Wollman } 451a0292f23SGarrett Wollman 45281165e48SAndras Olah /* 453c94c54e4SAndre Oppermann * Be careful not to send data and/or FIN on SYN segments. 45481165e48SAndras Olah * This measure is needed to prevent interoperability problems 45581165e48SAndras Olah * with not fully conformant TCP implementations. 45681165e48SAndras Olah */ 457c94c54e4SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 45881165e48SAndras Olah len = 0; 45981165e48SAndras Olah flags &= ~TH_FIN; 46081165e48SAndras Olah } 46181165e48SAndras Olah 462281a0fd4SPatrick Kelsey /* 463c560df6fSPatrick Kelsey * On TFO sockets, ensure no data is sent in the following cases: 464c560df6fSPatrick Kelsey * 465c560df6fSPatrick Kelsey * - When retransmitting SYN|ACK on a passively-created socket 466c560df6fSPatrick Kelsey * 467c560df6fSPatrick Kelsey * - When retransmitting SYN on an actively created socket 468c560df6fSPatrick Kelsey * 469c560df6fSPatrick Kelsey * - When sending a zero-length cookie (cookie request) on an 470c560df6fSPatrick Kelsey * actively created socket 471c560df6fSPatrick Kelsey * 472c560df6fSPatrick Kelsey * - When the socket is in the CLOSED state (RST is being sent) 473281a0fd4SPatrick Kelsey */ 47468bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 475c560df6fSPatrick Kelsey (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 476c560df6fSPatrick Kelsey ((tp->t_state == TCPS_SYN_SENT) && 477c560df6fSPatrick Kelsey (tp->t_tfo_client_cookie_len == 0)) || 478281a0fd4SPatrick Kelsey (flags & TH_RST))) 479281a0fd4SPatrick Kelsey len = 0; 48047a8e865SXin LI if (len <= 0) { 481df8bae1dSRodney W. Grimes /* 482df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 483df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 48428257b5cSMatthew Dillon * len will be < 0. Otherwise, window shrank 485df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 4862d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 4872d8266afSDavid Greenman * to (closed) window, and set the persist timer 4882d8266afSDavid Greenman * if it isn't already going. If the window didn't 4892d8266afSDavid Greenman * close completely, just wait for an ACK. 49047a8e865SXin LI * 49147a8e865SXin LI * We also do a general check here to ensure that 49247a8e865SXin LI * we will set the persist timer when we have data 49347a8e865SXin LI * to send, but a 0-byte window. This makes sure 49447a8e865SXin LI * the persist timer is set even if the packet 49547a8e865SXin LI * hits one of the "goto send" lines below. 496df8bae1dSRodney W. Grimes */ 497df8bae1dSRodney W. Grimes len = 0; 49847a8e865SXin LI if ((sendwin == 0) && (TCPS_HAVEESTABLISHED(tp->t_state)) && 49947a8e865SXin LI (off < (int) sbavail(&so->so_snd))) { 500b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 5012d8266afSDavid Greenman tp->t_rxtshift = 0; 502df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 503b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_PERSIST)) 5042d8266afSDavid Greenman tcp_setpersist(tp); 505df8bae1dSRodney W. Grimes } 506df8bae1dSRodney W. Grimes } 50728257b5cSMatthew Dillon 5086741ecf5SAndre Oppermann /* len will be >= 0 after this point. */ 509c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 5106741ecf5SAndre Oppermann 51166492feaSGleb Smirnoff tcp_sndbuf_autoscale(tp, so, sendwin); 5126741ecf5SAndre Oppermann 5136741ecf5SAndre Oppermann /* 514ed420311SAndre Oppermann * Decide if we can use TCP Segmentation Offloading (if supported by 515ed420311SAndre Oppermann * hardware). 516b3c0f300SAndre Oppermann * 517b3c0f300SAndre Oppermann * TSO may only be used if we are in a pure bulk sending state. The 518b3c0f300SAndre Oppermann * presence of TCP-MD5, SACK retransmits, SACK advertizements and 519b3c0f300SAndre Oppermann * IP options prevent using TSO. With TSO the TCP header is the same 520b3c0f300SAndre Oppermann * (except for the sequence number) for all generated packets. This 521b3c0f300SAndre Oppermann * makes it impossible to transmit any options which vary per generated 522b3c0f300SAndre Oppermann * segment or packet. 523b6ff6724SHiren Panchasara * 524b6ff6724SHiren Panchasara * IPv4 handling has a clear separation of ip options and ip header 525b6ff6724SHiren Panchasara * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does 526b6ff6724SHiren Panchasara * the right thing below to provide length of just ip options and thus 527b6ff6724SHiren Panchasara * checking for ipoptlen is enough to decide if ip options are present. 52828257b5cSMatthew Dillon */ 529fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 5309ad0173dSBjoern A. Zeeb /* 5319ad0173dSBjoern A. Zeeb * Pre-calculate here as we save another lookup into the darknesses 5329ad0173dSBjoern A. Zeeb * of IPsec that way and can actually decide if TSO is ok. 5339ad0173dSBjoern A. Zeeb */ 534fcf59617SAndrey V. Elsukov #ifdef INET6 535fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6)) 536fcf59617SAndrey V. Elsukov ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb); 537fcf59617SAndrey V. Elsukov #ifdef INET 538fcf59617SAndrey V. Elsukov else 5399ad0173dSBjoern A. Zeeb #endif 540fcf59617SAndrey V. Elsukov #endif /* INET6 */ 541fcf59617SAndrey V. Elsukov #ifdef INET 542fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) 543fcf59617SAndrey V. Elsukov ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb); 544fcf59617SAndrey V. Elsukov #endif /* INET */ 545fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 546b6ff6724SHiren Panchasara #ifdef INET6 547b6ff6724SHiren Panchasara if (isipv6) 548b6ff6724SHiren Panchasara ipoptlen = ip6_optlen(tp->t_inpcb); 549b6ff6724SHiren Panchasara else 550b6ff6724SHiren Panchasara #endif 551b6ff6724SHiren Panchasara if (tp->t_inpcb->inp_options) 552b6ff6724SHiren Panchasara ipoptlen = tp->t_inpcb->inp_options->m_len - 553b6ff6724SHiren Panchasara offsetof(struct ipoption, ipopt_list); 554b6ff6724SHiren Panchasara else 555b6ff6724SHiren Panchasara ipoptlen = 0; 556fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 557b6ff6724SHiren Panchasara ipoptlen += ipsec_optlen; 558b6ff6724SHiren Panchasara #endif 559b6ff6724SHiren Panchasara 560ed420311SAndre Oppermann if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg && 561b3c0f300SAndre Oppermann ((tp->t_flags & TF_SIGNATURE) == 0) && 562b3c0f300SAndre Oppermann tp->rcv_numsacks == 0 && sack_rxmit == 0 && 563c560df6fSPatrick Kelsey ipoptlen == 0 && !(flags & TH_SYN)) 564b3c0f300SAndre Oppermann tso = 1; 565153e5b57SAndre Oppermann 5665d3b1b75SJayanth Vijayaraghavan if (sack_rxmit) { 567cfa6009eSGleb Smirnoff if (SEQ_LT(p->rxmit + len, tp->snd_una + sbused(&so->so_snd))) 568df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 5695d3b1b75SJayanth Vijayaraghavan } else { 570cfa6009eSGleb Smirnoff if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + 571cfa6009eSGleb Smirnoff sbused(&so->so_snd))) 5725d3b1b75SJayanth Vijayaraghavan flags &= ~TH_FIN; 5735d3b1b75SJayanth Vijayaraghavan } 574df8bae1dSRodney W. Grimes 5753ac12506SJonathan T. Looney recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 5763ac12506SJonathan T. Looney (long)TCP_MAXWIN << tp->rcv_scale); 577df8bae1dSRodney W. Grimes 578df8bae1dSRodney W. Grimes /* 579262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 580262c1c1aSMatthew Dillon * conditions when len is non-zero: 581262c1c1aSMatthew Dillon * 582b3c0f300SAndre Oppermann * - We have a full segment (or more with TSO) 583262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 584262c1c1aSMatthew Dillon * either idle or running NODELAY 585262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 586262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 587262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 588262c1c1aSMatthew Dillon * - we need to retransmit 589df8bae1dSRodney W. Grimes */ 590df8bae1dSRodney W. Grimes if (len) { 591b3c0f300SAndre Oppermann if (len >= tp->t_maxseg) 592df8bae1dSRodney W. Grimes goto send; 593262c1c1aSMatthew Dillon /* 594262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 595262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 596262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 597262c1c1aSMatthew Dillon * 598262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 599262c1c1aSMatthew Dillon */ 600262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 601262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 6023ac12506SJonathan T. Looney (uint32_t)len + (uint32_t)off >= sbavail(&so->so_snd) && 603262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 604df8bae1dSRodney W. Grimes goto send; 605262c1c1aSMatthew Dillon } 6062cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */ 607df8bae1dSRodney W. Grimes goto send; 608a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 609df8bae1dSRodney W. Grimes goto send; 610262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 611df8bae1dSRodney W. Grimes goto send; 6126d90faf3SPaul Saab if (sack_rxmit) 6136d90faf3SPaul Saab goto send; 614df8bae1dSRodney W. Grimes } 615df8bae1dSRodney W. Grimes 616df8bae1dSRodney W. Grimes /* 617f62563d3SAndre Oppermann * Sending of standalone window updates. 618f62563d3SAndre Oppermann * 61978f59b4bSAndre Oppermann * Window updates are important when we close our window due to a 62078f59b4bSAndre Oppermann * full socket buffer and are opening it again after the application 621f62563d3SAndre Oppermann * reads data from it. Once the window has opened again and the 622f62563d3SAndre Oppermann * remote end starts to send again the ACK clock takes over and 623f62563d3SAndre Oppermann * provides the most current window information. 624f62563d3SAndre Oppermann * 62578f59b4bSAndre Oppermann * We must avoid the silly window syndrome whereas every read 626f62563d3SAndre Oppermann * from the receive buffer, no matter how small, causes a window 627f62563d3SAndre Oppermann * update to be sent. We also should avoid sending a flurry of 628f62563d3SAndre Oppermann * window updates when the socket buffer had queued a lot of data 629f62563d3SAndre Oppermann * and the application is doing small reads. 630f62563d3SAndre Oppermann * 631f62563d3SAndre Oppermann * Prevent a flurry of pointless window updates by only sending 632f62563d3SAndre Oppermann * an update when we can increase the advertized window by more 633f62563d3SAndre Oppermann * than 1/4th of the socket buffer capacity. When the buffer is 634f62563d3SAndre Oppermann * getting full or is very small be more aggressive and send an 635f62563d3SAndre Oppermann * update whenever we can increase by two mss sized segments. 636f62563d3SAndre Oppermann * In all other situations the ACK's to new incoming data will 637f62563d3SAndre Oppermann * carry further window increases. 6384249614cSAndre Oppermann * 6394249614cSAndre Oppermann * Don't send an independent window update if a delayed 6404249614cSAndre Oppermann * ACK is pending (it will get piggy-backed on it) or the 6414249614cSAndre Oppermann * remote side already has done a half-close and won't send 642f62563d3SAndre Oppermann * more data. Skip this if the connection is in T/TCP 643f62563d3SAndre Oppermann * half-open state. 644df8bae1dSRodney W. Grimes */ 645b7de7d87SAndre Oppermann if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 6464249614cSAndre Oppermann !(tp->t_flags & TF_DELACK) && 647b7de7d87SAndre Oppermann !TCPS_HAVERCVDFIN(tp->t_state)) { 648df8bae1dSRodney W. Grimes /* 649f62563d3SAndre Oppermann * "adv" is the amount we could increase the window, 650df8bae1dSRodney W. Grimes * taking into account that we are limited by 651df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 652df8bae1dSRodney W. Grimes */ 6533ac12506SJonathan T. Looney int32_t adv; 654f701e30dSJohn Baldwin int oldwin; 655f701e30dSJohn Baldwin 6563ac12506SJonathan T. Looney adv = recwin; 657f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 658f701e30dSJohn Baldwin oldwin = (tp->rcv_adv - tp->rcv_nxt); 659f092a3c7SRandall Stewart if (adv > oldwin) 660f701e30dSJohn Baldwin adv -= oldwin; 661f092a3c7SRandall Stewart else 662f092a3c7SRandall Stewart adv = 0; 663f701e30dSJohn Baldwin } else 664f701e30dSJohn Baldwin oldwin = 0; 665df8bae1dSRodney W. Grimes 666da84b2e6SJohn Baldwin /* 6670dda76b8SJonathan T. Looney * If the new window size ends up being the same as or less 6680dda76b8SJonathan T. Looney * than the old size when it is scaled, then don't force 6690dda76b8SJonathan T. Looney * a window update. 670da84b2e6SJohn Baldwin */ 6710dda76b8SJonathan T. Looney if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 672da84b2e6SJohn Baldwin goto dontupdate; 673f62563d3SAndre Oppermann 6743ac12506SJonathan T. Looney if (adv >= (int32_t)(2 * tp->t_maxseg) && 6753ac12506SJonathan T. Looney (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) || 6763ac12506SJonathan T. Looney recwin <= (so->so_rcv.sb_hiwat / 8) || 67750075939SStephen Hurd so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg || 67850075939SStephen Hurd adv >= TCP_MAXWIN << tp->rcv_scale)) 679df8bae1dSRodney W. Grimes goto send; 6808d62aae8SMichael Tuexen if (2 * adv >= (int32_t)so->so_rcv.sb_hiwat) 6818d62aae8SMichael Tuexen goto send; 682df8bae1dSRodney W. Grimes } 683da84b2e6SJohn Baldwin dontupdate: 684df8bae1dSRodney W. Grimes 685df8bae1dSRodney W. Grimes /* 68628257b5cSMatthew Dillon * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 68728257b5cSMatthew Dillon * is also a catch-all for the retransmit timer timeout case. 688df8bae1dSRodney W. Grimes */ 689df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 690df8bae1dSRodney W. Grimes goto send; 691a0292f23SGarrett Wollman if ((flags & TH_RST) || 692a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 693df8bae1dSRodney W. Grimes goto send; 694df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 695df8bae1dSRodney W. Grimes goto send; 696df8bae1dSRodney W. Grimes /* 697df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 69828257b5cSMatthew Dillon * and we have not yet done so, then we need to send. 699df8bae1dSRodney W. Grimes */ 700df8bae1dSRodney W. Grimes if (flags & TH_FIN && 701df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 702df8bae1dSRodney W. Grimes goto send; 7036d90faf3SPaul Saab /* 7046d90faf3SPaul Saab * In SACK, it is possible for tcp_output to fail to send a segment 7056d90faf3SPaul Saab * after the retransmission timer has been turned off. Make sure 7066d90faf3SPaul Saab * that the retransmission timer is set. 7076d90faf3SPaul Saab */ 7083529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 7093529149eSAndre Oppermann SEQ_GT(tp->snd_max, tp->snd_una) && 710b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_REXMT) && 711b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 712b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 713cf2942b6SRobert Watson goto just_return; 7146d90faf3SPaul Saab } 715df8bae1dSRodney W. Grimes /* 716df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 717df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 718df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 719df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 720df8bae1dSRodney W. Grimes * persisting to move a small or zero window 721df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 722df8bae1dSRodney W. Grimes * 723b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_PERSIST) 7249b8b58e0SJonathan Lemon * is true when we are in persist state. 7252cdbfa66SPaul Saab * (tp->t_flags & TF_FORCEDATA) 726df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 727b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_REXMT) 728df8bae1dSRodney W. Grimes * is set when we are retransmitting 729df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 730df8bae1dSRodney W. Grimes * 731df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 732df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 733df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 734df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 735df8bae1dSRodney W. Grimes * otherwise force out a byte. 736df8bae1dSRodney W. Grimes */ 737cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd) && !tcp_timer_active(tp, TT_REXMT) && 738b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 739df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 740df8bae1dSRodney W. Grimes tcp_setpersist(tp); 741df8bae1dSRodney W. Grimes } 742df8bae1dSRodney W. Grimes 743df8bae1dSRodney W. Grimes /* 744df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 745df8bae1dSRodney W. Grimes */ 746cf2942b6SRobert Watson just_return: 747cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 748df8bae1dSRodney W. Grimes return (0); 749df8bae1dSRodney W. Grimes 750df8bae1dSRodney W. Grimes send: 751cf2942b6SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 752f6f6703fSSean Bruno if (len > 0) { 753f6f6703fSSean Bruno if (len >= tp->t_maxseg) 754f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 755f6f6703fSSean Bruno else 756f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 757f6f6703fSSean Bruno } 758df8bae1dSRodney W. Grimes /* 759df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 760df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 761df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 762df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 763df8bae1dSRodney W. Grimes * link header, i.e. 76433841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 765df8bae1dSRodney W. Grimes */ 766df8bae1dSRodney W. Grimes optlen = 0; 767fb59c426SYoshinobu Inoue #ifdef INET6 768fb59c426SYoshinobu Inoue if (isipv6) 769fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 770fb59c426SYoshinobu Inoue else 771fb59c426SYoshinobu Inoue #endif 772df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 77302a1a643SAndre Oppermann 77402a1a643SAndre Oppermann /* 77502a1a643SAndre Oppermann * Compute options for segment. 77602a1a643SAndre Oppermann * We only have to care about SYN and established connection 77702a1a643SAndre Oppermann * segments. Options for SYN-ACK segments are handled in TCP 77802a1a643SAndre Oppermann * syncache. 77902a1a643SAndre Oppermann */ 78002a1a643SAndre Oppermann to.to_flags = 0; 781f73d9fd2SGleb Smirnoff if ((tp->t_flags & TF_NOOPT) == 0) { 78202a1a643SAndre Oppermann /* Maximum segment size. */ 783df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 784df8bae1dSRodney W. Grimes tp->snd_nxt = tp->iss; 78502a1a643SAndre Oppermann to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc); 78602a1a643SAndre Oppermann to.to_flags |= TOF_MSS; 78718a75309SPatrick Kelsey 788281a0fd4SPatrick Kelsey /* 789c560df6fSPatrick Kelsey * On SYN or SYN|ACK transmits on TFO connections, 790c560df6fSPatrick Kelsey * only include the TFO option if it is not a 791c560df6fSPatrick Kelsey * retransmit, as the presence of the TFO option may 792c560df6fSPatrick Kelsey * have caused the original SYN or SYN|ACK to have 793c560df6fSPatrick Kelsey * been dropped by a middlebox. 794281a0fd4SPatrick Kelsey */ 79568bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 796281a0fd4SPatrick Kelsey (tp->t_rxtshift == 0)) { 797c560df6fSPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED) { 798281a0fd4SPatrick Kelsey to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 799c560df6fSPatrick Kelsey to.to_tfo_cookie = 800c560df6fSPatrick Kelsey (u_int8_t *)&tp->t_tfo_cookie.server; 801281a0fd4SPatrick Kelsey to.to_flags |= TOF_FASTOPEN; 802c560df6fSPatrick Kelsey wanted_cookie = 1; 803c560df6fSPatrick Kelsey } else if (tp->t_state == TCPS_SYN_SENT) { 804c560df6fSPatrick Kelsey to.to_tfo_len = 805c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len; 806c560df6fSPatrick Kelsey to.to_tfo_cookie = 807c560df6fSPatrick Kelsey tp->t_tfo_cookie.client; 808c560df6fSPatrick Kelsey to.to_flags |= TOF_FASTOPEN; 809c560df6fSPatrick Kelsey wanted_cookie = 1; 810c560df6fSPatrick Kelsey /* 811c560df6fSPatrick Kelsey * If we wind up having more data to 812c560df6fSPatrick Kelsey * send with the SYN than can fit in 813c560df6fSPatrick Kelsey * one segment, don't send any more 814c560df6fSPatrick Kelsey * until the SYN|ACK comes back from 815c560df6fSPatrick Kelsey * the other end. 816c560df6fSPatrick Kelsey */ 817c560df6fSPatrick Kelsey dont_sendalot = 1; 818c560df6fSPatrick Kelsey } 819281a0fd4SPatrick Kelsey } 820df8bae1dSRodney W. Grimes } 82102a1a643SAndre Oppermann /* Window scaling. */ 82202a1a643SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 82302a1a643SAndre Oppermann to.to_wscale = tp->request_r_scale; 82402a1a643SAndre Oppermann to.to_flags |= TOF_SCALE; 825df8bae1dSRodney W. Grimes } 82602a1a643SAndre Oppermann /* Timestamps. */ 82702a1a643SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) || 82802a1a643SAndre Oppermann ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 82910d20c84SMatt Macy curticks = tcp_ts_getticks(); 83010d20c84SMatt Macy to.to_tsval = curticks + tp->ts_offset; 83102a1a643SAndre Oppermann to.to_tsecr = tp->ts_recent; 83202a1a643SAndre Oppermann to.to_flags |= TOF_TS; 83310d20c84SMatt Macy if (tp->t_rxtshift == 1) 83410d20c84SMatt Macy tp->t_badrxtwin = curticks; 835e44c1887SSteven Hartland } 836e44c1887SSteven Hartland 8376741ecf5SAndre Oppermann /* Set receive buffer autosizing timestamp. */ 83802a1a643SAndre Oppermann if (tp->rfbuf_ts == 0 && 83902a1a643SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) 840d8951c8aSBjoern A. Zeeb tp->rfbuf_ts = tcp_ts_getticks(); 841e44c1887SSteven Hartland 84202a1a643SAndre Oppermann /* Selective ACK's. */ 8433529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 84402a1a643SAndre Oppermann if (flags & TH_SYN) 84502a1a643SAndre Oppermann to.to_flags |= TOF_SACKPERM; 84602a1a643SAndre Oppermann else if (TCPS_HAVEESTABLISHED(tp->t_state) && 84702a1a643SAndre Oppermann (tp->t_flags & TF_SACK_PERMIT) && 84802a1a643SAndre Oppermann tp->rcv_numsacks > 0) { 84902a1a643SAndre Oppermann to.to_flags |= TOF_SACK; 85002a1a643SAndre Oppermann to.to_nsacks = tp->rcv_numsacks; 85102a1a643SAndre Oppermann to.to_sacks = (u_char *)tp->sackblks; 85202a1a643SAndre Oppermann } 85302a1a643SAndre Oppermann } 854fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 85502a1a643SAndre Oppermann /* TCP-MD5 (RFC2385). */ 856fcf59617SAndrey V. Elsukov /* 857fcf59617SAndrey V. Elsukov * Check that TCP_MD5SIG is enabled in tcpcb to 858fcf59617SAndrey V. Elsukov * account the size needed to set this TCP option. 859fcf59617SAndrey V. Elsukov */ 86002a1a643SAndre Oppermann if (tp->t_flags & TF_SIGNATURE) 86102a1a643SAndre Oppermann to.to_flags |= TOF_SIGNATURE; 8621cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 8631cfd4b53SBruce M Simpson 86402a1a643SAndre Oppermann /* Processing the options. */ 8654a411b9fSBjoern A. Zeeb hdrlen += optlen = tcp_addoptions(&to, opt); 866c560df6fSPatrick Kelsey /* 867c560df6fSPatrick Kelsey * If we wanted a TFO option to be added, but it was unable 868c560df6fSPatrick Kelsey * to fit, ensure no data is sent. 869c560df6fSPatrick Kelsey */ 870c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && wanted_cookie && 871c560df6fSPatrick Kelsey !(to.to_flags & TOF_FASTOPEN)) 872c560df6fSPatrick Kelsey len = 0; 873be3f3b5eSPaul Saab } 874be3f3b5eSPaul Saab 875df8bae1dSRodney W. Grimes /* 876df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 8770c39d38dSGleb Smirnoff * bump the packet length beyond the t_maxseg length. 878a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 879a0292f23SGarrett Wollman * the segment. 880df8bae1dSRodney W. Grimes */ 8810c39d38dSGleb Smirnoff if (len + optlen + ipoptlen > tp->t_maxseg) { 882297a37f3SDavid Greenman flags &= ~TH_FIN; 883ed420311SAndre Oppermann 884b3c0f300SAndre Oppermann if (tso) { 8859fd573c3SHans Petter Selasky u_int if_hw_tsomax; 8869fd573c3SHans Petter Selasky u_int moff; 8879fd573c3SHans Petter Selasky int max_len; 8889fd573c3SHans Petter Selasky 8899fd573c3SHans Petter Selasky /* extract TSO information */ 8909fd573c3SHans Petter Selasky if_hw_tsomax = tp->t_tsomax; 8919fd573c3SHans Petter Selasky if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 8929fd573c3SHans Petter Selasky if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 8939fd573c3SHans Petter Selasky 8949fd573c3SHans Petter Selasky /* 8959fd573c3SHans Petter Selasky * Limit a TSO burst to prevent it from 8969fd573c3SHans Petter Selasky * overflowing or exceeding the maximum length 8979fd573c3SHans Petter Selasky * allowed by the network interface: 8989fd573c3SHans Petter Selasky */ 899ed420311SAndre Oppermann KASSERT(ipoptlen == 0, 900ed420311SAndre Oppermann ("%s: TSO can't do IP options", __func__)); 901ed420311SAndre Oppermann 902ed420311SAndre Oppermann /* 9039fd573c3SHans Petter Selasky * Check if we should limit by maximum payload 9049fd573c3SHans Petter Selasky * length: 905ed420311SAndre Oppermann */ 9069fd573c3SHans Petter Selasky if (if_hw_tsomax != 0) { 9079fd573c3SHans Petter Selasky /* compute maximum TSO length */ 908d76d4012SHans Petter Selasky max_len = (if_hw_tsomax - hdrlen - 909d76d4012SHans Petter Selasky max_linkhdr); 9109fd573c3SHans Petter Selasky if (max_len <= 0) { 9119fd573c3SHans Petter Selasky len = 0; 9123c7c188cSHans Petter Selasky } else if (len > max_len) { 913b3c0f300SAndre Oppermann sendalot = 1; 9143c7c188cSHans Petter Selasky len = max_len; 9159fd573c3SHans Petter Selasky } 9169fd573c3SHans Petter Selasky } 91774e10fb6SJohn Baldwin 918ed420311SAndre Oppermann /* 919ed420311SAndre Oppermann * Prevent the last segment from being 9209fd573c3SHans Petter Selasky * fractional unless the send sockbuf can be 9219fd573c3SHans Petter Selasky * emptied: 922ed420311SAndre Oppermann */ 9230c39d38dSGleb Smirnoff max_len = (tp->t_maxseg - optlen); 9243ac12506SJonathan T. Looney if (((uint32_t)off + (uint32_t)len) < 9253ac12506SJonathan T. Looney sbavail(&so->so_snd)) { 9263c7c188cSHans Petter Selasky moff = len % max_len; 9279fd573c3SHans Petter Selasky if (moff != 0) { 9289fd573c3SHans Petter Selasky len -= moff; 929b3c0f300SAndre Oppermann sendalot = 1; 930ed420311SAndre Oppermann } 9319fd573c3SHans Petter Selasky } 9329fd573c3SHans Petter Selasky 9339fd573c3SHans Petter Selasky /* 9349fd573c3SHans Petter Selasky * In case there are too many small fragments 9359fd573c3SHans Petter Selasky * don't use TSO: 9369fd573c3SHans Petter Selasky */ 9373c7c188cSHans Petter Selasky if (len <= max_len) { 9383c7c188cSHans Petter Selasky len = max_len; 9399fd573c3SHans Petter Selasky sendalot = 1; 9409fd573c3SHans Petter Selasky tso = 0; 9419fd573c3SHans Petter Selasky } 942ed420311SAndre Oppermann 943ed420311SAndre Oppermann /* 944ed420311SAndre Oppermann * Send the FIN in a separate segment 945ed420311SAndre Oppermann * after the bulk sending is done. 946ed420311SAndre Oppermann * We don't trust the TSO implementations 947ed420311SAndre Oppermann * to clear the FIN flag on all but the 948ed420311SAndre Oppermann * last segment. 949ed420311SAndre Oppermann */ 950ed420311SAndre Oppermann if (tp->t_flags & TF_NEEDFIN) 951ed420311SAndre Oppermann sendalot = 1; 952b3c0f300SAndre Oppermann } else { 95312a43d0dSMichael Tuexen if (optlen + ipoptlen >= tp->t_maxseg) { 95412a43d0dSMichael Tuexen /* 95512a43d0dSMichael Tuexen * Since we don't have enough space to put 95612a43d0dSMichael Tuexen * the IP header chain and the TCP header in 95712a43d0dSMichael Tuexen * one packet as required by RFC 7112, don't 95812a43d0dSMichael Tuexen * send it. Also ensure that at least one 95912a43d0dSMichael Tuexen * byte of the payload can be put into the 96012a43d0dSMichael Tuexen * TCP segment. 96112a43d0dSMichael Tuexen */ 96212a43d0dSMichael Tuexen SOCKBUF_UNLOCK(&so->so_snd); 96312a43d0dSMichael Tuexen error = EMSGSIZE; 96412a43d0dSMichael Tuexen sack_rxmit = 0; 96512a43d0dSMichael Tuexen goto out; 96612a43d0dSMichael Tuexen } 9670c39d38dSGleb Smirnoff len = tp->t_maxseg - optlen - ipoptlen; 968df8bae1dSRodney W. Grimes sendalot = 1; 969c560df6fSPatrick Kelsey if (dont_sendalot) 970c560df6fSPatrick Kelsey sendalot = 0; 971df8bae1dSRodney W. Grimes } 972ed420311SAndre Oppermann } else 973ed420311SAndre Oppermann tso = 0; 974ed420311SAndre Oppermann 975ed420311SAndre Oppermann KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 976ed420311SAndre Oppermann ("%s: len > IP_MAXPACKET", __func__)); 977df8bae1dSRodney W. Grimes 978a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 979a683a7ddSYoshinobu Inoue #ifdef INET6 980a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 981a683a7ddSYoshinobu Inoue #else 982df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 983a683a7ddSYoshinobu Inoue #endif 984f10e85d7SLuigi Rizzo panic("tcphdr too big"); 985a0292f23SGarrett Wollman /*#endif*/ 986df8bae1dSRodney W. Grimes 987df8bae1dSRodney W. Grimes /* 988c4982faeSBjoern A. Zeeb * This KASSERT is here to catch edge cases at a well defined place. 989c4982faeSBjoern A. Zeeb * Before, those had triggered (random) panic conditions further down. 990c4982faeSBjoern A. Zeeb */ 991c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 992c4982faeSBjoern A. Zeeb 993c4982faeSBjoern A. Zeeb /* 994df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 995df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 996df8bae1dSRodney W. Grimes * the template for sends on this connection. 997df8bae1dSRodney W. Grimes */ 998df8bae1dSRodney W. Grimes if (len) { 9994e023759SAndre Oppermann struct mbuf *mb; 1000581a046aSRandall Stewart struct sockbuf *msb; 10014e023759SAndre Oppermann u_int moff; 10024e023759SAndre Oppermann 1003adc56f5aSEdward Tomasz Napierala if ((tp->t_flags & TF_FORCEDATA) && len == 1) { 100478b50714SRobert Watson TCPSTAT_INC(tcps_sndprobe); 1005adc56f5aSEdward Tomasz Napierala #ifdef STATS 1006adc56f5aSEdward Tomasz Napierala if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1007adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, 1008adc56f5aSEdward Tomasz Napierala VOI_TCP_RETXPB, len); 1009adc56f5aSEdward Tomasz Napierala else 1010adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u64(tp->t_stats, 1011adc56f5aSEdward Tomasz Napierala VOI_TCP_TXPB, len); 1012adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 1013adc56f5aSEdward Tomasz Napierala } else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) { 1014f5d34df5SGeorge V. Neville-Neil tp->t_sndrexmitpack++; 101578b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitpack); 101678b50714SRobert Watson TCPSTAT_ADD(tcps_sndrexmitbyte, len); 1017adc56f5aSEdward Tomasz Napierala #ifdef STATS 1018adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 1019adc56f5aSEdward Tomasz Napierala len); 1020adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 1021df8bae1dSRodney W. Grimes } else { 102278b50714SRobert Watson TCPSTAT_INC(tcps_sndpack); 102378b50714SRobert Watson TCPSTAT_ADD(tcps_sndbyte, len); 1024adc56f5aSEdward Tomasz Napierala #ifdef STATS 1025adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 1026adc56f5aSEdward Tomasz Napierala len); 1027adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 1028df8bae1dSRodney W. Grimes } 102939f6074eSGleb Smirnoff #ifdef INET6 103039f6074eSGleb Smirnoff if (MHLEN < hdrlen + max_linkhdr) 103139f6074eSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 103239f6074eSGleb Smirnoff else 103339f6074eSGleb Smirnoff #endif 103439f6074eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 103539f6074eSGleb Smirnoff 1036df8bae1dSRodney W. Grimes if (m == NULL) { 1037cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1038df8bae1dSRodney W. Grimes error = ENOBUFS; 10390e2bc05cSGleb Smirnoff sack_rxmit = 0; 1040df8bae1dSRodney W. Grimes goto out; 1041df8bae1dSRodney W. Grimes } 104239f6074eSGleb Smirnoff 1043df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1044df8bae1dSRodney W. Grimes m->m_len = hdrlen; 10454e023759SAndre Oppermann 10464e023759SAndre Oppermann /* 10474e023759SAndre Oppermann * Start the m_copy functions from the closest mbuf 10484e023759SAndre Oppermann * to the offset in the socket buffer chain. 10494e023759SAndre Oppermann */ 1050581a046aSRandall Stewart mb = sbsndptr_noadv(&so->so_snd, off, &moff); 1051b2e60773SJohn Baldwin if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 10523ac12506SJonathan T. Looney m_copydata(mb, moff, len, 1053df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 1054581a046aSRandall Stewart if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1055581a046aSRandall Stewart sbsndptr_adv(&so->so_snd, mb, len); 1056df8bae1dSRodney W. Grimes m->m_len += len; 1057df8bae1dSRodney W. Grimes } else { 1058581a046aSRandall Stewart if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1059581a046aSRandall Stewart msb = NULL; 1060581a046aSRandall Stewart else 1061581a046aSRandall Stewart msb = &so->so_snd; 1062581a046aSRandall Stewart m->m_next = tcp_m_copym(mb, moff, 1063581a046aSRandall Stewart &len, if_hw_tsomaxsegcount, 1064b2e60773SJohn Baldwin if_hw_tsomaxsegsize, msb, hw_tls); 1065581a046aSRandall Stewart if (len <= (tp->t_maxseg - optlen)) { 1066581a046aSRandall Stewart /* 1067581a046aSRandall Stewart * Must have ran out of mbufs for the copy 1068581a046aSRandall Stewart * shorten it to no longer need tso. Lets 1069581a046aSRandall Stewart * not put on sendalot since we are low on 1070581a046aSRandall Stewart * mbufs. 1071581a046aSRandall Stewart */ 1072581a046aSRandall Stewart tso = 0; 1073581a046aSRandall Stewart } 10744e023759SAndre Oppermann if (m->m_next == NULL) { 1075cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1076d7f570e6SGarrett Wollman (void) m_free(m); 107751823c3aSGarrett Wollman error = ENOBUFS; 10780e2bc05cSGleb Smirnoff sack_rxmit = 0; 107951823c3aSGarrett Wollman goto out; 108051823c3aSGarrett Wollman } 1081df8bae1dSRodney W. Grimes } 1082472ea5beSColin Percival 1083df8bae1dSRodney W. Grimes /* 1084df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 1085df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 1086df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 1087df8bae1dSRodney W. Grimes * a PUSH comes in.) 1088df8bae1dSRodney W. Grimes */ 10893ac12506SJonathan T. Looney if (((uint32_t)off + (uint32_t)len == sbused(&so->so_snd)) && 10903ac12506SJonathan T. Looney !(flags & TH_SYN)) 1091df8bae1dSRodney W. Grimes flags |= TH_PUSH; 1092cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1093df8bae1dSRodney W. Grimes } else { 1094cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1095df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 109678b50714SRobert Watson TCPSTAT_INC(tcps_sndacks); 1097df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 109878b50714SRobert Watson TCPSTAT_INC(tcps_sndctrl); 1099df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 110078b50714SRobert Watson TCPSTAT_INC(tcps_sndurg); 1101df8bae1dSRodney W. Grimes else 110278b50714SRobert Watson TCPSTAT_INC(tcps_sndwinup); 1103df8bae1dSRodney W. Grimes 1104aa8bd99dSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 1105df8bae1dSRodney W. Grimes if (m == NULL) { 1106df8bae1dSRodney W. Grimes error = ENOBUFS; 11070e2bc05cSGleb Smirnoff sack_rxmit = 0; 1108df8bae1dSRodney W. Grimes goto out; 1109df8bae1dSRodney W. Grimes } 1110fb59c426SYoshinobu Inoue #ifdef INET6 1111fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 1112fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 1113ed6a66caSRobert Watson M_ALIGN(m, hdrlen); 1114fb59c426SYoshinobu Inoue } else 1115fb59c426SYoshinobu Inoue #endif 1116df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1117df8bae1dSRodney W. Grimes m->m_len = hdrlen; 1118df8bae1dSRodney W. Grimes } 1119cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); 1120df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 1121c488362eSRobert Watson #ifdef MAC 112230d239bcSRobert Watson mac_inpcb_create_mbuf(tp->t_inpcb, m); 1123c488362eSRobert Watson #endif 1124fb59c426SYoshinobu Inoue #ifdef INET6 1125fb59c426SYoshinobu Inoue if (isipv6) { 1126fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 1127fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 112879909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip6, th); 1129fb59c426SYoshinobu Inoue } else 1130fb59c426SYoshinobu Inoue #endif /* INET6 */ 1131fb59c426SYoshinobu Inoue { 1132fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 1133151ba793SAlexander Kabaev #ifdef TCPDEBUG 1134fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 1135151ba793SAlexander Kabaev #endif 1136fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 113779909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip, th); 1138fb59c426SYoshinobu Inoue } 1139df8bae1dSRodney W. Grimes 1140df8bae1dSRodney W. Grimes /* 1141df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 1142df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 1143df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 1144df8bae1dSRodney W. Grimes */ 1145df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 1146df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 1147df8bae1dSRodney W. Grimes tp->snd_nxt--; 1148df8bae1dSRodney W. Grimes /* 1149f2512ba1SRui Paulo * If we are starting a connection, send ECN setup 1150f2512ba1SRui Paulo * SYN packet. If we are on a retransmit, we may 1151f2512ba1SRui Paulo * resend those bits a number of times as per 1152f2512ba1SRui Paulo * RFC 3168. 1153f2512ba1SRui Paulo */ 1154883054b4SDon Lewis if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn == 1) { 1155f2512ba1SRui Paulo if (tp->t_rxtshift >= 1) { 1156603724d3SBjoern A. Zeeb if (tp->t_rxtshift <= V_tcp_ecn_maxretries) 1157f2512ba1SRui Paulo flags |= TH_ECE|TH_CWR; 1158f2512ba1SRui Paulo } else 1159f2512ba1SRui Paulo flags |= TH_ECE|TH_CWR; 1160f2512ba1SRui Paulo } 11616e16d877SRichard Scheffenegger /* Handle parallel SYN for ECN */ 11626e16d877SRichard Scheffenegger if ((tp->t_state == TCPS_SYN_RECEIVED) && 11636e16d877SRichard Scheffenegger (tp->t_flags2 & TF2_ECN_SND_ECE)) { 11646e16d877SRichard Scheffenegger flags |= TH_ECE; 11656e16d877SRichard Scheffenegger tp->t_flags2 &= ~TF2_ECN_SND_ECE; 11666e16d877SRichard Scheffenegger } 1167f2512ba1SRui Paulo 1168f2512ba1SRui Paulo if (tp->t_state == TCPS_ESTABLISHED && 11693cf38784SMichael Tuexen (tp->t_flags2 & TF2_ECN_PERMIT)) { 1170f2512ba1SRui Paulo /* 1171f2512ba1SRui Paulo * If the peer has ECN, mark data packets with 1172f2512ba1SRui Paulo * ECN capable transmission (ECT). 1173f2512ba1SRui Paulo * Ignore pure ack packets, retransmissions and window probes. 1174f2512ba1SRui Paulo */ 1175f2512ba1SRui Paulo if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) && 117647e2c17cSMichael Tuexen (sack_rxmit == 0) && 1177af2fb894SRichard Scheffenegger !((tp->t_flags & TF_FORCEDATA) && len == 1 && 1178af2fb894SRichard Scheffenegger SEQ_LT(tp->snd_una, tp->snd_max))) { 1179f2512ba1SRui Paulo #ifdef INET6 1180f2512ba1SRui Paulo if (isipv6) 1181f2512ba1SRui Paulo ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 1182f2512ba1SRui Paulo else 1183f2512ba1SRui Paulo #endif 1184f2512ba1SRui Paulo ip->ip_tos |= IPTOS_ECN_ECT0; 118578b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1186f2512ba1SRui Paulo /* 1187f2512ba1SRui Paulo * Reply with proper ECN notifications. 1188af2fb894SRichard Scheffenegger * Only set CWR on new data segments. 1189f2512ba1SRui Paulo */ 11903cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_SND_CWR) { 1191f2512ba1SRui Paulo flags |= TH_CWR; 11923cf38784SMichael Tuexen tp->t_flags2 &= ~TF2_ECN_SND_CWR; 1193f2512ba1SRui Paulo } 1194af2fb894SRichard Scheffenegger } 11953cf38784SMichael Tuexen if (tp->t_flags2 & TF2_ECN_SND_ECE) 1196f2512ba1SRui Paulo flags |= TH_ECE; 1197f2512ba1SRui Paulo } 1198f2512ba1SRui Paulo 1199f2512ba1SRui Paulo /* 1200df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 1201df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 1202df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 1203df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 1204df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 1205df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 1206df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 1207df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 1208df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 1209df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 1210df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 1211df8bae1dSRodney W. Grimes */ 1212a55db2b6SPaul Saab if (sack_rxmit == 0) { 1213b8152ba7SAndre Oppermann if (len || (flags & (TH_SYN|TH_FIN)) || 1214b8152ba7SAndre Oppermann tcp_timer_active(tp, TT_PERSIST)) 1215fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 1216df8bae1dSRodney W. Grimes else 1217fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 1218a55db2b6SPaul Saab } else { 12196d90faf3SPaul Saab th->th_seq = htonl(p->rxmit); 12206d90faf3SPaul Saab p->rxmit += len; 12210077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += len; 12226d90faf3SPaul Saab } 1223fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 1224df8bae1dSRodney W. Grimes if (optlen) { 1225fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 1226fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 1227df8bae1dSRodney W. Grimes } 1228fb59c426SYoshinobu Inoue th->th_flags = flags; 1229df8bae1dSRodney W. Grimes /* 1230df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 1231df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 123279410718SMichael Tuexen * If a RST segment is sent, advertise a window of zero. 1233df8bae1dSRodney W. Grimes */ 123479410718SMichael Tuexen if (flags & TH_RST) { 123579410718SMichael Tuexen recwin = 0; 123679410718SMichael Tuexen } else { 12373ac12506SJonathan T. Looney if (recwin < (so->so_rcv.sb_hiwat / 4) && 12383ac12506SJonathan T. Looney recwin < tp->t_maxseg) 1239201d185bSAndre Oppermann recwin = 0; 1240f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 12413ac12506SJonathan T. Looney recwin < (tp->rcv_adv - tp->rcv_nxt)) 12423ac12506SJonathan T. Looney recwin = (tp->rcv_adv - tp->rcv_nxt); 124379410718SMichael Tuexen } 1244104ebb2aSAndre Oppermann /* 1245104ebb2aSAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1246104ebb2aSAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> 1247104ebb2aSAndre Oppermann * case is handled in syncache. 1248104ebb2aSAndre Oppermann */ 1249104ebb2aSAndre Oppermann if (flags & TH_SYN) 1250104ebb2aSAndre Oppermann th->th_win = htons((u_short) 1251104ebb2aSAndre Oppermann (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 12529028b6e0SRichard Scheffenegger else { 12539028b6e0SRichard Scheffenegger /* Avoid shrinking window with window scaling. */ 12549028b6e0SRichard Scheffenegger recwin = roundup2(recwin, 1 << tp->rcv_scale); 1255104ebb2aSAndre Oppermann th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 12569028b6e0SRichard Scheffenegger } 1257262c1c1aSMatthew Dillon 1258262c1c1aSMatthew Dillon /* 1259262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 1260262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 1261262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 1262262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 1263b2722702SRui Paulo * to read more data than can be buffered prior to transmitting on 1264262c1c1aSMatthew Dillon * the connection. 1265262c1c1aSMatthew Dillon */ 1266f5d34df5SGeorge V. Neville-Neil if (th->th_win == 0) { 1267f5d34df5SGeorge V. Neville-Neil tp->t_sndzerowin++; 1268262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 1269f5d34df5SGeorge V. Neville-Neil } else 1270262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 1271df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 1272fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 1273fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 1274df8bae1dSRodney W. Grimes } else 1275df8bae1dSRodney W. Grimes /* 1276df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 1277df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 1278df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 1279df8bae1dSRodney W. Grimes * number wraparound. 1280df8bae1dSRodney W. Grimes */ 1281df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 1282df8bae1dSRodney W. Grimes 1283df8bae1dSRodney W. Grimes /* 1284df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 1285df8bae1dSRodney W. Grimes * checksum extended header and data. 1286df8bae1dSRodney W. Grimes */ 1287fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 128845747ba5SBjoern A. Zeeb m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 1289fcf59617SAndrey V. Elsukov 1290fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1291fcf59617SAndrey V. Elsukov if (to.to_flags & TOF_SIGNATURE) { 1292fcf59617SAndrey V. Elsukov /* 1293fcf59617SAndrey V. Elsukov * Calculate MD5 signature and put it into the place 1294fcf59617SAndrey V. Elsukov * determined before. 1295fcf59617SAndrey V. Elsukov * NOTE: since TCP options buffer doesn't point into 1296fcf59617SAndrey V. Elsukov * mbuf's data, calculate offset and use it. 1297fcf59617SAndrey V. Elsukov */ 12982aad6240SAndrey V. Elsukov if (!TCPMD5_ENABLED() || (error = TCPMD5_OUTPUT(m, th, 12992aad6240SAndrey V. Elsukov (u_char *)(th + 1) + (to.to_signature - opt))) != 0) { 1300fcf59617SAndrey V. Elsukov /* 1301fcf59617SAndrey V. Elsukov * Do not send segment if the calculation of MD5 1302fcf59617SAndrey V. Elsukov * digest has failed. 1303fcf59617SAndrey V. Elsukov */ 13042aad6240SAndrey V. Elsukov m_freem(m); 1305fcf59617SAndrey V. Elsukov goto out; 1306fcf59617SAndrey V. Elsukov } 1307fcf59617SAndrey V. Elsukov } 1308fcf59617SAndrey V. Elsukov #endif 1309fb59c426SYoshinobu Inoue #ifdef INET6 131045747ba5SBjoern A. Zeeb if (isipv6) { 1311fb59c426SYoshinobu Inoue /* 13123df96ee6SCy Schubert * There is no need to fill in ip6_plen right now. 13133df96ee6SCy Schubert * It will be filled later by ip6_output. 1314fb59c426SYoshinobu Inoue */ 1315356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 131645747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) + 131745747ba5SBjoern A. Zeeb optlen + len, IPPROTO_TCP, 0); 131845747ba5SBjoern A. Zeeb } 131945747ba5SBjoern A. Zeeb #endif 132045747ba5SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1321fb59c426SYoshinobu Inoue else 132245747ba5SBjoern A. Zeeb #endif 132345747ba5SBjoern A. Zeeb #ifdef INET 1324fb59c426SYoshinobu Inoue { 1325356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP; 132679909384SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 132779909384SJonathan Lemon htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen)); 1328fb59c426SYoshinobu Inoue 1329db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 1330db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 13316e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 1332fb59c426SYoshinobu Inoue } 133345747ba5SBjoern A. Zeeb #endif 1334df8bae1dSRodney W. Grimes 1335df8bae1dSRodney W. Grimes /* 1336b3c0f300SAndre Oppermann * Enable TSO and specify the size of the segments. 1337b3c0f300SAndre Oppermann * The TCP pseudo header checksum is always provided. 1338b3c0f300SAndre Oppermann */ 1339b3c0f300SAndre Oppermann if (tso) { 13400c39d38dSGleb Smirnoff KASSERT(len > tp->t_maxseg - optlen, 1341153e5b57SAndre Oppermann ("%s: len <= tso_segsz", __func__)); 13423579cf4cSKenneth D. Merry m->m_pkthdr.csum_flags |= CSUM_TSO; 13430c39d38dSGleb Smirnoff m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen; 1344b3c0f300SAndre Oppermann } 1345b3c0f300SAndre Oppermann 134605fb056cSMichael Tuexen KASSERT(len + hdrlen == m_length(m, NULL), 134705fb056cSMichael Tuexen ("%s: mbuf chain shorter than expected: %d + %u != %u", 134805fb056cSMichael Tuexen __func__, len, hdrlen, m_length(m, NULL))); 1349ed420311SAndre Oppermann 1350bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 135139bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */ 135239bc9de5SLawrence Stewart hhook_run_tcp_est_out(tp, th, &to, len, tso); 1353bd79708dSJonathan T. Looney #endif 135439bc9de5SLawrence Stewart 1355610ee2f9SDavid Greenman #ifdef TCPDEBUG 1356df8bae1dSRodney W. Grimes /* 1357df8bae1dSRodney W. Grimes * Trace. 1358df8bae1dSRodney W. Grimes */ 135991f467d5SHartmut Brandt if (so->so_options & SO_DEBUG) { 1360f3e0b7efSBruce M Simpson u_short save = 0; 13615214cb3fSBruce M Simpson #ifdef INET6 13625214cb3fSBruce M Simpson if (!isipv6) 13635214cb3fSBruce M Simpson #endif 13645214cb3fSBruce M Simpson { 13655214cb3fSBruce M Simpson save = ipov->ih_len; 136691f467d5SHartmut Brandt ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */); 13675214cb3fSBruce M Simpson } 1368fb59c426SYoshinobu Inoue tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 13695214cb3fSBruce M Simpson #ifdef INET6 13705214cb3fSBruce M Simpson if (!isipv6) 13715214cb3fSBruce M Simpson #endif 137291f467d5SHartmut Brandt ipov->ih_len = save; 137391f467d5SHartmut Brandt } 1374b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 13752b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__output, tp, th, m); 1376df8bae1dSRodney W. Grimes 1377dcaffbd6SJonathan T. Looney /* We're getting ready to send; log now. */ 1378dcaffbd6SJonathan T. Looney TCP_LOG_EVENT(tp, th, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, ERRNO_UNK, 1379dcaffbd6SJonathan T. Looney len, NULL, false); 1380dcaffbd6SJonathan T. Looney 1381df8bae1dSRodney W. Grimes /* 1382df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 1383df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 1384df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 1385df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 1386df8bae1dSRodney W. Grimes */ 1387fb59c426SYoshinobu Inoue /* 138843630e62SHiren Panchasara * m->m_pkthdr.len should have been set before checksum calculation, 1389fb59c426SYoshinobu Inoue * because in6_cksum() need it. 1390fb59c426SYoshinobu Inoue */ 1391fb59c426SYoshinobu Inoue #ifdef INET6 1392fb59c426SYoshinobu Inoue if (isipv6) { 1393fb59c426SYoshinobu Inoue /* 1394fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 1395fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 1396fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 1397fb59c426SYoshinobu Inoue * Neighbor Discovery. 1398fb59c426SYoshinobu Inoue */ 139997d8d152SAndre Oppermann ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1400fb59c426SYoshinobu Inoue 140157f60867SMark Johnston /* 140257f60867SMark Johnston * Set the packet size here for the benefit of DTrace probes. 140357f60867SMark Johnston * ip6_output() will set it properly; it's supposed to include 140457f60867SMark Johnston * the option header lengths as well. 140557f60867SMark Johnston */ 140657f60867SMark Johnston ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 140757f60867SMark Johnston 14080c39d38dSGleb Smirnoff if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 14090f3e3bc5SSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 14100f3e3bc5SSean Bruno else 14110f3e3bc5SSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 14120f3e3bc5SSean Bruno 141357f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1414d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 141557f60867SMark Johnston 141657f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip6, tp, th); 141757f60867SMark Johnston 141886a996e6SHiren Panchasara #ifdef TCPPCAP 141986a996e6SHiren Panchasara /* Save packet, if requested. */ 142086a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_outpkts)); 142186a996e6SHiren Panchasara #endif 142286a996e6SHiren Panchasara 1423fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 14244a5c6c6aSMike Karels error = ip6_output(m, tp->t_inpcb->in6p_outputopts, 14254a5c6c6aSMike Karels &tp->t_inpcb->inp_route6, 1426df0633a1SGleb Smirnoff ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 1427df0633a1SGleb Smirnoff NULL, NULL, tp->t_inpcb); 1428df0633a1SGleb Smirnoff 1429983066f0SAlexander V. Chernikov if (error == EMSGSIZE && tp->t_inpcb->inp_route6.ro_nh != NULL) 1430983066f0SAlexander V. Chernikov mtu = tp->t_inpcb->inp_route6.ro_nh->nh_mtu; 1431b287c6c7SBjoern A. Zeeb } 1432fb59c426SYoshinobu Inoue #endif /* INET6 */ 1433b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1434b287c6c7SBjoern A. Zeeb else 1435b287c6c7SBjoern A. Zeeb #endif 1436b287c6c7SBjoern A. Zeeb #ifdef INET 1437df8bae1dSRodney W. Grimes { 14388f134647SGleb Smirnoff ip->ip_len = htons(m->m_pkthdr.len); 1439686cdd19SJun-ichiro itojun Hagino #ifdef INET6 14405cd54324SBjoern A. Zeeb if (tp->t_inpcb->inp_vflag & INP_IPV6PROTO) 144197d8d152SAndre Oppermann ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL); 1442686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 1443f138387aSGarrett Wollman /* 144497d8d152SAndre Oppermann * If we do path MTU discovery, then we set DF on every packet. 144597d8d152SAndre Oppermann * This might not be the best thing to do according to RFC3390 144697d8d152SAndre Oppermann * Section 2. However the tcp hostcache migitates the problem 144797d8d152SAndre Oppermann * so it affects only the first tcp connection with a host. 1448e4e92660SAndre Oppermann * 1449e4e92660SAndre Oppermann * NB: Don't set DF on small MTU/MSS to have a safe fallback. 1450f138387aSGarrett Wollman */ 14510c39d38dSGleb Smirnoff if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 14528f134647SGleb Smirnoff ip->ip_off |= htons(IP_DF); 1453f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 1454f6f6703fSSean Bruno } else { 1455f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 1456f6f6703fSSean Bruno } 145797d8d152SAndre Oppermann 145857f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1459d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 146057f60867SMark Johnston 146157f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip, tp, th); 146257f60867SMark Johnston 146386a996e6SHiren Panchasara #ifdef TCPPCAP 146486a996e6SHiren Panchasara /* Save packet, if requested. */ 146586a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_outpkts)); 146686a996e6SHiren Panchasara #endif 146786a996e6SHiren Panchasara 146884cc0778SGeorge V. Neville-Neil error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 1469b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 1470b5d47ff5SJohn-Mark Gurney tp->t_inpcb); 1471df0633a1SGleb Smirnoff 1472983066f0SAlexander V. Chernikov if (error == EMSGSIZE && tp->t_inpcb->inp_route.ro_nh != NULL) 1473983066f0SAlexander V. Chernikov mtu = tp->t_inpcb->inp_route.ro_nh->nh_mtu; 1474df8bae1dSRodney W. Grimes } 1475b287c6c7SBjoern A. Zeeb #endif /* INET */ 14760e2bc05cSGleb Smirnoff 14770e2bc05cSGleb Smirnoff out: 14780e2bc05cSGleb Smirnoff /* 14790e2bc05cSGleb Smirnoff * In transmit state, time the transmission and arrange for 14800e2bc05cSGleb Smirnoff * the retransmit. In persist state, just set snd_max. 14810e2bc05cSGleb Smirnoff */ 14820e2bc05cSGleb Smirnoff if ((tp->t_flags & TF_FORCEDATA) == 0 || 14830e2bc05cSGleb Smirnoff !tcp_timer_active(tp, TT_PERSIST)) { 14840e2bc05cSGleb Smirnoff tcp_seq startseq = tp->snd_nxt; 14850e2bc05cSGleb Smirnoff 14860e2bc05cSGleb Smirnoff /* 14870e2bc05cSGleb Smirnoff * Advance snd_nxt over sequence space of this segment. 14880e2bc05cSGleb Smirnoff */ 14890e2bc05cSGleb Smirnoff if (flags & (TH_SYN|TH_FIN)) { 14900e2bc05cSGleb Smirnoff if (flags & TH_SYN) 14910e2bc05cSGleb Smirnoff tp->snd_nxt++; 14920e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 14930e2bc05cSGleb Smirnoff tp->snd_nxt++; 14940e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 14950e2bc05cSGleb Smirnoff } 14960e2bc05cSGleb Smirnoff } 14970e2bc05cSGleb Smirnoff if (sack_rxmit) 14980e2bc05cSGleb Smirnoff goto timer; 14990e2bc05cSGleb Smirnoff tp->snd_nxt += len; 15000e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 15010e2bc05cSGleb Smirnoff tp->snd_max = tp->snd_nxt; 15020e2bc05cSGleb Smirnoff /* 15030e2bc05cSGleb Smirnoff * Time this transmission if not a retransmission and 15040e2bc05cSGleb Smirnoff * not currently timing anything. 15050e2bc05cSGleb Smirnoff */ 1506*9dc7d8a2SRichard Scheffenegger tp->t_sndtime = ticks; 15070e2bc05cSGleb Smirnoff if (tp->t_rtttime == 0) { 15080e2bc05cSGleb Smirnoff tp->t_rtttime = ticks; 15090e2bc05cSGleb Smirnoff tp->t_rtseq = startseq; 15100e2bc05cSGleb Smirnoff TCPSTAT_INC(tcps_segstimed); 15110e2bc05cSGleb Smirnoff } 1512adc56f5aSEdward Tomasz Napierala #ifdef STATS 1513adc56f5aSEdward Tomasz Napierala if (!(tp->t_flags & TF_GPUTINPROG) && len) { 1514adc56f5aSEdward Tomasz Napierala tp->t_flags |= TF_GPUTINPROG; 1515adc56f5aSEdward Tomasz Napierala tp->gput_seq = startseq; 1516adc56f5aSEdward Tomasz Napierala tp->gput_ack = startseq + 1517adc56f5aSEdward Tomasz Napierala ulmin(sbavail(&so->so_snd) - off, sendwin); 1518adc56f5aSEdward Tomasz Napierala tp->gput_ts = tcp_ts_getticks(); 1519adc56f5aSEdward Tomasz Napierala } 1520adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 15210e2bc05cSGleb Smirnoff } 15220e2bc05cSGleb Smirnoff 15230e2bc05cSGleb Smirnoff /* 15240e2bc05cSGleb Smirnoff * Set retransmit timer if not currently set, 15250e2bc05cSGleb Smirnoff * and not doing a pure ack or a keep-alive probe. 15260e2bc05cSGleb Smirnoff * Initial value for retransmit timer is smoothed 15270e2bc05cSGleb Smirnoff * round-trip time + 2 * round-trip time variance. 15280e2bc05cSGleb Smirnoff * Initialize shift counter which is used for backoff 15290e2bc05cSGleb Smirnoff * of retransmit time. 15300e2bc05cSGleb Smirnoff */ 15310e2bc05cSGleb Smirnoff timer: 15320e2bc05cSGleb Smirnoff if (!tcp_timer_active(tp, TT_REXMT) && 15330e2bc05cSGleb Smirnoff ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 15340e2bc05cSGleb Smirnoff (tp->snd_nxt != tp->snd_una))) { 15350e2bc05cSGleb Smirnoff if (tcp_timer_active(tp, TT_PERSIST)) { 15360e2bc05cSGleb Smirnoff tcp_timer_activate(tp, TT_PERSIST, 0); 15370e2bc05cSGleb Smirnoff tp->t_rxtshift = 0; 15380e2bc05cSGleb Smirnoff } 15390e2bc05cSGleb Smirnoff tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 1540f8568079SHiren Panchasara } else if (len == 0 && sbavail(&so->so_snd) && 1541f8568079SHiren Panchasara !tcp_timer_active(tp, TT_REXMT) && 1542f8568079SHiren Panchasara !tcp_timer_active(tp, TT_PERSIST)) { 1543f8568079SHiren Panchasara /* 1544f8568079SHiren Panchasara * Avoid a situation where we do not set persist timer 1545f8568079SHiren Panchasara * after a zero window condition. For example: 1546f8568079SHiren Panchasara * 1) A -> B: packet with enough data to fill the window 1547f8568079SHiren Panchasara * 2) B -> A: ACK for #1 + new data (0 window 1548f8568079SHiren Panchasara * advertisement) 1549f8568079SHiren Panchasara * 3) A -> B: ACK for #2, 0 len packet 1550f8568079SHiren Panchasara * 1551f8568079SHiren Panchasara * In this case, A will not activate the persist timer, 1552f8568079SHiren Panchasara * because it chose to send a packet. Unless tcp_output 1553f8568079SHiren Panchasara * is called for some other reason (delayed ack timer, 1554f8568079SHiren Panchasara * another input packet from B, socket syscall), A will 1555f8568079SHiren Panchasara * not send zero window probes. 1556f8568079SHiren Panchasara * 1557f8568079SHiren Panchasara * So, if you send a 0-length packet, but there is data 1558f8568079SHiren Panchasara * in the socket buffer, and neither the rexmt or 1559f8568079SHiren Panchasara * persist timer is already set, then activate the 1560f8568079SHiren Panchasara * persist timer. 1561f8568079SHiren Panchasara */ 1562f8568079SHiren Panchasara tp->t_rxtshift = 0; 1563f8568079SHiren Panchasara tcp_setpersist(tp); 15640e2bc05cSGleb Smirnoff } 15650e2bc05cSGleb Smirnoff } else { 15660e2bc05cSGleb Smirnoff /* 15670e2bc05cSGleb Smirnoff * Persist case, update snd_max but since we are in 15680e2bc05cSGleb Smirnoff * persist mode (no window) we do not update snd_nxt. 15690e2bc05cSGleb Smirnoff */ 15700e2bc05cSGleb Smirnoff int xlen = len; 15710e2bc05cSGleb Smirnoff if (flags & TH_SYN) 15720e2bc05cSGleb Smirnoff ++xlen; 15730e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 15740e2bc05cSGleb Smirnoff ++xlen; 15750e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 15760e2bc05cSGleb Smirnoff } 15770e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 157815c82571SJonathan T. Looney tp->snd_max = tp->snd_nxt + xlen; 15790e2bc05cSGleb Smirnoff } 1580e5926fd3SRandall Stewart if ((error == 0) && 1581e5926fd3SRandall Stewart (TCPS_HAVEESTABLISHED(tp->t_state) && 1582e5926fd3SRandall Stewart (tp->t_flags & TF_SACK_PERMIT) && 1583e5926fd3SRandall Stewart tp->rcv_numsacks > 0)) { 1584e5926fd3SRandall Stewart /* Clean up any DSACK's sent */ 1585e5926fd3SRandall Stewart tcp_clean_dsack_blocks(tp); 1586e5926fd3SRandall Stewart } 1587df8bae1dSRodney W. Grimes if (error) { 15882529f56eSJonathan T. Looney /* Record the error. */ 15892529f56eSJonathan T. Looney TCP_LOG_EVENT(tp, NULL, &so->so_rcv, &so->so_snd, TCP_LOG_OUT, 15902529f56eSJonathan T. Looney error, 0, NULL, false); 15917734ea06SArchie Cobbs 15927734ea06SArchie Cobbs /* 15937734ea06SArchie Cobbs * We know that the packet was lost, so back out the 15947734ea06SArchie Cobbs * sequence number advance, if any. 15952c30ec0aSAndre Oppermann * 15962c30ec0aSAndre Oppermann * If the error is EPERM the packet got blocked by the 15972c30ec0aSAndre Oppermann * local firewall. Normally we should terminate the 15982c30ec0aSAndre Oppermann * connection but the blocking may have been spurious 15992c30ec0aSAndre Oppermann * due to a firewall reconfiguration cycle. So we treat 16002c30ec0aSAndre Oppermann * it like a packet loss and let the retransmit timer and 16012c30ec0aSAndre Oppermann * timeouts do their work over time. 16022c30ec0aSAndre Oppermann * XXX: It is a POLA question whether calling tcp_drop right 16032c30ec0aSAndre Oppermann * away would be the really correct behavior instead. 16047734ea06SArchie Cobbs */ 160572757d9aSGleb Smirnoff if (((tp->t_flags & TF_FORCEDATA) == 0 || 1606b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) && 160772757d9aSGleb Smirnoff ((flags & TH_SYN) == 0) && 160872757d9aSGleb Smirnoff (error != EPERM)) { 16090077b016SPaul Saab if (sack_rxmit) { 16105d3b1b75SJayanth Vijayaraghavan p->rxmit -= len; 16110077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= len; 161272757d9aSGleb Smirnoff KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 16130077b016SPaul Saab ("sackhint bytes rtx >= 0")); 16140077b016SPaul Saab } else 16157734ea06SArchie Cobbs tp->snd_nxt -= len; 16167734ea06SArchie Cobbs } 1617cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */ 161872757d9aSGleb Smirnoff switch (error) { 1619fcf59617SAndrey V. Elsukov case EACCES: 162072757d9aSGleb Smirnoff case EPERM: 162172757d9aSGleb Smirnoff tp->t_softerror = error; 162272757d9aSGleb Smirnoff return (error); 162372757d9aSGleb Smirnoff case ENOBUFS: 1624425b7639SSepherosa Ziehau TCP_XMIT_TIMER_ASSERT(tp, len, flags); 16251600372bSAndre Oppermann tp->snd_cwnd = tp->t_maxseg; 1626df8bae1dSRodney W. Grimes return (0); 162772757d9aSGleb Smirnoff case EMSGSIZE: 16283d1f141bSGarrett Wollman /* 1629b3c0f300SAndre Oppermann * For some reason the interface we used initially 1630b3c0f300SAndre Oppermann * to send segments changed to another or lowered 1631b3c0f300SAndre Oppermann * its MTU. 1632b3c0f300SAndre Oppermann * If TSO was active we either got an interface 1633b3c0f300SAndre Oppermann * without TSO capabilits or TSO was turned off. 1634df0633a1SGleb Smirnoff * If we obtained mtu from ip_output() then update 1635df0633a1SGleb Smirnoff * it and try again. 16363d1f141bSGarrett Wollman */ 1637b3c0f300SAndre Oppermann if (tso) 1638b3c0f300SAndre Oppermann tp->t_flags &= ~TF_TSO; 1639df0633a1SGleb Smirnoff if (mtu != 0) { 1640df0633a1SGleb Smirnoff tcp_mss_update(tp, -1, mtu, NULL, NULL); 1641df0633a1SGleb Smirnoff goto again; 1642df0633a1SGleb Smirnoff } 1643df0633a1SGleb Smirnoff return (error); 16448bec3467SGleb Smirnoff case EHOSTDOWN: 164572757d9aSGleb Smirnoff case EHOSTUNREACH: 164672757d9aSGleb Smirnoff case ENETDOWN: 16478bec3467SGleb Smirnoff case ENETUNREACH: 164872757d9aSGleb Smirnoff if (TCPS_HAVERCVDSYN(tp->t_state)) { 1649df8bae1dSRodney W. Grimes tp->t_softerror = error; 1650df8bae1dSRodney W. Grimes return (0); 1651df8bae1dSRodney W. Grimes } 165272757d9aSGleb Smirnoff /* FALLTHROUGH */ 165372757d9aSGleb Smirnoff default: 1654df8bae1dSRodney W. Grimes return (error); 1655df8bae1dSRodney W. Grimes } 165672757d9aSGleb Smirnoff } 165778b50714SRobert Watson TCPSTAT_INC(tcps_sndtotal); 1658df8bae1dSRodney W. Grimes 1659df8bae1dSRodney W. Grimes /* 1660df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 1661df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 1662df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 1663df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 1664df8bae1dSRodney W. Grimes */ 16653ac12506SJonathan T. Looney if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1666201d185bSAndre Oppermann tp->rcv_adv = tp->rcv_nxt + recwin; 1667df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 16683bfd6421SJonathan Lemon tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 1669b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_DELACK)) 1670b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 0); 1671d912c694SMatthew Dillon #if 0 1672d912c694SMatthew Dillon /* 1673d912c694SMatthew Dillon * This completely breaks TCP if newreno is turned on. What happens 1674d912c694SMatthew Dillon * is that if delayed-acks are turned on on the receiver, this code 1675d912c694SMatthew Dillon * on the transmitter effectively destroys the TCP window, forcing 1676d912c694SMatthew Dillon * it to four packets (1.5Kx4 = 6K window). 1677d912c694SMatthew Dillon */ 1678dbc42409SLawrence Stewart if (sendalot && --maxburst) 1679df8bae1dSRodney W. Grimes goto again; 1680d912c694SMatthew Dillon #endif 1681d912c694SMatthew Dillon if (sendalot) 1682d912c694SMatthew Dillon goto again; 1683df8bae1dSRodney W. Grimes return (0); 1684df8bae1dSRodney W. Grimes } 1685df8bae1dSRodney W. Grimes 1686df8bae1dSRodney W. Grimes void 1687ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp) 1688df8bae1dSRodney W. Grimes { 16899b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 16909b8b58e0SJonathan Lemon int tt; 1691df8bae1dSRodney W. Grimes 1692672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 1693b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_REXMT)) 16949b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 1695df8bae1dSRodney W. Grimes /* 1696a4641f4eSPedro F. Giffuni * Start/restart persistence timer. 1697df8bae1dSRodney W. Grimes */ 16989b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 16990645c604SHiren Panchasara tcp_persmin, tcp_persmax); 1700b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, tt); 1701df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1702df8bae1dSRodney W. Grimes tp->t_rxtshift++; 1703df8bae1dSRodney W. Grimes } 170402a1a643SAndre Oppermann 170502a1a643SAndre Oppermann /* 170602a1a643SAndre Oppermann * Insert TCP options according to the supplied parameters to the place 170702a1a643SAndre Oppermann * optp in a consistent way. Can handle unaligned destinations. 170802a1a643SAndre Oppermann * 170902a1a643SAndre Oppermann * The order of the option processing is crucial for optimal packing and 171002a1a643SAndre Oppermann * alignment for the scarce option space. 171102a1a643SAndre Oppermann * 171202a1a643SAndre Oppermann * The optimal order for a SYN/SYN-ACK segment is: 171302a1a643SAndre Oppermann * MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) + 171402a1a643SAndre Oppermann * Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40. 171502a1a643SAndre Oppermann * 171602a1a643SAndre Oppermann * The SACK options should be last. SACK blocks consume 8*n+2 bytes. 171702a1a643SAndre Oppermann * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks). 171802a1a643SAndre Oppermann * At minimum we need 10 bytes (to generate 1 SACK block). If both 171902a1a643SAndre Oppermann * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present, 172002a1a643SAndre Oppermann * we only have 10 bytes for SACK options (40 - (12 + 18)). 172102a1a643SAndre Oppermann */ 172202a1a643SAndre Oppermann int 172302a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp) 172402a1a643SAndre Oppermann { 17255d20f974SJonathan T. Looney u_int32_t mask, optlen = 0; 172602a1a643SAndre Oppermann 172702a1a643SAndre Oppermann for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) { 172802a1a643SAndre Oppermann if ((to->to_flags & mask) != mask) 172902a1a643SAndre Oppermann continue; 17303a4018c4SAndre Oppermann if (optlen == TCP_MAXOLEN) 17313a4018c4SAndre Oppermann break; 173202a1a643SAndre Oppermann switch (to->to_flags & mask) { 173302a1a643SAndre Oppermann case TOF_MSS: 173402a1a643SAndre Oppermann while (optlen % 4) { 173502a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 173602a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 173702a1a643SAndre Oppermann } 17383a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG) 17393a4018c4SAndre Oppermann continue; 174002a1a643SAndre Oppermann optlen += TCPOLEN_MAXSEG; 174102a1a643SAndre Oppermann *optp++ = TCPOPT_MAXSEG; 174202a1a643SAndre Oppermann *optp++ = TCPOLEN_MAXSEG; 174302a1a643SAndre Oppermann to->to_mss = htons(to->to_mss); 174402a1a643SAndre Oppermann bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss)); 174502a1a643SAndre Oppermann optp += sizeof(to->to_mss); 174602a1a643SAndre Oppermann break; 174702a1a643SAndre Oppermann case TOF_SCALE: 174802a1a643SAndre Oppermann while (!optlen || optlen % 2 != 1) { 174902a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 175002a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 175102a1a643SAndre Oppermann } 17523a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW) 17533a4018c4SAndre Oppermann continue; 175402a1a643SAndre Oppermann optlen += TCPOLEN_WINDOW; 175502a1a643SAndre Oppermann *optp++ = TCPOPT_WINDOW; 175602a1a643SAndre Oppermann *optp++ = TCPOLEN_WINDOW; 175702a1a643SAndre Oppermann *optp++ = to->to_wscale; 175802a1a643SAndre Oppermann break; 175902a1a643SAndre Oppermann case TOF_SACKPERM: 176002a1a643SAndre Oppermann while (optlen % 2) { 176102a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 176202a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 176302a1a643SAndre Oppermann } 17643a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED) 17653a4018c4SAndre Oppermann continue; 176602a1a643SAndre Oppermann optlen += TCPOLEN_SACK_PERMITTED; 176702a1a643SAndre Oppermann *optp++ = TCPOPT_SACK_PERMITTED; 176802a1a643SAndre Oppermann *optp++ = TCPOLEN_SACK_PERMITTED; 176902a1a643SAndre Oppermann break; 177002a1a643SAndre Oppermann case TOF_TS: 177102a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 177202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 177302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 177402a1a643SAndre Oppermann } 17753a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP) 17763a4018c4SAndre Oppermann continue; 177702a1a643SAndre Oppermann optlen += TCPOLEN_TIMESTAMP; 177802a1a643SAndre Oppermann *optp++ = TCPOPT_TIMESTAMP; 177902a1a643SAndre Oppermann *optp++ = TCPOLEN_TIMESTAMP; 178002a1a643SAndre Oppermann to->to_tsval = htonl(to->to_tsval); 178102a1a643SAndre Oppermann to->to_tsecr = htonl(to->to_tsecr); 178202a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval)); 178302a1a643SAndre Oppermann optp += sizeof(to->to_tsval); 178402a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr)); 178502a1a643SAndre Oppermann optp += sizeof(to->to_tsecr); 178602a1a643SAndre Oppermann break; 178702a1a643SAndre Oppermann case TOF_SIGNATURE: 178802a1a643SAndre Oppermann { 178902a1a643SAndre Oppermann int siglen = TCPOLEN_SIGNATURE - 2; 179002a1a643SAndre Oppermann 179102a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 179202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 179302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 179402a1a643SAndre Oppermann } 1795fcf59617SAndrey V. Elsukov if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE) { 1796fcf59617SAndrey V. Elsukov to->to_flags &= ~TOF_SIGNATURE; 179702a1a643SAndre Oppermann continue; 1798fcf59617SAndrey V. Elsukov } 179902a1a643SAndre Oppermann optlen += TCPOLEN_SIGNATURE; 180002a1a643SAndre Oppermann *optp++ = TCPOPT_SIGNATURE; 180102a1a643SAndre Oppermann *optp++ = TCPOLEN_SIGNATURE; 180202a1a643SAndre Oppermann to->to_signature = optp; 180302a1a643SAndre Oppermann while (siglen--) 180402a1a643SAndre Oppermann *optp++ = 0; 180502a1a643SAndre Oppermann break; 180602a1a643SAndre Oppermann } 180702a1a643SAndre Oppermann case TOF_SACK: 180802a1a643SAndre Oppermann { 180902a1a643SAndre Oppermann int sackblks = 0; 181002a1a643SAndre Oppermann struct sackblk *sack = (struct sackblk *)to->to_sacks; 181102a1a643SAndre Oppermann tcp_seq sack_seq; 181202a1a643SAndre Oppermann 181302a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 181402a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 181502a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 181602a1a643SAndre Oppermann } 18173a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK) 181802a1a643SAndre Oppermann continue; 181902a1a643SAndre Oppermann optlen += TCPOLEN_SACKHDR; 182002a1a643SAndre Oppermann *optp++ = TCPOPT_SACK; 182102a1a643SAndre Oppermann sackblks = min(to->to_nsacks, 18220d957bbaSAndre Oppermann (TCP_MAXOLEN - optlen) / TCPOLEN_SACK); 182302a1a643SAndre Oppermann *optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK; 182402a1a643SAndre Oppermann while (sackblks--) { 182502a1a643SAndre Oppermann sack_seq = htonl(sack->start); 182602a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 182702a1a643SAndre Oppermann optp += sizeof(sack_seq); 182802a1a643SAndre Oppermann sack_seq = htonl(sack->end); 182902a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 183002a1a643SAndre Oppermann optp += sizeof(sack_seq); 183102a1a643SAndre Oppermann optlen += TCPOLEN_SACK; 183202a1a643SAndre Oppermann sack++; 183302a1a643SAndre Oppermann } 183478b50714SRobert Watson TCPSTAT_INC(tcps_sack_send_blocks); 183502a1a643SAndre Oppermann break; 183602a1a643SAndre Oppermann } 1837281a0fd4SPatrick Kelsey case TOF_FASTOPEN: 1838281a0fd4SPatrick Kelsey { 1839281a0fd4SPatrick Kelsey int total_len; 1840281a0fd4SPatrick Kelsey 1841281a0fd4SPatrick Kelsey /* XXX is there any point to aligning this option? */ 1842281a0fd4SPatrick Kelsey total_len = TCPOLEN_FAST_OPEN_EMPTY + to->to_tfo_len; 1843c560df6fSPatrick Kelsey if (TCP_MAXOLEN - optlen < total_len) { 1844c560df6fSPatrick Kelsey to->to_flags &= ~TOF_FASTOPEN; 1845281a0fd4SPatrick Kelsey continue; 1846c560df6fSPatrick Kelsey } 1847281a0fd4SPatrick Kelsey *optp++ = TCPOPT_FAST_OPEN; 1848281a0fd4SPatrick Kelsey *optp++ = total_len; 1849281a0fd4SPatrick Kelsey if (to->to_tfo_len > 0) { 1850281a0fd4SPatrick Kelsey bcopy(to->to_tfo_cookie, optp, to->to_tfo_len); 1851281a0fd4SPatrick Kelsey optp += to->to_tfo_len; 1852281a0fd4SPatrick Kelsey } 1853281a0fd4SPatrick Kelsey optlen += total_len; 1854281a0fd4SPatrick Kelsey break; 1855281a0fd4SPatrick Kelsey } 185602a1a643SAndre Oppermann default: 185702a1a643SAndre Oppermann panic("%s: unknown TCP option type", __func__); 185802a1a643SAndre Oppermann break; 185902a1a643SAndre Oppermann } 186002a1a643SAndre Oppermann } 186102a1a643SAndre Oppermann 186202a1a643SAndre Oppermann /* Terminate and pad TCP options to a 4 byte boundary. */ 186302a1a643SAndre Oppermann if (optlen % 4) { 186402a1a643SAndre Oppermann optlen += TCPOLEN_EOL; 186502a1a643SAndre Oppermann *optp++ = TCPOPT_EOL; 186602a1a643SAndre Oppermann } 1867413deb12SBjoern A. Zeeb /* 1868413deb12SBjoern A. Zeeb * According to RFC 793 (STD0007): 1869413deb12SBjoern A. Zeeb * "The content of the header beyond the End-of-Option option 1870413deb12SBjoern A. Zeeb * must be header padding (i.e., zero)." 1871413deb12SBjoern A. Zeeb * and later: "The padding is composed of zeros." 1872413deb12SBjoern A. Zeeb */ 187302a1a643SAndre Oppermann while (optlen % 4) { 1874c343c524SAndre Oppermann optlen += TCPOLEN_PAD; 1875c343c524SAndre Oppermann *optp++ = TCPOPT_PAD; 187602a1a643SAndre Oppermann } 187702a1a643SAndre Oppermann 18780d957bbaSAndre Oppermann KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__)); 187902a1a643SAndre Oppermann return (optlen); 188002a1a643SAndre Oppermann } 188166492feaSGleb Smirnoff 188289e560f4SRandall Stewart /* 188389e560f4SRandall Stewart * This is a copy of m_copym(), taking the TSO segment size/limit 188489e560f4SRandall Stewart * constraints into account, and advancing the sndptr as it goes. 188589e560f4SRandall Stewart */ 188689e560f4SRandall Stewart struct mbuf * 188789e560f4SRandall Stewart tcp_m_copym(struct mbuf *m, int32_t off0, int32_t *plen, 1888b2e60773SJohn Baldwin int32_t seglimit, int32_t segsize, struct sockbuf *sb, bool hw_tls) 188989e560f4SRandall Stewart { 1890b2e60773SJohn Baldwin #ifdef KERN_TLS 1891b2e60773SJohn Baldwin struct ktls_session *tls, *ntls; 1892b2e60773SJohn Baldwin struct mbuf *start; 1893b2e60773SJohn Baldwin #endif 189489e560f4SRandall Stewart struct mbuf *n, **np; 189589e560f4SRandall Stewart struct mbuf *top; 189689e560f4SRandall Stewart int32_t off = off0; 189789e560f4SRandall Stewart int32_t len = *plen; 189889e560f4SRandall Stewart int32_t fragsize; 189989e560f4SRandall Stewart int32_t len_cp = 0; 190089e560f4SRandall Stewart int32_t *pkthdrlen; 190189e560f4SRandall Stewart uint32_t mlen, frags; 190289e560f4SRandall Stewart bool copyhdr; 190389e560f4SRandall Stewart 190489e560f4SRandall Stewart 190589e560f4SRandall Stewart KASSERT(off >= 0, ("tcp_m_copym, negative off %d", off)); 190689e560f4SRandall Stewart KASSERT(len >= 0, ("tcp_m_copym, negative len %d", len)); 190789e560f4SRandall Stewart if (off == 0 && m->m_flags & M_PKTHDR) 190889e560f4SRandall Stewart copyhdr = true; 190989e560f4SRandall Stewart else 191089e560f4SRandall Stewart copyhdr = false; 191189e560f4SRandall Stewart while (off > 0) { 191289e560f4SRandall Stewart KASSERT(m != NULL, ("tcp_m_copym, offset > size of mbuf chain")); 191389e560f4SRandall Stewart if (off < m->m_len) 191489e560f4SRandall Stewart break; 191589e560f4SRandall Stewart off -= m->m_len; 191689e560f4SRandall Stewart if ((sb) && (m == sb->sb_sndptr)) { 191789e560f4SRandall Stewart sb->sb_sndptroff += m->m_len; 191889e560f4SRandall Stewart sb->sb_sndptr = m->m_next; 191989e560f4SRandall Stewart } 192089e560f4SRandall Stewart m = m->m_next; 192189e560f4SRandall Stewart } 192289e560f4SRandall Stewart np = ⊤ 192389e560f4SRandall Stewart top = NULL; 192489e560f4SRandall Stewart pkthdrlen = NULL; 1925b2e60773SJohn Baldwin #ifdef KERN_TLS 19266edfd179SGleb Smirnoff if (hw_tls && (m->m_flags & M_EXTPG)) 19277b6c99d0SGleb Smirnoff tls = m->m_epg_tls; 1928b2e60773SJohn Baldwin else 1929b2e60773SJohn Baldwin tls = NULL; 1930b2e60773SJohn Baldwin start = m; 1931b2e60773SJohn Baldwin #endif 193289e560f4SRandall Stewart while (len > 0) { 193389e560f4SRandall Stewart if (m == NULL) { 193489e560f4SRandall Stewart KASSERT(len == M_COPYALL, 193589e560f4SRandall Stewart ("tcp_m_copym, length > size of mbuf chain")); 193689e560f4SRandall Stewart *plen = len_cp; 193789e560f4SRandall Stewart if (pkthdrlen != NULL) 193889e560f4SRandall Stewart *pkthdrlen = len_cp; 193989e560f4SRandall Stewart break; 194089e560f4SRandall Stewart } 1941b2e60773SJohn Baldwin #ifdef KERN_TLS 1942b2e60773SJohn Baldwin if (hw_tls) { 19436edfd179SGleb Smirnoff if (m->m_flags & M_EXTPG) 19447b6c99d0SGleb Smirnoff ntls = m->m_epg_tls; 1945b2e60773SJohn Baldwin else 1946b2e60773SJohn Baldwin ntls = NULL; 1947b2e60773SJohn Baldwin 1948b2e60773SJohn Baldwin /* 1949b2e60773SJohn Baldwin * Avoid mixing TLS records with handshake 1950b2e60773SJohn Baldwin * data or TLS records from different 1951b2e60773SJohn Baldwin * sessions. 1952b2e60773SJohn Baldwin */ 1953b2e60773SJohn Baldwin if (tls != ntls) { 1954b2e60773SJohn Baldwin MPASS(m != start); 1955b2e60773SJohn Baldwin *plen = len_cp; 1956b2e60773SJohn Baldwin if (pkthdrlen != NULL) 1957b2e60773SJohn Baldwin *pkthdrlen = len_cp; 1958b2e60773SJohn Baldwin break; 1959b2e60773SJohn Baldwin } 1960b2e60773SJohn Baldwin 1961b2e60773SJohn Baldwin /* 1962b2e60773SJohn Baldwin * Don't end a send in the middle of a TLS 1963b2e60773SJohn Baldwin * record if it spans multiple TLS records. 1964b2e60773SJohn Baldwin */ 1965b2e60773SJohn Baldwin if (tls != NULL && (m != start) && len < m->m_len) { 1966b2e60773SJohn Baldwin *plen = len_cp; 1967b2e60773SJohn Baldwin if (pkthdrlen != NULL) 1968b2e60773SJohn Baldwin *pkthdrlen = len_cp; 1969b2e60773SJohn Baldwin break; 1970b2e60773SJohn Baldwin } 1971b2e60773SJohn Baldwin } 1972b2e60773SJohn Baldwin #endif 197389e560f4SRandall Stewart mlen = min(len, m->m_len - off); 197489e560f4SRandall Stewart if (seglimit) { 197589e560f4SRandall Stewart /* 19766edfd179SGleb Smirnoff * For M_EXTPG mbufs, add 3 segments 197789e560f4SRandall Stewart * + 1 in case we are crossing page boundaries 197889e560f4SRandall Stewart * + 2 in case the TLS hdr/trailer are used 197989e560f4SRandall Stewart * It is cheaper to just add the segments 198089e560f4SRandall Stewart * than it is to take the cache miss to look 198189e560f4SRandall Stewart * at the mbuf ext_pgs state in detail. 198289e560f4SRandall Stewart */ 19836edfd179SGleb Smirnoff if (m->m_flags & M_EXTPG) { 198489e560f4SRandall Stewart fragsize = min(segsize, PAGE_SIZE); 198589e560f4SRandall Stewart frags = 3; 198689e560f4SRandall Stewart } else { 198789e560f4SRandall Stewart fragsize = segsize; 198889e560f4SRandall Stewart frags = 0; 198989e560f4SRandall Stewart } 199089e560f4SRandall Stewart 199189e560f4SRandall Stewart /* Break if we really can't fit anymore. */ 199289e560f4SRandall Stewart if ((frags + 1) >= seglimit) { 199389e560f4SRandall Stewart *plen = len_cp; 199489e560f4SRandall Stewart if (pkthdrlen != NULL) 199589e560f4SRandall Stewart *pkthdrlen = len_cp; 199689e560f4SRandall Stewart break; 199789e560f4SRandall Stewart } 199889e560f4SRandall Stewart 199989e560f4SRandall Stewart /* 200089e560f4SRandall Stewart * Reduce size if you can't copy the whole 200189e560f4SRandall Stewart * mbuf. If we can't copy the whole mbuf, also 200289e560f4SRandall Stewart * adjust len so the loop will end after this 200389e560f4SRandall Stewart * mbuf. 200489e560f4SRandall Stewart */ 200589e560f4SRandall Stewart if ((frags + howmany(mlen, fragsize)) >= seglimit) { 200689e560f4SRandall Stewart mlen = (seglimit - frags - 1) * fragsize; 200789e560f4SRandall Stewart len = mlen; 200889e560f4SRandall Stewart *plen = len_cp + len; 200989e560f4SRandall Stewart if (pkthdrlen != NULL) 201089e560f4SRandall Stewart *pkthdrlen = *plen; 201189e560f4SRandall Stewart } 201289e560f4SRandall Stewart frags += howmany(mlen, fragsize); 201389e560f4SRandall Stewart if (frags == 0) 201489e560f4SRandall Stewart frags++; 201589e560f4SRandall Stewart seglimit -= frags; 201689e560f4SRandall Stewart KASSERT(seglimit > 0, 201789e560f4SRandall Stewart ("%s: seglimit went too low", __func__)); 201889e560f4SRandall Stewart } 201989e560f4SRandall Stewart if (copyhdr) 202089e560f4SRandall Stewart n = m_gethdr(M_NOWAIT, m->m_type); 202189e560f4SRandall Stewart else 202289e560f4SRandall Stewart n = m_get(M_NOWAIT, m->m_type); 202389e560f4SRandall Stewart *np = n; 202489e560f4SRandall Stewart if (n == NULL) 202589e560f4SRandall Stewart goto nospace; 202689e560f4SRandall Stewart if (copyhdr) { 202789e560f4SRandall Stewart if (!m_dup_pkthdr(n, m, M_NOWAIT)) 202889e560f4SRandall Stewart goto nospace; 202989e560f4SRandall Stewart if (len == M_COPYALL) 203089e560f4SRandall Stewart n->m_pkthdr.len -= off0; 203189e560f4SRandall Stewart else 203289e560f4SRandall Stewart n->m_pkthdr.len = len; 203389e560f4SRandall Stewart pkthdrlen = &n->m_pkthdr.len; 203489e560f4SRandall Stewart copyhdr = false; 203589e560f4SRandall Stewart } 203689e560f4SRandall Stewart n->m_len = mlen; 203789e560f4SRandall Stewart len_cp += n->m_len; 203861664ee7SGleb Smirnoff if (m->m_flags & (M_EXT|M_EXTPG)) { 203989e560f4SRandall Stewart n->m_data = m->m_data + off; 204089e560f4SRandall Stewart mb_dupcl(n, m); 204189e560f4SRandall Stewart } else 204289e560f4SRandall Stewart bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 204389e560f4SRandall Stewart (u_int)n->m_len); 204489e560f4SRandall Stewart 204589e560f4SRandall Stewart if (sb && (sb->sb_sndptr == m) && 204689e560f4SRandall Stewart ((n->m_len + off) >= m->m_len) && m->m_next) { 204789e560f4SRandall Stewart sb->sb_sndptroff += m->m_len; 204889e560f4SRandall Stewart sb->sb_sndptr = m->m_next; 204989e560f4SRandall Stewart } 205089e560f4SRandall Stewart off = 0; 205189e560f4SRandall Stewart if (len != M_COPYALL) { 205289e560f4SRandall Stewart len -= n->m_len; 205389e560f4SRandall Stewart } 205489e560f4SRandall Stewart m = m->m_next; 205589e560f4SRandall Stewart np = &n->m_next; 205689e560f4SRandall Stewart } 205789e560f4SRandall Stewart return (top); 205889e560f4SRandall Stewart nospace: 205989e560f4SRandall Stewart m_freem(top); 206089e560f4SRandall Stewart return (NULL); 206189e560f4SRandall Stewart } 206289e560f4SRandall Stewart 206366492feaSGleb Smirnoff void 206466492feaSGleb Smirnoff tcp_sndbuf_autoscale(struct tcpcb *tp, struct socket *so, uint32_t sendwin) 206566492feaSGleb Smirnoff { 206666492feaSGleb Smirnoff 206766492feaSGleb Smirnoff /* 206866492feaSGleb Smirnoff * Automatic sizing of send socket buffer. Often the send buffer 206966492feaSGleb Smirnoff * size is not optimally adjusted to the actual network conditions 207066492feaSGleb Smirnoff * at hand (delay bandwidth product). Setting the buffer size too 207166492feaSGleb Smirnoff * small limits throughput on links with high bandwidth and high 207266492feaSGleb Smirnoff * delay (eg. trans-continental/oceanic links). Setting the 207366492feaSGleb Smirnoff * buffer size too big consumes too much real kernel memory, 207466492feaSGleb Smirnoff * especially with many connections on busy servers. 207566492feaSGleb Smirnoff * 207666492feaSGleb Smirnoff * The criteria to step up the send buffer one notch are: 207766492feaSGleb Smirnoff * 1. receive window of remote host is larger than send buffer 207866492feaSGleb Smirnoff * (with a fudge factor of 5/4th); 207966492feaSGleb Smirnoff * 2. send buffer is filled to 7/8th with data (so we actually 208066492feaSGleb Smirnoff * have data to make use of it); 208166492feaSGleb Smirnoff * 3. send buffer fill has not hit maximal automatic size; 208266492feaSGleb Smirnoff * 4. our send window (slow start and cogestion controlled) is 208366492feaSGleb Smirnoff * larger than sent but unacknowledged data in send buffer. 208466492feaSGleb Smirnoff * 208566492feaSGleb Smirnoff * The remote host receive window scaling factor may limit the 208666492feaSGleb Smirnoff * growing of the send buffer before it reaches its allowed 208766492feaSGleb Smirnoff * maximum. 208866492feaSGleb Smirnoff * 208966492feaSGleb Smirnoff * It scales directly with slow start or congestion window 209066492feaSGleb Smirnoff * and does at most one step per received ACK. This fast 209166492feaSGleb Smirnoff * scaling has the drawback of growing the send buffer beyond 209266492feaSGleb Smirnoff * what is strictly necessary to make full use of a given 209366492feaSGleb Smirnoff * delay*bandwidth product. However testing has shown this not 209466492feaSGleb Smirnoff * to be much of an problem. At worst we are trading wasting 209566492feaSGleb Smirnoff * of available bandwidth (the non-use of it) for wasting some 209666492feaSGleb Smirnoff * socket buffer memory. 209766492feaSGleb Smirnoff * 209866492feaSGleb Smirnoff * TODO: Shrink send buffer during idle periods together 209966492feaSGleb Smirnoff * with congestion window. Requires another timer. Has to 210066492feaSGleb Smirnoff * wait for upcoming tcp timer rewrite. 210166492feaSGleb Smirnoff * 210266492feaSGleb Smirnoff * XXXGL: should there be used sbused() or sbavail()? 210366492feaSGleb Smirnoff */ 210466492feaSGleb Smirnoff if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 210566492feaSGleb Smirnoff int lowat; 210666492feaSGleb Smirnoff 210766492feaSGleb Smirnoff lowat = V_tcp_sendbuf_auto_lowat ? so->so_snd.sb_lowat : 0; 210866492feaSGleb Smirnoff if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat - lowat && 210966492feaSGleb Smirnoff sbused(&so->so_snd) >= 211066492feaSGleb Smirnoff (so->so_snd.sb_hiwat / 8 * 7) - lowat && 211166492feaSGleb Smirnoff sbused(&so->so_snd) < V_tcp_autosndbuf_max && 211266492feaSGleb Smirnoff sendwin >= (sbused(&so->so_snd) - 211366492feaSGleb Smirnoff (tp->snd_nxt - tp->snd_una))) { 211466492feaSGleb Smirnoff if (!sbreserve_locked(&so->so_snd, 211566492feaSGleb Smirnoff min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc, 211666492feaSGleb Smirnoff V_tcp_autosndbuf_max), so, curthread)) 211766492feaSGleb Smirnoff so->so_snd.sb_flags &= ~SB_AUTOSIZE; 211866492feaSGleb Smirnoff } 211966492feaSGleb Smirnoff } 212066492feaSGleb Smirnoff } 2121