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 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 331cfd4b53SBruce M Simpson #include "opt_inet.h" 34fb59c426SYoshinobu Inoue #include "opt_inet6.h" 353a2a9f79SYoshinobu Inoue #include "opt_ipsec.h" 36c488362eSRobert Watson #include "opt_mac.h" 370cc12cc5SJoerg Wunsch #include "opt_tcpdebug.h" 386d90faf3SPaul Saab #include "opt_tcp_sack.h" 390cc12cc5SJoerg Wunsch 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41df8bae1dSRodney W. Grimes #include <sys/systm.h> 42686cdd19SJun-ichiro itojun Hagino #include <sys/domain.h> 43fb919e4dSMark Murray #include <sys/kernel.h> 44fb919e4dSMark Murray #include <sys/lock.h> 45fb919e4dSMark Murray #include <sys/mbuf.h> 46fb919e4dSMark Murray #include <sys/mutex.h> 47df8bae1dSRodney W. Grimes #include <sys/protosw.h> 48df8bae1dSRodney W. Grimes #include <sys/socket.h> 49df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 50fb919e4dSMark Murray #include <sys/sysctl.h> 51df8bae1dSRodney W. Grimes 52df8bae1dSRodney W. Grimes #include <net/route.h> 53df8bae1dSRodney W. Grimes 54df8bae1dSRodney W. Grimes #include <netinet/in.h> 55df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 56df8bae1dSRodney W. Grimes #include <netinet/ip.h> 57df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h> 58df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 59ef39adf0SAndre Oppermann #include <netinet/ip_options.h> 60fb59c426SYoshinobu Inoue #ifdef INET6 61686cdd19SJun-ichiro itojun Hagino #include <netinet6/in6_pcb.h> 62686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 63fb59c426SYoshinobu Inoue #include <netinet6/ip6_var.h> 64fb59c426SYoshinobu Inoue #endif 65df8bae1dSRodney W. Grimes #include <netinet/tcp.h> 66df8bae1dSRodney W. Grimes #define TCPOUTFLAGS 67df8bae1dSRodney W. Grimes #include <netinet/tcp_fsm.h> 68df8bae1dSRodney W. Grimes #include <netinet/tcp_seq.h> 69df8bae1dSRodney W. Grimes #include <netinet/tcp_timer.h> 70df8bae1dSRodney W. Grimes #include <netinet/tcp_var.h> 71df8bae1dSRodney W. Grimes #include <netinet/tcpip.h> 72610ee2f9SDavid Greenman #ifdef TCPDEBUG 73df8bae1dSRodney W. Grimes #include <netinet/tcp_debug.h> 74610ee2f9SDavid Greenman #endif 75df8bae1dSRodney W. Grimes 763a2a9f79SYoshinobu Inoue #ifdef IPSEC 773a2a9f79SYoshinobu Inoue #include <netinet6/ipsec.h> 783a2a9f79SYoshinobu Inoue #endif /*IPSEC*/ 793a2a9f79SYoshinobu Inoue 80b9234fafSSam Leffler #ifdef FAST_IPSEC 81b9234fafSSam Leffler #include <netipsec/ipsec.h> 82b9234fafSSam Leffler #define IPSEC 83b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 84b9234fafSSam Leffler 85db4f9cc7SJonathan Lemon #include <machine/in_cksum.h> 86db4f9cc7SJonathan Lemon 87aed55708SRobert Watson #include <security/mac/mac_framework.h> 88aed55708SRobert Watson 89df8bae1dSRodney W. Grimes #ifdef notyet 90df8bae1dSRodney W. Grimes extern struct mbuf *m_copypack(); 91df8bae1dSRodney W. Grimes #endif 92df8bae1dSRodney W. Grimes 93eb5afebaSMike Silbersack int path_mtu_discovery = 1; 949a039a5fSDavid Greenman SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW, 959a039a5fSDavid Greenman &path_mtu_discovery, 1, "Enable Path MTU Discovery"); 969a039a5fSDavid Greenman 979b8b58e0SJonathan Lemon int ss_fltsz = 1; 989b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW, 999b8b58e0SJonathan Lemon &ss_fltsz, 1, "Slow start flight size"); 1009b8b58e0SJonathan Lemon 1013260eb18SMike Silbersack int ss_fltsz_local = 4; 1029b8b58e0SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW, 1039b8b58e0SJonathan Lemon &ss_fltsz_local, 1, "Slow start flight size for local networks"); 104df8bae1dSRodney W. Grimes 1057d200109SJayanth Vijayaraghavan int tcp_do_newreno = 1; 10646f58482SJonathan Lemon SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno, 10746f58482SJonathan Lemon 0, "Enable NewReno Algorithms"); 108314e5a3dSJeffrey Hsu 109b3c0f300SAndre Oppermann int tcp_do_tso = 1; 110b3c0f300SAndre Oppermann SYSCTL_INT(_net_inet_tcp, OID_AUTO, tso, CTLFLAG_RW, 111b3c0f300SAndre Oppermann &tcp_do_tso, 0, "Enable TCP Segmentation Offload"); 112b3c0f300SAndre Oppermann 113df8bae1dSRodney W. Grimes /* 114df8bae1dSRodney W. Grimes * Tcp output routine: figure out what should be sent and send it. 115df8bae1dSRodney W. Grimes */ 116df8bae1dSRodney W. Grimes int 117f10e85d7SLuigi Rizzo tcp_output(struct tcpcb *tp) 118df8bae1dSRodney W. Grimes { 119f10e85d7SLuigi Rizzo struct socket *so = tp->t_inpcb->inp_socket; 120201d185bSAndre Oppermann long len, recwin, sendwin; 121df8bae1dSRodney W. Grimes int off, flags, error; 12245d370eeSBruce M Simpson #ifdef TCP_SIGNATURE 1231cfd4b53SBruce M Simpson int sigoff = 0; 124265ed012SBruce M Simpson #endif 125f10e85d7SLuigi Rizzo struct mbuf *m; 126fb59c426SYoshinobu Inoue struct ip *ip = NULL; 127f10e85d7SLuigi Rizzo struct ipovly *ipov = NULL; 128f10e85d7SLuigi Rizzo struct tcphdr *th; 129a0292f23SGarrett Wollman u_char opt[TCP_MAXOLEN]; 130a04884fcSBill Fenner unsigned ipoptlen, optlen, hdrlen; 131df8bae1dSRodney W. Grimes int idle, sendalot; 1326d90faf3SPaul Saab int i, sack_rxmit; 133a55db2b6SPaul Saab int sack_bytes_rxmt; 1346d90faf3SPaul Saab struct sackhole *p; 135b3c0f300SAndre Oppermann int tso = 0; 136262c1c1aSMatthew Dillon #if 0 13746f58482SJonathan Lemon int maxburst = TCP_MAXBURST; 138262c1c1aSMatthew Dillon #endif 139fb59c426SYoshinobu Inoue #ifdef INET6 140f10e85d7SLuigi Rizzo struct ip6_hdr *ip6 = NULL; 141fb59c426SYoshinobu Inoue int isipv6; 142fb59c426SYoshinobu Inoue 143fb59c426SYoshinobu Inoue isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0; 144fb59c426SYoshinobu Inoue #endif 145df8bae1dSRodney W. Grimes 146fa286d7dSSam Leffler INP_LOCK_ASSERT(tp->t_inpcb); 1473d6ade3aSJennifer Yang 148df8bae1dSRodney W. Grimes /* 149df8bae1dSRodney W. Grimes * Determine length of data that should be transmitted, 150df8bae1dSRodney W. Grimes * and flags that will be used. 151df8bae1dSRodney W. Grimes * If there is some data or critical controls (SYN, RST) 152df8bae1dSRodney W. Grimes * to send, then transmit; otherwise, investigate further. 153df8bae1dSRodney W. Grimes */ 154c24d5daeSJayanth Vijayaraghavan idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una); 1559b8b58e0SJonathan Lemon if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) { 156df8bae1dSRodney W. Grimes /* 157df8bae1dSRodney W. Grimes * We have been idle for "a while" and no acks are 158df8bae1dSRodney W. Grimes * expected to clock out any data we send -- 159df8bae1dSRodney W. Grimes * slow start to get ack "clock" running again. 1609b8b58e0SJonathan Lemon * 1619b8b58e0SJonathan Lemon * Set the slow-start flight size depending on whether 1629b8b58e0SJonathan Lemon * this is a local network or not. 163df8bae1dSRodney W. Grimes */ 164f10e85d7SLuigi Rizzo int ss = ss_fltsz; 165fb59c426SYoshinobu Inoue #ifdef INET6 166f10e85d7SLuigi Rizzo if (isipv6) { 167f10e85d7SLuigi Rizzo if (in6_localaddr(&tp->t_inpcb->in6p_faddr)) 168f10e85d7SLuigi Rizzo ss = ss_fltsz_local; 169f10e85d7SLuigi Rizzo } else 170f10e85d7SLuigi Rizzo #endif /* INET6 */ 171f10e85d7SLuigi Rizzo if (in_localaddr(tp->t_inpcb->inp_faddr)) 172f10e85d7SLuigi Rizzo ss = ss_fltsz_local; 173f10e85d7SLuigi Rizzo tp->snd_cwnd = tp->t_maxseg * ss; 1749b8b58e0SJonathan Lemon } 175c24d5daeSJayanth Vijayaraghavan tp->t_flags &= ~TF_LASTIDLE; 176c24d5daeSJayanth Vijayaraghavan if (idle) { 177c24d5daeSJayanth Vijayaraghavan if (tp->t_flags & TF_MORETOCOME) { 178c24d5daeSJayanth Vijayaraghavan tp->t_flags |= TF_LASTIDLE; 179c24d5daeSJayanth Vijayaraghavan idle = 0; 180c24d5daeSJayanth Vijayaraghavan } 181c24d5daeSJayanth Vijayaraghavan } 182df8bae1dSRodney W. Grimes again: 1836d90faf3SPaul Saab /* 1846d90faf3SPaul Saab * If we've recently taken a timeout, snd_max will be greater than 1856d90faf3SPaul Saab * snd_nxt. There may be SACK information that allows us to avoid 1866d90faf3SPaul Saab * resending already delivered data. Adjust snd_nxt accordingly. 1876d90faf3SPaul Saab */ 1886d90faf3SPaul Saab if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max)) 1896d90faf3SPaul Saab tcp_sack_adjust(tp); 190df8bae1dSRodney W. Grimes sendalot = 0; 191df8bae1dSRodney W. Grimes off = tp->snd_nxt - tp->snd_una; 192201d185bSAndre Oppermann sendwin = min(tp->snd_wnd, tp->snd_cwnd); 193201d185bSAndre Oppermann sendwin = min(sendwin, tp->snd_bwnd); 194df8bae1dSRodney W. Grimes 195df8bae1dSRodney W. Grimes flags = tcp_outflags[tp->t_state]; 196a0292f23SGarrett Wollman /* 1976d90faf3SPaul Saab * Send any SACK-generated retransmissions. If we're explicitly trying 1986d90faf3SPaul Saab * to send out new data (when sendalot is 1), bypass this function. 1996d90faf3SPaul Saab * If we retransmit in fast recovery mode, decrement snd_cwnd, since 2006d90faf3SPaul Saab * we're replacing a (future) new transmission with a retransmission 2016d90faf3SPaul Saab * now, and we previously incremented snd_cwnd in tcp_input(). 2026d90faf3SPaul Saab */ 2036d90faf3SPaul Saab /* 2046d90faf3SPaul Saab * Still in sack recovery , reset rxmit flag to zero. 2056d90faf3SPaul Saab */ 2066d90faf3SPaul Saab sack_rxmit = 0; 207a55db2b6SPaul Saab sack_bytes_rxmt = 0; 2086d90faf3SPaul Saab len = 0; 2096d90faf3SPaul Saab p = NULL; 21004f0d9a0SJayanth Vijayaraghavan if (tp->sack_enable && IN_FASTRECOVERY(tp) && 211a55db2b6SPaul Saab (p = tcp_sack_output(tp, &sack_bytes_rxmt))) { 212a55db2b6SPaul Saab long cwin; 213a55db2b6SPaul Saab 214a55db2b6SPaul Saab cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt; 215a55db2b6SPaul Saab if (cwin < 0) 216a55db2b6SPaul Saab cwin = 0; 217f787edd8SJayanth Vijayaraghavan /* Do not retransmit SACK segments beyond snd_recover */ 218f787edd8SJayanth Vijayaraghavan if (SEQ_GT(p->end, tp->snd_recover)) { 219f787edd8SJayanth Vijayaraghavan /* 220f787edd8SJayanth Vijayaraghavan * (At least) part of sack hole extends beyond 221f787edd8SJayanth Vijayaraghavan * snd_recover. Check to see if we can rexmit data 222f787edd8SJayanth Vijayaraghavan * for this hole. 223f787edd8SJayanth Vijayaraghavan */ 224f787edd8SJayanth Vijayaraghavan if (SEQ_GEQ(p->rxmit, tp->snd_recover)) { 225f787edd8SJayanth Vijayaraghavan /* 226f787edd8SJayanth Vijayaraghavan * Can't rexmit any more data for this hole. 227f787edd8SJayanth Vijayaraghavan * That data will be rexmitted in the next 228f787edd8SJayanth Vijayaraghavan * sack recovery episode, when snd_recover 229f787edd8SJayanth Vijayaraghavan * moves past p->rxmit. 230f787edd8SJayanth Vijayaraghavan */ 231f787edd8SJayanth Vijayaraghavan p = NULL; 232f787edd8SJayanth Vijayaraghavan goto after_sack_rexmit; 233f787edd8SJayanth Vijayaraghavan } else 234f787edd8SJayanth Vijayaraghavan /* Can rexmit part of the current hole */ 235a55db2b6SPaul Saab len = ((long)ulmin(cwin, 236f787edd8SJayanth Vijayaraghavan tp->snd_recover - p->rxmit)); 237f787edd8SJayanth Vijayaraghavan } else 238a55db2b6SPaul Saab len = ((long)ulmin(cwin, p->end - p->rxmit)); 2396d90faf3SPaul Saab off = p->rxmit - tp->snd_una; 240f787edd8SJayanth Vijayaraghavan KASSERT(off >= 0,("%s: sack block to the left of una : %d", 241f787edd8SJayanth Vijayaraghavan __func__, off)); 2426d90faf3SPaul Saab if (len > 0) { 243ab5c14d8SRobert Watson sack_rxmit = 1; 244ab5c14d8SRobert Watson sendalot = 1; 2456d90faf3SPaul Saab tcpstat.tcps_sack_rexmits++; 2466d90faf3SPaul Saab tcpstat.tcps_sack_rexmit_bytes += 2476d90faf3SPaul Saab min(len, tp->t_maxseg); 2486d90faf3SPaul Saab } 2496d90faf3SPaul Saab } 250f787edd8SJayanth Vijayaraghavan after_sack_rexmit: 2516d90faf3SPaul Saab /* 252a0292f23SGarrett Wollman * Get standard flags, and add SYN or FIN if requested by 'hidden' 253a0292f23SGarrett Wollman * state flags. 254a0292f23SGarrett Wollman */ 255a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDFIN) 256a0292f23SGarrett Wollman flags |= TH_FIN; 257a0292f23SGarrett Wollman if (tp->t_flags & TF_NEEDSYN) 258a0292f23SGarrett Wollman flags |= TH_SYN; 259a0292f23SGarrett Wollman 260cf2942b6SRobert Watson SOCKBUF_LOCK(&so->so_snd); 261df8bae1dSRodney W. Grimes /* 262df8bae1dSRodney W. Grimes * If in persist timeout with window of 0, send 1 byte. 263df8bae1dSRodney W. Grimes * Otherwise, if window is small but nonzero 264df8bae1dSRodney W. Grimes * and timer expired, we will send what we can 265df8bae1dSRodney W. Grimes * and go to transmit state. 266df8bae1dSRodney W. Grimes */ 2672cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) { 268201d185bSAndre Oppermann if (sendwin == 0) { 269df8bae1dSRodney W. Grimes /* 270df8bae1dSRodney W. Grimes * If we still have some data to send, then 271df8bae1dSRodney W. Grimes * clear the FIN bit. Usually this would 272df8bae1dSRodney W. Grimes * happen below when it realizes that we 273df8bae1dSRodney W. Grimes * aren't sending all the data. However, 27429089b51SJulian Elischer * if we have exactly 1 byte of unsent data, 275df8bae1dSRodney W. Grimes * then it won't clear the FIN bit below, 276df8bae1dSRodney W. Grimes * and if we are in persist state, we wind 277df8bae1dSRodney W. Grimes * up sending the packet without recording 278df8bae1dSRodney W. Grimes * that we sent the FIN bit. 279df8bae1dSRodney W. Grimes * 280df8bae1dSRodney W. Grimes * We can't just blindly clear the FIN bit, 281df8bae1dSRodney W. Grimes * because if we don't have any more data 282df8bae1dSRodney W. Grimes * to send then the probe will be the FIN 283df8bae1dSRodney W. Grimes * itself. 284df8bae1dSRodney W. Grimes */ 285df8bae1dSRodney W. Grimes if (off < so->so_snd.sb_cc) 286df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 287201d185bSAndre Oppermann sendwin = 1; 288df8bae1dSRodney W. Grimes } else { 2899b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 290df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 291df8bae1dSRodney W. Grimes } 292df8bae1dSRodney W. Grimes } 293df8bae1dSRodney W. Grimes 29428257b5cSMatthew Dillon /* 29528257b5cSMatthew Dillon * If snd_nxt == snd_max and we have transmitted a FIN, the 29628257b5cSMatthew Dillon * offset will be > 0 even if so_snd.sb_cc is 0, resulting in 297201d185bSAndre Oppermann * a negative length. This can also occur when TCP opens up 29828257b5cSMatthew Dillon * its congestion window while receiving additional duplicate 29928257b5cSMatthew Dillon * acks after fast-retransmit because TCP will reset snd_nxt 30028257b5cSMatthew Dillon * to snd_max after the fast-retransmit. 30128257b5cSMatthew Dillon * 30228257b5cSMatthew Dillon * In the normal retransmit-FIN-only case, however, snd_nxt will 30328257b5cSMatthew Dillon * be set to snd_una, the offset will be 0, and the length may 30428257b5cSMatthew Dillon * wind up 0. 3056d90faf3SPaul Saab * 3066d90faf3SPaul Saab * If sack_rxmit is true we are retransmitting from the scoreboard 3076d90faf3SPaul Saab * in which case len is already set. 30828257b5cSMatthew Dillon */ 309a55db2b6SPaul Saab if (sack_rxmit == 0) { 310a55db2b6SPaul Saab if (sack_bytes_rxmt == 0) 3116d90faf3SPaul Saab len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off); 312a55db2b6SPaul Saab else { 313a55db2b6SPaul Saab long cwin; 314a55db2b6SPaul Saab 315a55db2b6SPaul Saab /* 316a55db2b6SPaul Saab * We are inside of a SACK recovery episode and are 317a55db2b6SPaul Saab * sending new data, having retransmitted all the 318a55db2b6SPaul Saab * data possible in the scoreboard. 319a55db2b6SPaul Saab */ 3207d5ed1ceSPaul Saab len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd) 3217d5ed1ceSPaul Saab - off); 3228d03f2b5SPaul Saab /* 3238d03f2b5SPaul Saab * Don't remove this (len > 0) check ! 3248d03f2b5SPaul Saab * We explicitly check for len > 0 here (although it 3258d03f2b5SPaul Saab * isn't really necessary), to work around a gcc 3268d03f2b5SPaul Saab * optimization issue - to force gcc to compute 3278d03f2b5SPaul Saab * len above. Without this check, the computation 3288d03f2b5SPaul Saab * of len is bungled by the optimizer. 3298d03f2b5SPaul Saab */ 3308d03f2b5SPaul Saab if (len > 0) { 3317d5ed1ceSPaul Saab cwin = tp->snd_cwnd - 3327d5ed1ceSPaul Saab (tp->snd_nxt - tp->sack_newdata) - 333a55db2b6SPaul Saab sack_bytes_rxmt; 334a55db2b6SPaul Saab if (cwin < 0) 335a55db2b6SPaul Saab cwin = 0; 336a55db2b6SPaul Saab len = lmin(len, cwin); 337a55db2b6SPaul Saab } 338a55db2b6SPaul Saab } 3398d03f2b5SPaul Saab } 340a0292f23SGarrett Wollman 341a0292f23SGarrett Wollman /* 342a0292f23SGarrett Wollman * Lop off SYN bit if it has already been sent. However, if this 343a0292f23SGarrett Wollman * is SYN-SENT state and if segment contains data and if we don't 344a0292f23SGarrett Wollman * know that foreign host supports TAO, suppress sending segment. 345a0292f23SGarrett Wollman */ 346a0292f23SGarrett Wollman if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { 3474b8e98d6SQing Li if (tp->t_state != TCPS_SYN_RECEIVED) 348a0292f23SGarrett Wollman flags &= ~TH_SYN; 349a0292f23SGarrett Wollman off--, len++; 350a0292f23SGarrett Wollman } 351a0292f23SGarrett Wollman 35281165e48SAndras Olah /* 353c94c54e4SAndre Oppermann * Be careful not to send data and/or FIN on SYN segments. 35481165e48SAndras Olah * This measure is needed to prevent interoperability problems 35581165e48SAndras Olah * with not fully conformant TCP implementations. 35681165e48SAndras Olah */ 357c94c54e4SAndre Oppermann if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) { 35881165e48SAndras Olah len = 0; 35981165e48SAndras Olah flags &= ~TH_FIN; 36081165e48SAndras Olah } 36181165e48SAndras Olah 362df8bae1dSRodney W. Grimes if (len < 0) { 363df8bae1dSRodney W. Grimes /* 364df8bae1dSRodney W. Grimes * If FIN has been sent but not acked, 365df8bae1dSRodney W. Grimes * but we haven't been called to retransmit, 36628257b5cSMatthew Dillon * len will be < 0. Otherwise, window shrank 367df8bae1dSRodney W. Grimes * after we sent into it. If window shrank to 0, 3682d8266afSDavid Greenman * cancel pending retransmit, pull snd_nxt back 3692d8266afSDavid Greenman * to (closed) window, and set the persist timer 3702d8266afSDavid Greenman * if it isn't already going. If the window didn't 3712d8266afSDavid Greenman * close completely, just wait for an ACK. 372df8bae1dSRodney W. Grimes */ 373df8bae1dSRodney W. Grimes len = 0; 374201d185bSAndre Oppermann if (sendwin == 0) { 3759b8b58e0SJonathan Lemon callout_stop(tp->tt_rexmt); 3762d8266afSDavid Greenman tp->t_rxtshift = 0; 377df8bae1dSRodney W. Grimes tp->snd_nxt = tp->snd_una; 3789b8b58e0SJonathan Lemon if (!callout_active(tp->tt_persist)) 3792d8266afSDavid Greenman tcp_setpersist(tp); 380df8bae1dSRodney W. Grimes } 381df8bae1dSRodney W. Grimes } 38228257b5cSMatthew Dillon 38328257b5cSMatthew Dillon /* 38428257b5cSMatthew Dillon * len will be >= 0 after this point. Truncate to the maximum 385b3c0f300SAndre Oppermann * segment length or enable TCP Segmentation Offloading (if supported 386b3c0f300SAndre Oppermann * by hardware) and ensure that FIN is removed if the length no longer 387b3c0f300SAndre Oppermann * contains the last data byte. 388b3c0f300SAndre Oppermann * 389b3c0f300SAndre Oppermann * TSO may only be used if we are in a pure bulk sending state. The 390b3c0f300SAndre Oppermann * presence of TCP-MD5, SACK retransmits, SACK advertizements and 391b3c0f300SAndre Oppermann * IP options prevent using TSO. With TSO the TCP header is the same 392b3c0f300SAndre Oppermann * (except for the sequence number) for all generated packets. This 393b3c0f300SAndre Oppermann * makes it impossible to transmit any options which vary per generated 394b3c0f300SAndre Oppermann * segment or packet. 395b3c0f300SAndre Oppermann * 396b3c0f300SAndre Oppermann * The length of TSO bursts is limited to TCP_MAXWIN. That limit and 397b3c0f300SAndre Oppermann * removal of FIN (if not already catched here) are handled later after 398b3c0f300SAndre Oppermann * the exact length of the TCP options are known. 39928257b5cSMatthew Dillon */ 400df8bae1dSRodney W. Grimes if (len > tp->t_maxseg) { 401b3c0f300SAndre Oppermann if ((tp->t_flags & TF_TSO) && tcp_do_tso && 402b3c0f300SAndre Oppermann ((tp->t_flags & TF_SIGNATURE) == 0) && 403b3c0f300SAndre Oppermann tp->rcv_numsacks == 0 && sack_rxmit == 0 && 404b3c0f300SAndre Oppermann tp->t_inpcb->inp_options == NULL && 405b3c0f300SAndre Oppermann tp->t_inpcb->in6p_options == NULL && 406b3c0f300SAndre Oppermann tp->t_inpcb->inp_sp == NULL) { 407b3c0f300SAndre Oppermann tso = 1; 408b3c0f300SAndre Oppermann } else { 409df8bae1dSRodney W. Grimes len = tp->t_maxseg; 410df8bae1dSRodney W. Grimes sendalot = 1; 411b3c0f300SAndre Oppermann tso = 0; 412b3c0f300SAndre Oppermann } 413df8bae1dSRodney W. Grimes } 4145d3b1b75SJayanth Vijayaraghavan if (sack_rxmit) { 4155d3b1b75SJayanth Vijayaraghavan if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc)) 416df8bae1dSRodney W. Grimes flags &= ~TH_FIN; 4175d3b1b75SJayanth Vijayaraghavan } else { 4185d3b1b75SJayanth Vijayaraghavan if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) 4195d3b1b75SJayanth Vijayaraghavan flags &= ~TH_FIN; 4205d3b1b75SJayanth Vijayaraghavan } 421df8bae1dSRodney W. Grimes 422201d185bSAndre Oppermann recwin = sbspace(&so->so_rcv); 423df8bae1dSRodney W. Grimes 424df8bae1dSRodney W. Grimes /* 425262c1c1aSMatthew Dillon * Sender silly window avoidance. We transmit under the following 426262c1c1aSMatthew Dillon * conditions when len is non-zero: 427262c1c1aSMatthew Dillon * 428b3c0f300SAndre Oppermann * - We have a full segment (or more with TSO) 429262c1c1aSMatthew Dillon * - This is the last buffer in a write()/send() and we are 430262c1c1aSMatthew Dillon * either idle or running NODELAY 431262c1c1aSMatthew Dillon * - we've timed out (e.g. persist timer) 432262c1c1aSMatthew Dillon * - we have more then 1/2 the maximum send window's worth of 433262c1c1aSMatthew Dillon * data (receiver may be limited the window size) 434262c1c1aSMatthew Dillon * - we need to retransmit 435df8bae1dSRodney W. Grimes */ 436df8bae1dSRodney W. Grimes if (len) { 437b3c0f300SAndre Oppermann if (len >= tp->t_maxseg) 438df8bae1dSRodney W. Grimes goto send; 439262c1c1aSMatthew Dillon /* 440262c1c1aSMatthew Dillon * NOTE! on localhost connections an 'ack' from the remote 441262c1c1aSMatthew Dillon * end may occur synchronously with the output and cause 442262c1c1aSMatthew Dillon * us to flush a buffer queued with moretocome. XXX 443262c1c1aSMatthew Dillon * 444262c1c1aSMatthew Dillon * note: the len + off check is almost certainly unnecessary. 445262c1c1aSMatthew Dillon */ 446262c1c1aSMatthew Dillon if (!(tp->t_flags & TF_MORETOCOME) && /* normal case */ 447262c1c1aSMatthew Dillon (idle || (tp->t_flags & TF_NODELAY)) && 448262c1c1aSMatthew Dillon len + off >= so->so_snd.sb_cc && 449262c1c1aSMatthew Dillon (tp->t_flags & TF_NOPUSH) == 0) { 450df8bae1dSRodney W. Grimes goto send; 451262c1c1aSMatthew Dillon } 4522cdbfa66SPaul Saab if (tp->t_flags & TF_FORCEDATA) /* typ. timeout case */ 453df8bae1dSRodney W. Grimes goto send; 454a0292f23SGarrett Wollman if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) 455df8bae1dSRodney W. Grimes goto send; 456262c1c1aSMatthew Dillon if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */ 457df8bae1dSRodney W. Grimes goto send; 4586d90faf3SPaul Saab if (sack_rxmit) 4596d90faf3SPaul Saab goto send; 460df8bae1dSRodney W. Grimes } 461df8bae1dSRodney W. Grimes 462df8bae1dSRodney W. Grimes /* 463df8bae1dSRodney W. Grimes * Compare available window to amount of window 464df8bae1dSRodney W. Grimes * known to peer (as advertised window less 465df8bae1dSRodney W. Grimes * next expected input). If the difference is at least two 466df8bae1dSRodney W. Grimes * max size segments, or at least 50% of the maximum possible 467df8bae1dSRodney W. Grimes * window, then want to send a window update to peer. 4683bfd6421SJonathan Lemon * Skip this if the connection is in T/TCP half-open state. 469df8bae1dSRodney W. Grimes */ 470201d185bSAndre Oppermann if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) { 471df8bae1dSRodney W. Grimes /* 472df8bae1dSRodney W. Grimes * "adv" is the amount we can increase the window, 473df8bae1dSRodney W. Grimes * taking into account that we are limited by 474df8bae1dSRodney W. Grimes * TCP_MAXWIN << tp->rcv_scale. 475df8bae1dSRodney W. Grimes */ 476201d185bSAndre Oppermann long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) - 477df8bae1dSRodney W. Grimes (tp->rcv_adv - tp->rcv_nxt); 478df8bae1dSRodney W. Grimes 479df8bae1dSRodney W. Grimes if (adv >= (long) (2 * tp->t_maxseg)) 480df8bae1dSRodney W. Grimes goto send; 481df8bae1dSRodney W. Grimes if (2 * adv >= (long) so->so_rcv.sb_hiwat) 482df8bae1dSRodney W. Grimes goto send; 483df8bae1dSRodney W. Grimes } 484df8bae1dSRodney W. Grimes 485df8bae1dSRodney W. Grimes /* 48628257b5cSMatthew Dillon * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW 48728257b5cSMatthew Dillon * is also a catch-all for the retransmit timer timeout case. 488df8bae1dSRodney W. Grimes */ 489df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 490df8bae1dSRodney W. Grimes goto send; 491a0292f23SGarrett Wollman if ((flags & TH_RST) || 492a0292f23SGarrett Wollman ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) 493df8bae1dSRodney W. Grimes goto send; 494df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_una)) 495df8bae1dSRodney W. Grimes goto send; 496df8bae1dSRodney W. Grimes /* 497df8bae1dSRodney W. Grimes * If our state indicates that FIN should be sent 49828257b5cSMatthew Dillon * and we have not yet done so, then we need to send. 499df8bae1dSRodney W. Grimes */ 500df8bae1dSRodney W. Grimes if (flags & TH_FIN && 501df8bae1dSRodney W. Grimes ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) 502df8bae1dSRodney W. Grimes goto send; 5036d90faf3SPaul Saab /* 5046d90faf3SPaul Saab * In SACK, it is possible for tcp_output to fail to send a segment 5056d90faf3SPaul Saab * after the retransmission timer has been turned off. Make sure 5066d90faf3SPaul Saab * that the retransmission timer is set. 5076d90faf3SPaul Saab */ 5086d90faf3SPaul Saab if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) && 5096d90faf3SPaul Saab !callout_active(tp->tt_rexmt) && 5106d90faf3SPaul Saab !callout_active(tp->tt_persist)) { 5116d90faf3SPaul Saab callout_reset(tp->tt_rexmt, tp->t_rxtcur, 5126d90faf3SPaul Saab tcp_timer_rexmt, tp); 513cf2942b6SRobert Watson goto just_return; 5146d90faf3SPaul Saab } 515df8bae1dSRodney W. Grimes /* 516df8bae1dSRodney W. Grimes * TCP window updates are not reliable, rather a polling protocol 517df8bae1dSRodney W. Grimes * using ``persist'' packets is used to insure receipt of window 518df8bae1dSRodney W. Grimes * updates. The three ``states'' for the output side are: 519df8bae1dSRodney W. Grimes * idle not doing retransmits or persists 520df8bae1dSRodney W. Grimes * persisting to move a small or zero window 521df8bae1dSRodney W. Grimes * (re)transmitting and thereby not persisting 522df8bae1dSRodney W. Grimes * 5239b8b58e0SJonathan Lemon * callout_active(tp->tt_persist) 5249b8b58e0SJonathan Lemon * is true when we are in persist state. 5252cdbfa66SPaul Saab * (tp->t_flags & TF_FORCEDATA) 526df8bae1dSRodney W. Grimes * is set when we are called to send a persist packet. 5279b8b58e0SJonathan Lemon * callout_active(tp->tt_rexmt) 528df8bae1dSRodney W. Grimes * is set when we are retransmitting 529df8bae1dSRodney W. Grimes * The output side is idle when both timers are zero. 530df8bae1dSRodney W. Grimes * 531df8bae1dSRodney W. Grimes * If send window is too small, there is data to transmit, and no 532df8bae1dSRodney W. Grimes * retransmit or persist is pending, then go to persist state. 533df8bae1dSRodney W. Grimes * If nothing happens soon, send when timer expires: 534df8bae1dSRodney W. Grimes * if window is nonzero, transmit what we can, 535df8bae1dSRodney W. Grimes * otherwise force out a byte. 536df8bae1dSRodney W. Grimes */ 5379b8b58e0SJonathan Lemon if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) && 5389b8b58e0SJonathan Lemon !callout_active(tp->tt_persist)) { 539df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 540df8bae1dSRodney W. Grimes tcp_setpersist(tp); 541df8bae1dSRodney W. Grimes } 542df8bae1dSRodney W. Grimes 543df8bae1dSRodney W. Grimes /* 544df8bae1dSRodney W. Grimes * No reason to send a segment, just return. 545df8bae1dSRodney W. Grimes */ 546cf2942b6SRobert Watson just_return: 547cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 548df8bae1dSRodney W. Grimes return (0); 549df8bae1dSRodney W. Grimes 550df8bae1dSRodney W. Grimes send: 551cf2942b6SRobert Watson SOCKBUF_LOCK_ASSERT(&so->so_snd); 552df8bae1dSRodney W. Grimes /* 553df8bae1dSRodney W. Grimes * Before ESTABLISHED, force sending of initial options 554df8bae1dSRodney W. Grimes * unless TCP set not to do any options. 555df8bae1dSRodney W. Grimes * NOTE: we assume that the IP/TCP header plus TCP options 556df8bae1dSRodney W. Grimes * always fit in a single mbuf, leaving room for a maximum 557df8bae1dSRodney W. Grimes * link header, i.e. 55833841545SHajimu UMEMOTO * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES 559df8bae1dSRodney W. Grimes */ 560df8bae1dSRodney W. Grimes optlen = 0; 561fb59c426SYoshinobu Inoue #ifdef INET6 562fb59c426SYoshinobu Inoue if (isipv6) 563fb59c426SYoshinobu Inoue hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); 564fb59c426SYoshinobu Inoue else 565fb59c426SYoshinobu Inoue #endif 566df8bae1dSRodney W. Grimes hdrlen = sizeof (struct tcpiphdr); 567df8bae1dSRodney W. Grimes if (flags & TH_SYN) { 568df8bae1dSRodney W. Grimes tp->snd_nxt = tp->iss; 569df8bae1dSRodney W. Grimes if ((tp->t_flags & TF_NOOPT) == 0) { 570df8bae1dSRodney W. Grimes u_short mss; 571df8bae1dSRodney W. Grimes 572df8bae1dSRodney W. Grimes opt[0] = TCPOPT_MAXSEG; 573a0292f23SGarrett Wollman opt[1] = TCPOLEN_MAXSEG; 57497d8d152SAndre Oppermann mss = htons((u_short) tcp_mssopt(&tp->t_inpcb->inp_inc)); 57594a5d9b6SDavid Greenman (void)memcpy(opt + 2, &mss, sizeof(mss)); 576a0292f23SGarrett Wollman optlen = TCPOLEN_MAXSEG; 577df8bae1dSRodney W. Grimes 578df8bae1dSRodney W. Grimes if ((tp->t_flags & TF_REQ_SCALE) && 579df8bae1dSRodney W. Grimes ((flags & TH_ACK) == 0 || 580df8bae1dSRodney W. Grimes (tp->t_flags & TF_RCVD_SCALE))) { 5819105bb46SBruce Evans *((u_int32_t *)(opt + optlen)) = htonl( 582df8bae1dSRodney W. Grimes TCPOPT_NOP << 24 | 583df8bae1dSRodney W. Grimes TCPOPT_WINDOW << 16 | 584df8bae1dSRodney W. Grimes TCPOLEN_WINDOW << 8 | 585df8bae1dSRodney W. Grimes tp->request_r_scale); 586df8bae1dSRodney W. Grimes optlen += 4; 587df8bae1dSRodney W. Grimes } 588df8bae1dSRodney W. Grimes } 589df8bae1dSRodney W. Grimes } 590df8bae1dSRodney W. Grimes 591df8bae1dSRodney W. Grimes /* 592df8bae1dSRodney W. Grimes * Send a timestamp and echo-reply if this is a SYN and our side 593df8bae1dSRodney W. Grimes * wants to use timestamps (TF_REQ_TSTMP is set) or both our side 594df8bae1dSRodney W. Grimes * and our peer have sent timestamps in our SYN's. 595df8bae1dSRodney W. Grimes */ 596df8bae1dSRodney W. Grimes if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && 597df8bae1dSRodney W. Grimes (flags & TH_RST) == 0 && 598a0292f23SGarrett Wollman ((flags & TH_ACK) == 0 || 599df8bae1dSRodney W. Grimes (tp->t_flags & TF_RCVD_TSTMP))) { 6009105bb46SBruce Evans u_int32_t *lp = (u_int32_t *)(opt + optlen); 601df8bae1dSRodney W. Grimes 602df8bae1dSRodney W. Grimes /* Form timestamp option as shown in appendix A of RFC 1323. */ 603df8bae1dSRodney W. Grimes *lp++ = htonl(TCPOPT_TSTAMP_HDR); 604bf6d304aSAndre Oppermann *lp++ = htonl(ticks + tp->ts_offset); 605df8bae1dSRodney W. Grimes *lp = htonl(tp->ts_recent); 606df8bae1dSRodney W. Grimes optlen += TCPOLEN_TSTAMP_APPA; 607df8bae1dSRodney W. Grimes } 608df8bae1dSRodney W. Grimes 6091cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 6101cfd4b53SBruce M Simpson #ifdef INET6 6111cfd4b53SBruce M Simpson if (!isipv6) 6121cfd4b53SBruce M Simpson #endif 6131cfd4b53SBruce M Simpson if (tp->t_flags & TF_SIGNATURE) { 6141cfd4b53SBruce M Simpson int i; 6151cfd4b53SBruce M Simpson u_char *bp; 616bca0e5bfSBruce M Simpson 617bca0e5bfSBruce M Simpson /* Initialize TCP-MD5 option (RFC2385) */ 6181cfd4b53SBruce M Simpson bp = (u_char *)opt + optlen; 6191cfd4b53SBruce M Simpson *bp++ = TCPOPT_SIGNATURE; 6201cfd4b53SBruce M Simpson *bp++ = TCPOLEN_SIGNATURE; 6211cfd4b53SBruce M Simpson sigoff = optlen + 2; 6221cfd4b53SBruce M Simpson for (i = 0; i < TCP_SIGLEN; i++) 6231cfd4b53SBruce M Simpson *bp++ = 0; 6241cfd4b53SBruce M Simpson optlen += TCPOLEN_SIGNATURE; 6251cfd4b53SBruce M Simpson } 6261cfd4b53SBruce M Simpson #endif /* TCP_SIGNATURE */ 6271cfd4b53SBruce M Simpson 628be3f3b5eSPaul Saab if (tp->sack_enable && ((tp->t_flags & TF_NOOPT) == 0)) { 629be3f3b5eSPaul Saab /* 630be3f3b5eSPaul Saab * Tack on the SACK permitted option *last*. 631be3f3b5eSPaul Saab * And do padding of options after tacking this on. 632be3f3b5eSPaul Saab * This is because of MSS, TS, WinScale and Signatures are 633be3f3b5eSPaul Saab * all present, we have just 2 bytes left for the SACK 634be3f3b5eSPaul Saab * permitted option, which is just enough. 635be3f3b5eSPaul Saab */ 636be3f3b5eSPaul Saab /* 637be3f3b5eSPaul Saab * If this is the first SYN of connection (not a SYN 638be3f3b5eSPaul Saab * ACK), include SACK permitted option. If this is a 639be3f3b5eSPaul Saab * SYN ACK, include SACK permitted option if peer has 640be3f3b5eSPaul Saab * already done so. This is only for active connect, 641be3f3b5eSPaul Saab * since the syncache takes care of the passive connect. 642be3f3b5eSPaul Saab */ 643be3f3b5eSPaul Saab if ((flags & TH_SYN) && 644be3f3b5eSPaul Saab (!(flags & TH_ACK) || (tp->t_flags & TF_SACK_PERMIT))) { 645be3f3b5eSPaul Saab u_char *bp; 646be3f3b5eSPaul Saab bp = (u_char *)opt + optlen; 647be3f3b5eSPaul Saab 648be3f3b5eSPaul Saab *bp++ = TCPOPT_SACK_PERMITTED; 649be3f3b5eSPaul Saab *bp++ = TCPOLEN_SACK_PERMITTED; 650be3f3b5eSPaul Saab optlen += TCPOLEN_SACK_PERMITTED; 651be3f3b5eSPaul Saab } 652be3f3b5eSPaul Saab 653be3f3b5eSPaul Saab /* 654be3f3b5eSPaul Saab * Send SACKs if necessary. This should be the last 655be3f3b5eSPaul Saab * option processed. Only as many SACKs are sent as 656be3f3b5eSPaul Saab * are permitted by the maximum options size. 657be3f3b5eSPaul Saab * 658be3f3b5eSPaul Saab * In general, SACK blocks consume 8*n+2 bytes. 659be3f3b5eSPaul Saab * So a full size SACK blocks option is 34 bytes 660be3f3b5eSPaul Saab * (to generate 4 SACK blocks). At a minimum, 661be3f3b5eSPaul Saab * we need 10 bytes (to generate 1 SACK block). 662be3f3b5eSPaul Saab * If TCP Timestamps (12 bytes) and TCP Signatures 663be3f3b5eSPaul Saab * (18 bytes) are both present, we'll just have 664be3f3b5eSPaul Saab * 10 bytes for SACK options 40 - (12 + 18). 665be3f3b5eSPaul Saab */ 666be3f3b5eSPaul Saab if (TCPS_HAVEESTABLISHED(tp->t_state) && 667be3f3b5eSPaul Saab (tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0 && 668be3f3b5eSPaul Saab MAX_TCPOPTLEN - optlen - 2 >= TCPOLEN_SACK) { 669be3f3b5eSPaul Saab int nsack, sackoptlen, padlen; 670be3f3b5eSPaul Saab u_char *bp = (u_char *)opt + optlen; 671be3f3b5eSPaul Saab u_int32_t *lp; 672be3f3b5eSPaul Saab 673be3f3b5eSPaul Saab nsack = (MAX_TCPOPTLEN - optlen - 2) / TCPOLEN_SACK; 674be3f3b5eSPaul Saab nsack = min(nsack, tp->rcv_numsacks); 675be3f3b5eSPaul Saab sackoptlen = (2 + nsack * TCPOLEN_SACK); 676be3f3b5eSPaul Saab 677be3f3b5eSPaul Saab /* 678be3f3b5eSPaul Saab * First we need to pad options so that the 679be3f3b5eSPaul Saab * SACK blocks can start at a 4-byte boundary 680be3f3b5eSPaul Saab * (sack option and length are at a 2 byte offset). 681be3f3b5eSPaul Saab */ 682be3f3b5eSPaul Saab padlen = (MAX_TCPOPTLEN - optlen - sackoptlen) % 4; 683be3f3b5eSPaul Saab optlen += padlen; 684be3f3b5eSPaul Saab while (padlen-- > 0) 685be3f3b5eSPaul Saab *bp++ = TCPOPT_NOP; 686be3f3b5eSPaul Saab 687be3f3b5eSPaul Saab tcpstat.tcps_sack_send_blocks++; 688be3f3b5eSPaul Saab *bp++ = TCPOPT_SACK; 689be3f3b5eSPaul Saab *bp++ = sackoptlen; 690be3f3b5eSPaul Saab lp = (u_int32_t *)bp; 691be3f3b5eSPaul Saab for (i = 0; i < nsack; i++) { 692be3f3b5eSPaul Saab struct sackblk sack = tp->sackblks[i]; 693be3f3b5eSPaul Saab *lp++ = htonl(sack.start); 694be3f3b5eSPaul Saab *lp++ = htonl(sack.end); 695be3f3b5eSPaul Saab } 696be3f3b5eSPaul Saab optlen += sackoptlen; 697be3f3b5eSPaul Saab } 698be3f3b5eSPaul Saab } 699be3f3b5eSPaul Saab 700be3f3b5eSPaul Saab /* Pad TCP options to a 4 byte boundary */ 701be3f3b5eSPaul Saab if (optlen < MAX_TCPOPTLEN && (optlen % sizeof(u_int32_t))) { 702be3f3b5eSPaul Saab int pad = sizeof(u_int32_t) - (optlen % sizeof(u_int32_t)); 703be3f3b5eSPaul Saab u_char *bp = (u_char *)opt + optlen; 704be3f3b5eSPaul Saab 705be3f3b5eSPaul Saab optlen += pad; 706be3f3b5eSPaul Saab while (pad) { 707be3f3b5eSPaul Saab *bp++ = TCPOPT_EOL; 708be3f3b5eSPaul Saab pad--; 709be3f3b5eSPaul Saab } 710be3f3b5eSPaul Saab } 711be3f3b5eSPaul Saab 712df8bae1dSRodney W. Grimes hdrlen += optlen; 713df8bae1dSRodney W. Grimes 714fb59c426SYoshinobu Inoue #ifdef INET6 715fb59c426SYoshinobu Inoue if (isipv6) 716fb59c426SYoshinobu Inoue ipoptlen = ip6_optlen(tp->t_inpcb); 717fb59c426SYoshinobu Inoue else 718fb59c426SYoshinobu Inoue #endif 719f10e85d7SLuigi Rizzo if (tp->t_inpcb->inp_options) 720a04884fcSBill Fenner ipoptlen = tp->t_inpcb->inp_options->m_len - 721a04884fcSBill Fenner offsetof(struct ipoption, ipopt_list); 722f10e85d7SLuigi Rizzo else 723a04884fcSBill Fenner ipoptlen = 0; 724fb59c426SYoshinobu Inoue #ifdef IPSEC 725fb59c426SYoshinobu Inoue ipoptlen += ipsec_hdrsiz_tcp(tp); 726fb59c426SYoshinobu Inoue #endif 727a04884fcSBill Fenner 728df8bae1dSRodney W. Grimes /* 729df8bae1dSRodney W. Grimes * Adjust data length if insertion of options will 730a0292f23SGarrett Wollman * bump the packet length beyond the t_maxopd length. 731a0292f23SGarrett Wollman * Clear the FIN bit because we cut off the tail of 732a0292f23SGarrett Wollman * the segment. 733b3c0f300SAndre Oppermann * 73431ecb34aSAndre Oppermann * When doing TSO limit a burst to TCP_MAXWIN minus the 73531ecb34aSAndre Oppermann * IP, TCP and Options length to keep ip->ip_len from 73631ecb34aSAndre Oppermann * overflowing. Prevent the last segment from being 73731ecb34aSAndre Oppermann * fractional thus making them all equal sized and set 73831ecb34aSAndre Oppermann * the flag to continue sending. 739df8bae1dSRodney W. Grimes */ 740a04884fcSBill Fenner if (len + optlen + ipoptlen > tp->t_maxopd) { 741297a37f3SDavid Greenman flags &= ~TH_FIN; 742b3c0f300SAndre Oppermann if (tso) { 7436a2257d9SAndre Oppermann if (len > TCP_MAXWIN - hdrlen) { 74431ecb34aSAndre Oppermann len = TCP_MAXWIN - hdrlen; 74531ecb34aSAndre Oppermann len = len - (len % (tp->t_maxopd - optlen)); 746b3c0f300SAndre Oppermann sendalot = 1; 747b3c0f300SAndre Oppermann } else if (tp->t_flags & TF_NEEDFIN) 748b3c0f300SAndre Oppermann sendalot = 1; 749b3c0f300SAndre Oppermann } else { 750a04884fcSBill Fenner len = tp->t_maxopd - optlen - ipoptlen; 751df8bae1dSRodney W. Grimes sendalot = 1; 752df8bae1dSRodney W. Grimes } 753b3c0f300SAndre Oppermann } 754df8bae1dSRodney W. Grimes 755a0292f23SGarrett Wollman /*#ifdef DIAGNOSTIC*/ 756a683a7ddSYoshinobu Inoue #ifdef INET6 757a683a7ddSYoshinobu Inoue if (max_linkhdr + hdrlen > MCLBYTES) 758a683a7ddSYoshinobu Inoue #else 759df8bae1dSRodney W. Grimes if (max_linkhdr + hdrlen > MHLEN) 760a683a7ddSYoshinobu Inoue #endif 761f10e85d7SLuigi Rizzo panic("tcphdr too big"); 762a0292f23SGarrett Wollman /*#endif*/ 763df8bae1dSRodney W. Grimes 764df8bae1dSRodney W. Grimes /* 765df8bae1dSRodney W. Grimes * Grab a header mbuf, attaching a copy of data to 766df8bae1dSRodney W. Grimes * be transmitted, and initialize the header from 767df8bae1dSRodney W. Grimes * the template for sends on this connection. 768df8bae1dSRodney W. Grimes */ 769df8bae1dSRodney W. Grimes if (len) { 7702cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) && len == 1) 771df8bae1dSRodney W. Grimes tcpstat.tcps_sndprobe++; 772df8bae1dSRodney W. Grimes else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { 773df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitpack++; 774df8bae1dSRodney W. Grimes tcpstat.tcps_sndrexmitbyte += len; 775df8bae1dSRodney W. Grimes } else { 776df8bae1dSRodney W. Grimes tcpstat.tcps_sndpack++; 777df8bae1dSRodney W. Grimes tcpstat.tcps_sndbyte += len; 778df8bae1dSRodney W. Grimes } 779df8bae1dSRodney W. Grimes #ifdef notyet 780df8bae1dSRodney W. Grimes if ((m = m_copypack(so->so_snd.sb_mb, off, 781df8bae1dSRodney W. Grimes (int)len, max_linkhdr + hdrlen)) == 0) { 782cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 783df8bae1dSRodney W. Grimes error = ENOBUFS; 784df8bae1dSRodney W. Grimes goto out; 785df8bae1dSRodney W. Grimes } 786df8bae1dSRodney W. Grimes /* 787df8bae1dSRodney W. Grimes * m_copypack left space for our hdr; use it. 788df8bae1dSRodney W. Grimes */ 789df8bae1dSRodney W. Grimes m->m_len += hdrlen; 790df8bae1dSRodney W. Grimes m->m_data -= hdrlen; 791df8bae1dSRodney W. Grimes #else 79234333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 793df8bae1dSRodney W. Grimes if (m == NULL) { 794cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 795df8bae1dSRodney W. Grimes error = ENOBUFS; 796df8bae1dSRodney W. Grimes goto out; 797df8bae1dSRodney W. Grimes } 798fb59c426SYoshinobu Inoue #ifdef INET6 799a683a7ddSYoshinobu Inoue if (MHLEN < hdrlen + max_linkhdr) { 800a163d034SWarner Losh MCLGET(m, M_DONTWAIT); 801a683a7ddSYoshinobu Inoue if ((m->m_flags & M_EXT) == 0) { 802cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 803a683a7ddSYoshinobu Inoue m_freem(m); 804a683a7ddSYoshinobu Inoue error = ENOBUFS; 805a683a7ddSYoshinobu Inoue goto out; 806a683a7ddSYoshinobu Inoue } 807a683a7ddSYoshinobu Inoue } 808fb59c426SYoshinobu Inoue #endif 809df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 810df8bae1dSRodney W. Grimes m->m_len = hdrlen; 811df8bae1dSRodney W. Grimes if (len <= MHLEN - hdrlen - max_linkhdr) { 812df8bae1dSRodney W. Grimes m_copydata(so->so_snd.sb_mb, off, (int) len, 813df8bae1dSRodney W. Grimes mtod(m, caddr_t) + hdrlen); 814df8bae1dSRodney W. Grimes m->m_len += len; 815df8bae1dSRodney W. Grimes } else { 816df8bae1dSRodney W. Grimes m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 81751823c3aSGarrett Wollman if (m->m_next == 0) { 818cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 819d7f570e6SGarrett Wollman (void) m_free(m); 82051823c3aSGarrett Wollman error = ENOBUFS; 82151823c3aSGarrett Wollman goto out; 82251823c3aSGarrett Wollman } 823df8bae1dSRodney W. Grimes } 824df8bae1dSRodney W. Grimes #endif 825df8bae1dSRodney W. Grimes /* 826df8bae1dSRodney W. Grimes * If we're sending everything we've got, set PUSH. 827df8bae1dSRodney W. Grimes * (This will keep happy those implementations which only 828df8bae1dSRodney W. Grimes * give data to the user when a buffer fills or 829df8bae1dSRodney W. Grimes * a PUSH comes in.) 830df8bae1dSRodney W. Grimes */ 831df8bae1dSRodney W. Grimes if (off + len == so->so_snd.sb_cc) 832df8bae1dSRodney W. Grimes flags |= TH_PUSH; 833cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 834df8bae1dSRodney W. Grimes } else { 835cf2942b6SRobert Watson SOCKBUF_UNLOCK(&so->so_snd); 836df8bae1dSRodney W. Grimes if (tp->t_flags & TF_ACKNOW) 837df8bae1dSRodney W. Grimes tcpstat.tcps_sndacks++; 838df8bae1dSRodney W. Grimes else if (flags & (TH_SYN|TH_FIN|TH_RST)) 839df8bae1dSRodney W. Grimes tcpstat.tcps_sndctrl++; 840df8bae1dSRodney W. Grimes else if (SEQ_GT(tp->snd_up, tp->snd_una)) 841df8bae1dSRodney W. Grimes tcpstat.tcps_sndurg++; 842df8bae1dSRodney W. Grimes else 843df8bae1dSRodney W. Grimes tcpstat.tcps_sndwinup++; 844df8bae1dSRodney W. Grimes 84534333b16SAndre Oppermann MGETHDR(m, M_DONTWAIT, MT_DATA); 846df8bae1dSRodney W. Grimes if (m == NULL) { 847df8bae1dSRodney W. Grimes error = ENOBUFS; 848df8bae1dSRodney W. Grimes goto out; 849df8bae1dSRodney W. Grimes } 850fb59c426SYoshinobu Inoue #ifdef INET6 851fb59c426SYoshinobu Inoue if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && 852fb59c426SYoshinobu Inoue MHLEN >= hdrlen) { 853fb59c426SYoshinobu Inoue MH_ALIGN(m, hdrlen); 854fb59c426SYoshinobu Inoue } else 855fb59c426SYoshinobu Inoue #endif 856df8bae1dSRodney W. Grimes m->m_data += max_linkhdr; 857df8bae1dSRodney W. Grimes m->m_len = hdrlen; 858df8bae1dSRodney W. Grimes } 859cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); 860df8bae1dSRodney W. Grimes m->m_pkthdr.rcvif = (struct ifnet *)0; 861c488362eSRobert Watson #ifdef MAC 862c18b97c6SRobert Watson mac_create_mbuf_from_inpcb(tp->t_inpcb, m); 863c488362eSRobert Watson #endif 864fb59c426SYoshinobu Inoue #ifdef INET6 865fb59c426SYoshinobu Inoue if (isipv6) { 866fb59c426SYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 867fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip6 + 1); 86879909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip6, th); 869fb59c426SYoshinobu Inoue } else 870fb59c426SYoshinobu Inoue #endif /* INET6 */ 871fb59c426SYoshinobu Inoue { 872fb59c426SYoshinobu Inoue ip = mtod(m, struct ip *); 873fb59c426SYoshinobu Inoue ipov = (struct ipovly *)ip; 874fb59c426SYoshinobu Inoue th = (struct tcphdr *)(ip + 1); 87579909384SJonathan Lemon tcpip_fillheaders(tp->t_inpcb, ip, th); 876fb59c426SYoshinobu Inoue } 877df8bae1dSRodney W. Grimes 878df8bae1dSRodney W. Grimes /* 879df8bae1dSRodney W. Grimes * Fill in fields, remembering maximum advertised 880df8bae1dSRodney W. Grimes * window for use in delaying messages about window sizes. 881df8bae1dSRodney W. Grimes * If resending a FIN, be sure not to use a new sequence number. 882df8bae1dSRodney W. Grimes */ 883df8bae1dSRodney W. Grimes if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && 884df8bae1dSRodney W. Grimes tp->snd_nxt == tp->snd_max) 885df8bae1dSRodney W. Grimes tp->snd_nxt--; 886df8bae1dSRodney W. Grimes /* 887df8bae1dSRodney W. Grimes * If we are doing retransmissions, then snd_nxt will 888df8bae1dSRodney W. Grimes * not reflect the first unsent octet. For ACK only 889df8bae1dSRodney W. Grimes * packets, we do not want the sequence number of the 890df8bae1dSRodney W. Grimes * retransmitted packet, we want the sequence number 891df8bae1dSRodney W. Grimes * of the next unsent octet. So, if there is no data 892df8bae1dSRodney W. Grimes * (and no SYN or FIN), use snd_max instead of snd_nxt 893df8bae1dSRodney W. Grimes * when filling in ti_seq. But if we are in persist 894df8bae1dSRodney W. Grimes * state, snd_max might reflect one byte beyond the 895df8bae1dSRodney W. Grimes * right edge of the window, so use snd_nxt in that 896df8bae1dSRodney W. Grimes * case, since we know we aren't doing a retransmission. 897df8bae1dSRodney W. Grimes * (retransmit and persist are mutually exclusive...) 898df8bae1dSRodney W. Grimes */ 899a55db2b6SPaul Saab if (sack_rxmit == 0) { 9009b8b58e0SJonathan Lemon if (len || (flags & (TH_SYN|TH_FIN)) 9019b8b58e0SJonathan Lemon || callout_active(tp->tt_persist)) 902fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_nxt); 903df8bae1dSRodney W. Grimes else 904fb59c426SYoshinobu Inoue th->th_seq = htonl(tp->snd_max); 905a55db2b6SPaul Saab } else { 9066d90faf3SPaul Saab th->th_seq = htonl(p->rxmit); 9076d90faf3SPaul Saab p->rxmit += len; 9080077b016SPaul Saab tp->sackhint.sack_bytes_rexmit += len; 9096d90faf3SPaul Saab } 910fb59c426SYoshinobu Inoue th->th_ack = htonl(tp->rcv_nxt); 911df8bae1dSRodney W. Grimes if (optlen) { 912fb59c426SYoshinobu Inoue bcopy(opt, th + 1, optlen); 913fb59c426SYoshinobu Inoue th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; 914df8bae1dSRodney W. Grimes } 915fb59c426SYoshinobu Inoue th->th_flags = flags; 916df8bae1dSRodney W. Grimes /* 917df8bae1dSRodney W. Grimes * Calculate receive window. Don't shrink window, 918df8bae1dSRodney W. Grimes * but avoid silly window syndrome. 919df8bae1dSRodney W. Grimes */ 920201d185bSAndre Oppermann if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && 921201d185bSAndre Oppermann recwin < (long)tp->t_maxseg) 922201d185bSAndre Oppermann recwin = 0; 923201d185bSAndre Oppermann if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt)) 924201d185bSAndre Oppermann recwin = (long)(tp->rcv_adv - tp->rcv_nxt); 925201d185bSAndre Oppermann if (recwin > (long)TCP_MAXWIN << tp->rcv_scale) 926201d185bSAndre Oppermann recwin = (long)TCP_MAXWIN << tp->rcv_scale; 927201d185bSAndre Oppermann th->th_win = htons((u_short) (recwin >> tp->rcv_scale)); 928262c1c1aSMatthew Dillon 929262c1c1aSMatthew Dillon 930262c1c1aSMatthew Dillon /* 931262c1c1aSMatthew Dillon * Adjust the RXWIN0SENT flag - indicate that we have advertised 932262c1c1aSMatthew Dillon * a 0 window. This may cause the remote transmitter to stall. This 933262c1c1aSMatthew Dillon * flag tells soreceive() to disable delayed acknowledgements when 934262c1c1aSMatthew Dillon * draining the buffer. This can occur if the receiver is attempting 935262c1c1aSMatthew Dillon * to read more data then can be buffered prior to transmitting on 936262c1c1aSMatthew Dillon * the connection. 937262c1c1aSMatthew Dillon */ 938201d185bSAndre Oppermann if (recwin == 0) 939262c1c1aSMatthew Dillon tp->t_flags |= TF_RXWIN0SENT; 940262c1c1aSMatthew Dillon else 941262c1c1aSMatthew Dillon tp->t_flags &= ~TF_RXWIN0SENT; 942df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { 943fb59c426SYoshinobu Inoue th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); 944fb59c426SYoshinobu Inoue th->th_flags |= TH_URG; 945df8bae1dSRodney W. Grimes } else 946df8bae1dSRodney W. Grimes /* 947df8bae1dSRodney W. Grimes * If no urgent pointer to send, then we pull 948df8bae1dSRodney W. Grimes * the urgent pointer to the left edge of the send window 949df8bae1dSRodney W. Grimes * so that it doesn't drift into the send window on sequence 950df8bae1dSRodney W. Grimes * number wraparound. 951df8bae1dSRodney W. Grimes */ 952df8bae1dSRodney W. Grimes tp->snd_up = tp->snd_una; /* drag it along */ 953df8bae1dSRodney W. Grimes 9541cfd4b53SBruce M Simpson #ifdef TCP_SIGNATURE 9551cfd4b53SBruce M Simpson #ifdef INET6 9561cfd4b53SBruce M Simpson if (!isipv6) 9571cfd4b53SBruce M Simpson #endif 9581cfd4b53SBruce M Simpson if (tp->t_flags & TF_SIGNATURE) 959265ed012SBruce M Simpson tcp_signature_compute(m, sizeof(struct ip), len, optlen, 9601cfd4b53SBruce M Simpson (u_char *)(th + 1) + sigoff, IPSEC_DIR_OUTBOUND); 961265ed012SBruce M Simpson #endif 9621cfd4b53SBruce M Simpson 963df8bae1dSRodney W. Grimes /* 964df8bae1dSRodney W. Grimes * Put TCP length in extended header, and then 965df8bae1dSRodney W. Grimes * checksum extended header and data. 966df8bae1dSRodney W. Grimes */ 967fb59c426SYoshinobu Inoue m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ 968fb59c426SYoshinobu Inoue #ifdef INET6 969fb59c426SYoshinobu Inoue if (isipv6) 970fb59c426SYoshinobu Inoue /* 971fb59c426SYoshinobu Inoue * ip6_plen is not need to be filled now, and will be filled 972fb59c426SYoshinobu Inoue * in ip6_output. 973fb59c426SYoshinobu Inoue */ 974fb59c426SYoshinobu Inoue th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), 975fb59c426SYoshinobu Inoue sizeof(struct tcphdr) + optlen + len); 976fb59c426SYoshinobu Inoue else 977fb59c426SYoshinobu Inoue #endif /* INET6 */ 978fb59c426SYoshinobu Inoue { 979db4f9cc7SJonathan Lemon m->m_pkthdr.csum_flags = CSUM_TCP; 980db4f9cc7SJonathan Lemon m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); 98179909384SJonathan Lemon th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 98279909384SJonathan Lemon htons(sizeof(struct tcphdr) + IPPROTO_TCP + len + optlen)); 983fb59c426SYoshinobu Inoue 984db4f9cc7SJonathan Lemon /* IP version must be set here for ipv4/ipv6 checking later */ 985db4f9cc7SJonathan Lemon KASSERT(ip->ip_v == IPVERSION, 9866e551fb6SDavid E. O'Brien ("%s: IP version incorrect: %d", __func__, ip->ip_v)); 987fb59c426SYoshinobu Inoue } 988df8bae1dSRodney W. Grimes 989df8bae1dSRodney W. Grimes /* 990b3c0f300SAndre Oppermann * Enable TSO and specify the size of the segments. 991b3c0f300SAndre Oppermann * The TCP pseudo header checksum is always provided. 992b3c0f300SAndre Oppermann * XXX: Fixme: This is currently not the case for IPv6. 993b3c0f300SAndre Oppermann */ 994b3c0f300SAndre Oppermann if (tso) { 995b3c0f300SAndre Oppermann m->m_pkthdr.csum_flags = CSUM_TSO; 996b3c0f300SAndre Oppermann m->m_pkthdr.tso_segsz = tp->t_maxopd - optlen; 997b3c0f300SAndre Oppermann } 998b3c0f300SAndre Oppermann 999b3c0f300SAndre Oppermann /* 1000df8bae1dSRodney W. Grimes * In transmit state, time the transmission and arrange for 1001df8bae1dSRodney W. Grimes * the retransmit. In persist state, just set snd_max. 1002df8bae1dSRodney W. Grimes */ 10032cdbfa66SPaul Saab if ((tp->t_flags & TF_FORCEDATA) == 0 || 10042cdbfa66SPaul Saab !callout_active(tp->tt_persist)) { 1005df8bae1dSRodney W. Grimes tcp_seq startseq = tp->snd_nxt; 1006df8bae1dSRodney W. Grimes 1007df8bae1dSRodney W. Grimes /* 1008df8bae1dSRodney W. Grimes * Advance snd_nxt over sequence space of this segment. 1009df8bae1dSRodney W. Grimes */ 1010df8bae1dSRodney W. Grimes if (flags & (TH_SYN|TH_FIN)) { 1011df8bae1dSRodney W. Grimes if (flags & TH_SYN) 1012df8bae1dSRodney W. Grimes tp->snd_nxt++; 1013df8bae1dSRodney W. Grimes if (flags & TH_FIN) { 1014df8bae1dSRodney W. Grimes tp->snd_nxt++; 1015df8bae1dSRodney W. Grimes tp->t_flags |= TF_SENTFIN; 1016df8bae1dSRodney W. Grimes } 1017df8bae1dSRodney W. Grimes } 1018a55db2b6SPaul Saab if (sack_rxmit) 10196d90faf3SPaul Saab goto timer; 1020df8bae1dSRodney W. Grimes tp->snd_nxt += len; 1021df8bae1dSRodney W. Grimes if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { 1022df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt; 1023df8bae1dSRodney W. Grimes /* 1024df8bae1dSRodney W. Grimes * Time this transmission if not a retransmission and 1025df8bae1dSRodney W. Grimes * not currently timing anything. 1026df8bae1dSRodney W. Grimes */ 10279b8b58e0SJonathan Lemon if (tp->t_rtttime == 0) { 10289b8b58e0SJonathan Lemon tp->t_rtttime = ticks; 1029df8bae1dSRodney W. Grimes tp->t_rtseq = startseq; 1030df8bae1dSRodney W. Grimes tcpstat.tcps_segstimed++; 1031df8bae1dSRodney W. Grimes } 1032df8bae1dSRodney W. Grimes } 1033df8bae1dSRodney W. Grimes 1034df8bae1dSRodney W. Grimes /* 1035df8bae1dSRodney W. Grimes * Set retransmit timer if not currently set, 103628257b5cSMatthew Dillon * and not doing a pure ack or a keep-alive probe. 1037df8bae1dSRodney W. Grimes * Initial value for retransmit timer is smoothed 1038df8bae1dSRodney W. Grimes * round-trip time + 2 * round-trip time variance. 1039df8bae1dSRodney W. Grimes * Initialize shift counter which is used for backoff 1040df8bae1dSRodney W. Grimes * of retransmit time. 1041df8bae1dSRodney W. Grimes */ 10426d90faf3SPaul Saab timer: 10439b8b58e0SJonathan Lemon if (!callout_active(tp->tt_rexmt) && 1044a55db2b6SPaul Saab ((sack_rxmit && tp->snd_nxt != tp->snd_max) || 1045a55db2b6SPaul Saab (tp->snd_nxt != tp->snd_una))) { 10469b8b58e0SJonathan Lemon if (callout_active(tp->tt_persist)) { 10479b8b58e0SJonathan Lemon callout_stop(tp->tt_persist); 1048df8bae1dSRodney W. Grimes tp->t_rxtshift = 0; 1049df8bae1dSRodney W. Grimes } 105046f58482SJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 105146f58482SJonathan Lemon tcp_timer_rexmt, tp); 1052df8bae1dSRodney W. Grimes } 105328257b5cSMatthew Dillon } else { 105428257b5cSMatthew Dillon /* 105528257b5cSMatthew Dillon * Persist case, update snd_max but since we are in 105628257b5cSMatthew Dillon * persist mode (no window) we do not update snd_nxt. 105728257b5cSMatthew Dillon */ 105828257b5cSMatthew Dillon int xlen = len; 105928257b5cSMatthew Dillon if (flags & TH_SYN) 106028257b5cSMatthew Dillon ++xlen; 106128257b5cSMatthew Dillon if (flags & TH_FIN) { 106228257b5cSMatthew Dillon ++xlen; 106328257b5cSMatthew Dillon tp->t_flags |= TF_SENTFIN; 106428257b5cSMatthew Dillon } 1065abac41a6SMatthew Dillon if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max)) 1066df8bae1dSRodney W. Grimes tp->snd_max = tp->snd_nxt + len; 106728257b5cSMatthew Dillon } 1068df8bae1dSRodney W. Grimes 1069610ee2f9SDavid Greenman #ifdef TCPDEBUG 1070df8bae1dSRodney W. Grimes /* 1071df8bae1dSRodney W. Grimes * Trace. 1072df8bae1dSRodney W. Grimes */ 107391f467d5SHartmut Brandt if (so->so_options & SO_DEBUG) { 1074f3e0b7efSBruce M Simpson u_short save = 0; 10755214cb3fSBruce M Simpson #ifdef INET6 10765214cb3fSBruce M Simpson if (!isipv6) 10775214cb3fSBruce M Simpson #endif 10785214cb3fSBruce M Simpson { 10795214cb3fSBruce M Simpson save = ipov->ih_len; 108091f467d5SHartmut Brandt ipov->ih_len = htons(m->m_pkthdr.len /* - hdrlen + (th->th_off << 2) */); 10815214cb3fSBruce M Simpson } 1082fb59c426SYoshinobu Inoue tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); 10835214cb3fSBruce M Simpson #ifdef INET6 10845214cb3fSBruce M Simpson if (!isipv6) 10855214cb3fSBruce M Simpson #endif 108691f467d5SHartmut Brandt ipov->ih_len = save; 108791f467d5SHartmut Brandt } 1088610ee2f9SDavid Greenman #endif 1089df8bae1dSRodney W. Grimes 1090df8bae1dSRodney W. Grimes /* 1091df8bae1dSRodney W. Grimes * Fill in IP length and desired time to live and 1092df8bae1dSRodney W. Grimes * send to IP level. There should be a better way 1093df8bae1dSRodney W. Grimes * to handle ttl and tos; we could keep them in 1094df8bae1dSRodney W. Grimes * the template, but need a way to checksum without them. 1095df8bae1dSRodney W. Grimes */ 1096fb59c426SYoshinobu Inoue /* 1097fb59c426SYoshinobu Inoue * m->m_pkthdr.len should have been set before cksum calcuration, 1098fb59c426SYoshinobu Inoue * because in6_cksum() need it. 1099fb59c426SYoshinobu Inoue */ 1100fb59c426SYoshinobu Inoue #ifdef INET6 1101fb59c426SYoshinobu Inoue if (isipv6) { 1102fb59c426SYoshinobu Inoue /* 1103fb59c426SYoshinobu Inoue * we separately set hoplimit for every segment, since the 1104fb59c426SYoshinobu Inoue * user might want to change the value via setsockopt. 1105fb59c426SYoshinobu Inoue * Also, desired default hop limit might be changed via 1106fb59c426SYoshinobu Inoue * Neighbor Discovery. 1107fb59c426SYoshinobu Inoue */ 110897d8d152SAndre Oppermann ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, NULL); 1109fb59c426SYoshinobu Inoue 1110fb59c426SYoshinobu Inoue /* TODO: IPv6 IP6TOS_ECT bit on */ 1111fb59c426SYoshinobu Inoue error = ip6_output(m, 111297d8d152SAndre Oppermann tp->t_inpcb->in6p_outputopts, NULL, 1113b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? 1114b5d47ff5SJohn-Mark Gurney IP_ROUTETOIF : 0), NULL, NULL, tp->t_inpcb); 1115fb59c426SYoshinobu Inoue } else 1116fb59c426SYoshinobu Inoue #endif /* INET6 */ 1117df8bae1dSRodney W. Grimes { 1118fb59c426SYoshinobu Inoue ip->ip_len = m->m_pkthdr.len; 1119686cdd19SJun-ichiro itojun Hagino #ifdef INET6 1120686cdd19SJun-ichiro itojun Hagino if (INP_CHECK_SOCKAF(so, AF_INET6)) 112197d8d152SAndre Oppermann ip->ip_ttl = in6_selecthlim(tp->t_inpcb, NULL); 1122686cdd19SJun-ichiro itojun Hagino #endif /* INET6 */ 1123f138387aSGarrett Wollman /* 112497d8d152SAndre Oppermann * If we do path MTU discovery, then we set DF on every packet. 112597d8d152SAndre Oppermann * This might not be the best thing to do according to RFC3390 112697d8d152SAndre Oppermann * Section 2. However the tcp hostcache migitates the problem 112797d8d152SAndre Oppermann * so it affects only the first tcp connection with a host. 1128f138387aSGarrett Wollman */ 112997d8d152SAndre Oppermann if (path_mtu_discovery) 1130fb59c426SYoshinobu Inoue ip->ip_off |= IP_DF; 113197d8d152SAndre Oppermann 113297d8d152SAndre Oppermann error = ip_output(m, tp->t_inpcb->inp_options, NULL, 1133b5d47ff5SJohn-Mark Gurney ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0), 0, 1134b5d47ff5SJohn-Mark Gurney tp->t_inpcb); 1135df8bae1dSRodney W. Grimes } 1136df8bae1dSRodney W. Grimes if (error) { 11377734ea06SArchie Cobbs 11387734ea06SArchie Cobbs /* 11397734ea06SArchie Cobbs * We know that the packet was lost, so back out the 11407734ea06SArchie Cobbs * sequence number advance, if any. 11412c30ec0aSAndre Oppermann * 11422c30ec0aSAndre Oppermann * If the error is EPERM the packet got blocked by the 11432c30ec0aSAndre Oppermann * local firewall. Normally we should terminate the 11442c30ec0aSAndre Oppermann * connection but the blocking may have been spurious 11452c30ec0aSAndre Oppermann * due to a firewall reconfiguration cycle. So we treat 11462c30ec0aSAndre Oppermann * it like a packet loss and let the retransmit timer and 11472c30ec0aSAndre Oppermann * timeouts do their work over time. 11482c30ec0aSAndre Oppermann * XXX: It is a POLA question whether calling tcp_drop right 11492c30ec0aSAndre Oppermann * away would be the really correct behavior instead. 11507734ea06SArchie Cobbs */ 11512c30ec0aSAndre Oppermann if (error != EPERM && ((tp->t_flags & TF_FORCEDATA) == 0 || 11522c30ec0aSAndre Oppermann !callout_active(tp->tt_persist))) { 11537734ea06SArchie Cobbs /* 11547734ea06SArchie Cobbs * No need to check for TH_FIN here because 11557734ea06SArchie Cobbs * the TF_SENTFIN flag handles that case. 11567734ea06SArchie Cobbs */ 11575d3b1b75SJayanth Vijayaraghavan if ((flags & TH_SYN) == 0) { 11580077b016SPaul Saab if (sack_rxmit) { 11595d3b1b75SJayanth Vijayaraghavan p->rxmit -= len; 11600077b016SPaul Saab tp->sackhint.sack_bytes_rexmit -= len; 11610077b016SPaul Saab KASSERT(tp->sackhint.sack_bytes_rexmit 11620077b016SPaul Saab >= 0, 11630077b016SPaul Saab ("sackhint bytes rtx >= 0")); 11640077b016SPaul Saab } else 11657734ea06SArchie Cobbs tp->snd_nxt -= len; 11667734ea06SArchie Cobbs } 11675d3b1b75SJayanth Vijayaraghavan } 11682c30ec0aSAndre Oppermann if (error == EPERM) { 11692c30ec0aSAndre Oppermann tp->t_softerror = error; 11702c30ec0aSAndre Oppermann return (error); 11712c30ec0aSAndre Oppermann } 11727734ea06SArchie Cobbs 1173df8bae1dSRodney W. Grimes out: 1174cf2942b6SRobert Watson SOCKBUF_UNLOCK_ASSERT(&so->so_snd); /* Check gotos. */ 1175df8bae1dSRodney W. Grimes if (error == ENOBUFS) { 117659f577adSJonathan Lemon if (!callout_active(tp->tt_rexmt) && 117759f577adSJonathan Lemon !callout_active(tp->tt_persist)) 117859f577adSJonathan Lemon callout_reset(tp->tt_rexmt, tp->t_rxtcur, 117959f577adSJonathan Lemon tcp_timer_rexmt, tp); 11801600372bSAndre Oppermann tp->snd_cwnd = tp->t_maxseg; 1181df8bae1dSRodney W. Grimes return (0); 1182df8bae1dSRodney W. Grimes } 11833d1f141bSGarrett Wollman if (error == EMSGSIZE) { 11843d1f141bSGarrett Wollman /* 1185b3c0f300SAndre Oppermann * For some reason the interface we used initially 1186b3c0f300SAndre Oppermann * to send segments changed to another or lowered 1187b3c0f300SAndre Oppermann * its MTU. 1188b3c0f300SAndre Oppermann * 1189b3c0f300SAndre Oppermann * tcp_mtudisc() will find out the new MTU and as 1190b3c0f300SAndre Oppermann * its last action, initiate retransmission, so it 1191b3c0f300SAndre Oppermann * is important to not do so here. 1192b3c0f300SAndre Oppermann * 1193b3c0f300SAndre Oppermann * If TSO was active we either got an interface 1194b3c0f300SAndre Oppermann * without TSO capabilits or TSO was turned off. 1195b3c0f300SAndre Oppermann * Disable it for this connection as too and 1196b3c0f300SAndre Oppermann * immediatly retry with MSS sized segments generated 1197b3c0f300SAndre Oppermann * by this function. 11983d1f141bSGarrett Wollman */ 1199b3c0f300SAndre Oppermann if (tso) 1200b3c0f300SAndre Oppermann tp->t_flags &= ~TF_TSO; 12013d1f141bSGarrett Wollman tcp_mtudisc(tp->t_inpcb, 0); 12023d1f141bSGarrett Wollman return 0; 12033d1f141bSGarrett Wollman } 1204df8bae1dSRodney W. Grimes if ((error == EHOSTUNREACH || error == ENETDOWN) 1205df8bae1dSRodney W. Grimes && TCPS_HAVERCVDSYN(tp->t_state)) { 1206df8bae1dSRodney W. Grimes tp->t_softerror = error; 1207df8bae1dSRodney W. Grimes return (0); 1208df8bae1dSRodney W. Grimes } 1209df8bae1dSRodney W. Grimes return (error); 1210df8bae1dSRodney W. Grimes } 1211df8bae1dSRodney W. Grimes tcpstat.tcps_sndtotal++; 1212df8bae1dSRodney W. Grimes 1213df8bae1dSRodney W. Grimes /* 1214df8bae1dSRodney W. Grimes * Data sent (as far as we can tell). 1215df8bae1dSRodney W. Grimes * If this advertises a larger window than any other segment, 1216df8bae1dSRodney W. Grimes * then remember the size of the advertised window. 1217df8bae1dSRodney W. Grimes * Any pending ACK has now been sent. 1218df8bae1dSRodney W. Grimes */ 1219201d185bSAndre Oppermann if (recwin > 0 && SEQ_GT(tp->rcv_nxt + recwin, tp->rcv_adv)) 1220201d185bSAndre Oppermann tp->rcv_adv = tp->rcv_nxt + recwin; 1221df8bae1dSRodney W. Grimes tp->last_ack_sent = tp->rcv_nxt; 12223bfd6421SJonathan Lemon tp->t_flags &= ~(TF_ACKNOW | TF_DELACK); 12233bfd6421SJonathan Lemon if (callout_active(tp->tt_delack)) 12249b8b58e0SJonathan Lemon callout_stop(tp->tt_delack); 1225d912c694SMatthew Dillon #if 0 1226d912c694SMatthew Dillon /* 1227d912c694SMatthew Dillon * This completely breaks TCP if newreno is turned on. What happens 1228d912c694SMatthew Dillon * is that if delayed-acks are turned on on the receiver, this code 1229d912c694SMatthew Dillon * on the transmitter effectively destroys the TCP window, forcing 1230d912c694SMatthew Dillon * it to four packets (1.5Kx4 = 6K window). 1231d912c694SMatthew Dillon */ 123246f58482SJonathan Lemon if (sendalot && (!tcp_do_newreno || --maxburst)) 1233df8bae1dSRodney W. Grimes goto again; 1234d912c694SMatthew Dillon #endif 1235d912c694SMatthew Dillon if (sendalot) 1236d912c694SMatthew Dillon goto again; 1237df8bae1dSRodney W. Grimes return (0); 1238df8bae1dSRodney W. Grimes } 1239df8bae1dSRodney W. Grimes 1240df8bae1dSRodney W. Grimes void 1241df8bae1dSRodney W. Grimes tcp_setpersist(tp) 1242df8bae1dSRodney W. Grimes register struct tcpcb *tp; 1243df8bae1dSRodney W. Grimes { 12449b8b58e0SJonathan Lemon int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; 12459b8b58e0SJonathan Lemon int tt; 1246df8bae1dSRodney W. Grimes 12479b8b58e0SJonathan Lemon if (callout_active(tp->tt_rexmt)) 12489b8b58e0SJonathan Lemon panic("tcp_setpersist: retransmit pending"); 1249df8bae1dSRodney W. Grimes /* 1250df8bae1dSRodney W. Grimes * Start/restart persistance timer. 1251df8bae1dSRodney W. Grimes */ 12529b8b58e0SJonathan Lemon TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift], 1253df8bae1dSRodney W. Grimes TCPTV_PERSMIN, TCPTV_PERSMAX); 12549b8b58e0SJonathan Lemon callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp); 1255df8bae1dSRodney W. Grimes if (tp->t_rxtshift < TCP_MAXRXTSHIFT) 1256df8bae1dSRodney W. Grimes tp->t_rxtshift++; 1257df8bae1dSRodney W. Grimes } 1258