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> 4339bc9de5SLawrence Stewart #include <sys/hhook.h> 44fb919e4dSMark Murray #include <sys/kernel.h> 45fb919e4dSMark Murray #include <sys/lock.h> 46fb919e4dSMark Murray #include <sys/mbuf.h> 47fb919e4dSMark Murray #include <sys/mutex.h> 48df8bae1dSRodney W. Grimes #include <sys/protosw.h> 4957f60867SMark Johnston #include <sys/sdt.h> 50df8bae1dSRodney W. Grimes #include <sys/socket.h> 51df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 52fb919e4dSMark Murray #include <sys/sysctl.h> 53df8bae1dSRodney W. Grimes 544b79449eSBjoern A. Zeeb #include <net/if.h> 55df8bae1dSRodney W. Grimes #include <net/route.h> 56530c0060SRobert Watson #include <net/vnet.h> 57df8bae1dSRodney W. Grimes 58dbc42409SLawrence Stewart #include <netinet/cc.h> 59df8bae1dSRodney W. Grimes #include <netinet/in.h> 6057f60867SMark Johnston #include <netinet/in_kdtrace.h> 61df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 62df8bae1dSRodney W. Grimes #include <netinet/ip.h> 63df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 64df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 65ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 66fb59c426SYoshinobu Inoue #ifdef INET6 67686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 68686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 69fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 70fb59c426SYoshinobu Inoue #endif 71df8bae1dSRodney W. Grimes #define TCPOUTFLAGS 72df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 73df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 74df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 75df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 76df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 77610ee2f9SDavid Greenman #ifdef TCPDEBUG 78df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 79610ee2f9SDavid Greenman #endif 8009fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 8109fe6320SNavdeep Parhar #include <netinet/tcp_offload.h> 8209fe6320SNavdeep Parhar #endif 83df8bae1dSRodney W. Grimes 84b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 85b9234fafSSam Leffler #include <netipsec/ipsec.h> 86b2630c29SGeorge V. Neville-Neil #endif /*IPSEC*/ 87b9234fafSSam Leffler 88db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 89db4f9cc7SJonathan Lemon 90aed55708SRobert Watson #include <security/mac/mac_framework.h> 91aed55708SRobert Watson 9282cea7e6SBjoern A. Zeeb VNET_DEFINE(int, path_mtu_discovery) = 1; 936df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_VNET | CTLFLAG_RW, 94eddfbb76SRobert Watson &VNET_NAME(path_mtu_discovery), 1, 95eddfbb76SRobert Watson "Enable Path MTU Discovery"); 969a039a5fSDavid Greenman 9782cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_tso) = 1; 9882cea7e6SBjoern A. Zeeb #define V_tcp_do_tso VNET(tcp_do_tso) 996df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_VNET | CTLFLAG_RW, 100eddfbb76SRobert Watson &VNET_NAME(tcp_do_tso), 0, 101eddfbb76SRobert Watson "Enable TCP Segmentation Offload"); 102b3c0f300SAndre Oppermann 103873789cbSAndre Oppermann VNET_DEFINE(int, tcp_sendspace) = 1024*32; 104873789cbSAndre Oppermann #define V_tcp_sendspace VNET(tcp_sendspace) 1056df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_VNET | CTLFLAG_RW, 106873789cbSAndre Oppermann &VNET_NAME(tcp_sendspace), 0, "Initial send socket buffer size"); 107873789cbSAndre Oppermann 10882cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_do_autosndbuf) = 1; 10982cea7e6SBjoern A. Zeeb #define V_tcp_do_autosndbuf VNET(tcp_do_autosndbuf) 1106df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_auto, CTLFLAG_VNET | CTLFLAG_RW, 111eddfbb76SRobert Watson &VNET_NAME(tcp_do_autosndbuf), 0, 112eddfbb76SRobert Watson "Enable automatic send buffer sizing"); 1136741ecf5SAndre Oppermann 11482cea7e6SBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_inc) = 8*1024; 11582cea7e6SBjoern A. Zeeb #define V_tcp_autosndbuf_inc VNET(tcp_autosndbuf_inc) 1166df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_inc, CTLFLAG_VNET | CTLFLAG_RW, 117eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_inc), 0, 1188b615593SMarko Zec "Incrementor step size of automatic send buffer"); 1196741ecf5SAndre Oppermann 120b233773bSBjoern A. Zeeb VNET_DEFINE(int, tcp_autosndbuf_max) = 2*1024*1024; 12182cea7e6SBjoern A. Zeeb #define V_tcp_autosndbuf_max VNET(tcp_autosndbuf_max) 1226df8a710SGleb Smirnoff SYSCTL_INT(_net_inet_tcp, OID_AUTO, sendbuf_max, CTLFLAG_VNET | CTLFLAG_RW, 123eddfbb76SRobert Watson &VNET_NAME(tcp_autosndbuf_max), 0, 1248b615593SMarko Zec "Max size of automatic send buffer"); 1256741ecf5SAndre Oppermann 12639bc9de5SLawrence Stewart static void inline hhook_run_tcp_est_out(struct tcpcb *tp, 12739bc9de5SLawrence Stewart struct tcphdr *th, struct tcpopt *to, 12839bc9de5SLawrence Stewart long len, int tso); 129dbc42409SLawrence Stewart static void inline cc_after_idle(struct tcpcb *tp); 130dbc42409SLawrence Stewart 131dbc42409SLawrence Stewart /* 1320f5e7edcSKevin Lo * Wrapper for the TCP established output helper hook. 13339bc9de5SLawrence Stewart */ 13439bc9de5SLawrence Stewart static void inline 13539bc9de5SLawrence Stewart hhook_run_tcp_est_out(struct tcpcb *tp, struct tcphdr *th, 13639bc9de5SLawrence Stewart struct tcpopt *to, long len, int tso) 13739bc9de5SLawrence Stewart { 13839bc9de5SLawrence Stewart struct tcp_hhook_data hhook_data; 13939bc9de5SLawrence Stewart 14039bc9de5SLawrence Stewart if (V_tcp_hhh[HHOOK_TCP_EST_OUT]->hhh_nhooks > 0) { 14139bc9de5SLawrence Stewart hhook_data.tp = tp; 14239bc9de5SLawrence Stewart hhook_data.th = th; 14339bc9de5SLawrence Stewart hhook_data.to = to; 14439bc9de5SLawrence Stewart hhook_data.len = len; 14539bc9de5SLawrence Stewart hhook_data.tso = tso; 14639bc9de5SLawrence Stewart 14739bc9de5SLawrence Stewart hhook_run_hooks(V_tcp_hhh[HHOOK_TCP_EST_OUT], &hhook_data, 14839bc9de5SLawrence Stewart tp->osd); 14939bc9de5SLawrence Stewart } 15039bc9de5SLawrence Stewart } 15139bc9de5SLawrence Stewart 15239bc9de5SLawrence Stewart /* 153dbc42409SLawrence Stewart * CC wrapper hook functions 154dbc42409SLawrence Stewart */ 155dbc42409SLawrence Stewart static void inline 156dbc42409SLawrence Stewart cc_after_idle(struct tcpcb *tp) 157dbc42409SLawrence Stewart { 158dbc42409SLawrence Stewart INP_WLOCK_ASSERT(tp->t_inpcb); 159dbc42409SLawrence Stewart 160dbc42409SLawrence Stewart if (CC_ALGO(tp)->after_idle != NULL) 161dbc42409SLawrence Stewart CC_ALGO(tp)->after_idle(tp->ccv); 162dbc42409SLawrence Stewart } 1636741ecf5SAndre Oppermann 164df8bae1dSRodney W. Grimes /* 165df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 166df8bae1dSRodney W. Grimes */ 167df8bae1dSRodney W. Grimes int 168f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp) 169df8bae1dSRodney W. Grimes { 170f10e85d7SLuigi Rizzo struct socket *so = tp->t_inpcb->inp_socket; 171201d185bSAndre Oppermann long len, recwin, sendwin; 172b287c6c7SBjoern A. Zeeb int off, flags, error = 0; /* Keep compiler happy */ 173f10e85d7SLuigi Rizzo struct mbuf *m; 174fb59c426SYoshinobu Inoue struct ip *ip = NULL; 175f10e85d7SLuigi Rizzo struct ipovly *ipov = NULL; 176f10e85d7SLuigi Rizzo struct tcphdr *th; 177a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 178a04884fcSBill Fenner unsigned ipoptlen, optlen, hdrlen; 1799ad0173dSBjoern A. Zeeb #ifdef IPSEC 1809ad0173dSBjoern A. Zeeb unsigned ipsec_optlen = 0; 1819ad0173dSBjoern A. Zeeb #endif 182df8bae1dSRodney W. Grimes int idle, sendalot; 18302a1a643SAndre Oppermann int sack_rxmit, sack_bytes_rxmt; 1846d90faf3SPaul Saab struct sackhole *p; 185df0633a1SGleb Smirnoff int tso, mtu; 18602a1a643SAndre Oppermann struct tcpopt to; 187262c1c1aSMatthew Dillon #if 0 18846f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 189262c1c1aSMatthew Dillon #endif 190fb59c426SYoshinobu Inoue #ifdef INET6 191f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 192fb59c426SYoshinobu Inoue int isipv6; 193fb59c426SYoshinobu Inoue 194fb59c426SYoshinobu Inoue isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 195fb59c426SYoshinobu Inoue #endif 196df8bae1dSRodney W. Grimes 1978501a69cSRobert Watson INP_WLOCK_ASSERT(tp->t_inpcb); 1983d6ade3aSJennifer Yang 19909fe6320SNavdeep Parhar #ifdef TCP_OFFLOAD 20009fe6320SNavdeep Parhar if (tp->t_flags & TF_TOE) 20109fe6320SNavdeep Parhar return (tcp_offload_output(tp)); 20209fe6320SNavdeep Parhar #endif 20309fe6320SNavdeep Parhar 204df8bae1dSRodney W. Grimes /* 205df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 206df8bae1dSRodney W. Grimes * and flags that will be used. 207df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 208df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 209df8bae1dSRodney W. Grimes */ 210c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 2112ea8da28SLawrence Stewart if (idle && ticks - tp->t_rcvtime >= tp->t_rxtcur) 2122ea8da28SLawrence Stewart cc_after_idle(tp); 213c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 214c24d5daeSJayanth Vijayaraghavan if (idle) { 215c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 216c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 217c24d5daeSJayanth Vijayaraghavan idle = 0; 218c24d5daeSJayanth Vijayaraghavan } 219c24d5daeSJayanth Vijayaraghavan } 220df8bae1dSRodney W. Grimes again: 2216d90faf3SPaul Saab /* 2226d90faf3SPaul Saab * If we've recently taken a timeout, snd_max will be greater than 2236d90faf3SPaul Saab * snd_nxt. There may be SACK information that allows us to avoid 2246d90faf3SPaul Saab * resending already delivered data. Adjust snd_nxt accordingly. 2256d90faf3SPaul Saab */ 2263529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 2273529149eSAndre Oppermann SEQ_LT(tp->snd_nxt, tp->snd_max)) 2286d90faf3SPaul Saab tcp_sack_adjust(tp); 229df8bae1dSRodney W. Grimes sendalot = 0; 230153e5b57SAndre Oppermann tso = 0; 231df0633a1SGleb Smirnoff mtu = 0; 232df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 233201d185bSAndre Oppermann sendwin = min(tp->snd_wnd, tp->snd_cwnd); 234df8bae1dSRodney W. Grimes 235df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 236a0292f23SGarrett Wollman /* 2376d90faf3SPaul Saab * Send any SACK-generated retransmissions. If we're explicitly trying 2386d90faf3SPaul Saab * to send out new data (when sendalot is 1), bypass this function. 2396d90faf3SPaul Saab * If we retransmit in fast recovery mode, decrement snd_cwnd, since 2406d90faf3SPaul Saab * we're replacing a (future) new transmission with a retransmission 2416d90faf3SPaul Saab * now, and we previously incremented snd_cwnd in tcp_input(). 2426d90faf3SPaul Saab */ 2436d90faf3SPaul Saab /* 2446d90faf3SPaul Saab * Still in sack recovery , reset rxmit flag to zero. 2456d90faf3SPaul Saab */ 2466d90faf3SPaul Saab sack_rxmit = 0; 247a55db2b6SPaul Saab sack_bytes_rxmt = 0; 2486d90faf3SPaul Saab len = 0; 2496d90faf3SPaul Saab p = NULL; 250dbc42409SLawrence Stewart if ((tp->t_flags & TF_SACK_PERMIT) && IN_FASTRECOVERY(tp->t_flags) && 251a55db2b6SPaul Saab (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 252a55db2b6SPaul Saab long cwin; 253a55db2b6SPaul Saab 254a55db2b6SPaul Saab cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt; 255a55db2b6SPaul Saab if (cwin < 0) 256a55db2b6SPaul Saab cwin = 0; 257f787edd8SJayanth Vijayaraghavan /* Do not retransmit SACK segments beyond snd_recover */ 258f787edd8SJayanth Vijayaraghavan if (SEQ_GT(p->end, tp->snd_recover)) { 259f787edd8SJayanth Vijayaraghavan /* 260f787edd8SJayanth Vijayaraghavan * (At least) part of sack hole extends beyond 261f787edd8SJayanth Vijayaraghavan * snd_recover. Check to see if we can rexmit data 262f787edd8SJayanth Vijayaraghavan * for this hole. 263f787edd8SJayanth Vijayaraghavan */ 264f787edd8SJayanth Vijayaraghavan if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 265f787edd8SJayanth Vijayaraghavan /* 266f787edd8SJayanth Vijayaraghavan * Can't rexmit any more data for this hole. 267f787edd8SJayanth Vijayaraghavan * That data will be rexmitted in the next 268f787edd8SJayanth Vijayaraghavan * sack recovery episode, when snd_recover 269f787edd8SJayanth Vijayaraghavan * moves past p->rxmit. 270f787edd8SJayanth Vijayaraghavan */ 271f787edd8SJayanth Vijayaraghavan p = NULL; 272f787edd8SJayanth Vijayaraghavan goto after_sack_rexmit; 273f787edd8SJayanth Vijayaraghavan } else 274f787edd8SJayanth Vijayaraghavan /* Can rexmit part of the current hole */ 275a55db2b6SPaul Saab len = ((long)ulmin(cwin, 276f787edd8SJayanth Vijayaraghavan tp->snd_recover - p->rxmit)); 277f787edd8SJayanth Vijayaraghavan } else 278a55db2b6SPaul Saab len = ((long)ulmin(cwin, p->end - p->rxmit)); 2796d90faf3SPaul Saab off = p->rxmit - tp->snd_una; 280f787edd8SJayanth Vijayaraghavan KASSERT(off >= 0,("%s: sack block to the left of una : %d", 281f787edd8SJayanth Vijayaraghavan __func__, off)); 2826d90faf3SPaul Saab if (len > 0) { 283ab5c14d8SRobert Watson sack_rxmit = 1; 284ab5c14d8SRobert Watson sendalot = 1; 28578b50714SRobert Watson TCPSTAT_INC(tcps_sack_rexmits); 28678b50714SRobert Watson TCPSTAT_ADD(tcps_sack_rexmit_bytes, 28778b50714SRobert Watson min(len, tp->t_maxseg)); 2886d90faf3SPaul Saab } 2896d90faf3SPaul Saab } 290f787edd8SJayanth Vijayaraghavan after_sack_rexmit: 2916d90faf3SPaul Saab /* 292a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 293a0292f23SGarrett Wollman * state flags. 294a0292f23SGarrett Wollman */ 295a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 296a0292f23SGarrett Wollman flags |= TH_FIN; 297a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 298a0292f23SGarrett Wollman flags |= TH_SYN; 299a0292f23SGarrett Wollman 300cf2942b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 301df8bae1dSRodney W. Grimes /* 302df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 303df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 304df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 305df8bae1dSRodney W. Grimes * and go to transmit state. 306df8bae1dSRodney W. Grimes */ 3072cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) { 308201d185bSAndre Oppermann if (sendwin == 0) { 309df8bae1dSRodney W. Grimes /* 310df8bae1dSRodney W. Grimes * If we still have some data to send, then 311df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 312df8bae1dSRodney W. Grimes * happen below when it realizes that we 313df8bae1dSRodney W. Grimes * aren't sending all the data. However, 31429089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 315df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 316df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 317df8bae1dSRodney W. Grimes * up sending the packet without recording 318df8bae1dSRodney W. Grimes * that we sent the FIN bit. 319df8bae1dSRodney W. Grimes * 320df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 321df8bae1dSRodney W. Grimes * because if we don't have any more data 322df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 323df8bae1dSRodney W. Grimes * itself. 324df8bae1dSRodney W. Grimes */ 325cfa6009eSGleb Smirnoff if (off < sbused(&so->so_snd)) 326df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 327201d185bSAndre Oppermann sendwin = 1; 328df8bae1dSRodney W. Grimes } else { 329b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, 0); 330df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 331df8bae1dSRodney W. Grimes } 332df8bae1dSRodney W. Grimes } 333df8bae1dSRodney W. Grimes 33428257b5cSMatthew Dillon /* 33528257b5cSMatthew Dillon * If snd_nxt == snd_max and we have transmitted a FIN, the 33628257b5cSMatthew Dillon * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 337201d185bSAndre Oppermann * a negative length. This can also occur when TCP opens up 33828257b5cSMatthew Dillon * its congestion window while receiving additional duplicate 33928257b5cSMatthew Dillon * acks after fast-retransmit because TCP will reset snd_nxt 34028257b5cSMatthew Dillon * to snd_max after the fast-retransmit. 34128257b5cSMatthew Dillon * 34228257b5cSMatthew Dillon * In the normal retransmit-FIN-only case, however, snd_nxt will 34328257b5cSMatthew Dillon * be set to snd_una, the offset will be 0, and the length may 34428257b5cSMatthew Dillon * wind up 0. 3456d90faf3SPaul Saab * 3466d90faf3SPaul Saab * If sack_rxmit is true we are retransmitting from the scoreboard 3476d90faf3SPaul Saab * in which case len is already set. 34828257b5cSMatthew Dillon */ 349a55db2b6SPaul Saab if (sack_rxmit == 0) { 350a55db2b6SPaul Saab if (sack_bytes_rxmt == 0) 351cfa6009eSGleb Smirnoff len = ((long)ulmin(sbavail(&so->so_snd), sendwin) - 352cfa6009eSGleb Smirnoff off); 353a55db2b6SPaul Saab else { 354a55db2b6SPaul Saab long cwin; 355a55db2b6SPaul Saab 356a55db2b6SPaul Saab /* 357a55db2b6SPaul Saab * We are inside of a SACK recovery episode and are 358a55db2b6SPaul Saab * sending new data, having retransmitted all the 359a55db2b6SPaul Saab * data possible in the scoreboard. 360a55db2b6SPaul Saab */ 361cfa6009eSGleb Smirnoff len = ((long)ulmin(sbavail(&so->so_snd), tp->snd_wnd) - 362cfa6009eSGleb Smirnoff off); 3638d03f2b5SPaul Saab /* 3648d03f2b5SPaul Saab * Don't remove this (len > 0) check ! 3658d03f2b5SPaul Saab * We explicitly check for len > 0 here (although it 3668d03f2b5SPaul Saab * isn't really necessary), to work around a gcc 3678d03f2b5SPaul Saab * optimization issue - to force gcc to compute 3688d03f2b5SPaul Saab * len above. Without this check, the computation 3698d03f2b5SPaul Saab * of len is bungled by the optimizer. 3708d03f2b5SPaul Saab */ 3718d03f2b5SPaul Saab if (len > 0) { 3727d5ed1ceSPaul Saab cwin = tp->snd_cwnd - 3737d5ed1ceSPaul Saab (tp->snd_nxt - tp->sack_newdata) - 374a55db2b6SPaul Saab sack_bytes_rxmt; 375a55db2b6SPaul Saab if (cwin < 0) 376a55db2b6SPaul Saab cwin = 0; 377a55db2b6SPaul Saab len = lmin(len, cwin); 378a55db2b6SPaul Saab } 379a55db2b6SPaul Saab } 3808d03f2b5SPaul Saab } 381a0292f23SGarrett Wollman 382a0292f23SGarrett Wollman /* 383a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 384a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 385a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 386a0292f23SGarrett Wollman */ 387a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 3884b8e98d6SQing Li if (tp->t_state != TCPS_SYN_RECEIVED) 389a0292f23SGarrett Wollman flags &= ~TH_SYN; 390a0292f23SGarrett Wollman off--, len++; 391a0292f23SGarrett Wollman } 392a0292f23SGarrett Wollman 39381165e48SAndras Olah /* 394c94c54e4SAndre Oppermann * Be careful not to send data and/or FIN on SYN segments. 39581165e48SAndras Olah * This measure is needed to prevent interoperability problems 39681165e48SAndras Olah * with not fully conformant TCP implementations. 39781165e48SAndras Olah */ 398c94c54e4SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 39981165e48SAndras Olah len = 0; 40081165e48SAndras Olah flags &= ~TH_FIN; 40181165e48SAndras Olah } 40281165e48SAndras Olah 403*47a8e865SXin LI if (len <= 0) { 404df8bae1dSRodney W. Grimes /* 405df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 406df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 40728257b5cSMatthew Dillon * len will be < 0. Otherwise, window shrank 408df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 4092d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 4102d8266afSDavid Greenman * to (closed) window, and set the persist timer 4112d8266afSDavid Greenman * if it isn't already going. If the window didn't 4122d8266afSDavid Greenman * close completely, just wait for an ACK. 413*47a8e865SXin LI * 414*47a8e865SXin LI * We also do a general check here to ensure that 415*47a8e865SXin LI * we will set the persist timer when we have data 416*47a8e865SXin LI * to send, but a 0-byte window. This makes sure 417*47a8e865SXin LI * the persist timer is set even if the packet 418*47a8e865SXin LI * hits one of the "goto send" lines below. 419df8bae1dSRodney W. Grimes */ 420df8bae1dSRodney W. Grimes len = 0; 421*47a8e865SXin LI if ((sendwin == 0) && (TCPS_HAVEESTABLISHED(tp->t_state)) && 422*47a8e865SXin LI (off < (int) sbavail(&so->so_snd))) { 423b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, 0); 4242d8266afSDavid Greenman tp->t_rxtshift = 0; 425df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 426b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_PERSIST)) 4272d8266afSDavid Greenman tcp_setpersist(tp); 428df8bae1dSRodney W. Grimes } 429df8bae1dSRodney W. Grimes } 43028257b5cSMatthew Dillon 4316741ecf5SAndre Oppermann /* len will be >= 0 after this point. */ 432c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 4336741ecf5SAndre Oppermann 43428257b5cSMatthew Dillon /* 4356741ecf5SAndre Oppermann * Automatic sizing of send socket buffer. Often the send buffer 4366741ecf5SAndre Oppermann * size is not optimally adjusted to the actual network conditions 4376741ecf5SAndre Oppermann * at hand (delay bandwidth product). Setting the buffer size too 4386741ecf5SAndre Oppermann * small limits throughput on links with high bandwidth and high 4396741ecf5SAndre Oppermann * delay (eg. trans-continental/oceanic links). Setting the 4406741ecf5SAndre Oppermann * buffer size too big consumes too much real kernel memory, 4416741ecf5SAndre Oppermann * especially with many connections on busy servers. 4426741ecf5SAndre Oppermann * 4436741ecf5SAndre Oppermann * The criteria to step up the send buffer one notch are: 4446741ecf5SAndre Oppermann * 1. receive window of remote host is larger than send buffer 4456741ecf5SAndre Oppermann * (with a fudge factor of 5/4th); 4466741ecf5SAndre Oppermann * 2. send buffer is filled to 7/8th with data (so we actually 4476741ecf5SAndre Oppermann * have data to make use of it); 4486741ecf5SAndre Oppermann * 3. send buffer fill has not hit maximal automatic size; 4496741ecf5SAndre Oppermann * 4. our send window (slow start and cogestion controlled) is 4506741ecf5SAndre Oppermann * larger than sent but unacknowledged data in send buffer. 4516741ecf5SAndre Oppermann * 4526741ecf5SAndre Oppermann * The remote host receive window scaling factor may limit the 4536741ecf5SAndre Oppermann * growing of the send buffer before it reaches its allowed 4546741ecf5SAndre Oppermann * maximum. 4556741ecf5SAndre Oppermann * 4566741ecf5SAndre Oppermann * It scales directly with slow start or congestion window 4576741ecf5SAndre Oppermann * and does at most one step per received ACK. This fast 4586741ecf5SAndre Oppermann * scaling has the drawback of growing the send buffer beyond 4596741ecf5SAndre Oppermann * what is strictly necessary to make full use of a given 4606741ecf5SAndre Oppermann * delay*bandwith product. However testing has shown this not 4616741ecf5SAndre Oppermann * to be much of an problem. At worst we are trading wasting 4626741ecf5SAndre Oppermann * of available bandwith (the non-use of it) for wasting some 4636741ecf5SAndre Oppermann * socket buffer memory. 4646741ecf5SAndre Oppermann * 4656741ecf5SAndre Oppermann * TODO: Shrink send buffer during idle periods together 4666741ecf5SAndre Oppermann * with congestion window. Requires another timer. Has to 4676741ecf5SAndre Oppermann * wait for upcoming tcp timer rewrite. 468cfa6009eSGleb Smirnoff * 469cfa6009eSGleb Smirnoff * XXXGL: should there be used sbused() or sbavail()? 4706741ecf5SAndre Oppermann */ 471603724d3SBjoern A. Zeeb if (V_tcp_do_autosndbuf && so->so_snd.sb_flags & SB_AUTOSIZE) { 4726741ecf5SAndre Oppermann if ((tp->snd_wnd / 4 * 5) >= so->so_snd.sb_hiwat && 473cfa6009eSGleb Smirnoff sbused(&so->so_snd) >= (so->so_snd.sb_hiwat / 8 * 7) && 474cfa6009eSGleb Smirnoff sbused(&so->so_snd) < V_tcp_autosndbuf_max && 475cfa6009eSGleb Smirnoff sendwin >= (sbused(&so->so_snd) - 476cfa6009eSGleb Smirnoff (tp->snd_nxt - tp->snd_una))) { 4776741ecf5SAndre Oppermann if (!sbreserve_locked(&so->so_snd, 478603724d3SBjoern A. Zeeb min(so->so_snd.sb_hiwat + V_tcp_autosndbuf_inc, 479603724d3SBjoern A. Zeeb V_tcp_autosndbuf_max), so, curthread)) 4806741ecf5SAndre Oppermann so->so_snd.sb_flags &= ~SB_AUTOSIZE; 4816741ecf5SAndre Oppermann } 4826741ecf5SAndre Oppermann } 4836741ecf5SAndre Oppermann 4846741ecf5SAndre Oppermann /* 485ed420311SAndre Oppermann * Decide if we can use TCP Segmentation Offloading (if supported by 486ed420311SAndre Oppermann * hardware). 487b3c0f300SAndre Oppermann * 488b3c0f300SAndre Oppermann * TSO may only be used if we are in a pure bulk sending state. The 489b3c0f300SAndre Oppermann * presence of TCP-MD5, SACK retransmits, SACK advertizements and 490b3c0f300SAndre Oppermann * IP options prevent using TSO. With TSO the TCP header is the same 491b3c0f300SAndre Oppermann * (except for the sequence number) for all generated packets. This 492b3c0f300SAndre Oppermann * makes it impossible to transmit any options which vary per generated 493b3c0f300SAndre Oppermann * segment or packet. 49428257b5cSMatthew Dillon */ 4959ad0173dSBjoern A. Zeeb #ifdef IPSEC 4969ad0173dSBjoern A. Zeeb /* 4979ad0173dSBjoern A. Zeeb * Pre-calculate here as we save another lookup into the darknesses 4989ad0173dSBjoern A. Zeeb * of IPsec that way and can actually decide if TSO is ok. 4999ad0173dSBjoern A. Zeeb */ 5009ad0173dSBjoern A. Zeeb ipsec_optlen = ipsec_hdrsiz_tcp(tp); 5019ad0173dSBjoern A. Zeeb #endif 502ed420311SAndre Oppermann if ((tp->t_flags & TF_TSO) && V_tcp_do_tso && len > tp->t_maxseg && 503b3c0f300SAndre Oppermann ((tp->t_flags & TF_SIGNATURE) == 0) && 504b3c0f300SAndre Oppermann tp->rcv_numsacks == 0 && sack_rxmit == 0 && 5059ad0173dSBjoern A. Zeeb #ifdef IPSEC 506ed420311SAndre Oppermann ipsec_optlen == 0 && 5079ad0173dSBjoern A. Zeeb #endif 508ed420311SAndre Oppermann tp->t_inpcb->inp_options == NULL && 509ed420311SAndre Oppermann tp->t_inpcb->in6p_options == NULL) 510b3c0f300SAndre Oppermann tso = 1; 511153e5b57SAndre Oppermann 5125d3b1b75SJayanth Vijayaraghavan if (sack_rxmit) { 513cfa6009eSGleb Smirnoff if (SEQ_LT(p->rxmit + len, tp->snd_una + sbused(&so->so_snd))) 514df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 5155d3b1b75SJayanth Vijayaraghavan } else { 516cfa6009eSGleb Smirnoff if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + 517cfa6009eSGleb Smirnoff sbused(&so->so_snd))) 5185d3b1b75SJayanth Vijayaraghavan flags &= ~TH_FIN; 5195d3b1b75SJayanth Vijayaraghavan } 520df8bae1dSRodney W. Grimes 521201d185bSAndre Oppermann recwin = sbspace(&so->so_rcv); 522df8bae1dSRodney W. Grimes 523df8bae1dSRodney W. Grimes /* 524262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 525262c1c1aSMatthew Dillon * conditions when len is non-zero: 526262c1c1aSMatthew Dillon * 527b3c0f300SAndre Oppermann * - We have a full segment (or more with TSO) 528262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 529262c1c1aSMatthew Dillon * either idle or running NODELAY 530262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 531262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 532262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 533262c1c1aSMatthew Dillon * - we need to retransmit 534df8bae1dSRodney W. Grimes */ 535df8bae1dSRodney W. Grimes if (len) { 536b3c0f300SAndre Oppermann if (len >= tp->t_maxseg) 537df8bae1dSRodney W. Grimes goto send; 538262c1c1aSMatthew Dillon /* 539262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 540262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 541262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 542262c1c1aSMatthew Dillon * 543262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 544262c1c1aSMatthew Dillon */ 545262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 546262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 547cfa6009eSGleb Smirnoff len + off >= sbavail(&so->so_snd) && 548262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 549df8bae1dSRodney W. Grimes goto send; 550262c1c1aSMatthew Dillon } 5512cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */ 552df8bae1dSRodney W. Grimes goto send; 553a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 554df8bae1dSRodney W. Grimes goto send; 555262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 556df8bae1dSRodney W. Grimes goto send; 5576d90faf3SPaul Saab if (sack_rxmit) 5586d90faf3SPaul Saab goto send; 559df8bae1dSRodney W. Grimes } 560df8bae1dSRodney W. Grimes 561df8bae1dSRodney W. Grimes /* 562f62563d3SAndre Oppermann * Sending of standalone window updates. 563f62563d3SAndre Oppermann * 56478f59b4bSAndre Oppermann * Window updates are important when we close our window due to a 56578f59b4bSAndre Oppermann * full socket buffer and are opening it again after the application 566f62563d3SAndre Oppermann * reads data from it. Once the window has opened again and the 567f62563d3SAndre Oppermann * remote end starts to send again the ACK clock takes over and 568f62563d3SAndre Oppermann * provides the most current window information. 569f62563d3SAndre Oppermann * 57078f59b4bSAndre Oppermann * We must avoid the silly window syndrome whereas every read 571f62563d3SAndre Oppermann * from the receive buffer, no matter how small, causes a window 572f62563d3SAndre Oppermann * update to be sent. We also should avoid sending a flurry of 573f62563d3SAndre Oppermann * window updates when the socket buffer had queued a lot of data 574f62563d3SAndre Oppermann * and the application is doing small reads. 575f62563d3SAndre Oppermann * 576f62563d3SAndre Oppermann * Prevent a flurry of pointless window updates by only sending 577f62563d3SAndre Oppermann * an update when we can increase the advertized window by more 578f62563d3SAndre Oppermann * than 1/4th of the socket buffer capacity. When the buffer is 579f62563d3SAndre Oppermann * getting full or is very small be more aggressive and send an 580f62563d3SAndre Oppermann * update whenever we can increase by two mss sized segments. 581f62563d3SAndre Oppermann * In all other situations the ACK's to new incoming data will 582f62563d3SAndre Oppermann * carry further window increases. 5834249614cSAndre Oppermann * 5844249614cSAndre Oppermann * Don't send an independent window update if a delayed 5854249614cSAndre Oppermann * ACK is pending (it will get piggy-backed on it) or the 5864249614cSAndre Oppermann * remote side already has done a half-close and won't send 587f62563d3SAndre Oppermann * more data. Skip this if the connection is in T/TCP 588f62563d3SAndre Oppermann * half-open state. 589df8bae1dSRodney W. Grimes */ 590b7de7d87SAndre Oppermann if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN) && 5914249614cSAndre Oppermann !(tp->t_flags & TF_DELACK) && 592b7de7d87SAndre Oppermann !TCPS_HAVERCVDFIN(tp->t_state)) { 593df8bae1dSRodney W. Grimes /* 594f62563d3SAndre Oppermann * "adv" is the amount we could increase the window, 595df8bae1dSRodney W. Grimes * taking into account that we are limited by 596df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 597df8bae1dSRodney W. Grimes */ 598f701e30dSJohn Baldwin long adv; 599f701e30dSJohn Baldwin int oldwin; 600f701e30dSJohn Baldwin 601f701e30dSJohn Baldwin adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale); 602f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt)) { 603f701e30dSJohn Baldwin oldwin = (tp->rcv_adv - tp->rcv_nxt); 604f701e30dSJohn Baldwin adv -= oldwin; 605f701e30dSJohn Baldwin } else 606f701e30dSJohn Baldwin oldwin = 0; 607df8bae1dSRodney W. Grimes 608da84b2e6SJohn Baldwin /* 609da84b2e6SJohn Baldwin * If the new window size ends up being the same as the old 610da84b2e6SJohn Baldwin * size when it is scaled, then don't force a window update. 611da84b2e6SJohn Baldwin */ 612f701e30dSJohn Baldwin if (oldwin >> tp->rcv_scale == (adv + oldwin) >> tp->rcv_scale) 613da84b2e6SJohn Baldwin goto dontupdate; 614f62563d3SAndre Oppermann 615f62563d3SAndre Oppermann if (adv >= (long)(2 * tp->t_maxseg) && 616f62563d3SAndre Oppermann (adv >= (long)(so->so_rcv.sb_hiwat / 4) || 617f62563d3SAndre Oppermann recwin <= (long)(so->so_rcv.sb_hiwat / 8) || 618f62563d3SAndre Oppermann so->so_rcv.sb_hiwat <= 8 * tp->t_maxseg)) 619df8bae1dSRodney W. Grimes goto send; 620df8bae1dSRodney W. Grimes } 621da84b2e6SJohn Baldwin dontupdate: 622df8bae1dSRodney W. Grimes 623df8bae1dSRodney W. Grimes /* 62428257b5cSMatthew Dillon * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 62528257b5cSMatthew Dillon * is also a catch-all for the retransmit timer timeout case. 626df8bae1dSRodney W. Grimes */ 627df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 628df8bae1dSRodney W. Grimes goto send; 629a0292f23SGarrett Wollman if ((flags & TH_RST) || 630a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 631df8bae1dSRodney W. Grimes goto send; 632df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 633df8bae1dSRodney W. Grimes goto send; 634df8bae1dSRodney W. Grimes /* 635df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 63628257b5cSMatthew Dillon * and we have not yet done so, then we need to send. 637df8bae1dSRodney W. Grimes */ 638df8bae1dSRodney W. Grimes if (flags & TH_FIN && 639df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 640df8bae1dSRodney W. Grimes goto send; 6416d90faf3SPaul Saab /* 6426d90faf3SPaul Saab * In SACK, it is possible for tcp_output to fail to send a segment 6436d90faf3SPaul Saab * after the retransmission timer has been turned off. Make sure 6446d90faf3SPaul Saab * that the retransmission timer is set. 6456d90faf3SPaul Saab */ 6463529149eSAndre Oppermann if ((tp->t_flags & TF_SACK_PERMIT) && 6473529149eSAndre Oppermann SEQ_GT(tp->snd_max, tp->snd_una) && 648b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_REXMT) && 649b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 650b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 651cf2942b6SRobert Watson goto just_return; 6526d90faf3SPaul Saab } 653df8bae1dSRodney W. Grimes /* 654df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 655df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 656df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 657df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 658df8bae1dSRodney W. Grimes * persisting to move a small or zero window 659df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 660df8bae1dSRodney W. Grimes * 661b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_PERSIST) 6629b8b58e0SJonathan Lemon * is true when we are in persist state. 6632cdbfa66SPaul Saab * (tp->t_flags & TF_FORCEDATA) 664df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 665b8152ba7SAndre Oppermann * tcp_timer_active(tp, TT_REXMT) 666df8bae1dSRodney W. Grimes * is set when we are retransmitting 667df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 668df8bae1dSRodney W. Grimes * 669df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 670df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 671df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 672df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 673df8bae1dSRodney W. Grimes * otherwise force out a byte. 674df8bae1dSRodney W. Grimes */ 675cfa6009eSGleb Smirnoff if (sbavail(&so->so_snd) && !tcp_timer_active(tp, TT_REXMT) && 676b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) { 677df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 678df8bae1dSRodney W. Grimes tcp_setpersist(tp); 679df8bae1dSRodney W. Grimes } 680df8bae1dSRodney W. Grimes 681df8bae1dSRodney W. Grimes /* 682df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 683df8bae1dSRodney W. Grimes */ 684cf2942b6SRobert Watson just_return: 685cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 686df8bae1dSRodney W. Grimes return (0); 687df8bae1dSRodney W. Grimes 688df8bae1dSRodney W. Grimes send: 689cf2942b6SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 690f6f6703fSSean Bruno if (len > 0) { 691f6f6703fSSean Bruno if (len >= tp->t_maxseg) 692f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_MAXSEGSNT; 693f6f6703fSSean Bruno else 694f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_MAXSEGSNT; 695f6f6703fSSean Bruno } 696df8bae1dSRodney W. Grimes /* 697df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 698df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 699df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 700df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 701df8bae1dSRodney W. Grimes * link header, i.e. 70233841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 703df8bae1dSRodney W. Grimes */ 704df8bae1dSRodney W. Grimes optlen = 0; 705fb59c426SYoshinobu Inoue #ifdef INET6 706fb59c426SYoshinobu Inoue if (isipv6) 707fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 708fb59c426SYoshinobu Inoue else 709fb59c426SYoshinobu Inoue #endif 710df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 71102a1a643SAndre Oppermann 71202a1a643SAndre Oppermann /* 71302a1a643SAndre Oppermann * Compute options for segment. 71402a1a643SAndre Oppermann * We only have to care about SYN and established connection 71502a1a643SAndre Oppermann * segments. Options for SYN-ACK segments are handled in TCP 71602a1a643SAndre Oppermann * syncache. 71702a1a643SAndre Oppermann */ 71802a1a643SAndre Oppermann if ((tp->t_flags & TF_NOOPT) == 0) { 71902a1a643SAndre Oppermann to.to_flags = 0; 72002a1a643SAndre Oppermann /* Maximum segment size. */ 721df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 722df8bae1dSRodney W. Grimes tp->snd_nxt = tp->iss; 72302a1a643SAndre Oppermann to.to_mss = tcp_mssopt(&tp->t_inpcb->inp_inc); 72402a1a643SAndre Oppermann to.to_flags |= TOF_MSS; 725df8bae1dSRodney W. Grimes } 72602a1a643SAndre Oppermann /* Window scaling. */ 72702a1a643SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_REQ_SCALE)) { 72802a1a643SAndre Oppermann to.to_wscale = tp->request_r_scale; 72902a1a643SAndre Oppermann to.to_flags |= TOF_SCALE; 730df8bae1dSRodney W. Grimes } 73102a1a643SAndre Oppermann /* Timestamps. */ 73202a1a643SAndre Oppermann if ((tp->t_flags & TF_RCVD_TSTMP) || 73302a1a643SAndre Oppermann ((flags & TH_SYN) && (tp->t_flags & TF_REQ_TSTMP))) { 734d8951c8aSBjoern A. Zeeb to.to_tsval = tcp_ts_getticks() + tp->ts_offset; 73502a1a643SAndre Oppermann to.to_tsecr = tp->ts_recent; 73602a1a643SAndre Oppermann to.to_flags |= TOF_TS; 7376741ecf5SAndre Oppermann /* Set receive buffer autosizing timestamp. */ 73802a1a643SAndre Oppermann if (tp->rfbuf_ts == 0 && 73902a1a643SAndre Oppermann (so->so_rcv.sb_flags & SB_AUTOSIZE)) 740d8951c8aSBjoern A. Zeeb tp->rfbuf_ts = tcp_ts_getticks(); 7411cfd4b53SBruce M Simpson } 74202a1a643SAndre Oppermann /* Selective ACK's. */ 7433529149eSAndre Oppermann if (tp->t_flags & TF_SACK_PERMIT) { 74402a1a643SAndre Oppermann if (flags & TH_SYN) 74502a1a643SAndre Oppermann to.to_flags |= TOF_SACKPERM; 74602a1a643SAndre Oppermann else if (TCPS_HAVEESTABLISHED(tp->t_state) && 74702a1a643SAndre Oppermann (tp->t_flags & TF_SACK_PERMIT) && 74802a1a643SAndre Oppermann tp->rcv_numsacks > 0) { 74902a1a643SAndre Oppermann to.to_flags |= TOF_SACK; 75002a1a643SAndre Oppermann to.to_nsacks = tp->rcv_numsacks; 75102a1a643SAndre Oppermann to.to_sacks = (u_char *)tp->sackblks; 75202a1a643SAndre Oppermann } 75302a1a643SAndre Oppermann } 75402a1a643SAndre Oppermann #ifdef TCP_SIGNATURE 75502a1a643SAndre Oppermann /* TCP-MD5 (RFC2385). */ 75602a1a643SAndre Oppermann if (tp->t_flags & TF_SIGNATURE) 75702a1a643SAndre Oppermann to.to_flags |= TOF_SIGNATURE; 7581cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 7591cfd4b53SBruce M Simpson 76002a1a643SAndre Oppermann /* Processing the options. */ 7614a411b9fSBjoern A. Zeeb hdrlen += optlen = tcp_addoptions(&to, opt); 762be3f3b5eSPaul Saab } 763be3f3b5eSPaul Saab 764fb59c426SYoshinobu Inoue #ifdef INET6 765fb59c426SYoshinobu Inoue if (isipv6) 766fb59c426SYoshinobu Inoue ipoptlen = ip6_optlen(tp->t_inpcb); 767fb59c426SYoshinobu Inoue else 768fb59c426SYoshinobu Inoue #endif 769f10e85d7SLuigi Rizzo if (tp->t_inpcb->inp_options) 770a04884fcSBill Fenner ipoptlen = tp->t_inpcb->inp_options->m_len - 771a04884fcSBill Fenner offsetof(struct ipoption, ipopt_list); 772f10e85d7SLuigi Rizzo else 773a04884fcSBill Fenner ipoptlen = 0; 774b2630c29SGeorge V. Neville-Neil #ifdef IPSEC 7759ad0173dSBjoern A. Zeeb ipoptlen += ipsec_optlen; 776fb59c426SYoshinobu Inoue #endif 777a04884fcSBill Fenner 778df8bae1dSRodney W. Grimes /* 779df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 780a0292f23SGarrett Wollman * bump the packet length beyond the t_maxopd length. 781a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 782a0292f23SGarrett Wollman * the segment. 783df8bae1dSRodney W. Grimes */ 784a04884fcSBill Fenner if (len + optlen + ipoptlen > tp->t_maxopd) { 785297a37f3SDavid Greenman flags &= ~TH_FIN; 786ed420311SAndre Oppermann 787b3c0f300SAndre Oppermann if (tso) { 7889fd573c3SHans Petter Selasky u_int if_hw_tsomax; 7899fd573c3SHans Petter Selasky u_int if_hw_tsomaxsegcount; 7909fd573c3SHans Petter Selasky u_int if_hw_tsomaxsegsize; 7919fd573c3SHans Petter Selasky struct mbuf *mb; 7929fd573c3SHans Petter Selasky u_int moff; 7939fd573c3SHans Petter Selasky int max_len; 7949fd573c3SHans Petter Selasky 7959fd573c3SHans Petter Selasky /* extract TSO information */ 7969fd573c3SHans Petter Selasky if_hw_tsomax = tp->t_tsomax; 7979fd573c3SHans Petter Selasky if_hw_tsomaxsegcount = tp->t_tsomaxsegcount; 7989fd573c3SHans Petter Selasky if_hw_tsomaxsegsize = tp->t_tsomaxsegsize; 7999fd573c3SHans Petter Selasky 8009fd573c3SHans Petter Selasky /* 8019fd573c3SHans Petter Selasky * Limit a TSO burst to prevent it from 8029fd573c3SHans Petter Selasky * overflowing or exceeding the maximum length 8039fd573c3SHans Petter Selasky * allowed by the network interface: 8049fd573c3SHans Petter Selasky */ 805ed420311SAndre Oppermann KASSERT(ipoptlen == 0, 806ed420311SAndre Oppermann ("%s: TSO can't do IP options", __func__)); 807ed420311SAndre Oppermann 808ed420311SAndre Oppermann /* 8099fd573c3SHans Petter Selasky * Check if we should limit by maximum payload 8109fd573c3SHans Petter Selasky * length: 811ed420311SAndre Oppermann */ 8129fd573c3SHans Petter Selasky if (if_hw_tsomax != 0) { 8139fd573c3SHans Petter Selasky /* compute maximum TSO length */ 8149fd573c3SHans Petter Selasky max_len = (if_hw_tsomax - hdrlen); 8159fd573c3SHans Petter Selasky if (max_len <= 0) { 8169fd573c3SHans Petter Selasky len = 0; 8173c7c188cSHans Petter Selasky } else if (len > max_len) { 818b3c0f300SAndre Oppermann sendalot = 1; 8193c7c188cSHans Petter Selasky len = max_len; 8209fd573c3SHans Petter Selasky } 8219fd573c3SHans Petter Selasky } 8229fd573c3SHans Petter Selasky 8239fd573c3SHans Petter Selasky /* 8249fd573c3SHans Petter Selasky * Check if we should limit by maximum segment 8259fd573c3SHans Petter Selasky * size and count: 8269fd573c3SHans Petter Selasky */ 827b228e6bfSHans Petter Selasky if (if_hw_tsomaxsegcount != 0 && 828b228e6bfSHans Petter Selasky if_hw_tsomaxsegsize != 0) { 8299fd573c3SHans Petter Selasky max_len = 0; 8309fd573c3SHans Petter Selasky mb = sbsndmbuf(&so->so_snd, off, &moff); 8319fd573c3SHans Petter Selasky 8323c7c188cSHans Petter Selasky while (mb != NULL && max_len < len) { 833b228e6bfSHans Petter Selasky u_int mlen; 834b228e6bfSHans Petter Selasky u_int frags; 8359fd573c3SHans Petter Selasky 8369fd573c3SHans Petter Selasky /* 8379fd573c3SHans Petter Selasky * Get length of mbuf fragment 838b228e6bfSHans Petter Selasky * and how many hardware frags, 839b228e6bfSHans Petter Selasky * rounded up, it would use: 8409fd573c3SHans Petter Selasky */ 841b228e6bfSHans Petter Selasky mlen = (mb->m_len - moff); 842b228e6bfSHans Petter Selasky frags = howmany(mlen, 843b228e6bfSHans Petter Selasky if_hw_tsomaxsegsize); 8449fd573c3SHans Petter Selasky 8459fd573c3SHans Petter Selasky /* Handle special case: Zero Length Mbuf */ 846b228e6bfSHans Petter Selasky if (frags == 0) 847b228e6bfSHans Petter Selasky frags = 1; 8489fd573c3SHans Petter Selasky 8499fd573c3SHans Petter Selasky /* 8509fd573c3SHans Petter Selasky * Check if the fragment limit 851b228e6bfSHans Petter Selasky * will be reached or exceeded: 8529fd573c3SHans Petter Selasky */ 853b228e6bfSHans Petter Selasky if (frags >= if_hw_tsomaxsegcount) { 854b228e6bfSHans Petter Selasky max_len += min(mlen, 8559fd573c3SHans Petter Selasky if_hw_tsomaxsegcount * 8569fd573c3SHans Petter Selasky if_hw_tsomaxsegsize); 8579fd573c3SHans Petter Selasky break; 8589fd573c3SHans Petter Selasky } 859b228e6bfSHans Petter Selasky max_len += mlen; 860b228e6bfSHans Petter Selasky if_hw_tsomaxsegcount -= frags; 8619fd573c3SHans Petter Selasky moff = 0; 8629fd573c3SHans Petter Selasky mb = mb->m_next; 8639fd573c3SHans Petter Selasky } 8649fd573c3SHans Petter Selasky if (max_len <= 0) { 8659fd573c3SHans Petter Selasky len = 0; 8663c7c188cSHans Petter Selasky } else if (len > max_len) { 8679fd573c3SHans Petter Selasky sendalot = 1; 8683c7c188cSHans Petter Selasky len = max_len; 8699fd573c3SHans Petter Selasky } 870ed420311SAndre Oppermann } 871ed420311SAndre Oppermann 872ed420311SAndre Oppermann /* 873ed420311SAndre Oppermann * Prevent the last segment from being 8749fd573c3SHans Petter Selasky * fractional unless the send sockbuf can be 8759fd573c3SHans Petter Selasky * emptied: 876ed420311SAndre Oppermann */ 8779fd573c3SHans Petter Selasky max_len = (tp->t_maxopd - optlen); 878cfa6009eSGleb Smirnoff if ((off + len) < sbavail(&so->so_snd)) { 8793c7c188cSHans Petter Selasky moff = len % max_len; 8809fd573c3SHans Petter Selasky if (moff != 0) { 8819fd573c3SHans Petter Selasky len -= moff; 882b3c0f300SAndre Oppermann sendalot = 1; 883ed420311SAndre Oppermann } 8849fd573c3SHans Petter Selasky } 8859fd573c3SHans Petter Selasky 8869fd573c3SHans Petter Selasky /* 8879fd573c3SHans Petter Selasky * In case there are too many small fragments 8889fd573c3SHans Petter Selasky * don't use TSO: 8899fd573c3SHans Petter Selasky */ 8903c7c188cSHans Petter Selasky if (len <= max_len) { 8913c7c188cSHans Petter Selasky len = max_len; 8929fd573c3SHans Petter Selasky sendalot = 1; 8939fd573c3SHans Petter Selasky tso = 0; 8949fd573c3SHans Petter Selasky } 895ed420311SAndre Oppermann 896ed420311SAndre Oppermann /* 897ed420311SAndre Oppermann * Send the FIN in a separate segment 898ed420311SAndre Oppermann * after the bulk sending is done. 899ed420311SAndre Oppermann * We don't trust the TSO implementations 900ed420311SAndre Oppermann * to clear the FIN flag on all but the 901ed420311SAndre Oppermann * last segment. 902ed420311SAndre Oppermann */ 903ed420311SAndre Oppermann if (tp->t_flags & TF_NEEDFIN) 904ed420311SAndre Oppermann sendalot = 1; 905ed420311SAndre Oppermann 906b3c0f300SAndre Oppermann } else { 907a04884fcSBill Fenner len = tp->t_maxopd - optlen - ipoptlen; 908df8bae1dSRodney W. Grimes sendalot = 1; 909df8bae1dSRodney W. Grimes } 910ed420311SAndre Oppermann } else 911ed420311SAndre Oppermann tso = 0; 912ed420311SAndre Oppermann 913ed420311SAndre Oppermann KASSERT(len + hdrlen + ipoptlen <= IP_MAXPACKET, 914ed420311SAndre Oppermann ("%s: len > IP_MAXPACKET", __func__)); 915df8bae1dSRodney W. Grimes 916a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 917a683a7ddSYoshinobu Inoue #ifdef INET6 918a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 919a683a7ddSYoshinobu Inoue #else 920df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 921a683a7ddSYoshinobu Inoue #endif 922f10e85d7SLuigi Rizzo panic("tcphdr too big"); 923a0292f23SGarrett Wollman /*#endif*/ 924df8bae1dSRodney W. Grimes 925df8bae1dSRodney W. Grimes /* 926c4982faeSBjoern A. Zeeb * This KASSERT is here to catch edge cases at a well defined place. 927c4982faeSBjoern A. Zeeb * Before, those had triggered (random) panic conditions further down. 928c4982faeSBjoern A. Zeeb */ 929c4982faeSBjoern A. Zeeb KASSERT(len >= 0, ("[%s:%d]: len < 0", __func__, __LINE__)); 930c4982faeSBjoern A. Zeeb 931c4982faeSBjoern A. Zeeb /* 932df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 933df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 934df8bae1dSRodney W. Grimes * the template for sends on this connection. 935df8bae1dSRodney W. Grimes */ 936df8bae1dSRodney W. Grimes if (len) { 9374e023759SAndre Oppermann struct mbuf *mb; 9384e023759SAndre Oppermann u_int moff; 9394e023759SAndre Oppermann 9402cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) && len == 1) 94178b50714SRobert Watson TCPSTAT_INC(tcps_sndprobe); 9420ba5d2eeSJohn Baldwin else if (SEQ_LT(tp->snd_nxt, tp->snd_max) || sack_rxmit) { 943f5d34df5SGeorge V. Neville-Neil tp->t_sndrexmitpack++; 94478b50714SRobert Watson TCPSTAT_INC(tcps_sndrexmitpack); 94578b50714SRobert Watson TCPSTAT_ADD(tcps_sndrexmitbyte, len); 946df8bae1dSRodney W. Grimes } else { 94778b50714SRobert Watson TCPSTAT_INC(tcps_sndpack); 94878b50714SRobert Watson TCPSTAT_ADD(tcps_sndbyte, len); 949df8bae1dSRodney W. Grimes } 95039f6074eSGleb Smirnoff #ifdef INET6 95139f6074eSGleb Smirnoff if (MHLEN < hdrlen + max_linkhdr) 95239f6074eSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 95339f6074eSGleb Smirnoff else 95439f6074eSGleb Smirnoff #endif 95539f6074eSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 95639f6074eSGleb Smirnoff 957df8bae1dSRodney W. Grimes if (m == NULL) { 958cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 959df8bae1dSRodney W. Grimes error = ENOBUFS; 9600e2bc05cSGleb Smirnoff sack_rxmit = 0; 961df8bae1dSRodney W. Grimes goto out; 962df8bae1dSRodney W. Grimes } 96339f6074eSGleb Smirnoff 964df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 965df8bae1dSRodney W. Grimes m->m_len = hdrlen; 9664e023759SAndre Oppermann 9674e023759SAndre Oppermann /* 9684e023759SAndre Oppermann * Start the m_copy functions from the closest mbuf 9694e023759SAndre Oppermann * to the offset in the socket buffer chain. 9704e023759SAndre Oppermann */ 9714e023759SAndre Oppermann mb = sbsndptr(&so->so_snd, off, len, &moff); 9724e023759SAndre Oppermann 973df8bae1dSRodney W. Grimes if (len <= MHLEN - hdrlen - max_linkhdr) { 9744e023759SAndre Oppermann m_copydata(mb, moff, (int)len, 975df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 976df8bae1dSRodney W. Grimes m->m_len += len; 977df8bae1dSRodney W. Grimes } else { 9784e023759SAndre Oppermann m->m_next = m_copy(mb, moff, (int)len); 9794e023759SAndre Oppermann if (m->m_next == NULL) { 980cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 981d7f570e6SGarrett Wollman (void) m_free(m); 98251823c3aSGarrett Wollman error = ENOBUFS; 9830e2bc05cSGleb Smirnoff sack_rxmit = 0; 98451823c3aSGarrett Wollman goto out; 98551823c3aSGarrett Wollman } 986df8bae1dSRodney W. Grimes } 987472ea5beSColin Percival 988df8bae1dSRodney W. Grimes /* 989df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 990df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 991df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 992df8bae1dSRodney W. Grimes * a PUSH comes in.) 993df8bae1dSRodney W. Grimes */ 994cfa6009eSGleb Smirnoff if (off + len == sbused(&so->so_snd)) 995df8bae1dSRodney W. Grimes flags |= TH_PUSH; 996cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 997df8bae1dSRodney W. Grimes } else { 998cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 999df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 100078b50714SRobert Watson TCPSTAT_INC(tcps_sndacks); 1001df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 100278b50714SRobert Watson TCPSTAT_INC(tcps_sndctrl); 1003df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 100478b50714SRobert Watson TCPSTAT_INC(tcps_sndurg); 1005df8bae1dSRodney W. Grimes else 100678b50714SRobert Watson TCPSTAT_INC(tcps_sndwinup); 1007df8bae1dSRodney W. Grimes 1008aa8bd99dSGleb Smirnoff m = m_gethdr(M_NOWAIT, MT_DATA); 1009df8bae1dSRodney W. Grimes if (m == NULL) { 1010df8bae1dSRodney W. Grimes error = ENOBUFS; 10110e2bc05cSGleb Smirnoff sack_rxmit = 0; 1012df8bae1dSRodney W. Grimes goto out; 1013df8bae1dSRodney W. Grimes } 1014fb59c426SYoshinobu Inoue #ifdef INET6 1015fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 1016fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 1017ed6a66caSRobert Watson M_ALIGN(m, hdrlen); 1018fb59c426SYoshinobu Inoue } else 1019fb59c426SYoshinobu Inoue #endif 1020df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 1021df8bae1dSRodney W. Grimes m->m_len = hdrlen; 1022df8bae1dSRodney W. Grimes } 1023cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); 1024df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 1025c488362eSRobert Watson #ifdef MAC 102630d239bcSRobert Watson mac_inpcb_create_mbuf(tp->t_inpcb, m); 1027c488362eSRobert Watson #endif 1028fb59c426SYoshinobu Inoue #ifdef INET6 1029fb59c426SYoshinobu Inoue if (isipv6) { 1030fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 1031fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 103279909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip6, th); 1033fb59c426SYoshinobu Inoue } else 1034fb59c426SYoshinobu Inoue #endif /* INET6 */ 1035fb59c426SYoshinobu Inoue { 1036fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 1037fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 1038fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 103979909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip, th); 1040fb59c426SYoshinobu Inoue } 1041df8bae1dSRodney W. Grimes 1042df8bae1dSRodney W. Grimes /* 1043df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 1044df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 1045df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 1046df8bae1dSRodney W. Grimes */ 1047df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 1048df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 1049df8bae1dSRodney W. Grimes tp->snd_nxt--; 1050df8bae1dSRodney W. Grimes /* 1051f2512ba1SRui Paulo * If we are starting a connection, send ECN setup 1052f2512ba1SRui Paulo * SYN packet. If we are on a retransmit, we may 1053f2512ba1SRui Paulo * resend those bits a number of times as per 1054f2512ba1SRui Paulo * RFC 3168. 1055f2512ba1SRui Paulo */ 1056603724d3SBjoern A. Zeeb if (tp->t_state == TCPS_SYN_SENT && V_tcp_do_ecn) { 1057f2512ba1SRui Paulo if (tp->t_rxtshift >= 1) { 1058603724d3SBjoern A. Zeeb if (tp->t_rxtshift <= V_tcp_ecn_maxretries) 1059f2512ba1SRui Paulo flags |= TH_ECE|TH_CWR; 1060f2512ba1SRui Paulo } else 1061f2512ba1SRui Paulo flags |= TH_ECE|TH_CWR; 1062f2512ba1SRui Paulo } 1063f2512ba1SRui Paulo 1064f2512ba1SRui Paulo if (tp->t_state == TCPS_ESTABLISHED && 1065f2512ba1SRui Paulo (tp->t_flags & TF_ECN_PERMIT)) { 1066f2512ba1SRui Paulo /* 1067f2512ba1SRui Paulo * If the peer has ECN, mark data packets with 1068f2512ba1SRui Paulo * ECN capable transmission (ECT). 1069f2512ba1SRui Paulo * Ignore pure ack packets, retransmissions and window probes. 1070f2512ba1SRui Paulo */ 1071f2512ba1SRui Paulo if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) && 1072f2512ba1SRui Paulo !((tp->t_flags & TF_FORCEDATA) && len == 1)) { 1073f2512ba1SRui Paulo #ifdef INET6 1074f2512ba1SRui Paulo if (isipv6) 1075f2512ba1SRui Paulo ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20); 1076f2512ba1SRui Paulo else 1077f2512ba1SRui Paulo #endif 1078f2512ba1SRui Paulo ip->ip_tos |= IPTOS_ECN_ECT0; 107978b50714SRobert Watson TCPSTAT_INC(tcps_ecn_ect0); 1080f2512ba1SRui Paulo } 1081f2512ba1SRui Paulo 1082f2512ba1SRui Paulo /* 1083f2512ba1SRui Paulo * Reply with proper ECN notifications. 1084f2512ba1SRui Paulo */ 1085f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_SND_CWR) { 1086f2512ba1SRui Paulo flags |= TH_CWR; 1087f2512ba1SRui Paulo tp->t_flags &= ~TF_ECN_SND_CWR; 1088f2512ba1SRui Paulo } 1089f2512ba1SRui Paulo if (tp->t_flags & TF_ECN_SND_ECE) 1090f2512ba1SRui Paulo flags |= TH_ECE; 1091f2512ba1SRui Paulo } 1092f2512ba1SRui Paulo 1093f2512ba1SRui Paulo /* 1094df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 1095df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 1096df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 1097df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 1098df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 1099df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 1100df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 1101df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 1102df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 1103df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 1104df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 1105df8bae1dSRodney W. Grimes */ 1106a55db2b6SPaul Saab if (sack_rxmit == 0) { 1107b8152ba7SAndre Oppermann if (len || (flags & (TH_SYN|TH_FIN)) || 1108b8152ba7SAndre Oppermann tcp_timer_active(tp, TT_PERSIST)) 1109fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 1110df8bae1dSRodney W. Grimes else 1111fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 1112a55db2b6SPaul Saab } else { 11136d90faf3SPaul Saab th->th_seq = htonl(p->rxmit); 11146d90faf3SPaul Saab p->rxmit += len; 11150077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += len; 11166d90faf3SPaul Saab } 1117fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 1118df8bae1dSRodney W. Grimes if (optlen) { 1119fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 1120fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 1121df8bae1dSRodney W. Grimes } 1122fb59c426SYoshinobu Inoue th->th_flags = flags; 1123df8bae1dSRodney W. Grimes /* 1124df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 1125df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 1126df8bae1dSRodney W. Grimes */ 1127201d185bSAndre Oppermann if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && 1128201d185bSAndre Oppermann recwin < (long)tp->t_maxseg) 1129201d185bSAndre Oppermann recwin = 0; 1130f701e30dSJohn Baldwin if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) && 1131f701e30dSJohn Baldwin recwin < (long)(tp->rcv_adv - tp->rcv_nxt)) 1132201d185bSAndre Oppermann recwin = (long)(tp->rcv_adv - tp->rcv_nxt); 1133201d185bSAndre Oppermann if (recwin > (long)TCP_MAXWIN << tp->rcv_scale) 1134201d185bSAndre Oppermann recwin = (long)TCP_MAXWIN << tp->rcv_scale; 1135262c1c1aSMatthew Dillon 1136104ebb2aSAndre Oppermann /* 1137104ebb2aSAndre Oppermann * According to RFC1323 the window field in a SYN (i.e., a <SYN> 1138104ebb2aSAndre Oppermann * or <SYN,ACK>) segment itself is never scaled. The <SYN,ACK> 1139104ebb2aSAndre Oppermann * case is handled in syncache. 1140104ebb2aSAndre Oppermann */ 1141104ebb2aSAndre Oppermann if (flags & TH_SYN) 1142104ebb2aSAndre Oppermann th->th_win = htons((u_short) 1143104ebb2aSAndre Oppermann (min(sbspace(&so->so_rcv), TCP_MAXWIN))); 1144104ebb2aSAndre Oppermann else 1145104ebb2aSAndre Oppermann th->th_win = htons((u_short)(recwin >> tp->rcv_scale)); 1146262c1c1aSMatthew Dillon 1147262c1c1aSMatthew Dillon /* 1148262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 1149262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 1150262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 1151262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 1152b2722702SRui Paulo * to read more data than can be buffered prior to transmitting on 1153262c1c1aSMatthew Dillon * the connection. 1154262c1c1aSMatthew Dillon */ 1155f5d34df5SGeorge V. Neville-Neil if (th->th_win == 0) { 1156f5d34df5SGeorge V. Neville-Neil tp->t_sndzerowin++; 1157262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 1158f5d34df5SGeorge V. Neville-Neil } else 1159262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 1160df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 1161fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 1162fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 1163df8bae1dSRodney W. Grimes } else 1164df8bae1dSRodney W. Grimes /* 1165df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 1166df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 1167df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 1168df8bae1dSRodney W. Grimes * number wraparound. 1169df8bae1dSRodney W. Grimes */ 1170df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 1171df8bae1dSRodney W. Grimes 11721cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 1173ee763d0dSBjoern A. Zeeb if (tp->t_flags & TF_SIGNATURE) { 1174ee763d0dSBjoern A. Zeeb int sigoff = to.to_signature - opt; 11753418daf2SBjoern A. Zeeb tcp_signature_compute(m, 0, len, optlen, 11761cfd4b53SBruce M Simpson (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND); 1177ee763d0dSBjoern A. Zeeb } 1178265ed012SBruce M Simpson #endif 11791cfd4b53SBruce M Simpson 1180df8bae1dSRodney W. Grimes /* 1181df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 1182df8bae1dSRodney W. Grimes * checksum extended header and data. 1183df8bae1dSRodney W. Grimes */ 1184fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 118545747ba5SBjoern A. Zeeb m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 1186fb59c426SYoshinobu Inoue #ifdef INET6 118745747ba5SBjoern A. Zeeb if (isipv6) { 1188fb59c426SYoshinobu Inoue /* 1189fb59c426SYoshinobu Inoue * ip6_plen is not need to be filled now, and will be filled 1190fb59c426SYoshinobu Inoue * in ip6_output. 1191fb59c426SYoshinobu Inoue */ 1192356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP_IPV6; 119345747ba5SBjoern A. Zeeb th->th_sum = in6_cksum_pseudo(ip6, sizeof(struct tcphdr) + 119445747ba5SBjoern A. Zeeb optlen + len, IPPROTO_TCP, 0); 119545747ba5SBjoern A. Zeeb } 119645747ba5SBjoern A. Zeeb #endif 119745747ba5SBjoern A. Zeeb #if defined(INET6) && defined(INET) 1198fb59c426SYoshinobu Inoue else 119945747ba5SBjoern A. Zeeb #endif 120045747ba5SBjoern A. Zeeb #ifdef INET 1201fb59c426SYoshinobu Inoue { 1202356ab07eSBjoern A. Zeeb m->m_pkthdr.csum_flags = CSUM_TCP; 120379909384SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 120479909384SJonathan Lemon htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen)); 1205fb59c426SYoshinobu Inoue 1206db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 1207db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 12086e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 1209fb59c426SYoshinobu Inoue } 121045747ba5SBjoern A. Zeeb #endif 1211df8bae1dSRodney W. Grimes 1212df8bae1dSRodney W. Grimes /* 1213b3c0f300SAndre Oppermann * Enable TSO and specify the size of the segments. 1214b3c0f300SAndre Oppermann * The TCP pseudo header checksum is always provided. 1215b3c0f300SAndre Oppermann * XXX: Fixme: This is currently not the case for IPv6. 1216b3c0f300SAndre Oppermann */ 1217b3c0f300SAndre Oppermann if (tso) { 1218153e5b57SAndre Oppermann KASSERT(len > tp->t_maxopd - optlen, 1219153e5b57SAndre Oppermann ("%s: len <= tso_segsz", __func__)); 12203579cf4cSKenneth D. Merry m->m_pkthdr.csum_flags |= CSUM_TSO; 1221b3c0f300SAndre Oppermann m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen; 1222b3c0f300SAndre Oppermann } 1223b3c0f300SAndre Oppermann 122475497cc5SBjoern A. Zeeb #ifdef IPSEC 122575497cc5SBjoern A. Zeeb KASSERT(len + hdrlen + ipoptlen - ipsec_optlen == m_length(m, NULL), 122675497cc5SBjoern A. Zeeb ("%s: mbuf chain shorter than expected: %ld + %u + %u - %u != %u", 122775497cc5SBjoern A. Zeeb __func__, len, hdrlen, ipoptlen, ipsec_optlen, m_length(m, NULL))); 122875497cc5SBjoern A. Zeeb #else 1229ed420311SAndre Oppermann KASSERT(len + hdrlen + ipoptlen == m_length(m, NULL), 123075497cc5SBjoern A. Zeeb ("%s: mbuf chain shorter than expected: %ld + %u + %u != %u", 123175497cc5SBjoern A. Zeeb __func__, len, hdrlen, ipoptlen, m_length(m, NULL))); 123275497cc5SBjoern A. Zeeb #endif 1233ed420311SAndre Oppermann 123439bc9de5SLawrence Stewart /* Run HHOOK_TCP_ESTABLISHED_OUT helper hooks. */ 123539bc9de5SLawrence Stewart hhook_run_tcp_est_out(tp, th, &to, len, tso); 123639bc9de5SLawrence Stewart 1237610ee2f9SDavid Greenman #ifdef TCPDEBUG 1238df8bae1dSRodney W. Grimes /* 1239df8bae1dSRodney W. Grimes * Trace. 1240df8bae1dSRodney W. Grimes */ 124191f467d5SHartmut Brandt if (so->so_options & SO_DEBUG) { 1242f3e0b7efSBruce M Simpson u_short save = 0; 12435214cb3fSBruce M Simpson #ifdef INET6 12445214cb3fSBruce M Simpson if (!isipv6) 12455214cb3fSBruce M Simpson #endif 12465214cb3fSBruce M Simpson { 12475214cb3fSBruce M Simpson save = ipov->ih_len; 124891f467d5SHartmut Brandt ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */); 12495214cb3fSBruce M Simpson } 1250fb59c426SYoshinobu Inoue tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 12515214cb3fSBruce M Simpson #ifdef INET6 12525214cb3fSBruce M Simpson if (!isipv6) 12535214cb3fSBruce M Simpson #endif 125491f467d5SHartmut Brandt ipov->ih_len = save; 125591f467d5SHartmut Brandt } 1256b287c6c7SBjoern A. Zeeb #endif /* TCPDEBUG */ 1257df8bae1dSRodney W. Grimes 1258df8bae1dSRodney W. Grimes /* 1259df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 1260df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 1261df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 1262df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 1263df8bae1dSRodney W. Grimes */ 1264fb59c426SYoshinobu Inoue /* 126543630e62SHiren Panchasara * m->m_pkthdr.len should have been set before checksum calculation, 1266fb59c426SYoshinobu Inoue * because in6_cksum() need it. 1267fb59c426SYoshinobu Inoue */ 1268fb59c426SYoshinobu Inoue #ifdef INET6 1269fb59c426SYoshinobu Inoue if (isipv6) { 1270df0633a1SGleb Smirnoff struct route_in6 ro; 1271df0633a1SGleb Smirnoff 1272df0633a1SGleb Smirnoff bzero(&ro, sizeof(ro)); 1273fb59c426SYoshinobu Inoue /* 1274fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 1275fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 1276fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 1277fb59c426SYoshinobu Inoue * Neighbor Discovery. 1278fb59c426SYoshinobu Inoue */ 127997d8d152SAndre Oppermann ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1280fb59c426SYoshinobu Inoue 128157f60867SMark Johnston /* 128257f60867SMark Johnston * Set the packet size here for the benefit of DTrace probes. 128357f60867SMark Johnston * ip6_output() will set it properly; it's supposed to include 128457f60867SMark Johnston * the option header lengths as well. 128557f60867SMark Johnston */ 128657f60867SMark Johnston ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(*ip6)); 128757f60867SMark Johnston 12880f3e3bc5SSean Bruno if (V_path_mtu_discovery && tp->t_maxopd > V_tcp_minmss) 12890f3e3bc5SSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 12900f3e3bc5SSean Bruno else 12910f3e3bc5SSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 12920f3e3bc5SSean Bruno 129357f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1294d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip6, tp, th); 129557f60867SMark Johnston 129657f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip6, tp, th); 129757f60867SMark Johnston 1298fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 1299df0633a1SGleb Smirnoff error = ip6_output(m, tp->t_inpcb->in6p_outputopts, &ro, 1300df0633a1SGleb Smirnoff ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 1301df0633a1SGleb Smirnoff NULL, NULL, tp->t_inpcb); 1302df0633a1SGleb Smirnoff 1303df0633a1SGleb Smirnoff if (error == EMSGSIZE && ro.ro_rt != NULL) 1304e3a7aa6fSGleb Smirnoff mtu = ro.ro_rt->rt_mtu; 1305df0633a1SGleb Smirnoff RO_RTFREE(&ro); 1306b287c6c7SBjoern A. Zeeb } 1307fb59c426SYoshinobu Inoue #endif /* INET6 */ 1308b287c6c7SBjoern A. Zeeb #if defined(INET) && defined(INET6) 1309b287c6c7SBjoern A. Zeeb else 1310b287c6c7SBjoern A. Zeeb #endif 1311b287c6c7SBjoern A. Zeeb #ifdef INET 1312df8bae1dSRodney W. Grimes { 1313df0633a1SGleb Smirnoff struct route ro; 1314df0633a1SGleb Smirnoff 1315df0633a1SGleb Smirnoff bzero(&ro, sizeof(ro)); 13168f134647SGleb Smirnoff ip->ip_len = htons(m->m_pkthdr.len); 1317686cdd19SJun-ichiro itojun Hagino #ifdef INET6 13185cd54324SBjoern A. Zeeb if (tp->t_inpcb->inp_vflag & INP_IPV6PROTO) 131997d8d152SAndre Oppermann ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL); 1320686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 1321f138387aSGarrett Wollman /* 132297d8d152SAndre Oppermann * If we do path MTU discovery, then we set DF on every packet. 132397d8d152SAndre Oppermann * This might not be the best thing to do according to RFC3390 132497d8d152SAndre Oppermann * Section 2. However the tcp hostcache migitates the problem 132597d8d152SAndre Oppermann * so it affects only the first tcp connection with a host. 1326e4e92660SAndre Oppermann * 1327e4e92660SAndre Oppermann * NB: Don't set DF on small MTU/MSS to have a safe fallback. 1328f138387aSGarrett Wollman */ 1329f6f6703fSSean Bruno if (V_path_mtu_discovery && tp->t_maxopd > V_tcp_minmss) { 13308f134647SGleb Smirnoff ip->ip_off |= htons(IP_DF); 1331f6f6703fSSean Bruno tp->t_flags2 |= TF2_PLPMTU_PMTUD; 1332f6f6703fSSean Bruno } else { 1333f6f6703fSSean Bruno tp->t_flags2 &= ~TF2_PLPMTU_PMTUD; 1334f6f6703fSSean Bruno } 133597d8d152SAndre Oppermann 133657f60867SMark Johnston if (tp->t_state == TCPS_SYN_SENT) 1337d9fae5abSAndriy Gapon TCP_PROBE5(connect__request, NULL, tp, ip, tp, th); 133857f60867SMark Johnston 133957f60867SMark Johnston TCP_PROBE5(send, NULL, tp, ip, tp, th); 134057f60867SMark Johnston 1341df0633a1SGleb Smirnoff error = ip_output(m, tp->t_inpcb->inp_options, &ro, 1342b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 1343b5d47ff5SJohn-Mark Gurney tp->t_inpcb); 1344df0633a1SGleb Smirnoff 1345df0633a1SGleb Smirnoff if (error == EMSGSIZE && ro.ro_rt != NULL) 1346e3a7aa6fSGleb Smirnoff mtu = ro.ro_rt->rt_mtu; 1347df0633a1SGleb Smirnoff RO_RTFREE(&ro); 1348df8bae1dSRodney W. Grimes } 1349b287c6c7SBjoern A. Zeeb #endif /* INET */ 13500e2bc05cSGleb Smirnoff 13510e2bc05cSGleb Smirnoff out: 13520e2bc05cSGleb Smirnoff /* 13530e2bc05cSGleb Smirnoff * In transmit state, time the transmission and arrange for 13540e2bc05cSGleb Smirnoff * the retransmit. In persist state, just set snd_max. 13550e2bc05cSGleb Smirnoff */ 13560e2bc05cSGleb Smirnoff if ((tp->t_flags & TF_FORCEDATA) == 0 || 13570e2bc05cSGleb Smirnoff !tcp_timer_active(tp, TT_PERSIST)) { 13580e2bc05cSGleb Smirnoff tcp_seq startseq = tp->snd_nxt; 13590e2bc05cSGleb Smirnoff 13600e2bc05cSGleb Smirnoff /* 13610e2bc05cSGleb Smirnoff * Advance snd_nxt over sequence space of this segment. 13620e2bc05cSGleb Smirnoff */ 13630e2bc05cSGleb Smirnoff if (flags & (TH_SYN|TH_FIN)) { 13640e2bc05cSGleb Smirnoff if (flags & TH_SYN) 13650e2bc05cSGleb Smirnoff tp->snd_nxt++; 13660e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 13670e2bc05cSGleb Smirnoff tp->snd_nxt++; 13680e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 13690e2bc05cSGleb Smirnoff } 13700e2bc05cSGleb Smirnoff } 13710e2bc05cSGleb Smirnoff if (sack_rxmit) 13720e2bc05cSGleb Smirnoff goto timer; 13730e2bc05cSGleb Smirnoff tp->snd_nxt += len; 13740e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 13750e2bc05cSGleb Smirnoff tp->snd_max = tp->snd_nxt; 13760e2bc05cSGleb Smirnoff /* 13770e2bc05cSGleb Smirnoff * Time this transmission if not a retransmission and 13780e2bc05cSGleb Smirnoff * not currently timing anything. 13790e2bc05cSGleb Smirnoff */ 13800e2bc05cSGleb Smirnoff if (tp->t_rtttime == 0) { 13810e2bc05cSGleb Smirnoff tp->t_rtttime = ticks; 13820e2bc05cSGleb Smirnoff tp->t_rtseq = startseq; 13830e2bc05cSGleb Smirnoff TCPSTAT_INC(tcps_segstimed); 13840e2bc05cSGleb Smirnoff } 13850e2bc05cSGleb Smirnoff } 13860e2bc05cSGleb Smirnoff 13870e2bc05cSGleb Smirnoff /* 13880e2bc05cSGleb Smirnoff * Set retransmit timer if not currently set, 13890e2bc05cSGleb Smirnoff * and not doing a pure ack or a keep-alive probe. 13900e2bc05cSGleb Smirnoff * Initial value for retransmit timer is smoothed 13910e2bc05cSGleb Smirnoff * round-trip time + 2 * round-trip time variance. 13920e2bc05cSGleb Smirnoff * Initialize shift counter which is used for backoff 13930e2bc05cSGleb Smirnoff * of retransmit time. 13940e2bc05cSGleb Smirnoff */ 13950e2bc05cSGleb Smirnoff timer: 13960e2bc05cSGleb Smirnoff if (!tcp_timer_active(tp, TT_REXMT) && 13970e2bc05cSGleb Smirnoff ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 13980e2bc05cSGleb Smirnoff (tp->snd_nxt != tp->snd_una))) { 13990e2bc05cSGleb Smirnoff if (tcp_timer_active(tp, TT_PERSIST)) { 14000e2bc05cSGleb Smirnoff tcp_timer_activate(tp, TT_PERSIST, 0); 14010e2bc05cSGleb Smirnoff tp->t_rxtshift = 0; 14020e2bc05cSGleb Smirnoff } 14030e2bc05cSGleb Smirnoff tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 1404f8568079SHiren Panchasara } else if (len == 0 && sbavail(&so->so_snd) && 1405f8568079SHiren Panchasara !tcp_timer_active(tp, TT_REXMT) && 1406f8568079SHiren Panchasara !tcp_timer_active(tp, TT_PERSIST)) { 1407f8568079SHiren Panchasara /* 1408f8568079SHiren Panchasara * Avoid a situation where we do not set persist timer 1409f8568079SHiren Panchasara * after a zero window condition. For example: 1410f8568079SHiren Panchasara * 1) A -> B: packet with enough data to fill the window 1411f8568079SHiren Panchasara * 2) B -> A: ACK for #1 + new data (0 window 1412f8568079SHiren Panchasara * advertisement) 1413f8568079SHiren Panchasara * 3) A -> B: ACK for #2, 0 len packet 1414f8568079SHiren Panchasara * 1415f8568079SHiren Panchasara * In this case, A will not activate the persist timer, 1416f8568079SHiren Panchasara * because it chose to send a packet. Unless tcp_output 1417f8568079SHiren Panchasara * is called for some other reason (delayed ack timer, 1418f8568079SHiren Panchasara * another input packet from B, socket syscall), A will 1419f8568079SHiren Panchasara * not send zero window probes. 1420f8568079SHiren Panchasara * 1421f8568079SHiren Panchasara * So, if you send a 0-length packet, but there is data 1422f8568079SHiren Panchasara * in the socket buffer, and neither the rexmt or 1423f8568079SHiren Panchasara * persist timer is already set, then activate the 1424f8568079SHiren Panchasara * persist timer. 1425f8568079SHiren Panchasara */ 1426f8568079SHiren Panchasara tp->t_rxtshift = 0; 1427f8568079SHiren Panchasara tcp_setpersist(tp); 14280e2bc05cSGleb Smirnoff } 14290e2bc05cSGleb Smirnoff } else { 14300e2bc05cSGleb Smirnoff /* 14310e2bc05cSGleb Smirnoff * Persist case, update snd_max but since we are in 14320e2bc05cSGleb Smirnoff * persist mode (no window) we do not update snd_nxt. 14330e2bc05cSGleb Smirnoff */ 14340e2bc05cSGleb Smirnoff int xlen = len; 14350e2bc05cSGleb Smirnoff if (flags & TH_SYN) 14360e2bc05cSGleb Smirnoff ++xlen; 14370e2bc05cSGleb Smirnoff if (flags & TH_FIN) { 14380e2bc05cSGleb Smirnoff ++xlen; 14390e2bc05cSGleb Smirnoff tp->t_flags |= TF_SENTFIN; 14400e2bc05cSGleb Smirnoff } 14410e2bc05cSGleb Smirnoff if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 14420e2bc05cSGleb Smirnoff tp->snd_max = tp->snd_nxt + len; 14430e2bc05cSGleb Smirnoff } 14440e2bc05cSGleb Smirnoff 1445df8bae1dSRodney W. Grimes if (error) { 14467734ea06SArchie Cobbs 14477734ea06SArchie Cobbs /* 14487734ea06SArchie Cobbs * We know that the packet was lost, so back out the 14497734ea06SArchie Cobbs * sequence number advance, if any. 14502c30ec0aSAndre Oppermann * 14512c30ec0aSAndre Oppermann * If the error is EPERM the packet got blocked by the 14522c30ec0aSAndre Oppermann * local firewall. Normally we should terminate the 14532c30ec0aSAndre Oppermann * connection but the blocking may have been spurious 14542c30ec0aSAndre Oppermann * due to a firewall reconfiguration cycle. So we treat 14552c30ec0aSAndre Oppermann * it like a packet loss and let the retransmit timer and 14562c30ec0aSAndre Oppermann * timeouts do their work over time. 14572c30ec0aSAndre Oppermann * XXX: It is a POLA question whether calling tcp_drop right 14582c30ec0aSAndre Oppermann * away would be the really correct behavior instead. 14597734ea06SArchie Cobbs */ 146072757d9aSGleb Smirnoff if (((tp->t_flags & TF_FORCEDATA) == 0 || 1461b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) && 146272757d9aSGleb Smirnoff ((flags & TH_SYN) == 0) && 146372757d9aSGleb Smirnoff (error != EPERM)) { 14640077b016SPaul Saab if (sack_rxmit) { 14655d3b1b75SJayanth Vijayaraghavan p->rxmit -= len; 14660077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= len; 146772757d9aSGleb Smirnoff KASSERT(tp->sackhint.sack_bytes_rexmit >= 0, 14680077b016SPaul Saab ("sackhint bytes rtx >= 0")); 14690077b016SPaul Saab } else 14707734ea06SArchie Cobbs tp->snd_nxt -= len; 14717734ea06SArchie Cobbs } 1472cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */ 147372757d9aSGleb Smirnoff switch (error) { 147472757d9aSGleb Smirnoff case EPERM: 147572757d9aSGleb Smirnoff tp->t_softerror = error; 147672757d9aSGleb Smirnoff return (error); 147772757d9aSGleb Smirnoff case ENOBUFS: 1478b8152ba7SAndre Oppermann if (!tcp_timer_active(tp, TT_REXMT) && 1479b8152ba7SAndre Oppermann !tcp_timer_active(tp, TT_PERSIST)) 1480b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_REXMT, tp->t_rxtcur); 14811600372bSAndre Oppermann tp->snd_cwnd = tp->t_maxseg; 1482df8bae1dSRodney W. Grimes return (0); 148372757d9aSGleb Smirnoff case EMSGSIZE: 14843d1f141bSGarrett Wollman /* 1485b3c0f300SAndre Oppermann * For some reason the interface we used initially 1486b3c0f300SAndre Oppermann * to send segments changed to another or lowered 1487b3c0f300SAndre Oppermann * its MTU. 1488b3c0f300SAndre Oppermann * If TSO was active we either got an interface 1489b3c0f300SAndre Oppermann * without TSO capabilits or TSO was turned off. 1490df0633a1SGleb Smirnoff * If we obtained mtu from ip_output() then update 1491df0633a1SGleb Smirnoff * it and try again. 14923d1f141bSGarrett Wollman */ 1493b3c0f300SAndre Oppermann if (tso) 1494b3c0f300SAndre Oppermann tp->t_flags &= ~TF_TSO; 1495df0633a1SGleb Smirnoff if (mtu != 0) { 1496df0633a1SGleb Smirnoff tcp_mss_update(tp, -1, mtu, NULL, NULL); 1497df0633a1SGleb Smirnoff goto again; 1498df0633a1SGleb Smirnoff } 1499df0633a1SGleb Smirnoff return (error); 15008bec3467SGleb Smirnoff case EHOSTDOWN: 150172757d9aSGleb Smirnoff case EHOSTUNREACH: 150272757d9aSGleb Smirnoff case ENETDOWN: 15038bec3467SGleb Smirnoff case ENETUNREACH: 150472757d9aSGleb Smirnoff if (TCPS_HAVERCVDSYN(tp->t_state)) { 1505df8bae1dSRodney W. Grimes tp->t_softerror = error; 1506df8bae1dSRodney W. Grimes return (0); 1507df8bae1dSRodney W. Grimes } 150872757d9aSGleb Smirnoff /* FALLTHROUGH */ 150972757d9aSGleb Smirnoff default: 1510df8bae1dSRodney W. Grimes return (error); 1511df8bae1dSRodney W. Grimes } 151272757d9aSGleb Smirnoff } 151378b50714SRobert Watson TCPSTAT_INC(tcps_sndtotal); 1514df8bae1dSRodney W. Grimes 1515df8bae1dSRodney W. Grimes /* 1516df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 1517df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 1518df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 1519df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 1520df8bae1dSRodney W. Grimes */ 15216b7c15e5SJohn Baldwin if (recwin >= 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1522201d185bSAndre Oppermann tp->rcv_adv = tp->rcv_nxt + recwin; 1523df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 15243bfd6421SJonathan Lemon tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 1525b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_DELACK)) 1526b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_DELACK, 0); 1527d912c694SMatthew Dillon #if 0 1528d912c694SMatthew Dillon /* 1529d912c694SMatthew Dillon * This completely breaks TCP if newreno is turned on. What happens 1530d912c694SMatthew Dillon * is that if delayed-acks are turned on on the receiver, this code 1531d912c694SMatthew Dillon * on the transmitter effectively destroys the TCP window, forcing 1532d912c694SMatthew Dillon * it to four packets (1.5Kx4 = 6K window). 1533d912c694SMatthew Dillon */ 1534dbc42409SLawrence Stewart if (sendalot && --maxburst) 1535df8bae1dSRodney W. Grimes goto again; 1536d912c694SMatthew Dillon #endif 1537d912c694SMatthew Dillon if (sendalot) 1538d912c694SMatthew Dillon goto again; 1539df8bae1dSRodney W. Grimes return (0); 1540df8bae1dSRodney W. Grimes } 1541df8bae1dSRodney W. Grimes 1542df8bae1dSRodney W. Grimes void 1543ad3f9ab3SAndre Oppermann tcp_setpersist(struct tcpcb *tp) 1544df8bae1dSRodney W. Grimes { 15459b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 15469b8b58e0SJonathan Lemon int tt; 1547df8bae1dSRodney W. Grimes 1548672dc4aeSJohn Baldwin tp->t_flags &= ~TF_PREVVALID; 1549b8152ba7SAndre Oppermann if (tcp_timer_active(tp, TT_REXMT)) 15509b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 1551df8bae1dSRodney W. Grimes /* 1552df8bae1dSRodney W. Grimes * Start/restart persistance timer. 1553df8bae1dSRodney W. Grimes */ 15549b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 1555df8bae1dSRodney W. Grimes TCPTV_PERSMIN, TCPTV_PERSMAX); 1556b8152ba7SAndre Oppermann tcp_timer_activate(tp, TT_PERSIST, tt); 1557df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1558df8bae1dSRodney W. Grimes tp->t_rxtshift++; 1559df8bae1dSRodney W. Grimes } 156002a1a643SAndre Oppermann 156102a1a643SAndre Oppermann /* 156202a1a643SAndre Oppermann * Insert TCP options according to the supplied parameters to the place 156302a1a643SAndre Oppermann * optp in a consistent way. Can handle unaligned destinations. 156402a1a643SAndre Oppermann * 156502a1a643SAndre Oppermann * The order of the option processing is crucial for optimal packing and 156602a1a643SAndre Oppermann * alignment for the scarce option space. 156702a1a643SAndre Oppermann * 156802a1a643SAndre Oppermann * The optimal order for a SYN/SYN-ACK segment is: 156902a1a643SAndre Oppermann * MSS (4) + NOP (1) + Window scale (3) + SACK permitted (2) + 157002a1a643SAndre Oppermann * Timestamp (10) + Signature (18) = 38 bytes out of a maximum of 40. 157102a1a643SAndre Oppermann * 157202a1a643SAndre Oppermann * The SACK options should be last. SACK blocks consume 8*n+2 bytes. 157302a1a643SAndre Oppermann * So a full size SACK blocks option is 34 bytes (with 4 SACK blocks). 157402a1a643SAndre Oppermann * At minimum we need 10 bytes (to generate 1 SACK block). If both 157502a1a643SAndre Oppermann * TCP Timestamps (12 bytes) and TCP Signatures (18 bytes) are present, 157602a1a643SAndre Oppermann * we only have 10 bytes for SACK options (40 - (12 + 18)). 157702a1a643SAndre Oppermann */ 157802a1a643SAndre Oppermann int 157902a1a643SAndre Oppermann tcp_addoptions(struct tcpopt *to, u_char *optp) 158002a1a643SAndre Oppermann { 158102a1a643SAndre Oppermann u_int mask, optlen = 0; 158202a1a643SAndre Oppermann 158302a1a643SAndre Oppermann for (mask = 1; mask < TOF_MAXOPT; mask <<= 1) { 158402a1a643SAndre Oppermann if ((to->to_flags & mask) != mask) 158502a1a643SAndre Oppermann continue; 15863a4018c4SAndre Oppermann if (optlen == TCP_MAXOLEN) 15873a4018c4SAndre Oppermann break; 158802a1a643SAndre Oppermann switch (to->to_flags & mask) { 158902a1a643SAndre Oppermann case TOF_MSS: 159002a1a643SAndre Oppermann while (optlen % 4) { 159102a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 159202a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 159302a1a643SAndre Oppermann } 15943a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_MAXSEG) 15953a4018c4SAndre Oppermann continue; 159602a1a643SAndre Oppermann optlen += TCPOLEN_MAXSEG; 159702a1a643SAndre Oppermann *optp++ = TCPOPT_MAXSEG; 159802a1a643SAndre Oppermann *optp++ = TCPOLEN_MAXSEG; 159902a1a643SAndre Oppermann to->to_mss = htons(to->to_mss); 160002a1a643SAndre Oppermann bcopy((u_char *)&to->to_mss, optp, sizeof(to->to_mss)); 160102a1a643SAndre Oppermann optp += sizeof(to->to_mss); 160202a1a643SAndre Oppermann break; 160302a1a643SAndre Oppermann case TOF_SCALE: 160402a1a643SAndre Oppermann while (!optlen || optlen % 2 != 1) { 160502a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 160602a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 160702a1a643SAndre Oppermann } 16083a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_WINDOW) 16093a4018c4SAndre Oppermann continue; 161002a1a643SAndre Oppermann optlen += TCPOLEN_WINDOW; 161102a1a643SAndre Oppermann *optp++ = TCPOPT_WINDOW; 161202a1a643SAndre Oppermann *optp++ = TCPOLEN_WINDOW; 161302a1a643SAndre Oppermann *optp++ = to->to_wscale; 161402a1a643SAndre Oppermann break; 161502a1a643SAndre Oppermann case TOF_SACKPERM: 161602a1a643SAndre Oppermann while (optlen % 2) { 161702a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 161802a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 161902a1a643SAndre Oppermann } 16203a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACK_PERMITTED) 16213a4018c4SAndre Oppermann continue; 162202a1a643SAndre Oppermann optlen += TCPOLEN_SACK_PERMITTED; 162302a1a643SAndre Oppermann *optp++ = TCPOPT_SACK_PERMITTED; 162402a1a643SAndre Oppermann *optp++ = TCPOLEN_SACK_PERMITTED; 162502a1a643SAndre Oppermann break; 162602a1a643SAndre Oppermann case TOF_TS: 162702a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 162802a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 162902a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 163002a1a643SAndre Oppermann } 16313a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_TIMESTAMP) 16323a4018c4SAndre Oppermann continue; 163302a1a643SAndre Oppermann optlen += TCPOLEN_TIMESTAMP; 163402a1a643SAndre Oppermann *optp++ = TCPOPT_TIMESTAMP; 163502a1a643SAndre Oppermann *optp++ = TCPOLEN_TIMESTAMP; 163602a1a643SAndre Oppermann to->to_tsval = htonl(to->to_tsval); 163702a1a643SAndre Oppermann to->to_tsecr = htonl(to->to_tsecr); 163802a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsval, optp, sizeof(to->to_tsval)); 163902a1a643SAndre Oppermann optp += sizeof(to->to_tsval); 164002a1a643SAndre Oppermann bcopy((u_char *)&to->to_tsecr, optp, sizeof(to->to_tsecr)); 164102a1a643SAndre Oppermann optp += sizeof(to->to_tsecr); 164202a1a643SAndre Oppermann break; 164302a1a643SAndre Oppermann case TOF_SIGNATURE: 164402a1a643SAndre Oppermann { 164502a1a643SAndre Oppermann int siglen = TCPOLEN_SIGNATURE - 2; 164602a1a643SAndre Oppermann 164702a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 164802a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 164902a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 165002a1a643SAndre Oppermann } 16510d957bbaSAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SIGNATURE) 165202a1a643SAndre Oppermann continue; 165302a1a643SAndre Oppermann optlen += TCPOLEN_SIGNATURE; 165402a1a643SAndre Oppermann *optp++ = TCPOPT_SIGNATURE; 165502a1a643SAndre Oppermann *optp++ = TCPOLEN_SIGNATURE; 165602a1a643SAndre Oppermann to->to_signature = optp; 165702a1a643SAndre Oppermann while (siglen--) 165802a1a643SAndre Oppermann *optp++ = 0; 165902a1a643SAndre Oppermann break; 166002a1a643SAndre Oppermann } 166102a1a643SAndre Oppermann case TOF_SACK: 166202a1a643SAndre Oppermann { 166302a1a643SAndre Oppermann int sackblks = 0; 166402a1a643SAndre Oppermann struct sackblk *sack = (struct sackblk *)to->to_sacks; 166502a1a643SAndre Oppermann tcp_seq sack_seq; 166602a1a643SAndre Oppermann 166702a1a643SAndre Oppermann while (!optlen || optlen % 4 != 2) { 166802a1a643SAndre Oppermann optlen += TCPOLEN_NOP; 166902a1a643SAndre Oppermann *optp++ = TCPOPT_NOP; 167002a1a643SAndre Oppermann } 16713a4018c4SAndre Oppermann if (TCP_MAXOLEN - optlen < TCPOLEN_SACKHDR + TCPOLEN_SACK) 167202a1a643SAndre Oppermann continue; 167302a1a643SAndre Oppermann optlen += TCPOLEN_SACKHDR; 167402a1a643SAndre Oppermann *optp++ = TCPOPT_SACK; 167502a1a643SAndre Oppermann sackblks = min(to->to_nsacks, 16760d957bbaSAndre Oppermann (TCP_MAXOLEN - optlen) / TCPOLEN_SACK); 167702a1a643SAndre Oppermann *optp++ = TCPOLEN_SACKHDR + sackblks * TCPOLEN_SACK; 167802a1a643SAndre Oppermann while (sackblks--) { 167902a1a643SAndre Oppermann sack_seq = htonl(sack->start); 168002a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 168102a1a643SAndre Oppermann optp += sizeof(sack_seq); 168202a1a643SAndre Oppermann sack_seq = htonl(sack->end); 168302a1a643SAndre Oppermann bcopy((u_char *)&sack_seq, optp, sizeof(sack_seq)); 168402a1a643SAndre Oppermann optp += sizeof(sack_seq); 168502a1a643SAndre Oppermann optlen += TCPOLEN_SACK; 168602a1a643SAndre Oppermann sack++; 168702a1a643SAndre Oppermann } 168878b50714SRobert Watson TCPSTAT_INC(tcps_sack_send_blocks); 168902a1a643SAndre Oppermann break; 169002a1a643SAndre Oppermann } 169102a1a643SAndre Oppermann default: 169202a1a643SAndre Oppermann panic("%s: unknown TCP option type", __func__); 169302a1a643SAndre Oppermann break; 169402a1a643SAndre Oppermann } 169502a1a643SAndre Oppermann } 169602a1a643SAndre Oppermann 169702a1a643SAndre Oppermann /* Terminate and pad TCP options to a 4 byte boundary. */ 169802a1a643SAndre Oppermann if (optlen % 4) { 169902a1a643SAndre Oppermann optlen += TCPOLEN_EOL; 170002a1a643SAndre Oppermann *optp++ = TCPOPT_EOL; 170102a1a643SAndre Oppermann } 1702413deb12SBjoern A. Zeeb /* 1703413deb12SBjoern A. Zeeb * According to RFC 793 (STD0007): 1704413deb12SBjoern A. Zeeb * "The content of the header beyond the End-of-Option option 1705413deb12SBjoern A. Zeeb * must be header padding (i.e., zero)." 1706413deb12SBjoern A. Zeeb * and later: "The padding is composed of zeros." 1707413deb12SBjoern A. Zeeb */ 170802a1a643SAndre Oppermann while (optlen % 4) { 1709c343c524SAndre Oppermann optlen += TCPOLEN_PAD; 1710c343c524SAndre Oppermann *optp++ = TCPOPT_PAD; 171102a1a643SAndre Oppermann } 171202a1a643SAndre Oppermann 17130d957bbaSAndre Oppermann KASSERT(optlen <= TCP_MAXOLEN, ("%s: TCP options too long", __func__)); 171402a1a643SAndre Oppermann return (optlen); 171502a1a643SAndre Oppermann } 1716