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 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43df8bae1dSRodney W. Grimes #include <sys/systm.h> 44adc56f5aSEdward Tomasz Napierala #include <sys/arb.h> 45686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h> 46bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 4739bc9de5SLawrence Stewart #include <sys/hhook.h> 48bd79708dSJonathan T. Looney #endif 49fb919e4dSMark Murray #include <sys/kernel.h> 50b2e60773SJohn Baldwin #ifdef KERN_TLS 51b2e60773SJohn Baldwin #include <sys/ktls.h> 52b2e60773SJohn Baldwin #endif 53fb919e4dSMark Murray #include <sys/lock.h> 54fb919e4dSMark Murray #include <sys/mbuf.h> 55fb919e4dSMark Murray #include <sys/mutex.h> 56df8bae1dSRodney W. Grimes #include <sys/protosw.h> 57adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h> 5857f60867SMark Johnston #include <sys/sdt.h> 59df8bae1dSRodney W. Grimes #include <sys/socket.h> 60df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 61fb919e4dSMark Murray #include <sys/sysctl.h> 62adc56f5aSEdward Tomasz Napierala #include <sys/stats.h> 63df8bae1dSRodney W. Grimes 644b79449eSBjoern A. Zeeb #include <net/if.h> 65df8bae1dSRodney W. Grimes #include <net/route.h> 66983066f0SAlexander V. Chernikov #include <net/route/nhop.h> 67530c0060SRobert Watson #include <net/vnet.h> 68df8bae1dSRodney W. Grimes 69df8bae1dSRodney W. Grimes #include <netinet/in.h> 7057f60867SMark Johnston #include <netinet/in_kdtrace.h> 71df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 72df8bae1dSRodney W. Grimes #include <netinet/ip.h> 73df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 74df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 75ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 76fb59c426SYoshinobu Inoue #ifdef INET6 77686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 78686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 79fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 80fb59c426SYoshinobu Inoue #endif 812de3e790SGleb Smirnoff #include <netinet/tcp.h> 82a00f4ac2SGleb Smirnoff #define TCPOUTFLAGS 83df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 842529f56eSJonathan T. Looney #include <netinet/tcp_log_buf.h> 85df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 86df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 87f7220c48SRichard Scheffenegger #include <netinet/tcp_syncache.h> 88f7220c48SRichard Scheffenegger #include <netinet/tcp_timer.h> 89df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 904644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 91c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 9286a996e6SHiren Panchasara #ifdef TCPPCAP 9386a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 9486a996e6SHiren Panchasara #endif 9509fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 9609fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 9709fe6320SNavdeep Parhar #endif 98f7220c48SRichard Scheffenegger #include <netinet/tcp_ecn.h> 99df8bae1dSRodney W. Grimes 100fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 101b9234fafSSam Leffler 1029e644c23SMichael Tuexen #include <netinet/udp.h> 1039e644c23SMichael Tuexen #include <netinet/udp_var.h> 104db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 105db4f9cc7SJonathan Lemon 106aed55708SRobert Watson #include <security/mac/mac_framework.h> 107aed55708SRobert Watson 10882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, path_mtu_discovery) = 1; 1096df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_VNET | CTLFLAG_RW, 110eddfbb76SRobert Watson &VNET_NAME(path_mtu_discovery), 1, 111eddfbb76SRobert Watson "Enable Path MTU Discovery"); 1129a039a5fSDavid Greenman 11382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_tso) = 1; 1146df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_VNET | CTLFLAG_RW, 115eddfbb76SRobert Watson &VNET_NAME(tcp_do_tso), 0, 116eddfbb76SRobert Watson "Enable TCP Segmentation Offload"); 117b3c0f300SAndre Oppermann 118873789cbSAndre Oppermann VNET_DEFINE(int, tcp_sendspace) = 1024*32; 119873789cbSAndre Oppermann #define V_tcp_sendspace VNET(tcp_sendspace) 1206df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_VNET | CTLFLAG_RW, 121873789cbSAndre Oppermann &VNET_NAME(tcp_sendspace), 0, "Initial send socket buffer size"); 122873789cbSAndre Oppermann 12382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autosndbuf) = 1; 1246df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 125eddfbb76SRobert Watson &VNET_NAME(tcp_do_autosndbuf), 0, 126eddfbb76SRobert Watson "Enable automatic send buffer sizing"); 1276741ecf5SAndre Oppermann 12882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_inc) = 8*1024; 1296df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 130eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_inc), 0, 1318b615593SMarko Zec "Incrementor step size of automatic send buffer"); 1326741ecf5SAndre Oppermann 133b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_max) = 2*1024*1024; 1346df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 135eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_max), 0, 1368b615593SMarko Zec "Max size of automatic send buffer"); 1376741ecf5SAndre Oppermann 138ac952dd2SSean Bruno VNET_DEFINE(int, tcp_sendbuf_auto_lowat) = 0; 139ac952dd2SSean Bruno #define V_tcp_sendbuf_auto_lowat VNET(tcp_sendbuf_auto_lowat) 140ac952dd2SSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto_lowat, CTLFLAG_VNET | CTLFLAG_RW, 141ac952dd2SSean Bruno &VNET_NAME(tcp_sendbuf_auto_lowat), 0, 142ac952dd2SSean Bruno "Modify threshold for auto send buffer growth to account for SO_SNDLOWAT"); 143ac952dd2SSean Bruno 144425b7639SSepherosa Ziehau /* 145425b7639SSepherosa Ziehau * Make sure that either retransmit or persist timer is set for SYN, FIN and 146425b7639SSepherosa Ziehau * non-ACK. 147425b7639SSepherosa Ziehau */ 148425b7639SSepherosa Ziehau #define TCP_XMIT_TIMER_ASSERT(tp, len, th_flags) \ 149fd29ff5dSRandall Stewart KASSERT(((len) == 0 && ((th_flags) & (TH_SYN | TH_FIN)) == 0) ||\ 150425b7639SSepherosa Ziehau tcp_timer_active((tp), TT_REXMT) || \ 151425b7639SSepherosa Ziehau tcp_timer_active((tp), TT_PERSIST), \ 152425b7639SSepherosa Ziehau ("neither rexmt nor persist timer is set")) 153425b7639SSepherosa Ziehau 154bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 155dbc42409SLawrence Stewart /* 1560f5e7edcSKevin Lo * Wrapper for the TCP established output helper hook. 15739bc9de5SLawrence Stewart */ 15889e560f4SRandall Stewart void 15939bc9de5SLawrence Stewart hhook_run_tcp_est_out(struct tcpcb *tp, struct tcphdr *th, 1603ac12506SJonathan T. Looney struct tcpopt *to, uint32_t len, int tso) 16139bc9de5SLawrence Stewart { 16239bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 16339bc9de5SLawrence Stewart 16439bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_OUT]->hhh_nhooks > 0) { 16539bc9de5SLawrence Stewart hhook_data.tp = tp; 16639bc9de5SLawrence Stewart hhook_data.th = th; 16739bc9de5SLawrence Stewart hhook_data.to = to; 16839bc9de5SLawrence Stewart hhook_data.len = len; 16939bc9de5SLawrence Stewart hhook_data.tso = tso; 17039bc9de5SLawrence Stewart 17139bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_OUT], &hhook_data, 172e68b3792SGleb Smirnoff &tp->t_osd); 17339bc9de5SLawrence Stewart } 17439bc9de5SLawrence Stewart } 175bd79708dSJonathan T. Looney #endif 17639bc9de5SLawrence Stewart 17739bc9de5SLawrence Stewart /* 178dbc42409SLawrence Stewart * CC wrapper hook functions 179dbc42409SLawrence Stewart */ 180cd84e78fSRandall Stewart void 181dbc42409SLawrence Stewart cc_after_idle(struct tcpcb *tp) 182dbc42409SLawrence Stewart { 1839eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 184dbc42409SLawrence Stewart 185dbc42409SLawrence Stewart if (CC_ALGO(tp)->after_idle != NULL) 186e68b3792SGleb Smirnoff CC_ALGO(tp)->after_idle(&tp->t_ccv); 187dbc42409SLawrence Stewart } 1886741ecf5SAndre Oppermann 189df8bae1dSRodney W. Grimes /* 190df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 191df8bae1dSRodney W. Grimes */ 192df8bae1dSRodney W. Grimes int 1935b08b46aSGleb Smirnoff tcp_default_output(struct tcpcb *tp) 194df8bae1dSRodney W. Grimes { 1959eb0e832SGleb Smirnoff struct socket *so = tptosocket(tp); 1969eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 1973ac12506SJonathan T. Looney int32_t len; 1983ac12506SJonathan T. Looney uint32_t recwin, sendwin; 199f7220c48SRichard Scheffenegger uint16_t flags; 200f7220c48SRichard Scheffenegger int off, error = 0; /* Keep compiler happy */ 201581a046aSRandall Stewart u_int if_hw_tsomaxsegcount = 0; 20282e837f8SWarner Losh u_int if_hw_tsomaxsegsize = 0; 203f10e85d7SLuigi Rizzo struct mbuf *m; 204fb59c426SYoshinobu Inoue struct ip *ip = NULL; 205f10e85d7SLuigi Rizzo struct tcphdr *th; 206a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 2079e644c23SMichael Tuexen unsigned ipoptlen, optlen, hdrlen, ulen; 208fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 2099ad0173dSBjoern A. Zeeb unsigned ipsec_optlen = 0; 2109ad0173dSBjoern A. Zeeb #endif 21110d20c84SMatt Macy int idle, sendalot, curticks; 21202a1a643SAndre Oppermann int sack_rxmit, sack_bytes_rxmt; 2136d90faf3SPaul Saab struct sackhole *p; 214df0633a1SGleb Smirnoff int tso, mtu; 21502a1a643SAndre Oppermann struct tcpopt to; 2169e644c23SMichael Tuexen struct udphdr *udp = NULL; 217bd30a121SMichael Tuexen struct tcp_log_buffer *lgb; 218c560df6fSPatrick Kelsey unsigned int wanted_cookie = 0; 219c560df6fSPatrick Kelsey unsigned int dont_sendalot = 0; 220262c1c1aSMatthew Dillon #if 0 22146f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 222262c1c1aSMatthew Dillon #endif 223fb59c426SYoshinobu Inoue #ifdef INET6 224f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 225*2f201df1SAlfonso const bool isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 226fb59c426SYoshinobu Inoue #endif 227b2e60773SJohn Baldwin #ifdef KERN_TLS 228c0e4090eSAndrew Gallatin const bool hw_tls = tp->t_nic_ktls_xmit != 0; 229b2e60773SJohn Baldwin #else 230b2e60773SJohn Baldwin const bool hw_tls = false; 231b2e60773SJohn Baldwin #endif 232df8bae1dSRodney W. Grimes 233109eb549SGleb Smirnoff NET_EPOCH_ASSERT(); 2349eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(inp); 2353d6ade3aSJennifer Yang 23609fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 23709fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 23809fe6320SNavdeep Parhar return (tcp_offload_output(tp)); 23909fe6320SNavdeep Parhar #endif 24009fe6320SNavdeep Parhar 241281a0fd4SPatrick Kelsey /* 2428db239dcSMichael Tuexen * For TFO connections in SYN_SENT or SYN_RECEIVED, 2438db239dcSMichael Tuexen * only allow the initial SYN or SYN|ACK and those sent 2448db239dcSMichael Tuexen * by the retransmit timer. 245281a0fd4SPatrick Kelsey */ 24668bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 2478db239dcSMichael Tuexen ((tp->t_state == TCPS_SYN_SENT) || 2488db239dcSMichael Tuexen (tp->t_state == TCPS_SYN_RECEIVED)) && 2498db239dcSMichael Tuexen SEQ_GT(tp->snd_max, tp->snd_una) && /* initial SYN or SYN|ACK sent */ 250281a0fd4SPatrick Kelsey (tp->snd_nxt != tp->snd_una)) /* not a retransmit */ 251281a0fd4SPatrick Kelsey return (0); 25218a75309SPatrick Kelsey 253df8bae1dSRodney W. Grimes /* 254df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 255df8bae1dSRodney W. Grimes * and flags that will be used. 256df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 257df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 258df8bae1dSRodney W. Grimes */ 259c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 2609dc7d8a2SRichard Scheffenegger if (idle && (((ticks - tp->t_rcvtime) >= tp->t_rxtcur) || 2619dc7d8a2SRichard Scheffenegger (tp->t_sndtime && ((ticks - tp->t_sndtime) >= tp->t_rxtcur)))) 2622ea8da28SLawrence Stewart cc_after_idle(tp); 263c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 264c24d5daeSJayanth Vijayaraghavan if (idle) { 265c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 266c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 267c24d5daeSJayanth Vijayaraghavan idle = 0; 268c24d5daeSJayanth Vijayaraghavan } 269c24d5daeSJayanth Vijayaraghavan } 270df8bae1dSRodney W. Grimes again: 2716d90faf3SPaul Saab /* 2726d90faf3SPaul Saab * If we've recently taken a timeout, snd_max will be greater than 2736d90faf3SPaul Saab * snd_nxt. There may be SACK information that allows us to avoid 2746d90faf3SPaul Saab * resending already delivered data. Adjust snd_nxt accordingly. 2756d90faf3SPaul Saab */ 2763529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2773529149eSAndre Oppermann SEQ_LT(tp->snd_nxt, tp->snd_max)) 2786d90faf3SPaul Saab tcp_sack_adjust(tp); 279df8bae1dSRodney W. Grimes sendalot = 0; 280153e5b57SAndre Oppermann tso = 0; 281df0633a1SGleb Smirnoff mtu = 0; 282df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 283201d185bSAndre Oppermann sendwin = min(tp->snd_wnd, tp->snd_cwnd); 284df8bae1dSRodney W. Grimes 285df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 286a0292f23SGarrett Wollman /* 2876d90faf3SPaul Saab * Send any SACK-generated retransmissions. If we're explicitly trying 2886d90faf3SPaul Saab * to send out new data (when sendalot is 1), bypass this function. 2896d90faf3SPaul Saab * If we retransmit in fast recovery mode, decrement snd_cwnd, since 2906d90faf3SPaul Saab * we're replacing a (future) new transmission with a retransmission 2916d90faf3SPaul Saab * now, and we previously incremented snd_cwnd in tcp_input(). 2926d90faf3SPaul Saab */ 2936d90faf3SPaul Saab /* 2946d90faf3SPaul Saab * Still in sack recovery , reset rxmit flag to zero. 2956d90faf3SPaul Saab */ 2966d90faf3SPaul Saab sack_rxmit = 0; 297a55db2b6SPaul Saab sack_bytes_rxmt = 0; 2986d90faf3SPaul Saab len = 0; 2996d90faf3SPaul Saab p = NULL; 300dbc42409SLawrence Stewart if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp->t_flags) && 301a55db2b6SPaul Saab (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 3023ac12506SJonathan T. Looney uint32_t cwin; 303a55db2b6SPaul Saab 3043ac12506SJonathan T. Looney cwin = 3053ac12506SJonathan T. Looney imax(min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt, 0); 306f787edd8SJayanth Vijayaraghavan /* Do not retransmit SACK segments beyond snd_recover */ 307f787edd8SJayanth Vijayaraghavan if (SEQ_GT(p->end, tp->snd_recover)) { 308f787edd8SJayanth Vijayaraghavan /* 309f787edd8SJayanth Vijayaraghavan * (At least) part of sack hole extends beyond 310f787edd8SJayanth Vijayaraghavan * snd_recover. Check to see if we can rexmit data 311f787edd8SJayanth Vijayaraghavan * for this hole. 312f787edd8SJayanth Vijayaraghavan */ 313f787edd8SJayanth Vijayaraghavan if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 314f787edd8SJayanth Vijayaraghavan /* 315f787edd8SJayanth Vijayaraghavan * Can't rexmit any more data for this hole. 316f787edd8SJayanth Vijayaraghavan * That data will be rexmitted in the next 317f787edd8SJayanth Vijayaraghavan * sack recovery episode, when snd_recover 318f787edd8SJayanth Vijayaraghavan * moves past p->rxmit. 319f787edd8SJayanth Vijayaraghavan */ 320f787edd8SJayanth Vijayaraghavan p = NULL; 321f787edd8SJayanth Vijayaraghavan goto after_sack_rexmit; 32228173d49SHans Petter Selasky } else { 323f787edd8SJayanth Vijayaraghavan /* Can rexmit part of the current hole */ 3243ac12506SJonathan T. Looney len = ((int32_t)ulmin(cwin, 32528173d49SHans Petter Selasky SEQ_SUB(tp->snd_recover, p->rxmit))); 32628173d49SHans Petter Selasky } 32728173d49SHans Petter Selasky } else { 32828173d49SHans Petter Selasky len = ((int32_t)ulmin(cwin, 32928173d49SHans Petter Selasky SEQ_SUB(p->end, p->rxmit))); 33028173d49SHans Petter Selasky } 3316d9e911fSMichael Tuexen if (len > 0) { 33228173d49SHans Petter Selasky off = SEQ_SUB(p->rxmit, tp->snd_una); 333f787edd8SJayanth Vijayaraghavan KASSERT(off >= 0,("%s: sack block to the left of una : %d", 334f787edd8SJayanth Vijayaraghavan __func__, off)); 335ab5c14d8SRobert Watson sack_rxmit = 1; 336ab5c14d8SRobert Watson sendalot = 1; 33778b50714SRobert Watson TCPSTAT_INC(tcps_sack_rexmits); 33878b50714SRobert Watson TCPSTAT_ADD(tcps_sack_rexmit_bytes, 3394b72ae16SRichard Scheffenegger min(len, tcp_maxseg(tp))); 3406d90faf3SPaul Saab } 3416d90faf3SPaul Saab } 342f787edd8SJayanth Vijayaraghavan after_sack_rexmit: 3436d90faf3SPaul Saab /* 344a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 345a0292f23SGarrett Wollman * state flags. 346a0292f23SGarrett Wollman */ 347a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 348a0292f23SGarrett Wollman flags |= TH_FIN; 349a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 350a0292f23SGarrett Wollman flags |= TH_SYN; 351a0292f23SGarrett Wollman 352cf2942b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 353df8bae1dSRodney W. Grimes /* 354df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 355df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 356df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 357df8bae1dSRodney W. Grimes * and go to transmit state. 358df8bae1dSRodney W. Grimes */ 3592cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) { 360201d185bSAndre Oppermann if (sendwin == 0) { 361df8bae1dSRodney W. Grimes /* 362df8bae1dSRodney W. Grimes * If we still have some data to send, then 363df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 364df8bae1dSRodney W. Grimes * happen below when it realizes that we 365df8bae1dSRodney W. Grimes * aren't sending all the data. However, 36629089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 367df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 368df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 369df8bae1dSRodney W. Grimes * up sending the packet without recording 370df8bae1dSRodney W. Grimes * that we sent the FIN bit. 371df8bae1dSRodney W. Grimes * 372df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 373df8bae1dSRodney W. Grimes * because if we don't have any more data 374df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 375df8bae1dSRodney W. Grimes * itself. 376df8bae1dSRodney W. Grimes */ 377cfa6009eSGleb Smirnoff if (off < sbused(&so->so_snd)) 378df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 379201d185bSAndre Oppermann sendwin = 1; 380df8bae1dSRodney W. Grimes } else { 381b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, 0); 382df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 383df8bae1dSRodney W. Grimes } 384df8bae1dSRodney W. Grimes } 385df8bae1dSRodney W. Grimes 38628257b5cSMatthew Dillon /* 38728257b5cSMatthew Dillon * If snd_nxt == snd_max and we have transmitted a FIN, the 38828257b5cSMatthew Dillon * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 389201d185bSAndre Oppermann * a negative length. This can also occur when TCP opens up 39028257b5cSMatthew Dillon * its congestion window while receiving additional duplicate 39128257b5cSMatthew Dillon * acks after fast-retransmit because TCP will reset snd_nxt 39228257b5cSMatthew Dillon * to snd_max after the fast-retransmit. 39328257b5cSMatthew Dillon * 39428257b5cSMatthew Dillon * In the normal retransmit-FIN-only case, however, snd_nxt will 39528257b5cSMatthew Dillon * be set to snd_una, the offset will be 0, and the length may 39628257b5cSMatthew Dillon * wind up 0. 3976d90faf3SPaul Saab * 3986d90faf3SPaul Saab * If sack_rxmit is true we are retransmitting from the scoreboard 3996d90faf3SPaul Saab * in which case len is already set. 40028257b5cSMatthew Dillon */ 401a55db2b6SPaul Saab if (sack_rxmit == 0) { 402a55db2b6SPaul Saab if (sack_bytes_rxmt == 0) 403cb503ae2SJonathan T. Looney len = ((int32_t)min(sbavail(&so->so_snd), sendwin) - 404cfa6009eSGleb Smirnoff off); 405a55db2b6SPaul Saab else { 4063ac12506SJonathan T. Looney int32_t cwin; 407a55db2b6SPaul Saab 408a55db2b6SPaul Saab /* 409a55db2b6SPaul Saab * We are inside of a SACK recovery episode and are 410a55db2b6SPaul Saab * sending new data, having retransmitted all the 411a55db2b6SPaul Saab * data possible in the scoreboard. 412a55db2b6SPaul Saab */ 4133ac12506SJonathan T. Looney len = ((int32_t)min(sbavail(&so->so_snd), tp->snd_wnd) - 414cfa6009eSGleb Smirnoff off); 4158d03f2b5SPaul Saab /* 4168d03f2b5SPaul Saab * Don't remove this (len > 0) check ! 4178d03f2b5SPaul Saab * We explicitly check for len > 0 here (although it 4188d03f2b5SPaul Saab * isn't really necessary), to work around a gcc 4198d03f2b5SPaul Saab * optimization issue - to force gcc to compute 4208d03f2b5SPaul Saab * len above. Without this check, the computation 4218d03f2b5SPaul Saab * of len is bungled by the optimizer. 4228d03f2b5SPaul Saab */ 4238d03f2b5SPaul Saab if (len > 0) { 424a743fc88SRichard Scheffenegger cwin = tp->snd_cwnd - imax(0, (int32_t) 425a743fc88SRichard Scheffenegger (tp->snd_nxt - tp->snd_recover)) - 426a55db2b6SPaul Saab sack_bytes_rxmt; 427a55db2b6SPaul Saab if (cwin < 0) 428a55db2b6SPaul Saab cwin = 0; 4293ac12506SJonathan T. Looney len = imin(len, cwin); 430a55db2b6SPaul Saab } 431a55db2b6SPaul Saab } 4328d03f2b5SPaul Saab } 433a0292f23SGarrett Wollman 434a0292f23SGarrett Wollman /* 435a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 436a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 437a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 438a0292f23SGarrett Wollman */ 439a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 4404b8e98d6SQing Li if (tp->t_state != TCPS_SYN_RECEIVED) 441a0292f23SGarrett Wollman flags &= ~TH_SYN; 442281a0fd4SPatrick Kelsey /* 443281a0fd4SPatrick Kelsey * When sending additional segments following a TFO SYN|ACK, 444281a0fd4SPatrick Kelsey * do not include the SYN bit. 445281a0fd4SPatrick Kelsey */ 44668bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 447281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 448281a0fd4SPatrick Kelsey flags &= ~TH_SYN; 449a0292f23SGarrett Wollman off--, len++; 450a0292f23SGarrett Wollman } 451a0292f23SGarrett Wollman 45281165e48SAndras Olah /* 453c94c54e4SAndre Oppermann * Be careful not to send data and/or FIN on SYN segments. 45481165e48SAndras Olah * This measure is needed to prevent interoperability problems 45581165e48SAndras Olah * with not fully conformant TCP implementations. 45681165e48SAndras Olah */ 457c94c54e4SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 45881165e48SAndras Olah len = 0; 45981165e48SAndras Olah flags &= ~TH_FIN; 46081165e48SAndras Olah } 46181165e48SAndras Olah 462281a0fd4SPatrick Kelsey /* 463c560df6fSPatrick Kelsey * On TFO sockets, ensure no data is sent in the following cases: 464c560df6fSPatrick Kelsey * 465c560df6fSPatrick Kelsey * - When retransmitting SYN|ACK on a passively-created socket 466c560df6fSPatrick Kelsey * 467c560df6fSPatrick Kelsey * - When retransmitting SYN on an actively created socket 468c560df6fSPatrick Kelsey * 469c560df6fSPatrick Kelsey * - When sending a zero-length cookie (cookie request) on an 470c560df6fSPatrick Kelsey * actively created socket 471c560df6fSPatrick Kelsey * 472c560df6fSPatrick Kelsey * - When the socket is in the CLOSED state (RST is being sent) 473281a0fd4SPatrick Kelsey */ 47468bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 475c560df6fSPatrick Kelsey (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 476c560df6fSPatrick Kelsey ((tp->t_state == TCPS_SYN_SENT) && 477c560df6fSPatrick Kelsey (tp->t_tfo_client_cookie_len == 0)) || 478281a0fd4SPatrick Kelsey (flags & TH_RST))) 479281a0fd4SPatrick Kelsey len = 0; 48047a8e865SXin LI if (len <= 0) { 481df8bae1dSRodney W. Grimes /* 482df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 483df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 48428257b5cSMatthew Dillon * len will be < 0. Otherwise, window shrank 485df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 4862d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 4872d8266afSDavid Greenman * to (closed) window, and set the persist timer 4882d8266afSDavid Greenman * if it isn't already going. If the window didn't 4892d8266afSDavid Greenman * close completely, just wait for an ACK. 49047a8e865SXin LI * 49147a8e865SXin LI * We also do a general check here to ensure that 49247a8e865SXin LI * we will set the persist timer when we have data 49347a8e865SXin LI * to send, but a 0-byte window. This makes sure 49447a8e865SXin LI * the persist timer is set even if the packet 49547a8e865SXin LI * hits one of the "goto send" lines below. 496df8bae1dSRodney W. Grimes */ 497df8bae1dSRodney W. Grimes len = 0; 49847a8e865SXin LI if ((sendwin == 0) && (TCPS_HAVEESTABLISHED(tp->t_state)) && 49908af8aacSRandall Stewart (off < (int) sbavail(&so->so_snd)) && 50008af8aacSRandall Stewart !tcp_timer_active(tp, TT_PERSIST)) { 501b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 5022d8266afSDavid Greenman tp->t_rxtshift = 0; 503df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 504b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_PERSIST)) 5052d8266afSDavid Greenman tcp_setpersist(tp); 506df8bae1dSRodney W. Grimes } 507df8bae1dSRodney W. Grimes } 50828257b5cSMatthew Dillon 5096741ecf5SAndre Oppermann /* len will be >= 0 after this point. */ 510c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 5116741ecf5SAndre Oppermann 51266492feaSGleb Smirnoff tcp_sndbuf_autoscale(tp, so, sendwin); 5136741ecf5SAndre Oppermann 5146741ecf5SAndre Oppermann /* 515ed420311SAndre Oppermann * Decide if we can use TCP Segmentation Offloading (if supported by 516ed420311SAndre Oppermann * hardware). 517b3c0f300SAndre Oppermann * 518b3c0f300SAndre Oppermann * TSO may only be used if we are in a pure bulk sending state. The 519b3c0f300SAndre Oppermann * presence of TCP-MD5, SACK retransmits, SACK advertizements and 520b3c0f300SAndre Oppermann * IP options prevent using TSO. With TSO the TCP header is the same 521b3c0f300SAndre Oppermann * (except for the sequence number) for all generated packets. This 522b3c0f300SAndre Oppermann * makes it impossible to transmit any options which vary per generated 523b3c0f300SAndre Oppermann * segment or packet. 524b6ff6724SHiren Panchasara * 525b6ff6724SHiren Panchasara * IPv4 handling has a clear separation of ip options and ip header 526b6ff6724SHiren Panchasara * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does 527b6ff6724SHiren Panchasara * the right thing below to provide length of just ip options and thus 528b6ff6724SHiren Panchasara * checking for ipoptlen is enough to decide if ip options are present. 52928257b5cSMatthew Dillon */ 530fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 5319ad0173dSBjoern A. Zeeb /* 5329ad0173dSBjoern A. Zeeb * Pre-calculate here as we save another lookup into the darknesses 5339ad0173dSBjoern A. Zeeb * of IPsec that way and can actually decide if TSO is ok. 5349ad0173dSBjoern A. Zeeb */ 535fcf59617SAndrey V. Elsukov #ifdef INET6 536fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6)) 5379eb0e832SGleb Smirnoff ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp); 538fcf59617SAndrey V. Elsukov #ifdef INET 539fcf59617SAndrey V. Elsukov else 5409ad0173dSBjoern A. Zeeb #endif 541fcf59617SAndrey V. Elsukov #endif /* INET6 */ 542fcf59617SAndrey V. Elsukov #ifdef INET 543fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) 5449eb0e832SGleb Smirnoff ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp); 545fcf59617SAndrey V. Elsukov #endif /* INET */ 546fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 547b6ff6724SHiren Panchasara #ifdef INET6 548b6ff6724SHiren Panchasara if (isipv6) 5499eb0e832SGleb Smirnoff ipoptlen = ip6_optlen(inp); 550b6ff6724SHiren Panchasara else 551b6ff6724SHiren Panchasara #endif 5529eb0e832SGleb Smirnoff if (inp->inp_options) 5539eb0e832SGleb Smirnoff ipoptlen = inp->inp_options->m_len - 554b6ff6724SHiren Panchasara offsetof(struct ipoption, ipopt_list); 555b6ff6724SHiren Panchasara else 556b6ff6724SHiren Panchasara ipoptlen = 0; 557fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 558b6ff6724SHiren Panchasara ipoptlen += ipsec_optlen; 559b6ff6724SHiren Panchasara #endif 560b6ff6724SHiren Panchasara 561ed420311SAndre Oppermann if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg && 5629e644c23SMichael Tuexen (tp->t_port == 0) && 563b3c0f300SAndre Oppermann ((tp->t_flags & TF_SIGNATURE) == 0) && 564b3c0f300SAndre Oppermann tp->rcv_numsacks == 0 && sack_rxmit == 0 && 565c560df6fSPatrick Kelsey ipoptlen == 0 && !(flags & TH_SYN)) 566b3c0f300SAndre Oppermann tso = 1; 567153e5b57SAndre Oppermann 5685d3b1b75SJayanth Vijayaraghavan if (sack_rxmit) { 569cfa6009eSGleb Smirnoff if (SEQ_LT(p->rxmit + len, tp->snd_una + sbused(&so->so_snd))) 570df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 5715d3b1b75SJayanth Vijayaraghavan } else { 572cfa6009eSGleb Smirnoff if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + 573cfa6009eSGleb Smirnoff sbused(&so->so_snd))) 5745d3b1b75SJayanth Vijayaraghavan flags &= ~TH_FIN; 5755d3b1b75SJayanth Vijayaraghavan } 576df8bae1dSRodney W. Grimes 5773ac12506SJonathan T. Looney recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 5783ac12506SJonathan T. Looney (long)TCP_MAXWIN << tp->rcv_scale); 579df8bae1dSRodney W. Grimes 580df8bae1dSRodney W. Grimes /* 581262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 582262c1c1aSMatthew Dillon * conditions when len is non-zero: 583262c1c1aSMatthew Dillon * 584b3c0f300SAndre Oppermann * - We have a full segment (or more with TSO) 585262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 586262c1c1aSMatthew Dillon * either idle or running NODELAY 587262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 588262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 589262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 590262c1c1aSMatthew Dillon * - we need to retransmit 591df8bae1dSRodney W. Grimes */ 592df8bae1dSRodney W. Grimes if (len) { 593b3c0f300SAndre Oppermann if (len >= tp->t_maxseg) 594df8bae1dSRodney W. Grimes goto send; 595262c1c1aSMatthew Dillon /* 596e3995661SRichard Scheffenegger * As the TCP header options are now 597e3995661SRichard Scheffenegger * considered when setting up the initial 598e3995661SRichard Scheffenegger * window, we would not send the last segment 599e3995661SRichard Scheffenegger * if we skip considering the option length here. 600e3995661SRichard Scheffenegger * Note: this may not work when tcp headers change 601e3995661SRichard Scheffenegger * very dynamically in the future. 602e3995661SRichard Scheffenegger */ 603e3995661SRichard Scheffenegger if ((((tp->t_flags & TF_SIGNATURE) ? 604e3995661SRichard Scheffenegger PADTCPOLEN(TCPOLEN_SIGNATURE) : 0) + 605e3995661SRichard Scheffenegger ((tp->t_flags & TF_RCVD_TSTMP) ? 606e3995661SRichard Scheffenegger PADTCPOLEN(TCPOLEN_TIMESTAMP) : 0) + 607e3995661SRichard Scheffenegger len) >= tp->t_maxseg) 608e3995661SRichard Scheffenegger goto send; 609e3995661SRichard Scheffenegger /* 610262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 611262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 612262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 613262c1c1aSMatthew Dillon * 614262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 615262c1c1aSMatthew Dillon */ 616262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 617262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 6183ac12506SJonathan T. Looney (uint32_t)len + (uint32_t)off >= sbavail(&so->so_snd) && 619262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 620df8bae1dSRodney W. Grimes goto send; 621262c1c1aSMatthew Dillon } 6222cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */ 623df8bae1dSRodney W. Grimes goto send; 624a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 625df8bae1dSRodney W. Grimes goto send; 626262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 627df8bae1dSRodney W. Grimes goto send; 6286d90faf3SPaul Saab if (sack_rxmit) 6296d90faf3SPaul Saab goto send; 630df8bae1dSRodney W. Grimes } 631df8bae1dSRodney W. Grimes 632df8bae1dSRodney W. Grimes /* 633f62563d3SAndre Oppermann * Sending of standalone window updates. 634f62563d3SAndre Oppermann * 63578f59b4bSAndre Oppermann * Window updates are important when we close our window due to a 63678f59b4bSAndre Oppermann * full socket buffer and are opening it again after the application 637f62563d3SAndre Oppermann * reads data from it. Once the window has opened again and the 638f62563d3SAndre Oppermann * remote end starts to send again the ACK clock takes over and 639f62563d3SAndre Oppermann * provides the most current window information. 640f62563d3SAndre Oppermann * 64178f59b4bSAndre Oppermann * We must avoid the silly window syndrome whereas every read 642f62563d3SAndre Oppermann * from the receive buffer, no matter how small, causes a window 643f62563d3SAndre Oppermann * update to be sent. We also should avoid sending a flurry of 644f62563d3SAndre Oppermann * window updates when the socket buffer had queued a lot of data 645f62563d3SAndre Oppermann * and the application is doing small reads. 646f62563d3SAndre Oppermann * 647f62563d3SAndre Oppermann * Prevent a flurry of pointless window updates by only sending 648f62563d3SAndre Oppermann * an update when we can increase the advertized window by more 649f62563d3SAndre Oppermann * than 1/4th of the socket buffer capacity. When the buffer is 650f62563d3SAndre Oppermann * getting full or is very small be more aggressive and send an 651f62563d3SAndre Oppermann * update whenever we can increase by two mss sized segments. 652f62563d3SAndre Oppermann * In all other situations the ACK's to new incoming data will 653f62563d3SAndre Oppermann * carry further window increases. 6544249614cSAndre Oppermann * 6554249614cSAndre Oppermann * Don't send an independent window update if a delayed 6564249614cSAndre Oppermann * ACK is pending (it will get piggy-backed on it) or the 6574249614cSAndre Oppermann * remote side already has done a half-close and won't send 658f62563d3SAndre Oppermann * more data. Skip this if the connection is in T/TCP 659f62563d3SAndre Oppermann * half-open state. 660df8bae1dSRodney W. Grimes */ 661b7de7d87SAndre Oppermann if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 6624249614cSAndre Oppermann !(tp->t_flags & TF_DELACK) && 663b7de7d87SAndre Oppermann !TCPS_HAVERCVDFIN(tp->t_state)) { 664df8bae1dSRodney W. Grimes /* 665f62563d3SAndre Oppermann * "adv" is the amount we could increase the window, 666df8bae1dSRodney W. Grimes * taking into account that we are limited by 667df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 668df8bae1dSRodney W. Grimes */ 6693ac12506SJonathan T. Looney int32_t adv; 670f701e30dSJohn Baldwin int oldwin; 671f701e30dSJohn Baldwin 6723ac12506SJonathan T. Looney adv = recwin; 673f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 674f701e30dSJohn Baldwin oldwin = (tp->rcv_adv - tp->rcv_nxt); 675f092a3c7SRandall Stewart if (adv > oldwin) 676f701e30dSJohn Baldwin adv -= oldwin; 677f092a3c7SRandall Stewart else 678f092a3c7SRandall Stewart adv = 0; 679f701e30dSJohn Baldwin } else 680f701e30dSJohn Baldwin oldwin = 0; 681df8bae1dSRodney W. Grimes 682da84b2e6SJohn Baldwin /* 6830dda76b8SJonathan T. Looney * If the new window size ends up being the same as or less 6840dda76b8SJonathan T. Looney * than the old size when it is scaled, then don't force 6850dda76b8SJonathan T. Looney * a window update. 686da84b2e6SJohn Baldwin */ 6870dda76b8SJonathan T. Looney if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 688da84b2e6SJohn Baldwin goto dontupdate; 689f62563d3SAndre Oppermann 6903ac12506SJonathan T. Looney if (adv >= (int32_t)(2 * tp->t_maxseg) && 6913ac12506SJonathan T. Looney (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) || 6923ac12506SJonathan T. Looney recwin <= (so->so_rcv.sb_hiwat / 8) || 69350075939SStephen Hurd so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg || 69450075939SStephen Hurd adv >= TCP_MAXWIN << tp->rcv_scale)) 695df8bae1dSRodney W. Grimes goto send; 6968d62aae8SMichael Tuexen if (2 * adv >= (int32_t)so->so_rcv.sb_hiwat) 6978d62aae8SMichael Tuexen goto send; 698df8bae1dSRodney W. Grimes } 699da84b2e6SJohn Baldwin dontupdate: 700df8bae1dSRodney W. Grimes 701df8bae1dSRodney W. Grimes /* 70228257b5cSMatthew Dillon * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 70328257b5cSMatthew Dillon * is also a catch-all for the retransmit timer timeout case. 704df8bae1dSRodney W. Grimes */ 705df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 706df8bae1dSRodney W. Grimes goto send; 707a0292f23SGarrett Wollman if ((flags & TH_RST) || 708a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 709df8bae1dSRodney W. Grimes goto send; 710df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 711df8bae1dSRodney W. Grimes goto send; 712df8bae1dSRodney W. Grimes /* 713df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 71428257b5cSMatthew Dillon * and we have not yet done so, then we need to send. 715df8bae1dSRodney W. Grimes */ 716df8bae1dSRodney W. Grimes if (flags & TH_FIN && 717df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 718df8bae1dSRodney W. Grimes goto send; 7196d90faf3SPaul Saab /* 7206d90faf3SPaul Saab * In SACK, it is possible for tcp_output to fail to send a segment 7216d90faf3SPaul Saab * after the retransmission timer has been turned off. Make sure 7226d90faf3SPaul Saab * that the retransmission timer is set. 7236d90faf3SPaul Saab */ 7243529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 7253529149eSAndre Oppermann SEQ_GT(tp->snd_max, tp->snd_una) && 726b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_REXMT) && 727b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 72808af8aacSRandall Stewart tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp)); 729cf2942b6SRobert Watson goto just_return; 7306d90faf3SPaul Saab } 731df8bae1dSRodney W. Grimes /* 732df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 733df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 734df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 735df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 736df8bae1dSRodney W. Grimes * persisting to move a small or zero window 737df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 738df8bae1dSRodney W. Grimes * 739b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_PERSIST) 7409b8b58e0SJonathan Lemon * is true when we are in persist state. 7412cdbfa66SPaul Saab * (tp->t_flags & TF_FORCEDATA) 742df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 743b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_REXMT) 744df8bae1dSRodney W. Grimes * is set when we are retransmitting 745df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 746df8bae1dSRodney W. Grimes * 747df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 748df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 749df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 750df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 751df8bae1dSRodney W. Grimes * otherwise force out a byte. 752df8bae1dSRodney W. Grimes */ 753cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd) && !tcp_timer_active(tp, TT_REXMT) && 754b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 755df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 756df8bae1dSRodney W. Grimes tcp_setpersist(tp); 757df8bae1dSRodney W. Grimes } 758df8bae1dSRodney W. Grimes 759df8bae1dSRodney W. Grimes /* 760df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 761df8bae1dSRodney W. Grimes */ 762cf2942b6SRobert Watson just_return: 763cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 764df8bae1dSRodney W. Grimes return (0); 765df8bae1dSRodney W. Grimes 766df8bae1dSRodney W. Grimes send: 767cf2942b6SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 768f6f6703fSSean Bruno if (len > 0) { 769f6f6703fSSean Bruno if (len >= tp->t_maxseg) 770f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 771f6f6703fSSean Bruno else 772f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 773f6f6703fSSean Bruno } 774df8bae1dSRodney W. Grimes /* 775df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 776df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 777df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 778df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 779df8bae1dSRodney W. Grimes * link header, i.e. 78033841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 781df8bae1dSRodney W. Grimes */ 782df8bae1dSRodney W. Grimes optlen = 0; 783fb59c426SYoshinobu Inoue #ifdef INET6 784fb59c426SYoshinobu Inoue if (isipv6) 785fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 786fb59c426SYoshinobu Inoue else 787fb59c426SYoshinobu Inoue #endif 788df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 78902a1a643SAndre Oppermann 790ed782b9fSMichael Tuexen if (flags & TH_SYN) { 791ed782b9fSMichael Tuexen tp->snd_nxt = tp->iss; 792ed782b9fSMichael Tuexen } 793ed782b9fSMichael Tuexen 79402a1a643SAndre Oppermann /* 79502a1a643SAndre Oppermann * Compute options for segment. 79602a1a643SAndre Oppermann * We only have to care about SYN and established connection 79702a1a643SAndre Oppermann * segments. Options for SYN-ACK segments are handled in TCP 79802a1a643SAndre Oppermann * syncache. 79902a1a643SAndre Oppermann */ 80002a1a643SAndre Oppermann to.to_flags = 0; 801f73d9fd2SGleb Smirnoff if ((tp->t_flags & TF_NOOPT) == 0) { 80202a1a643SAndre Oppermann /* Maximum segment size. */ 803df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 8049eb0e832SGleb Smirnoff to.to_mss = tcp_mssopt(&inp->inp_inc); 8059e644c23SMichael Tuexen if (tp->t_port) 8069e644c23SMichael Tuexen to.to_mss -= V_tcp_udp_tunneling_overhead; 80702a1a643SAndre Oppermann to.to_flags |= TOF_MSS; 80818a75309SPatrick Kelsey 809281a0fd4SPatrick Kelsey /* 810c560df6fSPatrick Kelsey * On SYN or SYN|ACK transmits on TFO connections, 811c560df6fSPatrick Kelsey * only include the TFO option if it is not a 812c560df6fSPatrick Kelsey * retransmit, as the presence of the TFO option may 813c560df6fSPatrick Kelsey * have caused the original SYN or SYN|ACK to have 814c560df6fSPatrick Kelsey * been dropped by a middlebox. 815281a0fd4SPatrick Kelsey */ 81668bd7ed1SJonathan T. Looney if (IS_FASTOPEN(tp->t_flags) && 817281a0fd4SPatrick Kelsey (tp->t_rxtshift == 0)) { 818c560df6fSPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED) { 819281a0fd4SPatrick Kelsey to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 820c560df6fSPatrick Kelsey to.to_tfo_cookie = 821c560df6fSPatrick Kelsey (u_int8_t *)&tp->t_tfo_cookie.server; 822281a0fd4SPatrick Kelsey to.to_flags |= TOF_FASTOPEN; 823c560df6fSPatrick Kelsey wanted_cookie = 1; 824c560df6fSPatrick Kelsey } else if (tp->t_state == TCPS_SYN_SENT) { 825c560df6fSPatrick Kelsey to.to_tfo_len = 826c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len; 827c560df6fSPatrick Kelsey to.to_tfo_cookie = 828c560df6fSPatrick Kelsey tp->t_tfo_cookie.client; 829c560df6fSPatrick Kelsey to.to_flags |= TOF_FASTOPEN; 830c560df6fSPatrick Kelsey wanted_cookie = 1; 831c560df6fSPatrick Kelsey /* 832c560df6fSPatrick Kelsey * If we wind up having more data to 833c560df6fSPatrick Kelsey * send with the SYN than can fit in 834c560df6fSPatrick Kelsey * one segment, don't send any more 835c560df6fSPatrick Kelsey * until the SYN|ACK comes back from 836c560df6fSPatrick Kelsey * the other end. 837c560df6fSPatrick Kelsey */ 838c560df6fSPatrick Kelsey dont_sendalot = 1; 839c560df6fSPatrick Kelsey } 840281a0fd4SPatrick Kelsey } 841df8bae1dSRodney W. Grimes } 84202a1a643SAndre Oppermann /* Window scaling. */ 84302a1a643SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 84402a1a643SAndre Oppermann to.to_wscale = tp->request_r_scale; 84502a1a643SAndre Oppermann to.to_flags |= TOF_SCALE; 846df8bae1dSRodney W. Grimes } 84702a1a643SAndre Oppermann /* Timestamps. */ 84802a1a643SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) || 84902a1a643SAndre Oppermann ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 85010d20c84SMatt Macy curticks = tcp_ts_getticks(); 85110d20c84SMatt Macy to.to_tsval = curticks + tp->ts_offset; 85202a1a643SAndre Oppermann to.to_tsecr = tp->ts_recent; 85302a1a643SAndre Oppermann to.to_flags |= TOF_TS; 85410d20c84SMatt Macy if (tp->t_rxtshift == 1) 85510d20c84SMatt Macy tp->t_badrxtwin = curticks; 856e44c1887SSteven Hartland } 857e44c1887SSteven Hartland 8586741ecf5SAndre Oppermann /* Set receive buffer autosizing timestamp. */ 85902a1a643SAndre Oppermann if (tp->rfbuf_ts == 0 && 86002a1a643SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) 861d8951c8aSBjoern A. Zeeb tp->rfbuf_ts = tcp_ts_getticks(); 862e44c1887SSteven Hartland 86302a1a643SAndre Oppermann /* Selective ACK's. */ 8643529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 86502a1a643SAndre Oppermann if (flags & TH_SYN) 86602a1a643SAndre Oppermann to.to_flags |= TOF_SACKPERM; 86702a1a643SAndre Oppermann else if (TCPS_HAVEESTABLISHED(tp->t_state) && 86802a1a643SAndre Oppermann tp->rcv_numsacks > 0) { 86902a1a643SAndre Oppermann to.to_flags |= TOF_SACK; 87002a1a643SAndre Oppermann to.to_nsacks = tp->rcv_numsacks; 87102a1a643SAndre Oppermann to.to_sacks = (u_char *)tp->sackblks; 87202a1a643SAndre Oppermann } 87302a1a643SAndre Oppermann } 874fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 87502a1a643SAndre Oppermann /* TCP-MD5 (RFC2385). */ 876fcf59617SAndrey V. Elsukov /* 877fcf59617SAndrey V. Elsukov * Check that TCP_MD5SIG is enabled in tcpcb to 878fcf59617SAndrey V. Elsukov * account the size needed to set this TCP option. 879fcf59617SAndrey V. Elsukov */ 88002a1a643SAndre Oppermann if (tp->t_flags & TF_SIGNATURE) 88102a1a643SAndre Oppermann to.to_flags |= TOF_SIGNATURE; 8821cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 8831cfd4b53SBruce M Simpson 88402a1a643SAndre Oppermann /* Processing the options. */ 8854a411b9fSBjoern A. Zeeb hdrlen += optlen = tcp_addoptions(&to, opt); 886c560df6fSPatrick Kelsey /* 887c560df6fSPatrick Kelsey * If we wanted a TFO option to be added, but it was unable 888c560df6fSPatrick Kelsey * to fit, ensure no data is sent. 889c560df6fSPatrick Kelsey */ 890c560df6fSPatrick Kelsey if (IS_FASTOPEN(tp->t_flags) && wanted_cookie && 891c560df6fSPatrick Kelsey !(to.to_flags & TOF_FASTOPEN)) 892c560df6fSPatrick Kelsey len = 0; 893be3f3b5eSPaul Saab } 8949e644c23SMichael Tuexen if (tp->t_port) { 8959e644c23SMichael Tuexen if (V_tcp_udp_tunneling_port == 0) { 8969e644c23SMichael Tuexen /* The port was removed?? */ 8979e644c23SMichael Tuexen SOCKBUF_UNLOCK(&so->so_snd); 8989e644c23SMichael Tuexen return (EHOSTUNREACH); 8999e644c23SMichael Tuexen } 9009e644c23SMichael Tuexen hdrlen += sizeof(struct udphdr); 9019e644c23SMichael Tuexen } 902df8bae1dSRodney W. Grimes /* 903df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 9040c39d38dSGleb Smirnoff * bump the packet length beyond the t_maxseg length. 905a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 906a0292f23SGarrett Wollman * the segment. 907df8bae1dSRodney W. Grimes */ 9080c39d38dSGleb Smirnoff if (len + optlen + ipoptlen > tp->t_maxseg) { 909297a37f3SDavid Greenman flags &= ~TH_FIN; 910ed420311SAndre Oppermann 911b3c0f300SAndre Oppermann if (tso) { 9129fd573c3SHans Petter Selasky u_int if_hw_tsomax; 9139fd573c3SHans Petter Selasky u_int moff; 9149fd573c3SHans Petter Selasky int max_len; 9159fd573c3SHans Petter Selasky 9169fd573c3SHans Petter Selasky /* extract TSO information */ 9179fd573c3SHans Petter Selasky if_hw_tsomax = tp->t_tsomax; 9189fd573c3SHans Petter Selasky if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 9199fd573c3SHans Petter Selasky if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 9209fd573c3SHans Petter Selasky 9219fd573c3SHans Petter Selasky /* 9229fd573c3SHans Petter Selasky * Limit a TSO burst to prevent it from 9239fd573c3SHans Petter Selasky * overflowing or exceeding the maximum length 9249fd573c3SHans Petter Selasky * allowed by the network interface: 9259fd573c3SHans Petter Selasky */ 926ed420311SAndre Oppermann KASSERT(ipoptlen == 0, 927ed420311SAndre Oppermann ("%s: TSO can't do IP options", __func__)); 928ed420311SAndre Oppermann 929ed420311SAndre Oppermann /* 9309fd573c3SHans Petter Selasky * Check if we should limit by maximum payload 9319fd573c3SHans Petter Selasky * length: 932ed420311SAndre Oppermann */ 9339fd573c3SHans Petter Selasky if (if_hw_tsomax != 0) { 9349fd573c3SHans Petter Selasky /* compute maximum TSO length */ 935d76d4012SHans Petter Selasky max_len = (if_hw_tsomax - hdrlen - 936d76d4012SHans Petter Selasky max_linkhdr); 9379fd573c3SHans Petter Selasky if (max_len <= 0) { 9389fd573c3SHans Petter Selasky len = 0; 9393c7c188cSHans Petter Selasky } else if (len > max_len) { 940b3c0f300SAndre Oppermann sendalot = 1; 9413c7c188cSHans Petter Selasky len = max_len; 9429fd573c3SHans Petter Selasky } 9439fd573c3SHans Petter Selasky } 94474e10fb6SJohn Baldwin 945ed420311SAndre Oppermann /* 946ed420311SAndre Oppermann * Prevent the last segment from being 9479fd573c3SHans Petter Selasky * fractional unless the send sockbuf can be 9489fd573c3SHans Petter Selasky * emptied: 949ed420311SAndre Oppermann */ 9500c39d38dSGleb Smirnoff max_len = (tp->t_maxseg - optlen); 9513ac12506SJonathan T. Looney if (((uint32_t)off + (uint32_t)len) < 9523ac12506SJonathan T. Looney sbavail(&so->so_snd)) { 9533c7c188cSHans Petter Selasky moff = len % max_len; 9549fd573c3SHans Petter Selasky if (moff != 0) { 9559fd573c3SHans Petter Selasky len -= moff; 956b3c0f300SAndre Oppermann sendalot = 1; 957ed420311SAndre Oppermann } 9589fd573c3SHans Petter Selasky } 9599fd573c3SHans Petter Selasky 9609fd573c3SHans Petter Selasky /* 9619fd573c3SHans Petter Selasky * In case there are too many small fragments 9629fd573c3SHans Petter Selasky * don't use TSO: 9639fd573c3SHans Petter Selasky */ 9643c7c188cSHans Petter Selasky if (len <= max_len) { 9653c7c188cSHans Petter Selasky len = max_len; 9669fd573c3SHans Petter Selasky sendalot = 1; 9679fd573c3SHans Petter Selasky tso = 0; 9689fd573c3SHans Petter Selasky } 969ed420311SAndre Oppermann 970ed420311SAndre Oppermann /* 971ed420311SAndre Oppermann * Send the FIN in a separate segment 972ed420311SAndre Oppermann * after the bulk sending is done. 973ed420311SAndre Oppermann * We don't trust the TSO implementations 974ed420311SAndre Oppermann * to clear the FIN flag on all but the 975ed420311SAndre Oppermann * last segment. 976ed420311SAndre Oppermann */ 977ed420311SAndre Oppermann if (tp->t_flags & TF_NEEDFIN) 978ed420311SAndre Oppermann sendalot = 1; 979b3c0f300SAndre Oppermann } else { 98012a43d0dSMichael Tuexen if (optlen + ipoptlen >= tp->t_maxseg) { 98112a43d0dSMichael Tuexen /* 98212a43d0dSMichael Tuexen * Since we don't have enough space to put 98312a43d0dSMichael Tuexen * the IP header chain and the TCP header in 98412a43d0dSMichael Tuexen * one packet as required by RFC 7112, don't 98512a43d0dSMichael Tuexen * send it. Also ensure that at least one 98612a43d0dSMichael Tuexen * byte of the payload can be put into the 98712a43d0dSMichael Tuexen * TCP segment. 98812a43d0dSMichael Tuexen */ 98912a43d0dSMichael Tuexen SOCKBUF_UNLOCK(&so->so_snd); 99012a43d0dSMichael Tuexen error = EMSGSIZE; 99112a43d0dSMichael Tuexen sack_rxmit = 0; 99212a43d0dSMichael Tuexen goto out; 99312a43d0dSMichael Tuexen } 9940c39d38dSGleb Smirnoff len = tp->t_maxseg - optlen - ipoptlen; 995df8bae1dSRodney W. Grimes sendalot = 1; 996c560df6fSPatrick Kelsey if (dont_sendalot) 997c560df6fSPatrick Kelsey sendalot = 0; 998df8bae1dSRodney W. Grimes } 999ed420311SAndre Oppermann } else 1000ed420311SAndre Oppermann tso = 0; 1001ed420311SAndre Oppermann 1002ed420311SAndre Oppermann KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 1003ed420311SAndre Oppermann ("%s: len > IP_MAXPACKET", __func__)); 1004df8bae1dSRodney W. Grimes 1005a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 1006a683a7ddSYoshinobu Inoue #ifdef INET6 1007a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 1008a683a7ddSYoshinobu Inoue #else 1009df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 1010a683a7ddSYoshinobu Inoue #endif 1011f10e85d7SLuigi Rizzo panic("tcphdr too big"); 1012a0292f23SGarrett Wollman /*#endif*/ 1013df8bae1dSRodney W. Grimes 1014df8bae1dSRodney W. Grimes /* 1015c4982faeSBjoern A. Zeeb * This KASSERT is here to catch edge cases at a well defined place. 1016c4982faeSBjoern A. Zeeb * Before, those had triggered (random) panic conditions further down. 1017c4982faeSBjoern A. Zeeb */ 1018c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 1019c4982faeSBjoern A. Zeeb 1020c4982faeSBjoern A. Zeeb /* 1021df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 1022df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 1023df8bae1dSRodney W. Grimes * the template for sends on this connection. 1024df8bae1dSRodney W. Grimes */ 1025df8bae1dSRodney W. Grimes if (len) { 10264e023759SAndre Oppermann struct mbuf *mb; 1027581a046aSRandall Stewart struct sockbuf *msb; 10284e023759SAndre Oppermann u_int moff; 10294e023759SAndre Oppermann 1030adc56f5aSEdward Tomasz Napierala if ((tp->t_flags & TF_FORCEDATA) && len == 1) { 103178b50714SRobert Watson TCPSTAT_INC(tcps_sndprobe); 1032adc56f5aSEdward Tomasz Napierala #ifdef STATS 1033adc56f5aSEdward Tomasz Napierala if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1034adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, 1035adc56f5aSEdward Tomasz Napierala VOI_TCP_RETXPB, len); 1036adc56f5aSEdward Tomasz Napierala else 1037adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u64(tp->t_stats, 1038adc56f5aSEdward Tomasz Napierala VOI_TCP_TXPB, len); 1039adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 1040adc56f5aSEdward Tomasz Napierala } else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) { 1041f5d34df5SGeorge V. Neville-Neil tp->t_sndrexmitpack++; 104278b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitpack); 104378b50714SRobert Watson TCPSTAT_ADD(tcps_sndrexmitbyte, len); 1044adc56f5aSEdward Tomasz Napierala #ifdef STATS 1045adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, VOI_TCP_RETXPB, 1046adc56f5aSEdward Tomasz Napierala len); 1047adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 1048df8bae1dSRodney W. Grimes } else { 104978b50714SRobert Watson TCPSTAT_INC(tcps_sndpack); 105078b50714SRobert Watson TCPSTAT_ADD(tcps_sndbyte, len); 1051adc56f5aSEdward Tomasz Napierala #ifdef STATS 1052adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u64(tp->t_stats, VOI_TCP_TXPB, 1053adc56f5aSEdward Tomasz Napierala len); 1054adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 1055df8bae1dSRodney W. Grimes } 105639f6074eSGleb Smirnoff #ifdef INET6 105739f6074eSGleb Smirnoff if (MHLEN < hdrlen + max_linkhdr) 105839f6074eSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 105939f6074eSGleb Smirnoff else 106039f6074eSGleb Smirnoff #endif 106139f6074eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 106239f6074eSGleb Smirnoff 1063df8bae1dSRodney W. Grimes if (m == NULL) { 1064cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1065df8bae1dSRodney W. Grimes error = ENOBUFS; 10660e2bc05cSGleb Smirnoff sack_rxmit = 0; 1067df8bae1dSRodney W. Grimes goto out; 1068df8bae1dSRodney W. Grimes } 106939f6074eSGleb Smirnoff 1070df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1071df8bae1dSRodney W. Grimes m->m_len = hdrlen; 10724e023759SAndre Oppermann 10734e023759SAndre Oppermann /* 10744e023759SAndre Oppermann * Start the m_copy functions from the closest mbuf 10754e023759SAndre Oppermann * to the offset in the socket buffer chain. 10764e023759SAndre Oppermann */ 1077581a046aSRandall Stewart mb = sbsndptr_noadv(&so->so_snd, off, &moff); 1078b2e60773SJohn Baldwin if (len <= MHLEN - hdrlen - max_linkhdr && !hw_tls) { 10793ac12506SJonathan T. Looney m_copydata(mb, moff, len, 1080df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 1081581a046aSRandall Stewart if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1082581a046aSRandall Stewart sbsndptr_adv(&so->so_snd, mb, len); 1083df8bae1dSRodney W. Grimes m->m_len += len; 1084df8bae1dSRodney W. Grimes } else { 1085581a046aSRandall Stewart if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1086581a046aSRandall Stewart msb = NULL; 1087581a046aSRandall Stewart else 1088581a046aSRandall Stewart msb = &so->so_snd; 1089581a046aSRandall Stewart m->m_next = tcp_m_copym(mb, moff, 1090581a046aSRandall Stewart &len, if_hw_tsomaxsegcount, 1091b2e60773SJohn Baldwin if_hw_tsomaxsegsize, msb, hw_tls); 1092581a046aSRandall Stewart if (len <= (tp->t_maxseg - optlen)) { 1093581a046aSRandall Stewart /* 1094581a046aSRandall Stewart * Must have ran out of mbufs for the copy 1095581a046aSRandall Stewart * shorten it to no longer need tso. Lets 1096581a046aSRandall Stewart * not put on sendalot since we are low on 1097581a046aSRandall Stewart * mbufs. 1098581a046aSRandall Stewart */ 1099581a046aSRandall Stewart tso = 0; 1100581a046aSRandall Stewart } 11014e023759SAndre Oppermann if (m->m_next == NULL) { 1102cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1103d7f570e6SGarrett Wollman (void) m_free(m); 110451823c3aSGarrett Wollman error = ENOBUFS; 11050e2bc05cSGleb Smirnoff sack_rxmit = 0; 110651823c3aSGarrett Wollman goto out; 110751823c3aSGarrett Wollman } 1108df8bae1dSRodney W. Grimes } 1109472ea5beSColin Percival 1110df8bae1dSRodney W. Grimes /* 1111df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 1112df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 1113df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 1114df8bae1dSRodney W. Grimes * a PUSH comes in.) 1115df8bae1dSRodney W. Grimes */ 11163ac12506SJonathan T. Looney if (((uint32_t)off + (uint32_t)len == sbused(&so->so_snd)) && 11173ac12506SJonathan T. Looney !(flags & TH_SYN)) 1118df8bae1dSRodney W. Grimes flags |= TH_PUSH; 1119cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1120df8bae1dSRodney W. Grimes } else { 1121cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 1122df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 112378b50714SRobert Watson TCPSTAT_INC(tcps_sndacks); 1124df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 112578b50714SRobert Watson TCPSTAT_INC(tcps_sndctrl); 1126df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 112778b50714SRobert Watson TCPSTAT_INC(tcps_sndurg); 1128df8bae1dSRodney W. Grimes else 112978b50714SRobert Watson TCPSTAT_INC(tcps_sndwinup); 1130df8bae1dSRodney W. Grimes 1131aa8bd99dSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 1132df8bae1dSRodney W. Grimes if (m == NULL) { 1133df8bae1dSRodney W. Grimes error = ENOBUFS; 11340e2bc05cSGleb Smirnoff sack_rxmit = 0; 1135df8bae1dSRodney W. Grimes goto out; 1136df8bae1dSRodney W. Grimes } 1137fb59c426SYoshinobu Inoue #ifdef INET6 1138fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 1139fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 1140ed6a66caSRobert Watson M_ALIGN(m, hdrlen); 1141fb59c426SYoshinobu Inoue } else 1142fb59c426SYoshinobu Inoue #endif 1143df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1144df8bae1dSRodney W. Grimes m->m_len = hdrlen; 1145df8bae1dSRodney W. Grimes } 1146cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); 1147df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 1148c488362eSRobert Watson #ifdef MAC 11499eb0e832SGleb Smirnoff mac_inpcb_create_mbuf(inp, m); 1150c488362eSRobert Watson #endif 1151fb59c426SYoshinobu Inoue #ifdef INET6 1152fb59c426SYoshinobu Inoue if (isipv6) { 1153fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 11549e644c23SMichael Tuexen if (tp->t_port) { 1155500eb6ddSMichael Tuexen udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 11569e644c23SMichael Tuexen udp->uh_sport = htons(V_tcp_udp_tunneling_port); 11579e644c23SMichael Tuexen udp->uh_dport = tp->t_port; 11589e644c23SMichael Tuexen ulen = hdrlen + len - sizeof(struct ip6_hdr); 11599e644c23SMichael Tuexen udp->uh_ulen = htons(ulen); 11609e644c23SMichael Tuexen th = (struct tcphdr *)(udp + 1); 11619e644c23SMichael Tuexen } else { 1162fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 11639e644c23SMichael Tuexen } 11649eb0e832SGleb Smirnoff tcpip_fillheaders(inp, tp->t_port, ip6, th); 1165fb59c426SYoshinobu Inoue } else 1166fb59c426SYoshinobu Inoue #endif /* INET6 */ 1167fb59c426SYoshinobu Inoue { 1168fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 11699e644c23SMichael Tuexen if (tp->t_port) { 1170500eb6ddSMichael Tuexen udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 11719e644c23SMichael Tuexen udp->uh_sport = htons(V_tcp_udp_tunneling_port); 11729e644c23SMichael Tuexen udp->uh_dport = tp->t_port; 11739e644c23SMichael Tuexen ulen = hdrlen + len - sizeof(struct ip); 11749e644c23SMichael Tuexen udp->uh_ulen = htons(ulen); 11759e644c23SMichael Tuexen th = (struct tcphdr *)(udp + 1); 11769e644c23SMichael Tuexen } else 1177fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 11789eb0e832SGleb Smirnoff tcpip_fillheaders(inp, tp->t_port, ip, th); 1179fb59c426SYoshinobu Inoue } 1180df8bae1dSRodney W. Grimes 1181df8bae1dSRodney W. Grimes /* 1182df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 1183df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 1184df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 1185df8bae1dSRodney W. Grimes */ 1186df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 1187df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 1188df8bae1dSRodney W. Grimes tp->snd_nxt--; 1189df8bae1dSRodney W. Grimes /* 1190f2512ba1SRui Paulo * If we are starting a connection, send ECN setup 1191f2512ba1SRui Paulo * SYN packet. If we are on a retransmit, we may 1192f2512ba1SRui Paulo * resend those bits a number of times as per 1193f2512ba1SRui Paulo * RFC 3168. 1194f2512ba1SRui Paulo */ 1195f7220c48SRichard Scheffenegger if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) { 1196f7220c48SRichard Scheffenegger flags |= tcp_ecn_output_syn_sent(tp); 1197f2512ba1SRui Paulo } 1198f7220c48SRichard Scheffenegger /* Also handle parallel SYN for ECN */ 1199f7220c48SRichard Scheffenegger if ((TCPS_HAVERCVDSYN(tp->t_state)) && 12004012ef77SRichard Scheffenegger (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) { 12012ff07d92SRichard Scheffenegger int ect = tcp_ecn_output_established(tp, &flags, len, sack_rxmit); 1202f7220c48SRichard Scheffenegger if ((tp->t_state == TCPS_SYN_RECEIVED) && 1203f7220c48SRichard Scheffenegger (tp->t_flags2 & TF2_ECN_SND_ECE)) 1204f7220c48SRichard Scheffenegger tp->t_flags2 &= ~TF2_ECN_SND_ECE; 1205f2512ba1SRui Paulo #ifdef INET6 1206f026275eSRichard Scheffenegger if (isipv6) { 1207f026275eSRichard Scheffenegger ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << 20); 1208f7220c48SRichard Scheffenegger ip6->ip6_flow |= htonl(ect << 20); 1209f026275eSRichard Scheffenegger } 1210f2512ba1SRui Paulo else 1211f2512ba1SRui Paulo #endif 1212f026275eSRichard Scheffenegger { 1213f026275eSRichard Scheffenegger ip->ip_tos &= ~IPTOS_ECN_MASK; 1214f7220c48SRichard Scheffenegger ip->ip_tos |= ect; 1215f026275eSRichard Scheffenegger } 1216f2512ba1SRui Paulo } 1217f2512ba1SRui Paulo 1218f2512ba1SRui Paulo /* 1219df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 1220df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 1221df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 1222df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 1223df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 1224df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 1225df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 1226df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 1227df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 1228df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 1229df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 1230df8bae1dSRodney W. Grimes */ 1231a55db2b6SPaul Saab if (sack_rxmit == 0) { 1232b8152ba7SAndre Oppermann if (len || (flags & (TH_SYN|TH_FIN)) || 1233b8152ba7SAndre Oppermann tcp_timer_active(tp, TT_PERSIST)) 1234fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 1235df8bae1dSRodney W. Grimes else 1236fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 1237a55db2b6SPaul Saab } else { 12386d90faf3SPaul Saab th->th_seq = htonl(p->rxmit); 12396d90faf3SPaul Saab p->rxmit += len; 12400471a8c7SRichard Scheffenegger /* 12410471a8c7SRichard Scheffenegger * Lost Retransmission Detection 12420471a8c7SRichard Scheffenegger * trigger resending of a (then 12430471a8c7SRichard Scheffenegger * still existing) hole, when 12440471a8c7SRichard Scheffenegger * fack acks recoverypoint. 12450471a8c7SRichard Scheffenegger */ 12460471a8c7SRichard Scheffenegger if ((tp->t_flags & TF_LRD) && SEQ_GEQ(p->rxmit, p->end)) 12470471a8c7SRichard Scheffenegger p->rxmit = tp->snd_recover; 12480077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += len; 12496d90faf3SPaul Saab } 1250e5313869SRichard Scheffenegger if (IN_RECOVERY(tp->t_flags)) { 1251e5313869SRichard Scheffenegger /* 1252e5313869SRichard Scheffenegger * Account all bytes transmitted while 1253e5313869SRichard Scheffenegger * IN_RECOVERY, simplifying PRR and 1254e5313869SRichard Scheffenegger * Lost Retransmit Detection 1255e5313869SRichard Scheffenegger */ 1256e5313869SRichard Scheffenegger tp->sackhint.prr_out += len; 1257e5313869SRichard Scheffenegger } 1258fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 1259df8bae1dSRodney W. Grimes if (optlen) { 1260fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 1261fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 1262df8bae1dSRodney W. Grimes } 12631ebf4607SRichard Scheffenegger tcp_set_flags(th, flags); 1264df8bae1dSRodney W. Grimes /* 1265df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 1266df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 126779410718SMichael Tuexen * If a RST segment is sent, advertise a window of zero. 1268df8bae1dSRodney W. Grimes */ 126979410718SMichael Tuexen if (flags & TH_RST) { 127079410718SMichael Tuexen recwin = 0; 127179410718SMichael Tuexen } else { 12723ac12506SJonathan T. Looney if (recwin < (so->so_rcv.sb_hiwat / 4) && 12733ac12506SJonathan T. Looney recwin < tp->t_maxseg) 1274201d185bSAndre Oppermann recwin = 0; 1275f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 12763ac12506SJonathan T. Looney recwin < (tp->rcv_adv - tp->rcv_nxt)) 12773ac12506SJonathan T. Looney recwin = (tp->rcv_adv - tp->rcv_nxt); 127879410718SMichael Tuexen } 1279104ebb2aSAndre Oppermann /* 1280104ebb2aSAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1281104ebb2aSAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> 1282104ebb2aSAndre Oppermann * case is handled in syncache. 1283104ebb2aSAndre Oppermann */ 1284104ebb2aSAndre Oppermann if (flags & TH_SYN) 1285104ebb2aSAndre Oppermann th->th_win = htons((u_short) 1286104ebb2aSAndre Oppermann (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 12879028b6e0SRichard Scheffenegger else { 12889028b6e0SRichard Scheffenegger /* Avoid shrinking window with window scaling. */ 12899028b6e0SRichard Scheffenegger recwin = roundup2(recwin, 1 << tp->rcv_scale); 1290104ebb2aSAndre Oppermann th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 12919028b6e0SRichard Scheffenegger } 1292262c1c1aSMatthew Dillon 1293262c1c1aSMatthew Dillon /* 1294262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 1295262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 1296262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 1297262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 1298b2722702SRui Paulo * to read more data than can be buffered prior to transmitting on 1299262c1c1aSMatthew Dillon * the connection. 1300262c1c1aSMatthew Dillon */ 1301f5d34df5SGeorge V. Neville-Neil if (th->th_win == 0) { 1302f5d34df5SGeorge V. Neville-Neil tp->t_sndzerowin++; 1303262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 1304f5d34df5SGeorge V. Neville-Neil } else 1305262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 1306df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 1307fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 1308fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 1309df8bae1dSRodney W. Grimes } else 1310df8bae1dSRodney W. Grimes /* 1311df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 1312df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 1313df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 1314df8bae1dSRodney W. Grimes * number wraparound. 1315df8bae1dSRodney W. Grimes */ 1316df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 1317df8bae1dSRodney W. Grimes 1318df8bae1dSRodney W. Grimes /* 1319df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 1320df8bae1dSRodney W. Grimes * checksum extended header and data. 1321df8bae1dSRodney W. Grimes */ 1322fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 1323fcf59617SAndrey V. Elsukov 1324fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1325fcf59617SAndrey V. Elsukov if (to.to_flags & TOF_SIGNATURE) { 1326fcf59617SAndrey V. Elsukov /* 1327fcf59617SAndrey V. Elsukov * Calculate MD5 signature and put it into the place 1328fcf59617SAndrey V. Elsukov * determined before. 1329fcf59617SAndrey V. Elsukov * NOTE: since TCP options buffer doesn't point into 1330fcf59617SAndrey V. Elsukov * mbuf's data, calculate offset and use it. 1331fcf59617SAndrey V. Elsukov */ 13322aad6240SAndrey V. Elsukov if (!TCPMD5_ENABLED() || (error = TCPMD5_OUTPUT(m, th, 13332aad6240SAndrey V. Elsukov (u_char *)(th + 1) + (to.to_signature - opt))) != 0) { 1334fcf59617SAndrey V. Elsukov /* 1335fcf59617SAndrey V. Elsukov * Do not send segment if the calculation of MD5 1336fcf59617SAndrey V. Elsukov * digest has failed. 1337fcf59617SAndrey V. Elsukov */ 13382aad6240SAndrey V. Elsukov m_freem(m); 1339fcf59617SAndrey V. Elsukov goto out; 1340fcf59617SAndrey V. Elsukov } 1341fcf59617SAndrey V. Elsukov } 1342fcf59617SAndrey V. Elsukov #endif 1343fb59c426SYoshinobu Inoue #ifdef INET6 134445747ba5SBjoern A. Zeeb if (isipv6) { 1345fb59c426SYoshinobu Inoue /* 13463df96ee6SCy Schubert * There is no need to fill in ip6_plen right now. 13473df96ee6SCy Schubert * It will be filled later by ip6_output. 1348fb59c426SYoshinobu Inoue */ 13499e644c23SMichael Tuexen if (tp->t_port) { 13509e644c23SMichael Tuexen m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 13519e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13529e644c23SMichael Tuexen udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 13539e644c23SMichael Tuexen th->th_sum = htons(0); 13549e644c23SMichael Tuexen UDPSTAT_INC(udps_opackets); 13559e644c23SMichael Tuexen } else { 1356356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 13579e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13589e644c23SMichael Tuexen th->th_sum = in6_cksum_pseudo(ip6, 13599e644c23SMichael Tuexen sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 13609e644c23SMichael Tuexen 0); 13619e644c23SMichael Tuexen } 136245747ba5SBjoern A. Zeeb } 136345747ba5SBjoern A. Zeeb #endif 136445747ba5SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1365fb59c426SYoshinobu Inoue else 136645747ba5SBjoern A. Zeeb #endif 136745747ba5SBjoern A. Zeeb #ifdef INET 1368fb59c426SYoshinobu Inoue { 13699e644c23SMichael Tuexen if (tp->t_port) { 13709e644c23SMichael Tuexen m->m_pkthdr.csum_flags = CSUM_UDP; 13719e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13729e644c23SMichael Tuexen udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 13739e644c23SMichael Tuexen ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 13749e644c23SMichael Tuexen th->th_sum = htons(0); 13759e644c23SMichael Tuexen UDPSTAT_INC(udps_opackets); 13769e644c23SMichael Tuexen } else { 1377356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP; 13789e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13799e644c23SMichael Tuexen th->th_sum = in_pseudo(ip->ip_src.s_addr, 13809e644c23SMichael Tuexen ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 13819e644c23SMichael Tuexen IPPROTO_TCP + len + optlen)); 13829e644c23SMichael Tuexen } 1383fb59c426SYoshinobu Inoue 1384db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 1385db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 13866e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 1387fb59c426SYoshinobu Inoue } 138845747ba5SBjoern A. Zeeb #endif 1389df8bae1dSRodney W. Grimes 1390df8bae1dSRodney W. Grimes /* 1391b3c0f300SAndre Oppermann * Enable TSO and specify the size of the segments. 1392b3c0f300SAndre Oppermann * The TCP pseudo header checksum is always provided. 1393b3c0f300SAndre Oppermann */ 1394b3c0f300SAndre Oppermann if (tso) { 13950c39d38dSGleb Smirnoff KASSERT(len > tp->t_maxseg - optlen, 1396153e5b57SAndre Oppermann ("%s: len <= tso_segsz", __func__)); 13973579cf4cSKenneth D. Merry m->m_pkthdr.csum_flags |= CSUM_TSO; 13980c39d38dSGleb Smirnoff m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen; 1399b3c0f300SAndre Oppermann } 1400b3c0f300SAndre Oppermann 140105fb056cSMichael Tuexen KASSERT(len + hdrlen == m_length(m, NULL), 140205fb056cSMichael Tuexen ("%s: mbuf chain shorter than expected: %d + %u != %u", 140305fb056cSMichael Tuexen __func__, len, hdrlen, m_length(m, NULL))); 1404ed420311SAndre Oppermann 1405bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 140639bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */ 140739bc9de5SLawrence Stewart hhook_run_tcp_est_out(tp, th, &to, len, tso); 1408bd79708dSJonathan T. Looney #endif 140939bc9de5SLawrence Stewart 14102b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__output, tp, th, m); 1411df8bae1dSRodney W. Grimes 1412dcaffbd6SJonathan T. Looney /* We're getting ready to send; log now. */ 1413bd30a121SMichael Tuexen /* XXXMT: We are not honoring verbose logging. */ 1414bd30a121SMichael Tuexen if (tp->t_logstate != TCP_LOG_STATE_OFF) 1415bd30a121SMichael Tuexen lgb = tcp_log_event_(tp, th, &so->so_rcv, &so->so_snd, 1416bd30a121SMichael Tuexen TCP_LOG_OUT, ERRNO_UNK, len, NULL, false, NULL, NULL, 0, 1417bd30a121SMichael Tuexen NULL); 1418bd30a121SMichael Tuexen else 1419bd30a121SMichael Tuexen lgb = NULL; 1420dcaffbd6SJonathan T. Looney 1421df8bae1dSRodney W. Grimes /* 1422df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 1423df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 1424df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 1425df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 1426df8bae1dSRodney W. Grimes */ 1427fb59c426SYoshinobu Inoue /* 142843630e62SHiren Panchasara * m->m_pkthdr.len should have been set before checksum calculation, 1429fb59c426SYoshinobu Inoue * because in6_cksum() need it. 1430fb59c426SYoshinobu Inoue */ 1431fb59c426SYoshinobu Inoue #ifdef INET6 1432fb59c426SYoshinobu Inoue if (isipv6) { 1433fb59c426SYoshinobu Inoue /* 1434fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 1435fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 1436fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 1437fb59c426SYoshinobu Inoue * Neighbor Discovery. 1438fb59c426SYoshinobu Inoue */ 14399eb0e832SGleb Smirnoff ip6->ip6_hlim = in6_selecthlim(inp, NULL); 1440fb59c426SYoshinobu Inoue 144157f60867SMark Johnston /* 144257f60867SMark Johnston * Set the packet size here for the benefit of DTrace probes. 144357f60867SMark Johnston * ip6_output() will set it properly; it's supposed to include 144457f60867SMark Johnston * the option header lengths as well. 144557f60867SMark Johnston */ 144657f60867SMark Johnston ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 144757f60867SMark Johnston 14480c39d38dSGleb Smirnoff if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 14490f3e3bc5SSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 14500f3e3bc5SSean Bruno else 14510f3e3bc5SSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 14520f3e3bc5SSean Bruno 145357f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1454d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 145557f60867SMark Johnston 145657f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip6, tp, th); 145757f60867SMark Johnston 145886a996e6SHiren Panchasara #ifdef TCPPCAP 145986a996e6SHiren Panchasara /* Save packet, if requested. */ 146086a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_outpkts)); 146186a996e6SHiren Panchasara #endif 146286a996e6SHiren Panchasara 1463fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 14649eb0e832SGleb Smirnoff error = ip6_output(m, inp->in6p_outputopts, &inp->inp_route6, 1465df0633a1SGleb Smirnoff ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 14669eb0e832SGleb Smirnoff NULL, NULL, inp); 1467df0633a1SGleb Smirnoff 14689eb0e832SGleb Smirnoff if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL) 14699eb0e832SGleb Smirnoff mtu = inp->inp_route6.ro_nh->nh_mtu; 1470b287c6c7SBjoern A. Zeeb } 1471fb59c426SYoshinobu Inoue #endif /* INET6 */ 1472b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1473b287c6c7SBjoern A. Zeeb else 1474b287c6c7SBjoern A. Zeeb #endif 1475b287c6c7SBjoern A. Zeeb #ifdef INET 1476df8bae1dSRodney W. Grimes { 14778f134647SGleb Smirnoff ip->ip_len = htons(m->m_pkthdr.len); 1478686cdd19SJun-ichiro itojun Hagino #ifdef INET6 14799eb0e832SGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 14809eb0e832SGleb Smirnoff ip->ip_ttl = in6_selecthlim(inp, NULL); 1481686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 1482f138387aSGarrett Wollman /* 148397d8d152SAndre Oppermann * If we do path MTU discovery, then we set DF on every packet. 148497d8d152SAndre Oppermann * This might not be the best thing to do according to RFC3390 148597d8d152SAndre Oppermann * Section 2. However the tcp hostcache migitates the problem 148697d8d152SAndre Oppermann * so it affects only the first tcp connection with a host. 1487e4e92660SAndre Oppermann * 1488e4e92660SAndre Oppermann * NB: Don't set DF on small MTU/MSS to have a safe fallback. 1489f138387aSGarrett Wollman */ 14900c39d38dSGleb Smirnoff if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 1491f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 14929e644c23SMichael Tuexen if (tp->t_port == 0 || len < V_tcp_minmss) { 14939e644c23SMichael Tuexen ip->ip_off |= htons(IP_DF); 14949e644c23SMichael Tuexen } 1495f6f6703fSSean Bruno } else { 1496f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 1497f6f6703fSSean Bruno } 149897d8d152SAndre Oppermann 149957f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1500d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 150157f60867SMark Johnston 150257f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip, tp, th); 150357f60867SMark Johnston 150486a996e6SHiren Panchasara #ifdef TCPPCAP 150586a996e6SHiren Panchasara /* Save packet, if requested. */ 150686a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_outpkts)); 150786a996e6SHiren Panchasara #endif 150886a996e6SHiren Panchasara 15099eb0e832SGleb Smirnoff error = ip_output(m, inp->inp_options, &inp->inp_route, 15109eb0e832SGleb Smirnoff ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, inp); 1511df0633a1SGleb Smirnoff 15129eb0e832SGleb Smirnoff if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL) 15139eb0e832SGleb Smirnoff mtu = inp->inp_route.ro_nh->nh_mtu; 1514df8bae1dSRodney W. Grimes } 1515b287c6c7SBjoern A. Zeeb #endif /* INET */ 15160e2bc05cSGleb Smirnoff 1517bd30a121SMichael Tuexen if (lgb != NULL) { 1518bd30a121SMichael Tuexen lgb->tlb_errno = error; 1519bd30a121SMichael Tuexen lgb = NULL; 1520bd30a121SMichael Tuexen } 15210e2bc05cSGleb Smirnoff out: 152267e89281SRandall Stewart if (error == 0) 15239e4d9e4cSRandall Stewart tcp_account_for_send(tp, len, (tp->snd_nxt != tp->snd_max), 0, hw_tls); 15240e2bc05cSGleb Smirnoff /* 15250e2bc05cSGleb Smirnoff * In transmit state, time the transmission and arrange for 15260e2bc05cSGleb Smirnoff * the retransmit. In persist state, just set snd_max. 15270e2bc05cSGleb Smirnoff */ 15280e2bc05cSGleb Smirnoff if ((tp->t_flags & TF_FORCEDATA) == 0 || 15290e2bc05cSGleb Smirnoff !tcp_timer_active(tp, TT_PERSIST)) { 15300e2bc05cSGleb Smirnoff tcp_seq startseq = tp->snd_nxt; 15310e2bc05cSGleb Smirnoff 15320e2bc05cSGleb Smirnoff /* 15330e2bc05cSGleb Smirnoff * Advance snd_nxt over sequence space of this segment. 15340e2bc05cSGleb Smirnoff */ 15350e2bc05cSGleb Smirnoff if (flags & (TH_SYN|TH_FIN)) { 15360e2bc05cSGleb Smirnoff if (flags & TH_SYN) 15370e2bc05cSGleb Smirnoff tp->snd_nxt++; 15380e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 15390e2bc05cSGleb Smirnoff tp->snd_nxt++; 15400e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 15410e2bc05cSGleb Smirnoff } 15420e2bc05cSGleb Smirnoff } 15430e2bc05cSGleb Smirnoff if (sack_rxmit) 15440e2bc05cSGleb Smirnoff goto timer; 15450e2bc05cSGleb Smirnoff tp->snd_nxt += len; 15460e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 154708af8aacSRandall Stewart /* 154808af8aacSRandall Stewart * Update "made progress" indication if we just 154908af8aacSRandall Stewart * added new data to an empty socket buffer. 155008af8aacSRandall Stewart */ 155108af8aacSRandall Stewart if (tp->snd_una == tp->snd_max) 155208af8aacSRandall Stewart tp->t_acktime = ticks; 15530e2bc05cSGleb Smirnoff tp->snd_max = tp->snd_nxt; 15540e2bc05cSGleb Smirnoff /* 15550e2bc05cSGleb Smirnoff * Time this transmission if not a retransmission and 15560e2bc05cSGleb Smirnoff * not currently timing anything. 15570e2bc05cSGleb Smirnoff */ 15589dc7d8a2SRichard Scheffenegger tp->t_sndtime = ticks; 15590e2bc05cSGleb Smirnoff if (tp->t_rtttime == 0) { 15600e2bc05cSGleb Smirnoff tp->t_rtttime = ticks; 15610e2bc05cSGleb Smirnoff tp->t_rtseq = startseq; 15620e2bc05cSGleb Smirnoff TCPSTAT_INC(tcps_segstimed); 15630e2bc05cSGleb Smirnoff } 1564adc56f5aSEdward Tomasz Napierala #ifdef STATS 1565adc56f5aSEdward Tomasz Napierala if (!(tp->t_flags & TF_GPUTINPROG) && len) { 1566adc56f5aSEdward Tomasz Napierala tp->t_flags |= TF_GPUTINPROG; 1567adc56f5aSEdward Tomasz Napierala tp->gput_seq = startseq; 1568adc56f5aSEdward Tomasz Napierala tp->gput_ack = startseq + 1569adc56f5aSEdward Tomasz Napierala ulmin(sbavail(&so->so_snd) - off, sendwin); 1570adc56f5aSEdward Tomasz Napierala tp->gput_ts = tcp_ts_getticks(); 1571adc56f5aSEdward Tomasz Napierala } 1572adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 15730e2bc05cSGleb Smirnoff } 15740e2bc05cSGleb Smirnoff 15750e2bc05cSGleb Smirnoff /* 15760e2bc05cSGleb Smirnoff * Set retransmit timer if not currently set, 15770e2bc05cSGleb Smirnoff * and not doing a pure ack or a keep-alive probe. 15780e2bc05cSGleb Smirnoff * Initial value for retransmit timer is smoothed 15790e2bc05cSGleb Smirnoff * round-trip time + 2 * round-trip time variance. 15800e2bc05cSGleb Smirnoff * Initialize shift counter which is used for backoff 15810e2bc05cSGleb Smirnoff * of retransmit time. 15820e2bc05cSGleb Smirnoff */ 15830e2bc05cSGleb Smirnoff timer: 15840e2bc05cSGleb Smirnoff if (!tcp_timer_active(tp, TT_REXMT) && 15850e2bc05cSGleb Smirnoff ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 15860e2bc05cSGleb Smirnoff (tp->snd_nxt != tp->snd_una))) { 15870e2bc05cSGleb Smirnoff if (tcp_timer_active(tp, TT_PERSIST)) { 15880e2bc05cSGleb Smirnoff tcp_timer_activate(tp, TT_PERSIST, 0); 15890e2bc05cSGleb Smirnoff tp->t_rxtshift = 0; 15900e2bc05cSGleb Smirnoff } 159108af8aacSRandall Stewart tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp)); 1592f8568079SHiren Panchasara } else if (len == 0 && sbavail(&so->so_snd) && 1593f8568079SHiren Panchasara !tcp_timer_active(tp, TT_REXMT) && 1594f8568079SHiren Panchasara !tcp_timer_active(tp, TT_PERSIST)) { 1595f8568079SHiren Panchasara /* 1596f8568079SHiren Panchasara * Avoid a situation where we do not set persist timer 1597f8568079SHiren Panchasara * after a zero window condition. For example: 1598f8568079SHiren Panchasara * 1) A -> B: packet with enough data to fill the window 1599f8568079SHiren Panchasara * 2) B -> A: ACK for #1 + new data (0 window 1600f8568079SHiren Panchasara * advertisement) 1601f8568079SHiren Panchasara * 3) A -> B: ACK for #2, 0 len packet 1602f8568079SHiren Panchasara * 1603f8568079SHiren Panchasara * In this case, A will not activate the persist timer, 1604f8568079SHiren Panchasara * because it chose to send a packet. Unless tcp_output 1605f8568079SHiren Panchasara * is called for some other reason (delayed ack timer, 1606f8568079SHiren Panchasara * another input packet from B, socket syscall), A will 1607f8568079SHiren Panchasara * not send zero window probes. 1608f8568079SHiren Panchasara * 1609f8568079SHiren Panchasara * So, if you send a 0-length packet, but there is data 1610f8568079SHiren Panchasara * in the socket buffer, and neither the rexmt or 1611f8568079SHiren Panchasara * persist timer is already set, then activate the 1612f8568079SHiren Panchasara * persist timer. 1613f8568079SHiren Panchasara */ 1614f8568079SHiren Panchasara tp->t_rxtshift = 0; 1615f8568079SHiren Panchasara tcp_setpersist(tp); 16160e2bc05cSGleb Smirnoff } 16170e2bc05cSGleb Smirnoff } else { 16180e2bc05cSGleb Smirnoff /* 16190e2bc05cSGleb Smirnoff * Persist case, update snd_max but since we are in 16200e2bc05cSGleb Smirnoff * persist mode (no window) we do not update snd_nxt. 16210e2bc05cSGleb Smirnoff */ 16220e2bc05cSGleb Smirnoff int xlen = len; 16230e2bc05cSGleb Smirnoff if (flags & TH_SYN) 16240e2bc05cSGleb Smirnoff ++xlen; 16250e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 16260e2bc05cSGleb Smirnoff ++xlen; 16270e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 16280e2bc05cSGleb Smirnoff } 16290e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 163015c82571SJonathan T. Looney tp->snd_max = tp->snd_nxt + xlen; 16310e2bc05cSGleb Smirnoff } 1632e5926fd3SRandall Stewart if ((error == 0) && 1633e5926fd3SRandall Stewart (TCPS_HAVEESTABLISHED(tp->t_state) && 1634e5926fd3SRandall Stewart (tp->t_flags & TF_SACK_PERMIT) && 1635e5926fd3SRandall Stewart tp->rcv_numsacks > 0)) { 1636e5926fd3SRandall Stewart /* Clean up any DSACK's sent */ 1637e5926fd3SRandall Stewart tcp_clean_dsack_blocks(tp); 1638e5926fd3SRandall Stewart } 1639df8bae1dSRodney W. Grimes if (error) { 16407734ea06SArchie Cobbs /* 16417734ea06SArchie Cobbs * We know that the packet was lost, so back out the 16427734ea06SArchie Cobbs * sequence number advance, if any. 16432c30ec0aSAndre Oppermann * 16442c30ec0aSAndre Oppermann * If the error is EPERM the packet got blocked by the 16452c30ec0aSAndre Oppermann * local firewall. Normally we should terminate the 16462c30ec0aSAndre Oppermann * connection but the blocking may have been spurious 16472c30ec0aSAndre Oppermann * due to a firewall reconfiguration cycle. So we treat 16482c30ec0aSAndre Oppermann * it like a packet loss and let the retransmit timer and 16492c30ec0aSAndre Oppermann * timeouts do their work over time. 16502c30ec0aSAndre Oppermann * XXX: It is a POLA question whether calling tcp_drop right 16512c30ec0aSAndre Oppermann * away would be the really correct behavior instead. 16527734ea06SArchie Cobbs */ 165372757d9aSGleb Smirnoff if (((tp->t_flags & TF_FORCEDATA) == 0 || 1654b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) && 165572757d9aSGleb Smirnoff ((flags & TH_SYN) == 0) && 165672757d9aSGleb Smirnoff (error != EPERM)) { 16570077b016SPaul Saab if (sack_rxmit) { 16585d3b1b75SJayanth Vijayaraghavan p->rxmit -= len; 16590077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= len; 166072757d9aSGleb Smirnoff KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 16610077b016SPaul Saab ("sackhint bytes rtx >= 0")); 166266605ff7SRichard Scheffenegger KASSERT((flags & TH_FIN) == 0, 166366605ff7SRichard Scheffenegger ("error while FIN with SACK rxmit")); 166466605ff7SRichard Scheffenegger } else { 16657734ea06SArchie Cobbs tp->snd_nxt -= len; 166666605ff7SRichard Scheffenegger if (flags & TH_FIN) 166766605ff7SRichard Scheffenegger tp->snd_nxt--; 166866605ff7SRichard Scheffenegger } 16697734ea06SArchie Cobbs } 1670cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */ 167172757d9aSGleb Smirnoff switch (error) { 1672fcf59617SAndrey V. Elsukov case EACCES: 167372757d9aSGleb Smirnoff case EPERM: 167472757d9aSGleb Smirnoff tp->t_softerror = error; 167572757d9aSGleb Smirnoff return (error); 167672757d9aSGleb Smirnoff case ENOBUFS: 1677425b7639SSepherosa Ziehau TCP_XMIT_TIMER_ASSERT(tp, len, flags); 16781600372bSAndre Oppermann tp->snd_cwnd = tp->t_maxseg; 1679df8bae1dSRodney W. Grimes return (0); 168072757d9aSGleb Smirnoff case EMSGSIZE: 16813d1f141bSGarrett Wollman /* 1682b3c0f300SAndre Oppermann * For some reason the interface we used initially 1683b3c0f300SAndre Oppermann * to send segments changed to another or lowered 1684b3c0f300SAndre Oppermann * its MTU. 1685b3c0f300SAndre Oppermann * If TSO was active we either got an interface 1686b3c0f300SAndre Oppermann * without TSO capabilits or TSO was turned off. 1687df0633a1SGleb Smirnoff * If we obtained mtu from ip_output() then update 1688df0633a1SGleb Smirnoff * it and try again. 16893d1f141bSGarrett Wollman */ 1690b3c0f300SAndre Oppermann if (tso) 1691b3c0f300SAndre Oppermann tp->t_flags &= ~TF_TSO; 1692df0633a1SGleb Smirnoff if (mtu != 0) { 1693df0633a1SGleb Smirnoff tcp_mss_update(tp, -1, mtu, NULL, NULL); 1694df0633a1SGleb Smirnoff goto again; 1695df0633a1SGleb Smirnoff } 1696df0633a1SGleb Smirnoff return (error); 16978bec3467SGleb Smirnoff case EHOSTDOWN: 169872757d9aSGleb Smirnoff case EHOSTUNREACH: 169972757d9aSGleb Smirnoff case ENETDOWN: 17008bec3467SGleb Smirnoff case ENETUNREACH: 170172757d9aSGleb Smirnoff if (TCPS_HAVERCVDSYN(tp->t_state)) { 1702df8bae1dSRodney W. Grimes tp->t_softerror = error; 1703df8bae1dSRodney W. Grimes return (0); 1704df8bae1dSRodney W. Grimes } 170572757d9aSGleb Smirnoff /* FALLTHROUGH */ 170672757d9aSGleb Smirnoff default: 1707df8bae1dSRodney W. Grimes return (error); 1708df8bae1dSRodney W. Grimes } 170972757d9aSGleb Smirnoff } 171078b50714SRobert Watson TCPSTAT_INC(tcps_sndtotal); 1711df8bae1dSRodney W. Grimes 1712df8bae1dSRodney W. Grimes /* 1713df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 1714df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 1715df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 1716df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 1717df8bae1dSRodney W. Grimes */ 17183ac12506SJonathan T. Looney if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1719201d185bSAndre Oppermann tp->rcv_adv = tp->rcv_nxt + recwin; 1720df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 17213bfd6421SJonathan Lemon tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 1722b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_DELACK)) 1723b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 0); 1724d912c694SMatthew Dillon #if 0 1725d912c694SMatthew Dillon /* 1726d912c694SMatthew Dillon * This completely breaks TCP if newreno is turned on. What happens 1727d912c694SMatthew Dillon * is that if delayed-acks are turned on on the receiver, this code 1728d912c694SMatthew Dillon * on the transmitter effectively destroys the TCP window, forcing 1729d912c694SMatthew Dillon * it to four packets (1.5Kx4 = 6K window). 1730d912c694SMatthew Dillon */ 1731dbc42409SLawrence Stewart if (sendalot && --maxburst) 1732df8bae1dSRodney W. Grimes goto again; 1733d912c694SMatthew Dillon #endif 1734d912c694SMatthew Dillon if (sendalot) 1735d912c694SMatthew Dillon goto again; 1736df8bae1dSRodney W. Grimes return (0); 1737df8bae1dSRodney W. Grimes } 1738df8bae1dSRodney W. Grimes 1739df8bae1dSRodney W. Grimes void 1740ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp) 1741df8bae1dSRodney W. Grimes { 17429b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 17439b8b58e0SJonathan Lemon int tt; 174408af8aacSRandall Stewart int maxunacktime; 1745df8bae1dSRodney W. Grimes 1746672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 1747b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_REXMT)) 17489b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 1749df8bae1dSRodney W. Grimes /* 175008af8aacSRandall Stewart * If the state is already closed, don't bother. 175108af8aacSRandall Stewart */ 175208af8aacSRandall Stewart if (tp->t_state == TCPS_CLOSED) 175308af8aacSRandall Stewart return; 175408af8aacSRandall Stewart 175508af8aacSRandall Stewart /* 1756a4641f4eSPedro F. Giffuni * Start/restart persistence timer. 1757df8bae1dSRodney W. Grimes */ 17589b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 17590645c604SHiren Panchasara tcp_persmin, tcp_persmax); 176008af8aacSRandall Stewart if (TP_MAXUNACKTIME(tp) && tp->t_acktime) { 176108af8aacSRandall Stewart maxunacktime = tp->t_acktime + TP_MAXUNACKTIME(tp) - ticks; 176208af8aacSRandall Stewart if (maxunacktime < 1) 176308af8aacSRandall Stewart maxunacktime = 1; 176408af8aacSRandall Stewart if (maxunacktime < tt) 176508af8aacSRandall Stewart tt = maxunacktime; 176608af8aacSRandall Stewart } 1767b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, tt); 1768df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1769df8bae1dSRodney W. Grimes tp->t_rxtshift++; 1770df8bae1dSRodney W. Grimes } 177102a1a643SAndre Oppermann 177202a1a643SAndre Oppermann /* 177302a1a643SAndre Oppermann * Insert TCP options according to the supplied parameters to the place 177402a1a643SAndre Oppermann * optp in a consistent way. Can handle unaligned destinations. 177502a1a643SAndre Oppermann * 177602a1a643SAndre Oppermann * The order of the option processing is crucial for optimal packing and 177702a1a643SAndre Oppermann * alignment for the scarce option space. 177802a1a643SAndre Oppermann * 177902a1a643SAndre Oppermann * The optimal order for a SYN/SYN-ACK segment is: 178002a1a643SAndre Oppermann * MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) + 178102a1a643SAndre Oppermann * Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40. 178202a1a643SAndre Oppermann * 178302a1a643SAndre Oppermann * The SACK options should be last. SACK blocks consume 8*n+2 bytes. 178402a1a643SAndre Oppermann * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks). 178502a1a643SAndre Oppermann * At minimum we need 10 bytes (to generate 1 SACK block). If both 178602a1a643SAndre Oppermann * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present, 178702a1a643SAndre Oppermann * we only have 10 bytes for SACK options (40 - (12 + 18)). 178802a1a643SAndre Oppermann */ 178902a1a643SAndre Oppermann int 179002a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp) 179102a1a643SAndre Oppermann { 17925d20f974SJonathan T. Looney u_int32_t mask, optlen = 0; 179302a1a643SAndre Oppermann 179402a1a643SAndre Oppermann for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) { 179502a1a643SAndre Oppermann if ((to->to_flags & mask) != mask) 179602a1a643SAndre Oppermann continue; 17973a4018c4SAndre Oppermann if (optlen == TCP_MAXOLEN) 17983a4018c4SAndre Oppermann break; 179902a1a643SAndre Oppermann switch (to->to_flags & mask) { 180002a1a643SAndre Oppermann case TOF_MSS: 180102a1a643SAndre Oppermann while (optlen % 4) { 180202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 180302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 180402a1a643SAndre Oppermann } 18053a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG) 18063a4018c4SAndre Oppermann continue; 180702a1a643SAndre Oppermann optlen += TCPOLEN_MAXSEG; 180802a1a643SAndre Oppermann *optp++ = TCPOPT_MAXSEG; 180902a1a643SAndre Oppermann *optp++ = TCPOLEN_MAXSEG; 181002a1a643SAndre Oppermann to->to_mss = htons(to->to_mss); 181102a1a643SAndre Oppermann bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss)); 181202a1a643SAndre Oppermann optp += sizeof(to->to_mss); 181302a1a643SAndre Oppermann break; 181402a1a643SAndre Oppermann case TOF_SCALE: 181502a1a643SAndre Oppermann while (!optlen || optlen % 2 != 1) { 181602a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 181702a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 181802a1a643SAndre Oppermann } 18193a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW) 18203a4018c4SAndre Oppermann continue; 182102a1a643SAndre Oppermann optlen += TCPOLEN_WINDOW; 182202a1a643SAndre Oppermann *optp++ = TCPOPT_WINDOW; 182302a1a643SAndre Oppermann *optp++ = TCPOLEN_WINDOW; 182402a1a643SAndre Oppermann *optp++ = to->to_wscale; 182502a1a643SAndre Oppermann break; 182602a1a643SAndre Oppermann case TOF_SACKPERM: 182702a1a643SAndre Oppermann while (optlen % 2) { 182802a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 182902a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 183002a1a643SAndre Oppermann } 18313a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED) 18323a4018c4SAndre Oppermann continue; 183302a1a643SAndre Oppermann optlen += TCPOLEN_SACK_PERMITTED; 183402a1a643SAndre Oppermann *optp++ = TCPOPT_SACK_PERMITTED; 183502a1a643SAndre Oppermann *optp++ = TCPOLEN_SACK_PERMITTED; 183602a1a643SAndre Oppermann break; 183702a1a643SAndre Oppermann case TOF_TS: 183802a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 183902a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 184002a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 184102a1a643SAndre Oppermann } 18423a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP) 18433a4018c4SAndre Oppermann continue; 184402a1a643SAndre Oppermann optlen += TCPOLEN_TIMESTAMP; 184502a1a643SAndre Oppermann *optp++ = TCPOPT_TIMESTAMP; 184602a1a643SAndre Oppermann *optp++ = TCPOLEN_TIMESTAMP; 184702a1a643SAndre Oppermann to->to_tsval = htonl(to->to_tsval); 184802a1a643SAndre Oppermann to->to_tsecr = htonl(to->to_tsecr); 184902a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval)); 185002a1a643SAndre Oppermann optp += sizeof(to->to_tsval); 185102a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr)); 185202a1a643SAndre Oppermann optp += sizeof(to->to_tsecr); 185302a1a643SAndre Oppermann break; 185402a1a643SAndre Oppermann case TOF_SIGNATURE: 185502a1a643SAndre Oppermann { 185602a1a643SAndre Oppermann int siglen = TCPOLEN_SIGNATURE - 2; 185702a1a643SAndre Oppermann 185802a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 185902a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 186002a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 186102a1a643SAndre Oppermann } 1862fcf59617SAndrey V. Elsukov if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE) { 1863fcf59617SAndrey V. Elsukov to->to_flags &= ~TOF_SIGNATURE; 186402a1a643SAndre Oppermann continue; 1865fcf59617SAndrey V. Elsukov } 186602a1a643SAndre Oppermann optlen += TCPOLEN_SIGNATURE; 186702a1a643SAndre Oppermann *optp++ = TCPOPT_SIGNATURE; 186802a1a643SAndre Oppermann *optp++ = TCPOLEN_SIGNATURE; 186902a1a643SAndre Oppermann to->to_signature = optp; 187002a1a643SAndre Oppermann while (siglen--) 187102a1a643SAndre Oppermann *optp++ = 0; 187202a1a643SAndre Oppermann break; 187302a1a643SAndre Oppermann } 187402a1a643SAndre Oppermann case TOF_SACK: 187502a1a643SAndre Oppermann { 187602a1a643SAndre Oppermann int sackblks = 0; 187702a1a643SAndre Oppermann struct sackblk *sack = (struct sackblk *)to->to_sacks; 187802a1a643SAndre Oppermann tcp_seq sack_seq; 187902a1a643SAndre Oppermann 188002a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 188102a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 188202a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 188302a1a643SAndre Oppermann } 18843a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK) 188502a1a643SAndre Oppermann continue; 188602a1a643SAndre Oppermann optlen += TCPOLEN_SACKHDR; 188702a1a643SAndre Oppermann *optp++ = TCPOPT_SACK; 188802a1a643SAndre Oppermann sackblks = min(to->to_nsacks, 18890d957bbaSAndre Oppermann (TCP_MAXOLEN - optlen) / TCPOLEN_SACK); 189002a1a643SAndre Oppermann *optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK; 189102a1a643SAndre Oppermann while (sackblks--) { 189202a1a643SAndre Oppermann sack_seq = htonl(sack->start); 189302a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 189402a1a643SAndre Oppermann optp += sizeof(sack_seq); 189502a1a643SAndre Oppermann sack_seq = htonl(sack->end); 189602a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 189702a1a643SAndre Oppermann optp += sizeof(sack_seq); 189802a1a643SAndre Oppermann optlen += TCPOLEN_SACK; 189902a1a643SAndre Oppermann sack++; 190002a1a643SAndre Oppermann } 190178b50714SRobert Watson TCPSTAT_INC(tcps_sack_send_blocks); 190202a1a643SAndre Oppermann break; 190302a1a643SAndre Oppermann } 1904281a0fd4SPatrick Kelsey case TOF_FASTOPEN: 1905281a0fd4SPatrick Kelsey { 1906281a0fd4SPatrick Kelsey int total_len; 1907281a0fd4SPatrick Kelsey 1908281a0fd4SPatrick Kelsey /* XXX is there any point to aligning this option? */ 1909281a0fd4SPatrick Kelsey total_len = TCPOLEN_FAST_OPEN_EMPTY + to->to_tfo_len; 1910c560df6fSPatrick Kelsey if (TCP_MAXOLEN - optlen < total_len) { 1911c560df6fSPatrick Kelsey to->to_flags &= ~TOF_FASTOPEN; 1912281a0fd4SPatrick Kelsey continue; 1913c560df6fSPatrick Kelsey } 1914281a0fd4SPatrick Kelsey *optp++ = TCPOPT_FAST_OPEN; 1915281a0fd4SPatrick Kelsey *optp++ = total_len; 1916281a0fd4SPatrick Kelsey if (to->to_tfo_len > 0) { 1917281a0fd4SPatrick Kelsey bcopy(to->to_tfo_cookie, optp, to->to_tfo_len); 1918281a0fd4SPatrick Kelsey optp += to->to_tfo_len; 1919281a0fd4SPatrick Kelsey } 1920281a0fd4SPatrick Kelsey optlen += total_len; 1921281a0fd4SPatrick Kelsey break; 1922281a0fd4SPatrick Kelsey } 192302a1a643SAndre Oppermann default: 192402a1a643SAndre Oppermann panic("%s: unknown TCP option type", __func__); 192502a1a643SAndre Oppermann break; 192602a1a643SAndre Oppermann } 192702a1a643SAndre Oppermann } 192802a1a643SAndre Oppermann 192902a1a643SAndre Oppermann /* Terminate and pad TCP options to a 4 byte boundary. */ 193002a1a643SAndre Oppermann if (optlen % 4) { 193102a1a643SAndre Oppermann optlen += TCPOLEN_EOL; 193202a1a643SAndre Oppermann *optp++ = TCPOPT_EOL; 193302a1a643SAndre Oppermann } 1934413deb12SBjoern A. Zeeb /* 1935413deb12SBjoern A. Zeeb * According to RFC 793 (STD0007): 1936413deb12SBjoern A. Zeeb * "The content of the header beyond the End-of-Option option 1937413deb12SBjoern A. Zeeb * must be header padding (i.e., zero)." 1938413deb12SBjoern A. Zeeb * and later: "The padding is composed of zeros." 1939413deb12SBjoern A. Zeeb */ 194002a1a643SAndre Oppermann while (optlen % 4) { 1941c343c524SAndre Oppermann optlen += TCPOLEN_PAD; 1942c343c524SAndre Oppermann *optp++ = TCPOPT_PAD; 194302a1a643SAndre Oppermann } 194402a1a643SAndre Oppermann 19450d957bbaSAndre Oppermann KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__)); 194602a1a643SAndre Oppermann return (optlen); 194702a1a643SAndre Oppermann } 194866492feaSGleb Smirnoff 194989e560f4SRandall Stewart /* 195089e560f4SRandall Stewart * This is a copy of m_copym(), taking the TSO segment size/limit 195189e560f4SRandall Stewart * constraints into account, and advancing the sndptr as it goes. 195289e560f4SRandall Stewart */ 195389e560f4SRandall Stewart struct mbuf * 195489e560f4SRandall Stewart tcp_m_copym(struct mbuf *m, int32_t off0, int32_t *plen, 1955b2e60773SJohn Baldwin int32_t seglimit, int32_t segsize, struct sockbuf *sb, bool hw_tls) 195689e560f4SRandall Stewart { 1957b2e60773SJohn Baldwin #ifdef KERN_TLS 1958b2e60773SJohn Baldwin struct ktls_session *tls, *ntls; 1959732b6d4dSJohn Baldwin struct mbuf *start __diagused; 1960b2e60773SJohn Baldwin #endif 196189e560f4SRandall Stewart struct mbuf *n, **np; 196289e560f4SRandall Stewart struct mbuf *top; 196389e560f4SRandall Stewart int32_t off = off0; 196489e560f4SRandall Stewart int32_t len = *plen; 196589e560f4SRandall Stewart int32_t fragsize; 196689e560f4SRandall Stewart int32_t len_cp = 0; 196789e560f4SRandall Stewart int32_t *pkthdrlen; 196889e560f4SRandall Stewart uint32_t mlen, frags; 196989e560f4SRandall Stewart bool copyhdr; 197089e560f4SRandall Stewart 197189e560f4SRandall Stewart KASSERT(off >= 0, ("tcp_m_copym, negative off %d", off)); 197289e560f4SRandall Stewart KASSERT(len >= 0, ("tcp_m_copym, negative len %d", len)); 197389e560f4SRandall Stewart if (off == 0 && m->m_flags & M_PKTHDR) 197489e560f4SRandall Stewart copyhdr = true; 197589e560f4SRandall Stewart else 197689e560f4SRandall Stewart copyhdr = false; 197789e560f4SRandall Stewart while (off > 0) { 197889e560f4SRandall Stewart KASSERT(m != NULL, ("tcp_m_copym, offset > size of mbuf chain")); 197989e560f4SRandall Stewart if (off < m->m_len) 198089e560f4SRandall Stewart break; 198189e560f4SRandall Stewart off -= m->m_len; 198289e560f4SRandall Stewart if ((sb) && (m == sb->sb_sndptr)) { 198389e560f4SRandall Stewart sb->sb_sndptroff += m->m_len; 198489e560f4SRandall Stewart sb->sb_sndptr = m->m_next; 198589e560f4SRandall Stewart } 198689e560f4SRandall Stewart m = m->m_next; 198789e560f4SRandall Stewart } 198889e560f4SRandall Stewart np = ⊤ 198989e560f4SRandall Stewart top = NULL; 199089e560f4SRandall Stewart pkthdrlen = NULL; 1991b2e60773SJohn Baldwin #ifdef KERN_TLS 19926edfd179SGleb Smirnoff if (hw_tls && (m->m_flags & M_EXTPG)) 19937b6c99d0SGleb Smirnoff tls = m->m_epg_tls; 1994b2e60773SJohn Baldwin else 1995b2e60773SJohn Baldwin tls = NULL; 1996b2e60773SJohn Baldwin start = m; 1997b2e60773SJohn Baldwin #endif 199889e560f4SRandall Stewart while (len > 0) { 199989e560f4SRandall Stewart if (m == NULL) { 200089e560f4SRandall Stewart KASSERT(len == M_COPYALL, 200189e560f4SRandall Stewart ("tcp_m_copym, length > size of mbuf chain")); 200289e560f4SRandall Stewart *plen = len_cp; 200389e560f4SRandall Stewart if (pkthdrlen != NULL) 200489e560f4SRandall Stewart *pkthdrlen = len_cp; 200589e560f4SRandall Stewart break; 200689e560f4SRandall Stewart } 2007b2e60773SJohn Baldwin #ifdef KERN_TLS 2008b2e60773SJohn Baldwin if (hw_tls) { 20096edfd179SGleb Smirnoff if (m->m_flags & M_EXTPG) 20107b6c99d0SGleb Smirnoff ntls = m->m_epg_tls; 2011b2e60773SJohn Baldwin else 2012b2e60773SJohn Baldwin ntls = NULL; 2013b2e60773SJohn Baldwin 2014b2e60773SJohn Baldwin /* 2015b2e60773SJohn Baldwin * Avoid mixing TLS records with handshake 2016b2e60773SJohn Baldwin * data or TLS records from different 2017b2e60773SJohn Baldwin * sessions. 2018b2e60773SJohn Baldwin */ 2019b2e60773SJohn Baldwin if (tls != ntls) { 2020b2e60773SJohn Baldwin MPASS(m != start); 2021b2e60773SJohn Baldwin *plen = len_cp; 2022b2e60773SJohn Baldwin if (pkthdrlen != NULL) 2023b2e60773SJohn Baldwin *pkthdrlen = len_cp; 2024b2e60773SJohn Baldwin break; 2025b2e60773SJohn Baldwin } 2026b2e60773SJohn Baldwin } 2027b2e60773SJohn Baldwin #endif 202889e560f4SRandall Stewart mlen = min(len, m->m_len - off); 202989e560f4SRandall Stewart if (seglimit) { 203089e560f4SRandall Stewart /* 20316edfd179SGleb Smirnoff * For M_EXTPG mbufs, add 3 segments 203289e560f4SRandall Stewart * + 1 in case we are crossing page boundaries 203389e560f4SRandall Stewart * + 2 in case the TLS hdr/trailer are used 203489e560f4SRandall Stewart * It is cheaper to just add the segments 203589e560f4SRandall Stewart * than it is to take the cache miss to look 203689e560f4SRandall Stewart * at the mbuf ext_pgs state in detail. 203789e560f4SRandall Stewart */ 20386edfd179SGleb Smirnoff if (m->m_flags & M_EXTPG) { 203989e560f4SRandall Stewart fragsize = min(segsize, PAGE_SIZE); 204089e560f4SRandall Stewart frags = 3; 204189e560f4SRandall Stewart } else { 204289e560f4SRandall Stewart fragsize = segsize; 204389e560f4SRandall Stewart frags = 0; 204489e560f4SRandall Stewart } 204589e560f4SRandall Stewart 204689e560f4SRandall Stewart /* Break if we really can't fit anymore. */ 204789e560f4SRandall Stewart if ((frags + 1) >= seglimit) { 204889e560f4SRandall Stewart *plen = len_cp; 204989e560f4SRandall Stewart if (pkthdrlen != NULL) 205089e560f4SRandall Stewart *pkthdrlen = len_cp; 205189e560f4SRandall Stewart break; 205289e560f4SRandall Stewart } 205389e560f4SRandall Stewart 205489e560f4SRandall Stewart /* 205589e560f4SRandall Stewart * Reduce size if you can't copy the whole 205689e560f4SRandall Stewart * mbuf. If we can't copy the whole mbuf, also 205789e560f4SRandall Stewart * adjust len so the loop will end after this 205889e560f4SRandall Stewart * mbuf. 205989e560f4SRandall Stewart */ 206089e560f4SRandall Stewart if ((frags + howmany(mlen, fragsize)) >= seglimit) { 206189e560f4SRandall Stewart mlen = (seglimit - frags - 1) * fragsize; 206289e560f4SRandall Stewart len = mlen; 206389e560f4SRandall Stewart *plen = len_cp + len; 206489e560f4SRandall Stewart if (pkthdrlen != NULL) 206589e560f4SRandall Stewart *pkthdrlen = *plen; 206689e560f4SRandall Stewart } 206789e560f4SRandall Stewart frags += howmany(mlen, fragsize); 206889e560f4SRandall Stewart if (frags == 0) 206989e560f4SRandall Stewart frags++; 207089e560f4SRandall Stewart seglimit -= frags; 207189e560f4SRandall Stewart KASSERT(seglimit > 0, 207289e560f4SRandall Stewart ("%s: seglimit went too low", __func__)); 207389e560f4SRandall Stewart } 207489e560f4SRandall Stewart if (copyhdr) 207589e560f4SRandall Stewart n = m_gethdr(M_NOWAIT, m->m_type); 207689e560f4SRandall Stewart else 207789e560f4SRandall Stewart n = m_get(M_NOWAIT, m->m_type); 207889e560f4SRandall Stewart *np = n; 207989e560f4SRandall Stewart if (n == NULL) 208089e560f4SRandall Stewart goto nospace; 208189e560f4SRandall Stewart if (copyhdr) { 208289e560f4SRandall Stewart if (!m_dup_pkthdr(n, m, M_NOWAIT)) 208389e560f4SRandall Stewart goto nospace; 208489e560f4SRandall Stewart if (len == M_COPYALL) 208589e560f4SRandall Stewart n->m_pkthdr.len -= off0; 208689e560f4SRandall Stewart else 208789e560f4SRandall Stewart n->m_pkthdr.len = len; 208889e560f4SRandall Stewart pkthdrlen = &n->m_pkthdr.len; 208989e560f4SRandall Stewart copyhdr = false; 209089e560f4SRandall Stewart } 209189e560f4SRandall Stewart n->m_len = mlen; 209289e560f4SRandall Stewart len_cp += n->m_len; 209361664ee7SGleb Smirnoff if (m->m_flags & (M_EXT|M_EXTPG)) { 209489e560f4SRandall Stewart n->m_data = m->m_data + off; 209589e560f4SRandall Stewart mb_dupcl(n, m); 209689e560f4SRandall Stewart } else 209789e560f4SRandall Stewart bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 209889e560f4SRandall Stewart (u_int)n->m_len); 209989e560f4SRandall Stewart 210089e560f4SRandall Stewart if (sb && (sb->sb_sndptr == m) && 210189e560f4SRandall Stewart ((n->m_len + off) >= m->m_len) && m->m_next) { 210289e560f4SRandall Stewart sb->sb_sndptroff += m->m_len; 210389e560f4SRandall Stewart sb->sb_sndptr = m->m_next; 210489e560f4SRandall Stewart } 210589e560f4SRandall Stewart off = 0; 210689e560f4SRandall Stewart if (len != M_COPYALL) { 210789e560f4SRandall Stewart len -= n->m_len; 210889e560f4SRandall Stewart } 210989e560f4SRandall Stewart m = m->m_next; 211089e560f4SRandall Stewart np = &n->m_next; 211189e560f4SRandall Stewart } 211289e560f4SRandall Stewart return (top); 211389e560f4SRandall Stewart nospace: 211489e560f4SRandall Stewart m_freem(top); 211589e560f4SRandall Stewart return (NULL); 211689e560f4SRandall Stewart } 211789e560f4SRandall Stewart 211866492feaSGleb Smirnoff void 211966492feaSGleb Smirnoff tcp_sndbuf_autoscale(struct tcpcb *tp, struct socket *so, uint32_t sendwin) 212066492feaSGleb Smirnoff { 212166492feaSGleb Smirnoff 212266492feaSGleb Smirnoff /* 212366492feaSGleb Smirnoff * Automatic sizing of send socket buffer. Often the send buffer 212466492feaSGleb Smirnoff * size is not optimally adjusted to the actual network conditions 212566492feaSGleb Smirnoff * at hand (delay bandwidth product). Setting the buffer size too 212666492feaSGleb Smirnoff * small limits throughput on links with high bandwidth and high 212766492feaSGleb Smirnoff * delay (eg. trans-continental/oceanic links). Setting the 212866492feaSGleb Smirnoff * buffer size too big consumes too much real kernel memory, 212966492feaSGleb Smirnoff * especially with many connections on busy servers. 213066492feaSGleb Smirnoff * 213166492feaSGleb Smirnoff * The criteria to step up the send buffer one notch are: 213266492feaSGleb Smirnoff * 1. receive window of remote host is larger than send buffer 213366492feaSGleb Smirnoff * (with a fudge factor of 5/4th); 213466492feaSGleb Smirnoff * 2. send buffer is filled to 7/8th with data (so we actually 213566492feaSGleb Smirnoff * have data to make use of it); 213666492feaSGleb Smirnoff * 3. send buffer fill has not hit maximal automatic size; 213766492feaSGleb Smirnoff * 4. our send window (slow start and cogestion controlled) is 213866492feaSGleb Smirnoff * larger than sent but unacknowledged data in send buffer. 213966492feaSGleb Smirnoff * 214066492feaSGleb Smirnoff * The remote host receive window scaling factor may limit the 214166492feaSGleb Smirnoff * growing of the send buffer before it reaches its allowed 214266492feaSGleb Smirnoff * maximum. 214366492feaSGleb Smirnoff * 214466492feaSGleb Smirnoff * It scales directly with slow start or congestion window 214566492feaSGleb Smirnoff * and does at most one step per received ACK. This fast 214666492feaSGleb Smirnoff * scaling has the drawback of growing the send buffer beyond 214766492feaSGleb Smirnoff * what is strictly necessary to make full use of a given 214866492feaSGleb Smirnoff * delay*bandwidth product. However testing has shown this not 214966492feaSGleb Smirnoff * to be much of an problem. At worst we are trading wasting 215066492feaSGleb Smirnoff * of available bandwidth (the non-use of it) for wasting some 215166492feaSGleb Smirnoff * socket buffer memory. 215266492feaSGleb Smirnoff * 215366492feaSGleb Smirnoff * TODO: Shrink send buffer during idle periods together 215466492feaSGleb Smirnoff * with congestion window. Requires another timer. Has to 215566492feaSGleb Smirnoff * wait for upcoming tcp timer rewrite. 215666492feaSGleb Smirnoff * 215766492feaSGleb Smirnoff * XXXGL: should there be used sbused() or sbavail()? 215866492feaSGleb Smirnoff */ 215966492feaSGleb Smirnoff if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 216066492feaSGleb Smirnoff int lowat; 216166492feaSGleb Smirnoff 216266492feaSGleb Smirnoff lowat = V_tcp_sendbuf_auto_lowat ? so->so_snd.sb_lowat : 0; 216366492feaSGleb Smirnoff if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat - lowat && 216466492feaSGleb Smirnoff sbused(&so->so_snd) >= 216566492feaSGleb Smirnoff (so->so_snd.sb_hiwat / 8 * 7) - lowat && 216666492feaSGleb Smirnoff sbused(&so->so_snd) < V_tcp_autosndbuf_max && 216766492feaSGleb Smirnoff sendwin >= (sbused(&so->so_snd) - 216866492feaSGleb Smirnoff (tp->snd_nxt - tp->snd_una))) { 216943283184SGleb Smirnoff if (!sbreserve_locked(so, SO_SND, 217066492feaSGleb Smirnoff min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc, 217143283184SGleb Smirnoff V_tcp_autosndbuf_max), curthread)) 217266492feaSGleb Smirnoff so->so_snd.sb_flags &= ~SB_AUTOSIZE; 217366492feaSGleb Smirnoff } 217466492feaSGleb Smirnoff } 217566492feaSGleb Smirnoff } 2176