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 */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 331cfd4b53SBruce M Simpson #include "opt_inet.h" 34fb59c426SYoshinobu Inoue #include "opt_inet6.h" 353a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 36b2e60773SJohn Baldwin #include "opt_kern_tls.h" 370cc12cc5SJoerg Wunsch 38df8bae1dSRodney W. Grimes #include <sys/param.h> 39df8bae1dSRodney W. Grimes #include <sys/systm.h> 40adc56f5aSEdward Tomasz Napierala #include <sys/arb.h> 41686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h> 42bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 4339bc9de5SLawrence Stewart #include <sys/hhook.h> 44bd79708dSJonathan T. Looney #endif 45fb919e4dSMark Murray #include <sys/kernel.h> 46b2e60773SJohn Baldwin #ifdef KERN_TLS 47b2e60773SJohn Baldwin #include <sys/ktls.h> 48b2e60773SJohn Baldwin #endif 49fb919e4dSMark Murray #include <sys/lock.h> 50fb919e4dSMark Murray #include <sys/mbuf.h> 51fb919e4dSMark Murray #include <sys/mutex.h> 52df8bae1dSRodney W. Grimes #include <sys/protosw.h> 53adc56f5aSEdward Tomasz Napierala #include <sys/qmath.h> 5457f60867SMark Johnston #include <sys/sdt.h> 55df8bae1dSRodney W. Grimes #include <sys/socket.h> 56df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 57fb919e4dSMark Murray #include <sys/sysctl.h> 58adc56f5aSEdward Tomasz Napierala #include <sys/stats.h> 59df8bae1dSRodney W. Grimes 604b79449eSBjoern A. Zeeb #include <net/if.h> 61df8bae1dSRodney W. Grimes #include <net/route.h> 62983066f0SAlexander V. Chernikov #include <net/route/nhop.h> 63530c0060SRobert Watson #include <net/vnet.h> 64df8bae1dSRodney W. Grimes 65df8bae1dSRodney W. Grimes #include <netinet/in.h> 6657f60867SMark Johnston #include <netinet/in_kdtrace.h> 67df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 68df8bae1dSRodney W. Grimes #include <netinet/ip.h> 69df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 70df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 71ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 72fb59c426SYoshinobu Inoue #ifdef INET6 73686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 74686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 75fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 76fb59c426SYoshinobu Inoue #endif 772de3e790SGleb Smirnoff #include <netinet/tcp.h> 78a00f4ac2SGleb Smirnoff #define TCPOUTFLAGS 79df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 80df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 81df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 8269c7c811SRandall Stewart #include <netinet/tcp_log_buf.h> 83f7220c48SRichard Scheffenegger #include <netinet/tcp_syncache.h> 84f7220c48SRichard Scheffenegger #include <netinet/tcp_timer.h> 85df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 864644fda3SGleb Smirnoff #include <netinet/cc/cc.h> 87c560df6fSPatrick Kelsey #include <netinet/tcp_fastopen.h> 8886a996e6SHiren Panchasara #ifdef TCPPCAP 8986a996e6SHiren Panchasara #include <netinet/tcp_pcap.h> 9086a996e6SHiren Panchasara #endif 9109fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 9209fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 9309fe6320SNavdeep Parhar #endif 94f7220c48SRichard Scheffenegger #include <netinet/tcp_ecn.h> 95df8bae1dSRodney W. Grimes 96fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 97b9234fafSSam Leffler 989e644c23SMichael Tuexen #include <netinet/udp.h> 999e644c23SMichael Tuexen #include <netinet/udp_var.h> 100db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 101db4f9cc7SJonathan Lemon 102aed55708SRobert Watson #include <security/mac/mac_framework.h> 103aed55708SRobert Watson 10482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, path_mtu_discovery) = 1; 1056df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_VNET | CTLFLAG_RW, 106eddfbb76SRobert Watson &VNET_NAME(path_mtu_discovery), 1, 107eddfbb76SRobert Watson "Enable Path MTU Discovery"); 1089a039a5fSDavid Greenman 10982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_tso) = 1; 1106df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_VNET | CTLFLAG_RW, 111eddfbb76SRobert Watson &VNET_NAME(tcp_do_tso), 0, 112eddfbb76SRobert Watson "Enable TCP Segmentation Offload"); 113b3c0f300SAndre Oppermann 114873789cbSAndre Oppermann VNET_DEFINE(int, tcp_sendspace) = 1024*32; 115873789cbSAndre Oppermann #define V_tcp_sendspace VNET(tcp_sendspace) 1166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_VNET | CTLFLAG_RW, 117873789cbSAndre Oppermann &VNET_NAME(tcp_sendspace), 0, "Initial send socket buffer size"); 118873789cbSAndre Oppermann 11982cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autosndbuf) = 1; 1206df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 121eddfbb76SRobert Watson &VNET_NAME(tcp_do_autosndbuf), 0, 122eddfbb76SRobert Watson "Enable automatic send buffer sizing"); 1236741ecf5SAndre Oppermann 12482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_inc) = 8*1024; 1256df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 126eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_inc), 0, 1278b615593SMarko Zec "Incrementor step size of automatic send buffer"); 1286741ecf5SAndre Oppermann 129b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_max) = 2*1024*1024; 1306df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 131eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_max), 0, 1328b615593SMarko Zec "Max size of automatic send buffer"); 1336741ecf5SAndre Oppermann 134ac952dd2SSean Bruno VNET_DEFINE(int, tcp_sendbuf_auto_lowat) = 0; 135ac952dd2SSean Bruno #define V_tcp_sendbuf_auto_lowat VNET(tcp_sendbuf_auto_lowat) 136ac952dd2SSean Bruno SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto_lowat, CTLFLAG_VNET | CTLFLAG_RW, 137ac952dd2SSean Bruno &VNET_NAME(tcp_sendbuf_auto_lowat), 0, 138ac952dd2SSean Bruno "Modify threshold for auto send buffer growth to account for SO_SNDLOWAT"); 139ac952dd2SSean Bruno 140425b7639SSepherosa Ziehau /* 141425b7639SSepherosa Ziehau * Make sure that either retransmit or persist timer is set for SYN, FIN and 142425b7639SSepherosa Ziehau * non-ACK. 143425b7639SSepherosa Ziehau */ 144425b7639SSepherosa Ziehau #define TCP_XMIT_TIMER_ASSERT(tp, len, th_flags) \ 145fd29ff5dSRandall Stewart KASSERT(((len) == 0 && ((th_flags) & (TH_SYN | TH_FIN)) == 0) ||\ 146425b7639SSepherosa Ziehau tcp_timer_active((tp), TT_REXMT) || \ 147425b7639SSepherosa Ziehau tcp_timer_active((tp), TT_PERSIST), \ 148425b7639SSepherosa Ziehau ("neither rexmt nor persist timer is set")) 149425b7639SSepherosa Ziehau 150bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 151dbc42409SLawrence Stewart /* 1520f5e7edcSKevin Lo * Wrapper for the TCP established output helper hook. 15339bc9de5SLawrence Stewart */ 15489e560f4SRandall Stewart void 15539bc9de5SLawrence Stewart hhook_run_tcp_est_out(struct tcpcb *tp, struct tcphdr *th, 1563ac12506SJonathan T. Looney struct tcpopt *to, uint32_t len, int tso) 15739bc9de5SLawrence Stewart { 15839bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 15939bc9de5SLawrence Stewart 16039bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_OUT]->hhh_nhooks > 0) { 16139bc9de5SLawrence Stewart hhook_data.tp = tp; 16239bc9de5SLawrence Stewart hhook_data.th = th; 16339bc9de5SLawrence Stewart hhook_data.to = to; 16439bc9de5SLawrence Stewart hhook_data.len = len; 16539bc9de5SLawrence Stewart hhook_data.tso = tso; 16639bc9de5SLawrence Stewart 16739bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_OUT], &hhook_data, 168e68b3792SGleb Smirnoff &tp->t_osd); 16939bc9de5SLawrence Stewart } 17039bc9de5SLawrence Stewart } 171bd79708dSJonathan T. Looney #endif 17239bc9de5SLawrence Stewart 17339bc9de5SLawrence Stewart /* 174dbc42409SLawrence Stewart * CC wrapper hook functions 175dbc42409SLawrence Stewart */ 176cd84e78fSRandall Stewart void 177dbc42409SLawrence Stewart cc_after_idle(struct tcpcb *tp) 178dbc42409SLawrence Stewart { 1799eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(tptoinpcb(tp)); 180dbc42409SLawrence Stewart 181dbc42409SLawrence Stewart if (CC_ALGO(tp)->after_idle != NULL) 182e68b3792SGleb Smirnoff CC_ALGO(tp)->after_idle(&tp->t_ccv); 183dbc42409SLawrence Stewart } 1846741ecf5SAndre Oppermann 185df8bae1dSRodney W. Grimes /* 186df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 187df8bae1dSRodney W. Grimes */ 188df8bae1dSRodney W. Grimes int 1895b08b46aSGleb Smirnoff tcp_default_output(struct tcpcb *tp) 190df8bae1dSRodney W. Grimes { 1919eb0e832SGleb Smirnoff struct socket *so = tptosocket(tp); 1929eb0e832SGleb Smirnoff struct inpcb *inp = tptoinpcb(tp); 1933ac12506SJonathan T. Looney int32_t len; 1943ac12506SJonathan T. Looney uint32_t recwin, sendwin; 195f7220c48SRichard Scheffenegger uint16_t flags; 196f7220c48SRichard Scheffenegger int off, error = 0; /* Keep compiler happy */ 197581a046aSRandall Stewart u_int if_hw_tsomaxsegcount = 0; 19882e837f8SWarner Losh u_int if_hw_tsomaxsegsize = 0; 199f10e85d7SLuigi Rizzo struct mbuf *m; 200fb59c426SYoshinobu Inoue struct ip *ip = NULL; 201f10e85d7SLuigi Rizzo struct tcphdr *th; 202a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 2039e644c23SMichael Tuexen unsigned ipoptlen, optlen, hdrlen, ulen; 2049ad0173dSBjoern A. Zeeb unsigned ipsec_optlen = 0; 20510d20c84SMatt Macy int idle, sendalot, curticks; 20602a1a643SAndre Oppermann int sack_rxmit, sack_bytes_rxmt; 2076d90faf3SPaul Saab struct sackhole *p; 208df0633a1SGleb Smirnoff int tso, mtu; 20902a1a643SAndre Oppermann struct tcpopt to; 2109e644c23SMichael Tuexen struct udphdr *udp = NULL; 211bd30a121SMichael Tuexen struct tcp_log_buffer *lgb; 212c560df6fSPatrick Kelsey unsigned int wanted_cookie = 0; 213c560df6fSPatrick Kelsey unsigned int dont_sendalot = 0; 214fb59c426SYoshinobu Inoue #ifdef INET6 215f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 2162f201df1SAlfonso const bool isipv6 = (inp->inp_vflag & INP_IPV6) != 0; 217fb59c426SYoshinobu Inoue #endif 218b2e60773SJohn Baldwin #ifdef KERN_TLS 219c0e4090eSAndrew Gallatin const bool hw_tls = tp->t_nic_ktls_xmit != 0; 220b2e60773SJohn Baldwin #else 221b2e60773SJohn Baldwin const bool hw_tls = false; 222b2e60773SJohn Baldwin #endif 223df8bae1dSRodney W. Grimes 224109eb549SGleb Smirnoff NET_EPOCH_ASSERT(); 2259eb0e832SGleb Smirnoff INP_WLOCK_ASSERT(inp); 2263d6ade3aSJennifer Yang 22709fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 22809fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 22909fe6320SNavdeep Parhar return (tcp_offload_output(tp)); 23009fe6320SNavdeep Parhar #endif 23109fe6320SNavdeep Parhar 232281a0fd4SPatrick Kelsey /* 2338db239dcSMichael Tuexen * For TFO connections in SYN_SENT or SYN_RECEIVED, 2348db239dcSMichael Tuexen * only allow the initial SYN or SYN|ACK and those sent 2358db239dcSMichael Tuexen * by the retransmit timer. 236281a0fd4SPatrick Kelsey */ 237dd7b86e2SGleb Smirnoff if ((tp->t_flags & TF_FASTOPEN) && 2388db239dcSMichael Tuexen ((tp->t_state == TCPS_SYN_SENT) || 2398db239dcSMichael Tuexen (tp->t_state == TCPS_SYN_RECEIVED)) && 240dd7b86e2SGleb Smirnoff SEQ_GT(tp->snd_max, tp->snd_una) && /* SYN or SYN|ACK sent */ 241281a0fd4SPatrick Kelsey (tp->snd_nxt != tp->snd_una)) /* not a retransmit */ 242281a0fd4SPatrick Kelsey return (0); 24318a75309SPatrick Kelsey 244df8bae1dSRodney W. Grimes /* 245df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 246df8bae1dSRodney W. Grimes * and flags that will be used. 247df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 248df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 249df8bae1dSRodney W. Grimes */ 250c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 2519dc7d8a2SRichard Scheffenegger if (idle && (((ticks - tp->t_rcvtime) >= tp->t_rxtcur) || 2529dc7d8a2SRichard Scheffenegger (tp->t_sndtime && ((ticks - tp->t_sndtime) >= tp->t_rxtcur)))) 2532ea8da28SLawrence Stewart cc_after_idle(tp); 254c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 255c24d5daeSJayanth Vijayaraghavan if (idle) { 256c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 257c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 258c24d5daeSJayanth Vijayaraghavan idle = 0; 259c24d5daeSJayanth Vijayaraghavan } 260c24d5daeSJayanth Vijayaraghavan } 261df8bae1dSRodney W. Grimes again: 262440f4ba1SRichard Scheffenegger sendwin = 0; 2636d90faf3SPaul Saab /* 2646d90faf3SPaul Saab * If we've recently taken a timeout, snd_max will be greater than 2656d90faf3SPaul Saab * snd_nxt. There may be SACK information that allows us to avoid 2666d90faf3SPaul Saab * resending already delivered data. Adjust snd_nxt accordingly. 2676d90faf3SPaul Saab */ 2683529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2697dc78150SRichard Scheffenegger (tp->sackhint.nexthole != NULL) && 2707dc78150SRichard Scheffenegger !IN_FASTRECOVERY(tp->t_flags)) { 271440f4ba1SRichard Scheffenegger sendwin = tcp_sack_adjust(tp); 2727dc78150SRichard Scheffenegger } 273df8bae1dSRodney W. Grimes sendalot = 0; 274153e5b57SAndre Oppermann tso = 0; 275df0633a1SGleb Smirnoff mtu = 0; 276df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 277440f4ba1SRichard Scheffenegger sendwin = min(tp->snd_wnd, tp->snd_cwnd + sendwin); 278df8bae1dSRodney W. Grimes 279df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 280a0292f23SGarrett Wollman /* 2816d90faf3SPaul Saab * Send any SACK-generated retransmissions. If we're explicitly trying 2826d90faf3SPaul Saab * to send out new data (when sendalot is 1), bypass this function. 2836d90faf3SPaul Saab * If we retransmit in fast recovery mode, decrement snd_cwnd, since 2846d90faf3SPaul Saab * we're replacing a (future) new transmission with a retransmission 2856d90faf3SPaul Saab * now, and we previously incremented snd_cwnd in tcp_input(). 2866d90faf3SPaul Saab */ 2876d90faf3SPaul Saab /* 2886d90faf3SPaul Saab * Still in sack recovery , reset rxmit flag to zero. 2896d90faf3SPaul Saab */ 2906d90faf3SPaul Saab sack_rxmit = 0; 291a55db2b6SPaul Saab sack_bytes_rxmt = 0; 2926d90faf3SPaul Saab len = 0; 2936d90faf3SPaul Saab p = NULL; 294440f4ba1SRichard Scheffenegger if ((tp->t_flags & TF_SACK_PERMIT) && 2958f5a2e21SRichard Scheffenegger (IN_FASTRECOVERY(tp->t_flags) || 2968f5a2e21SRichard Scheffenegger (SEQ_LT(tp->snd_nxt, tp->snd_max) && (tp->t_dupacks >= tcprexmtthresh))) && 297a55db2b6SPaul Saab (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 2987dc78150SRichard Scheffenegger int32_t cwin; 299a55db2b6SPaul Saab 3007dc78150SRichard Scheffenegger if (IN_FASTRECOVERY(tp->t_flags)) { 3017dc78150SRichard Scheffenegger cwin = imax(sendwin - tcp_compute_pipe(tp), 0); 3027dc78150SRichard Scheffenegger } else { 3037dc78150SRichard Scheffenegger cwin = imax(sendwin - off, 0); 3047dc78150SRichard Scheffenegger } 305f787edd8SJayanth Vijayaraghavan /* Do not retransmit SACK segments beyond snd_recover */ 306f787edd8SJayanth Vijayaraghavan if (SEQ_GT(p->end, tp->snd_recover)) { 307f787edd8SJayanth Vijayaraghavan /* 308f787edd8SJayanth Vijayaraghavan * (At least) part of sack hole extends beyond 309f787edd8SJayanth Vijayaraghavan * snd_recover. Check to see if we can rexmit data 310f787edd8SJayanth Vijayaraghavan * for this hole. 311f787edd8SJayanth Vijayaraghavan */ 312f787edd8SJayanth Vijayaraghavan if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 313f787edd8SJayanth Vijayaraghavan /* 314f787edd8SJayanth Vijayaraghavan * Can't rexmit any more data for this hole. 315f787edd8SJayanth Vijayaraghavan * That data will be rexmitted in the next 316f787edd8SJayanth Vijayaraghavan * sack recovery episode, when snd_recover 317f787edd8SJayanth Vijayaraghavan * moves past p->rxmit. 318f787edd8SJayanth Vijayaraghavan */ 319f787edd8SJayanth Vijayaraghavan p = NULL; 320f787edd8SJayanth Vijayaraghavan goto after_sack_rexmit; 32128173d49SHans Petter Selasky } else { 322f787edd8SJayanth Vijayaraghavan /* Can rexmit part of the current hole */ 3237dc78150SRichard Scheffenegger len = SEQ_SUB(tp->snd_recover, p->rxmit); 3247dc78150SRichard Scheffenegger if (cwin <= len) { 3257dc78150SRichard Scheffenegger len = cwin; 3267dc78150SRichard Scheffenegger } else { 3277dc78150SRichard Scheffenegger sendalot = 1; 3287dc78150SRichard Scheffenegger } 32928173d49SHans Petter Selasky } 33028173d49SHans Petter Selasky } else { 3317dc78150SRichard Scheffenegger len = SEQ_SUB(p->end, p->rxmit); 3327dc78150SRichard Scheffenegger if (cwin <= len) { 3337dc78150SRichard Scheffenegger len = cwin; 3347dc78150SRichard Scheffenegger } else { 3357dc78150SRichard Scheffenegger sendalot = 1; 33628173d49SHans Petter Selasky } 3377dc78150SRichard Scheffenegger } 3387dc78150SRichard Scheffenegger /* we could have transmitted from the scoreboard, 3397dc78150SRichard Scheffenegger * but sendwin (expected flightsize) - pipe didn't 3407dc78150SRichard Scheffenegger * allow any transmission. 3417dc78150SRichard Scheffenegger * Bypass recalculating the possible transmission 3427dc78150SRichard Scheffenegger * length further down by setting sack_rxmit. 3437dc78150SRichard Scheffenegger * Wouldn't be here if there would have been 3447dc78150SRichard Scheffenegger * nothing in the scoreboard to transmit. 3457dc78150SRichard Scheffenegger */ 3467dc78150SRichard Scheffenegger sack_rxmit = 1; 3476d9e911fSMichael Tuexen if (len > 0) { 34828173d49SHans Petter Selasky off = SEQ_SUB(p->rxmit, tp->snd_una); 349f787edd8SJayanth Vijayaraghavan KASSERT(off >= 0,("%s: sack block to the left of una : %d", 350f787edd8SJayanth Vijayaraghavan __func__, off)); 3516d90faf3SPaul Saab } 3526d90faf3SPaul Saab } 353f787edd8SJayanth Vijayaraghavan after_sack_rexmit: 3546d90faf3SPaul Saab /* 355a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 356a0292f23SGarrett Wollman * state flags. 357a0292f23SGarrett Wollman */ 358a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 359a0292f23SGarrett Wollman flags |= TH_FIN; 360a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 361a0292f23SGarrett Wollman flags |= TH_SYN; 362a0292f23SGarrett Wollman 363dded4e9eSRichard Scheffenegger SOCK_SENDBUF_LOCK(so); 364df8bae1dSRodney W. Grimes /* 365df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 366df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 367df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 368df8bae1dSRodney W. Grimes * and go to transmit state. 369df8bae1dSRodney W. Grimes */ 3702cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) { 371201d185bSAndre Oppermann if (sendwin == 0) { 372df8bae1dSRodney W. Grimes /* 373df8bae1dSRodney W. Grimes * If we still have some data to send, then 374df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 375df8bae1dSRodney W. Grimes * happen below when it realizes that we 376df8bae1dSRodney W. Grimes * aren't sending all the data. However, 37729089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 378df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 379df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 380df8bae1dSRodney W. Grimes * up sending the packet without recording 381df8bae1dSRodney W. Grimes * that we sent the FIN bit. 382df8bae1dSRodney W. Grimes * 383df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 384df8bae1dSRodney W. Grimes * because if we don't have any more data 385df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 386df8bae1dSRodney W. Grimes * itself. 387df8bae1dSRodney W. Grimes */ 388cfa6009eSGleb Smirnoff if (off < sbused(&so->so_snd)) 389df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 390201d185bSAndre Oppermann sendwin = 1; 391df8bae1dSRodney W. Grimes } else { 392b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, 0); 393df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 394df8bae1dSRodney W. Grimes } 395df8bae1dSRodney W. Grimes } 396df8bae1dSRodney W. Grimes 39728257b5cSMatthew Dillon /* 39828257b5cSMatthew Dillon * If snd_nxt == snd_max and we have transmitted a FIN, the 39928257b5cSMatthew Dillon * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 400201d185bSAndre Oppermann * a negative length. This can also occur when TCP opens up 40128257b5cSMatthew Dillon * its congestion window while receiving additional duplicate 40228257b5cSMatthew Dillon * acks after fast-retransmit because TCP will reset snd_nxt 40328257b5cSMatthew Dillon * to snd_max after the fast-retransmit. 40428257b5cSMatthew Dillon * 40528257b5cSMatthew Dillon * In the normal retransmit-FIN-only case, however, snd_nxt will 40628257b5cSMatthew Dillon * be set to snd_una, the offset will be 0, and the length may 40728257b5cSMatthew Dillon * wind up 0. 4086d90faf3SPaul Saab * 4096d90faf3SPaul Saab * If sack_rxmit is true we are retransmitting from the scoreboard 4106d90faf3SPaul Saab * in which case len is already set. 41128257b5cSMatthew Dillon */ 412a55db2b6SPaul Saab if (sack_rxmit == 0) { 413440f4ba1SRichard Scheffenegger if ((sack_bytes_rxmt == 0) || SEQ_LT(tp->snd_nxt, tp->snd_max)) { 4147dc78150SRichard Scheffenegger len = imin(sbavail(&so->so_snd), sendwin) - off; 4152d05a1c8SRichard Scheffenegger } else { 416a55db2b6SPaul Saab /* 417a55db2b6SPaul Saab * We are inside of a SACK recovery episode and are 418a55db2b6SPaul Saab * sending new data, having retransmitted all the 419a55db2b6SPaul Saab * data possible in the scoreboard. 420a55db2b6SPaul Saab */ 4217dc78150SRichard Scheffenegger len = imin(sbavail(&so->so_snd) - off, 4227dc78150SRichard Scheffenegger sendwin - tcp_compute_pipe(tp)); 423a55db2b6SPaul Saab } 4248d03f2b5SPaul Saab } 425a0292f23SGarrett Wollman 426a0292f23SGarrett Wollman /* 427a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 428a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 429a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 430a0292f23SGarrett Wollman */ 431a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 4324b8e98d6SQing Li if (tp->t_state != TCPS_SYN_RECEIVED) 433a0292f23SGarrett Wollman flags &= ~TH_SYN; 434281a0fd4SPatrick Kelsey /* 435281a0fd4SPatrick Kelsey * When sending additional segments following a TFO SYN|ACK, 436281a0fd4SPatrick Kelsey * do not include the SYN bit. 437281a0fd4SPatrick Kelsey */ 438dd7b86e2SGleb Smirnoff if ((tp->t_flags & TF_FASTOPEN) && 439281a0fd4SPatrick Kelsey (tp->t_state == TCPS_SYN_RECEIVED)) 440281a0fd4SPatrick Kelsey flags &= ~TH_SYN; 441a0292f23SGarrett Wollman off--, len++; 442a0292f23SGarrett Wollman } 443a0292f23SGarrett Wollman 44481165e48SAndras Olah /* 445c94c54e4SAndre Oppermann * Be careful not to send data and/or FIN on SYN segments. 44681165e48SAndras Olah * This measure is needed to prevent interoperability problems 44781165e48SAndras Olah * with not fully conformant TCP implementations. 44881165e48SAndras Olah */ 449c94c54e4SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 45081165e48SAndras Olah len = 0; 45181165e48SAndras Olah flags &= ~TH_FIN; 45281165e48SAndras Olah } 45381165e48SAndras Olah 454281a0fd4SPatrick Kelsey /* 455c560df6fSPatrick Kelsey * On TFO sockets, ensure no data is sent in the following cases: 456c560df6fSPatrick Kelsey * 457c560df6fSPatrick Kelsey * - When retransmitting SYN|ACK on a passively-created socket 458c560df6fSPatrick Kelsey * 459c560df6fSPatrick Kelsey * - When retransmitting SYN on an actively created socket 460c560df6fSPatrick Kelsey * 461c560df6fSPatrick Kelsey * - When sending a zero-length cookie (cookie request) on an 462c560df6fSPatrick Kelsey * actively created socket 463c560df6fSPatrick Kelsey * 464c560df6fSPatrick Kelsey * - When the socket is in the CLOSED state (RST is being sent) 465281a0fd4SPatrick Kelsey */ 466dd7b86e2SGleb Smirnoff if ((tp->t_flags & TF_FASTOPEN) && 467c560df6fSPatrick Kelsey (((flags & TH_SYN) && (tp->t_rxtshift > 0)) || 468c560df6fSPatrick Kelsey ((tp->t_state == TCPS_SYN_SENT) && 469c560df6fSPatrick Kelsey (tp->t_tfo_client_cookie_len == 0)) || 470281a0fd4SPatrick Kelsey (flags & TH_RST))) 471281a0fd4SPatrick Kelsey len = 0; 472af700f43SMichael Tuexen 473af700f43SMichael Tuexen /* Without fast-open there should never be data sent on a SYN. */ 474af700f43SMichael Tuexen if ((flags & TH_SYN) && !(tp->t_flags & TF_FASTOPEN)) { 475af700f43SMichael Tuexen len = 0; 476af700f43SMichael Tuexen } 477af700f43SMichael Tuexen 47847a8e865SXin LI if (len <= 0) { 479df8bae1dSRodney W. Grimes /* 480df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 481df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 48228257b5cSMatthew Dillon * len will be < 0. Otherwise, window shrank 483df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 4842d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 4852d8266afSDavid Greenman * to (closed) window, and set the persist timer 4862d8266afSDavid Greenman * if it isn't already going. If the window didn't 4872d8266afSDavid Greenman * close completely, just wait for an ACK. 48847a8e865SXin LI * 48947a8e865SXin LI * We also do a general check here to ensure that 49047a8e865SXin LI * we will set the persist timer when we have data 49147a8e865SXin LI * to send, but a 0-byte window. This makes sure 49247a8e865SXin LI * the persist timer is set even if the packet 49347a8e865SXin LI * hits one of the "goto send" lines below. 494df8bae1dSRodney W. Grimes */ 495df8bae1dSRodney W. Grimes len = 0; 49647a8e865SXin LI if ((sendwin == 0) && (TCPS_HAVEESTABLISHED(tp->t_state)) && 49708af8aacSRandall Stewart (off < (int) sbavail(&so->so_snd)) && 49808af8aacSRandall Stewart !tcp_timer_active(tp, TT_PERSIST)) { 499b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 5002d8266afSDavid Greenman tp->t_rxtshift = 0; 501df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 502b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_PERSIST)) 5032d8266afSDavid Greenman tcp_setpersist(tp); 504df8bae1dSRodney W. Grimes } 505df8bae1dSRodney W. Grimes } 50628257b5cSMatthew Dillon 5076741ecf5SAndre Oppermann /* len will be >= 0 after this point. */ 508c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 5096741ecf5SAndre Oppermann 51066492feaSGleb Smirnoff tcp_sndbuf_autoscale(tp, so, sendwin); 5116741ecf5SAndre Oppermann 5126741ecf5SAndre Oppermann /* 513ed420311SAndre Oppermann * Decide if we can use TCP Segmentation Offloading (if supported by 514ed420311SAndre Oppermann * hardware). 515b3c0f300SAndre Oppermann * 516b3c0f300SAndre Oppermann * TSO may only be used if we are in a pure bulk sending state. The 517c9047eb7SRichard Scheffenegger * presence of TCP-MD5, IP options (IPsec), and possibly SACK 518c9047eb7SRichard Scheffenegger * retransmits prevent using TSO. With TSO the TCP header is the same 519b3c0f300SAndre Oppermann * (except for the sequence number) for all generated packets. This 520b3c0f300SAndre Oppermann * makes it impossible to transmit any options which vary per generated 521b3c0f300SAndre Oppermann * segment or packet. 522b6ff6724SHiren Panchasara * 523b6ff6724SHiren Panchasara * IPv4 handling has a clear separation of ip options and ip header 524b6ff6724SHiren Panchasara * flags while IPv6 combines both in in6p_outputopts. ip6_optlen() does 525b6ff6724SHiren Panchasara * the right thing below to provide length of just ip options and thus 526b6ff6724SHiren Panchasara * checking for ipoptlen is enough to decide if ip options are present. 52728257b5cSMatthew Dillon */ 528fcf59617SAndrey V. Elsukov #if defined(IPSEC) || defined(IPSEC_SUPPORT) 5299ad0173dSBjoern A. Zeeb /* 5309ad0173dSBjoern A. Zeeb * Pre-calculate here as we save another lookup into the darknesses 5319ad0173dSBjoern A. Zeeb * of IPsec that way and can actually decide if TSO is ok. 5329ad0173dSBjoern A. Zeeb */ 533fcf59617SAndrey V. Elsukov #ifdef INET6 534fcf59617SAndrey V. Elsukov if (isipv6 && IPSEC_ENABLED(ipv6)) 5359eb0e832SGleb Smirnoff ipsec_optlen = IPSEC_HDRSIZE(ipv6, inp); 536fcf59617SAndrey V. Elsukov #ifdef INET 537fcf59617SAndrey V. Elsukov else 5389ad0173dSBjoern A. Zeeb #endif 539fcf59617SAndrey V. Elsukov #endif /* INET6 */ 540fcf59617SAndrey V. Elsukov #ifdef INET 541fcf59617SAndrey V. Elsukov if (IPSEC_ENABLED(ipv4)) 5429eb0e832SGleb Smirnoff ipsec_optlen = IPSEC_HDRSIZE(ipv4, inp); 543fcf59617SAndrey V. Elsukov #endif /* INET */ 544fcf59617SAndrey V. Elsukov #endif /* IPSEC */ 545b6ff6724SHiren Panchasara #ifdef INET6 546b6ff6724SHiren Panchasara if (isipv6) 5479eb0e832SGleb Smirnoff ipoptlen = ip6_optlen(inp); 548b6ff6724SHiren Panchasara else 549b6ff6724SHiren Panchasara #endif 5509eb0e832SGleb Smirnoff if (inp->inp_options) 5519eb0e832SGleb Smirnoff ipoptlen = inp->inp_options->m_len - 552b6ff6724SHiren Panchasara offsetof(struct ipoption, ipopt_list); 553b6ff6724SHiren Panchasara else 554b6ff6724SHiren Panchasara ipoptlen = 0; 555b6ff6724SHiren Panchasara ipoptlen += ipsec_optlen; 556b6ff6724SHiren Panchasara 557ed420311SAndre Oppermann if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg && 5589e644c23SMichael Tuexen (tp->t_port == 0) && 559b3c0f300SAndre Oppermann ((tp->t_flags & TF_SIGNATURE) == 0) && 560c9047eb7SRichard Scheffenegger ((sack_rxmit == 0) || V_tcp_sack_tso) && 561b6919741SKonstantin Belousov (ipoptlen == 0 || (ipoptlen == ipsec_optlen && 562b6919741SKonstantin Belousov (tp->t_flags2 & TF2_IPSEC_TSO) != 0)) && 563b6919741SKonstantin Belousov !(flags & TH_SYN)) 564b3c0f300SAndre Oppermann tso = 1; 565153e5b57SAndre Oppermann 5662d05a1c8SRichard Scheffenegger if (SEQ_LT((sack_rxmit ? p->rxmit : tp->snd_nxt) + len, 5672d05a1c8SRichard Scheffenegger tp->snd_una + sbused(&so->so_snd))) { 5685d3b1b75SJayanth Vijayaraghavan flags &= ~TH_FIN; 5695d3b1b75SJayanth Vijayaraghavan } 570df8bae1dSRodney W. Grimes 5713ac12506SJonathan T. Looney recwin = lmin(lmax(sbspace(&so->so_rcv), 0), 5723ac12506SJonathan T. Looney (long)TCP_MAXWIN << tp->rcv_scale); 573df8bae1dSRodney W. Grimes 574df8bae1dSRodney W. Grimes /* 575262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 576262c1c1aSMatthew Dillon * conditions when len is non-zero: 577262c1c1aSMatthew Dillon * 578b3c0f300SAndre Oppermann * - We have a full segment (or more with TSO) 579262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 580262c1c1aSMatthew Dillon * either idle or running NODELAY 581262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 582262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 583262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 584262c1c1aSMatthew Dillon * - we need to retransmit 585df8bae1dSRodney W. Grimes */ 586df8bae1dSRodney W. Grimes if (len) { 587b3c0f300SAndre Oppermann if (len >= tp->t_maxseg) 588df8bae1dSRodney W. Grimes goto send; 589262c1c1aSMatthew Dillon /* 590e3995661SRichard Scheffenegger * As the TCP header options are now 591e3995661SRichard Scheffenegger * considered when setting up the initial 592e3995661SRichard Scheffenegger * window, we would not send the last segment 593e3995661SRichard Scheffenegger * if we skip considering the option length here. 594e3995661SRichard Scheffenegger * Note: this may not work when tcp headers change 595e3995661SRichard Scheffenegger * very dynamically in the future. 596e3995661SRichard Scheffenegger */ 597e3995661SRichard Scheffenegger if ((((tp->t_flags & TF_SIGNATURE) ? 598e3995661SRichard Scheffenegger PADTCPOLEN(TCPOLEN_SIGNATURE) : 0) + 599e3995661SRichard Scheffenegger ((tp->t_flags & TF_RCVD_TSTMP) ? 600e3995661SRichard Scheffenegger PADTCPOLEN(TCPOLEN_TIMESTAMP) : 0) + 601e3995661SRichard Scheffenegger len) >= tp->t_maxseg) 602e3995661SRichard Scheffenegger goto send; 603e3995661SRichard Scheffenegger /* 604262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 605262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 606262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 607262c1c1aSMatthew Dillon * 608262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 609262c1c1aSMatthew Dillon */ 610262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 611262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 6123ac12506SJonathan T. Looney (uint32_t)len + (uint32_t)off >= sbavail(&so->so_snd) && 613262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 614df8bae1dSRodney W. Grimes goto send; 615262c1c1aSMatthew Dillon } 6162cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */ 617df8bae1dSRodney W. Grimes goto send; 618a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 619df8bae1dSRodney W. Grimes goto send; 620262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 621df8bae1dSRodney W. Grimes goto send; 6226d90faf3SPaul Saab if (sack_rxmit) 6236d90faf3SPaul Saab goto send; 624df8bae1dSRodney W. Grimes } 625df8bae1dSRodney W. Grimes 626df8bae1dSRodney W. Grimes /* 627f62563d3SAndre Oppermann * Sending of standalone window updates. 628f62563d3SAndre Oppermann * 62978f59b4bSAndre Oppermann * Window updates are important when we close our window due to a 63078f59b4bSAndre Oppermann * full socket buffer and are opening it again after the application 631f62563d3SAndre Oppermann * reads data from it. Once the window has opened again and the 632f62563d3SAndre Oppermann * remote end starts to send again the ACK clock takes over and 633f62563d3SAndre Oppermann * provides the most current window information. 634f62563d3SAndre Oppermann * 63578f59b4bSAndre Oppermann * We must avoid the silly window syndrome whereas every read 636f62563d3SAndre Oppermann * from the receive buffer, no matter how small, causes a window 637f62563d3SAndre Oppermann * update to be sent. We also should avoid sending a flurry of 638f62563d3SAndre Oppermann * window updates when the socket buffer had queued a lot of data 639f62563d3SAndre Oppermann * and the application is doing small reads. 640f62563d3SAndre Oppermann * 641f62563d3SAndre Oppermann * Prevent a flurry of pointless window updates by only sending 642f62563d3SAndre Oppermann * an update when we can increase the advertized window by more 643f62563d3SAndre Oppermann * than 1/4th of the socket buffer capacity. When the buffer is 644f62563d3SAndre Oppermann * getting full or is very small be more aggressive and send an 645f62563d3SAndre Oppermann * update whenever we can increase by two mss sized segments. 646f62563d3SAndre Oppermann * In all other situations the ACK's to new incoming data will 647f62563d3SAndre Oppermann * carry further window increases. 6484249614cSAndre Oppermann * 6494249614cSAndre Oppermann * Don't send an independent window update if a delayed 6504249614cSAndre Oppermann * ACK is pending (it will get piggy-backed on it) or the 6514249614cSAndre Oppermann * remote side already has done a half-close and won't send 652c809435bSGleb Smirnoff * more data. 653df8bae1dSRodney W. Grimes */ 654b7de7d87SAndre Oppermann if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 6554249614cSAndre Oppermann !(tp->t_flags & TF_DELACK) && 656b7de7d87SAndre Oppermann !TCPS_HAVERCVDFIN(tp->t_state)) { 657df8bae1dSRodney W. Grimes /* 658f62563d3SAndre Oppermann * "adv" is the amount we could increase the window, 659df8bae1dSRodney W. Grimes * taking into account that we are limited by 660df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 661df8bae1dSRodney W. Grimes */ 6623ac12506SJonathan T. Looney int32_t adv; 663f701e30dSJohn Baldwin int oldwin; 664f701e30dSJohn Baldwin 6653ac12506SJonathan T. Looney adv = recwin; 666f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 667f701e30dSJohn Baldwin oldwin = (tp->rcv_adv - tp->rcv_nxt); 668f092a3c7SRandall Stewart if (adv > oldwin) 669f701e30dSJohn Baldwin adv -= oldwin; 670f092a3c7SRandall Stewart else 671f092a3c7SRandall Stewart adv = 0; 672f701e30dSJohn Baldwin } else 673f701e30dSJohn Baldwin oldwin = 0; 674df8bae1dSRodney W. Grimes 675da84b2e6SJohn Baldwin /* 6760dda76b8SJonathan T. Looney * If the new window size ends up being the same as or less 6770dda76b8SJonathan T. Looney * than the old size when it is scaled, then don't force 6780dda76b8SJonathan T. Looney * a window update. 679da84b2e6SJohn Baldwin */ 6800dda76b8SJonathan T. Looney if (oldwin >> tp->rcv_scale >= (adv + oldwin) >> tp->rcv_scale) 681da84b2e6SJohn Baldwin goto dontupdate; 682f62563d3SAndre Oppermann 6833ac12506SJonathan T. Looney if (adv >= (int32_t)(2 * tp->t_maxseg) && 6843ac12506SJonathan T. Looney (adv >= (int32_t)(so->so_rcv.sb_hiwat / 4) || 6853ac12506SJonathan T. Looney recwin <= (so->so_rcv.sb_hiwat / 8) || 68650075939SStephen Hurd so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg || 68750075939SStephen Hurd adv >= TCP_MAXWIN << tp->rcv_scale)) 688df8bae1dSRodney W. Grimes goto send; 6898d62aae8SMichael Tuexen if (2 * adv >= (int32_t)so->so_rcv.sb_hiwat) 6908d62aae8SMichael Tuexen goto send; 691df8bae1dSRodney W. Grimes } 692da84b2e6SJohn Baldwin dontupdate: 693df8bae1dSRodney W. Grimes 694df8bae1dSRodney W. Grimes /* 69528257b5cSMatthew Dillon * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 69628257b5cSMatthew Dillon * is also a catch-all for the retransmit timer timeout case. 697df8bae1dSRodney W. Grimes */ 698df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 699df8bae1dSRodney W. Grimes goto send; 700a0292f23SGarrett Wollman if ((flags & TH_RST) || 701a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 702df8bae1dSRodney W. Grimes goto send; 703df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 704df8bae1dSRodney W. Grimes goto send; 705df8bae1dSRodney W. Grimes /* 706df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 70728257b5cSMatthew Dillon * and we have not yet done so, then we need to send. 708df8bae1dSRodney W. Grimes */ 709df8bae1dSRodney W. Grimes if (flags & TH_FIN && 710df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 711df8bae1dSRodney W. Grimes goto send; 7126d90faf3SPaul Saab /* 7136d90faf3SPaul Saab * In SACK, it is possible for tcp_output to fail to send a segment 7146d90faf3SPaul Saab * after the retransmission timer has been turned off. Make sure 7156d90faf3SPaul Saab * that the retransmission timer is set. 7166d90faf3SPaul Saab */ 7173529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 7183529149eSAndre Oppermann SEQ_GT(tp->snd_max, tp->snd_una) && 719b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_REXMT) && 720b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 72108af8aacSRandall Stewart tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp)); 722cf2942b6SRobert Watson goto just_return; 7236d90faf3SPaul Saab } 724df8bae1dSRodney W. Grimes /* 725df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 726df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 727df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 728df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 729df8bae1dSRodney W. Grimes * persisting to move a small or zero window 730df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 731df8bae1dSRodney W. Grimes * 732b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_PERSIST) 7339b8b58e0SJonathan Lemon * is true when we are in persist state. 7342cdbfa66SPaul Saab * (tp->t_flags & TF_FORCEDATA) 735df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 736b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_REXMT) 737df8bae1dSRodney W. Grimes * is set when we are retransmitting 738df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 739df8bae1dSRodney W. Grimes * 740df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 741df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 742df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 743df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 744df8bae1dSRodney W. Grimes * otherwise force out a byte. 745df8bae1dSRodney W. Grimes */ 746cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd) && !tcp_timer_active(tp, TT_REXMT) && 747b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 748df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 749df8bae1dSRodney W. Grimes tcp_setpersist(tp); 750df8bae1dSRodney W. Grimes } 751df8bae1dSRodney W. Grimes 752df8bae1dSRodney W. Grimes /* 753df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 754df8bae1dSRodney W. Grimes */ 755cf2942b6SRobert Watson just_return: 756dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK(so); 757df8bae1dSRodney W. Grimes return (0); 758df8bae1dSRodney W. Grimes 759df8bae1dSRodney W. Grimes send: 760dded4e9eSRichard Scheffenegger SOCK_SENDBUF_LOCK_ASSERT(so); 761f6f6703fSSean Bruno if (len > 0) { 762f6f6703fSSean Bruno if (len >= tp->t_maxseg) 763f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 764f6f6703fSSean Bruno else 765f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 766f6f6703fSSean Bruno } 767df8bae1dSRodney W. Grimes /* 768df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 769df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 770df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 771df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 772df8bae1dSRodney W. Grimes * link header, i.e. 77333841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 774df8bae1dSRodney W. Grimes */ 775df8bae1dSRodney W. Grimes optlen = 0; 776fb59c426SYoshinobu Inoue #ifdef INET6 777fb59c426SYoshinobu Inoue if (isipv6) 778fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 779fb59c426SYoshinobu Inoue else 780fb59c426SYoshinobu Inoue #endif 781df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 78202a1a643SAndre Oppermann 783ed782b9fSMichael Tuexen if (flags & TH_SYN) { 784ed782b9fSMichael Tuexen tp->snd_nxt = tp->iss; 785ed782b9fSMichael Tuexen } 786ed782b9fSMichael Tuexen 78702a1a643SAndre Oppermann /* 78802a1a643SAndre Oppermann * Compute options for segment. 78902a1a643SAndre Oppermann * We only have to care about SYN and established connection 79002a1a643SAndre Oppermann * segments. Options for SYN-ACK segments are handled in TCP 79102a1a643SAndre Oppermann * syncache. 79202a1a643SAndre Oppermann */ 79302a1a643SAndre Oppermann to.to_flags = 0; 794f73d9fd2SGleb Smirnoff if ((tp->t_flags & TF_NOOPT) == 0) { 79502a1a643SAndre Oppermann /* Maximum segment size. */ 796df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 7979eb0e832SGleb Smirnoff to.to_mss = tcp_mssopt(&inp->inp_inc); 7989e644c23SMichael Tuexen if (tp->t_port) 7999e644c23SMichael Tuexen to.to_mss -= V_tcp_udp_tunneling_overhead; 80002a1a643SAndre Oppermann to.to_flags |= TOF_MSS; 80118a75309SPatrick Kelsey 802281a0fd4SPatrick Kelsey /* 803c560df6fSPatrick Kelsey * On SYN or SYN|ACK transmits on TFO connections, 804c560df6fSPatrick Kelsey * only include the TFO option if it is not a 805c560df6fSPatrick Kelsey * retransmit, as the presence of the TFO option may 806c560df6fSPatrick Kelsey * have caused the original SYN or SYN|ACK to have 807c560df6fSPatrick Kelsey * been dropped by a middlebox. 808281a0fd4SPatrick Kelsey */ 809dd7b86e2SGleb Smirnoff if ((tp->t_flags & TF_FASTOPEN) && 810281a0fd4SPatrick Kelsey (tp->t_rxtshift == 0)) { 811c560df6fSPatrick Kelsey if (tp->t_state == TCPS_SYN_RECEIVED) { 812281a0fd4SPatrick Kelsey to.to_tfo_len = TCP_FASTOPEN_COOKIE_LEN; 813c560df6fSPatrick Kelsey to.to_tfo_cookie = 814c560df6fSPatrick Kelsey (u_int8_t *)&tp->t_tfo_cookie.server; 815281a0fd4SPatrick Kelsey to.to_flags |= TOF_FASTOPEN; 816c560df6fSPatrick Kelsey wanted_cookie = 1; 817c560df6fSPatrick Kelsey } else if (tp->t_state == TCPS_SYN_SENT) { 818c560df6fSPatrick Kelsey to.to_tfo_len = 819c560df6fSPatrick Kelsey tp->t_tfo_client_cookie_len; 820c560df6fSPatrick Kelsey to.to_tfo_cookie = 821c560df6fSPatrick Kelsey tp->t_tfo_cookie.client; 822c560df6fSPatrick Kelsey to.to_flags |= TOF_FASTOPEN; 823c560df6fSPatrick Kelsey wanted_cookie = 1; 824c560df6fSPatrick Kelsey /* 825c560df6fSPatrick Kelsey * If we wind up having more data to 826c560df6fSPatrick Kelsey * send with the SYN than can fit in 827c560df6fSPatrick Kelsey * one segment, don't send any more 828c560df6fSPatrick Kelsey * until the SYN|ACK comes back from 829c560df6fSPatrick Kelsey * the other end. 830c560df6fSPatrick Kelsey */ 831c560df6fSPatrick Kelsey dont_sendalot = 1; 832c560df6fSPatrick Kelsey } 833281a0fd4SPatrick Kelsey } 834df8bae1dSRodney W. Grimes } 83502a1a643SAndre Oppermann /* Window scaling. */ 83602a1a643SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 83702a1a643SAndre Oppermann to.to_wscale = tp->request_r_scale; 83802a1a643SAndre Oppermann to.to_flags |= TOF_SCALE; 839df8bae1dSRodney W. Grimes } 84002a1a643SAndre Oppermann /* Timestamps. */ 84102a1a643SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) || 84202a1a643SAndre Oppermann ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 84310d20c84SMatt Macy curticks = tcp_ts_getticks(); 84410d20c84SMatt Macy to.to_tsval = curticks + tp->ts_offset; 84502a1a643SAndre Oppermann to.to_tsecr = tp->ts_recent; 84602a1a643SAndre Oppermann to.to_flags |= TOF_TS; 84710d20c84SMatt Macy if (tp->t_rxtshift == 1) 84810d20c84SMatt Macy tp->t_badrxtwin = curticks; 849e44c1887SSteven Hartland } 850e44c1887SSteven Hartland 8516741ecf5SAndre Oppermann /* Set receive buffer autosizing timestamp. */ 85202a1a643SAndre Oppermann if (tp->rfbuf_ts == 0 && 85302a1a643SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) 854d8951c8aSBjoern A. Zeeb tp->rfbuf_ts = tcp_ts_getticks(); 855e44c1887SSteven Hartland 85602a1a643SAndre Oppermann /* Selective ACK's. */ 8573529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 85802a1a643SAndre Oppermann if (flags & TH_SYN) 85902a1a643SAndre Oppermann to.to_flags |= TOF_SACKPERM; 86002a1a643SAndre Oppermann else if (TCPS_HAVEESTABLISHED(tp->t_state) && 86102a1a643SAndre Oppermann tp->rcv_numsacks > 0) { 86202a1a643SAndre Oppermann to.to_flags |= TOF_SACK; 86302a1a643SAndre Oppermann to.to_nsacks = tp->rcv_numsacks; 86402a1a643SAndre Oppermann to.to_sacks = (u_char *)tp->sackblks; 86502a1a643SAndre Oppermann } 86602a1a643SAndre Oppermann } 867fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 86802a1a643SAndre Oppermann /* TCP-MD5 (RFC2385). */ 869fcf59617SAndrey V. Elsukov /* 870fcf59617SAndrey V. Elsukov * Check that TCP_MD5SIG is enabled in tcpcb to 871fcf59617SAndrey V. Elsukov * account the size needed to set this TCP option. 872fcf59617SAndrey V. Elsukov */ 87302a1a643SAndre Oppermann if (tp->t_flags & TF_SIGNATURE) 87402a1a643SAndre Oppermann to.to_flags |= TOF_SIGNATURE; 8751cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 8761cfd4b53SBruce M Simpson 87702a1a643SAndre Oppermann /* Processing the options. */ 8784a411b9fSBjoern A. Zeeb hdrlen += optlen = tcp_addoptions(&to, opt); 879c560df6fSPatrick Kelsey /* 880c560df6fSPatrick Kelsey * If we wanted a TFO option to be added, but it was unable 881c560df6fSPatrick Kelsey * to fit, ensure no data is sent. 882c560df6fSPatrick Kelsey */ 883dd7b86e2SGleb Smirnoff if ((tp->t_flags & TF_FASTOPEN) && wanted_cookie && 884c560df6fSPatrick Kelsey !(to.to_flags & TOF_FASTOPEN)) 885c560df6fSPatrick Kelsey len = 0; 886be3f3b5eSPaul Saab } 8879e644c23SMichael Tuexen if (tp->t_port) { 8889e644c23SMichael Tuexen if (V_tcp_udp_tunneling_port == 0) { 8899e644c23SMichael Tuexen /* The port was removed?? */ 890dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK(so); 8919e644c23SMichael Tuexen return (EHOSTUNREACH); 8929e644c23SMichael Tuexen } 8939e644c23SMichael Tuexen hdrlen += sizeof(struct udphdr); 8949e644c23SMichael Tuexen } 895df8bae1dSRodney W. Grimes /* 896df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 8970c39d38dSGleb Smirnoff * bump the packet length beyond the t_maxseg length. 898a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 899a0292f23SGarrett Wollman * the segment. 900df8bae1dSRodney W. Grimes */ 9010c39d38dSGleb Smirnoff if (len + optlen + ipoptlen > tp->t_maxseg) { 902297a37f3SDavid Greenman flags &= ~TH_FIN; 903ed420311SAndre Oppermann 904b3c0f300SAndre Oppermann if (tso) { 9059fd573c3SHans Petter Selasky u_int if_hw_tsomax; 9069fd573c3SHans Petter Selasky u_int moff; 9079fd573c3SHans Petter Selasky int max_len; 9089fd573c3SHans Petter Selasky 9099fd573c3SHans Petter Selasky /* extract TSO information */ 9109fd573c3SHans Petter Selasky if_hw_tsomax = tp->t_tsomax; 9119fd573c3SHans Petter Selasky if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 9129fd573c3SHans Petter Selasky if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 9139fd573c3SHans Petter Selasky 9149fd573c3SHans Petter Selasky /* 9159fd573c3SHans Petter Selasky * Limit a TSO burst to prevent it from 9169fd573c3SHans Petter Selasky * overflowing or exceeding the maximum length 9179fd573c3SHans Petter Selasky * allowed by the network interface: 9189fd573c3SHans Petter Selasky */ 919b6919741SKonstantin Belousov KASSERT(ipoptlen == ipsec_optlen, 920ed420311SAndre Oppermann ("%s: TSO can't do IP options", __func__)); 921ed420311SAndre Oppermann 922ed420311SAndre Oppermann /* 9239fd573c3SHans Petter Selasky * Check if we should limit by maximum payload 9249fd573c3SHans Petter Selasky * length: 925ed420311SAndre Oppermann */ 9269fd573c3SHans Petter Selasky if (if_hw_tsomax != 0) { 9279fd573c3SHans Petter Selasky /* compute maximum TSO length */ 928b6919741SKonstantin Belousov max_len = if_hw_tsomax - hdrlen - 929b6919741SKonstantin Belousov ipsec_optlen - max_linkhdr; 9309fd573c3SHans Petter Selasky if (max_len <= 0) { 9319fd573c3SHans Petter Selasky len = 0; 9323c7c188cSHans Petter Selasky } else if (len > max_len) { 933b3c0f300SAndre Oppermann sendalot = 1; 9343c7c188cSHans Petter Selasky len = max_len; 9359fd573c3SHans Petter Selasky } 9369fd573c3SHans Petter Selasky } 93774e10fb6SJohn Baldwin 938ed420311SAndre Oppermann /* 939ed420311SAndre Oppermann * Prevent the last segment from being 9409fd573c3SHans Petter Selasky * fractional unless the send sockbuf can be 9419fd573c3SHans Petter Selasky * emptied: 942ed420311SAndre Oppermann */ 943b6919741SKonstantin Belousov max_len = tp->t_maxseg - optlen - ipsec_optlen; 9443ac12506SJonathan T. Looney if (((uint32_t)off + (uint32_t)len) < 9453ac12506SJonathan T. Looney sbavail(&so->so_snd)) { 9463c7c188cSHans Petter Selasky moff = len % max_len; 9479fd573c3SHans Petter Selasky if (moff != 0) { 9489fd573c3SHans Petter Selasky len -= moff; 949b3c0f300SAndre Oppermann sendalot = 1; 950ed420311SAndre Oppermann } 9519fd573c3SHans Petter Selasky } 9529fd573c3SHans Petter Selasky 9539fd573c3SHans Petter Selasky /* 9549fd573c3SHans Petter Selasky * In case there are too many small fragments 9559fd573c3SHans Petter Selasky * don't use TSO: 9569fd573c3SHans Petter Selasky */ 9573c7c188cSHans Petter Selasky if (len <= max_len) { 9583c7c188cSHans Petter Selasky len = max_len; 9599fd573c3SHans Petter Selasky sendalot = 1; 9609fd573c3SHans Petter Selasky tso = 0; 9619fd573c3SHans Petter Selasky } 962ed420311SAndre Oppermann 963ed420311SAndre Oppermann /* 964ed420311SAndre Oppermann * Send the FIN in a separate segment 965ed420311SAndre Oppermann * after the bulk sending is done. 966ed420311SAndre Oppermann * We don't trust the TSO implementations 967ed420311SAndre Oppermann * to clear the FIN flag on all but the 968ed420311SAndre Oppermann * last segment. 969ed420311SAndre Oppermann */ 970ed420311SAndre Oppermann if (tp->t_flags & TF_NEEDFIN) 971ed420311SAndre Oppermann sendalot = 1; 972b3c0f300SAndre Oppermann } else { 97312a43d0dSMichael Tuexen if (optlen + ipoptlen >= tp->t_maxseg) { 97412a43d0dSMichael Tuexen /* 97512a43d0dSMichael Tuexen * Since we don't have enough space to put 97612a43d0dSMichael Tuexen * the IP header chain and the TCP header in 97712a43d0dSMichael Tuexen * one packet as required by RFC 7112, don't 97812a43d0dSMichael Tuexen * send it. Also ensure that at least one 97912a43d0dSMichael Tuexen * byte of the payload can be put into the 98012a43d0dSMichael Tuexen * TCP segment. 98112a43d0dSMichael Tuexen */ 982dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK(so); 98312a43d0dSMichael Tuexen error = EMSGSIZE; 98412a43d0dSMichael Tuexen sack_rxmit = 0; 98512a43d0dSMichael Tuexen goto out; 98612a43d0dSMichael Tuexen } 9870c39d38dSGleb Smirnoff len = tp->t_maxseg - optlen - ipoptlen; 988df8bae1dSRodney W. Grimes sendalot = 1; 989c560df6fSPatrick Kelsey if (dont_sendalot) 990c560df6fSPatrick Kelsey sendalot = 0; 991df8bae1dSRodney W. Grimes } 992ed420311SAndre Oppermann } else 993ed420311SAndre Oppermann tso = 0; 994ed420311SAndre Oppermann 995ed420311SAndre Oppermann KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 996ed420311SAndre Oppermann ("%s: len > IP_MAXPACKET", __func__)); 997df8bae1dSRodney W. Grimes 998a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 999a683a7ddSYoshinobu Inoue #ifdef INET6 1000a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 1001a683a7ddSYoshinobu Inoue #else 1002df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 1003a683a7ddSYoshinobu Inoue #endif 1004f10e85d7SLuigi Rizzo panic("tcphdr too big"); 1005a0292f23SGarrett Wollman /*#endif*/ 1006df8bae1dSRodney W. Grimes 1007df8bae1dSRodney W. Grimes /* 1008c4982faeSBjoern A. Zeeb * This KASSERT is here to catch edge cases at a well defined place. 1009c4982faeSBjoern A. Zeeb * Before, those had triggered (random) panic conditions further down. 1010c4982faeSBjoern A. Zeeb */ 1011c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 1012c4982faeSBjoern A. Zeeb 1013c4982faeSBjoern A. Zeeb /* 1014df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 1015df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 1016df8bae1dSRodney W. Grimes * the template for sends on this connection. 1017df8bae1dSRodney W. Grimes */ 1018df8bae1dSRodney W. Grimes if (len) { 10194e023759SAndre Oppermann struct mbuf *mb; 1020581a046aSRandall Stewart struct sockbuf *msb; 10214e023759SAndre Oppermann u_int moff; 10224e023759SAndre Oppermann 1023adc56f5aSEdward Tomasz Napierala if ((tp->t_flags & TF_FORCEDATA) && len == 1) { 102478b50714SRobert Watson TCPSTAT_INC(tcps_sndprobe); 1025adc56f5aSEdward Tomasz Napierala #ifdef STATS 1026adc56f5aSEdward Tomasz Napierala if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1027adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u32(tp->t_stats, 1028adc56f5aSEdward Tomasz Napierala VOI_TCP_RETXPB, len); 1029adc56f5aSEdward Tomasz Napierala else 1030adc56f5aSEdward Tomasz Napierala stats_voi_update_abs_u64(tp->t_stats, 1031adc56f5aSEdward Tomasz Napierala VOI_TCP_TXPB, len); 1032adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 1033adc56f5aSEdward Tomasz Napierala } else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) { 1034f5d34df5SGeorge V. Neville-Neil tp->t_sndrexmitpack++; 103578b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitpack); 103678b50714SRobert Watson TCPSTAT_ADD(tcps_sndrexmitbyte, len); 10370932fb56SRichard Scheffenegger if (sack_rxmit) { 10380932fb56SRichard Scheffenegger TCPSTAT_INC(tcps_sack_rexmits); 10392a9aae9eSRichard Scheffenegger if (tso) { 10402a9aae9eSRichard Scheffenegger TCPSTAT_INC(tcps_sack_rexmits_tso); 10412a9aae9eSRichard Scheffenegger } 10420932fb56SRichard Scheffenegger TCPSTAT_ADD(tcps_sack_rexmit_bytes, len); 10430932fb56SRichard Scheffenegger } 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) { 1064dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK(so); 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 { 1085519981e3SJohn Baldwin int32_t old_len; 1086519981e3SJohn Baldwin 1087581a046aSRandall Stewart if (SEQ_LT(tp->snd_nxt, tp->snd_max)) 1088581a046aSRandall Stewart msb = NULL; 1089581a046aSRandall Stewart else 1090581a046aSRandall Stewart msb = &so->so_snd; 1091519981e3SJohn Baldwin old_len = len; 1092581a046aSRandall Stewart m->m_next = tcp_m_copym(mb, moff, 1093581a046aSRandall Stewart &len, if_hw_tsomaxsegcount, 1094b2e60773SJohn Baldwin if_hw_tsomaxsegsize, msb, hw_tls); 1095519981e3SJohn Baldwin if (old_len != len) 1096519981e3SJohn Baldwin flags &= ~TH_FIN; 1097581a046aSRandall Stewart if (len <= (tp->t_maxseg - optlen)) { 1098581a046aSRandall Stewart /* 1099581a046aSRandall Stewart * Must have ran out of mbufs for the copy 1100581a046aSRandall Stewart * shorten it to no longer need tso. Lets 1101581a046aSRandall Stewart * not put on sendalot since we are low on 1102581a046aSRandall Stewart * mbufs. 1103581a046aSRandall Stewart */ 1104581a046aSRandall Stewart tso = 0; 1105581a046aSRandall Stewart } 11064e023759SAndre Oppermann if (m->m_next == NULL) { 1107dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK(so); 1108d7f570e6SGarrett Wollman (void) m_free(m); 110951823c3aSGarrett Wollman error = ENOBUFS; 11100e2bc05cSGleb Smirnoff sack_rxmit = 0; 111151823c3aSGarrett Wollman goto out; 111251823c3aSGarrett Wollman } 1113df8bae1dSRodney W. Grimes } 1114472ea5beSColin Percival 1115df8bae1dSRodney W. Grimes /* 1116df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 1117df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 1118df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 1119df8bae1dSRodney W. Grimes * a PUSH comes in.) 1120df8bae1dSRodney W. Grimes */ 11213ac12506SJonathan T. Looney if (((uint32_t)off + (uint32_t)len == sbused(&so->so_snd)) && 11223ac12506SJonathan T. Looney !(flags & TH_SYN)) 1123df8bae1dSRodney W. Grimes flags |= TH_PUSH; 1124dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK(so); 1125df8bae1dSRodney W. Grimes } else { 1126dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK(so); 1127df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 112878b50714SRobert Watson TCPSTAT_INC(tcps_sndacks); 1129df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 113078b50714SRobert Watson TCPSTAT_INC(tcps_sndctrl); 1131df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 113278b50714SRobert Watson TCPSTAT_INC(tcps_sndurg); 1133df8bae1dSRodney W. Grimes else 113478b50714SRobert Watson TCPSTAT_INC(tcps_sndwinup); 1135df8bae1dSRodney W. Grimes 1136aa8bd99dSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 1137df8bae1dSRodney W. Grimes if (m == NULL) { 1138df8bae1dSRodney W. Grimes error = ENOBUFS; 11390e2bc05cSGleb Smirnoff sack_rxmit = 0; 1140df8bae1dSRodney W. Grimes goto out; 1141df8bae1dSRodney W. Grimes } 1142fb59c426SYoshinobu Inoue #ifdef INET6 1143fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 1144fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 1145ed6a66caSRobert Watson M_ALIGN(m, hdrlen); 1146fb59c426SYoshinobu Inoue } else 1147fb59c426SYoshinobu Inoue #endif 1148df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1149df8bae1dSRodney W. Grimes m->m_len = hdrlen; 1150df8bae1dSRodney W. Grimes } 1151dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK_ASSERT(so); 1152df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 1153c488362eSRobert Watson #ifdef MAC 11549eb0e832SGleb Smirnoff mac_inpcb_create_mbuf(inp, m); 1155c488362eSRobert Watson #endif 1156fb59c426SYoshinobu Inoue #ifdef INET6 1157fb59c426SYoshinobu Inoue if (isipv6) { 1158fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 11599e644c23SMichael Tuexen if (tp->t_port) { 1160500eb6ddSMichael Tuexen udp = (struct udphdr *)((caddr_t)ip6 + sizeof(struct ip6_hdr)); 11619e644c23SMichael Tuexen udp->uh_sport = htons(V_tcp_udp_tunneling_port); 11629e644c23SMichael Tuexen udp->uh_dport = tp->t_port; 11639e644c23SMichael Tuexen ulen = hdrlen + len - sizeof(struct ip6_hdr); 11649e644c23SMichael Tuexen udp->uh_ulen = htons(ulen); 11659e644c23SMichael Tuexen th = (struct tcphdr *)(udp + 1); 11669e644c23SMichael Tuexen } else { 1167fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 11689e644c23SMichael Tuexen } 11699eb0e832SGleb Smirnoff tcpip_fillheaders(inp, tp->t_port, ip6, th); 1170fb59c426SYoshinobu Inoue } else 1171fb59c426SYoshinobu Inoue #endif /* INET6 */ 1172fb59c426SYoshinobu Inoue { 1173fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 11749e644c23SMichael Tuexen if (tp->t_port) { 1175500eb6ddSMichael Tuexen udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip)); 11769e644c23SMichael Tuexen udp->uh_sport = htons(V_tcp_udp_tunneling_port); 11779e644c23SMichael Tuexen udp->uh_dport = tp->t_port; 11789e644c23SMichael Tuexen ulen = hdrlen + len - sizeof(struct ip); 11799e644c23SMichael Tuexen udp->uh_ulen = htons(ulen); 11809e644c23SMichael Tuexen th = (struct tcphdr *)(udp + 1); 11819e644c23SMichael Tuexen } else 1182fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 11839eb0e832SGleb Smirnoff tcpip_fillheaders(inp, tp->t_port, ip, th); 1184fb59c426SYoshinobu Inoue } 1185df8bae1dSRodney W. Grimes 1186df8bae1dSRodney W. Grimes /* 1187df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 1188df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 1189df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 1190df8bae1dSRodney W. Grimes */ 1191df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 1192df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 1193df8bae1dSRodney W. Grimes tp->snd_nxt--; 1194df8bae1dSRodney W. Grimes /* 1195f2512ba1SRui Paulo * If we are starting a connection, send ECN setup 1196f2512ba1SRui Paulo * SYN packet. If we are on a retransmit, we may 1197f2512ba1SRui Paulo * resend those bits a number of times as per 1198f2512ba1SRui Paulo * RFC 3168. 1199f2512ba1SRui Paulo */ 1200f7220c48SRichard Scheffenegger if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) { 1201f7220c48SRichard Scheffenegger flags |= tcp_ecn_output_syn_sent(tp); 1202f2512ba1SRui Paulo } 1203f7220c48SRichard Scheffenegger /* Also handle parallel SYN for ECN */ 1204f7220c48SRichard Scheffenegger if ((TCPS_HAVERCVDSYN(tp->t_state)) && 12054012ef77SRichard Scheffenegger (tp->t_flags2 & (TF2_ECN_PERMIT | TF2_ACE_PERMIT))) { 12062ff07d92SRichard Scheffenegger int ect = tcp_ecn_output_established(tp, &flags, len, sack_rxmit); 1207f7220c48SRichard Scheffenegger if ((tp->t_state == TCPS_SYN_RECEIVED) && 1208f7220c48SRichard Scheffenegger (tp->t_flags2 & TF2_ECN_SND_ECE)) 1209f7220c48SRichard Scheffenegger tp->t_flags2 &= ~TF2_ECN_SND_ECE; 1210f2512ba1SRui Paulo #ifdef INET6 1211f026275eSRichard Scheffenegger if (isipv6) { 12122169f712SRichard Scheffenegger ip6->ip6_flow &= ~htonl(IPTOS_ECN_MASK << IPV6_FLOWLABEL_LEN); 12132169f712SRichard Scheffenegger ip6->ip6_flow |= htonl(ect << IPV6_FLOWLABEL_LEN); 1214f026275eSRichard Scheffenegger } 1215f2512ba1SRui Paulo else 1216f2512ba1SRui Paulo #endif 1217f026275eSRichard Scheffenegger { 1218f026275eSRichard Scheffenegger ip->ip_tos &= ~IPTOS_ECN_MASK; 1219f7220c48SRichard Scheffenegger ip->ip_tos |= ect; 1220f026275eSRichard Scheffenegger } 1221f2512ba1SRui Paulo } 1222f2512ba1SRui Paulo 1223f2512ba1SRui Paulo /* 1224df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 1225df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 1226df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 1227df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 1228df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 1229df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 1230df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 1231df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 1232df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 1233df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 1234df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 1235df8bae1dSRodney W. Grimes */ 1236a55db2b6SPaul Saab if (sack_rxmit == 0) { 1237b8152ba7SAndre Oppermann if (len || (flags & (TH_SYN|TH_FIN)) || 1238b8152ba7SAndre Oppermann tcp_timer_active(tp, TT_PERSIST)) 1239fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 1240df8bae1dSRodney W. Grimes else 1241fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 1242a55db2b6SPaul Saab } else { 12436d90faf3SPaul Saab th->th_seq = htonl(p->rxmit); 12446d90faf3SPaul Saab p->rxmit += len; 12450471a8c7SRichard Scheffenegger /* 12460471a8c7SRichard Scheffenegger * Lost Retransmission Detection 12470471a8c7SRichard Scheffenegger * trigger resending of a (then 12480471a8c7SRichard Scheffenegger * still existing) hole, when 12490471a8c7SRichard Scheffenegger * fack acks recoverypoint. 12500471a8c7SRichard Scheffenegger */ 12510471a8c7SRichard Scheffenegger if ((tp->t_flags & TF_LRD) && SEQ_GEQ(p->rxmit, p->end)) 12520471a8c7SRichard Scheffenegger p->rxmit = tp->snd_recover; 12530077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += len; 12546d90faf3SPaul Saab } 1255e5313869SRichard Scheffenegger if (IN_RECOVERY(tp->t_flags)) { 1256e5313869SRichard Scheffenegger /* 1257e5313869SRichard Scheffenegger * Account all bytes transmitted while 1258e5313869SRichard Scheffenegger * IN_RECOVERY, simplifying PRR and 1259e5313869SRichard Scheffenegger * Lost Retransmit Detection 1260e5313869SRichard Scheffenegger */ 1261e5313869SRichard Scheffenegger tp->sackhint.prr_out += len; 1262e5313869SRichard Scheffenegger } 1263fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 1264df8bae1dSRodney W. Grimes if (optlen) { 1265fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 1266fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 1267df8bae1dSRodney W. Grimes } 12681ebf4607SRichard Scheffenegger tcp_set_flags(th, flags); 1269df8bae1dSRodney W. Grimes /* 1270df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 1271df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 127279410718SMichael Tuexen * If a RST segment is sent, advertise a window of zero. 1273df8bae1dSRodney W. Grimes */ 127479410718SMichael Tuexen if (flags & TH_RST) { 127579410718SMichael Tuexen recwin = 0; 127679410718SMichael Tuexen } else { 12773ac12506SJonathan T. Looney if (recwin < (so->so_rcv.sb_hiwat / 4) && 12783ac12506SJonathan T. Looney recwin < tp->t_maxseg) 1279201d185bSAndre Oppermann recwin = 0; 1280f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 12813ac12506SJonathan T. Looney recwin < (tp->rcv_adv - tp->rcv_nxt)) 12823ac12506SJonathan T. Looney recwin = (tp->rcv_adv - tp->rcv_nxt); 128379410718SMichael Tuexen } 1284104ebb2aSAndre Oppermann /* 1285104ebb2aSAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1286104ebb2aSAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> 1287104ebb2aSAndre Oppermann * case is handled in syncache. 1288104ebb2aSAndre Oppermann */ 1289104ebb2aSAndre Oppermann if (flags & TH_SYN) 1290104ebb2aSAndre Oppermann th->th_win = htons((u_short) 1291104ebb2aSAndre Oppermann (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 12929028b6e0SRichard Scheffenegger else { 12939028b6e0SRichard Scheffenegger /* Avoid shrinking window with window scaling. */ 12949028b6e0SRichard Scheffenegger recwin = roundup2(recwin, 1 << tp->rcv_scale); 1295104ebb2aSAndre Oppermann th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 12969028b6e0SRichard Scheffenegger } 1297262c1c1aSMatthew Dillon 1298262c1c1aSMatthew Dillon /* 1299262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 1300262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 1301262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 1302262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 1303b2722702SRui Paulo * to read more data than can be buffered prior to transmitting on 1304262c1c1aSMatthew Dillon * the connection. 1305262c1c1aSMatthew Dillon */ 1306f5d34df5SGeorge V. Neville-Neil if (th->th_win == 0) { 1307f5d34df5SGeorge V. Neville-Neil tp->t_sndzerowin++; 1308262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 1309f5d34df5SGeorge V. Neville-Neil } else 1310262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 1311df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 1312fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 1313fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 1314df8bae1dSRodney W. Grimes } else 1315df8bae1dSRodney W. Grimes /* 1316df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 1317df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 1318df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 1319df8bae1dSRodney W. Grimes * number wraparound. 1320df8bae1dSRodney W. Grimes */ 1321df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 1322df8bae1dSRodney W. Grimes 1323df8bae1dSRodney W. Grimes /* 1324df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 1325df8bae1dSRodney W. Grimes * checksum extended header and data. 1326df8bae1dSRodney W. Grimes */ 1327fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 1328fcf59617SAndrey V. Elsukov 1329fcf59617SAndrey V. Elsukov #if defined(IPSEC_SUPPORT) || defined(TCP_SIGNATURE) 1330fcf59617SAndrey V. Elsukov if (to.to_flags & TOF_SIGNATURE) { 1331fcf59617SAndrey V. Elsukov /* 1332fcf59617SAndrey V. Elsukov * Calculate MD5 signature and put it into the place 1333fcf59617SAndrey V. Elsukov * determined before. 1334fcf59617SAndrey V. Elsukov * NOTE: since TCP options buffer doesn't point into 1335fcf59617SAndrey V. Elsukov * mbuf's data, calculate offset and use it. 1336fcf59617SAndrey V. Elsukov */ 13372aad6240SAndrey V. Elsukov if (!TCPMD5_ENABLED() || (error = TCPMD5_OUTPUT(m, th, 13382aad6240SAndrey V. Elsukov (u_char *)(th + 1) + (to.to_signature - opt))) != 0) { 1339fcf59617SAndrey V. Elsukov /* 1340fcf59617SAndrey V. Elsukov * Do not send segment if the calculation of MD5 1341fcf59617SAndrey V. Elsukov * digest has failed. 1342fcf59617SAndrey V. Elsukov */ 13432aad6240SAndrey V. Elsukov m_freem(m); 1344fcf59617SAndrey V. Elsukov goto out; 1345fcf59617SAndrey V. Elsukov } 1346fcf59617SAndrey V. Elsukov } 1347fcf59617SAndrey V. Elsukov #endif 1348fb59c426SYoshinobu Inoue #ifdef INET6 134945747ba5SBjoern A. Zeeb if (isipv6) { 1350fb59c426SYoshinobu Inoue /* 13513df96ee6SCy Schubert * There is no need to fill in ip6_plen right now. 13523df96ee6SCy Schubert * It will be filled later by ip6_output. 1353fb59c426SYoshinobu Inoue */ 13549e644c23SMichael Tuexen if (tp->t_port) { 13559e644c23SMichael Tuexen m->m_pkthdr.csum_flags = CSUM_UDP_IPV6; 13569e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13579e644c23SMichael Tuexen udp->uh_sum = in6_cksum_pseudo(ip6, ulen, IPPROTO_UDP, 0); 13589e644c23SMichael Tuexen th->th_sum = htons(0); 13599e644c23SMichael Tuexen UDPSTAT_INC(udps_opackets); 13609e644c23SMichael Tuexen } else { 1361356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 13629e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13639e644c23SMichael Tuexen th->th_sum = in6_cksum_pseudo(ip6, 13649e644c23SMichael Tuexen sizeof(struct tcphdr) + optlen + len, IPPROTO_TCP, 13659e644c23SMichael Tuexen 0); 13669e644c23SMichael Tuexen } 136745747ba5SBjoern A. Zeeb } 136845747ba5SBjoern A. Zeeb #endif 136945747ba5SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1370fb59c426SYoshinobu Inoue else 137145747ba5SBjoern A. Zeeb #endif 137245747ba5SBjoern A. Zeeb #ifdef INET 1373fb59c426SYoshinobu Inoue { 13749e644c23SMichael Tuexen if (tp->t_port) { 13759e644c23SMichael Tuexen m->m_pkthdr.csum_flags = CSUM_UDP; 13769e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 13779e644c23SMichael Tuexen udp->uh_sum = in_pseudo(ip->ip_src.s_addr, 13789e644c23SMichael Tuexen ip->ip_dst.s_addr, htons(ulen + IPPROTO_UDP)); 13799e644c23SMichael Tuexen th->th_sum = htons(0); 13809e644c23SMichael Tuexen UDPSTAT_INC(udps_opackets); 13819e644c23SMichael Tuexen } else { 1382356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP; 13839e644c23SMichael Tuexen m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 13849e644c23SMichael Tuexen th->th_sum = in_pseudo(ip->ip_src.s_addr, 13859e644c23SMichael Tuexen ip->ip_dst.s_addr, htons(sizeof(struct tcphdr) + 13869e644c23SMichael Tuexen IPPROTO_TCP + len + optlen)); 13879e644c23SMichael Tuexen } 1388fb59c426SYoshinobu Inoue 1389db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 1390db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 13916e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 1392fb59c426SYoshinobu Inoue } 139345747ba5SBjoern A. Zeeb #endif 1394df8bae1dSRodney W. Grimes 1395df8bae1dSRodney W. Grimes /* 1396b3c0f300SAndre Oppermann * Enable TSO and specify the size of the segments. 1397b3c0f300SAndre Oppermann * The TCP pseudo header checksum is always provided. 1398b3c0f300SAndre Oppermann */ 1399b3c0f300SAndre Oppermann if (tso) { 1400b6919741SKonstantin Belousov KASSERT(len > tp->t_maxseg - optlen - ipsec_optlen, 1401153e5b57SAndre Oppermann ("%s: len <= tso_segsz", __func__)); 14023579cf4cSKenneth D. Merry m->m_pkthdr.csum_flags |= CSUM_TSO; 1403b6919741SKonstantin Belousov m->m_pkthdr.tso_segsz = tp->t_maxseg - optlen - ipsec_optlen; 1404b3c0f300SAndre Oppermann } 1405b3c0f300SAndre Oppermann 140605fb056cSMichael Tuexen KASSERT(len + hdrlen == m_length(m, NULL), 140705fb056cSMichael Tuexen ("%s: mbuf chain shorter than expected: %d + %u != %u", 140805fb056cSMichael Tuexen __func__, len, hdrlen, m_length(m, NULL))); 1409ed420311SAndre Oppermann 1410bd79708dSJonathan T. Looney #ifdef TCP_HHOOK 141139bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */ 141239bc9de5SLawrence Stewart hhook_run_tcp_est_out(tp, th, &to, len, tso); 1413bd79708dSJonathan T. Looney #endif 141439bc9de5SLawrence Stewart 14152b9c9984SGeorge V. Neville-Neil TCP_PROBE3(debug__output, tp, th, m); 1416df8bae1dSRodney W. Grimes 1417dcaffbd6SJonathan T. Looney /* We're getting ready to send; log now. */ 1418bd30a121SMichael Tuexen /* XXXMT: We are not honoring verbose logging. */ 141969c7c811SRandall Stewart 142069c7c811SRandall Stewart if (tcp_bblogging_on(tp)) 142169c7c811SRandall Stewart lgb = tcp_log_event(tp, th, &so->so_rcv, &so->so_snd, 1422bd30a121SMichael Tuexen TCP_LOG_OUT, ERRNO_UNK, len, NULL, false, NULL, NULL, 0, 1423bd30a121SMichael Tuexen NULL); 1424bd30a121SMichael Tuexen else 1425bd30a121SMichael Tuexen lgb = NULL; 1426dcaffbd6SJonathan T. Looney 1427df8bae1dSRodney W. Grimes /* 1428df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 1429df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 1430df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 1431df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 1432df8bae1dSRodney W. Grimes */ 1433fb59c426SYoshinobu Inoue /* 143443630e62SHiren Panchasara * m->m_pkthdr.len should have been set before checksum calculation, 1435fb59c426SYoshinobu Inoue * because in6_cksum() need it. 1436fb59c426SYoshinobu Inoue */ 1437fb59c426SYoshinobu Inoue #ifdef INET6 1438fb59c426SYoshinobu Inoue if (isipv6) { 1439fb59c426SYoshinobu Inoue /* 1440fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 1441fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 1442fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 1443fb59c426SYoshinobu Inoue * Neighbor Discovery. 1444fb59c426SYoshinobu Inoue */ 14459eb0e832SGleb Smirnoff ip6->ip6_hlim = in6_selecthlim(inp, NULL); 1446fb59c426SYoshinobu Inoue 144757f60867SMark Johnston /* 144857f60867SMark Johnston * Set the packet size here for the benefit of DTrace probes. 144957f60867SMark Johnston * ip6_output() will set it properly; it's supposed to include 145057f60867SMark Johnston * the option header lengths as well. 145157f60867SMark Johnston */ 145257f60867SMark Johnston ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 145357f60867SMark Johnston 14540c39d38dSGleb Smirnoff if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) 14550f3e3bc5SSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 14560f3e3bc5SSean Bruno else 14570f3e3bc5SSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 14580f3e3bc5SSean Bruno 145957f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1460d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 146157f60867SMark Johnston 146257f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip6, tp, th); 146357f60867SMark Johnston 146486a996e6SHiren Panchasara #ifdef TCPPCAP 146586a996e6SHiren Panchasara /* Save packet, if requested. */ 146686a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_outpkts)); 146786a996e6SHiren Panchasara #endif 146886a996e6SHiren Panchasara 1469fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 14709eb0e832SGleb Smirnoff error = ip6_output(m, inp->in6p_outputopts, &inp->inp_route6, 1471df0633a1SGleb Smirnoff ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 14729eb0e832SGleb Smirnoff NULL, NULL, inp); 1473df0633a1SGleb Smirnoff 14749eb0e832SGleb Smirnoff if (error == EMSGSIZE && inp->inp_route6.ro_nh != NULL) 14759eb0e832SGleb Smirnoff mtu = inp->inp_route6.ro_nh->nh_mtu; 1476b287c6c7SBjoern A. Zeeb } 1477fb59c426SYoshinobu Inoue #endif /* INET6 */ 1478b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1479b287c6c7SBjoern A. Zeeb else 1480b287c6c7SBjoern A. Zeeb #endif 1481b287c6c7SBjoern A. Zeeb #ifdef INET 1482df8bae1dSRodney W. Grimes { 14838f134647SGleb Smirnoff ip->ip_len = htons(m->m_pkthdr.len); 1484686cdd19SJun-ichiro itojun Hagino #ifdef INET6 14859eb0e832SGleb Smirnoff if (inp->inp_vflag & INP_IPV6PROTO) 14869eb0e832SGleb Smirnoff ip->ip_ttl = in6_selecthlim(inp, NULL); 1487686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 1488f138387aSGarrett Wollman /* 148997d8d152SAndre Oppermann * If we do path MTU discovery, then we set DF on every packet. 149097d8d152SAndre Oppermann * This might not be the best thing to do according to RFC3390 149197d8d152SAndre Oppermann * Section 2. However the tcp hostcache migitates the problem 149297d8d152SAndre Oppermann * so it affects only the first tcp connection with a host. 1493e4e92660SAndre Oppermann * 1494e4e92660SAndre Oppermann * NB: Don't set DF on small MTU/MSS to have a safe fallback. 1495f138387aSGarrett Wollman */ 14960c39d38dSGleb Smirnoff if (V_path_mtu_discovery && tp->t_maxseg > V_tcp_minmss) { 1497f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 14989e644c23SMichael Tuexen if (tp->t_port == 0 || len < V_tcp_minmss) { 14999e644c23SMichael Tuexen ip->ip_off |= htons(IP_DF); 15009e644c23SMichael Tuexen } 1501f6f6703fSSean Bruno } else { 1502f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 1503f6f6703fSSean Bruno } 150497d8d152SAndre Oppermann 150557f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1506d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 150757f60867SMark Johnston 150857f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip, tp, th); 150957f60867SMark Johnston 151086a996e6SHiren Panchasara #ifdef TCPPCAP 151186a996e6SHiren Panchasara /* Save packet, if requested. */ 151286a996e6SHiren Panchasara tcp_pcap_add(th, m, &(tp->t_outpkts)); 151386a996e6SHiren Panchasara #endif 151486a996e6SHiren Panchasara 15159eb0e832SGleb Smirnoff error = ip_output(m, inp->inp_options, &inp->inp_route, 15169eb0e832SGleb Smirnoff ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, inp); 1517df0633a1SGleb Smirnoff 15189eb0e832SGleb Smirnoff if (error == EMSGSIZE && inp->inp_route.ro_nh != NULL) 15199eb0e832SGleb Smirnoff mtu = inp->inp_route.ro_nh->nh_mtu; 1520df8bae1dSRodney W. Grimes } 1521b287c6c7SBjoern A. Zeeb #endif /* INET */ 15220e2bc05cSGleb Smirnoff 1523bd30a121SMichael Tuexen if (lgb != NULL) { 1524bd30a121SMichael Tuexen lgb->tlb_errno = error; 1525bd30a121SMichael Tuexen lgb = NULL; 1526bd30a121SMichael Tuexen } 15270e2bc05cSGleb Smirnoff out: 152867e89281SRandall Stewart if (error == 0) 15299e4d9e4cSRandall Stewart tcp_account_for_send(tp, len, (tp->snd_nxt != tp->snd_max), 0, hw_tls); 15300e2bc05cSGleb Smirnoff /* 15310e2bc05cSGleb Smirnoff * In transmit state, time the transmission and arrange for 1532a8e817cfSRichard Scheffenegger * the retransmit. In persist state, just set snd_max. In a closed 1533a8e817cfSRichard Scheffenegger * state just return. 15340e2bc05cSGleb Smirnoff */ 1535a8e817cfSRichard Scheffenegger if (flags & TH_RST) { 1536a8e817cfSRichard Scheffenegger TCPSTAT_INC(tcps_sndtotal); 1537a8e817cfSRichard Scheffenegger return (0); 1538a8e817cfSRichard Scheffenegger } else if ((tp->t_flags & TF_FORCEDATA) == 0 || 15390e2bc05cSGleb Smirnoff !tcp_timer_active(tp, TT_PERSIST)) { 15400e2bc05cSGleb Smirnoff tcp_seq startseq = tp->snd_nxt; 15410e2bc05cSGleb Smirnoff 15420e2bc05cSGleb Smirnoff /* 15430e2bc05cSGleb Smirnoff * Advance snd_nxt over sequence space of this segment. 15440e2bc05cSGleb Smirnoff */ 15450e2bc05cSGleb Smirnoff if (flags & (TH_SYN|TH_FIN)) { 15460e2bc05cSGleb Smirnoff if (flags & TH_SYN) 15470e2bc05cSGleb Smirnoff tp->snd_nxt++; 15480e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 15490e2bc05cSGleb Smirnoff tp->snd_nxt++; 15500e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 15510e2bc05cSGleb Smirnoff } 15520e2bc05cSGleb Smirnoff } 15530e2bc05cSGleb Smirnoff if (sack_rxmit) 15540e2bc05cSGleb Smirnoff goto timer; 15550e2bc05cSGleb Smirnoff tp->snd_nxt += len; 15560e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 155708af8aacSRandall Stewart /* 155808af8aacSRandall Stewart * Update "made progress" indication if we just 155908af8aacSRandall Stewart * added new data to an empty socket buffer. 156008af8aacSRandall Stewart */ 156108af8aacSRandall Stewart if (tp->snd_una == tp->snd_max) 156208af8aacSRandall Stewart tp->t_acktime = ticks; 15630e2bc05cSGleb Smirnoff tp->snd_max = tp->snd_nxt; 15640e2bc05cSGleb Smirnoff /* 15650e2bc05cSGleb Smirnoff * Time this transmission if not a retransmission and 15660e2bc05cSGleb Smirnoff * not currently timing anything. 15670e2bc05cSGleb Smirnoff */ 15689dc7d8a2SRichard Scheffenegger tp->t_sndtime = ticks; 15690e2bc05cSGleb Smirnoff if (tp->t_rtttime == 0) { 15700e2bc05cSGleb Smirnoff tp->t_rtttime = ticks; 15710e2bc05cSGleb Smirnoff tp->t_rtseq = startseq; 15720e2bc05cSGleb Smirnoff TCPSTAT_INC(tcps_segstimed); 15730e2bc05cSGleb Smirnoff } 1574adc56f5aSEdward Tomasz Napierala #ifdef STATS 1575adc56f5aSEdward Tomasz Napierala if (!(tp->t_flags & TF_GPUTINPROG) && len) { 1576adc56f5aSEdward Tomasz Napierala tp->t_flags |= TF_GPUTINPROG; 1577adc56f5aSEdward Tomasz Napierala tp->gput_seq = startseq; 1578adc56f5aSEdward Tomasz Napierala tp->gput_ack = startseq + 1579adc56f5aSEdward Tomasz Napierala ulmin(sbavail(&so->so_snd) - off, sendwin); 1580adc56f5aSEdward Tomasz Napierala tp->gput_ts = tcp_ts_getticks(); 1581adc56f5aSEdward Tomasz Napierala } 1582adc56f5aSEdward Tomasz Napierala #endif /* STATS */ 15830e2bc05cSGleb Smirnoff } 15840e2bc05cSGleb Smirnoff 15850e2bc05cSGleb Smirnoff /* 15860e2bc05cSGleb Smirnoff * Set retransmit timer if not currently set, 15870e2bc05cSGleb Smirnoff * and not doing a pure ack or a keep-alive probe. 15880e2bc05cSGleb Smirnoff * Initial value for retransmit timer is smoothed 15890e2bc05cSGleb Smirnoff * round-trip time + 2 * round-trip time variance. 15900e2bc05cSGleb Smirnoff * Initialize shift counter which is used for backoff 15910e2bc05cSGleb Smirnoff * of retransmit time. 15920e2bc05cSGleb Smirnoff */ 15930e2bc05cSGleb Smirnoff timer: 15940e2bc05cSGleb Smirnoff if (!tcp_timer_active(tp, TT_REXMT) && 15950e2bc05cSGleb Smirnoff ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 15960e2bc05cSGleb Smirnoff (tp->snd_nxt != tp->snd_una))) { 15970e2bc05cSGleb Smirnoff if (tcp_timer_active(tp, TT_PERSIST)) { 15980e2bc05cSGleb Smirnoff tcp_timer_activate(tp, TT_PERSIST, 0); 15990e2bc05cSGleb Smirnoff tp->t_rxtshift = 0; 16000e2bc05cSGleb Smirnoff } 160108af8aacSRandall Stewart tcp_timer_activate(tp, TT_REXMT, TP_RXTCUR(tp)); 1602f8568079SHiren Panchasara } else if (len == 0 && sbavail(&so->so_snd) && 1603f8568079SHiren Panchasara !tcp_timer_active(tp, TT_REXMT) && 1604f8568079SHiren Panchasara !tcp_timer_active(tp, TT_PERSIST)) { 1605f8568079SHiren Panchasara /* 1606f8568079SHiren Panchasara * Avoid a situation where we do not set persist timer 1607f8568079SHiren Panchasara * after a zero window condition. For example: 1608f8568079SHiren Panchasara * 1) A -> B: packet with enough data to fill the window 1609f8568079SHiren Panchasara * 2) B -> A: ACK for #1 + new data (0 window 1610f8568079SHiren Panchasara * advertisement) 1611f8568079SHiren Panchasara * 3) A -> B: ACK for #2, 0 len packet 1612f8568079SHiren Panchasara * 1613f8568079SHiren Panchasara * In this case, A will not activate the persist timer, 1614f8568079SHiren Panchasara * because it chose to send a packet. Unless tcp_output 1615f8568079SHiren Panchasara * is called for some other reason (delayed ack timer, 1616f8568079SHiren Panchasara * another input packet from B, socket syscall), A will 1617f8568079SHiren Panchasara * not send zero window probes. 1618f8568079SHiren Panchasara * 1619f8568079SHiren Panchasara * So, if you send a 0-length packet, but there is data 1620f8568079SHiren Panchasara * in the socket buffer, and neither the rexmt or 1621f8568079SHiren Panchasara * persist timer is already set, then activate the 1622f8568079SHiren Panchasara * persist timer. 1623f8568079SHiren Panchasara */ 1624f8568079SHiren Panchasara tp->t_rxtshift = 0; 1625f8568079SHiren Panchasara tcp_setpersist(tp); 16260e2bc05cSGleb Smirnoff } 16270e2bc05cSGleb Smirnoff } else { 16280e2bc05cSGleb Smirnoff /* 16290e2bc05cSGleb Smirnoff * Persist case, update snd_max but since we are in 16300e2bc05cSGleb Smirnoff * persist mode (no window) we do not update snd_nxt. 16310e2bc05cSGleb Smirnoff */ 16320e2bc05cSGleb Smirnoff int xlen = len; 16330e2bc05cSGleb Smirnoff if (flags & TH_SYN) 16340e2bc05cSGleb Smirnoff ++xlen; 16350e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 16360e2bc05cSGleb Smirnoff ++xlen; 16370e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 16380e2bc05cSGleb Smirnoff } 16390e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 164015c82571SJonathan T. Looney tp->snd_max = tp->snd_nxt + xlen; 16410e2bc05cSGleb Smirnoff } 1642e5926fd3SRandall Stewart if ((error == 0) && 1643440f4ba1SRichard Scheffenegger (tp->rcv_numsacks > 0) && 1644440f4ba1SRichard Scheffenegger TCPS_HAVEESTABLISHED(tp->t_state) && 1645440f4ba1SRichard Scheffenegger (tp->t_flags & TF_SACK_PERMIT)) { 1646e5926fd3SRandall Stewart /* Clean up any DSACK's sent */ 1647e5926fd3SRandall Stewart tcp_clean_dsack_blocks(tp); 1648e5926fd3SRandall Stewart } 1649440f4ba1SRichard Scheffenegger if ((error == 0) && 1650440f4ba1SRichard Scheffenegger sack_rxmit && 1651440f4ba1SRichard Scheffenegger SEQ_LT(tp->snd_nxt, SEQ_MIN(p->rxmit, p->end))) { 16527dc78150SRichard Scheffenegger /* 16537dc78150SRichard Scheffenegger * When transmitting from SACK scoreboard 16547dc78150SRichard Scheffenegger * after an RTO, pull snd_nxt along. 16557dc78150SRichard Scheffenegger */ 1656440f4ba1SRichard Scheffenegger tp->snd_nxt = SEQ_MIN(p->rxmit, p->end); 1657440f4ba1SRichard Scheffenegger } 1658df8bae1dSRodney W. Grimes if (error) { 16597734ea06SArchie Cobbs /* 16607734ea06SArchie Cobbs * We know that the packet was lost, so back out the 16617734ea06SArchie Cobbs * sequence number advance, if any. 16622c30ec0aSAndre Oppermann * 16632c30ec0aSAndre Oppermann * If the error is EPERM the packet got blocked by the 16642c30ec0aSAndre Oppermann * local firewall. Normally we should terminate the 16652c30ec0aSAndre Oppermann * connection but the blocking may have been spurious 16662c30ec0aSAndre Oppermann * due to a firewall reconfiguration cycle. So we treat 16672c30ec0aSAndre Oppermann * it like a packet loss and let the retransmit timer and 16682c30ec0aSAndre Oppermann * timeouts do their work over time. 16692c30ec0aSAndre Oppermann * XXX: It is a POLA question whether calling tcp_drop right 16702c30ec0aSAndre Oppermann * away would be the really correct behavior instead. 16717734ea06SArchie Cobbs */ 167272757d9aSGleb Smirnoff if (((tp->t_flags & TF_FORCEDATA) == 0 || 1673b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) && 167472757d9aSGleb Smirnoff ((flags & TH_SYN) == 0) && 167572757d9aSGleb Smirnoff (error != EPERM)) { 16760077b016SPaul Saab if (sack_rxmit) { 1677e3b9058eSRichard Scheffenegger p->rxmit = SEQ_MIN(p->end, p->rxmit) - len; 16780077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= len; 167972757d9aSGleb Smirnoff KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 16800077b016SPaul Saab ("sackhint bytes rtx >= 0")); 168166605ff7SRichard Scheffenegger KASSERT((flags & TH_FIN) == 0, 168266605ff7SRichard Scheffenegger ("error while FIN with SACK rxmit")); 168366605ff7SRichard Scheffenegger } else { 16847734ea06SArchie Cobbs tp->snd_nxt -= len; 168566605ff7SRichard Scheffenegger if (flags & TH_FIN) 168666605ff7SRichard Scheffenegger tp->snd_nxt--; 168766605ff7SRichard Scheffenegger } 1688429f14f8SRichard Scheffenegger if (IN_RECOVERY(tp->t_flags)) 1689429f14f8SRichard Scheffenegger tp->sackhint.prr_out -= len; 16907734ea06SArchie Cobbs } 1691dded4e9eSRichard Scheffenegger SOCK_SENDBUF_UNLOCK_ASSERT(so); /* Check gotos. */ 169272757d9aSGleb Smirnoff switch (error) { 1693fcf59617SAndrey V. Elsukov case EACCES: 169472757d9aSGleb Smirnoff case EPERM: 169572757d9aSGleb Smirnoff tp->t_softerror = error; 169672757d9aSGleb Smirnoff return (error); 169772757d9aSGleb Smirnoff case ENOBUFS: 1698425b7639SSepherosa Ziehau TCP_XMIT_TIMER_ASSERT(tp, len, flags); 1699*22dcc812SRichard Scheffenegger tp->snd_cwnd = tcp_maxseg(tp); 1700df8bae1dSRodney W. Grimes return (0); 170172757d9aSGleb Smirnoff case EMSGSIZE: 17023d1f141bSGarrett Wollman /* 1703b3c0f300SAndre Oppermann * For some reason the interface we used initially 1704b3c0f300SAndre Oppermann * to send segments changed to another or lowered 1705b3c0f300SAndre Oppermann * its MTU. 1706b3c0f300SAndre Oppermann * If TSO was active we either got an interface 1707b3c0f300SAndre Oppermann * without TSO capabilits or TSO was turned off. 1708df0633a1SGleb Smirnoff * If we obtained mtu from ip_output() then update 1709df0633a1SGleb Smirnoff * it and try again. 17103d1f141bSGarrett Wollman */ 1711b3c0f300SAndre Oppermann if (tso) 1712b3c0f300SAndre Oppermann tp->t_flags &= ~TF_TSO; 1713df0633a1SGleb Smirnoff if (mtu != 0) { 1714df0633a1SGleb Smirnoff tcp_mss_update(tp, -1, mtu, NULL, NULL); 1715df0633a1SGleb Smirnoff goto again; 1716df0633a1SGleb Smirnoff } 1717df0633a1SGleb Smirnoff return (error); 17188bec3467SGleb Smirnoff case EHOSTDOWN: 171972757d9aSGleb Smirnoff case EHOSTUNREACH: 172072757d9aSGleb Smirnoff case ENETDOWN: 17218bec3467SGleb Smirnoff case ENETUNREACH: 172272757d9aSGleb Smirnoff if (TCPS_HAVERCVDSYN(tp->t_state)) { 1723df8bae1dSRodney W. Grimes tp->t_softerror = error; 1724df8bae1dSRodney W. Grimes return (0); 1725df8bae1dSRodney W. Grimes } 172672757d9aSGleb Smirnoff /* FALLTHROUGH */ 172772757d9aSGleb Smirnoff default: 1728df8bae1dSRodney W. Grimes return (error); 1729df8bae1dSRodney W. Grimes } 173072757d9aSGleb Smirnoff } 173178b50714SRobert Watson TCPSTAT_INC(tcps_sndtotal); 1732df8bae1dSRodney W. Grimes 1733df8bae1dSRodney W. Grimes /* 1734df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 1735df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 1736df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 1737df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 1738df8bae1dSRodney W. Grimes */ 17393ac12506SJonathan T. Looney if (SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1740201d185bSAndre Oppermann tp->rcv_adv = tp->rcv_nxt + recwin; 1741df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 17423bfd6421SJonathan Lemon tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 1743b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_DELACK)) 1744b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 0); 1745d912c694SMatthew Dillon if (sendalot) 1746d912c694SMatthew Dillon goto again; 1747df8bae1dSRodney W. Grimes return (0); 1748df8bae1dSRodney W. Grimes } 1749df8bae1dSRodney W. Grimes 1750df8bae1dSRodney W. Grimes void 1751ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp) 1752df8bae1dSRodney W. Grimes { 17539b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 17549b8b58e0SJonathan Lemon int tt; 175508af8aacSRandall Stewart int maxunacktime; 1756df8bae1dSRodney W. Grimes 1757672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 1758b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_REXMT)) 17599b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 1760df8bae1dSRodney W. Grimes /* 176108af8aacSRandall Stewart * If the state is already closed, don't bother. 176208af8aacSRandall Stewart */ 176308af8aacSRandall Stewart if (tp->t_state == TCPS_CLOSED) 176408af8aacSRandall Stewart return; 176508af8aacSRandall Stewart 176608af8aacSRandall Stewart /* 1767a4641f4eSPedro F. Giffuni * Start/restart persistence timer. 1768df8bae1dSRodney W. Grimes */ 17699b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 17700645c604SHiren Panchasara tcp_persmin, tcp_persmax); 177108af8aacSRandall Stewart if (TP_MAXUNACKTIME(tp) && tp->t_acktime) { 177208af8aacSRandall Stewart maxunacktime = tp->t_acktime + TP_MAXUNACKTIME(tp) - ticks; 177308af8aacSRandall Stewart if (maxunacktime < 1) 177408af8aacSRandall Stewart maxunacktime = 1; 177508af8aacSRandall Stewart if (maxunacktime < tt) 177608af8aacSRandall Stewart tt = maxunacktime; 177708af8aacSRandall Stewart } 1778b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, tt); 177943b117f8SRichard Scheffenegger if (tp->t_rxtshift < V_tcp_retries) 1780df8bae1dSRodney W. Grimes tp->t_rxtshift++; 1781df8bae1dSRodney W. Grimes } 178202a1a643SAndre Oppermann 178302a1a643SAndre Oppermann /* 178402a1a643SAndre Oppermann * Insert TCP options according to the supplied parameters to the place 178502a1a643SAndre Oppermann * optp in a consistent way. Can handle unaligned destinations. 178602a1a643SAndre Oppermann * 178702a1a643SAndre Oppermann * The order of the option processing is crucial for optimal packing and 178802a1a643SAndre Oppermann * alignment for the scarce option space. 178902a1a643SAndre Oppermann * 179002a1a643SAndre Oppermann * The optimal order for a SYN/SYN-ACK segment is: 179102a1a643SAndre Oppermann * MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) + 179202a1a643SAndre Oppermann * Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40. 179302a1a643SAndre Oppermann * 179402a1a643SAndre Oppermann * The SACK options should be last. SACK blocks consume 8*n+2 bytes. 179502a1a643SAndre Oppermann * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks). 179602a1a643SAndre Oppermann * At minimum we need 10 bytes (to generate 1 SACK block). If both 179702a1a643SAndre Oppermann * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present, 179802a1a643SAndre Oppermann * we only have 10 bytes for SACK options (40 - (12 + 18)). 179902a1a643SAndre Oppermann */ 180002a1a643SAndre Oppermann int 180102a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp) 180202a1a643SAndre Oppermann { 18035d20f974SJonathan T. Looney u_int32_t mask, optlen = 0; 180402a1a643SAndre Oppermann 180502a1a643SAndre Oppermann for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) { 180602a1a643SAndre Oppermann if ((to->to_flags & mask) != mask) 180702a1a643SAndre Oppermann continue; 18083a4018c4SAndre Oppermann if (optlen == TCP_MAXOLEN) 18093a4018c4SAndre Oppermann break; 181002a1a643SAndre Oppermann switch (to->to_flags & mask) { 181102a1a643SAndre Oppermann case TOF_MSS: 181202a1a643SAndre Oppermann while (optlen % 4) { 181302a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 181402a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 181502a1a643SAndre Oppermann } 18163a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG) 18173a4018c4SAndre Oppermann continue; 181802a1a643SAndre Oppermann optlen += TCPOLEN_MAXSEG; 181902a1a643SAndre Oppermann *optp++ = TCPOPT_MAXSEG; 182002a1a643SAndre Oppermann *optp++ = TCPOLEN_MAXSEG; 182102a1a643SAndre Oppermann to->to_mss = htons(to->to_mss); 182202a1a643SAndre Oppermann bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss)); 182302a1a643SAndre Oppermann optp += sizeof(to->to_mss); 182402a1a643SAndre Oppermann break; 182502a1a643SAndre Oppermann case TOF_SCALE: 182602a1a643SAndre Oppermann while (!optlen || optlen % 2 != 1) { 182702a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 182802a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 182902a1a643SAndre Oppermann } 18303a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW) 18313a4018c4SAndre Oppermann continue; 183202a1a643SAndre Oppermann optlen += TCPOLEN_WINDOW; 183302a1a643SAndre Oppermann *optp++ = TCPOPT_WINDOW; 183402a1a643SAndre Oppermann *optp++ = TCPOLEN_WINDOW; 183502a1a643SAndre Oppermann *optp++ = to->to_wscale; 183602a1a643SAndre Oppermann break; 183702a1a643SAndre Oppermann case TOF_SACKPERM: 183802a1a643SAndre Oppermann while (optlen % 2) { 183902a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 184002a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 184102a1a643SAndre Oppermann } 18423a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED) 18433a4018c4SAndre Oppermann continue; 184402a1a643SAndre Oppermann optlen += TCPOLEN_SACK_PERMITTED; 184502a1a643SAndre Oppermann *optp++ = TCPOPT_SACK_PERMITTED; 184602a1a643SAndre Oppermann *optp++ = TCPOLEN_SACK_PERMITTED; 184702a1a643SAndre Oppermann break; 184802a1a643SAndre Oppermann case TOF_TS: 184902a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 185002a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 185102a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 185202a1a643SAndre Oppermann } 18533a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP) 18543a4018c4SAndre Oppermann continue; 185502a1a643SAndre Oppermann optlen += TCPOLEN_TIMESTAMP; 185602a1a643SAndre Oppermann *optp++ = TCPOPT_TIMESTAMP; 185702a1a643SAndre Oppermann *optp++ = TCPOLEN_TIMESTAMP; 185802a1a643SAndre Oppermann to->to_tsval = htonl(to->to_tsval); 185902a1a643SAndre Oppermann to->to_tsecr = htonl(to->to_tsecr); 186002a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval)); 186102a1a643SAndre Oppermann optp += sizeof(to->to_tsval); 186202a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr)); 186302a1a643SAndre Oppermann optp += sizeof(to->to_tsecr); 186402a1a643SAndre Oppermann break; 186502a1a643SAndre Oppermann case TOF_SIGNATURE: 186602a1a643SAndre Oppermann { 186702a1a643SAndre Oppermann int siglen = TCPOLEN_SIGNATURE - 2; 186802a1a643SAndre Oppermann 186902a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 187002a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 187102a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 187202a1a643SAndre Oppermann } 1873fcf59617SAndrey V. Elsukov if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE) { 1874fcf59617SAndrey V. Elsukov to->to_flags &= ~TOF_SIGNATURE; 187502a1a643SAndre Oppermann continue; 1876fcf59617SAndrey V. Elsukov } 187702a1a643SAndre Oppermann optlen += TCPOLEN_SIGNATURE; 187802a1a643SAndre Oppermann *optp++ = TCPOPT_SIGNATURE; 187902a1a643SAndre Oppermann *optp++ = TCPOLEN_SIGNATURE; 188002a1a643SAndre Oppermann to->to_signature = optp; 188102a1a643SAndre Oppermann while (siglen--) 188202a1a643SAndre Oppermann *optp++ = 0; 188302a1a643SAndre Oppermann break; 188402a1a643SAndre Oppermann } 188502a1a643SAndre Oppermann case TOF_SACK: 188602a1a643SAndre Oppermann { 188702a1a643SAndre Oppermann int sackblks = 0; 188802a1a643SAndre Oppermann struct sackblk *sack = (struct sackblk *)to->to_sacks; 188902a1a643SAndre Oppermann tcp_seq sack_seq; 189002a1a643SAndre Oppermann 189102a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 189202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 189302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 189402a1a643SAndre Oppermann } 18953a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK) 189602a1a643SAndre Oppermann continue; 189702a1a643SAndre Oppermann optlen += TCPOLEN_SACKHDR; 189802a1a643SAndre Oppermann *optp++ = TCPOPT_SACK; 189902a1a643SAndre Oppermann sackblks = min(to->to_nsacks, 19000d957bbaSAndre Oppermann (TCP_MAXOLEN - optlen) / TCPOLEN_SACK); 190102a1a643SAndre Oppermann *optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK; 190202a1a643SAndre Oppermann while (sackblks--) { 190302a1a643SAndre Oppermann sack_seq = htonl(sack->start); 190402a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 190502a1a643SAndre Oppermann optp += sizeof(sack_seq); 190602a1a643SAndre Oppermann sack_seq = htonl(sack->end); 190702a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 190802a1a643SAndre Oppermann optp += sizeof(sack_seq); 190902a1a643SAndre Oppermann optlen += TCPOLEN_SACK; 191002a1a643SAndre Oppermann sack++; 191102a1a643SAndre Oppermann } 191278b50714SRobert Watson TCPSTAT_INC(tcps_sack_send_blocks); 191302a1a643SAndre Oppermann break; 191402a1a643SAndre Oppermann } 1915281a0fd4SPatrick Kelsey case TOF_FASTOPEN: 1916281a0fd4SPatrick Kelsey { 1917281a0fd4SPatrick Kelsey int total_len; 1918281a0fd4SPatrick Kelsey 1919281a0fd4SPatrick Kelsey /* XXX is there any point to aligning this option? */ 1920281a0fd4SPatrick Kelsey total_len = TCPOLEN_FAST_OPEN_EMPTY + to->to_tfo_len; 1921c560df6fSPatrick Kelsey if (TCP_MAXOLEN - optlen < total_len) { 1922c560df6fSPatrick Kelsey to->to_flags &= ~TOF_FASTOPEN; 1923281a0fd4SPatrick Kelsey continue; 1924c560df6fSPatrick Kelsey } 1925281a0fd4SPatrick Kelsey *optp++ = TCPOPT_FAST_OPEN; 1926281a0fd4SPatrick Kelsey *optp++ = total_len; 1927281a0fd4SPatrick Kelsey if (to->to_tfo_len > 0) { 1928281a0fd4SPatrick Kelsey bcopy(to->to_tfo_cookie, optp, to->to_tfo_len); 1929281a0fd4SPatrick Kelsey optp += to->to_tfo_len; 1930281a0fd4SPatrick Kelsey } 1931281a0fd4SPatrick Kelsey optlen += total_len; 1932281a0fd4SPatrick Kelsey break; 1933281a0fd4SPatrick Kelsey } 193402a1a643SAndre Oppermann default: 193502a1a643SAndre Oppermann panic("%s: unknown TCP option type", __func__); 193602a1a643SAndre Oppermann break; 193702a1a643SAndre Oppermann } 193802a1a643SAndre Oppermann } 193902a1a643SAndre Oppermann 194002a1a643SAndre Oppermann /* Terminate and pad TCP options to a 4 byte boundary. */ 194102a1a643SAndre Oppermann if (optlen % 4) { 194202a1a643SAndre Oppermann optlen += TCPOLEN_EOL; 194302a1a643SAndre Oppermann *optp++ = TCPOPT_EOL; 194402a1a643SAndre Oppermann } 1945413deb12SBjoern A. Zeeb /* 1946413deb12SBjoern A. Zeeb * According to RFC 793 (STD0007): 1947413deb12SBjoern A. Zeeb * "The content of the header beyond the End-of-Option option 1948413deb12SBjoern A. Zeeb * must be header padding (i.e., zero)." 1949413deb12SBjoern A. Zeeb * and later: "The padding is composed of zeros." 1950413deb12SBjoern A. Zeeb */ 195102a1a643SAndre Oppermann while (optlen % 4) { 1952c343c524SAndre Oppermann optlen += TCPOLEN_PAD; 1953c343c524SAndre Oppermann *optp++ = TCPOPT_PAD; 195402a1a643SAndre Oppermann } 195502a1a643SAndre Oppermann 19560d957bbaSAndre Oppermann KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__)); 195702a1a643SAndre Oppermann return (optlen); 195802a1a643SAndre Oppermann } 195966492feaSGleb Smirnoff 196089e560f4SRandall Stewart /* 196189e560f4SRandall Stewart * This is a copy of m_copym(), taking the TSO segment size/limit 196289e560f4SRandall Stewart * constraints into account, and advancing the sndptr as it goes. 196389e560f4SRandall Stewart */ 196489e560f4SRandall Stewart struct mbuf * 196589e560f4SRandall Stewart tcp_m_copym(struct mbuf *m, int32_t off0, int32_t *plen, 1966b2e60773SJohn Baldwin int32_t seglimit, int32_t segsize, struct sockbuf *sb, bool hw_tls) 196789e560f4SRandall Stewart { 1968b2e60773SJohn Baldwin #ifdef KERN_TLS 1969b2e60773SJohn Baldwin struct ktls_session *tls, *ntls; 1970732b6d4dSJohn Baldwin struct mbuf *start __diagused; 1971b2e60773SJohn Baldwin #endif 197289e560f4SRandall Stewart struct mbuf *n, **np; 197389e560f4SRandall Stewart struct mbuf *top; 197489e560f4SRandall Stewart int32_t off = off0; 197589e560f4SRandall Stewart int32_t len = *plen; 197689e560f4SRandall Stewart int32_t fragsize; 197789e560f4SRandall Stewart int32_t len_cp = 0; 197889e560f4SRandall Stewart int32_t *pkthdrlen; 197989e560f4SRandall Stewart uint32_t mlen, frags; 198089e560f4SRandall Stewart bool copyhdr; 198189e560f4SRandall Stewart 198289e560f4SRandall Stewart KASSERT(off >= 0, ("tcp_m_copym, negative off %d", off)); 198389e560f4SRandall Stewart KASSERT(len >= 0, ("tcp_m_copym, negative len %d", len)); 198489e560f4SRandall Stewart if (off == 0 && m->m_flags & M_PKTHDR) 198589e560f4SRandall Stewart copyhdr = true; 198689e560f4SRandall Stewart else 198789e560f4SRandall Stewart copyhdr = false; 198889e560f4SRandall Stewart while (off > 0) { 198989e560f4SRandall Stewart KASSERT(m != NULL, ("tcp_m_copym, offset > size of mbuf chain")); 199089e560f4SRandall Stewart if (off < m->m_len) 199189e560f4SRandall Stewart break; 199289e560f4SRandall Stewart off -= m->m_len; 199389e560f4SRandall Stewart if ((sb) && (m == sb->sb_sndptr)) { 199489e560f4SRandall Stewart sb->sb_sndptroff += m->m_len; 199589e560f4SRandall Stewart sb->sb_sndptr = m->m_next; 199689e560f4SRandall Stewart } 199789e560f4SRandall Stewart m = m->m_next; 199889e560f4SRandall Stewart } 199989e560f4SRandall Stewart np = ⊤ 200089e560f4SRandall Stewart top = NULL; 200189e560f4SRandall Stewart pkthdrlen = NULL; 2002b2e60773SJohn Baldwin #ifdef KERN_TLS 20036edfd179SGleb Smirnoff if (hw_tls && (m->m_flags & M_EXTPG)) 20047b6c99d0SGleb Smirnoff tls = m->m_epg_tls; 2005b2e60773SJohn Baldwin else 2006b2e60773SJohn Baldwin tls = NULL; 2007b2e60773SJohn Baldwin start = m; 2008b2e60773SJohn Baldwin #endif 200989e560f4SRandall Stewart while (len > 0) { 201089e560f4SRandall Stewart if (m == NULL) { 201189e560f4SRandall Stewart KASSERT(len == M_COPYALL, 201289e560f4SRandall Stewart ("tcp_m_copym, length > size of mbuf chain")); 201389e560f4SRandall Stewart *plen = len_cp; 201489e560f4SRandall Stewart if (pkthdrlen != NULL) 201589e560f4SRandall Stewart *pkthdrlen = len_cp; 201689e560f4SRandall Stewart break; 201789e560f4SRandall Stewart } 2018b2e60773SJohn Baldwin #ifdef KERN_TLS 2019b2e60773SJohn Baldwin if (hw_tls) { 20206edfd179SGleb Smirnoff if (m->m_flags & M_EXTPG) 20217b6c99d0SGleb Smirnoff ntls = m->m_epg_tls; 2022b2e60773SJohn Baldwin else 2023b2e60773SJohn Baldwin ntls = NULL; 2024b2e60773SJohn Baldwin 2025b2e60773SJohn Baldwin /* 2026b2e60773SJohn Baldwin * Avoid mixing TLS records with handshake 2027b2e60773SJohn Baldwin * data or TLS records from different 2028b2e60773SJohn Baldwin * sessions. 2029b2e60773SJohn Baldwin */ 2030b2e60773SJohn Baldwin if (tls != ntls) { 2031b2e60773SJohn Baldwin MPASS(m != start); 2032b2e60773SJohn Baldwin *plen = len_cp; 2033b2e60773SJohn Baldwin if (pkthdrlen != NULL) 2034b2e60773SJohn Baldwin *pkthdrlen = len_cp; 2035b2e60773SJohn Baldwin break; 2036b2e60773SJohn Baldwin } 2037b2e60773SJohn Baldwin } 2038b2e60773SJohn Baldwin #endif 203989e560f4SRandall Stewart mlen = min(len, m->m_len - off); 204089e560f4SRandall Stewart if (seglimit) { 204189e560f4SRandall Stewart /* 20426edfd179SGleb Smirnoff * For M_EXTPG mbufs, add 3 segments 204389e560f4SRandall Stewart * + 1 in case we are crossing page boundaries 204489e560f4SRandall Stewart * + 2 in case the TLS hdr/trailer are used 204589e560f4SRandall Stewart * It is cheaper to just add the segments 204689e560f4SRandall Stewart * than it is to take the cache miss to look 204789e560f4SRandall Stewart * at the mbuf ext_pgs state in detail. 204889e560f4SRandall Stewart */ 20496edfd179SGleb Smirnoff if (m->m_flags & M_EXTPG) { 205089e560f4SRandall Stewart fragsize = min(segsize, PAGE_SIZE); 205189e560f4SRandall Stewart frags = 3; 205289e560f4SRandall Stewart } else { 205389e560f4SRandall Stewart fragsize = segsize; 205489e560f4SRandall Stewart frags = 0; 205589e560f4SRandall Stewart } 205689e560f4SRandall Stewart 205789e560f4SRandall Stewart /* Break if we really can't fit anymore. */ 205889e560f4SRandall Stewart if ((frags + 1) >= seglimit) { 205989e560f4SRandall Stewart *plen = len_cp; 206089e560f4SRandall Stewart if (pkthdrlen != NULL) 206189e560f4SRandall Stewart *pkthdrlen = len_cp; 206289e560f4SRandall Stewart break; 206389e560f4SRandall Stewart } 206489e560f4SRandall Stewart 206589e560f4SRandall Stewart /* 206689e560f4SRandall Stewart * Reduce size if you can't copy the whole 206789e560f4SRandall Stewart * mbuf. If we can't copy the whole mbuf, also 206889e560f4SRandall Stewart * adjust len so the loop will end after this 206989e560f4SRandall Stewart * mbuf. 207089e560f4SRandall Stewart */ 207189e560f4SRandall Stewart if ((frags + howmany(mlen, fragsize)) >= seglimit) { 207289e560f4SRandall Stewart mlen = (seglimit - frags - 1) * fragsize; 207389e560f4SRandall Stewart len = mlen; 207489e560f4SRandall Stewart *plen = len_cp + len; 207589e560f4SRandall Stewart if (pkthdrlen != NULL) 207689e560f4SRandall Stewart *pkthdrlen = *plen; 207789e560f4SRandall Stewart } 207889e560f4SRandall Stewart frags += howmany(mlen, fragsize); 207989e560f4SRandall Stewart if (frags == 0) 208089e560f4SRandall Stewart frags++; 208189e560f4SRandall Stewart seglimit -= frags; 208289e560f4SRandall Stewart KASSERT(seglimit > 0, 208389e560f4SRandall Stewart ("%s: seglimit went too low", __func__)); 208489e560f4SRandall Stewart } 208589e560f4SRandall Stewart if (copyhdr) 208689e560f4SRandall Stewart n = m_gethdr(M_NOWAIT, m->m_type); 208789e560f4SRandall Stewart else 208889e560f4SRandall Stewart n = m_get(M_NOWAIT, m->m_type); 208989e560f4SRandall Stewart *np = n; 209089e560f4SRandall Stewart if (n == NULL) 209189e560f4SRandall Stewart goto nospace; 209289e560f4SRandall Stewart if (copyhdr) { 209389e560f4SRandall Stewart if (!m_dup_pkthdr(n, m, M_NOWAIT)) 209489e560f4SRandall Stewart goto nospace; 209589e560f4SRandall Stewart if (len == M_COPYALL) 209689e560f4SRandall Stewart n->m_pkthdr.len -= off0; 209789e560f4SRandall Stewart else 209889e560f4SRandall Stewart n->m_pkthdr.len = len; 209989e560f4SRandall Stewart pkthdrlen = &n->m_pkthdr.len; 210089e560f4SRandall Stewart copyhdr = false; 210189e560f4SRandall Stewart } 210289e560f4SRandall Stewart n->m_len = mlen; 210389e560f4SRandall Stewart len_cp += n->m_len; 210461664ee7SGleb Smirnoff if (m->m_flags & (M_EXT | M_EXTPG)) { 210589e560f4SRandall Stewart n->m_data = m->m_data + off; 210689e560f4SRandall Stewart mb_dupcl(n, m); 210789e560f4SRandall Stewart } else 210889e560f4SRandall Stewart bcopy(mtod(m, caddr_t)+off, mtod(n, caddr_t), 210989e560f4SRandall Stewart (u_int)n->m_len); 211089e560f4SRandall Stewart 211189e560f4SRandall Stewart if (sb && (sb->sb_sndptr == m) && 211289e560f4SRandall Stewart ((n->m_len + off) >= m->m_len) && m->m_next) { 211389e560f4SRandall Stewart sb->sb_sndptroff += m->m_len; 211489e560f4SRandall Stewart sb->sb_sndptr = m->m_next; 211589e560f4SRandall Stewart } 211689e560f4SRandall Stewart off = 0; 211789e560f4SRandall Stewart if (len != M_COPYALL) { 211889e560f4SRandall Stewart len -= n->m_len; 211989e560f4SRandall Stewart } 212089e560f4SRandall Stewart m = m->m_next; 212189e560f4SRandall Stewart np = &n->m_next; 212289e560f4SRandall Stewart } 212389e560f4SRandall Stewart return (top); 212489e560f4SRandall Stewart nospace: 212589e560f4SRandall Stewart m_freem(top); 212689e560f4SRandall Stewart return (NULL); 212789e560f4SRandall Stewart } 212889e560f4SRandall Stewart 212966492feaSGleb Smirnoff void 213066492feaSGleb Smirnoff tcp_sndbuf_autoscale(struct tcpcb *tp, struct socket *so, uint32_t sendwin) 213166492feaSGleb Smirnoff { 213266492feaSGleb Smirnoff 213366492feaSGleb Smirnoff /* 213466492feaSGleb Smirnoff * Automatic sizing of send socket buffer. Often the send buffer 213566492feaSGleb Smirnoff * size is not optimally adjusted to the actual network conditions 213666492feaSGleb Smirnoff * at hand (delay bandwidth product). Setting the buffer size too 213766492feaSGleb Smirnoff * small limits throughput on links with high bandwidth and high 213866492feaSGleb Smirnoff * delay (eg. trans-continental/oceanic links). Setting the 213966492feaSGleb Smirnoff * buffer size too big consumes too much real kernel memory, 214066492feaSGleb Smirnoff * especially with many connections on busy servers. 214166492feaSGleb Smirnoff * 214266492feaSGleb Smirnoff * The criteria to step up the send buffer one notch are: 214366492feaSGleb Smirnoff * 1. receive window of remote host is larger than send buffer 214466492feaSGleb Smirnoff * (with a fudge factor of 5/4th); 214566492feaSGleb Smirnoff * 2. send buffer is filled to 7/8th with data (so we actually 214666492feaSGleb Smirnoff * have data to make use of it); 214766492feaSGleb Smirnoff * 3. send buffer fill has not hit maximal automatic size; 214866492feaSGleb Smirnoff * 4. our send window (slow start and cogestion controlled) is 214966492feaSGleb Smirnoff * larger than sent but unacknowledged data in send buffer. 215066492feaSGleb Smirnoff * 215166492feaSGleb Smirnoff * The remote host receive window scaling factor may limit the 215266492feaSGleb Smirnoff * growing of the send buffer before it reaches its allowed 215366492feaSGleb Smirnoff * maximum. 215466492feaSGleb Smirnoff * 215566492feaSGleb Smirnoff * It scales directly with slow start or congestion window 215666492feaSGleb Smirnoff * and does at most one step per received ACK. This fast 215766492feaSGleb Smirnoff * scaling has the drawback of growing the send buffer beyond 215866492feaSGleb Smirnoff * what is strictly necessary to make full use of a given 215966492feaSGleb Smirnoff * delay*bandwidth product. However testing has shown this not 216066492feaSGleb Smirnoff * to be much of an problem. At worst we are trading wasting 216166492feaSGleb Smirnoff * of available bandwidth (the non-use of it) for wasting some 216266492feaSGleb Smirnoff * socket buffer memory. 216366492feaSGleb Smirnoff * 216466492feaSGleb Smirnoff * TODO: Shrink send buffer during idle periods together 216566492feaSGleb Smirnoff * with congestion window. Requires another timer. Has to 216666492feaSGleb Smirnoff * wait for upcoming tcp timer rewrite. 216766492feaSGleb Smirnoff * 216866492feaSGleb Smirnoff * XXXGL: should there be used sbused() or sbavail()? 216966492feaSGleb Smirnoff */ 217066492feaSGleb Smirnoff if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 217166492feaSGleb Smirnoff int lowat; 217266492feaSGleb Smirnoff 217366492feaSGleb Smirnoff lowat = V_tcp_sendbuf_auto_lowat ? so->so_snd.sb_lowat : 0; 217466492feaSGleb Smirnoff if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat - lowat && 217566492feaSGleb Smirnoff sbused(&so->so_snd) >= 217666492feaSGleb Smirnoff (so->so_snd.sb_hiwat / 8 * 7) - lowat && 217766492feaSGleb Smirnoff sbused(&so->so_snd) < V_tcp_autosndbuf_max && 217866492feaSGleb Smirnoff sendwin >= (sbused(&so->so_snd) - 217966492feaSGleb Smirnoff (tp->snd_nxt - tp->snd_una))) { 218043283184SGleb Smirnoff if (!sbreserve_locked(so, SO_SND, 218166492feaSGleb Smirnoff min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc, 218243283184SGleb Smirnoff V_tcp_autosndbuf_max), curthread)) 218366492feaSGleb Smirnoff so->so_snd.sb_flags &= ~SB_AUTOSIZE; 218466492feaSGleb Smirnoff } 218566492feaSGleb Smirnoff } 218666492feaSGleb Smirnoff } 2187