1c398230bSWarner Losh /*- 2d7f570e6SGarrett Wollman * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29d7f570e6SGarrett Wollman * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 324b421e2dSMike Silbersack #include <sys/cdefs.h> 334b421e2dSMike Silbersack __FBSDID("$FreeBSD$"); 344b421e2dSMike Silbersack 351cfd4b53SBruce M Simpson #include "opt_inet.h" 36fb59c426SYoshinobu Inoue #include "opt_inet6.h" 373a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 380cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 390cc12cc5SJoerg Wunsch 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41df8bae1dSRodney W. Grimes #include <sys/systm.h> 42686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h> 43fb919e4dSMark Murray #include <sys/kernel.h> 44fb919e4dSMark Murray #include <sys/lock.h> 45fb919e4dSMark Murray #include <sys/mbuf.h> 46fb919e4dSMark Murray #include <sys/mutex.h> 47df8bae1dSRodney W. Grimes #include <sys/protosw.h> 48df8bae1dSRodney W. Grimes #include <sys/socket.h> 49df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 50fb919e4dSMark Murray #include <sys/sysctl.h> 51df8bae1dSRodney W. Grimes 524b79449eSBjoern A. Zeeb #include <net/if.h> 53df8bae1dSRodney W. Grimes #include <net/route.h> 54530c0060SRobert Watson #include <net/vnet.h> 55df8bae1dSRodney W. Grimes 56dbc42409SLawrence Stewart #include <netinet/cc.h> 57df8bae1dSRodney W. Grimes #include <netinet/in.h> 58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 59df8bae1dSRodney W. Grimes #include <netinet/ip.h> 60df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 61df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 62ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 63fb59c426SYoshinobu Inoue #ifdef INET6 64686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 65686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 66fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 67fb59c426SYoshinobu Inoue #endif 68df8bae1dSRodney W. Grimes #define TCPOUTFLAGS 69df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 70df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 71df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 72df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 73df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 74610ee2f9SDavid Greenman #ifdef TCPDEBUG 75df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 76610ee2f9SDavid Greenman #endif 77df8bae1dSRodney W. Grimes 78b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 79b9234fafSSam Leffler #include <netipsec/ipsec.h> 80b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 81b9234fafSSam Leffler 82db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 83db4f9cc7SJonathan Lemon 84aed55708SRobert Watson #include <security/mac/mac_framework.h> 85aed55708SRobert Watson 86df8bae1dSRodney W. Grimes #ifdef notyet 87df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack(); 88df8bae1dSRodney W. Grimes #endif 89df8bae1dSRodney W. Grimes 9082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, path_mtu_discovery) = 1; 91eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW, 92eddfbb76SRobert Watson &VNET_NAME(path_mtu_discovery), 1, 93eddfbb76SRobert Watson "Enable Path MTU Discovery"); 949a039a5fSDavid Greenman 9582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ss_fltsz) = 1; 96eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW, 97eddfbb76SRobert Watson &VNET_NAME(ss_fltsz), 1, 98eddfbb76SRobert Watson "Slow start flight size"); 999b8b58e0SJonathan Lemon 10082cea7e6SBjoern A. Zeeb VNET_DEFINE(int, ss_fltsz_local) = 4; 101eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, 102eddfbb76SRobert Watson CTLFLAG_RW, &VNET_NAME(ss_fltsz_local), 1, 103eddfbb76SRobert Watson "Slow start flight size for local networks"); 104df8bae1dSRodney W. Grimes 10582cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_tso) = 1; 10682cea7e6SBjoern A. Zeeb #define V_tcp_do_tso VNET(tcp_do_tso) 107eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW, 108eddfbb76SRobert Watson &VNET_NAME(tcp_do_tso), 0, 109eddfbb76SRobert Watson "Enable TCP Segmentation Offload"); 110b3c0f300SAndre Oppermann 11182cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autosndbuf) = 1; 11282cea7e6SBjoern A. Zeeb #define V_tcp_do_autosndbuf VNET(tcp_do_autosndbuf) 113eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_RW, 114eddfbb76SRobert Watson &VNET_NAME(tcp_do_autosndbuf), 0, 115eddfbb76SRobert Watson "Enable automatic send buffer sizing"); 1166741ecf5SAndre Oppermann 11782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_inc) = 8*1024; 11882cea7e6SBjoern A. Zeeb #define V_tcp_autosndbuf_inc VNET(tcp_autosndbuf_inc) 119eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_RW, 120eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_inc), 0, 1218b615593SMarko Zec "Incrementor step size of automatic send buffer"); 1226741ecf5SAndre Oppermann 12382cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_max) = 256*1024; 12482cea7e6SBjoern A. Zeeb #define V_tcp_autosndbuf_max VNET(tcp_autosndbuf_max) 125eddfbb76SRobert Watson SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_RW, 126eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_max), 0, 1278b615593SMarko Zec "Max size of automatic send buffer"); 1286741ecf5SAndre Oppermann 129dbc42409SLawrence Stewart static void inline cc_after_idle(struct tcpcb *tp); 130dbc42409SLawrence Stewart 131dbc42409SLawrence Stewart /* 132dbc42409SLawrence Stewart * CC wrapper hook functions 133dbc42409SLawrence Stewart */ 134dbc42409SLawrence Stewart static void inline 135dbc42409SLawrence Stewart cc_after_idle(struct tcpcb *tp) 136dbc42409SLawrence Stewart { 137dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 138dbc42409SLawrence Stewart 139dbc42409SLawrence Stewart if (CC_ALGO(tp)->after_idle != NULL) 140dbc42409SLawrence Stewart CC_ALGO(tp)->after_idle(tp->ccv); 141dbc42409SLawrence Stewart } 1426741ecf5SAndre Oppermann 143df8bae1dSRodney W. Grimes /* 144df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 145df8bae1dSRodney W. Grimes */ 146df8bae1dSRodney W. Grimes int 147f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp) 148df8bae1dSRodney W. Grimes { 149f10e85d7SLuigi Rizzo struct socket *so = tp->t_inpcb->inp_socket; 150201d185bSAndre Oppermann long len, recwin, sendwin; 151*2ea8da28SLawrence Stewart int off, flags, error; 152f10e85d7SLuigi Rizzo struct mbuf *m; 153fb59c426SYoshinobu Inoue struct ip *ip = NULL; 154f10e85d7SLuigi Rizzo struct ipovly *ipov = NULL; 155f10e85d7SLuigi Rizzo struct tcphdr *th; 156a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 157a04884fcSBill Fenner unsigned ipoptlen, optlen, hdrlen; 1589ad0173dSBjoern A. Zeeb #ifdef IPSEC 1599ad0173dSBjoern A. Zeeb unsigned ipsec_optlen = 0; 1609ad0173dSBjoern A. Zeeb #endif 161df8bae1dSRodney W. Grimes int idle, sendalot; 16202a1a643SAndre Oppermann int sack_rxmit, sack_bytes_rxmt; 1636d90faf3SPaul Saab struct sackhole *p; 164153e5b57SAndre Oppermann int tso; 16502a1a643SAndre Oppermann struct tcpopt to; 166262c1c1aSMatthew Dillon #if 0 16746f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 168262c1c1aSMatthew Dillon #endif 169fb59c426SYoshinobu Inoue #ifdef INET6 170f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 171fb59c426SYoshinobu Inoue int isipv6; 172fb59c426SYoshinobu Inoue 173fb59c426SYoshinobu Inoue isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 174fb59c426SYoshinobu Inoue #endif 175df8bae1dSRodney W. Grimes 1768501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1773d6ade3aSJennifer Yang 178df8bae1dSRodney W. Grimes /* 179df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 180df8bae1dSRodney W. Grimes * and flags that will be used. 181df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 182df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 183df8bae1dSRodney W. Grimes */ 184c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 185*2ea8da28SLawrence Stewart if (idle && ticks - tp->t_rcvtime >= tp->t_rxtcur) 186*2ea8da28SLawrence Stewart cc_after_idle(tp); 187c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 188c24d5daeSJayanth Vijayaraghavan if (idle) { 189c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 190c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 191c24d5daeSJayanth Vijayaraghavan idle = 0; 192c24d5daeSJayanth Vijayaraghavan } 193c24d5daeSJayanth Vijayaraghavan } 194df8bae1dSRodney W. Grimes again: 1956d90faf3SPaul Saab /* 1966d90faf3SPaul Saab * If we've recently taken a timeout, snd_max will be greater than 1976d90faf3SPaul Saab * snd_nxt. There may be SACK information that allows us to avoid 1986d90faf3SPaul Saab * resending already delivered data. Adjust snd_nxt accordingly. 1996d90faf3SPaul Saab */ 2003529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2013529149eSAndre Oppermann SEQ_LT(tp->snd_nxt, tp->snd_max)) 2026d90faf3SPaul Saab tcp_sack_adjust(tp); 203df8bae1dSRodney W. Grimes sendalot = 0; 204153e5b57SAndre Oppermann tso = 0; 205df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 206201d185bSAndre Oppermann sendwin = min(tp->snd_wnd, tp->snd_cwnd); 207df8bae1dSRodney W. Grimes 208df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 209a0292f23SGarrett Wollman /* 2106d90faf3SPaul Saab * Send any SACK-generated retransmissions. If we're explicitly trying 2116d90faf3SPaul Saab * to send out new data (when sendalot is 1), bypass this function. 2126d90faf3SPaul Saab * If we retransmit in fast recovery mode, decrement snd_cwnd, since 2136d90faf3SPaul Saab * we're replacing a (future) new transmission with a retransmission 2146d90faf3SPaul Saab * now, and we previously incremented snd_cwnd in tcp_input(). 2156d90faf3SPaul Saab */ 2166d90faf3SPaul Saab /* 2176d90faf3SPaul Saab * Still in sack recovery , reset rxmit flag to zero. 2186d90faf3SPaul Saab */ 2196d90faf3SPaul Saab sack_rxmit = 0; 220a55db2b6SPaul Saab sack_bytes_rxmt = 0; 2216d90faf3SPaul Saab len = 0; 2226d90faf3SPaul Saab p = NULL; 223dbc42409SLawrence Stewart if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp->t_flags) && 224a55db2b6SPaul Saab (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 225a55db2b6SPaul Saab long cwin; 226a55db2b6SPaul Saab 227a55db2b6SPaul Saab cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt; 228a55db2b6SPaul Saab if (cwin < 0) 229a55db2b6SPaul Saab cwin = 0; 230f787edd8SJayanth Vijayaraghavan /* Do not retransmit SACK segments beyond snd_recover */ 231f787edd8SJayanth Vijayaraghavan if (SEQ_GT(p->end, tp->snd_recover)) { 232f787edd8SJayanth Vijayaraghavan /* 233f787edd8SJayanth Vijayaraghavan * (At least) part of sack hole extends beyond 234f787edd8SJayanth Vijayaraghavan * snd_recover. Check to see if we can rexmit data 235f787edd8SJayanth Vijayaraghavan * for this hole. 236f787edd8SJayanth Vijayaraghavan */ 237f787edd8SJayanth Vijayaraghavan if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 238f787edd8SJayanth Vijayaraghavan /* 239f787edd8SJayanth Vijayaraghavan * Can't rexmit any more data for this hole. 240f787edd8SJayanth Vijayaraghavan * That data will be rexmitted in the next 241f787edd8SJayanth Vijayaraghavan * sack recovery episode, when snd_recover 242f787edd8SJayanth Vijayaraghavan * moves past p->rxmit. 243f787edd8SJayanth Vijayaraghavan */ 244f787edd8SJayanth Vijayaraghavan p = NULL; 245f787edd8SJayanth Vijayaraghavan goto after_sack_rexmit; 246f787edd8SJayanth Vijayaraghavan } else 247f787edd8SJayanth Vijayaraghavan /* Can rexmit part of the current hole */ 248a55db2b6SPaul Saab len = ((long)ulmin(cwin, 249f787edd8SJayanth Vijayaraghavan tp->snd_recover - p->rxmit)); 250f787edd8SJayanth Vijayaraghavan } else 251a55db2b6SPaul Saab len = ((long)ulmin(cwin, p->end - p->rxmit)); 2526d90faf3SPaul Saab off = p->rxmit - tp->snd_una; 253f787edd8SJayanth Vijayaraghavan KASSERT(off >= 0,("%s: sack block to the left of una : %d", 254f787edd8SJayanth Vijayaraghavan __func__, off)); 2556d90faf3SPaul Saab if (len > 0) { 256ab5c14d8SRobert Watson sack_rxmit = 1; 257ab5c14d8SRobert Watson sendalot = 1; 25878b50714SRobert Watson TCPSTAT_INC(tcps_sack_rexmits); 25978b50714SRobert Watson TCPSTAT_ADD(tcps_sack_rexmit_bytes, 26078b50714SRobert Watson min(len, tp->t_maxseg)); 2616d90faf3SPaul Saab } 2626d90faf3SPaul Saab } 263f787edd8SJayanth Vijayaraghavan after_sack_rexmit: 2646d90faf3SPaul Saab /* 265a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 266a0292f23SGarrett Wollman * state flags. 267a0292f23SGarrett Wollman */ 268a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 269a0292f23SGarrett Wollman flags |= TH_FIN; 270a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 271a0292f23SGarrett Wollman flags |= TH_SYN; 272a0292f23SGarrett Wollman 273cf2942b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 274df8bae1dSRodney W. Grimes /* 275df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 276df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 277df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 278df8bae1dSRodney W. Grimes * and go to transmit state. 279df8bae1dSRodney W. Grimes */ 2802cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) { 281201d185bSAndre Oppermann if (sendwin == 0) { 282df8bae1dSRodney W. Grimes /* 283df8bae1dSRodney W. Grimes * If we still have some data to send, then 284df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 285df8bae1dSRodney W. Grimes * happen below when it realizes that we 286df8bae1dSRodney W. Grimes * aren't sending all the data. However, 28729089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 288df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 289df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 290df8bae1dSRodney W. Grimes * up sending the packet without recording 291df8bae1dSRodney W. Grimes * that we sent the FIN bit. 292df8bae1dSRodney W. Grimes * 293df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 294df8bae1dSRodney W. Grimes * because if we don't have any more data 295df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 296df8bae1dSRodney W. Grimes * itself. 297df8bae1dSRodney W. Grimes */ 298df8bae1dSRodney W. Grimes if (off < so->so_snd.sb_cc) 299df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 300201d185bSAndre Oppermann sendwin = 1; 301df8bae1dSRodney W. Grimes } else { 302b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, 0); 303df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 304df8bae1dSRodney W. Grimes } 305df8bae1dSRodney W. Grimes } 306df8bae1dSRodney W. Grimes 30728257b5cSMatthew Dillon /* 30828257b5cSMatthew Dillon * If snd_nxt == snd_max and we have transmitted a FIN, the 30928257b5cSMatthew Dillon * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 310201d185bSAndre Oppermann * a negative length. This can also occur when TCP opens up 31128257b5cSMatthew Dillon * its congestion window while receiving additional duplicate 31228257b5cSMatthew Dillon * acks after fast-retransmit because TCP will reset snd_nxt 31328257b5cSMatthew Dillon * to snd_max after the fast-retransmit. 31428257b5cSMatthew Dillon * 31528257b5cSMatthew Dillon * In the normal retransmit-FIN-only case, however, snd_nxt will 31628257b5cSMatthew Dillon * be set to snd_una, the offset will be 0, and the length may 31728257b5cSMatthew Dillon * wind up 0. 3186d90faf3SPaul Saab * 3196d90faf3SPaul Saab * If sack_rxmit is true we are retransmitting from the scoreboard 3206d90faf3SPaul Saab * in which case len is already set. 32128257b5cSMatthew Dillon */ 322a55db2b6SPaul Saab if (sack_rxmit == 0) { 323a55db2b6SPaul Saab if (sack_bytes_rxmt == 0) 3246d90faf3SPaul Saab len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off); 325a55db2b6SPaul Saab else { 326a55db2b6SPaul Saab long cwin; 327a55db2b6SPaul Saab 328a55db2b6SPaul Saab /* 329a55db2b6SPaul Saab * We are inside of a SACK recovery episode and are 330a55db2b6SPaul Saab * sending new data, having retransmitted all the 331a55db2b6SPaul Saab * data possible in the scoreboard. 332a55db2b6SPaul Saab */ 3337d5ed1ceSPaul Saab len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd) 3347d5ed1ceSPaul Saab - off); 3358d03f2b5SPaul Saab /* 3368d03f2b5SPaul Saab * Don't remove this (len > 0) check ! 3378d03f2b5SPaul Saab * We explicitly check for len > 0 here (although it 3388d03f2b5SPaul Saab * isn't really necessary), to work around a gcc 3398d03f2b5SPaul Saab * optimization issue - to force gcc to compute 3408d03f2b5SPaul Saab * len above. Without this check, the computation 3418d03f2b5SPaul Saab * of len is bungled by the optimizer. 3428d03f2b5SPaul Saab */ 3438d03f2b5SPaul Saab if (len > 0) { 3447d5ed1ceSPaul Saab cwin = tp->snd_cwnd - 3457d5ed1ceSPaul Saab (tp->snd_nxt - tp->sack_newdata) - 346a55db2b6SPaul Saab sack_bytes_rxmt; 347a55db2b6SPaul Saab if (cwin < 0) 348a55db2b6SPaul Saab cwin = 0; 349a55db2b6SPaul Saab len = lmin(len, cwin); 350a55db2b6SPaul Saab } 351a55db2b6SPaul Saab } 3528d03f2b5SPaul Saab } 353a0292f23SGarrett Wollman 354a0292f23SGarrett Wollman /* 355a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 356a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 357a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 358a0292f23SGarrett Wollman */ 359a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 3604b8e98d6SQing Li if (tp->t_state != TCPS_SYN_RECEIVED) 361a0292f23SGarrett Wollman flags &= ~TH_SYN; 362a0292f23SGarrett Wollman off--, len++; 363a0292f23SGarrett Wollman } 364a0292f23SGarrett Wollman 36581165e48SAndras Olah /* 366c94c54e4SAndre Oppermann * Be careful not to send data and/or FIN on SYN segments. 36781165e48SAndras Olah * This measure is needed to prevent interoperability problems 36881165e48SAndras Olah * with not fully conformant TCP implementations. 36981165e48SAndras Olah */ 370c94c54e4SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 37181165e48SAndras Olah len = 0; 37281165e48SAndras Olah flags &= ~TH_FIN; 37381165e48SAndras Olah } 37481165e48SAndras Olah 375df8bae1dSRodney W. Grimes if (len < 0) { 376df8bae1dSRodney W. Grimes /* 377df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 378df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 37928257b5cSMatthew Dillon * len will be < 0. Otherwise, window shrank 380df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 3812d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 3822d8266afSDavid Greenman * to (closed) window, and set the persist timer 3832d8266afSDavid Greenman * if it isn't already going. If the window didn't 3842d8266afSDavid Greenman * close completely, just wait for an ACK. 385df8bae1dSRodney W. Grimes */ 386df8bae1dSRodney W. Grimes len = 0; 387201d185bSAndre Oppermann if (sendwin == 0) { 388b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 3892d8266afSDavid Greenman tp->t_rxtshift = 0; 390df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 391b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_PERSIST)) 3922d8266afSDavid Greenman tcp_setpersist(tp); 393df8bae1dSRodney W. Grimes } 394df8bae1dSRodney W. Grimes } 39528257b5cSMatthew Dillon 3966741ecf5SAndre Oppermann /* len will be >= 0 after this point. */ 397c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 3986741ecf5SAndre Oppermann 39928257b5cSMatthew Dillon /* 4006741ecf5SAndre Oppermann * Automatic sizing of send socket buffer. Often the send buffer 4016741ecf5SAndre Oppermann * size is not optimally adjusted to the actual network conditions 4026741ecf5SAndre Oppermann * at hand (delay bandwidth product). Setting the buffer size too 4036741ecf5SAndre Oppermann * small limits throughput on links with high bandwidth and high 4046741ecf5SAndre Oppermann * delay (eg. trans-continental/oceanic links). Setting the 4056741ecf5SAndre Oppermann * buffer size too big consumes too much real kernel memory, 4066741ecf5SAndre Oppermann * especially with many connections on busy servers. 4076741ecf5SAndre Oppermann * 4086741ecf5SAndre Oppermann * The criteria to step up the send buffer one notch are: 4096741ecf5SAndre Oppermann * 1. receive window of remote host is larger than send buffer 4106741ecf5SAndre Oppermann * (with a fudge factor of 5/4th); 4116741ecf5SAndre Oppermann * 2. send buffer is filled to 7/8th with data (so we actually 4126741ecf5SAndre Oppermann * have data to make use of it); 4136741ecf5SAndre Oppermann * 3. send buffer fill has not hit maximal automatic size; 4146741ecf5SAndre Oppermann * 4. our send window (slow start and cogestion controlled) is 4156741ecf5SAndre Oppermann * larger than sent but unacknowledged data in send buffer. 4166741ecf5SAndre Oppermann * 4176741ecf5SAndre Oppermann * The remote host receive window scaling factor may limit the 4186741ecf5SAndre Oppermann * growing of the send buffer before it reaches its allowed 4196741ecf5SAndre Oppermann * maximum. 4206741ecf5SAndre Oppermann * 4216741ecf5SAndre Oppermann * It scales directly with slow start or congestion window 4226741ecf5SAndre Oppermann * and does at most one step per received ACK. This fast 4236741ecf5SAndre Oppermann * scaling has the drawback of growing the send buffer beyond 4246741ecf5SAndre Oppermann * what is strictly necessary to make full use of a given 4256741ecf5SAndre Oppermann * delay*bandwith product. However testing has shown this not 4266741ecf5SAndre Oppermann * to be much of an problem. At worst we are trading wasting 4276741ecf5SAndre Oppermann * of available bandwith (the non-use of it) for wasting some 4286741ecf5SAndre Oppermann * socket buffer memory. 4296741ecf5SAndre Oppermann * 4306741ecf5SAndre Oppermann * TODO: Shrink send buffer during idle periods together 4316741ecf5SAndre Oppermann * with congestion window. Requires another timer. Has to 4326741ecf5SAndre Oppermann * wait for upcoming tcp timer rewrite. 4336741ecf5SAndre Oppermann */ 434603724d3SBjoern A. Zeeb if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 4356741ecf5SAndre Oppermann if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat && 4366741ecf5SAndre Oppermann so->so_snd.sb_cc >= (so->so_snd.sb_hiwat / 8 * 7) && 437603724d3SBjoern A. Zeeb so->so_snd.sb_cc < V_tcp_autosndbuf_max && 4386741ecf5SAndre Oppermann sendwin >= (so->so_snd.sb_cc - (tp->snd_nxt - tp->snd_una))) { 4396741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_snd, 440603724d3SBjoern A. Zeeb min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc, 441603724d3SBjoern A. Zeeb V_tcp_autosndbuf_max), so, curthread)) 4426741ecf5SAndre Oppermann so->so_snd.sb_flags &= ~SB_AUTOSIZE; 4436741ecf5SAndre Oppermann } 4446741ecf5SAndre Oppermann } 4456741ecf5SAndre Oppermann 4466741ecf5SAndre Oppermann /* 447ed420311SAndre Oppermann * Decide if we can use TCP Segmentation Offloading (if supported by 448ed420311SAndre Oppermann * hardware). 449b3c0f300SAndre Oppermann * 450b3c0f300SAndre Oppermann * TSO may only be used if we are in a pure bulk sending state. The 451b3c0f300SAndre Oppermann * presence of TCP-MD5, SACK retransmits, SACK advertizements and 452b3c0f300SAndre Oppermann * IP options prevent using TSO. With TSO the TCP header is the same 453b3c0f300SAndre Oppermann * (except for the sequence number) for all generated packets. This 454b3c0f300SAndre Oppermann * makes it impossible to transmit any options which vary per generated 455b3c0f300SAndre Oppermann * segment or packet. 45628257b5cSMatthew Dillon */ 4579ad0173dSBjoern A. Zeeb #ifdef IPSEC 4589ad0173dSBjoern A. Zeeb /* 4599ad0173dSBjoern A. Zeeb * Pre-calculate here as we save another lookup into the darknesses 4609ad0173dSBjoern A. Zeeb * of IPsec that way and can actually decide if TSO is ok. 4619ad0173dSBjoern A. Zeeb */ 4629ad0173dSBjoern A. Zeeb ipsec_optlen = ipsec_hdrsiz_tcp(tp); 4639ad0173dSBjoern A. Zeeb #endif 464ed420311SAndre Oppermann if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg && 465b3c0f300SAndre Oppermann ((tp->t_flags & TF_SIGNATURE) == 0) && 466b3c0f300SAndre Oppermann tp->rcv_numsacks == 0 && sack_rxmit == 0 && 4679ad0173dSBjoern A. Zeeb #ifdef IPSEC 468ed420311SAndre Oppermann ipsec_optlen == 0 && 4699ad0173dSBjoern A. Zeeb #endif 470ed420311SAndre Oppermann tp->t_inpcb->inp_options == NULL && 471ed420311SAndre Oppermann tp->t_inpcb->in6p_options == NULL) 472b3c0f300SAndre Oppermann tso = 1; 473153e5b57SAndre Oppermann 4745d3b1b75SJayanth Vijayaraghavan if (sack_rxmit) { 4755d3b1b75SJayanth Vijayaraghavan if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc)) 476df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 4775d3b1b75SJayanth Vijayaraghavan } else { 4785d3b1b75SJayanth Vijayaraghavan if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 4795d3b1b75SJayanth Vijayaraghavan flags &= ~TH_FIN; 4805d3b1b75SJayanth Vijayaraghavan } 481df8bae1dSRodney W. Grimes 482201d185bSAndre Oppermann recwin = sbspace(&so->so_rcv); 483df8bae1dSRodney W. Grimes 484df8bae1dSRodney W. Grimes /* 485262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 486262c1c1aSMatthew Dillon * conditions when len is non-zero: 487262c1c1aSMatthew Dillon * 488b3c0f300SAndre Oppermann * - We have a full segment (or more with TSO) 489262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 490262c1c1aSMatthew Dillon * either idle or running NODELAY 491262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 492262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 493262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 494262c1c1aSMatthew Dillon * - we need to retransmit 495df8bae1dSRodney W. Grimes */ 496df8bae1dSRodney W. Grimes if (len) { 497b3c0f300SAndre Oppermann if (len >= tp->t_maxseg) 498df8bae1dSRodney W. Grimes goto send; 499262c1c1aSMatthew Dillon /* 500262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 501262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 502262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 503262c1c1aSMatthew Dillon * 504262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 505262c1c1aSMatthew Dillon */ 506262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 507262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 508262c1c1aSMatthew Dillon len + off >= so->so_snd.sb_cc && 509262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 510df8bae1dSRodney W. Grimes goto send; 511262c1c1aSMatthew Dillon } 5122cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */ 513df8bae1dSRodney W. Grimes goto send; 514a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 515df8bae1dSRodney W. Grimes goto send; 516262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 517df8bae1dSRodney W. Grimes goto send; 5186d90faf3SPaul Saab if (sack_rxmit) 5196d90faf3SPaul Saab goto send; 520df8bae1dSRodney W. Grimes } 521df8bae1dSRodney W. Grimes 522df8bae1dSRodney W. Grimes /* 523df8bae1dSRodney W. Grimes * Compare available window to amount of window 524df8bae1dSRodney W. Grimes * known to peer (as advertised window less 525df8bae1dSRodney W. Grimes * next expected input). If the difference is at least two 526df8bae1dSRodney W. Grimes * max size segments, or at least 50% of the maximum possible 527df8bae1dSRodney W. Grimes * window, then want to send a window update to peer. 5283bfd6421SJonathan Lemon * Skip this if the connection is in T/TCP half-open state. 529b7de7d87SAndre Oppermann * Don't send pure window updates when the peer has closed 530b7de7d87SAndre Oppermann * the connection and won't ever send more data. 531df8bae1dSRodney W. Grimes */ 532b7de7d87SAndre Oppermann if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 533b7de7d87SAndre Oppermann !TCPS_HAVERCVDFIN(tp->t_state)) { 534df8bae1dSRodney W. Grimes /* 535df8bae1dSRodney W. Grimes * "adv" is the amount we can increase the window, 536df8bae1dSRodney W. Grimes * taking into account that we are limited by 537df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 538df8bae1dSRodney W. Grimes */ 539201d185bSAndre Oppermann long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) - 540df8bae1dSRodney W. Grimes (tp->rcv_adv - tp->rcv_nxt); 541df8bae1dSRodney W. Grimes 542df8bae1dSRodney W. Grimes if (adv >= (long) (2 * tp->t_maxseg)) 543df8bae1dSRodney W. Grimes goto send; 544df8bae1dSRodney W. Grimes if (2 * adv >= (long) so->so_rcv.sb_hiwat) 545df8bae1dSRodney W. Grimes goto send; 546df8bae1dSRodney W. Grimes } 547df8bae1dSRodney W. Grimes 548df8bae1dSRodney W. Grimes /* 54928257b5cSMatthew Dillon * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 55028257b5cSMatthew Dillon * is also a catch-all for the retransmit timer timeout case. 551df8bae1dSRodney W. Grimes */ 552df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 553df8bae1dSRodney W. Grimes goto send; 554a0292f23SGarrett Wollman if ((flags & TH_RST) || 555a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 556df8bae1dSRodney W. Grimes goto send; 557df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 558df8bae1dSRodney W. Grimes goto send; 559df8bae1dSRodney W. Grimes /* 560df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 56128257b5cSMatthew Dillon * and we have not yet done so, then we need to send. 562df8bae1dSRodney W. Grimes */ 563df8bae1dSRodney W. Grimes if (flags & TH_FIN && 564df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 565df8bae1dSRodney W. Grimes goto send; 5666d90faf3SPaul Saab /* 5676d90faf3SPaul Saab * In SACK, it is possible for tcp_output to fail to send a segment 5686d90faf3SPaul Saab * after the retransmission timer has been turned off. Make sure 5696d90faf3SPaul Saab * that the retransmission timer is set. 5706d90faf3SPaul Saab */ 5713529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 5723529149eSAndre Oppermann SEQ_GT(tp->snd_max, tp->snd_una) && 573b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_REXMT) && 574b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 575b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 576cf2942b6SRobert Watson goto just_return; 5776d90faf3SPaul Saab } 578df8bae1dSRodney W. Grimes /* 579df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 580df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 581df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 582df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 583df8bae1dSRodney W. Grimes * persisting to move a small or zero window 584df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 585df8bae1dSRodney W. Grimes * 586b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_PERSIST) 5879b8b58e0SJonathan Lemon * is true when we are in persist state. 5882cdbfa66SPaul Saab * (tp->t_flags & TF_FORCEDATA) 589df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 590b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_REXMT) 591df8bae1dSRodney W. Grimes * is set when we are retransmitting 592df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 593df8bae1dSRodney W. Grimes * 594df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 595df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 596df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 597df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 598df8bae1dSRodney W. Grimes * otherwise force out a byte. 599df8bae1dSRodney W. Grimes */ 600b8152ba7SAndre Oppermann if (so->so_snd.sb_cc && !tcp_timer_active(tp, TT_REXMT) && 601b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 602df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 603df8bae1dSRodney W. Grimes tcp_setpersist(tp); 604df8bae1dSRodney W. Grimes } 605df8bae1dSRodney W. Grimes 606df8bae1dSRodney W. Grimes /* 607df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 608df8bae1dSRodney W. Grimes */ 609cf2942b6SRobert Watson just_return: 610cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 611df8bae1dSRodney W. Grimes return (0); 612df8bae1dSRodney W. Grimes 613df8bae1dSRodney W. Grimes send: 614cf2942b6SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 615df8bae1dSRodney W. Grimes /* 616df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 617df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 618df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 619df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 620df8bae1dSRodney W. Grimes * link header, i.e. 62133841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 622df8bae1dSRodney W. Grimes */ 623df8bae1dSRodney W. Grimes optlen = 0; 624fb59c426SYoshinobu Inoue #ifdef INET6 625fb59c426SYoshinobu Inoue if (isipv6) 626fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 627fb59c426SYoshinobu Inoue else 628fb59c426SYoshinobu Inoue #endif 629df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 63002a1a643SAndre Oppermann 63102a1a643SAndre Oppermann /* 63202a1a643SAndre Oppermann * Compute options for segment. 63302a1a643SAndre Oppermann * We only have to care about SYN and established connection 63402a1a643SAndre Oppermann * segments. Options for SYN-ACK segments are handled in TCP 63502a1a643SAndre Oppermann * syncache. 63602a1a643SAndre Oppermann */ 63702a1a643SAndre Oppermann if ((tp->t_flags & TF_NOOPT) == 0) { 63802a1a643SAndre Oppermann to.to_flags = 0; 63902a1a643SAndre Oppermann /* Maximum segment size. */ 640df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 641df8bae1dSRodney W. Grimes tp->snd_nxt = tp->iss; 64202a1a643SAndre Oppermann to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc); 64302a1a643SAndre Oppermann to.to_flags |= TOF_MSS; 644df8bae1dSRodney W. Grimes } 64502a1a643SAndre Oppermann /* Window scaling. */ 64602a1a643SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 64702a1a643SAndre Oppermann to.to_wscale = tp->request_r_scale; 64802a1a643SAndre Oppermann to.to_flags |= TOF_SCALE; 649df8bae1dSRodney W. Grimes } 65002a1a643SAndre Oppermann /* Timestamps. */ 65102a1a643SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) || 65202a1a643SAndre Oppermann ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 65302a1a643SAndre Oppermann to.to_tsval = ticks + tp->ts_offset; 65402a1a643SAndre Oppermann to.to_tsecr = tp->ts_recent; 65502a1a643SAndre Oppermann to.to_flags |= TOF_TS; 6566741ecf5SAndre Oppermann /* Set receive buffer autosizing timestamp. */ 65702a1a643SAndre Oppermann if (tp->rfbuf_ts == 0 && 65802a1a643SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) 6596741ecf5SAndre Oppermann tp->rfbuf_ts = ticks; 6601cfd4b53SBruce M Simpson } 66102a1a643SAndre Oppermann /* Selective ACK's. */ 6623529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 66302a1a643SAndre Oppermann if (flags & TH_SYN) 66402a1a643SAndre Oppermann to.to_flags |= TOF_SACKPERM; 66502a1a643SAndre Oppermann else if (TCPS_HAVEESTABLISHED(tp->t_state) && 66602a1a643SAndre Oppermann (tp->t_flags & TF_SACK_PERMIT) && 66702a1a643SAndre Oppermann tp->rcv_numsacks > 0) { 66802a1a643SAndre Oppermann to.to_flags |= TOF_SACK; 66902a1a643SAndre Oppermann to.to_nsacks = tp->rcv_numsacks; 67002a1a643SAndre Oppermann to.to_sacks = (u_char *)tp->sackblks; 67102a1a643SAndre Oppermann } 67202a1a643SAndre Oppermann } 67302a1a643SAndre Oppermann #ifdef TCP_SIGNATURE 67402a1a643SAndre Oppermann /* TCP-MD5 (RFC2385). */ 67502a1a643SAndre Oppermann if (tp->t_flags & TF_SIGNATURE) 67602a1a643SAndre Oppermann to.to_flags |= TOF_SIGNATURE; 6771cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 6781cfd4b53SBruce M Simpson 67902a1a643SAndre Oppermann /* Processing the options. */ 6804a411b9fSBjoern A. Zeeb hdrlen += optlen = tcp_addoptions(&to, opt); 681be3f3b5eSPaul Saab } 682be3f3b5eSPaul Saab 683fb59c426SYoshinobu Inoue #ifdef INET6 684fb59c426SYoshinobu Inoue if (isipv6) 685fb59c426SYoshinobu Inoue ipoptlen = ip6_optlen(tp->t_inpcb); 686fb59c426SYoshinobu Inoue else 687fb59c426SYoshinobu Inoue #endif 688f10e85d7SLuigi Rizzo if (tp->t_inpcb->inp_options) 689a04884fcSBill Fenner ipoptlen = tp->t_inpcb->inp_options->m_len - 690a04884fcSBill Fenner offsetof(struct ipoption, ipopt_list); 691f10e85d7SLuigi Rizzo else 692a04884fcSBill Fenner ipoptlen = 0; 693b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 6949ad0173dSBjoern A. Zeeb ipoptlen += ipsec_optlen; 695fb59c426SYoshinobu Inoue #endif 696a04884fcSBill Fenner 697df8bae1dSRodney W. Grimes /* 698df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 699a0292f23SGarrett Wollman * bump the packet length beyond the t_maxopd length. 700a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 701a0292f23SGarrett Wollman * the segment. 702df8bae1dSRodney W. Grimes */ 703a04884fcSBill Fenner if (len + optlen + ipoptlen > tp->t_maxopd) { 704297a37f3SDavid Greenman flags &= ~TH_FIN; 705ed420311SAndre Oppermann 706b3c0f300SAndre Oppermann if (tso) { 707ed420311SAndre Oppermann KASSERT(ipoptlen == 0, 708ed420311SAndre Oppermann ("%s: TSO can't do IP options", __func__)); 709ed420311SAndre Oppermann 710ed420311SAndre Oppermann /* 711ed420311SAndre Oppermann * Limit a burst to IP_MAXPACKET minus IP, 712ed420311SAndre Oppermann * TCP and options length to keep ip->ip_len 713ed420311SAndre Oppermann * from overflowing. 714ed420311SAndre Oppermann */ 715ed420311SAndre Oppermann if (len > IP_MAXPACKET - hdrlen) { 716ed420311SAndre Oppermann len = IP_MAXPACKET - hdrlen; 717b3c0f300SAndre Oppermann sendalot = 1; 718ed420311SAndre Oppermann } 719ed420311SAndre Oppermann 720ed420311SAndre Oppermann /* 721ed420311SAndre Oppermann * Prevent the last segment from being 722ed420311SAndre Oppermann * fractional unless the send sockbuf can 723ed420311SAndre Oppermann * be emptied. 724ed420311SAndre Oppermann */ 725ed420311SAndre Oppermann if (sendalot && off + len < so->so_snd.sb_cc) { 726ed420311SAndre Oppermann len -= len % (tp->t_maxopd - optlen); 727b3c0f300SAndre Oppermann sendalot = 1; 728ed420311SAndre Oppermann } 729ed420311SAndre Oppermann 730ed420311SAndre Oppermann /* 731ed420311SAndre Oppermann * Send the FIN in a separate segment 732ed420311SAndre Oppermann * after the bulk sending is done. 733ed420311SAndre Oppermann * We don't trust the TSO implementations 734ed420311SAndre Oppermann * to clear the FIN flag on all but the 735ed420311SAndre Oppermann * last segment. 736ed420311SAndre Oppermann */ 737ed420311SAndre Oppermann if (tp->t_flags & TF_NEEDFIN) 738ed420311SAndre Oppermann sendalot = 1; 739ed420311SAndre Oppermann 740b3c0f300SAndre Oppermann } else { 741a04884fcSBill Fenner len = tp->t_maxopd - optlen - ipoptlen; 742df8bae1dSRodney W. Grimes sendalot = 1; 743df8bae1dSRodney W. Grimes } 744ed420311SAndre Oppermann } else 745ed420311SAndre Oppermann tso = 0; 746ed420311SAndre Oppermann 747ed420311SAndre Oppermann KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 748ed420311SAndre Oppermann ("%s: len > IP_MAXPACKET", __func__)); 749df8bae1dSRodney W. Grimes 750a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 751a683a7ddSYoshinobu Inoue #ifdef INET6 752a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 753a683a7ddSYoshinobu Inoue #else 754df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 755a683a7ddSYoshinobu Inoue #endif 756f10e85d7SLuigi Rizzo panic("tcphdr too big"); 757a0292f23SGarrett Wollman /*#endif*/ 758df8bae1dSRodney W. Grimes 759df8bae1dSRodney W. Grimes /* 760c4982faeSBjoern A. Zeeb * This KASSERT is here to catch edge cases at a well defined place. 761c4982faeSBjoern A. Zeeb * Before, those had triggered (random) panic conditions further down. 762c4982faeSBjoern A. Zeeb */ 763c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 764c4982faeSBjoern A. Zeeb 765c4982faeSBjoern A. Zeeb /* 766df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 767df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 768df8bae1dSRodney W. Grimes * the template for sends on this connection. 769df8bae1dSRodney W. Grimes */ 770df8bae1dSRodney W. Grimes if (len) { 7714e023759SAndre Oppermann struct mbuf *mb; 7724e023759SAndre Oppermann u_int moff; 7734e023759SAndre Oppermann 7742cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) && len == 1) 77578b50714SRobert Watson TCPSTAT_INC(tcps_sndprobe); 7760ba5d2eeSJohn Baldwin else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) { 777f5d34df5SGeorge V. Neville-Neil tp->t_sndrexmitpack++; 77878b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitpack); 77978b50714SRobert Watson TCPSTAT_ADD(tcps_sndrexmitbyte, len); 780df8bae1dSRodney W. Grimes } else { 78178b50714SRobert Watson TCPSTAT_INC(tcps_sndpack); 78278b50714SRobert Watson TCPSTAT_ADD(tcps_sndbyte, len); 783df8bae1dSRodney W. Grimes } 784df8bae1dSRodney W. Grimes #ifdef notyet 785df8bae1dSRodney W. Grimes if ((m = m_copypack(so->so_snd.sb_mb, off, 786df8bae1dSRodney W. Grimes (int)len, max_linkhdr + hdrlen)) == 0) { 787cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 788df8bae1dSRodney W. Grimes error = ENOBUFS; 789df8bae1dSRodney W. Grimes goto out; 790df8bae1dSRodney W. Grimes } 791df8bae1dSRodney W. Grimes /* 792df8bae1dSRodney W. Grimes * m_copypack left space for our hdr; use it. 793df8bae1dSRodney W. Grimes */ 794df8bae1dSRodney W. Grimes m->m_len += hdrlen; 795df8bae1dSRodney W. Grimes m->m_data -= hdrlen; 796df8bae1dSRodney W. Grimes #else 79734333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 798df8bae1dSRodney W. Grimes if (m == NULL) { 799cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 800df8bae1dSRodney W. Grimes error = ENOBUFS; 801df8bae1dSRodney W. Grimes goto out; 802df8bae1dSRodney W. Grimes } 803fb59c426SYoshinobu Inoue #ifdef INET6 804a683a7ddSYoshinobu Inoue if (MHLEN < hdrlen + max_linkhdr) { 805a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 806a683a7ddSYoshinobu Inoue if ((m->m_flags & M_EXT) == 0) { 807cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 808a683a7ddSYoshinobu Inoue m_freem(m); 809a683a7ddSYoshinobu Inoue error = ENOBUFS; 810a683a7ddSYoshinobu Inoue goto out; 811a683a7ddSYoshinobu Inoue } 812a683a7ddSYoshinobu Inoue } 813fb59c426SYoshinobu Inoue #endif 814df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 815df8bae1dSRodney W. Grimes m->m_len = hdrlen; 8164e023759SAndre Oppermann 8174e023759SAndre Oppermann /* 8184e023759SAndre Oppermann * Start the m_copy functions from the closest mbuf 8194e023759SAndre Oppermann * to the offset in the socket buffer chain. 8204e023759SAndre Oppermann */ 8214e023759SAndre Oppermann mb = sbsndptr(&so->so_snd, off, len, &moff); 8224e023759SAndre Oppermann 823df8bae1dSRodney W. Grimes if (len <= MHLEN - hdrlen - max_linkhdr) { 8244e023759SAndre Oppermann m_copydata(mb, moff, (int)len, 825df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 826df8bae1dSRodney W. Grimes m->m_len += len; 827df8bae1dSRodney W. Grimes } else { 8284e023759SAndre Oppermann m->m_next = m_copy(mb, moff, (int)len); 8294e023759SAndre Oppermann if (m->m_next == NULL) { 830cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 831d7f570e6SGarrett Wollman (void) m_free(m); 83251823c3aSGarrett Wollman error = ENOBUFS; 83351823c3aSGarrett Wollman goto out; 83451823c3aSGarrett Wollman } 835df8bae1dSRodney W. Grimes } 836df8bae1dSRodney W. Grimes #endif 837df8bae1dSRodney W. Grimes /* 838df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 839df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 840df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 841df8bae1dSRodney W. Grimes * a PUSH comes in.) 842df8bae1dSRodney W. Grimes */ 843df8bae1dSRodney W. Grimes if (off + len == so->so_snd.sb_cc) 844df8bae1dSRodney W. Grimes flags |= TH_PUSH; 845cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 846df8bae1dSRodney W. Grimes } else { 847cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 848df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 84978b50714SRobert Watson TCPSTAT_INC(tcps_sndacks); 850df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 85178b50714SRobert Watson TCPSTAT_INC(tcps_sndctrl); 852df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 85378b50714SRobert Watson TCPSTAT_INC(tcps_sndurg); 854df8bae1dSRodney W. Grimes else 85578b50714SRobert Watson TCPSTAT_INC(tcps_sndwinup); 856df8bae1dSRodney W. Grimes 85734333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 858df8bae1dSRodney W. Grimes if (m == NULL) { 859df8bae1dSRodney W. Grimes error = ENOBUFS; 860df8bae1dSRodney W. Grimes goto out; 861df8bae1dSRodney W. Grimes } 862fb59c426SYoshinobu Inoue #ifdef INET6 863fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 864fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 865fb59c426SYoshinobu Inoue MH_ALIGN(m, hdrlen); 866fb59c426SYoshinobu Inoue } else 867fb59c426SYoshinobu Inoue #endif 868df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 869df8bae1dSRodney W. Grimes m->m_len = hdrlen; 870df8bae1dSRodney W. Grimes } 871cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); 872df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 873c488362eSRobert Watson #ifdef MAC 87430d239bcSRobert Watson mac_inpcb_create_mbuf(tp->t_inpcb, m); 875c488362eSRobert Watson #endif 876fb59c426SYoshinobu Inoue #ifdef INET6 877fb59c426SYoshinobu Inoue if (isipv6) { 878fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 879fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 88079909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip6, th); 881fb59c426SYoshinobu Inoue } else 882fb59c426SYoshinobu Inoue #endif /* INET6 */ 883fb59c426SYoshinobu Inoue { 884fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 885fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 886fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 88779909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip, th); 888fb59c426SYoshinobu Inoue } 889df8bae1dSRodney W. Grimes 890df8bae1dSRodney W. Grimes /* 891df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 892df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 893df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 894df8bae1dSRodney W. Grimes */ 895df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 896df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 897df8bae1dSRodney W. Grimes tp->snd_nxt--; 898df8bae1dSRodney W. Grimes /* 899f2512ba1SRui Paulo * If we are starting a connection, send ECN setup 900f2512ba1SRui Paulo * SYN packet. If we are on a retransmit, we may 901f2512ba1SRui Paulo * resend those bits a number of times as per 902f2512ba1SRui Paulo * RFC 3168. 903f2512ba1SRui Paulo */ 904603724d3SBjoern A. Zeeb if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) { 905f2512ba1SRui Paulo if (tp->t_rxtshift >= 1) { 906603724d3SBjoern A. Zeeb if (tp->t_rxtshift <= V_tcp_ecn_maxretries) 907f2512ba1SRui Paulo flags |= TH_ECE|TH_CWR; 908f2512ba1SRui Paulo } else 909f2512ba1SRui Paulo flags |= TH_ECE|TH_CWR; 910f2512ba1SRui Paulo } 911f2512ba1SRui Paulo 912f2512ba1SRui Paulo if (tp->t_state == TCPS_ESTABLISHED && 913f2512ba1SRui Paulo (tp->t_flags & TF_ECN_PERMIT)) { 914f2512ba1SRui Paulo /* 915f2512ba1SRui Paulo * If the peer has ECN, mark data packets with 916f2512ba1SRui Paulo * ECN capable transmission (ECT). 917f2512ba1SRui Paulo * Ignore pure ack packets, retransmissions and window probes. 918f2512ba1SRui Paulo */ 919f2512ba1SRui Paulo if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) && 920f2512ba1SRui Paulo !((tp->t_flags & TF_FORCEDATA) && len == 1)) { 921f2512ba1SRui Paulo #ifdef INET6 922f2512ba1SRui Paulo if (isipv6) 923f2512ba1SRui Paulo ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 924f2512ba1SRui Paulo else 925f2512ba1SRui Paulo #endif 926f2512ba1SRui Paulo ip->ip_tos |= IPTOS_ECN_ECT0; 92778b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 928f2512ba1SRui Paulo } 929f2512ba1SRui Paulo 930f2512ba1SRui Paulo /* 931f2512ba1SRui Paulo * Reply with proper ECN notifications. 932f2512ba1SRui Paulo */ 933f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_SND_CWR) { 934f2512ba1SRui Paulo flags |= TH_CWR; 935f2512ba1SRui Paulo tp->t_flags &= ~TF_ECN_SND_CWR; 936f2512ba1SRui Paulo } 937f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_SND_ECE) 938f2512ba1SRui Paulo flags |= TH_ECE; 939f2512ba1SRui Paulo } 940f2512ba1SRui Paulo 941f2512ba1SRui Paulo /* 942df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 943df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 944df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 945df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 946df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 947df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 948df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 949df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 950df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 951df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 952df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 953df8bae1dSRodney W. Grimes */ 954a55db2b6SPaul Saab if (sack_rxmit == 0) { 955b8152ba7SAndre Oppermann if (len || (flags & (TH_SYN|TH_FIN)) || 956b8152ba7SAndre Oppermann tcp_timer_active(tp, TT_PERSIST)) 957fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 958df8bae1dSRodney W. Grimes else 959fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 960a55db2b6SPaul Saab } else { 9616d90faf3SPaul Saab th->th_seq = htonl(p->rxmit); 9626d90faf3SPaul Saab p->rxmit += len; 9630077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += len; 9646d90faf3SPaul Saab } 965fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 966df8bae1dSRodney W. Grimes if (optlen) { 967fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 968fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 969df8bae1dSRodney W. Grimes } 970fb59c426SYoshinobu Inoue th->th_flags = flags; 971df8bae1dSRodney W. Grimes /* 972df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 973df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 974df8bae1dSRodney W. Grimes */ 975201d185bSAndre Oppermann if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && 976201d185bSAndre Oppermann recwin < (long)tp->t_maxseg) 977201d185bSAndre Oppermann recwin = 0; 978201d185bSAndre Oppermann if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt)) 979201d185bSAndre Oppermann recwin = (long)(tp->rcv_adv - tp->rcv_nxt); 980201d185bSAndre Oppermann if (recwin > (long)TCP_MAXWIN << tp->rcv_scale) 981201d185bSAndre Oppermann recwin = (long)TCP_MAXWIN << tp->rcv_scale; 982262c1c1aSMatthew Dillon 983104ebb2aSAndre Oppermann /* 984104ebb2aSAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 985104ebb2aSAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> 986104ebb2aSAndre Oppermann * case is handled in syncache. 987104ebb2aSAndre Oppermann */ 988104ebb2aSAndre Oppermann if (flags & TH_SYN) 989104ebb2aSAndre Oppermann th->th_win = htons((u_short) 990104ebb2aSAndre Oppermann (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 991104ebb2aSAndre Oppermann else 992104ebb2aSAndre Oppermann th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 993262c1c1aSMatthew Dillon 994262c1c1aSMatthew Dillon /* 995262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 996262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 997262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 998262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 999b2722702SRui Paulo * to read more data than can be buffered prior to transmitting on 1000262c1c1aSMatthew Dillon * the connection. 1001262c1c1aSMatthew Dillon */ 1002f5d34df5SGeorge V. Neville-Neil if (th->th_win == 0) { 1003f5d34df5SGeorge V. Neville-Neil tp->t_sndzerowin++; 1004262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 1005f5d34df5SGeorge V. Neville-Neil } else 1006262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 1007df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 1008fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 1009fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 1010df8bae1dSRodney W. Grimes } else 1011df8bae1dSRodney W. Grimes /* 1012df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 1013df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 1014df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 1015df8bae1dSRodney W. Grimes * number wraparound. 1016df8bae1dSRodney W. Grimes */ 1017df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 1018df8bae1dSRodney W. Grimes 10191cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 1020ee763d0dSBjoern A. Zeeb if (tp->t_flags & TF_SIGNATURE) { 1021ee763d0dSBjoern A. Zeeb int sigoff = to.to_signature - opt; 10223418daf2SBjoern A. Zeeb tcp_signature_compute(m, 0, len, optlen, 10231cfd4b53SBruce M Simpson (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND); 1024ee763d0dSBjoern A. Zeeb } 1025265ed012SBruce M Simpson #endif 10261cfd4b53SBruce M Simpson 1027df8bae1dSRodney W. Grimes /* 1028df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 1029df8bae1dSRodney W. Grimes * checksum extended header and data. 1030df8bae1dSRodney W. Grimes */ 1031fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 1032fb59c426SYoshinobu Inoue #ifdef INET6 1033fb59c426SYoshinobu Inoue if (isipv6) 1034fb59c426SYoshinobu Inoue /* 1035fb59c426SYoshinobu Inoue * ip6_plen is not need to be filled now, and will be filled 1036fb59c426SYoshinobu Inoue * in ip6_output. 1037fb59c426SYoshinobu Inoue */ 1038fb59c426SYoshinobu Inoue th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 1039fb59c426SYoshinobu Inoue sizeof(struct tcphdr) + optlen + len); 1040fb59c426SYoshinobu Inoue else 1041fb59c426SYoshinobu Inoue #endif /* INET6 */ 1042fb59c426SYoshinobu Inoue { 1043db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_TCP; 1044db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 104579909384SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 104679909384SJonathan Lemon htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen)); 1047fb59c426SYoshinobu Inoue 1048db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 1049db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 10506e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 1051fb59c426SYoshinobu Inoue } 1052df8bae1dSRodney W. Grimes 1053df8bae1dSRodney W. Grimes /* 1054b3c0f300SAndre Oppermann * Enable TSO and specify the size of the segments. 1055b3c0f300SAndre Oppermann * The TCP pseudo header checksum is always provided. 1056b3c0f300SAndre Oppermann * XXX: Fixme: This is currently not the case for IPv6. 1057b3c0f300SAndre Oppermann */ 1058b3c0f300SAndre Oppermann if (tso) { 1059153e5b57SAndre Oppermann KASSERT(len > tp->t_maxopd - optlen, 1060153e5b57SAndre Oppermann ("%s: len <= tso_segsz", __func__)); 10613579cf4cSKenneth D. Merry m->m_pkthdr.csum_flags |= CSUM_TSO; 1062b3c0f300SAndre Oppermann m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen; 1063b3c0f300SAndre Oppermann } 1064b3c0f300SAndre Oppermann 1065ed420311SAndre Oppermann KASSERT(len + hdrlen + ipoptlen == m_length(m, NULL), 1066ed420311SAndre Oppermann ("%s: mbuf chain shorter than expected", __func__)); 1067ed420311SAndre Oppermann 1068b3c0f300SAndre Oppermann /* 1069df8bae1dSRodney W. Grimes * In transmit state, time the transmission and arrange for 1070df8bae1dSRodney W. Grimes * the retransmit. In persist state, just set snd_max. 1071df8bae1dSRodney W. Grimes */ 10722cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) == 0 || 1073b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 1074df8bae1dSRodney W. Grimes tcp_seq startseq = tp->snd_nxt; 1075df8bae1dSRodney W. Grimes 1076df8bae1dSRodney W. Grimes /* 1077df8bae1dSRodney W. Grimes * Advance snd_nxt over sequence space of this segment. 1078df8bae1dSRodney W. Grimes */ 1079df8bae1dSRodney W. Grimes if (flags & (TH_SYN|TH_FIN)) { 1080df8bae1dSRodney W. Grimes if (flags & TH_SYN) 1081df8bae1dSRodney W. Grimes tp->snd_nxt++; 1082df8bae1dSRodney W. Grimes if (flags & TH_FIN) { 1083df8bae1dSRodney W. Grimes tp->snd_nxt++; 1084df8bae1dSRodney W. Grimes tp->t_flags |= TF_SENTFIN; 1085df8bae1dSRodney W. Grimes } 1086df8bae1dSRodney W. Grimes } 1087a55db2b6SPaul Saab if (sack_rxmit) 10886d90faf3SPaul Saab goto timer; 1089df8bae1dSRodney W. Grimes tp->snd_nxt += len; 1090df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 1091df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt; 1092df8bae1dSRodney W. Grimes /* 1093df8bae1dSRodney W. Grimes * Time this transmission if not a retransmission and 1094df8bae1dSRodney W. Grimes * not currently timing anything. 1095df8bae1dSRodney W. Grimes */ 10969b8b58e0SJonathan Lemon if (tp->t_rtttime == 0) { 10979b8b58e0SJonathan Lemon tp->t_rtttime = ticks; 1098df8bae1dSRodney W. Grimes tp->t_rtseq = startseq; 109978b50714SRobert Watson TCPSTAT_INC(tcps_segstimed); 1100df8bae1dSRodney W. Grimes } 1101df8bae1dSRodney W. Grimes } 1102df8bae1dSRodney W. Grimes 1103df8bae1dSRodney W. Grimes /* 1104df8bae1dSRodney W. Grimes * Set retransmit timer if not currently set, 110528257b5cSMatthew Dillon * and not doing a pure ack or a keep-alive probe. 1106df8bae1dSRodney W. Grimes * Initial value for retransmit timer is smoothed 1107df8bae1dSRodney W. Grimes * round-trip time + 2 * round-trip time variance. 1108df8bae1dSRodney W. Grimes * Initialize shift counter which is used for backoff 1109df8bae1dSRodney W. Grimes * of retransmit time. 1110df8bae1dSRodney W. Grimes */ 11116d90faf3SPaul Saab timer: 11124b8e42baSAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) && 1113a55db2b6SPaul Saab ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 1114a55db2b6SPaul Saab (tp->snd_nxt != tp->snd_una))) { 1115b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_PERSIST)) { 1116b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, 0); 1117df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 1118df8bae1dSRodney W. Grimes } 1119b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 1120df8bae1dSRodney W. Grimes } 112128257b5cSMatthew Dillon } else { 112228257b5cSMatthew Dillon /* 112328257b5cSMatthew Dillon * Persist case, update snd_max but since we are in 112428257b5cSMatthew Dillon * persist mode (no window) we do not update snd_nxt. 112528257b5cSMatthew Dillon */ 112628257b5cSMatthew Dillon int xlen = len; 112728257b5cSMatthew Dillon if (flags & TH_SYN) 112828257b5cSMatthew Dillon ++xlen; 112928257b5cSMatthew Dillon if (flags & TH_FIN) { 113028257b5cSMatthew Dillon ++xlen; 113128257b5cSMatthew Dillon tp->t_flags |= TF_SENTFIN; 113228257b5cSMatthew Dillon } 1133abac41a6SMatthew Dillon if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 1134df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt + len; 113528257b5cSMatthew Dillon } 1136df8bae1dSRodney W. Grimes 1137610ee2f9SDavid Greenman #ifdef TCPDEBUG 1138df8bae1dSRodney W. Grimes /* 1139df8bae1dSRodney W. Grimes * Trace. 1140df8bae1dSRodney W. Grimes */ 114191f467d5SHartmut Brandt if (so->so_options & SO_DEBUG) { 1142f3e0b7efSBruce M Simpson u_short save = 0; 11435214cb3fSBruce M Simpson #ifdef INET6 11445214cb3fSBruce M Simpson if (!isipv6) 11455214cb3fSBruce M Simpson #endif 11465214cb3fSBruce M Simpson { 11475214cb3fSBruce M Simpson save = ipov->ih_len; 114891f467d5SHartmut Brandt ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */); 11495214cb3fSBruce M Simpson } 1150fb59c426SYoshinobu Inoue tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 11515214cb3fSBruce M Simpson #ifdef INET6 11525214cb3fSBruce M Simpson if (!isipv6) 11535214cb3fSBruce M Simpson #endif 115491f467d5SHartmut Brandt ipov->ih_len = save; 115591f467d5SHartmut Brandt } 1156610ee2f9SDavid Greenman #endif 1157df8bae1dSRodney W. Grimes 1158df8bae1dSRodney W. Grimes /* 1159df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 1160df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 1161df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 1162df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 1163df8bae1dSRodney W. Grimes */ 1164fb59c426SYoshinobu Inoue /* 1165fb59c426SYoshinobu Inoue * m->m_pkthdr.len should have been set before cksum calcuration, 1166fb59c426SYoshinobu Inoue * because in6_cksum() need it. 1167fb59c426SYoshinobu Inoue */ 1168fb59c426SYoshinobu Inoue #ifdef INET6 1169fb59c426SYoshinobu Inoue if (isipv6) { 1170fb59c426SYoshinobu Inoue /* 1171fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 1172fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 1173fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 1174fb59c426SYoshinobu Inoue * Neighbor Discovery. 1175fb59c426SYoshinobu Inoue */ 117697d8d152SAndre Oppermann ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1177fb59c426SYoshinobu Inoue 1178fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 1179fb59c426SYoshinobu Inoue error = ip6_output(m, 118097d8d152SAndre Oppermann tp->t_inpcb->in6p_outputopts, NULL, 1181b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? 1182b5d47ff5SJohn-Mark Gurney IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb); 1183fb59c426SYoshinobu Inoue } else 1184fb59c426SYoshinobu Inoue #endif /* INET6 */ 1185df8bae1dSRodney W. Grimes { 1186fb59c426SYoshinobu Inoue ip->ip_len = m->m_pkthdr.len; 1187686cdd19SJun-ichiro itojun Hagino #ifdef INET6 11885cd54324SBjoern A. Zeeb if (tp->t_inpcb->inp_vflag & INP_IPV6PROTO) 118997d8d152SAndre Oppermann ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL); 1190686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 1191f138387aSGarrett Wollman /* 119297d8d152SAndre Oppermann * If we do path MTU discovery, then we set DF on every packet. 119397d8d152SAndre Oppermann * This might not be the best thing to do according to RFC3390 119497d8d152SAndre Oppermann * Section 2. However the tcp hostcache migitates the problem 119597d8d152SAndre Oppermann * so it affects only the first tcp connection with a host. 1196e4e92660SAndre Oppermann * 1197e4e92660SAndre Oppermann * NB: Don't set DF on small MTU/MSS to have a safe fallback. 1198f138387aSGarrett Wollman */ 1199e4e92660SAndre Oppermann if (V_path_mtu_discovery && tp->t_maxopd > V_tcp_minmss) 1200fb59c426SYoshinobu Inoue ip->ip_off |= IP_DF; 120197d8d152SAndre Oppermann 120297d8d152SAndre Oppermann error = ip_output(m, tp->t_inpcb->inp_options, NULL, 1203b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 1204b5d47ff5SJohn-Mark Gurney tp->t_inpcb); 1205df8bae1dSRodney W. Grimes } 1206df8bae1dSRodney W. Grimes if (error) { 12077734ea06SArchie Cobbs 12087734ea06SArchie Cobbs /* 12097734ea06SArchie Cobbs * We know that the packet was lost, so back out the 12107734ea06SArchie Cobbs * sequence number advance, if any. 12112c30ec0aSAndre Oppermann * 12122c30ec0aSAndre Oppermann * If the error is EPERM the packet got blocked by the 12132c30ec0aSAndre Oppermann * local firewall. Normally we should terminate the 12142c30ec0aSAndre Oppermann * connection but the blocking may have been spurious 12152c30ec0aSAndre Oppermann * due to a firewall reconfiguration cycle. So we treat 12162c30ec0aSAndre Oppermann * it like a packet loss and let the retransmit timer and 12172c30ec0aSAndre Oppermann * timeouts do their work over time. 12182c30ec0aSAndre Oppermann * XXX: It is a POLA question whether calling tcp_drop right 12192c30ec0aSAndre Oppermann * away would be the really correct behavior instead. 12207734ea06SArchie Cobbs */ 122172757d9aSGleb Smirnoff if (((tp->t_flags & TF_FORCEDATA) == 0 || 1222b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) && 122372757d9aSGleb Smirnoff ((flags & TH_SYN) == 0) && 122472757d9aSGleb Smirnoff (error != EPERM)) { 12250077b016SPaul Saab if (sack_rxmit) { 12265d3b1b75SJayanth Vijayaraghavan p->rxmit -= len; 12270077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= len; 122872757d9aSGleb Smirnoff KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 12290077b016SPaul Saab ("sackhint bytes rtx >= 0")); 12300077b016SPaul Saab } else 12317734ea06SArchie Cobbs tp->snd_nxt -= len; 12327734ea06SArchie Cobbs } 1233df8bae1dSRodney W. Grimes out: 1234cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */ 123572757d9aSGleb Smirnoff switch (error) { 123672757d9aSGleb Smirnoff case EPERM: 123772757d9aSGleb Smirnoff tp->t_softerror = error; 123872757d9aSGleb Smirnoff return (error); 123972757d9aSGleb Smirnoff case ENOBUFS: 1240b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) && 1241b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) 1242b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 12431600372bSAndre Oppermann tp->snd_cwnd = tp->t_maxseg; 1244df8bae1dSRodney W. Grimes return (0); 124572757d9aSGleb Smirnoff case EMSGSIZE: 12463d1f141bSGarrett Wollman /* 1247b3c0f300SAndre Oppermann * For some reason the interface we used initially 1248b3c0f300SAndre Oppermann * to send segments changed to another or lowered 1249b3c0f300SAndre Oppermann * its MTU. 1250b3c0f300SAndre Oppermann * 1251b3c0f300SAndre Oppermann * tcp_mtudisc() will find out the new MTU and as 1252b3c0f300SAndre Oppermann * its last action, initiate retransmission, so it 1253b3c0f300SAndre Oppermann * is important to not do so here. 1254b3c0f300SAndre Oppermann * 1255b3c0f300SAndre Oppermann * If TSO was active we either got an interface 1256b3c0f300SAndre Oppermann * without TSO capabilits or TSO was turned off. 1257b3c0f300SAndre Oppermann * Disable it for this connection as too and 1258b3c0f300SAndre Oppermann * immediatly retry with MSS sized segments generated 1259b3c0f300SAndre Oppermann * by this function. 12603d1f141bSGarrett Wollman */ 1261b3c0f300SAndre Oppermann if (tso) 1262b3c0f300SAndre Oppermann tp->t_flags &= ~TF_TSO; 12633d1f141bSGarrett Wollman tcp_mtudisc(tp->t_inpcb, 0); 126472757d9aSGleb Smirnoff return (0); 12658bec3467SGleb Smirnoff case EHOSTDOWN: 126672757d9aSGleb Smirnoff case EHOSTUNREACH: 126772757d9aSGleb Smirnoff case ENETDOWN: 12688bec3467SGleb Smirnoff case ENETUNREACH: 126972757d9aSGleb Smirnoff if (TCPS_HAVERCVDSYN(tp->t_state)) { 1270df8bae1dSRodney W. Grimes tp->t_softerror = error; 1271df8bae1dSRodney W. Grimes return (0); 1272df8bae1dSRodney W. Grimes } 127372757d9aSGleb Smirnoff /* FALLTHROUGH */ 127472757d9aSGleb Smirnoff default: 1275df8bae1dSRodney W. Grimes return (error); 1276df8bae1dSRodney W. Grimes } 127772757d9aSGleb Smirnoff } 127878b50714SRobert Watson TCPSTAT_INC(tcps_sndtotal); 1279df8bae1dSRodney W. Grimes 1280df8bae1dSRodney W. Grimes /* 1281df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 1282df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 1283df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 1284df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 1285df8bae1dSRodney W. Grimes */ 1286201d185bSAndre Oppermann if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1287201d185bSAndre Oppermann tp->rcv_adv = tp->rcv_nxt + recwin; 1288df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 12893bfd6421SJonathan Lemon tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 1290b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_DELACK)) 1291b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 0); 1292d912c694SMatthew Dillon #if 0 1293d912c694SMatthew Dillon /* 1294d912c694SMatthew Dillon * This completely breaks TCP if newreno is turned on. What happens 1295d912c694SMatthew Dillon * is that if delayed-acks are turned on on the receiver, this code 1296d912c694SMatthew Dillon * on the transmitter effectively destroys the TCP window, forcing 1297d912c694SMatthew Dillon * it to four packets (1.5Kx4 = 6K window). 1298d912c694SMatthew Dillon */ 1299dbc42409SLawrence Stewart if (sendalot && --maxburst) 1300df8bae1dSRodney W. Grimes goto again; 1301d912c694SMatthew Dillon #endif 1302d912c694SMatthew Dillon if (sendalot) 1303d912c694SMatthew Dillon goto again; 1304df8bae1dSRodney W. Grimes return (0); 1305df8bae1dSRodney W. Grimes } 1306df8bae1dSRodney W. Grimes 1307df8bae1dSRodney W. Grimes void 1308ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp) 1309df8bae1dSRodney W. Grimes { 13109b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 13119b8b58e0SJonathan Lemon int tt; 1312df8bae1dSRodney W. Grimes 1313b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_REXMT)) 13149b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 1315df8bae1dSRodney W. Grimes /* 1316df8bae1dSRodney W. Grimes * Start/restart persistance timer. 1317df8bae1dSRodney W. Grimes */ 13189b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 1319df8bae1dSRodney W. Grimes TCPTV_PERSMIN, TCPTV_PERSMAX); 1320b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, tt); 1321df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1322df8bae1dSRodney W. Grimes tp->t_rxtshift++; 1323df8bae1dSRodney W. Grimes } 132402a1a643SAndre Oppermann 132502a1a643SAndre Oppermann /* 132602a1a643SAndre Oppermann * Insert TCP options according to the supplied parameters to the place 132702a1a643SAndre Oppermann * optp in a consistent way. Can handle unaligned destinations. 132802a1a643SAndre Oppermann * 132902a1a643SAndre Oppermann * The order of the option processing is crucial for optimal packing and 133002a1a643SAndre Oppermann * alignment for the scarce option space. 133102a1a643SAndre Oppermann * 133202a1a643SAndre Oppermann * The optimal order for a SYN/SYN-ACK segment is: 133302a1a643SAndre Oppermann * MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) + 133402a1a643SAndre Oppermann * Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40. 133502a1a643SAndre Oppermann * 133602a1a643SAndre Oppermann * The SACK options should be last. SACK blocks consume 8*n+2 bytes. 133702a1a643SAndre Oppermann * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks). 133802a1a643SAndre Oppermann * At minimum we need 10 bytes (to generate 1 SACK block). If both 133902a1a643SAndre Oppermann * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present, 134002a1a643SAndre Oppermann * we only have 10 bytes for SACK options (40 - (12 + 18)). 134102a1a643SAndre Oppermann */ 134202a1a643SAndre Oppermann int 134302a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp) 134402a1a643SAndre Oppermann { 134502a1a643SAndre Oppermann u_int mask, optlen = 0; 134602a1a643SAndre Oppermann 134702a1a643SAndre Oppermann for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) { 134802a1a643SAndre Oppermann if ((to->to_flags & mask) != mask) 134902a1a643SAndre Oppermann continue; 13503a4018c4SAndre Oppermann if (optlen == TCP_MAXOLEN) 13513a4018c4SAndre Oppermann break; 135202a1a643SAndre Oppermann switch (to->to_flags & mask) { 135302a1a643SAndre Oppermann case TOF_MSS: 135402a1a643SAndre Oppermann while (optlen % 4) { 135502a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 135602a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 135702a1a643SAndre Oppermann } 13583a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG) 13593a4018c4SAndre Oppermann continue; 136002a1a643SAndre Oppermann optlen += TCPOLEN_MAXSEG; 136102a1a643SAndre Oppermann *optp++ = TCPOPT_MAXSEG; 136202a1a643SAndre Oppermann *optp++ = TCPOLEN_MAXSEG; 136302a1a643SAndre Oppermann to->to_mss = htons(to->to_mss); 136402a1a643SAndre Oppermann bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss)); 136502a1a643SAndre Oppermann optp += sizeof(to->to_mss); 136602a1a643SAndre Oppermann break; 136702a1a643SAndre Oppermann case TOF_SCALE: 136802a1a643SAndre Oppermann while (!optlen || optlen % 2 != 1) { 136902a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 137002a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 137102a1a643SAndre Oppermann } 13723a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW) 13733a4018c4SAndre Oppermann continue; 137402a1a643SAndre Oppermann optlen += TCPOLEN_WINDOW; 137502a1a643SAndre Oppermann *optp++ = TCPOPT_WINDOW; 137602a1a643SAndre Oppermann *optp++ = TCPOLEN_WINDOW; 137702a1a643SAndre Oppermann *optp++ = to->to_wscale; 137802a1a643SAndre Oppermann break; 137902a1a643SAndre Oppermann case TOF_SACKPERM: 138002a1a643SAndre Oppermann while (optlen % 2) { 138102a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 138202a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 138302a1a643SAndre Oppermann } 13843a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED) 13853a4018c4SAndre Oppermann continue; 138602a1a643SAndre Oppermann optlen += TCPOLEN_SACK_PERMITTED; 138702a1a643SAndre Oppermann *optp++ = TCPOPT_SACK_PERMITTED; 138802a1a643SAndre Oppermann *optp++ = TCPOLEN_SACK_PERMITTED; 138902a1a643SAndre Oppermann break; 139002a1a643SAndre Oppermann case TOF_TS: 139102a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 139202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 139302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 139402a1a643SAndre Oppermann } 13953a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP) 13963a4018c4SAndre Oppermann continue; 139702a1a643SAndre Oppermann optlen += TCPOLEN_TIMESTAMP; 139802a1a643SAndre Oppermann *optp++ = TCPOPT_TIMESTAMP; 139902a1a643SAndre Oppermann *optp++ = TCPOLEN_TIMESTAMP; 140002a1a643SAndre Oppermann to->to_tsval = htonl(to->to_tsval); 140102a1a643SAndre Oppermann to->to_tsecr = htonl(to->to_tsecr); 140202a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval)); 140302a1a643SAndre Oppermann optp += sizeof(to->to_tsval); 140402a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr)); 140502a1a643SAndre Oppermann optp += sizeof(to->to_tsecr); 140602a1a643SAndre Oppermann break; 140702a1a643SAndre Oppermann case TOF_SIGNATURE: 140802a1a643SAndre Oppermann { 140902a1a643SAndre Oppermann int siglen = TCPOLEN_SIGNATURE - 2; 141002a1a643SAndre Oppermann 141102a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 141202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 141302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 141402a1a643SAndre Oppermann } 14150d957bbaSAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE) 141602a1a643SAndre Oppermann continue; 141702a1a643SAndre Oppermann optlen += TCPOLEN_SIGNATURE; 141802a1a643SAndre Oppermann *optp++ = TCPOPT_SIGNATURE; 141902a1a643SAndre Oppermann *optp++ = TCPOLEN_SIGNATURE; 142002a1a643SAndre Oppermann to->to_signature = optp; 142102a1a643SAndre Oppermann while (siglen--) 142202a1a643SAndre Oppermann *optp++ = 0; 142302a1a643SAndre Oppermann break; 142402a1a643SAndre Oppermann } 142502a1a643SAndre Oppermann case TOF_SACK: 142602a1a643SAndre Oppermann { 142702a1a643SAndre Oppermann int sackblks = 0; 142802a1a643SAndre Oppermann struct sackblk *sack = (struct sackblk *)to->to_sacks; 142902a1a643SAndre Oppermann tcp_seq sack_seq; 143002a1a643SAndre Oppermann 143102a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 143202a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 143302a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 143402a1a643SAndre Oppermann } 14353a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK) 143602a1a643SAndre Oppermann continue; 143702a1a643SAndre Oppermann optlen += TCPOLEN_SACKHDR; 143802a1a643SAndre Oppermann *optp++ = TCPOPT_SACK; 143902a1a643SAndre Oppermann sackblks = min(to->to_nsacks, 14400d957bbaSAndre Oppermann (TCP_MAXOLEN - optlen) / TCPOLEN_SACK); 144102a1a643SAndre Oppermann *optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK; 144202a1a643SAndre Oppermann while (sackblks--) { 144302a1a643SAndre Oppermann sack_seq = htonl(sack->start); 144402a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 144502a1a643SAndre Oppermann optp += sizeof(sack_seq); 144602a1a643SAndre Oppermann sack_seq = htonl(sack->end); 144702a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 144802a1a643SAndre Oppermann optp += sizeof(sack_seq); 144902a1a643SAndre Oppermann optlen += TCPOLEN_SACK; 145002a1a643SAndre Oppermann sack++; 145102a1a643SAndre Oppermann } 145278b50714SRobert Watson TCPSTAT_INC(tcps_sack_send_blocks); 145302a1a643SAndre Oppermann break; 145402a1a643SAndre Oppermann } 145502a1a643SAndre Oppermann default: 145602a1a643SAndre Oppermann panic("%s: unknown TCP option type", __func__); 145702a1a643SAndre Oppermann break; 145802a1a643SAndre Oppermann } 145902a1a643SAndre Oppermann } 146002a1a643SAndre Oppermann 146102a1a643SAndre Oppermann /* Terminate and pad TCP options to a 4 byte boundary. */ 146202a1a643SAndre Oppermann if (optlen % 4) { 146302a1a643SAndre Oppermann optlen += TCPOLEN_EOL; 146402a1a643SAndre Oppermann *optp++ = TCPOPT_EOL; 146502a1a643SAndre Oppermann } 1466413deb12SBjoern A. Zeeb /* 1467413deb12SBjoern A. Zeeb * According to RFC 793 (STD0007): 1468413deb12SBjoern A. Zeeb * "The content of the header beyond the End-of-Option option 1469413deb12SBjoern A. Zeeb * must be header padding (i.e., zero)." 1470413deb12SBjoern A. Zeeb * and later: "The padding is composed of zeros." 1471413deb12SBjoern A. Zeeb */ 147202a1a643SAndre Oppermann while (optlen % 4) { 1473c343c524SAndre Oppermann optlen += TCPOLEN_PAD; 1474c343c524SAndre Oppermann *optp++ = TCPOPT_PAD; 147502a1a643SAndre Oppermann } 147602a1a643SAndre Oppermann 14770d957bbaSAndre Oppermann KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__)); 147802a1a643SAndre Oppermann return (optlen); 147902a1a643SAndre Oppermann } 1480