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_var.h> 88f7220c48SRichard Scheffenegger #include <netinet/tcp_syncache.h> 89f7220c48SRichard Scheffenegger #include <netinet/tcp_timer.h> 90df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 914644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 92c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 9386a996e6SHiren Panchasara #ifdef TCPPCAP 9486a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 9586a996e6SHiren Panchasara #endif 96610ee2f9SDavid Greenman #ifdef TCPDEBUG 97df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 98610ee2f9SDavid Greenman #endif 9909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 10009fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 10109fe6320SNavdeep Parhar #endif 102f7220c48SRichard Scheffenegger #include <netinet/tcp_ecn.h> 103df8bae1dSRodney W. Grimes 104fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 105b9234fafSSam Leffler 1069e644c23SMichael Tuexen #include <netinet/udp.h> 1079e644c23SMichael Tuexen #include <netinet/udp_var.h> 108db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 109db4f9cc7SJonathan Lemon 110aed55708SRobert Watson #include <security/mac/mac_framework.h> 111aed55708SRobert Watson 11282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, path_mtu_discovery) = 1; 1136df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_VNET | CTLFLAG_RW, 114eddfbb76SRobert Watson &VNET_NAME(path_mtu_discovery), 1, 115eddfbb76SRobert Watson "Enable Path MTU Discovery"); 1169a039a5fSDavid Greenman 11782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_tso) = 1; 1186df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_VNET | CTLFLAG_RW, 119eddfbb76SRobert Watson &VNET_NAME(tcp_do_tso), 0, 120eddfbb76SRobert Watson "Enable TCP Segmentation Offload"); 121b3c0f300SAndre Oppermann 122873789cbSAndre Oppermann VNET_DEFINE(int, tcp_sendspace) = 1024*32; 123873789cbSAndre Oppermann #define V_tcp_sendspace VNET(tcp_sendspace) 1246df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_VNET | CTLFLAG_RW, 125873789cbSAndre Oppermann &VNET_NAME(tcp_sendspace), 0, "Initial send socket buffer size"); 126873789cbSAndre Oppermann 12782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autosndbuf) = 1; 1286df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 129eddfbb76SRobert Watson &VNET_NAME(tcp_do_autosndbuf), 0, 130eddfbb76SRobert Watson "Enable automatic send buffer sizing"); 1316741ecf5SAndre Oppermann 13282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_inc) = 8*1024; 1336df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 134eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_inc), 0, 1358b615593SMarko Zec "Incrementor step size of automatic send buffer"); 1366741ecf5SAndre Oppermann 137b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_max) = 2*1024*1024; 1386df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 139eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_max), 0, 1408b615593SMarko Zec "Max size of automatic send buffer"); 1416741ecf5SAndre Oppermann 142ac952dd2SSean Bruno VNET_DEFINE(int, tcp_sendbuf_auto_lowat) = 0; 143ac952dd2SSean Bruno #define V_tcp_sendbuf_auto_lowat VNET(tcp_sendbuf_auto_lowat) 144ac952dd2SSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto_lowat, CTLFLAG_VNET | CTLFLAG_RW, 145ac952dd2SSean Bruno &VNET_NAME(tcp_sendbuf_auto_lowat), 0, 146ac952dd2SSean Bruno "Modify threshold for auto send buffer growth to account for SO_SNDLOWAT"); 147ac952dd2SSean Bruno 148425b7639SSepherosa Ziehau /* 149425b7639SSepherosa Ziehau * Make sure that either retransmit or persist timer is set for SYN, FIN and 150425b7639SSepherosa Ziehau * non-ACK. 151425b7639SSepherosa Ziehau */ 152425b7639SSepherosa Ziehau #define TCP_XMIT_TIMER_ASSERT(tp, len, th_flags) \ 153fd29ff5dSRandall Stewart KASSERT(((len) == 0 && ((th_flags) & (TH_SYN | TH_FIN)) == 0) ||\ 154425b7639SSepherosa Ziehau tcp_timer_active((tp), TT_REXMT) || \ 155425b7639SSepherosa Ziehau tcp_timer_active((tp), TT_PERSIST), \ 156425b7639SSepherosa Ziehau ("neither rexmt nor persist timer is set")) 157425b7639SSepherosa Ziehau 158dbc42409SLawrence Stewart static void inline cc_after_idle(struct tcpcb *tp); 159dbc42409SLawrence Stewart 160bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 161dbc42409SLawrence Stewart /* 1620f5e7edcSKevin Lo * Wrapper for the TCP established output helper hook. 16339bc9de5SLawrence Stewart */ 16489e560f4SRandall Stewart void 16539bc9de5SLawrence Stewart hhook_run_tcp_est_out(struct tcpcb *tp, struct tcphdr *th, 1663ac12506SJonathan T. Looney struct tcpopt *to, uint32_t len, int tso) 16739bc9de5SLawrence Stewart { 16839bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 16939bc9de5SLawrence Stewart 17039bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_OUT]->hhh_nhooks > 0) { 17139bc9de5SLawrence Stewart hhook_data.tp = tp; 17239bc9de5SLawrence Stewart hhook_data.th = th; 17339bc9de5SLawrence Stewart hhook_data.to = to; 17439bc9de5SLawrence Stewart hhook_data.len = len; 17539bc9de5SLawrence Stewart hhook_data.tso = tso; 17639bc9de5SLawrence Stewart 17739bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_OUT], &hhook_data, 17839bc9de5SLawrence Stewart tp->osd); 17939bc9de5SLawrence Stewart } 18039bc9de5SLawrence Stewart } 181bd79708dSJonathan T. Looney #endif 18239bc9de5SLawrence Stewart 18339bc9de5SLawrence Stewart /* 184dbc42409SLawrence Stewart * CC wrapper hook functions 185dbc42409SLawrence Stewart */ 186dbc42409SLawrence Stewart static void inline 187dbc42409SLawrence Stewart cc_after_idle(struct tcpcb *tp) 188dbc42409SLawrence Stewart { 189dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 190dbc42409SLawrence Stewart 191dbc42409SLawrence Stewart if (CC_ALGO(tp)->after_idle != NULL) 192dbc42409SLawrence Stewart CC_ALGO(tp)->after_idle(tp->ccv); 193dbc42409SLawrence Stewart } 1946741ecf5SAndre Oppermann 195df8bae1dSRodney W. Grimes /* 196df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 197df8bae1dSRodney W. Grimes */ 198df8bae1dSRodney W. Grimes int 1995b08b46aSGleb Smirnoff tcp_default_output(struct tcpcb *tp) 200df8bae1dSRodney W. Grimes { 201f10e85d7SLuigi Rizzo struct socket *so = tp->t_inpcb->inp_socket; 2023ac12506SJonathan T. Looney int32_t len; 2033ac12506SJonathan T. Looney uint32_t recwin, sendwin; 204f7220c48SRichard Scheffenegger uint16_t flags; 205f7220c48SRichard Scheffenegger int off, error = 0; /* Keep compiler happy */ 206581a046aSRandall Stewart u_int if_hw_tsomaxsegcount = 0; 20782e837f8SWarner Losh u_int if_hw_tsomaxsegsize = 0; 208f10e85d7SLuigi Rizzo struct mbuf *m; 209fb59c426SYoshinobu Inoue struct ip *ip = NULL; 210151ba793SAlexander Kabaev #ifdef TCPDEBUG 211f10e85d7SLuigi Rizzo struct ipovly *ipov = NULL; 212151ba793SAlexander Kabaev #endif 213f10e85d7SLuigi Rizzo struct tcphdr *th; 214a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 2159e644c23SMichael Tuexen unsigned ipoptlen, optlen, hdrlen, ulen; 216fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 2179ad0173dSBjoern A. Zeeb unsigned ipsec_optlen = 0; 2189ad0173dSBjoern A. Zeeb #endif 21910d20c84SMatt Macy int idle, sendalot, curticks; 22002a1a643SAndre Oppermann int sack_rxmit, sack_bytes_rxmt; 2216d90faf3SPaul Saab struct sackhole *p; 222df0633a1SGleb Smirnoff int tso, mtu; 22302a1a643SAndre Oppermann struct tcpopt to; 2249e644c23SMichael Tuexen struct udphdr *udp = NULL; 225bd30a121SMichael Tuexen struct tcp_log_buffer *lgb; 226c560df6fSPatrick Kelsey unsigned int wanted_cookie = 0; 227c560df6fSPatrick Kelsey unsigned int dont_sendalot = 0; 228262c1c1aSMatthew Dillon #if 0 22946f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 230262c1c1aSMatthew Dillon #endif 231fb59c426SYoshinobu Inoue #ifdef INET6 232f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 233fb59c426SYoshinobu Inoue int isipv6; 234fb59c426SYoshinobu Inoue 235fb59c426SYoshinobu Inoue isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 236fb59c426SYoshinobu Inoue #endif 237b2e60773SJohn Baldwin #ifdef KERN_TLS 238b2e60773SJohn Baldwin const bool hw_tls = (so->so_snd.sb_flags & SB_TLS_IFNET) != 0; 239b2e60773SJohn Baldwin #else 240b2e60773SJohn Baldwin const bool hw_tls = false; 241b2e60773SJohn Baldwin #endif 242df8bae1dSRodney W. Grimes 243109eb549SGleb Smirnoff NET_EPOCH_ASSERT(); 2448501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 2453d6ade3aSJennifer Yang 24609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 24709fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 24809fe6320SNavdeep Parhar return (tcp_offload_output(tp)); 24909fe6320SNavdeep Parhar #endif 25009fe6320SNavdeep Parhar 251281a0fd4SPatrick Kelsey /* 2528db239dcSMichael Tuexen * For TFO connections in SYN_SENT or SYN_RECEIVED, 2538db239dcSMichael Tuexen * only allow the initial SYN or SYN|ACK and those sent 2548db239dcSMichael Tuexen * by the retransmit timer. 255281a0fd4SPatrick Kelsey */ 25668bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 2578db239dcSMichael Tuexen ((tp->t_state == TCPS_SYN_SENT) || 2588db239dcSMichael Tuexen (tp->t_state == TCPS_SYN_RECEIVED)) && 2598db239dcSMichael Tuexen SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 260281a0fd4SPatrick Kelsey (tp->snd_nxt != tp->snd_una)) /* not a retransmit */ 261281a0fd4SPatrick Kelsey return (0); 26218a75309SPatrick Kelsey 263df8bae1dSRodney W. Grimes /* 264df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 265df8bae1dSRodney W. Grimes * and flags that will be used. 266df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 267df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 268df8bae1dSRodney W. Grimes */ 269c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 2709dc7d8a2SRichard Scheffenegger if (idle && (((ticks - tp->t_rcvtime) >= tp->t_rxtcur) || 2719dc7d8a2SRichard Scheffenegger (tp->t_sndtime && ((ticks - tp->t_sndtime) >= tp->t_rxtcur)))) 2722ea8da28SLawrence Stewart cc_after_idle(tp); 273c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 274c24d5daeSJayanth Vijayaraghavan if (idle) { 275c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 276c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 277c24d5daeSJayanth Vijayaraghavan idle = 0; 278c24d5daeSJayanth Vijayaraghavan } 279c24d5daeSJayanth Vijayaraghavan } 280df8bae1dSRodney W. Grimes again: 2816d90faf3SPaul Saab /* 2826d90faf3SPaul Saab * If we've recently taken a timeout, snd_max will be greater than 2836d90faf3SPaul Saab * snd_nxt. There may be SACK information that allows us to avoid 2846d90faf3SPaul Saab * resending already delivered data. Adjust snd_nxt accordingly. 2856d90faf3SPaul Saab */ 2863529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2873529149eSAndre Oppermann SEQ_LT(tp->snd_nxt, tp->snd_max)) 2886d90faf3SPaul Saab tcp_sack_adjust(tp); 289df8bae1dSRodney W. Grimes sendalot = 0; 290153e5b57SAndre Oppermann tso = 0; 291df0633a1SGleb Smirnoff mtu = 0; 292df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 293201d185bSAndre Oppermann sendwin = min(tp->snd_wnd, tp->snd_cwnd); 294df8bae1dSRodney W. Grimes 295df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 296a0292f23SGarrett Wollman /* 2976d90faf3SPaul Saab * Send any SACK-generated retransmissions. If we're explicitly trying 2986d90faf3SPaul Saab * to send out new data (when sendalot is 1), bypass this function. 2996d90faf3SPaul Saab * If we retransmit in fast recovery mode, decrement snd_cwnd, since 3006d90faf3SPaul Saab * we're replacing a (future) new transmission with a retransmission 3016d90faf3SPaul Saab * now, and we previously incremented snd_cwnd in tcp_input(). 3026d90faf3SPaul Saab */ 3036d90faf3SPaul Saab /* 3046d90faf3SPaul Saab * Still in sack recovery , reset rxmit flag to zero. 3056d90faf3SPaul Saab */ 3066d90faf3SPaul Saab sack_rxmit = 0; 307a55db2b6SPaul Saab sack_bytes_rxmt = 0; 3086d90faf3SPaul Saab len = 0; 3096d90faf3SPaul Saab p = NULL; 310dbc42409SLawrence Stewart if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp->t_flags) && 311a55db2b6SPaul Saab (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 3123ac12506SJonathan T. Looney uint32_t cwin; 313a55db2b6SPaul Saab 3143ac12506SJonathan T. Looney cwin = 3153ac12506SJonathan T. Looney imax(min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt, 0); 316f787edd8SJayanth Vijayaraghavan /* Do not retransmit SACK segments beyond snd_recover */ 317f787edd8SJayanth Vijayaraghavan if (SEQ_GT(p->end, tp->snd_recover)) { 318f787edd8SJayanth Vijayaraghavan /* 319f787edd8SJayanth Vijayaraghavan * (At least) part of sack hole extends beyond 320f787edd8SJayanth Vijayaraghavan * snd_recover. Check to see if we can rexmit data 321f787edd8SJayanth Vijayaraghavan * for this hole. 322f787edd8SJayanth Vijayaraghavan */ 323f787edd8SJayanth Vijayaraghavan if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 324f787edd8SJayanth Vijayaraghavan /* 325f787edd8SJayanth Vijayaraghavan * Can't rexmit any more data for this hole. 326f787edd8SJayanth Vijayaraghavan * That data will be rexmitted in the next 327f787edd8SJayanth Vijayaraghavan * sack recovery episode, when snd_recover 328f787edd8SJayanth Vijayaraghavan * moves past p->rxmit. 329f787edd8SJayanth Vijayaraghavan */ 330f787edd8SJayanth Vijayaraghavan p = NULL; 331f787edd8SJayanth Vijayaraghavan goto after_sack_rexmit; 33228173d49SHans Petter Selasky } else { 333f787edd8SJayanth Vijayaraghavan /* Can rexmit part of the current hole */ 3343ac12506SJonathan T. Looney len = ((int32_t)ulmin(cwin, 33528173d49SHans Petter Selasky SEQ_SUB(tp->snd_recover, p->rxmit))); 33628173d49SHans Petter Selasky } 33728173d49SHans Petter Selasky } else { 33828173d49SHans Petter Selasky len = ((int32_t)ulmin(cwin, 33928173d49SHans Petter Selasky SEQ_SUB(p->end, p->rxmit))); 34028173d49SHans Petter Selasky } 341*6d9e911fSMichael Tuexen if (len > 0) { 34228173d49SHans Petter Selasky off = SEQ_SUB(p->rxmit, tp->snd_una); 343f787edd8SJayanth Vijayaraghavan KASSERT(off >= 0,("%s: sack block to the left of una : %d", 344f787edd8SJayanth Vijayaraghavan __func__, off)); 345ab5c14d8SRobert Watson sack_rxmit = 1; 346ab5c14d8SRobert Watson sendalot = 1; 34778b50714SRobert Watson TCPSTAT_INC(tcps_sack_rexmits); 34878b50714SRobert Watson TCPSTAT_ADD(tcps_sack_rexmit_bytes, 3494b72ae16SRichard Scheffenegger min(len, tcp_maxseg(tp))); 3506d90faf3SPaul Saab } 3516d90faf3SPaul Saab } 352f787edd8SJayanth Vijayaraghavan after_sack_rexmit: 3536d90faf3SPaul Saab /* 354a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 355a0292f23SGarrett Wollman * state flags. 356a0292f23SGarrett Wollman */ 357a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 358a0292f23SGarrett Wollman flags |= TH_FIN; 359a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 360a0292f23SGarrett Wollman flags |= TH_SYN; 361a0292f23SGarrett Wollman 362cf2942b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 363df8bae1dSRodney W. Grimes /* 364df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 365df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 366df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 367df8bae1dSRodney W. Grimes * and go to transmit state. 368df8bae1dSRodney W. Grimes */ 3692cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) { 370201d185bSAndre Oppermann if (sendwin == 0) { 371df8bae1dSRodney W. Grimes /* 372df8bae1dSRodney W. Grimes * If we still have some data to send, then 373df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 374df8bae1dSRodney W. Grimes * happen below when it realizes that we 375df8bae1dSRodney W. Grimes * aren't sending all the data. However, 37629089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 377df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 378df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 379df8bae1dSRodney W. Grimes * up sending the packet without recording 380df8bae1dSRodney W. Grimes * that we sent the FIN bit. 381df8bae1dSRodney W. Grimes * 382df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 383df8bae1dSRodney W. Grimes * because if we don't have any more data 384df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 385df8bae1dSRodney W. Grimes * itself. 386df8bae1dSRodney W. Grimes */ 387cfa6009eSGleb Smirnoff if (off < sbused(&so->so_snd)) 388df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 389201d185bSAndre Oppermann sendwin = 1; 390df8bae1dSRodney W. Grimes } else { 391b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, 0); 392df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 393df8bae1dSRodney W. Grimes } 394df8bae1dSRodney W. Grimes } 395df8bae1dSRodney W. Grimes 39628257b5cSMatthew Dillon /* 39728257b5cSMatthew Dillon * If snd_nxt == snd_max and we have transmitted a FIN, the 39828257b5cSMatthew Dillon * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 399201d185bSAndre Oppermann * a negative length. This can also occur when TCP opens up 40028257b5cSMatthew Dillon * its congestion window while receiving additional duplicate 40128257b5cSMatthew Dillon * acks after fast-retransmit because TCP will reset snd_nxt 40228257b5cSMatthew Dillon * to snd_max after the fast-retransmit. 40328257b5cSMatthew Dillon * 40428257b5cSMatthew Dillon * In the normal retransmit-FIN-only case, however, snd_nxt will 40528257b5cSMatthew Dillon * be set to snd_una, the offset will be 0, and the length may 40628257b5cSMatthew Dillon * wind up 0. 4076d90faf3SPaul Saab * 4086d90faf3SPaul Saab * If sack_rxmit is true we are retransmitting from the scoreboard 4096d90faf3SPaul Saab * in which case len is already set. 41028257b5cSMatthew Dillon */ 411a55db2b6SPaul Saab if (sack_rxmit == 0) { 412a55db2b6SPaul Saab if (sack_bytes_rxmt == 0) 413cb503ae2SJonathan T. Looney len = ((int32_t)min(sbavail(&so->so_snd), sendwin) - 414cfa6009eSGleb Smirnoff off); 415a55db2b6SPaul Saab else { 4163ac12506SJonathan T. Looney int32_t cwin; 417a55db2b6SPaul Saab 418a55db2b6SPaul Saab /* 419a55db2b6SPaul Saab * We are inside of a SACK recovery episode and are 420a55db2b6SPaul Saab * sending new data, having retransmitted all the 421a55db2b6SPaul Saab * data possible in the scoreboard. 422a55db2b6SPaul Saab */ 4233ac12506SJonathan T. Looney len = ((int32_t)min(sbavail(&so->so_snd), tp->snd_wnd) - 424cfa6009eSGleb Smirnoff off); 4258d03f2b5SPaul Saab /* 4268d03f2b5SPaul Saab * Don't remove this (len > 0) check ! 4278d03f2b5SPaul Saab * We explicitly check for len > 0 here (although it 4288d03f2b5SPaul Saab * isn't really necessary), to work around a gcc 4298d03f2b5SPaul Saab * optimization issue - to force gcc to compute 4308d03f2b5SPaul Saab * len above. Without this check, the computation 4318d03f2b5SPaul Saab * of len is bungled by the optimizer. 4328d03f2b5SPaul Saab */ 4338d03f2b5SPaul Saab if (len > 0) { 4347d5ed1ceSPaul Saab cwin = tp->snd_cwnd - 435a3574665SMichael Tuexen (tp->snd_nxt - tp->snd_recover) - 436a55db2b6SPaul Saab sack_bytes_rxmt; 437a55db2b6SPaul Saab if (cwin < 0) 438a55db2b6SPaul Saab cwin = 0; 4393ac12506SJonathan T. Looney len = imin(len, cwin); 440a55db2b6SPaul Saab } 441a55db2b6SPaul Saab } 4428d03f2b5SPaul Saab } 443a0292f23SGarrett Wollman 444a0292f23SGarrett Wollman /* 445a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 446a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 447a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 448a0292f23SGarrett Wollman */ 449a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 4504b8e98d6SQing Li if (tp->t_state != TCPS_SYN_RECEIVED) 451a0292f23SGarrett Wollman flags &= ~TH_SYN; 452281a0fd4SPatrick Kelsey /* 453281a0fd4SPatrick Kelsey * When sending additional segments following a TFO SYN|ACK, 454281a0fd4SPatrick Kelsey * do not include the SYN bit. 455281a0fd4SPatrick Kelsey */ 45668bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 457281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 458281a0fd4SPatrick Kelsey flags &= ~TH_SYN; 459a0292f23SGarrett Wollman off--, len++; 460a0292f23SGarrett Wollman } 461a0292f23SGarrett Wollman 46281165e48SAndras Olah /* 463c94c54e4SAndre Oppermann * Be careful not to send data and/or FIN on SYN segments. 46481165e48SAndras Olah * This measure is needed to prevent interoperability problems 46581165e48SAndras Olah * with not fully conformant TCP implementations. 46681165e48SAndras Olah */ 467c94c54e4SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 46881165e48SAndras Olah len = 0; 46981165e48SAndras Olah flags &= ~TH_FIN; 47081165e48SAndras Olah } 47181165e48SAndras Olah 472281a0fd4SPatrick Kelsey /* 473c560df6fSPatrick Kelsey * On TFO sockets, ensure no data is sent in the following cases: 474c560df6fSPatrick Kelsey * 475c560df6fSPatrick Kelsey * - When retransmitting SYN|ACK on a passively-created socket 476c560df6fSPatrick Kelsey * 477c560df6fSPatrick Kelsey * - When retransmitting SYN on an actively created socket 478c560df6fSPatrick Kelsey * 479c560df6fSPatrick Kelsey * - When sending a zero-length cookie (cookie request) on an 480c560df6fSPatrick Kelsey * actively created socket 481c560df6fSPatrick Kelsey * 482c560df6fSPatrick Kelsey * - When the socket is in the CLOSED state (RST is being sent) 483281a0fd4SPatrick Kelsey */ 48468bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 485c560df6fSPatrick Kelsey (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 486c560df6fSPatrick Kelsey ((tp->t_state == TCPS_SYN_SENT) && 487c560df6fSPatrick Kelsey (tp->t_tfo_client_cookie_len == 0)) || 488281a0fd4SPatrick Kelsey (flags & TH_RST))) 489281a0fd4SPatrick Kelsey len = 0; 49047a8e865SXin LI if (len <= 0) { 491df8bae1dSRodney W. Grimes /* 492df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 493df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 49428257b5cSMatthew Dillon * len will be < 0. Otherwise, window shrank 495df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 4962d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 4972d8266afSDavid Greenman * to (closed) window, and set the persist timer 4982d8266afSDavid Greenman * if it isn't already going. If the window didn't 4992d8266afSDavid Greenman * close completely, just wait for an ACK. 50047a8e865SXin LI * 50147a8e865SXin LI * We also do a general check here to ensure that 50247a8e865SXin LI * we will set the persist timer when we have data 50347a8e865SXin LI * to send, but a 0-byte window. This makes sure 50447a8e865SXin LI * the persist timer is set even if the packet 50547a8e865SXin LI * hits one of the "goto send" lines below. 506df8bae1dSRodney W. Grimes */ 507df8bae1dSRodney W. Grimes len = 0; 50847a8e865SXin LI if ((sendwin == 0) && (TCPS_HAVEESTABLISHED(tp->t_state)) && 50947a8e865SXin LI (off < (int) sbavail(&so->so_snd))) { 510b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 5112d8266afSDavid Greenman tp->t_rxtshift = 0; 512df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 513b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_PERSIST)) 5142d8266afSDavid Greenman tcp_setpersist(tp); 515df8bae1dSRodney W. Grimes } 516df8bae1dSRodney W. Grimes } 51728257b5cSMatthew Dillon 5186741ecf5SAndre Oppermann /* len will be >= 0 after this point. */ 519c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 5206741ecf5SAndre Oppermann 52166492feaSGleb Smirnoff tcp_sndbuf_autoscale(tp, so, sendwin); 5226741ecf5SAndre Oppermann 5236741ecf5SAndre Oppermann /* 524ed420311SAndre Oppermann * Decide if we can use TCP Segmentation Offloading (if supported by 525ed420311SAndre Oppermann * hardware). 526b3c0f300SAndre Oppermann * 527b3c0f300SAndre Oppermann * TSO may only be used if we are in a pure bulk sending state. The 528b3c0f300SAndre Oppermann * presence of TCP-MD5, SACK retransmits, SACK advertizements and 529b3c0f300SAndre Oppermann * IP options prevent using TSO. With TSO the TCP header is the same 530b3c0f300SAndre Oppermann * (except for the sequence number) for all generated packets. This 531b3c0f300SAndre Oppermann * makes it impossible to transmit any options which vary per generated 532b3c0f300SAndre Oppermann * segment or packet. 533b6ff6724SHiren Panchasara * 534b6ff6724SHiren Panchasara * IPv4 handling has a clear separation of ip options and ip header 535b6ff6724SHiren Panchasara * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does 536b6ff6724SHiren Panchasara * the right thing below to provide length of just ip options and thus 537b6ff6724SHiren Panchasara * checking for ipoptlen is enough to decide if ip options are present. 53828257b5cSMatthew Dillon */ 539fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 5409ad0173dSBjoern A. Zeeb /* 5419ad0173dSBjoern A. Zeeb * Pre-calculate here as we save another lookup into the darknesses 5429ad0173dSBjoern A. Zeeb * of IPsec that way and can actually decide if TSO is ok. 5439ad0173dSBjoern A. Zeeb */ 544fcf59617SAndrey V. Elsukov #ifdef INET6 545fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6)) 546fcf59617SAndrey V. Elsukov ipsec_optlen = IPSEC_HDRSIZE(ipv6, tp->t_inpcb); 547fcf59617SAndrey V. Elsukov #ifdef INET 548fcf59617SAndrey V. Elsukov else 5499ad0173dSBjoern A. Zeeb #endif 550fcf59617SAndrey V. Elsukov #endif /* INET6 */ 551fcf59617SAndrey V. Elsukov #ifdef INET 552fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) 553fcf59617SAndrey V. Elsukov ipsec_optlen = IPSEC_HDRSIZE(ipv4, tp->t_inpcb); 554fcf59617SAndrey V. Elsukov #endif /* INET */ 555fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 556b6ff6724SHiren Panchasara #ifdef INET6 557b6ff6724SHiren Panchasara if (isipv6) 558b6ff6724SHiren Panchasara ipoptlen = ip6_optlen(tp->t_inpcb); 559b6ff6724SHiren Panchasara else 560b6ff6724SHiren Panchasara #endif 561b6ff6724SHiren Panchasara if (tp->t_inpcb->inp_options) 562b6ff6724SHiren Panchasara ipoptlen = tp->t_inpcb->inp_options->m_len - 563b6ff6724SHiren Panchasara offsetof(struct ipoption, ipopt_list); 564b6ff6724SHiren Panchasara else 565b6ff6724SHiren Panchasara ipoptlen = 0; 566fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 567b6ff6724SHiren Panchasara ipoptlen += ipsec_optlen; 568b6ff6724SHiren Panchasara #endif 569b6ff6724SHiren Panchasara 570ed420311SAndre Oppermann if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg && 5719e644c23SMichael Tuexen (tp->t_port == 0) && 572b3c0f300SAndre Oppermann ((tp->t_flags & TF_SIGNATURE) == 0) && 573b3c0f300SAndre Oppermann tp->rcv_numsacks == 0 && sack_rxmit == 0 && 574c560df6fSPatrick Kelsey ipoptlen == 0 && !(flags & TH_SYN)) 575b3c0f300SAndre Oppermann tso = 1; 576153e5b57SAndre Oppermann 5775d3b1b75SJayanth Vijayaraghavan if (sack_rxmit) { 578cfa6009eSGleb Smirnoff if (SEQ_LT(p->rxmit + len, tp->snd_una + sbused(&so->so_snd))) 579df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 5805d3b1b75SJayanth Vijayaraghavan } else { 581cfa6009eSGleb Smirnoff if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + 582cfa6009eSGleb Smirnoff sbused(&so->so_snd))) 5835d3b1b75SJayanth Vijayaraghavan flags &= ~TH_FIN; 5845d3b1b75SJayanth Vijayaraghavan } 585df8bae1dSRodney W. Grimes 5863ac12506SJonathan T. Looney recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 5873ac12506SJonathan T. Looney (long)TCP_MAXWIN << tp->rcv_scale); 588df8bae1dSRodney W. Grimes 589df8bae1dSRodney W. Grimes /* 590262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 591262c1c1aSMatthew Dillon * conditions when len is non-zero: 592262c1c1aSMatthew Dillon * 593b3c0f300SAndre Oppermann * - We have a full segment (or more with TSO) 594262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 595262c1c1aSMatthew Dillon * either idle or running NODELAY 596262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 597262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 598262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 599262c1c1aSMatthew Dillon * - we need to retransmit 600df8bae1dSRodney W. Grimes */ 601df8bae1dSRodney W. Grimes if (len) { 602b3c0f300SAndre Oppermann if (len >= tp->t_maxseg) 603df8bae1dSRodney W. Grimes goto send; 604262c1c1aSMatthew Dillon /* 605e3995661SRichard Scheffenegger * As the TCP header options are now 606e3995661SRichard Scheffenegger * considered when setting up the initial 607e3995661SRichard Scheffenegger * window, we would not send the last segment 608e3995661SRichard Scheffenegger * if we skip considering the option length here. 609e3995661SRichard Scheffenegger * Note: this may not work when tcp headers change 610e3995661SRichard Scheffenegger * very dynamically in the future. 611e3995661SRichard Scheffenegger */ 612e3995661SRichard Scheffenegger if ((((tp->t_flags & TF_SIGNATURE) ? 613e3995661SRichard Scheffenegger PADTCPOLEN(TCPOLEN_SIGNATURE) : 0) + 614e3995661SRichard Scheffenegger ((tp->t_flags & TF_RCVD_TSTMP) ? 615e3995661SRichard Scheffenegger PADTCPOLEN(TCPOLEN_TIMESTAMP) : 0) + 616e3995661SRichard Scheffenegger len) >= tp->t_maxseg) 617e3995661SRichard Scheffenegger goto send; 618e3995661SRichard Scheffenegger /* 619262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 620262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 621262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 622262c1c1aSMatthew Dillon * 623262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 624262c1c1aSMatthew Dillon */ 625262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 626262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 6273ac12506SJonathan T. Looney (uint32_t)len + (uint32_t)off >= sbavail(&so->so_snd) && 628262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 629df8bae1dSRodney W. Grimes goto send; 630262c1c1aSMatthew Dillon } 6312cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */ 632df8bae1dSRodney W. Grimes goto send; 633a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 634df8bae1dSRodney W. Grimes goto send; 635262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 636df8bae1dSRodney W. Grimes goto send; 6376d90faf3SPaul Saab if (sack_rxmit) 6386d90faf3SPaul Saab goto send; 639df8bae1dSRodney W. Grimes } 640df8bae1dSRodney W. Grimes 641df8bae1dSRodney W. Grimes /* 642f62563d3SAndre Oppermann * Sending of standalone window updates. 643f62563d3SAndre Oppermann * 64478f59b4bSAndre Oppermann * Window updates are important when we close our window due to a 64578f59b4bSAndre Oppermann * full socket buffer and are opening it again after the application 646f62563d3SAndre Oppermann * reads data from it. Once the window has opened again and the 647f62563d3SAndre Oppermann * remote end starts to send again the ACK clock takes over and 648f62563d3SAndre Oppermann * provides the most current window information. 649f62563d3SAndre Oppermann * 65078f59b4bSAndre Oppermann * We must avoid the silly window syndrome whereas every read 651f62563d3SAndre Oppermann * from the receive buffer, no matter how small, causes a window 652f62563d3SAndre Oppermann * update to be sent. We also should avoid sending a flurry of 653f62563d3SAndre Oppermann * window updates when the socket buffer had queued a lot of data 654f62563d3SAndre Oppermann * and the application is doing small reads. 655f62563d3SAndre Oppermann * 656f62563d3SAndre Oppermann * Prevent a flurry of pointless window updates by only sending 657f62563d3SAndre Oppermann * an update when we can increase the advertized window by more 658f62563d3SAndre Oppermann * than 1/4th of the socket buffer capacity. When the buffer is 659f62563d3SAndre Oppermann * getting full or is very small be more aggressive and send an 660f62563d3SAndre Oppermann * update whenever we can increase by two mss sized segments. 661f62563d3SAndre Oppermann * In all other situations the ACK's to new incoming data will 662f62563d3SAndre Oppermann * carry further window increases. 6634249614cSAndre Oppermann * 6644249614cSAndre Oppermann * Don't send an independent window update if a delayed 6654249614cSAndre Oppermann * ACK is pending (it will get piggy-backed on it) or the 6664249614cSAndre Oppermann * remote side already has done a half-close and won't send 667f62563d3SAndre Oppermann * more data. Skip this if the connection is in T/TCP 668f62563d3SAndre Oppermann * half-open state. 669df8bae1dSRodney W. Grimes */ 670b7de7d87SAndre Oppermann if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 6714249614cSAndre Oppermann !(tp->t_flags & TF_DELACK) && 672b7de7d87SAndre Oppermann !TCPS_HAVERCVDFIN(tp->t_state)) { 673df8bae1dSRodney W. Grimes /* 674f62563d3SAndre Oppermann * "adv" is the amount we could increase the window, 675df8bae1dSRodney W. Grimes * taking into account that we are limited by 676df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 677df8bae1dSRodney W. Grimes */ 6783ac12506SJonathan T. Looney int32_t adv; 679f701e30dSJohn Baldwin int oldwin; 680f701e30dSJohn Baldwin 6813ac12506SJonathan T. Looney adv = recwin; 682f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 683f701e30dSJohn Baldwin oldwin = (tp->rcv_adv - tp->rcv_nxt); 684f092a3c7SRandall Stewart if (adv > oldwin) 685f701e30dSJohn Baldwin adv -= oldwin; 686f092a3c7SRandall Stewart else 687f092a3c7SRandall Stewart adv = 0; 688f701e30dSJohn Baldwin } else 689f701e30dSJohn Baldwin oldwin = 0; 690df8bae1dSRodney W. Grimes 691da84b2e6SJohn Baldwin /* 6920dda76b8SJonathan T. Looney * If the new window size ends up being the same as or less 6930dda76b8SJonathan T. Looney * than the old size when it is scaled, then don't force 6940dda76b8SJonathan T. Looney * a window update. 695da84b2e6SJohn Baldwin */ 6960dda76b8SJonathan T. Looney if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 697da84b2e6SJohn Baldwin goto dontupdate; 698f62563d3SAndre Oppermann 6993ac12506SJonathan T. Looney if (adv >= (int32_t)(2 * tp->t_maxseg) && 7003ac12506SJonathan T. Looney (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) || 7013ac12506SJonathan T. Looney recwin <= (so->so_rcv.sb_hiwat / 8) || 70250075939SStephen Hurd so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg || 70350075939SStephen Hurd adv >= TCP_MAXWIN << tp->rcv_scale)) 704df8bae1dSRodney W. Grimes goto send; 7058d62aae8SMichael Tuexen if (2 * adv >= (int32_t)so->so_rcv.sb_hiwat) 7068d62aae8SMichael Tuexen goto send; 707df8bae1dSRodney W. Grimes } 708da84b2e6SJohn Baldwin dontupdate: 709df8bae1dSRodney W. Grimes 710df8bae1dSRodney W. Grimes /* 71128257b5cSMatthew Dillon * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 71228257b5cSMatthew Dillon * is also a catch-all for the retransmit timer timeout case. 713df8bae1dSRodney W. Grimes */ 714df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 715df8bae1dSRodney W. Grimes goto send; 716a0292f23SGarrett Wollman if ((flags & TH_RST) || 717a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 718df8bae1dSRodney W. Grimes goto send; 719df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 720df8bae1dSRodney W. Grimes goto send; 721df8bae1dSRodney W. Grimes /* 722df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 72328257b5cSMatthew Dillon * and we have not yet done so, then we need to send. 724df8bae1dSRodney W. Grimes */ 725df8bae1dSRodney W. Grimes if (flags & TH_FIN && 726df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 727df8bae1dSRodney W. Grimes goto send; 7286d90faf3SPaul Saab /* 7296d90faf3SPaul Saab * In SACK, it is possible for tcp_output to fail to send a segment 7306d90faf3SPaul Saab * after the retransmission timer has been turned off. Make sure 7316d90faf3SPaul Saab * that the retransmission timer is set. 7326d90faf3SPaul Saab */ 7333529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 7343529149eSAndre Oppermann SEQ_GT(tp->snd_max, tp->snd_una) && 735b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_REXMT) && 736b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 737b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 738cf2942b6SRobert Watson goto just_return; 7396d90faf3SPaul Saab } 740df8bae1dSRodney W. Grimes /* 741df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 742df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 743df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 744df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 745df8bae1dSRodney W. Grimes * persisting to move a small or zero window 746df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 747df8bae1dSRodney W. Grimes * 748b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_PERSIST) 7499b8b58e0SJonathan Lemon * is true when we are in persist state. 7502cdbfa66SPaul Saab * (tp->t_flags & TF_FORCEDATA) 751df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 752b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_REXMT) 753df8bae1dSRodney W. Grimes * is set when we are retransmitting 754df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 755df8bae1dSRodney W. Grimes * 756df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 757df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 758df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 759df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 760df8bae1dSRodney W. Grimes * otherwise force out a byte. 761df8bae1dSRodney W. Grimes */ 762cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd) && !tcp_timer_active(tp, TT_REXMT) && 763b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 764df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 765df8bae1dSRodney W. Grimes tcp_setpersist(tp); 766df8bae1dSRodney W. Grimes } 767df8bae1dSRodney W. Grimes 768df8bae1dSRodney W. Grimes /* 769df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 770df8bae1dSRodney W. Grimes */ 771cf2942b6SRobert Watson just_return: 772cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 773df8bae1dSRodney W. Grimes return (0); 774df8bae1dSRodney W. Grimes 775df8bae1dSRodney W. Grimes send: 776cf2942b6SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 777f6f6703fSSean Bruno if (len > 0) { 778f6f6703fSSean Bruno if (len >= tp->t_maxseg) 779f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 780f6f6703fSSean Bruno else 781f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 782f6f6703fSSean Bruno } 783df8bae1dSRodney W. Grimes /* 784df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 785df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 786df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 787df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 788df8bae1dSRodney W. Grimes * link header, i.e. 78933841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 790df8bae1dSRodney W. Grimes */ 791df8bae1dSRodney W. Grimes optlen = 0; 792fb59c426SYoshinobu Inoue #ifdef INET6 793fb59c426SYoshinobu Inoue if (isipv6) 794fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 795fb59c426SYoshinobu Inoue else 796fb59c426SYoshinobu Inoue #endif 797df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 79802a1a643SAndre Oppermann 799ed782b9fSMichael Tuexen if (flags & TH_SYN) { 800ed782b9fSMichael Tuexen tp->snd_nxt = tp->iss; 801ed782b9fSMichael Tuexen } 802ed782b9fSMichael Tuexen 80302a1a643SAndre Oppermann /* 80402a1a643SAndre Oppermann * Compute options for segment. 80502a1a643SAndre Oppermann * We only have to care about SYN and established connection 80602a1a643SAndre Oppermann * segments. Options for SYN-ACK segments are handled in TCP 80702a1a643SAndre Oppermann * syncache. 80802a1a643SAndre Oppermann */ 80902a1a643SAndre Oppermann to.to_flags = 0; 810f73d9fd2SGleb Smirnoff if ((tp->t_flags & TF_NOOPT) == 0) { 81102a1a643SAndre Oppermann /* Maximum segment size. */ 812df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 81302a1a643SAndre Oppermann to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc); 8149e644c23SMichael Tuexen if (tp->t_port) 8159e644c23SMichael Tuexen to.to_mss -= V_tcp_udp_tunneling_overhead; 81602a1a643SAndre Oppermann to.to_flags |= TOF_MSS; 81718a75309SPatrick Kelsey 818281a0fd4SPatrick Kelsey /* 819c560df6fSPatrick Kelsey * On SYN or SYN|ACK transmits on TFO connections, 820c560df6fSPatrick Kelsey * only include the TFO option if it is not a 821c560df6fSPatrick Kelsey * retransmit, as the presence of the TFO option may 822c560df6fSPatrick Kelsey * have caused the original SYN or SYN|ACK to have 823c560df6fSPatrick Kelsey * been dropped by a middlebox. 824281a0fd4SPatrick Kelsey */ 82568bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 826281a0fd4SPatrick Kelsey (tp->t_rxtshift == 0)) { 827c560df6fSPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED) { 828281a0fd4SPatrick Kelsey to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 829c560df6fSPatrick Kelsey to.to_tfo_cookie = 830c560df6fSPatrick Kelsey (u_int8_t *)&tp->t_tfo_cookie.server; 831281a0fd4SPatrick Kelsey to.to_flags |= TOF_FASTOPEN; 832c560df6fSPatrick Kelsey wanted_cookie = 1; 833c560df6fSPatrick Kelsey } else if (tp->t_state == TCPS_SYN_SENT) { 834c560df6fSPatrick Kelsey to.to_tfo_len = 835c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len; 836c560df6fSPatrick Kelsey to.to_tfo_cookie = 837c560df6fSPatrick Kelsey tp->t_tfo_cookie.client; 838c560df6fSPatrick Kelsey to.to_flags |= TOF_FASTOPEN; 839c560df6fSPatrick Kelsey wanted_cookie = 1; 840c560df6fSPatrick Kelsey /* 841c560df6fSPatrick Kelsey * If we wind up having more data to 842c560df6fSPatrick Kelsey * send with the SYN than can fit in 843c560df6fSPatrick Kelsey * one segment, don't send any more 844c560df6fSPatrick Kelsey * until the SYN|ACK comes back from 845c560df6fSPatrick Kelsey * the other end. 846c560df6fSPatrick Kelsey */ 847c560df6fSPatrick Kelsey dont_sendalot = 1; 848c560df6fSPatrick Kelsey } 849281a0fd4SPatrick Kelsey } 850df8bae1dSRodney W. Grimes } 85102a1a643SAndre Oppermann /* Window scaling. */ 85202a1a643SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 85302a1a643SAndre Oppermann to.to_wscale = tp->request_r_scale; 85402a1a643SAndre Oppermann to.to_flags |= TOF_SCALE; 855df8bae1dSRodney W. Grimes } 85602a1a643SAndre Oppermann /* Timestamps. */ 85702a1a643SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) || 85802a1a643SAndre Oppermann ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 85910d20c84SMatt Macy curticks = tcp_ts_getticks(); 86010d20c84SMatt Macy to.to_tsval = curticks + tp->ts_offset; 86102a1a643SAndre Oppermann to.to_tsecr = tp->ts_recent; 86202a1a643SAndre Oppermann to.to_flags |= TOF_TS; 86310d20c84SMatt Macy if (tp->t_rxtshift == 1) 86410d20c84SMatt Macy tp->t_badrxtwin = curticks; 865e44c1887SSteven Hartland } 866e44c1887SSteven Hartland 8676741ecf5SAndre Oppermann /* Set receive buffer autosizing timestamp. */ 86802a1a643SAndre Oppermann if (tp->rfbuf_ts == 0 && 86902a1a643SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) 870d8951c8aSBjoern A. Zeeb tp->rfbuf_ts = tcp_ts_getticks(); 871e44c1887SSteven Hartland 87202a1a643SAndre Oppermann /* Selective ACK's. */ 8733529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 87402a1a643SAndre Oppermann if (flags & TH_SYN) 87502a1a643SAndre Oppermann to.to_flags |= TOF_SACKPERM; 87602a1a643SAndre Oppermann else if (TCPS_HAVEESTABLISHED(tp->t_state) && 87702a1a643SAndre Oppermann tp->rcv_numsacks > 0) { 87802a1a643SAndre Oppermann to.to_flags |= TOF_SACK; 87902a1a643SAndre Oppermann to.to_nsacks = tp->rcv_numsacks; 88002a1a643SAndre Oppermann to.to_sacks = (u_char *)tp->sackblks; 88102a1a643SAndre Oppermann } 88202a1a643SAndre Oppermann } 883fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 88402a1a643SAndre Oppermann /* TCP-MD5 (RFC2385). */ 885fcf59617SAndrey V. Elsukov /* 886fcf59617SAndrey V. Elsukov * Check that TCP_MD5SIG is enabled in tcpcb to 887fcf59617SAndrey V. Elsukov * account the size needed to set this TCP option. 888fcf59617SAndrey V. Elsukov */ 88902a1a643SAndre Oppermann if (tp->t_flags & TF_SIGNATURE) 89002a1a643SAndre Oppermann to.to_flags |= TOF_SIGNATURE; 8911cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 8921cfd4b53SBruce M Simpson 89302a1a643SAndre Oppermann /* Processing the options. */ 8944a411b9fSBjoern A. Zeeb hdrlen += optlen = tcp_addoptions(&to, opt); 895c560df6fSPatrick Kelsey /* 896c560df6fSPatrick Kelsey * If we wanted a TFO option to be added, but it was unable 897c560df6fSPatrick Kelsey * to fit, ensure no data is sent. 898c560df6fSPatrick Kelsey */ 899c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && wanted_cookie && 900c560df6fSPatrick Kelsey !(to.to_flags & TOF_FASTOPEN)) 901c560df6fSPatrick Kelsey len = 0; 902be3f3b5eSPaul Saab } 9039e644c23SMichael Tuexen if (tp->t_port) { 9049e644c23SMichael Tuexen if (V_tcp_udp_tunneling_port == 0) { 9059e644c23SMichael Tuexen /* The port was removed?? */ 9069e644c23SMichael Tuexen SOCKBUF_UNLOCK(&so->so_snd); 9079e644c23SMichael Tuexen return (EHOSTUNREACH); 9089e644c23SMichael Tuexen } 9099e644c23SMichael Tuexen hdrlen += sizeof(struct udphdr); 9109e644c23SMichael Tuexen } 911df8bae1dSRodney W. Grimes /* 912df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 9130c39d38dSGleb Smirnoff * bump the packet length beyond the t_maxseg length. 914a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 915a0292f23SGarrett Wollman * the segment. 916df8bae1dSRodney W. Grimes */ 9170c39d38dSGleb Smirnoff if (len + optlen + ipoptlen > tp->t_maxseg) { 918297a37f3SDavid Greenman flags &= ~TH_FIN; 919ed420311SAndre Oppermann 920b3c0f300SAndre Oppermann if (tso) { 9219fd573c3SHans Petter Selasky u_int if_hw_tsomax; 9229fd573c3SHans Petter Selasky u_int moff; 9239fd573c3SHans Petter Selasky int max_len; 9249fd573c3SHans Petter Selasky 9259fd573c3SHans Petter Selasky /* extract TSO information */ 9269fd573c3SHans Petter Selasky if_hw_tsomax = tp->t_tsomax; 9279fd573c3SHans Petter Selasky if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 9289fd573c3SHans Petter Selasky if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 9299fd573c3SHans Petter Selasky 9309fd573c3SHans Petter Selasky /* 9319fd573c3SHans Petter Selasky * Limit a TSO burst to prevent it from 9329fd573c3SHans Petter Selasky * overflowing or exceeding the maximum length 9339fd573c3SHans Petter Selasky * allowed by the network interface: 9349fd573c3SHans Petter Selasky */ 935ed420311SAndre Oppermann KASSERT(ipoptlen == 0, 936ed420311SAndre Oppermann ("%s: TSO can't do IP options", __func__)); 937ed420311SAndre Oppermann 938ed420311SAndre Oppermann /* 9399fd573c3SHans Petter Selasky * Check if we should limit by maximum payload 9409fd573c3SHans Petter Selasky * length: 941ed420311SAndre Oppermann */ 9429fd573c3SHans Petter Selasky if (if_hw_tsomax != 0) { 9439fd573c3SHans Petter Selasky /* compute maximum TSO length */ 944d76d4012SHans Petter Selasky max_len = (if_hw_tsomax - hdrlen - 945d76d4012SHans Petter Selasky max_linkhdr); 9469fd573c3SHans Petter Selasky if (max_len <= 0) { 9479fd573c3SHans Petter Selasky len = 0; 9483c7c188cSHans Petter Selasky } else if (len > max_len) { 949b3c0f300SAndre Oppermann sendalot = 1; 9503c7c188cSHans Petter Selasky len = max_len; 9519fd573c3SHans Petter Selasky } 9529fd573c3SHans Petter Selasky } 95374e10fb6SJohn Baldwin 954ed420311SAndre Oppermann /* 955ed420311SAndre Oppermann * Prevent the last segment from being 9569fd573c3SHans Petter Selasky * fractional unless the send sockbuf can be 9579fd573c3SHans Petter Selasky * emptied: 958ed420311SAndre Oppermann */ 9590c39d38dSGleb Smirnoff max_len = (tp->t_maxseg - optlen); 9603ac12506SJonathan T. Looney if (((uint32_t)off + (uint32_t)len) < 9613ac12506SJonathan T. Looney sbavail(&so->so_snd)) { 9623c7c188cSHans Petter Selasky moff = len % max_len; 9639fd573c3SHans Petter Selasky if (moff != 0) { 9649fd573c3SHans Petter Selasky len -= moff; 965b3c0f300SAndre Oppermann sendalot = 1; 966ed420311SAndre Oppermann } 9679fd573c3SHans Petter Selasky } 9689fd573c3SHans Petter Selasky 9699fd573c3SHans Petter Selasky /* 9709fd573c3SHans Petter Selasky * In case there are too many small fragments 9719fd573c3SHans Petter Selasky * don't use TSO: 9729fd573c3SHans Petter Selasky */ 9733c7c188cSHans Petter Selasky if (len <= max_len) { 9743c7c188cSHans Petter Selasky len = max_len; 9759fd573c3SHans Petter Selasky sendalot = 1; 9769fd573c3SHans Petter Selasky tso = 0; 9779fd573c3SHans Petter Selasky } 978ed420311SAndre Oppermann 979ed420311SAndre Oppermann /* 980ed420311SAndre Oppermann * Send the FIN in a separate segment 981ed420311SAndre Oppermann * after the bulk sending is done. 982ed420311SAndre Oppermann * We don't trust the TSO implementations 983ed420311SAndre Oppermann * to clear the FIN flag on all but the 984ed420311SAndre Oppermann * last segment. 985ed420311SAndre Oppermann */ 986ed420311SAndre Oppermann if (tp->t_flags & TF_NEEDFIN) 987ed420311SAndre Oppermann sendalot = 1; 988b3c0f300SAndre Oppermann } else { 98912a43d0dSMichael Tuexen if (optlen + ipoptlen >= tp->t_maxseg) { 99012a43d0dSMichael Tuexen /* 99112a43d0dSMichael Tuexen * Since we don't have enough space to put 99212a43d0dSMichael Tuexen * the IP header chain and the TCP header in 99312a43d0dSMichael Tuexen * one packet as required by RFC 7112, don't 99412a43d0dSMichael Tuexen * send it. Also ensure that at least one 99512a43d0dSMichael Tuexen * byte of the payload can be put into the 99612a43d0dSMichael Tuexen * TCP segment. 99712a43d0dSMichael Tuexen */ 99812a43d0dSMichael Tuexen SOCKBUF_UNLOCK(&so->so_snd); 99912a43d0dSMichael Tuexen error = EMSGSIZE; 100012a43d0dSMichael Tuexen sack_rxmit = 0; 100112a43d0dSMichael Tuexen goto out; 100212a43d0dSMichael Tuexen } 10030c39d38dSGleb Smirnoff len = tp->t_maxseg - optlen - ipoptlen; 1004df8bae1dSRodney W. Grimes sendalot = 1; 1005c560df6fSPatrick Kelsey if (dont_sendalot) 1006c560df6fSPatrick Kelsey sendalot = 0; 1007df8bae1dSRodney W. Grimes } 1008ed420311SAndre Oppermann } else 1009ed420311SAndre Oppermann tso = 0; 1010ed420311SAndre Oppermann 1011ed420311SAndre Oppermann KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 1012ed420311SAndre Oppermann ("%s: len > IP_MAXPACKET", __func__)); 1013df8bae1dSRodney W. Grimes 1014a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 1015a683a7ddSYoshinobu Inoue #ifdef INET6 1016a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 1017a683a7ddSYoshinobu Inoue #else 1018df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 1019a683a7ddSYoshinobu Inoue #endif 1020f10e85d7SLuigi Rizzo panic("tcphdr too big"); 1021a0292f23SGarrett Wollman /*#endif*/ 1022df8bae1dSRodney W. Grimes 1023df8bae1dSRodney W. Grimes /* 1024c4982faeSBjoern A. Zeeb * This KASSERT is here to catch edge cases at a well defined place. 1025c4982faeSBjoern A. Zeeb * Before, those had triggered (random) panic conditions further down. 1026c4982faeSBjoern A. Zeeb */ 1027c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 1028c4982faeSBjoern A. Zeeb 1029c4982faeSBjoern A. Zeeb /* 1030df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 1031df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 1032df8bae1dSRodney W. Grimes * the template for sends on this connection. 1033df8bae1dSRodney W. Grimes */ 1034df8bae1dSRodney W. Grimes if (len) { 10354e023759SAndre Oppermann struct mbuf *mb; 1036581a046aSRandall Stewart struct sockbuf *msb; 10374e023759SAndre Oppermann u_int moff; 10384e023759SAndre Oppermann 1039adc56f5aSEdward Tomasz Napierala if ((tp->t_flags & TF_FORCEDATA) && len == 1) { 104078b50714SRobert Watson TCPSTAT_INC(tcps_sndprobe); 1041adc56f5aSEdward Tomasz Napierala #ifdef STATS 1042adc56f5aSEdward Tomasz Napierala if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1043adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, 1044adc56f5aSEdward Tomasz Napierala VOI_TCP_RETXPB, len); 1045adc56f5aSEdward Tomasz Napierala else 1046adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u64(tp->t_stats, 1047adc56f5aSEdward Tomasz Napierala VOI_TCP_TXPB, len); 1048adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 1049adc56f5aSEdward Tomasz Napierala } else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) { 1050f5d34df5SGeorge V. Neville-Neil tp->t_sndrexmitpack++; 105178b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitpack); 105278b50714SRobert Watson TCPSTAT_ADD(tcps_sndrexmitbyte, len); 1053adc56f5aSEdward Tomasz Napierala #ifdef STATS 1054adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 1055adc56f5aSEdward Tomasz Napierala len); 1056adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 1057df8bae1dSRodney W. Grimes } else { 105878b50714SRobert Watson TCPSTAT_INC(tcps_sndpack); 105978b50714SRobert Watson TCPSTAT_ADD(tcps_sndbyte, len); 1060adc56f5aSEdward Tomasz Napierala #ifdef STATS 1061adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 1062adc56f5aSEdward Tomasz Napierala len); 1063adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 1064df8bae1dSRodney W. Grimes } 106539f6074eSGleb Smirnoff #ifdef INET6 106639f6074eSGleb Smirnoff if (MHLEN < hdrlen + max_linkhdr) 106739f6074eSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 106839f6074eSGleb Smirnoff else 106939f6074eSGleb Smirnoff #endif 107039f6074eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 107139f6074eSGleb Smirnoff 1072df8bae1dSRodney W. Grimes if (m == NULL) { 1073cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1074df8bae1dSRodney W. Grimes error = ENOBUFS; 10750e2bc05cSGleb Smirnoff sack_rxmit = 0; 1076df8bae1dSRodney W. Grimes goto out; 1077df8bae1dSRodney W. Grimes } 107839f6074eSGleb Smirnoff 1079df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1080df8bae1dSRodney W. Grimes m->m_len = hdrlen; 10814e023759SAndre Oppermann 10824e023759SAndre Oppermann /* 10834e023759SAndre Oppermann * Start the m_copy functions from the closest mbuf 10844e023759SAndre Oppermann * to the offset in the socket buffer chain. 10854e023759SAndre Oppermann */ 1086581a046aSRandall Stewart mb = sbsndptr_noadv(&so->so_snd, off, &moff); 1087b2e60773SJohn Baldwin if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 10883ac12506SJonathan T. Looney m_copydata(mb, moff, len, 1089df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 1090581a046aSRandall Stewart if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1091581a046aSRandall Stewart sbsndptr_adv(&so->so_snd, mb, len); 1092df8bae1dSRodney W. Grimes m->m_len += len; 1093df8bae1dSRodney W. Grimes } else { 1094581a046aSRandall Stewart if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1095581a046aSRandall Stewart msb = NULL; 1096581a046aSRandall Stewart else 1097581a046aSRandall Stewart msb = &so->so_snd; 1098581a046aSRandall Stewart m->m_next = tcp_m_copym(mb, moff, 1099581a046aSRandall Stewart &len, if_hw_tsomaxsegcount, 1100b2e60773SJohn Baldwin if_hw_tsomaxsegsize, msb, hw_tls); 1101581a046aSRandall Stewart if (len <= (tp->t_maxseg - optlen)) { 1102581a046aSRandall Stewart /* 1103581a046aSRandall Stewart * Must have ran out of mbufs for the copy 1104581a046aSRandall Stewart * shorten it to no longer need tso. Lets 1105581a046aSRandall Stewart * not put on sendalot since we are low on 1106581a046aSRandall Stewart * mbufs. 1107581a046aSRandall Stewart */ 1108581a046aSRandall Stewart tso = 0; 1109581a046aSRandall Stewart } 11104e023759SAndre Oppermann if (m->m_next == NULL) { 1111cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1112d7f570e6SGarrett Wollman (void) m_free(m); 111351823c3aSGarrett Wollman error = ENOBUFS; 11140e2bc05cSGleb Smirnoff sack_rxmit = 0; 111551823c3aSGarrett Wollman goto out; 111651823c3aSGarrett Wollman } 1117df8bae1dSRodney W. Grimes } 1118472ea5beSColin Percival 1119df8bae1dSRodney W. Grimes /* 1120df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 1121df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 1122df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 1123df8bae1dSRodney W. Grimes * a PUSH comes in.) 1124df8bae1dSRodney W. Grimes */ 11253ac12506SJonathan T. Looney if (((uint32_t)off + (uint32_t)len == sbused(&so->so_snd)) && 11263ac12506SJonathan T. Looney !(flags & TH_SYN)) 1127df8bae1dSRodney W. Grimes flags |= TH_PUSH; 1128cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1129df8bae1dSRodney W. Grimes } else { 1130cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1131df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 113278b50714SRobert Watson TCPSTAT_INC(tcps_sndacks); 1133df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 113478b50714SRobert Watson TCPSTAT_INC(tcps_sndctrl); 1135df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 113678b50714SRobert Watson TCPSTAT_INC(tcps_sndurg); 1137df8bae1dSRodney W. Grimes else 113878b50714SRobert Watson TCPSTAT_INC(tcps_sndwinup); 1139df8bae1dSRodney W. Grimes 1140aa8bd99dSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 1141df8bae1dSRodney W. Grimes if (m == NULL) { 1142df8bae1dSRodney W. Grimes error = ENOBUFS; 11430e2bc05cSGleb Smirnoff sack_rxmit = 0; 1144df8bae1dSRodney W. Grimes goto out; 1145df8bae1dSRodney W. Grimes } 1146fb59c426SYoshinobu Inoue #ifdef INET6 1147fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 1148fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 1149ed6a66caSRobert Watson M_ALIGN(m, hdrlen); 1150fb59c426SYoshinobu Inoue } else 1151fb59c426SYoshinobu Inoue #endif 1152df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1153df8bae1dSRodney W. Grimes m->m_len = hdrlen; 1154df8bae1dSRodney W. Grimes } 1155cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); 1156df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 1157c488362eSRobert Watson #ifdef MAC 115830d239bcSRobert Watson mac_inpcb_create_mbuf(tp->t_inpcb, m); 1159c488362eSRobert Watson #endif 1160fb59c426SYoshinobu Inoue #ifdef INET6 1161fb59c426SYoshinobu Inoue if (isipv6) { 1162fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 11639e644c23SMichael Tuexen if (tp->t_port) { 1164500eb6ddSMichael Tuexen udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 11659e644c23SMichael Tuexen udp->uh_sport = htons(V_tcp_udp_tunneling_port); 11669e644c23SMichael Tuexen udp->uh_dport = tp->t_port; 11679e644c23SMichael Tuexen ulen = hdrlen + len - sizeof(struct ip6_hdr); 11689e644c23SMichael Tuexen udp->uh_ulen = htons(ulen); 11699e644c23SMichael Tuexen th = (struct tcphdr *)(udp + 1); 11709e644c23SMichael Tuexen } else { 1171fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 11729e644c23SMichael Tuexen } 11739e644c23SMichael Tuexen tcpip_fillheaders(tp->t_inpcb, tp->t_port, ip6, th); 1174fb59c426SYoshinobu Inoue } else 1175fb59c426SYoshinobu Inoue #endif /* INET6 */ 1176fb59c426SYoshinobu Inoue { 1177fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 1178151ba793SAlexander Kabaev #ifdef TCPDEBUG 1179fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 1180151ba793SAlexander Kabaev #endif 11819e644c23SMichael Tuexen if (tp->t_port) { 1182500eb6ddSMichael Tuexen udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 11839e644c23SMichael Tuexen udp->uh_sport = htons(V_tcp_udp_tunneling_port); 11849e644c23SMichael Tuexen udp->uh_dport = tp->t_port; 11859e644c23SMichael Tuexen ulen = hdrlen + len - sizeof(struct ip); 11869e644c23SMichael Tuexen udp->uh_ulen = htons(ulen); 11879e644c23SMichael Tuexen th = (struct tcphdr *)(udp + 1); 11889e644c23SMichael Tuexen } else 1189fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 11909e644c23SMichael Tuexen tcpip_fillheaders(tp->t_inpcb, tp->t_port, ip, th); 1191fb59c426SYoshinobu Inoue } 1192df8bae1dSRodney W. Grimes 1193df8bae1dSRodney W. Grimes /* 1194df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 1195df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 1196df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 1197df8bae1dSRodney W. Grimes */ 1198df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 1199df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 1200df8bae1dSRodney W. Grimes tp->snd_nxt--; 1201df8bae1dSRodney W. Grimes /* 1202f2512ba1SRui Paulo * If we are starting a connection, send ECN setup 1203f2512ba1SRui Paulo * SYN packet. If we are on a retransmit, we may 1204f2512ba1SRui Paulo * resend those bits a number of times as per 1205f2512ba1SRui Paulo * RFC 3168. 1206f2512ba1SRui Paulo */ 1207f7220c48SRichard Scheffenegger if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) { 1208f7220c48SRichard Scheffenegger flags |= tcp_ecn_output_syn_sent(tp); 1209f2512ba1SRui Paulo } 1210f7220c48SRichard Scheffenegger /* Also handle parallel SYN for ECN */ 1211f7220c48SRichard Scheffenegger if ((TCPS_HAVERCVDSYN(tp->t_state)) && 12124012ef77SRichard Scheffenegger (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) { 12132ff07d92SRichard Scheffenegger int ect = tcp_ecn_output_established(tp, &flags, len, sack_rxmit); 1214f7220c48SRichard Scheffenegger if ((tp->t_state == TCPS_SYN_RECEIVED) && 1215f7220c48SRichard Scheffenegger (tp->t_flags2 & TF2_ECN_SND_ECE)) 1216f7220c48SRichard Scheffenegger tp->t_flags2 &= ~TF2_ECN_SND_ECE; 1217f2512ba1SRui Paulo #ifdef INET6 1218f026275eSRichard Scheffenegger if (isipv6) { 1219f026275eSRichard Scheffenegger ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20); 1220f7220c48SRichard Scheffenegger ip6->ip6_flow |= htonl(ect << 20); 1221f026275eSRichard Scheffenegger } 1222f2512ba1SRui Paulo else 1223f2512ba1SRui Paulo #endif 1224f026275eSRichard Scheffenegger { 1225f026275eSRichard Scheffenegger ip->ip_tos &= ~IPTOS_ECN_MASK; 1226f7220c48SRichard Scheffenegger ip->ip_tos |= ect; 1227f026275eSRichard Scheffenegger } 1228f2512ba1SRui Paulo } 1229f2512ba1SRui Paulo 1230f2512ba1SRui Paulo /* 1231df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 1232df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 1233df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 1234df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 1235df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 1236df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 1237df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 1238df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 1239df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 1240df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 1241df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 1242df8bae1dSRodney W. Grimes */ 1243a55db2b6SPaul Saab if (sack_rxmit == 0) { 1244b8152ba7SAndre Oppermann if (len || (flags & (TH_SYN|TH_FIN)) || 1245b8152ba7SAndre Oppermann tcp_timer_active(tp, TT_PERSIST)) 1246fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 1247df8bae1dSRodney W. Grimes else 1248fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 1249a55db2b6SPaul Saab } else { 12506d90faf3SPaul Saab th->th_seq = htonl(p->rxmit); 12516d90faf3SPaul Saab p->rxmit += len; 12520471a8c7SRichard Scheffenegger /* 12530471a8c7SRichard Scheffenegger * Lost Retransmission Detection 12540471a8c7SRichard Scheffenegger * trigger resending of a (then 12550471a8c7SRichard Scheffenegger * still existing) hole, when 12560471a8c7SRichard Scheffenegger * fack acks recoverypoint. 12570471a8c7SRichard Scheffenegger */ 12580471a8c7SRichard Scheffenegger if ((tp->t_flags & TF_LRD) && SEQ_GEQ(p->rxmit, p->end)) 12590471a8c7SRichard Scheffenegger p->rxmit = tp->snd_recover; 12600077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += len; 12616d90faf3SPaul Saab } 1262e5313869SRichard Scheffenegger if (IN_RECOVERY(tp->t_flags)) { 1263e5313869SRichard Scheffenegger /* 1264e5313869SRichard Scheffenegger * Account all bytes transmitted while 1265e5313869SRichard Scheffenegger * IN_RECOVERY, simplifying PRR and 1266e5313869SRichard Scheffenegger * Lost Retransmit Detection 1267e5313869SRichard Scheffenegger */ 1268e5313869SRichard Scheffenegger tp->sackhint.prr_out += len; 1269e5313869SRichard Scheffenegger } 1270fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 1271df8bae1dSRodney W. Grimes if (optlen) { 1272fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 1273fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 1274df8bae1dSRodney W. Grimes } 12751ebf4607SRichard Scheffenegger tcp_set_flags(th, flags); 1276df8bae1dSRodney W. Grimes /* 1277df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 1278df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 127979410718SMichael Tuexen * If a RST segment is sent, advertise a window of zero. 1280df8bae1dSRodney W. Grimes */ 128179410718SMichael Tuexen if (flags & TH_RST) { 128279410718SMichael Tuexen recwin = 0; 128379410718SMichael Tuexen } else { 12843ac12506SJonathan T. Looney if (recwin < (so->so_rcv.sb_hiwat / 4) && 12853ac12506SJonathan T. Looney recwin < tp->t_maxseg) 1286201d185bSAndre Oppermann recwin = 0; 1287f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 12883ac12506SJonathan T. Looney recwin < (tp->rcv_adv - tp->rcv_nxt)) 12893ac12506SJonathan T. Looney recwin = (tp->rcv_adv - tp->rcv_nxt); 129079410718SMichael Tuexen } 1291104ebb2aSAndre Oppermann /* 1292104ebb2aSAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1293104ebb2aSAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> 1294104ebb2aSAndre Oppermann * case is handled in syncache. 1295104ebb2aSAndre Oppermann */ 1296104ebb2aSAndre Oppermann if (flags & TH_SYN) 1297104ebb2aSAndre Oppermann th->th_win = htons((u_short) 1298104ebb2aSAndre Oppermann (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 12999028b6e0SRichard Scheffenegger else { 13009028b6e0SRichard Scheffenegger /* Avoid shrinking window with window scaling. */ 13019028b6e0SRichard Scheffenegger recwin = roundup2(recwin, 1 << tp->rcv_scale); 1302104ebb2aSAndre Oppermann th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 13039028b6e0SRichard Scheffenegger } 1304262c1c1aSMatthew Dillon 1305262c1c1aSMatthew Dillon /* 1306262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 1307262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 1308262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 1309262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 1310b2722702SRui Paulo * to read more data than can be buffered prior to transmitting on 1311262c1c1aSMatthew Dillon * the connection. 1312262c1c1aSMatthew Dillon */ 1313f5d34df5SGeorge V. Neville-Neil if (th->th_win == 0) { 1314f5d34df5SGeorge V. Neville-Neil tp->t_sndzerowin++; 1315262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 1316f5d34df5SGeorge V. Neville-Neil } else 1317262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 1318df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 1319fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 1320fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 1321df8bae1dSRodney W. Grimes } else 1322df8bae1dSRodney W. Grimes /* 1323df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 1324df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 1325df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 1326df8bae1dSRodney W. Grimes * number wraparound. 1327df8bae1dSRodney W. Grimes */ 1328df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 1329df8bae1dSRodney W. Grimes 1330df8bae1dSRodney W. Grimes /* 1331df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 1332df8bae1dSRodney W. Grimes * checksum extended header and data. 1333df8bae1dSRodney W. Grimes */ 1334fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 1335fcf59617SAndrey V. Elsukov 1336fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1337fcf59617SAndrey V. Elsukov if (to.to_flags & TOF_SIGNATURE) { 1338fcf59617SAndrey V. Elsukov /* 1339fcf59617SAndrey V. Elsukov * Calculate MD5 signature and put it into the place 1340fcf59617SAndrey V. Elsukov * determined before. 1341fcf59617SAndrey V. Elsukov * NOTE: since TCP options buffer doesn't point into 1342fcf59617SAndrey V. Elsukov * mbuf's data, calculate offset and use it. 1343fcf59617SAndrey V. Elsukov */ 13442aad6240SAndrey V. Elsukov if (!TCPMD5_ENABLED() || (error = TCPMD5_OUTPUT(m, th, 13452aad6240SAndrey V. Elsukov (u_char *)(th + 1) + (to.to_signature - opt))) != 0) { 1346fcf59617SAndrey V. Elsukov /* 1347fcf59617SAndrey V. Elsukov * Do not send segment if the calculation of MD5 1348fcf59617SAndrey V. Elsukov * digest has failed. 1349fcf59617SAndrey V. Elsukov */ 13502aad6240SAndrey V. Elsukov m_freem(m); 1351fcf59617SAndrey V. Elsukov goto out; 1352fcf59617SAndrey V. Elsukov } 1353fcf59617SAndrey V. Elsukov } 1354fcf59617SAndrey V. Elsukov #endif 1355fb59c426SYoshinobu Inoue #ifdef INET6 135645747ba5SBjoern A. Zeeb if (isipv6) { 1357fb59c426SYoshinobu Inoue /* 13583df96ee6SCy Schubert * There is no need to fill in ip6_plen right now. 13593df96ee6SCy Schubert * It will be filled later by ip6_output. 1360fb59c426SYoshinobu Inoue */ 13619e644c23SMichael Tuexen if (tp->t_port) { 13629e644c23SMichael Tuexen m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 13639e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13649e644c23SMichael Tuexen udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 13659e644c23SMichael Tuexen th->th_sum = htons(0); 13669e644c23SMichael Tuexen UDPSTAT_INC(udps_opackets); 13679e644c23SMichael Tuexen } else { 1368356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 13699e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13709e644c23SMichael Tuexen th->th_sum = in6_cksum_pseudo(ip6, 13719e644c23SMichael Tuexen sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 13729e644c23SMichael Tuexen 0); 13739e644c23SMichael Tuexen } 137445747ba5SBjoern A. Zeeb } 137545747ba5SBjoern A. Zeeb #endif 137645747ba5SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1377fb59c426SYoshinobu Inoue else 137845747ba5SBjoern A. Zeeb #endif 137945747ba5SBjoern A. Zeeb #ifdef INET 1380fb59c426SYoshinobu Inoue { 13819e644c23SMichael Tuexen if (tp->t_port) { 13829e644c23SMichael Tuexen m->m_pkthdr.csum_flags = CSUM_UDP; 13839e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13849e644c23SMichael Tuexen udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 13859e644c23SMichael Tuexen ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 13869e644c23SMichael Tuexen th->th_sum = htons(0); 13879e644c23SMichael Tuexen UDPSTAT_INC(udps_opackets); 13889e644c23SMichael Tuexen } else { 1389356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP; 13909e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13919e644c23SMichael Tuexen th->th_sum = in_pseudo(ip->ip_src.s_addr, 13929e644c23SMichael Tuexen ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 13939e644c23SMichael Tuexen IPPROTO_TCP + len + optlen)); 13949e644c23SMichael Tuexen } 1395fb59c426SYoshinobu Inoue 1396db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 1397db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 13986e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 1399fb59c426SYoshinobu Inoue } 140045747ba5SBjoern A. Zeeb #endif 1401df8bae1dSRodney W. Grimes 1402df8bae1dSRodney W. Grimes /* 1403b3c0f300SAndre Oppermann * Enable TSO and specify the size of the segments. 1404b3c0f300SAndre Oppermann * The TCP pseudo header checksum is always provided. 1405b3c0f300SAndre Oppermann */ 1406b3c0f300SAndre Oppermann if (tso) { 14070c39d38dSGleb Smirnoff KASSERT(len > tp->t_maxseg - optlen, 1408153e5b57SAndre Oppermann ("%s: len <= tso_segsz", __func__)); 14093579cf4cSKenneth D. Merry m->m_pkthdr.csum_flags |= CSUM_TSO; 14100c39d38dSGleb Smirnoff m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen; 1411b3c0f300SAndre Oppermann } 1412b3c0f300SAndre Oppermann 141305fb056cSMichael Tuexen KASSERT(len + hdrlen == m_length(m, NULL), 141405fb056cSMichael Tuexen ("%s: mbuf chain shorter than expected: %d + %u != %u", 141505fb056cSMichael Tuexen __func__, len, hdrlen, m_length(m, NULL))); 1416ed420311SAndre Oppermann 1417bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 141839bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */ 141939bc9de5SLawrence Stewart hhook_run_tcp_est_out(tp, th, &to, len, tso); 1420bd79708dSJonathan T. Looney #endif 142139bc9de5SLawrence Stewart 1422610ee2f9SDavid Greenman #ifdef TCPDEBUG 1423df8bae1dSRodney W. Grimes /* 1424df8bae1dSRodney W. Grimes * Trace. 1425df8bae1dSRodney W. Grimes */ 142691f467d5SHartmut Brandt if (so->so_options & SO_DEBUG) { 1427f3e0b7efSBruce M Simpson u_short save = 0; 14285214cb3fSBruce M Simpson #ifdef INET6 14295214cb3fSBruce M Simpson if (!isipv6) 14305214cb3fSBruce M Simpson #endif 14315214cb3fSBruce M Simpson { 14325214cb3fSBruce M Simpson save = ipov->ih_len; 143391f467d5SHartmut Brandt ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */); 14345214cb3fSBruce M Simpson } 1435fb59c426SYoshinobu Inoue tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 14365214cb3fSBruce M Simpson #ifdef INET6 14375214cb3fSBruce M Simpson if (!isipv6) 14385214cb3fSBruce M Simpson #endif 143991f467d5SHartmut Brandt ipov->ih_len = save; 144091f467d5SHartmut Brandt } 1441b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 14422b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__output, tp, th, m); 1443df8bae1dSRodney W. Grimes 1444dcaffbd6SJonathan T. Looney /* We're getting ready to send; log now. */ 1445bd30a121SMichael Tuexen /* XXXMT: We are not honoring verbose logging. */ 1446bd30a121SMichael Tuexen if (tp->t_logstate != TCP_LOG_STATE_OFF) 1447bd30a121SMichael Tuexen lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, 1448bd30a121SMichael Tuexen TCP_LOG_OUT, ERRNO_UNK, len, NULL, false, NULL, NULL, 0, 1449bd30a121SMichael Tuexen NULL); 1450bd30a121SMichael Tuexen else 1451bd30a121SMichael Tuexen lgb = NULL; 1452dcaffbd6SJonathan T. Looney 1453df8bae1dSRodney W. Grimes /* 1454df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 1455df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 1456df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 1457df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 1458df8bae1dSRodney W. Grimes */ 1459fb59c426SYoshinobu Inoue /* 146043630e62SHiren Panchasara * m->m_pkthdr.len should have been set before checksum calculation, 1461fb59c426SYoshinobu Inoue * because in6_cksum() need it. 1462fb59c426SYoshinobu Inoue */ 1463fb59c426SYoshinobu Inoue #ifdef INET6 1464fb59c426SYoshinobu Inoue if (isipv6) { 1465fb59c426SYoshinobu Inoue /* 1466fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 1467fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 1468fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 1469fb59c426SYoshinobu Inoue * Neighbor Discovery. 1470fb59c426SYoshinobu Inoue */ 147197d8d152SAndre Oppermann ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1472fb59c426SYoshinobu Inoue 147357f60867SMark Johnston /* 147457f60867SMark Johnston * Set the packet size here for the benefit of DTrace probes. 147557f60867SMark Johnston * ip6_output() will set it properly; it's supposed to include 147657f60867SMark Johnston * the option header lengths as well. 147757f60867SMark Johnston */ 147857f60867SMark Johnston ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 147957f60867SMark Johnston 14800c39d38dSGleb Smirnoff if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 14810f3e3bc5SSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 14820f3e3bc5SSean Bruno else 14830f3e3bc5SSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 14840f3e3bc5SSean Bruno 148557f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1486d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 148757f60867SMark Johnston 148857f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip6, tp, th); 148957f60867SMark Johnston 149086a996e6SHiren Panchasara #ifdef TCPPCAP 149186a996e6SHiren Panchasara /* Save packet, if requested. */ 149286a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_outpkts)); 149386a996e6SHiren Panchasara #endif 149486a996e6SHiren Panchasara 1495fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 14964a5c6c6aSMike Karels error = ip6_output(m, tp->t_inpcb->in6p_outputopts, 14974a5c6c6aSMike Karels &tp->t_inpcb->inp_route6, 1498df0633a1SGleb Smirnoff ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 1499df0633a1SGleb Smirnoff NULL, NULL, tp->t_inpcb); 1500df0633a1SGleb Smirnoff 1501983066f0SAlexander V. Chernikov if (error == EMSGSIZE && tp->t_inpcb->inp_route6.ro_nh != NULL) 1502983066f0SAlexander V. Chernikov mtu = tp->t_inpcb->inp_route6.ro_nh->nh_mtu; 1503b287c6c7SBjoern A. Zeeb } 1504fb59c426SYoshinobu Inoue #endif /* INET6 */ 1505b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1506b287c6c7SBjoern A. Zeeb else 1507b287c6c7SBjoern A. Zeeb #endif 1508b287c6c7SBjoern A. Zeeb #ifdef INET 1509df8bae1dSRodney W. Grimes { 15108f134647SGleb Smirnoff ip->ip_len = htons(m->m_pkthdr.len); 1511686cdd19SJun-ichiro itojun Hagino #ifdef INET6 15125cd54324SBjoern A. Zeeb if (tp->t_inpcb->inp_vflag & INP_IPV6PROTO) 151397d8d152SAndre Oppermann ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL); 1514686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 1515f138387aSGarrett Wollman /* 151697d8d152SAndre Oppermann * If we do path MTU discovery, then we set DF on every packet. 151797d8d152SAndre Oppermann * This might not be the best thing to do according to RFC3390 151897d8d152SAndre Oppermann * Section 2. However the tcp hostcache migitates the problem 151997d8d152SAndre Oppermann * so it affects only the first tcp connection with a host. 1520e4e92660SAndre Oppermann * 1521e4e92660SAndre Oppermann * NB: Don't set DF on small MTU/MSS to have a safe fallback. 1522f138387aSGarrett Wollman */ 15230c39d38dSGleb Smirnoff if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 1524f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 15259e644c23SMichael Tuexen if (tp->t_port == 0 || len < V_tcp_minmss) { 15269e644c23SMichael Tuexen ip->ip_off |= htons(IP_DF); 15279e644c23SMichael Tuexen } 1528f6f6703fSSean Bruno } else { 1529f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 1530f6f6703fSSean Bruno } 153197d8d152SAndre Oppermann 153257f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1533d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 153457f60867SMark Johnston 153557f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip, tp, th); 153657f60867SMark Johnston 153786a996e6SHiren Panchasara #ifdef TCPPCAP 153886a996e6SHiren Panchasara /* Save packet, if requested. */ 153986a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_outpkts)); 154086a996e6SHiren Panchasara #endif 154186a996e6SHiren Panchasara 154284cc0778SGeorge V. Neville-Neil error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, 1543b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 1544b5d47ff5SJohn-Mark Gurney tp->t_inpcb); 1545df0633a1SGleb Smirnoff 1546983066f0SAlexander V. Chernikov if (error == EMSGSIZE && tp->t_inpcb->inp_route.ro_nh != NULL) 1547983066f0SAlexander V. Chernikov mtu = tp->t_inpcb->inp_route.ro_nh->nh_mtu; 1548df8bae1dSRodney W. Grimes } 1549b287c6c7SBjoern A. Zeeb #endif /* INET */ 15500e2bc05cSGleb Smirnoff 1551bd30a121SMichael Tuexen if (lgb != NULL) { 1552bd30a121SMichael Tuexen lgb->tlb_errno = error; 1553bd30a121SMichael Tuexen lgb = NULL; 1554bd30a121SMichael Tuexen } 15550e2bc05cSGleb Smirnoff out: 155667e89281SRandall Stewart if (error == 0) 15579e4d9e4cSRandall Stewart tcp_account_for_send(tp, len, (tp->snd_nxt != tp->snd_max), 0, hw_tls); 15580e2bc05cSGleb Smirnoff /* 15590e2bc05cSGleb Smirnoff * In transmit state, time the transmission and arrange for 15600e2bc05cSGleb Smirnoff * the retransmit. In persist state, just set snd_max. 15610e2bc05cSGleb Smirnoff */ 15620e2bc05cSGleb Smirnoff if ((tp->t_flags & TF_FORCEDATA) == 0 || 15630e2bc05cSGleb Smirnoff !tcp_timer_active(tp, TT_PERSIST)) { 15640e2bc05cSGleb Smirnoff tcp_seq startseq = tp->snd_nxt; 15650e2bc05cSGleb Smirnoff 15660e2bc05cSGleb Smirnoff /* 15670e2bc05cSGleb Smirnoff * Advance snd_nxt over sequence space of this segment. 15680e2bc05cSGleb Smirnoff */ 15690e2bc05cSGleb Smirnoff if (flags & (TH_SYN|TH_FIN)) { 15700e2bc05cSGleb Smirnoff if (flags & TH_SYN) 15710e2bc05cSGleb Smirnoff tp->snd_nxt++; 15720e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 15730e2bc05cSGleb Smirnoff tp->snd_nxt++; 15740e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 15750e2bc05cSGleb Smirnoff } 15760e2bc05cSGleb Smirnoff } 15770e2bc05cSGleb Smirnoff if (sack_rxmit) 15780e2bc05cSGleb Smirnoff goto timer; 15790e2bc05cSGleb Smirnoff tp->snd_nxt += len; 15800e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 15810e2bc05cSGleb Smirnoff tp->snd_max = tp->snd_nxt; 15820e2bc05cSGleb Smirnoff /* 15830e2bc05cSGleb Smirnoff * Time this transmission if not a retransmission and 15840e2bc05cSGleb Smirnoff * not currently timing anything. 15850e2bc05cSGleb Smirnoff */ 15869dc7d8a2SRichard Scheffenegger tp->t_sndtime = ticks; 15870e2bc05cSGleb Smirnoff if (tp->t_rtttime == 0) { 15880e2bc05cSGleb Smirnoff tp->t_rtttime = ticks; 15890e2bc05cSGleb Smirnoff tp->t_rtseq = startseq; 15900e2bc05cSGleb Smirnoff TCPSTAT_INC(tcps_segstimed); 15910e2bc05cSGleb Smirnoff } 1592adc56f5aSEdward Tomasz Napierala #ifdef STATS 1593adc56f5aSEdward Tomasz Napierala if (!(tp->t_flags & TF_GPUTINPROG) && len) { 1594adc56f5aSEdward Tomasz Napierala tp->t_flags |= TF_GPUTINPROG; 1595adc56f5aSEdward Tomasz Napierala tp->gput_seq = startseq; 1596adc56f5aSEdward Tomasz Napierala tp->gput_ack = startseq + 1597adc56f5aSEdward Tomasz Napierala ulmin(sbavail(&so->so_snd) - off, sendwin); 1598adc56f5aSEdward Tomasz Napierala tp->gput_ts = tcp_ts_getticks(); 1599adc56f5aSEdward Tomasz Napierala } 1600adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 16010e2bc05cSGleb Smirnoff } 16020e2bc05cSGleb Smirnoff 16030e2bc05cSGleb Smirnoff /* 16040e2bc05cSGleb Smirnoff * Set retransmit timer if not currently set, 16050e2bc05cSGleb Smirnoff * and not doing a pure ack or a keep-alive probe. 16060e2bc05cSGleb Smirnoff * Initial value for retransmit timer is smoothed 16070e2bc05cSGleb Smirnoff * round-trip time + 2 * round-trip time variance. 16080e2bc05cSGleb Smirnoff * Initialize shift counter which is used for backoff 16090e2bc05cSGleb Smirnoff * of retransmit time. 16100e2bc05cSGleb Smirnoff */ 16110e2bc05cSGleb Smirnoff timer: 16120e2bc05cSGleb Smirnoff if (!tcp_timer_active(tp, TT_REXMT) && 16130e2bc05cSGleb Smirnoff ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 16140e2bc05cSGleb Smirnoff (tp->snd_nxt != tp->snd_una))) { 16150e2bc05cSGleb Smirnoff if (tcp_timer_active(tp, TT_PERSIST)) { 16160e2bc05cSGleb Smirnoff tcp_timer_activate(tp, TT_PERSIST, 0); 16170e2bc05cSGleb Smirnoff tp->t_rxtshift = 0; 16180e2bc05cSGleb Smirnoff } 16190e2bc05cSGleb Smirnoff tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 1620f8568079SHiren Panchasara } else if (len == 0 && sbavail(&so->so_snd) && 1621f8568079SHiren Panchasara !tcp_timer_active(tp, TT_REXMT) && 1622f8568079SHiren Panchasara !tcp_timer_active(tp, TT_PERSIST)) { 1623f8568079SHiren Panchasara /* 1624f8568079SHiren Panchasara * Avoid a situation where we do not set persist timer 1625f8568079SHiren Panchasara * after a zero window condition. For example: 1626f8568079SHiren Panchasara * 1) A -> B: packet with enough data to fill the window 1627f8568079SHiren Panchasara * 2) B -> A: ACK for #1 + new data (0 window 1628f8568079SHiren Panchasara * advertisement) 1629f8568079SHiren Panchasara * 3) A -> B: ACK for #2, 0 len packet 1630f8568079SHiren Panchasara * 1631f8568079SHiren Panchasara * In this case, A will not activate the persist timer, 1632f8568079SHiren Panchasara * because it chose to send a packet. Unless tcp_output 1633f8568079SHiren Panchasara * is called for some other reason (delayed ack timer, 1634f8568079SHiren Panchasara * another input packet from B, socket syscall), A will 1635f8568079SHiren Panchasara * not send zero window probes. 1636f8568079SHiren Panchasara * 1637f8568079SHiren Panchasara * So, if you send a 0-length packet, but there is data 1638f8568079SHiren Panchasara * in the socket buffer, and neither the rexmt or 1639f8568079SHiren Panchasara * persist timer is already set, then activate the 1640f8568079SHiren Panchasara * persist timer. 1641f8568079SHiren Panchasara */ 1642f8568079SHiren Panchasara tp->t_rxtshift = 0; 1643f8568079SHiren Panchasara tcp_setpersist(tp); 16440e2bc05cSGleb Smirnoff } 16450e2bc05cSGleb Smirnoff } else { 16460e2bc05cSGleb Smirnoff /* 16470e2bc05cSGleb Smirnoff * Persist case, update snd_max but since we are in 16480e2bc05cSGleb Smirnoff * persist mode (no window) we do not update snd_nxt. 16490e2bc05cSGleb Smirnoff */ 16500e2bc05cSGleb Smirnoff int xlen = len; 16510e2bc05cSGleb Smirnoff if (flags & TH_SYN) 16520e2bc05cSGleb Smirnoff ++xlen; 16530e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 16540e2bc05cSGleb Smirnoff ++xlen; 16550e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 16560e2bc05cSGleb Smirnoff } 16570e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 165815c82571SJonathan T. Looney tp->snd_max = tp->snd_nxt + xlen; 16590e2bc05cSGleb Smirnoff } 1660e5926fd3SRandall Stewart if ((error == 0) && 1661e5926fd3SRandall Stewart (TCPS_HAVEESTABLISHED(tp->t_state) && 1662e5926fd3SRandall Stewart (tp->t_flags & TF_SACK_PERMIT) && 1663e5926fd3SRandall Stewart tp->rcv_numsacks > 0)) { 1664e5926fd3SRandall Stewart /* Clean up any DSACK's sent */ 1665e5926fd3SRandall Stewart tcp_clean_dsack_blocks(tp); 1666e5926fd3SRandall Stewart } 1667df8bae1dSRodney W. Grimes if (error) { 16687734ea06SArchie Cobbs /* 16697734ea06SArchie Cobbs * We know that the packet was lost, so back out the 16707734ea06SArchie Cobbs * sequence number advance, if any. 16712c30ec0aSAndre Oppermann * 16722c30ec0aSAndre Oppermann * If the error is EPERM the packet got blocked by the 16732c30ec0aSAndre Oppermann * local firewall. Normally we should terminate the 16742c30ec0aSAndre Oppermann * connection but the blocking may have been spurious 16752c30ec0aSAndre Oppermann * due to a firewall reconfiguration cycle. So we treat 16762c30ec0aSAndre Oppermann * it like a packet loss and let the retransmit timer and 16772c30ec0aSAndre Oppermann * timeouts do their work over time. 16782c30ec0aSAndre Oppermann * XXX: It is a POLA question whether calling tcp_drop right 16792c30ec0aSAndre Oppermann * away would be the really correct behavior instead. 16807734ea06SArchie Cobbs */ 168172757d9aSGleb Smirnoff if (((tp->t_flags & TF_FORCEDATA) == 0 || 1682b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) && 168372757d9aSGleb Smirnoff ((flags & TH_SYN) == 0) && 168472757d9aSGleb Smirnoff (error != EPERM)) { 16850077b016SPaul Saab if (sack_rxmit) { 16865d3b1b75SJayanth Vijayaraghavan p->rxmit -= len; 16870077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= len; 168872757d9aSGleb Smirnoff KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 16890077b016SPaul Saab ("sackhint bytes rtx >= 0")); 169066605ff7SRichard Scheffenegger KASSERT((flags & TH_FIN) == 0, 169166605ff7SRichard Scheffenegger ("error while FIN with SACK rxmit")); 169266605ff7SRichard Scheffenegger } else { 16937734ea06SArchie Cobbs tp->snd_nxt -= len; 169466605ff7SRichard Scheffenegger if (flags & TH_FIN) 169566605ff7SRichard Scheffenegger tp->snd_nxt--; 169666605ff7SRichard Scheffenegger } 16977734ea06SArchie Cobbs } 1698cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */ 169972757d9aSGleb Smirnoff switch (error) { 1700fcf59617SAndrey V. Elsukov case EACCES: 170172757d9aSGleb Smirnoff case EPERM: 170272757d9aSGleb Smirnoff tp->t_softerror = error; 170372757d9aSGleb Smirnoff return (error); 170472757d9aSGleb Smirnoff case ENOBUFS: 1705425b7639SSepherosa Ziehau TCP_XMIT_TIMER_ASSERT(tp, len, flags); 17061600372bSAndre Oppermann tp->snd_cwnd = tp->t_maxseg; 1707df8bae1dSRodney W. Grimes return (0); 170872757d9aSGleb Smirnoff case EMSGSIZE: 17093d1f141bSGarrett Wollman /* 1710b3c0f300SAndre Oppermann * For some reason the interface we used initially 1711b3c0f300SAndre Oppermann * to send segments changed to another or lowered 1712b3c0f300SAndre Oppermann * its MTU. 1713b3c0f300SAndre Oppermann * If TSO was active we either got an interface 1714b3c0f300SAndre Oppermann * without TSO capabilits or TSO was turned off. 1715df0633a1SGleb Smirnoff * If we obtained mtu from ip_output() then update 1716df0633a1SGleb Smirnoff * it and try again. 17173d1f141bSGarrett Wollman */ 1718b3c0f300SAndre Oppermann if (tso) 1719b3c0f300SAndre Oppermann tp->t_flags &= ~TF_TSO; 1720df0633a1SGleb Smirnoff if (mtu != 0) { 1721df0633a1SGleb Smirnoff tcp_mss_update(tp, -1, mtu, NULL, NULL); 1722df0633a1SGleb Smirnoff goto again; 1723df0633a1SGleb Smirnoff } 1724df0633a1SGleb Smirnoff return (error); 17258bec3467SGleb Smirnoff case EHOSTDOWN: 172672757d9aSGleb Smirnoff case EHOSTUNREACH: 172772757d9aSGleb Smirnoff case ENETDOWN: 17288bec3467SGleb Smirnoff case ENETUNREACH: 172972757d9aSGleb Smirnoff if (TCPS_HAVERCVDSYN(tp->t_state)) { 1730df8bae1dSRodney W. Grimes tp->t_softerror = error; 1731df8bae1dSRodney W. Grimes return (0); 1732df8bae1dSRodney W. Grimes } 173372757d9aSGleb Smirnoff /* FALLTHROUGH */ 173472757d9aSGleb Smirnoff default: 1735df8bae1dSRodney W. Grimes return (error); 1736df8bae1dSRodney W. Grimes } 173772757d9aSGleb Smirnoff } 173878b50714SRobert Watson TCPSTAT_INC(tcps_sndtotal); 1739df8bae1dSRodney W. Grimes 1740df8bae1dSRodney W. Grimes /* 1741df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 1742df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 1743df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 1744df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 1745df8bae1dSRodney W. Grimes */ 17463ac12506SJonathan T. Looney if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1747201d185bSAndre Oppermann tp->rcv_adv = tp->rcv_nxt + recwin; 1748df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 17493bfd6421SJonathan Lemon tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 1750b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_DELACK)) 1751b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 0); 1752d912c694SMatthew Dillon #if 0 1753d912c694SMatthew Dillon /* 1754d912c694SMatthew Dillon * This completely breaks TCP if newreno is turned on. What happens 1755d912c694SMatthew Dillon * is that if delayed-acks are turned on on the receiver, this code 1756d912c694SMatthew Dillon * on the transmitter effectively destroys the TCP window, forcing 1757d912c694SMatthew Dillon * it to four packets (1.5Kx4 = 6K window). 1758d912c694SMatthew Dillon */ 1759dbc42409SLawrence Stewart if (sendalot && --maxburst) 1760df8bae1dSRodney W. Grimes goto again; 1761d912c694SMatthew Dillon #endif 1762d912c694SMatthew Dillon if (sendalot) 1763d912c694SMatthew Dillon goto again; 1764df8bae1dSRodney W. Grimes return (0); 1765df8bae1dSRodney W. Grimes } 1766df8bae1dSRodney W. Grimes 1767df8bae1dSRodney W. Grimes void 1768ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp) 1769df8bae1dSRodney W. Grimes { 17709b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 17719b8b58e0SJonathan Lemon int tt; 1772df8bae1dSRodney W. Grimes 1773672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 1774b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_REXMT)) 17759b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 1776df8bae1dSRodney W. Grimes /* 1777a4641f4eSPedro F. Giffuni * Start/restart persistence timer. 1778df8bae1dSRodney W. Grimes */ 17799b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 17800645c604SHiren Panchasara tcp_persmin, tcp_persmax); 1781b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, tt); 1782df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1783df8bae1dSRodney W. Grimes tp->t_rxtshift++; 1784df8bae1dSRodney W. Grimes } 178502a1a643SAndre Oppermann 178602a1a643SAndre Oppermann /* 178702a1a643SAndre Oppermann * Insert TCP options according to the supplied parameters to the place 178802a1a643SAndre Oppermann * optp in a consistent way. Can handle unaligned destinations. 178902a1a643SAndre Oppermann * 179002a1a643SAndre Oppermann * The order of the option processing is crucial for optimal packing and 179102a1a643SAndre Oppermann * alignment for the scarce option space. 179202a1a643SAndre Oppermann * 179302a1a643SAndre Oppermann * The optimal order for a SYN/SYN-ACK segment is: 179402a1a643SAndre Oppermann * MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) + 179502a1a643SAndre Oppermann * Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40. 179602a1a643SAndre Oppermann * 179702a1a643SAndre Oppermann * The SACK options should be last. SACK blocks consume 8*n+2 bytes. 179802a1a643SAndre Oppermann * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks). 179902a1a643SAndre Oppermann * At minimum we need 10 bytes (to generate 1 SACK block). If both 180002a1a643SAndre Oppermann * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present, 180102a1a643SAndre Oppermann * we only have 10 bytes for SACK options (40 - (12 + 18)). 180202a1a643SAndre Oppermann */ 180302a1a643SAndre Oppermann int 180402a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp) 180502a1a643SAndre Oppermann { 18065d20f974SJonathan T. Looney u_int32_t mask, optlen = 0; 180702a1a643SAndre Oppermann 180802a1a643SAndre Oppermann for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) { 180902a1a643SAndre Oppermann if ((to->to_flags & mask) != mask) 181002a1a643SAndre Oppermann continue; 18113a4018c4SAndre Oppermann if (optlen == TCP_MAXOLEN) 18123a4018c4SAndre Oppermann break; 181302a1a643SAndre Oppermann switch (to->to_flags & mask) { 181402a1a643SAndre Oppermann case TOF_MSS: 181502a1a643SAndre Oppermann while (optlen % 4) { 181602a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 181702a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 181802a1a643SAndre Oppermann } 18193a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG) 18203a4018c4SAndre Oppermann continue; 182102a1a643SAndre Oppermann optlen += TCPOLEN_MAXSEG; 182202a1a643SAndre Oppermann *optp++ = TCPOPT_MAXSEG; 182302a1a643SAndre Oppermann *optp++ = TCPOLEN_MAXSEG; 182402a1a643SAndre Oppermann to->to_mss = htons(to->to_mss); 182502a1a643SAndre Oppermann bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss)); 182602a1a643SAndre Oppermann optp += sizeof(to->to_mss); 182702a1a643SAndre Oppermann break; 182802a1a643SAndre Oppermann case TOF_SCALE: 182902a1a643SAndre Oppermann while (!optlen || optlen % 2 != 1) { 183002a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 183102a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 183202a1a643SAndre Oppermann } 18333a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW) 18343a4018c4SAndre Oppermann continue; 183502a1a643SAndre Oppermann optlen += TCPOLEN_WINDOW; 183602a1a643SAndre Oppermann *optp++ = TCPOPT_WINDOW; 183702a1a643SAndre Oppermann *optp++ = TCPOLEN_WINDOW; 183802a1a643SAndre Oppermann *optp++ = to->to_wscale; 183902a1a643SAndre Oppermann break; 184002a1a643SAndre Oppermann case TOF_SACKPERM: 184102a1a643SAndre Oppermann while (optlen % 2) { 184202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 184302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 184402a1a643SAndre Oppermann } 18453a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED) 18463a4018c4SAndre Oppermann continue; 184702a1a643SAndre Oppermann optlen += TCPOLEN_SACK_PERMITTED; 184802a1a643SAndre Oppermann *optp++ = TCPOPT_SACK_PERMITTED; 184902a1a643SAndre Oppermann *optp++ = TCPOLEN_SACK_PERMITTED; 185002a1a643SAndre Oppermann break; 185102a1a643SAndre Oppermann case TOF_TS: 185202a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 185302a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 185402a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 185502a1a643SAndre Oppermann } 18563a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP) 18573a4018c4SAndre Oppermann continue; 185802a1a643SAndre Oppermann optlen += TCPOLEN_TIMESTAMP; 185902a1a643SAndre Oppermann *optp++ = TCPOPT_TIMESTAMP; 186002a1a643SAndre Oppermann *optp++ = TCPOLEN_TIMESTAMP; 186102a1a643SAndre Oppermann to->to_tsval = htonl(to->to_tsval); 186202a1a643SAndre Oppermann to->to_tsecr = htonl(to->to_tsecr); 186302a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval)); 186402a1a643SAndre Oppermann optp += sizeof(to->to_tsval); 186502a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr)); 186602a1a643SAndre Oppermann optp += sizeof(to->to_tsecr); 186702a1a643SAndre Oppermann break; 186802a1a643SAndre Oppermann case TOF_SIGNATURE: 186902a1a643SAndre Oppermann { 187002a1a643SAndre Oppermann int siglen = TCPOLEN_SIGNATURE - 2; 187102a1a643SAndre Oppermann 187202a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 187302a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 187402a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 187502a1a643SAndre Oppermann } 1876fcf59617SAndrey V. Elsukov if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE) { 1877fcf59617SAndrey V. Elsukov to->to_flags &= ~TOF_SIGNATURE; 187802a1a643SAndre Oppermann continue; 1879fcf59617SAndrey V. Elsukov } 188002a1a643SAndre Oppermann optlen += TCPOLEN_SIGNATURE; 188102a1a643SAndre Oppermann *optp++ = TCPOPT_SIGNATURE; 188202a1a643SAndre Oppermann *optp++ = TCPOLEN_SIGNATURE; 188302a1a643SAndre Oppermann to->to_signature = optp; 188402a1a643SAndre Oppermann while (siglen--) 188502a1a643SAndre Oppermann *optp++ = 0; 188602a1a643SAndre Oppermann break; 188702a1a643SAndre Oppermann } 188802a1a643SAndre Oppermann case TOF_SACK: 188902a1a643SAndre Oppermann { 189002a1a643SAndre Oppermann int sackblks = 0; 189102a1a643SAndre Oppermann struct sackblk *sack = (struct sackblk *)to->to_sacks; 189202a1a643SAndre Oppermann tcp_seq sack_seq; 189302a1a643SAndre Oppermann 189402a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 189502a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 189602a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 189702a1a643SAndre Oppermann } 18983a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK) 189902a1a643SAndre Oppermann continue; 190002a1a643SAndre Oppermann optlen += TCPOLEN_SACKHDR; 190102a1a643SAndre Oppermann *optp++ = TCPOPT_SACK; 190202a1a643SAndre Oppermann sackblks = min(to->to_nsacks, 19030d957bbaSAndre Oppermann (TCP_MAXOLEN - optlen) / TCPOLEN_SACK); 190402a1a643SAndre Oppermann *optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK; 190502a1a643SAndre Oppermann while (sackblks--) { 190602a1a643SAndre Oppermann sack_seq = htonl(sack->start); 190702a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 190802a1a643SAndre Oppermann optp += sizeof(sack_seq); 190902a1a643SAndre Oppermann sack_seq = htonl(sack->end); 191002a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 191102a1a643SAndre Oppermann optp += sizeof(sack_seq); 191202a1a643SAndre Oppermann optlen += TCPOLEN_SACK; 191302a1a643SAndre Oppermann sack++; 191402a1a643SAndre Oppermann } 191578b50714SRobert Watson TCPSTAT_INC(tcps_sack_send_blocks); 191602a1a643SAndre Oppermann break; 191702a1a643SAndre Oppermann } 1918281a0fd4SPatrick Kelsey case TOF_FASTOPEN: 1919281a0fd4SPatrick Kelsey { 1920281a0fd4SPatrick Kelsey int total_len; 1921281a0fd4SPatrick Kelsey 1922281a0fd4SPatrick Kelsey /* XXX is there any point to aligning this option? */ 1923281a0fd4SPatrick Kelsey total_len = TCPOLEN_FAST_OPEN_EMPTY + to->to_tfo_len; 1924c560df6fSPatrick Kelsey if (TCP_MAXOLEN - optlen < total_len) { 1925c560df6fSPatrick Kelsey to->to_flags &= ~TOF_FASTOPEN; 1926281a0fd4SPatrick Kelsey continue; 1927c560df6fSPatrick Kelsey } 1928281a0fd4SPatrick Kelsey *optp++ = TCPOPT_FAST_OPEN; 1929281a0fd4SPatrick Kelsey *optp++ = total_len; 1930281a0fd4SPatrick Kelsey if (to->to_tfo_len > 0) { 1931281a0fd4SPatrick Kelsey bcopy(to->to_tfo_cookie, optp, to->to_tfo_len); 1932281a0fd4SPatrick Kelsey optp += to->to_tfo_len; 1933281a0fd4SPatrick Kelsey } 1934281a0fd4SPatrick Kelsey optlen += total_len; 1935281a0fd4SPatrick Kelsey break; 1936281a0fd4SPatrick Kelsey } 193702a1a643SAndre Oppermann default: 193802a1a643SAndre Oppermann panic("%s: unknown TCP option type", __func__); 193902a1a643SAndre Oppermann break; 194002a1a643SAndre Oppermann } 194102a1a643SAndre Oppermann } 194202a1a643SAndre Oppermann 194302a1a643SAndre Oppermann /* Terminate and pad TCP options to a 4 byte boundary. */ 194402a1a643SAndre Oppermann if (optlen % 4) { 194502a1a643SAndre Oppermann optlen += TCPOLEN_EOL; 194602a1a643SAndre Oppermann *optp++ = TCPOPT_EOL; 194702a1a643SAndre Oppermann } 1948413deb12SBjoern A. Zeeb /* 1949413deb12SBjoern A. Zeeb * According to RFC 793 (STD0007): 1950413deb12SBjoern A. Zeeb * "The content of the header beyond the End-of-Option option 1951413deb12SBjoern A. Zeeb * must be header padding (i.e., zero)." 1952413deb12SBjoern A. Zeeb * and later: "The padding is composed of zeros." 1953413deb12SBjoern A. Zeeb */ 195402a1a643SAndre Oppermann while (optlen % 4) { 1955c343c524SAndre Oppermann optlen += TCPOLEN_PAD; 1956c343c524SAndre Oppermann *optp++ = TCPOPT_PAD; 195702a1a643SAndre Oppermann } 195802a1a643SAndre Oppermann 19590d957bbaSAndre Oppermann KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__)); 196002a1a643SAndre Oppermann return (optlen); 196102a1a643SAndre Oppermann } 196266492feaSGleb Smirnoff 196389e560f4SRandall Stewart /* 196489e560f4SRandall Stewart * This is a copy of m_copym(), taking the TSO segment size/limit 196589e560f4SRandall Stewart * constraints into account, and advancing the sndptr as it goes. 196689e560f4SRandall Stewart */ 196789e560f4SRandall Stewart struct mbuf * 196889e560f4SRandall Stewart tcp_m_copym(struct mbuf *m, int32_t off0, int32_t *plen, 1969b2e60773SJohn Baldwin int32_t seglimit, int32_t segsize, struct sockbuf *sb, bool hw_tls) 197089e560f4SRandall Stewart { 1971b2e60773SJohn Baldwin #ifdef KERN_TLS 1972b2e60773SJohn Baldwin struct ktls_session *tls, *ntls; 1973732b6d4dSJohn Baldwin struct mbuf *start __diagused; 1974b2e60773SJohn Baldwin #endif 197589e560f4SRandall Stewart struct mbuf *n, **np; 197689e560f4SRandall Stewart struct mbuf *top; 197789e560f4SRandall Stewart int32_t off = off0; 197889e560f4SRandall Stewart int32_t len = *plen; 197989e560f4SRandall Stewart int32_t fragsize; 198089e560f4SRandall Stewart int32_t len_cp = 0; 198189e560f4SRandall Stewart int32_t *pkthdrlen; 198289e560f4SRandall Stewart uint32_t mlen, frags; 198389e560f4SRandall Stewart bool copyhdr; 198489e560f4SRandall Stewart 198589e560f4SRandall Stewart KASSERT(off >= 0, ("tcp_m_copym, negative off %d", off)); 198689e560f4SRandall Stewart KASSERT(len >= 0, ("tcp_m_copym, negative len %d", len)); 198789e560f4SRandall Stewart if (off == 0 && m->m_flags & M_PKTHDR) 198889e560f4SRandall Stewart copyhdr = true; 198989e560f4SRandall Stewart else 199089e560f4SRandall Stewart copyhdr = false; 199189e560f4SRandall Stewart while (off > 0) { 199289e560f4SRandall Stewart KASSERT(m != NULL, ("tcp_m_copym, offset > size of mbuf chain")); 199389e560f4SRandall Stewart if (off < m->m_len) 199489e560f4SRandall Stewart break; 199589e560f4SRandall Stewart off -= m->m_len; 199689e560f4SRandall Stewart if ((sb) && (m == sb->sb_sndptr)) { 199789e560f4SRandall Stewart sb->sb_sndptroff += m->m_len; 199889e560f4SRandall Stewart sb->sb_sndptr = m->m_next; 199989e560f4SRandall Stewart } 200089e560f4SRandall Stewart m = m->m_next; 200189e560f4SRandall Stewart } 200289e560f4SRandall Stewart np = ⊤ 200389e560f4SRandall Stewart top = NULL; 200489e560f4SRandall Stewart pkthdrlen = NULL; 2005b2e60773SJohn Baldwin #ifdef KERN_TLS 20066edfd179SGleb Smirnoff if (hw_tls && (m->m_flags & M_EXTPG)) 20077b6c99d0SGleb Smirnoff tls = m->m_epg_tls; 2008b2e60773SJohn Baldwin else 2009b2e60773SJohn Baldwin tls = NULL; 2010b2e60773SJohn Baldwin start = m; 2011b2e60773SJohn Baldwin #endif 201289e560f4SRandall Stewart while (len > 0) { 201389e560f4SRandall Stewart if (m == NULL) { 201489e560f4SRandall Stewart KASSERT(len == M_COPYALL, 201589e560f4SRandall Stewart ("tcp_m_copym, length > size of mbuf chain")); 201689e560f4SRandall Stewart *plen = len_cp; 201789e560f4SRandall Stewart if (pkthdrlen != NULL) 201889e560f4SRandall Stewart *pkthdrlen = len_cp; 201989e560f4SRandall Stewart break; 202089e560f4SRandall Stewart } 2021b2e60773SJohn Baldwin #ifdef KERN_TLS 2022b2e60773SJohn Baldwin if (hw_tls) { 20236edfd179SGleb Smirnoff if (m->m_flags & M_EXTPG) 20247b6c99d0SGleb Smirnoff ntls = m->m_epg_tls; 2025b2e60773SJohn Baldwin else 2026b2e60773SJohn Baldwin ntls = NULL; 2027b2e60773SJohn Baldwin 2028b2e60773SJohn Baldwin /* 2029b2e60773SJohn Baldwin * Avoid mixing TLS records with handshake 2030b2e60773SJohn Baldwin * data or TLS records from different 2031b2e60773SJohn Baldwin * sessions. 2032b2e60773SJohn Baldwin */ 2033b2e60773SJohn Baldwin if (tls != ntls) { 2034b2e60773SJohn Baldwin MPASS(m != start); 2035b2e60773SJohn Baldwin *plen = len_cp; 2036b2e60773SJohn Baldwin if (pkthdrlen != NULL) 2037b2e60773SJohn Baldwin *pkthdrlen = len_cp; 2038b2e60773SJohn Baldwin break; 2039b2e60773SJohn Baldwin } 2040b2e60773SJohn Baldwin } 2041b2e60773SJohn Baldwin #endif 204289e560f4SRandall Stewart mlen = min(len, m->m_len - off); 204389e560f4SRandall Stewart if (seglimit) { 204489e560f4SRandall Stewart /* 20456edfd179SGleb Smirnoff * For M_EXTPG mbufs, add 3 segments 204689e560f4SRandall Stewart * + 1 in case we are crossing page boundaries 204789e560f4SRandall Stewart * + 2 in case the TLS hdr/trailer are used 204889e560f4SRandall Stewart * It is cheaper to just add the segments 204989e560f4SRandall Stewart * than it is to take the cache miss to look 205089e560f4SRandall Stewart * at the mbuf ext_pgs state in detail. 205189e560f4SRandall Stewart */ 20526edfd179SGleb Smirnoff if (m->m_flags & M_EXTPG) { 205389e560f4SRandall Stewart fragsize = min(segsize, PAGE_SIZE); 205489e560f4SRandall Stewart frags = 3; 205589e560f4SRandall Stewart } else { 205689e560f4SRandall Stewart fragsize = segsize; 205789e560f4SRandall Stewart frags = 0; 205889e560f4SRandall Stewart } 205989e560f4SRandall Stewart 206089e560f4SRandall Stewart /* Break if we really can't fit anymore. */ 206189e560f4SRandall Stewart if ((frags + 1) >= seglimit) { 206289e560f4SRandall Stewart *plen = len_cp; 206389e560f4SRandall Stewart if (pkthdrlen != NULL) 206489e560f4SRandall Stewart *pkthdrlen = len_cp; 206589e560f4SRandall Stewart break; 206689e560f4SRandall Stewart } 206789e560f4SRandall Stewart 206889e560f4SRandall Stewart /* 206989e560f4SRandall Stewart * Reduce size if you can't copy the whole 207089e560f4SRandall Stewart * mbuf. If we can't copy the whole mbuf, also 207189e560f4SRandall Stewart * adjust len so the loop will end after this 207289e560f4SRandall Stewart * mbuf. 207389e560f4SRandall Stewart */ 207489e560f4SRandall Stewart if ((frags + howmany(mlen, fragsize)) >= seglimit) { 207589e560f4SRandall Stewart mlen = (seglimit - frags - 1) * fragsize; 207689e560f4SRandall Stewart len = mlen; 207789e560f4SRandall Stewart *plen = len_cp + len; 207889e560f4SRandall Stewart if (pkthdrlen != NULL) 207989e560f4SRandall Stewart *pkthdrlen = *plen; 208089e560f4SRandall Stewart } 208189e560f4SRandall Stewart frags += howmany(mlen, fragsize); 208289e560f4SRandall Stewart if (frags == 0) 208389e560f4SRandall Stewart frags++; 208489e560f4SRandall Stewart seglimit -= frags; 208589e560f4SRandall Stewart KASSERT(seglimit > 0, 208689e560f4SRandall Stewart ("%s: seglimit went too low", __func__)); 208789e560f4SRandall Stewart } 208889e560f4SRandall Stewart if (copyhdr) 208989e560f4SRandall Stewart n = m_gethdr(M_NOWAIT, m->m_type); 209089e560f4SRandall Stewart else 209189e560f4SRandall Stewart n = m_get(M_NOWAIT, m->m_type); 209289e560f4SRandall Stewart *np = n; 209389e560f4SRandall Stewart if (n == NULL) 209489e560f4SRandall Stewart goto nospace; 209589e560f4SRandall Stewart if (copyhdr) { 209689e560f4SRandall Stewart if (!m_dup_pkthdr(n, m, M_NOWAIT)) 209789e560f4SRandall Stewart goto nospace; 209889e560f4SRandall Stewart if (len == M_COPYALL) 209989e560f4SRandall Stewart n->m_pkthdr.len -= off0; 210089e560f4SRandall Stewart else 210189e560f4SRandall Stewart n->m_pkthdr.len = len; 210289e560f4SRandall Stewart pkthdrlen = &n->m_pkthdr.len; 210389e560f4SRandall Stewart copyhdr = false; 210489e560f4SRandall Stewart } 210589e560f4SRandall Stewart n->m_len = mlen; 210689e560f4SRandall Stewart len_cp += n->m_len; 210761664ee7SGleb Smirnoff if (m->m_flags & (M_EXT|M_EXTPG)) { 210889e560f4SRandall Stewart n->m_data = m->m_data + off; 210989e560f4SRandall Stewart mb_dupcl(n, m); 211089e560f4SRandall Stewart } else 211189e560f4SRandall Stewart bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 211289e560f4SRandall Stewart (u_int)n->m_len); 211389e560f4SRandall Stewart 211489e560f4SRandall Stewart if (sb && (sb->sb_sndptr == m) && 211589e560f4SRandall Stewart ((n->m_len + off) >= m->m_len) && m->m_next) { 211689e560f4SRandall Stewart sb->sb_sndptroff += m->m_len; 211789e560f4SRandall Stewart sb->sb_sndptr = m->m_next; 211889e560f4SRandall Stewart } 211989e560f4SRandall Stewart off = 0; 212089e560f4SRandall Stewart if (len != M_COPYALL) { 212189e560f4SRandall Stewart len -= n->m_len; 212289e560f4SRandall Stewart } 212389e560f4SRandall Stewart m = m->m_next; 212489e560f4SRandall Stewart np = &n->m_next; 212589e560f4SRandall Stewart } 212689e560f4SRandall Stewart return (top); 212789e560f4SRandall Stewart nospace: 212889e560f4SRandall Stewart m_freem(top); 212989e560f4SRandall Stewart return (NULL); 213089e560f4SRandall Stewart } 213189e560f4SRandall Stewart 213266492feaSGleb Smirnoff void 213366492feaSGleb Smirnoff tcp_sndbuf_autoscale(struct tcpcb *tp, struct socket *so, uint32_t sendwin) 213466492feaSGleb Smirnoff { 213566492feaSGleb Smirnoff 213666492feaSGleb Smirnoff /* 213766492feaSGleb Smirnoff * Automatic sizing of send socket buffer. Often the send buffer 213866492feaSGleb Smirnoff * size is not optimally adjusted to the actual network conditions 213966492feaSGleb Smirnoff * at hand (delay bandwidth product). Setting the buffer size too 214066492feaSGleb Smirnoff * small limits throughput on links with high bandwidth and high 214166492feaSGleb Smirnoff * delay (eg. trans-continental/oceanic links). Setting the 214266492feaSGleb Smirnoff * buffer size too big consumes too much real kernel memory, 214366492feaSGleb Smirnoff * especially with many connections on busy servers. 214466492feaSGleb Smirnoff * 214566492feaSGleb Smirnoff * The criteria to step up the send buffer one notch are: 214666492feaSGleb Smirnoff * 1. receive window of remote host is larger than send buffer 214766492feaSGleb Smirnoff * (with a fudge factor of 5/4th); 214866492feaSGleb Smirnoff * 2. send buffer is filled to 7/8th with data (so we actually 214966492feaSGleb Smirnoff * have data to make use of it); 215066492feaSGleb Smirnoff * 3. send buffer fill has not hit maximal automatic size; 215166492feaSGleb Smirnoff * 4. our send window (slow start and cogestion controlled) is 215266492feaSGleb Smirnoff * larger than sent but unacknowledged data in send buffer. 215366492feaSGleb Smirnoff * 215466492feaSGleb Smirnoff * The remote host receive window scaling factor may limit the 215566492feaSGleb Smirnoff * growing of the send buffer before it reaches its allowed 215666492feaSGleb Smirnoff * maximum. 215766492feaSGleb Smirnoff * 215866492feaSGleb Smirnoff * It scales directly with slow start or congestion window 215966492feaSGleb Smirnoff * and does at most one step per received ACK. This fast 216066492feaSGleb Smirnoff * scaling has the drawback of growing the send buffer beyond 216166492feaSGleb Smirnoff * what is strictly necessary to make full use of a given 216266492feaSGleb Smirnoff * delay*bandwidth product. However testing has shown this not 216366492feaSGleb Smirnoff * to be much of an problem. At worst we are trading wasting 216466492feaSGleb Smirnoff * of available bandwidth (the non-use of it) for wasting some 216566492feaSGleb Smirnoff * socket buffer memory. 216666492feaSGleb Smirnoff * 216766492feaSGleb Smirnoff * TODO: Shrink send buffer during idle periods together 216866492feaSGleb Smirnoff * with congestion window. Requires another timer. Has to 216966492feaSGleb Smirnoff * wait for upcoming tcp timer rewrite. 217066492feaSGleb Smirnoff * 217166492feaSGleb Smirnoff * XXXGL: should there be used sbused() or sbavail()? 217266492feaSGleb Smirnoff */ 217366492feaSGleb Smirnoff if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 217466492feaSGleb Smirnoff int lowat; 217566492feaSGleb Smirnoff 217666492feaSGleb Smirnoff lowat = V_tcp_sendbuf_auto_lowat ? so->so_snd.sb_lowat : 0; 217766492feaSGleb Smirnoff if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat - lowat && 217866492feaSGleb Smirnoff sbused(&so->so_snd) >= 217966492feaSGleb Smirnoff (so->so_snd.sb_hiwat / 8 * 7) - lowat && 218066492feaSGleb Smirnoff sbused(&so->so_snd) < V_tcp_autosndbuf_max && 218166492feaSGleb Smirnoff sendwin >= (sbused(&so->so_snd) - 218266492feaSGleb Smirnoff (tp->snd_nxt - tp->snd_una))) { 218343283184SGleb Smirnoff if (!sbreserve_locked(so, SO_SND, 218466492feaSGleb Smirnoff min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc, 218543283184SGleb Smirnoff V_tcp_autosndbuf_max), curthread)) 218666492feaSGleb Smirnoff so->so_snd.sb_flags &= ~SB_AUTOSIZE; 218766492feaSGleb Smirnoff } 218866492feaSGleb Smirnoff } 218966492feaSGleb Smirnoff } 2190